2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
30 #include <linux/pm_runtime.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include <sound/asoundef.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <sound/jack.h>
37 #include "hda_local.h"
40 #include <sound/hda_hwdep.h>
43 #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
44 #define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
47 #define codec_in_pm(codec) 0
48 #define hda_codec_is_power_on(codec) 1
51 #define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53 #define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
57 * Send and receive a verb - passed to exec_verb override for hdac_device
59 static int codec_exec_verb(struct hdac_device
*dev
, unsigned int cmd
,
60 unsigned int flags
, unsigned int *res
)
62 struct hda_codec
*codec
= container_of(dev
, struct hda_codec
, core
);
63 struct hda_bus
*bus
= codec
->bus
;
70 snd_hda_power_up_pm(codec
);
71 mutex_lock(&bus
->core
.cmd_mutex
);
72 if (flags
& HDA_RW_NO_RESPONSE_FALLBACK
)
73 bus
->no_response_fallback
= 1;
74 err
= snd_hdac_bus_exec_verb_unlocked(&bus
->core
, codec
->core
.addr
,
76 bus
->no_response_fallback
= 0;
77 mutex_unlock(&bus
->core
.cmd_mutex
);
78 snd_hda_power_down_pm(codec
);
79 if (!codec_in_pm(codec
) && res
&& err
== -EAGAIN
) {
80 if (bus
->response_reset
) {
82 "resetting BUS due to fatal communication error\n");
83 snd_hda_bus_reset(bus
);
87 /* clear reset-flag when the communication gets recovered */
88 if (!err
|| codec_in_pm(codec
))
89 bus
->response_reset
= 0;
94 * snd_hda_sequence_write - sequence writes
95 * @codec: the HDA codec
96 * @seq: VERB array to send
98 * Send the commands sequentially from the given array.
99 * The array must be terminated with NID=0.
101 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
103 for (; seq
->nid
; seq
++)
104 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
106 EXPORT_SYMBOL_GPL(snd_hda_sequence_write
);
108 /* connection list element */
109 struct hda_conn_list
{
110 struct list_head list
;
116 /* look up the cached results */
117 static struct hda_conn_list
*
118 lookup_conn_list(struct hda_codec
*codec
, hda_nid_t nid
)
120 struct hda_conn_list
*p
;
121 list_for_each_entry(p
, &codec
->conn_list
, list
) {
128 static int add_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
129 const hda_nid_t
*list
)
131 struct hda_conn_list
*p
;
133 p
= kmalloc(sizeof(*p
) + len
* sizeof(hda_nid_t
), GFP_KERNEL
);
138 memcpy(p
->conns
, list
, len
* sizeof(hda_nid_t
));
139 list_add(&p
->list
, &codec
->conn_list
);
143 static void remove_conn_list(struct hda_codec
*codec
)
145 while (!list_empty(&codec
->conn_list
)) {
146 struct hda_conn_list
*p
;
147 p
= list_first_entry(&codec
->conn_list
, typeof(*p
), list
);
153 /* read the connection and add to the cache */
154 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
157 hda_nid_t
*result
= list
;
160 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
161 if (len
== -ENOSPC
) {
162 len
= snd_hda_get_num_raw_conns(codec
, nid
);
163 result
= kmalloc(sizeof(hda_nid_t
) * len
, GFP_KERNEL
);
166 len
= snd_hda_get_raw_connections(codec
, nid
, result
, len
);
169 len
= snd_hda_override_conn_list(codec
, nid
, len
, result
);
176 * snd_hda_get_conn_list - get connection list
177 * @codec: the HDA codec
179 * @listp: the pointer to store NID list
181 * Parses the connection list of the given widget and stores the pointer
182 * to the list of NIDs.
184 * Returns the number of connections, or a negative error code.
186 * Note that the returned pointer isn't protected against the list
187 * modification. If snd_hda_override_conn_list() might be called
188 * concurrently, protect with a mutex appropriately.
190 int snd_hda_get_conn_list(struct hda_codec
*codec
, hda_nid_t nid
,
191 const hda_nid_t
**listp
)
197 const struct hda_conn_list
*p
;
199 /* if the connection-list is already cached, read it */
200 p
= lookup_conn_list(codec
, nid
);
206 if (snd_BUG_ON(added
))
209 err
= read_and_add_raw_conns(codec
, nid
);
215 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list
);
218 * snd_hda_get_connections - copy connection list
219 * @codec: the HDA codec
221 * @conn_list: connection list array; when NULL, checks only the size
222 * @max_conns: max. number of connections to store
224 * Parses the connection list of the given widget and stores the list
227 * Returns the number of connections, or a negative error code.
229 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
230 hda_nid_t
*conn_list
, int max_conns
)
232 const hda_nid_t
*list
;
233 int len
= snd_hda_get_conn_list(codec
, nid
, &list
);
235 if (len
> 0 && conn_list
) {
236 if (len
> max_conns
) {
237 codec_err(codec
, "Too many connections %d for NID 0x%x\n",
241 memcpy(conn_list
, list
, len
* sizeof(hda_nid_t
));
246 EXPORT_SYMBOL_GPL(snd_hda_get_connections
);
249 * snd_hda_override_conn_list - add/modify the connection-list to cache
250 * @codec: the HDA codec
252 * @len: number of connection list entries
253 * @list: the list of connection entries
255 * Add or modify the given connection-list to the cache. If the corresponding
256 * cache already exists, invalidate it and append a new one.
258 * Returns zero or a negative error code.
260 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
261 const hda_nid_t
*list
)
263 struct hda_conn_list
*p
;
265 p
= lookup_conn_list(codec
, nid
);
271 return add_conn_list(codec
, nid
, len
, list
);
273 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list
);
276 * snd_hda_get_conn_index - get the connection index of the given NID
277 * @codec: the HDA codec
278 * @mux: NID containing the list
279 * @nid: NID to select
280 * @recursive: 1 when searching NID recursively, otherwise 0
282 * Parses the connection list of the widget @mux and checks whether the
283 * widget @nid is present. If it is, return the connection index.
284 * Otherwise it returns -1.
286 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
287 hda_nid_t nid
, int recursive
)
289 const hda_nid_t
*conn
;
292 nums
= snd_hda_get_conn_list(codec
, mux
, &conn
);
293 for (i
= 0; i
< nums
; i
++)
298 if (recursive
> 10) {
299 codec_dbg(codec
, "too deep connection for 0x%x\n", nid
);
303 for (i
= 0; i
< nums
; i
++) {
304 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
305 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
307 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
312 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index
);
315 /* return DEVLIST_LEN parameter of the given widget */
316 static unsigned int get_num_devices(struct hda_codec
*codec
, hda_nid_t nid
)
318 unsigned int wcaps
= get_wcaps(codec
, nid
);
321 if (!codec
->dp_mst
|| !(wcaps
& AC_WCAP_DIGITAL
) ||
322 get_wcaps_type(wcaps
) != AC_WID_PIN
)
325 parm
= snd_hdac_read_parm_uncached(&codec
->core
, nid
, AC_PAR_DEVLIST_LEN
);
328 return parm
& AC_DEV_LIST_LEN_MASK
;
332 * snd_hda_get_devices - copy device list without cache
333 * @codec: the HDA codec
334 * @nid: NID of the pin to parse
335 * @dev_list: device list array
336 * @max_devices: max. number of devices to store
338 * Copy the device list. This info is dynamic and so not cached.
339 * Currently called only from hda_proc.c, so not exported.
341 int snd_hda_get_devices(struct hda_codec
*codec
, hda_nid_t nid
,
342 u8
*dev_list
, int max_devices
)
345 int i
, dev_len
, devices
;
347 parm
= get_num_devices(codec
, nid
);
348 if (!parm
) /* not multi-stream capable */
352 dev_len
= dev_len
< max_devices
? dev_len
: max_devices
;
355 while (devices
< dev_len
) {
356 if (snd_hdac_read(&codec
->core
, nid
,
357 AC_VERB_GET_DEVICE_LIST
, devices
, &parm
))
360 for (i
= 0; i
< 8; i
++) {
361 dev_list
[devices
] = (u8
)parm
;
364 if (devices
>= dev_len
)
372 * read widget caps for each widget and store in cache
374 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
379 codec
->wcaps
= kmalloc(codec
->core
.num_nodes
* 4, GFP_KERNEL
);
382 nid
= codec
->core
.start_nid
;
383 for (i
= 0; i
< codec
->core
.num_nodes
; i
++, nid
++)
384 codec
->wcaps
[i
] = snd_hdac_read_parm_uncached(&codec
->core
,
385 nid
, AC_PAR_AUDIO_WIDGET_CAP
);
389 /* read all pin default configurations and save codec->init_pins */
390 static int read_pin_defaults(struct hda_codec
*codec
)
394 for_each_hda_codec_node(nid
, codec
) {
395 struct hda_pincfg
*pin
;
396 unsigned int wcaps
= get_wcaps(codec
, nid
);
397 unsigned int wid_type
= get_wcaps_type(wcaps
);
398 if (wid_type
!= AC_WID_PIN
)
400 pin
= snd_array_new(&codec
->init_pins
);
404 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
405 AC_VERB_GET_CONFIG_DEFAULT
, 0);
406 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
407 AC_VERB_GET_PIN_WIDGET_CONTROL
,
413 /* look up the given pin config list and return the item matching with NID */
414 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
415 struct snd_array
*array
,
419 for (i
= 0; i
< array
->used
; i
++) {
420 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
427 /* set the current pin config value for the given NID.
428 * the value is cached, and read via snd_hda_codec_get_pincfg()
430 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
431 hda_nid_t nid
, unsigned int cfg
)
433 struct hda_pincfg
*pin
;
435 /* the check below may be invalid when pins are added by a fixup
436 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
440 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
444 pin
= look_up_pincfg(codec
, list
, nid
);
446 pin
= snd_array_new(list
);
456 * snd_hda_codec_set_pincfg - Override a pin default configuration
457 * @codec: the HDA codec
458 * @nid: NID to set the pin config
459 * @cfg: the pin default config value
461 * Override a pin default configuration value in the cache.
462 * This value can be read by snd_hda_codec_get_pincfg() in a higher
463 * priority than the real hardware value.
465 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
466 hda_nid_t nid
, unsigned int cfg
)
468 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
470 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg
);
473 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
474 * @codec: the HDA codec
475 * @nid: NID to get the pin config
477 * Get the current pin config value of the given pin NID.
478 * If the pincfg value is cached or overridden via sysfs or driver,
479 * returns the cached value.
481 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
483 struct hda_pincfg
*pin
;
485 #ifdef CONFIG_SND_HDA_RECONFIG
487 unsigned int cfg
= 0;
488 mutex_lock(&codec
->user_mutex
);
489 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
492 mutex_unlock(&codec
->user_mutex
);
497 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
500 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
505 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg
);
508 * snd_hda_codec_set_pin_target - remember the current pinctl target value
509 * @codec: the HDA codec
511 * @val: assigned pinctl value
513 * This function stores the given value to a pinctl target value in the
514 * pincfg table. This isn't always as same as the actually written value
515 * but can be referred at any time via snd_hda_codec_get_pin_target().
517 int snd_hda_codec_set_pin_target(struct hda_codec
*codec
, hda_nid_t nid
,
520 struct hda_pincfg
*pin
;
522 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
528 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target
);
531 * snd_hda_codec_get_pin_target - return the current pinctl target value
532 * @codec: the HDA codec
535 int snd_hda_codec_get_pin_target(struct hda_codec
*codec
, hda_nid_t nid
)
537 struct hda_pincfg
*pin
;
539 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
544 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target
);
547 * snd_hda_shutup_pins - Shut up all pins
548 * @codec: the HDA codec
550 * Clear all pin controls to shup up before suspend for avoiding click noise.
551 * The controls aren't cached so that they can be resumed properly.
553 void snd_hda_shutup_pins(struct hda_codec
*codec
)
556 /* don't shut up pins when unloading the driver; otherwise it breaks
557 * the default pin setup at the next load of the driver
559 if (codec
->bus
->shutdown
)
561 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
562 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
563 /* use read here for syncing after issuing each verb */
564 snd_hda_codec_read(codec
, pin
->nid
, 0,
565 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
567 codec
->pins_shutup
= 1;
569 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins
);
572 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
573 static void restore_shutup_pins(struct hda_codec
*codec
)
576 if (!codec
->pins_shutup
)
578 if (codec
->bus
->shutdown
)
580 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
581 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
582 snd_hda_codec_write(codec
, pin
->nid
, 0,
583 AC_VERB_SET_PIN_WIDGET_CONTROL
,
586 codec
->pins_shutup
= 0;
590 static void hda_jackpoll_work(struct work_struct
*work
)
592 struct hda_codec
*codec
=
593 container_of(work
, struct hda_codec
, jackpoll_work
.work
);
595 snd_hda_jack_set_dirty_all(codec
);
596 snd_hda_jack_poll_all(codec
);
598 if (!codec
->jackpoll_interval
)
601 schedule_delayed_work(&codec
->jackpoll_work
,
602 codec
->jackpoll_interval
);
605 /* release all pincfg lists */
606 static void free_init_pincfgs(struct hda_codec
*codec
)
608 snd_array_free(&codec
->driver_pins
);
609 #ifdef CONFIG_SND_HDA_RECONFIG
610 snd_array_free(&codec
->user_pins
);
612 snd_array_free(&codec
->init_pins
);
616 * audio-converter setup caches
618 struct hda_cvt_setup
{
623 unsigned char active
; /* cvt is currently used */
624 unsigned char dirty
; /* setups should be cleared */
627 /* get or create a cache entry for the given audio converter NID */
628 static struct hda_cvt_setup
*
629 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
631 struct hda_cvt_setup
*p
;
634 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
635 p
= snd_array_elem(&codec
->cvt_setups
, i
);
639 p
= snd_array_new(&codec
->cvt_setups
);
648 static void release_pcm(struct kref
*kref
)
650 struct hda_pcm
*pcm
= container_of(kref
, struct hda_pcm
, kref
);
653 snd_device_free(pcm
->codec
->card
, pcm
->pcm
);
654 clear_bit(pcm
->device
, pcm
->codec
->bus
->pcm_dev_bits
);
659 void snd_hda_codec_pcm_put(struct hda_pcm
*pcm
)
661 kref_put(&pcm
->kref
, release_pcm
);
663 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put
);
665 struct hda_pcm
*snd_hda_codec_pcm_new(struct hda_codec
*codec
,
666 const char *fmt
, ...)
671 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
676 kref_init(&pcm
->kref
);
678 pcm
->name
= kvasprintf(GFP_KERNEL
, fmt
, args
);
685 list_add_tail(&pcm
->list
, &codec
->pcm_list_head
);
688 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new
);
693 static void codec_release_pcms(struct hda_codec
*codec
)
695 struct hda_pcm
*pcm
, *n
;
697 list_for_each_entry_safe(pcm
, n
, &codec
->pcm_list_head
, list
) {
698 list_del_init(&pcm
->list
);
700 snd_device_disconnect(codec
->card
, pcm
->pcm
);
701 snd_hda_codec_pcm_put(pcm
);
705 void snd_hda_codec_cleanup_for_unbind(struct hda_codec
*codec
)
707 if (codec
->registered
) {
708 /* pm_runtime_put() is called in snd_hdac_device_exit() */
709 pm_runtime_get_noresume(hda_codec_dev(codec
));
710 pm_runtime_disable(hda_codec_dev(codec
));
711 codec
->registered
= 0;
714 cancel_delayed_work_sync(&codec
->jackpoll_work
);
715 if (!codec
->in_freeing
)
716 snd_hda_ctls_clear(codec
);
717 codec_release_pcms(codec
);
718 snd_hda_detach_beep_device(codec
);
719 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
720 snd_hda_jack_tbl_clear(codec
);
721 codec
->proc_widget_hook
= NULL
;
724 /* free only driver_pins so that init_pins + user_pins are restored */
725 snd_array_free(&codec
->driver_pins
);
726 snd_array_free(&codec
->cvt_setups
);
727 snd_array_free(&codec
->spdif_out
);
728 snd_array_free(&codec
->verbs
);
729 codec
->preset
= NULL
;
730 codec
->slave_dig_outs
= NULL
;
731 codec
->spdif_status_reset
= 0;
732 snd_array_free(&codec
->mixers
);
733 snd_array_free(&codec
->nids
);
734 remove_conn_list(codec
);
735 snd_hdac_regmap_exit(&codec
->core
);
738 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
739 unsigned int power_state
);
741 /* also called from hda_bind.c */
742 void snd_hda_codec_register(struct hda_codec
*codec
)
744 if (codec
->registered
)
746 if (device_is_registered(hda_codec_dev(codec
))) {
747 snd_hda_register_beep_device(codec
);
748 snd_hdac_link_power(&codec
->core
, true);
749 pm_runtime_enable(hda_codec_dev(codec
));
750 /* it was powered up in snd_hda_codec_new(), now all done */
751 snd_hda_power_down(codec
);
752 codec
->registered
= 1;
756 static int snd_hda_codec_dev_register(struct snd_device
*device
)
758 snd_hda_codec_register(device
->device_data
);
762 static int snd_hda_codec_dev_disconnect(struct snd_device
*device
)
764 struct hda_codec
*codec
= device
->device_data
;
766 snd_hda_detach_beep_device(codec
);
770 static int snd_hda_codec_dev_free(struct snd_device
*device
)
772 struct hda_codec
*codec
= device
->device_data
;
774 codec
->in_freeing
= 1;
775 snd_hdac_device_unregister(&codec
->core
);
776 snd_hdac_link_power(&codec
->core
, false);
777 put_device(hda_codec_dev(codec
));
781 static void snd_hda_codec_dev_release(struct device
*dev
)
783 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
785 free_init_pincfgs(codec
);
786 snd_hdac_device_exit(&codec
->core
);
787 snd_hda_sysfs_clear(codec
);
788 kfree(codec
->modelname
);
794 * snd_hda_codec_new - create a HDA codec
795 * @bus: the bus to assign
796 * @codec_addr: the codec address
797 * @codecp: the pointer to store the generated codec
799 * Returns 0 if successful, or a negative error code.
801 int snd_hda_codec_new(struct hda_bus
*bus
, struct snd_card
*card
,
802 unsigned int codec_addr
, struct hda_codec
**codecp
)
804 struct hda_codec
*codec
;
808 static struct snd_device_ops dev_ops
= {
809 .dev_register
= snd_hda_codec_dev_register
,
810 .dev_disconnect
= snd_hda_codec_dev_disconnect
,
811 .dev_free
= snd_hda_codec_dev_free
,
814 if (snd_BUG_ON(!bus
))
816 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
819 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
823 sprintf(component
, "hdaudioC%dD%d", card
->number
, codec_addr
);
824 err
= snd_hdac_device_init(&codec
->core
, &bus
->core
, component
,
831 codec
->core
.dev
.release
= snd_hda_codec_dev_release
;
832 codec
->core
.type
= HDA_DEV_LEGACY
;
833 codec
->core
.exec_verb
= codec_exec_verb
;
837 codec
->addr
= codec_addr
;
838 mutex_init(&codec
->spdif_mutex
);
839 mutex_init(&codec
->control_mutex
);
840 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
841 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
842 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
843 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
844 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
845 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
846 snd_array_init(&codec
->jacktbl
, sizeof(struct hda_jack_tbl
), 16);
847 snd_array_init(&codec
->verbs
, sizeof(struct hda_verb
*), 8);
848 INIT_LIST_HEAD(&codec
->conn_list
);
849 INIT_LIST_HEAD(&codec
->pcm_list_head
);
851 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
852 codec
->depop_delay
= -1;
853 codec
->fixup_id
= HDA_FIXUP_ID_NOT_SET
;
856 codec
->power_jiffies
= jiffies
;
859 snd_hda_sysfs_init(codec
);
861 if (codec
->bus
->modelname
) {
862 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
863 if (!codec
->modelname
) {
869 fg
= codec
->core
.afg
? codec
->core
.afg
: codec
->core
.mfg
;
870 err
= read_widget_caps(codec
, fg
);
873 err
= read_pin_defaults(codec
);
877 /* power-up all before initialization */
878 hda_set_power_state(codec
, AC_PWRST_D0
);
880 snd_hda_codec_proc_new(codec
);
882 snd_hda_create_hwdep(codec
);
884 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->core
.vendor_id
,
885 codec
->core
.subsystem_id
, codec
->core
.revision_id
);
886 snd_component_add(card
, component
);
888 err
= snd_device_new(card
, SNDRV_DEV_CODEC
, codec
, &dev_ops
);
897 put_device(hda_codec_dev(codec
));
900 EXPORT_SYMBOL_GPL(snd_hda_codec_new
);
903 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
904 * @codec: the HDA codec
906 * Forcibly refresh the all widget caps and the init pin configurations of
909 int snd_hda_codec_update_widgets(struct hda_codec
*codec
)
914 err
= snd_hdac_refresh_widget_sysfs(&codec
->core
);
918 /* Assume the function group node does not change,
919 * only the widget nodes may change.
922 fg
= codec
->core
.afg
? codec
->core
.afg
: codec
->core
.mfg
;
923 err
= read_widget_caps(codec
, fg
);
927 snd_array_free(&codec
->init_pins
);
928 err
= read_pin_defaults(codec
);
932 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets
);
934 /* update the stream-id if changed */
935 static void update_pcm_stream_id(struct hda_codec
*codec
,
936 struct hda_cvt_setup
*p
, hda_nid_t nid
,
937 u32 stream_tag
, int channel_id
)
939 unsigned int oldval
, newval
;
941 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
942 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
943 newval
= (stream_tag
<< 4) | channel_id
;
944 if (oldval
!= newval
)
945 snd_hda_codec_write(codec
, nid
, 0,
946 AC_VERB_SET_CHANNEL_STREAMID
,
948 p
->stream_tag
= stream_tag
;
949 p
->channel_id
= channel_id
;
953 /* update the format-id if changed */
954 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
955 hda_nid_t nid
, int format
)
959 if (p
->format_id
!= format
) {
960 oldval
= snd_hda_codec_read(codec
, nid
, 0,
961 AC_VERB_GET_STREAM_FORMAT
, 0);
962 if (oldval
!= format
) {
964 snd_hda_codec_write(codec
, nid
, 0,
965 AC_VERB_SET_STREAM_FORMAT
,
968 p
->format_id
= format
;
973 * snd_hda_codec_setup_stream - set up the codec for streaming
974 * @codec: the CODEC to set up
975 * @nid: the NID to set up
976 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
977 * @channel_id: channel id to pass, zero based.
978 * @format: stream format.
980 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
982 int channel_id
, int format
)
985 struct hda_cvt_setup
*p
;
993 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
994 nid
, stream_tag
, channel_id
, format
);
995 p
= get_hda_cvt_setup(codec
, nid
);
999 if (codec
->patch_ops
.stream_pm
)
1000 codec
->patch_ops
.stream_pm(codec
, nid
, true);
1001 if (codec
->pcm_format_first
)
1002 update_pcm_format(codec
, p
, nid
, format
);
1003 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1004 if (!codec
->pcm_format_first
)
1005 update_pcm_format(codec
, p
, nid
, format
);
1010 /* make other inactive cvts with the same stream-tag dirty */
1011 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1012 list_for_each_codec(c
, codec
->bus
) {
1013 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1014 p
= snd_array_elem(&c
->cvt_setups
, i
);
1015 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1016 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1021 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream
);
1023 static void really_cleanup_stream(struct hda_codec
*codec
,
1024 struct hda_cvt_setup
*q
);
1027 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1028 * @codec: the CODEC to clean up
1029 * @nid: the NID to clean up
1030 * @do_now: really clean up the stream instead of clearing the active flag
1032 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1035 struct hda_cvt_setup
*p
;
1040 if (codec
->no_sticky_stream
)
1043 codec_dbg(codec
, "hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1044 p
= get_hda_cvt_setup(codec
, nid
);
1046 /* here we just clear the active flag when do_now isn't set;
1047 * actual clean-ups will be done later in
1048 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1051 really_cleanup_stream(codec
, p
);
1056 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream
);
1058 static void really_cleanup_stream(struct hda_codec
*codec
,
1059 struct hda_cvt_setup
*q
)
1061 hda_nid_t nid
= q
->nid
;
1062 if (q
->stream_tag
|| q
->channel_id
)
1063 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1065 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1067 memset(q
, 0, sizeof(*q
));
1069 if (codec
->patch_ops
.stream_pm
)
1070 codec
->patch_ops
.stream_pm(codec
, nid
, false);
1073 /* clean up the all conflicting obsolete streams */
1074 static void purify_inactive_streams(struct hda_codec
*codec
)
1076 struct hda_codec
*c
;
1079 list_for_each_codec(c
, codec
->bus
) {
1080 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1081 struct hda_cvt_setup
*p
;
1082 p
= snd_array_elem(&c
->cvt_setups
, i
);
1084 really_cleanup_stream(c
, p
);
1090 /* clean up all streams; called from suspend */
1091 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1095 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1096 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1098 really_cleanup_stream(codec
, p
);
1104 * amp access functions
1108 * query_amp_caps - query AMP capabilities
1109 * @codec: the HD-auio codec
1110 * @nid: the NID to query
1111 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1113 * Query AMP capabilities for the given widget and direction.
1114 * Returns the obtained capability bits.
1116 * When cap bits have been already read, this doesn't read again but
1117 * returns the cached value.
1119 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1121 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1122 nid
= codec
->core
.afg
;
1123 return snd_hda_param_read(codec
, nid
,
1124 direction
== HDA_OUTPUT
?
1125 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1127 EXPORT_SYMBOL_GPL(query_amp_caps
);
1130 * snd_hda_check_amp_caps - query AMP capabilities
1131 * @codec: the HD-audio codec
1132 * @nid: the NID to query
1133 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1134 * @bits: bit mask to check the result
1136 * Check whether the widget has the given amp capability for the direction.
1138 bool snd_hda_check_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1139 int dir
, unsigned int bits
)
1143 if (get_wcaps(codec
, nid
) & (1 << (dir
+ 1)))
1144 if (query_amp_caps(codec
, nid
, dir
) & bits
)
1148 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps
);
1151 * snd_hda_override_amp_caps - Override the AMP capabilities
1152 * @codec: the CODEC to clean up
1153 * @nid: the NID to clean up
1154 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1155 * @caps: the capability bits to set
1157 * Override the cached AMP caps bits value by the given one.
1158 * This function is useful if the driver needs to adjust the AMP ranges,
1159 * e.g. limit to 0dB, etc.
1161 * Returns zero if successful or a negative error code.
1163 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1168 snd_hda_override_wcaps(codec
, nid
,
1169 get_wcaps(codec
, nid
) | AC_WCAP_AMP_OVRD
);
1170 parm
= dir
== HDA_OUTPUT
? AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
;
1171 return snd_hdac_override_parm(&codec
->core
, nid
, parm
, caps
);
1173 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps
);
1176 * snd_hda_codec_amp_update - update the AMP mono value
1177 * @codec: HD-audio codec
1178 * @nid: NID to read the AMP value
1179 * @ch: channel to update (0 or 1)
1180 * @dir: #HDA_INPUT or #HDA_OUTPUT
1181 * @idx: the index value (only for input direction)
1182 * @mask: bit mask to set
1183 * @val: the bits value to set
1185 * Update the AMP values for the given channel, direction and index.
1187 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
,
1188 int ch
, int dir
, int idx
, int mask
, int val
)
1190 unsigned int cmd
= snd_hdac_regmap_encode_amp(nid
, ch
, dir
, idx
);
1192 /* enable fake mute if no h/w mute but min=mute */
1193 if ((query_amp_caps(codec
, nid
, dir
) &
1194 (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) == AC_AMPCAP_MIN_MUTE
)
1195 cmd
|= AC_AMP_FAKE_MUTE
;
1196 return snd_hdac_regmap_update_raw(&codec
->core
, cmd
, mask
, val
);
1198 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update
);
1201 * snd_hda_codec_amp_stereo - update the AMP stereo values
1202 * @codec: HD-audio codec
1203 * @nid: NID to read the AMP value
1204 * @direction: #HDA_INPUT or #HDA_OUTPUT
1205 * @idx: the index value (only for input direction)
1206 * @mask: bit mask to set
1207 * @val: the bits value to set
1209 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1210 * stereo widget with the same mask and value.
1212 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1213 int direction
, int idx
, int mask
, int val
)
1217 if (snd_BUG_ON(mask
& ~0xff))
1219 for (ch
= 0; ch
< 2; ch
++)
1220 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1224 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo
);
1227 * snd_hda_codec_amp_init - initialize the AMP value
1228 * @codec: the HDA codec
1229 * @nid: NID to read the AMP value
1230 * @ch: channel (left=0 or right=1)
1231 * @dir: #HDA_INPUT or #HDA_OUTPUT
1232 * @idx: the index value (only for input direction)
1233 * @mask: bit mask to set
1234 * @val: the bits value to set
1236 * Works like snd_hda_codec_amp_update() but it writes the value only at
1237 * the first access. If the amp was already initialized / updated beforehand,
1238 * this does nothing.
1240 int snd_hda_codec_amp_init(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1241 int dir
, int idx
, int mask
, int val
)
1245 if (!codec
->core
.regmap
)
1247 regcache_cache_only(codec
->core
.regmap
, true);
1248 orig
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1249 regcache_cache_only(codec
->core
.regmap
, false);
1252 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
, mask
, val
);
1254 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init
);
1257 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1258 * @codec: the HDA codec
1259 * @nid: NID to read the AMP value
1260 * @dir: #HDA_INPUT or #HDA_OUTPUT
1261 * @idx: the index value (only for input direction)
1262 * @mask: bit mask to set
1263 * @val: the bits value to set
1265 * Call snd_hda_codec_amp_init() for both stereo channels.
1267 int snd_hda_codec_amp_init_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1268 int dir
, int idx
, int mask
, int val
)
1272 if (snd_BUG_ON(mask
& ~0xff))
1274 for (ch
= 0; ch
< 2; ch
++)
1275 ret
|= snd_hda_codec_amp_init(codec
, nid
, ch
, dir
,
1279 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo
);
1281 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1284 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1286 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1293 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1294 * @kcontrol: referred ctl element
1295 * @uinfo: pointer to get/store the data
1297 * The control element is supposed to have the private_value field
1298 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1300 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1301 struct snd_ctl_elem_info
*uinfo
)
1303 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1304 u16 nid
= get_amp_nid(kcontrol
);
1305 u8 chs
= get_amp_channels(kcontrol
);
1306 int dir
= get_amp_direction(kcontrol
);
1307 unsigned int ofs
= get_amp_offset(kcontrol
);
1309 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1310 uinfo
->count
= chs
== 3 ? 2 : 1;
1311 uinfo
->value
.integer
.min
= 0;
1312 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
1313 if (!uinfo
->value
.integer
.max
) {
1315 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1316 nid
, kcontrol
->id
.name
);
1321 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info
);
1324 static inline unsigned int
1325 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1326 int ch
, int dir
, int idx
, unsigned int ofs
)
1329 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1330 val
&= HDA_AMP_VOLMASK
;
1339 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1340 int ch
, int dir
, int idx
, unsigned int ofs
,
1343 unsigned int maxval
;
1347 /* ofs = 0: raw max value */
1348 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
1351 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
1352 HDA_AMP_VOLMASK
, val
);
1356 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1357 * @kcontrol: ctl element
1358 * @ucontrol: pointer to get/store the data
1360 * The control element is supposed to have the private_value field
1361 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1363 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
1364 struct snd_ctl_elem_value
*ucontrol
)
1366 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1367 hda_nid_t nid
= get_amp_nid(kcontrol
);
1368 int chs
= get_amp_channels(kcontrol
);
1369 int dir
= get_amp_direction(kcontrol
);
1370 int idx
= get_amp_index(kcontrol
);
1371 unsigned int ofs
= get_amp_offset(kcontrol
);
1372 long *valp
= ucontrol
->value
.integer
.value
;
1375 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
1377 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
1380 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get
);
1383 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1384 * @kcontrol: ctl element
1385 * @ucontrol: pointer to get/store the data
1387 * The control element is supposed to have the private_value field
1388 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1390 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
1391 struct snd_ctl_elem_value
*ucontrol
)
1393 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1394 hda_nid_t nid
= get_amp_nid(kcontrol
);
1395 int chs
= get_amp_channels(kcontrol
);
1396 int dir
= get_amp_direction(kcontrol
);
1397 int idx
= get_amp_index(kcontrol
);
1398 unsigned int ofs
= get_amp_offset(kcontrol
);
1399 long *valp
= ucontrol
->value
.integer
.value
;
1403 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
1407 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
1410 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put
);
1413 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1414 * @kcontrol: ctl element
1415 * @op_flag: operation flag
1416 * @size: byte size of input TLV
1419 * The control element is supposed to have the private_value field
1420 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1422 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
1423 unsigned int size
, unsigned int __user
*_tlv
)
1425 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1426 hda_nid_t nid
= get_amp_nid(kcontrol
);
1427 int dir
= get_amp_direction(kcontrol
);
1428 unsigned int ofs
= get_amp_offset(kcontrol
);
1429 bool min_mute
= get_amp_min_mute(kcontrol
);
1430 u32 caps
, val1
, val2
;
1432 if (size
< 4 * sizeof(unsigned int))
1434 caps
= query_amp_caps(codec
, nid
, dir
);
1435 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1436 val2
= (val2
+ 1) * 25;
1437 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
1439 val1
= ((int)val1
) * ((int)val2
);
1440 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
1441 val2
|= TLV_DB_SCALE_MUTE
;
1442 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
1444 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
1446 if (put_user(val1
, _tlv
+ 2))
1448 if (put_user(val2
, _tlv
+ 3))
1452 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv
);
1455 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1456 * @codec: HD-audio codec
1457 * @nid: NID of a reference widget
1458 * @dir: #HDA_INPUT or #HDA_OUTPUT
1459 * @tlv: TLV data to be stored, at least 4 elements
1461 * Set (static) TLV data for a virtual master volume using the AMP caps
1462 * obtained from the reference NID.
1463 * The volume range is recalculated as if the max volume is 0dB.
1465 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1471 caps
= query_amp_caps(codec
, nid
, dir
);
1472 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1473 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1474 step
= (step
+ 1) * 25;
1475 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
1476 tlv
[1] = 2 * sizeof(unsigned int);
1477 tlv
[2] = -nums
* step
;
1480 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv
);
1482 /* find a mixer control element with the given name */
1483 static struct snd_kcontrol
*
1484 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
1486 struct snd_ctl_elem_id id
;
1487 memset(&id
, 0, sizeof(id
));
1488 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1491 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
1493 strcpy(id
.name
, name
);
1494 return snd_ctl_find_id(codec
->card
, &id
);
1498 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1499 * @codec: HD-audio codec
1500 * @name: ctl id name string
1502 * Get the control element with the given id string and IFACE_MIXER.
1504 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
1507 return find_mixer_ctl(codec
, name
, 0, 0);
1509 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl
);
1511 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
1515 /* 16 ctlrs should be large enough */
1516 for (i
= 0, idx
= start_idx
; i
< 16; i
++, idx
++) {
1517 if (!find_mixer_ctl(codec
, name
, 0, idx
))
1524 * snd_hda_ctl_add - Add a control element and assign to the codec
1525 * @codec: HD-audio codec
1526 * @nid: corresponding NID (optional)
1527 * @kctl: the control element to assign
1529 * Add the given control element to an array inside the codec instance.
1530 * All control elements belonging to a codec are supposed to be added
1531 * by this function so that a proper clean-up works at the free or
1532 * reconfiguration time.
1534 * If non-zero @nid is passed, the NID is assigned to the control element.
1535 * The assignment is shown in the codec proc file.
1537 * snd_hda_ctl_add() checks the control subdev id field whether
1538 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1539 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1540 * specifies if kctl->private_value is a HDA amplifier value.
1542 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
1543 struct snd_kcontrol
*kctl
)
1546 unsigned short flags
= 0;
1547 struct hda_nid_item
*item
;
1549 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
1550 flags
|= HDA_NID_ITEM_AMP
;
1552 nid
= get_amp_nid_(kctl
->private_value
);
1554 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
1555 nid
= kctl
->id
.subdevice
& 0xffff;
1556 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
1557 kctl
->id
.subdevice
= 0;
1558 err
= snd_ctl_add(codec
->card
, kctl
);
1561 item
= snd_array_new(&codec
->mixers
);
1566 item
->flags
= flags
;
1569 EXPORT_SYMBOL_GPL(snd_hda_ctl_add
);
1572 * snd_hda_add_nid - Assign a NID to a control element
1573 * @codec: HD-audio codec
1574 * @nid: corresponding NID (optional)
1575 * @kctl: the control element to assign
1576 * @index: index to kctl
1578 * Add the given control element to an array inside the codec instance.
1579 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1580 * NID:KCTL mapping - for example "Capture Source" selector.
1582 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
1583 unsigned int index
, hda_nid_t nid
)
1585 struct hda_nid_item
*item
;
1588 item
= snd_array_new(&codec
->nids
);
1592 item
->index
= index
;
1596 codec_err(codec
, "no NID for mapping control %s:%d:%d\n",
1597 kctl
->id
.name
, kctl
->id
.index
, index
);
1600 EXPORT_SYMBOL_GPL(snd_hda_add_nid
);
1603 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1604 * @codec: HD-audio codec
1606 void snd_hda_ctls_clear(struct hda_codec
*codec
)
1609 struct hda_nid_item
*items
= codec
->mixers
.list
;
1610 for (i
= 0; i
< codec
->mixers
.used
; i
++)
1611 snd_ctl_remove(codec
->card
, items
[i
].kctl
);
1612 snd_array_free(&codec
->mixers
);
1613 snd_array_free(&codec
->nids
);
1617 * snd_hda_lock_devices - pseudo device locking
1620 * toggle card->shutdown to allow/disallow the device access (as a hack)
1622 int snd_hda_lock_devices(struct hda_bus
*bus
)
1624 struct snd_card
*card
= bus
->card
;
1625 struct hda_codec
*codec
;
1627 spin_lock(&card
->files_lock
);
1631 if (!list_empty(&card
->ctl_files
))
1634 list_for_each_codec(codec
, bus
) {
1635 struct hda_pcm
*cpcm
;
1636 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
1639 if (cpcm
->pcm
->streams
[0].substream_opened
||
1640 cpcm
->pcm
->streams
[1].substream_opened
)
1644 spin_unlock(&card
->files_lock
);
1650 spin_unlock(&card
->files_lock
);
1653 EXPORT_SYMBOL_GPL(snd_hda_lock_devices
);
1656 * snd_hda_unlock_devices - pseudo device unlocking
1659 void snd_hda_unlock_devices(struct hda_bus
*bus
)
1661 struct snd_card
*card
= bus
->card
;
1663 spin_lock(&card
->files_lock
);
1665 spin_unlock(&card
->files_lock
);
1667 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices
);
1670 * snd_hda_codec_reset - Clear all objects assigned to the codec
1671 * @codec: HD-audio codec
1673 * This frees the all PCM and control elements assigned to the codec, and
1674 * clears the caches and restores the pin default configurations.
1676 * When a device is being used, it returns -EBSY. If successfully freed,
1679 int snd_hda_codec_reset(struct hda_codec
*codec
)
1681 struct hda_bus
*bus
= codec
->bus
;
1683 if (snd_hda_lock_devices(bus
) < 0)
1686 /* OK, let it free */
1687 snd_hdac_device_unregister(&codec
->core
);
1689 /* allow device access again */
1690 snd_hda_unlock_devices(bus
);
1694 typedef int (*map_slave_func_t
)(struct hda_codec
*, void *, struct snd_kcontrol
*);
1696 /* apply the function to all matching slave ctls in the mixer list */
1697 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
1698 const char *suffix
, map_slave_func_t func
, void *data
)
1700 struct hda_nid_item
*items
;
1701 const char * const *s
;
1704 items
= codec
->mixers
.list
;
1705 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
1706 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
1707 if (!sctl
|| sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
1709 for (s
= slaves
; *s
; s
++) {
1710 char tmpname
[sizeof(sctl
->id
.name
)];
1711 const char *name
= *s
;
1713 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
1717 if (!strcmp(sctl
->id
.name
, name
)) {
1718 err
= func(codec
, data
, sctl
);
1728 static int check_slave_present(struct hda_codec
*codec
,
1729 void *data
, struct snd_kcontrol
*sctl
)
1734 /* guess the value corresponding to 0dB */
1735 static int get_kctl_0dB_offset(struct hda_codec
*codec
,
1736 struct snd_kcontrol
*kctl
, int *step_to_check
)
1739 const int *tlv
= NULL
;
1742 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
1743 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1744 mm_segment_t fs
= get_fs();
1746 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
1749 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
1751 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
) {
1753 step
&= ~TLV_DB_SCALE_MUTE
;
1756 if (*step_to_check
&& *step_to_check
!= step
) {
1757 codec_err(codec
, "Mismatching dB step for vmaster slave (%d!=%d)\n",
1758 - *step_to_check
, step
);
1761 *step_to_check
= step
;
1762 val
= -tlv
[2] / step
;
1767 /* call kctl->put with the given value(s) */
1768 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
1770 struct snd_ctl_elem_value
*ucontrol
;
1771 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
1774 ucontrol
->value
.integer
.value
[0] = val
;
1775 ucontrol
->value
.integer
.value
[1] = val
;
1776 kctl
->put(kctl
, ucontrol
);
1781 /* initialize the slave volume with 0dB */
1782 static int init_slave_0dB(struct hda_codec
*codec
,
1783 void *data
, struct snd_kcontrol
*slave
)
1785 int offset
= get_kctl_0dB_offset(codec
, slave
, data
);
1787 put_kctl_with_value(slave
, offset
);
1791 /* unmute the slave */
1792 static int init_slave_unmute(struct hda_codec
*codec
,
1793 void *data
, struct snd_kcontrol
*slave
)
1795 return put_kctl_with_value(slave
, 1);
1798 static int add_slave(struct hda_codec
*codec
,
1799 void *data
, struct snd_kcontrol
*slave
)
1801 return snd_ctl_add_slave(data
, slave
);
1805 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1806 * @codec: HD-audio codec
1807 * @name: vmaster control name
1808 * @tlv: TLV data (optional)
1809 * @slaves: slave control names (optional)
1810 * @suffix: suffix string to each slave name (optional)
1811 * @init_slave_vol: initialize slaves to unmute/0dB
1812 * @ctl_ret: store the vmaster kcontrol in return
1814 * Create a virtual master control with the given name. The TLV data
1815 * must be either NULL or a valid data.
1817 * @slaves is a NULL-terminated array of strings, each of which is a
1818 * slave control name. All controls with these names are assigned to
1819 * the new virtual master control.
1821 * This function returns zero if successful or a negative error code.
1823 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
1824 unsigned int *tlv
, const char * const *slaves
,
1825 const char *suffix
, bool init_slave_vol
,
1826 struct snd_kcontrol
**ctl_ret
)
1828 struct snd_kcontrol
*kctl
;
1834 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
1836 codec_dbg(codec
, "No slave found for %s\n", name
);
1839 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
1842 err
= snd_hda_ctl_add(codec
, 0, kctl
);
1846 err
= map_slaves(codec
, slaves
, suffix
, add_slave
, kctl
);
1850 /* init with master mute & zero volume */
1851 put_kctl_with_value(kctl
, 0);
1852 if (init_slave_vol
) {
1854 map_slaves(codec
, slaves
, suffix
,
1855 tlv
? init_slave_0dB
: init_slave_unmute
, &step
);
1862 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster
);
1865 * mute-LED control using vmaster
1867 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
1868 struct snd_ctl_elem_info
*uinfo
)
1870 static const char * const texts
[] = {
1871 "On", "Off", "Follow Master"
1874 return snd_ctl_enum_info(uinfo
, 1, 3, texts
);
1877 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
1878 struct snd_ctl_elem_value
*ucontrol
)
1880 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
1881 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
1885 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
1886 struct snd_ctl_elem_value
*ucontrol
)
1888 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
1889 unsigned int old_mode
= hook
->mute_mode
;
1891 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
1892 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
1893 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
1894 if (old_mode
== hook
->mute_mode
)
1896 snd_hda_sync_vmaster_hook(hook
);
1900 static struct snd_kcontrol_new vmaster_mute_mode
= {
1901 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1902 .name
= "Mute-LED Mode",
1903 .info
= vmaster_mute_mode_info
,
1904 .get
= vmaster_mute_mode_get
,
1905 .put
= vmaster_mute_mode_put
,
1908 /* meta hook to call each driver's vmaster hook */
1909 static void vmaster_hook(void *private_data
, int enabled
)
1911 struct hda_vmaster_mute_hook
*hook
= private_data
;
1913 if (hook
->mute_mode
!= HDA_VMUTE_FOLLOW_MASTER
)
1914 enabled
= hook
->mute_mode
;
1915 hook
->hook(hook
->codec
, enabled
);
1919 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1920 * @codec: the HDA codec
1921 * @hook: the vmaster hook object
1922 * @expose_enum_ctl: flag to create an enum ctl
1924 * Add a mute-LED hook with the given vmaster switch kctl.
1925 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1926 * created and associated with the given hook.
1928 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
1929 struct hda_vmaster_mute_hook
*hook
,
1930 bool expose_enum_ctl
)
1932 struct snd_kcontrol
*kctl
;
1934 if (!hook
->hook
|| !hook
->sw_kctl
)
1936 hook
->codec
= codec
;
1937 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
1938 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, vmaster_hook
, hook
);
1939 if (!expose_enum_ctl
)
1941 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
1944 return snd_hda_ctl_add(codec
, 0, kctl
);
1946 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook
);
1949 * snd_hda_sync_vmaster_hook - Sync vmaster hook
1950 * @hook: the vmaster hook
1952 * Call the hook with the current value for synchronization.
1953 * Should be called in init callback.
1955 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
1957 if (!hook
->hook
|| !hook
->codec
)
1959 /* don't call vmaster hook in the destructor since it might have
1960 * been already destroyed
1962 if (hook
->codec
->bus
->shutdown
)
1964 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
1966 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook
);
1970 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
1971 * @kcontrol: referred ctl element
1972 * @uinfo: pointer to get/store the data
1974 * The control element is supposed to have the private_value field
1975 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1977 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
1978 struct snd_ctl_elem_info
*uinfo
)
1980 int chs
= get_amp_channels(kcontrol
);
1982 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1983 uinfo
->count
= chs
== 3 ? 2 : 1;
1984 uinfo
->value
.integer
.min
= 0;
1985 uinfo
->value
.integer
.max
= 1;
1988 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info
);
1991 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
1992 * @kcontrol: ctl element
1993 * @ucontrol: pointer to get/store the data
1995 * The control element is supposed to have the private_value field
1996 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1998 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
1999 struct snd_ctl_elem_value
*ucontrol
)
2001 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2002 hda_nid_t nid
= get_amp_nid(kcontrol
);
2003 int chs
= get_amp_channels(kcontrol
);
2004 int dir
= get_amp_direction(kcontrol
);
2005 int idx
= get_amp_index(kcontrol
);
2006 long *valp
= ucontrol
->value
.integer
.value
;
2009 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2010 HDA_AMP_MUTE
) ? 0 : 1;
2012 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2013 HDA_AMP_MUTE
) ? 0 : 1;
2016 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get
);
2019 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2020 * @kcontrol: ctl element
2021 * @ucontrol: pointer to get/store the data
2023 * The control element is supposed to have the private_value field
2024 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2026 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2027 struct snd_ctl_elem_value
*ucontrol
)
2029 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2030 hda_nid_t nid
= get_amp_nid(kcontrol
);
2031 int chs
= get_amp_channels(kcontrol
);
2032 int dir
= get_amp_direction(kcontrol
);
2033 int idx
= get_amp_index(kcontrol
);
2034 long *valp
= ucontrol
->value
.integer
.value
;
2038 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2040 *valp
? 0 : HDA_AMP_MUTE
);
2044 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2046 *valp
? 0 : HDA_AMP_MUTE
);
2047 hda_call_check_power_status(codec
, nid
);
2050 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put
);
2053 * bound volume controls
2055 * bind multiple volumes (# indices, from 0)
2058 #define AMP_VAL_IDX_SHIFT 19
2059 #define AMP_VAL_IDX_MASK (0x0f<<19)
2062 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2063 * @kcontrol: ctl element
2064 * @ucontrol: pointer to get/store the data
2066 * The control element is supposed to have the private_value field
2067 * set up via HDA_BIND_MUTE*() macros.
2069 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2070 struct snd_ctl_elem_value
*ucontrol
)
2072 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2076 mutex_lock(&codec
->control_mutex
);
2077 pval
= kcontrol
->private_value
;
2078 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2079 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2080 kcontrol
->private_value
= pval
;
2081 mutex_unlock(&codec
->control_mutex
);
2084 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get
);
2087 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2088 * @kcontrol: ctl element
2089 * @ucontrol: pointer to get/store the data
2091 * The control element is supposed to have the private_value field
2092 * set up via HDA_BIND_MUTE*() macros.
2094 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2095 struct snd_ctl_elem_value
*ucontrol
)
2097 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2099 int i
, indices
, err
= 0, change
= 0;
2101 mutex_lock(&codec
->control_mutex
);
2102 pval
= kcontrol
->private_value
;
2103 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2104 for (i
= 0; i
< indices
; i
++) {
2105 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2106 (i
<< AMP_VAL_IDX_SHIFT
);
2107 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2112 kcontrol
->private_value
= pval
;
2113 mutex_unlock(&codec
->control_mutex
);
2114 return err
< 0 ? err
: change
;
2116 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put
);
2119 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2120 * @kcontrol: referred ctl element
2121 * @uinfo: pointer to get/store the data
2123 * The control element is supposed to have the private_value field
2124 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2126 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2127 struct snd_ctl_elem_info
*uinfo
)
2129 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2130 struct hda_bind_ctls
*c
;
2133 mutex_lock(&codec
->control_mutex
);
2134 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2135 kcontrol
->private_value
= *c
->values
;
2136 err
= c
->ops
->info(kcontrol
, uinfo
);
2137 kcontrol
->private_value
= (long)c
;
2138 mutex_unlock(&codec
->control_mutex
);
2141 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info
);
2144 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2145 * @kcontrol: ctl element
2146 * @ucontrol: pointer to get/store the data
2148 * The control element is supposed to have the private_value field
2149 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2151 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2152 struct snd_ctl_elem_value
*ucontrol
)
2154 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2155 struct hda_bind_ctls
*c
;
2158 mutex_lock(&codec
->control_mutex
);
2159 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2160 kcontrol
->private_value
= *c
->values
;
2161 err
= c
->ops
->get(kcontrol
, ucontrol
);
2162 kcontrol
->private_value
= (long)c
;
2163 mutex_unlock(&codec
->control_mutex
);
2166 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get
);
2169 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2170 * @kcontrol: ctl element
2171 * @ucontrol: pointer to get/store the data
2173 * The control element is supposed to have the private_value field
2174 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2176 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2177 struct snd_ctl_elem_value
*ucontrol
)
2179 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2180 struct hda_bind_ctls
*c
;
2181 unsigned long *vals
;
2182 int err
= 0, change
= 0;
2184 mutex_lock(&codec
->control_mutex
);
2185 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2186 for (vals
= c
->values
; *vals
; vals
++) {
2187 kcontrol
->private_value
= *vals
;
2188 err
= c
->ops
->put(kcontrol
, ucontrol
);
2193 kcontrol
->private_value
= (long)c
;
2194 mutex_unlock(&codec
->control_mutex
);
2195 return err
< 0 ? err
: change
;
2197 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put
);
2200 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2201 * @kcontrol: ctl element
2202 * @op_flag: operation flag
2203 * @size: byte size of input TLV
2206 * The control element is supposed to have the private_value field
2207 * set up via HDA_BIND_VOL() macro.
2209 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2210 unsigned int size
, unsigned int __user
*tlv
)
2212 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2213 struct hda_bind_ctls
*c
;
2216 mutex_lock(&codec
->control_mutex
);
2217 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2218 kcontrol
->private_value
= *c
->values
;
2219 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
2220 kcontrol
->private_value
= (long)c
;
2221 mutex_unlock(&codec
->control_mutex
);
2224 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv
);
2226 struct hda_ctl_ops snd_hda_bind_vol
= {
2227 .info
= snd_hda_mixer_amp_volume_info
,
2228 .get
= snd_hda_mixer_amp_volume_get
,
2229 .put
= snd_hda_mixer_amp_volume_put
,
2230 .tlv
= snd_hda_mixer_amp_tlv
2232 EXPORT_SYMBOL_GPL(snd_hda_bind_vol
);
2234 struct hda_ctl_ops snd_hda_bind_sw
= {
2235 .info
= snd_hda_mixer_amp_switch_info
,
2236 .get
= snd_hda_mixer_amp_switch_get
,
2237 .put
= snd_hda_mixer_amp_switch_put
,
2238 .tlv
= snd_hda_mixer_amp_tlv
2240 EXPORT_SYMBOL_GPL(snd_hda_bind_sw
);
2243 * SPDIF out controls
2246 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2247 struct snd_ctl_elem_info
*uinfo
)
2249 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2254 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2255 struct snd_ctl_elem_value
*ucontrol
)
2257 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2258 IEC958_AES0_NONAUDIO
|
2259 IEC958_AES0_CON_EMPHASIS_5015
|
2260 IEC958_AES0_CON_NOT_COPYRIGHT
;
2261 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2262 IEC958_AES1_CON_ORIGINAL
;
2266 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2267 struct snd_ctl_elem_value
*ucontrol
)
2269 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2270 IEC958_AES0_NONAUDIO
|
2271 IEC958_AES0_PRO_EMPHASIS_5015
;
2275 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2276 struct snd_ctl_elem_value
*ucontrol
)
2278 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2279 int idx
= kcontrol
->private_value
;
2280 struct hda_spdif_out
*spdif
;
2282 mutex_lock(&codec
->spdif_mutex
);
2283 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2284 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
2285 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
2286 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
2287 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
2288 mutex_unlock(&codec
->spdif_mutex
);
2293 /* convert from SPDIF status bits to HDA SPDIF bits
2294 * bit 0 (DigEn) is always set zero (to be filled later)
2296 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2298 unsigned short val
= 0;
2300 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2301 val
|= AC_DIG1_PROFESSIONAL
;
2302 if (sbits
& IEC958_AES0_NONAUDIO
)
2303 val
|= AC_DIG1_NONAUDIO
;
2304 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2305 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2306 IEC958_AES0_PRO_EMPHASIS_5015
)
2307 val
|= AC_DIG1_EMPHASIS
;
2309 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2310 IEC958_AES0_CON_EMPHASIS_5015
)
2311 val
|= AC_DIG1_EMPHASIS
;
2312 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2313 val
|= AC_DIG1_COPYRIGHT
;
2314 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2315 val
|= AC_DIG1_LEVEL
;
2316 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2321 /* convert to SPDIF status bits from HDA SPDIF bits
2323 static unsigned int convert_to_spdif_status(unsigned short val
)
2325 unsigned int sbits
= 0;
2327 if (val
& AC_DIG1_NONAUDIO
)
2328 sbits
|= IEC958_AES0_NONAUDIO
;
2329 if (val
& AC_DIG1_PROFESSIONAL
)
2330 sbits
|= IEC958_AES0_PROFESSIONAL
;
2331 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2332 if (val
& AC_DIG1_EMPHASIS
)
2333 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
2335 if (val
& AC_DIG1_EMPHASIS
)
2336 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
2337 if (!(val
& AC_DIG1_COPYRIGHT
))
2338 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
2339 if (val
& AC_DIG1_LEVEL
)
2340 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
2341 sbits
|= val
& (0x7f << 8);
2346 /* set digital convert verbs both for the given NID and its slaves */
2347 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
2352 snd_hdac_regmap_update(&codec
->core
, nid
, AC_VERB_SET_DIGI_CONVERT_1
,
2354 d
= codec
->slave_dig_outs
;
2358 snd_hdac_regmap_update(&codec
->core
, *d
,
2359 AC_VERB_SET_DIGI_CONVERT_1
, mask
, val
);
2362 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
2365 unsigned int mask
= 0;
2366 unsigned int val
= 0;
2376 set_dig_out(codec
, nid
, mask
, val
);
2379 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
2380 struct snd_ctl_elem_value
*ucontrol
)
2382 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2383 int idx
= kcontrol
->private_value
;
2384 struct hda_spdif_out
*spdif
;
2389 mutex_lock(&codec
->spdif_mutex
);
2390 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2392 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
2393 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
2394 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
2395 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
2396 val
= convert_from_spdif_status(spdif
->status
);
2397 val
|= spdif
->ctls
& 1;
2398 change
= spdif
->ctls
!= val
;
2400 if (change
&& nid
!= (u16
)-1)
2401 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2402 mutex_unlock(&codec
->spdif_mutex
);
2406 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2408 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
2409 struct snd_ctl_elem_value
*ucontrol
)
2411 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2412 int idx
= kcontrol
->private_value
;
2413 struct hda_spdif_out
*spdif
;
2415 mutex_lock(&codec
->spdif_mutex
);
2416 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2417 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
2418 mutex_unlock(&codec
->spdif_mutex
);
2422 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
2425 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
2426 /* unmute amp switch (if any) */
2427 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
2428 (dig1
& AC_DIG1_ENABLE
))
2429 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
2433 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
2434 struct snd_ctl_elem_value
*ucontrol
)
2436 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2437 int idx
= kcontrol
->private_value
;
2438 struct hda_spdif_out
*spdif
;
2443 mutex_lock(&codec
->spdif_mutex
);
2444 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2446 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
2447 if (ucontrol
->value
.integer
.value
[0])
2448 val
|= AC_DIG1_ENABLE
;
2449 change
= spdif
->ctls
!= val
;
2451 if (change
&& nid
!= (u16
)-1)
2452 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
2453 mutex_unlock(&codec
->spdif_mutex
);
2457 static struct snd_kcontrol_new dig_mixes
[] = {
2459 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2460 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2461 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
2462 .info
= snd_hda_spdif_mask_info
,
2463 .get
= snd_hda_spdif_cmask_get
,
2466 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2467 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2468 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
2469 .info
= snd_hda_spdif_mask_info
,
2470 .get
= snd_hda_spdif_pmask_get
,
2473 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2474 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
2475 .info
= snd_hda_spdif_mask_info
,
2476 .get
= snd_hda_spdif_default_get
,
2477 .put
= snd_hda_spdif_default_put
,
2480 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2481 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
2482 .info
= snd_hda_spdif_out_switch_info
,
2483 .get
= snd_hda_spdif_out_switch_get
,
2484 .put
= snd_hda_spdif_out_switch_put
,
2490 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2491 * @codec: the HDA codec
2492 * @associated_nid: NID that new ctls associated with
2493 * @cvt_nid: converter NID
2494 * @type: HDA_PCM_TYPE_*
2495 * Creates controls related with the digital output.
2496 * Called from each patch supporting the digital out.
2498 * Returns 0 if successful, or a negative error code.
2500 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
2501 hda_nid_t associated_nid
,
2506 struct snd_kcontrol
*kctl
;
2507 struct snd_kcontrol_new
*dig_mix
;
2510 const int spdif_index
= 16;
2511 struct hda_spdif_out
*spdif
;
2512 struct hda_bus
*bus
= codec
->bus
;
2514 if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
2515 type
== HDA_PCM_TYPE_SPDIF
) {
2517 } else if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
2518 type
== HDA_PCM_TYPE_HDMI
) {
2519 /* suppose a single SPDIF device */
2520 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2521 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, 0);
2524 kctl
->id
.index
= spdif_index
;
2526 bus
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
2528 if (!bus
->primary_dig_out_type
)
2529 bus
->primary_dig_out_type
= type
;
2531 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", idx
);
2533 codec_err(codec
, "too many IEC958 outputs\n");
2536 spdif
= snd_array_new(&codec
->spdif_out
);
2539 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2540 kctl
= snd_ctl_new1(dig_mix
, codec
);
2543 kctl
->id
.index
= idx
;
2544 kctl
->private_value
= codec
->spdif_out
.used
- 1;
2545 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
2549 spdif
->nid
= cvt_nid
;
2550 snd_hdac_regmap_read(&codec
->core
, cvt_nid
,
2551 AC_VERB_GET_DIGI_CONVERT_1
, &val
);
2553 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
2556 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls
);
2559 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2560 * @codec: the HDA codec
2563 * call within spdif_mutex lock
2565 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
2569 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
2570 struct hda_spdif_out
*spdif
=
2571 snd_array_elem(&codec
->spdif_out
, i
);
2572 if (spdif
->nid
== nid
)
2577 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid
);
2580 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2581 * @codec: the HDA codec
2582 * @idx: the SPDIF ctl index
2584 * Unassign the widget from the given SPDIF control.
2586 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
2588 struct hda_spdif_out
*spdif
;
2590 mutex_lock(&codec
->spdif_mutex
);
2591 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2592 spdif
->nid
= (u16
)-1;
2593 mutex_unlock(&codec
->spdif_mutex
);
2595 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign
);
2598 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2599 * @codec: the HDA codec
2600 * @idx: the SPDIF ctl idx
2603 * Assign the widget to the SPDIF control with the given index.
2605 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
2607 struct hda_spdif_out
*spdif
;
2610 mutex_lock(&codec
->spdif_mutex
);
2611 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2612 if (spdif
->nid
!= nid
) {
2615 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2617 mutex_unlock(&codec
->spdif_mutex
);
2619 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign
);
2622 * SPDIF sharing with analog output
2624 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
2625 struct snd_ctl_elem_value
*ucontrol
)
2627 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2628 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
2632 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
2633 struct snd_ctl_elem_value
*ucontrol
)
2635 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2636 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
2640 static struct snd_kcontrol_new spdif_share_sw
= {
2641 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2642 .name
= "IEC958 Default PCM Playback Switch",
2643 .info
= snd_ctl_boolean_mono_info
,
2644 .get
= spdif_share_sw_get
,
2645 .put
= spdif_share_sw_put
,
2649 * snd_hda_create_spdif_share_sw - create Default PCM switch
2650 * @codec: the HDA codec
2651 * @mout: multi-out instance
2653 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
2654 struct hda_multi_out
*mout
)
2656 struct snd_kcontrol
*kctl
;
2658 if (!mout
->dig_out_nid
)
2661 kctl
= snd_ctl_new1(&spdif_share_sw
, mout
);
2664 /* ATTENTION: here mout is passed as private_data, instead of codec */
2665 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
, kctl
);
2667 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw
);
2673 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2675 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
2676 struct snd_ctl_elem_value
*ucontrol
)
2678 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2680 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
2684 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
2685 struct snd_ctl_elem_value
*ucontrol
)
2687 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2688 hda_nid_t nid
= kcontrol
->private_value
;
2689 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
2692 mutex_lock(&codec
->spdif_mutex
);
2693 change
= codec
->spdif_in_enable
!= val
;
2695 codec
->spdif_in_enable
= val
;
2696 snd_hdac_regmap_write(&codec
->core
, nid
,
2697 AC_VERB_SET_DIGI_CONVERT_1
, val
);
2699 mutex_unlock(&codec
->spdif_mutex
);
2703 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
2704 struct snd_ctl_elem_value
*ucontrol
)
2706 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2707 hda_nid_t nid
= kcontrol
->private_value
;
2711 snd_hdac_regmap_read(&codec
->core
, nid
,
2712 AC_VERB_GET_DIGI_CONVERT_1
, &val
);
2713 sbits
= convert_to_spdif_status(val
);
2714 ucontrol
->value
.iec958
.status
[0] = sbits
;
2715 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
2716 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
2717 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
2721 static struct snd_kcontrol_new dig_in_ctls
[] = {
2723 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2724 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
2725 .info
= snd_hda_spdif_in_switch_info
,
2726 .get
= snd_hda_spdif_in_switch_get
,
2727 .put
= snd_hda_spdif_in_switch_put
,
2730 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2731 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2732 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
2733 .info
= snd_hda_spdif_mask_info
,
2734 .get
= snd_hda_spdif_in_status_get
,
2740 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2741 * @codec: the HDA codec
2742 * @nid: audio in widget NID
2744 * Creates controls related with the SPDIF input.
2745 * Called from each patch supporting the SPDIF in.
2747 * Returns 0 if successful, or a negative error code.
2749 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
2752 struct snd_kcontrol
*kctl
;
2753 struct snd_kcontrol_new
*dig_mix
;
2756 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
2758 codec_err(codec
, "too many IEC958 inputs\n");
2761 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
2762 kctl
= snd_ctl_new1(dig_mix
, codec
);
2765 kctl
->private_value
= nid
;
2766 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
2770 codec
->spdif_in_enable
=
2771 snd_hda_codec_read(codec
, nid
, 0,
2772 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
2776 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls
);
2779 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2780 * @codec: the HDA codec
2781 * @fg: function group (not used now)
2782 * @power_state: the power state to set (AC_PWRST_*)
2784 * Set the given power state to all widgets that have the power control.
2785 * If the codec has power_filter set, it evaluates the power state and
2786 * filter out if it's unchanged as D3.
2788 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
2789 unsigned int power_state
)
2793 for_each_hda_codec_node(nid
, codec
) {
2794 unsigned int wcaps
= get_wcaps(codec
, nid
);
2795 unsigned int state
= power_state
;
2796 if (!(wcaps
& AC_WCAP_POWER
))
2798 if (codec
->power_filter
) {
2799 state
= codec
->power_filter(codec
, nid
, power_state
);
2800 if (state
!= power_state
&& power_state
== AC_PWRST_D3
)
2803 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
2807 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all
);
2810 * wait until the state is reached, returns the current state
2812 static unsigned int hda_sync_power_state(struct hda_codec
*codec
,
2814 unsigned int power_state
)
2816 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
2817 unsigned int state
, actual_state
;
2820 state
= snd_hda_codec_read(codec
, fg
, 0,
2821 AC_VERB_GET_POWER_STATE
, 0);
2822 if (state
& AC_PWRST_ERROR
)
2824 actual_state
= (state
>> 4) & 0x0f;
2825 if (actual_state
== power_state
)
2827 if (time_after_eq(jiffies
, end_time
))
2829 /* wait until the codec reachs to the target state */
2836 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2837 * @codec: the HDA codec
2839 * @power_state: power state to evalue
2841 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2842 * This can be used a codec power_filter callback.
2844 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec
*codec
,
2846 unsigned int power_state
)
2848 if (nid
== codec
->core
.afg
|| nid
== codec
->core
.mfg
)
2850 if (power_state
== AC_PWRST_D3
&&
2851 get_wcaps_type(get_wcaps(codec
, nid
)) == AC_WID_PIN
&&
2852 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
2853 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
2854 AC_VERB_GET_EAPD_BTLENABLE
, 0);
2860 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter
);
2863 * set power state of the codec, and return the power state
2865 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
2866 unsigned int power_state
)
2868 hda_nid_t fg
= codec
->core
.afg
? codec
->core
.afg
: codec
->core
.mfg
;
2873 /* this delay seems necessary to avoid click noise at power-down */
2874 if (power_state
== AC_PWRST_D3
) {
2875 if (codec
->depop_delay
< 0)
2876 msleep(codec_has_epss(codec
) ? 10 : 100);
2877 else if (codec
->depop_delay
> 0)
2878 msleep(codec
->depop_delay
);
2879 flags
= HDA_RW_NO_RESPONSE_FALLBACK
;
2882 /* repeat power states setting at most 10 times*/
2883 for (count
= 0; count
< 10; count
++) {
2884 if (codec
->patch_ops
.set_power_state
)
2885 codec
->patch_ops
.set_power_state(codec
, fg
,
2888 state
= power_state
;
2889 if (codec
->power_filter
)
2890 state
= codec
->power_filter(codec
, fg
, state
);
2891 if (state
== power_state
|| power_state
!= AC_PWRST_D3
)
2892 snd_hda_codec_read(codec
, fg
, flags
,
2893 AC_VERB_SET_POWER_STATE
,
2895 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
);
2897 state
= hda_sync_power_state(codec
, fg
, power_state
);
2898 if (!(state
& AC_PWRST_ERROR
))
2905 /* sync power states of all widgets;
2906 * this is called at the end of codec parsing
2908 static void sync_power_up_states(struct hda_codec
*codec
)
2912 /* don't care if no filter is used */
2913 if (!codec
->power_filter
)
2916 for_each_hda_codec_node(nid
, codec
) {
2917 unsigned int wcaps
= get_wcaps(codec
, nid
);
2918 unsigned int target
;
2919 if (!(wcaps
& AC_WCAP_POWER
))
2921 target
= codec
->power_filter(codec
, nid
, AC_PWRST_D0
);
2922 if (target
== AC_PWRST_D0
)
2924 if (!snd_hda_check_power_state(codec
, nid
, target
))
2925 snd_hda_codec_write(codec
, nid
, 0,
2926 AC_VERB_SET_POWER_STATE
, target
);
2930 #ifdef CONFIG_SND_HDA_RECONFIG
2931 /* execute additional init verbs */
2932 static void hda_exec_init_verbs(struct hda_codec
*codec
)
2934 if (codec
->init_verbs
.list
)
2935 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
2938 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
2942 /* update the power on/off account with the current jiffies */
2943 static void update_power_acct(struct hda_codec
*codec
, bool on
)
2945 unsigned long delta
= jiffies
- codec
->power_jiffies
;
2948 codec
->power_on_acct
+= delta
;
2950 codec
->power_off_acct
+= delta
;
2951 codec
->power_jiffies
+= delta
;
2954 void snd_hda_update_power_acct(struct hda_codec
*codec
)
2956 update_power_acct(codec
, hda_codec_is_power_on(codec
));
2960 * call suspend and power-down; used both from PM and power-save
2961 * this function returns the power state in the end
2963 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
)
2967 atomic_inc(&codec
->core
.in_pm
);
2969 if (codec
->patch_ops
.suspend
)
2970 codec
->patch_ops
.suspend(codec
);
2971 hda_cleanup_all_streams(codec
);
2972 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
2973 update_power_acct(codec
, true);
2974 atomic_dec(&codec
->core
.in_pm
);
2979 * kick up codec; used both from PM and power-save
2981 static void hda_call_codec_resume(struct hda_codec
*codec
)
2983 atomic_inc(&codec
->core
.in_pm
);
2985 if (codec
->core
.regmap
)
2986 regcache_mark_dirty(codec
->core
.regmap
);
2988 codec
->power_jiffies
= jiffies
;
2990 hda_set_power_state(codec
, AC_PWRST_D0
);
2991 restore_shutup_pins(codec
);
2992 hda_exec_init_verbs(codec
);
2993 snd_hda_jack_set_dirty_all(codec
);
2994 if (codec
->patch_ops
.resume
)
2995 codec
->patch_ops
.resume(codec
);
2997 if (codec
->patch_ops
.init
)
2998 codec
->patch_ops
.init(codec
);
2999 if (codec
->core
.regmap
)
3000 regcache_sync(codec
->core
.regmap
);
3003 if (codec
->jackpoll_interval
)
3004 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3006 snd_hda_jack_report_sync(codec
);
3007 atomic_dec(&codec
->core
.in_pm
);
3010 static int hda_codec_runtime_suspend(struct device
*dev
)
3012 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
3013 struct hda_pcm
*pcm
;
3016 cancel_delayed_work_sync(&codec
->jackpoll_work
);
3017 list_for_each_entry(pcm
, &codec
->pcm_list_head
, list
)
3018 snd_pcm_suspend_all(pcm
->pcm
);
3019 state
= hda_call_codec_suspend(codec
);
3020 if (codec_has_clkstop(codec
) && codec_has_epss(codec
) &&
3021 (state
& AC_PWRST_CLK_STOP_OK
))
3022 snd_hdac_codec_link_down(&codec
->core
);
3023 snd_hdac_link_power(&codec
->core
, false);
3027 static int hda_codec_runtime_resume(struct device
*dev
)
3029 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
3031 snd_hdac_link_power(&codec
->core
, true);
3032 snd_hdac_codec_link_up(&codec
->core
);
3033 hda_call_codec_resume(codec
);
3034 pm_runtime_mark_last_busy(dev
);
3037 #endif /* CONFIG_PM */
3039 /* referred in hda_bind.c */
3040 const struct dev_pm_ops hda_codec_driver_pm
= {
3041 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
3042 pm_runtime_force_resume
)
3043 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend
, hda_codec_runtime_resume
,
3048 * add standard channel maps if not specified
3050 static int add_std_chmaps(struct hda_codec
*codec
)
3052 struct hda_pcm
*pcm
;
3055 list_for_each_entry(pcm
, &codec
->pcm_list_head
, list
) {
3056 for (str
= 0; str
< 2; str
++) {
3057 struct hda_pcm_stream
*hinfo
= &pcm
->stream
[str
];
3058 struct snd_pcm_chmap
*chmap
;
3059 const struct snd_pcm_chmap_elem
*elem
;
3061 if (!pcm
->pcm
|| pcm
->own_chmap
|| !hinfo
->substreams
)
3063 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
3064 err
= snd_pcm_add_chmap_ctls(pcm
->pcm
, str
, elem
,
3065 hinfo
->channels_max
,
3069 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
3075 /* default channel maps for 2.1 speakers;
3076 * since HD-audio supports only stereo, odd number channels are omitted
3078 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
3080 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
3082 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
3083 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
3086 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
3088 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
3091 hda_exec_init_verbs(codec
);
3092 /* continue to initialize... */
3093 if (codec
->patch_ops
.init
)
3094 err
= codec
->patch_ops
.init(codec
);
3095 if (!err
&& codec
->patch_ops
.build_controls
)
3096 err
= codec
->patch_ops
.build_controls(codec
);
3100 /* we create chmaps here instead of build_pcms */
3101 err
= add_std_chmaps(codec
);
3105 if (codec
->jackpoll_interval
)
3106 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3108 snd_hda_jack_report_sync(codec
); /* call at the last init point */
3109 sync_power_up_states(codec
);
3116 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
3117 struct hda_codec
*codec
,
3118 struct snd_pcm_substream
*substream
)
3123 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
3124 struct hda_codec
*codec
,
3125 unsigned int stream_tag
,
3126 unsigned int format
,
3127 struct snd_pcm_substream
*substream
)
3129 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3133 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
3134 struct hda_codec
*codec
,
3135 struct snd_pcm_substream
*substream
)
3137 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3141 static int set_pcm_default_values(struct hda_codec
*codec
,
3142 struct hda_pcm_stream
*info
)
3146 /* query support PCM information from the given NID */
3147 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
3148 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
3149 info
->rates
? NULL
: &info
->rates
,
3150 info
->formats
? NULL
: &info
->formats
,
3151 info
->maxbps
? NULL
: &info
->maxbps
);
3155 if (info
->ops
.open
== NULL
)
3156 info
->ops
.open
= hda_pcm_default_open_close
;
3157 if (info
->ops
.close
== NULL
)
3158 info
->ops
.close
= hda_pcm_default_open_close
;
3159 if (info
->ops
.prepare
== NULL
) {
3160 if (snd_BUG_ON(!info
->nid
))
3162 info
->ops
.prepare
= hda_pcm_default_prepare
;
3164 if (info
->ops
.cleanup
== NULL
) {
3165 if (snd_BUG_ON(!info
->nid
))
3167 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
3173 * codec prepare/cleanup entries
3176 * snd_hda_codec_prepare - Prepare a stream
3177 * @codec: the HDA codec
3178 * @hinfo: PCM information
3179 * @stream: stream tag to assign
3180 * @format: format id to assign
3181 * @substream: PCM substream to assign
3183 * Calls the prepare callback set by the codec with the given arguments.
3184 * Clean up the inactive streams when successful.
3186 int snd_hda_codec_prepare(struct hda_codec
*codec
,
3187 struct hda_pcm_stream
*hinfo
,
3188 unsigned int stream
,
3189 unsigned int format
,
3190 struct snd_pcm_substream
*substream
)
3193 mutex_lock(&codec
->bus
->prepare_mutex
);
3194 if (hinfo
->ops
.prepare
)
3195 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
,
3200 purify_inactive_streams(codec
);
3201 mutex_unlock(&codec
->bus
->prepare_mutex
);
3204 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare
);
3207 * snd_hda_codec_cleanup - Prepare a stream
3208 * @codec: the HDA codec
3209 * @hinfo: PCM information
3210 * @substream: PCM substream
3212 * Calls the cleanup callback set by the codec with the given arguments.
3214 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
3215 struct hda_pcm_stream
*hinfo
,
3216 struct snd_pcm_substream
*substream
)
3218 mutex_lock(&codec
->bus
->prepare_mutex
);
3219 if (hinfo
->ops
.cleanup
)
3220 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
3221 mutex_unlock(&codec
->bus
->prepare_mutex
);
3223 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup
);
3226 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
3227 "Audio", "SPDIF", "HDMI", "Modem"
3231 * get the empty PCM device number to assign
3233 static int get_empty_pcm_device(struct hda_bus
*bus
, unsigned int type
)
3235 /* audio device indices; not linear to keep compatibility */
3236 /* assigned to static slots up to dev#10; if more needed, assign
3237 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3239 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
3240 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
3241 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
3242 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
3243 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
3247 if (type
>= HDA_PCM_NTYPES
) {
3248 dev_err(bus
->card
->dev
, "Invalid PCM type %d\n", type
);
3252 for (i
= 0; audio_idx
[type
][i
] >= 0; i
++) {
3253 #ifndef CONFIG_SND_DYNAMIC_MINORS
3254 if (audio_idx
[type
][i
] >= 8)
3257 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
3258 return audio_idx
[type
][i
];
3261 #ifdef CONFIG_SND_DYNAMIC_MINORS
3262 /* non-fixed slots starting from 10 */
3263 for (i
= 10; i
< 32; i
++) {
3264 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
3269 dev_warn(bus
->card
->dev
, "Too many %s devices\n",
3270 snd_hda_pcm_type_name
[type
]);
3271 #ifndef CONFIG_SND_DYNAMIC_MINORS
3272 dev_warn(bus
->card
->dev
,
3273 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3278 /* call build_pcms ops of the given codec and set up the default parameters */
3279 int snd_hda_codec_parse_pcms(struct hda_codec
*codec
)
3281 struct hda_pcm
*cpcm
;
3284 if (!list_empty(&codec
->pcm_list_head
))
3285 return 0; /* already parsed */
3287 if (!codec
->patch_ops
.build_pcms
)
3290 err
= codec
->patch_ops
.build_pcms(codec
);
3292 codec_err(codec
, "cannot build PCMs for #%d (error %d)\n",
3293 codec
->core
.addr
, err
);
3297 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
3300 for (stream
= 0; stream
< 2; stream
++) {
3301 struct hda_pcm_stream
*info
= &cpcm
->stream
[stream
];
3303 if (!info
->substreams
)
3305 err
= set_pcm_default_values(codec
, info
);
3308 "fail to setup default for PCM %s\n",
3318 /* assign all PCMs of the given codec */
3319 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
3321 struct hda_bus
*bus
= codec
->bus
;
3322 struct hda_pcm
*cpcm
;
3325 err
= snd_hda_codec_parse_pcms(codec
);
3329 /* attach a new PCM streams */
3330 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
3332 continue; /* already attached */
3333 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
3334 continue; /* no substreams assigned */
3336 dev
= get_empty_pcm_device(bus
, cpcm
->pcm_type
);
3338 continue; /* no fatal error */
3340 err
= snd_hda_attach_pcm_stream(bus
, codec
, cpcm
);
3343 "cannot attach PCM stream %d for codec #%d\n",
3344 dev
, codec
->core
.addr
);
3345 continue; /* no fatal error */
3353 * snd_hda_add_new_ctls - create controls from the array
3354 * @codec: the HDA codec
3355 * @knew: the array of struct snd_kcontrol_new
3357 * This helper function creates and add new controls in the given array.
3358 * The array must be terminated with an empty entry as terminator.
3360 * Returns 0 if successful, or a negative error code.
3362 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
3363 const struct snd_kcontrol_new
*knew
)
3367 for (; knew
->name
; knew
++) {
3368 struct snd_kcontrol
*kctl
;
3369 int addr
= 0, idx
= 0;
3370 if (knew
->iface
== -1) /* skip this codec private value */
3373 kctl
= snd_ctl_new1(knew
, codec
);
3377 kctl
->id
.device
= addr
;
3379 kctl
->id
.index
= idx
;
3380 err
= snd_hda_ctl_add(codec
, 0, kctl
);
3383 /* try first with another device index corresponding to
3384 * the codec addr; if it still fails (or it's the
3385 * primary codec), then try another control index
3387 if (!addr
&& codec
->core
.addr
)
3388 addr
= codec
->core
.addr
;
3389 else if (!idx
&& !knew
->index
) {
3390 idx
= find_empty_mixer_ctl_idx(codec
,
3400 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls
);
3403 static void codec_set_power_save(struct hda_codec
*codec
, int delay
)
3405 struct device
*dev
= hda_codec_dev(codec
);
3407 if (delay
== 0 && codec
->auto_runtime_pm
)
3411 pm_runtime_set_autosuspend_delay(dev
, delay
);
3412 pm_runtime_use_autosuspend(dev
);
3413 pm_runtime_allow(dev
);
3414 if (!pm_runtime_suspended(dev
))
3415 pm_runtime_mark_last_busy(dev
);
3417 pm_runtime_dont_use_autosuspend(dev
);
3418 pm_runtime_forbid(dev
);
3423 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3424 * @bus: HD-audio bus
3425 * @delay: autosuspend delay in msec, 0 = off
3427 * Synchronize the runtime PM autosuspend state from the power_save option.
3429 void snd_hda_set_power_save(struct hda_bus
*bus
, int delay
)
3431 struct hda_codec
*c
;
3433 list_for_each_codec(c
, bus
)
3434 codec_set_power_save(c
, delay
);
3436 EXPORT_SYMBOL_GPL(snd_hda_set_power_save
);
3439 * snd_hda_check_amp_list_power - Check the amp list and update the power
3440 * @codec: HD-audio codec
3441 * @check: the object containing an AMP list and the status
3442 * @nid: NID to check / update
3444 * Check whether the given NID is in the amp list. If it's in the list,
3445 * check the current AMP status, and update the the power-status according
3446 * to the mute status.
3448 * This function is supposed to be set or called from the check_power_status
3451 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
3452 struct hda_loopback_check
*check
,
3455 const struct hda_amp_list
*p
;
3458 if (!check
->amplist
)
3460 for (p
= check
->amplist
; p
->nid
; p
++) {
3465 return 0; /* nothing changed */
3467 for (p
= check
->amplist
; p
->nid
; p
++) {
3468 for (ch
= 0; ch
< 2; ch
++) {
3469 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
3471 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
3472 if (!check
->power_on
) {
3473 check
->power_on
= 1;
3474 snd_hda_power_up_pm(codec
);
3480 if (check
->power_on
) {
3481 check
->power_on
= 0;
3482 snd_hda_power_down_pm(codec
);
3486 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power
);
3494 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3495 * @imux: imux helper object
3496 * @uinfo: pointer to get/store the data
3498 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
3499 struct snd_ctl_elem_info
*uinfo
)
3503 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
3505 uinfo
->value
.enumerated
.items
= imux
->num_items
;
3506 if (!imux
->num_items
)
3508 index
= uinfo
->value
.enumerated
.item
;
3509 if (index
>= imux
->num_items
)
3510 index
= imux
->num_items
- 1;
3511 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
3514 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info
);
3517 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3518 * @codec: the HDA codec
3519 * @imux: imux helper object
3520 * @ucontrol: pointer to get/store the data
3521 * @nid: input mux NID
3522 * @cur_val: pointer to get/store the current imux value
3524 int snd_hda_input_mux_put(struct hda_codec
*codec
,
3525 const struct hda_input_mux
*imux
,
3526 struct snd_ctl_elem_value
*ucontrol
,
3528 unsigned int *cur_val
)
3532 if (!imux
->num_items
)
3534 idx
= ucontrol
->value
.enumerated
.item
[0];
3535 if (idx
>= imux
->num_items
)
3536 idx
= imux
->num_items
- 1;
3537 if (*cur_val
== idx
)
3539 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
3540 imux
->items
[idx
].index
);
3544 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put
);
3548 * snd_hda_enum_helper_info - Helper for simple enum ctls
3549 * @kcontrol: ctl element
3550 * @uinfo: pointer to get/store the data
3551 * @num_items: number of enum items
3552 * @texts: enum item string array
3554 * process kcontrol info callback of a simple string enum array
3555 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3557 int snd_hda_enum_helper_info(struct snd_kcontrol
*kcontrol
,
3558 struct snd_ctl_elem_info
*uinfo
,
3559 int num_items
, const char * const *texts
)
3561 static const char * const texts_default
[] = {
3562 "Disabled", "Enabled"
3565 if (!texts
|| !num_items
) {
3567 texts
= texts_default
;
3570 return snd_ctl_enum_info(uinfo
, 1, num_items
, texts
);
3572 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info
);
3575 * Multi-channel / digital-out PCM helper functions
3578 /* setup SPDIF output stream */
3579 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
3580 unsigned int stream_tag
, unsigned int format
)
3582 struct hda_spdif_out
*spdif
;
3583 unsigned int curr_fmt
;
3586 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
3587 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
3588 AC_VERB_GET_STREAM_FORMAT
, 0);
3589 reset
= codec
->spdif_status_reset
&&
3590 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
3593 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3596 set_dig_out_convert(codec
, nid
,
3597 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
3599 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
3600 if (codec
->slave_dig_outs
) {
3602 for (d
= codec
->slave_dig_outs
; *d
; d
++)
3603 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
3606 /* turn on again (if needed) */
3608 set_dig_out_convert(codec
, nid
,
3609 spdif
->ctls
& 0xff, -1);
3612 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
3614 snd_hda_codec_cleanup_stream(codec
, nid
);
3615 if (codec
->slave_dig_outs
) {
3617 for (d
= codec
->slave_dig_outs
; *d
; d
++)
3618 snd_hda_codec_cleanup_stream(codec
, *d
);
3623 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3624 * @codec: the HDA codec
3625 * @mout: hda_multi_out object
3627 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
3628 struct hda_multi_out
*mout
)
3630 mutex_lock(&codec
->spdif_mutex
);
3631 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
3632 /* already opened as analog dup; reset it once */
3633 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3634 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
3635 mutex_unlock(&codec
->spdif_mutex
);
3638 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open
);
3641 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3642 * @codec: the HDA codec
3643 * @mout: hda_multi_out object
3644 * @stream_tag: stream tag to assign
3645 * @format: format id to assign
3646 * @substream: PCM substream to assign
3648 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
3649 struct hda_multi_out
*mout
,
3650 unsigned int stream_tag
,
3651 unsigned int format
,
3652 struct snd_pcm_substream
*substream
)
3654 mutex_lock(&codec
->spdif_mutex
);
3655 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
3656 mutex_unlock(&codec
->spdif_mutex
);
3659 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare
);
3662 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3663 * @codec: the HDA codec
3664 * @mout: hda_multi_out object
3666 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
3667 struct hda_multi_out
*mout
)
3669 mutex_lock(&codec
->spdif_mutex
);
3670 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3671 mutex_unlock(&codec
->spdif_mutex
);
3674 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup
);
3677 * snd_hda_multi_out_dig_close - release the digital out stream
3678 * @codec: the HDA codec
3679 * @mout: hda_multi_out object
3681 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
3682 struct hda_multi_out
*mout
)
3684 mutex_lock(&codec
->spdif_mutex
);
3685 mout
->dig_out_used
= 0;
3686 mutex_unlock(&codec
->spdif_mutex
);
3689 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close
);
3692 * snd_hda_multi_out_analog_open - open analog outputs
3693 * @codec: the HDA codec
3694 * @mout: hda_multi_out object
3695 * @substream: PCM substream to assign
3696 * @hinfo: PCM information to assign
3698 * Open analog outputs and set up the hw-constraints.
3699 * If the digital outputs can be opened as slave, open the digital
3702 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
3703 struct hda_multi_out
*mout
,
3704 struct snd_pcm_substream
*substream
,
3705 struct hda_pcm_stream
*hinfo
)
3707 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3708 runtime
->hw
.channels_max
= mout
->max_channels
;
3709 if (mout
->dig_out_nid
) {
3710 if (!mout
->analog_rates
) {
3711 mout
->analog_rates
= hinfo
->rates
;
3712 mout
->analog_formats
= hinfo
->formats
;
3713 mout
->analog_maxbps
= hinfo
->maxbps
;
3715 runtime
->hw
.rates
= mout
->analog_rates
;
3716 runtime
->hw
.formats
= mout
->analog_formats
;
3717 hinfo
->maxbps
= mout
->analog_maxbps
;
3719 if (!mout
->spdif_rates
) {
3720 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
3722 &mout
->spdif_formats
,
3723 &mout
->spdif_maxbps
);
3725 mutex_lock(&codec
->spdif_mutex
);
3726 if (mout
->share_spdif
) {
3727 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
3728 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
3729 runtime
->hw
.rates
&= mout
->spdif_rates
;
3730 runtime
->hw
.formats
&= mout
->spdif_formats
;
3731 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
3732 hinfo
->maxbps
= mout
->spdif_maxbps
;
3734 mout
->share_spdif
= 0;
3735 /* FIXME: need notify? */
3738 mutex_unlock(&codec
->spdif_mutex
);
3740 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
3741 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
3743 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open
);
3746 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3747 * @codec: the HDA codec
3748 * @mout: hda_multi_out object
3749 * @stream_tag: stream tag to assign
3750 * @format: format id to assign
3751 * @substream: PCM substream to assign
3753 * Set up the i/o for analog out.
3754 * When the digital out is available, copy the front out to digital out, too.
3756 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
3757 struct hda_multi_out
*mout
,
3758 unsigned int stream_tag
,
3759 unsigned int format
,
3760 struct snd_pcm_substream
*substream
)
3762 const hda_nid_t
*nids
= mout
->dac_nids
;
3763 int chs
= substream
->runtime
->channels
;
3764 struct hda_spdif_out
*spdif
;
3767 mutex_lock(&codec
->spdif_mutex
);
3768 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
3769 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
3770 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
3772 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
3774 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
3775 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
3776 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
3777 stream_tag
, format
);
3779 mout
->dig_out_used
= 0;
3780 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3783 mutex_unlock(&codec
->spdif_mutex
);
3786 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
3788 if (!mout
->no_share_stream
&&
3789 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
3790 /* headphone out will just decode front left/right (stereo) */
3791 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
3793 /* extra outputs copied from front */
3794 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
3795 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
3796 snd_hda_codec_setup_stream(codec
,
3797 mout
->hp_out_nid
[i
],
3798 stream_tag
, 0, format
);
3801 for (i
= 1; i
< mout
->num_dacs
; i
++) {
3802 if (chs
>= (i
+ 1) * 2) /* independent out */
3803 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
3805 else if (!mout
->no_share_stream
) /* copy front */
3806 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
3810 /* extra surrounds */
3811 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++) {
3813 if (!mout
->extra_out_nid
[i
])
3815 if (chs
>= (i
+ 1) * 2)
3817 else if (!mout
->no_share_stream
)
3819 snd_hda_codec_setup_stream(codec
, mout
->extra_out_nid
[i
],
3820 stream_tag
, ch
, format
);
3825 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare
);
3828 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3829 * @codec: the HDA codec
3830 * @mout: hda_multi_out object
3832 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
3833 struct hda_multi_out
*mout
)
3835 const hda_nid_t
*nids
= mout
->dac_nids
;
3838 for (i
= 0; i
< mout
->num_dacs
; i
++)
3839 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
3841 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
3842 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
3843 if (mout
->hp_out_nid
[i
])
3844 snd_hda_codec_cleanup_stream(codec
,
3845 mout
->hp_out_nid
[i
]);
3846 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
3847 if (mout
->extra_out_nid
[i
])
3848 snd_hda_codec_cleanup_stream(codec
,
3849 mout
->extra_out_nid
[i
]);
3850 mutex_lock(&codec
->spdif_mutex
);
3851 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
3852 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3853 mout
->dig_out_used
= 0;
3855 mutex_unlock(&codec
->spdif_mutex
);
3858 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup
);
3861 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3862 * @codec: the HDA codec
3863 * @pin: referred pin NID
3865 * Guess the suitable VREF pin bits to be set as the pin-control value.
3866 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3868 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
3870 unsigned int pincap
;
3871 unsigned int oldval
;
3872 oldval
= snd_hda_codec_read(codec
, pin
, 0,
3873 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
3874 pincap
= snd_hda_query_pin_caps(codec
, pin
);
3875 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
3876 /* Exception: if the default pin setup is vref50, we give it priority */
3877 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
3878 return AC_PINCTL_VREF_80
;
3879 else if (pincap
& AC_PINCAP_VREF_50
)
3880 return AC_PINCTL_VREF_50
;
3881 else if (pincap
& AC_PINCAP_VREF_100
)
3882 return AC_PINCTL_VREF_100
;
3883 else if (pincap
& AC_PINCAP_VREF_GRD
)
3884 return AC_PINCTL_VREF_GRD
;
3885 return AC_PINCTL_VREF_HIZ
;
3887 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref
);
3890 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3891 * @codec: the HDA codec
3892 * @pin: referred pin NID
3893 * @val: pin ctl value to audit
3895 unsigned int snd_hda_correct_pin_ctl(struct hda_codec
*codec
,
3896 hda_nid_t pin
, unsigned int val
)
3898 static unsigned int cap_lists
[][2] = {
3899 { AC_PINCTL_VREF_100
, AC_PINCAP_VREF_100
},
3900 { AC_PINCTL_VREF_80
, AC_PINCAP_VREF_80
},
3901 { AC_PINCTL_VREF_50
, AC_PINCAP_VREF_50
},
3902 { AC_PINCTL_VREF_GRD
, AC_PINCAP_VREF_GRD
},
3908 cap
= snd_hda_query_pin_caps(codec
, pin
);
3910 return val
; /* don't know what to do... */
3912 if (val
& AC_PINCTL_OUT_EN
) {
3913 if (!(cap
& AC_PINCAP_OUT
))
3914 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
3915 else if ((val
& AC_PINCTL_HP_EN
) && !(cap
& AC_PINCAP_HP_DRV
))
3916 val
&= ~AC_PINCTL_HP_EN
;
3919 if (val
& AC_PINCTL_IN_EN
) {
3920 if (!(cap
& AC_PINCAP_IN
))
3921 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
3923 unsigned int vcap
, vref
;
3925 vcap
= (cap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
3926 vref
= val
& AC_PINCTL_VREFEN
;
3927 for (i
= 0; i
< ARRAY_SIZE(cap_lists
); i
++) {
3928 if (vref
== cap_lists
[i
][0] &&
3929 !(vcap
& cap_lists
[i
][1])) {
3930 if (i
== ARRAY_SIZE(cap_lists
) - 1)
3931 vref
= AC_PINCTL_VREF_HIZ
;
3933 vref
= cap_lists
[i
+ 1][0];
3936 val
&= ~AC_PINCTL_VREFEN
;
3943 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl
);
3946 * _snd_hda_pin_ctl - Helper to set pin ctl value
3947 * @codec: the HDA codec
3948 * @pin: referred pin NID
3949 * @val: pin control value to set
3950 * @cached: access over codec pinctl cache or direct write
3952 * This function is a helper to set a pin ctl value more safely.
3953 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
3954 * value in pin target array via snd_hda_codec_set_pin_target(), then
3955 * actually writes the value via either snd_hda_codec_update_cache() or
3956 * snd_hda_codec_write() depending on @cached flag.
3958 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
3959 unsigned int val
, bool cached
)
3961 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
3962 snd_hda_codec_set_pin_target(codec
, pin
, val
);
3964 return snd_hda_codec_update_cache(codec
, pin
, 0,
3965 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
3967 return snd_hda_codec_write(codec
, pin
, 0,
3968 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
3970 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl
);
3973 * snd_hda_add_imux_item - Add an item to input_mux
3974 * @codec: the HDA codec
3975 * @imux: imux helper object
3976 * @label: the name of imux item to assign
3977 * @index: index number of imux item to assign
3978 * @type_idx: pointer to store the resultant label index
3980 * When the same label is used already in the existing items, the number
3981 * suffix is appended to the label. This label index number is stored
3982 * to type_idx when non-NULL pointer is given.
3984 int snd_hda_add_imux_item(struct hda_codec
*codec
,
3985 struct hda_input_mux
*imux
, const char *label
,
3986 int index
, int *type_idx
)
3988 int i
, label_idx
= 0;
3989 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
3990 codec_err(codec
, "hda_codec: Too many imux items!\n");
3993 for (i
= 0; i
< imux
->num_items
; i
++) {
3994 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
3998 *type_idx
= label_idx
;
4000 snprintf(imux
->items
[imux
->num_items
].label
,
4001 sizeof(imux
->items
[imux
->num_items
].label
),
4002 "%s %d", label
, label_idx
);
4004 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
4005 sizeof(imux
->items
[imux
->num_items
].label
));
4006 imux
->items
[imux
->num_items
].index
= index
;
4010 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item
);
4013 * snd_hda_bus_reset_codecs - Reset the bus
4014 * @bus: HD-audio bus
4016 void snd_hda_bus_reset_codecs(struct hda_bus
*bus
)
4018 struct hda_codec
*codec
;
4020 list_for_each_codec(codec
, bus
) {
4021 /* FIXME: maybe a better way needed for forced reset */
4022 cancel_delayed_work_sync(&codec
->jackpoll_work
);
4024 if (hda_codec_is_power_on(codec
)) {
4025 hda_call_codec_suspend(codec
);
4026 hda_call_codec_resume(codec
);
4033 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4034 * @pcm: PCM caps bits
4035 * @buf: the string buffer to write
4036 * @buflen: the max buffer length
4038 * used by hda_proc.c and hda_eld.c
4040 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
4042 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
4045 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
4046 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
4047 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
4049 buf
[j
] = '\0'; /* necessary when j == 0 */
4051 EXPORT_SYMBOL_GPL(snd_print_pcm_bits
);
4053 MODULE_DESCRIPTION("HDA codec core");
4054 MODULE_LICENSE("GPL");