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 static void hda_power_work(struct work_struct
*work
);
99 static void hda_keep_power_on(struct hda_codec
*codec
);
100 #define hda_codec_is_power_on(codec) ((codec)->power_on)
101 static inline void hda_call_pm_notify(struct hda_bus
*bus
, bool power_up
)
103 if (bus
->ops
.pm_notify
)
104 bus
->ops
.pm_notify(bus
, power_up
);
107 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
108 #define hda_codec_is_power_on(codec) 1
109 #define hda_call_pm_notify(bus, state) {}
113 * snd_hda_get_jack_location - Give a location string of the jack
114 * @cfg: pin default config value
116 * Parse the pin default config value and returns the string of the
117 * jack location, e.g. "Rear", "Front", etc.
119 const char *snd_hda_get_jack_location(u32 cfg
)
121 static char *bases
[7] = {
122 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
124 static unsigned char specials_idx
[] = {
129 static char *specials
[] = {
130 "Rear Panel", "Drive Bar",
131 "Riser", "HDMI", "ATAPI",
132 "Mobile-In", "Mobile-Out"
135 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
136 if ((cfg
& 0x0f) < 7)
137 return bases
[cfg
& 0x0f];
138 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
139 if (cfg
== specials_idx
[i
])
144 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location
);
147 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
148 * @cfg: pin default config value
150 * Parse the pin default config value and returns the string of the
151 * jack connectivity, i.e. external or internal connection.
153 const char *snd_hda_get_jack_connectivity(u32 cfg
)
155 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
157 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
159 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity
);
162 * snd_hda_get_jack_type - Give a type string of the jack
163 * @cfg: pin default config value
165 * Parse the pin default config value and returns the string of the
166 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
168 const char *snd_hda_get_jack_type(u32 cfg
)
170 static char *jack_types
[16] = {
171 "Line Out", "Speaker", "HP Out", "CD",
172 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
173 "Line In", "Aux", "Mic", "Telephony",
174 "SPDIF In", "Digitial In", "Reserved", "Other"
177 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
178 >> AC_DEFCFG_DEVICE_SHIFT
];
180 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type
);
183 * Compose a 32bit command word to be sent to the HD-audio controller
185 static inline unsigned int
186 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
187 unsigned int verb
, unsigned int parm
)
191 if ((codec
->addr
& ~0xf) || (direct
& ~1) || (nid
& ~0x7f) ||
192 (verb
& ~0xfff) || (parm
& ~0xffff)) {
193 printk(KERN_ERR
"hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
194 codec
->addr
, direct
, nid
, verb
, parm
);
198 val
= (u32
)codec
->addr
<< 28;
199 val
|= (u32
)direct
<< 27;
200 val
|= (u32
)nid
<< 20;
207 * Send and receive a verb
209 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
212 struct hda_bus
*bus
= codec
->bus
;
221 snd_hda_power_up(codec
);
222 mutex_lock(&bus
->cmd_mutex
);
223 trace_hda_send_cmd(codec
, cmd
);
224 err
= bus
->ops
.command(bus
, cmd
);
226 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
227 trace_hda_get_response(codec
, *res
);
229 mutex_unlock(&bus
->cmd_mutex
);
230 snd_hda_power_down(codec
);
231 if (!codec
->in_pm
&& res
&& *res
== -1 && bus
->rirb_error
) {
232 if (bus
->response_reset
) {
233 snd_printd("hda_codec: resetting BUS due to "
234 "fatal communication error\n");
235 trace_hda_bus_reset(bus
);
236 bus
->ops
.bus_reset(bus
);
240 /* clear reset-flag when the communication gets recovered */
241 if (!err
|| codec
->in_pm
)
242 bus
->response_reset
= 0;
247 * snd_hda_codec_read - send a command and get the response
248 * @codec: the HDA codec
249 * @nid: NID to send the command
250 * @direct: direct flag
251 * @verb: the verb to send
252 * @parm: the parameter for the verb
254 * Send a single command and read the corresponding response.
256 * Returns the obtained response value, or -1 for an error.
258 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
260 unsigned int verb
, unsigned int parm
)
262 unsigned cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
264 if (codec_exec_verb(codec
, cmd
, &res
))
268 EXPORT_SYMBOL_HDA(snd_hda_codec_read
);
271 * snd_hda_codec_write - send a single command without waiting for response
272 * @codec: the HDA codec
273 * @nid: NID to send the command
274 * @direct: direct flag
275 * @verb: the verb to send
276 * @parm: the parameter for the verb
278 * Send a single command without waiting for response.
280 * Returns 0 if successful, or a negative error code.
282 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
283 unsigned int verb
, unsigned int parm
)
285 unsigned int cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
287 return codec_exec_verb(codec
, cmd
,
288 codec
->bus
->sync_write
? &res
: NULL
);
290 EXPORT_SYMBOL_HDA(snd_hda_codec_write
);
293 * snd_hda_sequence_write - sequence writes
294 * @codec: the HDA codec
295 * @seq: VERB array to send
297 * Send the commands sequentially from the given array.
298 * The array must be terminated with NID=0.
300 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
302 for (; seq
->nid
; seq
++)
303 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
305 EXPORT_SYMBOL_HDA(snd_hda_sequence_write
);
308 * snd_hda_get_sub_nodes - get the range of sub nodes
309 * @codec: the HDA codec
311 * @start_id: the pointer to store the start NID
313 * Parse the NID and store the start NID of its sub-nodes.
314 * Returns the number of sub-nodes.
316 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
321 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
324 *start_id
= (parm
>> 16) & 0x7fff;
325 return (int)(parm
& 0x7fff);
327 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
329 /* look up the cached results */
330 static hda_nid_t
*lookup_conn_list(struct snd_array
*array
, hda_nid_t nid
)
333 for (i
= 0; i
< array
->used
; ) {
334 hda_nid_t
*p
= snd_array_elem(array
, i
);
343 /* read the connection and add to the cache */
344 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
346 hda_nid_t list
[HDA_MAX_CONNECTIONS
];
349 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
352 return snd_hda_override_conn_list(codec
, nid
, len
, list
);
356 * snd_hda_get_connections - copy connection list
357 * @codec: the HDA codec
359 * @conn_list: connection list array; when NULL, checks only the size
360 * @max_conns: max. number of connections to store
362 * Parses the connection list of the given widget and stores the list
365 * Returns the number of connections, or a negative error code.
367 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
368 hda_nid_t
*conn_list
, int max_conns
)
370 struct snd_array
*array
= &codec
->conn_lists
;
376 mutex_lock(&codec
->hash_mutex
);
378 /* if the connection-list is already cached, read it */
379 p
= lookup_conn_list(array
, nid
);
382 if (conn_list
&& len
> max_conns
) {
383 snd_printk(KERN_ERR
"hda_codec: "
384 "Too many connections %d for NID 0x%x\n",
386 mutex_unlock(&codec
->hash_mutex
);
389 if (conn_list
&& len
)
390 memcpy(conn_list
, p
+ 2, len
* sizeof(hda_nid_t
));
392 mutex_unlock(&codec
->hash_mutex
);
395 if (snd_BUG_ON(added
))
398 len
= read_and_add_raw_conns(codec
, nid
);
404 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
407 * snd_hda_get_raw_connections - copy connection list without cache
408 * @codec: the HDA codec
410 * @conn_list: connection list array
411 * @max_conns: max. number of connections to store
413 * Like snd_hda_get_connections(), copy the connection list but without
414 * checking through the connection-list cache.
415 * Currently called only from hda_proc.c, so not exported.
417 int snd_hda_get_raw_connections(struct hda_codec
*codec
, hda_nid_t nid
,
418 hda_nid_t
*conn_list
, int max_conns
)
421 int i
, conn_len
, conns
;
422 unsigned int shift
, num_elems
, mask
;
426 if (snd_BUG_ON(!conn_list
|| max_conns
<= 0))
429 wcaps
= get_wcaps(codec
, nid
);
430 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
431 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
434 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
435 if (parm
& AC_CLIST_LONG
) {
444 conn_len
= parm
& AC_CLIST_LENGTH
;
445 mask
= (1 << (shift
-1)) - 1;
448 return 0; /* no connection */
451 /* single connection */
452 parm
= snd_hda_codec_read(codec
, nid
, 0,
453 AC_VERB_GET_CONNECT_LIST
, 0);
454 if (parm
== -1 && codec
->bus
->rirb_error
)
456 conn_list
[0] = parm
& mask
;
460 /* multi connection */
463 for (i
= 0; i
< conn_len
; i
++) {
467 if (i
% num_elems
== 0) {
468 parm
= snd_hda_codec_read(codec
, nid
, 0,
469 AC_VERB_GET_CONNECT_LIST
, i
);
470 if (parm
== -1 && codec
->bus
->rirb_error
)
473 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
476 snd_printk(KERN_WARNING
"hda_codec: "
477 "invalid CONNECT_LIST verb %x[%i]:%x\n",
483 /* ranges between the previous and this one */
484 if (!prev_nid
|| prev_nid
>= val
) {
485 snd_printk(KERN_WARNING
"hda_codec: "
486 "invalid dep_range_val %x:%x\n",
490 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
491 if (conns
>= max_conns
) {
492 snd_printk(KERN_ERR
"hda_codec: "
493 "Too many connections %d for NID 0x%x\n",
497 conn_list
[conns
++] = n
;
500 if (conns
>= max_conns
) {
501 snd_printk(KERN_ERR
"hda_codec: "
502 "Too many connections %d for NID 0x%x\n",
506 conn_list
[conns
++] = val
;
513 static bool add_conn_list(struct snd_array
*array
, hda_nid_t nid
)
515 hda_nid_t
*p
= snd_array_new(array
);
523 * snd_hda_override_conn_list - add/modify the connection-list to cache
524 * @codec: the HDA codec
526 * @len: number of connection list entries
527 * @list: the list of connection entries
529 * Add or modify the given connection-list to the cache. If the corresponding
530 * cache already exists, invalidate it and append a new one.
532 * Returns zero or a negative error code.
534 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
535 const hda_nid_t
*list
)
537 struct snd_array
*array
= &codec
->conn_lists
;
541 mutex_lock(&codec
->hash_mutex
);
542 p
= lookup_conn_list(array
, nid
);
544 *p
= -1; /* invalidate the old entry */
546 old_used
= array
->used
;
547 if (!add_conn_list(array
, nid
) || !add_conn_list(array
, len
))
549 for (i
= 0; i
< len
; i
++)
550 if (!add_conn_list(array
, list
[i
]))
552 mutex_unlock(&codec
->hash_mutex
);
556 array
->used
= old_used
;
557 mutex_unlock(&codec
->hash_mutex
);
560 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list
);
563 * snd_hda_get_conn_index - get the connection index of the given NID
564 * @codec: the HDA codec
565 * @mux: NID containing the list
566 * @nid: NID to select
567 * @recursive: 1 when searching NID recursively, otherwise 0
569 * Parses the connection list of the widget @mux and checks whether the
570 * widget @nid is present. If it is, return the connection index.
571 * Otherwise it returns -1.
573 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
574 hda_nid_t nid
, int recursive
)
576 hda_nid_t conn
[HDA_MAX_NUM_INPUTS
];
579 nums
= snd_hda_get_connections(codec
, mux
, conn
, ARRAY_SIZE(conn
));
580 for (i
= 0; i
< nums
; i
++)
586 snd_printd("hda_codec: too deep connection for 0x%x\n", nid
);
590 for (i
= 0; i
< nums
; i
++) {
591 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
592 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
594 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
599 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index
);
602 * snd_hda_queue_unsol_event - add an unsolicited event to queue
604 * @res: unsolicited event (lower 32bit of RIRB entry)
605 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
607 * Adds the given event to the queue. The events are processed in
608 * the workqueue asynchronously. Call this function in the interrupt
609 * hanlder when RIRB receives an unsolicited event.
611 * Returns 0 if successful, or a negative error code.
613 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
615 struct hda_bus_unsolicited
*unsol
;
618 trace_hda_unsol_event(bus
, res
, res_ex
);
623 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
627 unsol
->queue
[wp
] = res
;
628 unsol
->queue
[wp
+ 1] = res_ex
;
630 queue_work(bus
->workq
, &unsol
->work
);
634 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event
);
637 * process queued unsolicited events
639 static void process_unsol_events(struct work_struct
*work
)
641 struct hda_bus_unsolicited
*unsol
=
642 container_of(work
, struct hda_bus_unsolicited
, work
);
643 struct hda_bus
*bus
= unsol
->bus
;
644 struct hda_codec
*codec
;
645 unsigned int rp
, caddr
, res
;
647 while (unsol
->rp
!= unsol
->wp
) {
648 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
651 res
= unsol
->queue
[rp
];
652 caddr
= unsol
->queue
[rp
+ 1];
653 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
655 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
656 if (codec
&& codec
->patch_ops
.unsol_event
)
657 codec
->patch_ops
.unsol_event(codec
, res
);
662 * initialize unsolicited queue
664 static int init_unsol_queue(struct hda_bus
*bus
)
666 struct hda_bus_unsolicited
*unsol
;
668 if (bus
->unsol
) /* already initialized */
671 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
673 snd_printk(KERN_ERR
"hda_codec: "
674 "can't allocate unsolicited queue\n");
677 INIT_WORK(&unsol
->work
, process_unsol_events
);
686 static void snd_hda_codec_free(struct hda_codec
*codec
);
688 static int snd_hda_bus_free(struct hda_bus
*bus
)
690 struct hda_codec
*codec
, *n
;
695 flush_workqueue(bus
->workq
);
698 list_for_each_entry_safe(codec
, n
, &bus
->codec_list
, list
) {
699 snd_hda_codec_free(codec
);
701 if (bus
->ops
.private_free
)
702 bus
->ops
.private_free(bus
);
704 destroy_workqueue(bus
->workq
);
709 static int snd_hda_bus_dev_free(struct snd_device
*device
)
711 struct hda_bus
*bus
= device
->device_data
;
713 return snd_hda_bus_free(bus
);
716 #ifdef CONFIG_SND_HDA_HWDEP
717 static int snd_hda_bus_dev_register(struct snd_device
*device
)
719 struct hda_bus
*bus
= device
->device_data
;
720 struct hda_codec
*codec
;
721 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
722 snd_hda_hwdep_add_sysfs(codec
);
723 snd_hda_hwdep_add_power_sysfs(codec
);
728 #define snd_hda_bus_dev_register NULL
732 * snd_hda_bus_new - create a HDA bus
733 * @card: the card entry
734 * @temp: the template for hda_bus information
735 * @busp: the pointer to store the created bus instance
737 * Returns 0 if successful, or a negative error code.
739 int /*__devinit*/ snd_hda_bus_new(struct snd_card
*card
,
740 const struct hda_bus_template
*temp
,
741 struct hda_bus
**busp
)
745 static struct snd_device_ops dev_ops
= {
746 .dev_register
= snd_hda_bus_dev_register
,
747 .dev_free
= snd_hda_bus_dev_free
,
750 if (snd_BUG_ON(!temp
))
752 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
758 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
760 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
765 bus
->private_data
= temp
->private_data
;
766 bus
->pci
= temp
->pci
;
767 bus
->modelname
= temp
->modelname
;
768 bus
->power_save
= temp
->power_save
;
769 bus
->ops
= temp
->ops
;
771 mutex_init(&bus
->cmd_mutex
);
772 mutex_init(&bus
->prepare_mutex
);
773 INIT_LIST_HEAD(&bus
->codec_list
);
775 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
776 "hd-audio%d", card
->number
);
777 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
779 snd_printk(KERN_ERR
"cannot create workqueue %s\n",
785 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
787 snd_hda_bus_free(bus
);
794 EXPORT_SYMBOL_HDA(snd_hda_bus_new
);
796 #ifdef CONFIG_SND_HDA_GENERIC
797 #define is_generic_config(codec) \
798 (codec->modelname && !strcmp(codec->modelname, "generic"))
800 #define is_generic_config(codec) 0
804 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
806 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
810 * find a matching codec preset
812 static const struct hda_codec_preset
*
813 find_codec_preset(struct hda_codec
*codec
)
815 struct hda_codec_preset_list
*tbl
;
816 const struct hda_codec_preset
*preset
;
817 unsigned int mod_requested
= 0;
819 if (is_generic_config(codec
))
820 return NULL
; /* use the generic parser */
823 mutex_lock(&preset_mutex
);
824 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
825 if (!try_module_get(tbl
->owner
)) {
826 snd_printk(KERN_ERR
"hda_codec: cannot module_get\n");
829 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
830 u32 mask
= preset
->mask
;
831 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
833 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
837 if (preset
->id
== (codec
->vendor_id
& mask
) &&
839 preset
->rev
== codec
->revision_id
)) {
840 mutex_unlock(&preset_mutex
);
841 codec
->owner
= tbl
->owner
;
845 module_put(tbl
->owner
);
847 mutex_unlock(&preset_mutex
);
849 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
852 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%08x",
855 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%04x*",
856 (codec
->vendor_id
>> 16) & 0xffff);
857 request_module(name
);
865 * get_codec_name - store the codec name
867 static int get_codec_name(struct hda_codec
*codec
)
869 const struct hda_vendor_id
*c
;
870 const char *vendor
= NULL
;
871 u16 vendor_id
= codec
->vendor_id
>> 16;
874 if (codec
->vendor_name
)
877 for (c
= hda_vendor_ids
; c
->id
; c
++) {
878 if (c
->id
== vendor_id
) {
884 sprintf(tmp
, "Generic %04x", vendor_id
);
887 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
888 if (!codec
->vendor_name
)
892 if (codec
->chip_name
)
895 if (codec
->preset
&& codec
->preset
->name
)
896 codec
->chip_name
= kstrdup(codec
->preset
->name
, GFP_KERNEL
);
898 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
899 codec
->chip_name
= kstrdup(tmp
, GFP_KERNEL
);
901 if (!codec
->chip_name
)
907 * look for an AFG and MFG nodes
909 static void /*__devinit*/ setup_fg_nodes(struct hda_codec
*codec
)
911 int i
, total_nodes
, function_id
;
914 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
915 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
916 function_id
= snd_hda_param_read(codec
, nid
,
917 AC_PAR_FUNCTION_TYPE
);
918 switch (function_id
& 0xff) {
919 case AC_GRP_AUDIO_FUNCTION
:
921 codec
->afg_function_id
= function_id
& 0xff;
922 codec
->afg_unsol
= (function_id
>> 8) & 1;
924 case AC_GRP_MODEM_FUNCTION
:
926 codec
->mfg_function_id
= function_id
& 0xff;
927 codec
->mfg_unsol
= (function_id
>> 8) & 1;
936 * read widget caps for each widget and store in cache
938 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
943 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
945 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
948 nid
= codec
->start_nid
;
949 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
950 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
951 AC_PAR_AUDIO_WIDGET_CAP
);
955 /* read all pin default configurations and save codec->init_pins */
956 static int read_pin_defaults(struct hda_codec
*codec
)
959 hda_nid_t nid
= codec
->start_nid
;
961 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
962 struct hda_pincfg
*pin
;
963 unsigned int wcaps
= get_wcaps(codec
, nid
);
964 unsigned int wid_type
= get_wcaps_type(wcaps
);
965 if (wid_type
!= AC_WID_PIN
)
967 pin
= snd_array_new(&codec
->init_pins
);
971 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
972 AC_VERB_GET_CONFIG_DEFAULT
, 0);
973 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
974 AC_VERB_GET_PIN_WIDGET_CONTROL
,
980 /* look up the given pin config list and return the item matching with NID */
981 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
982 struct snd_array
*array
,
986 for (i
= 0; i
< array
->used
; i
++) {
987 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
994 /* write a config value for the given NID */
995 static void set_pincfg(struct hda_codec
*codec
, hda_nid_t nid
,
999 for (i
= 0; i
< 4; i
++) {
1000 snd_hda_codec_write(codec
, nid
, 0,
1001 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0
+ i
,
1007 /* set the current pin config value for the given NID.
1008 * the value is cached, and read via snd_hda_codec_get_pincfg()
1010 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
1011 hda_nid_t nid
, unsigned int cfg
)
1013 struct hda_pincfg
*pin
;
1014 unsigned int oldcfg
;
1016 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_PIN
)
1019 oldcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1020 pin
= look_up_pincfg(codec
, list
, nid
);
1022 pin
= snd_array_new(list
);
1029 /* change only when needed; e.g. if the pincfg is already present
1030 * in user_pins[], don't write it
1032 cfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1034 set_pincfg(codec
, nid
, cfg
);
1039 * snd_hda_codec_set_pincfg - Override a pin default configuration
1040 * @codec: the HDA codec
1041 * @nid: NID to set the pin config
1042 * @cfg: the pin default config value
1044 * Override a pin default configuration value in the cache.
1045 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1046 * priority than the real hardware value.
1048 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
1049 hda_nid_t nid
, unsigned int cfg
)
1051 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
1053 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg
);
1056 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1057 * @codec: the HDA codec
1058 * @nid: NID to get the pin config
1060 * Get the current pin config value of the given pin NID.
1061 * If the pincfg value is cached or overridden via sysfs or driver,
1062 * returns the cached value.
1064 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
1066 struct hda_pincfg
*pin
;
1068 #ifdef CONFIG_SND_HDA_HWDEP
1069 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
1073 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
1076 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1081 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg
);
1083 /* restore all current pin configs */
1084 static void restore_pincfgs(struct hda_codec
*codec
)
1087 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1088 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1089 set_pincfg(codec
, pin
->nid
,
1090 snd_hda_codec_get_pincfg(codec
, pin
->nid
));
1095 * snd_hda_shutup_pins - Shut up all pins
1096 * @codec: the HDA codec
1098 * Clear all pin controls to shup up before suspend for avoiding click noise.
1099 * The controls aren't cached so that they can be resumed properly.
1101 void snd_hda_shutup_pins(struct hda_codec
*codec
)
1104 /* don't shut up pins when unloading the driver; otherwise it breaks
1105 * the default pin setup at the next load of the driver
1107 if (codec
->bus
->shutdown
)
1109 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1110 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1111 /* use read here for syncing after issuing each verb */
1112 snd_hda_codec_read(codec
, pin
->nid
, 0,
1113 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
1115 codec
->pins_shutup
= 1;
1117 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins
);
1120 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1121 static void restore_shutup_pins(struct hda_codec
*codec
)
1124 if (!codec
->pins_shutup
)
1126 if (codec
->bus
->shutdown
)
1128 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1129 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1130 snd_hda_codec_write(codec
, pin
->nid
, 0,
1131 AC_VERB_SET_PIN_WIDGET_CONTROL
,
1134 codec
->pins_shutup
= 0;
1138 static void hda_jackpoll_work(struct work_struct
*work
)
1140 struct hda_codec
*codec
=
1141 container_of(work
, struct hda_codec
, jackpoll_work
.work
);
1142 if (!codec
->jackpoll_interval
)
1145 snd_hda_jack_set_dirty_all(codec
);
1146 snd_hda_jack_poll_all(codec
);
1147 queue_delayed_work(codec
->bus
->workq
, &codec
->jackpoll_work
,
1148 codec
->jackpoll_interval
);
1151 static void init_hda_cache(struct hda_cache_rec
*cache
,
1152 unsigned int record_size
);
1153 static void free_hda_cache(struct hda_cache_rec
*cache
);
1155 /* restore the initial pin cfgs and release all pincfg lists */
1156 static void restore_init_pincfgs(struct hda_codec
*codec
)
1158 /* first free driver_pins and user_pins, then call restore_pincfg
1159 * so that only the values in init_pins are restored
1161 snd_array_free(&codec
->driver_pins
);
1162 #ifdef CONFIG_SND_HDA_HWDEP
1163 snd_array_free(&codec
->user_pins
);
1165 restore_pincfgs(codec
);
1166 snd_array_free(&codec
->init_pins
);
1170 * audio-converter setup caches
1172 struct hda_cvt_setup
{
1177 unsigned char active
; /* cvt is currently used */
1178 unsigned char dirty
; /* setups should be cleared */
1181 /* get or create a cache entry for the given audio converter NID */
1182 static struct hda_cvt_setup
*
1183 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1185 struct hda_cvt_setup
*p
;
1188 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1189 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1193 p
= snd_array_new(&codec
->cvt_setups
);
1202 static void snd_hda_codec_free(struct hda_codec
*codec
)
1206 cancel_delayed_work_sync(&codec
->jackpoll_work
);
1207 snd_hda_jack_tbl_clear(codec
);
1208 restore_init_pincfgs(codec
);
1210 cancel_delayed_work(&codec
->power_work
);
1211 flush_workqueue(codec
->bus
->workq
);
1213 list_del(&codec
->list
);
1214 snd_array_free(&codec
->mixers
);
1215 snd_array_free(&codec
->nids
);
1216 snd_array_free(&codec
->cvt_setups
);
1217 snd_array_free(&codec
->conn_lists
);
1218 snd_array_free(&codec
->spdif_out
);
1219 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1220 if (codec
->patch_ops
.free
)
1221 codec
->patch_ops
.free(codec
);
1223 if (!codec
->pm_down_notified
) /* cancel leftover refcounts */
1224 hda_call_pm_notify(codec
->bus
, false);
1226 module_put(codec
->owner
);
1227 free_hda_cache(&codec
->amp_cache
);
1228 free_hda_cache(&codec
->cmd_cache
);
1229 kfree(codec
->vendor_name
);
1230 kfree(codec
->chip_name
);
1231 kfree(codec
->modelname
);
1232 kfree(codec
->wcaps
);
1236 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
,
1237 hda_nid_t fg
, unsigned int power_state
);
1239 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
1240 unsigned int power_state
);
1243 * snd_hda_codec_new - create a HDA codec
1244 * @bus: the bus to assign
1245 * @codec_addr: the codec address
1246 * @codecp: the pointer to store the generated codec
1248 * Returns 0 if successful, or a negative error code.
1250 int /*__devinit*/ snd_hda_codec_new(struct hda_bus
*bus
,
1251 unsigned int codec_addr
,
1252 struct hda_codec
**codecp
)
1254 struct hda_codec
*codec
;
1259 if (snd_BUG_ON(!bus
))
1261 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1264 if (bus
->caddr_tbl
[codec_addr
]) {
1265 snd_printk(KERN_ERR
"hda_codec: "
1266 "address 0x%x is already occupied\n", codec_addr
);
1270 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1271 if (codec
== NULL
) {
1272 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
1277 codec
->addr
= codec_addr
;
1278 mutex_init(&codec
->spdif_mutex
);
1279 mutex_init(&codec
->control_mutex
);
1280 mutex_init(&codec
->hash_mutex
);
1281 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1282 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1283 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1284 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1285 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1286 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1287 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1288 snd_array_init(&codec
->conn_lists
, sizeof(hda_nid_t
), 64);
1289 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1290 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
1293 spin_lock_init(&codec
->power_lock
);
1294 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1295 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1296 * the caller has to power down appropriatley after initialization
1299 hda_keep_power_on(codec
);
1300 hda_call_pm_notify(bus
, true);
1303 if (codec
->bus
->modelname
) {
1304 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1305 if (!codec
->modelname
) {
1306 snd_hda_codec_free(codec
);
1311 list_add_tail(&codec
->list
, &bus
->codec_list
);
1312 bus
->caddr_tbl
[codec_addr
] = codec
;
1314 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1316 if (codec
->vendor_id
== -1)
1317 /* read again, hopefully the access method was corrected
1318 * in the last read...
1320 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1322 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1323 AC_PAR_SUBSYSTEM_ID
);
1324 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1327 setup_fg_nodes(codec
);
1328 if (!codec
->afg
&& !codec
->mfg
) {
1329 snd_printdd("hda_codec: no AFG or MFG node found\n");
1334 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1335 err
= read_widget_caps(codec
, fg
);
1337 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1340 err
= read_pin_defaults(codec
);
1344 if (!codec
->subsystem_id
) {
1345 codec
->subsystem_id
=
1346 snd_hda_codec_read(codec
, fg
, 0,
1347 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1351 codec
->d3_stop_clk
= snd_hda_codec_get_supported_ps(codec
, fg
,
1353 if (!codec
->d3_stop_clk
)
1354 bus
->power_keep_link_on
= 1;
1356 codec
->epss
= snd_hda_codec_get_supported_ps(codec
, fg
,
1359 /* power-up all before initialization */
1360 hda_set_power_state(codec
, AC_PWRST_D0
);
1362 snd_hda_codec_proc_new(codec
);
1364 snd_hda_create_hwdep(codec
);
1366 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1367 codec
->subsystem_id
, codec
->revision_id
);
1368 snd_component_add(codec
->bus
->card
, component
);
1375 snd_hda_codec_free(codec
);
1378 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
1381 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1382 * @codec: the HDA codec
1384 * Start parsing of the given codec tree and (re-)initialize the whole
1387 * Returns 0 if successful or a negative error code.
1389 int snd_hda_codec_configure(struct hda_codec
*codec
)
1393 codec
->preset
= find_codec_preset(codec
);
1394 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1395 err
= get_codec_name(codec
);
1400 if (is_generic_config(codec
)) {
1401 err
= snd_hda_parse_generic_codec(codec
);
1404 if (codec
->preset
&& codec
->preset
->patch
) {
1405 err
= codec
->preset
->patch(codec
);
1409 /* call the default parser */
1410 err
= snd_hda_parse_generic_codec(codec
);
1412 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1415 if (!err
&& codec
->patch_ops
.unsol_event
)
1416 err
= init_unsol_queue(codec
->bus
);
1417 /* audio codec should override the mixer name */
1418 if (!err
&& (codec
->afg
|| !*codec
->bus
->card
->mixername
))
1419 snprintf(codec
->bus
->card
->mixername
,
1420 sizeof(codec
->bus
->card
->mixername
),
1421 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1424 EXPORT_SYMBOL_HDA(snd_hda_codec_configure
);
1426 /* update the stream-id if changed */
1427 static void update_pcm_stream_id(struct hda_codec
*codec
,
1428 struct hda_cvt_setup
*p
, hda_nid_t nid
,
1429 u32 stream_tag
, int channel_id
)
1431 unsigned int oldval
, newval
;
1433 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1434 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1435 newval
= (stream_tag
<< 4) | channel_id
;
1436 if (oldval
!= newval
)
1437 snd_hda_codec_write(codec
, nid
, 0,
1438 AC_VERB_SET_CHANNEL_STREAMID
,
1440 p
->stream_tag
= stream_tag
;
1441 p
->channel_id
= channel_id
;
1445 /* update the format-id if changed */
1446 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
1447 hda_nid_t nid
, int format
)
1449 unsigned int oldval
;
1451 if (p
->format_id
!= format
) {
1452 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1453 AC_VERB_GET_STREAM_FORMAT
, 0);
1454 if (oldval
!= format
) {
1456 snd_hda_codec_write(codec
, nid
, 0,
1457 AC_VERB_SET_STREAM_FORMAT
,
1460 p
->format_id
= format
;
1465 * snd_hda_codec_setup_stream - set up the codec for streaming
1466 * @codec: the CODEC to set up
1467 * @nid: the NID to set up
1468 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1469 * @channel_id: channel id to pass, zero based.
1470 * @format: stream format.
1472 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1474 int channel_id
, int format
)
1476 struct hda_codec
*c
;
1477 struct hda_cvt_setup
*p
;
1484 snd_printdd("hda_codec_setup_stream: "
1485 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1486 nid
, stream_tag
, channel_id
, format
);
1487 p
= get_hda_cvt_setup(codec
, nid
);
1491 if (codec
->pcm_format_first
)
1492 update_pcm_format(codec
, p
, nid
, format
);
1493 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1494 if (!codec
->pcm_format_first
)
1495 update_pcm_format(codec
, p
, nid
, format
);
1500 /* make other inactive cvts with the same stream-tag dirty */
1501 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1502 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1503 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1504 p
= snd_array_elem(&c
->cvt_setups
, i
);
1505 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1506 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1511 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1513 static void really_cleanup_stream(struct hda_codec
*codec
,
1514 struct hda_cvt_setup
*q
);
1517 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1518 * @codec: the CODEC to clean up
1519 * @nid: the NID to clean up
1520 * @do_now: really clean up the stream instead of clearing the active flag
1522 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1525 struct hda_cvt_setup
*p
;
1530 if (codec
->no_sticky_stream
)
1533 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1534 p
= get_hda_cvt_setup(codec
, nid
);
1536 /* here we just clear the active flag when do_now isn't set;
1537 * actual clean-ups will be done later in
1538 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1541 really_cleanup_stream(codec
, p
);
1546 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream
);
1548 static void really_cleanup_stream(struct hda_codec
*codec
,
1549 struct hda_cvt_setup
*q
)
1551 hda_nid_t nid
= q
->nid
;
1552 if (q
->stream_tag
|| q
->channel_id
)
1553 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1555 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1557 memset(q
, 0, sizeof(*q
));
1561 /* clean up the all conflicting obsolete streams */
1562 static void purify_inactive_streams(struct hda_codec
*codec
)
1564 struct hda_codec
*c
;
1567 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1568 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1569 struct hda_cvt_setup
*p
;
1570 p
= snd_array_elem(&c
->cvt_setups
, i
);
1572 really_cleanup_stream(c
, p
);
1578 /* clean up all streams; called from suspend */
1579 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1583 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1584 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1586 really_cleanup_stream(codec
, p
);
1592 * amp access functions
1595 /* FIXME: more better hash key? */
1596 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1597 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1598 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1599 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1600 #define INFO_AMP_CAPS (1<<0)
1601 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1603 /* initialize the hash table */
1604 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec
*cache
,
1605 unsigned int record_size
)
1607 memset(cache
, 0, sizeof(*cache
));
1608 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1609 snd_array_init(&cache
->buf
, record_size
, 64);
1612 static void free_hda_cache(struct hda_cache_rec
*cache
)
1614 snd_array_free(&cache
->buf
);
1617 /* query the hash. allocate an entry if not found. */
1618 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1620 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1621 u16 cur
= cache
->hash
[idx
];
1622 struct hda_cache_head
*info
;
1624 while (cur
!= 0xffff) {
1625 info
= snd_array_elem(&cache
->buf
, cur
);
1626 if (info
->key
== key
)
1633 /* query the hash. allocate an entry if not found. */
1634 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1637 struct hda_cache_head
*info
= get_hash(cache
, key
);
1640 /* add a new hash entry */
1641 info
= snd_array_new(&cache
->buf
);
1644 cur
= snd_array_index(&cache
->buf
, info
);
1647 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1648 info
->next
= cache
->hash
[idx
];
1649 cache
->hash
[idx
] = cur
;
1654 /* query and allocate an amp hash entry */
1655 static inline struct hda_amp_info
*
1656 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1658 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1661 /* overwrite the value with the key in the caps hash */
1662 static int write_caps_hash(struct hda_codec
*codec
, u32 key
, unsigned int val
)
1664 struct hda_amp_info
*info
;
1666 mutex_lock(&codec
->hash_mutex
);
1667 info
= get_alloc_amp_hash(codec
, key
);
1669 mutex_unlock(&codec
->hash_mutex
);
1672 info
->amp_caps
= val
;
1673 info
->head
.val
|= INFO_AMP_CAPS
;
1674 mutex_unlock(&codec
->hash_mutex
);
1678 /* query the value from the caps hash; if not found, fetch the current
1679 * value from the given function and store in the hash
1682 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, u32 key
,
1683 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
, int))
1685 struct hda_amp_info
*info
;
1688 mutex_lock(&codec
->hash_mutex
);
1689 info
= get_alloc_amp_hash(codec
, key
);
1691 mutex_unlock(&codec
->hash_mutex
);
1694 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1695 mutex_unlock(&codec
->hash_mutex
); /* for reentrance */
1696 val
= func(codec
, nid
, dir
);
1697 write_caps_hash(codec
, key
, val
);
1699 val
= info
->amp_caps
;
1700 mutex_unlock(&codec
->hash_mutex
);
1705 static unsigned int read_amp_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1708 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1710 return snd_hda_param_read(codec
, nid
,
1711 direction
== HDA_OUTPUT
?
1712 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1716 * query_amp_caps - query AMP capabilities
1717 * @codec: the HD-auio codec
1718 * @nid: the NID to query
1719 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1721 * Query AMP capabilities for the given widget and direction.
1722 * Returns the obtained capability bits.
1724 * When cap bits have been already read, this doesn't read again but
1725 * returns the cached value.
1727 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1729 return query_caps_hash(codec
, nid
, direction
,
1730 HDA_HASH_KEY(nid
, direction
, 0),
1733 EXPORT_SYMBOL_HDA(query_amp_caps
);
1736 * snd_hda_override_amp_caps - Override the AMP capabilities
1737 * @codec: the CODEC to clean up
1738 * @nid: the NID to clean up
1739 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1740 * @caps: the capability bits to set
1742 * Override the cached AMP caps bits value by the given one.
1743 * This function is useful if the driver needs to adjust the AMP ranges,
1744 * e.g. limit to 0dB, etc.
1746 * Returns zero if successful or a negative error code.
1748 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1751 return write_caps_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0), caps
);
1753 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1755 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1758 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1762 * snd_hda_query_pin_caps - Query PIN capabilities
1763 * @codec: the HD-auio codec
1764 * @nid: the NID to query
1766 * Query PIN capabilities for the given widget.
1767 * Returns the obtained capability bits.
1769 * When cap bits have been already read, this doesn't read again but
1770 * returns the cached value.
1772 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1774 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PINCAP_KEY(nid
),
1777 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1780 * snd_hda_override_pin_caps - Override the pin capabilities
1782 * @nid: the NID to override
1783 * @caps: the capability bits to set
1785 * Override the cached PIN capabilitiy bits value by the given one.
1787 * Returns zero if successful or a negative error code.
1789 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1792 return write_caps_hash(codec
, HDA_HASH_PINCAP_KEY(nid
), caps
);
1794 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps
);
1796 /* read or sync the hash value with the current value;
1797 * call within hash_mutex
1799 static struct hda_amp_info
*
1800 update_amp_hash(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1801 int direction
, int index
)
1803 struct hda_amp_info
*info
;
1804 unsigned int parm
, val
= 0;
1805 bool val_read
= false;
1808 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1811 if (!(info
->head
.val
& INFO_AMP_VOL(ch
))) {
1813 mutex_unlock(&codec
->hash_mutex
);
1814 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1815 parm
|= direction
== HDA_OUTPUT
?
1816 AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1818 val
= snd_hda_codec_read(codec
, nid
, 0,
1819 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1822 mutex_lock(&codec
->hash_mutex
);
1825 info
->vol
[ch
] = val
;
1826 info
->head
.val
|= INFO_AMP_VOL(ch
);
1832 * write the current volume in info to the h/w
1834 static void put_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
1835 hda_nid_t nid
, int ch
, int direction
, int index
,
1840 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1841 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1842 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1843 if ((val
& HDA_AMP_MUTE
) && !(info
->amp_caps
& AC_AMPCAP_MUTE
) &&
1844 (info
->amp_caps
& AC_AMPCAP_MIN_MUTE
))
1845 ; /* set the zero value as a fake mute */
1848 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1852 * snd_hda_codec_amp_read - Read AMP value
1853 * @codec: HD-audio codec
1854 * @nid: NID to read the AMP value
1855 * @ch: channel (left=0 or right=1)
1856 * @direction: #HDA_INPUT or #HDA_OUTPUT
1857 * @index: the index value (only for input direction)
1859 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1861 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1862 int direction
, int index
)
1864 struct hda_amp_info
*info
;
1865 unsigned int val
= 0;
1867 mutex_lock(&codec
->hash_mutex
);
1868 info
= update_amp_hash(codec
, nid
, ch
, direction
, index
);
1870 val
= info
->vol
[ch
];
1871 mutex_unlock(&codec
->hash_mutex
);
1874 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
1877 * snd_hda_codec_amp_update - update the AMP value
1878 * @codec: HD-audio codec
1879 * @nid: NID to read the AMP value
1880 * @ch: channel (left=0 or right=1)
1881 * @direction: #HDA_INPUT or #HDA_OUTPUT
1882 * @idx: the index value (only for input direction)
1883 * @mask: bit mask to set
1884 * @val: the bits value to set
1886 * Update the AMP value with a bit mask.
1887 * Returns 0 if the value is unchanged, 1 if changed.
1889 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1890 int direction
, int idx
, int mask
, int val
)
1892 struct hda_amp_info
*info
;
1894 if (snd_BUG_ON(mask
& ~0xff))
1898 mutex_lock(&codec
->hash_mutex
);
1899 info
= update_amp_hash(codec
, nid
, ch
, direction
, idx
);
1901 mutex_unlock(&codec
->hash_mutex
);
1904 val
|= info
->vol
[ch
] & ~mask
;
1905 if (info
->vol
[ch
] == val
) {
1906 mutex_unlock(&codec
->hash_mutex
);
1909 info
->vol
[ch
] = val
;
1910 mutex_unlock(&codec
->hash_mutex
);
1911 put_vol_mute(codec
, info
, nid
, ch
, direction
, idx
, val
);
1914 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
1917 * snd_hda_codec_amp_stereo - update the AMP stereo values
1918 * @codec: HD-audio codec
1919 * @nid: NID to read the AMP value
1920 * @direction: #HDA_INPUT or #HDA_OUTPUT
1921 * @idx: the index value (only for input direction)
1922 * @mask: bit mask to set
1923 * @val: the bits value to set
1925 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1926 * stereo widget with the same mask and value.
1928 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1929 int direction
, int idx
, int mask
, int val
)
1933 if (snd_BUG_ON(mask
& ~0xff))
1935 for (ch
= 0; ch
< 2; ch
++)
1936 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1940 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
1944 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1945 * @codec: HD-audio codec
1947 * Resume the all amp commands from the cache.
1949 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
1951 struct hda_amp_info
*buffer
= codec
->amp_cache
.buf
.list
;
1954 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++, buffer
++) {
1955 u32 key
= buffer
->head
.key
;
1957 unsigned int idx
, dir
, ch
;
1961 idx
= (key
>> 16) & 0xff;
1962 dir
= (key
>> 24) & 0xff;
1963 for (ch
= 0; ch
< 2; ch
++) {
1964 if (!(buffer
->head
.val
& INFO_AMP_VOL(ch
)))
1966 put_vol_mute(codec
, buffer
, nid
, ch
, dir
, idx
,
1971 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
1972 #endif /* CONFIG_PM */
1974 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1977 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1979 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1986 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1988 * The control element is supposed to have the private_value field
1989 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1991 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1992 struct snd_ctl_elem_info
*uinfo
)
1994 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1995 u16 nid
= get_amp_nid(kcontrol
);
1996 u8 chs
= get_amp_channels(kcontrol
);
1997 int dir
= get_amp_direction(kcontrol
);
1998 unsigned int ofs
= get_amp_offset(kcontrol
);
2000 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2001 uinfo
->count
= chs
== 3 ? 2 : 1;
2002 uinfo
->value
.integer
.min
= 0;
2003 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
2004 if (!uinfo
->value
.integer
.max
) {
2005 printk(KERN_WARNING
"hda_codec: "
2006 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
2012 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
2015 static inline unsigned int
2016 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2017 int ch
, int dir
, int idx
, unsigned int ofs
)
2020 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
2021 val
&= HDA_AMP_VOLMASK
;
2030 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2031 int ch
, int dir
, int idx
, unsigned int ofs
,
2034 unsigned int maxval
;
2038 /* ofs = 0: raw max value */
2039 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
2042 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
2043 HDA_AMP_VOLMASK
, val
);
2047 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2049 * The control element is supposed to have the private_value field
2050 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2052 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
2053 struct snd_ctl_elem_value
*ucontrol
)
2055 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2056 hda_nid_t nid
= get_amp_nid(kcontrol
);
2057 int chs
= get_amp_channels(kcontrol
);
2058 int dir
= get_amp_direction(kcontrol
);
2059 int idx
= get_amp_index(kcontrol
);
2060 unsigned int ofs
= get_amp_offset(kcontrol
);
2061 long *valp
= ucontrol
->value
.integer
.value
;
2064 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
2066 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
2069 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
2072 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2074 * The control element is supposed to have the private_value field
2075 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2077 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
2078 struct snd_ctl_elem_value
*ucontrol
)
2080 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2081 hda_nid_t nid
= get_amp_nid(kcontrol
);
2082 int chs
= get_amp_channels(kcontrol
);
2083 int dir
= get_amp_direction(kcontrol
);
2084 int idx
= get_amp_index(kcontrol
);
2085 unsigned int ofs
= get_amp_offset(kcontrol
);
2086 long *valp
= ucontrol
->value
.integer
.value
;
2089 snd_hda_power_up(codec
);
2091 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
2095 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
2096 snd_hda_power_down(codec
);
2099 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
2102 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2104 * The control element is supposed to have the private_value field
2105 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2107 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2108 unsigned int size
, unsigned int __user
*_tlv
)
2110 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2111 hda_nid_t nid
= get_amp_nid(kcontrol
);
2112 int dir
= get_amp_direction(kcontrol
);
2113 unsigned int ofs
= get_amp_offset(kcontrol
);
2114 bool min_mute
= get_amp_min_mute(kcontrol
);
2115 u32 caps
, val1
, val2
;
2117 if (size
< 4 * sizeof(unsigned int))
2119 caps
= query_amp_caps(codec
, nid
, dir
);
2120 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2121 val2
= (val2
+ 1) * 25;
2122 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2124 val1
= ((int)val1
) * ((int)val2
);
2125 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
2126 val2
|= TLV_DB_SCALE_MUTE
;
2127 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2129 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2131 if (put_user(val1
, _tlv
+ 2))
2133 if (put_user(val2
, _tlv
+ 3))
2137 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
2140 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2141 * @codec: HD-audio codec
2142 * @nid: NID of a reference widget
2143 * @dir: #HDA_INPUT or #HDA_OUTPUT
2144 * @tlv: TLV data to be stored, at least 4 elements
2146 * Set (static) TLV data for a virtual master volume using the AMP caps
2147 * obtained from the reference NID.
2148 * The volume range is recalculated as if the max volume is 0dB.
2150 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2156 caps
= query_amp_caps(codec
, nid
, dir
);
2157 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2158 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2159 step
= (step
+ 1) * 25;
2160 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2161 tlv
[1] = 2 * sizeof(unsigned int);
2162 tlv
[2] = -nums
* step
;
2165 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
2167 /* find a mixer control element with the given name */
2168 static struct snd_kcontrol
*
2169 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
2171 struct snd_ctl_elem_id id
;
2172 memset(&id
, 0, sizeof(id
));
2173 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2176 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2178 strcpy(id
.name
, name
);
2179 return snd_ctl_find_id(codec
->bus
->card
, &id
);
2183 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2184 * @codec: HD-audio codec
2185 * @name: ctl id name string
2187 * Get the control element with the given id string and IFACE_MIXER.
2189 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2192 return find_mixer_ctl(codec
, name
, 0, 0);
2194 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
2196 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
2200 for (idx
= 0; idx
< 16; idx
++) { /* 16 ctlrs should be large enough */
2201 if (!find_mixer_ctl(codec
, name
, dev
, idx
))
2208 * snd_hda_ctl_add - Add a control element and assign to the codec
2209 * @codec: HD-audio codec
2210 * @nid: corresponding NID (optional)
2211 * @kctl: the control element to assign
2213 * Add the given control element to an array inside the codec instance.
2214 * All control elements belonging to a codec are supposed to be added
2215 * by this function so that a proper clean-up works at the free or
2216 * reconfiguration time.
2218 * If non-zero @nid is passed, the NID is assigned to the control element.
2219 * The assignment is shown in the codec proc file.
2221 * snd_hda_ctl_add() checks the control subdev id field whether
2222 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2223 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2224 * specifies if kctl->private_value is a HDA amplifier value.
2226 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2227 struct snd_kcontrol
*kctl
)
2230 unsigned short flags
= 0;
2231 struct hda_nid_item
*item
;
2233 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2234 flags
|= HDA_NID_ITEM_AMP
;
2236 nid
= get_amp_nid_(kctl
->private_value
);
2238 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2239 nid
= kctl
->id
.subdevice
& 0xffff;
2240 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2241 kctl
->id
.subdevice
= 0;
2242 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
2245 item
= snd_array_new(&codec
->mixers
);
2250 item
->flags
= flags
;
2253 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
2256 * snd_hda_add_nid - Assign a NID to a control element
2257 * @codec: HD-audio codec
2258 * @nid: corresponding NID (optional)
2259 * @kctl: the control element to assign
2260 * @index: index to kctl
2262 * Add the given control element to an array inside the codec instance.
2263 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2264 * NID:KCTL mapping - for example "Capture Source" selector.
2266 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2267 unsigned int index
, hda_nid_t nid
)
2269 struct hda_nid_item
*item
;
2272 item
= snd_array_new(&codec
->nids
);
2276 item
->index
= index
;
2280 printk(KERN_ERR
"hda-codec: no NID for mapping control %s:%d:%d\n",
2281 kctl
->id
.name
, kctl
->id
.index
, index
);
2284 EXPORT_SYMBOL_HDA(snd_hda_add_nid
);
2287 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2288 * @codec: HD-audio codec
2290 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2293 struct hda_nid_item
*items
= codec
->mixers
.list
;
2294 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2295 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
2296 snd_array_free(&codec
->mixers
);
2297 snd_array_free(&codec
->nids
);
2300 /* pseudo device locking
2301 * toggle card->shutdown to allow/disallow the device access (as a hack)
2303 int snd_hda_lock_devices(struct hda_bus
*bus
)
2305 struct snd_card
*card
= bus
->card
;
2306 struct hda_codec
*codec
;
2308 spin_lock(&card
->files_lock
);
2312 if (!list_empty(&card
->ctl_files
))
2315 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2317 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2318 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2321 if (cpcm
->pcm
->streams
[0].substream_opened
||
2322 cpcm
->pcm
->streams
[1].substream_opened
)
2326 spin_unlock(&card
->files_lock
);
2332 spin_unlock(&card
->files_lock
);
2335 EXPORT_SYMBOL_HDA(snd_hda_lock_devices
);
2337 void snd_hda_unlock_devices(struct hda_bus
*bus
)
2339 struct snd_card
*card
= bus
->card
;
2342 spin_lock(&card
->files_lock
);
2344 spin_unlock(&card
->files_lock
);
2346 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices
);
2349 * snd_hda_codec_reset - Clear all objects assigned to the codec
2350 * @codec: HD-audio codec
2352 * This frees the all PCM and control elements assigned to the codec, and
2353 * clears the caches and restores the pin default configurations.
2355 * When a device is being used, it returns -EBSY. If successfully freed,
2358 int snd_hda_codec_reset(struct hda_codec
*codec
)
2360 struct hda_bus
*bus
= codec
->bus
;
2361 struct snd_card
*card
= bus
->card
;
2364 if (snd_hda_lock_devices(bus
) < 0)
2367 /* OK, let it free */
2368 cancel_delayed_work_sync(&codec
->jackpoll_work
);
2370 cancel_delayed_work_sync(&codec
->power_work
);
2371 codec
->power_on
= 0;
2372 codec
->power_transition
= 0;
2373 codec
->power_jiffies
= jiffies
;
2374 flush_workqueue(bus
->workq
);
2376 snd_hda_ctls_clear(codec
);
2378 for (i
= 0; i
< codec
->num_pcms
; i
++) {
2379 if (codec
->pcm_info
[i
].pcm
) {
2380 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
2381 clear_bit(codec
->pcm_info
[i
].device
,
2385 if (codec
->patch_ops
.free
)
2386 codec
->patch_ops
.free(codec
);
2387 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2388 snd_hda_jack_tbl_clear(codec
);
2389 codec
->proc_widget_hook
= NULL
;
2391 free_hda_cache(&codec
->amp_cache
);
2392 free_hda_cache(&codec
->cmd_cache
);
2393 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2394 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2395 /* free only driver_pins so that init_pins + user_pins are restored */
2396 snd_array_free(&codec
->driver_pins
);
2397 restore_pincfgs(codec
);
2398 snd_array_free(&codec
->cvt_setups
);
2399 snd_array_free(&codec
->spdif_out
);
2400 codec
->num_pcms
= 0;
2401 codec
->pcm_info
= NULL
;
2402 codec
->preset
= NULL
;
2403 codec
->slave_dig_outs
= NULL
;
2404 codec
->spdif_status_reset
= 0;
2405 module_put(codec
->owner
);
2406 codec
->owner
= NULL
;
2408 /* allow device access again */
2409 snd_hda_unlock_devices(bus
);
2413 typedef int (*map_slave_func_t
)(void *, struct snd_kcontrol
*);
2415 /* apply the function to all matching slave ctls in the mixer list */
2416 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2417 const char *suffix
, map_slave_func_t func
, void *data
)
2419 struct hda_nid_item
*items
;
2420 const char * const *s
;
2423 items
= codec
->mixers
.list
;
2424 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2425 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2426 if (!sctl
|| !sctl
->id
.name
||
2427 sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2429 for (s
= slaves
; *s
; s
++) {
2430 char tmpname
[sizeof(sctl
->id
.name
)];
2431 const char *name
= *s
;
2433 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
2437 if (!strcmp(sctl
->id
.name
, name
)) {
2438 err
= func(data
, sctl
);
2448 static int check_slave_present(void *data
, struct snd_kcontrol
*sctl
)
2453 /* guess the value corresponding to 0dB */
2454 static int get_kctl_0dB_offset(struct snd_kcontrol
*kctl
)
2457 const int *tlv
= NULL
;
2460 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
2461 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2462 mm_segment_t fs
= get_fs();
2464 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
2467 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
2469 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
)
2470 val
= -tlv
[2] / tlv
[3];
2474 /* call kctl->put with the given value(s) */
2475 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
2477 struct snd_ctl_elem_value
*ucontrol
;
2478 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
2481 ucontrol
->value
.integer
.value
[0] = val
;
2482 ucontrol
->value
.integer
.value
[1] = val
;
2483 kctl
->put(kctl
, ucontrol
);
2488 /* initialize the slave volume with 0dB */
2489 static int init_slave_0dB(void *data
, struct snd_kcontrol
*slave
)
2491 int offset
= get_kctl_0dB_offset(slave
);
2493 put_kctl_with_value(slave
, offset
);
2497 /* unmute the slave */
2498 static int init_slave_unmute(void *data
, struct snd_kcontrol
*slave
)
2500 return put_kctl_with_value(slave
, 1);
2504 * snd_hda_add_vmaster - create a virtual master control and add slaves
2505 * @codec: HD-audio codec
2506 * @name: vmaster control name
2507 * @tlv: TLV data (optional)
2508 * @slaves: slave control names (optional)
2509 * @suffix: suffix string to each slave name (optional)
2510 * @init_slave_vol: initialize slaves to unmute/0dB
2511 * @ctl_ret: store the vmaster kcontrol in return
2513 * Create a virtual master control with the given name. The TLV data
2514 * must be either NULL or a valid data.
2516 * @slaves is a NULL-terminated array of strings, each of which is a
2517 * slave control name. All controls with these names are assigned to
2518 * the new virtual master control.
2520 * This function returns zero if successful or a negative error code.
2522 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2523 unsigned int *tlv
, const char * const *slaves
,
2524 const char *suffix
, bool init_slave_vol
,
2525 struct snd_kcontrol
**ctl_ret
)
2527 struct snd_kcontrol
*kctl
;
2533 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
2535 snd_printdd("No slave found for %s\n", name
);
2538 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2541 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2545 err
= map_slaves(codec
, slaves
, suffix
,
2546 (map_slave_func_t
)snd_ctl_add_slave
, kctl
);
2550 /* init with master mute & zero volume */
2551 put_kctl_with_value(kctl
, 0);
2553 map_slaves(codec
, slaves
, suffix
,
2554 tlv
? init_slave_0dB
: init_slave_unmute
, kctl
);
2560 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster
);
2563 * mute-LED control using vmaster
2565 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
2566 struct snd_ctl_elem_info
*uinfo
)
2568 static const char * const texts
[] = {
2569 "Off", "On", "Follow Master"
2573 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2575 uinfo
->value
.enumerated
.items
= 3;
2576 index
= uinfo
->value
.enumerated
.item
;
2579 strcpy(uinfo
->value
.enumerated
.name
, texts
[index
]);
2583 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
2584 struct snd_ctl_elem_value
*ucontrol
)
2586 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2587 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
2591 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
2592 struct snd_ctl_elem_value
*ucontrol
)
2594 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2595 unsigned int old_mode
= hook
->mute_mode
;
2597 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
2598 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
2599 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2600 if (old_mode
== hook
->mute_mode
)
2602 snd_hda_sync_vmaster_hook(hook
);
2606 static struct snd_kcontrol_new vmaster_mute_mode
= {
2607 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2608 .name
= "Mute-LED Mode",
2609 .info
= vmaster_mute_mode_info
,
2610 .get
= vmaster_mute_mode_get
,
2611 .put
= vmaster_mute_mode_put
,
2615 * Add a mute-LED hook with the given vmaster switch kctl
2616 * "Mute-LED Mode" control is automatically created and associated with
2619 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
2620 struct hda_vmaster_mute_hook
*hook
,
2621 bool expose_enum_ctl
)
2623 struct snd_kcontrol
*kctl
;
2625 if (!hook
->hook
|| !hook
->sw_kctl
)
2627 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, hook
->hook
, codec
);
2628 hook
->codec
= codec
;
2629 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2630 if (!expose_enum_ctl
)
2632 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
2635 return snd_hda_ctl_add(codec
, 0, kctl
);
2637 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook
);
2640 * Call the hook with the current value for synchronization
2641 * Should be called in init callback
2643 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
2645 if (!hook
->hook
|| !hook
->codec
)
2647 switch (hook
->mute_mode
) {
2648 case HDA_VMUTE_FOLLOW_MASTER
:
2649 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
2652 hook
->hook(hook
->codec
, hook
->mute_mode
);
2656 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook
);
2660 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2662 * The control element is supposed to have the private_value field
2663 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2665 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2666 struct snd_ctl_elem_info
*uinfo
)
2668 int chs
= get_amp_channels(kcontrol
);
2670 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2671 uinfo
->count
= chs
== 3 ? 2 : 1;
2672 uinfo
->value
.integer
.min
= 0;
2673 uinfo
->value
.integer
.max
= 1;
2676 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
2679 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2681 * The control element is supposed to have the private_value field
2682 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2684 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2685 struct snd_ctl_elem_value
*ucontrol
)
2687 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2688 hda_nid_t nid
= get_amp_nid(kcontrol
);
2689 int chs
= get_amp_channels(kcontrol
);
2690 int dir
= get_amp_direction(kcontrol
);
2691 int idx
= get_amp_index(kcontrol
);
2692 long *valp
= ucontrol
->value
.integer
.value
;
2695 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2696 HDA_AMP_MUTE
) ? 0 : 1;
2698 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2699 HDA_AMP_MUTE
) ? 0 : 1;
2702 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
2705 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2707 * The control element is supposed to have the private_value field
2708 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2710 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2711 struct snd_ctl_elem_value
*ucontrol
)
2713 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2714 hda_nid_t nid
= get_amp_nid(kcontrol
);
2715 int chs
= get_amp_channels(kcontrol
);
2716 int dir
= get_amp_direction(kcontrol
);
2717 int idx
= get_amp_index(kcontrol
);
2718 long *valp
= ucontrol
->value
.integer
.value
;
2721 snd_hda_power_up(codec
);
2723 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2725 *valp
? 0 : HDA_AMP_MUTE
);
2729 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2731 *valp
? 0 : HDA_AMP_MUTE
);
2732 hda_call_check_power_status(codec
, nid
);
2733 snd_hda_power_down(codec
);
2736 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
2739 * bound volume controls
2741 * bind multiple volumes (# indices, from 0)
2744 #define AMP_VAL_IDX_SHIFT 19
2745 #define AMP_VAL_IDX_MASK (0x0f<<19)
2748 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2750 * The control element is supposed to have the private_value field
2751 * set up via HDA_BIND_MUTE*() macros.
2753 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2754 struct snd_ctl_elem_value
*ucontrol
)
2756 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2760 mutex_lock(&codec
->control_mutex
);
2761 pval
= kcontrol
->private_value
;
2762 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2763 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2764 kcontrol
->private_value
= pval
;
2765 mutex_unlock(&codec
->control_mutex
);
2768 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
2771 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2773 * The control element is supposed to have the private_value field
2774 * set up via HDA_BIND_MUTE*() macros.
2776 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2777 struct snd_ctl_elem_value
*ucontrol
)
2779 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2781 int i
, indices
, err
= 0, change
= 0;
2783 mutex_lock(&codec
->control_mutex
);
2784 pval
= kcontrol
->private_value
;
2785 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2786 for (i
= 0; i
< indices
; i
++) {
2787 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2788 (i
<< AMP_VAL_IDX_SHIFT
);
2789 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2794 kcontrol
->private_value
= pval
;
2795 mutex_unlock(&codec
->control_mutex
);
2796 return err
< 0 ? err
: change
;
2798 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
2801 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2803 * The control element is supposed to have the private_value field
2804 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2806 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2807 struct snd_ctl_elem_info
*uinfo
)
2809 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2810 struct hda_bind_ctls
*c
;
2813 mutex_lock(&codec
->control_mutex
);
2814 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2815 kcontrol
->private_value
= *c
->values
;
2816 err
= c
->ops
->info(kcontrol
, uinfo
);
2817 kcontrol
->private_value
= (long)c
;
2818 mutex_unlock(&codec
->control_mutex
);
2821 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
2824 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2826 * The control element is supposed to have the private_value field
2827 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2829 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2830 struct snd_ctl_elem_value
*ucontrol
)
2832 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2833 struct hda_bind_ctls
*c
;
2836 mutex_lock(&codec
->control_mutex
);
2837 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2838 kcontrol
->private_value
= *c
->values
;
2839 err
= c
->ops
->get(kcontrol
, ucontrol
);
2840 kcontrol
->private_value
= (long)c
;
2841 mutex_unlock(&codec
->control_mutex
);
2844 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
2847 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2849 * The control element is supposed to have the private_value field
2850 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2852 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2853 struct snd_ctl_elem_value
*ucontrol
)
2855 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2856 struct hda_bind_ctls
*c
;
2857 unsigned long *vals
;
2858 int err
= 0, change
= 0;
2860 mutex_lock(&codec
->control_mutex
);
2861 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2862 for (vals
= c
->values
; *vals
; vals
++) {
2863 kcontrol
->private_value
= *vals
;
2864 err
= c
->ops
->put(kcontrol
, ucontrol
);
2869 kcontrol
->private_value
= (long)c
;
2870 mutex_unlock(&codec
->control_mutex
);
2871 return err
< 0 ? err
: change
;
2873 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
2876 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2878 * The control element is supposed to have the private_value field
2879 * set up via HDA_BIND_VOL() macro.
2881 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2882 unsigned int size
, unsigned int __user
*tlv
)
2884 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2885 struct hda_bind_ctls
*c
;
2888 mutex_lock(&codec
->control_mutex
);
2889 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2890 kcontrol
->private_value
= *c
->values
;
2891 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
2892 kcontrol
->private_value
= (long)c
;
2893 mutex_unlock(&codec
->control_mutex
);
2896 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
2898 struct hda_ctl_ops snd_hda_bind_vol
= {
2899 .info
= snd_hda_mixer_amp_volume_info
,
2900 .get
= snd_hda_mixer_amp_volume_get
,
2901 .put
= snd_hda_mixer_amp_volume_put
,
2902 .tlv
= snd_hda_mixer_amp_tlv
2904 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
2906 struct hda_ctl_ops snd_hda_bind_sw
= {
2907 .info
= snd_hda_mixer_amp_switch_info
,
2908 .get
= snd_hda_mixer_amp_switch_get
,
2909 .put
= snd_hda_mixer_amp_switch_put
,
2910 .tlv
= snd_hda_mixer_amp_tlv
2912 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
2915 * SPDIF out controls
2918 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2919 struct snd_ctl_elem_info
*uinfo
)
2921 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2926 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2927 struct snd_ctl_elem_value
*ucontrol
)
2929 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2930 IEC958_AES0_NONAUDIO
|
2931 IEC958_AES0_CON_EMPHASIS_5015
|
2932 IEC958_AES0_CON_NOT_COPYRIGHT
;
2933 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2934 IEC958_AES1_CON_ORIGINAL
;
2938 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2939 struct snd_ctl_elem_value
*ucontrol
)
2941 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2942 IEC958_AES0_NONAUDIO
|
2943 IEC958_AES0_PRO_EMPHASIS_5015
;
2947 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2948 struct snd_ctl_elem_value
*ucontrol
)
2950 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2951 int idx
= kcontrol
->private_value
;
2952 struct hda_spdif_out
*spdif
;
2954 mutex_lock(&codec
->spdif_mutex
);
2955 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2956 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
2957 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
2958 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
2959 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
2960 mutex_unlock(&codec
->spdif_mutex
);
2965 /* convert from SPDIF status bits to HDA SPDIF bits
2966 * bit 0 (DigEn) is always set zero (to be filled later)
2968 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2970 unsigned short val
= 0;
2972 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2973 val
|= AC_DIG1_PROFESSIONAL
;
2974 if (sbits
& IEC958_AES0_NONAUDIO
)
2975 val
|= AC_DIG1_NONAUDIO
;
2976 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2977 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2978 IEC958_AES0_PRO_EMPHASIS_5015
)
2979 val
|= AC_DIG1_EMPHASIS
;
2981 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2982 IEC958_AES0_CON_EMPHASIS_5015
)
2983 val
|= AC_DIG1_EMPHASIS
;
2984 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2985 val
|= AC_DIG1_COPYRIGHT
;
2986 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2987 val
|= AC_DIG1_LEVEL
;
2988 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2993 /* convert to SPDIF status bits from HDA SPDIF bits
2995 static unsigned int convert_to_spdif_status(unsigned short val
)
2997 unsigned int sbits
= 0;
2999 if (val
& AC_DIG1_NONAUDIO
)
3000 sbits
|= IEC958_AES0_NONAUDIO
;
3001 if (val
& AC_DIG1_PROFESSIONAL
)
3002 sbits
|= IEC958_AES0_PROFESSIONAL
;
3003 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3004 if (sbits
& AC_DIG1_EMPHASIS
)
3005 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
3007 if (val
& AC_DIG1_EMPHASIS
)
3008 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
3009 if (!(val
& AC_DIG1_COPYRIGHT
))
3010 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
3011 if (val
& AC_DIG1_LEVEL
)
3012 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
3013 sbits
|= val
& (0x7f << 8);
3018 /* set digital convert verbs both for the given NID and its slaves */
3019 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
3024 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
3025 d
= codec
->slave_dig_outs
;
3029 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
3032 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
3036 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
3038 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
3041 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
3042 struct snd_ctl_elem_value
*ucontrol
)
3044 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3045 int idx
= kcontrol
->private_value
;
3046 struct hda_spdif_out
*spdif
;
3051 mutex_lock(&codec
->spdif_mutex
);
3052 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3054 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
3055 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
3056 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
3057 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
3058 val
= convert_from_spdif_status(spdif
->status
);
3059 val
|= spdif
->ctls
& 1;
3060 change
= spdif
->ctls
!= val
;
3062 if (change
&& nid
!= (u16
)-1)
3063 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3064 mutex_unlock(&codec
->spdif_mutex
);
3068 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3070 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
3071 struct snd_ctl_elem_value
*ucontrol
)
3073 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3074 int idx
= kcontrol
->private_value
;
3075 struct hda_spdif_out
*spdif
;
3077 mutex_lock(&codec
->spdif_mutex
);
3078 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3079 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
3080 mutex_unlock(&codec
->spdif_mutex
);
3084 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
3087 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
3088 /* unmute amp switch (if any) */
3089 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
3090 (dig1
& AC_DIG1_ENABLE
))
3091 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
3095 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
3096 struct snd_ctl_elem_value
*ucontrol
)
3098 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3099 int idx
= kcontrol
->private_value
;
3100 struct hda_spdif_out
*spdif
;
3105 mutex_lock(&codec
->spdif_mutex
);
3106 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3108 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
3109 if (ucontrol
->value
.integer
.value
[0])
3110 val
|= AC_DIG1_ENABLE
;
3111 change
= spdif
->ctls
!= val
;
3113 if (change
&& nid
!= (u16
)-1)
3114 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
3115 mutex_unlock(&codec
->spdif_mutex
);
3119 static struct snd_kcontrol_new dig_mixes
[] = {
3121 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3122 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3123 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
3124 .info
= snd_hda_spdif_mask_info
,
3125 .get
= snd_hda_spdif_cmask_get
,
3128 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3129 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3130 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
3131 .info
= snd_hda_spdif_mask_info
,
3132 .get
= snd_hda_spdif_pmask_get
,
3135 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3136 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
3137 .info
= snd_hda_spdif_mask_info
,
3138 .get
= snd_hda_spdif_default_get
,
3139 .put
= snd_hda_spdif_default_put
,
3142 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3143 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
3144 .info
= snd_hda_spdif_out_switch_info
,
3145 .get
= snd_hda_spdif_out_switch_get
,
3146 .put
= snd_hda_spdif_out_switch_put
,
3152 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3153 * @codec: the HDA codec
3154 * @associated_nid: NID that new ctls associated with
3155 * @cvt_nid: converter NID
3156 * @type: HDA_PCM_TYPE_*
3157 * Creates controls related with the digital output.
3158 * Called from each patch supporting the digital out.
3160 * Returns 0 if successful, or a negative error code.
3162 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
3163 hda_nid_t associated_nid
,
3168 struct snd_kcontrol
*kctl
;
3169 struct snd_kcontrol_new
*dig_mix
;
3171 const int spdif_pcm_dev
= 1;
3172 struct hda_spdif_out
*spdif
;
3174 if (codec
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
3175 type
== HDA_PCM_TYPE_SPDIF
) {
3176 dev
= spdif_pcm_dev
;
3177 } else if (codec
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
3178 type
== HDA_PCM_TYPE_HDMI
) {
3179 for (idx
= 0; idx
< codec
->spdif_out
.used
; idx
++) {
3180 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3181 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3182 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, idx
);
3185 kctl
->id
.device
= spdif_pcm_dev
;
3188 codec
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
3190 if (!codec
->primary_dig_out_type
)
3191 codec
->primary_dig_out_type
= type
;
3193 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", dev
);
3195 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
3198 spdif
= snd_array_new(&codec
->spdif_out
);
3199 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3200 kctl
= snd_ctl_new1(dig_mix
, codec
);
3203 kctl
->id
.device
= dev
;
3204 kctl
->id
.index
= idx
;
3205 kctl
->private_value
= codec
->spdif_out
.used
- 1;
3206 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
3210 spdif
->nid
= cvt_nid
;
3211 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
3212 AC_VERB_GET_DIGI_CONVERT_1
, 0);
3213 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
3216 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls
);
3218 /* get the hda_spdif_out entry from the given NID
3219 * call within spdif_mutex lock
3221 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
3225 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
3226 struct hda_spdif_out
*spdif
=
3227 snd_array_elem(&codec
->spdif_out
, i
);
3228 if (spdif
->nid
== nid
)
3233 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid
);
3235 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
3237 struct hda_spdif_out
*spdif
;
3239 mutex_lock(&codec
->spdif_mutex
);
3240 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3241 spdif
->nid
= (u16
)-1;
3242 mutex_unlock(&codec
->spdif_mutex
);
3244 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign
);
3246 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
3248 struct hda_spdif_out
*spdif
;
3251 mutex_lock(&codec
->spdif_mutex
);
3252 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3253 if (spdif
->nid
!= nid
) {
3256 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3258 mutex_unlock(&codec
->spdif_mutex
);
3260 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign
);
3263 * SPDIF sharing with analog output
3265 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
3266 struct snd_ctl_elem_value
*ucontrol
)
3268 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3269 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
3273 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
3274 struct snd_ctl_elem_value
*ucontrol
)
3276 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3277 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
3281 static struct snd_kcontrol_new spdif_share_sw
= {
3282 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3283 .name
= "IEC958 Default PCM Playback Switch",
3284 .info
= snd_ctl_boolean_mono_info
,
3285 .get
= spdif_share_sw_get
,
3286 .put
= spdif_share_sw_put
,
3290 * snd_hda_create_spdif_share_sw - create Default PCM switch
3291 * @codec: the HDA codec
3292 * @mout: multi-out instance
3294 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
3295 struct hda_multi_out
*mout
)
3297 if (!mout
->dig_out_nid
)
3299 /* ATTENTION: here mout is passed as private_data, instead of codec */
3300 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
,
3301 snd_ctl_new1(&spdif_share_sw
, mout
));
3303 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
3309 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3311 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3312 struct snd_ctl_elem_value
*ucontrol
)
3314 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3316 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3320 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3321 struct snd_ctl_elem_value
*ucontrol
)
3323 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3324 hda_nid_t nid
= kcontrol
->private_value
;
3325 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3328 mutex_lock(&codec
->spdif_mutex
);
3329 change
= codec
->spdif_in_enable
!= val
;
3331 codec
->spdif_in_enable
= val
;
3332 snd_hda_codec_write_cache(codec
, nid
, 0,
3333 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3335 mutex_unlock(&codec
->spdif_mutex
);
3339 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3340 struct snd_ctl_elem_value
*ucontrol
)
3342 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3343 hda_nid_t nid
= kcontrol
->private_value
;
3347 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3348 sbits
= convert_to_spdif_status(val
);
3349 ucontrol
->value
.iec958
.status
[0] = sbits
;
3350 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3351 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3352 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3356 static struct snd_kcontrol_new dig_in_ctls
[] = {
3358 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3359 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3360 .info
= snd_hda_spdif_in_switch_info
,
3361 .get
= snd_hda_spdif_in_switch_get
,
3362 .put
= snd_hda_spdif_in_switch_put
,
3365 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3366 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3367 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3368 .info
= snd_hda_spdif_mask_info
,
3369 .get
= snd_hda_spdif_in_status_get
,
3375 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3376 * @codec: the HDA codec
3377 * @nid: audio in widget NID
3379 * Creates controls related with the SPDIF input.
3380 * Called from each patch supporting the SPDIF in.
3382 * Returns 0 if successful, or a negative error code.
3384 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3387 struct snd_kcontrol
*kctl
;
3388 struct snd_kcontrol_new
*dig_mix
;
3391 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
3393 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
3396 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3397 kctl
= snd_ctl_new1(dig_mix
, codec
);
3400 kctl
->private_value
= nid
;
3401 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3405 codec
->spdif_in_enable
=
3406 snd_hda_codec_read(codec
, nid
, 0,
3407 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3411 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
3418 /* build a 32bit cache key with the widget id and the command parameter */
3419 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3420 #define get_cmd_cache_nid(key) ((key) & 0xff)
3421 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3424 * snd_hda_codec_write_cache - send a single command with caching
3425 * @codec: the HDA codec
3426 * @nid: NID to send the command
3427 * @direct: direct flag
3428 * @verb: the verb to send
3429 * @parm: the parameter for the verb
3431 * Send a single command without waiting for response.
3433 * Returns 0 if successful, or a negative error code.
3435 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3436 int direct
, unsigned int verb
, unsigned int parm
)
3438 int err
= snd_hda_codec_write(codec
, nid
, direct
, verb
, parm
);
3439 struct hda_cache_head
*c
;
3444 /* parm may contain the verb stuff for get/set amp */
3445 verb
= verb
| (parm
>> 8);
3447 key
= build_cmd_cache_key(nid
, verb
);
3448 mutex_lock(&codec
->bus
->cmd_mutex
);
3449 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3452 mutex_unlock(&codec
->bus
->cmd_mutex
);
3455 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
3458 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3459 * @codec: the HDA codec
3460 * @nid: NID to send the command
3461 * @direct: direct flag
3462 * @verb: the verb to send
3463 * @parm: the parameter for the verb
3465 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3466 * command if the parameter is already identical with the cached value.
3467 * If not, it sends the command and refreshes the cache.
3469 * Returns 0 if successful, or a negative error code.
3471 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3472 int direct
, unsigned int verb
, unsigned int parm
)
3474 struct hda_cache_head
*c
;
3477 /* parm may contain the verb stuff for get/set amp */
3478 verb
= verb
| (parm
>> 8);
3480 key
= build_cmd_cache_key(nid
, verb
);
3481 mutex_lock(&codec
->bus
->cmd_mutex
);
3482 c
= get_hash(&codec
->cmd_cache
, key
);
3483 if (c
&& c
->val
== parm
) {
3484 mutex_unlock(&codec
->bus
->cmd_mutex
);
3487 mutex_unlock(&codec
->bus
->cmd_mutex
);
3488 return snd_hda_codec_write_cache(codec
, nid
, direct
, verb
, parm
);
3490 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache
);
3493 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3494 * @codec: HD-audio codec
3496 * Execute all verbs recorded in the command caches to resume.
3498 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3500 struct hda_cache_head
*buffer
= codec
->cmd_cache
.buf
.list
;
3503 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++, buffer
++) {
3504 u32 key
= buffer
->key
;
3507 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3508 get_cmd_cache_cmd(key
), buffer
->val
);
3511 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
3514 * snd_hda_sequence_write_cache - sequence writes with caching
3515 * @codec: the HDA codec
3516 * @seq: VERB array to send
3518 * Send the commands sequentially from the given array.
3519 * Thte commands are recorded on cache for power-save and resume.
3520 * The array must be terminated with NID=0.
3522 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3523 const struct hda_verb
*seq
)
3525 for (; seq
->nid
; seq
++)
3526 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3529 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
3530 #endif /* CONFIG_PM */
3532 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
3533 unsigned int power_state
,
3534 bool eapd_workaround
)
3536 hda_nid_t nid
= codec
->start_nid
;
3539 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3540 unsigned int wcaps
= get_wcaps(codec
, nid
);
3541 if (!(wcaps
& AC_WCAP_POWER
))
3543 /* don't power down the widget if it controls eapd and
3544 * EAPD_BTLENABLE is set.
3546 if (eapd_workaround
&& power_state
== AC_PWRST_D3
&&
3547 get_wcaps_type(wcaps
) == AC_WID_PIN
&&
3548 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
3549 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
3550 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3554 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
3558 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all
);
3561 * supported power states check
3563 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
, hda_nid_t fg
,
3564 unsigned int power_state
)
3566 int sup
= snd_hda_param_read(codec
, fg
, AC_PAR_POWER_STATE
);
3570 if (sup
& power_state
)
3577 * wait until the state is reached, returns the current state
3579 static unsigned int hda_sync_power_state(struct hda_codec
*codec
,
3581 unsigned int power_state
)
3583 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
3584 unsigned int state
, actual_state
;
3587 state
= snd_hda_codec_read(codec
, fg
, 0,
3588 AC_VERB_GET_POWER_STATE
, 0);
3589 if (state
& AC_PWRST_ERROR
)
3591 actual_state
= (state
>> 4) & 0x0f;
3592 if (actual_state
== power_state
)
3594 if (time_after_eq(jiffies
, end_time
))
3596 /* wait until the codec reachs to the target state */
3603 * set power state of the codec, and return the power state
3605 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
3606 unsigned int power_state
)
3608 hda_nid_t fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
3612 /* this delay seems necessary to avoid click noise at power-down */
3613 if (power_state
== AC_PWRST_D3
) {
3614 /* transition time less than 10ms for power down */
3615 msleep(codec
->epss
? 10 : 100);
3618 /* repeat power states setting at most 10 times*/
3619 for (count
= 0; count
< 10; count
++) {
3620 if (codec
->patch_ops
.set_power_state
)
3621 codec
->patch_ops
.set_power_state(codec
, fg
,
3624 snd_hda_codec_read(codec
, fg
, 0,
3625 AC_VERB_SET_POWER_STATE
,
3627 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
,
3630 state
= hda_sync_power_state(codec
, fg
, power_state
);
3631 if (!(state
& AC_PWRST_ERROR
))
3638 #ifdef CONFIG_SND_HDA_HWDEP
3639 /* execute additional init verbs */
3640 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3642 if (codec
->init_verbs
.list
)
3643 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3646 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3651 * call suspend and power-down; used both from PM and power-save
3652 * this function returns the power state in the end
3654 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
, bool in_wq
)
3660 if (codec
->patch_ops
.suspend
)
3661 codec
->patch_ops
.suspend(codec
);
3662 hda_cleanup_all_streams(codec
);
3663 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
3664 /* Cancel delayed work if we aren't currently running from it. */
3666 cancel_delayed_work_sync(&codec
->power_work
);
3667 spin_lock(&codec
->power_lock
);
3668 snd_hda_update_power_acct(codec
);
3669 trace_hda_power_down(codec
);
3670 codec
->power_on
= 0;
3671 codec
->power_transition
= 0;
3672 codec
->power_jiffies
= jiffies
;
3673 spin_unlock(&codec
->power_lock
);
3679 * kick up codec; used both from PM and power-save
3681 static void hda_call_codec_resume(struct hda_codec
*codec
)
3685 /* set as if powered on for avoiding re-entering the resume
3686 * in the resume / power-save sequence
3688 hda_keep_power_on(codec
);
3689 hda_set_power_state(codec
, AC_PWRST_D0
);
3690 restore_pincfgs(codec
); /* restore all current pin configs */
3691 restore_shutup_pins(codec
);
3692 hda_exec_init_verbs(codec
);
3693 if (codec
->patch_ops
.resume
)
3694 codec
->patch_ops
.resume(codec
);
3696 if (codec
->patch_ops
.init
)
3697 codec
->patch_ops
.init(codec
);
3698 snd_hda_codec_resume_amp(codec
);
3699 snd_hda_codec_resume_cache(codec
);
3702 if (codec
->jackpoll_interval
)
3703 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3705 snd_hda_jack_set_dirty_all(codec
);
3706 snd_hda_jack_report_sync(codec
);
3710 snd_hda_power_down(codec
); /* flag down before returning */
3712 #endif /* CONFIG_PM */
3716 * snd_hda_build_controls - build mixer controls
3719 * Creates mixer controls for each codec included in the bus.
3721 * Returns 0 if successful, otherwise a negative error code.
3723 int /*__devinit*/ snd_hda_build_controls(struct hda_bus
*bus
)
3725 struct hda_codec
*codec
;
3727 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3728 int err
= snd_hda_codec_build_controls(codec
);
3730 printk(KERN_ERR
"hda_codec: cannot build controls "
3731 "for #%d (error %d)\n", codec
->addr
, err
);
3732 err
= snd_hda_codec_reset(codec
);
3735 "hda_codec: cannot revert codec\n");
3742 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
3745 * add standard channel maps if not specified
3747 static int add_std_chmaps(struct hda_codec
*codec
)
3751 for (i
= 0; i
< codec
->num_pcms
; i
++) {
3752 for (str
= 0; str
< 2; str
++) {
3753 struct snd_pcm
*pcm
= codec
->pcm_info
[i
].pcm
;
3754 struct hda_pcm_stream
*hinfo
=
3755 &codec
->pcm_info
[i
].stream
[str
];
3756 struct snd_pcm_chmap
*chmap
;
3757 const struct snd_pcm_chmap_elem
*elem
;
3759 if (codec
->pcm_info
[i
].own_chmap
)
3761 if (!pcm
|| !hinfo
->substreams
)
3763 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
3764 err
= snd_pcm_add_chmap_ctls(pcm
, str
, elem
,
3765 hinfo
->channels_max
,
3769 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
3775 /* default channel maps for 2.1 speakers;
3776 * since HD-audio supports only stereo, odd number channels are omitted
3778 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
3780 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
3782 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
3783 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
3786 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
3788 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
3791 hda_exec_init_verbs(codec
);
3792 /* continue to initialize... */
3793 if (codec
->patch_ops
.init
)
3794 err
= codec
->patch_ops
.init(codec
);
3795 if (!err
&& codec
->patch_ops
.build_controls
)
3796 err
= codec
->patch_ops
.build_controls(codec
);
3800 /* we create chmaps here instead of build_pcms */
3801 err
= add_std_chmaps(codec
);
3805 if (codec
->jackpoll_interval
)
3806 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3808 snd_hda_jack_report_sync(codec
); /* call at the last init point */
3815 struct hda_rate_tbl
{
3817 unsigned int alsa_bits
;
3818 unsigned int hda_fmt
;
3821 /* rate = base * mult / div */
3822 #define HDA_RATE(base, mult, div) \
3823 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3824 (((div) - 1) << AC_FMT_DIV_SHIFT))
3826 static struct hda_rate_tbl rate_bits
[] = {
3827 /* rate in Hz, ALSA rate bitmask, HDA format value */
3829 /* autodetected value used in snd_hda_query_supported_pcm */
3830 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
3831 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
3832 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
3833 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
3834 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
3835 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
3836 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
3837 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
3838 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
3839 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
3840 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
3841 #define AC_PAR_PCM_RATE_BITS 11
3842 /* up to bits 10, 384kHZ isn't supported properly */
3844 /* not autodetected value */
3845 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
3847 { 0 } /* terminator */
3851 * snd_hda_calc_stream_format - calculate format bitset
3852 * @rate: the sample rate
3853 * @channels: the number of channels
3854 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3855 * @maxbps: the max. bps
3857 * Calculate the format bitset from the given rate, channels and th PCM format.
3859 * Return zero if invalid.
3861 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
3862 unsigned int channels
,
3863 unsigned int format
,
3864 unsigned int maxbps
,
3865 unsigned short spdif_ctls
)
3868 unsigned int val
= 0;
3870 for (i
= 0; rate_bits
[i
].hz
; i
++)
3871 if (rate_bits
[i
].hz
== rate
) {
3872 val
= rate_bits
[i
].hda_fmt
;
3875 if (!rate_bits
[i
].hz
) {
3876 snd_printdd("invalid rate %d\n", rate
);
3880 if (channels
== 0 || channels
> 8) {
3881 snd_printdd("invalid channels %d\n", channels
);
3884 val
|= channels
- 1;
3886 switch (snd_pcm_format_width(format
)) {
3888 val
|= AC_FMT_BITS_8
;
3891 val
|= AC_FMT_BITS_16
;
3896 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
3897 val
|= AC_FMT_BITS_32
;
3898 else if (maxbps
>= 24)
3899 val
|= AC_FMT_BITS_24
;
3901 val
|= AC_FMT_BITS_20
;
3904 snd_printdd("invalid format width %d\n",
3905 snd_pcm_format_width(format
));
3909 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
3910 val
|= AC_FMT_TYPE_NON_PCM
;
3914 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
3916 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
,
3919 unsigned int val
= 0;
3920 if (nid
!= codec
->afg
&&
3921 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
3922 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
3923 if (!val
|| val
== -1)
3924 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
3925 if (!val
|| val
== -1)
3930 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3932 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARPCM_KEY(nid
),
3936 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
,
3939 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
3940 if (!streams
|| streams
== -1)
3941 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
3942 if (!streams
|| streams
== -1)
3947 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3949 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARSTR_KEY(nid
),
3954 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3955 * @codec: the HDA codec
3956 * @nid: NID to query
3957 * @ratesp: the pointer to store the detected rate bitflags
3958 * @formatsp: the pointer to store the detected formats
3959 * @bpsp: the pointer to store the detected format widths
3961 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3962 * or @bsps argument is ignored.
3964 * Returns 0 if successful, otherwise a negative error code.
3966 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
3967 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
3969 unsigned int i
, val
, wcaps
;
3971 wcaps
= get_wcaps(codec
, nid
);
3972 val
= query_pcm_param(codec
, nid
);
3976 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
3978 rates
|= rate_bits
[i
].alsa_bits
;
3981 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
3982 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3984 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
3990 if (formatsp
|| bpsp
) {
3992 unsigned int streams
, bps
;
3994 streams
= query_stream_param(codec
, nid
);
3999 if (streams
& AC_SUPFMT_PCM
) {
4000 if (val
& AC_SUPPCM_BITS_8
) {
4001 formats
|= SNDRV_PCM_FMTBIT_U8
;
4004 if (val
& AC_SUPPCM_BITS_16
) {
4005 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
4008 if (wcaps
& AC_WCAP_DIGITAL
) {
4009 if (val
& AC_SUPPCM_BITS_32
)
4010 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
4011 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
4012 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4013 if (val
& AC_SUPPCM_BITS_24
)
4015 else if (val
& AC_SUPPCM_BITS_20
)
4017 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
4018 AC_SUPPCM_BITS_32
)) {
4019 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4020 if (val
& AC_SUPPCM_BITS_32
)
4022 else if (val
& AC_SUPPCM_BITS_24
)
4024 else if (val
& AC_SUPPCM_BITS_20
)
4028 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4029 if (streams
& AC_SUPFMT_FLOAT32
) {
4030 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
4035 if (streams
== AC_SUPFMT_AC3
) {
4036 /* should be exclusive */
4037 /* temporary hack: we have still no proper support
4038 * for the direct AC3 stream...
4040 formats
|= SNDRV_PCM_FMTBIT_U8
;
4044 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
4045 "(nid=0x%x, val=0x%x, ovrd=%i, "
4048 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
4053 *formatsp
= formats
;
4060 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm
);
4063 * snd_hda_is_supported_format - Check the validity of the format
4064 * @codec: HD-audio codec
4065 * @nid: NID to check
4066 * @format: the HD-audio format value to check
4068 * Check whether the given node supports the format value.
4070 * Returns 1 if supported, 0 if not.
4072 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
4073 unsigned int format
)
4076 unsigned int val
= 0, rate
, stream
;
4078 val
= query_pcm_param(codec
, nid
);
4082 rate
= format
& 0xff00;
4083 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
4084 if (rate_bits
[i
].hda_fmt
== rate
) {
4089 if (i
>= AC_PAR_PCM_RATE_BITS
)
4092 stream
= query_stream_param(codec
, nid
);
4096 if (stream
& AC_SUPFMT_PCM
) {
4097 switch (format
& 0xf0) {
4099 if (!(val
& AC_SUPPCM_BITS_8
))
4103 if (!(val
& AC_SUPPCM_BITS_16
))
4107 if (!(val
& AC_SUPPCM_BITS_20
))
4111 if (!(val
& AC_SUPPCM_BITS_24
))
4115 if (!(val
& AC_SUPPCM_BITS_32
))
4122 /* FIXME: check for float32 and AC3? */
4127 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
4132 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
4133 struct hda_codec
*codec
,
4134 struct snd_pcm_substream
*substream
)
4139 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
4140 struct hda_codec
*codec
,
4141 unsigned int stream_tag
,
4142 unsigned int format
,
4143 struct snd_pcm_substream
*substream
)
4145 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
4149 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
4150 struct hda_codec
*codec
,
4151 struct snd_pcm_substream
*substream
)
4153 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
4157 static int set_pcm_default_values(struct hda_codec
*codec
,
4158 struct hda_pcm_stream
*info
)
4162 /* query support PCM information from the given NID */
4163 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
4164 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
4165 info
->rates
? NULL
: &info
->rates
,
4166 info
->formats
? NULL
: &info
->formats
,
4167 info
->maxbps
? NULL
: &info
->maxbps
);
4171 if (info
->ops
.open
== NULL
)
4172 info
->ops
.open
= hda_pcm_default_open_close
;
4173 if (info
->ops
.close
== NULL
)
4174 info
->ops
.close
= hda_pcm_default_open_close
;
4175 if (info
->ops
.prepare
== NULL
) {
4176 if (snd_BUG_ON(!info
->nid
))
4178 info
->ops
.prepare
= hda_pcm_default_prepare
;
4180 if (info
->ops
.cleanup
== NULL
) {
4181 if (snd_BUG_ON(!info
->nid
))
4183 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
4189 * codec prepare/cleanup entries
4191 int snd_hda_codec_prepare(struct hda_codec
*codec
,
4192 struct hda_pcm_stream
*hinfo
,
4193 unsigned int stream
,
4194 unsigned int format
,
4195 struct snd_pcm_substream
*substream
)
4198 mutex_lock(&codec
->bus
->prepare_mutex
);
4199 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
, substream
);
4201 purify_inactive_streams(codec
);
4202 mutex_unlock(&codec
->bus
->prepare_mutex
);
4205 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare
);
4207 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
4208 struct hda_pcm_stream
*hinfo
,
4209 struct snd_pcm_substream
*substream
)
4211 mutex_lock(&codec
->bus
->prepare_mutex
);
4212 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
4213 mutex_unlock(&codec
->bus
->prepare_mutex
);
4215 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup
);
4218 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
4219 "Audio", "SPDIF", "HDMI", "Modem"
4223 * get the empty PCM device number to assign
4225 * note the max device number is limited by HDA_MAX_PCMS, currently 10
4227 static int get_empty_pcm_device(struct hda_bus
*bus
, int type
)
4229 /* audio device indices; not linear to keep compatibility */
4230 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
4231 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
4232 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
4233 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
4234 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
4238 if (type
>= HDA_PCM_NTYPES
) {
4239 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
4243 for (i
= 0; audio_idx
[type
][i
] >= 0 ; i
++)
4244 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
4245 return audio_idx
[type
][i
];
4247 /* non-fixed slots starting from 10 */
4248 for (i
= 10; i
< 32; i
++) {
4249 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
4253 snd_printk(KERN_WARNING
"Too many %s devices\n",
4254 snd_hda_pcm_type_name
[type
]);
4259 * attach a new PCM stream
4261 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
4263 struct hda_bus
*bus
= codec
->bus
;
4264 struct hda_pcm_stream
*info
;
4267 if (snd_BUG_ON(!pcm
->name
))
4269 for (stream
= 0; stream
< 2; stream
++) {
4270 info
= &pcm
->stream
[stream
];
4271 if (info
->substreams
) {
4272 err
= set_pcm_default_values(codec
, info
);
4277 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
4280 /* assign all PCMs of the given codec */
4281 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
4286 if (!codec
->num_pcms
) {
4287 if (!codec
->patch_ops
.build_pcms
)
4289 err
= codec
->patch_ops
.build_pcms(codec
);
4291 printk(KERN_ERR
"hda_codec: cannot build PCMs"
4292 "for #%d (error %d)\n", codec
->addr
, err
);
4293 err
= snd_hda_codec_reset(codec
);
4296 "hda_codec: cannot revert codec\n");
4301 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
4302 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
4305 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
4306 continue; /* no substreams assigned */
4309 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
4311 continue; /* no fatal error */
4313 err
= snd_hda_attach_pcm(codec
, cpcm
);
4315 printk(KERN_ERR
"hda_codec: cannot attach "
4316 "PCM stream %d for codec #%d\n",
4318 continue; /* no fatal error */
4326 * snd_hda_build_pcms - build PCM information
4329 * Create PCM information for each codec included in the bus.
4331 * The build_pcms codec patch is requested to set up codec->num_pcms and
4332 * codec->pcm_info properly. The array is referred by the top-level driver
4333 * to create its PCM instances.
4334 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4337 * At least, substreams, channels_min and channels_max must be filled for
4338 * each stream. substreams = 0 indicates that the stream doesn't exist.
4339 * When rates and/or formats are zero, the supported values are queried
4340 * from the given nid. The nid is used also by the default ops.prepare
4341 * and ops.cleanup callbacks.
4343 * The driver needs to call ops.open in its open callback. Similarly,
4344 * ops.close is supposed to be called in the close callback.
4345 * ops.prepare should be called in the prepare or hw_params callback
4346 * with the proper parameters for set up.
4347 * ops.cleanup should be called in hw_free for clean up of streams.
4349 * This function returns 0 if successful, or a negative error code.
4351 int snd_hda_build_pcms(struct hda_bus
*bus
)
4353 struct hda_codec
*codec
;
4355 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4356 int err
= snd_hda_codec_build_pcms(codec
);
4362 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
4365 * snd_hda_check_board_config - compare the current codec with the config table
4366 * @codec: the HDA codec
4367 * @num_configs: number of config enums
4368 * @models: array of model name strings
4369 * @tbl: configuration table, terminated by null entries
4371 * Compares the modelname or PCI subsystem id of the current codec with the
4372 * given configuration table. If a matching entry is found, returns its
4373 * config value (supposed to be 0 or positive).
4375 * If no entries are matching, the function returns a negative value.
4377 int snd_hda_check_board_config(struct hda_codec
*codec
,
4378 int num_configs
, const char * const *models
,
4379 const struct snd_pci_quirk
*tbl
)
4381 if (codec
->modelname
&& models
) {
4383 for (i
= 0; i
< num_configs
; i
++) {
4385 !strcmp(codec
->modelname
, models
[i
])) {
4386 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
4387 "selected\n", models
[i
]);
4393 if (!codec
->bus
->pci
|| !tbl
)
4396 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
4399 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4400 #ifdef CONFIG_SND_DEBUG_VERBOSE
4402 const char *model
= NULL
;
4404 model
= models
[tbl
->value
];
4406 sprintf(tmp
, "#%d", tbl
->value
);
4409 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4410 "for config %x:%x (%s)\n",
4411 model
, tbl
->subvendor
, tbl
->subdevice
,
4412 (tbl
->name
? tbl
->name
: "Unknown device"));
4418 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
4421 * snd_hda_check_board_codec_sid_config - compare the current codec
4422 subsystem ID with the
4425 This is important for Gateway notebooks with SB450 HDA Audio
4426 where the vendor ID of the PCI device is:
4427 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4428 and the vendor/subvendor are found only at the codec.
4430 * @codec: the HDA codec
4431 * @num_configs: number of config enums
4432 * @models: array of model name strings
4433 * @tbl: configuration table, terminated by null entries
4435 * Compares the modelname or PCI subsystem id of the current codec with the
4436 * given configuration table. If a matching entry is found, returns its
4437 * config value (supposed to be 0 or positive).
4439 * If no entries are matching, the function returns a negative value.
4441 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
4442 int num_configs
, const char * const *models
,
4443 const struct snd_pci_quirk
*tbl
)
4445 const struct snd_pci_quirk
*q
;
4447 /* Search for codec ID */
4448 for (q
= tbl
; q
->subvendor
; q
++) {
4449 unsigned int mask
= 0xffff0000 | q
->subdevice_mask
;
4450 unsigned int id
= (q
->subdevice
| (q
->subvendor
<< 16)) & mask
;
4451 if ((codec
->subsystem_id
& mask
) == id
)
4460 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4461 #ifdef CONFIG_SND_DEBUG_VERBOSE
4463 const char *model
= NULL
;
4465 model
= models
[tbl
->value
];
4467 sprintf(tmp
, "#%d", tbl
->value
);
4470 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4471 "for config %x:%x (%s)\n",
4472 model
, tbl
->subvendor
, tbl
->subdevice
,
4473 (tbl
->name
? tbl
->name
: "Unknown device"));
4479 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
4482 * snd_hda_add_new_ctls - create controls from the array
4483 * @codec: the HDA codec
4484 * @knew: the array of struct snd_kcontrol_new
4486 * This helper function creates and add new controls in the given array.
4487 * The array must be terminated with an empty entry as terminator.
4489 * Returns 0 if successful, or a negative error code.
4491 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4492 const struct snd_kcontrol_new
*knew
)
4496 for (; knew
->name
; knew
++) {
4497 struct snd_kcontrol
*kctl
;
4498 int addr
= 0, idx
= 0;
4499 if (knew
->iface
== -1) /* skip this codec private value */
4502 kctl
= snd_ctl_new1(knew
, codec
);
4506 kctl
->id
.device
= addr
;
4508 kctl
->id
.index
= idx
;
4509 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4512 /* try first with another device index corresponding to
4513 * the codec addr; if it still fails (or it's the
4514 * primary codec), then try another control index
4516 if (!addr
&& codec
->addr
)
4518 else if (!idx
&& !knew
->index
) {
4519 idx
= find_empty_mixer_ctl_idx(codec
,
4529 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
4532 static void hda_power_work(struct work_struct
*work
)
4534 struct hda_codec
*codec
=
4535 container_of(work
, struct hda_codec
, power_work
.work
);
4536 struct hda_bus
*bus
= codec
->bus
;
4539 spin_lock(&codec
->power_lock
);
4540 if (codec
->power_transition
> 0) { /* during power-up sequence? */
4541 spin_unlock(&codec
->power_lock
);
4544 if (!codec
->power_on
|| codec
->power_count
) {
4545 codec
->power_transition
= 0;
4546 spin_unlock(&codec
->power_lock
);
4549 spin_unlock(&codec
->power_lock
);
4551 state
= hda_call_codec_suspend(codec
, true);
4552 codec
->pm_down_notified
= 0;
4553 if (!bus
->power_keep_link_on
&& (state
& AC_PWRST_CLK_STOP_OK
)) {
4554 codec
->pm_down_notified
= 1;
4555 hda_call_pm_notify(bus
, false);
4559 static void hda_keep_power_on(struct hda_codec
*codec
)
4561 spin_lock(&codec
->power_lock
);
4562 codec
->power_count
++;
4563 codec
->power_on
= 1;
4564 codec
->power_jiffies
= jiffies
;
4565 spin_unlock(&codec
->power_lock
);
4568 /* update the power on/off account with the current jiffies */
4569 void snd_hda_update_power_acct(struct hda_codec
*codec
)
4571 unsigned long delta
= jiffies
- codec
->power_jiffies
;
4572 if (codec
->power_on
)
4573 codec
->power_on_acct
+= delta
;
4575 codec
->power_off_acct
+= delta
;
4576 codec
->power_jiffies
+= delta
;
4579 /* Transition to powered up, if wait_power_down then wait for a pending
4580 * transition to D3 to complete. A pending D3 transition is indicated
4581 * with power_transition == -1. */
4582 /* call this with codec->power_lock held! */
4583 static void __snd_hda_power_up(struct hda_codec
*codec
, bool wait_power_down
)
4585 struct hda_bus
*bus
= codec
->bus
;
4587 /* Return if power_on or transitioning to power_on, unless currently
4589 if ((codec
->power_on
|| codec
->power_transition
> 0) &&
4590 !(wait_power_down
&& codec
->power_transition
< 0))
4592 spin_unlock(&codec
->power_lock
);
4594 cancel_delayed_work_sync(&codec
->power_work
);
4596 spin_lock(&codec
->power_lock
);
4597 /* If the power down delayed work was cancelled above before starting,
4598 * then there is no need to go through power up here.
4600 if (codec
->power_on
) {
4601 if (codec
->power_transition
< 0)
4602 codec
->power_transition
= 0;
4606 trace_hda_power_up(codec
);
4607 snd_hda_update_power_acct(codec
);
4608 codec
->power_on
= 1;
4609 codec
->power_jiffies
= jiffies
;
4610 codec
->power_transition
= 1; /* avoid reentrance */
4611 spin_unlock(&codec
->power_lock
);
4613 if (codec
->pm_down_notified
) {
4614 codec
->pm_down_notified
= 0;
4615 hda_call_pm_notify(bus
, true);
4618 hda_call_codec_resume(codec
);
4620 spin_lock(&codec
->power_lock
);
4621 codec
->power_transition
= 0;
4624 #define power_save(codec) \
4625 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4627 /* Transition to powered down */
4628 static void __snd_hda_power_down(struct hda_codec
*codec
)
4630 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
4633 if (power_save(codec
)) {
4634 codec
->power_transition
= -1; /* avoid reentrance */
4635 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
4636 msecs_to_jiffies(power_save(codec
) * 1000));
4641 * snd_hda_power_save - Power-up/down/sync the codec
4642 * @codec: HD-audio codec
4643 * @delta: the counter delta to change
4645 * Change the power-up counter via @delta, and power up or down the hardware
4646 * appropriately. For the power-down, queue to the delayed action.
4647 * Passing zero to @delta means to synchronize the power state.
4649 void snd_hda_power_save(struct hda_codec
*codec
, int delta
, bool d3wait
)
4651 spin_lock(&codec
->power_lock
);
4652 codec
->power_count
+= delta
;
4653 trace_hda_power_count(codec
);
4655 __snd_hda_power_up(codec
, d3wait
);
4657 __snd_hda_power_down(codec
);
4658 spin_unlock(&codec
->power_lock
);
4660 EXPORT_SYMBOL_HDA(snd_hda_power_save
);
4663 * snd_hda_check_amp_list_power - Check the amp list and update the power
4664 * @codec: HD-audio codec
4665 * @check: the object containing an AMP list and the status
4666 * @nid: NID to check / update
4668 * Check whether the given NID is in the amp list. If it's in the list,
4669 * check the current AMP status, and update the the power-status according
4670 * to the mute status.
4672 * This function is supposed to be set or called from the check_power_status
4675 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4676 struct hda_loopback_check
*check
,
4679 const struct hda_amp_list
*p
;
4682 if (!check
->amplist
)
4684 for (p
= check
->amplist
; p
->nid
; p
++) {
4689 return 0; /* nothing changed */
4691 for (p
= check
->amplist
; p
->nid
; p
++) {
4692 for (ch
= 0; ch
< 2; ch
++) {
4693 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
4695 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
4696 if (!check
->power_on
) {
4697 check
->power_on
= 1;
4698 snd_hda_power_up(codec
);
4704 if (check
->power_on
) {
4705 check
->power_on
= 0;
4706 snd_hda_power_down(codec
);
4710 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
4714 * Channel mode helper
4718 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4720 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
4721 struct snd_ctl_elem_info
*uinfo
,
4722 const struct hda_channel_mode
*chmode
,
4725 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4727 uinfo
->value
.enumerated
.items
= num_chmodes
;
4728 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
4729 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
4730 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
4731 chmode
[uinfo
->value
.enumerated
.item
].channels
);
4734 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
4737 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4739 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
4740 struct snd_ctl_elem_value
*ucontrol
,
4741 const struct hda_channel_mode
*chmode
,
4747 for (i
= 0; i
< num_chmodes
; i
++) {
4748 if (max_channels
== chmode
[i
].channels
) {
4749 ucontrol
->value
.enumerated
.item
[0] = i
;
4755 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
4758 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4760 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
4761 struct snd_ctl_elem_value
*ucontrol
,
4762 const struct hda_channel_mode
*chmode
,
4768 mode
= ucontrol
->value
.enumerated
.item
[0];
4769 if (mode
>= num_chmodes
)
4771 if (*max_channelsp
== chmode
[mode
].channels
)
4773 /* change the current channel setting */
4774 *max_channelsp
= chmode
[mode
].channels
;
4775 if (chmode
[mode
].sequence
)
4776 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
4779 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
4786 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4788 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
4789 struct snd_ctl_elem_info
*uinfo
)
4793 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4795 uinfo
->value
.enumerated
.items
= imux
->num_items
;
4796 if (!imux
->num_items
)
4798 index
= uinfo
->value
.enumerated
.item
;
4799 if (index
>= imux
->num_items
)
4800 index
= imux
->num_items
- 1;
4801 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
4804 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
4807 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4809 int snd_hda_input_mux_put(struct hda_codec
*codec
,
4810 const struct hda_input_mux
*imux
,
4811 struct snd_ctl_elem_value
*ucontrol
,
4813 unsigned int *cur_val
)
4817 if (!imux
->num_items
)
4819 idx
= ucontrol
->value
.enumerated
.item
[0];
4820 if (idx
>= imux
->num_items
)
4821 idx
= imux
->num_items
- 1;
4822 if (*cur_val
== idx
)
4824 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
4825 imux
->items
[idx
].index
);
4829 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
4833 * Multi-channel / digital-out PCM helper functions
4836 /* setup SPDIF output stream */
4837 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
4838 unsigned int stream_tag
, unsigned int format
)
4840 struct hda_spdif_out
*spdif
;
4841 unsigned int curr_fmt
;
4844 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
4845 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
4846 AC_VERB_GET_STREAM_FORMAT
, 0);
4847 reset
= codec
->spdif_status_reset
&&
4848 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
4851 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4854 set_dig_out_convert(codec
, nid
,
4855 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
4857 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
4858 if (codec
->slave_dig_outs
) {
4860 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4861 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
4864 /* turn on again (if needed) */
4866 set_dig_out_convert(codec
, nid
,
4867 spdif
->ctls
& 0xff, -1);
4870 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
4872 snd_hda_codec_cleanup_stream(codec
, nid
);
4873 if (codec
->slave_dig_outs
) {
4875 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4876 snd_hda_codec_cleanup_stream(codec
, *d
);
4881 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4882 * @bus: HD-audio bus
4884 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
4886 struct hda_codec
*codec
;
4890 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4891 if (hda_codec_is_power_on(codec
) &&
4892 codec
->patch_ops
.reboot_notify
)
4893 codec
->patch_ops
.reboot_notify(codec
);
4896 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify
);
4899 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4901 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
4902 struct hda_multi_out
*mout
)
4904 mutex_lock(&codec
->spdif_mutex
);
4905 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
4906 /* already opened as analog dup; reset it once */
4907 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4908 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
4909 mutex_unlock(&codec
->spdif_mutex
);
4912 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
4915 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4917 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
4918 struct hda_multi_out
*mout
,
4919 unsigned int stream_tag
,
4920 unsigned int format
,
4921 struct snd_pcm_substream
*substream
)
4923 mutex_lock(&codec
->spdif_mutex
);
4924 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
4925 mutex_unlock(&codec
->spdif_mutex
);
4928 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
4931 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4933 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
4934 struct hda_multi_out
*mout
)
4936 mutex_lock(&codec
->spdif_mutex
);
4937 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4938 mutex_unlock(&codec
->spdif_mutex
);
4941 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
4944 * snd_hda_multi_out_dig_close - release the digital out stream
4946 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
4947 struct hda_multi_out
*mout
)
4949 mutex_lock(&codec
->spdif_mutex
);
4950 mout
->dig_out_used
= 0;
4951 mutex_unlock(&codec
->spdif_mutex
);
4954 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
4957 * snd_hda_multi_out_analog_open - open analog outputs
4959 * Open analog outputs and set up the hw-constraints.
4960 * If the digital outputs can be opened as slave, open the digital
4963 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
4964 struct hda_multi_out
*mout
,
4965 struct snd_pcm_substream
*substream
,
4966 struct hda_pcm_stream
*hinfo
)
4968 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
4969 runtime
->hw
.channels_max
= mout
->max_channels
;
4970 if (mout
->dig_out_nid
) {
4971 if (!mout
->analog_rates
) {
4972 mout
->analog_rates
= hinfo
->rates
;
4973 mout
->analog_formats
= hinfo
->formats
;
4974 mout
->analog_maxbps
= hinfo
->maxbps
;
4976 runtime
->hw
.rates
= mout
->analog_rates
;
4977 runtime
->hw
.formats
= mout
->analog_formats
;
4978 hinfo
->maxbps
= mout
->analog_maxbps
;
4980 if (!mout
->spdif_rates
) {
4981 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
4983 &mout
->spdif_formats
,
4984 &mout
->spdif_maxbps
);
4986 mutex_lock(&codec
->spdif_mutex
);
4987 if (mout
->share_spdif
) {
4988 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
4989 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
4990 runtime
->hw
.rates
&= mout
->spdif_rates
;
4991 runtime
->hw
.formats
&= mout
->spdif_formats
;
4992 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
4993 hinfo
->maxbps
= mout
->spdif_maxbps
;
4995 mout
->share_spdif
= 0;
4996 /* FIXME: need notify? */
4999 mutex_unlock(&codec
->spdif_mutex
);
5001 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
5002 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
5004 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
5007 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5009 * Set up the i/o for analog out.
5010 * When the digital out is available, copy the front out to digital out, too.
5012 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
5013 struct hda_multi_out
*mout
,
5014 unsigned int stream_tag
,
5015 unsigned int format
,
5016 struct snd_pcm_substream
*substream
)
5018 const hda_nid_t
*nids
= mout
->dac_nids
;
5019 int chs
= substream
->runtime
->channels
;
5020 struct hda_spdif_out
*spdif
;
5023 mutex_lock(&codec
->spdif_mutex
);
5024 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
5025 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
5026 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
5028 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
5030 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
5031 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
5032 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
5033 stream_tag
, format
);
5035 mout
->dig_out_used
= 0;
5036 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5039 mutex_unlock(&codec
->spdif_mutex
);
5042 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
5044 if (!mout
->no_share_stream
&&
5045 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
5046 /* headphone out will just decode front left/right (stereo) */
5047 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
5049 /* extra outputs copied from front */
5050 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5051 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
5052 snd_hda_codec_setup_stream(codec
,
5053 mout
->hp_out_nid
[i
],
5054 stream_tag
, 0, format
);
5055 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5056 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
5057 snd_hda_codec_setup_stream(codec
,
5058 mout
->extra_out_nid
[i
],
5059 stream_tag
, 0, format
);
5062 for (i
= 1; i
< mout
->num_dacs
; i
++) {
5063 if (chs
>= (i
+ 1) * 2) /* independent out */
5064 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5066 else if (!mout
->no_share_stream
) /* copy front */
5067 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5072 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
5075 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5077 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
5078 struct hda_multi_out
*mout
)
5080 const hda_nid_t
*nids
= mout
->dac_nids
;
5083 for (i
= 0; i
< mout
->num_dacs
; i
++)
5084 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
5086 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
5087 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5088 if (mout
->hp_out_nid
[i
])
5089 snd_hda_codec_cleanup_stream(codec
,
5090 mout
->hp_out_nid
[i
]);
5091 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5092 if (mout
->extra_out_nid
[i
])
5093 snd_hda_codec_cleanup_stream(codec
,
5094 mout
->extra_out_nid
[i
]);
5095 mutex_lock(&codec
->spdif_mutex
);
5096 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
5097 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5098 mout
->dig_out_used
= 0;
5100 mutex_unlock(&codec
->spdif_mutex
);
5103 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
5106 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5108 * Guess the suitable VREF pin bits to be set as the pin-control value.
5109 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5111 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
5113 unsigned int pincap
;
5114 unsigned int oldval
;
5115 oldval
= snd_hda_codec_read(codec
, pin
, 0,
5116 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5117 pincap
= snd_hda_query_pin_caps(codec
, pin
);
5118 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5119 /* Exception: if the default pin setup is vref50, we give it priority */
5120 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
5121 return AC_PINCTL_VREF_80
;
5122 else if (pincap
& AC_PINCAP_VREF_50
)
5123 return AC_PINCTL_VREF_50
;
5124 else if (pincap
& AC_PINCAP_VREF_100
)
5125 return AC_PINCTL_VREF_100
;
5126 else if (pincap
& AC_PINCAP_VREF_GRD
)
5127 return AC_PINCTL_VREF_GRD
;
5128 return AC_PINCTL_VREF_HIZ
;
5130 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref
);
5132 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
5133 unsigned int val
, bool cached
)
5136 unsigned int cap
= snd_hda_query_pin_caps(codec
, pin
);
5137 if (cap
&& (val
& AC_PINCTL_OUT_EN
)) {
5138 if (!(cap
& AC_PINCAP_OUT
))
5139 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
5140 else if ((val
& AC_PINCTL_HP_EN
) &&
5141 !(cap
& AC_PINCAP_HP_DRV
))
5142 val
&= ~AC_PINCTL_HP_EN
;
5144 if (cap
&& (val
& AC_PINCTL_IN_EN
)) {
5145 if (!(cap
& AC_PINCAP_IN
))
5146 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
5150 return snd_hda_codec_update_cache(codec
, pin
, 0,
5151 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5153 return snd_hda_codec_write(codec
, pin
, 0,
5154 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5156 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl
);
5159 * snd_hda_add_imux_item - Add an item to input_mux
5161 * When the same label is used already in the existing items, the number
5162 * suffix is appended to the label. This label index number is stored
5163 * to type_idx when non-NULL pointer is given.
5165 int snd_hda_add_imux_item(struct hda_input_mux
*imux
, const char *label
,
5166 int index
, int *type_idx
)
5168 int i
, label_idx
= 0;
5169 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
5170 snd_printd(KERN_ERR
"hda_codec: Too many imux items!\n");
5173 for (i
= 0; i
< imux
->num_items
; i
++) {
5174 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
5178 *type_idx
= label_idx
;
5180 snprintf(imux
->items
[imux
->num_items
].label
,
5181 sizeof(imux
->items
[imux
->num_items
].label
),
5182 "%s %d", label
, label_idx
);
5184 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5185 sizeof(imux
->items
[imux
->num_items
].label
));
5186 imux
->items
[imux
->num_items
].index
= index
;
5190 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item
);
5199 * snd_hda_suspend - suspend the codecs
5202 * Returns 0 if successful.
5204 int snd_hda_suspend(struct hda_bus
*bus
)
5206 struct hda_codec
*codec
;
5208 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5209 cancel_delayed_work_sync(&codec
->jackpoll_work
);
5210 if (hda_codec_is_power_on(codec
))
5211 hda_call_codec_suspend(codec
, false);
5215 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
5218 * snd_hda_resume - resume the codecs
5221 * Returns 0 if successful.
5223 int snd_hda_resume(struct hda_bus
*bus
)
5225 struct hda_codec
*codec
;
5227 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5228 hda_call_codec_resume(codec
);
5232 EXPORT_SYMBOL_HDA(snd_hda_resume
);
5233 #endif /* CONFIG_PM */
5240 * snd_array_new - get a new element from the given array
5241 * @array: the array object
5243 * Get a new element from the given array. If it exceeds the
5244 * pre-allocated array size, re-allocate the array.
5246 * Returns NULL if allocation failed.
5248 void *snd_array_new(struct snd_array
*array
)
5250 if (snd_BUG_ON(!array
->elem_size
))
5252 if (array
->used
>= array
->alloced
) {
5253 int num
= array
->alloced
+ array
->alloc_align
;
5254 int size
= (num
+ 1) * array
->elem_size
;
5255 int oldsize
= array
->alloced
* array
->elem_size
;
5257 if (snd_BUG_ON(num
>= 4096))
5259 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
);
5262 memset(nlist
+ oldsize
, 0, size
- oldsize
);
5263 array
->list
= nlist
;
5264 array
->alloced
= num
;
5266 return snd_array_elem(array
, array
->used
++);
5268 EXPORT_SYMBOL_HDA(snd_array_new
);
5271 * snd_array_free - free the given array elements
5272 * @array: the array object
5274 void snd_array_free(struct snd_array
*array
)
5281 EXPORT_SYMBOL_HDA(snd_array_free
);
5284 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5285 * @pcm: PCM caps bits
5286 * @buf: the string buffer to write
5287 * @buflen: the max buffer length
5289 * used by hda_proc.c and hda_eld.c
5291 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5293 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5296 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5297 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5298 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5300 buf
[j
] = '\0'; /* necessary when j == 0 */
5302 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
5304 MODULE_DESCRIPTION("HDA codec core");
5305 MODULE_LICENSE("GPL");