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 "hda_local.h"
34 #include <sound/hda_hwdep.h>
37 * vendor / preset table
40 struct hda_vendor_id
{
45 /* codec vendor labels */
46 static struct hda_vendor_id hda_vendor_ids
[] = {
48 { 0x1013, "Cirrus Logic" },
49 { 0x1057, "Motorola" },
50 { 0x1095, "Silicon Image" },
52 { 0x10ec, "Realtek" },
53 { 0x1102, "Creative" },
57 { 0x11d4, "Analog Devices" },
58 { 0x13f6, "C-Media" },
59 { 0x14f1, "Conexant" },
60 { 0x17e8, "Chrontel" },
62 { 0x1aec, "Wolfson Microelectronics" },
63 { 0x434d, "C-Media" },
65 { 0x8384, "SigmaTel" },
69 static DEFINE_MUTEX(preset_mutex
);
70 static LIST_HEAD(hda_preset_tables
);
72 int snd_hda_add_codec_preset(struct hda_codec_preset_list
*preset
)
74 mutex_lock(&preset_mutex
);
75 list_add_tail(&preset
->list
, &hda_preset_tables
);
76 mutex_unlock(&preset_mutex
);
79 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset
);
81 int snd_hda_delete_codec_preset(struct hda_codec_preset_list
*preset
)
83 mutex_lock(&preset_mutex
);
84 list_del(&preset
->list
);
85 mutex_unlock(&preset_mutex
);
88 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset
);
90 #ifdef CONFIG_SND_HDA_POWER_SAVE
91 static void hda_power_work(struct work_struct
*work
);
92 static void hda_keep_power_on(struct hda_codec
*codec
);
94 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
98 * snd_hda_get_jack_location - Give a location string of the jack
99 * @cfg: pin default config value
101 * Parse the pin default config value and returns the string of the
102 * jack location, e.g. "Rear", "Front", etc.
104 const char *snd_hda_get_jack_location(u32 cfg
)
106 static char *bases
[7] = {
107 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 static unsigned char specials_idx
[] = {
114 static char *specials
[] = {
115 "Rear Panel", "Drive Bar",
116 "Riser", "HDMI", "ATAPI",
117 "Mobile-In", "Mobile-Out"
120 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
121 if ((cfg
& 0x0f) < 7)
122 return bases
[cfg
& 0x0f];
123 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
124 if (cfg
== specials_idx
[i
])
129 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location
);
132 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
133 * @cfg: pin default config value
135 * Parse the pin default config value and returns the string of the
136 * jack connectivity, i.e. external or internal connection.
138 const char *snd_hda_get_jack_connectivity(u32 cfg
)
140 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
142 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
144 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity
);
147 * snd_hda_get_jack_type - Give a type string of the jack
148 * @cfg: pin default config value
150 * Parse the pin default config value and returns the string of the
151 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 const char *snd_hda_get_jack_type(u32 cfg
)
155 static char *jack_types
[16] = {
156 "Line Out", "Speaker", "HP Out", "CD",
157 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
158 "Line In", "Aux", "Mic", "Telephony",
159 "SPDIF In", "Digitial In", "Reserved", "Other"
162 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
163 >> AC_DEFCFG_DEVICE_SHIFT
];
165 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type
);
168 * Compose a 32bit command word to be sent to the HD-audio controller
170 static inline unsigned int
171 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
172 unsigned int verb
, unsigned int parm
)
176 if ((codec
->addr
& ~0xf) || (direct
& ~1) || (nid
& ~0x7f) ||
177 (verb
& ~0xfff) || (parm
& ~0xffff)) {
178 printk(KERN_ERR
"hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
179 codec
->addr
, direct
, nid
, verb
, parm
);
183 val
= (u32
)codec
->addr
<< 28;
184 val
|= (u32
)direct
<< 27;
185 val
|= (u32
)nid
<< 20;
192 * Send and receive a verb
194 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
197 struct hda_bus
*bus
= codec
->bus
;
206 snd_hda_power_up(codec
);
207 mutex_lock(&bus
->cmd_mutex
);
208 err
= bus
->ops
.command(bus
, cmd
);
210 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
211 mutex_unlock(&bus
->cmd_mutex
);
212 snd_hda_power_down(codec
);
213 if (res
&& *res
== -1 && bus
->rirb_error
) {
214 if (bus
->response_reset
) {
215 snd_printd("hda_codec: resetting BUS due to "
216 "fatal communication error\n");
217 bus
->ops
.bus_reset(bus
);
221 /* clear reset-flag when the communication gets recovered */
223 bus
->response_reset
= 0;
228 * snd_hda_codec_read - send a command and get the response
229 * @codec: the HDA codec
230 * @nid: NID to send the command
231 * @direct: direct flag
232 * @verb: the verb to send
233 * @parm: the parameter for the verb
235 * Send a single command and read the corresponding response.
237 * Returns the obtained response value, or -1 for an error.
239 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
241 unsigned int verb
, unsigned int parm
)
243 unsigned cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
245 codec_exec_verb(codec
, cmd
, &res
);
248 EXPORT_SYMBOL_HDA(snd_hda_codec_read
);
251 * snd_hda_codec_write - send a single command without waiting for response
252 * @codec: the HDA codec
253 * @nid: NID to send the command
254 * @direct: direct flag
255 * @verb: the verb to send
256 * @parm: the parameter for the verb
258 * Send a single command without waiting for response.
260 * Returns 0 if successful, or a negative error code.
262 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
263 unsigned int verb
, unsigned int parm
)
265 unsigned int cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
267 return codec_exec_verb(codec
, cmd
,
268 codec
->bus
->sync_write
? &res
: NULL
);
270 EXPORT_SYMBOL_HDA(snd_hda_codec_write
);
273 * snd_hda_sequence_write - sequence writes
274 * @codec: the HDA codec
275 * @seq: VERB array to send
277 * Send the commands sequentially from the given array.
278 * The array must be terminated with NID=0.
280 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
282 for (; seq
->nid
; seq
++)
283 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
285 EXPORT_SYMBOL_HDA(snd_hda_sequence_write
);
288 * snd_hda_get_sub_nodes - get the range of sub nodes
289 * @codec: the HDA codec
291 * @start_id: the pointer to store the start NID
293 * Parse the NID and store the start NID of its sub-nodes.
294 * Returns the number of sub-nodes.
296 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
301 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
304 *start_id
= (parm
>> 16) & 0x7fff;
305 return (int)(parm
& 0x7fff);
307 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
310 * snd_hda_get_connections - get connection list
311 * @codec: the HDA codec
313 * @conn_list: connection list array
314 * @max_conns: max. number of connections to store
316 * Parses the connection list of the given widget and stores the list
319 * Returns the number of connections, or a negative error code.
321 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
322 hda_nid_t
*conn_list
, int max_conns
)
325 int i
, conn_len
, conns
;
326 unsigned int shift
, num_elems
, mask
;
330 if (snd_BUG_ON(!conn_list
|| max_conns
<= 0))
333 wcaps
= get_wcaps(codec
, nid
);
334 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
335 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
) {
336 snd_printk(KERN_WARNING
"hda_codec: "
337 "connection list not available for 0x%x\n", nid
);
341 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
342 if (parm
& AC_CLIST_LONG
) {
351 conn_len
= parm
& AC_CLIST_LENGTH
;
352 mask
= (1 << (shift
-1)) - 1;
355 return 0; /* no connection */
358 /* single connection */
359 parm
= snd_hda_codec_read(codec
, nid
, 0,
360 AC_VERB_GET_CONNECT_LIST
, 0);
361 if (parm
== -1 && codec
->bus
->rirb_error
)
363 conn_list
[0] = parm
& mask
;
367 /* multi connection */
370 for (i
= 0; i
< conn_len
; i
++) {
374 if (i
% num_elems
== 0) {
375 parm
= snd_hda_codec_read(codec
, nid
, 0,
376 AC_VERB_GET_CONNECT_LIST
, i
);
377 if (parm
== -1 && codec
->bus
->rirb_error
)
380 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
383 snd_printk(KERN_WARNING
"hda_codec: "
384 "invalid CONNECT_LIST verb %x[%i]:%x\n",
390 /* ranges between the previous and this one */
391 if (!prev_nid
|| prev_nid
>= val
) {
392 snd_printk(KERN_WARNING
"hda_codec: "
393 "invalid dep_range_val %x:%x\n",
397 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
398 if (conns
>= max_conns
) {
399 snd_printk(KERN_ERR
"hda_codec: "
400 "Too many connections %d for NID 0x%x\n",
404 conn_list
[conns
++] = n
;
407 if (conns
>= max_conns
) {
408 snd_printk(KERN_ERR
"hda_codec: "
409 "Too many connections %d for NID 0x%x\n",
413 conn_list
[conns
++] = val
;
419 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
423 * snd_hda_queue_unsol_event - add an unsolicited event to queue
425 * @res: unsolicited event (lower 32bit of RIRB entry)
426 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
428 * Adds the given event to the queue. The events are processed in
429 * the workqueue asynchronously. Call this function in the interrupt
430 * hanlder when RIRB receives an unsolicited event.
432 * Returns 0 if successful, or a negative error code.
434 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
436 struct hda_bus_unsolicited
*unsol
;
443 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
447 unsol
->queue
[wp
] = res
;
448 unsol
->queue
[wp
+ 1] = res_ex
;
450 queue_work(bus
->workq
, &unsol
->work
);
454 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event
);
457 * process queued unsolicited events
459 static void process_unsol_events(struct work_struct
*work
)
461 struct hda_bus_unsolicited
*unsol
=
462 container_of(work
, struct hda_bus_unsolicited
, work
);
463 struct hda_bus
*bus
= unsol
->bus
;
464 struct hda_codec
*codec
;
465 unsigned int rp
, caddr
, res
;
467 while (unsol
->rp
!= unsol
->wp
) {
468 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
471 res
= unsol
->queue
[rp
];
472 caddr
= unsol
->queue
[rp
+ 1];
473 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
475 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
476 if (codec
&& codec
->patch_ops
.unsol_event
)
477 codec
->patch_ops
.unsol_event(codec
, res
);
482 * initialize unsolicited queue
484 static int init_unsol_queue(struct hda_bus
*bus
)
486 struct hda_bus_unsolicited
*unsol
;
488 if (bus
->unsol
) /* already initialized */
491 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
493 snd_printk(KERN_ERR
"hda_codec: "
494 "can't allocate unsolicited queue\n");
497 INIT_WORK(&unsol
->work
, process_unsol_events
);
506 static void snd_hda_codec_free(struct hda_codec
*codec
);
508 static int snd_hda_bus_free(struct hda_bus
*bus
)
510 struct hda_codec
*codec
, *n
;
515 flush_workqueue(bus
->workq
);
518 list_for_each_entry_safe(codec
, n
, &bus
->codec_list
, list
) {
519 snd_hda_codec_free(codec
);
521 if (bus
->ops
.private_free
)
522 bus
->ops
.private_free(bus
);
524 destroy_workqueue(bus
->workq
);
529 static int snd_hda_bus_dev_free(struct snd_device
*device
)
531 struct hda_bus
*bus
= device
->device_data
;
533 return snd_hda_bus_free(bus
);
536 #ifdef CONFIG_SND_HDA_HWDEP
537 static int snd_hda_bus_dev_register(struct snd_device
*device
)
539 struct hda_bus
*bus
= device
->device_data
;
540 struct hda_codec
*codec
;
541 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
542 snd_hda_hwdep_add_sysfs(codec
);
543 snd_hda_hwdep_add_power_sysfs(codec
);
548 #define snd_hda_bus_dev_register NULL
552 * snd_hda_bus_new - create a HDA bus
553 * @card: the card entry
554 * @temp: the template for hda_bus information
555 * @busp: the pointer to store the created bus instance
557 * Returns 0 if successful, or a negative error code.
559 int /*__devinit*/ snd_hda_bus_new(struct snd_card
*card
,
560 const struct hda_bus_template
*temp
,
561 struct hda_bus
**busp
)
565 static struct snd_device_ops dev_ops
= {
566 .dev_register
= snd_hda_bus_dev_register
,
567 .dev_free
= snd_hda_bus_dev_free
,
570 if (snd_BUG_ON(!temp
))
572 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
578 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
580 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
585 bus
->private_data
= temp
->private_data
;
586 bus
->pci
= temp
->pci
;
587 bus
->modelname
= temp
->modelname
;
588 bus
->power_save
= temp
->power_save
;
589 bus
->ops
= temp
->ops
;
591 mutex_init(&bus
->cmd_mutex
);
592 INIT_LIST_HEAD(&bus
->codec_list
);
594 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
595 "hd-audio%d", card
->number
);
596 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
598 snd_printk(KERN_ERR
"cannot create workqueue %s\n",
604 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
606 snd_hda_bus_free(bus
);
613 EXPORT_SYMBOL_HDA(snd_hda_bus_new
);
615 #ifdef CONFIG_SND_HDA_GENERIC
616 #define is_generic_config(codec) \
617 (codec->modelname && !strcmp(codec->modelname, "generic"))
619 #define is_generic_config(codec) 0
623 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
625 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
629 * find a matching codec preset
631 static const struct hda_codec_preset
*
632 find_codec_preset(struct hda_codec
*codec
)
634 struct hda_codec_preset_list
*tbl
;
635 const struct hda_codec_preset
*preset
;
636 int mod_requested
= 0;
638 if (is_generic_config(codec
))
639 return NULL
; /* use the generic parser */
642 mutex_lock(&preset_mutex
);
643 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
644 if (!try_module_get(tbl
->owner
)) {
645 snd_printk(KERN_ERR
"hda_codec: cannot module_get\n");
648 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
649 u32 mask
= preset
->mask
;
650 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
652 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
656 if (preset
->id
== (codec
->vendor_id
& mask
) &&
658 preset
->rev
== codec
->revision_id
)) {
659 mutex_unlock(&preset_mutex
);
660 codec
->owner
= tbl
->owner
;
664 module_put(tbl
->owner
);
666 mutex_unlock(&preset_mutex
);
668 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
671 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%08x",
674 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%04x*",
675 (codec
->vendor_id
>> 16) & 0xffff);
676 request_module(name
);
684 * get_codec_name - store the codec name
686 static int get_codec_name(struct hda_codec
*codec
)
688 const struct hda_vendor_id
*c
;
689 const char *vendor
= NULL
;
690 u16 vendor_id
= codec
->vendor_id
>> 16;
693 if (codec
->vendor_name
)
696 for (c
= hda_vendor_ids
; c
->id
; c
++) {
697 if (c
->id
== vendor_id
) {
703 sprintf(tmp
, "Generic %04x", vendor_id
);
706 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
707 if (!codec
->vendor_name
)
711 if (codec
->chip_name
)
714 if (codec
->preset
&& codec
->preset
->name
)
715 codec
->chip_name
= kstrdup(codec
->preset
->name
, GFP_KERNEL
);
717 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
718 codec
->chip_name
= kstrdup(tmp
, GFP_KERNEL
);
720 if (!codec
->chip_name
)
726 * look for an AFG and MFG nodes
728 static void /*__devinit*/ setup_fg_nodes(struct hda_codec
*codec
)
730 int i
, total_nodes
, function_id
;
733 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
734 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
735 function_id
= snd_hda_param_read(codec
, nid
,
736 AC_PAR_FUNCTION_TYPE
) & 0xff;
737 switch (function_id
) {
738 case AC_GRP_AUDIO_FUNCTION
:
740 codec
->function_id
= function_id
;
742 case AC_GRP_MODEM_FUNCTION
:
744 codec
->function_id
= function_id
;
753 * read widget caps for each widget and store in cache
755 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
760 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
762 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
765 nid
= codec
->start_nid
;
766 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
767 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
768 AC_PAR_AUDIO_WIDGET_CAP
);
772 /* read all pin default configurations and save codec->init_pins */
773 static int read_pin_defaults(struct hda_codec
*codec
)
776 hda_nid_t nid
= codec
->start_nid
;
778 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
779 struct hda_pincfg
*pin
;
780 unsigned int wcaps
= get_wcaps(codec
, nid
);
781 unsigned int wid_type
= get_wcaps_type(wcaps
);
782 if (wid_type
!= AC_WID_PIN
)
784 pin
= snd_array_new(&codec
->init_pins
);
788 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
789 AC_VERB_GET_CONFIG_DEFAULT
, 0);
790 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
791 AC_VERB_GET_PIN_WIDGET_CONTROL
,
797 /* look up the given pin config list and return the item matching with NID */
798 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
799 struct snd_array
*array
,
803 for (i
= 0; i
< array
->used
; i
++) {
804 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
811 /* write a config value for the given NID */
812 static void set_pincfg(struct hda_codec
*codec
, hda_nid_t nid
,
816 for (i
= 0; i
< 4; i
++) {
817 snd_hda_codec_write(codec
, nid
, 0,
818 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0
+ i
,
824 /* set the current pin config value for the given NID.
825 * the value is cached, and read via snd_hda_codec_get_pincfg()
827 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
828 hda_nid_t nid
, unsigned int cfg
)
830 struct hda_pincfg
*pin
;
833 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_PIN
)
836 oldcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
837 pin
= look_up_pincfg(codec
, list
, nid
);
839 pin
= snd_array_new(list
);
846 /* change only when needed; e.g. if the pincfg is already present
847 * in user_pins[], don't write it
849 cfg
= snd_hda_codec_get_pincfg(codec
, nid
);
851 set_pincfg(codec
, nid
, cfg
);
856 * snd_hda_codec_set_pincfg - Override a pin default configuration
857 * @codec: the HDA codec
858 * @nid: NID to set the pin config
859 * @cfg: the pin default config value
861 * Override a pin default configuration value in the cache.
862 * This value can be read by snd_hda_codec_get_pincfg() in a higher
863 * priority than the real hardware value.
865 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
866 hda_nid_t nid
, unsigned int cfg
)
868 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
870 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg
);
873 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
874 * @codec: the HDA codec
875 * @nid: NID to get the pin config
877 * Get the current pin config value of the given pin NID.
878 * If the pincfg value is cached or overridden via sysfs or driver,
879 * returns the cached value.
881 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
883 struct hda_pincfg
*pin
;
885 #ifdef CONFIG_SND_HDA_HWDEP
886 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
890 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
893 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
898 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg
);
900 /* restore all current pin configs */
901 static void restore_pincfgs(struct hda_codec
*codec
)
904 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
905 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
906 set_pincfg(codec
, pin
->nid
,
907 snd_hda_codec_get_pincfg(codec
, pin
->nid
));
912 * snd_hda_shutup_pins - Shut up all pins
913 * @codec: the HDA codec
915 * Clear all pin controls to shup up before suspend for avoiding click noise.
916 * The controls aren't cached so that they can be resumed properly.
918 void snd_hda_shutup_pins(struct hda_codec
*codec
)
921 /* don't shut up pins when unloading the driver; otherwise it breaks
922 * the default pin setup at the next load of the driver
924 if (codec
->bus
->shutdown
)
926 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
927 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
928 /* use read here for syncing after issuing each verb */
929 snd_hda_codec_read(codec
, pin
->nid
, 0,
930 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
932 codec
->pins_shutup
= 1;
934 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins
);
936 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
937 static void restore_shutup_pins(struct hda_codec
*codec
)
940 if (!codec
->pins_shutup
)
942 if (codec
->bus
->shutdown
)
944 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
945 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
946 snd_hda_codec_write(codec
, pin
->nid
, 0,
947 AC_VERB_SET_PIN_WIDGET_CONTROL
,
950 codec
->pins_shutup
= 0;
953 static void init_hda_cache(struct hda_cache_rec
*cache
,
954 unsigned int record_size
);
955 static void free_hda_cache(struct hda_cache_rec
*cache
);
957 /* restore the initial pin cfgs and release all pincfg lists */
958 static void restore_init_pincfgs(struct hda_codec
*codec
)
960 /* first free driver_pins and user_pins, then call restore_pincfg
961 * so that only the values in init_pins are restored
963 snd_array_free(&codec
->driver_pins
);
964 #ifdef CONFIG_SND_HDA_HWDEP
965 snd_array_free(&codec
->user_pins
);
967 restore_pincfgs(codec
);
968 snd_array_free(&codec
->init_pins
);
974 static void snd_hda_codec_free(struct hda_codec
*codec
)
978 restore_init_pincfgs(codec
);
979 #ifdef CONFIG_SND_HDA_POWER_SAVE
980 cancel_delayed_work(&codec
->power_work
);
981 flush_workqueue(codec
->bus
->workq
);
983 list_del(&codec
->list
);
984 snd_array_free(&codec
->mixers
);
985 snd_array_free(&codec
->nids
);
986 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
987 if (codec
->patch_ops
.free
)
988 codec
->patch_ops
.free(codec
);
989 module_put(codec
->owner
);
990 free_hda_cache(&codec
->amp_cache
);
991 free_hda_cache(&codec
->cmd_cache
);
992 kfree(codec
->vendor_name
);
993 kfree(codec
->chip_name
);
994 kfree(codec
->modelname
);
999 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
1000 unsigned int power_state
);
1003 * snd_hda_codec_new - create a HDA codec
1004 * @bus: the bus to assign
1005 * @codec_addr: the codec address
1006 * @codecp: the pointer to store the generated codec
1008 * Returns 0 if successful, or a negative error code.
1010 int /*__devinit*/ snd_hda_codec_new(struct hda_bus
*bus
,
1011 unsigned int codec_addr
,
1012 struct hda_codec
**codecp
)
1014 struct hda_codec
*codec
;
1018 if (snd_BUG_ON(!bus
))
1020 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1023 if (bus
->caddr_tbl
[codec_addr
]) {
1024 snd_printk(KERN_ERR
"hda_codec: "
1025 "address 0x%x is already occupied\n", codec_addr
);
1029 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1030 if (codec
== NULL
) {
1031 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
1036 codec
->addr
= codec_addr
;
1037 mutex_init(&codec
->spdif_mutex
);
1038 mutex_init(&codec
->control_mutex
);
1039 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1040 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1041 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1042 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1043 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1044 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1045 if (codec
->bus
->modelname
) {
1046 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1047 if (!codec
->modelname
) {
1048 snd_hda_codec_free(codec
);
1053 #ifdef CONFIG_SND_HDA_POWER_SAVE
1054 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1055 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1056 * the caller has to power down appropriatley after initialization
1059 hda_keep_power_on(codec
);
1062 list_add_tail(&codec
->list
, &bus
->codec_list
);
1063 bus
->caddr_tbl
[codec_addr
] = codec
;
1065 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1067 if (codec
->vendor_id
== -1)
1068 /* read again, hopefully the access method was corrected
1069 * in the last read...
1071 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1073 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1074 AC_PAR_SUBSYSTEM_ID
);
1075 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1078 setup_fg_nodes(codec
);
1079 if (!codec
->afg
&& !codec
->mfg
) {
1080 snd_printdd("hda_codec: no AFG or MFG node found\n");
1085 err
= read_widget_caps(codec
, codec
->afg
? codec
->afg
: codec
->mfg
);
1087 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1090 err
= read_pin_defaults(codec
);
1094 if (!codec
->subsystem_id
) {
1095 hda_nid_t nid
= codec
->afg
? codec
->afg
: codec
->mfg
;
1096 codec
->subsystem_id
=
1097 snd_hda_codec_read(codec
, nid
, 0,
1098 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1101 /* power-up all before initialization */
1102 hda_set_power_state(codec
,
1103 codec
->afg
? codec
->afg
: codec
->mfg
,
1106 snd_hda_codec_proc_new(codec
);
1108 snd_hda_create_hwdep(codec
);
1110 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1111 codec
->subsystem_id
, codec
->revision_id
);
1112 snd_component_add(codec
->bus
->card
, component
);
1119 snd_hda_codec_free(codec
);
1122 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
1125 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1126 * @codec: the HDA codec
1128 * Start parsing of the given codec tree and (re-)initialize the whole
1131 * Returns 0 if successful or a negative error code.
1133 int snd_hda_codec_configure(struct hda_codec
*codec
)
1137 codec
->preset
= find_codec_preset(codec
);
1138 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1139 err
= get_codec_name(codec
);
1144 if (is_generic_config(codec
)) {
1145 err
= snd_hda_parse_generic_codec(codec
);
1148 if (codec
->preset
&& codec
->preset
->patch
) {
1149 err
= codec
->preset
->patch(codec
);
1153 /* call the default parser */
1154 err
= snd_hda_parse_generic_codec(codec
);
1156 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1159 if (!err
&& codec
->patch_ops
.unsol_event
)
1160 err
= init_unsol_queue(codec
->bus
);
1161 /* audio codec should override the mixer name */
1162 if (!err
&& (codec
->afg
|| !*codec
->bus
->card
->mixername
))
1163 snprintf(codec
->bus
->card
->mixername
,
1164 sizeof(codec
->bus
->card
->mixername
),
1165 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1168 EXPORT_SYMBOL_HDA(snd_hda_codec_configure
);
1171 * snd_hda_codec_setup_stream - set up the codec for streaming
1172 * @codec: the CODEC to set up
1173 * @nid: the NID to set up
1174 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1175 * @channel_id: channel id to pass, zero based.
1176 * @format: stream format.
1178 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1180 int channel_id
, int format
)
1185 snd_printdd("hda_codec_setup_stream: "
1186 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1187 nid
, stream_tag
, channel_id
, format
);
1188 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
,
1189 (stream_tag
<< 4) | channel_id
);
1191 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, format
);
1193 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1196 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1197 * @codec: the CODEC to clean up
1198 * @nid: the NID to clean up
1200 void snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
)
1205 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1206 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1207 #if 0 /* keep the format */
1209 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0);
1212 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream
);
1215 * amp access functions
1218 /* FIXME: more better hash key? */
1219 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1220 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1221 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1222 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1223 #define INFO_AMP_CAPS (1<<0)
1224 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1226 /* initialize the hash table */
1227 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec
*cache
,
1228 unsigned int record_size
)
1230 memset(cache
, 0, sizeof(*cache
));
1231 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1232 snd_array_init(&cache
->buf
, record_size
, 64);
1235 static void free_hda_cache(struct hda_cache_rec
*cache
)
1237 snd_array_free(&cache
->buf
);
1240 /* query the hash. allocate an entry if not found. */
1241 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1243 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1244 u16 cur
= cache
->hash
[idx
];
1245 struct hda_cache_head
*info
;
1247 while (cur
!= 0xffff) {
1248 info
= snd_array_elem(&cache
->buf
, cur
);
1249 if (info
->key
== key
)
1256 /* query the hash. allocate an entry if not found. */
1257 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1260 struct hda_cache_head
*info
= get_hash(cache
, key
);
1263 /* add a new hash entry */
1264 info
= snd_array_new(&cache
->buf
);
1267 cur
= snd_array_index(&cache
->buf
, info
);
1270 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1271 info
->next
= cache
->hash
[idx
];
1272 cache
->hash
[idx
] = cur
;
1277 /* query and allocate an amp hash entry */
1278 static inline struct hda_amp_info
*
1279 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1281 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1285 * query_amp_caps - query AMP capabilities
1286 * @codec: the HD-auio codec
1287 * @nid: the NID to query
1288 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1290 * Query AMP capabilities for the given widget and direction.
1291 * Returns the obtained capability bits.
1293 * When cap bits have been already read, this doesn't read again but
1294 * returns the cached value.
1296 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1298 struct hda_amp_info
*info
;
1300 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, 0));
1303 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1304 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1306 info
->amp_caps
= snd_hda_param_read(codec
, nid
,
1307 direction
== HDA_OUTPUT
?
1308 AC_PAR_AMP_OUT_CAP
:
1311 info
->head
.val
|= INFO_AMP_CAPS
;
1313 return info
->amp_caps
;
1315 EXPORT_SYMBOL_HDA(query_amp_caps
);
1318 * snd_hda_override_amp_caps - Override the AMP capabilities
1319 * @codec: the CODEC to clean up
1320 * @nid: the NID to clean up
1321 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1322 * @caps: the capability bits to set
1324 * Override the cached AMP caps bits value by the given one.
1325 * This function is useful if the driver needs to adjust the AMP ranges,
1326 * e.g. limit to 0dB, etc.
1328 * Returns zero if successful or a negative error code.
1330 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1333 struct hda_amp_info
*info
;
1335 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0));
1338 info
->amp_caps
= caps
;
1339 info
->head
.val
|= INFO_AMP_CAPS
;
1342 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1345 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, u32 key
,
1346 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
))
1348 struct hda_amp_info
*info
;
1350 info
= get_alloc_amp_hash(codec
, key
);
1353 if (!info
->head
.val
) {
1354 info
->head
.val
|= INFO_AMP_CAPS
;
1355 info
->amp_caps
= func(codec
, nid
);
1357 return info
->amp_caps
;
1360 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
)
1362 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1366 * snd_hda_query_pin_caps - Query PIN capabilities
1367 * @codec: the HD-auio codec
1368 * @nid: the NID to query
1370 * Query PIN capabilities for the given widget.
1371 * Returns the obtained capability bits.
1373 * When cap bits have been already read, this doesn't read again but
1374 * returns the cached value.
1376 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1378 return query_caps_hash(codec
, nid
, HDA_HASH_PINCAP_KEY(nid
),
1381 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1384 * snd_hda_pin_sense - execute pin sense measurement
1385 * @codec: the CODEC to sense
1386 * @nid: the pin NID to sense
1388 * Execute necessary pin sense measurement and return its Presence Detect,
1389 * Impedance, ELD Valid etc. status bits.
1391 u32
snd_hda_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
1395 if (!codec
->no_trigger_sense
) {
1396 pincap
= snd_hda_query_pin_caps(codec
, nid
);
1397 if (pincap
& AC_PINCAP_TRIG_REQ
) /* need trigger? */
1398 snd_hda_codec_read(codec
, nid
, 0,
1399 AC_VERB_SET_PIN_SENSE
, 0);
1401 return snd_hda_codec_read(codec
, nid
, 0,
1402 AC_VERB_GET_PIN_SENSE
, 0);
1404 EXPORT_SYMBOL_HDA(snd_hda_pin_sense
);
1407 * snd_hda_jack_detect - query pin Presence Detect status
1408 * @codec: the CODEC to sense
1409 * @nid: the pin NID to sense
1411 * Query and return the pin's Presence Detect status.
1413 int snd_hda_jack_detect(struct hda_codec
*codec
, hda_nid_t nid
)
1415 u32 sense
= snd_hda_pin_sense(codec
, nid
);
1416 return !!(sense
& AC_PINSENSE_PRESENCE
);
1418 EXPORT_SYMBOL_HDA(snd_hda_jack_detect
);
1421 * read the current volume to info
1422 * if the cache exists, read the cache value.
1424 static unsigned int get_vol_mute(struct hda_codec
*codec
,
1425 struct hda_amp_info
*info
, hda_nid_t nid
,
1426 int ch
, int direction
, int index
)
1430 if (info
->head
.val
& INFO_AMP_VOL(ch
))
1431 return info
->vol
[ch
];
1433 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1434 parm
|= direction
== HDA_OUTPUT
? AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1436 val
= snd_hda_codec_read(codec
, nid
, 0,
1437 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1438 info
->vol
[ch
] = val
& 0xff;
1439 info
->head
.val
|= INFO_AMP_VOL(ch
);
1440 return info
->vol
[ch
];
1444 * write the current volume in info to the h/w and update the cache
1446 static void put_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
1447 hda_nid_t nid
, int ch
, int direction
, int index
,
1452 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1453 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1454 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1456 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1457 info
->vol
[ch
] = val
;
1461 * snd_hda_codec_amp_read - Read AMP value
1462 * @codec: HD-audio codec
1463 * @nid: NID to read the AMP value
1464 * @ch: channel (left=0 or right=1)
1465 * @direction: #HDA_INPUT or #HDA_OUTPUT
1466 * @index: the index value (only for input direction)
1468 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1470 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1471 int direction
, int index
)
1473 struct hda_amp_info
*info
;
1474 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1477 return get_vol_mute(codec
, info
, nid
, ch
, direction
, index
);
1479 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
1482 * snd_hda_codec_amp_update - update the AMP value
1483 * @codec: HD-audio codec
1484 * @nid: NID to read the AMP value
1485 * @ch: channel (left=0 or right=1)
1486 * @direction: #HDA_INPUT or #HDA_OUTPUT
1487 * @idx: the index value (only for input direction)
1488 * @mask: bit mask to set
1489 * @val: the bits value to set
1491 * Update the AMP value with a bit mask.
1492 * Returns 0 if the value is unchanged, 1 if changed.
1494 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1495 int direction
, int idx
, int mask
, int val
)
1497 struct hda_amp_info
*info
;
1499 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, idx
));
1502 if (snd_BUG_ON(mask
& ~0xff))
1505 val
|= get_vol_mute(codec
, info
, nid
, ch
, direction
, idx
) & ~mask
;
1506 if (info
->vol
[ch
] == val
)
1508 put_vol_mute(codec
, info
, nid
, ch
, direction
, idx
, val
);
1511 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
1514 * snd_hda_codec_amp_stereo - update the AMP stereo values
1515 * @codec: HD-audio codec
1516 * @nid: NID to read the AMP value
1517 * @direction: #HDA_INPUT or #HDA_OUTPUT
1518 * @idx: the index value (only for input direction)
1519 * @mask: bit mask to set
1520 * @val: the bits value to set
1522 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1523 * stereo widget with the same mask and value.
1525 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1526 int direction
, int idx
, int mask
, int val
)
1530 if (snd_BUG_ON(mask
& ~0xff))
1532 for (ch
= 0; ch
< 2; ch
++)
1533 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1537 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
1539 #ifdef SND_HDA_NEEDS_RESUME
1541 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1542 * @codec: HD-audio codec
1544 * Resume the all amp commands from the cache.
1546 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
1548 struct hda_amp_info
*buffer
= codec
->amp_cache
.buf
.list
;
1551 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++, buffer
++) {
1552 u32 key
= buffer
->head
.key
;
1554 unsigned int idx
, dir
, ch
;
1558 idx
= (key
>> 16) & 0xff;
1559 dir
= (key
>> 24) & 0xff;
1560 for (ch
= 0; ch
< 2; ch
++) {
1561 if (!(buffer
->head
.val
& INFO_AMP_VOL(ch
)))
1563 put_vol_mute(codec
, buffer
, nid
, ch
, dir
, idx
,
1568 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
1569 #endif /* SND_HDA_NEEDS_RESUME */
1571 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1574 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1576 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1583 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1585 * The control element is supposed to have the private_value field
1586 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1588 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1589 struct snd_ctl_elem_info
*uinfo
)
1591 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1592 u16 nid
= get_amp_nid(kcontrol
);
1593 u8 chs
= get_amp_channels(kcontrol
);
1594 int dir
= get_amp_direction(kcontrol
);
1595 unsigned int ofs
= get_amp_offset(kcontrol
);
1597 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1598 uinfo
->count
= chs
== 3 ? 2 : 1;
1599 uinfo
->value
.integer
.min
= 0;
1600 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
1601 if (!uinfo
->value
.integer
.max
) {
1602 printk(KERN_WARNING
"hda_codec: "
1603 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
1609 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
1612 static inline unsigned int
1613 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1614 int ch
, int dir
, int idx
, unsigned int ofs
)
1617 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1618 val
&= HDA_AMP_VOLMASK
;
1627 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1628 int ch
, int dir
, int idx
, unsigned int ofs
,
1631 unsigned int maxval
;
1635 /* ofs = 0: raw max value */
1636 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
1639 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
1640 HDA_AMP_VOLMASK
, val
);
1644 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1646 * The control element is supposed to have the private_value field
1647 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1649 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
1650 struct snd_ctl_elem_value
*ucontrol
)
1652 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1653 hda_nid_t nid
= get_amp_nid(kcontrol
);
1654 int chs
= get_amp_channels(kcontrol
);
1655 int dir
= get_amp_direction(kcontrol
);
1656 int idx
= get_amp_index(kcontrol
);
1657 unsigned int ofs
= get_amp_offset(kcontrol
);
1658 long *valp
= ucontrol
->value
.integer
.value
;
1661 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
1663 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
1666 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
1669 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1671 * The control element is supposed to have the private_value field
1672 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1674 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
1675 struct snd_ctl_elem_value
*ucontrol
)
1677 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1678 hda_nid_t nid
= get_amp_nid(kcontrol
);
1679 int chs
= get_amp_channels(kcontrol
);
1680 int dir
= get_amp_direction(kcontrol
);
1681 int idx
= get_amp_index(kcontrol
);
1682 unsigned int ofs
= get_amp_offset(kcontrol
);
1683 long *valp
= ucontrol
->value
.integer
.value
;
1686 snd_hda_power_up(codec
);
1688 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
1692 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
1693 snd_hda_power_down(codec
);
1696 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
1699 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1701 * The control element is supposed to have the private_value field
1702 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1704 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
1705 unsigned int size
, unsigned int __user
*_tlv
)
1707 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1708 hda_nid_t nid
= get_amp_nid(kcontrol
);
1709 int dir
= get_amp_direction(kcontrol
);
1710 unsigned int ofs
= get_amp_offset(kcontrol
);
1711 u32 caps
, val1
, val2
;
1713 if (size
< 4 * sizeof(unsigned int))
1715 caps
= query_amp_caps(codec
, nid
, dir
);
1716 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1717 val2
= (val2
+ 1) * 25;
1718 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
1720 val1
= ((int)val1
) * ((int)val2
);
1721 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
1723 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
1725 if (put_user(val1
, _tlv
+ 2))
1727 if (put_user(val2
, _tlv
+ 3))
1731 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
1734 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1735 * @codec: HD-audio codec
1736 * @nid: NID of a reference widget
1737 * @dir: #HDA_INPUT or #HDA_OUTPUT
1738 * @tlv: TLV data to be stored, at least 4 elements
1740 * Set (static) TLV data for a virtual master volume using the AMP caps
1741 * obtained from the reference NID.
1742 * The volume range is recalculated as if the max volume is 0dB.
1744 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1750 caps
= query_amp_caps(codec
, nid
, dir
);
1751 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1752 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1753 step
= (step
+ 1) * 25;
1754 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
1755 tlv
[1] = 2 * sizeof(unsigned int);
1756 tlv
[2] = -nums
* step
;
1759 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
1761 /* find a mixer control element with the given name */
1762 static struct snd_kcontrol
*
1763 _snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
1764 const char *name
, int idx
)
1766 struct snd_ctl_elem_id id
;
1767 memset(&id
, 0, sizeof(id
));
1768 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1770 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
1772 strcpy(id
.name
, name
);
1773 return snd_ctl_find_id(codec
->bus
->card
, &id
);
1777 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1778 * @codec: HD-audio codec
1779 * @name: ctl id name string
1781 * Get the control element with the given id string and IFACE_MIXER.
1783 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
1786 return _snd_hda_find_mixer_ctl(codec
, name
, 0);
1788 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
1791 * snd_hda_ctl_add - Add a control element and assign to the codec
1792 * @codec: HD-audio codec
1793 * @nid: corresponding NID (optional)
1794 * @kctl: the control element to assign
1796 * Add the given control element to an array inside the codec instance.
1797 * All control elements belonging to a codec are supposed to be added
1798 * by this function so that a proper clean-up works at the free or
1799 * reconfiguration time.
1801 * If non-zero @nid is passed, the NID is assigned to the control element.
1802 * The assignment is shown in the codec proc file.
1804 * snd_hda_ctl_add() checks the control subdev id field whether
1805 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1806 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1807 * specifies if kctl->private_value is a HDA amplifier value.
1809 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
1810 struct snd_kcontrol
*kctl
)
1813 unsigned short flags
= 0;
1814 struct hda_nid_item
*item
;
1816 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
1817 flags
|= HDA_NID_ITEM_AMP
;
1819 nid
= get_amp_nid_(kctl
->private_value
);
1821 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
1822 nid
= kctl
->id
.subdevice
& 0xffff;
1823 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
1824 kctl
->id
.subdevice
= 0;
1825 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
1828 item
= snd_array_new(&codec
->mixers
);
1833 item
->flags
= flags
;
1836 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
1839 * snd_hda_add_nid - Assign a NID to a control element
1840 * @codec: HD-audio codec
1841 * @nid: corresponding NID (optional)
1842 * @kctl: the control element to assign
1843 * @index: index to kctl
1845 * Add the given control element to an array inside the codec instance.
1846 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1847 * NID:KCTL mapping - for example "Capture Source" selector.
1849 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
1850 unsigned int index
, hda_nid_t nid
)
1852 struct hda_nid_item
*item
;
1855 item
= snd_array_new(&codec
->nids
);
1859 item
->index
= index
;
1863 printk(KERN_ERR
"hda-codec: no NID for mapping control %s:%d:%d\n",
1864 kctl
->id
.name
, kctl
->id
.index
, index
);
1867 EXPORT_SYMBOL_HDA(snd_hda_add_nid
);
1870 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1871 * @codec: HD-audio codec
1873 void snd_hda_ctls_clear(struct hda_codec
*codec
)
1876 struct hda_nid_item
*items
= codec
->mixers
.list
;
1877 for (i
= 0; i
< codec
->mixers
.used
; i
++)
1878 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
1879 snd_array_free(&codec
->mixers
);
1880 snd_array_free(&codec
->nids
);
1883 /* pseudo device locking
1884 * toggle card->shutdown to allow/disallow the device access (as a hack)
1886 static int hda_lock_devices(struct snd_card
*card
)
1888 spin_lock(&card
->files_lock
);
1889 if (card
->shutdown
) {
1890 spin_unlock(&card
->files_lock
);
1894 spin_unlock(&card
->files_lock
);
1898 static void hda_unlock_devices(struct snd_card
*card
)
1900 spin_lock(&card
->files_lock
);
1902 spin_unlock(&card
->files_lock
);
1906 * snd_hda_codec_reset - Clear all objects assigned to the codec
1907 * @codec: HD-audio codec
1909 * This frees the all PCM and control elements assigned to the codec, and
1910 * clears the caches and restores the pin default configurations.
1912 * When a device is being used, it returns -EBSY. If successfully freed,
1915 int snd_hda_codec_reset(struct hda_codec
*codec
)
1917 struct snd_card
*card
= codec
->bus
->card
;
1920 if (hda_lock_devices(card
) < 0)
1922 /* check whether the codec isn't used by any mixer or PCM streams */
1923 if (!list_empty(&card
->ctl_files
)) {
1924 hda_unlock_devices(card
);
1927 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
1928 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
1931 if (cpcm
->pcm
->streams
[0].substream_opened
||
1932 cpcm
->pcm
->streams
[1].substream_opened
) {
1933 hda_unlock_devices(card
);
1938 /* OK, let it free */
1940 #ifdef CONFIG_SND_HDA_POWER_SAVE
1941 cancel_delayed_work(&codec
->power_work
);
1942 flush_workqueue(codec
->bus
->workq
);
1944 snd_hda_ctls_clear(codec
);
1946 for (i
= 0; i
< codec
->num_pcms
; i
++) {
1947 if (codec
->pcm_info
[i
].pcm
) {
1948 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
1949 clear_bit(codec
->pcm_info
[i
].device
,
1950 codec
->bus
->pcm_dev_bits
);
1953 if (codec
->patch_ops
.free
)
1954 codec
->patch_ops
.free(codec
);
1955 codec
->proc_widget_hook
= NULL
;
1957 free_hda_cache(&codec
->amp_cache
);
1958 free_hda_cache(&codec
->cmd_cache
);
1959 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1960 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1961 /* free only driver_pins so that init_pins + user_pins are restored */
1962 snd_array_free(&codec
->driver_pins
);
1963 restore_pincfgs(codec
);
1964 codec
->num_pcms
= 0;
1965 codec
->pcm_info
= NULL
;
1966 codec
->preset
= NULL
;
1967 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
1968 codec
->slave_dig_outs
= NULL
;
1969 codec
->spdif_status_reset
= 0;
1970 module_put(codec
->owner
);
1971 codec
->owner
= NULL
;
1973 /* allow device access again */
1974 hda_unlock_devices(card
);
1979 * snd_hda_add_vmaster - create a virtual master control and add slaves
1980 * @codec: HD-audio codec
1981 * @name: vmaster control name
1982 * @tlv: TLV data (optional)
1983 * @slaves: slave control names (optional)
1985 * Create a virtual master control with the given name. The TLV data
1986 * must be either NULL or a valid data.
1988 * @slaves is a NULL-terminated array of strings, each of which is a
1989 * slave control name. All controls with these names are assigned to
1990 * the new virtual master control.
1992 * This function returns zero if successful or a negative error code.
1994 int snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
1995 unsigned int *tlv
, const char **slaves
)
1997 struct snd_kcontrol
*kctl
;
2001 for (s
= slaves
; *s
&& !snd_hda_find_mixer_ctl(codec
, *s
); s
++)
2004 snd_printdd("No slave found for %s\n", name
);
2007 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2010 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2014 for (s
= slaves
; *s
; s
++) {
2015 struct snd_kcontrol
*sctl
;
2018 sctl
= _snd_hda_find_mixer_ctl(codec
, *s
, i
);
2021 snd_printdd("Cannot find slave %s, "
2025 err
= snd_ctl_add_slave(kctl
, sctl
);
2033 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster
);
2036 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2038 * The control element is supposed to have the private_value field
2039 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2041 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2042 struct snd_ctl_elem_info
*uinfo
)
2044 int chs
= get_amp_channels(kcontrol
);
2046 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2047 uinfo
->count
= chs
== 3 ? 2 : 1;
2048 uinfo
->value
.integer
.min
= 0;
2049 uinfo
->value
.integer
.max
= 1;
2052 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
2055 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2057 * The control element is supposed to have the private_value field
2058 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2060 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2061 struct snd_ctl_elem_value
*ucontrol
)
2063 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2064 hda_nid_t nid
= get_amp_nid(kcontrol
);
2065 int chs
= get_amp_channels(kcontrol
);
2066 int dir
= get_amp_direction(kcontrol
);
2067 int idx
= get_amp_index(kcontrol
);
2068 long *valp
= ucontrol
->value
.integer
.value
;
2071 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2072 HDA_AMP_MUTE
) ? 0 : 1;
2074 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2075 HDA_AMP_MUTE
) ? 0 : 1;
2078 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
2081 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2083 * The control element is supposed to have the private_value field
2084 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2086 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2087 struct snd_ctl_elem_value
*ucontrol
)
2089 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2090 hda_nid_t nid
= get_amp_nid(kcontrol
);
2091 int chs
= get_amp_channels(kcontrol
);
2092 int dir
= get_amp_direction(kcontrol
);
2093 int idx
= get_amp_index(kcontrol
);
2094 long *valp
= ucontrol
->value
.integer
.value
;
2097 snd_hda_power_up(codec
);
2099 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2101 *valp
? 0 : HDA_AMP_MUTE
);
2105 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2107 *valp
? 0 : HDA_AMP_MUTE
);
2108 #ifdef CONFIG_SND_HDA_POWER_SAVE
2109 if (codec
->patch_ops
.check_power_status
)
2110 codec
->patch_ops
.check_power_status(codec
, nid
);
2112 snd_hda_power_down(codec
);
2115 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
2117 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2119 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2121 * This function calls snd_hda_enable_beep_device(), which behaves differently
2122 * depending on beep_mode option.
2124 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol
*kcontrol
,
2125 struct snd_ctl_elem_value
*ucontrol
)
2127 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2128 long *valp
= ucontrol
->value
.integer
.value
;
2130 snd_hda_enable_beep_device(codec
, *valp
);
2131 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2133 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep
);
2134 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2137 * bound volume controls
2139 * bind multiple volumes (# indices, from 0)
2142 #define AMP_VAL_IDX_SHIFT 19
2143 #define AMP_VAL_IDX_MASK (0x0f<<19)
2146 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2148 * The control element is supposed to have the private_value field
2149 * set up via HDA_BIND_MUTE*() macros.
2151 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2152 struct snd_ctl_elem_value
*ucontrol
)
2154 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2158 mutex_lock(&codec
->control_mutex
);
2159 pval
= kcontrol
->private_value
;
2160 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2161 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2162 kcontrol
->private_value
= pval
;
2163 mutex_unlock(&codec
->control_mutex
);
2166 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
2169 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2171 * The control element is supposed to have the private_value field
2172 * set up via HDA_BIND_MUTE*() macros.
2174 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2175 struct snd_ctl_elem_value
*ucontrol
)
2177 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2179 int i
, indices
, err
= 0, change
= 0;
2181 mutex_lock(&codec
->control_mutex
);
2182 pval
= kcontrol
->private_value
;
2183 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2184 for (i
= 0; i
< indices
; i
++) {
2185 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2186 (i
<< AMP_VAL_IDX_SHIFT
);
2187 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2192 kcontrol
->private_value
= pval
;
2193 mutex_unlock(&codec
->control_mutex
);
2194 return err
< 0 ? err
: change
;
2196 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
2199 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2201 * The control element is supposed to have the private_value field
2202 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2204 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2205 struct snd_ctl_elem_info
*uinfo
)
2207 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2208 struct hda_bind_ctls
*c
;
2211 mutex_lock(&codec
->control_mutex
);
2212 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2213 kcontrol
->private_value
= *c
->values
;
2214 err
= c
->ops
->info(kcontrol
, uinfo
);
2215 kcontrol
->private_value
= (long)c
;
2216 mutex_unlock(&codec
->control_mutex
);
2219 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
2222 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2224 * The control element is supposed to have the private_value field
2225 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2227 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2228 struct snd_ctl_elem_value
*ucontrol
)
2230 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2231 struct hda_bind_ctls
*c
;
2234 mutex_lock(&codec
->control_mutex
);
2235 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2236 kcontrol
->private_value
= *c
->values
;
2237 err
= c
->ops
->get(kcontrol
, ucontrol
);
2238 kcontrol
->private_value
= (long)c
;
2239 mutex_unlock(&codec
->control_mutex
);
2242 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
2245 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2247 * The control element is supposed to have the private_value field
2248 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2250 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2251 struct snd_ctl_elem_value
*ucontrol
)
2253 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2254 struct hda_bind_ctls
*c
;
2255 unsigned long *vals
;
2256 int err
= 0, change
= 0;
2258 mutex_lock(&codec
->control_mutex
);
2259 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2260 for (vals
= c
->values
; *vals
; vals
++) {
2261 kcontrol
->private_value
= *vals
;
2262 err
= c
->ops
->put(kcontrol
, ucontrol
);
2267 kcontrol
->private_value
= (long)c
;
2268 mutex_unlock(&codec
->control_mutex
);
2269 return err
< 0 ? err
: change
;
2271 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
2274 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2276 * The control element is supposed to have the private_value field
2277 * set up via HDA_BIND_VOL() macro.
2279 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2280 unsigned int size
, unsigned int __user
*tlv
)
2282 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2283 struct hda_bind_ctls
*c
;
2286 mutex_lock(&codec
->control_mutex
);
2287 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2288 kcontrol
->private_value
= *c
->values
;
2289 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
2290 kcontrol
->private_value
= (long)c
;
2291 mutex_unlock(&codec
->control_mutex
);
2294 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
2296 struct hda_ctl_ops snd_hda_bind_vol
= {
2297 .info
= snd_hda_mixer_amp_volume_info
,
2298 .get
= snd_hda_mixer_amp_volume_get
,
2299 .put
= snd_hda_mixer_amp_volume_put
,
2300 .tlv
= snd_hda_mixer_amp_tlv
2302 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
2304 struct hda_ctl_ops snd_hda_bind_sw
= {
2305 .info
= snd_hda_mixer_amp_switch_info
,
2306 .get
= snd_hda_mixer_amp_switch_get
,
2307 .put
= snd_hda_mixer_amp_switch_put
,
2308 .tlv
= snd_hda_mixer_amp_tlv
2310 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
2313 * SPDIF out controls
2316 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2317 struct snd_ctl_elem_info
*uinfo
)
2319 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2324 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2325 struct snd_ctl_elem_value
*ucontrol
)
2327 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2328 IEC958_AES0_NONAUDIO
|
2329 IEC958_AES0_CON_EMPHASIS_5015
|
2330 IEC958_AES0_CON_NOT_COPYRIGHT
;
2331 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2332 IEC958_AES1_CON_ORIGINAL
;
2336 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2337 struct snd_ctl_elem_value
*ucontrol
)
2339 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2340 IEC958_AES0_NONAUDIO
|
2341 IEC958_AES0_PRO_EMPHASIS_5015
;
2345 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2346 struct snd_ctl_elem_value
*ucontrol
)
2348 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2350 ucontrol
->value
.iec958
.status
[0] = codec
->spdif_status
& 0xff;
2351 ucontrol
->value
.iec958
.status
[1] = (codec
->spdif_status
>> 8) & 0xff;
2352 ucontrol
->value
.iec958
.status
[2] = (codec
->spdif_status
>> 16) & 0xff;
2353 ucontrol
->value
.iec958
.status
[3] = (codec
->spdif_status
>> 24) & 0xff;
2358 /* convert from SPDIF status bits to HDA SPDIF bits
2359 * bit 0 (DigEn) is always set zero (to be filled later)
2361 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2363 unsigned short val
= 0;
2365 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2366 val
|= AC_DIG1_PROFESSIONAL
;
2367 if (sbits
& IEC958_AES0_NONAUDIO
)
2368 val
|= AC_DIG1_NONAUDIO
;
2369 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2370 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2371 IEC958_AES0_PRO_EMPHASIS_5015
)
2372 val
|= AC_DIG1_EMPHASIS
;
2374 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2375 IEC958_AES0_CON_EMPHASIS_5015
)
2376 val
|= AC_DIG1_EMPHASIS
;
2377 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2378 val
|= AC_DIG1_COPYRIGHT
;
2379 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2380 val
|= AC_DIG1_LEVEL
;
2381 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2386 /* convert to SPDIF status bits from HDA SPDIF bits
2388 static unsigned int convert_to_spdif_status(unsigned short val
)
2390 unsigned int sbits
= 0;
2392 if (val
& AC_DIG1_NONAUDIO
)
2393 sbits
|= IEC958_AES0_NONAUDIO
;
2394 if (val
& AC_DIG1_PROFESSIONAL
)
2395 sbits
|= IEC958_AES0_PROFESSIONAL
;
2396 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2397 if (sbits
& AC_DIG1_EMPHASIS
)
2398 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
2400 if (val
& AC_DIG1_EMPHASIS
)
2401 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
2402 if (!(val
& AC_DIG1_COPYRIGHT
))
2403 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
2404 if (val
& AC_DIG1_LEVEL
)
2405 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
2406 sbits
|= val
& (0x7f << 8);
2411 /* set digital convert verbs both for the given NID and its slaves */
2412 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
2417 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
2418 d
= codec
->slave_dig_outs
;
2422 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
2425 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
2429 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
2431 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
2434 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
2435 struct snd_ctl_elem_value
*ucontrol
)
2437 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2438 hda_nid_t nid
= kcontrol
->private_value
;
2442 mutex_lock(&codec
->spdif_mutex
);
2443 codec
->spdif_status
= ucontrol
->value
.iec958
.status
[0] |
2444 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
2445 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
2446 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
2447 val
= convert_from_spdif_status(codec
->spdif_status
);
2448 val
|= codec
->spdif_ctls
& 1;
2449 change
= codec
->spdif_ctls
!= val
;
2450 codec
->spdif_ctls
= val
;
2453 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2455 mutex_unlock(&codec
->spdif_mutex
);
2459 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2461 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
2462 struct snd_ctl_elem_value
*ucontrol
)
2464 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2466 ucontrol
->value
.integer
.value
[0] = codec
->spdif_ctls
& AC_DIG1_ENABLE
;
2470 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
2471 struct snd_ctl_elem_value
*ucontrol
)
2473 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2474 hda_nid_t nid
= kcontrol
->private_value
;
2478 mutex_lock(&codec
->spdif_mutex
);
2479 val
= codec
->spdif_ctls
& ~AC_DIG1_ENABLE
;
2480 if (ucontrol
->value
.integer
.value
[0])
2481 val
|= AC_DIG1_ENABLE
;
2482 change
= codec
->spdif_ctls
!= val
;
2484 codec
->spdif_ctls
= val
;
2485 set_dig_out_convert(codec
, nid
, val
& 0xff, -1);
2486 /* unmute amp switch (if any) */
2487 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
2488 (val
& AC_DIG1_ENABLE
))
2489 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
2492 mutex_unlock(&codec
->spdif_mutex
);
2496 static struct snd_kcontrol_new dig_mixes
[] = {
2498 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2499 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2500 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
2501 .info
= snd_hda_spdif_mask_info
,
2502 .get
= snd_hda_spdif_cmask_get
,
2505 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2506 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2507 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
2508 .info
= snd_hda_spdif_mask_info
,
2509 .get
= snd_hda_spdif_pmask_get
,
2512 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2513 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
2514 .info
= snd_hda_spdif_mask_info
,
2515 .get
= snd_hda_spdif_default_get
,
2516 .put
= snd_hda_spdif_default_put
,
2519 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2520 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
2521 .info
= snd_hda_spdif_out_switch_info
,
2522 .get
= snd_hda_spdif_out_switch_get
,
2523 .put
= snd_hda_spdif_out_switch_put
,
2528 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2531 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2532 * @codec: the HDA codec
2533 * @nid: audio out widget NID
2535 * Creates controls related with the SPDIF output.
2536 * Called from each patch supporting the SPDIF out.
2538 * Returns 0 if successful, or a negative error code.
2540 int snd_hda_create_spdif_out_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
2543 struct snd_kcontrol
*kctl
;
2544 struct snd_kcontrol_new
*dig_mix
;
2547 for (idx
= 0; idx
< SPDIF_MAX_IDX
; idx
++) {
2548 if (!_snd_hda_find_mixer_ctl(codec
, "IEC958 Playback Switch",
2552 if (idx
>= SPDIF_MAX_IDX
) {
2553 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
2556 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2557 kctl
= snd_ctl_new1(dig_mix
, codec
);
2560 kctl
->id
.index
= idx
;
2561 kctl
->private_value
= nid
;
2562 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
2567 snd_hda_codec_read(codec
, nid
, 0,
2568 AC_VERB_GET_DIGI_CONVERT_1
, 0);
2569 codec
->spdif_status
= convert_to_spdif_status(codec
->spdif_ctls
);
2572 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls
);
2575 * SPDIF sharing with analog output
2577 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
2578 struct snd_ctl_elem_value
*ucontrol
)
2580 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2581 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
2585 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
2586 struct snd_ctl_elem_value
*ucontrol
)
2588 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2589 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
2593 static struct snd_kcontrol_new spdif_share_sw
= {
2594 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2595 .name
= "IEC958 Default PCM Playback Switch",
2596 .info
= snd_ctl_boolean_mono_info
,
2597 .get
= spdif_share_sw_get
,
2598 .put
= spdif_share_sw_put
,
2602 * snd_hda_create_spdif_share_sw - create Default PCM switch
2603 * @codec: the HDA codec
2604 * @mout: multi-out instance
2606 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
2607 struct hda_multi_out
*mout
)
2609 if (!mout
->dig_out_nid
)
2611 /* ATTENTION: here mout is passed as private_data, instead of codec */
2612 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
,
2613 snd_ctl_new1(&spdif_share_sw
, mout
));
2615 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
2621 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2623 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
2624 struct snd_ctl_elem_value
*ucontrol
)
2626 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2628 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
2632 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
2633 struct snd_ctl_elem_value
*ucontrol
)
2635 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2636 hda_nid_t nid
= kcontrol
->private_value
;
2637 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
2640 mutex_lock(&codec
->spdif_mutex
);
2641 change
= codec
->spdif_in_enable
!= val
;
2643 codec
->spdif_in_enable
= val
;
2644 snd_hda_codec_write_cache(codec
, nid
, 0,
2645 AC_VERB_SET_DIGI_CONVERT_1
, val
);
2647 mutex_unlock(&codec
->spdif_mutex
);
2651 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
2652 struct snd_ctl_elem_value
*ucontrol
)
2654 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2655 hda_nid_t nid
= kcontrol
->private_value
;
2659 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
2660 sbits
= convert_to_spdif_status(val
);
2661 ucontrol
->value
.iec958
.status
[0] = sbits
;
2662 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
2663 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
2664 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
2668 static struct snd_kcontrol_new dig_in_ctls
[] = {
2670 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2671 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
2672 .info
= snd_hda_spdif_in_switch_info
,
2673 .get
= snd_hda_spdif_in_switch_get
,
2674 .put
= snd_hda_spdif_in_switch_put
,
2677 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2678 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2679 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
2680 .info
= snd_hda_spdif_mask_info
,
2681 .get
= snd_hda_spdif_in_status_get
,
2687 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2688 * @codec: the HDA codec
2689 * @nid: audio in widget NID
2691 * Creates controls related with the SPDIF input.
2692 * Called from each patch supporting the SPDIF in.
2694 * Returns 0 if successful, or a negative error code.
2696 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
2699 struct snd_kcontrol
*kctl
;
2700 struct snd_kcontrol_new
*dig_mix
;
2703 for (idx
= 0; idx
< SPDIF_MAX_IDX
; idx
++) {
2704 if (!_snd_hda_find_mixer_ctl(codec
, "IEC958 Capture Switch",
2708 if (idx
>= SPDIF_MAX_IDX
) {
2709 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
2712 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
2713 kctl
= snd_ctl_new1(dig_mix
, codec
);
2716 kctl
->private_value
= nid
;
2717 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
2721 codec
->spdif_in_enable
=
2722 snd_hda_codec_read(codec
, nid
, 0,
2723 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
2727 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
2729 #ifdef SND_HDA_NEEDS_RESUME
2734 /* build a 32bit cache key with the widget id and the command parameter */
2735 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2736 #define get_cmd_cache_nid(key) ((key) & 0xff)
2737 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2740 * snd_hda_codec_write_cache - send a single command with caching
2741 * @codec: the HDA codec
2742 * @nid: NID to send the command
2743 * @direct: direct flag
2744 * @verb: the verb to send
2745 * @parm: the parameter for the verb
2747 * Send a single command without waiting for response.
2749 * Returns 0 if successful, or a negative error code.
2751 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
2752 int direct
, unsigned int verb
, unsigned int parm
)
2754 int err
= snd_hda_codec_write(codec
, nid
, direct
, verb
, parm
);
2755 struct hda_cache_head
*c
;
2760 /* parm may contain the verb stuff for get/set amp */
2761 verb
= verb
| (parm
>> 8);
2763 key
= build_cmd_cache_key(nid
, verb
);
2764 mutex_lock(&codec
->bus
->cmd_mutex
);
2765 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
2768 mutex_unlock(&codec
->bus
->cmd_mutex
);
2771 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
2774 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2775 * @codec: the HDA codec
2776 * @nid: NID to send the command
2777 * @direct: direct flag
2778 * @verb: the verb to send
2779 * @parm: the parameter for the verb
2781 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2782 * command if the parameter is already identical with the cached value.
2783 * If not, it sends the command and refreshes the cache.
2785 * Returns 0 if successful, or a negative error code.
2787 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
2788 int direct
, unsigned int verb
, unsigned int parm
)
2790 struct hda_cache_head
*c
;
2793 /* parm may contain the verb stuff for get/set amp */
2794 verb
= verb
| (parm
>> 8);
2796 key
= build_cmd_cache_key(nid
, verb
);
2797 mutex_lock(&codec
->bus
->cmd_mutex
);
2798 c
= get_hash(&codec
->cmd_cache
, key
);
2799 if (c
&& c
->val
== parm
) {
2800 mutex_unlock(&codec
->bus
->cmd_mutex
);
2803 mutex_unlock(&codec
->bus
->cmd_mutex
);
2804 return snd_hda_codec_write_cache(codec
, nid
, direct
, verb
, parm
);
2806 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache
);
2809 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2810 * @codec: HD-audio codec
2812 * Execute all verbs recorded in the command caches to resume.
2814 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
2816 struct hda_cache_head
*buffer
= codec
->cmd_cache
.buf
.list
;
2819 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++, buffer
++) {
2820 u32 key
= buffer
->key
;
2823 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
2824 get_cmd_cache_cmd(key
), buffer
->val
);
2827 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
2830 * snd_hda_sequence_write_cache - sequence writes with caching
2831 * @codec: the HDA codec
2832 * @seq: VERB array to send
2834 * Send the commands sequentially from the given array.
2835 * Thte commands are recorded on cache for power-save and resume.
2836 * The array must be terminated with NID=0.
2838 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
2839 const struct hda_verb
*seq
)
2841 for (; seq
->nid
; seq
++)
2842 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
2845 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
2846 #endif /* SND_HDA_NEEDS_RESUME */
2849 * set power state of the codec
2851 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
2852 unsigned int power_state
)
2857 /* this delay seems necessary to avoid click noise at power-down */
2858 if (power_state
== AC_PWRST_D3
)
2860 snd_hda_codec_read(codec
, fg
, 0, AC_VERB_SET_POWER_STATE
,
2862 /* partial workaround for "azx_get_response timeout" */
2863 if (power_state
== AC_PWRST_D0
&&
2864 (codec
->vendor_id
& 0xffff0000) == 0x14f10000)
2867 nid
= codec
->start_nid
;
2868 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
2869 unsigned int wcaps
= get_wcaps(codec
, nid
);
2870 if (wcaps
& AC_WCAP_POWER
) {
2871 unsigned int wid_type
= get_wcaps_type(wcaps
);
2872 if (power_state
== AC_PWRST_D3
&&
2873 wid_type
== AC_WID_PIN
) {
2874 unsigned int pincap
;
2876 * don't power down the widget if it controls
2877 * eapd and EAPD_BTLENABLE is set.
2879 pincap
= snd_hda_query_pin_caps(codec
, nid
);
2880 if (pincap
& AC_PINCAP_EAPD
) {
2881 int eapd
= snd_hda_codec_read(codec
,
2883 AC_VERB_GET_EAPD_BTLENABLE
, 0);
2889 snd_hda_codec_write(codec
, nid
, 0,
2890 AC_VERB_SET_POWER_STATE
,
2895 if (power_state
== AC_PWRST_D0
) {
2896 unsigned long end_time
;
2898 /* wait until the codec reachs to D0 */
2899 end_time
= jiffies
+ msecs_to_jiffies(500);
2901 state
= snd_hda_codec_read(codec
, fg
, 0,
2902 AC_VERB_GET_POWER_STATE
, 0);
2903 if (state
== power_state
)
2906 } while (time_after_eq(end_time
, jiffies
));
2910 #ifdef CONFIG_SND_HDA_HWDEP
2911 /* execute additional init verbs */
2912 static void hda_exec_init_verbs(struct hda_codec
*codec
)
2914 if (codec
->init_verbs
.list
)
2915 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
2918 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
2921 #ifdef SND_HDA_NEEDS_RESUME
2923 * call suspend and power-down; used both from PM and power-save
2925 static void hda_call_codec_suspend(struct hda_codec
*codec
)
2927 if (codec
->patch_ops
.suspend
)
2928 codec
->patch_ops
.suspend(codec
, PMSG_SUSPEND
);
2929 hda_set_power_state(codec
,
2930 codec
->afg
? codec
->afg
: codec
->mfg
,
2932 #ifdef CONFIG_SND_HDA_POWER_SAVE
2933 snd_hda_update_power_acct(codec
);
2934 cancel_delayed_work(&codec
->power_work
);
2935 codec
->power_on
= 0;
2936 codec
->power_transition
= 0;
2937 codec
->power_jiffies
= jiffies
;
2942 * kick up codec; used both from PM and power-save
2944 static void hda_call_codec_resume(struct hda_codec
*codec
)
2946 hda_set_power_state(codec
,
2947 codec
->afg
? codec
->afg
: codec
->mfg
,
2949 restore_pincfgs(codec
); /* restore all current pin configs */
2950 restore_shutup_pins(codec
);
2951 hda_exec_init_verbs(codec
);
2952 if (codec
->patch_ops
.resume
)
2953 codec
->patch_ops
.resume(codec
);
2955 if (codec
->patch_ops
.init
)
2956 codec
->patch_ops
.init(codec
);
2957 snd_hda_codec_resume_amp(codec
);
2958 snd_hda_codec_resume_cache(codec
);
2961 #endif /* SND_HDA_NEEDS_RESUME */
2965 * snd_hda_build_controls - build mixer controls
2968 * Creates mixer controls for each codec included in the bus.
2970 * Returns 0 if successful, otherwise a negative error code.
2972 int /*__devinit*/ snd_hda_build_controls(struct hda_bus
*bus
)
2974 struct hda_codec
*codec
;
2976 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2977 int err
= snd_hda_codec_build_controls(codec
);
2979 printk(KERN_ERR
"hda_codec: cannot build controls "
2980 "for #%d (error %d)\n", codec
->addr
, err
);
2981 err
= snd_hda_codec_reset(codec
);
2984 "hda_codec: cannot revert codec\n");
2991 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
2993 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
2996 hda_exec_init_verbs(codec
);
2997 /* continue to initialize... */
2998 if (codec
->patch_ops
.init
)
2999 err
= codec
->patch_ops
.init(codec
);
3000 if (!err
&& codec
->patch_ops
.build_controls
)
3001 err
= codec
->patch_ops
.build_controls(codec
);
3010 struct hda_rate_tbl
{
3012 unsigned int alsa_bits
;
3013 unsigned int hda_fmt
;
3016 /* rate = base * mult / div */
3017 #define HDA_RATE(base, mult, div) \
3018 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3019 (((div) - 1) << AC_FMT_DIV_SHIFT))
3021 static struct hda_rate_tbl rate_bits
[] = {
3022 /* rate in Hz, ALSA rate bitmask, HDA format value */
3024 /* autodetected value used in snd_hda_query_supported_pcm */
3025 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
3026 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
3027 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
3028 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
3029 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
3030 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
3031 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
3032 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
3033 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
3034 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
3035 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
3036 #define AC_PAR_PCM_RATE_BITS 11
3037 /* up to bits 10, 384kHZ isn't supported properly */
3039 /* not autodetected value */
3040 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
3042 { 0 } /* terminator */
3046 * snd_hda_calc_stream_format - calculate format bitset
3047 * @rate: the sample rate
3048 * @channels: the number of channels
3049 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3050 * @maxbps: the max. bps
3052 * Calculate the format bitset from the given rate, channels and th PCM format.
3054 * Return zero if invalid.
3056 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
3057 unsigned int channels
,
3058 unsigned int format
,
3059 unsigned int maxbps
,
3060 unsigned short spdif_ctls
)
3063 unsigned int val
= 0;
3065 for (i
= 0; rate_bits
[i
].hz
; i
++)
3066 if (rate_bits
[i
].hz
== rate
) {
3067 val
= rate_bits
[i
].hda_fmt
;
3070 if (!rate_bits
[i
].hz
) {
3071 snd_printdd("invalid rate %d\n", rate
);
3075 if (channels
== 0 || channels
> 8) {
3076 snd_printdd("invalid channels %d\n", channels
);
3079 val
|= channels
- 1;
3081 switch (snd_pcm_format_width(format
)) {
3083 val
|= AC_FMT_BITS_8
;
3086 val
|= AC_FMT_BITS_16
;
3091 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
3092 val
|= AC_FMT_BITS_32
;
3093 else if (maxbps
>= 24)
3094 val
|= AC_FMT_BITS_24
;
3096 val
|= AC_FMT_BITS_20
;
3099 snd_printdd("invalid format width %d\n",
3100 snd_pcm_format_width(format
));
3104 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
3105 val
|= AC_FMT_TYPE_NON_PCM
;
3109 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
3111 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3113 unsigned int val
= 0;
3114 if (nid
!= codec
->afg
&&
3115 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
3116 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
3117 if (!val
|| val
== -1)
3118 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
3119 if (!val
|| val
== -1)
3124 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3126 return query_caps_hash(codec
, nid
, HDA_HASH_PARPCM_KEY(nid
),
3130 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3132 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
3133 if (!streams
|| streams
== -1)
3134 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
3135 if (!streams
|| streams
== -1)
3140 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3142 return query_caps_hash(codec
, nid
, HDA_HASH_PARSTR_KEY(nid
),
3147 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3148 * @codec: the HDA codec
3149 * @nid: NID to query
3150 * @ratesp: the pointer to store the detected rate bitflags
3151 * @formatsp: the pointer to store the detected formats
3152 * @bpsp: the pointer to store the detected format widths
3154 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3155 * or @bsps argument is ignored.
3157 * Returns 0 if successful, otherwise a negative error code.
3159 static int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
3160 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
3162 unsigned int i
, val
, wcaps
;
3164 wcaps
= get_wcaps(codec
, nid
);
3165 val
= query_pcm_param(codec
, nid
);
3169 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
3171 rates
|= rate_bits
[i
].alsa_bits
;
3174 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
3175 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3177 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
3183 if (formatsp
|| bpsp
) {
3185 unsigned int streams
, bps
;
3187 streams
= query_stream_param(codec
, nid
);
3192 if (streams
& AC_SUPFMT_PCM
) {
3193 if (val
& AC_SUPPCM_BITS_8
) {
3194 formats
|= SNDRV_PCM_FMTBIT_U8
;
3197 if (val
& AC_SUPPCM_BITS_16
) {
3198 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
3201 if (wcaps
& AC_WCAP_DIGITAL
) {
3202 if (val
& AC_SUPPCM_BITS_32
)
3203 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
3204 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
3205 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3206 if (val
& AC_SUPPCM_BITS_24
)
3208 else if (val
& AC_SUPPCM_BITS_20
)
3210 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
3211 AC_SUPPCM_BITS_32
)) {
3212 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3213 if (val
& AC_SUPPCM_BITS_32
)
3215 else if (val
& AC_SUPPCM_BITS_24
)
3217 else if (val
& AC_SUPPCM_BITS_20
)
3221 if (streams
& AC_SUPFMT_FLOAT32
) {
3222 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
3226 if (streams
== AC_SUPFMT_AC3
) {
3227 /* should be exclusive */
3228 /* temporary hack: we have still no proper support
3229 * for the direct AC3 stream...
3231 formats
|= SNDRV_PCM_FMTBIT_U8
;
3235 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
3236 "(nid=0x%x, val=0x%x, ovrd=%i, "
3239 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
3244 *formatsp
= formats
;
3253 * snd_hda_is_supported_format - Check the validity of the format
3254 * @codec: HD-audio codec
3255 * @nid: NID to check
3256 * @format: the HD-audio format value to check
3258 * Check whether the given node supports the format value.
3260 * Returns 1 if supported, 0 if not.
3262 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
3263 unsigned int format
)
3266 unsigned int val
= 0, rate
, stream
;
3268 val
= query_pcm_param(codec
, nid
);
3272 rate
= format
& 0xff00;
3273 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
3274 if (rate_bits
[i
].hda_fmt
== rate
) {
3279 if (i
>= AC_PAR_PCM_RATE_BITS
)
3282 stream
= query_stream_param(codec
, nid
);
3286 if (stream
& AC_SUPFMT_PCM
) {
3287 switch (format
& 0xf0) {
3289 if (!(val
& AC_SUPPCM_BITS_8
))
3293 if (!(val
& AC_SUPPCM_BITS_16
))
3297 if (!(val
& AC_SUPPCM_BITS_20
))
3301 if (!(val
& AC_SUPPCM_BITS_24
))
3305 if (!(val
& AC_SUPPCM_BITS_32
))
3312 /* FIXME: check for float32 and AC3? */
3317 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
3322 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
3323 struct hda_codec
*codec
,
3324 struct snd_pcm_substream
*substream
)
3329 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
3330 struct hda_codec
*codec
,
3331 unsigned int stream_tag
,
3332 unsigned int format
,
3333 struct snd_pcm_substream
*substream
)
3335 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3339 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
3340 struct hda_codec
*codec
,
3341 struct snd_pcm_substream
*substream
)
3343 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3347 static int set_pcm_default_values(struct hda_codec
*codec
,
3348 struct hda_pcm_stream
*info
)
3352 /* query support PCM information from the given NID */
3353 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
3354 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
3355 info
->rates
? NULL
: &info
->rates
,
3356 info
->formats
? NULL
: &info
->formats
,
3357 info
->maxbps
? NULL
: &info
->maxbps
);
3361 if (info
->ops
.open
== NULL
)
3362 info
->ops
.open
= hda_pcm_default_open_close
;
3363 if (info
->ops
.close
== NULL
)
3364 info
->ops
.close
= hda_pcm_default_open_close
;
3365 if (info
->ops
.prepare
== NULL
) {
3366 if (snd_BUG_ON(!info
->nid
))
3368 info
->ops
.prepare
= hda_pcm_default_prepare
;
3370 if (info
->ops
.cleanup
== NULL
) {
3371 if (snd_BUG_ON(!info
->nid
))
3373 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
3379 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
3380 "Audio", "SPDIF", "HDMI", "Modem"
3384 * get the empty PCM device number to assign
3386 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3388 static int get_empty_pcm_device(struct hda_bus
*bus
, int type
)
3390 /* audio device indices; not linear to keep compatibility */
3391 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
3392 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
3393 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
3394 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
3395 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
3399 if (type
>= HDA_PCM_NTYPES
) {
3400 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
3404 for (i
= 0; audio_idx
[type
][i
] >= 0 ; i
++)
3405 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
3406 return audio_idx
[type
][i
];
3408 snd_printk(KERN_WARNING
"Too many %s devices\n",
3409 snd_hda_pcm_type_name
[type
]);
3414 * attach a new PCM stream
3416 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
3418 struct hda_bus
*bus
= codec
->bus
;
3419 struct hda_pcm_stream
*info
;
3422 if (snd_BUG_ON(!pcm
->name
))
3424 for (stream
= 0; stream
< 2; stream
++) {
3425 info
= &pcm
->stream
[stream
];
3426 if (info
->substreams
) {
3427 err
= set_pcm_default_values(codec
, info
);
3432 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
3435 /* assign all PCMs of the given codec */
3436 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
3441 if (!codec
->num_pcms
) {
3442 if (!codec
->patch_ops
.build_pcms
)
3444 err
= codec
->patch_ops
.build_pcms(codec
);
3446 printk(KERN_ERR
"hda_codec: cannot build PCMs"
3447 "for #%d (error %d)\n", codec
->addr
, err
);
3448 err
= snd_hda_codec_reset(codec
);
3451 "hda_codec: cannot revert codec\n");
3456 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
3457 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
3460 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
3461 continue; /* no substreams assigned */
3464 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
3466 continue; /* no fatal error */
3468 err
= snd_hda_attach_pcm(codec
, cpcm
);
3470 printk(KERN_ERR
"hda_codec: cannot attach "
3471 "PCM stream %d for codec #%d\n",
3473 continue; /* no fatal error */
3481 * snd_hda_build_pcms - build PCM information
3484 * Create PCM information for each codec included in the bus.
3486 * The build_pcms codec patch is requested to set up codec->num_pcms and
3487 * codec->pcm_info properly. The array is referred by the top-level driver
3488 * to create its PCM instances.
3489 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3492 * At least, substreams, channels_min and channels_max must be filled for
3493 * each stream. substreams = 0 indicates that the stream doesn't exist.
3494 * When rates and/or formats are zero, the supported values are queried
3495 * from the given nid. The nid is used also by the default ops.prepare
3496 * and ops.cleanup callbacks.
3498 * The driver needs to call ops.open in its open callback. Similarly,
3499 * ops.close is supposed to be called in the close callback.
3500 * ops.prepare should be called in the prepare or hw_params callback
3501 * with the proper parameters for set up.
3502 * ops.cleanup should be called in hw_free for clean up of streams.
3504 * This function returns 0 if successfull, or a negative error code.
3506 int __devinit
snd_hda_build_pcms(struct hda_bus
*bus
)
3508 struct hda_codec
*codec
;
3510 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3511 int err
= snd_hda_codec_build_pcms(codec
);
3517 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
3520 * snd_hda_check_board_config - compare the current codec with the config table
3521 * @codec: the HDA codec
3522 * @num_configs: number of config enums
3523 * @models: array of model name strings
3524 * @tbl: configuration table, terminated by null entries
3526 * Compares the modelname or PCI subsystem id of the current codec with the
3527 * given configuration table. If a matching entry is found, returns its
3528 * config value (supposed to be 0 or positive).
3530 * If no entries are matching, the function returns a negative value.
3532 int snd_hda_check_board_config(struct hda_codec
*codec
,
3533 int num_configs
, const char **models
,
3534 const struct snd_pci_quirk
*tbl
)
3536 if (codec
->modelname
&& models
) {
3538 for (i
= 0; i
< num_configs
; i
++) {
3540 !strcmp(codec
->modelname
, models
[i
])) {
3541 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
3542 "selected\n", models
[i
]);
3548 if (!codec
->bus
->pci
|| !tbl
)
3551 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
3554 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
3555 #ifdef CONFIG_SND_DEBUG_VERBOSE
3557 const char *model
= NULL
;
3559 model
= models
[tbl
->value
];
3561 sprintf(tmp
, "#%d", tbl
->value
);
3564 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
3565 "for config %x:%x (%s)\n",
3566 model
, tbl
->subvendor
, tbl
->subdevice
,
3567 (tbl
->name
? tbl
->name
: "Unknown device"));
3573 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
3576 * snd_hda_check_board_codec_sid_config - compare the current codec
3577 subsystem ID with the
3580 This is important for Gateway notebooks with SB450 HDA Audio
3581 where the vendor ID of the PCI device is:
3582 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3583 and the vendor/subvendor are found only at the codec.
3585 * @codec: the HDA codec
3586 * @num_configs: number of config enums
3587 * @models: array of model name strings
3588 * @tbl: configuration table, terminated by null entries
3590 * Compares the modelname or PCI subsystem id of the current codec with the
3591 * given configuration table. If a matching entry is found, returns its
3592 * config value (supposed to be 0 or positive).
3594 * If no entries are matching, the function returns a negative value.
3596 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
3597 int num_configs
, const char **models
,
3598 const struct snd_pci_quirk
*tbl
)
3600 const struct snd_pci_quirk
*q
;
3602 /* Search for codec ID */
3603 for (q
= tbl
; q
->subvendor
; q
++) {
3604 unsigned long vendorid
= (q
->subdevice
) | (q
->subvendor
<< 16);
3606 if (vendorid
== codec
->subsystem_id
)
3615 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
3616 #ifdef CONFIG_SND_DEBUG_VERBOSE
3618 const char *model
= NULL
;
3620 model
= models
[tbl
->value
];
3622 sprintf(tmp
, "#%d", tbl
->value
);
3625 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
3626 "for config %x:%x (%s)\n",
3627 model
, tbl
->subvendor
, tbl
->subdevice
,
3628 (tbl
->name
? tbl
->name
: "Unknown device"));
3634 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
3637 * snd_hda_add_new_ctls - create controls from the array
3638 * @codec: the HDA codec
3639 * @knew: the array of struct snd_kcontrol_new
3641 * This helper function creates and add new controls in the given array.
3642 * The array must be terminated with an empty entry as terminator.
3644 * Returns 0 if successful, or a negative error code.
3646 int snd_hda_add_new_ctls(struct hda_codec
*codec
, struct snd_kcontrol_new
*knew
)
3650 for (; knew
->name
; knew
++) {
3651 struct snd_kcontrol
*kctl
;
3652 if (knew
->iface
== -1) /* skip this codec private value */
3654 kctl
= snd_ctl_new1(knew
, codec
);
3657 err
= snd_hda_ctl_add(codec
, 0, kctl
);
3661 kctl
= snd_ctl_new1(knew
, codec
);
3664 kctl
->id
.device
= codec
->addr
;
3665 err
= snd_hda_ctl_add(codec
, 0, kctl
);
3672 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
3674 #ifdef CONFIG_SND_HDA_POWER_SAVE
3675 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
3676 unsigned int power_state
);
3678 static void hda_power_work(struct work_struct
*work
)
3680 struct hda_codec
*codec
=
3681 container_of(work
, struct hda_codec
, power_work
.work
);
3682 struct hda_bus
*bus
= codec
->bus
;
3684 if (!codec
->power_on
|| codec
->power_count
) {
3685 codec
->power_transition
= 0;
3689 hda_call_codec_suspend(codec
);
3690 if (bus
->ops
.pm_notify
)
3691 bus
->ops
.pm_notify(bus
);
3694 static void hda_keep_power_on(struct hda_codec
*codec
)
3696 codec
->power_count
++;
3697 codec
->power_on
= 1;
3698 codec
->power_jiffies
= jiffies
;
3701 /* update the power on/off account with the current jiffies */
3702 void snd_hda_update_power_acct(struct hda_codec
*codec
)
3704 unsigned long delta
= jiffies
- codec
->power_jiffies
;
3705 if (codec
->power_on
)
3706 codec
->power_on_acct
+= delta
;
3708 codec
->power_off_acct
+= delta
;
3709 codec
->power_jiffies
+= delta
;
3713 * snd_hda_power_up - Power-up the codec
3714 * @codec: HD-audio codec
3716 * Increment the power-up counter and power up the hardware really when
3717 * not turned on yet.
3719 void snd_hda_power_up(struct hda_codec
*codec
)
3721 struct hda_bus
*bus
= codec
->bus
;
3723 codec
->power_count
++;
3724 if (codec
->power_on
|| codec
->power_transition
)
3727 snd_hda_update_power_acct(codec
);
3728 codec
->power_on
= 1;
3729 codec
->power_jiffies
= jiffies
;
3730 if (bus
->ops
.pm_notify
)
3731 bus
->ops
.pm_notify(bus
);
3732 hda_call_codec_resume(codec
);
3733 cancel_delayed_work(&codec
->power_work
);
3734 codec
->power_transition
= 0;
3736 EXPORT_SYMBOL_HDA(snd_hda_power_up
);
3738 #define power_save(codec) \
3739 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3742 * snd_hda_power_down - Power-down the codec
3743 * @codec: HD-audio codec
3745 * Decrement the power-up counter and schedules the power-off work if
3746 * the counter rearches to zero.
3748 void snd_hda_power_down(struct hda_codec
*codec
)
3750 --codec
->power_count
;
3751 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
3753 if (power_save(codec
)) {
3754 codec
->power_transition
= 1; /* avoid reentrance */
3755 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
3756 msecs_to_jiffies(power_save(codec
) * 1000));
3759 EXPORT_SYMBOL_HDA(snd_hda_power_down
);
3762 * snd_hda_check_amp_list_power - Check the amp list and update the power
3763 * @codec: HD-audio codec
3764 * @check: the object containing an AMP list and the status
3765 * @nid: NID to check / update
3767 * Check whether the given NID is in the amp list. If it's in the list,
3768 * check the current AMP status, and update the the power-status according
3769 * to the mute status.
3771 * This function is supposed to be set or called from the check_power_status
3774 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
3775 struct hda_loopback_check
*check
,
3778 struct hda_amp_list
*p
;
3781 if (!check
->amplist
)
3783 for (p
= check
->amplist
; p
->nid
; p
++) {
3788 return 0; /* nothing changed */
3790 for (p
= check
->amplist
; p
->nid
; p
++) {
3791 for (ch
= 0; ch
< 2; ch
++) {
3792 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
3794 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
3795 if (!check
->power_on
) {
3796 check
->power_on
= 1;
3797 snd_hda_power_up(codec
);
3803 if (check
->power_on
) {
3804 check
->power_on
= 0;
3805 snd_hda_power_down(codec
);
3809 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
3813 * Channel mode helper
3817 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3819 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
3820 struct snd_ctl_elem_info
*uinfo
,
3821 const struct hda_channel_mode
*chmode
,
3824 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
3826 uinfo
->value
.enumerated
.items
= num_chmodes
;
3827 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
3828 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
3829 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
3830 chmode
[uinfo
->value
.enumerated
.item
].channels
);
3833 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
3836 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3838 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
3839 struct snd_ctl_elem_value
*ucontrol
,
3840 const struct hda_channel_mode
*chmode
,
3846 for (i
= 0; i
< num_chmodes
; i
++) {
3847 if (max_channels
== chmode
[i
].channels
) {
3848 ucontrol
->value
.enumerated
.item
[0] = i
;
3854 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
3857 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3859 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
3860 struct snd_ctl_elem_value
*ucontrol
,
3861 const struct hda_channel_mode
*chmode
,
3867 mode
= ucontrol
->value
.enumerated
.item
[0];
3868 if (mode
>= num_chmodes
)
3870 if (*max_channelsp
== chmode
[mode
].channels
)
3872 /* change the current channel setting */
3873 *max_channelsp
= chmode
[mode
].channels
;
3874 if (chmode
[mode
].sequence
)
3875 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
3878 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
3885 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3887 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
3888 struct snd_ctl_elem_info
*uinfo
)
3892 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
3894 uinfo
->value
.enumerated
.items
= imux
->num_items
;
3895 if (!imux
->num_items
)
3897 index
= uinfo
->value
.enumerated
.item
;
3898 if (index
>= imux
->num_items
)
3899 index
= imux
->num_items
- 1;
3900 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
3903 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
3906 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3908 int snd_hda_input_mux_put(struct hda_codec
*codec
,
3909 const struct hda_input_mux
*imux
,
3910 struct snd_ctl_elem_value
*ucontrol
,
3912 unsigned int *cur_val
)
3916 if (!imux
->num_items
)
3918 idx
= ucontrol
->value
.enumerated
.item
[0];
3919 if (idx
>= imux
->num_items
)
3920 idx
= imux
->num_items
- 1;
3921 if (*cur_val
== idx
)
3923 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
3924 imux
->items
[idx
].index
);
3928 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
3932 * Multi-channel / digital-out PCM helper functions
3935 /* setup SPDIF output stream */
3936 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
3937 unsigned int stream_tag
, unsigned int format
)
3939 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3940 if (codec
->spdif_status_reset
&& (codec
->spdif_ctls
& AC_DIG1_ENABLE
))
3941 set_dig_out_convert(codec
, nid
,
3942 codec
->spdif_ctls
& ~AC_DIG1_ENABLE
& 0xff,
3944 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
3945 if (codec
->slave_dig_outs
) {
3947 for (d
= codec
->slave_dig_outs
; *d
; d
++)
3948 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
3951 /* turn on again (if needed) */
3952 if (codec
->spdif_status_reset
&& (codec
->spdif_ctls
& AC_DIG1_ENABLE
))
3953 set_dig_out_convert(codec
, nid
,
3954 codec
->spdif_ctls
& 0xff, -1);
3957 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
3959 snd_hda_codec_cleanup_stream(codec
, nid
);
3960 if (codec
->slave_dig_outs
) {
3962 for (d
= codec
->slave_dig_outs
; *d
; d
++)
3963 snd_hda_codec_cleanup_stream(codec
, *d
);
3968 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
3969 * @bus: HD-audio bus
3971 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
3973 struct hda_codec
*codec
;
3977 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3978 #ifdef CONFIG_SND_HDA_POWER_SAVE
3979 if (!codec
->power_on
)
3982 if (codec
->patch_ops
.reboot_notify
)
3983 codec
->patch_ops
.reboot_notify(codec
);
3986 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify
);
3989 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3991 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
3992 struct hda_multi_out
*mout
)
3994 mutex_lock(&codec
->spdif_mutex
);
3995 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
3996 /* already opened as analog dup; reset it once */
3997 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3998 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
3999 mutex_unlock(&codec
->spdif_mutex
);
4002 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
4005 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4007 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
4008 struct hda_multi_out
*mout
,
4009 unsigned int stream_tag
,
4010 unsigned int format
,
4011 struct snd_pcm_substream
*substream
)
4013 mutex_lock(&codec
->spdif_mutex
);
4014 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
4015 mutex_unlock(&codec
->spdif_mutex
);
4018 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
4021 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4023 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
4024 struct hda_multi_out
*mout
)
4026 mutex_lock(&codec
->spdif_mutex
);
4027 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4028 mutex_unlock(&codec
->spdif_mutex
);
4031 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
4034 * snd_hda_multi_out_dig_close - release the digital out stream
4036 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
4037 struct hda_multi_out
*mout
)
4039 mutex_lock(&codec
->spdif_mutex
);
4040 mout
->dig_out_used
= 0;
4041 mutex_unlock(&codec
->spdif_mutex
);
4044 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
4047 * snd_hda_multi_out_analog_open - open analog outputs
4049 * Open analog outputs and set up the hw-constraints.
4050 * If the digital outputs can be opened as slave, open the digital
4053 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
4054 struct hda_multi_out
*mout
,
4055 struct snd_pcm_substream
*substream
,
4056 struct hda_pcm_stream
*hinfo
)
4058 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
4059 runtime
->hw
.channels_max
= mout
->max_channels
;
4060 if (mout
->dig_out_nid
) {
4061 if (!mout
->analog_rates
) {
4062 mout
->analog_rates
= hinfo
->rates
;
4063 mout
->analog_formats
= hinfo
->formats
;
4064 mout
->analog_maxbps
= hinfo
->maxbps
;
4066 runtime
->hw
.rates
= mout
->analog_rates
;
4067 runtime
->hw
.formats
= mout
->analog_formats
;
4068 hinfo
->maxbps
= mout
->analog_maxbps
;
4070 if (!mout
->spdif_rates
) {
4071 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
4073 &mout
->spdif_formats
,
4074 &mout
->spdif_maxbps
);
4076 mutex_lock(&codec
->spdif_mutex
);
4077 if (mout
->share_spdif
) {
4078 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
4079 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
4080 runtime
->hw
.rates
&= mout
->spdif_rates
;
4081 runtime
->hw
.formats
&= mout
->spdif_formats
;
4082 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
4083 hinfo
->maxbps
= mout
->spdif_maxbps
;
4085 mout
->share_spdif
= 0;
4086 /* FIXME: need notify? */
4089 mutex_unlock(&codec
->spdif_mutex
);
4091 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
4092 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
4094 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
4097 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4099 * Set up the i/o for analog out.
4100 * When the digital out is available, copy the front out to digital out, too.
4102 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
4103 struct hda_multi_out
*mout
,
4104 unsigned int stream_tag
,
4105 unsigned int format
,
4106 struct snd_pcm_substream
*substream
)
4108 hda_nid_t
*nids
= mout
->dac_nids
;
4109 int chs
= substream
->runtime
->channels
;
4112 mutex_lock(&codec
->spdif_mutex
);
4113 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
4114 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
4116 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
4118 !(codec
->spdif_status
& IEC958_AES0_NONAUDIO
)) {
4119 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
4120 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
4121 stream_tag
, format
);
4123 mout
->dig_out_used
= 0;
4124 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4127 mutex_unlock(&codec
->spdif_mutex
);
4130 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
4132 if (!mout
->no_share_stream
&&
4133 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
4134 /* headphone out will just decode front left/right (stereo) */
4135 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
4137 /* extra outputs copied from front */
4138 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4139 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
4140 snd_hda_codec_setup_stream(codec
,
4141 mout
->extra_out_nid
[i
],
4142 stream_tag
, 0, format
);
4145 for (i
= 1; i
< mout
->num_dacs
; i
++) {
4146 if (chs
>= (i
+ 1) * 2) /* independent out */
4147 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4149 else if (!mout
->no_share_stream
) /* copy front */
4150 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4155 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
4158 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4160 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
4161 struct hda_multi_out
*mout
)
4163 hda_nid_t
*nids
= mout
->dac_nids
;
4166 for (i
= 0; i
< mout
->num_dacs
; i
++)
4167 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
4169 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
4170 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4171 if (mout
->extra_out_nid
[i
])
4172 snd_hda_codec_cleanup_stream(codec
,
4173 mout
->extra_out_nid
[i
]);
4174 mutex_lock(&codec
->spdif_mutex
);
4175 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
4176 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4177 mout
->dig_out_used
= 0;
4179 mutex_unlock(&codec
->spdif_mutex
);
4182 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
4185 * Helper for automatic pin configuration
4188 static int is_in_nid_list(hda_nid_t nid
, hda_nid_t
*list
)
4190 for (; *list
; list
++)
4198 * Sort an associated group of pins according to their sequence numbers.
4200 static void sort_pins_by_sequence(hda_nid_t
*pins
, short *sequences
,
4207 for (i
= 0; i
< num_pins
; i
++) {
4208 for (j
= i
+ 1; j
< num_pins
; j
++) {
4209 if (sequences
[i
] > sequences
[j
]) {
4211 sequences
[i
] = sequences
[j
];
4223 * Parse all pin widgets and store the useful pin nids to cfg
4225 * The number of line-outs or any primary output is stored in line_outs,
4226 * and the corresponding output pins are assigned to line_out_pins[],
4227 * in the order of front, rear, CLFE, side, ...
4229 * If more extra outputs (speaker and headphone) are found, the pins are
4230 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4231 * is detected, one of speaker of HP pins is assigned as the primary
4232 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4233 * if any analog output exists.
4235 * The analog input pins are assigned to input_pins array.
4236 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4239 int snd_hda_parse_pin_def_config(struct hda_codec
*codec
,
4240 struct auto_pin_cfg
*cfg
,
4241 hda_nid_t
*ignore_nids
)
4243 hda_nid_t nid
, end_nid
;
4244 short seq
, assoc_line_out
, assoc_speaker
;
4245 short sequences_line_out
[ARRAY_SIZE(cfg
->line_out_pins
)];
4246 short sequences_speaker
[ARRAY_SIZE(cfg
->speaker_pins
)];
4247 short sequences_hp
[ARRAY_SIZE(cfg
->hp_pins
)];
4249 memset(cfg
, 0, sizeof(*cfg
));
4251 memset(sequences_line_out
, 0, sizeof(sequences_line_out
));
4252 memset(sequences_speaker
, 0, sizeof(sequences_speaker
));
4253 memset(sequences_hp
, 0, sizeof(sequences_hp
));
4254 assoc_line_out
= assoc_speaker
= 0;
4256 end_nid
= codec
->start_nid
+ codec
->num_nodes
;
4257 for (nid
= codec
->start_nid
; nid
< end_nid
; nid
++) {
4258 unsigned int wid_caps
= get_wcaps(codec
, nid
);
4259 unsigned int wid_type
= get_wcaps_type(wid_caps
);
4260 unsigned int def_conf
;
4263 /* read all default configuration for pin complex */
4264 if (wid_type
!= AC_WID_PIN
)
4266 /* ignore the given nids (e.g. pc-beep returns error) */
4267 if (ignore_nids
&& is_in_nid_list(nid
, ignore_nids
))
4270 def_conf
= snd_hda_codec_get_pincfg(codec
, nid
);
4271 if (get_defcfg_connect(def_conf
) == AC_JACK_PORT_NONE
)
4273 loc
= get_defcfg_location(def_conf
);
4274 switch (get_defcfg_device(def_conf
)) {
4275 case AC_JACK_LINE_OUT
:
4276 seq
= get_defcfg_sequence(def_conf
);
4277 assoc
= get_defcfg_association(def_conf
);
4279 if (!(wid_caps
& AC_WCAP_STEREO
))
4280 if (!cfg
->mono_out_pin
)
4281 cfg
->mono_out_pin
= nid
;
4284 if (!assoc_line_out
)
4285 assoc_line_out
= assoc
;
4286 else if (assoc_line_out
!= assoc
)
4288 if (cfg
->line_outs
>= ARRAY_SIZE(cfg
->line_out_pins
))
4290 cfg
->line_out_pins
[cfg
->line_outs
] = nid
;
4291 sequences_line_out
[cfg
->line_outs
] = seq
;
4294 case AC_JACK_SPEAKER
:
4295 seq
= get_defcfg_sequence(def_conf
);
4296 assoc
= get_defcfg_association(def_conf
);
4300 assoc_speaker
= assoc
;
4301 else if (assoc_speaker
!= assoc
)
4303 if (cfg
->speaker_outs
>= ARRAY_SIZE(cfg
->speaker_pins
))
4305 cfg
->speaker_pins
[cfg
->speaker_outs
] = nid
;
4306 sequences_speaker
[cfg
->speaker_outs
] = seq
;
4307 cfg
->speaker_outs
++;
4309 case AC_JACK_HP_OUT
:
4310 seq
= get_defcfg_sequence(def_conf
);
4311 assoc
= get_defcfg_association(def_conf
);
4312 if (cfg
->hp_outs
>= ARRAY_SIZE(cfg
->hp_pins
))
4314 cfg
->hp_pins
[cfg
->hp_outs
] = nid
;
4315 sequences_hp
[cfg
->hp_outs
] = (assoc
<< 4) | seq
;
4318 case AC_JACK_MIC_IN
: {
4320 if (loc
== AC_JACK_LOC_FRONT
||
4321 (loc
& 0x30) == AC_JACK_LOC_INTERNAL
) {
4322 preferred
= AUTO_PIN_FRONT_MIC
;
4325 preferred
= AUTO_PIN_MIC
;
4326 alt
= AUTO_PIN_FRONT_MIC
;
4328 if (!cfg
->input_pins
[preferred
])
4329 cfg
->input_pins
[preferred
] = nid
;
4330 else if (!cfg
->input_pins
[alt
])
4331 cfg
->input_pins
[alt
] = nid
;
4334 case AC_JACK_LINE_IN
:
4335 if (loc
== AC_JACK_LOC_FRONT
)
4336 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
] = nid
;
4338 cfg
->input_pins
[AUTO_PIN_LINE
] = nid
;
4341 cfg
->input_pins
[AUTO_PIN_CD
] = nid
;
4344 cfg
->input_pins
[AUTO_PIN_AUX
] = nid
;
4346 case AC_JACK_SPDIF_OUT
:
4347 case AC_JACK_DIG_OTHER_OUT
:
4348 if (cfg
->dig_outs
>= ARRAY_SIZE(cfg
->dig_out_pins
))
4350 cfg
->dig_out_pins
[cfg
->dig_outs
] = nid
;
4351 cfg
->dig_out_type
[cfg
->dig_outs
] =
4352 (loc
== AC_JACK_LOC_HDMI
) ?
4353 HDA_PCM_TYPE_HDMI
: HDA_PCM_TYPE_SPDIF
;
4356 case AC_JACK_SPDIF_IN
:
4357 case AC_JACK_DIG_OTHER_IN
:
4358 cfg
->dig_in_pin
= nid
;
4359 if (loc
== AC_JACK_LOC_HDMI
)
4360 cfg
->dig_in_type
= HDA_PCM_TYPE_HDMI
;
4362 cfg
->dig_in_type
= HDA_PCM_TYPE_SPDIF
;
4368 * If no line-out is defined but multiple HPs are found,
4369 * some of them might be the real line-outs.
4371 if (!cfg
->line_outs
&& cfg
->hp_outs
> 1) {
4373 while (i
< cfg
->hp_outs
) {
4374 /* The real HPs should have the sequence 0x0f */
4375 if ((sequences_hp
[i
] & 0x0f) == 0x0f) {
4379 /* Move it to the line-out table */
4380 cfg
->line_out_pins
[cfg
->line_outs
] = cfg
->hp_pins
[i
];
4381 sequences_line_out
[cfg
->line_outs
] = sequences_hp
[i
];
4384 memmove(cfg
->hp_pins
+ i
, cfg
->hp_pins
+ i
+ 1,
4385 sizeof(cfg
->hp_pins
[0]) * (cfg
->hp_outs
- i
));
4386 memmove(sequences_hp
+ i
- 1, sequences_hp
+ i
,
4387 sizeof(sequences_hp
[0]) * (cfg
->hp_outs
- i
));
4391 /* sort by sequence */
4392 sort_pins_by_sequence(cfg
->line_out_pins
, sequences_line_out
,
4394 sort_pins_by_sequence(cfg
->speaker_pins
, sequences_speaker
,
4396 sort_pins_by_sequence(cfg
->hp_pins
, sequences_hp
,
4399 /* if we have only one mic, make it AUTO_PIN_MIC */
4400 if (!cfg
->input_pins
[AUTO_PIN_MIC
] &&
4401 cfg
->input_pins
[AUTO_PIN_FRONT_MIC
]) {
4402 cfg
->input_pins
[AUTO_PIN_MIC
] =
4403 cfg
->input_pins
[AUTO_PIN_FRONT_MIC
];
4404 cfg
->input_pins
[AUTO_PIN_FRONT_MIC
] = 0;
4406 /* ditto for line-in */
4407 if (!cfg
->input_pins
[AUTO_PIN_LINE
] &&
4408 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
]) {
4409 cfg
->input_pins
[AUTO_PIN_LINE
] =
4410 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
];
4411 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
] = 0;
4415 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4416 * as a primary output
4418 if (!cfg
->line_outs
) {
4419 if (cfg
->speaker_outs
) {
4420 cfg
->line_outs
= cfg
->speaker_outs
;
4421 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
4422 sizeof(cfg
->speaker_pins
));
4423 cfg
->speaker_outs
= 0;
4424 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
4425 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
4426 } else if (cfg
->hp_outs
) {
4427 cfg
->line_outs
= cfg
->hp_outs
;
4428 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
4429 sizeof(cfg
->hp_pins
));
4431 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
4432 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4436 /* Reorder the surround channels
4437 * ALSA sequence is front/surr/clfe/side
4439 * 4-ch: front/surr => OK as it is
4440 * 6-ch: front/clfe/surr
4441 * 8-ch: front/clfe/rear/side|fc
4443 switch (cfg
->line_outs
) {
4446 nid
= cfg
->line_out_pins
[1];
4447 cfg
->line_out_pins
[1] = cfg
->line_out_pins
[2];
4448 cfg
->line_out_pins
[2] = nid
;
4453 * debug prints of the parsed results
4455 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4456 cfg
->line_outs
, cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
4457 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
4458 cfg
->line_out_pins
[4]);
4459 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4460 cfg
->speaker_outs
, cfg
->speaker_pins
[0],
4461 cfg
->speaker_pins
[1], cfg
->speaker_pins
[2],
4462 cfg
->speaker_pins
[3], cfg
->speaker_pins
[4]);
4463 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4464 cfg
->hp_outs
, cfg
->hp_pins
[0],
4465 cfg
->hp_pins
[1], cfg
->hp_pins
[2],
4466 cfg
->hp_pins
[3], cfg
->hp_pins
[4]);
4467 snd_printd(" mono: mono_out=0x%x\n", cfg
->mono_out_pin
);
4469 snd_printd(" dig-out=0x%x/0x%x\n",
4470 cfg
->dig_out_pins
[0], cfg
->dig_out_pins
[1]);
4471 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4472 " cd=0x%x, aux=0x%x\n",
4473 cfg
->input_pins
[AUTO_PIN_MIC
],
4474 cfg
->input_pins
[AUTO_PIN_FRONT_MIC
],
4475 cfg
->input_pins
[AUTO_PIN_LINE
],
4476 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
],
4477 cfg
->input_pins
[AUTO_PIN_CD
],
4478 cfg
->input_pins
[AUTO_PIN_AUX
]);
4479 if (cfg
->dig_in_pin
)
4480 snd_printd(" dig-in=0x%x\n", cfg
->dig_in_pin
);
4484 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config
);
4486 /* labels for input pins */
4487 const char *auto_pin_cfg_labels
[AUTO_PIN_LAST
] = {
4488 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4490 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels
);
4499 * snd_hda_suspend - suspend the codecs
4502 * Returns 0 if successful.
4504 int snd_hda_suspend(struct hda_bus
*bus
)
4506 struct hda_codec
*codec
;
4508 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4509 #ifdef CONFIG_SND_HDA_POWER_SAVE
4510 if (!codec
->power_on
)
4513 hda_call_codec_suspend(codec
);
4517 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
4520 * snd_hda_resume - resume the codecs
4523 * Returns 0 if successful.
4525 * This fucntion is defined only when POWER_SAVE isn't set.
4526 * In the power-save mode, the codec is resumed dynamically.
4528 int snd_hda_resume(struct hda_bus
*bus
)
4530 struct hda_codec
*codec
;
4532 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4533 if (snd_hda_codec_needs_resume(codec
))
4534 hda_call_codec_resume(codec
);
4538 EXPORT_SYMBOL_HDA(snd_hda_resume
);
4539 #endif /* CONFIG_PM */
4546 * snd_array_new - get a new element from the given array
4547 * @array: the array object
4549 * Get a new element from the given array. If it exceeds the
4550 * pre-allocated array size, re-allocate the array.
4552 * Returns NULL if allocation failed.
4554 void *snd_array_new(struct snd_array
*array
)
4556 if (array
->used
>= array
->alloced
) {
4557 int num
= array
->alloced
+ array
->alloc_align
;
4559 if (snd_BUG_ON(num
>= 4096))
4561 nlist
= kcalloc(num
+ 1, array
->elem_size
, GFP_KERNEL
);
4565 memcpy(nlist
, array
->list
,
4566 array
->elem_size
* array
->alloced
);
4569 array
->list
= nlist
;
4570 array
->alloced
= num
;
4572 return snd_array_elem(array
, array
->used
++);
4574 EXPORT_SYMBOL_HDA(snd_array_new
);
4577 * snd_array_free - free the given array elements
4578 * @array: the array object
4580 void snd_array_free(struct snd_array
*array
)
4587 EXPORT_SYMBOL_HDA(snd_array_free
);
4590 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4591 * @pcm: PCM caps bits
4592 * @buf: the string buffer to write
4593 * @buflen: the max buffer length
4595 * used by hda_proc.c and hda_eld.c
4597 void snd_print_pcm_rates(int pcm
, char *buf
, int buflen
)
4599 static unsigned int rates
[] = {
4600 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4601 96000, 176400, 192000, 384000
4605 for (i
= 0, j
= 0; i
< ARRAY_SIZE(rates
); i
++)
4607 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", rates
[i
]);
4609 buf
[j
] = '\0'; /* necessary when j == 0 */
4611 EXPORT_SYMBOL_HDA(snd_print_pcm_rates
);
4614 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4615 * @pcm: PCM caps bits
4616 * @buf: the string buffer to write
4617 * @buflen: the max buffer length
4619 * used by hda_proc.c and hda_eld.c
4621 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
4623 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
4626 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
4627 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
4628 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
4630 buf
[j
] = '\0'; /* necessary when j == 0 */
4632 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
4634 MODULE_DESCRIPTION("HDA codec core");
4635 MODULE_LICENSE("GPL");