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 <sound/core.h>
28 #include "hda_codec.h"
29 #include <sound/asoundef.h>
30 #include <sound/tlv.h>
31 #include <sound/initval.h>
32 #include <sound/jack.h>
33 #include "hda_local.h"
35 #include <sound/hda_hwdep.h>
38 * vendor / preset table
41 struct hda_vendor_id
{
46 /* codec vendor labels */
47 static struct hda_vendor_id hda_vendor_ids
[] = {
49 { 0x1013, "Cirrus Logic" },
50 { 0x1057, "Motorola" },
51 { 0x1095, "Silicon Image" },
53 { 0x10ec, "Realtek" },
54 { 0x1102, "Creative" },
58 { 0x11d4, "Analog Devices" },
59 { 0x13f6, "C-Media" },
60 { 0x14f1, "Conexant" },
61 { 0x17e8, "Chrontel" },
63 { 0x1aec, "Wolfson Microelectronics" },
64 { 0x434d, "C-Media" },
66 { 0x8384, "SigmaTel" },
70 static DEFINE_MUTEX(preset_mutex
);
71 static LIST_HEAD(hda_preset_tables
);
73 int snd_hda_add_codec_preset(struct hda_codec_preset_list
*preset
)
75 mutex_lock(&preset_mutex
);
76 list_add_tail(&preset
->list
, &hda_preset_tables
);
77 mutex_unlock(&preset_mutex
);
80 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset
);
82 int snd_hda_delete_codec_preset(struct hda_codec_preset_list
*preset
)
84 mutex_lock(&preset_mutex
);
85 list_del(&preset
->list
);
86 mutex_unlock(&preset_mutex
);
89 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset
);
91 #ifdef CONFIG_SND_HDA_POWER_SAVE
92 static void hda_power_work(struct work_struct
*work
);
93 static void hda_keep_power_on(struct hda_codec
*codec
);
94 #define hda_codec_is_power_on(codec) ((codec)->power_on)
96 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
97 #define hda_codec_is_power_on(codec) 1
101 * snd_hda_get_jack_location - Give a location string of the jack
102 * @cfg: pin default config value
104 * Parse the pin default config value and returns the string of the
105 * jack location, e.g. "Rear", "Front", etc.
107 const char *snd_hda_get_jack_location(u32 cfg
)
109 static char *bases
[7] = {
110 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
112 static unsigned char specials_idx
[] = {
117 static char *specials
[] = {
118 "Rear Panel", "Drive Bar",
119 "Riser", "HDMI", "ATAPI",
120 "Mobile-In", "Mobile-Out"
123 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
124 if ((cfg
& 0x0f) < 7)
125 return bases
[cfg
& 0x0f];
126 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
127 if (cfg
== specials_idx
[i
])
132 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location
);
135 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
136 * @cfg: pin default config value
138 * Parse the pin default config value and returns the string of the
139 * jack connectivity, i.e. external or internal connection.
141 const char *snd_hda_get_jack_connectivity(u32 cfg
)
143 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
145 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
147 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity
);
150 * snd_hda_get_jack_type - Give a type string of the jack
151 * @cfg: pin default config value
153 * Parse the pin default config value and returns the string of the
154 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
156 const char *snd_hda_get_jack_type(u32 cfg
)
158 static char *jack_types
[16] = {
159 "Line Out", "Speaker", "HP Out", "CD",
160 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
161 "Line In", "Aux", "Mic", "Telephony",
162 "SPDIF In", "Digitial In", "Reserved", "Other"
165 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
166 >> AC_DEFCFG_DEVICE_SHIFT
];
168 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type
);
171 * Compose a 32bit command word to be sent to the HD-audio controller
173 static inline unsigned int
174 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
175 unsigned int verb
, unsigned int parm
)
179 if ((codec
->addr
& ~0xf) || (direct
& ~1) || (nid
& ~0x7f) ||
180 (verb
& ~0xfff) || (parm
& ~0xffff)) {
181 printk(KERN_ERR
"hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
182 codec
->addr
, direct
, nid
, verb
, parm
);
186 val
= (u32
)codec
->addr
<< 28;
187 val
|= (u32
)direct
<< 27;
188 val
|= (u32
)nid
<< 20;
195 * Send and receive a verb
197 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
200 struct hda_bus
*bus
= codec
->bus
;
209 snd_hda_power_up(codec
);
210 mutex_lock(&bus
->cmd_mutex
);
211 err
= bus
->ops
.command(bus
, cmd
);
213 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
214 mutex_unlock(&bus
->cmd_mutex
);
215 snd_hda_power_down(codec
);
216 if (res
&& *res
== -1 && bus
->rirb_error
) {
217 if (bus
->response_reset
) {
218 snd_printd("hda_codec: resetting BUS due to "
219 "fatal communication error\n");
220 bus
->ops
.bus_reset(bus
);
224 /* clear reset-flag when the communication gets recovered */
226 bus
->response_reset
= 0;
231 * snd_hda_codec_read - send a command and get the response
232 * @codec: the HDA codec
233 * @nid: NID to send the command
234 * @direct: direct flag
235 * @verb: the verb to send
236 * @parm: the parameter for the verb
238 * Send a single command and read the corresponding response.
240 * Returns the obtained response value, or -1 for an error.
242 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
244 unsigned int verb
, unsigned int parm
)
246 unsigned cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
248 if (codec_exec_verb(codec
, cmd
, &res
))
252 EXPORT_SYMBOL_HDA(snd_hda_codec_read
);
255 * snd_hda_codec_write - send a single command without waiting for response
256 * @codec: the HDA codec
257 * @nid: NID to send the command
258 * @direct: direct flag
259 * @verb: the verb to send
260 * @parm: the parameter for the verb
262 * Send a single command without waiting for response.
264 * Returns 0 if successful, or a negative error code.
266 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
267 unsigned int verb
, unsigned int parm
)
269 unsigned int cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
271 return codec_exec_verb(codec
, cmd
,
272 codec
->bus
->sync_write
? &res
: NULL
);
274 EXPORT_SYMBOL_HDA(snd_hda_codec_write
);
277 * snd_hda_sequence_write - sequence writes
278 * @codec: the HDA codec
279 * @seq: VERB array to send
281 * Send the commands sequentially from the given array.
282 * The array must be terminated with NID=0.
284 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
286 for (; seq
->nid
; seq
++)
287 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
289 EXPORT_SYMBOL_HDA(snd_hda_sequence_write
);
292 * snd_hda_get_sub_nodes - get the range of sub nodes
293 * @codec: the HDA codec
295 * @start_id: the pointer to store the start NID
297 * Parse the NID and store the start NID of its sub-nodes.
298 * Returns the number of sub-nodes.
300 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
305 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
308 *start_id
= (parm
>> 16) & 0x7fff;
309 return (int)(parm
& 0x7fff);
311 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
313 /* look up the cached results */
314 static hda_nid_t
*lookup_conn_list(struct snd_array
*array
, hda_nid_t nid
)
317 for (i
= 0; i
< array
->used
; ) {
318 hda_nid_t
*p
= snd_array_elem(array
, i
);
328 * snd_hda_get_conn_list - get connection list
329 * @codec: the HDA codec
331 * @listp: the pointer to store NID list
333 * Parses the connection list of the given widget and stores the list
336 * Returns the number of connections, or a negative error code.
338 int snd_hda_get_conn_list(struct hda_codec
*codec
, hda_nid_t nid
,
339 const hda_nid_t
**listp
)
341 struct snd_array
*array
= &codec
->conn_lists
;
343 hda_nid_t list
[HDA_MAX_CONNECTIONS
];
348 /* if the connection-list is already cached, read it */
349 p
= lookup_conn_list(array
, nid
);
355 if (snd_BUG_ON(added
))
358 /* read the connection and add to the cache */
359 len
= snd_hda_get_raw_connections(codec
, nid
, list
, HDA_MAX_CONNECTIONS
);
362 err
= snd_hda_override_conn_list(codec
, nid
, len
, list
);
368 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list
);
371 * snd_hda_get_connections - copy connection list
372 * @codec: the HDA codec
374 * @conn_list: connection list array
375 * @max_conns: max. number of connections to store
377 * Parses the connection list of the given widget and stores the list
380 * Returns the number of connections, or a negative error code.
382 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
383 hda_nid_t
*conn_list
, int max_conns
)
385 const hda_nid_t
*list
;
386 int len
= snd_hda_get_conn_list(codec
, nid
, &list
);
390 if (len
> max_conns
) {
391 snd_printk(KERN_ERR
"hda_codec: "
392 "Too many connections %d for NID 0x%x\n",
396 memcpy(conn_list
, list
, len
* sizeof(hda_nid_t
));
399 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
402 * snd_hda_get_raw_connections - copy connection list without cache
403 * @codec: the HDA codec
405 * @conn_list: connection list array
406 * @max_conns: max. number of connections to store
408 * Like snd_hda_get_connections(), copy the connection list but without
409 * checking through the connection-list cache.
410 * Currently called only from hda_proc.c, so not exported.
412 int snd_hda_get_raw_connections(struct hda_codec
*codec
, hda_nid_t nid
,
413 hda_nid_t
*conn_list
, int max_conns
)
416 int i
, conn_len
, conns
;
417 unsigned int shift
, num_elems
, mask
;
421 if (snd_BUG_ON(!conn_list
|| max_conns
<= 0))
424 wcaps
= get_wcaps(codec
, nid
);
425 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
426 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
429 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
430 if (parm
& AC_CLIST_LONG
) {
439 conn_len
= parm
& AC_CLIST_LENGTH
;
440 mask
= (1 << (shift
-1)) - 1;
443 return 0; /* no connection */
446 /* single connection */
447 parm
= snd_hda_codec_read(codec
, nid
, 0,
448 AC_VERB_GET_CONNECT_LIST
, 0);
449 if (parm
== -1 && codec
->bus
->rirb_error
)
451 conn_list
[0] = parm
& mask
;
455 /* multi connection */
458 for (i
= 0; i
< conn_len
; i
++) {
462 if (i
% num_elems
== 0) {
463 parm
= snd_hda_codec_read(codec
, nid
, 0,
464 AC_VERB_GET_CONNECT_LIST
, i
);
465 if (parm
== -1 && codec
->bus
->rirb_error
)
468 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
471 snd_printk(KERN_WARNING
"hda_codec: "
472 "invalid CONNECT_LIST verb %x[%i]:%x\n",
478 /* ranges between the previous and this one */
479 if (!prev_nid
|| prev_nid
>= val
) {
480 snd_printk(KERN_WARNING
"hda_codec: "
481 "invalid dep_range_val %x:%x\n",
485 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
486 if (conns
>= max_conns
) {
487 snd_printk(KERN_ERR
"hda_codec: "
488 "Too many connections %d for NID 0x%x\n",
492 conn_list
[conns
++] = n
;
495 if (conns
>= max_conns
) {
496 snd_printk(KERN_ERR
"hda_codec: "
497 "Too many connections %d for NID 0x%x\n",
501 conn_list
[conns
++] = val
;
508 static bool add_conn_list(struct snd_array
*array
, hda_nid_t nid
)
510 hda_nid_t
*p
= snd_array_new(array
);
518 * snd_hda_override_conn_list - add/modify the connection-list to cache
519 * @codec: the HDA codec
521 * @len: number of connection list entries
522 * @list: the list of connection entries
524 * Add or modify the given connection-list to the cache. If the corresponding
525 * cache already exists, invalidate it and append a new one.
527 * Returns zero or a negative error code.
529 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
530 const hda_nid_t
*list
)
532 struct snd_array
*array
= &codec
->conn_lists
;
536 p
= lookup_conn_list(array
, nid
);
538 *p
= -1; /* invalidate the old entry */
540 old_used
= array
->used
;
541 if (!add_conn_list(array
, nid
) || !add_conn_list(array
, len
))
543 for (i
= 0; i
< len
; i
++)
544 if (!add_conn_list(array
, list
[i
]))
549 array
->used
= old_used
;
552 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list
);
555 * snd_hda_get_conn_index - get the connection index of the given NID
556 * @codec: the HDA codec
557 * @mux: NID containing the list
558 * @nid: NID to select
559 * @recursive: 1 when searching NID recursively, otherwise 0
561 * Parses the connection list of the widget @mux and checks whether the
562 * widget @nid is present. If it is, return the connection index.
563 * Otherwise it returns -1.
565 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
566 hda_nid_t nid
, int recursive
)
568 hda_nid_t conn
[HDA_MAX_NUM_INPUTS
];
571 nums
= snd_hda_get_connections(codec
, mux
, conn
, ARRAY_SIZE(conn
));
572 for (i
= 0; i
< nums
; i
++)
578 snd_printd("hda_codec: too deep connection for 0x%x\n", nid
);
582 for (i
= 0; i
< nums
; i
++)
583 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
587 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index
);
590 * snd_hda_queue_unsol_event - add an unsolicited event to queue
592 * @res: unsolicited event (lower 32bit of RIRB entry)
593 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
595 * Adds the given event to the queue. The events are processed in
596 * the workqueue asynchronously. Call this function in the interrupt
597 * hanlder when RIRB receives an unsolicited event.
599 * Returns 0 if successful, or a negative error code.
601 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
603 struct hda_bus_unsolicited
*unsol
;
610 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
614 unsol
->queue
[wp
] = res
;
615 unsol
->queue
[wp
+ 1] = res_ex
;
617 queue_work(bus
->workq
, &unsol
->work
);
621 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event
);
624 * process queued unsolicited events
626 static void process_unsol_events(struct work_struct
*work
)
628 struct hda_bus_unsolicited
*unsol
=
629 container_of(work
, struct hda_bus_unsolicited
, work
);
630 struct hda_bus
*bus
= unsol
->bus
;
631 struct hda_codec
*codec
;
632 unsigned int rp
, caddr
, res
;
634 while (unsol
->rp
!= unsol
->wp
) {
635 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
638 res
= unsol
->queue
[rp
];
639 caddr
= unsol
->queue
[rp
+ 1];
640 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
642 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
643 if (codec
&& codec
->patch_ops
.unsol_event
)
644 codec
->patch_ops
.unsol_event(codec
, res
);
649 * initialize unsolicited queue
651 static int init_unsol_queue(struct hda_bus
*bus
)
653 struct hda_bus_unsolicited
*unsol
;
655 if (bus
->unsol
) /* already initialized */
658 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
660 snd_printk(KERN_ERR
"hda_codec: "
661 "can't allocate unsolicited queue\n");
664 INIT_WORK(&unsol
->work
, process_unsol_events
);
673 static void snd_hda_codec_free(struct hda_codec
*codec
);
675 static int snd_hda_bus_free(struct hda_bus
*bus
)
677 struct hda_codec
*codec
, *n
;
682 flush_workqueue(bus
->workq
);
685 list_for_each_entry_safe(codec
, n
, &bus
->codec_list
, list
) {
686 snd_hda_codec_free(codec
);
688 if (bus
->ops
.private_free
)
689 bus
->ops
.private_free(bus
);
691 destroy_workqueue(bus
->workq
);
696 static int snd_hda_bus_dev_free(struct snd_device
*device
)
698 struct hda_bus
*bus
= device
->device_data
;
700 return snd_hda_bus_free(bus
);
703 #ifdef CONFIG_SND_HDA_HWDEP
704 static int snd_hda_bus_dev_register(struct snd_device
*device
)
706 struct hda_bus
*bus
= device
->device_data
;
707 struct hda_codec
*codec
;
708 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
709 snd_hda_hwdep_add_sysfs(codec
);
710 snd_hda_hwdep_add_power_sysfs(codec
);
715 #define snd_hda_bus_dev_register NULL
719 * snd_hda_bus_new - create a HDA bus
720 * @card: the card entry
721 * @temp: the template for hda_bus information
722 * @busp: the pointer to store the created bus instance
724 * Returns 0 if successful, or a negative error code.
726 int /*__devinit*/ snd_hda_bus_new(struct snd_card
*card
,
727 const struct hda_bus_template
*temp
,
728 struct hda_bus
**busp
)
732 static struct snd_device_ops dev_ops
= {
733 .dev_register
= snd_hda_bus_dev_register
,
734 .dev_free
= snd_hda_bus_dev_free
,
737 if (snd_BUG_ON(!temp
))
739 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
745 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
747 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
752 bus
->private_data
= temp
->private_data
;
753 bus
->pci
= temp
->pci
;
754 bus
->modelname
= temp
->modelname
;
755 bus
->power_save
= temp
->power_save
;
756 bus
->ops
= temp
->ops
;
758 mutex_init(&bus
->cmd_mutex
);
759 mutex_init(&bus
->prepare_mutex
);
760 INIT_LIST_HEAD(&bus
->codec_list
);
762 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
763 "hd-audio%d", card
->number
);
764 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
766 snd_printk(KERN_ERR
"cannot create workqueue %s\n",
772 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
774 snd_hda_bus_free(bus
);
781 EXPORT_SYMBOL_HDA(snd_hda_bus_new
);
783 #ifdef CONFIG_SND_HDA_GENERIC
784 #define is_generic_config(codec) \
785 (codec->modelname && !strcmp(codec->modelname, "generic"))
787 #define is_generic_config(codec) 0
791 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
793 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
797 * find a matching codec preset
799 static const struct hda_codec_preset
*
800 find_codec_preset(struct hda_codec
*codec
)
802 struct hda_codec_preset_list
*tbl
;
803 const struct hda_codec_preset
*preset
;
804 int mod_requested
= 0;
806 if (is_generic_config(codec
))
807 return NULL
; /* use the generic parser */
810 mutex_lock(&preset_mutex
);
811 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
812 if (!try_module_get(tbl
->owner
)) {
813 snd_printk(KERN_ERR
"hda_codec: cannot module_get\n");
816 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
817 u32 mask
= preset
->mask
;
818 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
820 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
824 if (preset
->id
== (codec
->vendor_id
& mask
) &&
826 preset
->rev
== codec
->revision_id
)) {
827 mutex_unlock(&preset_mutex
);
828 codec
->owner
= tbl
->owner
;
832 module_put(tbl
->owner
);
834 mutex_unlock(&preset_mutex
);
836 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
839 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%08x",
842 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%04x*",
843 (codec
->vendor_id
>> 16) & 0xffff);
844 request_module(name
);
852 * get_codec_name - store the codec name
854 static int get_codec_name(struct hda_codec
*codec
)
856 const struct hda_vendor_id
*c
;
857 const char *vendor
= NULL
;
858 u16 vendor_id
= codec
->vendor_id
>> 16;
861 if (codec
->vendor_name
)
864 for (c
= hda_vendor_ids
; c
->id
; c
++) {
865 if (c
->id
== vendor_id
) {
871 sprintf(tmp
, "Generic %04x", vendor_id
);
874 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
875 if (!codec
->vendor_name
)
879 if (codec
->chip_name
)
882 if (codec
->preset
&& codec
->preset
->name
)
883 codec
->chip_name
= kstrdup(codec
->preset
->name
, GFP_KERNEL
);
885 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
886 codec
->chip_name
= kstrdup(tmp
, GFP_KERNEL
);
888 if (!codec
->chip_name
)
894 * look for an AFG and MFG nodes
896 static void /*__devinit*/ setup_fg_nodes(struct hda_codec
*codec
)
898 int i
, total_nodes
, function_id
;
901 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
902 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
903 function_id
= snd_hda_param_read(codec
, nid
,
904 AC_PAR_FUNCTION_TYPE
);
905 switch (function_id
& 0xff) {
906 case AC_GRP_AUDIO_FUNCTION
:
908 codec
->afg_function_id
= function_id
& 0xff;
909 codec
->afg_unsol
= (function_id
>> 8) & 1;
911 case AC_GRP_MODEM_FUNCTION
:
913 codec
->mfg_function_id
= function_id
& 0xff;
914 codec
->mfg_unsol
= (function_id
>> 8) & 1;
923 * read widget caps for each widget and store in cache
925 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
930 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
932 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
935 nid
= codec
->start_nid
;
936 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
937 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
938 AC_PAR_AUDIO_WIDGET_CAP
);
942 /* read all pin default configurations and save codec->init_pins */
943 static int read_pin_defaults(struct hda_codec
*codec
)
946 hda_nid_t nid
= codec
->start_nid
;
948 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
949 struct hda_pincfg
*pin
;
950 unsigned int wcaps
= get_wcaps(codec
, nid
);
951 unsigned int wid_type
= get_wcaps_type(wcaps
);
952 if (wid_type
!= AC_WID_PIN
)
954 pin
= snd_array_new(&codec
->init_pins
);
958 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
959 AC_VERB_GET_CONFIG_DEFAULT
, 0);
960 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
961 AC_VERB_GET_PIN_WIDGET_CONTROL
,
967 /* look up the given pin config list and return the item matching with NID */
968 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
969 struct snd_array
*array
,
973 for (i
= 0; i
< array
->used
; i
++) {
974 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
981 /* write a config value for the given NID */
982 static void set_pincfg(struct hda_codec
*codec
, hda_nid_t nid
,
986 for (i
= 0; i
< 4; i
++) {
987 snd_hda_codec_write(codec
, nid
, 0,
988 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0
+ i
,
994 /* set the current pin config value for the given NID.
995 * the value is cached, and read via snd_hda_codec_get_pincfg()
997 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
998 hda_nid_t nid
, unsigned int cfg
)
1000 struct hda_pincfg
*pin
;
1001 unsigned int oldcfg
;
1003 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_PIN
)
1006 oldcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1007 pin
= look_up_pincfg(codec
, list
, nid
);
1009 pin
= snd_array_new(list
);
1016 /* change only when needed; e.g. if the pincfg is already present
1017 * in user_pins[], don't write it
1019 cfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1021 set_pincfg(codec
, nid
, cfg
);
1026 * snd_hda_codec_set_pincfg - Override a pin default configuration
1027 * @codec: the HDA codec
1028 * @nid: NID to set the pin config
1029 * @cfg: the pin default config value
1031 * Override a pin default configuration value in the cache.
1032 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1033 * priority than the real hardware value.
1035 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
1036 hda_nid_t nid
, unsigned int cfg
)
1038 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
1040 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg
);
1043 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1044 * @codec: the HDA codec
1045 * @nid: NID to get the pin config
1047 * Get the current pin config value of the given pin NID.
1048 * If the pincfg value is cached or overridden via sysfs or driver,
1049 * returns the cached value.
1051 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
1053 struct hda_pincfg
*pin
;
1055 #ifdef CONFIG_SND_HDA_HWDEP
1056 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
1060 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
1063 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1068 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg
);
1070 /* restore all current pin configs */
1071 static void restore_pincfgs(struct hda_codec
*codec
)
1074 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1075 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1076 set_pincfg(codec
, pin
->nid
,
1077 snd_hda_codec_get_pincfg(codec
, pin
->nid
));
1082 * snd_hda_shutup_pins - Shut up all pins
1083 * @codec: the HDA codec
1085 * Clear all pin controls to shup up before suspend for avoiding click noise.
1086 * The controls aren't cached so that they can be resumed properly.
1088 void snd_hda_shutup_pins(struct hda_codec
*codec
)
1091 /* don't shut up pins when unloading the driver; otherwise it breaks
1092 * the default pin setup at the next load of the driver
1094 if (codec
->bus
->shutdown
)
1096 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1097 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1098 /* use read here for syncing after issuing each verb */
1099 snd_hda_codec_read(codec
, pin
->nid
, 0,
1100 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
1102 codec
->pins_shutup
= 1;
1104 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins
);
1107 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1108 static void restore_shutup_pins(struct hda_codec
*codec
)
1111 if (!codec
->pins_shutup
)
1113 if (codec
->bus
->shutdown
)
1115 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1116 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1117 snd_hda_codec_write(codec
, pin
->nid
, 0,
1118 AC_VERB_SET_PIN_WIDGET_CONTROL
,
1121 codec
->pins_shutup
= 0;
1125 static void init_hda_cache(struct hda_cache_rec
*cache
,
1126 unsigned int record_size
);
1127 static void free_hda_cache(struct hda_cache_rec
*cache
);
1129 /* restore the initial pin cfgs and release all pincfg lists */
1130 static void restore_init_pincfgs(struct hda_codec
*codec
)
1132 /* first free driver_pins and user_pins, then call restore_pincfg
1133 * so that only the values in init_pins are restored
1135 snd_array_free(&codec
->driver_pins
);
1136 #ifdef CONFIG_SND_HDA_HWDEP
1137 snd_array_free(&codec
->user_pins
);
1139 restore_pincfgs(codec
);
1140 snd_array_free(&codec
->init_pins
);
1144 * audio-converter setup caches
1146 struct hda_cvt_setup
{
1151 unsigned char active
; /* cvt is currently used */
1152 unsigned char dirty
; /* setups should be cleared */
1155 /* get or create a cache entry for the given audio converter NID */
1156 static struct hda_cvt_setup
*
1157 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1159 struct hda_cvt_setup
*p
;
1162 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1163 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1167 p
= snd_array_new(&codec
->cvt_setups
);
1176 static void snd_hda_codec_free(struct hda_codec
*codec
)
1180 restore_init_pincfgs(codec
);
1181 #ifdef CONFIG_SND_HDA_POWER_SAVE
1182 cancel_delayed_work(&codec
->power_work
);
1183 flush_workqueue(codec
->bus
->workq
);
1185 list_del(&codec
->list
);
1186 snd_array_free(&codec
->mixers
);
1187 snd_array_free(&codec
->nids
);
1188 snd_array_free(&codec
->conn_lists
);
1189 snd_array_free(&codec
->spdif_out
);
1190 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1191 if (codec
->patch_ops
.free
)
1192 codec
->patch_ops
.free(codec
);
1193 module_put(codec
->owner
);
1194 free_hda_cache(&codec
->amp_cache
);
1195 free_hda_cache(&codec
->cmd_cache
);
1196 kfree(codec
->vendor_name
);
1197 kfree(codec
->chip_name
);
1198 kfree(codec
->modelname
);
1199 kfree(codec
->wcaps
);
1203 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
1204 unsigned int power_state
);
1207 * snd_hda_codec_new - create a HDA codec
1208 * @bus: the bus to assign
1209 * @codec_addr: the codec address
1210 * @codecp: the pointer to store the generated codec
1212 * Returns 0 if successful, or a negative error code.
1214 int /*__devinit*/ snd_hda_codec_new(struct hda_bus
*bus
,
1215 unsigned int codec_addr
,
1216 struct hda_codec
**codecp
)
1218 struct hda_codec
*codec
;
1222 if (snd_BUG_ON(!bus
))
1224 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1227 if (bus
->caddr_tbl
[codec_addr
]) {
1228 snd_printk(KERN_ERR
"hda_codec: "
1229 "address 0x%x is already occupied\n", codec_addr
);
1233 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1234 if (codec
== NULL
) {
1235 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
1240 codec
->addr
= codec_addr
;
1241 mutex_init(&codec
->spdif_mutex
);
1242 mutex_init(&codec
->control_mutex
);
1243 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1244 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1245 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1246 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1247 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1248 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1249 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1250 snd_array_init(&codec
->conn_lists
, sizeof(hda_nid_t
), 64);
1251 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1252 if (codec
->bus
->modelname
) {
1253 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1254 if (!codec
->modelname
) {
1255 snd_hda_codec_free(codec
);
1260 #ifdef CONFIG_SND_HDA_POWER_SAVE
1261 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1262 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1263 * the caller has to power down appropriatley after initialization
1266 hda_keep_power_on(codec
);
1269 list_add_tail(&codec
->list
, &bus
->codec_list
);
1270 bus
->caddr_tbl
[codec_addr
] = codec
;
1272 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1274 if (codec
->vendor_id
== -1)
1275 /* read again, hopefully the access method was corrected
1276 * in the last read...
1278 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1280 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1281 AC_PAR_SUBSYSTEM_ID
);
1282 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1285 setup_fg_nodes(codec
);
1286 if (!codec
->afg
&& !codec
->mfg
) {
1287 snd_printdd("hda_codec: no AFG or MFG node found\n");
1292 err
= read_widget_caps(codec
, codec
->afg
? codec
->afg
: codec
->mfg
);
1294 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1297 err
= read_pin_defaults(codec
);
1301 if (!codec
->subsystem_id
) {
1302 hda_nid_t nid
= codec
->afg
? codec
->afg
: codec
->mfg
;
1303 codec
->subsystem_id
=
1304 snd_hda_codec_read(codec
, nid
, 0,
1305 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1308 /* power-up all before initialization */
1309 hda_set_power_state(codec
,
1310 codec
->afg
? codec
->afg
: codec
->mfg
,
1313 snd_hda_codec_proc_new(codec
);
1315 snd_hda_create_hwdep(codec
);
1317 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1318 codec
->subsystem_id
, codec
->revision_id
);
1319 snd_component_add(codec
->bus
->card
, component
);
1326 snd_hda_codec_free(codec
);
1329 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
1332 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1333 * @codec: the HDA codec
1335 * Start parsing of the given codec tree and (re-)initialize the whole
1338 * Returns 0 if successful or a negative error code.
1340 int snd_hda_codec_configure(struct hda_codec
*codec
)
1344 codec
->preset
= find_codec_preset(codec
);
1345 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1346 err
= get_codec_name(codec
);
1351 if (is_generic_config(codec
)) {
1352 err
= snd_hda_parse_generic_codec(codec
);
1355 if (codec
->preset
&& codec
->preset
->patch
) {
1356 err
= codec
->preset
->patch(codec
);
1360 /* call the default parser */
1361 err
= snd_hda_parse_generic_codec(codec
);
1363 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1366 if (!err
&& codec
->patch_ops
.unsol_event
)
1367 err
= init_unsol_queue(codec
->bus
);
1368 /* audio codec should override the mixer name */
1369 if (!err
&& (codec
->afg
|| !*codec
->bus
->card
->mixername
))
1370 snprintf(codec
->bus
->card
->mixername
,
1371 sizeof(codec
->bus
->card
->mixername
),
1372 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1375 EXPORT_SYMBOL_HDA(snd_hda_codec_configure
);
1378 * snd_hda_codec_setup_stream - set up the codec for streaming
1379 * @codec: the CODEC to set up
1380 * @nid: the NID to set up
1381 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1382 * @channel_id: channel id to pass, zero based.
1383 * @format: stream format.
1385 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1387 int channel_id
, int format
)
1389 struct hda_codec
*c
;
1390 struct hda_cvt_setup
*p
;
1391 unsigned int oldval
, newval
;
1398 snd_printdd("hda_codec_setup_stream: "
1399 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1400 nid
, stream_tag
, channel_id
, format
);
1401 p
= get_hda_cvt_setup(codec
, nid
);
1404 /* update the stream-id if changed */
1405 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1406 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1407 newval
= (stream_tag
<< 4) | channel_id
;
1408 if (oldval
!= newval
)
1409 snd_hda_codec_write(codec
, nid
, 0,
1410 AC_VERB_SET_CHANNEL_STREAMID
,
1412 p
->stream_tag
= stream_tag
;
1413 p
->channel_id
= channel_id
;
1415 /* update the format-id if changed */
1416 if (p
->format_id
!= format
) {
1417 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1418 AC_VERB_GET_STREAM_FORMAT
, 0);
1419 if (oldval
!= format
) {
1421 snd_hda_codec_write(codec
, nid
, 0,
1422 AC_VERB_SET_STREAM_FORMAT
,
1425 p
->format_id
= format
;
1430 /* make other inactive cvts with the same stream-tag dirty */
1431 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1432 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1433 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1434 p
= snd_array_elem(&c
->cvt_setups
, i
);
1435 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1436 get_wcaps_type(get_wcaps(codec
, p
->nid
)) == type
)
1441 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1443 static void really_cleanup_stream(struct hda_codec
*codec
,
1444 struct hda_cvt_setup
*q
);
1447 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1448 * @codec: the CODEC to clean up
1449 * @nid: the NID to clean up
1450 * @do_now: really clean up the stream instead of clearing the active flag
1452 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1455 struct hda_cvt_setup
*p
;
1460 if (codec
->no_sticky_stream
)
1463 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1464 p
= get_hda_cvt_setup(codec
, nid
);
1466 /* here we just clear the active flag when do_now isn't set;
1467 * actual clean-ups will be done later in
1468 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1471 really_cleanup_stream(codec
, p
);
1476 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream
);
1478 static void really_cleanup_stream(struct hda_codec
*codec
,
1479 struct hda_cvt_setup
*q
)
1481 hda_nid_t nid
= q
->nid
;
1482 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1483 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0);
1484 memset(q
, 0, sizeof(*q
));
1488 /* clean up the all conflicting obsolete streams */
1489 static void purify_inactive_streams(struct hda_codec
*codec
)
1491 struct hda_codec
*c
;
1494 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1495 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1496 struct hda_cvt_setup
*p
;
1497 p
= snd_array_elem(&c
->cvt_setups
, i
);
1499 really_cleanup_stream(c
, p
);
1505 /* clean up all streams; called from suspend */
1506 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1510 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1511 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1513 really_cleanup_stream(codec
, p
);
1519 * amp access functions
1522 /* FIXME: more better hash key? */
1523 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1524 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1525 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1526 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1527 #define INFO_AMP_CAPS (1<<0)
1528 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1530 /* initialize the hash table */
1531 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec
*cache
,
1532 unsigned int record_size
)
1534 memset(cache
, 0, sizeof(*cache
));
1535 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1536 snd_array_init(&cache
->buf
, record_size
, 64);
1539 static void free_hda_cache(struct hda_cache_rec
*cache
)
1541 snd_array_free(&cache
->buf
);
1544 /* query the hash. allocate an entry if not found. */
1545 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1547 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1548 u16 cur
= cache
->hash
[idx
];
1549 struct hda_cache_head
*info
;
1551 while (cur
!= 0xffff) {
1552 info
= snd_array_elem(&cache
->buf
, cur
);
1553 if (info
->key
== key
)
1560 /* query the hash. allocate an entry if not found. */
1561 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1564 struct hda_cache_head
*info
= get_hash(cache
, key
);
1567 /* add a new hash entry */
1568 info
= snd_array_new(&cache
->buf
);
1571 cur
= snd_array_index(&cache
->buf
, info
);
1574 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1575 info
->next
= cache
->hash
[idx
];
1576 cache
->hash
[idx
] = cur
;
1581 /* query and allocate an amp hash entry */
1582 static inline struct hda_amp_info
*
1583 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1585 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1589 * query_amp_caps - query AMP capabilities
1590 * @codec: the HD-auio codec
1591 * @nid: the NID to query
1592 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1594 * Query AMP capabilities for the given widget and direction.
1595 * Returns the obtained capability bits.
1597 * When cap bits have been already read, this doesn't read again but
1598 * returns the cached value.
1600 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1602 struct hda_amp_info
*info
;
1604 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, 0));
1607 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1608 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1610 info
->amp_caps
= snd_hda_param_read(codec
, nid
,
1611 direction
== HDA_OUTPUT
?
1612 AC_PAR_AMP_OUT_CAP
:
1615 info
->head
.val
|= INFO_AMP_CAPS
;
1617 return info
->amp_caps
;
1619 EXPORT_SYMBOL_HDA(query_amp_caps
);
1622 * snd_hda_override_amp_caps - Override the AMP capabilities
1623 * @codec: the CODEC to clean up
1624 * @nid: the NID to clean up
1625 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1626 * @caps: the capability bits to set
1628 * Override the cached AMP caps bits value by the given one.
1629 * This function is useful if the driver needs to adjust the AMP ranges,
1630 * e.g. limit to 0dB, etc.
1632 * Returns zero if successful or a negative error code.
1634 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1637 struct hda_amp_info
*info
;
1639 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0));
1642 info
->amp_caps
= caps
;
1643 info
->head
.val
|= INFO_AMP_CAPS
;
1646 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1649 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, u32 key
,
1650 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
))
1652 struct hda_amp_info
*info
;
1654 info
= get_alloc_amp_hash(codec
, key
);
1657 if (!info
->head
.val
) {
1658 info
->head
.val
|= INFO_AMP_CAPS
;
1659 info
->amp_caps
= func(codec
, nid
);
1661 return info
->amp_caps
;
1664 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
)
1666 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1670 * snd_hda_query_pin_caps - Query PIN capabilities
1671 * @codec: the HD-auio codec
1672 * @nid: the NID to query
1674 * Query PIN capabilities for the given widget.
1675 * Returns the obtained capability bits.
1677 * When cap bits have been already read, this doesn't read again but
1678 * returns the cached value.
1680 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1682 return query_caps_hash(codec
, nid
, HDA_HASH_PINCAP_KEY(nid
),
1685 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1688 * snd_hda_pin_sense - execute pin sense measurement
1689 * @codec: the CODEC to sense
1690 * @nid: the pin NID to sense
1692 * Execute necessary pin sense measurement and return its Presence Detect,
1693 * Impedance, ELD Valid etc. status bits.
1695 u32
snd_hda_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
1699 if (!codec
->no_trigger_sense
) {
1700 pincap
= snd_hda_query_pin_caps(codec
, nid
);
1701 if (pincap
& AC_PINCAP_TRIG_REQ
) /* need trigger? */
1702 snd_hda_codec_read(codec
, nid
, 0,
1703 AC_VERB_SET_PIN_SENSE
, 0);
1705 return snd_hda_codec_read(codec
, nid
, 0,
1706 AC_VERB_GET_PIN_SENSE
, 0);
1708 EXPORT_SYMBOL_HDA(snd_hda_pin_sense
);
1711 * snd_hda_jack_detect - query pin Presence Detect status
1712 * @codec: the CODEC to sense
1713 * @nid: the pin NID to sense
1715 * Query and return the pin's Presence Detect status.
1717 int snd_hda_jack_detect(struct hda_codec
*codec
, hda_nid_t nid
)
1719 u32 sense
= snd_hda_pin_sense(codec
, nid
);
1720 return !!(sense
& AC_PINSENSE_PRESENCE
);
1722 EXPORT_SYMBOL_HDA(snd_hda_jack_detect
);
1725 * read the current volume to info
1726 * if the cache exists, read the cache value.
1728 static unsigned int get_vol_mute(struct hda_codec
*codec
,
1729 struct hda_amp_info
*info
, hda_nid_t nid
,
1730 int ch
, int direction
, int index
)
1734 if (info
->head
.val
& INFO_AMP_VOL(ch
))
1735 return info
->vol
[ch
];
1737 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1738 parm
|= direction
== HDA_OUTPUT
? AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1740 val
= snd_hda_codec_read(codec
, nid
, 0,
1741 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1742 info
->vol
[ch
] = val
& 0xff;
1743 info
->head
.val
|= INFO_AMP_VOL(ch
);
1744 return info
->vol
[ch
];
1748 * write the current volume in info to the h/w and update the cache
1750 static void put_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
1751 hda_nid_t nid
, int ch
, int direction
, int index
,
1756 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1757 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1758 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1760 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1761 info
->vol
[ch
] = val
;
1765 * snd_hda_codec_amp_read - Read AMP value
1766 * @codec: HD-audio codec
1767 * @nid: NID to read the AMP value
1768 * @ch: channel (left=0 or right=1)
1769 * @direction: #HDA_INPUT or #HDA_OUTPUT
1770 * @index: the index value (only for input direction)
1772 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1774 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1775 int direction
, int index
)
1777 struct hda_amp_info
*info
;
1778 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1781 return get_vol_mute(codec
, info
, nid
, ch
, direction
, index
);
1783 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
1786 * snd_hda_codec_amp_update - update the AMP value
1787 * @codec: HD-audio codec
1788 * @nid: NID to read the AMP value
1789 * @ch: channel (left=0 or right=1)
1790 * @direction: #HDA_INPUT or #HDA_OUTPUT
1791 * @idx: the index value (only for input direction)
1792 * @mask: bit mask to set
1793 * @val: the bits value to set
1795 * Update the AMP value with a bit mask.
1796 * Returns 0 if the value is unchanged, 1 if changed.
1798 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1799 int direction
, int idx
, int mask
, int val
)
1801 struct hda_amp_info
*info
;
1803 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, idx
));
1806 if (snd_BUG_ON(mask
& ~0xff))
1809 val
|= get_vol_mute(codec
, info
, nid
, ch
, direction
, idx
) & ~mask
;
1810 if (info
->vol
[ch
] == val
)
1812 put_vol_mute(codec
, info
, nid
, ch
, direction
, idx
, val
);
1815 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
1818 * snd_hda_codec_amp_stereo - update the AMP stereo values
1819 * @codec: HD-audio codec
1820 * @nid: NID to read the AMP value
1821 * @direction: #HDA_INPUT or #HDA_OUTPUT
1822 * @idx: the index value (only for input direction)
1823 * @mask: bit mask to set
1824 * @val: the bits value to set
1826 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1827 * stereo widget with the same mask and value.
1829 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1830 int direction
, int idx
, int mask
, int val
)
1834 if (snd_BUG_ON(mask
& ~0xff))
1836 for (ch
= 0; ch
< 2; ch
++)
1837 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1841 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
1845 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1846 * @codec: HD-audio codec
1848 * Resume the all amp commands from the cache.
1850 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
1852 struct hda_amp_info
*buffer
= codec
->amp_cache
.buf
.list
;
1855 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++, buffer
++) {
1856 u32 key
= buffer
->head
.key
;
1858 unsigned int idx
, dir
, ch
;
1862 idx
= (key
>> 16) & 0xff;
1863 dir
= (key
>> 24) & 0xff;
1864 for (ch
= 0; ch
< 2; ch
++) {
1865 if (!(buffer
->head
.val
& INFO_AMP_VOL(ch
)))
1867 put_vol_mute(codec
, buffer
, nid
, ch
, dir
, idx
,
1872 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
1873 #endif /* CONFIG_PM */
1875 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1878 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1880 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1887 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1889 * The control element is supposed to have the private_value field
1890 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1892 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1893 struct snd_ctl_elem_info
*uinfo
)
1895 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1896 u16 nid
= get_amp_nid(kcontrol
);
1897 u8 chs
= get_amp_channels(kcontrol
);
1898 int dir
= get_amp_direction(kcontrol
);
1899 unsigned int ofs
= get_amp_offset(kcontrol
);
1901 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1902 uinfo
->count
= chs
== 3 ? 2 : 1;
1903 uinfo
->value
.integer
.min
= 0;
1904 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
1905 if (!uinfo
->value
.integer
.max
) {
1906 printk(KERN_WARNING
"hda_codec: "
1907 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
1913 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
1916 static inline unsigned int
1917 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1918 int ch
, int dir
, int idx
, unsigned int ofs
)
1921 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1922 val
&= HDA_AMP_VOLMASK
;
1931 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1932 int ch
, int dir
, int idx
, unsigned int ofs
,
1935 unsigned int maxval
;
1939 /* ofs = 0: raw max value */
1940 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
1943 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
1944 HDA_AMP_VOLMASK
, val
);
1948 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1950 * The control element is supposed to have the private_value field
1951 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1953 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
1954 struct snd_ctl_elem_value
*ucontrol
)
1956 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1957 hda_nid_t nid
= get_amp_nid(kcontrol
);
1958 int chs
= get_amp_channels(kcontrol
);
1959 int dir
= get_amp_direction(kcontrol
);
1960 int idx
= get_amp_index(kcontrol
);
1961 unsigned int ofs
= get_amp_offset(kcontrol
);
1962 long *valp
= ucontrol
->value
.integer
.value
;
1965 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
1967 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
1970 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
1973 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1975 * The control element is supposed to have the private_value field
1976 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1978 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
1979 struct snd_ctl_elem_value
*ucontrol
)
1981 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1982 hda_nid_t nid
= get_amp_nid(kcontrol
);
1983 int chs
= get_amp_channels(kcontrol
);
1984 int dir
= get_amp_direction(kcontrol
);
1985 int idx
= get_amp_index(kcontrol
);
1986 unsigned int ofs
= get_amp_offset(kcontrol
);
1987 long *valp
= ucontrol
->value
.integer
.value
;
1990 snd_hda_power_up(codec
);
1992 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
1996 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
1997 snd_hda_power_down(codec
);
2000 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
2003 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2005 * The control element is supposed to have the private_value field
2006 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2008 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2009 unsigned int size
, unsigned int __user
*_tlv
)
2011 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2012 hda_nid_t nid
= get_amp_nid(kcontrol
);
2013 int dir
= get_amp_direction(kcontrol
);
2014 unsigned int ofs
= get_amp_offset(kcontrol
);
2015 bool min_mute
= get_amp_min_mute(kcontrol
);
2016 u32 caps
, val1
, val2
;
2018 if (size
< 4 * sizeof(unsigned int))
2020 caps
= query_amp_caps(codec
, nid
, dir
);
2021 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2022 val2
= (val2
+ 1) * 25;
2023 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2025 val1
= ((int)val1
) * ((int)val2
);
2027 val2
|= TLV_DB_SCALE_MUTE
;
2028 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2030 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2032 if (put_user(val1
, _tlv
+ 2))
2034 if (put_user(val2
, _tlv
+ 3))
2038 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
2041 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2042 * @codec: HD-audio codec
2043 * @nid: NID of a reference widget
2044 * @dir: #HDA_INPUT or #HDA_OUTPUT
2045 * @tlv: TLV data to be stored, at least 4 elements
2047 * Set (static) TLV data for a virtual master volume using the AMP caps
2048 * obtained from the reference NID.
2049 * The volume range is recalculated as if the max volume is 0dB.
2051 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2057 caps
= query_amp_caps(codec
, nid
, dir
);
2058 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2059 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2060 step
= (step
+ 1) * 25;
2061 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2062 tlv
[1] = 2 * sizeof(unsigned int);
2063 tlv
[2] = -nums
* step
;
2066 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
2068 /* find a mixer control element with the given name */
2069 static struct snd_kcontrol
*
2070 _snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2071 const char *name
, int idx
)
2073 struct snd_ctl_elem_id id
;
2074 memset(&id
, 0, sizeof(id
));
2075 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2077 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2079 strcpy(id
.name
, name
);
2080 return snd_ctl_find_id(codec
->bus
->card
, &id
);
2084 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2085 * @codec: HD-audio codec
2086 * @name: ctl id name string
2088 * Get the control element with the given id string and IFACE_MIXER.
2090 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2093 return _snd_hda_find_mixer_ctl(codec
, name
, 0);
2095 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
2097 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
)
2100 for (idx
= 0; idx
< 16; idx
++) { /* 16 ctlrs should be large enough */
2101 if (!_snd_hda_find_mixer_ctl(codec
, name
, idx
))
2108 * snd_hda_ctl_add - Add a control element and assign to the codec
2109 * @codec: HD-audio codec
2110 * @nid: corresponding NID (optional)
2111 * @kctl: the control element to assign
2113 * Add the given control element to an array inside the codec instance.
2114 * All control elements belonging to a codec are supposed to be added
2115 * by this function so that a proper clean-up works at the free or
2116 * reconfiguration time.
2118 * If non-zero @nid is passed, the NID is assigned to the control element.
2119 * The assignment is shown in the codec proc file.
2121 * snd_hda_ctl_add() checks the control subdev id field whether
2122 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2123 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2124 * specifies if kctl->private_value is a HDA amplifier value.
2126 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2127 struct snd_kcontrol
*kctl
)
2130 unsigned short flags
= 0;
2131 struct hda_nid_item
*item
;
2133 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2134 flags
|= HDA_NID_ITEM_AMP
;
2136 nid
= get_amp_nid_(kctl
->private_value
);
2138 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2139 nid
= kctl
->id
.subdevice
& 0xffff;
2140 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2141 kctl
->id
.subdevice
= 0;
2142 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
2145 item
= snd_array_new(&codec
->mixers
);
2150 item
->flags
= flags
;
2153 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
2156 * snd_hda_add_nid - Assign a NID to a control element
2157 * @codec: HD-audio codec
2158 * @nid: corresponding NID (optional)
2159 * @kctl: the control element to assign
2160 * @index: index to kctl
2162 * Add the given control element to an array inside the codec instance.
2163 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2164 * NID:KCTL mapping - for example "Capture Source" selector.
2166 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2167 unsigned int index
, hda_nid_t nid
)
2169 struct hda_nid_item
*item
;
2172 item
= snd_array_new(&codec
->nids
);
2176 item
->index
= index
;
2180 printk(KERN_ERR
"hda-codec: no NID for mapping control %s:%d:%d\n",
2181 kctl
->id
.name
, kctl
->id
.index
, index
);
2184 EXPORT_SYMBOL_HDA(snd_hda_add_nid
);
2187 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2188 * @codec: HD-audio codec
2190 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2193 struct hda_nid_item
*items
= codec
->mixers
.list
;
2194 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2195 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
2196 snd_array_free(&codec
->mixers
);
2197 snd_array_free(&codec
->nids
);
2200 /* pseudo device locking
2201 * toggle card->shutdown to allow/disallow the device access (as a hack)
2203 static int hda_lock_devices(struct snd_card
*card
)
2205 spin_lock(&card
->files_lock
);
2206 if (card
->shutdown
) {
2207 spin_unlock(&card
->files_lock
);
2211 spin_unlock(&card
->files_lock
);
2215 static void hda_unlock_devices(struct snd_card
*card
)
2217 spin_lock(&card
->files_lock
);
2219 spin_unlock(&card
->files_lock
);
2223 * snd_hda_codec_reset - Clear all objects assigned to the codec
2224 * @codec: HD-audio codec
2226 * This frees the all PCM and control elements assigned to the codec, and
2227 * clears the caches and restores the pin default configurations.
2229 * When a device is being used, it returns -EBSY. If successfully freed,
2232 int snd_hda_codec_reset(struct hda_codec
*codec
)
2234 struct snd_card
*card
= codec
->bus
->card
;
2237 if (hda_lock_devices(card
) < 0)
2239 /* check whether the codec isn't used by any mixer or PCM streams */
2240 if (!list_empty(&card
->ctl_files
)) {
2241 hda_unlock_devices(card
);
2244 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2245 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2248 if (cpcm
->pcm
->streams
[0].substream_opened
||
2249 cpcm
->pcm
->streams
[1].substream_opened
) {
2250 hda_unlock_devices(card
);
2255 /* OK, let it free */
2257 #ifdef CONFIG_SND_HDA_POWER_SAVE
2258 cancel_delayed_work(&codec
->power_work
);
2259 flush_workqueue(codec
->bus
->workq
);
2261 snd_hda_ctls_clear(codec
);
2263 for (i
= 0; i
< codec
->num_pcms
; i
++) {
2264 if (codec
->pcm_info
[i
].pcm
) {
2265 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
2266 clear_bit(codec
->pcm_info
[i
].device
,
2267 codec
->bus
->pcm_dev_bits
);
2270 if (codec
->patch_ops
.free
)
2271 codec
->patch_ops
.free(codec
);
2272 codec
->proc_widget_hook
= NULL
;
2274 free_hda_cache(&codec
->amp_cache
);
2275 free_hda_cache(&codec
->cmd_cache
);
2276 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2277 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2278 /* free only driver_pins so that init_pins + user_pins are restored */
2279 snd_array_free(&codec
->driver_pins
);
2280 restore_pincfgs(codec
);
2281 codec
->num_pcms
= 0;
2282 codec
->pcm_info
= NULL
;
2283 codec
->preset
= NULL
;
2284 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2285 codec
->slave_dig_outs
= NULL
;
2286 codec
->spdif_status_reset
= 0;
2287 module_put(codec
->owner
);
2288 codec
->owner
= NULL
;
2290 /* allow device access again */
2291 hda_unlock_devices(card
);
2296 * snd_hda_add_vmaster - create a virtual master control and add slaves
2297 * @codec: HD-audio codec
2298 * @name: vmaster control name
2299 * @tlv: TLV data (optional)
2300 * @slaves: slave control names (optional)
2302 * Create a virtual master control with the given name. The TLV data
2303 * must be either NULL or a valid data.
2305 * @slaves is a NULL-terminated array of strings, each of which is a
2306 * slave control name. All controls with these names are assigned to
2307 * the new virtual master control.
2309 * This function returns zero if successful or a negative error code.
2311 int snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2312 unsigned int *tlv
, const char * const *slaves
)
2314 struct snd_kcontrol
*kctl
;
2315 const char * const *s
;
2318 for (s
= slaves
; *s
&& !snd_hda_find_mixer_ctl(codec
, *s
); s
++)
2321 snd_printdd("No slave found for %s\n", name
);
2324 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2327 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2331 for (s
= slaves
; *s
; s
++) {
2332 struct snd_kcontrol
*sctl
;
2335 sctl
= _snd_hda_find_mixer_ctl(codec
, *s
, i
);
2338 snd_printdd("Cannot find slave %s, "
2342 err
= snd_ctl_add_slave(kctl
, sctl
);
2350 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster
);
2353 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2355 * The control element is supposed to have the private_value field
2356 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2358 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2359 struct snd_ctl_elem_info
*uinfo
)
2361 int chs
= get_amp_channels(kcontrol
);
2363 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2364 uinfo
->count
= chs
== 3 ? 2 : 1;
2365 uinfo
->value
.integer
.min
= 0;
2366 uinfo
->value
.integer
.max
= 1;
2369 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
2372 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2374 * The control element is supposed to have the private_value field
2375 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2377 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2378 struct snd_ctl_elem_value
*ucontrol
)
2380 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2381 hda_nid_t nid
= get_amp_nid(kcontrol
);
2382 int chs
= get_amp_channels(kcontrol
);
2383 int dir
= get_amp_direction(kcontrol
);
2384 int idx
= get_amp_index(kcontrol
);
2385 long *valp
= ucontrol
->value
.integer
.value
;
2388 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2389 HDA_AMP_MUTE
) ? 0 : 1;
2391 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2392 HDA_AMP_MUTE
) ? 0 : 1;
2395 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
2398 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2400 * The control element is supposed to have the private_value field
2401 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2403 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2404 struct snd_ctl_elem_value
*ucontrol
)
2406 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2407 hda_nid_t nid
= get_amp_nid(kcontrol
);
2408 int chs
= get_amp_channels(kcontrol
);
2409 int dir
= get_amp_direction(kcontrol
);
2410 int idx
= get_amp_index(kcontrol
);
2411 long *valp
= ucontrol
->value
.integer
.value
;
2414 snd_hda_power_up(codec
);
2416 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2418 *valp
? 0 : HDA_AMP_MUTE
);
2422 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2424 *valp
? 0 : HDA_AMP_MUTE
);
2425 hda_call_check_power_status(codec
, nid
);
2426 snd_hda_power_down(codec
);
2429 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
2431 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2433 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2435 * This function calls snd_hda_enable_beep_device(), which behaves differently
2436 * depending on beep_mode option.
2438 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol
*kcontrol
,
2439 struct snd_ctl_elem_value
*ucontrol
)
2441 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2442 long *valp
= ucontrol
->value
.integer
.value
;
2444 snd_hda_enable_beep_device(codec
, *valp
);
2445 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2447 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep
);
2448 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2451 * bound volume controls
2453 * bind multiple volumes (# indices, from 0)
2456 #define AMP_VAL_IDX_SHIFT 19
2457 #define AMP_VAL_IDX_MASK (0x0f<<19)
2460 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2462 * The control element is supposed to have the private_value field
2463 * set up via HDA_BIND_MUTE*() macros.
2465 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2466 struct snd_ctl_elem_value
*ucontrol
)
2468 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2472 mutex_lock(&codec
->control_mutex
);
2473 pval
= kcontrol
->private_value
;
2474 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2475 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2476 kcontrol
->private_value
= pval
;
2477 mutex_unlock(&codec
->control_mutex
);
2480 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
2483 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2485 * The control element is supposed to have the private_value field
2486 * set up via HDA_BIND_MUTE*() macros.
2488 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2489 struct snd_ctl_elem_value
*ucontrol
)
2491 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2493 int i
, indices
, err
= 0, change
= 0;
2495 mutex_lock(&codec
->control_mutex
);
2496 pval
= kcontrol
->private_value
;
2497 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2498 for (i
= 0; i
< indices
; i
++) {
2499 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2500 (i
<< AMP_VAL_IDX_SHIFT
);
2501 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2506 kcontrol
->private_value
= pval
;
2507 mutex_unlock(&codec
->control_mutex
);
2508 return err
< 0 ? err
: change
;
2510 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
2513 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2515 * The control element is supposed to have the private_value field
2516 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2518 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2519 struct snd_ctl_elem_info
*uinfo
)
2521 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2522 struct hda_bind_ctls
*c
;
2525 mutex_lock(&codec
->control_mutex
);
2526 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2527 kcontrol
->private_value
= *c
->values
;
2528 err
= c
->ops
->info(kcontrol
, uinfo
);
2529 kcontrol
->private_value
= (long)c
;
2530 mutex_unlock(&codec
->control_mutex
);
2533 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
2536 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2538 * The control element is supposed to have the private_value field
2539 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2541 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2542 struct snd_ctl_elem_value
*ucontrol
)
2544 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2545 struct hda_bind_ctls
*c
;
2548 mutex_lock(&codec
->control_mutex
);
2549 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2550 kcontrol
->private_value
= *c
->values
;
2551 err
= c
->ops
->get(kcontrol
, ucontrol
);
2552 kcontrol
->private_value
= (long)c
;
2553 mutex_unlock(&codec
->control_mutex
);
2556 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
2559 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2561 * The control element is supposed to have the private_value field
2562 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2564 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2565 struct snd_ctl_elem_value
*ucontrol
)
2567 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2568 struct hda_bind_ctls
*c
;
2569 unsigned long *vals
;
2570 int err
= 0, change
= 0;
2572 mutex_lock(&codec
->control_mutex
);
2573 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2574 for (vals
= c
->values
; *vals
; vals
++) {
2575 kcontrol
->private_value
= *vals
;
2576 err
= c
->ops
->put(kcontrol
, ucontrol
);
2581 kcontrol
->private_value
= (long)c
;
2582 mutex_unlock(&codec
->control_mutex
);
2583 return err
< 0 ? err
: change
;
2585 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
2588 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2590 * The control element is supposed to have the private_value field
2591 * set up via HDA_BIND_VOL() macro.
2593 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2594 unsigned int size
, unsigned int __user
*tlv
)
2596 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2597 struct hda_bind_ctls
*c
;
2600 mutex_lock(&codec
->control_mutex
);
2601 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2602 kcontrol
->private_value
= *c
->values
;
2603 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
2604 kcontrol
->private_value
= (long)c
;
2605 mutex_unlock(&codec
->control_mutex
);
2608 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
2610 struct hda_ctl_ops snd_hda_bind_vol
= {
2611 .info
= snd_hda_mixer_amp_volume_info
,
2612 .get
= snd_hda_mixer_amp_volume_get
,
2613 .put
= snd_hda_mixer_amp_volume_put
,
2614 .tlv
= snd_hda_mixer_amp_tlv
2616 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
2618 struct hda_ctl_ops snd_hda_bind_sw
= {
2619 .info
= snd_hda_mixer_amp_switch_info
,
2620 .get
= snd_hda_mixer_amp_switch_get
,
2621 .put
= snd_hda_mixer_amp_switch_put
,
2622 .tlv
= snd_hda_mixer_amp_tlv
2624 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
2627 * SPDIF out controls
2630 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2631 struct snd_ctl_elem_info
*uinfo
)
2633 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2638 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2639 struct snd_ctl_elem_value
*ucontrol
)
2641 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2642 IEC958_AES0_NONAUDIO
|
2643 IEC958_AES0_CON_EMPHASIS_5015
|
2644 IEC958_AES0_CON_NOT_COPYRIGHT
;
2645 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2646 IEC958_AES1_CON_ORIGINAL
;
2650 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2651 struct snd_ctl_elem_value
*ucontrol
)
2653 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2654 IEC958_AES0_NONAUDIO
|
2655 IEC958_AES0_PRO_EMPHASIS_5015
;
2659 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2660 struct snd_ctl_elem_value
*ucontrol
)
2662 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2663 int idx
= kcontrol
->private_value
;
2664 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2666 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
2667 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
2668 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
2669 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
2674 /* convert from SPDIF status bits to HDA SPDIF bits
2675 * bit 0 (DigEn) is always set zero (to be filled later)
2677 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2679 unsigned short val
= 0;
2681 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2682 val
|= AC_DIG1_PROFESSIONAL
;
2683 if (sbits
& IEC958_AES0_NONAUDIO
)
2684 val
|= AC_DIG1_NONAUDIO
;
2685 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2686 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2687 IEC958_AES0_PRO_EMPHASIS_5015
)
2688 val
|= AC_DIG1_EMPHASIS
;
2690 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2691 IEC958_AES0_CON_EMPHASIS_5015
)
2692 val
|= AC_DIG1_EMPHASIS
;
2693 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2694 val
|= AC_DIG1_COPYRIGHT
;
2695 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2696 val
|= AC_DIG1_LEVEL
;
2697 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2702 /* convert to SPDIF status bits from HDA SPDIF bits
2704 static unsigned int convert_to_spdif_status(unsigned short val
)
2706 unsigned int sbits
= 0;
2708 if (val
& AC_DIG1_NONAUDIO
)
2709 sbits
|= IEC958_AES0_NONAUDIO
;
2710 if (val
& AC_DIG1_PROFESSIONAL
)
2711 sbits
|= IEC958_AES0_PROFESSIONAL
;
2712 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2713 if (sbits
& AC_DIG1_EMPHASIS
)
2714 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
2716 if (val
& AC_DIG1_EMPHASIS
)
2717 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
2718 if (!(val
& AC_DIG1_COPYRIGHT
))
2719 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
2720 if (val
& AC_DIG1_LEVEL
)
2721 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
2722 sbits
|= val
& (0x7f << 8);
2727 /* set digital convert verbs both for the given NID and its slaves */
2728 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
2733 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
2734 d
= codec
->slave_dig_outs
;
2738 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
2741 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
2745 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
2747 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
2750 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
2751 struct snd_ctl_elem_value
*ucontrol
)
2753 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2754 int idx
= kcontrol
->private_value
;
2755 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2756 hda_nid_t nid
= spdif
->nid
;
2760 mutex_lock(&codec
->spdif_mutex
);
2761 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
2762 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
2763 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
2764 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
2765 val
= convert_from_spdif_status(spdif
->status
);
2766 val
|= spdif
->ctls
& 1;
2767 change
= spdif
->ctls
!= val
;
2769 if (change
&& nid
!= (u16
)-1)
2770 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2771 mutex_unlock(&codec
->spdif_mutex
);
2775 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2777 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
2778 struct snd_ctl_elem_value
*ucontrol
)
2780 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2781 int idx
= kcontrol
->private_value
;
2782 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2784 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
2788 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
2791 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
2792 /* unmute amp switch (if any) */
2793 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
2794 (dig1
& AC_DIG1_ENABLE
))
2795 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
2799 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
2800 struct snd_ctl_elem_value
*ucontrol
)
2802 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2803 int idx
= kcontrol
->private_value
;
2804 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2805 hda_nid_t nid
= spdif
->nid
;
2809 mutex_lock(&codec
->spdif_mutex
);
2810 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
2811 if (ucontrol
->value
.integer
.value
[0])
2812 val
|= AC_DIG1_ENABLE
;
2813 change
= spdif
->ctls
!= val
;
2815 if (change
&& nid
!= (u16
)-1)
2816 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
2817 mutex_unlock(&codec
->spdif_mutex
);
2821 static struct snd_kcontrol_new dig_mixes
[] = {
2823 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2824 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2825 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
2826 .info
= snd_hda_spdif_mask_info
,
2827 .get
= snd_hda_spdif_cmask_get
,
2830 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2831 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2832 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
2833 .info
= snd_hda_spdif_mask_info
,
2834 .get
= snd_hda_spdif_pmask_get
,
2837 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2838 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
2839 .info
= snd_hda_spdif_mask_info
,
2840 .get
= snd_hda_spdif_default_get
,
2841 .put
= snd_hda_spdif_default_put
,
2844 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2845 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
2846 .info
= snd_hda_spdif_out_switch_info
,
2847 .get
= snd_hda_spdif_out_switch_get
,
2848 .put
= snd_hda_spdif_out_switch_put
,
2854 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2855 * @codec: the HDA codec
2856 * @nid: audio out widget NID
2858 * Creates controls related with the SPDIF output.
2859 * Called from each patch supporting the SPDIF out.
2861 * Returns 0 if successful, or a negative error code.
2863 int snd_hda_create_spdif_out_ctls(struct hda_codec
*codec
,
2864 hda_nid_t associated_nid
,
2868 struct snd_kcontrol
*kctl
;
2869 struct snd_kcontrol_new
*dig_mix
;
2871 struct hda_spdif_out
*spdif
;
2873 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch");
2875 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
2878 spdif
= snd_array_new(&codec
->spdif_out
);
2879 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2880 kctl
= snd_ctl_new1(dig_mix
, codec
);
2883 kctl
->id
.index
= idx
;
2884 kctl
->private_value
= codec
->spdif_out
.used
- 1;
2885 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
2889 spdif
->nid
= cvt_nid
;
2890 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
2891 AC_VERB_GET_DIGI_CONVERT_1
, 0);
2892 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
2895 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls
);
2897 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
2901 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
2902 struct hda_spdif_out
*spdif
=
2903 snd_array_elem(&codec
->spdif_out
, i
);
2904 if (spdif
->nid
== nid
)
2909 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid
);
2911 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
2913 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2915 mutex_lock(&codec
->spdif_mutex
);
2916 spdif
->nid
= (u16
)-1;
2917 mutex_unlock(&codec
->spdif_mutex
);
2919 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign
);
2921 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
2923 struct hda_spdif_out
*spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2926 mutex_lock(&codec
->spdif_mutex
);
2927 if (spdif
->nid
!= nid
) {
2930 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2932 mutex_unlock(&codec
->spdif_mutex
);
2934 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign
);
2937 * SPDIF sharing with analog output
2939 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
2940 struct snd_ctl_elem_value
*ucontrol
)
2942 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2943 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
2947 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
2948 struct snd_ctl_elem_value
*ucontrol
)
2950 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2951 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
2955 static struct snd_kcontrol_new spdif_share_sw
= {
2956 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2957 .name
= "IEC958 Default PCM Playback Switch",
2958 .info
= snd_ctl_boolean_mono_info
,
2959 .get
= spdif_share_sw_get
,
2960 .put
= spdif_share_sw_put
,
2964 * snd_hda_create_spdif_share_sw - create Default PCM switch
2965 * @codec: the HDA codec
2966 * @mout: multi-out instance
2968 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
2969 struct hda_multi_out
*mout
)
2971 if (!mout
->dig_out_nid
)
2973 /* ATTENTION: here mout is passed as private_data, instead of codec */
2974 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
,
2975 snd_ctl_new1(&spdif_share_sw
, mout
));
2977 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
2983 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2985 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
2986 struct snd_ctl_elem_value
*ucontrol
)
2988 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2990 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
2994 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
2995 struct snd_ctl_elem_value
*ucontrol
)
2997 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2998 hda_nid_t nid
= kcontrol
->private_value
;
2999 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3002 mutex_lock(&codec
->spdif_mutex
);
3003 change
= codec
->spdif_in_enable
!= val
;
3005 codec
->spdif_in_enable
= val
;
3006 snd_hda_codec_write_cache(codec
, nid
, 0,
3007 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3009 mutex_unlock(&codec
->spdif_mutex
);
3013 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3014 struct snd_ctl_elem_value
*ucontrol
)
3016 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3017 hda_nid_t nid
= kcontrol
->private_value
;
3021 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3022 sbits
= convert_to_spdif_status(val
);
3023 ucontrol
->value
.iec958
.status
[0] = sbits
;
3024 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3025 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3026 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3030 static struct snd_kcontrol_new dig_in_ctls
[] = {
3032 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3033 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3034 .info
= snd_hda_spdif_in_switch_info
,
3035 .get
= snd_hda_spdif_in_switch_get
,
3036 .put
= snd_hda_spdif_in_switch_put
,
3039 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3040 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3041 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3042 .info
= snd_hda_spdif_mask_info
,
3043 .get
= snd_hda_spdif_in_status_get
,
3049 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3050 * @codec: the HDA codec
3051 * @nid: audio in widget NID
3053 * Creates controls related with the SPDIF input.
3054 * Called from each patch supporting the SPDIF in.
3056 * Returns 0 if successful, or a negative error code.
3058 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3061 struct snd_kcontrol
*kctl
;
3062 struct snd_kcontrol_new
*dig_mix
;
3065 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch");
3067 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
3070 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3071 kctl
= snd_ctl_new1(dig_mix
, codec
);
3074 kctl
->private_value
= nid
;
3075 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3079 codec
->spdif_in_enable
=
3080 snd_hda_codec_read(codec
, nid
, 0,
3081 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3085 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
3092 /* build a 32bit cache key with the widget id and the command parameter */
3093 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3094 #define get_cmd_cache_nid(key) ((key) & 0xff)
3095 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3098 * snd_hda_codec_write_cache - send a single command with caching
3099 * @codec: the HDA codec
3100 * @nid: NID to send the command
3101 * @direct: direct flag
3102 * @verb: the verb to send
3103 * @parm: the parameter for the verb
3105 * Send a single command without waiting for response.
3107 * Returns 0 if successful, or a negative error code.
3109 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3110 int direct
, unsigned int verb
, unsigned int parm
)
3112 int err
= snd_hda_codec_write(codec
, nid
, direct
, verb
, parm
);
3113 struct hda_cache_head
*c
;
3118 /* parm may contain the verb stuff for get/set amp */
3119 verb
= verb
| (parm
>> 8);
3121 key
= build_cmd_cache_key(nid
, verb
);
3122 mutex_lock(&codec
->bus
->cmd_mutex
);
3123 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3126 mutex_unlock(&codec
->bus
->cmd_mutex
);
3129 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
3132 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3133 * @codec: the HDA codec
3134 * @nid: NID to send the command
3135 * @direct: direct flag
3136 * @verb: the verb to send
3137 * @parm: the parameter for the verb
3139 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3140 * command if the parameter is already identical with the cached value.
3141 * If not, it sends the command and refreshes the cache.
3143 * Returns 0 if successful, or a negative error code.
3145 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3146 int direct
, unsigned int verb
, unsigned int parm
)
3148 struct hda_cache_head
*c
;
3151 /* parm may contain the verb stuff for get/set amp */
3152 verb
= verb
| (parm
>> 8);
3154 key
= build_cmd_cache_key(nid
, verb
);
3155 mutex_lock(&codec
->bus
->cmd_mutex
);
3156 c
= get_hash(&codec
->cmd_cache
, key
);
3157 if (c
&& c
->val
== parm
) {
3158 mutex_unlock(&codec
->bus
->cmd_mutex
);
3161 mutex_unlock(&codec
->bus
->cmd_mutex
);
3162 return snd_hda_codec_write_cache(codec
, nid
, direct
, verb
, parm
);
3164 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache
);
3167 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3168 * @codec: HD-audio codec
3170 * Execute all verbs recorded in the command caches to resume.
3172 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3174 struct hda_cache_head
*buffer
= codec
->cmd_cache
.buf
.list
;
3177 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++, buffer
++) {
3178 u32 key
= buffer
->key
;
3181 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3182 get_cmd_cache_cmd(key
), buffer
->val
);
3185 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
3188 * snd_hda_sequence_write_cache - sequence writes with caching
3189 * @codec: the HDA codec
3190 * @seq: VERB array to send
3192 * Send the commands sequentially from the given array.
3193 * Thte commands are recorded on cache for power-save and resume.
3194 * The array must be terminated with NID=0.
3196 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3197 const struct hda_verb
*seq
)
3199 for (; seq
->nid
; seq
++)
3200 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3203 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
3204 #endif /* CONFIG_PM */
3207 * set power state of the codec
3209 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
3210 unsigned int power_state
)
3215 /* this delay seems necessary to avoid click noise at power-down */
3216 if (power_state
== AC_PWRST_D3
)
3218 snd_hda_codec_read(codec
, fg
, 0, AC_VERB_SET_POWER_STATE
,
3220 /* partial workaround for "azx_get_response timeout" */
3221 if (power_state
== AC_PWRST_D0
&&
3222 (codec
->vendor_id
& 0xffff0000) == 0x14f10000)
3225 nid
= codec
->start_nid
;
3226 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3227 unsigned int wcaps
= get_wcaps(codec
, nid
);
3228 if (wcaps
& AC_WCAP_POWER
) {
3229 unsigned int wid_type
= get_wcaps_type(wcaps
);
3230 if (power_state
== AC_PWRST_D3
&&
3231 wid_type
== AC_WID_PIN
) {
3232 unsigned int pincap
;
3234 * don't power down the widget if it controls
3235 * eapd and EAPD_BTLENABLE is set.
3237 pincap
= snd_hda_query_pin_caps(codec
, nid
);
3238 if (pincap
& AC_PINCAP_EAPD
) {
3239 int eapd
= snd_hda_codec_read(codec
,
3241 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3247 snd_hda_codec_write(codec
, nid
, 0,
3248 AC_VERB_SET_POWER_STATE
,
3253 if (power_state
== AC_PWRST_D0
) {
3254 unsigned long end_time
;
3256 /* wait until the codec reachs to D0 */
3257 end_time
= jiffies
+ msecs_to_jiffies(500);
3259 state
= snd_hda_codec_read(codec
, fg
, 0,
3260 AC_VERB_GET_POWER_STATE
, 0);
3261 if (state
== power_state
)
3264 } while (time_after_eq(end_time
, jiffies
));
3268 #ifdef CONFIG_SND_HDA_HWDEP
3269 /* execute additional init verbs */
3270 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3272 if (codec
->init_verbs
.list
)
3273 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3276 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3281 * call suspend and power-down; used both from PM and power-save
3283 static void hda_call_codec_suspend(struct hda_codec
*codec
)
3285 if (codec
->patch_ops
.suspend
)
3286 codec
->patch_ops
.suspend(codec
, PMSG_SUSPEND
);
3287 hda_cleanup_all_streams(codec
);
3288 hda_set_power_state(codec
,
3289 codec
->afg
? codec
->afg
: codec
->mfg
,
3291 #ifdef CONFIG_SND_HDA_POWER_SAVE
3292 snd_hda_update_power_acct(codec
);
3293 cancel_delayed_work(&codec
->power_work
);
3294 codec
->power_on
= 0;
3295 codec
->power_transition
= 0;
3296 codec
->power_jiffies
= jiffies
;
3301 * kick up codec; used both from PM and power-save
3303 static void hda_call_codec_resume(struct hda_codec
*codec
)
3305 hda_set_power_state(codec
,
3306 codec
->afg
? codec
->afg
: codec
->mfg
,
3308 restore_pincfgs(codec
); /* restore all current pin configs */
3309 restore_shutup_pins(codec
);
3310 hda_exec_init_verbs(codec
);
3311 if (codec
->patch_ops
.resume
)
3312 codec
->patch_ops
.resume(codec
);
3314 if (codec
->patch_ops
.init
)
3315 codec
->patch_ops
.init(codec
);
3316 snd_hda_codec_resume_amp(codec
);
3317 snd_hda_codec_resume_cache(codec
);
3320 #endif /* CONFIG_PM */
3324 * snd_hda_build_controls - build mixer controls
3327 * Creates mixer controls for each codec included in the bus.
3329 * Returns 0 if successful, otherwise a negative error code.
3331 int /*__devinit*/ snd_hda_build_controls(struct hda_bus
*bus
)
3333 struct hda_codec
*codec
;
3335 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3336 int err
= snd_hda_codec_build_controls(codec
);
3338 printk(KERN_ERR
"hda_codec: cannot build controls "
3339 "for #%d (error %d)\n", codec
->addr
, err
);
3340 err
= snd_hda_codec_reset(codec
);
3343 "hda_codec: cannot revert codec\n");
3350 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
3352 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
3355 hda_exec_init_verbs(codec
);
3356 /* continue to initialize... */
3357 if (codec
->patch_ops
.init
)
3358 err
= codec
->patch_ops
.init(codec
);
3359 if (!err
&& codec
->patch_ops
.build_controls
)
3360 err
= codec
->patch_ops
.build_controls(codec
);
3369 struct hda_rate_tbl
{
3371 unsigned int alsa_bits
;
3372 unsigned int hda_fmt
;
3375 /* rate = base * mult / div */
3376 #define HDA_RATE(base, mult, div) \
3377 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3378 (((div) - 1) << AC_FMT_DIV_SHIFT))
3380 static struct hda_rate_tbl rate_bits
[] = {
3381 /* rate in Hz, ALSA rate bitmask, HDA format value */
3383 /* autodetected value used in snd_hda_query_supported_pcm */
3384 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
3385 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
3386 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
3387 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
3388 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
3389 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
3390 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
3391 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
3392 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
3393 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
3394 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
3395 #define AC_PAR_PCM_RATE_BITS 11
3396 /* up to bits 10, 384kHZ isn't supported properly */
3398 /* not autodetected value */
3399 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
3401 { 0 } /* terminator */
3405 * snd_hda_calc_stream_format - calculate format bitset
3406 * @rate: the sample rate
3407 * @channels: the number of channels
3408 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3409 * @maxbps: the max. bps
3411 * Calculate the format bitset from the given rate, channels and th PCM format.
3413 * Return zero if invalid.
3415 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
3416 unsigned int channels
,
3417 unsigned int format
,
3418 unsigned int maxbps
,
3419 unsigned short spdif_ctls
)
3422 unsigned int val
= 0;
3424 for (i
= 0; rate_bits
[i
].hz
; i
++)
3425 if (rate_bits
[i
].hz
== rate
) {
3426 val
= rate_bits
[i
].hda_fmt
;
3429 if (!rate_bits
[i
].hz
) {
3430 snd_printdd("invalid rate %d\n", rate
);
3434 if (channels
== 0 || channels
> 8) {
3435 snd_printdd("invalid channels %d\n", channels
);
3438 val
|= channels
- 1;
3440 switch (snd_pcm_format_width(format
)) {
3442 val
|= AC_FMT_BITS_8
;
3445 val
|= AC_FMT_BITS_16
;
3450 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
3451 val
|= AC_FMT_BITS_32
;
3452 else if (maxbps
>= 24)
3453 val
|= AC_FMT_BITS_24
;
3455 val
|= AC_FMT_BITS_20
;
3458 snd_printdd("invalid format width %d\n",
3459 snd_pcm_format_width(format
));
3463 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
3464 val
|= AC_FMT_TYPE_NON_PCM
;
3468 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
3470 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3472 unsigned int val
= 0;
3473 if (nid
!= codec
->afg
&&
3474 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
3475 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
3476 if (!val
|| val
== -1)
3477 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
3478 if (!val
|| val
== -1)
3483 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3485 return query_caps_hash(codec
, nid
, HDA_HASH_PARPCM_KEY(nid
),
3489 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3491 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
3492 if (!streams
|| streams
== -1)
3493 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
3494 if (!streams
|| streams
== -1)
3499 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3501 return query_caps_hash(codec
, nid
, HDA_HASH_PARSTR_KEY(nid
),
3506 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3507 * @codec: the HDA codec
3508 * @nid: NID to query
3509 * @ratesp: the pointer to store the detected rate bitflags
3510 * @formatsp: the pointer to store the detected formats
3511 * @bpsp: the pointer to store the detected format widths
3513 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3514 * or @bsps argument is ignored.
3516 * Returns 0 if successful, otherwise a negative error code.
3518 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
3519 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
3521 unsigned int i
, val
, wcaps
;
3523 wcaps
= get_wcaps(codec
, nid
);
3524 val
= query_pcm_param(codec
, nid
);
3528 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
3530 rates
|= rate_bits
[i
].alsa_bits
;
3533 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
3534 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3536 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
3542 if (formatsp
|| bpsp
) {
3544 unsigned int streams
, bps
;
3546 streams
= query_stream_param(codec
, nid
);
3551 if (streams
& AC_SUPFMT_PCM
) {
3552 if (val
& AC_SUPPCM_BITS_8
) {
3553 formats
|= SNDRV_PCM_FMTBIT_U8
;
3556 if (val
& AC_SUPPCM_BITS_16
) {
3557 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
3560 if (wcaps
& AC_WCAP_DIGITAL
) {
3561 if (val
& AC_SUPPCM_BITS_32
)
3562 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
3563 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
3564 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3565 if (val
& AC_SUPPCM_BITS_24
)
3567 else if (val
& AC_SUPPCM_BITS_20
)
3569 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
3570 AC_SUPPCM_BITS_32
)) {
3571 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3572 if (val
& AC_SUPPCM_BITS_32
)
3574 else if (val
& AC_SUPPCM_BITS_24
)
3576 else if (val
& AC_SUPPCM_BITS_20
)
3580 if (streams
& AC_SUPFMT_FLOAT32
) {
3581 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
3585 if (streams
== AC_SUPFMT_AC3
) {
3586 /* should be exclusive */
3587 /* temporary hack: we have still no proper support
3588 * for the direct AC3 stream...
3590 formats
|= SNDRV_PCM_FMTBIT_U8
;
3594 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
3595 "(nid=0x%x, val=0x%x, ovrd=%i, "
3598 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
3603 *formatsp
= formats
;
3610 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm
);
3613 * snd_hda_is_supported_format - Check the validity of the format
3614 * @codec: HD-audio codec
3615 * @nid: NID to check
3616 * @format: the HD-audio format value to check
3618 * Check whether the given node supports the format value.
3620 * Returns 1 if supported, 0 if not.
3622 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
3623 unsigned int format
)
3626 unsigned int val
= 0, rate
, stream
;
3628 val
= query_pcm_param(codec
, nid
);
3632 rate
= format
& 0xff00;
3633 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
3634 if (rate_bits
[i
].hda_fmt
== rate
) {
3639 if (i
>= AC_PAR_PCM_RATE_BITS
)
3642 stream
= query_stream_param(codec
, nid
);
3646 if (stream
& AC_SUPFMT_PCM
) {
3647 switch (format
& 0xf0) {
3649 if (!(val
& AC_SUPPCM_BITS_8
))
3653 if (!(val
& AC_SUPPCM_BITS_16
))
3657 if (!(val
& AC_SUPPCM_BITS_20
))
3661 if (!(val
& AC_SUPPCM_BITS_24
))
3665 if (!(val
& AC_SUPPCM_BITS_32
))
3672 /* FIXME: check for float32 and AC3? */
3677 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
3682 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
3683 struct hda_codec
*codec
,
3684 struct snd_pcm_substream
*substream
)
3689 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
3690 struct hda_codec
*codec
,
3691 unsigned int stream_tag
,
3692 unsigned int format
,
3693 struct snd_pcm_substream
*substream
)
3695 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3699 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
3700 struct hda_codec
*codec
,
3701 struct snd_pcm_substream
*substream
)
3703 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3707 static int set_pcm_default_values(struct hda_codec
*codec
,
3708 struct hda_pcm_stream
*info
)
3712 /* query support PCM information from the given NID */
3713 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
3714 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
3715 info
->rates
? NULL
: &info
->rates
,
3716 info
->formats
? NULL
: &info
->formats
,
3717 info
->maxbps
? NULL
: &info
->maxbps
);
3721 if (info
->ops
.open
== NULL
)
3722 info
->ops
.open
= hda_pcm_default_open_close
;
3723 if (info
->ops
.close
== NULL
)
3724 info
->ops
.close
= hda_pcm_default_open_close
;
3725 if (info
->ops
.prepare
== NULL
) {
3726 if (snd_BUG_ON(!info
->nid
))
3728 info
->ops
.prepare
= hda_pcm_default_prepare
;
3730 if (info
->ops
.cleanup
== NULL
) {
3731 if (snd_BUG_ON(!info
->nid
))
3733 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
3739 * codec prepare/cleanup entries
3741 int snd_hda_codec_prepare(struct hda_codec
*codec
,
3742 struct hda_pcm_stream
*hinfo
,
3743 unsigned int stream
,
3744 unsigned int format
,
3745 struct snd_pcm_substream
*substream
)
3748 mutex_lock(&codec
->bus
->prepare_mutex
);
3749 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
, substream
);
3751 purify_inactive_streams(codec
);
3752 mutex_unlock(&codec
->bus
->prepare_mutex
);
3755 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare
);
3757 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
3758 struct hda_pcm_stream
*hinfo
,
3759 struct snd_pcm_substream
*substream
)
3761 mutex_lock(&codec
->bus
->prepare_mutex
);
3762 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
3763 mutex_unlock(&codec
->bus
->prepare_mutex
);
3765 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup
);
3768 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
3769 "Audio", "SPDIF", "HDMI", "Modem"
3773 * get the empty PCM device number to assign
3775 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3777 static int get_empty_pcm_device(struct hda_bus
*bus
, int type
)
3779 /* audio device indices; not linear to keep compatibility */
3780 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
3781 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
3782 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
3783 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
3784 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
3788 if (type
>= HDA_PCM_NTYPES
) {
3789 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
3793 for (i
= 0; audio_idx
[type
][i
] >= 0 ; i
++)
3794 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
3795 return audio_idx
[type
][i
];
3797 snd_printk(KERN_WARNING
"Too many %s devices\n",
3798 snd_hda_pcm_type_name
[type
]);
3803 * attach a new PCM stream
3805 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
3807 struct hda_bus
*bus
= codec
->bus
;
3808 struct hda_pcm_stream
*info
;
3811 if (snd_BUG_ON(!pcm
->name
))
3813 for (stream
= 0; stream
< 2; stream
++) {
3814 info
= &pcm
->stream
[stream
];
3815 if (info
->substreams
) {
3816 err
= set_pcm_default_values(codec
, info
);
3821 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
3824 /* assign all PCMs of the given codec */
3825 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
3830 if (!codec
->num_pcms
) {
3831 if (!codec
->patch_ops
.build_pcms
)
3833 err
= codec
->patch_ops
.build_pcms(codec
);
3835 printk(KERN_ERR
"hda_codec: cannot build PCMs"
3836 "for #%d (error %d)\n", codec
->addr
, err
);
3837 err
= snd_hda_codec_reset(codec
);
3840 "hda_codec: cannot revert codec\n");
3845 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
3846 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
3849 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
3850 continue; /* no substreams assigned */
3853 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
3855 continue; /* no fatal error */
3857 err
= snd_hda_attach_pcm(codec
, cpcm
);
3859 printk(KERN_ERR
"hda_codec: cannot attach "
3860 "PCM stream %d for codec #%d\n",
3862 continue; /* no fatal error */
3870 * snd_hda_build_pcms - build PCM information
3873 * Create PCM information for each codec included in the bus.
3875 * The build_pcms codec patch is requested to set up codec->num_pcms and
3876 * codec->pcm_info properly. The array is referred by the top-level driver
3877 * to create its PCM instances.
3878 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3881 * At least, substreams, channels_min and channels_max must be filled for
3882 * each stream. substreams = 0 indicates that the stream doesn't exist.
3883 * When rates and/or formats are zero, the supported values are queried
3884 * from the given nid. The nid is used also by the default ops.prepare
3885 * and ops.cleanup callbacks.
3887 * The driver needs to call ops.open in its open callback. Similarly,
3888 * ops.close is supposed to be called in the close callback.
3889 * ops.prepare should be called in the prepare or hw_params callback
3890 * with the proper parameters for set up.
3891 * ops.cleanup should be called in hw_free for clean up of streams.
3893 * This function returns 0 if successful, or a negative error code.
3895 int __devinit
snd_hda_build_pcms(struct hda_bus
*bus
)
3897 struct hda_codec
*codec
;
3899 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3900 int err
= snd_hda_codec_build_pcms(codec
);
3906 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
3909 * snd_hda_check_board_config - compare the current codec with the config table
3910 * @codec: the HDA codec
3911 * @num_configs: number of config enums
3912 * @models: array of model name strings
3913 * @tbl: configuration table, terminated by null entries
3915 * Compares the modelname or PCI subsystem id of the current codec with the
3916 * given configuration table. If a matching entry is found, returns its
3917 * config value (supposed to be 0 or positive).
3919 * If no entries are matching, the function returns a negative value.
3921 int snd_hda_check_board_config(struct hda_codec
*codec
,
3922 int num_configs
, const char * const *models
,
3923 const struct snd_pci_quirk
*tbl
)
3925 if (codec
->modelname
&& models
) {
3927 for (i
= 0; i
< num_configs
; i
++) {
3929 !strcmp(codec
->modelname
, models
[i
])) {
3930 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
3931 "selected\n", models
[i
]);
3937 if (!codec
->bus
->pci
|| !tbl
)
3940 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
3943 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
3944 #ifdef CONFIG_SND_DEBUG_VERBOSE
3946 const char *model
= NULL
;
3948 model
= models
[tbl
->value
];
3950 sprintf(tmp
, "#%d", tbl
->value
);
3953 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
3954 "for config %x:%x (%s)\n",
3955 model
, tbl
->subvendor
, tbl
->subdevice
,
3956 (tbl
->name
? tbl
->name
: "Unknown device"));
3962 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
3965 * snd_hda_check_board_codec_sid_config - compare the current codec
3966 subsystem ID with the
3969 This is important for Gateway notebooks with SB450 HDA Audio
3970 where the vendor ID of the PCI device is:
3971 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3972 and the vendor/subvendor are found only at the codec.
3974 * @codec: the HDA codec
3975 * @num_configs: number of config enums
3976 * @models: array of model name strings
3977 * @tbl: configuration table, terminated by null entries
3979 * Compares the modelname or PCI subsystem id of the current codec with the
3980 * given configuration table. If a matching entry is found, returns its
3981 * config value (supposed to be 0 or positive).
3983 * If no entries are matching, the function returns a negative value.
3985 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
3986 int num_configs
, const char * const *models
,
3987 const struct snd_pci_quirk
*tbl
)
3989 const struct snd_pci_quirk
*q
;
3991 /* Search for codec ID */
3992 for (q
= tbl
; q
->subvendor
; q
++) {
3993 unsigned long vendorid
= (q
->subdevice
) | (q
->subvendor
<< 16);
3995 if (vendorid
== codec
->subsystem_id
)
4004 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4005 #ifdef CONFIG_SND_DEBUG_VERBOSE
4007 const char *model
= NULL
;
4009 model
= models
[tbl
->value
];
4011 sprintf(tmp
, "#%d", tbl
->value
);
4014 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4015 "for config %x:%x (%s)\n",
4016 model
, tbl
->subvendor
, tbl
->subdevice
,
4017 (tbl
->name
? tbl
->name
: "Unknown device"));
4023 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
4026 * snd_hda_add_new_ctls - create controls from the array
4027 * @codec: the HDA codec
4028 * @knew: the array of struct snd_kcontrol_new
4030 * This helper function creates and add new controls in the given array.
4031 * The array must be terminated with an empty entry as terminator.
4033 * Returns 0 if successful, or a negative error code.
4035 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4036 const struct snd_kcontrol_new
*knew
)
4040 for (; knew
->name
; knew
++) {
4041 struct snd_kcontrol
*kctl
;
4042 int addr
= 0, idx
= 0;
4043 if (knew
->iface
== -1) /* skip this codec private value */
4046 kctl
= snd_ctl_new1(knew
, codec
);
4050 kctl
->id
.device
= addr
;
4052 kctl
->id
.index
= idx
;
4053 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4056 /* try first with another device index corresponding to
4057 * the codec addr; if it still fails (or it's the
4058 * primary codec), then try another control index
4060 if (!addr
&& codec
->addr
)
4062 else if (!idx
&& !knew
->index
) {
4063 idx
= find_empty_mixer_ctl_idx(codec
,
4073 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
4075 #ifdef CONFIG_SND_HDA_POWER_SAVE
4076 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
4077 unsigned int power_state
);
4079 static void hda_power_work(struct work_struct
*work
)
4081 struct hda_codec
*codec
=
4082 container_of(work
, struct hda_codec
, power_work
.work
);
4083 struct hda_bus
*bus
= codec
->bus
;
4085 if (!codec
->power_on
|| codec
->power_count
) {
4086 codec
->power_transition
= 0;
4090 hda_call_codec_suspend(codec
);
4091 if (bus
->ops
.pm_notify
)
4092 bus
->ops
.pm_notify(bus
);
4095 static void hda_keep_power_on(struct hda_codec
*codec
)
4097 codec
->power_count
++;
4098 codec
->power_on
= 1;
4099 codec
->power_jiffies
= jiffies
;
4102 /* update the power on/off account with the current jiffies */
4103 void snd_hda_update_power_acct(struct hda_codec
*codec
)
4105 unsigned long delta
= jiffies
- codec
->power_jiffies
;
4106 if (codec
->power_on
)
4107 codec
->power_on_acct
+= delta
;
4109 codec
->power_off_acct
+= delta
;
4110 codec
->power_jiffies
+= delta
;
4114 * snd_hda_power_up - Power-up the codec
4115 * @codec: HD-audio codec
4117 * Increment the power-up counter and power up the hardware really when
4118 * not turned on yet.
4120 void snd_hda_power_up(struct hda_codec
*codec
)
4122 struct hda_bus
*bus
= codec
->bus
;
4124 codec
->power_count
++;
4125 if (codec
->power_on
|| codec
->power_transition
)
4128 snd_hda_update_power_acct(codec
);
4129 codec
->power_on
= 1;
4130 codec
->power_jiffies
= jiffies
;
4131 if (bus
->ops
.pm_notify
)
4132 bus
->ops
.pm_notify(bus
);
4133 hda_call_codec_resume(codec
);
4134 cancel_delayed_work(&codec
->power_work
);
4135 codec
->power_transition
= 0;
4137 EXPORT_SYMBOL_HDA(snd_hda_power_up
);
4139 #define power_save(codec) \
4140 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4143 * snd_hda_power_down - Power-down the codec
4144 * @codec: HD-audio codec
4146 * Decrement the power-up counter and schedules the power-off work if
4147 * the counter rearches to zero.
4149 void snd_hda_power_down(struct hda_codec
*codec
)
4151 --codec
->power_count
;
4152 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
4154 if (power_save(codec
)) {
4155 codec
->power_transition
= 1; /* avoid reentrance */
4156 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
4157 msecs_to_jiffies(power_save(codec
) * 1000));
4160 EXPORT_SYMBOL_HDA(snd_hda_power_down
);
4163 * snd_hda_check_amp_list_power - Check the amp list and update the power
4164 * @codec: HD-audio codec
4165 * @check: the object containing an AMP list and the status
4166 * @nid: NID to check / update
4168 * Check whether the given NID is in the amp list. If it's in the list,
4169 * check the current AMP status, and update the the power-status according
4170 * to the mute status.
4172 * This function is supposed to be set or called from the check_power_status
4175 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4176 struct hda_loopback_check
*check
,
4179 const struct hda_amp_list
*p
;
4182 if (!check
->amplist
)
4184 for (p
= check
->amplist
; p
->nid
; p
++) {
4189 return 0; /* nothing changed */
4191 for (p
= check
->amplist
; p
->nid
; p
++) {
4192 for (ch
= 0; ch
< 2; ch
++) {
4193 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
4195 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
4196 if (!check
->power_on
) {
4197 check
->power_on
= 1;
4198 snd_hda_power_up(codec
);
4204 if (check
->power_on
) {
4205 check
->power_on
= 0;
4206 snd_hda_power_down(codec
);
4210 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
4214 * Channel mode helper
4218 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4220 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
4221 struct snd_ctl_elem_info
*uinfo
,
4222 const struct hda_channel_mode
*chmode
,
4225 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4227 uinfo
->value
.enumerated
.items
= num_chmodes
;
4228 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
4229 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
4230 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
4231 chmode
[uinfo
->value
.enumerated
.item
].channels
);
4234 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
4237 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4239 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
4240 struct snd_ctl_elem_value
*ucontrol
,
4241 const struct hda_channel_mode
*chmode
,
4247 for (i
= 0; i
< num_chmodes
; i
++) {
4248 if (max_channels
== chmode
[i
].channels
) {
4249 ucontrol
->value
.enumerated
.item
[0] = i
;
4255 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
4258 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4260 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
4261 struct snd_ctl_elem_value
*ucontrol
,
4262 const struct hda_channel_mode
*chmode
,
4268 mode
= ucontrol
->value
.enumerated
.item
[0];
4269 if (mode
>= num_chmodes
)
4271 if (*max_channelsp
== chmode
[mode
].channels
)
4273 /* change the current channel setting */
4274 *max_channelsp
= chmode
[mode
].channels
;
4275 if (chmode
[mode
].sequence
)
4276 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
4279 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
4286 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4288 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
4289 struct snd_ctl_elem_info
*uinfo
)
4293 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4295 uinfo
->value
.enumerated
.items
= imux
->num_items
;
4296 if (!imux
->num_items
)
4298 index
= uinfo
->value
.enumerated
.item
;
4299 if (index
>= imux
->num_items
)
4300 index
= imux
->num_items
- 1;
4301 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
4304 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
4307 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4309 int snd_hda_input_mux_put(struct hda_codec
*codec
,
4310 const struct hda_input_mux
*imux
,
4311 struct snd_ctl_elem_value
*ucontrol
,
4313 unsigned int *cur_val
)
4317 if (!imux
->num_items
)
4319 idx
= ucontrol
->value
.enumerated
.item
[0];
4320 if (idx
>= imux
->num_items
)
4321 idx
= imux
->num_items
- 1;
4322 if (*cur_val
== idx
)
4324 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
4325 imux
->items
[idx
].index
);
4329 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
4333 * Multi-channel / digital-out PCM helper functions
4336 /* setup SPDIF output stream */
4337 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
4338 unsigned int stream_tag
, unsigned int format
)
4340 struct hda_spdif_out
*spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
4342 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4343 if (codec
->spdif_status_reset
&& (spdif
->ctls
& AC_DIG1_ENABLE
))
4344 set_dig_out_convert(codec
, nid
,
4345 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
4347 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
4348 if (codec
->slave_dig_outs
) {
4350 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4351 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
4354 /* turn on again (if needed) */
4355 if (codec
->spdif_status_reset
&& (spdif
->ctls
& AC_DIG1_ENABLE
))
4356 set_dig_out_convert(codec
, nid
,
4357 spdif
->ctls
& 0xff, -1);
4360 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
4362 snd_hda_codec_cleanup_stream(codec
, nid
);
4363 if (codec
->slave_dig_outs
) {
4365 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4366 snd_hda_codec_cleanup_stream(codec
, *d
);
4371 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4372 * @bus: HD-audio bus
4374 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
4376 struct hda_codec
*codec
;
4380 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4381 if (hda_codec_is_power_on(codec
) &&
4382 codec
->patch_ops
.reboot_notify
)
4383 codec
->patch_ops
.reboot_notify(codec
);
4386 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify
);
4389 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4391 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
4392 struct hda_multi_out
*mout
)
4394 mutex_lock(&codec
->spdif_mutex
);
4395 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
4396 /* already opened as analog dup; reset it once */
4397 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4398 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
4399 mutex_unlock(&codec
->spdif_mutex
);
4402 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
4405 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4407 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
4408 struct hda_multi_out
*mout
,
4409 unsigned int stream_tag
,
4410 unsigned int format
,
4411 struct snd_pcm_substream
*substream
)
4413 mutex_lock(&codec
->spdif_mutex
);
4414 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
4415 mutex_unlock(&codec
->spdif_mutex
);
4418 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
4421 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4423 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
4424 struct hda_multi_out
*mout
)
4426 mutex_lock(&codec
->spdif_mutex
);
4427 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4428 mutex_unlock(&codec
->spdif_mutex
);
4431 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
4434 * snd_hda_multi_out_dig_close - release the digital out stream
4436 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
4437 struct hda_multi_out
*mout
)
4439 mutex_lock(&codec
->spdif_mutex
);
4440 mout
->dig_out_used
= 0;
4441 mutex_unlock(&codec
->spdif_mutex
);
4444 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
4447 * snd_hda_multi_out_analog_open - open analog outputs
4449 * Open analog outputs and set up the hw-constraints.
4450 * If the digital outputs can be opened as slave, open the digital
4453 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
4454 struct hda_multi_out
*mout
,
4455 struct snd_pcm_substream
*substream
,
4456 struct hda_pcm_stream
*hinfo
)
4458 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
4459 runtime
->hw
.channels_max
= mout
->max_channels
;
4460 if (mout
->dig_out_nid
) {
4461 if (!mout
->analog_rates
) {
4462 mout
->analog_rates
= hinfo
->rates
;
4463 mout
->analog_formats
= hinfo
->formats
;
4464 mout
->analog_maxbps
= hinfo
->maxbps
;
4466 runtime
->hw
.rates
= mout
->analog_rates
;
4467 runtime
->hw
.formats
= mout
->analog_formats
;
4468 hinfo
->maxbps
= mout
->analog_maxbps
;
4470 if (!mout
->spdif_rates
) {
4471 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
4473 &mout
->spdif_formats
,
4474 &mout
->spdif_maxbps
);
4476 mutex_lock(&codec
->spdif_mutex
);
4477 if (mout
->share_spdif
) {
4478 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
4479 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
4480 runtime
->hw
.rates
&= mout
->spdif_rates
;
4481 runtime
->hw
.formats
&= mout
->spdif_formats
;
4482 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
4483 hinfo
->maxbps
= mout
->spdif_maxbps
;
4485 mout
->share_spdif
= 0;
4486 /* FIXME: need notify? */
4489 mutex_unlock(&codec
->spdif_mutex
);
4491 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
4492 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
4494 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
4497 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4499 * Set up the i/o for analog out.
4500 * When the digital out is available, copy the front out to digital out, too.
4502 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
4503 struct hda_multi_out
*mout
,
4504 unsigned int stream_tag
,
4505 unsigned int format
,
4506 struct snd_pcm_substream
*substream
)
4508 const hda_nid_t
*nids
= mout
->dac_nids
;
4509 int chs
= substream
->runtime
->channels
;
4510 struct hda_spdif_out
*spdif
=
4511 snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
4514 mutex_lock(&codec
->spdif_mutex
);
4515 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
4516 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
4518 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
4520 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
4521 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
4522 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
4523 stream_tag
, format
);
4525 mout
->dig_out_used
= 0;
4526 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4529 mutex_unlock(&codec
->spdif_mutex
);
4532 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
4534 if (!mout
->no_share_stream
&&
4535 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
4536 /* headphone out will just decode front left/right (stereo) */
4537 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
4539 /* extra outputs copied from front */
4540 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4541 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
4542 snd_hda_codec_setup_stream(codec
,
4543 mout
->extra_out_nid
[i
],
4544 stream_tag
, 0, format
);
4547 for (i
= 1; i
< mout
->num_dacs
; i
++) {
4548 if (chs
>= (i
+ 1) * 2) /* independent out */
4549 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4551 else if (!mout
->no_share_stream
) /* copy front */
4552 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4557 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
4560 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4562 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
4563 struct hda_multi_out
*mout
)
4565 const hda_nid_t
*nids
= mout
->dac_nids
;
4568 for (i
= 0; i
< mout
->num_dacs
; i
++)
4569 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
4571 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
4572 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4573 if (mout
->extra_out_nid
[i
])
4574 snd_hda_codec_cleanup_stream(codec
,
4575 mout
->extra_out_nid
[i
]);
4576 mutex_lock(&codec
->spdif_mutex
);
4577 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
4578 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4579 mout
->dig_out_used
= 0;
4581 mutex_unlock(&codec
->spdif_mutex
);
4584 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
4587 * Helper for automatic pin configuration
4590 static int is_in_nid_list(hda_nid_t nid
, const hda_nid_t
*list
)
4592 for (; *list
; list
++)
4600 * Sort an associated group of pins according to their sequence numbers.
4602 static void sort_pins_by_sequence(hda_nid_t
*pins
, short *sequences
,
4609 for (i
= 0; i
< num_pins
; i
++) {
4610 for (j
= i
+ 1; j
< num_pins
; j
++) {
4611 if (sequences
[i
] > sequences
[j
]) {
4613 sequences
[i
] = sequences
[j
];
4624 /* add the found input-pin to the cfg->inputs[] table */
4625 static void add_auto_cfg_input_pin(struct auto_pin_cfg
*cfg
, hda_nid_t nid
,
4628 if (cfg
->num_inputs
< AUTO_CFG_MAX_INS
) {
4629 cfg
->inputs
[cfg
->num_inputs
].pin
= nid
;
4630 cfg
->inputs
[cfg
->num_inputs
].type
= type
;
4635 /* sort inputs in the order of AUTO_PIN_* type */
4636 static void sort_autocfg_input_pins(struct auto_pin_cfg
*cfg
)
4640 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4641 for (j
= i
+ 1; j
< cfg
->num_inputs
; j
++) {
4642 if (cfg
->inputs
[i
].type
> cfg
->inputs
[j
].type
) {
4643 struct auto_pin_cfg_item tmp
;
4644 tmp
= cfg
->inputs
[i
];
4645 cfg
->inputs
[i
] = cfg
->inputs
[j
];
4646 cfg
->inputs
[j
] = tmp
;
4653 * Parse all pin widgets and store the useful pin nids to cfg
4655 * The number of line-outs or any primary output is stored in line_outs,
4656 * and the corresponding output pins are assigned to line_out_pins[],
4657 * in the order of front, rear, CLFE, side, ...
4659 * If more extra outputs (speaker and headphone) are found, the pins are
4660 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4661 * is detected, one of speaker of HP pins is assigned as the primary
4662 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4663 * if any analog output exists.
4665 * The analog input pins are assigned to inputs array.
4666 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4669 int snd_hda_parse_pin_def_config(struct hda_codec
*codec
,
4670 struct auto_pin_cfg
*cfg
,
4671 const hda_nid_t
*ignore_nids
)
4673 hda_nid_t nid
, end_nid
;
4674 short seq
, assoc_line_out
, assoc_speaker
;
4675 short sequences_line_out
[ARRAY_SIZE(cfg
->line_out_pins
)];
4676 short sequences_speaker
[ARRAY_SIZE(cfg
->speaker_pins
)];
4677 short sequences_hp
[ARRAY_SIZE(cfg
->hp_pins
)];
4680 memset(cfg
, 0, sizeof(*cfg
));
4682 memset(sequences_line_out
, 0, sizeof(sequences_line_out
));
4683 memset(sequences_speaker
, 0, sizeof(sequences_speaker
));
4684 memset(sequences_hp
, 0, sizeof(sequences_hp
));
4685 assoc_line_out
= assoc_speaker
= 0;
4687 end_nid
= codec
->start_nid
+ codec
->num_nodes
;
4688 for (nid
= codec
->start_nid
; nid
< end_nid
; nid
++) {
4689 unsigned int wid_caps
= get_wcaps(codec
, nid
);
4690 unsigned int wid_type
= get_wcaps_type(wid_caps
);
4691 unsigned int def_conf
;
4692 short assoc
, loc
, conn
, dev
;
4694 /* read all default configuration for pin complex */
4695 if (wid_type
!= AC_WID_PIN
)
4697 /* ignore the given nids (e.g. pc-beep returns error) */
4698 if (ignore_nids
&& is_in_nid_list(nid
, ignore_nids
))
4701 def_conf
= snd_hda_codec_get_pincfg(codec
, nid
);
4702 conn
= get_defcfg_connect(def_conf
);
4703 if (conn
== AC_JACK_PORT_NONE
)
4705 loc
= get_defcfg_location(def_conf
);
4706 dev
= get_defcfg_device(def_conf
);
4708 /* workaround for buggy BIOS setups */
4709 if (dev
== AC_JACK_LINE_OUT
) {
4710 if (conn
== AC_JACK_PORT_FIXED
)
4711 dev
= AC_JACK_SPEAKER
;
4715 case AC_JACK_LINE_OUT
:
4716 seq
= get_defcfg_sequence(def_conf
);
4717 assoc
= get_defcfg_association(def_conf
);
4719 if (!(wid_caps
& AC_WCAP_STEREO
))
4720 if (!cfg
->mono_out_pin
)
4721 cfg
->mono_out_pin
= nid
;
4724 if (!assoc_line_out
)
4725 assoc_line_out
= assoc
;
4726 else if (assoc_line_out
!= assoc
)
4728 if (cfg
->line_outs
>= ARRAY_SIZE(cfg
->line_out_pins
))
4730 cfg
->line_out_pins
[cfg
->line_outs
] = nid
;
4731 sequences_line_out
[cfg
->line_outs
] = seq
;
4734 case AC_JACK_SPEAKER
:
4735 seq
= get_defcfg_sequence(def_conf
);
4736 assoc
= get_defcfg_association(def_conf
);
4740 assoc_speaker
= assoc
;
4741 else if (assoc_speaker
!= assoc
)
4743 if (cfg
->speaker_outs
>= ARRAY_SIZE(cfg
->speaker_pins
))
4745 cfg
->speaker_pins
[cfg
->speaker_outs
] = nid
;
4746 sequences_speaker
[cfg
->speaker_outs
] = seq
;
4747 cfg
->speaker_outs
++;
4749 case AC_JACK_HP_OUT
:
4750 seq
= get_defcfg_sequence(def_conf
);
4751 assoc
= get_defcfg_association(def_conf
);
4752 if (cfg
->hp_outs
>= ARRAY_SIZE(cfg
->hp_pins
))
4754 cfg
->hp_pins
[cfg
->hp_outs
] = nid
;
4755 sequences_hp
[cfg
->hp_outs
] = (assoc
<< 4) | seq
;
4758 case AC_JACK_MIC_IN
:
4759 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_MIC
);
4761 case AC_JACK_LINE_IN
:
4762 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_LINE_IN
);
4765 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_CD
);
4768 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_AUX
);
4770 case AC_JACK_SPDIF_OUT
:
4771 case AC_JACK_DIG_OTHER_OUT
:
4772 if (cfg
->dig_outs
>= ARRAY_SIZE(cfg
->dig_out_pins
))
4774 cfg
->dig_out_pins
[cfg
->dig_outs
] = nid
;
4775 cfg
->dig_out_type
[cfg
->dig_outs
] =
4776 (loc
== AC_JACK_LOC_HDMI
) ?
4777 HDA_PCM_TYPE_HDMI
: HDA_PCM_TYPE_SPDIF
;
4780 case AC_JACK_SPDIF_IN
:
4781 case AC_JACK_DIG_OTHER_IN
:
4782 cfg
->dig_in_pin
= nid
;
4783 if (loc
== AC_JACK_LOC_HDMI
)
4784 cfg
->dig_in_type
= HDA_PCM_TYPE_HDMI
;
4786 cfg
->dig_in_type
= HDA_PCM_TYPE_SPDIF
;
4792 * If no line-out is defined but multiple HPs are found,
4793 * some of them might be the real line-outs.
4795 if (!cfg
->line_outs
&& cfg
->hp_outs
> 1) {
4797 while (i
< cfg
->hp_outs
) {
4798 /* The real HPs should have the sequence 0x0f */
4799 if ((sequences_hp
[i
] & 0x0f) == 0x0f) {
4803 /* Move it to the line-out table */
4804 cfg
->line_out_pins
[cfg
->line_outs
] = cfg
->hp_pins
[i
];
4805 sequences_line_out
[cfg
->line_outs
] = sequences_hp
[i
];
4808 memmove(cfg
->hp_pins
+ i
, cfg
->hp_pins
+ i
+ 1,
4809 sizeof(cfg
->hp_pins
[0]) * (cfg
->hp_outs
- i
));
4810 memmove(sequences_hp
+ i
, sequences_hp
+ i
+ 1,
4811 sizeof(sequences_hp
[0]) * (cfg
->hp_outs
- i
));
4813 memset(cfg
->hp_pins
+ cfg
->hp_outs
, 0,
4814 sizeof(hda_nid_t
) * (AUTO_CFG_MAX_OUTS
- cfg
->hp_outs
));
4816 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4820 /* sort by sequence */
4821 sort_pins_by_sequence(cfg
->line_out_pins
, sequences_line_out
,
4823 sort_pins_by_sequence(cfg
->speaker_pins
, sequences_speaker
,
4825 sort_pins_by_sequence(cfg
->hp_pins
, sequences_hp
,
4829 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4830 * as a primary output
4832 if (!cfg
->line_outs
) {
4833 if (cfg
->speaker_outs
) {
4834 cfg
->line_outs
= cfg
->speaker_outs
;
4835 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
4836 sizeof(cfg
->speaker_pins
));
4837 cfg
->speaker_outs
= 0;
4838 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
4839 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
4840 } else if (cfg
->hp_outs
) {
4841 cfg
->line_outs
= cfg
->hp_outs
;
4842 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
4843 sizeof(cfg
->hp_pins
));
4845 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
4846 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4850 /* Reorder the surround channels
4851 * ALSA sequence is front/surr/clfe/side
4853 * 4-ch: front/surr => OK as it is
4854 * 6-ch: front/clfe/surr
4855 * 8-ch: front/clfe/rear/side|fc
4857 switch (cfg
->line_outs
) {
4860 nid
= cfg
->line_out_pins
[1];
4861 cfg
->line_out_pins
[1] = cfg
->line_out_pins
[2];
4862 cfg
->line_out_pins
[2] = nid
;
4866 sort_autocfg_input_pins(cfg
);
4869 * debug prints of the parsed results
4871 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
4872 cfg
->line_outs
, cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
4873 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
4874 cfg
->line_out_pins
[4],
4875 cfg
->line_out_type
== AUTO_PIN_HP_OUT
? "hp" :
4876 (cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
?
4877 "speaker" : "line"));
4878 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4879 cfg
->speaker_outs
, cfg
->speaker_pins
[0],
4880 cfg
->speaker_pins
[1], cfg
->speaker_pins
[2],
4881 cfg
->speaker_pins
[3], cfg
->speaker_pins
[4]);
4882 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4883 cfg
->hp_outs
, cfg
->hp_pins
[0],
4884 cfg
->hp_pins
[1], cfg
->hp_pins
[2],
4885 cfg
->hp_pins
[3], cfg
->hp_pins
[4]);
4886 snd_printd(" mono: mono_out=0x%x\n", cfg
->mono_out_pin
);
4888 snd_printd(" dig-out=0x%x/0x%x\n",
4889 cfg
->dig_out_pins
[0], cfg
->dig_out_pins
[1]);
4890 snd_printd(" inputs:");
4891 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4892 snd_printd(" %s=0x%x",
4893 hda_get_autocfg_input_label(codec
, cfg
, i
),
4894 cfg
->inputs
[i
].pin
);
4897 if (cfg
->dig_in_pin
)
4898 snd_printd(" dig-in=0x%x\n", cfg
->dig_in_pin
);
4902 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config
);
4904 int snd_hda_get_input_pin_attr(unsigned int def_conf
)
4906 unsigned int loc
= get_defcfg_location(def_conf
);
4907 unsigned int conn
= get_defcfg_connect(def_conf
);
4908 if (conn
== AC_JACK_PORT_NONE
)
4909 return INPUT_PIN_ATTR_UNUSED
;
4910 /* Windows may claim the internal mic to be BOTH, too */
4911 if (conn
== AC_JACK_PORT_FIXED
|| conn
== AC_JACK_PORT_BOTH
)
4912 return INPUT_PIN_ATTR_INT
;
4913 if ((loc
& 0x30) == AC_JACK_LOC_INTERNAL
)
4914 return INPUT_PIN_ATTR_INT
;
4915 if ((loc
& 0x30) == AC_JACK_LOC_SEPARATE
)
4916 return INPUT_PIN_ATTR_DOCK
;
4917 if (loc
== AC_JACK_LOC_REAR
)
4918 return INPUT_PIN_ATTR_REAR
;
4919 if (loc
== AC_JACK_LOC_FRONT
)
4920 return INPUT_PIN_ATTR_FRONT
;
4921 return INPUT_PIN_ATTR_NORMAL
;
4923 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr
);
4926 * hda_get_input_pin_label - Give a label for the given input pin
4928 * When check_location is true, the function checks the pin location
4929 * for mic and line-in pins, and set an appropriate prefix like "Front",
4930 * "Rear", "Internal".
4933 const char *hda_get_input_pin_label(struct hda_codec
*codec
, hda_nid_t pin
,
4936 unsigned int def_conf
;
4937 static const char * const mic_names
[] = {
4938 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4942 def_conf
= snd_hda_codec_get_pincfg(codec
, pin
);
4944 switch (get_defcfg_device(def_conf
)) {
4945 case AC_JACK_MIC_IN
:
4946 if (!check_location
)
4948 attr
= snd_hda_get_input_pin_attr(def_conf
);
4951 return mic_names
[attr
- 1];
4952 case AC_JACK_LINE_IN
:
4953 if (!check_location
)
4955 attr
= snd_hda_get_input_pin_attr(def_conf
);
4958 if (attr
== INPUT_PIN_ATTR_DOCK
)
4965 case AC_JACK_SPDIF_IN
:
4967 case AC_JACK_DIG_OTHER_IN
:
4968 return "Digital In";
4973 EXPORT_SYMBOL_HDA(hda_get_input_pin_label
);
4975 /* Check whether the location prefix needs to be added to the label.
4976 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4977 * have to put "Front" prefix to each label. In such a case, returns false.
4979 static int check_mic_location_need(struct hda_codec
*codec
,
4980 const struct auto_pin_cfg
*cfg
,
4986 defc
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[input
].pin
);
4987 attr
= snd_hda_get_input_pin_attr(defc
);
4988 /* for internal or docking mics, we need locations */
4989 if (attr
<= INPUT_PIN_ATTR_NORMAL
)
4993 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4994 defc
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[i
].pin
);
4995 attr2
= snd_hda_get_input_pin_attr(defc
);
4996 if (attr2
>= INPUT_PIN_ATTR_NORMAL
) {
4997 if (attr
&& attr
!= attr2
)
4998 return 1; /* different locations found */
5006 * hda_get_autocfg_input_label - Get a label for the given input
5008 * Get a label for the given input pin defined by the autocfg item.
5009 * Unlike hda_get_input_pin_label(), this function checks all inputs
5010 * defined in autocfg and avoids the redundant mic/line prefix as much as
5013 const char *hda_get_autocfg_input_label(struct hda_codec
*codec
,
5014 const struct auto_pin_cfg
*cfg
,
5017 int type
= cfg
->inputs
[input
].type
;
5018 int has_multiple_pins
= 0;
5020 if ((input
> 0 && cfg
->inputs
[input
- 1].type
== type
) ||
5021 (input
< cfg
->num_inputs
- 1 && cfg
->inputs
[input
+ 1].type
== type
))
5022 has_multiple_pins
= 1;
5023 if (has_multiple_pins
&& type
== AUTO_PIN_MIC
)
5024 has_multiple_pins
&= check_mic_location_need(codec
, cfg
, input
);
5025 return hda_get_input_pin_label(codec
, cfg
->inputs
[input
].pin
,
5028 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label
);
5031 * snd_hda_add_imux_item - Add an item to input_mux
5033 * When the same label is used already in the existing items, the number
5034 * suffix is appended to the label. This label index number is stored
5035 * to type_idx when non-NULL pointer is given.
5037 int snd_hda_add_imux_item(struct hda_input_mux
*imux
, const char *label
,
5038 int index
, int *type_idx
)
5040 int i
, label_idx
= 0;
5041 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
5042 snd_printd(KERN_ERR
"hda_codec: Too many imux items!\n");
5045 for (i
= 0; i
< imux
->num_items
; i
++) {
5046 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
5050 *type_idx
= label_idx
;
5052 snprintf(imux
->items
[imux
->num_items
].label
,
5053 sizeof(imux
->items
[imux
->num_items
].label
),
5054 "%s %d", label
, label_idx
);
5056 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5057 sizeof(imux
->items
[imux
->num_items
].label
));
5058 imux
->items
[imux
->num_items
].index
= index
;
5062 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item
);
5071 * snd_hda_suspend - suspend the codecs
5074 * Returns 0 if successful.
5076 int snd_hda_suspend(struct hda_bus
*bus
)
5078 struct hda_codec
*codec
;
5080 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5081 if (hda_codec_is_power_on(codec
))
5082 hda_call_codec_suspend(codec
);
5083 if (codec
->patch_ops
.post_suspend
)
5084 codec
->patch_ops
.post_suspend(codec
);
5088 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
5091 * snd_hda_resume - resume the codecs
5094 * Returns 0 if successful.
5096 * This function is defined only when POWER_SAVE isn't set.
5097 * In the power-save mode, the codec is resumed dynamically.
5099 int snd_hda_resume(struct hda_bus
*bus
)
5101 struct hda_codec
*codec
;
5103 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5104 if (codec
->patch_ops
.pre_resume
)
5105 codec
->patch_ops
.pre_resume(codec
);
5106 if (snd_hda_codec_needs_resume(codec
))
5107 hda_call_codec_resume(codec
);
5111 EXPORT_SYMBOL_HDA(snd_hda_resume
);
5112 #endif /* CONFIG_PM */
5119 * snd_array_new - get a new element from the given array
5120 * @array: the array object
5122 * Get a new element from the given array. If it exceeds the
5123 * pre-allocated array size, re-allocate the array.
5125 * Returns NULL if allocation failed.
5127 void *snd_array_new(struct snd_array
*array
)
5129 if (array
->used
>= array
->alloced
) {
5130 int num
= array
->alloced
+ array
->alloc_align
;
5131 int size
= (num
+ 1) * array
->elem_size
;
5132 int oldsize
= array
->alloced
* array
->elem_size
;
5134 if (snd_BUG_ON(num
>= 4096))
5136 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
);
5139 memset(nlist
+ oldsize
, 0, size
- oldsize
);
5140 array
->list
= nlist
;
5141 array
->alloced
= num
;
5143 return snd_array_elem(array
, array
->used
++);
5145 EXPORT_SYMBOL_HDA(snd_array_new
);
5148 * snd_array_free - free the given array elements
5149 * @array: the array object
5151 void snd_array_free(struct snd_array
*array
)
5158 EXPORT_SYMBOL_HDA(snd_array_free
);
5161 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5162 * @pcm: PCM caps bits
5163 * @buf: the string buffer to write
5164 * @buflen: the max buffer length
5166 * used by hda_proc.c and hda_eld.c
5168 void snd_print_pcm_rates(int pcm
, char *buf
, int buflen
)
5170 static unsigned int rates
[] = {
5171 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5172 96000, 176400, 192000, 384000
5176 for (i
= 0, j
= 0; i
< ARRAY_SIZE(rates
); i
++)
5178 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", rates
[i
]);
5180 buf
[j
] = '\0'; /* necessary when j == 0 */
5182 EXPORT_SYMBOL_HDA(snd_print_pcm_rates
);
5185 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5186 * @pcm: PCM caps bits
5187 * @buf: the string buffer to write
5188 * @buflen: the max buffer length
5190 * used by hda_proc.c and hda_eld.c
5192 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5194 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5197 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5198 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5199 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5201 buf
[j
] = '\0'; /* necessary when j == 0 */
5203 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
5205 #ifdef CONFIG_SND_HDA_INPUT_JACK
5207 * Input-jack notification support
5209 struct hda_jack_item
{
5212 struct snd_jack
*jack
;
5215 static const char *get_jack_default_name(struct hda_codec
*codec
, hda_nid_t nid
,
5219 case SND_JACK_HEADPHONE
:
5221 case SND_JACK_MICROPHONE
:
5223 case SND_JACK_LINEOUT
:
5225 case SND_JACK_HEADSET
:
5227 case SND_JACK_VIDEOOUT
:
5234 static void hda_free_jack_priv(struct snd_jack
*jack
)
5236 struct hda_jack_item
*jacks
= jack
->private_data
;
5241 int snd_hda_input_jack_add(struct hda_codec
*codec
, hda_nid_t nid
, int type
,
5244 struct hda_jack_item
*jack
;
5247 snd_array_init(&codec
->jacks
, sizeof(*jack
), 32);
5248 jack
= snd_array_new(&codec
->jacks
);
5255 name
= get_jack_default_name(codec
, nid
, type
);
5256 err
= snd_jack_new(codec
->bus
->card
, name
, type
, &jack
->jack
);
5261 jack
->jack
->private_data
= jack
;
5262 jack
->jack
->private_free
= hda_free_jack_priv
;
5265 EXPORT_SYMBOL_HDA(snd_hda_input_jack_add
);
5267 void snd_hda_input_jack_report(struct hda_codec
*codec
, hda_nid_t nid
)
5269 struct hda_jack_item
*jacks
= codec
->jacks
.list
;
5275 for (i
= 0; i
< codec
->jacks
.used
; i
++, jacks
++) {
5276 unsigned int pin_ctl
;
5277 unsigned int present
;
5280 if (jacks
->nid
!= nid
)
5282 present
= snd_hda_jack_detect(codec
, nid
);
5284 if (type
== (SND_JACK_HEADPHONE
| SND_JACK_LINEOUT
)) {
5285 pin_ctl
= snd_hda_codec_read(codec
, nid
, 0,
5286 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5287 type
= (pin_ctl
& AC_PINCTL_HP_EN
) ?
5288 SND_JACK_HEADPHONE
: SND_JACK_LINEOUT
;
5290 snd_jack_report(jacks
->jack
, present
? type
: 0);
5293 EXPORT_SYMBOL_HDA(snd_hda_input_jack_report
);
5295 /* free jack instances manually when clearing/reconfiguring */
5296 void snd_hda_input_jack_free(struct hda_codec
*codec
)
5298 if (!codec
->bus
->shutdown
&& codec
->jacks
.list
) {
5299 struct hda_jack_item
*jacks
= codec
->jacks
.list
;
5301 for (i
= 0; i
< codec
->jacks
.used
; i
++, jacks
++) {
5303 snd_device_free(codec
->bus
->card
, jacks
->jack
);
5306 snd_array_free(&codec
->jacks
);
5308 EXPORT_SYMBOL_HDA(snd_hda_input_jack_free
);
5309 #endif /* CONFIG_SND_HDA_INPUT_JACK */
5311 MODULE_DESCRIPTION("HDA codec core");
5312 MODULE_LICENSE("GPL");