2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 * vendor / preset table
47 struct hda_vendor_id
{
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids
[] = {
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
69 { 0x1aec, "Wolfson Microelectronics" },
70 { 0x434d, "C-Media" },
72 { 0x8384, "SigmaTel" },
76 static DEFINE_MUTEX(preset_mutex
);
77 static LIST_HEAD(hda_preset_tables
);
79 int snd_hda_add_codec_preset(struct hda_codec_preset_list
*preset
)
81 mutex_lock(&preset_mutex
);
82 list_add_tail(&preset
->list
, &hda_preset_tables
);
83 mutex_unlock(&preset_mutex
);
86 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset
);
88 int snd_hda_delete_codec_preset(struct hda_codec_preset_list
*preset
)
90 mutex_lock(&preset_mutex
);
91 list_del(&preset
->list
);
92 mutex_unlock(&preset_mutex
);
95 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset
);
98 #define codec_in_pm(codec) ((codec)->in_pm)
99 static void hda_power_work(struct work_struct
*work
);
100 static void hda_keep_power_on(struct hda_codec
*codec
);
101 #define hda_codec_is_power_on(codec) ((codec)->power_on)
102 static inline void hda_call_pm_notify(struct hda_bus
*bus
, bool power_up
)
104 if (bus
->ops
.pm_notify
)
105 bus
->ops
.pm_notify(bus
, power_up
);
108 #define codec_in_pm(codec) 0
109 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
110 #define hda_codec_is_power_on(codec) 1
111 #define hda_call_pm_notify(bus, state) {}
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
121 const char *snd_hda_get_jack_location(u32 cfg
)
123 static char *bases
[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
126 static unsigned char specials_idx
[] = {
131 static char *specials
[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
137 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
138 if ((cfg
& 0x0f) < 7)
139 return bases
[cfg
& 0x0f];
140 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
141 if (cfg
== specials_idx
[i
])
146 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location
);
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
155 const char *snd_hda_get_jack_connectivity(u32 cfg
)
157 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
159 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
161 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity
);
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
170 const char *snd_hda_get_jack_type(u32 cfg
)
172 static char *jack_types
[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digitial In", "Reserved", "Other"
179 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
180 >> AC_DEFCFG_DEVICE_SHIFT
];
182 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type
);
185 * Compose a 32bit command word to be sent to the HD-audio controller
187 static inline unsigned int
188 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
189 unsigned int verb
, unsigned int parm
)
193 if ((codec
->addr
& ~0xf) || (direct
& ~1) || (nid
& ~0x7f) ||
194 (verb
& ~0xfff) || (parm
& ~0xffff)) {
195 printk(KERN_ERR
"hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec
->addr
, direct
, nid
, verb
, parm
);
200 val
= (u32
)codec
->addr
<< 28;
201 val
|= (u32
)direct
<< 27;
202 val
|= (u32
)nid
<< 20;
209 * Send and receive a verb
211 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
214 struct hda_bus
*bus
= codec
->bus
;
223 snd_hda_power_up(codec
);
224 mutex_lock(&bus
->cmd_mutex
);
225 trace_hda_send_cmd(codec
, cmd
);
226 err
= bus
->ops
.command(bus
, cmd
);
228 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
229 trace_hda_get_response(codec
, *res
);
231 mutex_unlock(&bus
->cmd_mutex
);
232 snd_hda_power_down(codec
);
233 if (!codec_in_pm(codec
) && res
&& *res
== -1 && bus
->rirb_error
) {
234 if (bus
->response_reset
) {
235 snd_printd("hda_codec: resetting BUS due to "
236 "fatal communication error\n");
237 trace_hda_bus_reset(bus
);
238 bus
->ops
.bus_reset(bus
);
242 /* clear reset-flag when the communication gets recovered */
243 if (!err
|| codec_in_pm(codec
))
244 bus
->response_reset
= 0;
249 * snd_hda_codec_read - send a command and get the response
250 * @codec: the HDA codec
251 * @nid: NID to send the command
252 * @direct: direct flag
253 * @verb: the verb to send
254 * @parm: the parameter for the verb
256 * Send a single command and read the corresponding response.
258 * Returns the obtained response value, or -1 for an error.
260 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
262 unsigned int verb
, unsigned int parm
)
264 unsigned cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
266 if (codec_exec_verb(codec
, cmd
, &res
))
270 EXPORT_SYMBOL_HDA(snd_hda_codec_read
);
273 * snd_hda_codec_write - send a single command without waiting for response
274 * @codec: the HDA codec
275 * @nid: NID to send the command
276 * @direct: direct flag
277 * @verb: the verb to send
278 * @parm: the parameter for the verb
280 * Send a single command without waiting for response.
282 * Returns 0 if successful, or a negative error code.
284 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
285 unsigned int verb
, unsigned int parm
)
287 unsigned int cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
289 return codec_exec_verb(codec
, cmd
,
290 codec
->bus
->sync_write
? &res
: NULL
);
292 EXPORT_SYMBOL_HDA(snd_hda_codec_write
);
295 * snd_hda_sequence_write - sequence writes
296 * @codec: the HDA codec
297 * @seq: VERB array to send
299 * Send the commands sequentially from the given array.
300 * The array must be terminated with NID=0.
302 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
304 for (; seq
->nid
; seq
++)
305 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
307 EXPORT_SYMBOL_HDA(snd_hda_sequence_write
);
310 * snd_hda_get_sub_nodes - get the range of sub nodes
311 * @codec: the HDA codec
313 * @start_id: the pointer to store the start NID
315 * Parse the NID and store the start NID of its sub-nodes.
316 * Returns the number of sub-nodes.
318 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
323 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
326 *start_id
= (parm
>> 16) & 0x7fff;
327 return (int)(parm
& 0x7fff);
329 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
331 /* look up the cached results */
332 static hda_nid_t
*lookup_conn_list(struct snd_array
*array
, hda_nid_t nid
)
335 for (i
= 0; i
< array
->used
; ) {
336 hda_nid_t
*p
= snd_array_elem(array
, i
);
345 /* read the connection and add to the cache */
346 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
348 hda_nid_t list
[HDA_MAX_CONNECTIONS
];
351 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
354 return snd_hda_override_conn_list(codec
, nid
, len
, list
);
358 * snd_hda_get_connections - copy connection list
359 * @codec: the HDA codec
361 * @conn_list: connection list array; when NULL, checks only the size
362 * @max_conns: max. number of connections to store
364 * Parses the connection list of the given widget and stores the list
367 * Returns the number of connections, or a negative error code.
369 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
370 hda_nid_t
*conn_list
, int max_conns
)
372 struct snd_array
*array
= &codec
->conn_lists
;
378 mutex_lock(&codec
->hash_mutex
);
380 /* if the connection-list is already cached, read it */
381 p
= lookup_conn_list(array
, nid
);
384 if (conn_list
&& len
> max_conns
) {
385 snd_printk(KERN_ERR
"hda_codec: "
386 "Too many connections %d for NID 0x%x\n",
388 mutex_unlock(&codec
->hash_mutex
);
391 if (conn_list
&& len
)
392 memcpy(conn_list
, p
+ 2, len
* sizeof(hda_nid_t
));
394 mutex_unlock(&codec
->hash_mutex
);
397 if (snd_BUG_ON(added
))
400 len
= read_and_add_raw_conns(codec
, nid
);
406 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
409 * snd_hda_get_raw_connections - copy connection list without cache
410 * @codec: the HDA codec
412 * @conn_list: connection list array
413 * @max_conns: max. number of connections to store
415 * Like snd_hda_get_connections(), copy the connection list but without
416 * checking through the connection-list cache.
417 * Currently called only from hda_proc.c, so not exported.
419 int snd_hda_get_raw_connections(struct hda_codec
*codec
, hda_nid_t nid
,
420 hda_nid_t
*conn_list
, int max_conns
)
423 int i
, conn_len
, conns
;
424 unsigned int shift
, num_elems
, mask
;
428 if (snd_BUG_ON(!conn_list
|| max_conns
<= 0))
431 wcaps
= get_wcaps(codec
, nid
);
432 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
433 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
436 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
437 if (parm
& AC_CLIST_LONG
) {
446 conn_len
= parm
& AC_CLIST_LENGTH
;
447 mask
= (1 << (shift
-1)) - 1;
450 return 0; /* no connection */
453 /* single connection */
454 parm
= snd_hda_codec_read(codec
, nid
, 0,
455 AC_VERB_GET_CONNECT_LIST
, 0);
456 if (parm
== -1 && codec
->bus
->rirb_error
)
458 conn_list
[0] = parm
& mask
;
462 /* multi connection */
465 for (i
= 0; i
< conn_len
; i
++) {
469 if (i
% num_elems
== 0) {
470 parm
= snd_hda_codec_read(codec
, nid
, 0,
471 AC_VERB_GET_CONNECT_LIST
, i
);
472 if (parm
== -1 && codec
->bus
->rirb_error
)
475 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
478 snd_printk(KERN_WARNING
"hda_codec: "
479 "invalid CONNECT_LIST verb %x[%i]:%x\n",
485 /* ranges between the previous and this one */
486 if (!prev_nid
|| prev_nid
>= val
) {
487 snd_printk(KERN_WARNING
"hda_codec: "
488 "invalid dep_range_val %x:%x\n",
492 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
493 if (conns
>= max_conns
) {
494 snd_printk(KERN_ERR
"hda_codec: "
495 "Too many connections %d for NID 0x%x\n",
499 conn_list
[conns
++] = n
;
502 if (conns
>= max_conns
) {
503 snd_printk(KERN_ERR
"hda_codec: "
504 "Too many connections %d for NID 0x%x\n",
508 conn_list
[conns
++] = val
;
515 static bool add_conn_list(struct snd_array
*array
, hda_nid_t nid
)
517 hda_nid_t
*p
= snd_array_new(array
);
525 * snd_hda_override_conn_list - add/modify the connection-list to cache
526 * @codec: the HDA codec
528 * @len: number of connection list entries
529 * @list: the list of connection entries
531 * Add or modify the given connection-list to the cache. If the corresponding
532 * cache already exists, invalidate it and append a new one.
534 * Returns zero or a negative error code.
536 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
537 const hda_nid_t
*list
)
539 struct snd_array
*array
= &codec
->conn_lists
;
543 mutex_lock(&codec
->hash_mutex
);
544 p
= lookup_conn_list(array
, nid
);
546 *p
= -1; /* invalidate the old entry */
548 old_used
= array
->used
;
549 if (!add_conn_list(array
, nid
) || !add_conn_list(array
, len
))
551 for (i
= 0; i
< len
; i
++)
552 if (!add_conn_list(array
, list
[i
]))
554 mutex_unlock(&codec
->hash_mutex
);
558 array
->used
= old_used
;
559 mutex_unlock(&codec
->hash_mutex
);
562 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list
);
565 * snd_hda_get_conn_index - get the connection index of the given NID
566 * @codec: the HDA codec
567 * @mux: NID containing the list
568 * @nid: NID to select
569 * @recursive: 1 when searching NID recursively, otherwise 0
571 * Parses the connection list of the widget @mux and checks whether the
572 * widget @nid is present. If it is, return the connection index.
573 * Otherwise it returns -1.
575 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
576 hda_nid_t nid
, int recursive
)
578 hda_nid_t conn
[HDA_MAX_NUM_INPUTS
];
581 nums
= snd_hda_get_connections(codec
, mux
, conn
, ARRAY_SIZE(conn
));
582 for (i
= 0; i
< nums
; i
++)
588 snd_printd("hda_codec: too deep connection for 0x%x\n", nid
);
592 for (i
= 0; i
< nums
; i
++) {
593 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
594 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
596 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
601 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index
);
604 * snd_hda_queue_unsol_event - add an unsolicited event to queue
606 * @res: unsolicited event (lower 32bit of RIRB entry)
607 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
609 * Adds the given event to the queue. The events are processed in
610 * the workqueue asynchronously. Call this function in the interrupt
611 * hanlder when RIRB receives an unsolicited event.
613 * Returns 0 if successful, or a negative error code.
615 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
617 struct hda_bus_unsolicited
*unsol
;
620 trace_hda_unsol_event(bus
, res
, res_ex
);
625 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
629 unsol
->queue
[wp
] = res
;
630 unsol
->queue
[wp
+ 1] = res_ex
;
632 queue_work(bus
->workq
, &unsol
->work
);
636 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event
);
639 * process queued unsolicited events
641 static void process_unsol_events(struct work_struct
*work
)
643 struct hda_bus_unsolicited
*unsol
=
644 container_of(work
, struct hda_bus_unsolicited
, work
);
645 struct hda_bus
*bus
= unsol
->bus
;
646 struct hda_codec
*codec
;
647 unsigned int rp
, caddr
, res
;
649 while (unsol
->rp
!= unsol
->wp
) {
650 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
653 res
= unsol
->queue
[rp
];
654 caddr
= unsol
->queue
[rp
+ 1];
655 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
657 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
658 if (codec
&& codec
->patch_ops
.unsol_event
)
659 codec
->patch_ops
.unsol_event(codec
, res
);
664 * initialize unsolicited queue
666 static int init_unsol_queue(struct hda_bus
*bus
)
668 struct hda_bus_unsolicited
*unsol
;
670 if (bus
->unsol
) /* already initialized */
673 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
675 snd_printk(KERN_ERR
"hda_codec: "
676 "can't allocate unsolicited queue\n");
679 INIT_WORK(&unsol
->work
, process_unsol_events
);
688 static void snd_hda_codec_free(struct hda_codec
*codec
);
690 static int snd_hda_bus_free(struct hda_bus
*bus
)
692 struct hda_codec
*codec
, *n
;
697 flush_workqueue(bus
->workq
);
700 list_for_each_entry_safe(codec
, n
, &bus
->codec_list
, list
) {
701 snd_hda_codec_free(codec
);
703 if (bus
->ops
.private_free
)
704 bus
->ops
.private_free(bus
);
706 destroy_workqueue(bus
->workq
);
711 static int snd_hda_bus_dev_free(struct snd_device
*device
)
713 struct hda_bus
*bus
= device
->device_data
;
715 return snd_hda_bus_free(bus
);
718 #ifdef CONFIG_SND_HDA_HWDEP
719 static int snd_hda_bus_dev_register(struct snd_device
*device
)
721 struct hda_bus
*bus
= device
->device_data
;
722 struct hda_codec
*codec
;
723 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
724 snd_hda_hwdep_add_sysfs(codec
);
725 snd_hda_hwdep_add_power_sysfs(codec
);
730 #define snd_hda_bus_dev_register NULL
734 * snd_hda_bus_new - create a HDA bus
735 * @card: the card entry
736 * @temp: the template for hda_bus information
737 * @busp: the pointer to store the created bus instance
739 * Returns 0 if successful, or a negative error code.
741 int snd_hda_bus_new(struct snd_card
*card
,
742 const struct hda_bus_template
*temp
,
743 struct hda_bus
**busp
)
747 static struct snd_device_ops dev_ops
= {
748 .dev_register
= snd_hda_bus_dev_register
,
749 .dev_free
= snd_hda_bus_dev_free
,
752 if (snd_BUG_ON(!temp
))
754 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
760 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
762 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
767 bus
->private_data
= temp
->private_data
;
768 bus
->pci
= temp
->pci
;
769 bus
->modelname
= temp
->modelname
;
770 bus
->power_save
= temp
->power_save
;
771 bus
->ops
= temp
->ops
;
773 mutex_init(&bus
->cmd_mutex
);
774 mutex_init(&bus
->prepare_mutex
);
775 INIT_LIST_HEAD(&bus
->codec_list
);
777 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
778 "hd-audio%d", card
->number
);
779 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
781 snd_printk(KERN_ERR
"cannot create workqueue %s\n",
787 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
789 snd_hda_bus_free(bus
);
796 EXPORT_SYMBOL_HDA(snd_hda_bus_new
);
798 #ifdef CONFIG_SND_HDA_GENERIC
799 #define is_generic_config(codec) \
800 (codec->modelname && !strcmp(codec->modelname, "generic"))
802 #define is_generic_config(codec) 0
806 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
808 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
812 * find a matching codec preset
814 static const struct hda_codec_preset
*
815 find_codec_preset(struct hda_codec
*codec
)
817 struct hda_codec_preset_list
*tbl
;
818 const struct hda_codec_preset
*preset
;
819 unsigned int mod_requested
= 0;
821 if (is_generic_config(codec
))
822 return NULL
; /* use the generic parser */
825 mutex_lock(&preset_mutex
);
826 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
827 if (!try_module_get(tbl
->owner
)) {
828 snd_printk(KERN_ERR
"hda_codec: cannot module_get\n");
831 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
832 u32 mask
= preset
->mask
;
833 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
835 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
839 if (preset
->id
== (codec
->vendor_id
& mask
) &&
841 preset
->rev
== codec
->revision_id
)) {
842 mutex_unlock(&preset_mutex
);
843 codec
->owner
= tbl
->owner
;
847 module_put(tbl
->owner
);
849 mutex_unlock(&preset_mutex
);
851 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
854 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%08x",
857 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%04x*",
858 (codec
->vendor_id
>> 16) & 0xffff);
859 request_module(name
);
867 * get_codec_name - store the codec name
869 static int get_codec_name(struct hda_codec
*codec
)
871 const struct hda_vendor_id
*c
;
872 const char *vendor
= NULL
;
873 u16 vendor_id
= codec
->vendor_id
>> 16;
876 if (codec
->vendor_name
)
879 for (c
= hda_vendor_ids
; c
->id
; c
++) {
880 if (c
->id
== vendor_id
) {
886 sprintf(tmp
, "Generic %04x", vendor_id
);
889 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
890 if (!codec
->vendor_name
)
894 if (codec
->chip_name
)
897 if (codec
->preset
&& codec
->preset
->name
)
898 codec
->chip_name
= kstrdup(codec
->preset
->name
, GFP_KERNEL
);
900 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
901 codec
->chip_name
= kstrdup(tmp
, GFP_KERNEL
);
903 if (!codec
->chip_name
)
909 * look for an AFG and MFG nodes
911 static void setup_fg_nodes(struct hda_codec
*codec
)
913 int i
, total_nodes
, function_id
;
916 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
917 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
918 function_id
= snd_hda_param_read(codec
, nid
,
919 AC_PAR_FUNCTION_TYPE
);
920 switch (function_id
& 0xff) {
921 case AC_GRP_AUDIO_FUNCTION
:
923 codec
->afg_function_id
= function_id
& 0xff;
924 codec
->afg_unsol
= (function_id
>> 8) & 1;
926 case AC_GRP_MODEM_FUNCTION
:
928 codec
->mfg_function_id
= function_id
& 0xff;
929 codec
->mfg_unsol
= (function_id
>> 8) & 1;
938 * read widget caps for each widget and store in cache
940 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
945 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
947 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
950 nid
= codec
->start_nid
;
951 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
952 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
953 AC_PAR_AUDIO_WIDGET_CAP
);
957 /* read all pin default configurations and save codec->init_pins */
958 static int read_pin_defaults(struct hda_codec
*codec
)
961 hda_nid_t nid
= codec
->start_nid
;
963 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
964 struct hda_pincfg
*pin
;
965 unsigned int wcaps
= get_wcaps(codec
, nid
);
966 unsigned int wid_type
= get_wcaps_type(wcaps
);
967 if (wid_type
!= AC_WID_PIN
)
969 pin
= snd_array_new(&codec
->init_pins
);
973 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
974 AC_VERB_GET_CONFIG_DEFAULT
, 0);
975 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
976 AC_VERB_GET_PIN_WIDGET_CONTROL
,
982 /* look up the given pin config list and return the item matching with NID */
983 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
984 struct snd_array
*array
,
988 for (i
= 0; i
< array
->used
; i
++) {
989 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
996 /* set the current pin config value for the given NID.
997 * the value is cached, and read via snd_hda_codec_get_pincfg()
999 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
1000 hda_nid_t nid
, unsigned int cfg
)
1002 struct hda_pincfg
*pin
;
1004 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_PIN
)
1007 pin
= look_up_pincfg(codec
, list
, nid
);
1009 pin
= snd_array_new(list
);
1019 * snd_hda_codec_set_pincfg - Override a pin default configuration
1020 * @codec: the HDA codec
1021 * @nid: NID to set the pin config
1022 * @cfg: the pin default config value
1024 * Override a pin default configuration value in the cache.
1025 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1026 * priority than the real hardware value.
1028 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
1029 hda_nid_t nid
, unsigned int cfg
)
1031 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
1033 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg
);
1036 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1037 * @codec: the HDA codec
1038 * @nid: NID to get the pin config
1040 * Get the current pin config value of the given pin NID.
1041 * If the pincfg value is cached or overridden via sysfs or driver,
1042 * returns the cached value.
1044 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
1046 struct hda_pincfg
*pin
;
1048 #ifdef CONFIG_SND_HDA_HWDEP
1049 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
1053 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
1056 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1061 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg
);
1064 * snd_hda_shutup_pins - Shut up all pins
1065 * @codec: the HDA codec
1067 * Clear all pin controls to shup up before suspend for avoiding click noise.
1068 * The controls aren't cached so that they can be resumed properly.
1070 void snd_hda_shutup_pins(struct hda_codec
*codec
)
1073 /* don't shut up pins when unloading the driver; otherwise it breaks
1074 * the default pin setup at the next load of the driver
1076 if (codec
->bus
->shutdown
)
1078 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1079 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1080 /* use read here for syncing after issuing each verb */
1081 snd_hda_codec_read(codec
, pin
->nid
, 0,
1082 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
1084 codec
->pins_shutup
= 1;
1086 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins
);
1089 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1090 static void restore_shutup_pins(struct hda_codec
*codec
)
1093 if (!codec
->pins_shutup
)
1095 if (codec
->bus
->shutdown
)
1097 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1098 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1099 snd_hda_codec_write(codec
, pin
->nid
, 0,
1100 AC_VERB_SET_PIN_WIDGET_CONTROL
,
1103 codec
->pins_shutup
= 0;
1107 static void hda_jackpoll_work(struct work_struct
*work
)
1109 struct hda_codec
*codec
=
1110 container_of(work
, struct hda_codec
, jackpoll_work
.work
);
1111 if (!codec
->jackpoll_interval
)
1114 snd_hda_jack_set_dirty_all(codec
);
1115 snd_hda_jack_poll_all(codec
);
1116 queue_delayed_work(codec
->bus
->workq
, &codec
->jackpoll_work
,
1117 codec
->jackpoll_interval
);
1120 static void init_hda_cache(struct hda_cache_rec
*cache
,
1121 unsigned int record_size
);
1122 static void free_hda_cache(struct hda_cache_rec
*cache
);
1124 /* release all pincfg lists */
1125 static void free_init_pincfgs(struct hda_codec
*codec
)
1127 snd_array_free(&codec
->driver_pins
);
1128 #ifdef CONFIG_SND_HDA_HWDEP
1129 snd_array_free(&codec
->user_pins
);
1131 snd_array_free(&codec
->init_pins
);
1135 * audio-converter setup caches
1137 struct hda_cvt_setup
{
1142 unsigned char active
; /* cvt is currently used */
1143 unsigned char dirty
; /* setups should be cleared */
1146 /* get or create a cache entry for the given audio converter NID */
1147 static struct hda_cvt_setup
*
1148 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1150 struct hda_cvt_setup
*p
;
1153 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1154 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1158 p
= snd_array_new(&codec
->cvt_setups
);
1167 static void snd_hda_codec_free(struct hda_codec
*codec
)
1171 cancel_delayed_work_sync(&codec
->jackpoll_work
);
1172 snd_hda_jack_tbl_clear(codec
);
1173 free_init_pincfgs(codec
);
1175 cancel_delayed_work(&codec
->power_work
);
1176 flush_workqueue(codec
->bus
->workq
);
1178 list_del(&codec
->list
);
1179 snd_array_free(&codec
->mixers
);
1180 snd_array_free(&codec
->nids
);
1181 snd_array_free(&codec
->cvt_setups
);
1182 snd_array_free(&codec
->conn_lists
);
1183 snd_array_free(&codec
->spdif_out
);
1184 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1185 if (codec
->patch_ops
.free
)
1186 codec
->patch_ops
.free(codec
);
1188 if (!codec
->pm_down_notified
) /* cancel leftover refcounts */
1189 hda_call_pm_notify(codec
->bus
, false);
1191 module_put(codec
->owner
);
1192 free_hda_cache(&codec
->amp_cache
);
1193 free_hda_cache(&codec
->cmd_cache
);
1194 kfree(codec
->vendor_name
);
1195 kfree(codec
->chip_name
);
1196 kfree(codec
->modelname
);
1197 kfree(codec
->wcaps
);
1201 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
,
1202 hda_nid_t fg
, unsigned int power_state
);
1204 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
1205 unsigned int power_state
);
1208 * snd_hda_codec_new - create a HDA codec
1209 * @bus: the bus to assign
1210 * @codec_addr: the codec address
1211 * @codecp: the pointer to store the generated codec
1213 * Returns 0 if successful, or a negative error code.
1215 int snd_hda_codec_new(struct hda_bus
*bus
,
1216 unsigned int codec_addr
,
1217 struct hda_codec
**codecp
)
1219 struct hda_codec
*codec
;
1224 if (snd_BUG_ON(!bus
))
1226 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1229 if (bus
->caddr_tbl
[codec_addr
]) {
1230 snd_printk(KERN_ERR
"hda_codec: "
1231 "address 0x%x is already occupied\n", codec_addr
);
1235 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1236 if (codec
== NULL
) {
1237 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
1242 codec
->addr
= codec_addr
;
1243 mutex_init(&codec
->spdif_mutex
);
1244 mutex_init(&codec
->control_mutex
);
1245 mutex_init(&codec
->hash_mutex
);
1246 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1247 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1248 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1249 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1250 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1251 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1252 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1253 snd_array_init(&codec
->conn_lists
, sizeof(hda_nid_t
), 64);
1254 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1255 snd_array_init(&codec
->jacktbl
, sizeof(struct hda_jack_tbl
), 16);
1256 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
1259 spin_lock_init(&codec
->power_lock
);
1260 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1261 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1262 * the caller has to power down appropriatley after initialization
1265 hda_keep_power_on(codec
);
1266 hda_call_pm_notify(bus
, true);
1269 if (codec
->bus
->modelname
) {
1270 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1271 if (!codec
->modelname
) {
1272 snd_hda_codec_free(codec
);
1277 list_add_tail(&codec
->list
, &bus
->codec_list
);
1278 bus
->caddr_tbl
[codec_addr
] = codec
;
1280 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1282 if (codec
->vendor_id
== -1)
1283 /* read again, hopefully the access method was corrected
1284 * in the last read...
1286 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1288 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1289 AC_PAR_SUBSYSTEM_ID
);
1290 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1293 setup_fg_nodes(codec
);
1294 if (!codec
->afg
&& !codec
->mfg
) {
1295 snd_printdd("hda_codec: no AFG or MFG node found\n");
1300 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1301 err
= read_widget_caps(codec
, fg
);
1303 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1306 err
= read_pin_defaults(codec
);
1310 if (!codec
->subsystem_id
) {
1311 codec
->subsystem_id
=
1312 snd_hda_codec_read(codec
, fg
, 0,
1313 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1317 codec
->d3_stop_clk
= snd_hda_codec_get_supported_ps(codec
, fg
,
1319 if (!codec
->d3_stop_clk
)
1320 bus
->power_keep_link_on
= 1;
1322 codec
->epss
= snd_hda_codec_get_supported_ps(codec
, fg
,
1325 /* power-up all before initialization */
1326 hda_set_power_state(codec
, AC_PWRST_D0
);
1328 snd_hda_codec_proc_new(codec
);
1330 snd_hda_create_hwdep(codec
);
1332 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1333 codec
->subsystem_id
, codec
->revision_id
);
1334 snd_component_add(codec
->bus
->card
, component
);
1341 snd_hda_codec_free(codec
);
1344 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
1347 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1348 * @codec: the HDA codec
1350 * Start parsing of the given codec tree and (re-)initialize the whole
1353 * Returns 0 if successful or a negative error code.
1355 int snd_hda_codec_configure(struct hda_codec
*codec
)
1359 codec
->preset
= find_codec_preset(codec
);
1360 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1361 err
= get_codec_name(codec
);
1366 if (is_generic_config(codec
)) {
1367 err
= snd_hda_parse_generic_codec(codec
);
1370 if (codec
->preset
&& codec
->preset
->patch
) {
1371 err
= codec
->preset
->patch(codec
);
1375 /* call the default parser */
1376 err
= snd_hda_parse_generic_codec(codec
);
1378 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1381 if (!err
&& codec
->patch_ops
.unsol_event
)
1382 err
= init_unsol_queue(codec
->bus
);
1383 /* audio codec should override the mixer name */
1384 if (!err
&& (codec
->afg
|| !*codec
->bus
->card
->mixername
))
1385 snprintf(codec
->bus
->card
->mixername
,
1386 sizeof(codec
->bus
->card
->mixername
),
1387 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1390 EXPORT_SYMBOL_HDA(snd_hda_codec_configure
);
1392 /* update the stream-id if changed */
1393 static void update_pcm_stream_id(struct hda_codec
*codec
,
1394 struct hda_cvt_setup
*p
, hda_nid_t nid
,
1395 u32 stream_tag
, int channel_id
)
1397 unsigned int oldval
, newval
;
1399 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1400 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1401 newval
= (stream_tag
<< 4) | channel_id
;
1402 if (oldval
!= newval
)
1403 snd_hda_codec_write(codec
, nid
, 0,
1404 AC_VERB_SET_CHANNEL_STREAMID
,
1406 p
->stream_tag
= stream_tag
;
1407 p
->channel_id
= channel_id
;
1411 /* update the format-id if changed */
1412 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
1413 hda_nid_t nid
, int format
)
1415 unsigned int oldval
;
1417 if (p
->format_id
!= format
) {
1418 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1419 AC_VERB_GET_STREAM_FORMAT
, 0);
1420 if (oldval
!= format
) {
1422 snd_hda_codec_write(codec
, nid
, 0,
1423 AC_VERB_SET_STREAM_FORMAT
,
1426 p
->format_id
= format
;
1431 * snd_hda_codec_setup_stream - set up the codec for streaming
1432 * @codec: the CODEC to set up
1433 * @nid: the NID to set up
1434 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1435 * @channel_id: channel id to pass, zero based.
1436 * @format: stream format.
1438 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1440 int channel_id
, int format
)
1442 struct hda_codec
*c
;
1443 struct hda_cvt_setup
*p
;
1450 snd_printdd("hda_codec_setup_stream: "
1451 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1452 nid
, stream_tag
, channel_id
, format
);
1453 p
= get_hda_cvt_setup(codec
, nid
);
1457 if (codec
->pcm_format_first
)
1458 update_pcm_format(codec
, p
, nid
, format
);
1459 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1460 if (!codec
->pcm_format_first
)
1461 update_pcm_format(codec
, p
, nid
, format
);
1466 /* make other inactive cvts with the same stream-tag dirty */
1467 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1468 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1469 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1470 p
= snd_array_elem(&c
->cvt_setups
, i
);
1471 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1472 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1477 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1479 static void really_cleanup_stream(struct hda_codec
*codec
,
1480 struct hda_cvt_setup
*q
);
1483 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1484 * @codec: the CODEC to clean up
1485 * @nid: the NID to clean up
1486 * @do_now: really clean up the stream instead of clearing the active flag
1488 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1491 struct hda_cvt_setup
*p
;
1496 if (codec
->no_sticky_stream
)
1499 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1500 p
= get_hda_cvt_setup(codec
, nid
);
1502 /* here we just clear the active flag when do_now isn't set;
1503 * actual clean-ups will be done later in
1504 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1507 really_cleanup_stream(codec
, p
);
1512 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream
);
1514 static void really_cleanup_stream(struct hda_codec
*codec
,
1515 struct hda_cvt_setup
*q
)
1517 hda_nid_t nid
= q
->nid
;
1518 if (q
->stream_tag
|| q
->channel_id
)
1519 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1521 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1523 memset(q
, 0, sizeof(*q
));
1527 /* clean up the all conflicting obsolete streams */
1528 static void purify_inactive_streams(struct hda_codec
*codec
)
1530 struct hda_codec
*c
;
1533 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1534 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1535 struct hda_cvt_setup
*p
;
1536 p
= snd_array_elem(&c
->cvt_setups
, i
);
1538 really_cleanup_stream(c
, p
);
1544 /* clean up all streams; called from suspend */
1545 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1549 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1550 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1552 really_cleanup_stream(codec
, p
);
1558 * amp access functions
1561 /* FIXME: more better hash key? */
1562 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1563 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1564 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1565 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1566 #define INFO_AMP_CAPS (1<<0)
1567 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1569 /* initialize the hash table */
1570 static void init_hda_cache(struct hda_cache_rec
*cache
,
1571 unsigned int record_size
)
1573 memset(cache
, 0, sizeof(*cache
));
1574 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1575 snd_array_init(&cache
->buf
, record_size
, 64);
1578 static void free_hda_cache(struct hda_cache_rec
*cache
)
1580 snd_array_free(&cache
->buf
);
1583 /* query the hash. allocate an entry if not found. */
1584 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1586 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1587 u16 cur
= cache
->hash
[idx
];
1588 struct hda_cache_head
*info
;
1590 while (cur
!= 0xffff) {
1591 info
= snd_array_elem(&cache
->buf
, cur
);
1592 if (info
->key
== key
)
1599 /* query the hash. allocate an entry if not found. */
1600 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1603 struct hda_cache_head
*info
= get_hash(cache
, key
);
1606 /* add a new hash entry */
1607 info
= snd_array_new(&cache
->buf
);
1610 cur
= snd_array_index(&cache
->buf
, info
);
1613 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1614 info
->next
= cache
->hash
[idx
];
1615 cache
->hash
[idx
] = cur
;
1620 /* query and allocate an amp hash entry */
1621 static inline struct hda_amp_info
*
1622 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1624 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1627 /* overwrite the value with the key in the caps hash */
1628 static int write_caps_hash(struct hda_codec
*codec
, u32 key
, unsigned int val
)
1630 struct hda_amp_info
*info
;
1632 mutex_lock(&codec
->hash_mutex
);
1633 info
= get_alloc_amp_hash(codec
, key
);
1635 mutex_unlock(&codec
->hash_mutex
);
1638 info
->amp_caps
= val
;
1639 info
->head
.val
|= INFO_AMP_CAPS
;
1640 mutex_unlock(&codec
->hash_mutex
);
1644 /* query the value from the caps hash; if not found, fetch the current
1645 * value from the given function and store in the hash
1648 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, u32 key
,
1649 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
, int))
1651 struct hda_amp_info
*info
;
1654 mutex_lock(&codec
->hash_mutex
);
1655 info
= get_alloc_amp_hash(codec
, key
);
1657 mutex_unlock(&codec
->hash_mutex
);
1660 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1661 mutex_unlock(&codec
->hash_mutex
); /* for reentrance */
1662 val
= func(codec
, nid
, dir
);
1663 write_caps_hash(codec
, key
, val
);
1665 val
= info
->amp_caps
;
1666 mutex_unlock(&codec
->hash_mutex
);
1671 static unsigned int read_amp_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1674 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1676 return snd_hda_param_read(codec
, nid
,
1677 direction
== HDA_OUTPUT
?
1678 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1682 * query_amp_caps - query AMP capabilities
1683 * @codec: the HD-auio codec
1684 * @nid: the NID to query
1685 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1687 * Query AMP capabilities for the given widget and direction.
1688 * Returns the obtained capability bits.
1690 * When cap bits have been already read, this doesn't read again but
1691 * returns the cached value.
1693 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1695 return query_caps_hash(codec
, nid
, direction
,
1696 HDA_HASH_KEY(nid
, direction
, 0),
1699 EXPORT_SYMBOL_HDA(query_amp_caps
);
1702 * snd_hda_override_amp_caps - Override the AMP capabilities
1703 * @codec: the CODEC to clean up
1704 * @nid: the NID to clean up
1705 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1706 * @caps: the capability bits to set
1708 * Override the cached AMP caps bits value by the given one.
1709 * This function is useful if the driver needs to adjust the AMP ranges,
1710 * e.g. limit to 0dB, etc.
1712 * Returns zero if successful or a negative error code.
1714 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1717 return write_caps_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0), caps
);
1719 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1721 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1724 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1728 * snd_hda_query_pin_caps - Query PIN capabilities
1729 * @codec: the HD-auio codec
1730 * @nid: the NID to query
1732 * Query PIN capabilities for the given widget.
1733 * Returns the obtained capability bits.
1735 * When cap bits have been already read, this doesn't read again but
1736 * returns the cached value.
1738 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1740 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PINCAP_KEY(nid
),
1743 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1746 * snd_hda_override_pin_caps - Override the pin capabilities
1748 * @nid: the NID to override
1749 * @caps: the capability bits to set
1751 * Override the cached PIN capabilitiy bits value by the given one.
1753 * Returns zero if successful or a negative error code.
1755 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1758 return write_caps_hash(codec
, HDA_HASH_PINCAP_KEY(nid
), caps
);
1760 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps
);
1762 /* read or sync the hash value with the current value;
1763 * call within hash_mutex
1765 static struct hda_amp_info
*
1766 update_amp_hash(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1767 int direction
, int index
)
1769 struct hda_amp_info
*info
;
1770 unsigned int parm
, val
= 0;
1771 bool val_read
= false;
1774 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1777 if (!(info
->head
.val
& INFO_AMP_VOL(ch
))) {
1779 mutex_unlock(&codec
->hash_mutex
);
1780 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1781 parm
|= direction
== HDA_OUTPUT
?
1782 AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1784 val
= snd_hda_codec_read(codec
, nid
, 0,
1785 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1788 mutex_lock(&codec
->hash_mutex
);
1791 info
->vol
[ch
] = val
;
1792 info
->head
.val
|= INFO_AMP_VOL(ch
);
1798 * write the current volume in info to the h/w
1800 static void put_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
1801 hda_nid_t nid
, int ch
, int direction
, int index
,
1806 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1807 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1808 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1809 if ((val
& HDA_AMP_MUTE
) && !(info
->amp_caps
& AC_AMPCAP_MUTE
) &&
1810 (info
->amp_caps
& AC_AMPCAP_MIN_MUTE
))
1811 ; /* set the zero value as a fake mute */
1814 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1818 * snd_hda_codec_amp_read - Read AMP value
1819 * @codec: HD-audio codec
1820 * @nid: NID to read the AMP value
1821 * @ch: channel (left=0 or right=1)
1822 * @direction: #HDA_INPUT or #HDA_OUTPUT
1823 * @index: the index value (only for input direction)
1825 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1827 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1828 int direction
, int index
)
1830 struct hda_amp_info
*info
;
1831 unsigned int val
= 0;
1833 mutex_lock(&codec
->hash_mutex
);
1834 info
= update_amp_hash(codec
, nid
, ch
, direction
, index
);
1836 val
= info
->vol
[ch
];
1837 mutex_unlock(&codec
->hash_mutex
);
1840 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
1843 * snd_hda_codec_amp_update - update the AMP value
1844 * @codec: HD-audio codec
1845 * @nid: NID to read the AMP value
1846 * @ch: channel (left=0 or right=1)
1847 * @direction: #HDA_INPUT or #HDA_OUTPUT
1848 * @idx: the index value (only for input direction)
1849 * @mask: bit mask to set
1850 * @val: the bits value to set
1852 * Update the AMP value with a bit mask.
1853 * Returns 0 if the value is unchanged, 1 if changed.
1855 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1856 int direction
, int idx
, int mask
, int val
)
1858 struct hda_amp_info
*info
;
1860 if (snd_BUG_ON(mask
& ~0xff))
1864 mutex_lock(&codec
->hash_mutex
);
1865 info
= update_amp_hash(codec
, nid
, ch
, direction
, idx
);
1867 mutex_unlock(&codec
->hash_mutex
);
1870 val
|= info
->vol
[ch
] & ~mask
;
1871 if (info
->vol
[ch
] == val
) {
1872 mutex_unlock(&codec
->hash_mutex
);
1875 info
->vol
[ch
] = val
;
1876 mutex_unlock(&codec
->hash_mutex
);
1877 put_vol_mute(codec
, info
, nid
, ch
, direction
, idx
, val
);
1880 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
1883 * snd_hda_codec_amp_stereo - update the AMP stereo values
1884 * @codec: HD-audio codec
1885 * @nid: NID to read the AMP value
1886 * @direction: #HDA_INPUT or #HDA_OUTPUT
1887 * @idx: the index value (only for input direction)
1888 * @mask: bit mask to set
1889 * @val: the bits value to set
1891 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1892 * stereo widget with the same mask and value.
1894 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1895 int direction
, int idx
, int mask
, int val
)
1899 if (snd_BUG_ON(mask
& ~0xff))
1901 for (ch
= 0; ch
< 2; ch
++)
1902 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1906 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
1910 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1911 * @codec: HD-audio codec
1913 * Resume the all amp commands from the cache.
1915 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
1917 struct hda_amp_info
*buffer
= codec
->amp_cache
.buf
.list
;
1920 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++, buffer
++) {
1921 u32 key
= buffer
->head
.key
;
1923 unsigned int idx
, dir
, ch
;
1927 idx
= (key
>> 16) & 0xff;
1928 dir
= (key
>> 24) & 0xff;
1929 for (ch
= 0; ch
< 2; ch
++) {
1930 if (!(buffer
->head
.val
& INFO_AMP_VOL(ch
)))
1932 put_vol_mute(codec
, buffer
, nid
, ch
, dir
, idx
,
1937 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
1938 #endif /* CONFIG_PM */
1940 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1943 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1945 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1952 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1954 * The control element is supposed to have the private_value field
1955 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1957 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1958 struct snd_ctl_elem_info
*uinfo
)
1960 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1961 u16 nid
= get_amp_nid(kcontrol
);
1962 u8 chs
= get_amp_channels(kcontrol
);
1963 int dir
= get_amp_direction(kcontrol
);
1964 unsigned int ofs
= get_amp_offset(kcontrol
);
1966 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1967 uinfo
->count
= chs
== 3 ? 2 : 1;
1968 uinfo
->value
.integer
.min
= 0;
1969 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
1970 if (!uinfo
->value
.integer
.max
) {
1971 printk(KERN_WARNING
"hda_codec: "
1972 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
1978 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
1981 static inline unsigned int
1982 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1983 int ch
, int dir
, int idx
, unsigned int ofs
)
1986 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1987 val
&= HDA_AMP_VOLMASK
;
1996 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1997 int ch
, int dir
, int idx
, unsigned int ofs
,
2000 unsigned int maxval
;
2004 /* ofs = 0: raw max value */
2005 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
2008 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
2009 HDA_AMP_VOLMASK
, val
);
2013 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2015 * The control element is supposed to have the private_value field
2016 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2018 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
2019 struct snd_ctl_elem_value
*ucontrol
)
2021 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2022 hda_nid_t nid
= get_amp_nid(kcontrol
);
2023 int chs
= get_amp_channels(kcontrol
);
2024 int dir
= get_amp_direction(kcontrol
);
2025 int idx
= get_amp_index(kcontrol
);
2026 unsigned int ofs
= get_amp_offset(kcontrol
);
2027 long *valp
= ucontrol
->value
.integer
.value
;
2030 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
2032 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
2035 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
2038 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2040 * The control element is supposed to have the private_value field
2041 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2043 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
2044 struct snd_ctl_elem_value
*ucontrol
)
2046 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2047 hda_nid_t nid
= get_amp_nid(kcontrol
);
2048 int chs
= get_amp_channels(kcontrol
);
2049 int dir
= get_amp_direction(kcontrol
);
2050 int idx
= get_amp_index(kcontrol
);
2051 unsigned int ofs
= get_amp_offset(kcontrol
);
2052 long *valp
= ucontrol
->value
.integer
.value
;
2055 snd_hda_power_up(codec
);
2057 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
2061 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
2062 snd_hda_power_down(codec
);
2065 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
2068 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2070 * The control element is supposed to have the private_value field
2071 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2073 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2074 unsigned int size
, unsigned int __user
*_tlv
)
2076 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2077 hda_nid_t nid
= get_amp_nid(kcontrol
);
2078 int dir
= get_amp_direction(kcontrol
);
2079 unsigned int ofs
= get_amp_offset(kcontrol
);
2080 bool min_mute
= get_amp_min_mute(kcontrol
);
2081 u32 caps
, val1
, val2
;
2083 if (size
< 4 * sizeof(unsigned int))
2085 caps
= query_amp_caps(codec
, nid
, dir
);
2086 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2087 val2
= (val2
+ 1) * 25;
2088 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2090 val1
= ((int)val1
) * ((int)val2
);
2091 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
2092 val2
|= TLV_DB_SCALE_MUTE
;
2093 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2095 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2097 if (put_user(val1
, _tlv
+ 2))
2099 if (put_user(val2
, _tlv
+ 3))
2103 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
2106 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2107 * @codec: HD-audio codec
2108 * @nid: NID of a reference widget
2109 * @dir: #HDA_INPUT or #HDA_OUTPUT
2110 * @tlv: TLV data to be stored, at least 4 elements
2112 * Set (static) TLV data for a virtual master volume using the AMP caps
2113 * obtained from the reference NID.
2114 * The volume range is recalculated as if the max volume is 0dB.
2116 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2122 caps
= query_amp_caps(codec
, nid
, dir
);
2123 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2124 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2125 step
= (step
+ 1) * 25;
2126 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2127 tlv
[1] = 2 * sizeof(unsigned int);
2128 tlv
[2] = -nums
* step
;
2131 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
2133 /* find a mixer control element with the given name */
2134 static struct snd_kcontrol
*
2135 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
2137 struct snd_ctl_elem_id id
;
2138 memset(&id
, 0, sizeof(id
));
2139 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2142 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2144 strcpy(id
.name
, name
);
2145 return snd_ctl_find_id(codec
->bus
->card
, &id
);
2149 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2150 * @codec: HD-audio codec
2151 * @name: ctl id name string
2153 * Get the control element with the given id string and IFACE_MIXER.
2155 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2158 return find_mixer_ctl(codec
, name
, 0, 0);
2160 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
2162 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
2166 for (idx
= 0; idx
< 16; idx
++) { /* 16 ctlrs should be large enough */
2167 if (!find_mixer_ctl(codec
, name
, dev
, idx
))
2174 * snd_hda_ctl_add - Add a control element and assign to the codec
2175 * @codec: HD-audio codec
2176 * @nid: corresponding NID (optional)
2177 * @kctl: the control element to assign
2179 * Add the given control element to an array inside the codec instance.
2180 * All control elements belonging to a codec are supposed to be added
2181 * by this function so that a proper clean-up works at the free or
2182 * reconfiguration time.
2184 * If non-zero @nid is passed, the NID is assigned to the control element.
2185 * The assignment is shown in the codec proc file.
2187 * snd_hda_ctl_add() checks the control subdev id field whether
2188 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2189 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2190 * specifies if kctl->private_value is a HDA amplifier value.
2192 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2193 struct snd_kcontrol
*kctl
)
2196 unsigned short flags
= 0;
2197 struct hda_nid_item
*item
;
2199 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2200 flags
|= HDA_NID_ITEM_AMP
;
2202 nid
= get_amp_nid_(kctl
->private_value
);
2204 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2205 nid
= kctl
->id
.subdevice
& 0xffff;
2206 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2207 kctl
->id
.subdevice
= 0;
2208 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
2211 item
= snd_array_new(&codec
->mixers
);
2216 item
->flags
= flags
;
2219 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
2222 * snd_hda_add_nid - Assign a NID to a control element
2223 * @codec: HD-audio codec
2224 * @nid: corresponding NID (optional)
2225 * @kctl: the control element to assign
2226 * @index: index to kctl
2228 * Add the given control element to an array inside the codec instance.
2229 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2230 * NID:KCTL mapping - for example "Capture Source" selector.
2232 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2233 unsigned int index
, hda_nid_t nid
)
2235 struct hda_nid_item
*item
;
2238 item
= snd_array_new(&codec
->nids
);
2242 item
->index
= index
;
2246 printk(KERN_ERR
"hda-codec: no NID for mapping control %s:%d:%d\n",
2247 kctl
->id
.name
, kctl
->id
.index
, index
);
2250 EXPORT_SYMBOL_HDA(snd_hda_add_nid
);
2253 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2254 * @codec: HD-audio codec
2256 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2259 struct hda_nid_item
*items
= codec
->mixers
.list
;
2260 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2261 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
2262 snd_array_free(&codec
->mixers
);
2263 snd_array_free(&codec
->nids
);
2266 /* pseudo device locking
2267 * toggle card->shutdown to allow/disallow the device access (as a hack)
2269 int snd_hda_lock_devices(struct hda_bus
*bus
)
2271 struct snd_card
*card
= bus
->card
;
2272 struct hda_codec
*codec
;
2274 spin_lock(&card
->files_lock
);
2278 if (!list_empty(&card
->ctl_files
))
2281 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2283 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2284 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2287 if (cpcm
->pcm
->streams
[0].substream_opened
||
2288 cpcm
->pcm
->streams
[1].substream_opened
)
2292 spin_unlock(&card
->files_lock
);
2298 spin_unlock(&card
->files_lock
);
2301 EXPORT_SYMBOL_HDA(snd_hda_lock_devices
);
2303 void snd_hda_unlock_devices(struct hda_bus
*bus
)
2305 struct snd_card
*card
= bus
->card
;
2308 spin_lock(&card
->files_lock
);
2310 spin_unlock(&card
->files_lock
);
2312 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices
);
2315 * snd_hda_codec_reset - Clear all objects assigned to the codec
2316 * @codec: HD-audio codec
2318 * This frees the all PCM and control elements assigned to the codec, and
2319 * clears the caches and restores the pin default configurations.
2321 * When a device is being used, it returns -EBSY. If successfully freed,
2324 int snd_hda_codec_reset(struct hda_codec
*codec
)
2326 struct hda_bus
*bus
= codec
->bus
;
2327 struct snd_card
*card
= bus
->card
;
2330 if (snd_hda_lock_devices(bus
) < 0)
2333 /* OK, let it free */
2334 cancel_delayed_work_sync(&codec
->jackpoll_work
);
2336 cancel_delayed_work_sync(&codec
->power_work
);
2337 codec
->power_on
= 0;
2338 codec
->power_transition
= 0;
2339 codec
->power_jiffies
= jiffies
;
2340 flush_workqueue(bus
->workq
);
2342 snd_hda_ctls_clear(codec
);
2344 for (i
= 0; i
< codec
->num_pcms
; i
++) {
2345 if (codec
->pcm_info
[i
].pcm
) {
2346 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
2347 clear_bit(codec
->pcm_info
[i
].device
,
2351 if (codec
->patch_ops
.free
)
2352 codec
->patch_ops
.free(codec
);
2353 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2354 snd_hda_jack_tbl_clear(codec
);
2355 codec
->proc_widget_hook
= NULL
;
2357 free_hda_cache(&codec
->amp_cache
);
2358 free_hda_cache(&codec
->cmd_cache
);
2359 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2360 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2361 /* free only driver_pins so that init_pins + user_pins are restored */
2362 snd_array_free(&codec
->driver_pins
);
2363 snd_array_free(&codec
->cvt_setups
);
2364 snd_array_free(&codec
->spdif_out
);
2365 codec
->num_pcms
= 0;
2366 codec
->pcm_info
= NULL
;
2367 codec
->preset
= NULL
;
2368 codec
->slave_dig_outs
= NULL
;
2369 codec
->spdif_status_reset
= 0;
2370 module_put(codec
->owner
);
2371 codec
->owner
= NULL
;
2373 /* allow device access again */
2374 snd_hda_unlock_devices(bus
);
2378 typedef int (*map_slave_func_t
)(void *, struct snd_kcontrol
*);
2380 /* apply the function to all matching slave ctls in the mixer list */
2381 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2382 const char *suffix
, map_slave_func_t func
, void *data
)
2384 struct hda_nid_item
*items
;
2385 const char * const *s
;
2388 items
= codec
->mixers
.list
;
2389 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2390 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2391 if (!sctl
|| !sctl
->id
.name
||
2392 sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2394 for (s
= slaves
; *s
; s
++) {
2395 char tmpname
[sizeof(sctl
->id
.name
)];
2396 const char *name
= *s
;
2398 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
2402 if (!strcmp(sctl
->id
.name
, name
)) {
2403 err
= func(data
, sctl
);
2413 static int check_slave_present(void *data
, struct snd_kcontrol
*sctl
)
2418 /* guess the value corresponding to 0dB */
2419 static int get_kctl_0dB_offset(struct snd_kcontrol
*kctl
)
2422 const int *tlv
= NULL
;
2425 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
2426 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2427 mm_segment_t fs
= get_fs();
2429 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
2432 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
2434 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
)
2435 val
= -tlv
[2] / tlv
[3];
2439 /* call kctl->put with the given value(s) */
2440 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
2442 struct snd_ctl_elem_value
*ucontrol
;
2443 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
2446 ucontrol
->value
.integer
.value
[0] = val
;
2447 ucontrol
->value
.integer
.value
[1] = val
;
2448 kctl
->put(kctl
, ucontrol
);
2453 /* initialize the slave volume with 0dB */
2454 static int init_slave_0dB(void *data
, struct snd_kcontrol
*slave
)
2456 int offset
= get_kctl_0dB_offset(slave
);
2458 put_kctl_with_value(slave
, offset
);
2462 /* unmute the slave */
2463 static int init_slave_unmute(void *data
, struct snd_kcontrol
*slave
)
2465 return put_kctl_with_value(slave
, 1);
2469 * snd_hda_add_vmaster - create a virtual master control and add slaves
2470 * @codec: HD-audio codec
2471 * @name: vmaster control name
2472 * @tlv: TLV data (optional)
2473 * @slaves: slave control names (optional)
2474 * @suffix: suffix string to each slave name (optional)
2475 * @init_slave_vol: initialize slaves to unmute/0dB
2476 * @ctl_ret: store the vmaster kcontrol in return
2478 * Create a virtual master control with the given name. The TLV data
2479 * must be either NULL or a valid data.
2481 * @slaves is a NULL-terminated array of strings, each of which is a
2482 * slave control name. All controls with these names are assigned to
2483 * the new virtual master control.
2485 * This function returns zero if successful or a negative error code.
2487 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2488 unsigned int *tlv
, const char * const *slaves
,
2489 const char *suffix
, bool init_slave_vol
,
2490 struct snd_kcontrol
**ctl_ret
)
2492 struct snd_kcontrol
*kctl
;
2498 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
2500 snd_printdd("No slave found for %s\n", name
);
2503 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2506 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2510 err
= map_slaves(codec
, slaves
, suffix
,
2511 (map_slave_func_t
)snd_ctl_add_slave
, kctl
);
2515 /* init with master mute & zero volume */
2516 put_kctl_with_value(kctl
, 0);
2518 map_slaves(codec
, slaves
, suffix
,
2519 tlv
? init_slave_0dB
: init_slave_unmute
, kctl
);
2525 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster
);
2528 * mute-LED control using vmaster
2530 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
2531 struct snd_ctl_elem_info
*uinfo
)
2533 static const char * const texts
[] = {
2534 "On", "Off", "Follow Master"
2538 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2540 uinfo
->value
.enumerated
.items
= 3;
2541 index
= uinfo
->value
.enumerated
.item
;
2544 strcpy(uinfo
->value
.enumerated
.name
, texts
[index
]);
2548 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
2549 struct snd_ctl_elem_value
*ucontrol
)
2551 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2552 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
2556 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
2557 struct snd_ctl_elem_value
*ucontrol
)
2559 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2560 unsigned int old_mode
= hook
->mute_mode
;
2562 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
2563 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
2564 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2565 if (old_mode
== hook
->mute_mode
)
2567 snd_hda_sync_vmaster_hook(hook
);
2571 static struct snd_kcontrol_new vmaster_mute_mode
= {
2572 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2573 .name
= "Mute-LED Mode",
2574 .info
= vmaster_mute_mode_info
,
2575 .get
= vmaster_mute_mode_get
,
2576 .put
= vmaster_mute_mode_put
,
2580 * Add a mute-LED hook with the given vmaster switch kctl
2581 * "Mute-LED Mode" control is automatically created and associated with
2584 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
2585 struct hda_vmaster_mute_hook
*hook
,
2586 bool expose_enum_ctl
)
2588 struct snd_kcontrol
*kctl
;
2590 if (!hook
->hook
|| !hook
->sw_kctl
)
2592 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, hook
->hook
, codec
);
2593 hook
->codec
= codec
;
2594 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2595 if (!expose_enum_ctl
)
2597 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
2600 return snd_hda_ctl_add(codec
, 0, kctl
);
2602 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook
);
2605 * Call the hook with the current value for synchronization
2606 * Should be called in init callback
2608 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
2610 if (!hook
->hook
|| !hook
->codec
)
2612 switch (hook
->mute_mode
) {
2613 case HDA_VMUTE_FOLLOW_MASTER
:
2614 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
2617 hook
->hook(hook
->codec
, hook
->mute_mode
);
2621 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook
);
2625 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2627 * The control element is supposed to have the private_value field
2628 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2630 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2631 struct snd_ctl_elem_info
*uinfo
)
2633 int chs
= get_amp_channels(kcontrol
);
2635 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2636 uinfo
->count
= chs
== 3 ? 2 : 1;
2637 uinfo
->value
.integer
.min
= 0;
2638 uinfo
->value
.integer
.max
= 1;
2641 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
2644 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2646 * The control element is supposed to have the private_value field
2647 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2649 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2650 struct snd_ctl_elem_value
*ucontrol
)
2652 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2653 hda_nid_t nid
= get_amp_nid(kcontrol
);
2654 int chs
= get_amp_channels(kcontrol
);
2655 int dir
= get_amp_direction(kcontrol
);
2656 int idx
= get_amp_index(kcontrol
);
2657 long *valp
= ucontrol
->value
.integer
.value
;
2660 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2661 HDA_AMP_MUTE
) ? 0 : 1;
2663 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2664 HDA_AMP_MUTE
) ? 0 : 1;
2667 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
2670 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2672 * The control element is supposed to have the private_value field
2673 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2675 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2676 struct snd_ctl_elem_value
*ucontrol
)
2678 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2679 hda_nid_t nid
= get_amp_nid(kcontrol
);
2680 int chs
= get_amp_channels(kcontrol
);
2681 int dir
= get_amp_direction(kcontrol
);
2682 int idx
= get_amp_index(kcontrol
);
2683 long *valp
= ucontrol
->value
.integer
.value
;
2686 snd_hda_power_up(codec
);
2688 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2690 *valp
? 0 : HDA_AMP_MUTE
);
2694 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2696 *valp
? 0 : HDA_AMP_MUTE
);
2697 hda_call_check_power_status(codec
, nid
);
2698 snd_hda_power_down(codec
);
2701 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
2704 * bound volume controls
2706 * bind multiple volumes (# indices, from 0)
2709 #define AMP_VAL_IDX_SHIFT 19
2710 #define AMP_VAL_IDX_MASK (0x0f<<19)
2713 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2715 * The control element is supposed to have the private_value field
2716 * set up via HDA_BIND_MUTE*() macros.
2718 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2719 struct snd_ctl_elem_value
*ucontrol
)
2721 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2725 mutex_lock(&codec
->control_mutex
);
2726 pval
= kcontrol
->private_value
;
2727 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2728 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2729 kcontrol
->private_value
= pval
;
2730 mutex_unlock(&codec
->control_mutex
);
2733 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
2736 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2738 * The control element is supposed to have the private_value field
2739 * set up via HDA_BIND_MUTE*() macros.
2741 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2742 struct snd_ctl_elem_value
*ucontrol
)
2744 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2746 int i
, indices
, err
= 0, change
= 0;
2748 mutex_lock(&codec
->control_mutex
);
2749 pval
= kcontrol
->private_value
;
2750 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2751 for (i
= 0; i
< indices
; i
++) {
2752 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2753 (i
<< AMP_VAL_IDX_SHIFT
);
2754 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2759 kcontrol
->private_value
= pval
;
2760 mutex_unlock(&codec
->control_mutex
);
2761 return err
< 0 ? err
: change
;
2763 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
2766 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2768 * The control element is supposed to have the private_value field
2769 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2771 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2772 struct snd_ctl_elem_info
*uinfo
)
2774 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2775 struct hda_bind_ctls
*c
;
2778 mutex_lock(&codec
->control_mutex
);
2779 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2780 kcontrol
->private_value
= *c
->values
;
2781 err
= c
->ops
->info(kcontrol
, uinfo
);
2782 kcontrol
->private_value
= (long)c
;
2783 mutex_unlock(&codec
->control_mutex
);
2786 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
2789 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2791 * The control element is supposed to have the private_value field
2792 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2794 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2795 struct snd_ctl_elem_value
*ucontrol
)
2797 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2798 struct hda_bind_ctls
*c
;
2801 mutex_lock(&codec
->control_mutex
);
2802 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2803 kcontrol
->private_value
= *c
->values
;
2804 err
= c
->ops
->get(kcontrol
, ucontrol
);
2805 kcontrol
->private_value
= (long)c
;
2806 mutex_unlock(&codec
->control_mutex
);
2809 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
2812 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2814 * The control element is supposed to have the private_value field
2815 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2817 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2818 struct snd_ctl_elem_value
*ucontrol
)
2820 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2821 struct hda_bind_ctls
*c
;
2822 unsigned long *vals
;
2823 int err
= 0, change
= 0;
2825 mutex_lock(&codec
->control_mutex
);
2826 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2827 for (vals
= c
->values
; *vals
; vals
++) {
2828 kcontrol
->private_value
= *vals
;
2829 err
= c
->ops
->put(kcontrol
, ucontrol
);
2834 kcontrol
->private_value
= (long)c
;
2835 mutex_unlock(&codec
->control_mutex
);
2836 return err
< 0 ? err
: change
;
2838 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
2841 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2843 * The control element is supposed to have the private_value field
2844 * set up via HDA_BIND_VOL() macro.
2846 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2847 unsigned int size
, unsigned int __user
*tlv
)
2849 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2850 struct hda_bind_ctls
*c
;
2853 mutex_lock(&codec
->control_mutex
);
2854 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2855 kcontrol
->private_value
= *c
->values
;
2856 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
2857 kcontrol
->private_value
= (long)c
;
2858 mutex_unlock(&codec
->control_mutex
);
2861 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
2863 struct hda_ctl_ops snd_hda_bind_vol
= {
2864 .info
= snd_hda_mixer_amp_volume_info
,
2865 .get
= snd_hda_mixer_amp_volume_get
,
2866 .put
= snd_hda_mixer_amp_volume_put
,
2867 .tlv
= snd_hda_mixer_amp_tlv
2869 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
2871 struct hda_ctl_ops snd_hda_bind_sw
= {
2872 .info
= snd_hda_mixer_amp_switch_info
,
2873 .get
= snd_hda_mixer_amp_switch_get
,
2874 .put
= snd_hda_mixer_amp_switch_put
,
2875 .tlv
= snd_hda_mixer_amp_tlv
2877 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
2880 * SPDIF out controls
2883 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2884 struct snd_ctl_elem_info
*uinfo
)
2886 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2891 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2892 struct snd_ctl_elem_value
*ucontrol
)
2894 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2895 IEC958_AES0_NONAUDIO
|
2896 IEC958_AES0_CON_EMPHASIS_5015
|
2897 IEC958_AES0_CON_NOT_COPYRIGHT
;
2898 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2899 IEC958_AES1_CON_ORIGINAL
;
2903 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2904 struct snd_ctl_elem_value
*ucontrol
)
2906 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2907 IEC958_AES0_NONAUDIO
|
2908 IEC958_AES0_PRO_EMPHASIS_5015
;
2912 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2913 struct snd_ctl_elem_value
*ucontrol
)
2915 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2916 int idx
= kcontrol
->private_value
;
2917 struct hda_spdif_out
*spdif
;
2919 mutex_lock(&codec
->spdif_mutex
);
2920 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2921 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
2922 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
2923 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
2924 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
2925 mutex_unlock(&codec
->spdif_mutex
);
2930 /* convert from SPDIF status bits to HDA SPDIF bits
2931 * bit 0 (DigEn) is always set zero (to be filled later)
2933 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2935 unsigned short val
= 0;
2937 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2938 val
|= AC_DIG1_PROFESSIONAL
;
2939 if (sbits
& IEC958_AES0_NONAUDIO
)
2940 val
|= AC_DIG1_NONAUDIO
;
2941 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2942 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2943 IEC958_AES0_PRO_EMPHASIS_5015
)
2944 val
|= AC_DIG1_EMPHASIS
;
2946 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2947 IEC958_AES0_CON_EMPHASIS_5015
)
2948 val
|= AC_DIG1_EMPHASIS
;
2949 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2950 val
|= AC_DIG1_COPYRIGHT
;
2951 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2952 val
|= AC_DIG1_LEVEL
;
2953 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2958 /* convert to SPDIF status bits from HDA SPDIF bits
2960 static unsigned int convert_to_spdif_status(unsigned short val
)
2962 unsigned int sbits
= 0;
2964 if (val
& AC_DIG1_NONAUDIO
)
2965 sbits
|= IEC958_AES0_NONAUDIO
;
2966 if (val
& AC_DIG1_PROFESSIONAL
)
2967 sbits
|= IEC958_AES0_PROFESSIONAL
;
2968 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2969 if (sbits
& AC_DIG1_EMPHASIS
)
2970 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
2972 if (val
& AC_DIG1_EMPHASIS
)
2973 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
2974 if (!(val
& AC_DIG1_COPYRIGHT
))
2975 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
2976 if (val
& AC_DIG1_LEVEL
)
2977 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
2978 sbits
|= val
& (0x7f << 8);
2983 /* set digital convert verbs both for the given NID and its slaves */
2984 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
2989 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
2990 d
= codec
->slave_dig_outs
;
2994 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
2997 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
3001 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
3003 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
3006 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
3007 struct snd_ctl_elem_value
*ucontrol
)
3009 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3010 int idx
= kcontrol
->private_value
;
3011 struct hda_spdif_out
*spdif
;
3016 mutex_lock(&codec
->spdif_mutex
);
3017 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3019 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
3020 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
3021 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
3022 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
3023 val
= convert_from_spdif_status(spdif
->status
);
3024 val
|= spdif
->ctls
& 1;
3025 change
= spdif
->ctls
!= val
;
3027 if (change
&& nid
!= (u16
)-1)
3028 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3029 mutex_unlock(&codec
->spdif_mutex
);
3033 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3035 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
3036 struct snd_ctl_elem_value
*ucontrol
)
3038 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3039 int idx
= kcontrol
->private_value
;
3040 struct hda_spdif_out
*spdif
;
3042 mutex_lock(&codec
->spdif_mutex
);
3043 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3044 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
3045 mutex_unlock(&codec
->spdif_mutex
);
3049 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
3052 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
3053 /* unmute amp switch (if any) */
3054 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
3055 (dig1
& AC_DIG1_ENABLE
))
3056 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
3060 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
3061 struct snd_ctl_elem_value
*ucontrol
)
3063 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3064 int idx
= kcontrol
->private_value
;
3065 struct hda_spdif_out
*spdif
;
3070 mutex_lock(&codec
->spdif_mutex
);
3071 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3073 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
3074 if (ucontrol
->value
.integer
.value
[0])
3075 val
|= AC_DIG1_ENABLE
;
3076 change
= spdif
->ctls
!= val
;
3078 if (change
&& nid
!= (u16
)-1)
3079 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
3080 mutex_unlock(&codec
->spdif_mutex
);
3084 static struct snd_kcontrol_new dig_mixes
[] = {
3086 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3087 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3088 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
3089 .info
= snd_hda_spdif_mask_info
,
3090 .get
= snd_hda_spdif_cmask_get
,
3093 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3094 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3095 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
3096 .info
= snd_hda_spdif_mask_info
,
3097 .get
= snd_hda_spdif_pmask_get
,
3100 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3101 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
3102 .info
= snd_hda_spdif_mask_info
,
3103 .get
= snd_hda_spdif_default_get
,
3104 .put
= snd_hda_spdif_default_put
,
3107 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3108 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
3109 .info
= snd_hda_spdif_out_switch_info
,
3110 .get
= snd_hda_spdif_out_switch_get
,
3111 .put
= snd_hda_spdif_out_switch_put
,
3117 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3118 * @codec: the HDA codec
3119 * @associated_nid: NID that new ctls associated with
3120 * @cvt_nid: converter NID
3121 * @type: HDA_PCM_TYPE_*
3122 * Creates controls related with the digital output.
3123 * Called from each patch supporting the digital out.
3125 * Returns 0 if successful, or a negative error code.
3127 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
3128 hda_nid_t associated_nid
,
3133 struct snd_kcontrol
*kctl
;
3134 struct snd_kcontrol_new
*dig_mix
;
3136 const int spdif_pcm_dev
= 1;
3137 struct hda_spdif_out
*spdif
;
3139 if (codec
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
3140 type
== HDA_PCM_TYPE_SPDIF
) {
3141 dev
= spdif_pcm_dev
;
3142 } else if (codec
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
3143 type
== HDA_PCM_TYPE_HDMI
) {
3144 for (idx
= 0; idx
< codec
->spdif_out
.used
; idx
++) {
3145 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3146 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3147 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, idx
);
3150 kctl
->id
.device
= spdif_pcm_dev
;
3153 codec
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
3155 if (!codec
->primary_dig_out_type
)
3156 codec
->primary_dig_out_type
= type
;
3158 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", dev
);
3160 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
3163 spdif
= snd_array_new(&codec
->spdif_out
);
3164 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3165 kctl
= snd_ctl_new1(dig_mix
, codec
);
3168 kctl
->id
.device
= dev
;
3169 kctl
->id
.index
= idx
;
3170 kctl
->private_value
= codec
->spdif_out
.used
- 1;
3171 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
3175 spdif
->nid
= cvt_nid
;
3176 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
3177 AC_VERB_GET_DIGI_CONVERT_1
, 0);
3178 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
3181 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls
);
3183 /* get the hda_spdif_out entry from the given NID
3184 * call within spdif_mutex lock
3186 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
3190 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
3191 struct hda_spdif_out
*spdif
=
3192 snd_array_elem(&codec
->spdif_out
, i
);
3193 if (spdif
->nid
== nid
)
3198 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid
);
3200 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
3202 struct hda_spdif_out
*spdif
;
3204 mutex_lock(&codec
->spdif_mutex
);
3205 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3206 spdif
->nid
= (u16
)-1;
3207 mutex_unlock(&codec
->spdif_mutex
);
3209 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign
);
3211 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
3213 struct hda_spdif_out
*spdif
;
3216 mutex_lock(&codec
->spdif_mutex
);
3217 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3218 if (spdif
->nid
!= nid
) {
3221 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3223 mutex_unlock(&codec
->spdif_mutex
);
3225 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign
);
3228 * SPDIF sharing with analog output
3230 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
3231 struct snd_ctl_elem_value
*ucontrol
)
3233 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3234 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
3238 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
3239 struct snd_ctl_elem_value
*ucontrol
)
3241 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3242 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
3246 static struct snd_kcontrol_new spdif_share_sw
= {
3247 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3248 .name
= "IEC958 Default PCM Playback Switch",
3249 .info
= snd_ctl_boolean_mono_info
,
3250 .get
= spdif_share_sw_get
,
3251 .put
= spdif_share_sw_put
,
3255 * snd_hda_create_spdif_share_sw - create Default PCM switch
3256 * @codec: the HDA codec
3257 * @mout: multi-out instance
3259 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
3260 struct hda_multi_out
*mout
)
3262 if (!mout
->dig_out_nid
)
3264 /* ATTENTION: here mout is passed as private_data, instead of codec */
3265 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
,
3266 snd_ctl_new1(&spdif_share_sw
, mout
));
3268 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
3274 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3276 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3277 struct snd_ctl_elem_value
*ucontrol
)
3279 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3281 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3285 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3286 struct snd_ctl_elem_value
*ucontrol
)
3288 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3289 hda_nid_t nid
= kcontrol
->private_value
;
3290 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3293 mutex_lock(&codec
->spdif_mutex
);
3294 change
= codec
->spdif_in_enable
!= val
;
3296 codec
->spdif_in_enable
= val
;
3297 snd_hda_codec_write_cache(codec
, nid
, 0,
3298 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3300 mutex_unlock(&codec
->spdif_mutex
);
3304 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3305 struct snd_ctl_elem_value
*ucontrol
)
3307 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3308 hda_nid_t nid
= kcontrol
->private_value
;
3312 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3313 sbits
= convert_to_spdif_status(val
);
3314 ucontrol
->value
.iec958
.status
[0] = sbits
;
3315 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3316 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3317 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3321 static struct snd_kcontrol_new dig_in_ctls
[] = {
3323 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3324 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3325 .info
= snd_hda_spdif_in_switch_info
,
3326 .get
= snd_hda_spdif_in_switch_get
,
3327 .put
= snd_hda_spdif_in_switch_put
,
3330 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3331 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3332 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3333 .info
= snd_hda_spdif_mask_info
,
3334 .get
= snd_hda_spdif_in_status_get
,
3340 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3341 * @codec: the HDA codec
3342 * @nid: audio in widget NID
3344 * Creates controls related with the SPDIF input.
3345 * Called from each patch supporting the SPDIF in.
3347 * Returns 0 if successful, or a negative error code.
3349 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3352 struct snd_kcontrol
*kctl
;
3353 struct snd_kcontrol_new
*dig_mix
;
3356 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
3358 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
3361 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3362 kctl
= snd_ctl_new1(dig_mix
, codec
);
3365 kctl
->private_value
= nid
;
3366 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3370 codec
->spdif_in_enable
=
3371 snd_hda_codec_read(codec
, nid
, 0,
3372 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3376 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
3383 /* build a 32bit cache key with the widget id and the command parameter */
3384 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3385 #define get_cmd_cache_nid(key) ((key) & 0xff)
3386 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3389 * snd_hda_codec_write_cache - send a single command with caching
3390 * @codec: the HDA codec
3391 * @nid: NID to send the command
3392 * @direct: direct flag
3393 * @verb: the verb to send
3394 * @parm: the parameter for the verb
3396 * Send a single command without waiting for response.
3398 * Returns 0 if successful, or a negative error code.
3400 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3401 int direct
, unsigned int verb
, unsigned int parm
)
3403 int err
= snd_hda_codec_write(codec
, nid
, direct
, verb
, parm
);
3404 struct hda_cache_head
*c
;
3409 /* parm may contain the verb stuff for get/set amp */
3410 verb
= verb
| (parm
>> 8);
3412 key
= build_cmd_cache_key(nid
, verb
);
3413 mutex_lock(&codec
->bus
->cmd_mutex
);
3414 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3417 mutex_unlock(&codec
->bus
->cmd_mutex
);
3420 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
3423 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3424 * @codec: the HDA codec
3425 * @nid: NID to send the command
3426 * @direct: direct flag
3427 * @verb: the verb to send
3428 * @parm: the parameter for the verb
3430 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3431 * command if the parameter is already identical with the cached value.
3432 * If not, it sends the command and refreshes the cache.
3434 * Returns 0 if successful, or a negative error code.
3436 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3437 int direct
, unsigned int verb
, unsigned int parm
)
3439 struct hda_cache_head
*c
;
3442 /* parm may contain the verb stuff for get/set amp */
3443 verb
= verb
| (parm
>> 8);
3445 key
= build_cmd_cache_key(nid
, verb
);
3446 mutex_lock(&codec
->bus
->cmd_mutex
);
3447 c
= get_hash(&codec
->cmd_cache
, key
);
3448 if (c
&& c
->val
== parm
) {
3449 mutex_unlock(&codec
->bus
->cmd_mutex
);
3452 mutex_unlock(&codec
->bus
->cmd_mutex
);
3453 return snd_hda_codec_write_cache(codec
, nid
, direct
, verb
, parm
);
3455 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache
);
3458 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3459 * @codec: HD-audio codec
3461 * Execute all verbs recorded in the command caches to resume.
3463 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3465 struct hda_cache_head
*buffer
= codec
->cmd_cache
.buf
.list
;
3468 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++, buffer
++) {
3469 u32 key
= buffer
->key
;
3472 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3473 get_cmd_cache_cmd(key
), buffer
->val
);
3476 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
3479 * snd_hda_sequence_write_cache - sequence writes with caching
3480 * @codec: the HDA codec
3481 * @seq: VERB array to send
3483 * Send the commands sequentially from the given array.
3484 * Thte commands are recorded on cache for power-save and resume.
3485 * The array must be terminated with NID=0.
3487 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3488 const struct hda_verb
*seq
)
3490 for (; seq
->nid
; seq
++)
3491 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3494 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
3495 #endif /* CONFIG_PM */
3497 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
3498 unsigned int power_state
,
3499 bool eapd_workaround
)
3501 hda_nid_t nid
= codec
->start_nid
;
3504 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3505 unsigned int wcaps
= get_wcaps(codec
, nid
);
3506 if (!(wcaps
& AC_WCAP_POWER
))
3508 /* don't power down the widget if it controls eapd and
3509 * EAPD_BTLENABLE is set.
3511 if (eapd_workaround
&& power_state
== AC_PWRST_D3
&&
3512 get_wcaps_type(wcaps
) == AC_WID_PIN
&&
3513 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
3514 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
3515 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3519 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
3523 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all
);
3526 * supported power states check
3528 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
, hda_nid_t fg
,
3529 unsigned int power_state
)
3531 int sup
= snd_hda_param_read(codec
, fg
, AC_PAR_POWER_STATE
);
3535 if (sup
& power_state
)
3542 * wait until the state is reached, returns the current state
3544 static unsigned int hda_sync_power_state(struct hda_codec
*codec
,
3546 unsigned int power_state
)
3548 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
3549 unsigned int state
, actual_state
;
3552 state
= snd_hda_codec_read(codec
, fg
, 0,
3553 AC_VERB_GET_POWER_STATE
, 0);
3554 if (state
& AC_PWRST_ERROR
)
3556 actual_state
= (state
>> 4) & 0x0f;
3557 if (actual_state
== power_state
)
3559 if (time_after_eq(jiffies
, end_time
))
3561 /* wait until the codec reachs to the target state */
3568 * set power state of the codec, and return the power state
3570 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
3571 unsigned int power_state
)
3573 hda_nid_t fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
3577 /* this delay seems necessary to avoid click noise at power-down */
3578 if (power_state
== AC_PWRST_D3
) {
3579 /* transition time less than 10ms for power down */
3580 msleep(codec
->epss
? 10 : 100);
3583 /* repeat power states setting at most 10 times*/
3584 for (count
= 0; count
< 10; count
++) {
3585 if (codec
->patch_ops
.set_power_state
)
3586 codec
->patch_ops
.set_power_state(codec
, fg
,
3589 snd_hda_codec_read(codec
, fg
, 0,
3590 AC_VERB_SET_POWER_STATE
,
3592 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
,
3595 state
= hda_sync_power_state(codec
, fg
, power_state
);
3596 if (!(state
& AC_PWRST_ERROR
))
3603 #ifdef CONFIG_SND_HDA_HWDEP
3604 /* execute additional init verbs */
3605 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3607 if (codec
->init_verbs
.list
)
3608 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3611 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3616 * call suspend and power-down; used both from PM and power-save
3617 * this function returns the power state in the end
3619 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
, bool in_wq
)
3625 if (codec
->patch_ops
.suspend
)
3626 codec
->patch_ops
.suspend(codec
);
3627 hda_cleanup_all_streams(codec
);
3628 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
3629 /* Cancel delayed work if we aren't currently running from it. */
3631 cancel_delayed_work_sync(&codec
->power_work
);
3632 spin_lock(&codec
->power_lock
);
3633 snd_hda_update_power_acct(codec
);
3634 trace_hda_power_down(codec
);
3635 codec
->power_on
= 0;
3636 codec
->power_transition
= 0;
3637 codec
->power_jiffies
= jiffies
;
3638 spin_unlock(&codec
->power_lock
);
3644 * kick up codec; used both from PM and power-save
3646 static void hda_call_codec_resume(struct hda_codec
*codec
)
3650 /* set as if powered on for avoiding re-entering the resume
3651 * in the resume / power-save sequence
3653 hda_keep_power_on(codec
);
3654 hda_set_power_state(codec
, AC_PWRST_D0
);
3655 restore_shutup_pins(codec
);
3656 hda_exec_init_verbs(codec
);
3657 snd_hda_jack_set_dirty_all(codec
);
3658 if (codec
->patch_ops
.resume
)
3659 codec
->patch_ops
.resume(codec
);
3661 if (codec
->patch_ops
.init
)
3662 codec
->patch_ops
.init(codec
);
3663 snd_hda_codec_resume_amp(codec
);
3664 snd_hda_codec_resume_cache(codec
);
3667 if (codec
->jackpoll_interval
)
3668 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3670 snd_hda_jack_report_sync(codec
);
3673 snd_hda_power_down(codec
); /* flag down before returning */
3675 #endif /* CONFIG_PM */
3679 * snd_hda_build_controls - build mixer controls
3682 * Creates mixer controls for each codec included in the bus.
3684 * Returns 0 if successful, otherwise a negative error code.
3686 int snd_hda_build_controls(struct hda_bus
*bus
)
3688 struct hda_codec
*codec
;
3690 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3691 int err
= snd_hda_codec_build_controls(codec
);
3693 printk(KERN_ERR
"hda_codec: cannot build controls "
3694 "for #%d (error %d)\n", codec
->addr
, err
);
3695 err
= snd_hda_codec_reset(codec
);
3698 "hda_codec: cannot revert codec\n");
3705 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
3708 * add standard channel maps if not specified
3710 static int add_std_chmaps(struct hda_codec
*codec
)
3714 for (i
= 0; i
< codec
->num_pcms
; i
++) {
3715 for (str
= 0; str
< 2; str
++) {
3716 struct snd_pcm
*pcm
= codec
->pcm_info
[i
].pcm
;
3717 struct hda_pcm_stream
*hinfo
=
3718 &codec
->pcm_info
[i
].stream
[str
];
3719 struct snd_pcm_chmap
*chmap
;
3720 const struct snd_pcm_chmap_elem
*elem
;
3722 if (codec
->pcm_info
[i
].own_chmap
)
3724 if (!pcm
|| !hinfo
->substreams
)
3726 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
3727 err
= snd_pcm_add_chmap_ctls(pcm
, str
, elem
,
3728 hinfo
->channels_max
,
3732 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
3738 /* default channel maps for 2.1 speakers;
3739 * since HD-audio supports only stereo, odd number channels are omitted
3741 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
3743 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
3745 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
3746 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
3749 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
3751 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
3754 hda_exec_init_verbs(codec
);
3755 /* continue to initialize... */
3756 if (codec
->patch_ops
.init
)
3757 err
= codec
->patch_ops
.init(codec
);
3758 if (!err
&& codec
->patch_ops
.build_controls
)
3759 err
= codec
->patch_ops
.build_controls(codec
);
3763 /* we create chmaps here instead of build_pcms */
3764 err
= add_std_chmaps(codec
);
3768 if (codec
->jackpoll_interval
)
3769 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3771 snd_hda_jack_report_sync(codec
); /* call at the last init point */
3778 struct hda_rate_tbl
{
3780 unsigned int alsa_bits
;
3781 unsigned int hda_fmt
;
3784 /* rate = base * mult / div */
3785 #define HDA_RATE(base, mult, div) \
3786 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3787 (((div) - 1) << AC_FMT_DIV_SHIFT))
3789 static struct hda_rate_tbl rate_bits
[] = {
3790 /* rate in Hz, ALSA rate bitmask, HDA format value */
3792 /* autodetected value used in snd_hda_query_supported_pcm */
3793 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
3794 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
3795 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
3796 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
3797 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
3798 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
3799 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
3800 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
3801 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
3802 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
3803 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
3804 #define AC_PAR_PCM_RATE_BITS 11
3805 /* up to bits 10, 384kHZ isn't supported properly */
3807 /* not autodetected value */
3808 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
3810 { 0 } /* terminator */
3814 * snd_hda_calc_stream_format - calculate format bitset
3815 * @rate: the sample rate
3816 * @channels: the number of channels
3817 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3818 * @maxbps: the max. bps
3820 * Calculate the format bitset from the given rate, channels and th PCM format.
3822 * Return zero if invalid.
3824 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
3825 unsigned int channels
,
3826 unsigned int format
,
3827 unsigned int maxbps
,
3828 unsigned short spdif_ctls
)
3831 unsigned int val
= 0;
3833 for (i
= 0; rate_bits
[i
].hz
; i
++)
3834 if (rate_bits
[i
].hz
== rate
) {
3835 val
= rate_bits
[i
].hda_fmt
;
3838 if (!rate_bits
[i
].hz
) {
3839 snd_printdd("invalid rate %d\n", rate
);
3843 if (channels
== 0 || channels
> 8) {
3844 snd_printdd("invalid channels %d\n", channels
);
3847 val
|= channels
- 1;
3849 switch (snd_pcm_format_width(format
)) {
3851 val
|= AC_FMT_BITS_8
;
3854 val
|= AC_FMT_BITS_16
;
3859 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
3860 val
|= AC_FMT_BITS_32
;
3861 else if (maxbps
>= 24)
3862 val
|= AC_FMT_BITS_24
;
3864 val
|= AC_FMT_BITS_20
;
3867 snd_printdd("invalid format width %d\n",
3868 snd_pcm_format_width(format
));
3872 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
3873 val
|= AC_FMT_TYPE_NON_PCM
;
3877 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
3879 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
,
3882 unsigned int val
= 0;
3883 if (nid
!= codec
->afg
&&
3884 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
3885 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
3886 if (!val
|| val
== -1)
3887 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
3888 if (!val
|| val
== -1)
3893 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3895 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARPCM_KEY(nid
),
3899 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
,
3902 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
3903 if (!streams
|| streams
== -1)
3904 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
3905 if (!streams
|| streams
== -1)
3910 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3912 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARSTR_KEY(nid
),
3917 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3918 * @codec: the HDA codec
3919 * @nid: NID to query
3920 * @ratesp: the pointer to store the detected rate bitflags
3921 * @formatsp: the pointer to store the detected formats
3922 * @bpsp: the pointer to store the detected format widths
3924 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3925 * or @bsps argument is ignored.
3927 * Returns 0 if successful, otherwise a negative error code.
3929 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
3930 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
3932 unsigned int i
, val
, wcaps
;
3934 wcaps
= get_wcaps(codec
, nid
);
3935 val
= query_pcm_param(codec
, nid
);
3939 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
3941 rates
|= rate_bits
[i
].alsa_bits
;
3944 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
3945 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3947 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
3953 if (formatsp
|| bpsp
) {
3955 unsigned int streams
, bps
;
3957 streams
= query_stream_param(codec
, nid
);
3962 if (streams
& AC_SUPFMT_PCM
) {
3963 if (val
& AC_SUPPCM_BITS_8
) {
3964 formats
|= SNDRV_PCM_FMTBIT_U8
;
3967 if (val
& AC_SUPPCM_BITS_16
) {
3968 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
3971 if (wcaps
& AC_WCAP_DIGITAL
) {
3972 if (val
& AC_SUPPCM_BITS_32
)
3973 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
3974 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
3975 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3976 if (val
& AC_SUPPCM_BITS_24
)
3978 else if (val
& AC_SUPPCM_BITS_20
)
3980 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
3981 AC_SUPPCM_BITS_32
)) {
3982 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3983 if (val
& AC_SUPPCM_BITS_32
)
3985 else if (val
& AC_SUPPCM_BITS_24
)
3987 else if (val
& AC_SUPPCM_BITS_20
)
3991 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
3992 if (streams
& AC_SUPFMT_FLOAT32
) {
3993 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
3998 if (streams
== AC_SUPFMT_AC3
) {
3999 /* should be exclusive */
4000 /* temporary hack: we have still no proper support
4001 * for the direct AC3 stream...
4003 formats
|= SNDRV_PCM_FMTBIT_U8
;
4007 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
4008 "(nid=0x%x, val=0x%x, ovrd=%i, "
4011 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
4016 *formatsp
= formats
;
4023 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm
);
4026 * snd_hda_is_supported_format - Check the validity of the format
4027 * @codec: HD-audio codec
4028 * @nid: NID to check
4029 * @format: the HD-audio format value to check
4031 * Check whether the given node supports the format value.
4033 * Returns 1 if supported, 0 if not.
4035 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
4036 unsigned int format
)
4039 unsigned int val
= 0, rate
, stream
;
4041 val
= query_pcm_param(codec
, nid
);
4045 rate
= format
& 0xff00;
4046 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
4047 if (rate_bits
[i
].hda_fmt
== rate
) {
4052 if (i
>= AC_PAR_PCM_RATE_BITS
)
4055 stream
= query_stream_param(codec
, nid
);
4059 if (stream
& AC_SUPFMT_PCM
) {
4060 switch (format
& 0xf0) {
4062 if (!(val
& AC_SUPPCM_BITS_8
))
4066 if (!(val
& AC_SUPPCM_BITS_16
))
4070 if (!(val
& AC_SUPPCM_BITS_20
))
4074 if (!(val
& AC_SUPPCM_BITS_24
))
4078 if (!(val
& AC_SUPPCM_BITS_32
))
4085 /* FIXME: check for float32 and AC3? */
4090 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
4095 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
4096 struct hda_codec
*codec
,
4097 struct snd_pcm_substream
*substream
)
4102 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
4103 struct hda_codec
*codec
,
4104 unsigned int stream_tag
,
4105 unsigned int format
,
4106 struct snd_pcm_substream
*substream
)
4108 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
4112 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
4113 struct hda_codec
*codec
,
4114 struct snd_pcm_substream
*substream
)
4116 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
4120 static int set_pcm_default_values(struct hda_codec
*codec
,
4121 struct hda_pcm_stream
*info
)
4125 /* query support PCM information from the given NID */
4126 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
4127 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
4128 info
->rates
? NULL
: &info
->rates
,
4129 info
->formats
? NULL
: &info
->formats
,
4130 info
->maxbps
? NULL
: &info
->maxbps
);
4134 if (info
->ops
.open
== NULL
)
4135 info
->ops
.open
= hda_pcm_default_open_close
;
4136 if (info
->ops
.close
== NULL
)
4137 info
->ops
.close
= hda_pcm_default_open_close
;
4138 if (info
->ops
.prepare
== NULL
) {
4139 if (snd_BUG_ON(!info
->nid
))
4141 info
->ops
.prepare
= hda_pcm_default_prepare
;
4143 if (info
->ops
.cleanup
== NULL
) {
4144 if (snd_BUG_ON(!info
->nid
))
4146 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
4152 * codec prepare/cleanup entries
4154 int snd_hda_codec_prepare(struct hda_codec
*codec
,
4155 struct hda_pcm_stream
*hinfo
,
4156 unsigned int stream
,
4157 unsigned int format
,
4158 struct snd_pcm_substream
*substream
)
4161 mutex_lock(&codec
->bus
->prepare_mutex
);
4162 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
, substream
);
4164 purify_inactive_streams(codec
);
4165 mutex_unlock(&codec
->bus
->prepare_mutex
);
4168 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare
);
4170 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
4171 struct hda_pcm_stream
*hinfo
,
4172 struct snd_pcm_substream
*substream
)
4174 mutex_lock(&codec
->bus
->prepare_mutex
);
4175 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
4176 mutex_unlock(&codec
->bus
->prepare_mutex
);
4178 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup
);
4181 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
4182 "Audio", "SPDIF", "HDMI", "Modem"
4186 * get the empty PCM device number to assign
4188 * note the max device number is limited by HDA_MAX_PCMS, currently 10
4190 static int get_empty_pcm_device(struct hda_bus
*bus
, int type
)
4192 /* audio device indices; not linear to keep compatibility */
4193 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
4194 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
4195 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
4196 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
4197 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
4201 if (type
>= HDA_PCM_NTYPES
) {
4202 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
4206 for (i
= 0; audio_idx
[type
][i
] >= 0 ; i
++)
4207 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
4208 return audio_idx
[type
][i
];
4210 /* non-fixed slots starting from 10 */
4211 for (i
= 10; i
< 32; i
++) {
4212 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
4216 snd_printk(KERN_WARNING
"Too many %s devices\n",
4217 snd_hda_pcm_type_name
[type
]);
4222 * attach a new PCM stream
4224 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
4226 struct hda_bus
*bus
= codec
->bus
;
4227 struct hda_pcm_stream
*info
;
4230 if (snd_BUG_ON(!pcm
->name
))
4232 for (stream
= 0; stream
< 2; stream
++) {
4233 info
= &pcm
->stream
[stream
];
4234 if (info
->substreams
) {
4235 err
= set_pcm_default_values(codec
, info
);
4240 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
4243 /* assign all PCMs of the given codec */
4244 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
4249 if (!codec
->num_pcms
) {
4250 if (!codec
->patch_ops
.build_pcms
)
4252 err
= codec
->patch_ops
.build_pcms(codec
);
4254 printk(KERN_ERR
"hda_codec: cannot build PCMs"
4255 "for #%d (error %d)\n", codec
->addr
, err
);
4256 err
= snd_hda_codec_reset(codec
);
4259 "hda_codec: cannot revert codec\n");
4264 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
4265 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
4268 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
4269 continue; /* no substreams assigned */
4272 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
4274 continue; /* no fatal error */
4276 err
= snd_hda_attach_pcm(codec
, cpcm
);
4278 printk(KERN_ERR
"hda_codec: cannot attach "
4279 "PCM stream %d for codec #%d\n",
4281 continue; /* no fatal error */
4289 * snd_hda_build_pcms - build PCM information
4292 * Create PCM information for each codec included in the bus.
4294 * The build_pcms codec patch is requested to set up codec->num_pcms and
4295 * codec->pcm_info properly. The array is referred by the top-level driver
4296 * to create its PCM instances.
4297 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4300 * At least, substreams, channels_min and channels_max must be filled for
4301 * each stream. substreams = 0 indicates that the stream doesn't exist.
4302 * When rates and/or formats are zero, the supported values are queried
4303 * from the given nid. The nid is used also by the default ops.prepare
4304 * and ops.cleanup callbacks.
4306 * The driver needs to call ops.open in its open callback. Similarly,
4307 * ops.close is supposed to be called in the close callback.
4308 * ops.prepare should be called in the prepare or hw_params callback
4309 * with the proper parameters for set up.
4310 * ops.cleanup should be called in hw_free for clean up of streams.
4312 * This function returns 0 if successful, or a negative error code.
4314 int snd_hda_build_pcms(struct hda_bus
*bus
)
4316 struct hda_codec
*codec
;
4318 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4319 int err
= snd_hda_codec_build_pcms(codec
);
4325 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
4328 * snd_hda_check_board_config - compare the current codec with the config table
4329 * @codec: the HDA codec
4330 * @num_configs: number of config enums
4331 * @models: array of model name strings
4332 * @tbl: configuration table, terminated by null entries
4334 * Compares the modelname or PCI subsystem id of the current codec with the
4335 * given configuration table. If a matching entry is found, returns its
4336 * config value (supposed to be 0 or positive).
4338 * If no entries are matching, the function returns a negative value.
4340 int snd_hda_check_board_config(struct hda_codec
*codec
,
4341 int num_configs
, const char * const *models
,
4342 const struct snd_pci_quirk
*tbl
)
4344 if (codec
->modelname
&& models
) {
4346 for (i
= 0; i
< num_configs
; i
++) {
4348 !strcmp(codec
->modelname
, models
[i
])) {
4349 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
4350 "selected\n", models
[i
]);
4356 if (!codec
->bus
->pci
|| !tbl
)
4359 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
4362 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4363 #ifdef CONFIG_SND_DEBUG_VERBOSE
4365 const char *model
= NULL
;
4367 model
= models
[tbl
->value
];
4369 sprintf(tmp
, "#%d", tbl
->value
);
4372 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4373 "for config %x:%x (%s)\n",
4374 model
, tbl
->subvendor
, tbl
->subdevice
,
4375 (tbl
->name
? tbl
->name
: "Unknown device"));
4381 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
4384 * snd_hda_check_board_codec_sid_config - compare the current codec
4385 subsystem ID with the
4388 This is important for Gateway notebooks with SB450 HDA Audio
4389 where the vendor ID of the PCI device is:
4390 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4391 and the vendor/subvendor are found only at the codec.
4393 * @codec: the HDA codec
4394 * @num_configs: number of config enums
4395 * @models: array of model name strings
4396 * @tbl: configuration table, terminated by null entries
4398 * Compares the modelname or PCI subsystem id of the current codec with the
4399 * given configuration table. If a matching entry is found, returns its
4400 * config value (supposed to be 0 or positive).
4402 * If no entries are matching, the function returns a negative value.
4404 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
4405 int num_configs
, const char * const *models
,
4406 const struct snd_pci_quirk
*tbl
)
4408 const struct snd_pci_quirk
*q
;
4410 /* Search for codec ID */
4411 for (q
= tbl
; q
->subvendor
; q
++) {
4412 unsigned int mask
= 0xffff0000 | q
->subdevice_mask
;
4413 unsigned int id
= (q
->subdevice
| (q
->subvendor
<< 16)) & mask
;
4414 if ((codec
->subsystem_id
& mask
) == id
)
4423 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4424 #ifdef CONFIG_SND_DEBUG_VERBOSE
4426 const char *model
= NULL
;
4428 model
= models
[tbl
->value
];
4430 sprintf(tmp
, "#%d", tbl
->value
);
4433 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4434 "for config %x:%x (%s)\n",
4435 model
, tbl
->subvendor
, tbl
->subdevice
,
4436 (tbl
->name
? tbl
->name
: "Unknown device"));
4442 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
4445 * snd_hda_add_new_ctls - create controls from the array
4446 * @codec: the HDA codec
4447 * @knew: the array of struct snd_kcontrol_new
4449 * This helper function creates and add new controls in the given array.
4450 * The array must be terminated with an empty entry as terminator.
4452 * Returns 0 if successful, or a negative error code.
4454 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4455 const struct snd_kcontrol_new
*knew
)
4459 for (; knew
->name
; knew
++) {
4460 struct snd_kcontrol
*kctl
;
4461 int addr
= 0, idx
= 0;
4462 if (knew
->iface
== -1) /* skip this codec private value */
4465 kctl
= snd_ctl_new1(knew
, codec
);
4469 kctl
->id
.device
= addr
;
4471 kctl
->id
.index
= idx
;
4472 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4475 /* try first with another device index corresponding to
4476 * the codec addr; if it still fails (or it's the
4477 * primary codec), then try another control index
4479 if (!addr
&& codec
->addr
)
4481 else if (!idx
&& !knew
->index
) {
4482 idx
= find_empty_mixer_ctl_idx(codec
,
4492 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
4495 static void hda_power_work(struct work_struct
*work
)
4497 struct hda_codec
*codec
=
4498 container_of(work
, struct hda_codec
, power_work
.work
);
4499 struct hda_bus
*bus
= codec
->bus
;
4502 spin_lock(&codec
->power_lock
);
4503 if (codec
->power_transition
> 0) { /* during power-up sequence? */
4504 spin_unlock(&codec
->power_lock
);
4507 if (!codec
->power_on
|| codec
->power_count
) {
4508 codec
->power_transition
= 0;
4509 spin_unlock(&codec
->power_lock
);
4512 spin_unlock(&codec
->power_lock
);
4514 state
= hda_call_codec_suspend(codec
, true);
4515 codec
->pm_down_notified
= 0;
4516 if (!bus
->power_keep_link_on
&& (state
& AC_PWRST_CLK_STOP_OK
)) {
4517 codec
->pm_down_notified
= 1;
4518 hda_call_pm_notify(bus
, false);
4522 static void hda_keep_power_on(struct hda_codec
*codec
)
4524 spin_lock(&codec
->power_lock
);
4525 codec
->power_count
++;
4526 codec
->power_on
= 1;
4527 codec
->power_jiffies
= jiffies
;
4528 spin_unlock(&codec
->power_lock
);
4531 /* update the power on/off account with the current jiffies */
4532 void snd_hda_update_power_acct(struct hda_codec
*codec
)
4534 unsigned long delta
= jiffies
- codec
->power_jiffies
;
4535 if (codec
->power_on
)
4536 codec
->power_on_acct
+= delta
;
4538 codec
->power_off_acct
+= delta
;
4539 codec
->power_jiffies
+= delta
;
4542 /* Transition to powered up, if wait_power_down then wait for a pending
4543 * transition to D3 to complete. A pending D3 transition is indicated
4544 * with power_transition == -1. */
4545 /* call this with codec->power_lock held! */
4546 static void __snd_hda_power_up(struct hda_codec
*codec
, bool wait_power_down
)
4548 struct hda_bus
*bus
= codec
->bus
;
4550 /* Return if power_on or transitioning to power_on, unless currently
4552 if ((codec
->power_on
|| codec
->power_transition
> 0) &&
4553 !(wait_power_down
&& codec
->power_transition
< 0))
4555 spin_unlock(&codec
->power_lock
);
4557 cancel_delayed_work_sync(&codec
->power_work
);
4559 spin_lock(&codec
->power_lock
);
4560 /* If the power down delayed work was cancelled above before starting,
4561 * then there is no need to go through power up here.
4563 if (codec
->power_on
) {
4564 if (codec
->power_transition
< 0)
4565 codec
->power_transition
= 0;
4569 trace_hda_power_up(codec
);
4570 snd_hda_update_power_acct(codec
);
4571 codec
->power_on
= 1;
4572 codec
->power_jiffies
= jiffies
;
4573 codec
->power_transition
= 1; /* avoid reentrance */
4574 spin_unlock(&codec
->power_lock
);
4576 if (codec
->pm_down_notified
) {
4577 codec
->pm_down_notified
= 0;
4578 hda_call_pm_notify(bus
, true);
4581 hda_call_codec_resume(codec
);
4583 spin_lock(&codec
->power_lock
);
4584 codec
->power_transition
= 0;
4587 #define power_save(codec) \
4588 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4590 /* Transition to powered down */
4591 static void __snd_hda_power_down(struct hda_codec
*codec
)
4593 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
4596 if (power_save(codec
)) {
4597 codec
->power_transition
= -1; /* avoid reentrance */
4598 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
4599 msecs_to_jiffies(power_save(codec
) * 1000));
4604 * snd_hda_power_save - Power-up/down/sync the codec
4605 * @codec: HD-audio codec
4606 * @delta: the counter delta to change
4608 * Change the power-up counter via @delta, and power up or down the hardware
4609 * appropriately. For the power-down, queue to the delayed action.
4610 * Passing zero to @delta means to synchronize the power state.
4612 void snd_hda_power_save(struct hda_codec
*codec
, int delta
, bool d3wait
)
4614 spin_lock(&codec
->power_lock
);
4615 codec
->power_count
+= delta
;
4616 trace_hda_power_count(codec
);
4618 __snd_hda_power_up(codec
, d3wait
);
4620 __snd_hda_power_down(codec
);
4621 spin_unlock(&codec
->power_lock
);
4623 EXPORT_SYMBOL_HDA(snd_hda_power_save
);
4626 * snd_hda_check_amp_list_power - Check the amp list and update the power
4627 * @codec: HD-audio codec
4628 * @check: the object containing an AMP list and the status
4629 * @nid: NID to check / update
4631 * Check whether the given NID is in the amp list. If it's in the list,
4632 * check the current AMP status, and update the the power-status according
4633 * to the mute status.
4635 * This function is supposed to be set or called from the check_power_status
4638 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4639 struct hda_loopback_check
*check
,
4642 const struct hda_amp_list
*p
;
4645 if (!check
->amplist
)
4647 for (p
= check
->amplist
; p
->nid
; p
++) {
4652 return 0; /* nothing changed */
4654 for (p
= check
->amplist
; p
->nid
; p
++) {
4655 for (ch
= 0; ch
< 2; ch
++) {
4656 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
4658 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
4659 if (!check
->power_on
) {
4660 check
->power_on
= 1;
4661 snd_hda_power_up(codec
);
4667 if (check
->power_on
) {
4668 check
->power_on
= 0;
4669 snd_hda_power_down(codec
);
4673 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
4677 * Channel mode helper
4681 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4683 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
4684 struct snd_ctl_elem_info
*uinfo
,
4685 const struct hda_channel_mode
*chmode
,
4688 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4690 uinfo
->value
.enumerated
.items
= num_chmodes
;
4691 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
4692 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
4693 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
4694 chmode
[uinfo
->value
.enumerated
.item
].channels
);
4697 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
4700 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4702 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
4703 struct snd_ctl_elem_value
*ucontrol
,
4704 const struct hda_channel_mode
*chmode
,
4710 for (i
= 0; i
< num_chmodes
; i
++) {
4711 if (max_channels
== chmode
[i
].channels
) {
4712 ucontrol
->value
.enumerated
.item
[0] = i
;
4718 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
4721 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4723 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
4724 struct snd_ctl_elem_value
*ucontrol
,
4725 const struct hda_channel_mode
*chmode
,
4731 mode
= ucontrol
->value
.enumerated
.item
[0];
4732 if (mode
>= num_chmodes
)
4734 if (*max_channelsp
== chmode
[mode
].channels
)
4736 /* change the current channel setting */
4737 *max_channelsp
= chmode
[mode
].channels
;
4738 if (chmode
[mode
].sequence
)
4739 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
4742 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
4749 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4751 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
4752 struct snd_ctl_elem_info
*uinfo
)
4756 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4758 uinfo
->value
.enumerated
.items
= imux
->num_items
;
4759 if (!imux
->num_items
)
4761 index
= uinfo
->value
.enumerated
.item
;
4762 if (index
>= imux
->num_items
)
4763 index
= imux
->num_items
- 1;
4764 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
4767 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
4770 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4772 int snd_hda_input_mux_put(struct hda_codec
*codec
,
4773 const struct hda_input_mux
*imux
,
4774 struct snd_ctl_elem_value
*ucontrol
,
4776 unsigned int *cur_val
)
4780 if (!imux
->num_items
)
4782 idx
= ucontrol
->value
.enumerated
.item
[0];
4783 if (idx
>= imux
->num_items
)
4784 idx
= imux
->num_items
- 1;
4785 if (*cur_val
== idx
)
4787 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
4788 imux
->items
[idx
].index
);
4792 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
4796 * process kcontrol info callback of a simple string enum array
4797 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4799 int snd_hda_enum_helper_info(struct snd_kcontrol
*kcontrol
,
4800 struct snd_ctl_elem_info
*uinfo
,
4801 int num_items
, const char * const *texts
)
4803 static const char * const texts_default
[] = {
4804 "Disabled", "Enabled"
4807 if (!texts
|| !num_items
) {
4809 texts
= texts_default
;
4812 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4814 uinfo
->value
.enumerated
.items
= num_items
;
4815 if (uinfo
->value
.enumerated
.item
>= uinfo
->value
.enumerated
.items
)
4816 uinfo
->value
.enumerated
.item
= uinfo
->value
.enumerated
.items
- 1;
4817 strcpy(uinfo
->value
.enumerated
.name
,
4818 texts
[uinfo
->value
.enumerated
.item
]);
4821 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info
);
4824 * Multi-channel / digital-out PCM helper functions
4827 /* setup SPDIF output stream */
4828 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
4829 unsigned int stream_tag
, unsigned int format
)
4831 struct hda_spdif_out
*spdif
;
4832 unsigned int curr_fmt
;
4835 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
4836 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
4837 AC_VERB_GET_STREAM_FORMAT
, 0);
4838 reset
= codec
->spdif_status_reset
&&
4839 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
4842 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4845 set_dig_out_convert(codec
, nid
,
4846 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
4848 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
4849 if (codec
->slave_dig_outs
) {
4851 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4852 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
4855 /* turn on again (if needed) */
4857 set_dig_out_convert(codec
, nid
,
4858 spdif
->ctls
& 0xff, -1);
4861 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
4863 snd_hda_codec_cleanup_stream(codec
, nid
);
4864 if (codec
->slave_dig_outs
) {
4866 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4867 snd_hda_codec_cleanup_stream(codec
, *d
);
4872 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4873 * @bus: HD-audio bus
4875 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
4877 struct hda_codec
*codec
;
4881 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4882 if (hda_codec_is_power_on(codec
) &&
4883 codec
->patch_ops
.reboot_notify
)
4884 codec
->patch_ops
.reboot_notify(codec
);
4887 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify
);
4890 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4892 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
4893 struct hda_multi_out
*mout
)
4895 mutex_lock(&codec
->spdif_mutex
);
4896 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
4897 /* already opened as analog dup; reset it once */
4898 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4899 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
4900 mutex_unlock(&codec
->spdif_mutex
);
4903 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
4906 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4908 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
4909 struct hda_multi_out
*mout
,
4910 unsigned int stream_tag
,
4911 unsigned int format
,
4912 struct snd_pcm_substream
*substream
)
4914 mutex_lock(&codec
->spdif_mutex
);
4915 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
4916 mutex_unlock(&codec
->spdif_mutex
);
4919 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
4922 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4924 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
4925 struct hda_multi_out
*mout
)
4927 mutex_lock(&codec
->spdif_mutex
);
4928 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4929 mutex_unlock(&codec
->spdif_mutex
);
4932 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
4935 * snd_hda_multi_out_dig_close - release the digital out stream
4937 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
4938 struct hda_multi_out
*mout
)
4940 mutex_lock(&codec
->spdif_mutex
);
4941 mout
->dig_out_used
= 0;
4942 mutex_unlock(&codec
->spdif_mutex
);
4945 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
4948 * snd_hda_multi_out_analog_open - open analog outputs
4950 * Open analog outputs and set up the hw-constraints.
4951 * If the digital outputs can be opened as slave, open the digital
4954 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
4955 struct hda_multi_out
*mout
,
4956 struct snd_pcm_substream
*substream
,
4957 struct hda_pcm_stream
*hinfo
)
4959 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
4960 runtime
->hw
.channels_max
= mout
->max_channels
;
4961 if (mout
->dig_out_nid
) {
4962 if (!mout
->analog_rates
) {
4963 mout
->analog_rates
= hinfo
->rates
;
4964 mout
->analog_formats
= hinfo
->formats
;
4965 mout
->analog_maxbps
= hinfo
->maxbps
;
4967 runtime
->hw
.rates
= mout
->analog_rates
;
4968 runtime
->hw
.formats
= mout
->analog_formats
;
4969 hinfo
->maxbps
= mout
->analog_maxbps
;
4971 if (!mout
->spdif_rates
) {
4972 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
4974 &mout
->spdif_formats
,
4975 &mout
->spdif_maxbps
);
4977 mutex_lock(&codec
->spdif_mutex
);
4978 if (mout
->share_spdif
) {
4979 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
4980 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
4981 runtime
->hw
.rates
&= mout
->spdif_rates
;
4982 runtime
->hw
.formats
&= mout
->spdif_formats
;
4983 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
4984 hinfo
->maxbps
= mout
->spdif_maxbps
;
4986 mout
->share_spdif
= 0;
4987 /* FIXME: need notify? */
4990 mutex_unlock(&codec
->spdif_mutex
);
4992 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
4993 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
4995 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
4998 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5000 * Set up the i/o for analog out.
5001 * When the digital out is available, copy the front out to digital out, too.
5003 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
5004 struct hda_multi_out
*mout
,
5005 unsigned int stream_tag
,
5006 unsigned int format
,
5007 struct snd_pcm_substream
*substream
)
5009 const hda_nid_t
*nids
= mout
->dac_nids
;
5010 int chs
= substream
->runtime
->channels
;
5011 struct hda_spdif_out
*spdif
;
5014 mutex_lock(&codec
->spdif_mutex
);
5015 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
5016 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
5017 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
5019 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
5021 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
5022 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
5023 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
5024 stream_tag
, format
);
5026 mout
->dig_out_used
= 0;
5027 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5030 mutex_unlock(&codec
->spdif_mutex
);
5033 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
5035 if (!mout
->no_share_stream
&&
5036 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
5037 /* headphone out will just decode front left/right (stereo) */
5038 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
5040 /* extra outputs copied from front */
5041 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5042 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
5043 snd_hda_codec_setup_stream(codec
,
5044 mout
->hp_out_nid
[i
],
5045 stream_tag
, 0, format
);
5046 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5047 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
5048 snd_hda_codec_setup_stream(codec
,
5049 mout
->extra_out_nid
[i
],
5050 stream_tag
, 0, format
);
5053 for (i
= 1; i
< mout
->num_dacs
; i
++) {
5054 if (chs
>= (i
+ 1) * 2) /* independent out */
5055 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5057 else if (!mout
->no_share_stream
) /* copy front */
5058 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5063 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
5066 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5068 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
5069 struct hda_multi_out
*mout
)
5071 const hda_nid_t
*nids
= mout
->dac_nids
;
5074 for (i
= 0; i
< mout
->num_dacs
; i
++)
5075 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
5077 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
5078 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5079 if (mout
->hp_out_nid
[i
])
5080 snd_hda_codec_cleanup_stream(codec
,
5081 mout
->hp_out_nid
[i
]);
5082 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5083 if (mout
->extra_out_nid
[i
])
5084 snd_hda_codec_cleanup_stream(codec
,
5085 mout
->extra_out_nid
[i
]);
5086 mutex_lock(&codec
->spdif_mutex
);
5087 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
5088 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5089 mout
->dig_out_used
= 0;
5091 mutex_unlock(&codec
->spdif_mutex
);
5094 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
5097 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5099 * Guess the suitable VREF pin bits to be set as the pin-control value.
5100 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5102 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
5104 unsigned int pincap
;
5105 unsigned int oldval
;
5106 oldval
= snd_hda_codec_read(codec
, pin
, 0,
5107 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5108 pincap
= snd_hda_query_pin_caps(codec
, pin
);
5109 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5110 /* Exception: if the default pin setup is vref50, we give it priority */
5111 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
5112 return AC_PINCTL_VREF_80
;
5113 else if (pincap
& AC_PINCAP_VREF_50
)
5114 return AC_PINCTL_VREF_50
;
5115 else if (pincap
& AC_PINCAP_VREF_100
)
5116 return AC_PINCTL_VREF_100
;
5117 else if (pincap
& AC_PINCAP_VREF_GRD
)
5118 return AC_PINCTL_VREF_GRD
;
5119 return AC_PINCTL_VREF_HIZ
;
5121 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref
);
5123 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
5124 unsigned int val
, bool cached
)
5127 unsigned int cap
= snd_hda_query_pin_caps(codec
, pin
);
5128 if (cap
&& (val
& AC_PINCTL_OUT_EN
)) {
5129 if (!(cap
& AC_PINCAP_OUT
))
5130 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
5131 else if ((val
& AC_PINCTL_HP_EN
) &&
5132 !(cap
& AC_PINCAP_HP_DRV
))
5133 val
&= ~AC_PINCTL_HP_EN
;
5135 if (cap
&& (val
& AC_PINCTL_IN_EN
)) {
5136 if (!(cap
& AC_PINCAP_IN
))
5137 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
5141 return snd_hda_codec_update_cache(codec
, pin
, 0,
5142 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5144 return snd_hda_codec_write(codec
, pin
, 0,
5145 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5147 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl
);
5150 * snd_hda_add_imux_item - Add an item to input_mux
5152 * When the same label is used already in the existing items, the number
5153 * suffix is appended to the label. This label index number is stored
5154 * to type_idx when non-NULL pointer is given.
5156 int snd_hda_add_imux_item(struct hda_input_mux
*imux
, const char *label
,
5157 int index
, int *type_idx
)
5159 int i
, label_idx
= 0;
5160 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
5161 snd_printd(KERN_ERR
"hda_codec: Too many imux items!\n");
5164 for (i
= 0; i
< imux
->num_items
; i
++) {
5165 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
5169 *type_idx
= label_idx
;
5171 snprintf(imux
->items
[imux
->num_items
].label
,
5172 sizeof(imux
->items
[imux
->num_items
].label
),
5173 "%s %d", label
, label_idx
);
5175 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5176 sizeof(imux
->items
[imux
->num_items
].label
));
5177 imux
->items
[imux
->num_items
].index
= index
;
5181 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item
);
5190 * snd_hda_suspend - suspend the codecs
5193 * Returns 0 if successful.
5195 int snd_hda_suspend(struct hda_bus
*bus
)
5197 struct hda_codec
*codec
;
5199 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5200 cancel_delayed_work_sync(&codec
->jackpoll_work
);
5201 if (hda_codec_is_power_on(codec
))
5202 hda_call_codec_suspend(codec
, false);
5206 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
5209 * snd_hda_resume - resume the codecs
5212 * Returns 0 if successful.
5214 int snd_hda_resume(struct hda_bus
*bus
)
5216 struct hda_codec
*codec
;
5218 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5219 hda_call_codec_resume(codec
);
5223 EXPORT_SYMBOL_HDA(snd_hda_resume
);
5224 #endif /* CONFIG_PM */
5231 * snd_array_new - get a new element from the given array
5232 * @array: the array object
5234 * Get a new element from the given array. If it exceeds the
5235 * pre-allocated array size, re-allocate the array.
5237 * Returns NULL if allocation failed.
5239 void *snd_array_new(struct snd_array
*array
)
5241 if (snd_BUG_ON(!array
->elem_size
))
5243 if (array
->used
>= array
->alloced
) {
5244 int num
= array
->alloced
+ array
->alloc_align
;
5245 int size
= (num
+ 1) * array
->elem_size
;
5246 int oldsize
= array
->alloced
* array
->elem_size
;
5248 if (snd_BUG_ON(num
>= 4096))
5250 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
);
5253 memset(nlist
+ oldsize
, 0, size
- oldsize
);
5254 array
->list
= nlist
;
5255 array
->alloced
= num
;
5257 return snd_array_elem(array
, array
->used
++);
5259 EXPORT_SYMBOL_HDA(snd_array_new
);
5262 * snd_array_free - free the given array elements
5263 * @array: the array object
5265 void snd_array_free(struct snd_array
*array
)
5272 EXPORT_SYMBOL_HDA(snd_array_free
);
5275 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5276 * @pcm: PCM caps bits
5277 * @buf: the string buffer to write
5278 * @buflen: the max buffer length
5280 * used by hda_proc.c and hda_eld.c
5282 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5284 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5287 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5288 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5289 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5291 buf
[j
] = '\0'; /* necessary when j == 0 */
5293 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
5295 MODULE_DESCRIPTION("HDA codec core");
5296 MODULE_LICENSE("GPL");