2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/mutex.h>
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include <sound/asoundef.h>
30 #include <sound/tlv.h>
31 #include <sound/initval.h>
32 #include <sound/jack.h>
33 #include "hda_local.h"
35 #include <sound/hda_hwdep.h>
38 * vendor / preset table
41 struct hda_vendor_id
{
46 /* codec vendor labels */
47 static struct hda_vendor_id hda_vendor_ids
[] = {
49 { 0x1013, "Cirrus Logic" },
50 { 0x1057, "Motorola" },
51 { 0x1095, "Silicon Image" },
53 { 0x10ec, "Realtek" },
54 { 0x1102, "Creative" },
58 { 0x11d4, "Analog Devices" },
59 { 0x13f6, "C-Media" },
60 { 0x14f1, "Conexant" },
61 { 0x17e8, "Chrontel" },
63 { 0x1aec, "Wolfson Microelectronics" },
64 { 0x434d, "C-Media" },
66 { 0x8384, "SigmaTel" },
70 static DEFINE_MUTEX(preset_mutex
);
71 static LIST_HEAD(hda_preset_tables
);
73 int snd_hda_add_codec_preset(struct hda_codec_preset_list
*preset
)
75 mutex_lock(&preset_mutex
);
76 list_add_tail(&preset
->list
, &hda_preset_tables
);
77 mutex_unlock(&preset_mutex
);
80 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset
);
82 int snd_hda_delete_codec_preset(struct hda_codec_preset_list
*preset
)
84 mutex_lock(&preset_mutex
);
85 list_del(&preset
->list
);
86 mutex_unlock(&preset_mutex
);
89 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset
);
91 #ifdef CONFIG_SND_HDA_POWER_SAVE
92 static void hda_power_work(struct work_struct
*work
);
93 static void hda_keep_power_on(struct hda_codec
*codec
);
95 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
99 * snd_hda_get_jack_location - Give a location string of the jack
100 * @cfg: pin default config value
102 * Parse the pin default config value and returns the string of the
103 * jack location, e.g. "Rear", "Front", etc.
105 const char *snd_hda_get_jack_location(u32 cfg
)
107 static char *bases
[7] = {
108 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
110 static unsigned char specials_idx
[] = {
115 static char *specials
[] = {
116 "Rear Panel", "Drive Bar",
117 "Riser", "HDMI", "ATAPI",
118 "Mobile-In", "Mobile-Out"
121 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
122 if ((cfg
& 0x0f) < 7)
123 return bases
[cfg
& 0x0f];
124 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
125 if (cfg
== specials_idx
[i
])
130 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location
);
133 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
134 * @cfg: pin default config value
136 * Parse the pin default config value and returns the string of the
137 * jack connectivity, i.e. external or internal connection.
139 const char *snd_hda_get_jack_connectivity(u32 cfg
)
141 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
143 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
145 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity
);
148 * snd_hda_get_jack_type - Give a type string of the jack
149 * @cfg: pin default config value
151 * Parse the pin default config value and returns the string of the
152 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
154 const char *snd_hda_get_jack_type(u32 cfg
)
156 static char *jack_types
[16] = {
157 "Line Out", "Speaker", "HP Out", "CD",
158 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
159 "Line In", "Aux", "Mic", "Telephony",
160 "SPDIF In", "Digitial In", "Reserved", "Other"
163 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
164 >> AC_DEFCFG_DEVICE_SHIFT
];
166 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type
);
169 * Compose a 32bit command word to be sent to the HD-audio controller
171 static inline unsigned int
172 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
173 unsigned int verb
, unsigned int parm
)
177 if ((codec
->addr
& ~0xf) || (direct
& ~1) || (nid
& ~0x7f) ||
178 (verb
& ~0xfff) || (parm
& ~0xffff)) {
179 printk(KERN_ERR
"hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
180 codec
->addr
, direct
, nid
, verb
, parm
);
184 val
= (u32
)codec
->addr
<< 28;
185 val
|= (u32
)direct
<< 27;
186 val
|= (u32
)nid
<< 20;
193 * Send and receive a verb
195 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
198 struct hda_bus
*bus
= codec
->bus
;
207 snd_hda_power_up(codec
);
208 mutex_lock(&bus
->cmd_mutex
);
209 err
= bus
->ops
.command(bus
, cmd
);
211 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
212 mutex_unlock(&bus
->cmd_mutex
);
213 snd_hda_power_down(codec
);
214 if (res
&& *res
== -1 && bus
->rirb_error
) {
215 if (bus
->response_reset
) {
216 snd_printd("hda_codec: resetting BUS due to "
217 "fatal communication error\n");
218 bus
->ops
.bus_reset(bus
);
222 /* clear reset-flag when the communication gets recovered */
224 bus
->response_reset
= 0;
229 * snd_hda_codec_read - send a command and get the response
230 * @codec: the HDA codec
231 * @nid: NID to send the command
232 * @direct: direct flag
233 * @verb: the verb to send
234 * @parm: the parameter for the verb
236 * Send a single command and read the corresponding response.
238 * Returns the obtained response value, or -1 for an error.
240 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
242 unsigned int verb
, unsigned int parm
)
244 unsigned cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
246 codec_exec_verb(codec
, cmd
, &res
);
249 EXPORT_SYMBOL_HDA(snd_hda_codec_read
);
252 * snd_hda_codec_write - send a single command without waiting for response
253 * @codec: the HDA codec
254 * @nid: NID to send the command
255 * @direct: direct flag
256 * @verb: the verb to send
257 * @parm: the parameter for the verb
259 * Send a single command without waiting for response.
261 * Returns 0 if successful, or a negative error code.
263 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
264 unsigned int verb
, unsigned int parm
)
266 unsigned int cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
268 return codec_exec_verb(codec
, cmd
,
269 codec
->bus
->sync_write
? &res
: NULL
);
271 EXPORT_SYMBOL_HDA(snd_hda_codec_write
);
274 * snd_hda_sequence_write - sequence writes
275 * @codec: the HDA codec
276 * @seq: VERB array to send
278 * Send the commands sequentially from the given array.
279 * The array must be terminated with NID=0.
281 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
283 for (; seq
->nid
; seq
++)
284 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
286 EXPORT_SYMBOL_HDA(snd_hda_sequence_write
);
289 * snd_hda_get_sub_nodes - get the range of sub nodes
290 * @codec: the HDA codec
292 * @start_id: the pointer to store the start NID
294 * Parse the NID and store the start NID of its sub-nodes.
295 * Returns the number of sub-nodes.
297 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
302 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
305 *start_id
= (parm
>> 16) & 0x7fff;
306 return (int)(parm
& 0x7fff);
308 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
311 * snd_hda_get_connections - get connection list
312 * @codec: the HDA codec
314 * @conn_list: connection list array
315 * @max_conns: max. number of connections to store
317 * Parses the connection list of the given widget and stores the list
320 * Returns the number of connections, or a negative error code.
322 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
323 hda_nid_t
*conn_list
, int max_conns
)
326 int i
, conn_len
, conns
;
327 unsigned int shift
, num_elems
, mask
;
331 if (snd_BUG_ON(!conn_list
|| max_conns
<= 0))
334 wcaps
= get_wcaps(codec
, nid
);
335 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
336 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
) {
337 snd_printk(KERN_WARNING
"hda_codec: "
338 "connection list not available for 0x%x\n", nid
);
342 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
343 if (parm
& AC_CLIST_LONG
) {
352 conn_len
= parm
& AC_CLIST_LENGTH
;
353 mask
= (1 << (shift
-1)) - 1;
356 return 0; /* no connection */
359 /* single connection */
360 parm
= snd_hda_codec_read(codec
, nid
, 0,
361 AC_VERB_GET_CONNECT_LIST
, 0);
362 if (parm
== -1 && codec
->bus
->rirb_error
)
364 conn_list
[0] = parm
& mask
;
368 /* multi connection */
371 for (i
= 0; i
< conn_len
; i
++) {
375 if (i
% num_elems
== 0) {
376 parm
= snd_hda_codec_read(codec
, nid
, 0,
377 AC_VERB_GET_CONNECT_LIST
, i
);
378 if (parm
== -1 && codec
->bus
->rirb_error
)
381 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
384 snd_printk(KERN_WARNING
"hda_codec: "
385 "invalid CONNECT_LIST verb %x[%i]:%x\n",
391 /* ranges between the previous and this one */
392 if (!prev_nid
|| prev_nid
>= val
) {
393 snd_printk(KERN_WARNING
"hda_codec: "
394 "invalid dep_range_val %x:%x\n",
398 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
399 if (conns
>= max_conns
) {
400 snd_printk(KERN_ERR
"hda_codec: "
401 "Too many connections %d for NID 0x%x\n",
405 conn_list
[conns
++] = n
;
408 if (conns
>= max_conns
) {
409 snd_printk(KERN_ERR
"hda_codec: "
410 "Too many connections %d for NID 0x%x\n",
414 conn_list
[conns
++] = val
;
420 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
424 * snd_hda_queue_unsol_event - add an unsolicited event to queue
426 * @res: unsolicited event (lower 32bit of RIRB entry)
427 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
429 * Adds the given event to the queue. The events are processed in
430 * the workqueue asynchronously. Call this function in the interrupt
431 * hanlder when RIRB receives an unsolicited event.
433 * Returns 0 if successful, or a negative error code.
435 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
437 struct hda_bus_unsolicited
*unsol
;
444 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
448 unsol
->queue
[wp
] = res
;
449 unsol
->queue
[wp
+ 1] = res_ex
;
451 queue_work(bus
->workq
, &unsol
->work
);
455 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event
);
458 * process queued unsolicited events
460 static void process_unsol_events(struct work_struct
*work
)
462 struct hda_bus_unsolicited
*unsol
=
463 container_of(work
, struct hda_bus_unsolicited
, work
);
464 struct hda_bus
*bus
= unsol
->bus
;
465 struct hda_codec
*codec
;
466 unsigned int rp
, caddr
, res
;
468 while (unsol
->rp
!= unsol
->wp
) {
469 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
472 res
= unsol
->queue
[rp
];
473 caddr
= unsol
->queue
[rp
+ 1];
474 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
476 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
477 if (codec
&& codec
->patch_ops
.unsol_event
)
478 codec
->patch_ops
.unsol_event(codec
, res
);
483 * initialize unsolicited queue
485 static int init_unsol_queue(struct hda_bus
*bus
)
487 struct hda_bus_unsolicited
*unsol
;
489 if (bus
->unsol
) /* already initialized */
492 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
494 snd_printk(KERN_ERR
"hda_codec: "
495 "can't allocate unsolicited queue\n");
498 INIT_WORK(&unsol
->work
, process_unsol_events
);
507 static void snd_hda_codec_free(struct hda_codec
*codec
);
509 static int snd_hda_bus_free(struct hda_bus
*bus
)
511 struct hda_codec
*codec
, *n
;
516 flush_workqueue(bus
->workq
);
519 list_for_each_entry_safe(codec
, n
, &bus
->codec_list
, list
) {
520 snd_hda_codec_free(codec
);
522 if (bus
->ops
.private_free
)
523 bus
->ops
.private_free(bus
);
525 destroy_workqueue(bus
->workq
);
530 static int snd_hda_bus_dev_free(struct snd_device
*device
)
532 struct hda_bus
*bus
= device
->device_data
;
534 return snd_hda_bus_free(bus
);
537 #ifdef CONFIG_SND_HDA_HWDEP
538 static int snd_hda_bus_dev_register(struct snd_device
*device
)
540 struct hda_bus
*bus
= device
->device_data
;
541 struct hda_codec
*codec
;
542 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
543 snd_hda_hwdep_add_sysfs(codec
);
544 snd_hda_hwdep_add_power_sysfs(codec
);
549 #define snd_hda_bus_dev_register NULL
553 * snd_hda_bus_new - create a HDA bus
554 * @card: the card entry
555 * @temp: the template for hda_bus information
556 * @busp: the pointer to store the created bus instance
558 * Returns 0 if successful, or a negative error code.
560 int /*__devinit*/ snd_hda_bus_new(struct snd_card
*card
,
561 const struct hda_bus_template
*temp
,
562 struct hda_bus
**busp
)
566 static struct snd_device_ops dev_ops
= {
567 .dev_register
= snd_hda_bus_dev_register
,
568 .dev_free
= snd_hda_bus_dev_free
,
571 if (snd_BUG_ON(!temp
))
573 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
579 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
581 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
586 bus
->private_data
= temp
->private_data
;
587 bus
->pci
= temp
->pci
;
588 bus
->modelname
= temp
->modelname
;
589 bus
->power_save
= temp
->power_save
;
590 bus
->ops
= temp
->ops
;
592 mutex_init(&bus
->cmd_mutex
);
593 mutex_init(&bus
->prepare_mutex
);
594 INIT_LIST_HEAD(&bus
->codec_list
);
596 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
597 "hd-audio%d", card
->number
);
598 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
600 snd_printk(KERN_ERR
"cannot create workqueue %s\n",
606 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
608 snd_hda_bus_free(bus
);
615 EXPORT_SYMBOL_HDA(snd_hda_bus_new
);
617 #ifdef CONFIG_SND_HDA_GENERIC
618 #define is_generic_config(codec) \
619 (codec->modelname && !strcmp(codec->modelname, "generic"))
621 #define is_generic_config(codec) 0
625 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
627 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
631 * find a matching codec preset
633 static const struct hda_codec_preset
*
634 find_codec_preset(struct hda_codec
*codec
)
636 struct hda_codec_preset_list
*tbl
;
637 const struct hda_codec_preset
*preset
;
638 int mod_requested
= 0;
640 if (is_generic_config(codec
))
641 return NULL
; /* use the generic parser */
644 mutex_lock(&preset_mutex
);
645 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
646 if (!try_module_get(tbl
->owner
)) {
647 snd_printk(KERN_ERR
"hda_codec: cannot module_get\n");
650 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
651 u32 mask
= preset
->mask
;
652 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
654 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
658 if (preset
->id
== (codec
->vendor_id
& mask
) &&
660 preset
->rev
== codec
->revision_id
)) {
661 mutex_unlock(&preset_mutex
);
662 codec
->owner
= tbl
->owner
;
666 module_put(tbl
->owner
);
668 mutex_unlock(&preset_mutex
);
670 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
673 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%08x",
676 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%04x*",
677 (codec
->vendor_id
>> 16) & 0xffff);
678 request_module(name
);
686 * get_codec_name - store the codec name
688 static int get_codec_name(struct hda_codec
*codec
)
690 const struct hda_vendor_id
*c
;
691 const char *vendor
= NULL
;
692 u16 vendor_id
= codec
->vendor_id
>> 16;
695 if (codec
->vendor_name
)
698 for (c
= hda_vendor_ids
; c
->id
; c
++) {
699 if (c
->id
== vendor_id
) {
705 sprintf(tmp
, "Generic %04x", vendor_id
);
708 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
709 if (!codec
->vendor_name
)
713 if (codec
->chip_name
)
716 if (codec
->preset
&& codec
->preset
->name
)
717 codec
->chip_name
= kstrdup(codec
->preset
->name
, GFP_KERNEL
);
719 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
720 codec
->chip_name
= kstrdup(tmp
, GFP_KERNEL
);
722 if (!codec
->chip_name
)
728 * look for an AFG and MFG nodes
730 static void /*__devinit*/ setup_fg_nodes(struct hda_codec
*codec
)
732 int i
, total_nodes
, function_id
;
735 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
736 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
737 function_id
= snd_hda_param_read(codec
, nid
,
738 AC_PAR_FUNCTION_TYPE
);
739 switch (function_id
& 0xff) {
740 case AC_GRP_AUDIO_FUNCTION
:
742 codec
->afg_function_id
= function_id
& 0xff;
743 codec
->afg_unsol
= (function_id
>> 8) & 1;
745 case AC_GRP_MODEM_FUNCTION
:
747 codec
->mfg_function_id
= function_id
& 0xff;
748 codec
->mfg_unsol
= (function_id
>> 8) & 1;
757 * read widget caps for each widget and store in cache
759 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
764 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
766 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
769 nid
= codec
->start_nid
;
770 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
771 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
772 AC_PAR_AUDIO_WIDGET_CAP
);
776 /* read all pin default configurations and save codec->init_pins */
777 static int read_pin_defaults(struct hda_codec
*codec
)
780 hda_nid_t nid
= codec
->start_nid
;
782 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
783 struct hda_pincfg
*pin
;
784 unsigned int wcaps
= get_wcaps(codec
, nid
);
785 unsigned int wid_type
= get_wcaps_type(wcaps
);
786 if (wid_type
!= AC_WID_PIN
)
788 pin
= snd_array_new(&codec
->init_pins
);
792 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
793 AC_VERB_GET_CONFIG_DEFAULT
, 0);
794 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
795 AC_VERB_GET_PIN_WIDGET_CONTROL
,
801 /* look up the given pin config list and return the item matching with NID */
802 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
803 struct snd_array
*array
,
807 for (i
= 0; i
< array
->used
; i
++) {
808 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
815 /* write a config value for the given NID */
816 static void set_pincfg(struct hda_codec
*codec
, hda_nid_t nid
,
820 for (i
= 0; i
< 4; i
++) {
821 snd_hda_codec_write(codec
, nid
, 0,
822 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0
+ i
,
828 /* set the current pin config value for the given NID.
829 * the value is cached, and read via snd_hda_codec_get_pincfg()
831 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
832 hda_nid_t nid
, unsigned int cfg
)
834 struct hda_pincfg
*pin
;
837 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_PIN
)
840 oldcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
841 pin
= look_up_pincfg(codec
, list
, nid
);
843 pin
= snd_array_new(list
);
850 /* change only when needed; e.g. if the pincfg is already present
851 * in user_pins[], don't write it
853 cfg
= snd_hda_codec_get_pincfg(codec
, nid
);
855 set_pincfg(codec
, nid
, cfg
);
860 * snd_hda_codec_set_pincfg - Override a pin default configuration
861 * @codec: the HDA codec
862 * @nid: NID to set the pin config
863 * @cfg: the pin default config value
865 * Override a pin default configuration value in the cache.
866 * This value can be read by snd_hda_codec_get_pincfg() in a higher
867 * priority than the real hardware value.
869 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
870 hda_nid_t nid
, unsigned int cfg
)
872 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
874 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg
);
877 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
878 * @codec: the HDA codec
879 * @nid: NID to get the pin config
881 * Get the current pin config value of the given pin NID.
882 * If the pincfg value is cached or overridden via sysfs or driver,
883 * returns the cached value.
885 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
887 struct hda_pincfg
*pin
;
889 #ifdef CONFIG_SND_HDA_HWDEP
890 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
894 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
897 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
902 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg
);
904 /* restore all current pin configs */
905 static void restore_pincfgs(struct hda_codec
*codec
)
908 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
909 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
910 set_pincfg(codec
, pin
->nid
,
911 snd_hda_codec_get_pincfg(codec
, pin
->nid
));
916 * snd_hda_shutup_pins - Shut up all pins
917 * @codec: the HDA codec
919 * Clear all pin controls to shup up before suspend for avoiding click noise.
920 * The controls aren't cached so that they can be resumed properly.
922 void snd_hda_shutup_pins(struct hda_codec
*codec
)
925 /* don't shut up pins when unloading the driver; otherwise it breaks
926 * the default pin setup at the next load of the driver
928 if (codec
->bus
->shutdown
)
930 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
931 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
932 /* use read here for syncing after issuing each verb */
933 snd_hda_codec_read(codec
, pin
->nid
, 0,
934 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
936 codec
->pins_shutup
= 1;
938 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins
);
940 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
941 static void restore_shutup_pins(struct hda_codec
*codec
)
944 if (!codec
->pins_shutup
)
946 if (codec
->bus
->shutdown
)
948 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
949 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
950 snd_hda_codec_write(codec
, pin
->nid
, 0,
951 AC_VERB_SET_PIN_WIDGET_CONTROL
,
954 codec
->pins_shutup
= 0;
957 static void init_hda_cache(struct hda_cache_rec
*cache
,
958 unsigned int record_size
);
959 static void free_hda_cache(struct hda_cache_rec
*cache
);
961 /* restore the initial pin cfgs and release all pincfg lists */
962 static void restore_init_pincfgs(struct hda_codec
*codec
)
964 /* first free driver_pins and user_pins, then call restore_pincfg
965 * so that only the values in init_pins are restored
967 snd_array_free(&codec
->driver_pins
);
968 #ifdef CONFIG_SND_HDA_HWDEP
969 snd_array_free(&codec
->user_pins
);
971 restore_pincfgs(codec
);
972 snd_array_free(&codec
->init_pins
);
976 * audio-converter setup caches
978 struct hda_cvt_setup
{
983 unsigned char active
; /* cvt is currently used */
984 unsigned char dirty
; /* setups should be cleared */
987 /* get or create a cache entry for the given audio converter NID */
988 static struct hda_cvt_setup
*
989 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
991 struct hda_cvt_setup
*p
;
994 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
995 p
= snd_array_elem(&codec
->cvt_setups
, i
);
999 p
= snd_array_new(&codec
->cvt_setups
);
1008 static void snd_hda_codec_free(struct hda_codec
*codec
)
1012 restore_init_pincfgs(codec
);
1013 #ifdef CONFIG_SND_HDA_POWER_SAVE
1014 cancel_delayed_work(&codec
->power_work
);
1015 flush_workqueue(codec
->bus
->workq
);
1017 list_del(&codec
->list
);
1018 snd_array_free(&codec
->mixers
);
1019 snd_array_free(&codec
->nids
);
1020 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1021 if (codec
->patch_ops
.free
)
1022 codec
->patch_ops
.free(codec
);
1023 module_put(codec
->owner
);
1024 free_hda_cache(&codec
->amp_cache
);
1025 free_hda_cache(&codec
->cmd_cache
);
1026 kfree(codec
->vendor_name
);
1027 kfree(codec
->chip_name
);
1028 kfree(codec
->modelname
);
1029 kfree(codec
->wcaps
);
1033 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
1034 unsigned int power_state
);
1037 * snd_hda_codec_new - create a HDA codec
1038 * @bus: the bus to assign
1039 * @codec_addr: the codec address
1040 * @codecp: the pointer to store the generated codec
1042 * Returns 0 if successful, or a negative error code.
1044 int /*__devinit*/ snd_hda_codec_new(struct hda_bus
*bus
,
1045 unsigned int codec_addr
,
1046 struct hda_codec
**codecp
)
1048 struct hda_codec
*codec
;
1052 if (snd_BUG_ON(!bus
))
1054 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1057 if (bus
->caddr_tbl
[codec_addr
]) {
1058 snd_printk(KERN_ERR
"hda_codec: "
1059 "address 0x%x is already occupied\n", codec_addr
);
1063 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1064 if (codec
== NULL
) {
1065 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
1070 codec
->addr
= codec_addr
;
1071 mutex_init(&codec
->spdif_mutex
);
1072 mutex_init(&codec
->control_mutex
);
1073 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1074 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1075 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1076 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1077 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1078 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1079 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1080 if (codec
->bus
->modelname
) {
1081 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1082 if (!codec
->modelname
) {
1083 snd_hda_codec_free(codec
);
1088 #ifdef CONFIG_SND_HDA_POWER_SAVE
1089 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1090 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1091 * the caller has to power down appropriatley after initialization
1094 hda_keep_power_on(codec
);
1097 list_add_tail(&codec
->list
, &bus
->codec_list
);
1098 bus
->caddr_tbl
[codec_addr
] = codec
;
1100 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1102 if (codec
->vendor_id
== -1)
1103 /* read again, hopefully the access method was corrected
1104 * in the last read...
1106 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1108 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1109 AC_PAR_SUBSYSTEM_ID
);
1110 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1113 setup_fg_nodes(codec
);
1114 if (!codec
->afg
&& !codec
->mfg
) {
1115 snd_printdd("hda_codec: no AFG or MFG node found\n");
1120 err
= read_widget_caps(codec
, codec
->afg
? codec
->afg
: codec
->mfg
);
1122 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1125 err
= read_pin_defaults(codec
);
1129 if (!codec
->subsystem_id
) {
1130 hda_nid_t nid
= codec
->afg
? codec
->afg
: codec
->mfg
;
1131 codec
->subsystem_id
=
1132 snd_hda_codec_read(codec
, nid
, 0,
1133 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1136 /* power-up all before initialization */
1137 hda_set_power_state(codec
,
1138 codec
->afg
? codec
->afg
: codec
->mfg
,
1141 snd_hda_codec_proc_new(codec
);
1143 snd_hda_create_hwdep(codec
);
1145 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1146 codec
->subsystem_id
, codec
->revision_id
);
1147 snd_component_add(codec
->bus
->card
, component
);
1154 snd_hda_codec_free(codec
);
1157 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
1160 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1161 * @codec: the HDA codec
1163 * Start parsing of the given codec tree and (re-)initialize the whole
1166 * Returns 0 if successful or a negative error code.
1168 int snd_hda_codec_configure(struct hda_codec
*codec
)
1172 codec
->preset
= find_codec_preset(codec
);
1173 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1174 err
= get_codec_name(codec
);
1179 if (is_generic_config(codec
)) {
1180 err
= snd_hda_parse_generic_codec(codec
);
1183 if (codec
->preset
&& codec
->preset
->patch
) {
1184 err
= codec
->preset
->patch(codec
);
1188 /* call the default parser */
1189 err
= snd_hda_parse_generic_codec(codec
);
1191 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1194 if (!err
&& codec
->patch_ops
.unsol_event
)
1195 err
= init_unsol_queue(codec
->bus
);
1196 /* audio codec should override the mixer name */
1197 if (!err
&& (codec
->afg
|| !*codec
->bus
->card
->mixername
))
1198 snprintf(codec
->bus
->card
->mixername
,
1199 sizeof(codec
->bus
->card
->mixername
),
1200 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1203 EXPORT_SYMBOL_HDA(snd_hda_codec_configure
);
1206 * snd_hda_codec_setup_stream - set up the codec for streaming
1207 * @codec: the CODEC to set up
1208 * @nid: the NID to set up
1209 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1210 * @channel_id: channel id to pass, zero based.
1211 * @format: stream format.
1213 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1215 int channel_id
, int format
)
1217 struct hda_codec
*c
;
1218 struct hda_cvt_setup
*p
;
1219 unsigned int oldval
, newval
;
1226 snd_printdd("hda_codec_setup_stream: "
1227 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1228 nid
, stream_tag
, channel_id
, format
);
1229 p
= get_hda_cvt_setup(codec
, nid
);
1232 /* update the stream-id if changed */
1233 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1234 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1235 newval
= (stream_tag
<< 4) | channel_id
;
1236 if (oldval
!= newval
)
1237 snd_hda_codec_write(codec
, nid
, 0,
1238 AC_VERB_SET_CHANNEL_STREAMID
,
1240 p
->stream_tag
= stream_tag
;
1241 p
->channel_id
= channel_id
;
1243 /* update the format-id if changed */
1244 if (p
->format_id
!= format
) {
1245 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1246 AC_VERB_GET_STREAM_FORMAT
, 0);
1247 if (oldval
!= format
) {
1249 snd_hda_codec_write(codec
, nid
, 0,
1250 AC_VERB_SET_STREAM_FORMAT
,
1253 p
->format_id
= format
;
1258 /* make other inactive cvts with the same stream-tag dirty */
1259 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1260 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1261 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1262 p
= snd_array_elem(&c
->cvt_setups
, i
);
1263 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1264 get_wcaps_type(get_wcaps(codec
, p
->nid
)) == type
)
1269 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1271 static void really_cleanup_stream(struct hda_codec
*codec
,
1272 struct hda_cvt_setup
*q
);
1275 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1276 * @codec: the CODEC to clean up
1277 * @nid: the NID to clean up
1278 * @do_now: really clean up the stream instead of clearing the active flag
1280 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1283 struct hda_cvt_setup
*p
;
1288 if (codec
->no_sticky_stream
)
1291 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1292 p
= get_hda_cvt_setup(codec
, nid
);
1294 /* here we just clear the active flag when do_now isn't set;
1295 * actual clean-ups will be done later in
1296 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1299 really_cleanup_stream(codec
, p
);
1304 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream
);
1306 static void really_cleanup_stream(struct hda_codec
*codec
,
1307 struct hda_cvt_setup
*q
)
1309 hda_nid_t nid
= q
->nid
;
1310 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1311 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0);
1312 memset(q
, 0, sizeof(*q
));
1316 /* clean up the all conflicting obsolete streams */
1317 static void purify_inactive_streams(struct hda_codec
*codec
)
1319 struct hda_codec
*c
;
1322 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1323 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1324 struct hda_cvt_setup
*p
;
1325 p
= snd_array_elem(&c
->cvt_setups
, i
);
1327 really_cleanup_stream(c
, p
);
1332 /* clean up all streams; called from suspend */
1333 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1337 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1338 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1340 really_cleanup_stream(codec
, p
);
1345 * amp access functions
1348 /* FIXME: more better hash key? */
1349 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1350 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1351 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1352 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1353 #define INFO_AMP_CAPS (1<<0)
1354 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1356 /* initialize the hash table */
1357 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec
*cache
,
1358 unsigned int record_size
)
1360 memset(cache
, 0, sizeof(*cache
));
1361 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1362 snd_array_init(&cache
->buf
, record_size
, 64);
1365 static void free_hda_cache(struct hda_cache_rec
*cache
)
1367 snd_array_free(&cache
->buf
);
1370 /* query the hash. allocate an entry if not found. */
1371 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1373 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1374 u16 cur
= cache
->hash
[idx
];
1375 struct hda_cache_head
*info
;
1377 while (cur
!= 0xffff) {
1378 info
= snd_array_elem(&cache
->buf
, cur
);
1379 if (info
->key
== key
)
1386 /* query the hash. allocate an entry if not found. */
1387 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1390 struct hda_cache_head
*info
= get_hash(cache
, key
);
1393 /* add a new hash entry */
1394 info
= snd_array_new(&cache
->buf
);
1397 cur
= snd_array_index(&cache
->buf
, info
);
1400 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1401 info
->next
= cache
->hash
[idx
];
1402 cache
->hash
[idx
] = cur
;
1407 /* query and allocate an amp hash entry */
1408 static inline struct hda_amp_info
*
1409 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1411 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1415 * query_amp_caps - query AMP capabilities
1416 * @codec: the HD-auio codec
1417 * @nid: the NID to query
1418 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1420 * Query AMP capabilities for the given widget and direction.
1421 * Returns the obtained capability bits.
1423 * When cap bits have been already read, this doesn't read again but
1424 * returns the cached value.
1426 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1428 struct hda_amp_info
*info
;
1430 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, 0));
1433 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1434 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1436 info
->amp_caps
= snd_hda_param_read(codec
, nid
,
1437 direction
== HDA_OUTPUT
?
1438 AC_PAR_AMP_OUT_CAP
:
1441 info
->head
.val
|= INFO_AMP_CAPS
;
1443 return info
->amp_caps
;
1445 EXPORT_SYMBOL_HDA(query_amp_caps
);
1448 * snd_hda_override_amp_caps - Override the AMP capabilities
1449 * @codec: the CODEC to clean up
1450 * @nid: the NID to clean up
1451 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1452 * @caps: the capability bits to set
1454 * Override the cached AMP caps bits value by the given one.
1455 * This function is useful if the driver needs to adjust the AMP ranges,
1456 * e.g. limit to 0dB, etc.
1458 * Returns zero if successful or a negative error code.
1460 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1463 struct hda_amp_info
*info
;
1465 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0));
1468 info
->amp_caps
= caps
;
1469 info
->head
.val
|= INFO_AMP_CAPS
;
1472 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1475 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, u32 key
,
1476 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
))
1478 struct hda_amp_info
*info
;
1480 info
= get_alloc_amp_hash(codec
, key
);
1483 if (!info
->head
.val
) {
1484 info
->head
.val
|= INFO_AMP_CAPS
;
1485 info
->amp_caps
= func(codec
, nid
);
1487 return info
->amp_caps
;
1490 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
)
1492 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1496 * snd_hda_query_pin_caps - Query PIN capabilities
1497 * @codec: the HD-auio codec
1498 * @nid: the NID to query
1500 * Query PIN capabilities for the given widget.
1501 * Returns the obtained capability bits.
1503 * When cap bits have been already read, this doesn't read again but
1504 * returns the cached value.
1506 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1508 return query_caps_hash(codec
, nid
, HDA_HASH_PINCAP_KEY(nid
),
1511 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1514 * snd_hda_pin_sense - execute pin sense measurement
1515 * @codec: the CODEC to sense
1516 * @nid: the pin NID to sense
1518 * Execute necessary pin sense measurement and return its Presence Detect,
1519 * Impedance, ELD Valid etc. status bits.
1521 u32
snd_hda_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
1525 if (!codec
->no_trigger_sense
) {
1526 pincap
= snd_hda_query_pin_caps(codec
, nid
);
1527 if (pincap
& AC_PINCAP_TRIG_REQ
) /* need trigger? */
1528 snd_hda_codec_read(codec
, nid
, 0,
1529 AC_VERB_SET_PIN_SENSE
, 0);
1531 return snd_hda_codec_read(codec
, nid
, 0,
1532 AC_VERB_GET_PIN_SENSE
, 0);
1534 EXPORT_SYMBOL_HDA(snd_hda_pin_sense
);
1537 * snd_hda_jack_detect - query pin Presence Detect status
1538 * @codec: the CODEC to sense
1539 * @nid: the pin NID to sense
1541 * Query and return the pin's Presence Detect status.
1543 int snd_hda_jack_detect(struct hda_codec
*codec
, hda_nid_t nid
)
1545 u32 sense
= snd_hda_pin_sense(codec
, nid
);
1546 return !!(sense
& AC_PINSENSE_PRESENCE
);
1548 EXPORT_SYMBOL_HDA(snd_hda_jack_detect
);
1551 * read the current volume to info
1552 * if the cache exists, read the cache value.
1554 static unsigned int get_vol_mute(struct hda_codec
*codec
,
1555 struct hda_amp_info
*info
, hda_nid_t nid
,
1556 int ch
, int direction
, int index
)
1560 if (info
->head
.val
& INFO_AMP_VOL(ch
))
1561 return info
->vol
[ch
];
1563 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1564 parm
|= direction
== HDA_OUTPUT
? AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1566 val
= snd_hda_codec_read(codec
, nid
, 0,
1567 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1568 info
->vol
[ch
] = val
& 0xff;
1569 info
->head
.val
|= INFO_AMP_VOL(ch
);
1570 return info
->vol
[ch
];
1574 * write the current volume in info to the h/w and update the cache
1576 static void put_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
1577 hda_nid_t nid
, int ch
, int direction
, int index
,
1582 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1583 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1584 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1586 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1587 info
->vol
[ch
] = val
;
1591 * snd_hda_codec_amp_read - Read AMP value
1592 * @codec: HD-audio codec
1593 * @nid: NID to read the AMP value
1594 * @ch: channel (left=0 or right=1)
1595 * @direction: #HDA_INPUT or #HDA_OUTPUT
1596 * @index: the index value (only for input direction)
1598 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1600 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1601 int direction
, int index
)
1603 struct hda_amp_info
*info
;
1604 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1607 return get_vol_mute(codec
, info
, nid
, ch
, direction
, index
);
1609 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
1612 * snd_hda_codec_amp_update - update the AMP value
1613 * @codec: HD-audio codec
1614 * @nid: NID to read the AMP value
1615 * @ch: channel (left=0 or right=1)
1616 * @direction: #HDA_INPUT or #HDA_OUTPUT
1617 * @idx: the index value (only for input direction)
1618 * @mask: bit mask to set
1619 * @val: the bits value to set
1621 * Update the AMP value with a bit mask.
1622 * Returns 0 if the value is unchanged, 1 if changed.
1624 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1625 int direction
, int idx
, int mask
, int val
)
1627 struct hda_amp_info
*info
;
1629 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, idx
));
1632 if (snd_BUG_ON(mask
& ~0xff))
1635 val
|= get_vol_mute(codec
, info
, nid
, ch
, direction
, idx
) & ~mask
;
1636 if (info
->vol
[ch
] == val
)
1638 put_vol_mute(codec
, info
, nid
, ch
, direction
, idx
, val
);
1641 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
1644 * snd_hda_codec_amp_stereo - update the AMP stereo values
1645 * @codec: HD-audio codec
1646 * @nid: NID to read the AMP value
1647 * @direction: #HDA_INPUT or #HDA_OUTPUT
1648 * @idx: the index value (only for input direction)
1649 * @mask: bit mask to set
1650 * @val: the bits value to set
1652 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1653 * stereo widget with the same mask and value.
1655 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1656 int direction
, int idx
, int mask
, int val
)
1660 if (snd_BUG_ON(mask
& ~0xff))
1662 for (ch
= 0; ch
< 2; ch
++)
1663 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1667 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
1669 #ifdef SND_HDA_NEEDS_RESUME
1671 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1672 * @codec: HD-audio codec
1674 * Resume the all amp commands from the cache.
1676 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
1678 struct hda_amp_info
*buffer
= codec
->amp_cache
.buf
.list
;
1681 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++, buffer
++) {
1682 u32 key
= buffer
->head
.key
;
1684 unsigned int idx
, dir
, ch
;
1688 idx
= (key
>> 16) & 0xff;
1689 dir
= (key
>> 24) & 0xff;
1690 for (ch
= 0; ch
< 2; ch
++) {
1691 if (!(buffer
->head
.val
& INFO_AMP_VOL(ch
)))
1693 put_vol_mute(codec
, buffer
, nid
, ch
, dir
, idx
,
1698 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
1699 #endif /* SND_HDA_NEEDS_RESUME */
1701 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1704 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1706 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1713 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1715 * The control element is supposed to have the private_value field
1716 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1718 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1719 struct snd_ctl_elem_info
*uinfo
)
1721 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1722 u16 nid
= get_amp_nid(kcontrol
);
1723 u8 chs
= get_amp_channels(kcontrol
);
1724 int dir
= get_amp_direction(kcontrol
);
1725 unsigned int ofs
= get_amp_offset(kcontrol
);
1727 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1728 uinfo
->count
= chs
== 3 ? 2 : 1;
1729 uinfo
->value
.integer
.min
= 0;
1730 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
1731 if (!uinfo
->value
.integer
.max
) {
1732 printk(KERN_WARNING
"hda_codec: "
1733 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
1739 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
1742 static inline unsigned int
1743 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1744 int ch
, int dir
, int idx
, unsigned int ofs
)
1747 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1748 val
&= HDA_AMP_VOLMASK
;
1757 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1758 int ch
, int dir
, int idx
, unsigned int ofs
,
1761 unsigned int maxval
;
1765 /* ofs = 0: raw max value */
1766 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
1769 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
1770 HDA_AMP_VOLMASK
, val
);
1774 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1776 * The control element is supposed to have the private_value field
1777 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1779 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
1780 struct snd_ctl_elem_value
*ucontrol
)
1782 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1783 hda_nid_t nid
= get_amp_nid(kcontrol
);
1784 int chs
= get_amp_channels(kcontrol
);
1785 int dir
= get_amp_direction(kcontrol
);
1786 int idx
= get_amp_index(kcontrol
);
1787 unsigned int ofs
= get_amp_offset(kcontrol
);
1788 long *valp
= ucontrol
->value
.integer
.value
;
1791 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
1793 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
1796 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
1799 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1801 * The control element is supposed to have the private_value field
1802 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1804 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
1805 struct snd_ctl_elem_value
*ucontrol
)
1807 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1808 hda_nid_t nid
= get_amp_nid(kcontrol
);
1809 int chs
= get_amp_channels(kcontrol
);
1810 int dir
= get_amp_direction(kcontrol
);
1811 int idx
= get_amp_index(kcontrol
);
1812 unsigned int ofs
= get_amp_offset(kcontrol
);
1813 long *valp
= ucontrol
->value
.integer
.value
;
1816 snd_hda_power_up(codec
);
1818 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
1822 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
1823 snd_hda_power_down(codec
);
1826 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
1829 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1831 * The control element is supposed to have the private_value field
1832 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1834 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
1835 unsigned int size
, unsigned int __user
*_tlv
)
1837 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1838 hda_nid_t nid
= get_amp_nid(kcontrol
);
1839 int dir
= get_amp_direction(kcontrol
);
1840 unsigned int ofs
= get_amp_offset(kcontrol
);
1841 bool min_mute
= get_amp_min_mute(kcontrol
);
1842 u32 caps
, val1
, val2
;
1844 if (size
< 4 * sizeof(unsigned int))
1846 caps
= query_amp_caps(codec
, nid
, dir
);
1847 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1848 val2
= (val2
+ 1) * 25;
1849 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
1851 val1
= ((int)val1
) * ((int)val2
);
1853 val2
|= TLV_DB_SCALE_MUTE
;
1854 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
1856 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
1858 if (put_user(val1
, _tlv
+ 2))
1860 if (put_user(val2
, _tlv
+ 3))
1864 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
1867 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1868 * @codec: HD-audio codec
1869 * @nid: NID of a reference widget
1870 * @dir: #HDA_INPUT or #HDA_OUTPUT
1871 * @tlv: TLV data to be stored, at least 4 elements
1873 * Set (static) TLV data for a virtual master volume using the AMP caps
1874 * obtained from the reference NID.
1875 * The volume range is recalculated as if the max volume is 0dB.
1877 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1883 caps
= query_amp_caps(codec
, nid
, dir
);
1884 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1885 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1886 step
= (step
+ 1) * 25;
1887 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
1888 tlv
[1] = 2 * sizeof(unsigned int);
1889 tlv
[2] = -nums
* step
;
1892 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
1894 /* find a mixer control element with the given name */
1895 static struct snd_kcontrol
*
1896 _snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
1897 const char *name
, int idx
)
1899 struct snd_ctl_elem_id id
;
1900 memset(&id
, 0, sizeof(id
));
1901 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1903 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
1905 strcpy(id
.name
, name
);
1906 return snd_ctl_find_id(codec
->bus
->card
, &id
);
1910 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1911 * @codec: HD-audio codec
1912 * @name: ctl id name string
1914 * Get the control element with the given id string and IFACE_MIXER.
1916 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
1919 return _snd_hda_find_mixer_ctl(codec
, name
, 0);
1921 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
1923 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
)
1926 for (idx
= 0; idx
< 16; idx
++) { /* 16 ctlrs should be large enough */
1927 if (!_snd_hda_find_mixer_ctl(codec
, name
, idx
))
1934 * snd_hda_ctl_add - Add a control element and assign to the codec
1935 * @codec: HD-audio codec
1936 * @nid: corresponding NID (optional)
1937 * @kctl: the control element to assign
1939 * Add the given control element to an array inside the codec instance.
1940 * All control elements belonging to a codec are supposed to be added
1941 * by this function so that a proper clean-up works at the free or
1942 * reconfiguration time.
1944 * If non-zero @nid is passed, the NID is assigned to the control element.
1945 * The assignment is shown in the codec proc file.
1947 * snd_hda_ctl_add() checks the control subdev id field whether
1948 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1949 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1950 * specifies if kctl->private_value is a HDA amplifier value.
1952 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
1953 struct snd_kcontrol
*kctl
)
1956 unsigned short flags
= 0;
1957 struct hda_nid_item
*item
;
1959 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
1960 flags
|= HDA_NID_ITEM_AMP
;
1962 nid
= get_amp_nid_(kctl
->private_value
);
1964 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
1965 nid
= kctl
->id
.subdevice
& 0xffff;
1966 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
1967 kctl
->id
.subdevice
= 0;
1968 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
1971 item
= snd_array_new(&codec
->mixers
);
1976 item
->flags
= flags
;
1979 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
1982 * snd_hda_add_nid - Assign a NID to a control element
1983 * @codec: HD-audio codec
1984 * @nid: corresponding NID (optional)
1985 * @kctl: the control element to assign
1986 * @index: index to kctl
1988 * Add the given control element to an array inside the codec instance.
1989 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1990 * NID:KCTL mapping - for example "Capture Source" selector.
1992 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
1993 unsigned int index
, hda_nid_t nid
)
1995 struct hda_nid_item
*item
;
1998 item
= snd_array_new(&codec
->nids
);
2002 item
->index
= index
;
2006 printk(KERN_ERR
"hda-codec: no NID for mapping control %s:%d:%d\n",
2007 kctl
->id
.name
, kctl
->id
.index
, index
);
2010 EXPORT_SYMBOL_HDA(snd_hda_add_nid
);
2013 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2014 * @codec: HD-audio codec
2016 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2019 struct hda_nid_item
*items
= codec
->mixers
.list
;
2020 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2021 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
2022 snd_array_free(&codec
->mixers
);
2023 snd_array_free(&codec
->nids
);
2026 /* pseudo device locking
2027 * toggle card->shutdown to allow/disallow the device access (as a hack)
2029 static int hda_lock_devices(struct snd_card
*card
)
2031 spin_lock(&card
->files_lock
);
2032 if (card
->shutdown
) {
2033 spin_unlock(&card
->files_lock
);
2037 spin_unlock(&card
->files_lock
);
2041 static void hda_unlock_devices(struct snd_card
*card
)
2043 spin_lock(&card
->files_lock
);
2045 spin_unlock(&card
->files_lock
);
2049 * snd_hda_codec_reset - Clear all objects assigned to the codec
2050 * @codec: HD-audio codec
2052 * This frees the all PCM and control elements assigned to the codec, and
2053 * clears the caches and restores the pin default configurations.
2055 * When a device is being used, it returns -EBSY. If successfully freed,
2058 int snd_hda_codec_reset(struct hda_codec
*codec
)
2060 struct snd_card
*card
= codec
->bus
->card
;
2063 if (hda_lock_devices(card
) < 0)
2065 /* check whether the codec isn't used by any mixer or PCM streams */
2066 if (!list_empty(&card
->ctl_files
)) {
2067 hda_unlock_devices(card
);
2070 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2071 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2074 if (cpcm
->pcm
->streams
[0].substream_opened
||
2075 cpcm
->pcm
->streams
[1].substream_opened
) {
2076 hda_unlock_devices(card
);
2081 /* OK, let it free */
2083 #ifdef CONFIG_SND_HDA_POWER_SAVE
2084 cancel_delayed_work(&codec
->power_work
);
2085 flush_workqueue(codec
->bus
->workq
);
2087 snd_hda_ctls_clear(codec
);
2089 for (i
= 0; i
< codec
->num_pcms
; i
++) {
2090 if (codec
->pcm_info
[i
].pcm
) {
2091 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
2092 clear_bit(codec
->pcm_info
[i
].device
,
2093 codec
->bus
->pcm_dev_bits
);
2096 if (codec
->patch_ops
.free
)
2097 codec
->patch_ops
.free(codec
);
2098 codec
->proc_widget_hook
= NULL
;
2100 free_hda_cache(&codec
->amp_cache
);
2101 free_hda_cache(&codec
->cmd_cache
);
2102 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2103 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2104 /* free only driver_pins so that init_pins + user_pins are restored */
2105 snd_array_free(&codec
->driver_pins
);
2106 restore_pincfgs(codec
);
2107 codec
->num_pcms
= 0;
2108 codec
->pcm_info
= NULL
;
2109 codec
->preset
= NULL
;
2110 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2111 codec
->slave_dig_outs
= NULL
;
2112 codec
->spdif_status_reset
= 0;
2113 module_put(codec
->owner
);
2114 codec
->owner
= NULL
;
2116 /* allow device access again */
2117 hda_unlock_devices(card
);
2122 * snd_hda_add_vmaster - create a virtual master control and add slaves
2123 * @codec: HD-audio codec
2124 * @name: vmaster control name
2125 * @tlv: TLV data (optional)
2126 * @slaves: slave control names (optional)
2128 * Create a virtual master control with the given name. The TLV data
2129 * must be either NULL or a valid data.
2131 * @slaves is a NULL-terminated array of strings, each of which is a
2132 * slave control name. All controls with these names are assigned to
2133 * the new virtual master control.
2135 * This function returns zero if successful or a negative error code.
2137 int snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2138 unsigned int *tlv
, const char * const *slaves
)
2140 struct snd_kcontrol
*kctl
;
2141 const char * const *s
;
2144 for (s
= slaves
; *s
&& !snd_hda_find_mixer_ctl(codec
, *s
); s
++)
2147 snd_printdd("No slave found for %s\n", name
);
2150 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2153 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2157 for (s
= slaves
; *s
; s
++) {
2158 struct snd_kcontrol
*sctl
;
2161 sctl
= _snd_hda_find_mixer_ctl(codec
, *s
, i
);
2164 snd_printdd("Cannot find slave %s, "
2168 err
= snd_ctl_add_slave(kctl
, sctl
);
2176 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster
);
2179 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2181 * The control element is supposed to have the private_value field
2182 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2184 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2185 struct snd_ctl_elem_info
*uinfo
)
2187 int chs
= get_amp_channels(kcontrol
);
2189 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2190 uinfo
->count
= chs
== 3 ? 2 : 1;
2191 uinfo
->value
.integer
.min
= 0;
2192 uinfo
->value
.integer
.max
= 1;
2195 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
2198 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2200 * The control element is supposed to have the private_value field
2201 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2203 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2204 struct snd_ctl_elem_value
*ucontrol
)
2206 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2207 hda_nid_t nid
= get_amp_nid(kcontrol
);
2208 int chs
= get_amp_channels(kcontrol
);
2209 int dir
= get_amp_direction(kcontrol
);
2210 int idx
= get_amp_index(kcontrol
);
2211 long *valp
= ucontrol
->value
.integer
.value
;
2214 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2215 HDA_AMP_MUTE
) ? 0 : 1;
2217 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2218 HDA_AMP_MUTE
) ? 0 : 1;
2221 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
2224 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2226 * The control element is supposed to have the private_value field
2227 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2229 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2230 struct snd_ctl_elem_value
*ucontrol
)
2232 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2233 hda_nid_t nid
= get_amp_nid(kcontrol
);
2234 int chs
= get_amp_channels(kcontrol
);
2235 int dir
= get_amp_direction(kcontrol
);
2236 int idx
= get_amp_index(kcontrol
);
2237 long *valp
= ucontrol
->value
.integer
.value
;
2240 snd_hda_power_up(codec
);
2242 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2244 *valp
? 0 : HDA_AMP_MUTE
);
2248 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2250 *valp
? 0 : HDA_AMP_MUTE
);
2251 hda_call_check_power_status(codec
, nid
);
2252 snd_hda_power_down(codec
);
2255 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
2257 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2259 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2261 * This function calls snd_hda_enable_beep_device(), which behaves differently
2262 * depending on beep_mode option.
2264 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol
*kcontrol
,
2265 struct snd_ctl_elem_value
*ucontrol
)
2267 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2268 long *valp
= ucontrol
->value
.integer
.value
;
2270 snd_hda_enable_beep_device(codec
, *valp
);
2271 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2273 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep
);
2274 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2277 * bound volume controls
2279 * bind multiple volumes (# indices, from 0)
2282 #define AMP_VAL_IDX_SHIFT 19
2283 #define AMP_VAL_IDX_MASK (0x0f<<19)
2286 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2288 * The control element is supposed to have the private_value field
2289 * set up via HDA_BIND_MUTE*() macros.
2291 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2292 struct snd_ctl_elem_value
*ucontrol
)
2294 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2298 mutex_lock(&codec
->control_mutex
);
2299 pval
= kcontrol
->private_value
;
2300 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2301 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2302 kcontrol
->private_value
= pval
;
2303 mutex_unlock(&codec
->control_mutex
);
2306 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
2309 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2311 * The control element is supposed to have the private_value field
2312 * set up via HDA_BIND_MUTE*() macros.
2314 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2315 struct snd_ctl_elem_value
*ucontrol
)
2317 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2319 int i
, indices
, err
= 0, change
= 0;
2321 mutex_lock(&codec
->control_mutex
);
2322 pval
= kcontrol
->private_value
;
2323 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2324 for (i
= 0; i
< indices
; i
++) {
2325 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2326 (i
<< AMP_VAL_IDX_SHIFT
);
2327 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2332 kcontrol
->private_value
= pval
;
2333 mutex_unlock(&codec
->control_mutex
);
2334 return err
< 0 ? err
: change
;
2336 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
2339 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2341 * The control element is supposed to have the private_value field
2342 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2344 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2345 struct snd_ctl_elem_info
*uinfo
)
2347 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2348 struct hda_bind_ctls
*c
;
2351 mutex_lock(&codec
->control_mutex
);
2352 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2353 kcontrol
->private_value
= *c
->values
;
2354 err
= c
->ops
->info(kcontrol
, uinfo
);
2355 kcontrol
->private_value
= (long)c
;
2356 mutex_unlock(&codec
->control_mutex
);
2359 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
2362 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2364 * The control element is supposed to have the private_value field
2365 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2367 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2368 struct snd_ctl_elem_value
*ucontrol
)
2370 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2371 struct hda_bind_ctls
*c
;
2374 mutex_lock(&codec
->control_mutex
);
2375 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2376 kcontrol
->private_value
= *c
->values
;
2377 err
= c
->ops
->get(kcontrol
, ucontrol
);
2378 kcontrol
->private_value
= (long)c
;
2379 mutex_unlock(&codec
->control_mutex
);
2382 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
2385 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2387 * The control element is supposed to have the private_value field
2388 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2390 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2391 struct snd_ctl_elem_value
*ucontrol
)
2393 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2394 struct hda_bind_ctls
*c
;
2395 unsigned long *vals
;
2396 int err
= 0, change
= 0;
2398 mutex_lock(&codec
->control_mutex
);
2399 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2400 for (vals
= c
->values
; *vals
; vals
++) {
2401 kcontrol
->private_value
= *vals
;
2402 err
= c
->ops
->put(kcontrol
, ucontrol
);
2407 kcontrol
->private_value
= (long)c
;
2408 mutex_unlock(&codec
->control_mutex
);
2409 return err
< 0 ? err
: change
;
2411 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
2414 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2416 * The control element is supposed to have the private_value field
2417 * set up via HDA_BIND_VOL() macro.
2419 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2420 unsigned int size
, unsigned int __user
*tlv
)
2422 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2423 struct hda_bind_ctls
*c
;
2426 mutex_lock(&codec
->control_mutex
);
2427 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2428 kcontrol
->private_value
= *c
->values
;
2429 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
2430 kcontrol
->private_value
= (long)c
;
2431 mutex_unlock(&codec
->control_mutex
);
2434 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
2436 struct hda_ctl_ops snd_hda_bind_vol
= {
2437 .info
= snd_hda_mixer_amp_volume_info
,
2438 .get
= snd_hda_mixer_amp_volume_get
,
2439 .put
= snd_hda_mixer_amp_volume_put
,
2440 .tlv
= snd_hda_mixer_amp_tlv
2442 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
2444 struct hda_ctl_ops snd_hda_bind_sw
= {
2445 .info
= snd_hda_mixer_amp_switch_info
,
2446 .get
= snd_hda_mixer_amp_switch_get
,
2447 .put
= snd_hda_mixer_amp_switch_put
,
2448 .tlv
= snd_hda_mixer_amp_tlv
2450 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
2453 * SPDIF out controls
2456 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2457 struct snd_ctl_elem_info
*uinfo
)
2459 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2464 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2465 struct snd_ctl_elem_value
*ucontrol
)
2467 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2468 IEC958_AES0_NONAUDIO
|
2469 IEC958_AES0_CON_EMPHASIS_5015
|
2470 IEC958_AES0_CON_NOT_COPYRIGHT
;
2471 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2472 IEC958_AES1_CON_ORIGINAL
;
2476 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2477 struct snd_ctl_elem_value
*ucontrol
)
2479 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2480 IEC958_AES0_NONAUDIO
|
2481 IEC958_AES0_PRO_EMPHASIS_5015
;
2485 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2486 struct snd_ctl_elem_value
*ucontrol
)
2488 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2490 ucontrol
->value
.iec958
.status
[0] = codec
->spdif_status
& 0xff;
2491 ucontrol
->value
.iec958
.status
[1] = (codec
->spdif_status
>> 8) & 0xff;
2492 ucontrol
->value
.iec958
.status
[2] = (codec
->spdif_status
>> 16) & 0xff;
2493 ucontrol
->value
.iec958
.status
[3] = (codec
->spdif_status
>> 24) & 0xff;
2498 /* convert from SPDIF status bits to HDA SPDIF bits
2499 * bit 0 (DigEn) is always set zero (to be filled later)
2501 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2503 unsigned short val
= 0;
2505 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2506 val
|= AC_DIG1_PROFESSIONAL
;
2507 if (sbits
& IEC958_AES0_NONAUDIO
)
2508 val
|= AC_DIG1_NONAUDIO
;
2509 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2510 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2511 IEC958_AES0_PRO_EMPHASIS_5015
)
2512 val
|= AC_DIG1_EMPHASIS
;
2514 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2515 IEC958_AES0_CON_EMPHASIS_5015
)
2516 val
|= AC_DIG1_EMPHASIS
;
2517 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2518 val
|= AC_DIG1_COPYRIGHT
;
2519 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2520 val
|= AC_DIG1_LEVEL
;
2521 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2526 /* convert to SPDIF status bits from HDA SPDIF bits
2528 static unsigned int convert_to_spdif_status(unsigned short val
)
2530 unsigned int sbits
= 0;
2532 if (val
& AC_DIG1_NONAUDIO
)
2533 sbits
|= IEC958_AES0_NONAUDIO
;
2534 if (val
& AC_DIG1_PROFESSIONAL
)
2535 sbits
|= IEC958_AES0_PROFESSIONAL
;
2536 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2537 if (sbits
& AC_DIG1_EMPHASIS
)
2538 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
2540 if (val
& AC_DIG1_EMPHASIS
)
2541 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
2542 if (!(val
& AC_DIG1_COPYRIGHT
))
2543 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
2544 if (val
& AC_DIG1_LEVEL
)
2545 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
2546 sbits
|= val
& (0x7f << 8);
2551 /* set digital convert verbs both for the given NID and its slaves */
2552 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
2557 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
2558 d
= codec
->slave_dig_outs
;
2562 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
2565 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
2569 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
2571 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
2574 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
2575 struct snd_ctl_elem_value
*ucontrol
)
2577 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2578 hda_nid_t nid
= kcontrol
->private_value
;
2582 mutex_lock(&codec
->spdif_mutex
);
2583 codec
->spdif_status
= ucontrol
->value
.iec958
.status
[0] |
2584 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
2585 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
2586 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
2587 val
= convert_from_spdif_status(codec
->spdif_status
);
2588 val
|= codec
->spdif_ctls
& 1;
2589 change
= codec
->spdif_ctls
!= val
;
2590 codec
->spdif_ctls
= val
;
2593 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2595 mutex_unlock(&codec
->spdif_mutex
);
2599 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2601 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
2602 struct snd_ctl_elem_value
*ucontrol
)
2604 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2606 ucontrol
->value
.integer
.value
[0] = codec
->spdif_ctls
& AC_DIG1_ENABLE
;
2610 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
2611 struct snd_ctl_elem_value
*ucontrol
)
2613 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2614 hda_nid_t nid
= kcontrol
->private_value
;
2618 mutex_lock(&codec
->spdif_mutex
);
2619 val
= codec
->spdif_ctls
& ~AC_DIG1_ENABLE
;
2620 if (ucontrol
->value
.integer
.value
[0])
2621 val
|= AC_DIG1_ENABLE
;
2622 change
= codec
->spdif_ctls
!= val
;
2624 codec
->spdif_ctls
= val
;
2625 set_dig_out_convert(codec
, nid
, val
& 0xff, -1);
2626 /* unmute amp switch (if any) */
2627 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
2628 (val
& AC_DIG1_ENABLE
))
2629 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
2632 mutex_unlock(&codec
->spdif_mutex
);
2636 static struct snd_kcontrol_new dig_mixes
[] = {
2638 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2639 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2640 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
2641 .info
= snd_hda_spdif_mask_info
,
2642 .get
= snd_hda_spdif_cmask_get
,
2645 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2646 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2647 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
2648 .info
= snd_hda_spdif_mask_info
,
2649 .get
= snd_hda_spdif_pmask_get
,
2652 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2653 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
2654 .info
= snd_hda_spdif_mask_info
,
2655 .get
= snd_hda_spdif_default_get
,
2656 .put
= snd_hda_spdif_default_put
,
2659 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2660 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
2661 .info
= snd_hda_spdif_out_switch_info
,
2662 .get
= snd_hda_spdif_out_switch_get
,
2663 .put
= snd_hda_spdif_out_switch_put
,
2669 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2670 * @codec: the HDA codec
2671 * @nid: audio out widget NID
2673 * Creates controls related with the SPDIF output.
2674 * Called from each patch supporting the SPDIF out.
2676 * Returns 0 if successful, or a negative error code.
2678 int snd_hda_create_spdif_out_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
2681 struct snd_kcontrol
*kctl
;
2682 struct snd_kcontrol_new
*dig_mix
;
2685 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch");
2687 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
2690 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2691 kctl
= snd_ctl_new1(dig_mix
, codec
);
2694 kctl
->id
.index
= idx
;
2695 kctl
->private_value
= nid
;
2696 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
2701 snd_hda_codec_read(codec
, nid
, 0,
2702 AC_VERB_GET_DIGI_CONVERT_1
, 0);
2703 codec
->spdif_status
= convert_to_spdif_status(codec
->spdif_ctls
);
2706 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls
);
2709 * SPDIF sharing with analog output
2711 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
2712 struct snd_ctl_elem_value
*ucontrol
)
2714 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2715 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
2719 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
2720 struct snd_ctl_elem_value
*ucontrol
)
2722 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2723 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
2727 static struct snd_kcontrol_new spdif_share_sw
= {
2728 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2729 .name
= "IEC958 Default PCM Playback Switch",
2730 .info
= snd_ctl_boolean_mono_info
,
2731 .get
= spdif_share_sw_get
,
2732 .put
= spdif_share_sw_put
,
2736 * snd_hda_create_spdif_share_sw - create Default PCM switch
2737 * @codec: the HDA codec
2738 * @mout: multi-out instance
2740 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
2741 struct hda_multi_out
*mout
)
2743 if (!mout
->dig_out_nid
)
2745 /* ATTENTION: here mout is passed as private_data, instead of codec */
2746 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
,
2747 snd_ctl_new1(&spdif_share_sw
, mout
));
2749 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
2755 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2757 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
2758 struct snd_ctl_elem_value
*ucontrol
)
2760 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2762 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
2766 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
2767 struct snd_ctl_elem_value
*ucontrol
)
2769 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2770 hda_nid_t nid
= kcontrol
->private_value
;
2771 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
2774 mutex_lock(&codec
->spdif_mutex
);
2775 change
= codec
->spdif_in_enable
!= val
;
2777 codec
->spdif_in_enable
= val
;
2778 snd_hda_codec_write_cache(codec
, nid
, 0,
2779 AC_VERB_SET_DIGI_CONVERT_1
, val
);
2781 mutex_unlock(&codec
->spdif_mutex
);
2785 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
2786 struct snd_ctl_elem_value
*ucontrol
)
2788 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2789 hda_nid_t nid
= kcontrol
->private_value
;
2793 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
2794 sbits
= convert_to_spdif_status(val
);
2795 ucontrol
->value
.iec958
.status
[0] = sbits
;
2796 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
2797 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
2798 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
2802 static struct snd_kcontrol_new dig_in_ctls
[] = {
2804 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2805 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
2806 .info
= snd_hda_spdif_in_switch_info
,
2807 .get
= snd_hda_spdif_in_switch_get
,
2808 .put
= snd_hda_spdif_in_switch_put
,
2811 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2812 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2813 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
2814 .info
= snd_hda_spdif_mask_info
,
2815 .get
= snd_hda_spdif_in_status_get
,
2821 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2822 * @codec: the HDA codec
2823 * @nid: audio in widget NID
2825 * Creates controls related with the SPDIF input.
2826 * Called from each patch supporting the SPDIF in.
2828 * Returns 0 if successful, or a negative error code.
2830 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
2833 struct snd_kcontrol
*kctl
;
2834 struct snd_kcontrol_new
*dig_mix
;
2837 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch");
2839 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
2842 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
2843 kctl
= snd_ctl_new1(dig_mix
, codec
);
2846 kctl
->private_value
= nid
;
2847 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
2851 codec
->spdif_in_enable
=
2852 snd_hda_codec_read(codec
, nid
, 0,
2853 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
2857 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
2859 #ifdef SND_HDA_NEEDS_RESUME
2864 /* build a 32bit cache key with the widget id and the command parameter */
2865 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2866 #define get_cmd_cache_nid(key) ((key) & 0xff)
2867 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2870 * snd_hda_codec_write_cache - send a single command with caching
2871 * @codec: the HDA codec
2872 * @nid: NID to send the command
2873 * @direct: direct flag
2874 * @verb: the verb to send
2875 * @parm: the parameter for the verb
2877 * Send a single command without waiting for response.
2879 * Returns 0 if successful, or a negative error code.
2881 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
2882 int direct
, unsigned int verb
, unsigned int parm
)
2884 int err
= snd_hda_codec_write(codec
, nid
, direct
, verb
, parm
);
2885 struct hda_cache_head
*c
;
2890 /* parm may contain the verb stuff for get/set amp */
2891 verb
= verb
| (parm
>> 8);
2893 key
= build_cmd_cache_key(nid
, verb
);
2894 mutex_lock(&codec
->bus
->cmd_mutex
);
2895 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
2898 mutex_unlock(&codec
->bus
->cmd_mutex
);
2901 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
2904 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2905 * @codec: the HDA codec
2906 * @nid: NID to send the command
2907 * @direct: direct flag
2908 * @verb: the verb to send
2909 * @parm: the parameter for the verb
2911 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2912 * command if the parameter is already identical with the cached value.
2913 * If not, it sends the command and refreshes the cache.
2915 * Returns 0 if successful, or a negative error code.
2917 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
2918 int direct
, unsigned int verb
, unsigned int parm
)
2920 struct hda_cache_head
*c
;
2923 /* parm may contain the verb stuff for get/set amp */
2924 verb
= verb
| (parm
>> 8);
2926 key
= build_cmd_cache_key(nid
, verb
);
2927 mutex_lock(&codec
->bus
->cmd_mutex
);
2928 c
= get_hash(&codec
->cmd_cache
, key
);
2929 if (c
&& c
->val
== parm
) {
2930 mutex_unlock(&codec
->bus
->cmd_mutex
);
2933 mutex_unlock(&codec
->bus
->cmd_mutex
);
2934 return snd_hda_codec_write_cache(codec
, nid
, direct
, verb
, parm
);
2936 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache
);
2939 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2940 * @codec: HD-audio codec
2942 * Execute all verbs recorded in the command caches to resume.
2944 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
2946 struct hda_cache_head
*buffer
= codec
->cmd_cache
.buf
.list
;
2949 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++, buffer
++) {
2950 u32 key
= buffer
->key
;
2953 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
2954 get_cmd_cache_cmd(key
), buffer
->val
);
2957 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
2960 * snd_hda_sequence_write_cache - sequence writes with caching
2961 * @codec: the HDA codec
2962 * @seq: VERB array to send
2964 * Send the commands sequentially from the given array.
2965 * Thte commands are recorded on cache for power-save and resume.
2966 * The array must be terminated with NID=0.
2968 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
2969 const struct hda_verb
*seq
)
2971 for (; seq
->nid
; seq
++)
2972 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
2975 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
2976 #endif /* SND_HDA_NEEDS_RESUME */
2979 * set power state of the codec
2981 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
2982 unsigned int power_state
)
2987 /* this delay seems necessary to avoid click noise at power-down */
2988 if (power_state
== AC_PWRST_D3
)
2990 snd_hda_codec_read(codec
, fg
, 0, AC_VERB_SET_POWER_STATE
,
2992 /* partial workaround for "azx_get_response timeout" */
2993 if (power_state
== AC_PWRST_D0
&&
2994 (codec
->vendor_id
& 0xffff0000) == 0x14f10000)
2997 nid
= codec
->start_nid
;
2998 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
2999 unsigned int wcaps
= get_wcaps(codec
, nid
);
3000 if (wcaps
& AC_WCAP_POWER
) {
3001 unsigned int wid_type
= get_wcaps_type(wcaps
);
3002 if (power_state
== AC_PWRST_D3
&&
3003 wid_type
== AC_WID_PIN
) {
3004 unsigned int pincap
;
3006 * don't power down the widget if it controls
3007 * eapd and EAPD_BTLENABLE is set.
3009 pincap
= snd_hda_query_pin_caps(codec
, nid
);
3010 if (pincap
& AC_PINCAP_EAPD
) {
3011 int eapd
= snd_hda_codec_read(codec
,
3013 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3019 snd_hda_codec_write(codec
, nid
, 0,
3020 AC_VERB_SET_POWER_STATE
,
3025 if (power_state
== AC_PWRST_D0
) {
3026 unsigned long end_time
;
3028 /* wait until the codec reachs to D0 */
3029 end_time
= jiffies
+ msecs_to_jiffies(500);
3031 state
= snd_hda_codec_read(codec
, fg
, 0,
3032 AC_VERB_GET_POWER_STATE
, 0);
3033 if (state
== power_state
)
3036 } while (time_after_eq(end_time
, jiffies
));
3040 #ifdef CONFIG_SND_HDA_HWDEP
3041 /* execute additional init verbs */
3042 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3044 if (codec
->init_verbs
.list
)
3045 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3048 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3051 #ifdef SND_HDA_NEEDS_RESUME
3053 * call suspend and power-down; used both from PM and power-save
3055 static void hda_call_codec_suspend(struct hda_codec
*codec
)
3057 if (codec
->patch_ops
.suspend
)
3058 codec
->patch_ops
.suspend(codec
, PMSG_SUSPEND
);
3059 hda_cleanup_all_streams(codec
);
3060 hda_set_power_state(codec
,
3061 codec
->afg
? codec
->afg
: codec
->mfg
,
3063 #ifdef CONFIG_SND_HDA_POWER_SAVE
3064 snd_hda_update_power_acct(codec
);
3065 cancel_delayed_work(&codec
->power_work
);
3066 codec
->power_on
= 0;
3067 codec
->power_transition
= 0;
3068 codec
->power_jiffies
= jiffies
;
3073 * kick up codec; used both from PM and power-save
3075 static void hda_call_codec_resume(struct hda_codec
*codec
)
3077 hda_set_power_state(codec
,
3078 codec
->afg
? codec
->afg
: codec
->mfg
,
3080 restore_pincfgs(codec
); /* restore all current pin configs */
3081 restore_shutup_pins(codec
);
3082 hda_exec_init_verbs(codec
);
3083 if (codec
->patch_ops
.resume
)
3084 codec
->patch_ops
.resume(codec
);
3086 if (codec
->patch_ops
.init
)
3087 codec
->patch_ops
.init(codec
);
3088 snd_hda_codec_resume_amp(codec
);
3089 snd_hda_codec_resume_cache(codec
);
3092 #endif /* SND_HDA_NEEDS_RESUME */
3096 * snd_hda_build_controls - build mixer controls
3099 * Creates mixer controls for each codec included in the bus.
3101 * Returns 0 if successful, otherwise a negative error code.
3103 int /*__devinit*/ snd_hda_build_controls(struct hda_bus
*bus
)
3105 struct hda_codec
*codec
;
3107 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3108 int err
= snd_hda_codec_build_controls(codec
);
3110 printk(KERN_ERR
"hda_codec: cannot build controls "
3111 "for #%d (error %d)\n", codec
->addr
, err
);
3112 err
= snd_hda_codec_reset(codec
);
3115 "hda_codec: cannot revert codec\n");
3122 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
3124 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
3127 hda_exec_init_verbs(codec
);
3128 /* continue to initialize... */
3129 if (codec
->patch_ops
.init
)
3130 err
= codec
->patch_ops
.init(codec
);
3131 if (!err
&& codec
->patch_ops
.build_controls
)
3132 err
= codec
->patch_ops
.build_controls(codec
);
3141 struct hda_rate_tbl
{
3143 unsigned int alsa_bits
;
3144 unsigned int hda_fmt
;
3147 /* rate = base * mult / div */
3148 #define HDA_RATE(base, mult, div) \
3149 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3150 (((div) - 1) << AC_FMT_DIV_SHIFT))
3152 static struct hda_rate_tbl rate_bits
[] = {
3153 /* rate in Hz, ALSA rate bitmask, HDA format value */
3155 /* autodetected value used in snd_hda_query_supported_pcm */
3156 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
3157 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
3158 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
3159 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
3160 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
3161 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
3162 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
3163 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
3164 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
3165 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
3166 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
3167 #define AC_PAR_PCM_RATE_BITS 11
3168 /* up to bits 10, 384kHZ isn't supported properly */
3170 /* not autodetected value */
3171 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
3173 { 0 } /* terminator */
3177 * snd_hda_calc_stream_format - calculate format bitset
3178 * @rate: the sample rate
3179 * @channels: the number of channels
3180 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3181 * @maxbps: the max. bps
3183 * Calculate the format bitset from the given rate, channels and th PCM format.
3185 * Return zero if invalid.
3187 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
3188 unsigned int channels
,
3189 unsigned int format
,
3190 unsigned int maxbps
,
3191 unsigned short spdif_ctls
)
3194 unsigned int val
= 0;
3196 for (i
= 0; rate_bits
[i
].hz
; i
++)
3197 if (rate_bits
[i
].hz
== rate
) {
3198 val
= rate_bits
[i
].hda_fmt
;
3201 if (!rate_bits
[i
].hz
) {
3202 snd_printdd("invalid rate %d\n", rate
);
3206 if (channels
== 0 || channels
> 8) {
3207 snd_printdd("invalid channels %d\n", channels
);
3210 val
|= channels
- 1;
3212 switch (snd_pcm_format_width(format
)) {
3214 val
|= AC_FMT_BITS_8
;
3217 val
|= AC_FMT_BITS_16
;
3222 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
3223 val
|= AC_FMT_BITS_32
;
3224 else if (maxbps
>= 24)
3225 val
|= AC_FMT_BITS_24
;
3227 val
|= AC_FMT_BITS_20
;
3230 snd_printdd("invalid format width %d\n",
3231 snd_pcm_format_width(format
));
3235 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
3236 val
|= AC_FMT_TYPE_NON_PCM
;
3240 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
3242 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3244 unsigned int val
= 0;
3245 if (nid
!= codec
->afg
&&
3246 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
3247 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
3248 if (!val
|| val
== -1)
3249 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
3250 if (!val
|| val
== -1)
3255 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3257 return query_caps_hash(codec
, nid
, HDA_HASH_PARPCM_KEY(nid
),
3261 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3263 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
3264 if (!streams
|| streams
== -1)
3265 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
3266 if (!streams
|| streams
== -1)
3271 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3273 return query_caps_hash(codec
, nid
, HDA_HASH_PARSTR_KEY(nid
),
3278 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3279 * @codec: the HDA codec
3280 * @nid: NID to query
3281 * @ratesp: the pointer to store the detected rate bitflags
3282 * @formatsp: the pointer to store the detected formats
3283 * @bpsp: the pointer to store the detected format widths
3285 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3286 * or @bsps argument is ignored.
3288 * Returns 0 if successful, otherwise a negative error code.
3290 static int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
3291 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
3293 unsigned int i
, val
, wcaps
;
3295 wcaps
= get_wcaps(codec
, nid
);
3296 val
= query_pcm_param(codec
, nid
);
3300 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
3302 rates
|= rate_bits
[i
].alsa_bits
;
3305 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
3306 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3308 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
3314 if (formatsp
|| bpsp
) {
3316 unsigned int streams
, bps
;
3318 streams
= query_stream_param(codec
, nid
);
3323 if (streams
& AC_SUPFMT_PCM
) {
3324 if (val
& AC_SUPPCM_BITS_8
) {
3325 formats
|= SNDRV_PCM_FMTBIT_U8
;
3328 if (val
& AC_SUPPCM_BITS_16
) {
3329 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
3332 if (wcaps
& AC_WCAP_DIGITAL
) {
3333 if (val
& AC_SUPPCM_BITS_32
)
3334 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
3335 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
3336 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3337 if (val
& AC_SUPPCM_BITS_24
)
3339 else if (val
& AC_SUPPCM_BITS_20
)
3341 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
3342 AC_SUPPCM_BITS_32
)) {
3343 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3344 if (val
& AC_SUPPCM_BITS_32
)
3346 else if (val
& AC_SUPPCM_BITS_24
)
3348 else if (val
& AC_SUPPCM_BITS_20
)
3352 if (streams
& AC_SUPFMT_FLOAT32
) {
3353 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
3357 if (streams
== AC_SUPFMT_AC3
) {
3358 /* should be exclusive */
3359 /* temporary hack: we have still no proper support
3360 * for the direct AC3 stream...
3362 formats
|= SNDRV_PCM_FMTBIT_U8
;
3366 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
3367 "(nid=0x%x, val=0x%x, ovrd=%i, "
3370 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
3375 *formatsp
= formats
;
3384 * snd_hda_is_supported_format - Check the validity of the format
3385 * @codec: HD-audio codec
3386 * @nid: NID to check
3387 * @format: the HD-audio format value to check
3389 * Check whether the given node supports the format value.
3391 * Returns 1 if supported, 0 if not.
3393 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
3394 unsigned int format
)
3397 unsigned int val
= 0, rate
, stream
;
3399 val
= query_pcm_param(codec
, nid
);
3403 rate
= format
& 0xff00;
3404 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
3405 if (rate_bits
[i
].hda_fmt
== rate
) {
3410 if (i
>= AC_PAR_PCM_RATE_BITS
)
3413 stream
= query_stream_param(codec
, nid
);
3417 if (stream
& AC_SUPFMT_PCM
) {
3418 switch (format
& 0xf0) {
3420 if (!(val
& AC_SUPPCM_BITS_8
))
3424 if (!(val
& AC_SUPPCM_BITS_16
))
3428 if (!(val
& AC_SUPPCM_BITS_20
))
3432 if (!(val
& AC_SUPPCM_BITS_24
))
3436 if (!(val
& AC_SUPPCM_BITS_32
))
3443 /* FIXME: check for float32 and AC3? */
3448 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
3453 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
3454 struct hda_codec
*codec
,
3455 struct snd_pcm_substream
*substream
)
3460 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
3461 struct hda_codec
*codec
,
3462 unsigned int stream_tag
,
3463 unsigned int format
,
3464 struct snd_pcm_substream
*substream
)
3466 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3470 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
3471 struct hda_codec
*codec
,
3472 struct snd_pcm_substream
*substream
)
3474 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3478 static int set_pcm_default_values(struct hda_codec
*codec
,
3479 struct hda_pcm_stream
*info
)
3483 /* query support PCM information from the given NID */
3484 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
3485 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
3486 info
->rates
? NULL
: &info
->rates
,
3487 info
->formats
? NULL
: &info
->formats
,
3488 info
->maxbps
? NULL
: &info
->maxbps
);
3492 if (info
->ops
.open
== NULL
)
3493 info
->ops
.open
= hda_pcm_default_open_close
;
3494 if (info
->ops
.close
== NULL
)
3495 info
->ops
.close
= hda_pcm_default_open_close
;
3496 if (info
->ops
.prepare
== NULL
) {
3497 if (snd_BUG_ON(!info
->nid
))
3499 info
->ops
.prepare
= hda_pcm_default_prepare
;
3501 if (info
->ops
.cleanup
== NULL
) {
3502 if (snd_BUG_ON(!info
->nid
))
3504 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
3510 * codec prepare/cleanup entries
3512 int snd_hda_codec_prepare(struct hda_codec
*codec
,
3513 struct hda_pcm_stream
*hinfo
,
3514 unsigned int stream
,
3515 unsigned int format
,
3516 struct snd_pcm_substream
*substream
)
3519 mutex_lock(&codec
->bus
->prepare_mutex
);
3520 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
, substream
);
3522 purify_inactive_streams(codec
);
3523 mutex_unlock(&codec
->bus
->prepare_mutex
);
3526 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare
);
3528 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
3529 struct hda_pcm_stream
*hinfo
,
3530 struct snd_pcm_substream
*substream
)
3532 mutex_lock(&codec
->bus
->prepare_mutex
);
3533 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
3534 mutex_unlock(&codec
->bus
->prepare_mutex
);
3536 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup
);
3539 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
3540 "Audio", "SPDIF", "HDMI", "Modem"
3544 * get the empty PCM device number to assign
3546 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3548 static int get_empty_pcm_device(struct hda_bus
*bus
, int type
)
3550 /* audio device indices; not linear to keep compatibility */
3551 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
3552 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
3553 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
3554 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
3555 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
3559 if (type
>= HDA_PCM_NTYPES
) {
3560 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
3564 for (i
= 0; audio_idx
[type
][i
] >= 0 ; i
++)
3565 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
3566 return audio_idx
[type
][i
];
3568 snd_printk(KERN_WARNING
"Too many %s devices\n",
3569 snd_hda_pcm_type_name
[type
]);
3574 * attach a new PCM stream
3576 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
3578 struct hda_bus
*bus
= codec
->bus
;
3579 struct hda_pcm_stream
*info
;
3582 if (snd_BUG_ON(!pcm
->name
))
3584 for (stream
= 0; stream
< 2; stream
++) {
3585 info
= &pcm
->stream
[stream
];
3586 if (info
->substreams
) {
3587 err
= set_pcm_default_values(codec
, info
);
3592 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
3595 /* assign all PCMs of the given codec */
3596 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
3601 if (!codec
->num_pcms
) {
3602 if (!codec
->patch_ops
.build_pcms
)
3604 err
= codec
->patch_ops
.build_pcms(codec
);
3606 printk(KERN_ERR
"hda_codec: cannot build PCMs"
3607 "for #%d (error %d)\n", codec
->addr
, err
);
3608 err
= snd_hda_codec_reset(codec
);
3611 "hda_codec: cannot revert codec\n");
3616 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
3617 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
3620 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
3621 continue; /* no substreams assigned */
3624 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
3626 continue; /* no fatal error */
3628 err
= snd_hda_attach_pcm(codec
, cpcm
);
3630 printk(KERN_ERR
"hda_codec: cannot attach "
3631 "PCM stream %d for codec #%d\n",
3633 continue; /* no fatal error */
3641 * snd_hda_build_pcms - build PCM information
3644 * Create PCM information for each codec included in the bus.
3646 * The build_pcms codec patch is requested to set up codec->num_pcms and
3647 * codec->pcm_info properly. The array is referred by the top-level driver
3648 * to create its PCM instances.
3649 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3652 * At least, substreams, channels_min and channels_max must be filled for
3653 * each stream. substreams = 0 indicates that the stream doesn't exist.
3654 * When rates and/or formats are zero, the supported values are queried
3655 * from the given nid. The nid is used also by the default ops.prepare
3656 * and ops.cleanup callbacks.
3658 * The driver needs to call ops.open in its open callback. Similarly,
3659 * ops.close is supposed to be called in the close callback.
3660 * ops.prepare should be called in the prepare or hw_params callback
3661 * with the proper parameters for set up.
3662 * ops.cleanup should be called in hw_free for clean up of streams.
3664 * This function returns 0 if successfull, or a negative error code.
3666 int __devinit
snd_hda_build_pcms(struct hda_bus
*bus
)
3668 struct hda_codec
*codec
;
3670 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3671 int err
= snd_hda_codec_build_pcms(codec
);
3677 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
3680 * snd_hda_check_board_config - compare the current codec with the config table
3681 * @codec: the HDA codec
3682 * @num_configs: number of config enums
3683 * @models: array of model name strings
3684 * @tbl: configuration table, terminated by null entries
3686 * Compares the modelname or PCI subsystem id of the current codec with the
3687 * given configuration table. If a matching entry is found, returns its
3688 * config value (supposed to be 0 or positive).
3690 * If no entries are matching, the function returns a negative value.
3692 int snd_hda_check_board_config(struct hda_codec
*codec
,
3693 int num_configs
, const char * const *models
,
3694 const struct snd_pci_quirk
*tbl
)
3696 if (codec
->modelname
&& models
) {
3698 for (i
= 0; i
< num_configs
; i
++) {
3700 !strcmp(codec
->modelname
, models
[i
])) {
3701 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
3702 "selected\n", models
[i
]);
3708 if (!codec
->bus
->pci
|| !tbl
)
3711 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
3714 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
3715 #ifdef CONFIG_SND_DEBUG_VERBOSE
3717 const char *model
= NULL
;
3719 model
= models
[tbl
->value
];
3721 sprintf(tmp
, "#%d", tbl
->value
);
3724 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
3725 "for config %x:%x (%s)\n",
3726 model
, tbl
->subvendor
, tbl
->subdevice
,
3727 (tbl
->name
? tbl
->name
: "Unknown device"));
3733 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
3736 * snd_hda_check_board_codec_sid_config - compare the current codec
3737 subsystem ID with the
3740 This is important for Gateway notebooks with SB450 HDA Audio
3741 where the vendor ID of the PCI device is:
3742 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3743 and the vendor/subvendor are found only at the codec.
3745 * @codec: the HDA codec
3746 * @num_configs: number of config enums
3747 * @models: array of model name strings
3748 * @tbl: configuration table, terminated by null entries
3750 * Compares the modelname or PCI subsystem id of the current codec with the
3751 * given configuration table. If a matching entry is found, returns its
3752 * config value (supposed to be 0 or positive).
3754 * If no entries are matching, the function returns a negative value.
3756 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
3757 int num_configs
, const char * const *models
,
3758 const struct snd_pci_quirk
*tbl
)
3760 const struct snd_pci_quirk
*q
;
3762 /* Search for codec ID */
3763 for (q
= tbl
; q
->subvendor
; q
++) {
3764 unsigned long vendorid
= (q
->subdevice
) | (q
->subvendor
<< 16);
3766 if (vendorid
== codec
->subsystem_id
)
3775 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
3776 #ifdef CONFIG_SND_DEBUG_VERBOSE
3778 const char *model
= NULL
;
3780 model
= models
[tbl
->value
];
3782 sprintf(tmp
, "#%d", tbl
->value
);
3785 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
3786 "for config %x:%x (%s)\n",
3787 model
, tbl
->subvendor
, tbl
->subdevice
,
3788 (tbl
->name
? tbl
->name
: "Unknown device"));
3794 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
3797 * snd_hda_add_new_ctls - create controls from the array
3798 * @codec: the HDA codec
3799 * @knew: the array of struct snd_kcontrol_new
3801 * This helper function creates and add new controls in the given array.
3802 * The array must be terminated with an empty entry as terminator.
3804 * Returns 0 if successful, or a negative error code.
3806 int snd_hda_add_new_ctls(struct hda_codec
*codec
, struct snd_kcontrol_new
*knew
)
3810 for (; knew
->name
; knew
++) {
3811 struct snd_kcontrol
*kctl
;
3812 int addr
= 0, idx
= 0;
3813 if (knew
->iface
== -1) /* skip this codec private value */
3816 kctl
= snd_ctl_new1(knew
, codec
);
3820 kctl
->id
.device
= addr
;
3822 kctl
->id
.index
= idx
;
3823 err
= snd_hda_ctl_add(codec
, 0, kctl
);
3826 /* try first with another device index corresponding to
3827 * the codec addr; if it still fails (or it's the
3828 * primary codec), then try another control index
3830 if (!addr
&& codec
->addr
)
3832 else if (!idx
&& !knew
->index
) {
3833 idx
= find_empty_mixer_ctl_idx(codec
,
3843 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
3845 #ifdef CONFIG_SND_HDA_POWER_SAVE
3846 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
3847 unsigned int power_state
);
3849 static void hda_power_work(struct work_struct
*work
)
3851 struct hda_codec
*codec
=
3852 container_of(work
, struct hda_codec
, power_work
.work
);
3853 struct hda_bus
*bus
= codec
->bus
;
3855 if (!codec
->power_on
|| codec
->power_count
) {
3856 codec
->power_transition
= 0;
3860 hda_call_codec_suspend(codec
);
3861 if (bus
->ops
.pm_notify
)
3862 bus
->ops
.pm_notify(bus
);
3865 static void hda_keep_power_on(struct hda_codec
*codec
)
3867 codec
->power_count
++;
3868 codec
->power_on
= 1;
3869 codec
->power_jiffies
= jiffies
;
3872 /* update the power on/off account with the current jiffies */
3873 void snd_hda_update_power_acct(struct hda_codec
*codec
)
3875 unsigned long delta
= jiffies
- codec
->power_jiffies
;
3876 if (codec
->power_on
)
3877 codec
->power_on_acct
+= delta
;
3879 codec
->power_off_acct
+= delta
;
3880 codec
->power_jiffies
+= delta
;
3884 * snd_hda_power_up - Power-up the codec
3885 * @codec: HD-audio codec
3887 * Increment the power-up counter and power up the hardware really when
3888 * not turned on yet.
3890 void snd_hda_power_up(struct hda_codec
*codec
)
3892 struct hda_bus
*bus
= codec
->bus
;
3894 codec
->power_count
++;
3895 if (codec
->power_on
|| codec
->power_transition
)
3898 snd_hda_update_power_acct(codec
);
3899 codec
->power_on
= 1;
3900 codec
->power_jiffies
= jiffies
;
3901 if (bus
->ops
.pm_notify
)
3902 bus
->ops
.pm_notify(bus
);
3903 hda_call_codec_resume(codec
);
3904 cancel_delayed_work(&codec
->power_work
);
3905 codec
->power_transition
= 0;
3907 EXPORT_SYMBOL_HDA(snd_hda_power_up
);
3909 #define power_save(codec) \
3910 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3913 * snd_hda_power_down - Power-down the codec
3914 * @codec: HD-audio codec
3916 * Decrement the power-up counter and schedules the power-off work if
3917 * the counter rearches to zero.
3919 void snd_hda_power_down(struct hda_codec
*codec
)
3921 --codec
->power_count
;
3922 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
3924 if (power_save(codec
)) {
3925 codec
->power_transition
= 1; /* avoid reentrance */
3926 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
3927 msecs_to_jiffies(power_save(codec
) * 1000));
3930 EXPORT_SYMBOL_HDA(snd_hda_power_down
);
3933 * snd_hda_check_amp_list_power - Check the amp list and update the power
3934 * @codec: HD-audio codec
3935 * @check: the object containing an AMP list and the status
3936 * @nid: NID to check / update
3938 * Check whether the given NID is in the amp list. If it's in the list,
3939 * check the current AMP status, and update the the power-status according
3940 * to the mute status.
3942 * This function is supposed to be set or called from the check_power_status
3945 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
3946 struct hda_loopback_check
*check
,
3949 struct hda_amp_list
*p
;
3952 if (!check
->amplist
)
3954 for (p
= check
->amplist
; p
->nid
; p
++) {
3959 return 0; /* nothing changed */
3961 for (p
= check
->amplist
; p
->nid
; p
++) {
3962 for (ch
= 0; ch
< 2; ch
++) {
3963 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
3965 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
3966 if (!check
->power_on
) {
3967 check
->power_on
= 1;
3968 snd_hda_power_up(codec
);
3974 if (check
->power_on
) {
3975 check
->power_on
= 0;
3976 snd_hda_power_down(codec
);
3980 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
3984 * Channel mode helper
3988 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3990 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
3991 struct snd_ctl_elem_info
*uinfo
,
3992 const struct hda_channel_mode
*chmode
,
3995 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
3997 uinfo
->value
.enumerated
.items
= num_chmodes
;
3998 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
3999 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
4000 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
4001 chmode
[uinfo
->value
.enumerated
.item
].channels
);
4004 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
4007 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4009 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
4010 struct snd_ctl_elem_value
*ucontrol
,
4011 const struct hda_channel_mode
*chmode
,
4017 for (i
= 0; i
< num_chmodes
; i
++) {
4018 if (max_channels
== chmode
[i
].channels
) {
4019 ucontrol
->value
.enumerated
.item
[0] = i
;
4025 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
4028 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4030 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
4031 struct snd_ctl_elem_value
*ucontrol
,
4032 const struct hda_channel_mode
*chmode
,
4038 mode
= ucontrol
->value
.enumerated
.item
[0];
4039 if (mode
>= num_chmodes
)
4041 if (*max_channelsp
== chmode
[mode
].channels
)
4043 /* change the current channel setting */
4044 *max_channelsp
= chmode
[mode
].channels
;
4045 if (chmode
[mode
].sequence
)
4046 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
4049 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
4056 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4058 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
4059 struct snd_ctl_elem_info
*uinfo
)
4063 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4065 uinfo
->value
.enumerated
.items
= imux
->num_items
;
4066 if (!imux
->num_items
)
4068 index
= uinfo
->value
.enumerated
.item
;
4069 if (index
>= imux
->num_items
)
4070 index
= imux
->num_items
- 1;
4071 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
4074 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
4077 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4079 int snd_hda_input_mux_put(struct hda_codec
*codec
,
4080 const struct hda_input_mux
*imux
,
4081 struct snd_ctl_elem_value
*ucontrol
,
4083 unsigned int *cur_val
)
4087 if (!imux
->num_items
)
4089 idx
= ucontrol
->value
.enumerated
.item
[0];
4090 if (idx
>= imux
->num_items
)
4091 idx
= imux
->num_items
- 1;
4092 if (*cur_val
== idx
)
4094 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
4095 imux
->items
[idx
].index
);
4099 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
4103 * Multi-channel / digital-out PCM helper functions
4106 /* setup SPDIF output stream */
4107 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
4108 unsigned int stream_tag
, unsigned int format
)
4110 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4111 if (codec
->spdif_status_reset
&& (codec
->spdif_ctls
& AC_DIG1_ENABLE
))
4112 set_dig_out_convert(codec
, nid
,
4113 codec
->spdif_ctls
& ~AC_DIG1_ENABLE
& 0xff,
4115 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
4116 if (codec
->slave_dig_outs
) {
4118 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4119 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
4122 /* turn on again (if needed) */
4123 if (codec
->spdif_status_reset
&& (codec
->spdif_ctls
& AC_DIG1_ENABLE
))
4124 set_dig_out_convert(codec
, nid
,
4125 codec
->spdif_ctls
& 0xff, -1);
4128 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
4130 snd_hda_codec_cleanup_stream(codec
, nid
);
4131 if (codec
->slave_dig_outs
) {
4133 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4134 snd_hda_codec_cleanup_stream(codec
, *d
);
4139 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4140 * @bus: HD-audio bus
4142 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
4144 struct hda_codec
*codec
;
4148 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4149 #ifdef CONFIG_SND_HDA_POWER_SAVE
4150 if (!codec
->power_on
)
4153 if (codec
->patch_ops
.reboot_notify
)
4154 codec
->patch_ops
.reboot_notify(codec
);
4157 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify
);
4160 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4162 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
4163 struct hda_multi_out
*mout
)
4165 mutex_lock(&codec
->spdif_mutex
);
4166 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
4167 /* already opened as analog dup; reset it once */
4168 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4169 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
4170 mutex_unlock(&codec
->spdif_mutex
);
4173 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
4176 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4178 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
4179 struct hda_multi_out
*mout
,
4180 unsigned int stream_tag
,
4181 unsigned int format
,
4182 struct snd_pcm_substream
*substream
)
4184 mutex_lock(&codec
->spdif_mutex
);
4185 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
4186 mutex_unlock(&codec
->spdif_mutex
);
4189 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
4192 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4194 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
4195 struct hda_multi_out
*mout
)
4197 mutex_lock(&codec
->spdif_mutex
);
4198 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4199 mutex_unlock(&codec
->spdif_mutex
);
4202 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
4205 * snd_hda_multi_out_dig_close - release the digital out stream
4207 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
4208 struct hda_multi_out
*mout
)
4210 mutex_lock(&codec
->spdif_mutex
);
4211 mout
->dig_out_used
= 0;
4212 mutex_unlock(&codec
->spdif_mutex
);
4215 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
4218 * snd_hda_multi_out_analog_open - open analog outputs
4220 * Open analog outputs and set up the hw-constraints.
4221 * If the digital outputs can be opened as slave, open the digital
4224 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
4225 struct hda_multi_out
*mout
,
4226 struct snd_pcm_substream
*substream
,
4227 struct hda_pcm_stream
*hinfo
)
4229 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
4230 runtime
->hw
.channels_max
= mout
->max_channels
;
4231 if (mout
->dig_out_nid
) {
4232 if (!mout
->analog_rates
) {
4233 mout
->analog_rates
= hinfo
->rates
;
4234 mout
->analog_formats
= hinfo
->formats
;
4235 mout
->analog_maxbps
= hinfo
->maxbps
;
4237 runtime
->hw
.rates
= mout
->analog_rates
;
4238 runtime
->hw
.formats
= mout
->analog_formats
;
4239 hinfo
->maxbps
= mout
->analog_maxbps
;
4241 if (!mout
->spdif_rates
) {
4242 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
4244 &mout
->spdif_formats
,
4245 &mout
->spdif_maxbps
);
4247 mutex_lock(&codec
->spdif_mutex
);
4248 if (mout
->share_spdif
) {
4249 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
4250 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
4251 runtime
->hw
.rates
&= mout
->spdif_rates
;
4252 runtime
->hw
.formats
&= mout
->spdif_formats
;
4253 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
4254 hinfo
->maxbps
= mout
->spdif_maxbps
;
4256 mout
->share_spdif
= 0;
4257 /* FIXME: need notify? */
4260 mutex_unlock(&codec
->spdif_mutex
);
4262 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
4263 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
4265 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
4268 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4270 * Set up the i/o for analog out.
4271 * When the digital out is available, copy the front out to digital out, too.
4273 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
4274 struct hda_multi_out
*mout
,
4275 unsigned int stream_tag
,
4276 unsigned int format
,
4277 struct snd_pcm_substream
*substream
)
4279 hda_nid_t
*nids
= mout
->dac_nids
;
4280 int chs
= substream
->runtime
->channels
;
4283 mutex_lock(&codec
->spdif_mutex
);
4284 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
4285 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
4287 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
4289 !(codec
->spdif_status
& IEC958_AES0_NONAUDIO
)) {
4290 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
4291 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
4292 stream_tag
, format
);
4294 mout
->dig_out_used
= 0;
4295 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4298 mutex_unlock(&codec
->spdif_mutex
);
4301 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
4303 if (!mout
->no_share_stream
&&
4304 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
4305 /* headphone out will just decode front left/right (stereo) */
4306 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
4308 /* extra outputs copied from front */
4309 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4310 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
4311 snd_hda_codec_setup_stream(codec
,
4312 mout
->extra_out_nid
[i
],
4313 stream_tag
, 0, format
);
4316 for (i
= 1; i
< mout
->num_dacs
; i
++) {
4317 if (chs
>= (i
+ 1) * 2) /* independent out */
4318 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4320 else if (!mout
->no_share_stream
) /* copy front */
4321 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4326 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
4329 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4331 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
4332 struct hda_multi_out
*mout
)
4334 hda_nid_t
*nids
= mout
->dac_nids
;
4337 for (i
= 0; i
< mout
->num_dacs
; i
++)
4338 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
4340 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
4341 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4342 if (mout
->extra_out_nid
[i
])
4343 snd_hda_codec_cleanup_stream(codec
,
4344 mout
->extra_out_nid
[i
]);
4345 mutex_lock(&codec
->spdif_mutex
);
4346 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
4347 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4348 mout
->dig_out_used
= 0;
4350 mutex_unlock(&codec
->spdif_mutex
);
4353 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
4356 * Helper for automatic pin configuration
4359 static int is_in_nid_list(hda_nid_t nid
, hda_nid_t
*list
)
4361 for (; *list
; list
++)
4369 * Sort an associated group of pins according to their sequence numbers.
4371 static void sort_pins_by_sequence(hda_nid_t
*pins
, short *sequences
,
4378 for (i
= 0; i
< num_pins
; i
++) {
4379 for (j
= i
+ 1; j
< num_pins
; j
++) {
4380 if (sequences
[i
] > sequences
[j
]) {
4382 sequences
[i
] = sequences
[j
];
4393 /* add the found input-pin to the cfg->inputs[] table */
4394 static void add_auto_cfg_input_pin(struct auto_pin_cfg
*cfg
, hda_nid_t nid
,
4397 if (cfg
->num_inputs
< AUTO_CFG_MAX_INS
) {
4398 cfg
->inputs
[cfg
->num_inputs
].pin
= nid
;
4399 cfg
->inputs
[cfg
->num_inputs
].type
= type
;
4404 /* sort inputs in the order of AUTO_PIN_* type */
4405 static void sort_autocfg_input_pins(struct auto_pin_cfg
*cfg
)
4409 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4410 for (j
= i
+ 1; j
< cfg
->num_inputs
; j
++) {
4411 if (cfg
->inputs
[i
].type
> cfg
->inputs
[j
].type
) {
4412 struct auto_pin_cfg_item tmp
;
4413 tmp
= cfg
->inputs
[i
];
4414 cfg
->inputs
[i
] = cfg
->inputs
[j
];
4415 cfg
->inputs
[j
] = tmp
;
4422 * Parse all pin widgets and store the useful pin nids to cfg
4424 * The number of line-outs or any primary output is stored in line_outs,
4425 * and the corresponding output pins are assigned to line_out_pins[],
4426 * in the order of front, rear, CLFE, side, ...
4428 * If more extra outputs (speaker and headphone) are found, the pins are
4429 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4430 * is detected, one of speaker of HP pins is assigned as the primary
4431 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4432 * if any analog output exists.
4434 * The analog input pins are assigned to inputs array.
4435 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4438 int snd_hda_parse_pin_def_config(struct hda_codec
*codec
,
4439 struct auto_pin_cfg
*cfg
,
4440 hda_nid_t
*ignore_nids
)
4442 hda_nid_t nid
, end_nid
;
4443 short seq
, assoc_line_out
, assoc_speaker
;
4444 short sequences_line_out
[ARRAY_SIZE(cfg
->line_out_pins
)];
4445 short sequences_speaker
[ARRAY_SIZE(cfg
->speaker_pins
)];
4446 short sequences_hp
[ARRAY_SIZE(cfg
->hp_pins
)];
4449 memset(cfg
, 0, sizeof(*cfg
));
4451 memset(sequences_line_out
, 0, sizeof(sequences_line_out
));
4452 memset(sequences_speaker
, 0, sizeof(sequences_speaker
));
4453 memset(sequences_hp
, 0, sizeof(sequences_hp
));
4454 assoc_line_out
= assoc_speaker
= 0;
4456 end_nid
= codec
->start_nid
+ codec
->num_nodes
;
4457 for (nid
= codec
->start_nid
; nid
< end_nid
; nid
++) {
4458 unsigned int wid_caps
= get_wcaps(codec
, nid
);
4459 unsigned int wid_type
= get_wcaps_type(wid_caps
);
4460 unsigned int def_conf
;
4463 /* read all default configuration for pin complex */
4464 if (wid_type
!= AC_WID_PIN
)
4466 /* ignore the given nids (e.g. pc-beep returns error) */
4467 if (ignore_nids
&& is_in_nid_list(nid
, ignore_nids
))
4470 def_conf
= snd_hda_codec_get_pincfg(codec
, nid
);
4471 if (get_defcfg_connect(def_conf
) == AC_JACK_PORT_NONE
)
4473 loc
= get_defcfg_location(def_conf
);
4474 switch (get_defcfg_device(def_conf
)) {
4475 case AC_JACK_LINE_OUT
:
4476 seq
= get_defcfg_sequence(def_conf
);
4477 assoc
= get_defcfg_association(def_conf
);
4479 if (!(wid_caps
& AC_WCAP_STEREO
))
4480 if (!cfg
->mono_out_pin
)
4481 cfg
->mono_out_pin
= nid
;
4484 if (!assoc_line_out
)
4485 assoc_line_out
= assoc
;
4486 else if (assoc_line_out
!= assoc
)
4488 if (cfg
->line_outs
>= ARRAY_SIZE(cfg
->line_out_pins
))
4490 cfg
->line_out_pins
[cfg
->line_outs
] = nid
;
4491 sequences_line_out
[cfg
->line_outs
] = seq
;
4494 case AC_JACK_SPEAKER
:
4495 seq
= get_defcfg_sequence(def_conf
);
4496 assoc
= get_defcfg_association(def_conf
);
4500 assoc_speaker
= assoc
;
4501 else if (assoc_speaker
!= assoc
)
4503 if (cfg
->speaker_outs
>= ARRAY_SIZE(cfg
->speaker_pins
))
4505 cfg
->speaker_pins
[cfg
->speaker_outs
] = nid
;
4506 sequences_speaker
[cfg
->speaker_outs
] = seq
;
4507 cfg
->speaker_outs
++;
4509 case AC_JACK_HP_OUT
:
4510 seq
= get_defcfg_sequence(def_conf
);
4511 assoc
= get_defcfg_association(def_conf
);
4512 if (cfg
->hp_outs
>= ARRAY_SIZE(cfg
->hp_pins
))
4514 cfg
->hp_pins
[cfg
->hp_outs
] = nid
;
4515 sequences_hp
[cfg
->hp_outs
] = (assoc
<< 4) | seq
;
4518 case AC_JACK_MIC_IN
:
4519 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_MIC
);
4521 case AC_JACK_LINE_IN
:
4522 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_LINE_IN
);
4525 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_CD
);
4528 add_auto_cfg_input_pin(cfg
, nid
, AUTO_PIN_AUX
);
4530 case AC_JACK_SPDIF_OUT
:
4531 case AC_JACK_DIG_OTHER_OUT
:
4532 if (cfg
->dig_outs
>= ARRAY_SIZE(cfg
->dig_out_pins
))
4534 cfg
->dig_out_pins
[cfg
->dig_outs
] = nid
;
4535 cfg
->dig_out_type
[cfg
->dig_outs
] =
4536 (loc
== AC_JACK_LOC_HDMI
) ?
4537 HDA_PCM_TYPE_HDMI
: HDA_PCM_TYPE_SPDIF
;
4540 case AC_JACK_SPDIF_IN
:
4541 case AC_JACK_DIG_OTHER_IN
:
4542 cfg
->dig_in_pin
= nid
;
4543 if (loc
== AC_JACK_LOC_HDMI
)
4544 cfg
->dig_in_type
= HDA_PCM_TYPE_HDMI
;
4546 cfg
->dig_in_type
= HDA_PCM_TYPE_SPDIF
;
4552 * If no line-out is defined but multiple HPs are found,
4553 * some of them might be the real line-outs.
4555 if (!cfg
->line_outs
&& cfg
->hp_outs
> 1) {
4557 while (i
< cfg
->hp_outs
) {
4558 /* The real HPs should have the sequence 0x0f */
4559 if ((sequences_hp
[i
] & 0x0f) == 0x0f) {
4563 /* Move it to the line-out table */
4564 cfg
->line_out_pins
[cfg
->line_outs
] = cfg
->hp_pins
[i
];
4565 sequences_line_out
[cfg
->line_outs
] = sequences_hp
[i
];
4568 memmove(cfg
->hp_pins
+ i
, cfg
->hp_pins
+ i
+ 1,
4569 sizeof(cfg
->hp_pins
[0]) * (cfg
->hp_outs
- i
));
4570 memmove(sequences_hp
+ i
, sequences_hp
+ i
+ 1,
4571 sizeof(sequences_hp
[0]) * (cfg
->hp_outs
- i
));
4573 memset(cfg
->hp_pins
+ cfg
->hp_outs
, 0,
4574 sizeof(hda_nid_t
) * (AUTO_CFG_MAX_OUTS
- cfg
->hp_outs
));
4576 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4580 /* sort by sequence */
4581 sort_pins_by_sequence(cfg
->line_out_pins
, sequences_line_out
,
4583 sort_pins_by_sequence(cfg
->speaker_pins
, sequences_speaker
,
4585 sort_pins_by_sequence(cfg
->hp_pins
, sequences_hp
,
4589 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4590 * as a primary output
4592 if (!cfg
->line_outs
) {
4593 if (cfg
->speaker_outs
) {
4594 cfg
->line_outs
= cfg
->speaker_outs
;
4595 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
4596 sizeof(cfg
->speaker_pins
));
4597 cfg
->speaker_outs
= 0;
4598 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
4599 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
4600 } else if (cfg
->hp_outs
) {
4601 cfg
->line_outs
= cfg
->hp_outs
;
4602 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
4603 sizeof(cfg
->hp_pins
));
4605 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
4606 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4610 /* Reorder the surround channels
4611 * ALSA sequence is front/surr/clfe/side
4613 * 4-ch: front/surr => OK as it is
4614 * 6-ch: front/clfe/surr
4615 * 8-ch: front/clfe/rear/side|fc
4617 switch (cfg
->line_outs
) {
4620 nid
= cfg
->line_out_pins
[1];
4621 cfg
->line_out_pins
[1] = cfg
->line_out_pins
[2];
4622 cfg
->line_out_pins
[2] = nid
;
4626 sort_autocfg_input_pins(cfg
);
4629 * debug prints of the parsed results
4631 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4632 cfg
->line_outs
, cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
4633 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
4634 cfg
->line_out_pins
[4]);
4635 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4636 cfg
->speaker_outs
, cfg
->speaker_pins
[0],
4637 cfg
->speaker_pins
[1], cfg
->speaker_pins
[2],
4638 cfg
->speaker_pins
[3], cfg
->speaker_pins
[4]);
4639 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4640 cfg
->hp_outs
, cfg
->hp_pins
[0],
4641 cfg
->hp_pins
[1], cfg
->hp_pins
[2],
4642 cfg
->hp_pins
[3], cfg
->hp_pins
[4]);
4643 snd_printd(" mono: mono_out=0x%x\n", cfg
->mono_out_pin
);
4645 snd_printd(" dig-out=0x%x/0x%x\n",
4646 cfg
->dig_out_pins
[0], cfg
->dig_out_pins
[1]);
4647 snd_printd(" inputs:");
4648 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4649 snd_printdd(" %s=0x%x",
4650 hda_get_autocfg_input_label(codec
, cfg
, i
),
4651 cfg
->inputs
[i
].pin
);
4654 if (cfg
->dig_in_pin
)
4655 snd_printd(" dig-in=0x%x\n", cfg
->dig_in_pin
);
4659 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config
);
4661 int snd_hda_get_input_pin_attr(unsigned int def_conf
)
4663 unsigned int loc
= get_defcfg_location(def_conf
);
4664 unsigned int conn
= get_defcfg_connect(def_conf
);
4665 if (conn
== AC_JACK_PORT_NONE
)
4666 return INPUT_PIN_ATTR_UNUSED
;
4667 /* Windows may claim the internal mic to be BOTH, too */
4668 if (conn
== AC_JACK_PORT_FIXED
|| conn
== AC_JACK_PORT_BOTH
)
4669 return INPUT_PIN_ATTR_INT
;
4670 if ((loc
& 0x30) == AC_JACK_LOC_INTERNAL
)
4671 return INPUT_PIN_ATTR_INT
;
4672 if ((loc
& 0x30) == AC_JACK_LOC_SEPARATE
)
4673 return INPUT_PIN_ATTR_DOCK
;
4674 if (loc
== AC_JACK_LOC_REAR
)
4675 return INPUT_PIN_ATTR_REAR
;
4676 if (loc
== AC_JACK_LOC_FRONT
)
4677 return INPUT_PIN_ATTR_FRONT
;
4678 return INPUT_PIN_ATTR_NORMAL
;
4680 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr
);
4683 * hda_get_input_pin_label - Give a label for the given input pin
4685 * When check_location is true, the function checks the pin location
4686 * for mic and line-in pins, and set an appropriate prefix like "Front",
4687 * "Rear", "Internal".
4690 const char *hda_get_input_pin_label(struct hda_codec
*codec
, hda_nid_t pin
,
4693 unsigned int def_conf
;
4694 static const char * const mic_names
[] = {
4695 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4699 def_conf
= snd_hda_codec_get_pincfg(codec
, pin
);
4701 switch (get_defcfg_device(def_conf
)) {
4702 case AC_JACK_MIC_IN
:
4703 if (!check_location
)
4705 attr
= snd_hda_get_input_pin_attr(def_conf
);
4708 return mic_names
[attr
- 1];
4709 case AC_JACK_LINE_IN
:
4710 if (!check_location
)
4712 attr
= snd_hda_get_input_pin_attr(def_conf
);
4715 if (attr
== INPUT_PIN_ATTR_DOCK
)
4722 case AC_JACK_SPDIF_IN
:
4724 case AC_JACK_DIG_OTHER_IN
:
4725 return "Digital In";
4730 EXPORT_SYMBOL_HDA(hda_get_input_pin_label
);
4732 /* Check whether the location prefix needs to be added to the label.
4733 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4734 * have to put "Front" prefix to each label. In such a case, returns false.
4736 static int check_mic_location_need(struct hda_codec
*codec
,
4737 const struct auto_pin_cfg
*cfg
,
4743 defc
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[input
].pin
);
4744 attr
= snd_hda_get_input_pin_attr(defc
);
4745 /* for internal or docking mics, we need locations */
4746 if (attr
<= INPUT_PIN_ATTR_NORMAL
)
4750 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4751 defc
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[i
].pin
);
4752 attr2
= snd_hda_get_input_pin_attr(defc
);
4753 if (attr2
>= INPUT_PIN_ATTR_NORMAL
) {
4754 if (attr
&& attr
!= attr2
)
4755 return 1; /* different locations found */
4763 * hda_get_autocfg_input_label - Get a label for the given input
4765 * Get a label for the given input pin defined by the autocfg item.
4766 * Unlike hda_get_input_pin_label(), this function checks all inputs
4767 * defined in autocfg and avoids the redundant mic/line prefix as much as
4770 const char *hda_get_autocfg_input_label(struct hda_codec
*codec
,
4771 const struct auto_pin_cfg
*cfg
,
4774 int type
= cfg
->inputs
[input
].type
;
4775 int has_multiple_pins
= 0;
4777 if ((input
> 0 && cfg
->inputs
[input
- 1].type
== type
) ||
4778 (input
< cfg
->num_inputs
- 1 && cfg
->inputs
[input
+ 1].type
== type
))
4779 has_multiple_pins
= 1;
4780 if (has_multiple_pins
&& type
== AUTO_PIN_MIC
)
4781 has_multiple_pins
&= check_mic_location_need(codec
, cfg
, input
);
4782 return hda_get_input_pin_label(codec
, cfg
->inputs
[input
].pin
,
4785 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label
);
4788 * snd_hda_add_imux_item - Add an item to input_mux
4790 * When the same label is used already in the existing items, the number
4791 * suffix is appended to the label. This label index number is stored
4792 * to type_idx when non-NULL pointer is given.
4794 int snd_hda_add_imux_item(struct hda_input_mux
*imux
, const char *label
,
4795 int index
, int *type_idx
)
4797 int i
, label_idx
= 0;
4798 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
4799 snd_printd(KERN_ERR
"hda_codec: Too many imux items!\n");
4802 for (i
= 0; i
< imux
->num_items
; i
++) {
4803 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
4807 *type_idx
= label_idx
;
4809 snprintf(imux
->items
[imux
->num_items
].label
,
4810 sizeof(imux
->items
[imux
->num_items
].label
),
4811 "%s %d", label
, label_idx
);
4813 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
4814 sizeof(imux
->items
[imux
->num_items
].label
));
4815 imux
->items
[imux
->num_items
].index
= index
;
4819 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item
);
4828 * snd_hda_suspend - suspend the codecs
4831 * Returns 0 if successful.
4833 int snd_hda_suspend(struct hda_bus
*bus
)
4835 struct hda_codec
*codec
;
4837 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4838 #ifdef CONFIG_SND_HDA_POWER_SAVE
4839 if (!codec
->power_on
)
4842 hda_call_codec_suspend(codec
);
4846 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
4849 * snd_hda_resume - resume the codecs
4852 * Returns 0 if successful.
4854 * This fucntion is defined only when POWER_SAVE isn't set.
4855 * In the power-save mode, the codec is resumed dynamically.
4857 int snd_hda_resume(struct hda_bus
*bus
)
4859 struct hda_codec
*codec
;
4861 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4862 if (snd_hda_codec_needs_resume(codec
))
4863 hda_call_codec_resume(codec
);
4867 EXPORT_SYMBOL_HDA(snd_hda_resume
);
4868 #endif /* CONFIG_PM */
4875 * snd_array_new - get a new element from the given array
4876 * @array: the array object
4878 * Get a new element from the given array. If it exceeds the
4879 * pre-allocated array size, re-allocate the array.
4881 * Returns NULL if allocation failed.
4883 void *snd_array_new(struct snd_array
*array
)
4885 if (array
->used
>= array
->alloced
) {
4886 int num
= array
->alloced
+ array
->alloc_align
;
4888 if (snd_BUG_ON(num
>= 4096))
4890 nlist
= kcalloc(num
+ 1, array
->elem_size
, GFP_KERNEL
);
4894 memcpy(nlist
, array
->list
,
4895 array
->elem_size
* array
->alloced
);
4898 array
->list
= nlist
;
4899 array
->alloced
= num
;
4901 return snd_array_elem(array
, array
->used
++);
4903 EXPORT_SYMBOL_HDA(snd_array_new
);
4906 * snd_array_free - free the given array elements
4907 * @array: the array object
4909 void snd_array_free(struct snd_array
*array
)
4916 EXPORT_SYMBOL_HDA(snd_array_free
);
4919 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4920 * @pcm: PCM caps bits
4921 * @buf: the string buffer to write
4922 * @buflen: the max buffer length
4924 * used by hda_proc.c and hda_eld.c
4926 void snd_print_pcm_rates(int pcm
, char *buf
, int buflen
)
4928 static unsigned int rates
[] = {
4929 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4930 96000, 176400, 192000, 384000
4934 for (i
= 0, j
= 0; i
< ARRAY_SIZE(rates
); i
++)
4936 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", rates
[i
]);
4938 buf
[j
] = '\0'; /* necessary when j == 0 */
4940 EXPORT_SYMBOL_HDA(snd_print_pcm_rates
);
4943 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4944 * @pcm: PCM caps bits
4945 * @buf: the string buffer to write
4946 * @buflen: the max buffer length
4948 * used by hda_proc.c and hda_eld.c
4950 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
4952 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
4955 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
4956 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
4957 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
4959 buf
[j
] = '\0'; /* necessary when j == 0 */
4961 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
4963 #ifdef CONFIG_SND_HDA_INPUT_JACK
4965 * Input-jack notification support
4967 struct hda_jack_item
{
4970 struct snd_jack
*jack
;
4973 static const char *get_jack_default_name(struct hda_codec
*codec
, hda_nid_t nid
,
4977 case SND_JACK_HEADPHONE
:
4979 case SND_JACK_MICROPHONE
:
4981 case SND_JACK_LINEOUT
:
4983 case SND_JACK_HEADSET
:
4990 static void hda_free_jack_priv(struct snd_jack
*jack
)
4992 struct hda_jack_item
*jacks
= jack
->private_data
;
4997 int snd_hda_input_jack_add(struct hda_codec
*codec
, hda_nid_t nid
, int type
,
5000 struct hda_jack_item
*jack
;
5003 snd_array_init(&codec
->jacks
, sizeof(*jack
), 32);
5004 jack
= snd_array_new(&codec
->jacks
);
5011 name
= get_jack_default_name(codec
, nid
, type
);
5012 err
= snd_jack_new(codec
->bus
->card
, name
, type
, &jack
->jack
);
5017 jack
->jack
->private_data
= jack
;
5018 jack
->jack
->private_free
= hda_free_jack_priv
;
5021 EXPORT_SYMBOL_HDA(snd_hda_input_jack_add
);
5023 void snd_hda_input_jack_report(struct hda_codec
*codec
, hda_nid_t nid
)
5025 struct hda_jack_item
*jacks
= codec
->jacks
.list
;
5031 for (i
= 0; i
< codec
->jacks
.used
; i
++, jacks
++) {
5032 unsigned int pin_ctl
;
5033 unsigned int present
;
5036 if (jacks
->nid
!= nid
)
5038 present
= snd_hda_jack_detect(codec
, nid
);
5040 if (type
== (SND_JACK_HEADPHONE
| SND_JACK_LINEOUT
)) {
5041 pin_ctl
= snd_hda_codec_read(codec
, nid
, 0,
5042 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5043 type
= (pin_ctl
& AC_PINCTL_HP_EN
) ?
5044 SND_JACK_HEADPHONE
: SND_JACK_LINEOUT
;
5046 snd_jack_report(jacks
->jack
, present
? type
: 0);
5049 EXPORT_SYMBOL_HDA(snd_hda_input_jack_report
);
5051 /* free jack instances manually when clearing/reconfiguring */
5052 void snd_hda_input_jack_free(struct hda_codec
*codec
)
5054 if (!codec
->bus
->shutdown
&& codec
->jacks
.list
) {
5055 struct hda_jack_item
*jacks
= codec
->jacks
.list
;
5057 for (i
= 0; i
< codec
->jacks
.used
; i
++, jacks
++) {
5059 snd_device_free(codec
->bus
->card
, jacks
->jack
);
5062 snd_array_free(&codec
->jacks
);
5064 EXPORT_SYMBOL_HDA(snd_hda_input_jack_free
);
5065 #endif /* CONFIG_SND_HDA_INPUT_JACK */
5067 MODULE_DESCRIPTION("HDA codec core");
5068 MODULE_LICENSE("GPL");