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
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <sound/core.h>
29 #include "hda_codec.h"
30 #include <sound/asoundef.h>
31 #include <sound/tlv.h>
32 #include <sound/initval.h>
33 #include <sound/jack.h>
34 #include "hda_local.h"
36 #include <sound/hda_hwdep.h>
38 #define CREATE_TRACE_POINTS
39 #include "hda_trace.h"
42 * vendor / preset table
45 struct hda_vendor_id
{
50 /* codec vendor labels */
51 static struct hda_vendor_id hda_vendor_ids
[] = {
53 { 0x1013, "Cirrus Logic" },
54 { 0x1057, "Motorola" },
55 { 0x1095, "Silicon Image" },
57 { 0x10ec, "Realtek" },
58 { 0x1102, "Creative" },
62 { 0x11d4, "Analog Devices" },
63 { 0x13f6, "C-Media" },
64 { 0x14f1, "Conexant" },
65 { 0x17e8, "Chrontel" },
67 { 0x1aec, "Wolfson Microelectronics" },
68 { 0x434d, "C-Media" },
70 { 0x8384, "SigmaTel" },
74 static DEFINE_MUTEX(preset_mutex
);
75 static LIST_HEAD(hda_preset_tables
);
77 int snd_hda_add_codec_preset(struct hda_codec_preset_list
*preset
)
79 mutex_lock(&preset_mutex
);
80 list_add_tail(&preset
->list
, &hda_preset_tables
);
81 mutex_unlock(&preset_mutex
);
84 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset
);
86 int snd_hda_delete_codec_preset(struct hda_codec_preset_list
*preset
)
88 mutex_lock(&preset_mutex
);
89 list_del(&preset
->list
);
90 mutex_unlock(&preset_mutex
);
93 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset
);
95 #ifdef CONFIG_SND_HDA_POWER_SAVE
96 static void hda_power_work(struct work_struct
*work
);
97 static void hda_keep_power_on(struct hda_codec
*codec
);
98 #define hda_codec_is_power_on(codec) ((codec)->power_on)
100 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
101 #define hda_codec_is_power_on(codec) 1
105 * snd_hda_get_jack_location - Give a location string of the jack
106 * @cfg: pin default config value
108 * Parse the pin default config value and returns the string of the
109 * jack location, e.g. "Rear", "Front", etc.
111 const char *snd_hda_get_jack_location(u32 cfg
)
113 static char *bases
[7] = {
114 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
116 static unsigned char specials_idx
[] = {
121 static char *specials
[] = {
122 "Rear Panel", "Drive Bar",
123 "Riser", "HDMI", "ATAPI",
124 "Mobile-In", "Mobile-Out"
127 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
128 if ((cfg
& 0x0f) < 7)
129 return bases
[cfg
& 0x0f];
130 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
131 if (cfg
== specials_idx
[i
])
136 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location
);
139 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
140 * @cfg: pin default config value
142 * Parse the pin default config value and returns the string of the
143 * jack connectivity, i.e. external or internal connection.
145 const char *snd_hda_get_jack_connectivity(u32 cfg
)
147 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
149 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
151 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity
);
154 * snd_hda_get_jack_type - Give a type string of the jack
155 * @cfg: pin default config value
157 * Parse the pin default config value and returns the string of the
158 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
160 const char *snd_hda_get_jack_type(u32 cfg
)
162 static char *jack_types
[16] = {
163 "Line Out", "Speaker", "HP Out", "CD",
164 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
165 "Line In", "Aux", "Mic", "Telephony",
166 "SPDIF In", "Digitial In", "Reserved", "Other"
169 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
170 >> AC_DEFCFG_DEVICE_SHIFT
];
172 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type
);
175 * Compose a 32bit command word to be sent to the HD-audio controller
177 static inline unsigned int
178 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
179 unsigned int verb
, unsigned int parm
)
183 if ((codec
->addr
& ~0xf) || (direct
& ~1) || (nid
& ~0x7f) ||
184 (verb
& ~0xfff) || (parm
& ~0xffff)) {
185 printk(KERN_ERR
"hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
186 codec
->addr
, direct
, nid
, verb
, parm
);
190 val
= (u32
)codec
->addr
<< 28;
191 val
|= (u32
)direct
<< 27;
192 val
|= (u32
)nid
<< 20;
199 * Send and receive a verb
201 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
204 struct hda_bus
*bus
= codec
->bus
;
213 snd_hda_power_up(codec
);
214 mutex_lock(&bus
->cmd_mutex
);
215 trace_hda_send_cmd(codec
, cmd
);
216 err
= bus
->ops
.command(bus
, cmd
);
218 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
219 trace_hda_get_response(codec
, *res
);
221 mutex_unlock(&bus
->cmd_mutex
);
222 snd_hda_power_down(codec
);
223 if (res
&& *res
== -1 && bus
->rirb_error
) {
224 if (bus
->response_reset
) {
225 snd_printd("hda_codec: resetting BUS due to "
226 "fatal communication error\n");
227 trace_hda_bus_reset(bus
);
228 bus
->ops
.bus_reset(bus
);
232 /* clear reset-flag when the communication gets recovered */
234 bus
->response_reset
= 0;
239 * snd_hda_codec_read - send a command and get the response
240 * @codec: the HDA codec
241 * @nid: NID to send the command
242 * @direct: direct flag
243 * @verb: the verb to send
244 * @parm: the parameter for the verb
246 * Send a single command and read the corresponding response.
248 * Returns the obtained response value, or -1 for an error.
250 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
252 unsigned int verb
, unsigned int parm
)
254 unsigned cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
256 if (codec_exec_verb(codec
, cmd
, &res
))
260 EXPORT_SYMBOL_HDA(snd_hda_codec_read
);
263 * snd_hda_codec_write - send a single command without waiting for response
264 * @codec: the HDA codec
265 * @nid: NID to send the command
266 * @direct: direct flag
267 * @verb: the verb to send
268 * @parm: the parameter for the verb
270 * Send a single command without waiting for response.
272 * Returns 0 if successful, or a negative error code.
274 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
275 unsigned int verb
, unsigned int parm
)
277 unsigned int cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
279 return codec_exec_verb(codec
, cmd
,
280 codec
->bus
->sync_write
? &res
: NULL
);
282 EXPORT_SYMBOL_HDA(snd_hda_codec_write
);
285 * snd_hda_sequence_write - sequence writes
286 * @codec: the HDA codec
287 * @seq: VERB array to send
289 * Send the commands sequentially from the given array.
290 * The array must be terminated with NID=0.
292 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
294 for (; seq
->nid
; seq
++)
295 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
297 EXPORT_SYMBOL_HDA(snd_hda_sequence_write
);
300 * snd_hda_get_sub_nodes - get the range of sub nodes
301 * @codec: the HDA codec
303 * @start_id: the pointer to store the start NID
305 * Parse the NID and store the start NID of its sub-nodes.
306 * Returns the number of sub-nodes.
308 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
313 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
316 *start_id
= (parm
>> 16) & 0x7fff;
317 return (int)(parm
& 0x7fff);
319 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
321 /* look up the cached results */
322 static hda_nid_t
*lookup_conn_list(struct snd_array
*array
, hda_nid_t nid
)
325 for (i
= 0; i
< array
->used
; ) {
326 hda_nid_t
*p
= snd_array_elem(array
, i
);
336 * snd_hda_get_conn_list - get connection list
337 * @codec: the HDA codec
339 * @listp: the pointer to store NID list
341 * Parses the connection list of the given widget and stores the list
344 * Returns the number of connections, or a negative error code.
346 int snd_hda_get_conn_list(struct hda_codec
*codec
, hda_nid_t nid
,
347 const hda_nid_t
**listp
)
349 struct snd_array
*array
= &codec
->conn_lists
;
351 hda_nid_t list
[HDA_MAX_CONNECTIONS
];
356 /* if the connection-list is already cached, read it */
357 p
= lookup_conn_list(array
, nid
);
363 if (snd_BUG_ON(added
))
366 /* read the connection and add to the cache */
367 len
= snd_hda_get_raw_connections(codec
, nid
, list
, HDA_MAX_CONNECTIONS
);
370 err
= snd_hda_override_conn_list(codec
, nid
, len
, list
);
376 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list
);
379 * snd_hda_get_connections - copy connection list
380 * @codec: the HDA codec
382 * @conn_list: connection list array
383 * @max_conns: max. number of connections to store
385 * Parses the connection list of the given widget and stores the list
388 * Returns the number of connections, or a negative error code.
390 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
391 hda_nid_t
*conn_list
, int max_conns
)
393 const hda_nid_t
*list
;
394 int len
= snd_hda_get_conn_list(codec
, nid
, &list
);
398 if (len
> max_conns
) {
399 snd_printk(KERN_ERR
"hda_codec: "
400 "Too many connections %d for NID 0x%x\n",
404 memcpy(conn_list
, list
, len
* sizeof(hda_nid_t
));
407 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
410 * snd_hda_get_raw_connections - copy connection list without cache
411 * @codec: the HDA codec
413 * @conn_list: connection list array
414 * @max_conns: max. number of connections to store
416 * Like snd_hda_get_connections(), copy the connection list but without
417 * checking through the connection-list cache.
418 * Currently called only from hda_proc.c, so not exported.
420 int snd_hda_get_raw_connections(struct hda_codec
*codec
, hda_nid_t nid
,
421 hda_nid_t
*conn_list
, int max_conns
)
424 int i
, conn_len
, conns
;
425 unsigned int shift
, num_elems
, mask
;
429 if (snd_BUG_ON(!conn_list
|| max_conns
<= 0))
432 wcaps
= get_wcaps(codec
, nid
);
433 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
434 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
437 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
438 if (parm
& AC_CLIST_LONG
) {
447 conn_len
= parm
& AC_CLIST_LENGTH
;
448 mask
= (1 << (shift
-1)) - 1;
451 return 0; /* no connection */
454 /* single connection */
455 parm
= snd_hda_codec_read(codec
, nid
, 0,
456 AC_VERB_GET_CONNECT_LIST
, 0);
457 if (parm
== -1 && codec
->bus
->rirb_error
)
459 conn_list
[0] = parm
& mask
;
463 /* multi connection */
466 for (i
= 0; i
< conn_len
; i
++) {
470 if (i
% num_elems
== 0) {
471 parm
= snd_hda_codec_read(codec
, nid
, 0,
472 AC_VERB_GET_CONNECT_LIST
, i
);
473 if (parm
== -1 && codec
->bus
->rirb_error
)
476 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
479 snd_printk(KERN_WARNING
"hda_codec: "
480 "invalid CONNECT_LIST verb %x[%i]:%x\n",
486 /* ranges between the previous and this one */
487 if (!prev_nid
|| prev_nid
>= val
) {
488 snd_printk(KERN_WARNING
"hda_codec: "
489 "invalid dep_range_val %x:%x\n",
493 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
494 if (conns
>= max_conns
) {
495 snd_printk(KERN_ERR
"hda_codec: "
496 "Too many connections %d for NID 0x%x\n",
500 conn_list
[conns
++] = n
;
503 if (conns
>= max_conns
) {
504 snd_printk(KERN_ERR
"hda_codec: "
505 "Too many connections %d for NID 0x%x\n",
509 conn_list
[conns
++] = val
;
516 static bool add_conn_list(struct snd_array
*array
, hda_nid_t nid
)
518 hda_nid_t
*p
= snd_array_new(array
);
526 * snd_hda_override_conn_list - add/modify the connection-list to cache
527 * @codec: the HDA codec
529 * @len: number of connection list entries
530 * @list: the list of connection entries
532 * Add or modify the given connection-list to the cache. If the corresponding
533 * cache already exists, invalidate it and append a new one.
535 * Returns zero or a negative error code.
537 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
538 const hda_nid_t
*list
)
540 struct snd_array
*array
= &codec
->conn_lists
;
544 p
= lookup_conn_list(array
, nid
);
546 *p
= -1; /* invalidate the old entry */
548 old_used
= array
->used
;
549 if (!add_conn_list(array
, nid
) || !add_conn_list(array
, len
))
551 for (i
= 0; i
< len
; i
++)
552 if (!add_conn_list(array
, list
[i
]))
557 array
->used
= old_used
;
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 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 init_hda_cache(struct hda_cache_rec
*cache
,
1139 unsigned int record_size
);
1140 static void free_hda_cache(struct hda_cache_rec
*cache
);
1142 /* restore the initial pin cfgs and release all pincfg lists */
1143 static void restore_init_pincfgs(struct hda_codec
*codec
)
1145 /* first free driver_pins and user_pins, then call restore_pincfg
1146 * so that only the values in init_pins are restored
1148 snd_array_free(&codec
->driver_pins
);
1149 #ifdef CONFIG_SND_HDA_HWDEP
1150 snd_array_free(&codec
->user_pins
);
1152 restore_pincfgs(codec
);
1153 snd_array_free(&codec
->init_pins
);
1157 * audio-converter setup caches
1159 struct hda_cvt_setup
{
1164 unsigned char active
; /* cvt is currently used */
1165 unsigned char dirty
; /* setups should be cleared */
1168 /* get or create a cache entry for the given audio converter NID */
1169 static struct hda_cvt_setup
*
1170 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1172 struct hda_cvt_setup
*p
;
1175 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1176 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1180 p
= snd_array_new(&codec
->cvt_setups
);
1189 static void snd_hda_codec_free(struct hda_codec
*codec
)
1193 restore_init_pincfgs(codec
);
1194 #ifdef CONFIG_SND_HDA_POWER_SAVE
1195 cancel_delayed_work(&codec
->power_work
);
1196 flush_workqueue(codec
->bus
->workq
);
1198 list_del(&codec
->list
);
1199 snd_array_free(&codec
->mixers
);
1200 snd_array_free(&codec
->nids
);
1201 snd_array_free(&codec
->conn_lists
);
1202 snd_array_free(&codec
->spdif_out
);
1203 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1204 if (codec
->patch_ops
.free
)
1205 codec
->patch_ops
.free(codec
);
1206 module_put(codec
->owner
);
1207 free_hda_cache(&codec
->amp_cache
);
1208 free_hda_cache(&codec
->cmd_cache
);
1209 kfree(codec
->vendor_name
);
1210 kfree(codec
->chip_name
);
1211 kfree(codec
->modelname
);
1212 kfree(codec
->wcaps
);
1216 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
1217 unsigned int power_state
);
1220 * snd_hda_codec_new - create a HDA codec
1221 * @bus: the bus to assign
1222 * @codec_addr: the codec address
1223 * @codecp: the pointer to store the generated codec
1225 * Returns 0 if successful, or a negative error code.
1227 int /*__devinit*/ snd_hda_codec_new(struct hda_bus
*bus
,
1228 unsigned int codec_addr
,
1229 struct hda_codec
**codecp
)
1231 struct hda_codec
*codec
;
1235 if (snd_BUG_ON(!bus
))
1237 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1240 if (bus
->caddr_tbl
[codec_addr
]) {
1241 snd_printk(KERN_ERR
"hda_codec: "
1242 "address 0x%x is already occupied\n", codec_addr
);
1246 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1247 if (codec
== NULL
) {
1248 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
1253 codec
->addr
= codec_addr
;
1254 mutex_init(&codec
->spdif_mutex
);
1255 mutex_init(&codec
->control_mutex
);
1256 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1257 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1258 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1259 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1260 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1261 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1262 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1263 snd_array_init(&codec
->conn_lists
, sizeof(hda_nid_t
), 64);
1264 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1265 if (codec
->bus
->modelname
) {
1266 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1267 if (!codec
->modelname
) {
1268 snd_hda_codec_free(codec
);
1273 #ifdef CONFIG_SND_HDA_POWER_SAVE
1274 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1275 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1276 * the caller has to power down appropriatley after initialization
1279 hda_keep_power_on(codec
);
1282 list_add_tail(&codec
->list
, &bus
->codec_list
);
1283 bus
->caddr_tbl
[codec_addr
] = codec
;
1285 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1287 if (codec
->vendor_id
== -1)
1288 /* read again, hopefully the access method was corrected
1289 * in the last read...
1291 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1293 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1294 AC_PAR_SUBSYSTEM_ID
);
1295 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1298 setup_fg_nodes(codec
);
1299 if (!codec
->afg
&& !codec
->mfg
) {
1300 snd_printdd("hda_codec: no AFG or MFG node found\n");
1305 err
= read_widget_caps(codec
, codec
->afg
? codec
->afg
: codec
->mfg
);
1307 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1310 err
= read_pin_defaults(codec
);
1314 if (!codec
->subsystem_id
) {
1315 hda_nid_t nid
= codec
->afg
? codec
->afg
: codec
->mfg
;
1316 codec
->subsystem_id
=
1317 snd_hda_codec_read(codec
, nid
, 0,
1318 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1321 /* power-up all before initialization */
1322 hda_set_power_state(codec
,
1323 codec
->afg
? codec
->afg
: codec
->mfg
,
1326 snd_hda_codec_proc_new(codec
);
1328 snd_hda_create_hwdep(codec
);
1330 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1331 codec
->subsystem_id
, codec
->revision_id
);
1332 snd_component_add(codec
->bus
->card
, component
);
1339 snd_hda_codec_free(codec
);
1342 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
1345 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1346 * @codec: the HDA codec
1348 * Start parsing of the given codec tree and (re-)initialize the whole
1351 * Returns 0 if successful or a negative error code.
1353 int snd_hda_codec_configure(struct hda_codec
*codec
)
1357 codec
->preset
= find_codec_preset(codec
);
1358 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1359 err
= get_codec_name(codec
);
1364 if (is_generic_config(codec
)) {
1365 err
= snd_hda_parse_generic_codec(codec
);
1368 if (codec
->preset
&& codec
->preset
->patch
) {
1369 err
= codec
->preset
->patch(codec
);
1373 /* call the default parser */
1374 err
= snd_hda_parse_generic_codec(codec
);
1376 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1379 if (!err
&& codec
->patch_ops
.unsol_event
)
1380 err
= init_unsol_queue(codec
->bus
);
1381 /* audio codec should override the mixer name */
1382 if (!err
&& (codec
->afg
|| !*codec
->bus
->card
->mixername
))
1383 snprintf(codec
->bus
->card
->mixername
,
1384 sizeof(codec
->bus
->card
->mixername
),
1385 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1388 EXPORT_SYMBOL_HDA(snd_hda_codec_configure
);
1391 * snd_hda_codec_setup_stream - set up the codec for streaming
1392 * @codec: the CODEC to set up
1393 * @nid: the NID to set up
1394 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1395 * @channel_id: channel id to pass, zero based.
1396 * @format: stream format.
1398 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1400 int channel_id
, int format
)
1402 struct hda_codec
*c
;
1403 struct hda_cvt_setup
*p
;
1404 unsigned int oldval
, newval
;
1411 snd_printdd("hda_codec_setup_stream: "
1412 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1413 nid
, stream_tag
, channel_id
, format
);
1414 p
= get_hda_cvt_setup(codec
, nid
);
1417 /* update the stream-id if changed */
1418 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1419 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1420 newval
= (stream_tag
<< 4) | channel_id
;
1421 if (oldval
!= newval
)
1422 snd_hda_codec_write(codec
, nid
, 0,
1423 AC_VERB_SET_CHANNEL_STREAMID
,
1425 p
->stream_tag
= stream_tag
;
1426 p
->channel_id
= channel_id
;
1428 /* update the format-id if changed */
1429 if (p
->format_id
!= format
) {
1430 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1431 AC_VERB_GET_STREAM_FORMAT
, 0);
1432 if (oldval
!= format
) {
1434 snd_hda_codec_write(codec
, nid
, 0,
1435 AC_VERB_SET_STREAM_FORMAT
,
1438 p
->format_id
= format
;
1443 /* make other inactive cvts with the same stream-tag dirty */
1444 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1445 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1446 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1447 p
= snd_array_elem(&c
->cvt_setups
, i
);
1448 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1449 get_wcaps_type(get_wcaps(codec
, p
->nid
)) == type
)
1454 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1456 static void really_cleanup_stream(struct hda_codec
*codec
,
1457 struct hda_cvt_setup
*q
);
1460 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1461 * @codec: the CODEC to clean up
1462 * @nid: the NID to clean up
1463 * @do_now: really clean up the stream instead of clearing the active flag
1465 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1468 struct hda_cvt_setup
*p
;
1473 if (codec
->no_sticky_stream
)
1476 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1477 p
= get_hda_cvt_setup(codec
, nid
);
1479 /* here we just clear the active flag when do_now isn't set;
1480 * actual clean-ups will be done later in
1481 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1484 really_cleanup_stream(codec
, p
);
1489 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream
);
1491 static void really_cleanup_stream(struct hda_codec
*codec
,
1492 struct hda_cvt_setup
*q
)
1494 hda_nid_t nid
= q
->nid
;
1495 if (q
->stream_tag
|| q
->channel_id
)
1496 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1498 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1500 memset(q
, 0, sizeof(*q
));
1504 /* clean up the all conflicting obsolete streams */
1505 static void purify_inactive_streams(struct hda_codec
*codec
)
1507 struct hda_codec
*c
;
1510 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1511 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1512 struct hda_cvt_setup
*p
;
1513 p
= snd_array_elem(&c
->cvt_setups
, i
);
1515 really_cleanup_stream(c
, p
);
1521 /* clean up all streams; called from suspend */
1522 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1526 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1527 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1529 really_cleanup_stream(codec
, p
);
1535 * amp access functions
1538 /* FIXME: more better hash key? */
1539 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1540 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1541 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1542 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1543 #define INFO_AMP_CAPS (1<<0)
1544 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1546 /* initialize the hash table */
1547 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec
*cache
,
1548 unsigned int record_size
)
1550 memset(cache
, 0, sizeof(*cache
));
1551 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1552 snd_array_init(&cache
->buf
, record_size
, 64);
1555 static void free_hda_cache(struct hda_cache_rec
*cache
)
1557 snd_array_free(&cache
->buf
);
1560 /* query the hash. allocate an entry if not found. */
1561 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1563 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1564 u16 cur
= cache
->hash
[idx
];
1565 struct hda_cache_head
*info
;
1567 while (cur
!= 0xffff) {
1568 info
= snd_array_elem(&cache
->buf
, cur
);
1569 if (info
->key
== key
)
1576 /* query the hash. allocate an entry if not found. */
1577 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1580 struct hda_cache_head
*info
= get_hash(cache
, key
);
1583 /* add a new hash entry */
1584 info
= snd_array_new(&cache
->buf
);
1587 cur
= snd_array_index(&cache
->buf
, info
);
1590 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1591 info
->next
= cache
->hash
[idx
];
1592 cache
->hash
[idx
] = cur
;
1597 /* query and allocate an amp hash entry */
1598 static inline struct hda_amp_info
*
1599 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1601 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1605 * query_amp_caps - query AMP capabilities
1606 * @codec: the HD-auio codec
1607 * @nid: the NID to query
1608 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1610 * Query AMP capabilities for the given widget and direction.
1611 * Returns the obtained capability bits.
1613 * When cap bits have been already read, this doesn't read again but
1614 * returns the cached value.
1616 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1618 struct hda_amp_info
*info
;
1620 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, 0));
1623 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1624 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1626 info
->amp_caps
= snd_hda_param_read(codec
, nid
,
1627 direction
== HDA_OUTPUT
?
1628 AC_PAR_AMP_OUT_CAP
:
1631 info
->head
.val
|= INFO_AMP_CAPS
;
1633 return info
->amp_caps
;
1635 EXPORT_SYMBOL_HDA(query_amp_caps
);
1638 * snd_hda_override_amp_caps - Override the AMP capabilities
1639 * @codec: the CODEC to clean up
1640 * @nid: the NID to clean up
1641 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1642 * @caps: the capability bits to set
1644 * Override the cached AMP caps bits value by the given one.
1645 * This function is useful if the driver needs to adjust the AMP ranges,
1646 * e.g. limit to 0dB, etc.
1648 * Returns zero if successful or a negative error code.
1650 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1653 struct hda_amp_info
*info
;
1655 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0));
1658 info
->amp_caps
= caps
;
1659 info
->head
.val
|= INFO_AMP_CAPS
;
1662 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1665 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, u32 key
,
1666 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
))
1668 struct hda_amp_info
*info
;
1670 info
= get_alloc_amp_hash(codec
, key
);
1673 if (!info
->head
.val
) {
1674 info
->head
.val
|= INFO_AMP_CAPS
;
1675 info
->amp_caps
= func(codec
, nid
);
1677 return info
->amp_caps
;
1680 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
)
1682 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1686 * snd_hda_query_pin_caps - Query PIN capabilities
1687 * @codec: the HD-auio codec
1688 * @nid: the NID to query
1690 * Query PIN capabilities for the given widget.
1691 * Returns the obtained capability bits.
1693 * When cap bits have been already read, this doesn't read again but
1694 * returns the cached value.
1696 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1698 return query_caps_hash(codec
, nid
, HDA_HASH_PINCAP_KEY(nid
),
1701 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1704 * snd_hda_override_pin_caps - Override the pin capabilities
1706 * @nid: the NID to override
1707 * @caps: the capability bits to set
1709 * Override the cached PIN capabilitiy bits value by the given one.
1711 * Returns zero if successful or a negative error code.
1713 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1716 struct hda_amp_info
*info
;
1717 info
= get_alloc_amp_hash(codec
, HDA_HASH_PINCAP_KEY(nid
));
1720 info
->amp_caps
= caps
;
1721 info
->head
.val
|= INFO_AMP_CAPS
;
1724 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps
);
1727 * snd_hda_pin_sense - execute pin sense measurement
1728 * @codec: the CODEC to sense
1729 * @nid: the pin NID to sense
1731 * Execute necessary pin sense measurement and return its Presence Detect,
1732 * Impedance, ELD Valid etc. status bits.
1734 u32
snd_hda_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
1738 if (!codec
->no_trigger_sense
) {
1739 pincap
= snd_hda_query_pin_caps(codec
, nid
);
1740 if (pincap
& AC_PINCAP_TRIG_REQ
) /* need trigger? */
1741 snd_hda_codec_read(codec
, nid
, 0,
1742 AC_VERB_SET_PIN_SENSE
, 0);
1744 return snd_hda_codec_read(codec
, nid
, 0,
1745 AC_VERB_GET_PIN_SENSE
, 0);
1747 EXPORT_SYMBOL_HDA(snd_hda_pin_sense
);
1750 * snd_hda_jack_detect - query pin Presence Detect status
1751 * @codec: the CODEC to sense
1752 * @nid: the pin NID to sense
1754 * Query and return the pin's Presence Detect status.
1756 int snd_hda_jack_detect(struct hda_codec
*codec
, hda_nid_t nid
)
1758 u32 sense
= snd_hda_pin_sense(codec
, nid
);
1759 return !!(sense
& AC_PINSENSE_PRESENCE
);
1761 EXPORT_SYMBOL_HDA(snd_hda_jack_detect
);
1764 * read the current volume to info
1765 * if the cache exists, read the cache value.
1767 static unsigned int get_vol_mute(struct hda_codec
*codec
,
1768 struct hda_amp_info
*info
, hda_nid_t nid
,
1769 int ch
, int direction
, int index
)
1773 if (info
->head
.val
& INFO_AMP_VOL(ch
))
1774 return info
->vol
[ch
];
1776 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1777 parm
|= direction
== HDA_OUTPUT
? AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1779 val
= snd_hda_codec_read(codec
, nid
, 0,
1780 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1781 info
->vol
[ch
] = val
& 0xff;
1782 info
->head
.val
|= INFO_AMP_VOL(ch
);
1783 return info
->vol
[ch
];
1787 * write the current volume in info to the h/w and update the cache
1789 static void put_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
1790 hda_nid_t nid
, int ch
, int direction
, int index
,
1795 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1796 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1797 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1799 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1800 info
->vol
[ch
] = val
;
1804 * snd_hda_codec_amp_read - Read AMP value
1805 * @codec: HD-audio codec
1806 * @nid: NID to read the AMP value
1807 * @ch: channel (left=0 or right=1)
1808 * @direction: #HDA_INPUT or #HDA_OUTPUT
1809 * @index: the index value (only for input direction)
1811 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1813 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1814 int direction
, int index
)
1816 struct hda_amp_info
*info
;
1817 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1820 return get_vol_mute(codec
, info
, nid
, ch
, direction
, index
);
1822 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
1825 * snd_hda_codec_amp_update - update the AMP value
1826 * @codec: HD-audio codec
1827 * @nid: NID to read the AMP value
1828 * @ch: channel (left=0 or right=1)
1829 * @direction: #HDA_INPUT or #HDA_OUTPUT
1830 * @idx: the index value (only for input direction)
1831 * @mask: bit mask to set
1832 * @val: the bits value to set
1834 * Update the AMP value with a bit mask.
1835 * Returns 0 if the value is unchanged, 1 if changed.
1837 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1838 int direction
, int idx
, int mask
, int val
)
1840 struct hda_amp_info
*info
;
1842 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, idx
));
1845 if (snd_BUG_ON(mask
& ~0xff))
1848 val
|= get_vol_mute(codec
, info
, nid
, ch
, direction
, idx
) & ~mask
;
1849 if (info
->vol
[ch
] == val
)
1851 put_vol_mute(codec
, info
, nid
, ch
, direction
, idx
, val
);
1854 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
1857 * snd_hda_codec_amp_stereo - update the AMP stereo values
1858 * @codec: HD-audio codec
1859 * @nid: NID to read the AMP value
1860 * @direction: #HDA_INPUT or #HDA_OUTPUT
1861 * @idx: the index value (only for input direction)
1862 * @mask: bit mask to set
1863 * @val: the bits value to set
1865 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1866 * stereo widget with the same mask and value.
1868 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1869 int direction
, int idx
, int mask
, int val
)
1873 if (snd_BUG_ON(mask
& ~0xff))
1875 for (ch
= 0; ch
< 2; ch
++)
1876 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1880 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
1884 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1885 * @codec: HD-audio codec
1887 * Resume the all amp commands from the cache.
1889 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
1891 struct hda_amp_info
*buffer
= codec
->amp_cache
.buf
.list
;
1894 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++, buffer
++) {
1895 u32 key
= buffer
->head
.key
;
1897 unsigned int idx
, dir
, ch
;
1901 idx
= (key
>> 16) & 0xff;
1902 dir
= (key
>> 24) & 0xff;
1903 for (ch
= 0; ch
< 2; ch
++) {
1904 if (!(buffer
->head
.val
& INFO_AMP_VOL(ch
)))
1906 put_vol_mute(codec
, buffer
, nid
, ch
, dir
, idx
,
1911 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
1912 #endif /* CONFIG_PM */
1914 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1917 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1919 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1926 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1928 * The control element is supposed to have the private_value field
1929 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1931 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1932 struct snd_ctl_elem_info
*uinfo
)
1934 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1935 u16 nid
= get_amp_nid(kcontrol
);
1936 u8 chs
= get_amp_channels(kcontrol
);
1937 int dir
= get_amp_direction(kcontrol
);
1938 unsigned int ofs
= get_amp_offset(kcontrol
);
1940 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1941 uinfo
->count
= chs
== 3 ? 2 : 1;
1942 uinfo
->value
.integer
.min
= 0;
1943 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
1944 if (!uinfo
->value
.integer
.max
) {
1945 printk(KERN_WARNING
"hda_codec: "
1946 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
1952 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
1955 static inline unsigned int
1956 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1957 int ch
, int dir
, int idx
, unsigned int ofs
)
1960 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1961 val
&= HDA_AMP_VOLMASK
;
1970 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1971 int ch
, int dir
, int idx
, unsigned int ofs
,
1974 unsigned int maxval
;
1978 /* ofs = 0: raw max value */
1979 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
1982 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
1983 HDA_AMP_VOLMASK
, val
);
1987 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1989 * The control element is supposed to have the private_value field
1990 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1992 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
1993 struct snd_ctl_elem_value
*ucontrol
)
1995 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1996 hda_nid_t nid
= get_amp_nid(kcontrol
);
1997 int chs
= get_amp_channels(kcontrol
);
1998 int dir
= get_amp_direction(kcontrol
);
1999 int idx
= get_amp_index(kcontrol
);
2000 unsigned int ofs
= get_amp_offset(kcontrol
);
2001 long *valp
= ucontrol
->value
.integer
.value
;
2004 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
2006 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
2009 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
2012 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2014 * The control element is supposed to have the private_value field
2015 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2017 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
2018 struct snd_ctl_elem_value
*ucontrol
)
2020 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2021 hda_nid_t nid
= get_amp_nid(kcontrol
);
2022 int chs
= get_amp_channels(kcontrol
);
2023 int dir
= get_amp_direction(kcontrol
);
2024 int idx
= get_amp_index(kcontrol
);
2025 unsigned int ofs
= get_amp_offset(kcontrol
);
2026 long *valp
= ucontrol
->value
.integer
.value
;
2029 snd_hda_power_up(codec
);
2031 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
2035 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
2036 snd_hda_power_down(codec
);
2039 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
2042 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2044 * The control element is supposed to have the private_value field
2045 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2047 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2048 unsigned int size
, unsigned int __user
*_tlv
)
2050 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2051 hda_nid_t nid
= get_amp_nid(kcontrol
);
2052 int dir
= get_amp_direction(kcontrol
);
2053 unsigned int ofs
= get_amp_offset(kcontrol
);
2054 bool min_mute
= get_amp_min_mute(kcontrol
);
2055 u32 caps
, val1
, val2
;
2057 if (size
< 4 * sizeof(unsigned int))
2059 caps
= query_amp_caps(codec
, nid
, dir
);
2060 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2061 val2
= (val2
+ 1) * 25;
2062 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2064 val1
= ((int)val1
) * ((int)val2
);
2066 val2
|= TLV_DB_SCALE_MUTE
;
2067 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2069 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2071 if (put_user(val1
, _tlv
+ 2))
2073 if (put_user(val2
, _tlv
+ 3))
2077 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
2080 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2081 * @codec: HD-audio codec
2082 * @nid: NID of a reference widget
2083 * @dir: #HDA_INPUT or #HDA_OUTPUT
2084 * @tlv: TLV data to be stored, at least 4 elements
2086 * Set (static) TLV data for a virtual master volume using the AMP caps
2087 * obtained from the reference NID.
2088 * The volume range is recalculated as if the max volume is 0dB.
2090 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2096 caps
= query_amp_caps(codec
, nid
, dir
);
2097 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2098 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2099 step
= (step
+ 1) * 25;
2100 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2101 tlv
[1] = 2 * sizeof(unsigned int);
2102 tlv
[2] = -nums
* step
;
2105 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
2107 /* find a mixer control element with the given name */
2108 static struct snd_kcontrol
*
2109 _snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2110 const char *name
, int idx
)
2112 struct snd_ctl_elem_id id
;
2113 memset(&id
, 0, sizeof(id
));
2114 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2116 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2118 strcpy(id
.name
, name
);
2119 return snd_ctl_find_id(codec
->bus
->card
, &id
);
2123 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2124 * @codec: HD-audio codec
2125 * @name: ctl id name string
2127 * Get the control element with the given id string and IFACE_MIXER.
2129 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2132 return _snd_hda_find_mixer_ctl(codec
, name
, 0);
2134 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
2136 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
)
2139 for (idx
= 0; idx
< 16; idx
++) { /* 16 ctlrs should be large enough */
2140 if (!_snd_hda_find_mixer_ctl(codec
, name
, idx
))
2147 * snd_hda_ctl_add - Add a control element and assign to the codec
2148 * @codec: HD-audio codec
2149 * @nid: corresponding NID (optional)
2150 * @kctl: the control element to assign
2152 * Add the given control element to an array inside the codec instance.
2153 * All control elements belonging to a codec are supposed to be added
2154 * by this function so that a proper clean-up works at the free or
2155 * reconfiguration time.
2157 * If non-zero @nid is passed, the NID is assigned to the control element.
2158 * The assignment is shown in the codec proc file.
2160 * snd_hda_ctl_add() checks the control subdev id field whether
2161 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2162 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2163 * specifies if kctl->private_value is a HDA amplifier value.
2165 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2166 struct snd_kcontrol
*kctl
)
2169 unsigned short flags
= 0;
2170 struct hda_nid_item
*item
;
2172 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2173 flags
|= HDA_NID_ITEM_AMP
;
2175 nid
= get_amp_nid_(kctl
->private_value
);
2177 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2178 nid
= kctl
->id
.subdevice
& 0xffff;
2179 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2180 kctl
->id
.subdevice
= 0;
2181 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
2184 item
= snd_array_new(&codec
->mixers
);
2189 item
->flags
= flags
;
2192 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
2195 * snd_hda_add_nid - Assign a NID to a control element
2196 * @codec: HD-audio codec
2197 * @nid: corresponding NID (optional)
2198 * @kctl: the control element to assign
2199 * @index: index to kctl
2201 * Add the given control element to an array inside the codec instance.
2202 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2203 * NID:KCTL mapping - for example "Capture Source" selector.
2205 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2206 unsigned int index
, hda_nid_t nid
)
2208 struct hda_nid_item
*item
;
2211 item
= snd_array_new(&codec
->nids
);
2215 item
->index
= index
;
2219 printk(KERN_ERR
"hda-codec: no NID for mapping control %s:%d:%d\n",
2220 kctl
->id
.name
, kctl
->id
.index
, index
);
2223 EXPORT_SYMBOL_HDA(snd_hda_add_nid
);
2226 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2227 * @codec: HD-audio codec
2229 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2232 struct hda_nid_item
*items
= codec
->mixers
.list
;
2233 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2234 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
2235 snd_array_free(&codec
->mixers
);
2236 snd_array_free(&codec
->nids
);
2239 /* pseudo device locking
2240 * toggle card->shutdown to allow/disallow the device access (as a hack)
2242 static int hda_lock_devices(struct snd_card
*card
)
2244 spin_lock(&card
->files_lock
);
2245 if (card
->shutdown
) {
2246 spin_unlock(&card
->files_lock
);
2250 spin_unlock(&card
->files_lock
);
2254 static void hda_unlock_devices(struct snd_card
*card
)
2256 spin_lock(&card
->files_lock
);
2258 spin_unlock(&card
->files_lock
);
2262 * snd_hda_codec_reset - Clear all objects assigned to the codec
2263 * @codec: HD-audio codec
2265 * This frees the all PCM and control elements assigned to the codec, and
2266 * clears the caches and restores the pin default configurations.
2268 * When a device is being used, it returns -EBSY. If successfully freed,
2271 int snd_hda_codec_reset(struct hda_codec
*codec
)
2273 struct snd_card
*card
= codec
->bus
->card
;
2276 if (hda_lock_devices(card
) < 0)
2278 /* check whether the codec isn't used by any mixer or PCM streams */
2279 if (!list_empty(&card
->ctl_files
)) {
2280 hda_unlock_devices(card
);
2283 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2284 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2287 if (cpcm
->pcm
->streams
[0].substream_opened
||
2288 cpcm
->pcm
->streams
[1].substream_opened
) {
2289 hda_unlock_devices(card
);
2294 /* OK, let it free */
2296 #ifdef CONFIG_SND_HDA_POWER_SAVE
2297 cancel_delayed_work(&codec
->power_work
);
2298 flush_workqueue(codec
->bus
->workq
);
2300 snd_hda_ctls_clear(codec
);
2302 for (i
= 0; i
< codec
->num_pcms
; i
++) {
2303 if (codec
->pcm_info
[i
].pcm
) {
2304 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
2305 clear_bit(codec
->pcm_info
[i
].device
,
2306 codec
->bus
->pcm_dev_bits
);
2309 if (codec
->patch_ops
.free
)
2310 codec
->patch_ops
.free(codec
);
2311 codec
->proc_widget_hook
= NULL
;
2313 free_hda_cache(&codec
->amp_cache
);
2314 free_hda_cache(&codec
->cmd_cache
);
2315 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2316 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2317 /* free only driver_pins so that init_pins + user_pins are restored */
2318 snd_array_free(&codec
->driver_pins
);
2319 restore_pincfgs(codec
);
2320 codec
->num_pcms
= 0;
2321 codec
->pcm_info
= NULL
;
2322 codec
->preset
= NULL
;
2323 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2324 codec
->slave_dig_outs
= NULL
;
2325 codec
->spdif_status_reset
= 0;
2326 module_put(codec
->owner
);
2327 codec
->owner
= NULL
;
2329 /* allow device access again */
2330 hda_unlock_devices(card
);
2334 typedef int (*map_slave_func_t
)(void *, struct snd_kcontrol
*);
2336 /* apply the function to all matching slave ctls in the mixer list */
2337 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2338 map_slave_func_t func
, void *data
)
2340 struct hda_nid_item
*items
;
2341 const char * const *s
;
2344 items
= codec
->mixers
.list
;
2345 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2346 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2347 if (!sctl
|| !sctl
->id
.name
||
2348 sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2350 for (s
= slaves
; *s
; s
++) {
2351 if (!strcmp(sctl
->id
.name
, *s
)) {
2352 err
= func(data
, sctl
);
2362 static int check_slave_present(void *data
, struct snd_kcontrol
*sctl
)
2368 * snd_hda_add_vmaster - create a virtual master control and add slaves
2369 * @codec: HD-audio codec
2370 * @name: vmaster control name
2371 * @tlv: TLV data (optional)
2372 * @slaves: slave control names (optional)
2374 * Create a virtual master control with the given name. The TLV data
2375 * must be either NULL or a valid data.
2377 * @slaves is a NULL-terminated array of strings, each of which is a
2378 * slave control name. All controls with these names are assigned to
2379 * the new virtual master control.
2381 * This function returns zero if successful or a negative error code.
2383 int snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2384 unsigned int *tlv
, const char * const *slaves
)
2386 struct snd_kcontrol
*kctl
;
2389 err
= map_slaves(codec
, slaves
, check_slave_present
, NULL
);
2391 snd_printdd("No slave found for %s\n", name
);
2394 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2397 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2401 err
= map_slaves(codec
, slaves
, (map_slave_func_t
)snd_ctl_add_slave
,
2407 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster
);
2410 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2412 * The control element is supposed to have the private_value field
2413 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2415 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2416 struct snd_ctl_elem_info
*uinfo
)
2418 int chs
= get_amp_channels(kcontrol
);
2420 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2421 uinfo
->count
= chs
== 3 ? 2 : 1;
2422 uinfo
->value
.integer
.min
= 0;
2423 uinfo
->value
.integer
.max
= 1;
2426 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
2429 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2431 * The control element is supposed to have the private_value field
2432 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2434 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2435 struct snd_ctl_elem_value
*ucontrol
)
2437 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2438 hda_nid_t nid
= get_amp_nid(kcontrol
);
2439 int chs
= get_amp_channels(kcontrol
);
2440 int dir
= get_amp_direction(kcontrol
);
2441 int idx
= get_amp_index(kcontrol
);
2442 long *valp
= ucontrol
->value
.integer
.value
;
2445 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2446 HDA_AMP_MUTE
) ? 0 : 1;
2448 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2449 HDA_AMP_MUTE
) ? 0 : 1;
2452 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
2455 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2457 * The control element is supposed to have the private_value field
2458 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2460 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2461 struct snd_ctl_elem_value
*ucontrol
)
2463 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2464 hda_nid_t nid
= get_amp_nid(kcontrol
);
2465 int chs
= get_amp_channels(kcontrol
);
2466 int dir
= get_amp_direction(kcontrol
);
2467 int idx
= get_amp_index(kcontrol
);
2468 long *valp
= ucontrol
->value
.integer
.value
;
2471 snd_hda_power_up(codec
);
2473 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2475 *valp
? 0 : HDA_AMP_MUTE
);
2479 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2481 *valp
? 0 : HDA_AMP_MUTE
);
2482 hda_call_check_power_status(codec
, nid
);
2483 snd_hda_power_down(codec
);
2486 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
2488 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2490 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2492 * This function calls snd_hda_enable_beep_device(), which behaves differently
2493 * depending on beep_mode option.
2495 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol
*kcontrol
,
2496 struct snd_ctl_elem_value
*ucontrol
)
2498 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2499 long *valp
= ucontrol
->value
.integer
.value
;
2501 snd_hda_enable_beep_device(codec
, *valp
);
2502 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2504 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep
);
2505 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2508 * bound volume controls
2510 * bind multiple volumes (# indices, from 0)
2513 #define AMP_VAL_IDX_SHIFT 19
2514 #define AMP_VAL_IDX_MASK (0x0f<<19)
2517 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2519 * The control element is supposed to have the private_value field
2520 * set up via HDA_BIND_MUTE*() macros.
2522 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2523 struct snd_ctl_elem_value
*ucontrol
)
2525 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2529 mutex_lock(&codec
->control_mutex
);
2530 pval
= kcontrol
->private_value
;
2531 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2532 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2533 kcontrol
->private_value
= pval
;
2534 mutex_unlock(&codec
->control_mutex
);
2537 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
2540 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2542 * The control element is supposed to have the private_value field
2543 * set up via HDA_BIND_MUTE*() macros.
2545 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2546 struct snd_ctl_elem_value
*ucontrol
)
2548 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2550 int i
, indices
, err
= 0, change
= 0;
2552 mutex_lock(&codec
->control_mutex
);
2553 pval
= kcontrol
->private_value
;
2554 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2555 for (i
= 0; i
< indices
; i
++) {
2556 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2557 (i
<< AMP_VAL_IDX_SHIFT
);
2558 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2563 kcontrol
->private_value
= pval
;
2564 mutex_unlock(&codec
->control_mutex
);
2565 return err
< 0 ? err
: change
;
2567 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
2570 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2572 * The control element is supposed to have the private_value field
2573 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2575 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2576 struct snd_ctl_elem_info
*uinfo
)
2578 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2579 struct hda_bind_ctls
*c
;
2582 mutex_lock(&codec
->control_mutex
);
2583 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2584 kcontrol
->private_value
= *c
->values
;
2585 err
= c
->ops
->info(kcontrol
, uinfo
);
2586 kcontrol
->private_value
= (long)c
;
2587 mutex_unlock(&codec
->control_mutex
);
2590 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
2593 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2595 * The control element is supposed to have the private_value field
2596 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2598 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2599 struct snd_ctl_elem_value
*ucontrol
)
2601 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2602 struct hda_bind_ctls
*c
;
2605 mutex_lock(&codec
->control_mutex
);
2606 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2607 kcontrol
->private_value
= *c
->values
;
2608 err
= c
->ops
->get(kcontrol
, ucontrol
);
2609 kcontrol
->private_value
= (long)c
;
2610 mutex_unlock(&codec
->control_mutex
);
2613 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
2616 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2618 * The control element is supposed to have the private_value field
2619 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2621 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2622 struct snd_ctl_elem_value
*ucontrol
)
2624 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2625 struct hda_bind_ctls
*c
;
2626 unsigned long *vals
;
2627 int err
= 0, change
= 0;
2629 mutex_lock(&codec
->control_mutex
);
2630 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2631 for (vals
= c
->values
; *vals
; vals
++) {
2632 kcontrol
->private_value
= *vals
;
2633 err
= c
->ops
->put(kcontrol
, ucontrol
);
2638 kcontrol
->private_value
= (long)c
;
2639 mutex_unlock(&codec
->control_mutex
);
2640 return err
< 0 ? err
: change
;
2642 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
2645 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2647 * The control element is supposed to have the private_value field
2648 * set up via HDA_BIND_VOL() macro.
2650 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2651 unsigned int size
, unsigned int __user
*tlv
)
2653 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2654 struct hda_bind_ctls
*c
;
2657 mutex_lock(&codec
->control_mutex
);
2658 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2659 kcontrol
->private_value
= *c
->values
;
2660 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
2661 kcontrol
->private_value
= (long)c
;
2662 mutex_unlock(&codec
->control_mutex
);
2665 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
2667 struct hda_ctl_ops snd_hda_bind_vol
= {
2668 .info
= snd_hda_mixer_amp_volume_info
,
2669 .get
= snd_hda_mixer_amp_volume_get
,
2670 .put
= snd_hda_mixer_amp_volume_put
,
2671 .tlv
= snd_hda_mixer_amp_tlv
2673 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
2675 struct hda_ctl_ops snd_hda_bind_sw
= {
2676 .info
= snd_hda_mixer_amp_switch_info
,
2677 .get
= snd_hda_mixer_amp_switch_get
,
2678 .put
= snd_hda_mixer_amp_switch_put
,
2679 .tlv
= snd_hda_mixer_amp_tlv
2681 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
2684 * SPDIF out controls
2687 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2688 struct snd_ctl_elem_info
*uinfo
)
2690 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2695 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2696 struct snd_ctl_elem_value
*ucontrol
)
2698 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2699 IEC958_AES0_NONAUDIO
|
2700 IEC958_AES0_CON_EMPHASIS_5015
|
2701 IEC958_AES0_CON_NOT_COPYRIGHT
;
2702 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2703 IEC958_AES1_CON_ORIGINAL
;
2707 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2708 struct snd_ctl_elem_value
*ucontrol
)
2710 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2711 IEC958_AES0_NONAUDIO
|
2712 IEC958_AES0_PRO_EMPHASIS_5015
;
2716 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2717 struct snd_ctl_elem_value
*ucontrol
)
2719 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2720 int idx
= kcontrol
->private_value
;
2721 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2723 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
2724 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
2725 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
2726 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
2731 /* convert from SPDIF status bits to HDA SPDIF bits
2732 * bit 0 (DigEn) is always set zero (to be filled later)
2734 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2736 unsigned short val
= 0;
2738 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2739 val
|= AC_DIG1_PROFESSIONAL
;
2740 if (sbits
& IEC958_AES0_NONAUDIO
)
2741 val
|= AC_DIG1_NONAUDIO
;
2742 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2743 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2744 IEC958_AES0_PRO_EMPHASIS_5015
)
2745 val
|= AC_DIG1_EMPHASIS
;
2747 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2748 IEC958_AES0_CON_EMPHASIS_5015
)
2749 val
|= AC_DIG1_EMPHASIS
;
2750 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2751 val
|= AC_DIG1_COPYRIGHT
;
2752 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2753 val
|= AC_DIG1_LEVEL
;
2754 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2759 /* convert to SPDIF status bits from HDA SPDIF bits
2761 static unsigned int convert_to_spdif_status(unsigned short val
)
2763 unsigned int sbits
= 0;
2765 if (val
& AC_DIG1_NONAUDIO
)
2766 sbits
|= IEC958_AES0_NONAUDIO
;
2767 if (val
& AC_DIG1_PROFESSIONAL
)
2768 sbits
|= IEC958_AES0_PROFESSIONAL
;
2769 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2770 if (sbits
& AC_DIG1_EMPHASIS
)
2771 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
2773 if (val
& AC_DIG1_EMPHASIS
)
2774 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
2775 if (!(val
& AC_DIG1_COPYRIGHT
))
2776 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
2777 if (val
& AC_DIG1_LEVEL
)
2778 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
2779 sbits
|= val
& (0x7f << 8);
2784 /* set digital convert verbs both for the given NID and its slaves */
2785 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
2790 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
2791 d
= codec
->slave_dig_outs
;
2795 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
2798 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
2802 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
2804 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
2807 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
2808 struct snd_ctl_elem_value
*ucontrol
)
2810 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2811 int idx
= kcontrol
->private_value
;
2812 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2813 hda_nid_t nid
= spdif
->nid
;
2817 mutex_lock(&codec
->spdif_mutex
);
2818 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
2819 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
2820 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
2821 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
2822 val
= convert_from_spdif_status(spdif
->status
);
2823 val
|= spdif
->ctls
& 1;
2824 change
= spdif
->ctls
!= val
;
2826 if (change
&& nid
!= (u16
)-1)
2827 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2828 mutex_unlock(&codec
->spdif_mutex
);
2832 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2834 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
2835 struct snd_ctl_elem_value
*ucontrol
)
2837 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2838 int idx
= kcontrol
->private_value
;
2839 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2841 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
2845 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
2848 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
2849 /* unmute amp switch (if any) */
2850 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
2851 (dig1
& AC_DIG1_ENABLE
))
2852 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
2856 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
2857 struct snd_ctl_elem_value
*ucontrol
)
2859 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2860 int idx
= kcontrol
->private_value
;
2861 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2862 hda_nid_t nid
= spdif
->nid
;
2866 mutex_lock(&codec
->spdif_mutex
);
2867 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
2868 if (ucontrol
->value
.integer
.value
[0])
2869 val
|= AC_DIG1_ENABLE
;
2870 change
= spdif
->ctls
!= val
;
2872 if (change
&& nid
!= (u16
)-1)
2873 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
2874 mutex_unlock(&codec
->spdif_mutex
);
2878 static struct snd_kcontrol_new dig_mixes
[] = {
2880 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2881 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2882 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
2883 .info
= snd_hda_spdif_mask_info
,
2884 .get
= snd_hda_spdif_cmask_get
,
2887 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2888 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2889 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
2890 .info
= snd_hda_spdif_mask_info
,
2891 .get
= snd_hda_spdif_pmask_get
,
2894 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2895 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
2896 .info
= snd_hda_spdif_mask_info
,
2897 .get
= snd_hda_spdif_default_get
,
2898 .put
= snd_hda_spdif_default_put
,
2901 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2902 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
2903 .info
= snd_hda_spdif_out_switch_info
,
2904 .get
= snd_hda_spdif_out_switch_get
,
2905 .put
= snd_hda_spdif_out_switch_put
,
2911 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2912 * @codec: the HDA codec
2913 * @nid: audio out widget NID
2915 * Creates controls related with the SPDIF output.
2916 * Called from each patch supporting the SPDIF out.
2918 * Returns 0 if successful, or a negative error code.
2920 int snd_hda_create_spdif_out_ctls(struct hda_codec
*codec
,
2921 hda_nid_t associated_nid
,
2925 struct snd_kcontrol
*kctl
;
2926 struct snd_kcontrol_new
*dig_mix
;
2928 struct hda_spdif_out
*spdif
;
2930 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch");
2932 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
2935 spdif
= snd_array_new(&codec
->spdif_out
);
2936 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2937 kctl
= snd_ctl_new1(dig_mix
, codec
);
2940 kctl
->id
.index
= idx
;
2941 kctl
->private_value
= codec
->spdif_out
.used
- 1;
2942 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
2946 spdif
->nid
= cvt_nid
;
2947 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
2948 AC_VERB_GET_DIGI_CONVERT_1
, 0);
2949 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
2952 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls
);
2954 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
2958 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
2959 struct hda_spdif_out
*spdif
=
2960 snd_array_elem(&codec
->spdif_out
, i
);
2961 if (spdif
->nid
== nid
)
2966 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid
);
2968 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
2970 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2972 mutex_lock(&codec
->spdif_mutex
);
2973 spdif
->nid
= (u16
)-1;
2974 mutex_unlock(&codec
->spdif_mutex
);
2976 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign
);
2978 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
2980 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2983 mutex_lock(&codec
->spdif_mutex
);
2984 if (spdif
->nid
!= nid
) {
2987 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2989 mutex_unlock(&codec
->spdif_mutex
);
2991 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign
);
2994 * SPDIF sharing with analog output
2996 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
2997 struct snd_ctl_elem_value
*ucontrol
)
2999 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3000 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
3004 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
3005 struct snd_ctl_elem_value
*ucontrol
)
3007 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3008 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
3012 static struct snd_kcontrol_new spdif_share_sw
= {
3013 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3014 .name
= "IEC958 Default PCM Playback Switch",
3015 .info
= snd_ctl_boolean_mono_info
,
3016 .get
= spdif_share_sw_get
,
3017 .put
= spdif_share_sw_put
,
3021 * snd_hda_create_spdif_share_sw - create Default PCM switch
3022 * @codec: the HDA codec
3023 * @mout: multi-out instance
3025 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
3026 struct hda_multi_out
*mout
)
3028 if (!mout
->dig_out_nid
)
3030 /* ATTENTION: here mout is passed as private_data, instead of codec */
3031 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
,
3032 snd_ctl_new1(&spdif_share_sw
, mout
));
3034 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
3040 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3042 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3043 struct snd_ctl_elem_value
*ucontrol
)
3045 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3047 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3051 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3052 struct snd_ctl_elem_value
*ucontrol
)
3054 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3055 hda_nid_t nid
= kcontrol
->private_value
;
3056 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3059 mutex_lock(&codec
->spdif_mutex
);
3060 change
= codec
->spdif_in_enable
!= val
;
3062 codec
->spdif_in_enable
= val
;
3063 snd_hda_codec_write_cache(codec
, nid
, 0,
3064 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3066 mutex_unlock(&codec
->spdif_mutex
);
3070 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3071 struct snd_ctl_elem_value
*ucontrol
)
3073 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3074 hda_nid_t nid
= kcontrol
->private_value
;
3078 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3079 sbits
= convert_to_spdif_status(val
);
3080 ucontrol
->value
.iec958
.status
[0] = sbits
;
3081 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3082 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3083 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3087 static struct snd_kcontrol_new dig_in_ctls
[] = {
3089 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3090 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3091 .info
= snd_hda_spdif_in_switch_info
,
3092 .get
= snd_hda_spdif_in_switch_get
,
3093 .put
= snd_hda_spdif_in_switch_put
,
3096 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3097 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3098 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3099 .info
= snd_hda_spdif_mask_info
,
3100 .get
= snd_hda_spdif_in_status_get
,
3106 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3107 * @codec: the HDA codec
3108 * @nid: audio in widget NID
3110 * Creates controls related with the SPDIF input.
3111 * Called from each patch supporting the SPDIF in.
3113 * Returns 0 if successful, or a negative error code.
3115 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3118 struct snd_kcontrol
*kctl
;
3119 struct snd_kcontrol_new
*dig_mix
;
3122 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch");
3124 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
3127 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3128 kctl
= snd_ctl_new1(dig_mix
, codec
);
3131 kctl
->private_value
= nid
;
3132 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3136 codec
->spdif_in_enable
=
3137 snd_hda_codec_read(codec
, nid
, 0,
3138 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3142 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
3149 /* build a 32bit cache key with the widget id and the command parameter */
3150 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3151 #define get_cmd_cache_nid(key) ((key) & 0xff)
3152 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3155 * snd_hda_codec_write_cache - send a single command with caching
3156 * @codec: the HDA codec
3157 * @nid: NID to send the command
3158 * @direct: direct flag
3159 * @verb: the verb to send
3160 * @parm: the parameter for the verb
3162 * Send a single command without waiting for response.
3164 * Returns 0 if successful, or a negative error code.
3166 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3167 int direct
, unsigned int verb
, unsigned int parm
)
3169 int err
= snd_hda_codec_write(codec
, nid
, direct
, verb
, parm
);
3170 struct hda_cache_head
*c
;
3175 /* parm may contain the verb stuff for get/set amp */
3176 verb
= verb
| (parm
>> 8);
3178 key
= build_cmd_cache_key(nid
, verb
);
3179 mutex_lock(&codec
->bus
->cmd_mutex
);
3180 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3183 mutex_unlock(&codec
->bus
->cmd_mutex
);
3186 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
3189 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3190 * @codec: the HDA codec
3191 * @nid: NID to send the command
3192 * @direct: direct flag
3193 * @verb: the verb to send
3194 * @parm: the parameter for the verb
3196 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3197 * command if the parameter is already identical with the cached value.
3198 * If not, it sends the command and refreshes the cache.
3200 * Returns 0 if successful, or a negative error code.
3202 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3203 int direct
, unsigned int verb
, unsigned int parm
)
3205 struct hda_cache_head
*c
;
3208 /* parm may contain the verb stuff for get/set amp */
3209 verb
= verb
| (parm
>> 8);
3211 key
= build_cmd_cache_key(nid
, verb
);
3212 mutex_lock(&codec
->bus
->cmd_mutex
);
3213 c
= get_hash(&codec
->cmd_cache
, key
);
3214 if (c
&& c
->val
== parm
) {
3215 mutex_unlock(&codec
->bus
->cmd_mutex
);
3218 mutex_unlock(&codec
->bus
->cmd_mutex
);
3219 return snd_hda_codec_write_cache(codec
, nid
, direct
, verb
, parm
);
3221 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache
);
3224 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3225 * @codec: HD-audio codec
3227 * Execute all verbs recorded in the command caches to resume.
3229 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3231 struct hda_cache_head
*buffer
= codec
->cmd_cache
.buf
.list
;
3234 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++, buffer
++) {
3235 u32 key
= buffer
->key
;
3238 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3239 get_cmd_cache_cmd(key
), buffer
->val
);
3242 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
3245 * snd_hda_sequence_write_cache - sequence writes with caching
3246 * @codec: the HDA codec
3247 * @seq: VERB array to send
3249 * Send the commands sequentially from the given array.
3250 * Thte commands are recorded on cache for power-save and resume.
3251 * The array must be terminated with NID=0.
3253 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3254 const struct hda_verb
*seq
)
3256 for (; seq
->nid
; seq
++)
3257 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3260 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
3261 #endif /* CONFIG_PM */
3263 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
3264 unsigned int power_state
,
3265 bool eapd_workaround
)
3267 hda_nid_t nid
= codec
->start_nid
;
3270 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3271 unsigned int wcaps
= get_wcaps(codec
, nid
);
3272 if (!(wcaps
& AC_WCAP_POWER
))
3274 /* don't power down the widget if it controls eapd and
3275 * EAPD_BTLENABLE is set.
3277 if (eapd_workaround
&& power_state
== AC_PWRST_D3
&&
3278 get_wcaps_type(wcaps
) == AC_WID_PIN
&&
3279 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
3280 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
3281 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3285 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
3289 if (power_state
== AC_PWRST_D0
) {
3290 unsigned long end_time
;
3292 /* wait until the codec reachs to D0 */
3293 end_time
= jiffies
+ msecs_to_jiffies(500);
3295 state
= snd_hda_codec_read(codec
, fg
, 0,
3296 AC_VERB_GET_POWER_STATE
, 0);
3297 if (state
== power_state
)
3300 } while (time_after_eq(end_time
, jiffies
));
3303 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all
);
3306 * set power state of the codec
3308 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
3309 unsigned int power_state
)
3311 if (codec
->patch_ops
.set_power_state
) {
3312 codec
->patch_ops
.set_power_state(codec
, fg
, power_state
);
3316 /* this delay seems necessary to avoid click noise at power-down */
3317 if (power_state
== AC_PWRST_D3
)
3319 snd_hda_codec_read(codec
, fg
, 0, AC_VERB_SET_POWER_STATE
,
3321 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
, true);
3324 #ifdef CONFIG_SND_HDA_HWDEP
3325 /* execute additional init verbs */
3326 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3328 if (codec
->init_verbs
.list
)
3329 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3332 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3337 * call suspend and power-down; used both from PM and power-save
3339 static void hda_call_codec_suspend(struct hda_codec
*codec
)
3341 if (codec
->patch_ops
.suspend
)
3342 codec
->patch_ops
.suspend(codec
, PMSG_SUSPEND
);
3343 hda_cleanup_all_streams(codec
);
3344 hda_set_power_state(codec
,
3345 codec
->afg
? codec
->afg
: codec
->mfg
,
3347 #ifdef CONFIG_SND_HDA_POWER_SAVE
3348 snd_hda_update_power_acct(codec
);
3349 cancel_delayed_work(&codec
->power_work
);
3350 codec
->power_on
= 0;
3351 codec
->power_transition
= 0;
3352 codec
->power_jiffies
= jiffies
;
3357 * kick up codec; used both from PM and power-save
3359 static void hda_call_codec_resume(struct hda_codec
*codec
)
3361 hda_set_power_state(codec
,
3362 codec
->afg
? codec
->afg
: codec
->mfg
,
3364 restore_pincfgs(codec
); /* restore all current pin configs */
3365 restore_shutup_pins(codec
);
3366 hda_exec_init_verbs(codec
);
3367 if (codec
->patch_ops
.resume
)
3368 codec
->patch_ops
.resume(codec
);
3370 if (codec
->patch_ops
.init
)
3371 codec
->patch_ops
.init(codec
);
3372 snd_hda_codec_resume_amp(codec
);
3373 snd_hda_codec_resume_cache(codec
);
3376 #endif /* CONFIG_PM */
3380 * snd_hda_build_controls - build mixer controls
3383 * Creates mixer controls for each codec included in the bus.
3385 * Returns 0 if successful, otherwise a negative error code.
3387 int /*__devinit*/ snd_hda_build_controls(struct hda_bus
*bus
)
3389 struct hda_codec
*codec
;
3391 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3392 int err
= snd_hda_codec_build_controls(codec
);
3394 printk(KERN_ERR
"hda_codec: cannot build controls "
3395 "for #%d (error %d)\n", codec
->addr
, err
);
3396 err
= snd_hda_codec_reset(codec
);
3399 "hda_codec: cannot revert codec\n");
3406 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
3408 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
3411 hda_exec_init_verbs(codec
);
3412 /* continue to initialize... */
3413 if (codec
->patch_ops
.init
)
3414 err
= codec
->patch_ops
.init(codec
);
3415 if (!err
&& codec
->patch_ops
.build_controls
)
3416 err
= codec
->patch_ops
.build_controls(codec
);
3425 struct hda_rate_tbl
{
3427 unsigned int alsa_bits
;
3428 unsigned int hda_fmt
;
3431 /* rate = base * mult / div */
3432 #define HDA_RATE(base, mult, div) \
3433 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3434 (((div) - 1) << AC_FMT_DIV_SHIFT))
3436 static struct hda_rate_tbl rate_bits
[] = {
3437 /* rate in Hz, ALSA rate bitmask, HDA format value */
3439 /* autodetected value used in snd_hda_query_supported_pcm */
3440 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
3441 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
3442 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
3443 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
3444 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
3445 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
3446 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
3447 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
3448 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
3449 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
3450 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
3451 #define AC_PAR_PCM_RATE_BITS 11
3452 /* up to bits 10, 384kHZ isn't supported properly */
3454 /* not autodetected value */
3455 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
3457 { 0 } /* terminator */
3461 * snd_hda_calc_stream_format - calculate format bitset
3462 * @rate: the sample rate
3463 * @channels: the number of channels
3464 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3465 * @maxbps: the max. bps
3467 * Calculate the format bitset from the given rate, channels and th PCM format.
3469 * Return zero if invalid.
3471 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
3472 unsigned int channels
,
3473 unsigned int format
,
3474 unsigned int maxbps
,
3475 unsigned short spdif_ctls
)
3478 unsigned int val
= 0;
3480 for (i
= 0; rate_bits
[i
].hz
; i
++)
3481 if (rate_bits
[i
].hz
== rate
) {
3482 val
= rate_bits
[i
].hda_fmt
;
3485 if (!rate_bits
[i
].hz
) {
3486 snd_printdd("invalid rate %d\n", rate
);
3490 if (channels
== 0 || channels
> 8) {
3491 snd_printdd("invalid channels %d\n", channels
);
3494 val
|= channels
- 1;
3496 switch (snd_pcm_format_width(format
)) {
3498 val
|= AC_FMT_BITS_8
;
3501 val
|= AC_FMT_BITS_16
;
3506 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
3507 val
|= AC_FMT_BITS_32
;
3508 else if (maxbps
>= 24)
3509 val
|= AC_FMT_BITS_24
;
3511 val
|= AC_FMT_BITS_20
;
3514 snd_printdd("invalid format width %d\n",
3515 snd_pcm_format_width(format
));
3519 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
3520 val
|= AC_FMT_TYPE_NON_PCM
;
3524 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
3526 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3528 unsigned int val
= 0;
3529 if (nid
!= codec
->afg
&&
3530 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
3531 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
3532 if (!val
|| val
== -1)
3533 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
3534 if (!val
|| val
== -1)
3539 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3541 return query_caps_hash(codec
, nid
, HDA_HASH_PARPCM_KEY(nid
),
3545 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3547 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
3548 if (!streams
|| streams
== -1)
3549 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
3550 if (!streams
|| streams
== -1)
3555 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3557 return query_caps_hash(codec
, nid
, HDA_HASH_PARSTR_KEY(nid
),
3562 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3563 * @codec: the HDA codec
3564 * @nid: NID to query
3565 * @ratesp: the pointer to store the detected rate bitflags
3566 * @formatsp: the pointer to store the detected formats
3567 * @bpsp: the pointer to store the detected format widths
3569 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3570 * or @bsps argument is ignored.
3572 * Returns 0 if successful, otherwise a negative error code.
3574 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
3575 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
3577 unsigned int i
, val
, wcaps
;
3579 wcaps
= get_wcaps(codec
, nid
);
3580 val
= query_pcm_param(codec
, nid
);
3584 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
3586 rates
|= rate_bits
[i
].alsa_bits
;
3589 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
3590 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3592 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
3598 if (formatsp
|| bpsp
) {
3600 unsigned int streams
, bps
;
3602 streams
= query_stream_param(codec
, nid
);
3607 if (streams
& AC_SUPFMT_PCM
) {
3608 if (val
& AC_SUPPCM_BITS_8
) {
3609 formats
|= SNDRV_PCM_FMTBIT_U8
;
3612 if (val
& AC_SUPPCM_BITS_16
) {
3613 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
3616 if (wcaps
& AC_WCAP_DIGITAL
) {
3617 if (val
& AC_SUPPCM_BITS_32
)
3618 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
3619 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
3620 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3621 if (val
& AC_SUPPCM_BITS_24
)
3623 else if (val
& AC_SUPPCM_BITS_20
)
3625 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
3626 AC_SUPPCM_BITS_32
)) {
3627 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3628 if (val
& AC_SUPPCM_BITS_32
)
3630 else if (val
& AC_SUPPCM_BITS_24
)
3632 else if (val
& AC_SUPPCM_BITS_20
)
3636 if (streams
& AC_SUPFMT_FLOAT32
) {
3637 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
3641 if (streams
== AC_SUPFMT_AC3
) {
3642 /* should be exclusive */
3643 /* temporary hack: we have still no proper support
3644 * for the direct AC3 stream...
3646 formats
|= SNDRV_PCM_FMTBIT_U8
;
3650 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
3651 "(nid=0x%x, val=0x%x, ovrd=%i, "
3654 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
3659 *formatsp
= formats
;
3666 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm
);
3669 * snd_hda_is_supported_format - Check the validity of the format
3670 * @codec: HD-audio codec
3671 * @nid: NID to check
3672 * @format: the HD-audio format value to check
3674 * Check whether the given node supports the format value.
3676 * Returns 1 if supported, 0 if not.
3678 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
3679 unsigned int format
)
3682 unsigned int val
= 0, rate
, stream
;
3684 val
= query_pcm_param(codec
, nid
);
3688 rate
= format
& 0xff00;
3689 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
3690 if (rate_bits
[i
].hda_fmt
== rate
) {
3695 if (i
>= AC_PAR_PCM_RATE_BITS
)
3698 stream
= query_stream_param(codec
, nid
);
3702 if (stream
& AC_SUPFMT_PCM
) {
3703 switch (format
& 0xf0) {
3705 if (!(val
& AC_SUPPCM_BITS_8
))
3709 if (!(val
& AC_SUPPCM_BITS_16
))
3713 if (!(val
& AC_SUPPCM_BITS_20
))
3717 if (!(val
& AC_SUPPCM_BITS_24
))
3721 if (!(val
& AC_SUPPCM_BITS_32
))
3728 /* FIXME: check for float32 and AC3? */
3733 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
3738 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
3739 struct hda_codec
*codec
,
3740 struct snd_pcm_substream
*substream
)
3745 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
3746 struct hda_codec
*codec
,
3747 unsigned int stream_tag
,
3748 unsigned int format
,
3749 struct snd_pcm_substream
*substream
)
3751 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3755 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
3756 struct hda_codec
*codec
,
3757 struct snd_pcm_substream
*substream
)
3759 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3763 static int set_pcm_default_values(struct hda_codec
*codec
,
3764 struct hda_pcm_stream
*info
)
3768 /* query support PCM information from the given NID */
3769 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
3770 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
3771 info
->rates
? NULL
: &info
->rates
,
3772 info
->formats
? NULL
: &info
->formats
,
3773 info
->maxbps
? NULL
: &info
->maxbps
);
3777 if (info
->ops
.open
== NULL
)
3778 info
->ops
.open
= hda_pcm_default_open_close
;
3779 if (info
->ops
.close
== NULL
)
3780 info
->ops
.close
= hda_pcm_default_open_close
;
3781 if (info
->ops
.prepare
== NULL
) {
3782 if (snd_BUG_ON(!info
->nid
))
3784 info
->ops
.prepare
= hda_pcm_default_prepare
;
3786 if (info
->ops
.cleanup
== NULL
) {
3787 if (snd_BUG_ON(!info
->nid
))
3789 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
3795 * codec prepare/cleanup entries
3797 int snd_hda_codec_prepare(struct hda_codec
*codec
,
3798 struct hda_pcm_stream
*hinfo
,
3799 unsigned int stream
,
3800 unsigned int format
,
3801 struct snd_pcm_substream
*substream
)
3804 mutex_lock(&codec
->bus
->prepare_mutex
);
3805 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
, substream
);
3807 purify_inactive_streams(codec
);
3808 mutex_unlock(&codec
->bus
->prepare_mutex
);
3811 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare
);
3813 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
3814 struct hda_pcm_stream
*hinfo
,
3815 struct snd_pcm_substream
*substream
)
3817 mutex_lock(&codec
->bus
->prepare_mutex
);
3818 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
3819 mutex_unlock(&codec
->bus
->prepare_mutex
);
3821 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup
);
3824 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
3825 "Audio", "SPDIF", "HDMI", "Modem"
3829 * get the empty PCM device number to assign
3831 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3833 static int get_empty_pcm_device(struct hda_bus
*bus
, int type
)
3835 /* audio device indices; not linear to keep compatibility */
3836 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
3837 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
3838 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
3839 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
3840 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
3844 if (type
>= HDA_PCM_NTYPES
) {
3845 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
3849 for (i
= 0; audio_idx
[type
][i
] >= 0 ; i
++)
3850 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
3851 return audio_idx
[type
][i
];
3853 snd_printk(KERN_WARNING
"Too many %s devices\n",
3854 snd_hda_pcm_type_name
[type
]);
3859 * attach a new PCM stream
3861 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
3863 struct hda_bus
*bus
= codec
->bus
;
3864 struct hda_pcm_stream
*info
;
3867 if (snd_BUG_ON(!pcm
->name
))
3869 for (stream
= 0; stream
< 2; stream
++) {
3870 info
= &pcm
->stream
[stream
];
3871 if (info
->substreams
) {
3872 err
= set_pcm_default_values(codec
, info
);
3877 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
3880 /* assign all PCMs of the given codec */
3881 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
3886 if (!codec
->num_pcms
) {
3887 if (!codec
->patch_ops
.build_pcms
)
3889 err
= codec
->patch_ops
.build_pcms(codec
);
3891 printk(KERN_ERR
"hda_codec: cannot build PCMs"
3892 "for #%d (error %d)\n", codec
->addr
, err
);
3893 err
= snd_hda_codec_reset(codec
);
3896 "hda_codec: cannot revert codec\n");
3901 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
3902 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
3905 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
3906 continue; /* no substreams assigned */
3909 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
3911 continue; /* no fatal error */
3913 err
= snd_hda_attach_pcm(codec
, cpcm
);
3915 printk(KERN_ERR
"hda_codec: cannot attach "
3916 "PCM stream %d for codec #%d\n",
3918 continue; /* no fatal error */
3926 * snd_hda_build_pcms - build PCM information
3929 * Create PCM information for each codec included in the bus.
3931 * The build_pcms codec patch is requested to set up codec->num_pcms and
3932 * codec->pcm_info properly. The array is referred by the top-level driver
3933 * to create its PCM instances.
3934 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3937 * At least, substreams, channels_min and channels_max must be filled for
3938 * each stream. substreams = 0 indicates that the stream doesn't exist.
3939 * When rates and/or formats are zero, the supported values are queried
3940 * from the given nid. The nid is used also by the default ops.prepare
3941 * and ops.cleanup callbacks.
3943 * The driver needs to call ops.open in its open callback. Similarly,
3944 * ops.close is supposed to be called in the close callback.
3945 * ops.prepare should be called in the prepare or hw_params callback
3946 * with the proper parameters for set up.
3947 * ops.cleanup should be called in hw_free for clean up of streams.
3949 * This function returns 0 if successful, or a negative error code.
3951 int __devinit
snd_hda_build_pcms(struct hda_bus
*bus
)
3953 struct hda_codec
*codec
;
3955 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3956 int err
= snd_hda_codec_build_pcms(codec
);
3962 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
3965 * snd_hda_check_board_config - compare the current codec with the config table
3966 * @codec: the HDA codec
3967 * @num_configs: number of config enums
3968 * @models: array of model name strings
3969 * @tbl: configuration table, terminated by null entries
3971 * Compares the modelname or PCI subsystem id of the current codec with the
3972 * given configuration table. If a matching entry is found, returns its
3973 * config value (supposed to be 0 or positive).
3975 * If no entries are matching, the function returns a negative value.
3977 int snd_hda_check_board_config(struct hda_codec
*codec
,
3978 int num_configs
, const char * const *models
,
3979 const struct snd_pci_quirk
*tbl
)
3981 if (codec
->modelname
&& models
) {
3983 for (i
= 0; i
< num_configs
; i
++) {
3985 !strcmp(codec
->modelname
, models
[i
])) {
3986 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
3987 "selected\n", models
[i
]);
3993 if (!codec
->bus
->pci
|| !tbl
)
3996 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
3999 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4000 #ifdef CONFIG_SND_DEBUG_VERBOSE
4002 const char *model
= NULL
;
4004 model
= models
[tbl
->value
];
4006 sprintf(tmp
, "#%d", tbl
->value
);
4009 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4010 "for config %x:%x (%s)\n",
4011 model
, tbl
->subvendor
, tbl
->subdevice
,
4012 (tbl
->name
? tbl
->name
: "Unknown device"));
4018 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
4021 * snd_hda_check_board_codec_sid_config - compare the current codec
4022 subsystem ID with the
4025 This is important for Gateway notebooks with SB450 HDA Audio
4026 where the vendor ID of the PCI device is:
4027 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4028 and the vendor/subvendor are found only at the codec.
4030 * @codec: the HDA codec
4031 * @num_configs: number of config enums
4032 * @models: array of model name strings
4033 * @tbl: configuration table, terminated by null entries
4035 * Compares the modelname or PCI subsystem id of the current codec with the
4036 * given configuration table. If a matching entry is found, returns its
4037 * config value (supposed to be 0 or positive).
4039 * If no entries are matching, the function returns a negative value.
4041 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
4042 int num_configs
, const char * const *models
,
4043 const struct snd_pci_quirk
*tbl
)
4045 const struct snd_pci_quirk
*q
;
4047 /* Search for codec ID */
4048 for (q
= tbl
; q
->subvendor
; q
++) {
4049 unsigned int mask
= 0xffff0000 | q
->subdevice_mask
;
4050 unsigned int id
= (q
->subdevice
| (q
->subvendor
<< 16)) & mask
;
4051 if ((codec
->subsystem_id
& mask
) == id
)
4060 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4061 #ifdef CONFIG_SND_DEBUG_VERBOSE
4063 const char *model
= NULL
;
4065 model
= models
[tbl
->value
];
4067 sprintf(tmp
, "#%d", tbl
->value
);
4070 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4071 "for config %x:%x (%s)\n",
4072 model
, tbl
->subvendor
, tbl
->subdevice
,
4073 (tbl
->name
? tbl
->name
: "Unknown device"));
4079 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
4082 * snd_hda_add_new_ctls - create controls from the array
4083 * @codec: the HDA codec
4084 * @knew: the array of struct snd_kcontrol_new
4086 * This helper function creates and add new controls in the given array.
4087 * The array must be terminated with an empty entry as terminator.
4089 * Returns 0 if successful, or a negative error code.
4091 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4092 const struct snd_kcontrol_new
*knew
)
4096 for (; knew
->name
; knew
++) {
4097 struct snd_kcontrol
*kctl
;
4098 int addr
= 0, idx
= 0;
4099 if (knew
->iface
== -1) /* skip this codec private value */
4102 kctl
= snd_ctl_new1(knew
, codec
);
4106 kctl
->id
.device
= addr
;
4108 kctl
->id
.index
= idx
;
4109 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4112 /* try first with another device index corresponding to
4113 * the codec addr; if it still fails (or it's the
4114 * primary codec), then try another control index
4116 if (!addr
&& codec
->addr
)
4118 else if (!idx
&& !knew
->index
) {
4119 idx
= find_empty_mixer_ctl_idx(codec
,
4129 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
4131 #ifdef CONFIG_SND_HDA_POWER_SAVE
4132 static void hda_power_work(struct work_struct
*work
)
4134 struct hda_codec
*codec
=
4135 container_of(work
, struct hda_codec
, power_work
.work
);
4136 struct hda_bus
*bus
= codec
->bus
;
4138 if (!codec
->power_on
|| codec
->power_count
) {
4139 codec
->power_transition
= 0;
4143 trace_hda_power_down(codec
);
4144 hda_call_codec_suspend(codec
);
4145 if (bus
->ops
.pm_notify
)
4146 bus
->ops
.pm_notify(bus
);
4149 static void hda_keep_power_on(struct hda_codec
*codec
)
4151 codec
->power_count
++;
4152 codec
->power_on
= 1;
4153 codec
->power_jiffies
= jiffies
;
4156 /* update the power on/off account with the current jiffies */
4157 void snd_hda_update_power_acct(struct hda_codec
*codec
)
4159 unsigned long delta
= jiffies
- codec
->power_jiffies
;
4160 if (codec
->power_on
)
4161 codec
->power_on_acct
+= delta
;
4163 codec
->power_off_acct
+= delta
;
4164 codec
->power_jiffies
+= delta
;
4168 * snd_hda_power_up - Power-up the codec
4169 * @codec: HD-audio codec
4171 * Increment the power-up counter and power up the hardware really when
4172 * not turned on yet.
4174 void snd_hda_power_up(struct hda_codec
*codec
)
4176 struct hda_bus
*bus
= codec
->bus
;
4178 codec
->power_count
++;
4179 if (codec
->power_on
|| codec
->power_transition
)
4182 trace_hda_power_up(codec
);
4183 snd_hda_update_power_acct(codec
);
4184 codec
->power_on
= 1;
4185 codec
->power_jiffies
= jiffies
;
4186 if (bus
->ops
.pm_notify
)
4187 bus
->ops
.pm_notify(bus
);
4188 hda_call_codec_resume(codec
);
4189 cancel_delayed_work(&codec
->power_work
);
4190 codec
->power_transition
= 0;
4192 EXPORT_SYMBOL_HDA(snd_hda_power_up
);
4194 #define power_save(codec) \
4195 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4198 * snd_hda_power_down - Power-down the codec
4199 * @codec: HD-audio codec
4201 * Decrement the power-up counter and schedules the power-off work if
4202 * the counter rearches to zero.
4204 void snd_hda_power_down(struct hda_codec
*codec
)
4206 --codec
->power_count
;
4207 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
4209 if (power_save(codec
)) {
4210 codec
->power_transition
= 1; /* avoid reentrance */
4211 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
4212 msecs_to_jiffies(power_save(codec
) * 1000));
4215 EXPORT_SYMBOL_HDA(snd_hda_power_down
);
4218 * snd_hda_check_amp_list_power - Check the amp list and update the power
4219 * @codec: HD-audio codec
4220 * @check: the object containing an AMP list and the status
4221 * @nid: NID to check / update
4223 * Check whether the given NID is in the amp list. If it's in the list,
4224 * check the current AMP status, and update the the power-status according
4225 * to the mute status.
4227 * This function is supposed to be set or called from the check_power_status
4230 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4231 struct hda_loopback_check
*check
,
4234 const struct hda_amp_list
*p
;
4237 if (!check
->amplist
)
4239 for (p
= check
->amplist
; p
->nid
; p
++) {
4244 return 0; /* nothing changed */
4246 for (p
= check
->amplist
; p
->nid
; p
++) {
4247 for (ch
= 0; ch
< 2; ch
++) {
4248 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
4250 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
4251 if (!check
->power_on
) {
4252 check
->power_on
= 1;
4253 snd_hda_power_up(codec
);
4259 if (check
->power_on
) {
4260 check
->power_on
= 0;
4261 snd_hda_power_down(codec
);
4265 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
4269 * Channel mode helper
4273 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4275 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
4276 struct snd_ctl_elem_info
*uinfo
,
4277 const struct hda_channel_mode
*chmode
,
4280 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4282 uinfo
->value
.enumerated
.items
= num_chmodes
;
4283 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
4284 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
4285 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
4286 chmode
[uinfo
->value
.enumerated
.item
].channels
);
4289 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
4292 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4294 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
4295 struct snd_ctl_elem_value
*ucontrol
,
4296 const struct hda_channel_mode
*chmode
,
4302 for (i
= 0; i
< num_chmodes
; i
++) {
4303 if (max_channels
== chmode
[i
].channels
) {
4304 ucontrol
->value
.enumerated
.item
[0] = i
;
4310 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
4313 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4315 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
4316 struct snd_ctl_elem_value
*ucontrol
,
4317 const struct hda_channel_mode
*chmode
,
4323 mode
= ucontrol
->value
.enumerated
.item
[0];
4324 if (mode
>= num_chmodes
)
4326 if (*max_channelsp
== chmode
[mode
].channels
)
4328 /* change the current channel setting */
4329 *max_channelsp
= chmode
[mode
].channels
;
4330 if (chmode
[mode
].sequence
)
4331 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
4334 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
4341 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4343 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
4344 struct snd_ctl_elem_info
*uinfo
)
4348 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4350 uinfo
->value
.enumerated
.items
= imux
->num_items
;
4351 if (!imux
->num_items
)
4353 index
= uinfo
->value
.enumerated
.item
;
4354 if (index
>= imux
->num_items
)
4355 index
= imux
->num_items
- 1;
4356 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
4359 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
4362 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4364 int snd_hda_input_mux_put(struct hda_codec
*codec
,
4365 const struct hda_input_mux
*imux
,
4366 struct snd_ctl_elem_value
*ucontrol
,
4368 unsigned int *cur_val
)
4372 if (!imux
->num_items
)
4374 idx
= ucontrol
->value
.enumerated
.item
[0];
4375 if (idx
>= imux
->num_items
)
4376 idx
= imux
->num_items
- 1;
4377 if (*cur_val
== idx
)
4379 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
4380 imux
->items
[idx
].index
);
4384 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
4388 * Multi-channel / digital-out PCM helper functions
4391 /* setup SPDIF output stream */
4392 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
4393 unsigned int stream_tag
, unsigned int format
)
4395 struct hda_spdif_out
*spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
4397 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4398 if (codec
->spdif_status_reset
&& (spdif
->ctls
& AC_DIG1_ENABLE
))
4399 set_dig_out_convert(codec
, nid
,
4400 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
4402 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
4403 if (codec
->slave_dig_outs
) {
4405 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4406 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
4409 /* turn on again (if needed) */
4410 if (codec
->spdif_status_reset
&& (spdif
->ctls
& AC_DIG1_ENABLE
))
4411 set_dig_out_convert(codec
, nid
,
4412 spdif
->ctls
& 0xff, -1);
4415 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
4417 snd_hda_codec_cleanup_stream(codec
, nid
);
4418 if (codec
->slave_dig_outs
) {
4420 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4421 snd_hda_codec_cleanup_stream(codec
, *d
);
4426 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4427 * @bus: HD-audio bus
4429 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
4431 struct hda_codec
*codec
;
4435 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4436 if (hda_codec_is_power_on(codec
) &&
4437 codec
->patch_ops
.reboot_notify
)
4438 codec
->patch_ops
.reboot_notify(codec
);
4441 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify
);
4444 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4446 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
4447 struct hda_multi_out
*mout
)
4449 mutex_lock(&codec
->spdif_mutex
);
4450 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
4451 /* already opened as analog dup; reset it once */
4452 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4453 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
4454 mutex_unlock(&codec
->spdif_mutex
);
4457 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
4460 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4462 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
4463 struct hda_multi_out
*mout
,
4464 unsigned int stream_tag
,
4465 unsigned int format
,
4466 struct snd_pcm_substream
*substream
)
4468 mutex_lock(&codec
->spdif_mutex
);
4469 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
4470 mutex_unlock(&codec
->spdif_mutex
);
4473 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
4476 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4478 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
4479 struct hda_multi_out
*mout
)
4481 mutex_lock(&codec
->spdif_mutex
);
4482 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4483 mutex_unlock(&codec
->spdif_mutex
);
4486 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
4489 * snd_hda_multi_out_dig_close - release the digital out stream
4491 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
4492 struct hda_multi_out
*mout
)
4494 mutex_lock(&codec
->spdif_mutex
);
4495 mout
->dig_out_used
= 0;
4496 mutex_unlock(&codec
->spdif_mutex
);
4499 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
4502 * snd_hda_multi_out_analog_open - open analog outputs
4504 * Open analog outputs and set up the hw-constraints.
4505 * If the digital outputs can be opened as slave, open the digital
4508 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
4509 struct hda_multi_out
*mout
,
4510 struct snd_pcm_substream
*substream
,
4511 struct hda_pcm_stream
*hinfo
)
4513 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
4514 runtime
->hw
.channels_max
= mout
->max_channels
;
4515 if (mout
->dig_out_nid
) {
4516 if (!mout
->analog_rates
) {
4517 mout
->analog_rates
= hinfo
->rates
;
4518 mout
->analog_formats
= hinfo
->formats
;
4519 mout
->analog_maxbps
= hinfo
->maxbps
;
4521 runtime
->hw
.rates
= mout
->analog_rates
;
4522 runtime
->hw
.formats
= mout
->analog_formats
;
4523 hinfo
->maxbps
= mout
->analog_maxbps
;
4525 if (!mout
->spdif_rates
) {
4526 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
4528 &mout
->spdif_formats
,
4529 &mout
->spdif_maxbps
);
4531 mutex_lock(&codec
->spdif_mutex
);
4532 if (mout
->share_spdif
) {
4533 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
4534 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
4535 runtime
->hw
.rates
&= mout
->spdif_rates
;
4536 runtime
->hw
.formats
&= mout
->spdif_formats
;
4537 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
4538 hinfo
->maxbps
= mout
->spdif_maxbps
;
4540 mout
->share_spdif
= 0;
4541 /* FIXME: need notify? */
4544 mutex_unlock(&codec
->spdif_mutex
);
4546 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
4547 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
4549 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
4552 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4554 * Set up the i/o for analog out.
4555 * When the digital out is available, copy the front out to digital out, too.
4557 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
4558 struct hda_multi_out
*mout
,
4559 unsigned int stream_tag
,
4560 unsigned int format
,
4561 struct snd_pcm_substream
*substream
)
4563 const hda_nid_t
*nids
= mout
->dac_nids
;
4564 int chs
= substream
->runtime
->channels
;
4565 struct hda_spdif_out
*spdif
=
4566 snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
4569 mutex_lock(&codec
->spdif_mutex
);
4570 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
4571 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
4573 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
4575 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
4576 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
4577 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
4578 stream_tag
, format
);
4580 mout
->dig_out_used
= 0;
4581 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4584 mutex_unlock(&codec
->spdif_mutex
);
4587 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
4589 if (!mout
->no_share_stream
&&
4590 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
4591 /* headphone out will just decode front left/right (stereo) */
4592 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
4594 /* extra outputs copied from front */
4595 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
4596 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
4597 snd_hda_codec_setup_stream(codec
,
4598 mout
->hp_out_nid
[i
],
4599 stream_tag
, 0, format
);
4600 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4601 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
4602 snd_hda_codec_setup_stream(codec
,
4603 mout
->extra_out_nid
[i
],
4604 stream_tag
, 0, format
);
4607 for (i
= 1; i
< mout
->num_dacs
; i
++) {
4608 if (chs
>= (i
+ 1) * 2) /* independent out */
4609 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4611 else if (!mout
->no_share_stream
) /* copy front */
4612 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4617 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
4620 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4622 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
4623 struct hda_multi_out
*mout
)
4625 const hda_nid_t
*nids
= mout
->dac_nids
;
4628 for (i
= 0; i
< mout
->num_dacs
; i
++)
4629 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
4631 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
4632 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
4633 if (mout
->hp_out_nid
[i
])
4634 snd_hda_codec_cleanup_stream(codec
,
4635 mout
->hp_out_nid
[i
]);
4636 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4637 if (mout
->extra_out_nid
[i
])
4638 snd_hda_codec_cleanup_stream(codec
,
4639 mout
->extra_out_nid
[i
]);
4640 mutex_lock(&codec
->spdif_mutex
);
4641 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
4642 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4643 mout
->dig_out_used
= 0;
4645 mutex_unlock(&codec
->spdif_mutex
);
4648 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
4651 * Helper for automatic pin configuration
4654 static int is_in_nid_list(hda_nid_t nid
, const hda_nid_t
*list
)
4656 for (; *list
; list
++)
4664 * Sort an associated group of pins according to their sequence numbers.
4666 static void sort_pins_by_sequence(hda_nid_t
*pins
, short *sequences
,
4673 for (i
= 0; i
< num_pins
; i
++) {
4674 for (j
= i
+ 1; j
< num_pins
; j
++) {
4675 if (sequences
[i
] > sequences
[j
]) {
4677 sequences
[i
] = sequences
[j
];
4688 /* add the found input-pin to the cfg->inputs[] table */
4689 static void add_auto_cfg_input_pin(struct auto_pin_cfg
*cfg
, hda_nid_t nid
,
4692 if (cfg
->num_inputs
< AUTO_CFG_MAX_INS
) {
4693 cfg
->inputs
[cfg
->num_inputs
].pin
= nid
;
4694 cfg
->inputs
[cfg
->num_inputs
].type
= type
;
4699 /* sort inputs in the order of AUTO_PIN_* type */
4700 static void sort_autocfg_input_pins(struct auto_pin_cfg
*cfg
)
4704 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4705 for (j
= i
+ 1; j
< cfg
->num_inputs
; j
++) {
4706 if (cfg
->inputs
[i
].type
> cfg
->inputs
[j
].type
) {
4707 struct auto_pin_cfg_item tmp
;
4708 tmp
= cfg
->inputs
[i
];
4709 cfg
->inputs
[i
] = cfg
->inputs
[j
];
4710 cfg
->inputs
[j
] = tmp
;
4716 /* Reorder the surround channels
4717 * ALSA sequence is front/surr/clfe/side
4719 * 4-ch: front/surr => OK as it is
4720 * 6-ch: front/clfe/surr
4721 * 8-ch: front/clfe/rear/side|fc
4723 static void reorder_outputs(unsigned int nums
, hda_nid_t
*pins
)
4738 * Parse all pin widgets and store the useful pin nids to cfg
4740 * The number of line-outs or any primary output is stored in line_outs,
4741 * and the corresponding output pins are assigned to line_out_pins[],
4742 * in the order of front, rear, CLFE, side, ...
4744 * If more extra outputs (speaker and headphone) are found, the pins are
4745 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4746 * is detected, one of speaker of HP pins is assigned as the primary
4747 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4748 * if any analog output exists.
4750 * The analog input pins are assigned to inputs array.
4751 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4754 int snd_hda_parse_pin_defcfg(struct hda_codec
*codec
,
4755 struct auto_pin_cfg
*cfg
,
4756 const hda_nid_t
*ignore_nids
,
4757 unsigned int cond_flags
)
4759 hda_nid_t nid
, end_nid
;
4760 short seq
, assoc_line_out
;
4761 short sequences_line_out
[ARRAY_SIZE(cfg
->line_out_pins
)];
4762 short sequences_speaker
[ARRAY_SIZE(cfg
->speaker_pins
)];
4763 short sequences_hp
[ARRAY_SIZE(cfg
->hp_pins
)];
4766 memset(cfg
, 0, sizeof(*cfg
));
4768 memset(sequences_line_out
, 0, sizeof(sequences_line_out
));
4769 memset(sequences_speaker
, 0, sizeof(sequences_speaker
));
4770 memset(sequences_hp
, 0, sizeof(sequences_hp
));
4773 codec
->ignore_misc_bit
= true;
4774 end_nid
= codec
->start_nid
+ codec
->num_nodes
;
4775 for (nid
= codec
->start_nid
; nid
< end_nid
; nid
++) {
4776 unsigned int wid_caps
= get_wcaps(codec
, nid
);
4777 unsigned int wid_type
= get_wcaps_type(wid_caps
);
4778 unsigned int def_conf
;
4779 short assoc
, loc
, conn
, dev
;
4781 /* read all default configuration for pin complex */
4782 if (wid_type
!= AC_WID_PIN
)
4784 /* ignore the given nids (e.g. pc-beep returns error) */
4785 if (ignore_nids
&& is_in_nid_list(nid
, ignore_nids
))
4788 def_conf
= snd_hda_codec_get_pincfg(codec
, nid
);
4789 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec
, nid
)) &
4790 AC_DEFCFG_MISC_NO_PRESENCE
))
4791 codec
->ignore_misc_bit
= false;
4792 conn
= get_defcfg_connect(def_conf
);
4793 if (conn
== AC_JACK_PORT_NONE
)
4795 loc
= get_defcfg_location(def_conf
);
4796 dev
= get_defcfg_device(def_conf
);
4798 /* workaround for buggy BIOS setups */
4799 if (dev
== AC_JACK_LINE_OUT
) {
4800 if (conn
== AC_JACK_PORT_FIXED
)
4801 dev
= AC_JACK_SPEAKER
;
4805 case AC_JACK_LINE_OUT
:
4806 seq
= get_defcfg_sequence(def_conf
);
4807 assoc
= get_defcfg_association(def_conf
);
4809 if (!(wid_caps
& AC_WCAP_STEREO
))
4810 if (!cfg
->mono_out_pin
)
4811 cfg
->mono_out_pin
= nid
;
4814 if (!assoc_line_out
)
4815 assoc_line_out
= assoc
;
4816 else if (assoc_line_out
!= assoc
)
4818 if (cfg
->line_outs
>= ARRAY_SIZE(cfg
->line_out_pins
))
4820 cfg
->line_out_pins
[cfg
->line_outs
] = nid
;
4821 sequences_line_out
[cfg
->line_outs
] = seq
;
4824 case AC_JACK_SPEAKER
:
4825 seq
= get_defcfg_sequence(def_conf
);
4826 assoc
= get_defcfg_association(def_conf
);
4827 if (cfg
->speaker_outs
>= ARRAY_SIZE(cfg
->speaker_pins
))
4829 cfg
->speaker_pins
[cfg
->speaker_outs
] = nid
;
4830 sequences_speaker
[cfg
->speaker_outs
] = (assoc
<< 4) | seq
;
4831 cfg
->speaker_outs
++;
4833 case AC_JACK_HP_OUT
:
4834 seq
= get_defcfg_sequence(def_conf
);
4835 assoc
= get_defcfg_association(def_conf
);
4836 if (cfg
->hp_outs
>= ARRAY_SIZE(cfg
->hp_pins
))
4838 cfg
->hp_pins
[cfg
->hp_outs
] = nid
;
4839 sequences_hp
[cfg
->hp_outs
] = (assoc
<< 4) | seq
;
4842 case AC_JACK_MIC_IN
:
4843 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_MIC
);
4845 case AC_JACK_LINE_IN
:
4846 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_LINE_IN
);
4849 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_CD
);
4852 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_AUX
);
4854 case AC_JACK_SPDIF_OUT
:
4855 case AC_JACK_DIG_OTHER_OUT
:
4856 if (cfg
->dig_outs
>= ARRAY_SIZE(cfg
->dig_out_pins
))
4858 cfg
->dig_out_pins
[cfg
->dig_outs
] = nid
;
4859 cfg
->dig_out_type
[cfg
->dig_outs
] =
4860 (loc
== AC_JACK_LOC_HDMI
) ?
4861 HDA_PCM_TYPE_HDMI
: HDA_PCM_TYPE_SPDIF
;
4864 case AC_JACK_SPDIF_IN
:
4865 case AC_JACK_DIG_OTHER_IN
:
4866 cfg
->dig_in_pin
= nid
;
4867 if (loc
== AC_JACK_LOC_HDMI
)
4868 cfg
->dig_in_type
= HDA_PCM_TYPE_HDMI
;
4870 cfg
->dig_in_type
= HDA_PCM_TYPE_SPDIF
;
4876 * If no line-out is defined but multiple HPs are found,
4877 * some of them might be the real line-outs.
4879 if (!cfg
->line_outs
&& cfg
->hp_outs
> 1 &&
4880 !(cond_flags
& HDA_PINCFG_NO_HP_FIXUP
)) {
4882 while (i
< cfg
->hp_outs
) {
4883 /* The real HPs should have the sequence 0x0f */
4884 if ((sequences_hp
[i
] & 0x0f) == 0x0f) {
4888 /* Move it to the line-out table */
4889 cfg
->line_out_pins
[cfg
->line_outs
] = cfg
->hp_pins
[i
];
4890 sequences_line_out
[cfg
->line_outs
] = sequences_hp
[i
];
4893 memmove(cfg
->hp_pins
+ i
, cfg
->hp_pins
+ i
+ 1,
4894 sizeof(cfg
->hp_pins
[0]) * (cfg
->hp_outs
- i
));
4895 memmove(sequences_hp
+ i
, sequences_hp
+ i
+ 1,
4896 sizeof(sequences_hp
[0]) * (cfg
->hp_outs
- i
));
4898 memset(cfg
->hp_pins
+ cfg
->hp_outs
, 0,
4899 sizeof(hda_nid_t
) * (AUTO_CFG_MAX_OUTS
- cfg
->hp_outs
));
4901 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4905 /* sort by sequence */
4906 sort_pins_by_sequence(cfg
->line_out_pins
, sequences_line_out
,
4908 sort_pins_by_sequence(cfg
->speaker_pins
, sequences_speaker
,
4910 sort_pins_by_sequence(cfg
->hp_pins
, sequences_hp
,
4914 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4915 * as a primary output
4917 if (!cfg
->line_outs
&&
4918 !(cond_flags
& HDA_PINCFG_NO_LO_FIXUP
)) {
4919 if (cfg
->speaker_outs
) {
4920 cfg
->line_outs
= cfg
->speaker_outs
;
4921 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
4922 sizeof(cfg
->speaker_pins
));
4923 cfg
->speaker_outs
= 0;
4924 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
4925 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
4926 } else if (cfg
->hp_outs
) {
4927 cfg
->line_outs
= cfg
->hp_outs
;
4928 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
4929 sizeof(cfg
->hp_pins
));
4931 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
4932 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4936 reorder_outputs(cfg
->line_outs
, cfg
->line_out_pins
);
4937 reorder_outputs(cfg
->hp_outs
, cfg
->hp_pins
);
4938 reorder_outputs(cfg
->speaker_outs
, cfg
->speaker_pins
);
4940 sort_autocfg_input_pins(cfg
);
4943 * debug prints of the parsed results
4945 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
4946 cfg
->line_outs
, cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
4947 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
4948 cfg
->line_out_pins
[4],
4949 cfg
->line_out_type
== AUTO_PIN_HP_OUT
? "hp" :
4950 (cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
?
4951 "speaker" : "line"));
4952 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4953 cfg
->speaker_outs
, cfg
->speaker_pins
[0],
4954 cfg
->speaker_pins
[1], cfg
->speaker_pins
[2],
4955 cfg
->speaker_pins
[3], cfg
->speaker_pins
[4]);
4956 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4957 cfg
->hp_outs
, cfg
->hp_pins
[0],
4958 cfg
->hp_pins
[1], cfg
->hp_pins
[2],
4959 cfg
->hp_pins
[3], cfg
->hp_pins
[4]);
4960 snd_printd(" mono: mono_out=0x%x\n", cfg
->mono_out_pin
);
4962 snd_printd(" dig-out=0x%x/0x%x\n",
4963 cfg
->dig_out_pins
[0], cfg
->dig_out_pins
[1]);
4964 snd_printd(" inputs:");
4965 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4966 snd_printd(" %s=0x%x",
4967 hda_get_autocfg_input_label(codec
, cfg
, i
),
4968 cfg
->inputs
[i
].pin
);
4971 if (cfg
->dig_in_pin
)
4972 snd_printd(" dig-in=0x%x\n", cfg
->dig_in_pin
);
4976 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg
);
4978 int snd_hda_get_input_pin_attr(unsigned int def_conf
)
4980 unsigned int loc
= get_defcfg_location(def_conf
);
4981 unsigned int conn
= get_defcfg_connect(def_conf
);
4982 if (conn
== AC_JACK_PORT_NONE
)
4983 return INPUT_PIN_ATTR_UNUSED
;
4984 /* Windows may claim the internal mic to be BOTH, too */
4985 if (conn
== AC_JACK_PORT_FIXED
|| conn
== AC_JACK_PORT_BOTH
)
4986 return INPUT_PIN_ATTR_INT
;
4987 if ((loc
& 0x30) == AC_JACK_LOC_INTERNAL
)
4988 return INPUT_PIN_ATTR_INT
;
4989 if ((loc
& 0x30) == AC_JACK_LOC_SEPARATE
)
4990 return INPUT_PIN_ATTR_DOCK
;
4991 if (loc
== AC_JACK_LOC_REAR
)
4992 return INPUT_PIN_ATTR_REAR
;
4993 if (loc
== AC_JACK_LOC_FRONT
)
4994 return INPUT_PIN_ATTR_FRONT
;
4995 return INPUT_PIN_ATTR_NORMAL
;
4997 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr
);
5000 * hda_get_input_pin_label - Give a label for the given input pin
5002 * When check_location is true, the function checks the pin location
5003 * for mic and line-in pins, and set an appropriate prefix like "Front",
5004 * "Rear", "Internal".
5007 const char *hda_get_input_pin_label(struct hda_codec
*codec
, hda_nid_t pin
,
5010 unsigned int def_conf
;
5011 static const char * const mic_names
[] = {
5012 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
5016 def_conf
= snd_hda_codec_get_pincfg(codec
, pin
);
5018 switch (get_defcfg_device(def_conf
)) {
5019 case AC_JACK_MIC_IN
:
5020 if (!check_location
)
5022 attr
= snd_hda_get_input_pin_attr(def_conf
);
5025 return mic_names
[attr
- 1];
5026 case AC_JACK_LINE_IN
:
5027 if (!check_location
)
5029 attr
= snd_hda_get_input_pin_attr(def_conf
);
5032 if (attr
== INPUT_PIN_ATTR_DOCK
)
5039 case AC_JACK_SPDIF_IN
:
5041 case AC_JACK_DIG_OTHER_IN
:
5042 return "Digital In";
5047 EXPORT_SYMBOL_HDA(hda_get_input_pin_label
);
5049 /* Check whether the location prefix needs to be added to the label.
5050 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5051 * have to put "Front" prefix to each label. In such a case, returns false.
5053 static int check_mic_location_need(struct hda_codec
*codec
,
5054 const struct auto_pin_cfg
*cfg
,
5060 defc
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[input
].pin
);
5061 attr
= snd_hda_get_input_pin_attr(defc
);
5062 /* for internal or docking mics, we need locations */
5063 if (attr
<= INPUT_PIN_ATTR_NORMAL
)
5067 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
5068 defc
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[i
].pin
);
5069 attr2
= snd_hda_get_input_pin_attr(defc
);
5070 if (attr2
>= INPUT_PIN_ATTR_NORMAL
) {
5071 if (attr
&& attr
!= attr2
)
5072 return 1; /* different locations found */
5080 * hda_get_autocfg_input_label - Get a label for the given input
5082 * Get a label for the given input pin defined by the autocfg item.
5083 * Unlike hda_get_input_pin_label(), this function checks all inputs
5084 * defined in autocfg and avoids the redundant mic/line prefix as much as
5087 const char *hda_get_autocfg_input_label(struct hda_codec
*codec
,
5088 const struct auto_pin_cfg
*cfg
,
5091 int type
= cfg
->inputs
[input
].type
;
5092 int has_multiple_pins
= 0;
5094 if ((input
> 0 && cfg
->inputs
[input
- 1].type
== type
) ||
5095 (input
< cfg
->num_inputs
- 1 && cfg
->inputs
[input
+ 1].type
== type
))
5096 has_multiple_pins
= 1;
5097 if (has_multiple_pins
&& type
== AUTO_PIN_MIC
)
5098 has_multiple_pins
&= check_mic_location_need(codec
, cfg
, input
);
5099 return hda_get_input_pin_label(codec
, cfg
->inputs
[input
].pin
,
5102 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label
);
5105 * snd_hda_add_imux_item - Add an item to input_mux
5107 * When the same label is used already in the existing items, the number
5108 * suffix is appended to the label. This label index number is stored
5109 * to type_idx when non-NULL pointer is given.
5111 int snd_hda_add_imux_item(struct hda_input_mux
*imux
, const char *label
,
5112 int index
, int *type_idx
)
5114 int i
, label_idx
= 0;
5115 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
5116 snd_printd(KERN_ERR
"hda_codec: Too many imux items!\n");
5119 for (i
= 0; i
< imux
->num_items
; i
++) {
5120 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
5124 *type_idx
= label_idx
;
5126 snprintf(imux
->items
[imux
->num_items
].label
,
5127 sizeof(imux
->items
[imux
->num_items
].label
),
5128 "%s %d", label
, label_idx
);
5130 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5131 sizeof(imux
->items
[imux
->num_items
].label
));
5132 imux
->items
[imux
->num_items
].index
= index
;
5136 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item
);
5145 * snd_hda_suspend - suspend the codecs
5148 * Returns 0 if successful.
5150 int snd_hda_suspend(struct hda_bus
*bus
)
5152 struct hda_codec
*codec
;
5154 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5155 if (hda_codec_is_power_on(codec
))
5156 hda_call_codec_suspend(codec
);
5157 if (codec
->patch_ops
.post_suspend
)
5158 codec
->patch_ops
.post_suspend(codec
);
5162 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
5165 * snd_hda_resume - resume the codecs
5168 * Returns 0 if successful.
5170 * This function is defined only when POWER_SAVE isn't set.
5171 * In the power-save mode, the codec is resumed dynamically.
5173 int snd_hda_resume(struct hda_bus
*bus
)
5175 struct hda_codec
*codec
;
5177 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5178 if (codec
->patch_ops
.pre_resume
)
5179 codec
->patch_ops
.pre_resume(codec
);
5180 if (snd_hda_codec_needs_resume(codec
))
5181 hda_call_codec_resume(codec
);
5185 EXPORT_SYMBOL_HDA(snd_hda_resume
);
5186 #endif /* CONFIG_PM */
5193 * snd_array_new - get a new element from the given array
5194 * @array: the array object
5196 * Get a new element from the given array. If it exceeds the
5197 * pre-allocated array size, re-allocate the array.
5199 * Returns NULL if allocation failed.
5201 void *snd_array_new(struct snd_array
*array
)
5203 if (array
->used
>= array
->alloced
) {
5204 int num
= array
->alloced
+ array
->alloc_align
;
5205 int size
= (num
+ 1) * array
->elem_size
;
5206 int oldsize
= array
->alloced
* array
->elem_size
;
5208 if (snd_BUG_ON(num
>= 4096))
5210 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
);
5213 memset(nlist
+ oldsize
, 0, size
- oldsize
);
5214 array
->list
= nlist
;
5215 array
->alloced
= num
;
5217 return snd_array_elem(array
, array
->used
++);
5219 EXPORT_SYMBOL_HDA(snd_array_new
);
5222 * snd_array_free - free the given array elements
5223 * @array: the array object
5225 void snd_array_free(struct snd_array
*array
)
5232 EXPORT_SYMBOL_HDA(snd_array_free
);
5235 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5236 * @pcm: PCM caps bits
5237 * @buf: the string buffer to write
5238 * @buflen: the max buffer length
5240 * used by hda_proc.c and hda_eld.c
5242 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5244 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5247 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5248 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5249 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5251 buf
[j
] = '\0'; /* necessary when j == 0 */
5253 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
5255 #ifdef CONFIG_SND_HDA_INPUT_JACK
5257 * Input-jack notification support
5259 struct hda_jack_item
{
5262 struct snd_jack
*jack
;
5265 static const char *get_jack_default_name(struct hda_codec
*codec
, hda_nid_t nid
,
5269 case SND_JACK_HEADPHONE
:
5271 case SND_JACK_MICROPHONE
:
5273 case SND_JACK_LINEOUT
:
5275 case SND_JACK_LINEIN
:
5277 case SND_JACK_HEADSET
:
5279 case SND_JACK_VIDEOOUT
:
5286 static void hda_free_jack_priv(struct snd_jack
*jack
)
5288 struct hda_jack_item
*jacks
= jack
->private_data
;
5293 int snd_hda_input_jack_add(struct hda_codec
*codec
, hda_nid_t nid
, int type
,
5296 struct hda_jack_item
*jack
;
5299 snd_array_init(&codec
->jacks
, sizeof(*jack
), 32);
5300 jack
= snd_array_new(&codec
->jacks
);
5307 name
= get_jack_default_name(codec
, nid
, type
);
5308 err
= snd_jack_new(codec
->bus
->card
, name
, type
, &jack
->jack
);
5313 jack
->jack
->private_data
= jack
;
5314 jack
->jack
->private_free
= hda_free_jack_priv
;
5317 EXPORT_SYMBOL_HDA(snd_hda_input_jack_add
);
5319 void snd_hda_input_jack_report(struct hda_codec
*codec
, hda_nid_t nid
)
5321 struct hda_jack_item
*jacks
= codec
->jacks
.list
;
5327 for (i
= 0; i
< codec
->jacks
.used
; i
++, jacks
++) {
5328 unsigned int pin_ctl
;
5329 unsigned int present
;
5332 if (jacks
->nid
!= nid
)
5334 present
= snd_hda_jack_detect(codec
, nid
);
5336 if (type
== (SND_JACK_HEADPHONE
| SND_JACK_LINEOUT
)) {
5337 pin_ctl
= snd_hda_codec_read(codec
, nid
, 0,
5338 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5339 type
= (pin_ctl
& AC_PINCTL_HP_EN
) ?
5340 SND_JACK_HEADPHONE
: SND_JACK_LINEOUT
;
5342 snd_jack_report(jacks
->jack
, present
? type
: 0);
5345 EXPORT_SYMBOL_HDA(snd_hda_input_jack_report
);
5347 /* free jack instances manually when clearing/reconfiguring */
5348 void snd_hda_input_jack_free(struct hda_codec
*codec
)
5350 if (!codec
->bus
->shutdown
&& codec
->jacks
.list
) {
5351 struct hda_jack_item
*jacks
= codec
->jacks
.list
;
5353 for (i
= 0; i
< codec
->jacks
.used
; i
++, jacks
++) {
5355 snd_device_free(codec
->bus
->card
, jacks
->jack
);
5358 snd_array_free(&codec
->jacks
);
5360 EXPORT_SYMBOL_HDA(snd_hda_input_jack_free
);
5361 #endif /* CONFIG_SND_HDA_INPUT_JACK */
5363 MODULE_DESCRIPTION("HDA codec core");
5364 MODULE_LICENSE("GPL");