1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Universal Interface for Intel High Definition Audio Codec
5 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 #include <linux/init.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/mutex.h>
12 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <sound/core.h>
16 #include <sound/hda_codec.h>
17 #include <sound/asoundef.h>
18 #include <sound/tlv.h>
19 #include <sound/initval.h>
20 #include <sound/jack.h>
21 #include "hda_local.h"
24 #include <sound/hda_hwdep.h>
25 #include <sound/hda_component.h>
27 #define codec_in_pm(codec) snd_hdac_is_in_pm(&codec->core)
28 #define hda_codec_is_power_on(codec) snd_hdac_is_power_on(&codec->core)
29 #define codec_has_epss(codec) \
30 ((codec)->core.power_caps & AC_PWRST_EPSS)
31 #define codec_has_clkstop(codec) \
32 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
35 * Send and receive a verb - passed to exec_verb override for hdac_device
37 static int codec_exec_verb(struct hdac_device
*dev
, unsigned int cmd
,
38 unsigned int flags
, unsigned int *res
)
40 struct hda_codec
*codec
= container_of(dev
, struct hda_codec
, core
);
41 struct hda_bus
*bus
= codec
->bus
;
48 snd_hda_power_up_pm(codec
);
49 mutex_lock(&bus
->core
.cmd_mutex
);
50 if (flags
& HDA_RW_NO_RESPONSE_FALLBACK
)
51 bus
->no_response_fallback
= 1;
52 err
= snd_hdac_bus_exec_verb_unlocked(&bus
->core
, codec
->core
.addr
,
54 bus
->no_response_fallback
= 0;
55 mutex_unlock(&bus
->core
.cmd_mutex
);
56 snd_hda_power_down_pm(codec
);
57 if (!codec_in_pm(codec
) && res
&& err
== -EAGAIN
) {
58 if (bus
->response_reset
) {
60 "resetting BUS due to fatal communication error\n");
61 snd_hda_bus_reset(bus
);
65 /* clear reset-flag when the communication gets recovered */
66 if (!err
|| codec_in_pm(codec
))
67 bus
->response_reset
= 0;
72 * snd_hda_sequence_write - sequence writes
73 * @codec: the HDA codec
74 * @seq: VERB array to send
76 * Send the commands sequentially from the given array.
77 * The array must be terminated with NID=0.
79 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
81 for (; seq
->nid
; seq
++)
82 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
84 EXPORT_SYMBOL_GPL(snd_hda_sequence_write
);
86 /* connection list element */
87 struct hda_conn_list
{
88 struct list_head list
;
94 /* look up the cached results */
95 static struct hda_conn_list
*
96 lookup_conn_list(struct hda_codec
*codec
, hda_nid_t nid
)
98 struct hda_conn_list
*p
;
99 list_for_each_entry(p
, &codec
->conn_list
, list
) {
106 static int add_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
107 const hda_nid_t
*list
)
109 struct hda_conn_list
*p
;
111 p
= kmalloc(sizeof(*p
) + len
* sizeof(hda_nid_t
), GFP_KERNEL
);
116 memcpy(p
->conns
, list
, len
* sizeof(hda_nid_t
));
117 list_add(&p
->list
, &codec
->conn_list
);
121 static void remove_conn_list(struct hda_codec
*codec
)
123 while (!list_empty(&codec
->conn_list
)) {
124 struct hda_conn_list
*p
;
125 p
= list_first_entry(&codec
->conn_list
, typeof(*p
), list
);
131 /* read the connection and add to the cache */
132 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
135 hda_nid_t
*result
= list
;
138 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
139 if (len
== -ENOSPC
) {
140 len
= snd_hda_get_num_raw_conns(codec
, nid
);
141 result
= kmalloc_array(len
, sizeof(hda_nid_t
), GFP_KERNEL
);
144 len
= snd_hda_get_raw_connections(codec
, nid
, result
, len
);
147 len
= snd_hda_override_conn_list(codec
, nid
, len
, result
);
154 * snd_hda_get_conn_list - get connection list
155 * @codec: the HDA codec
157 * @listp: the pointer to store NID list
159 * Parses the connection list of the given widget and stores the pointer
160 * to the list of NIDs.
162 * Returns the number of connections, or a negative error code.
164 * Note that the returned pointer isn't protected against the list
165 * modification. If snd_hda_override_conn_list() might be called
166 * concurrently, protect with a mutex appropriately.
168 int snd_hda_get_conn_list(struct hda_codec
*codec
, hda_nid_t nid
,
169 const hda_nid_t
**listp
)
175 const struct hda_conn_list
*p
;
177 /* if the connection-list is already cached, read it */
178 p
= lookup_conn_list(codec
, nid
);
184 if (snd_BUG_ON(added
))
187 err
= read_and_add_raw_conns(codec
, nid
);
193 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list
);
196 * snd_hda_get_connections - copy connection list
197 * @codec: the HDA codec
199 * @conn_list: connection list array; when NULL, checks only the size
200 * @max_conns: max. number of connections to store
202 * Parses the connection list of the given widget and stores the list
205 * Returns the number of connections, or a negative error code.
207 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
208 hda_nid_t
*conn_list
, int max_conns
)
210 const hda_nid_t
*list
;
211 int len
= snd_hda_get_conn_list(codec
, nid
, &list
);
213 if (len
> 0 && conn_list
) {
214 if (len
> max_conns
) {
215 codec_err(codec
, "Too many connections %d for NID 0x%x\n",
219 memcpy(conn_list
, list
, len
* sizeof(hda_nid_t
));
224 EXPORT_SYMBOL_GPL(snd_hda_get_connections
);
227 * snd_hda_override_conn_list - add/modify the connection-list to cache
228 * @codec: the HDA codec
230 * @len: number of connection list entries
231 * @list: the list of connection entries
233 * Add or modify the given connection-list to the cache. If the corresponding
234 * cache already exists, invalidate it and append a new one.
236 * Returns zero or a negative error code.
238 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
239 const hda_nid_t
*list
)
241 struct hda_conn_list
*p
;
243 p
= lookup_conn_list(codec
, nid
);
249 return add_conn_list(codec
, nid
, len
, list
);
251 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list
);
254 * snd_hda_get_conn_index - get the connection index of the given NID
255 * @codec: the HDA codec
256 * @mux: NID containing the list
257 * @nid: NID to select
258 * @recursive: 1 when searching NID recursively, otherwise 0
260 * Parses the connection list of the widget @mux and checks whether the
261 * widget @nid is present. If it is, return the connection index.
262 * Otherwise it returns -1.
264 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
265 hda_nid_t nid
, int recursive
)
267 const hda_nid_t
*conn
;
270 nums
= snd_hda_get_conn_list(codec
, mux
, &conn
);
271 for (i
= 0; i
< nums
; i
++)
276 if (recursive
> 10) {
277 codec_dbg(codec
, "too deep connection for 0x%x\n", nid
);
281 for (i
= 0; i
< nums
; i
++) {
282 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
283 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
285 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
290 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index
);
293 * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget
294 * @codec: the HDA codec
295 * @nid: NID of the pin to parse
297 * Get the device entry number on the given widget. This is a feature of
298 * DP MST audio. Each pin can have several device entries in it.
300 unsigned int snd_hda_get_num_devices(struct hda_codec
*codec
, hda_nid_t nid
)
302 unsigned int wcaps
= get_wcaps(codec
, nid
);
305 if (!codec
->dp_mst
|| !(wcaps
& AC_WCAP_DIGITAL
) ||
306 get_wcaps_type(wcaps
) != AC_WID_PIN
)
309 parm
= snd_hdac_read_parm_uncached(&codec
->core
, nid
, AC_PAR_DEVLIST_LEN
);
312 return parm
& AC_DEV_LIST_LEN_MASK
;
314 EXPORT_SYMBOL_GPL(snd_hda_get_num_devices
);
317 * snd_hda_get_devices - copy device list without cache
318 * @codec: the HDA codec
319 * @nid: NID of the pin to parse
320 * @dev_list: device list array
321 * @max_devices: max. number of devices to store
323 * Copy the device list. This info is dynamic and so not cached.
324 * Currently called only from hda_proc.c, so not exported.
326 int snd_hda_get_devices(struct hda_codec
*codec
, hda_nid_t nid
,
327 u8
*dev_list
, int max_devices
)
330 int i
, dev_len
, devices
;
332 parm
= snd_hda_get_num_devices(codec
, nid
);
333 if (!parm
) /* not multi-stream capable */
337 dev_len
= dev_len
< max_devices
? dev_len
: max_devices
;
340 while (devices
< dev_len
) {
341 if (snd_hdac_read(&codec
->core
, nid
,
342 AC_VERB_GET_DEVICE_LIST
, devices
, &parm
))
345 for (i
= 0; i
< 8; i
++) {
346 dev_list
[devices
] = (u8
)parm
;
349 if (devices
>= dev_len
)
357 * snd_hda_get_dev_select - get device entry select on the pin
358 * @codec: the HDA codec
359 * @nid: NID of the pin to get device entry select
361 * Get the devcie entry select on the pin. Return the device entry
362 * id selected on the pin. Return 0 means the first device entry
363 * is selected or MST is not supported.
365 int snd_hda_get_dev_select(struct hda_codec
*codec
, hda_nid_t nid
)
367 /* not support dp_mst will always return 0, using first dev_entry */
371 return snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DEVICE_SEL
, 0);
373 EXPORT_SYMBOL_GPL(snd_hda_get_dev_select
);
376 * snd_hda_set_dev_select - set device entry select on the pin
377 * @codec: the HDA codec
378 * @nid: NID of the pin to set device entry select
379 * @dev_id: device entry id to be set
381 * Set the device entry select on the pin nid.
383 int snd_hda_set_dev_select(struct hda_codec
*codec
, hda_nid_t nid
, int dev_id
)
385 int ret
, num_devices
;
387 /* not support dp_mst will always return 0, using first dev_entry */
391 /* AC_PAR_DEVLIST_LEN is 0 based. */
392 num_devices
= snd_hda_get_num_devices(codec
, nid
) + 1;
393 /* If Device List Length is 0 (num_device = 1),
394 * the pin is not multi stream capable.
395 * Do nothing in this case.
397 if (num_devices
== 1)
400 /* Behavior of setting index being equal to or greater than
401 * Device List Length is not predictable
403 if (num_devices
<= dev_id
)
406 ret
= snd_hda_codec_write(codec
, nid
, 0,
407 AC_VERB_SET_DEVICE_SEL
, dev_id
);
411 EXPORT_SYMBOL_GPL(snd_hda_set_dev_select
);
414 * read widget caps for each widget and store in cache
416 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
421 codec
->wcaps
= kmalloc_array(codec
->core
.num_nodes
, 4, GFP_KERNEL
);
424 nid
= codec
->core
.start_nid
;
425 for (i
= 0; i
< codec
->core
.num_nodes
; i
++, nid
++)
426 codec
->wcaps
[i
] = snd_hdac_read_parm_uncached(&codec
->core
,
427 nid
, AC_PAR_AUDIO_WIDGET_CAP
);
431 /* read all pin default configurations and save codec->init_pins */
432 static int read_pin_defaults(struct hda_codec
*codec
)
436 for_each_hda_codec_node(nid
, codec
) {
437 struct hda_pincfg
*pin
;
438 unsigned int wcaps
= get_wcaps(codec
, nid
);
439 unsigned int wid_type
= get_wcaps_type(wcaps
);
440 if (wid_type
!= AC_WID_PIN
)
442 pin
= snd_array_new(&codec
->init_pins
);
446 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
447 AC_VERB_GET_CONFIG_DEFAULT
, 0);
449 * all device entries are the same widget control so far
450 * fixme: if any codec is different, need fix here
452 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
453 AC_VERB_GET_PIN_WIDGET_CONTROL
,
459 /* look up the given pin config list and return the item matching with NID */
460 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
461 struct snd_array
*array
,
464 struct hda_pincfg
*pin
;
467 snd_array_for_each(array
, i
, pin
) {
474 /* set the current pin config value for the given NID.
475 * the value is cached, and read via snd_hda_codec_get_pincfg()
477 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
478 hda_nid_t nid
, unsigned int cfg
)
480 struct hda_pincfg
*pin
;
482 /* the check below may be invalid when pins are added by a fixup
483 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
487 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
491 pin
= look_up_pincfg(codec
, list
, nid
);
493 pin
= snd_array_new(list
);
503 * snd_hda_codec_set_pincfg - Override a pin default configuration
504 * @codec: the HDA codec
505 * @nid: NID to set the pin config
506 * @cfg: the pin default config value
508 * Override a pin default configuration value in the cache.
509 * This value can be read by snd_hda_codec_get_pincfg() in a higher
510 * priority than the real hardware value.
512 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
513 hda_nid_t nid
, unsigned int cfg
)
515 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
517 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg
);
520 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
521 * @codec: the HDA codec
522 * @nid: NID to get the pin config
524 * Get the current pin config value of the given pin NID.
525 * If the pincfg value is cached or overridden via sysfs or driver,
526 * returns the cached value.
528 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
530 struct hda_pincfg
*pin
;
532 #ifdef CONFIG_SND_HDA_RECONFIG
534 unsigned int cfg
= 0;
535 mutex_lock(&codec
->user_mutex
);
536 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
539 mutex_unlock(&codec
->user_mutex
);
544 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
547 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
552 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg
);
555 * snd_hda_codec_set_pin_target - remember the current pinctl target value
556 * @codec: the HDA codec
558 * @val: assigned pinctl value
560 * This function stores the given value to a pinctl target value in the
561 * pincfg table. This isn't always as same as the actually written value
562 * but can be referred at any time via snd_hda_codec_get_pin_target().
564 int snd_hda_codec_set_pin_target(struct hda_codec
*codec
, hda_nid_t nid
,
567 struct hda_pincfg
*pin
;
569 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
575 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target
);
578 * snd_hda_codec_get_pin_target - return the current pinctl target value
579 * @codec: the HDA codec
582 int snd_hda_codec_get_pin_target(struct hda_codec
*codec
, hda_nid_t nid
)
584 struct hda_pincfg
*pin
;
586 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
591 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target
);
594 * snd_hda_shutup_pins - Shut up all pins
595 * @codec: the HDA codec
597 * Clear all pin controls to shup up before suspend for avoiding click noise.
598 * The controls aren't cached so that they can be resumed properly.
600 void snd_hda_shutup_pins(struct hda_codec
*codec
)
602 const struct hda_pincfg
*pin
;
605 /* don't shut up pins when unloading the driver; otherwise it breaks
606 * the default pin setup at the next load of the driver
608 if (codec
->bus
->shutdown
)
610 snd_array_for_each(&codec
->init_pins
, i
, pin
) {
611 /* use read here for syncing after issuing each verb */
612 snd_hda_codec_read(codec
, pin
->nid
, 0,
613 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
615 codec
->pins_shutup
= 1;
617 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins
);
620 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
621 static void restore_shutup_pins(struct hda_codec
*codec
)
623 const struct hda_pincfg
*pin
;
626 if (!codec
->pins_shutup
)
628 if (codec
->bus
->shutdown
)
630 snd_array_for_each(&codec
->init_pins
, i
, pin
) {
631 snd_hda_codec_write(codec
, pin
->nid
, 0,
632 AC_VERB_SET_PIN_WIDGET_CONTROL
,
635 codec
->pins_shutup
= 0;
639 static void hda_jackpoll_work(struct work_struct
*work
)
641 struct hda_codec
*codec
=
642 container_of(work
, struct hda_codec
, jackpoll_work
.work
);
644 snd_hda_jack_set_dirty_all(codec
);
645 snd_hda_jack_poll_all(codec
);
647 if (!codec
->jackpoll_interval
)
650 schedule_delayed_work(&codec
->jackpoll_work
,
651 codec
->jackpoll_interval
);
654 /* release all pincfg lists */
655 static void free_init_pincfgs(struct hda_codec
*codec
)
657 snd_array_free(&codec
->driver_pins
);
658 #ifdef CONFIG_SND_HDA_RECONFIG
659 snd_array_free(&codec
->user_pins
);
661 snd_array_free(&codec
->init_pins
);
665 * audio-converter setup caches
667 struct hda_cvt_setup
{
672 unsigned char active
; /* cvt is currently used */
673 unsigned char dirty
; /* setups should be cleared */
676 /* get or create a cache entry for the given audio converter NID */
677 static struct hda_cvt_setup
*
678 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
680 struct hda_cvt_setup
*p
;
683 snd_array_for_each(&codec
->cvt_setups
, i
, p
) {
687 p
= snd_array_new(&codec
->cvt_setups
);
696 static void release_pcm(struct kref
*kref
)
698 struct hda_pcm
*pcm
= container_of(kref
, struct hda_pcm
, kref
);
701 snd_device_free(pcm
->codec
->card
, pcm
->pcm
);
702 clear_bit(pcm
->device
, pcm
->codec
->bus
->pcm_dev_bits
);
707 void snd_hda_codec_pcm_put(struct hda_pcm
*pcm
)
709 kref_put(&pcm
->kref
, release_pcm
);
711 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put
);
713 struct hda_pcm
*snd_hda_codec_pcm_new(struct hda_codec
*codec
,
714 const char *fmt
, ...)
719 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
724 kref_init(&pcm
->kref
);
726 pcm
->name
= kvasprintf(GFP_KERNEL
, fmt
, args
);
733 list_add_tail(&pcm
->list
, &codec
->pcm_list_head
);
736 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new
);
741 static void codec_release_pcms(struct hda_codec
*codec
)
743 struct hda_pcm
*pcm
, *n
;
745 list_for_each_entry_safe(pcm
, n
, &codec
->pcm_list_head
, list
) {
746 list_del_init(&pcm
->list
);
748 snd_device_disconnect(codec
->card
, pcm
->pcm
);
749 snd_hda_codec_pcm_put(pcm
);
753 void snd_hda_codec_cleanup_for_unbind(struct hda_codec
*codec
)
755 if (codec
->registered
) {
756 /* pm_runtime_put() is called in snd_hdac_device_exit() */
757 pm_runtime_get_noresume(hda_codec_dev(codec
));
758 pm_runtime_disable(hda_codec_dev(codec
));
759 codec
->registered
= 0;
762 cancel_delayed_work_sync(&codec
->jackpoll_work
);
763 if (!codec
->in_freeing
)
764 snd_hda_ctls_clear(codec
);
765 codec_release_pcms(codec
);
766 snd_hda_detach_beep_device(codec
);
767 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
768 snd_hda_jack_tbl_clear(codec
);
769 codec
->proc_widget_hook
= NULL
;
772 /* free only driver_pins so that init_pins + user_pins are restored */
773 snd_array_free(&codec
->driver_pins
);
774 snd_array_free(&codec
->cvt_setups
);
775 snd_array_free(&codec
->spdif_out
);
776 snd_array_free(&codec
->verbs
);
777 codec
->preset
= NULL
;
778 codec
->slave_dig_outs
= NULL
;
779 codec
->spdif_status_reset
= 0;
780 snd_array_free(&codec
->mixers
);
781 snd_array_free(&codec
->nids
);
782 remove_conn_list(codec
);
783 snd_hdac_regmap_exit(&codec
->core
);
786 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
787 unsigned int power_state
);
789 /* enable/disable display power per codec */
790 static void codec_display_power(struct hda_codec
*codec
, bool enable
)
792 if (codec
->display_power_control
)
793 snd_hdac_display_power(&codec
->bus
->core
, codec
->addr
, enable
);
796 /* also called from hda_bind.c */
797 void snd_hda_codec_register(struct hda_codec
*codec
)
799 if (codec
->registered
)
801 if (device_is_registered(hda_codec_dev(codec
))) {
802 codec_display_power(codec
, true);
803 pm_runtime_enable(hda_codec_dev(codec
));
804 /* it was powered up in snd_hda_codec_new(), now all done */
805 snd_hda_power_down(codec
);
806 codec
->registered
= 1;
810 static int snd_hda_codec_dev_register(struct snd_device
*device
)
812 snd_hda_codec_register(device
->device_data
);
816 static int snd_hda_codec_dev_free(struct snd_device
*device
)
818 struct hda_codec
*codec
= device
->device_data
;
820 codec
->in_freeing
= 1;
822 * snd_hda_codec_device_new() is used by legacy HDA and ASoC driver.
823 * We can't unregister ASoC device since it will be unregistered in
824 * snd_hdac_ext_bus_device_remove().
826 if (codec
->core
.type
== HDA_DEV_LEGACY
)
827 snd_hdac_device_unregister(&codec
->core
);
828 codec_display_power(codec
, false);
829 put_device(hda_codec_dev(codec
));
833 static void snd_hda_codec_dev_release(struct device
*dev
)
835 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
837 free_init_pincfgs(codec
);
838 snd_hdac_device_exit(&codec
->core
);
839 snd_hda_sysfs_clear(codec
);
840 kfree(codec
->modelname
);
845 #define DEV_NAME_LEN 31
847 static int snd_hda_codec_device_init(struct hda_bus
*bus
, struct snd_card
*card
,
848 unsigned int codec_addr
, struct hda_codec
**codecp
)
850 char name
[DEV_NAME_LEN
];
851 struct hda_codec
*codec
;
854 dev_dbg(card
->dev
, "%s: entry\n", __func__
);
856 if (snd_BUG_ON(!bus
))
858 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
861 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
865 sprintf(name
, "hdaudioC%dD%d", card
->number
, codec_addr
);
866 err
= snd_hdac_device_init(&codec
->core
, &bus
->core
, name
, codec_addr
);
872 codec
->core
.type
= HDA_DEV_LEGACY
;
879 * snd_hda_codec_new - create a HDA codec
880 * @bus: the bus to assign
881 * @codec_addr: the codec address
882 * @codecp: the pointer to store the generated codec
884 * Returns 0 if successful, or a negative error code.
886 int snd_hda_codec_new(struct hda_bus
*bus
, struct snd_card
*card
,
887 unsigned int codec_addr
, struct hda_codec
**codecp
)
891 ret
= snd_hda_codec_device_init(bus
, card
, codec_addr
, codecp
);
895 return snd_hda_codec_device_new(bus
, card
, codec_addr
, *codecp
);
897 EXPORT_SYMBOL_GPL(snd_hda_codec_new
);
899 int snd_hda_codec_device_new(struct hda_bus
*bus
, struct snd_card
*card
,
900 unsigned int codec_addr
, struct hda_codec
*codec
)
905 static struct snd_device_ops dev_ops
= {
906 .dev_register
= snd_hda_codec_dev_register
,
907 .dev_free
= snd_hda_codec_dev_free
,
910 dev_dbg(card
->dev
, "%s: entry\n", __func__
);
912 if (snd_BUG_ON(!bus
))
914 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
917 codec
->core
.dev
.release
= snd_hda_codec_dev_release
;
918 codec
->core
.exec_verb
= codec_exec_verb
;
922 codec
->addr
= codec_addr
;
923 mutex_init(&codec
->spdif_mutex
);
924 mutex_init(&codec
->control_mutex
);
925 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
926 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
927 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
928 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
929 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
930 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
931 snd_array_init(&codec
->jacktbl
, sizeof(struct hda_jack_tbl
), 16);
932 snd_array_init(&codec
->verbs
, sizeof(struct hda_verb
*), 8);
933 INIT_LIST_HEAD(&codec
->conn_list
);
934 INIT_LIST_HEAD(&codec
->pcm_list_head
);
936 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
937 codec
->depop_delay
= -1;
938 codec
->fixup_id
= HDA_FIXUP_ID_NOT_SET
;
941 codec
->power_jiffies
= jiffies
;
944 snd_hda_sysfs_init(codec
);
946 if (codec
->bus
->modelname
) {
947 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
948 if (!codec
->modelname
) {
954 fg
= codec
->core
.afg
? codec
->core
.afg
: codec
->core
.mfg
;
955 err
= read_widget_caps(codec
, fg
);
958 err
= read_pin_defaults(codec
);
962 /* power-up all before initialization */
963 hda_set_power_state(codec
, AC_PWRST_D0
);
964 codec
->core
.dev
.power
.power_state
= PMSG_ON
;
966 snd_hda_codec_proc_new(codec
);
968 snd_hda_create_hwdep(codec
);
970 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->core
.vendor_id
,
971 codec
->core
.subsystem_id
, codec
->core
.revision_id
);
972 snd_component_add(card
, component
);
974 err
= snd_device_new(card
, SNDRV_DEV_CODEC
, codec
, &dev_ops
);
981 put_device(hda_codec_dev(codec
));
984 EXPORT_SYMBOL_GPL(snd_hda_codec_device_new
);
987 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
988 * @codec: the HDA codec
990 * Forcibly refresh the all widget caps and the init pin configurations of
993 int snd_hda_codec_update_widgets(struct hda_codec
*codec
)
998 err
= snd_hdac_refresh_widgets(&codec
->core
, true);
1002 /* Assume the function group node does not change,
1003 * only the widget nodes may change.
1005 kfree(codec
->wcaps
);
1006 fg
= codec
->core
.afg
? codec
->core
.afg
: codec
->core
.mfg
;
1007 err
= read_widget_caps(codec
, fg
);
1011 snd_array_free(&codec
->init_pins
);
1012 err
= read_pin_defaults(codec
);
1016 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets
);
1018 /* update the stream-id if changed */
1019 static void update_pcm_stream_id(struct hda_codec
*codec
,
1020 struct hda_cvt_setup
*p
, hda_nid_t nid
,
1021 u32 stream_tag
, int channel_id
)
1023 unsigned int oldval
, newval
;
1025 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1026 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1027 newval
= (stream_tag
<< 4) | channel_id
;
1028 if (oldval
!= newval
)
1029 snd_hda_codec_write(codec
, nid
, 0,
1030 AC_VERB_SET_CHANNEL_STREAMID
,
1032 p
->stream_tag
= stream_tag
;
1033 p
->channel_id
= channel_id
;
1037 /* update the format-id if changed */
1038 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
1039 hda_nid_t nid
, int format
)
1041 unsigned int oldval
;
1043 if (p
->format_id
!= format
) {
1044 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1045 AC_VERB_GET_STREAM_FORMAT
, 0);
1046 if (oldval
!= format
) {
1048 snd_hda_codec_write(codec
, nid
, 0,
1049 AC_VERB_SET_STREAM_FORMAT
,
1052 p
->format_id
= format
;
1057 * snd_hda_codec_setup_stream - set up the codec for streaming
1058 * @codec: the CODEC to set up
1059 * @nid: the NID to set up
1060 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1061 * @channel_id: channel id to pass, zero based.
1062 * @format: stream format.
1064 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1066 int channel_id
, int format
)
1068 struct hda_codec
*c
;
1069 struct hda_cvt_setup
*p
;
1077 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1078 nid
, stream_tag
, channel_id
, format
);
1079 p
= get_hda_cvt_setup(codec
, nid
);
1083 if (codec
->patch_ops
.stream_pm
)
1084 codec
->patch_ops
.stream_pm(codec
, nid
, true);
1085 if (codec
->pcm_format_first
)
1086 update_pcm_format(codec
, p
, nid
, format
);
1087 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1088 if (!codec
->pcm_format_first
)
1089 update_pcm_format(codec
, p
, nid
, format
);
1094 /* make other inactive cvts with the same stream-tag dirty */
1095 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1096 list_for_each_codec(c
, codec
->bus
) {
1097 snd_array_for_each(&c
->cvt_setups
, i
, p
) {
1098 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1099 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1104 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream
);
1106 static void really_cleanup_stream(struct hda_codec
*codec
,
1107 struct hda_cvt_setup
*q
);
1110 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1111 * @codec: the CODEC to clean up
1112 * @nid: the NID to clean up
1113 * @do_now: really clean up the stream instead of clearing the active flag
1115 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1118 struct hda_cvt_setup
*p
;
1123 if (codec
->no_sticky_stream
)
1126 codec_dbg(codec
, "hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1127 p
= get_hda_cvt_setup(codec
, nid
);
1129 /* here we just clear the active flag when do_now isn't set;
1130 * actual clean-ups will be done later in
1131 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1134 really_cleanup_stream(codec
, p
);
1139 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream
);
1141 static void really_cleanup_stream(struct hda_codec
*codec
,
1142 struct hda_cvt_setup
*q
)
1144 hda_nid_t nid
= q
->nid
;
1145 if (q
->stream_tag
|| q
->channel_id
)
1146 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1148 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1150 memset(q
, 0, sizeof(*q
));
1152 if (codec
->patch_ops
.stream_pm
)
1153 codec
->patch_ops
.stream_pm(codec
, nid
, false);
1156 /* clean up the all conflicting obsolete streams */
1157 static void purify_inactive_streams(struct hda_codec
*codec
)
1159 struct hda_codec
*c
;
1160 struct hda_cvt_setup
*p
;
1163 list_for_each_codec(c
, codec
->bus
) {
1164 snd_array_for_each(&c
->cvt_setups
, i
, p
) {
1166 really_cleanup_stream(c
, p
);
1172 /* clean up all streams; called from suspend */
1173 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1175 struct hda_cvt_setup
*p
;
1178 snd_array_for_each(&codec
->cvt_setups
, i
, p
) {
1180 really_cleanup_stream(codec
, p
);
1186 * amp access functions
1190 * query_amp_caps - query AMP capabilities
1191 * @codec: the HD-auio codec
1192 * @nid: the NID to query
1193 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1195 * Query AMP capabilities for the given widget and direction.
1196 * Returns the obtained capability bits.
1198 * When cap bits have been already read, this doesn't read again but
1199 * returns the cached value.
1201 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1203 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1204 nid
= codec
->core
.afg
;
1205 return snd_hda_param_read(codec
, nid
,
1206 direction
== HDA_OUTPUT
?
1207 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1209 EXPORT_SYMBOL_GPL(query_amp_caps
);
1212 * snd_hda_check_amp_caps - query AMP capabilities
1213 * @codec: the HD-audio codec
1214 * @nid: the NID to query
1215 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1216 * @bits: bit mask to check the result
1218 * Check whether the widget has the given amp capability for the direction.
1220 bool snd_hda_check_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1221 int dir
, unsigned int bits
)
1225 if (get_wcaps(codec
, nid
) & (1 << (dir
+ 1)))
1226 if (query_amp_caps(codec
, nid
, dir
) & bits
)
1230 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps
);
1233 * snd_hda_override_amp_caps - Override the AMP capabilities
1234 * @codec: the CODEC to clean up
1235 * @nid: the NID to clean up
1236 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1237 * @caps: the capability bits to set
1239 * Override the cached AMP caps bits value by the given one.
1240 * This function is useful if the driver needs to adjust the AMP ranges,
1241 * e.g. limit to 0dB, etc.
1243 * Returns zero if successful or a negative error code.
1245 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1250 snd_hda_override_wcaps(codec
, nid
,
1251 get_wcaps(codec
, nid
) | AC_WCAP_AMP_OVRD
);
1252 parm
= dir
== HDA_OUTPUT
? AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
;
1253 return snd_hdac_override_parm(&codec
->core
, nid
, parm
, caps
);
1255 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps
);
1258 * snd_hda_codec_amp_update - update the AMP mono value
1259 * @codec: HD-audio codec
1260 * @nid: NID to read the AMP value
1261 * @ch: channel to update (0 or 1)
1262 * @dir: #HDA_INPUT or #HDA_OUTPUT
1263 * @idx: the index value (only for input direction)
1264 * @mask: bit mask to set
1265 * @val: the bits value to set
1267 * Update the AMP values for the given channel, direction and index.
1269 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
,
1270 int ch
, int dir
, int idx
, int mask
, int val
)
1272 unsigned int cmd
= snd_hdac_regmap_encode_amp(nid
, ch
, dir
, idx
);
1274 /* enable fake mute if no h/w mute but min=mute */
1275 if ((query_amp_caps(codec
, nid
, dir
) &
1276 (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) == AC_AMPCAP_MIN_MUTE
)
1277 cmd
|= AC_AMP_FAKE_MUTE
;
1278 return snd_hdac_regmap_update_raw(&codec
->core
, cmd
, mask
, val
);
1280 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update
);
1283 * snd_hda_codec_amp_stereo - update the AMP stereo values
1284 * @codec: HD-audio codec
1285 * @nid: NID to read the AMP value
1286 * @direction: #HDA_INPUT or #HDA_OUTPUT
1287 * @idx: the index value (only for input direction)
1288 * @mask: bit mask to set
1289 * @val: the bits value to set
1291 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1292 * stereo widget with the same mask and value.
1294 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1295 int direction
, int idx
, int mask
, int val
)
1299 if (snd_BUG_ON(mask
& ~0xff))
1301 for (ch
= 0; ch
< 2; ch
++)
1302 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1306 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo
);
1309 * snd_hda_codec_amp_init - initialize the AMP value
1310 * @codec: the HDA codec
1311 * @nid: NID to read the AMP value
1312 * @ch: channel (left=0 or right=1)
1313 * @dir: #HDA_INPUT or #HDA_OUTPUT
1314 * @idx: the index value (only for input direction)
1315 * @mask: bit mask to set
1316 * @val: the bits value to set
1318 * Works like snd_hda_codec_amp_update() but it writes the value only at
1319 * the first access. If the amp was already initialized / updated beforehand,
1320 * this does nothing.
1322 int snd_hda_codec_amp_init(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1323 int dir
, int idx
, int mask
, int val
)
1327 if (!codec
->core
.regmap
)
1329 regcache_cache_only(codec
->core
.regmap
, true);
1330 orig
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1331 regcache_cache_only(codec
->core
.regmap
, false);
1334 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
, mask
, val
);
1336 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init
);
1339 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1340 * @codec: the HDA codec
1341 * @nid: NID to read the AMP value
1342 * @dir: #HDA_INPUT or #HDA_OUTPUT
1343 * @idx: the index value (only for input direction)
1344 * @mask: bit mask to set
1345 * @val: the bits value to set
1347 * Call snd_hda_codec_amp_init() for both stereo channels.
1349 int snd_hda_codec_amp_init_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1350 int dir
, int idx
, int mask
, int val
)
1354 if (snd_BUG_ON(mask
& ~0xff))
1356 for (ch
= 0; ch
< 2; ch
++)
1357 ret
|= snd_hda_codec_amp_init(codec
, nid
, ch
, dir
,
1361 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo
);
1363 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1366 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1368 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1375 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1376 * @kcontrol: referred ctl element
1377 * @uinfo: pointer to get/store the data
1379 * The control element is supposed to have the private_value field
1380 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1382 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1383 struct snd_ctl_elem_info
*uinfo
)
1385 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1386 u16 nid
= get_amp_nid(kcontrol
);
1387 u8 chs
= get_amp_channels(kcontrol
);
1388 int dir
= get_amp_direction(kcontrol
);
1389 unsigned int ofs
= get_amp_offset(kcontrol
);
1391 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1392 uinfo
->count
= chs
== 3 ? 2 : 1;
1393 uinfo
->value
.integer
.min
= 0;
1394 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
1395 if (!uinfo
->value
.integer
.max
) {
1397 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1398 nid
, kcontrol
->id
.name
);
1403 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info
);
1406 static inline unsigned int
1407 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1408 int ch
, int dir
, int idx
, unsigned int ofs
)
1411 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1412 val
&= HDA_AMP_VOLMASK
;
1421 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1422 int ch
, int dir
, int idx
, unsigned int ofs
,
1425 unsigned int maxval
;
1429 /* ofs = 0: raw max value */
1430 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
1433 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
1434 HDA_AMP_VOLMASK
, val
);
1438 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1439 * @kcontrol: ctl element
1440 * @ucontrol: pointer to get/store the data
1442 * The control element is supposed to have the private_value field
1443 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1445 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
1446 struct snd_ctl_elem_value
*ucontrol
)
1448 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1449 hda_nid_t nid
= get_amp_nid(kcontrol
);
1450 int chs
= get_amp_channels(kcontrol
);
1451 int dir
= get_amp_direction(kcontrol
);
1452 int idx
= get_amp_index(kcontrol
);
1453 unsigned int ofs
= get_amp_offset(kcontrol
);
1454 long *valp
= ucontrol
->value
.integer
.value
;
1457 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
1459 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
1462 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get
);
1465 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1466 * @kcontrol: ctl element
1467 * @ucontrol: pointer to get/store the data
1469 * The control element is supposed to have the private_value field
1470 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1472 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
1473 struct snd_ctl_elem_value
*ucontrol
)
1475 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1476 hda_nid_t nid
= get_amp_nid(kcontrol
);
1477 int chs
= get_amp_channels(kcontrol
);
1478 int dir
= get_amp_direction(kcontrol
);
1479 int idx
= get_amp_index(kcontrol
);
1480 unsigned int ofs
= get_amp_offset(kcontrol
);
1481 long *valp
= ucontrol
->value
.integer
.value
;
1485 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
1489 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
1492 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put
);
1494 /* inquiry the amp caps and convert to TLV */
1495 static void get_ctl_amp_tlv(struct snd_kcontrol
*kcontrol
, unsigned int *tlv
)
1497 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1498 hda_nid_t nid
= get_amp_nid(kcontrol
);
1499 int dir
= get_amp_direction(kcontrol
);
1500 unsigned int ofs
= get_amp_offset(kcontrol
);
1501 bool min_mute
= get_amp_min_mute(kcontrol
);
1502 u32 caps
, val1
, val2
;
1504 caps
= query_amp_caps(codec
, nid
, dir
);
1505 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1506 val2
= (val2
+ 1) * 25;
1507 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
1509 val1
= ((int)val1
) * ((int)val2
);
1510 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
1511 val2
|= TLV_DB_SCALE_MUTE
;
1512 tlv
[SNDRV_CTL_TLVO_TYPE
] = SNDRV_CTL_TLVT_DB_SCALE
;
1513 tlv
[SNDRV_CTL_TLVO_LEN
] = 2 * sizeof(unsigned int);
1514 tlv
[SNDRV_CTL_TLVO_DB_SCALE_MIN
] = val1
;
1515 tlv
[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP
] = val2
;
1519 * snd_hda_mixer_amp_tlv - TLV callback for a standard AMP mixer volume
1520 * @kcontrol: ctl element
1521 * @op_flag: operation flag
1522 * @size: byte size of input TLV
1525 * The control element is supposed to have the private_value field
1526 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1528 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
1529 unsigned int size
, unsigned int __user
*_tlv
)
1531 unsigned int tlv
[4];
1533 if (size
< 4 * sizeof(unsigned int))
1535 get_ctl_amp_tlv(kcontrol
, tlv
);
1536 if (copy_to_user(_tlv
, tlv
, sizeof(tlv
)))
1540 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv
);
1543 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1544 * @codec: HD-audio codec
1545 * @nid: NID of a reference widget
1546 * @dir: #HDA_INPUT or #HDA_OUTPUT
1547 * @tlv: TLV data to be stored, at least 4 elements
1549 * Set (static) TLV data for a virtual master volume using the AMP caps
1550 * obtained from the reference NID.
1551 * The volume range is recalculated as if the max volume is 0dB.
1553 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1559 caps
= query_amp_caps(codec
, nid
, dir
);
1560 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1561 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1562 step
= (step
+ 1) * 25;
1563 tlv
[SNDRV_CTL_TLVO_TYPE
] = SNDRV_CTL_TLVT_DB_SCALE
;
1564 tlv
[SNDRV_CTL_TLVO_LEN
] = 2 * sizeof(unsigned int);
1565 tlv
[SNDRV_CTL_TLVO_DB_SCALE_MIN
] = -nums
* step
;
1566 tlv
[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP
] = step
;
1568 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv
);
1570 /* find a mixer control element with the given name */
1571 static struct snd_kcontrol
*
1572 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
1574 struct snd_ctl_elem_id id
;
1575 memset(&id
, 0, sizeof(id
));
1576 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1579 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
1581 strcpy(id
.name
, name
);
1582 return snd_ctl_find_id(codec
->card
, &id
);
1586 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1587 * @codec: HD-audio codec
1588 * @name: ctl id name string
1590 * Get the control element with the given id string and IFACE_MIXER.
1592 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
1595 return find_mixer_ctl(codec
, name
, 0, 0);
1597 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl
);
1599 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
1603 /* 16 ctlrs should be large enough */
1604 for (i
= 0, idx
= start_idx
; i
< 16; i
++, idx
++) {
1605 if (!find_mixer_ctl(codec
, name
, 0, idx
))
1612 * snd_hda_ctl_add - Add a control element and assign to the codec
1613 * @codec: HD-audio codec
1614 * @nid: corresponding NID (optional)
1615 * @kctl: the control element to assign
1617 * Add the given control element to an array inside the codec instance.
1618 * All control elements belonging to a codec are supposed to be added
1619 * by this function so that a proper clean-up works at the free or
1620 * reconfiguration time.
1622 * If non-zero @nid is passed, the NID is assigned to the control element.
1623 * The assignment is shown in the codec proc file.
1625 * snd_hda_ctl_add() checks the control subdev id field whether
1626 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1627 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1628 * specifies if kctl->private_value is a HDA amplifier value.
1630 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
1631 struct snd_kcontrol
*kctl
)
1634 unsigned short flags
= 0;
1635 struct hda_nid_item
*item
;
1637 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
1638 flags
|= HDA_NID_ITEM_AMP
;
1640 nid
= get_amp_nid_(kctl
->private_value
);
1642 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
1643 nid
= kctl
->id
.subdevice
& 0xffff;
1644 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
1645 kctl
->id
.subdevice
= 0;
1646 err
= snd_ctl_add(codec
->card
, kctl
);
1649 item
= snd_array_new(&codec
->mixers
);
1654 item
->flags
= flags
;
1657 EXPORT_SYMBOL_GPL(snd_hda_ctl_add
);
1660 * snd_hda_add_nid - Assign a NID to a control element
1661 * @codec: HD-audio codec
1662 * @nid: corresponding NID (optional)
1663 * @kctl: the control element to assign
1664 * @index: index to kctl
1666 * Add the given control element to an array inside the codec instance.
1667 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1668 * NID:KCTL mapping - for example "Capture Source" selector.
1670 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
1671 unsigned int index
, hda_nid_t nid
)
1673 struct hda_nid_item
*item
;
1676 item
= snd_array_new(&codec
->nids
);
1680 item
->index
= index
;
1684 codec_err(codec
, "no NID for mapping control %s:%d:%d\n",
1685 kctl
->id
.name
, kctl
->id
.index
, index
);
1688 EXPORT_SYMBOL_GPL(snd_hda_add_nid
);
1691 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1692 * @codec: HD-audio codec
1694 void snd_hda_ctls_clear(struct hda_codec
*codec
)
1697 struct hda_nid_item
*items
= codec
->mixers
.list
;
1698 for (i
= 0; i
< codec
->mixers
.used
; i
++)
1699 snd_ctl_remove(codec
->card
, items
[i
].kctl
);
1700 snd_array_free(&codec
->mixers
);
1701 snd_array_free(&codec
->nids
);
1705 * snd_hda_lock_devices - pseudo device locking
1708 * toggle card->shutdown to allow/disallow the device access (as a hack)
1710 int snd_hda_lock_devices(struct hda_bus
*bus
)
1712 struct snd_card
*card
= bus
->card
;
1713 struct hda_codec
*codec
;
1715 spin_lock(&card
->files_lock
);
1719 if (!list_empty(&card
->ctl_files
))
1722 list_for_each_codec(codec
, bus
) {
1723 struct hda_pcm
*cpcm
;
1724 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
1727 if (cpcm
->pcm
->streams
[0].substream_opened
||
1728 cpcm
->pcm
->streams
[1].substream_opened
)
1732 spin_unlock(&card
->files_lock
);
1738 spin_unlock(&card
->files_lock
);
1741 EXPORT_SYMBOL_GPL(snd_hda_lock_devices
);
1744 * snd_hda_unlock_devices - pseudo device unlocking
1747 void snd_hda_unlock_devices(struct hda_bus
*bus
)
1749 struct snd_card
*card
= bus
->card
;
1751 spin_lock(&card
->files_lock
);
1753 spin_unlock(&card
->files_lock
);
1755 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices
);
1758 * snd_hda_codec_reset - Clear all objects assigned to the codec
1759 * @codec: HD-audio codec
1761 * This frees the all PCM and control elements assigned to the codec, and
1762 * clears the caches and restores the pin default configurations.
1764 * When a device is being used, it returns -EBSY. If successfully freed,
1767 int snd_hda_codec_reset(struct hda_codec
*codec
)
1769 struct hda_bus
*bus
= codec
->bus
;
1771 if (snd_hda_lock_devices(bus
) < 0)
1774 /* OK, let it free */
1775 snd_hdac_device_unregister(&codec
->core
);
1777 /* allow device access again */
1778 snd_hda_unlock_devices(bus
);
1782 typedef int (*map_slave_func_t
)(struct hda_codec
*, void *, struct snd_kcontrol
*);
1784 /* apply the function to all matching slave ctls in the mixer list */
1785 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
1786 const char *suffix
, map_slave_func_t func
, void *data
)
1788 struct hda_nid_item
*items
;
1789 const char * const *s
;
1792 items
= codec
->mixers
.list
;
1793 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
1794 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
1795 if (!sctl
|| sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
1797 for (s
= slaves
; *s
; s
++) {
1798 char tmpname
[sizeof(sctl
->id
.name
)];
1799 const char *name
= *s
;
1801 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
1805 if (!strcmp(sctl
->id
.name
, name
)) {
1806 err
= func(codec
, data
, sctl
);
1816 static int check_slave_present(struct hda_codec
*codec
,
1817 void *data
, struct snd_kcontrol
*sctl
)
1822 /* call kctl->put with the given value(s) */
1823 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
1825 struct snd_ctl_elem_value
*ucontrol
;
1826 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
1829 ucontrol
->value
.integer
.value
[0] = val
;
1830 ucontrol
->value
.integer
.value
[1] = val
;
1831 kctl
->put(kctl
, ucontrol
);
1836 struct slave_init_arg
{
1837 struct hda_codec
*codec
;
1841 /* initialize the slave volume with 0dB via snd_ctl_apply_vmaster_slaves() */
1842 static int init_slave_0dB(struct snd_kcontrol
*slave
,
1843 struct snd_kcontrol
*kctl
,
1846 struct slave_init_arg
*arg
= _arg
;
1848 const int *tlv
= NULL
;
1852 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
1853 if (kctl
->tlv
.c
!= snd_hda_mixer_amp_tlv
) {
1854 codec_err(arg
->codec
,
1855 "Unexpected TLV callback for slave %s:%d\n",
1856 kctl
->id
.name
, kctl
->id
.index
);
1857 return 0; /* ignore */
1859 get_ctl_amp_tlv(kctl
, _tlv
);
1861 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
1864 if (!tlv
|| tlv
[SNDRV_CTL_TLVO_TYPE
] != SNDRV_CTL_TLVT_DB_SCALE
)
1867 step
= tlv
[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP
];
1868 step
&= ~TLV_DB_SCALE_MUTE
;
1871 if (arg
->step
&& arg
->step
!= step
) {
1872 codec_err(arg
->codec
,
1873 "Mismatching dB step for vmaster slave (%d!=%d)\n",
1879 val
= -tlv
[SNDRV_CTL_TLVO_DB_SCALE_MIN
] / step
;
1881 put_kctl_with_value(slave
, val
);
1888 /* unmute the slave via snd_ctl_apply_vmaster_slaves() */
1889 static int init_slave_unmute(struct snd_kcontrol
*slave
,
1890 struct snd_kcontrol
*kctl
,
1893 return put_kctl_with_value(slave
, 1);
1896 static int add_slave(struct hda_codec
*codec
,
1897 void *data
, struct snd_kcontrol
*slave
)
1899 return snd_ctl_add_slave(data
, slave
);
1903 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1904 * @codec: HD-audio codec
1905 * @name: vmaster control name
1906 * @tlv: TLV data (optional)
1907 * @slaves: slave control names (optional)
1908 * @suffix: suffix string to each slave name (optional)
1909 * @init_slave_vol: initialize slaves to unmute/0dB
1910 * @ctl_ret: store the vmaster kcontrol in return
1912 * Create a virtual master control with the given name. The TLV data
1913 * must be either NULL or a valid data.
1915 * @slaves is a NULL-terminated array of strings, each of which is a
1916 * slave control name. All controls with these names are assigned to
1917 * the new virtual master control.
1919 * This function returns zero if successful or a negative error code.
1921 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
1922 unsigned int *tlv
, const char * const *slaves
,
1923 const char *suffix
, bool init_slave_vol
,
1924 struct snd_kcontrol
**ctl_ret
)
1926 struct snd_kcontrol
*kctl
;
1932 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
1934 codec_dbg(codec
, "No slave found for %s\n", name
);
1937 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
1940 err
= snd_hda_ctl_add(codec
, 0, kctl
);
1944 err
= map_slaves(codec
, slaves
, suffix
, add_slave
, kctl
);
1948 /* init with master mute & zero volume */
1949 put_kctl_with_value(kctl
, 0);
1950 if (init_slave_vol
) {
1951 struct slave_init_arg arg
= {
1955 snd_ctl_apply_vmaster_slaves(kctl
,
1956 tlv
? init_slave_0dB
: init_slave_unmute
,
1964 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster
);
1967 * mute-LED control using vmaster
1969 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
1970 struct snd_ctl_elem_info
*uinfo
)
1972 static const char * const texts
[] = {
1973 "On", "Off", "Follow Master"
1976 return snd_ctl_enum_info(uinfo
, 1, 3, texts
);
1979 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
1980 struct snd_ctl_elem_value
*ucontrol
)
1982 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
1983 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
1987 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
1988 struct snd_ctl_elem_value
*ucontrol
)
1990 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
1991 unsigned int old_mode
= hook
->mute_mode
;
1993 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
1994 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
1995 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
1996 if (old_mode
== hook
->mute_mode
)
1998 snd_hda_sync_vmaster_hook(hook
);
2002 static const struct snd_kcontrol_new vmaster_mute_mode
= {
2003 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2004 .name
= "Mute-LED Mode",
2005 .info
= vmaster_mute_mode_info
,
2006 .get
= vmaster_mute_mode_get
,
2007 .put
= vmaster_mute_mode_put
,
2010 /* meta hook to call each driver's vmaster hook */
2011 static void vmaster_hook(void *private_data
, int enabled
)
2013 struct hda_vmaster_mute_hook
*hook
= private_data
;
2015 if (hook
->mute_mode
!= HDA_VMUTE_FOLLOW_MASTER
)
2016 enabled
= hook
->mute_mode
;
2017 hook
->hook(hook
->codec
, enabled
);
2021 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2022 * @codec: the HDA codec
2023 * @hook: the vmaster hook object
2024 * @expose_enum_ctl: flag to create an enum ctl
2026 * Add a mute-LED hook with the given vmaster switch kctl.
2027 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2028 * created and associated with the given hook.
2030 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
2031 struct hda_vmaster_mute_hook
*hook
,
2032 bool expose_enum_ctl
)
2034 struct snd_kcontrol
*kctl
;
2036 if (!hook
->hook
|| !hook
->sw_kctl
)
2038 hook
->codec
= codec
;
2039 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2040 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, vmaster_hook
, hook
);
2041 if (!expose_enum_ctl
)
2043 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
2046 return snd_hda_ctl_add(codec
, 0, kctl
);
2048 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook
);
2051 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2052 * @hook: the vmaster hook
2054 * Call the hook with the current value for synchronization.
2055 * Should be called in init callback.
2057 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
2059 if (!hook
->hook
|| !hook
->codec
)
2061 /* don't call vmaster hook in the destructor since it might have
2062 * been already destroyed
2064 if (hook
->codec
->bus
->shutdown
)
2066 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
2068 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook
);
2072 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2073 * @kcontrol: referred ctl element
2074 * @uinfo: pointer to get/store the data
2076 * The control element is supposed to have the private_value field
2077 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2079 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2080 struct snd_ctl_elem_info
*uinfo
)
2082 int chs
= get_amp_channels(kcontrol
);
2084 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2085 uinfo
->count
= chs
== 3 ? 2 : 1;
2086 uinfo
->value
.integer
.min
= 0;
2087 uinfo
->value
.integer
.max
= 1;
2090 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info
);
2093 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2094 * @kcontrol: ctl element
2095 * @ucontrol: pointer to get/store the data
2097 * The control element is supposed to have the private_value field
2098 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2100 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2101 struct snd_ctl_elem_value
*ucontrol
)
2103 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2104 hda_nid_t nid
= get_amp_nid(kcontrol
);
2105 int chs
= get_amp_channels(kcontrol
);
2106 int dir
= get_amp_direction(kcontrol
);
2107 int idx
= get_amp_index(kcontrol
);
2108 long *valp
= ucontrol
->value
.integer
.value
;
2111 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2112 HDA_AMP_MUTE
) ? 0 : 1;
2114 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2115 HDA_AMP_MUTE
) ? 0 : 1;
2118 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get
);
2121 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2122 * @kcontrol: ctl element
2123 * @ucontrol: pointer to get/store the data
2125 * The control element is supposed to have the private_value field
2126 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2128 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2129 struct snd_ctl_elem_value
*ucontrol
)
2131 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2132 hda_nid_t nid
= get_amp_nid(kcontrol
);
2133 int chs
= get_amp_channels(kcontrol
);
2134 int dir
= get_amp_direction(kcontrol
);
2135 int idx
= get_amp_index(kcontrol
);
2136 long *valp
= ucontrol
->value
.integer
.value
;
2140 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2142 *valp
? 0 : HDA_AMP_MUTE
);
2146 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2148 *valp
? 0 : HDA_AMP_MUTE
);
2149 hda_call_check_power_status(codec
, nid
);
2152 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put
);
2155 * SPDIF out controls
2158 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2159 struct snd_ctl_elem_info
*uinfo
)
2161 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2166 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2167 struct snd_ctl_elem_value
*ucontrol
)
2169 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2170 IEC958_AES0_NONAUDIO
|
2171 IEC958_AES0_CON_EMPHASIS_5015
|
2172 IEC958_AES0_CON_NOT_COPYRIGHT
;
2173 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2174 IEC958_AES1_CON_ORIGINAL
;
2178 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2179 struct snd_ctl_elem_value
*ucontrol
)
2181 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2182 IEC958_AES0_NONAUDIO
|
2183 IEC958_AES0_PRO_EMPHASIS_5015
;
2187 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2188 struct snd_ctl_elem_value
*ucontrol
)
2190 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2191 int idx
= kcontrol
->private_value
;
2192 struct hda_spdif_out
*spdif
;
2194 if (WARN_ON(codec
->spdif_out
.used
<= idx
))
2196 mutex_lock(&codec
->spdif_mutex
);
2197 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2198 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
2199 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
2200 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
2201 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
2202 mutex_unlock(&codec
->spdif_mutex
);
2207 /* convert from SPDIF status bits to HDA SPDIF bits
2208 * bit 0 (DigEn) is always set zero (to be filled later)
2210 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2212 unsigned short val
= 0;
2214 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2215 val
|= AC_DIG1_PROFESSIONAL
;
2216 if (sbits
& IEC958_AES0_NONAUDIO
)
2217 val
|= AC_DIG1_NONAUDIO
;
2218 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2219 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2220 IEC958_AES0_PRO_EMPHASIS_5015
)
2221 val
|= AC_DIG1_EMPHASIS
;
2223 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2224 IEC958_AES0_CON_EMPHASIS_5015
)
2225 val
|= AC_DIG1_EMPHASIS
;
2226 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2227 val
|= AC_DIG1_COPYRIGHT
;
2228 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2229 val
|= AC_DIG1_LEVEL
;
2230 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2235 /* convert to SPDIF status bits from HDA SPDIF bits
2237 static unsigned int convert_to_spdif_status(unsigned short val
)
2239 unsigned int sbits
= 0;
2241 if (val
& AC_DIG1_NONAUDIO
)
2242 sbits
|= IEC958_AES0_NONAUDIO
;
2243 if (val
& AC_DIG1_PROFESSIONAL
)
2244 sbits
|= IEC958_AES0_PROFESSIONAL
;
2245 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2246 if (val
& AC_DIG1_EMPHASIS
)
2247 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
2249 if (val
& AC_DIG1_EMPHASIS
)
2250 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
2251 if (!(val
& AC_DIG1_COPYRIGHT
))
2252 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
2253 if (val
& AC_DIG1_LEVEL
)
2254 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
2255 sbits
|= val
& (0x7f << 8);
2260 /* set digital convert verbs both for the given NID and its slaves */
2261 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
2266 snd_hdac_regmap_update(&codec
->core
, nid
, AC_VERB_SET_DIGI_CONVERT_1
,
2268 d
= codec
->slave_dig_outs
;
2272 snd_hdac_regmap_update(&codec
->core
, *d
,
2273 AC_VERB_SET_DIGI_CONVERT_1
, mask
, val
);
2276 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
2279 unsigned int mask
= 0;
2280 unsigned int val
= 0;
2290 set_dig_out(codec
, nid
, mask
, val
);
2293 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
2294 struct snd_ctl_elem_value
*ucontrol
)
2296 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2297 int idx
= kcontrol
->private_value
;
2298 struct hda_spdif_out
*spdif
;
2303 if (WARN_ON(codec
->spdif_out
.used
<= idx
))
2305 mutex_lock(&codec
->spdif_mutex
);
2306 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2308 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
2309 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
2310 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
2311 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
2312 val
= convert_from_spdif_status(spdif
->status
);
2313 val
|= spdif
->ctls
& 1;
2314 change
= spdif
->ctls
!= val
;
2316 if (change
&& nid
!= (u16
)-1)
2317 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2318 mutex_unlock(&codec
->spdif_mutex
);
2322 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2324 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
2325 struct snd_ctl_elem_value
*ucontrol
)
2327 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2328 int idx
= kcontrol
->private_value
;
2329 struct hda_spdif_out
*spdif
;
2331 if (WARN_ON(codec
->spdif_out
.used
<= idx
))
2333 mutex_lock(&codec
->spdif_mutex
);
2334 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2335 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
2336 mutex_unlock(&codec
->spdif_mutex
);
2340 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
2343 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
2344 /* unmute amp switch (if any) */
2345 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
2346 (dig1
& AC_DIG1_ENABLE
))
2347 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
2351 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
2352 struct snd_ctl_elem_value
*ucontrol
)
2354 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2355 int idx
= kcontrol
->private_value
;
2356 struct hda_spdif_out
*spdif
;
2361 if (WARN_ON(codec
->spdif_out
.used
<= idx
))
2363 mutex_lock(&codec
->spdif_mutex
);
2364 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2366 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
2367 if (ucontrol
->value
.integer
.value
[0])
2368 val
|= AC_DIG1_ENABLE
;
2369 change
= spdif
->ctls
!= val
;
2371 if (change
&& nid
!= (u16
)-1)
2372 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
2373 mutex_unlock(&codec
->spdif_mutex
);
2377 static struct snd_kcontrol_new dig_mixes
[] = {
2379 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2380 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2381 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
2382 .info
= snd_hda_spdif_mask_info
,
2383 .get
= snd_hda_spdif_cmask_get
,
2386 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2387 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2388 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
2389 .info
= snd_hda_spdif_mask_info
,
2390 .get
= snd_hda_spdif_pmask_get
,
2393 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2394 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
2395 .info
= snd_hda_spdif_mask_info
,
2396 .get
= snd_hda_spdif_default_get
,
2397 .put
= snd_hda_spdif_default_put
,
2400 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2401 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
2402 .info
= snd_hda_spdif_out_switch_info
,
2403 .get
= snd_hda_spdif_out_switch_get
,
2404 .put
= snd_hda_spdif_out_switch_put
,
2410 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2411 * @codec: the HDA codec
2412 * @associated_nid: NID that new ctls associated with
2413 * @cvt_nid: converter NID
2414 * @type: HDA_PCM_TYPE_*
2415 * Creates controls related with the digital output.
2416 * Called from each patch supporting the digital out.
2418 * Returns 0 if successful, or a negative error code.
2420 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
2421 hda_nid_t associated_nid
,
2426 struct snd_kcontrol
*kctl
;
2427 struct snd_kcontrol_new
*dig_mix
;
2430 const int spdif_index
= 16;
2431 struct hda_spdif_out
*spdif
;
2432 struct hda_bus
*bus
= codec
->bus
;
2434 if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
2435 type
== HDA_PCM_TYPE_SPDIF
) {
2437 } else if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
2438 type
== HDA_PCM_TYPE_HDMI
) {
2439 /* suppose a single SPDIF device */
2440 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2441 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, 0);
2444 kctl
->id
.index
= spdif_index
;
2446 bus
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
2448 if (!bus
->primary_dig_out_type
)
2449 bus
->primary_dig_out_type
= type
;
2451 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", idx
);
2453 codec_err(codec
, "too many IEC958 outputs\n");
2456 spdif
= snd_array_new(&codec
->spdif_out
);
2459 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2460 kctl
= snd_ctl_new1(dig_mix
, codec
);
2463 kctl
->id
.index
= idx
;
2464 kctl
->private_value
= codec
->spdif_out
.used
- 1;
2465 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
2469 spdif
->nid
= cvt_nid
;
2470 snd_hdac_regmap_read(&codec
->core
, cvt_nid
,
2471 AC_VERB_GET_DIGI_CONVERT_1
, &val
);
2473 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
2476 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls
);
2479 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2480 * @codec: the HDA codec
2483 * call within spdif_mutex lock
2485 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
2488 struct hda_spdif_out
*spdif
;
2491 snd_array_for_each(&codec
->spdif_out
, i
, spdif
) {
2492 if (spdif
->nid
== nid
)
2497 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid
);
2500 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2501 * @codec: the HDA codec
2502 * @idx: the SPDIF ctl index
2504 * Unassign the widget from the given SPDIF control.
2506 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
2508 struct hda_spdif_out
*spdif
;
2510 if (WARN_ON(codec
->spdif_out
.used
<= idx
))
2512 mutex_lock(&codec
->spdif_mutex
);
2513 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2514 spdif
->nid
= (u16
)-1;
2515 mutex_unlock(&codec
->spdif_mutex
);
2517 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign
);
2520 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2521 * @codec: the HDA codec
2522 * @idx: the SPDIF ctl idx
2525 * Assign the widget to the SPDIF control with the given index.
2527 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
2529 struct hda_spdif_out
*spdif
;
2532 if (WARN_ON(codec
->spdif_out
.used
<= idx
))
2534 mutex_lock(&codec
->spdif_mutex
);
2535 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2536 if (spdif
->nid
!= nid
) {
2539 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2541 mutex_unlock(&codec
->spdif_mutex
);
2543 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign
);
2546 * SPDIF sharing with analog output
2548 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
2549 struct snd_ctl_elem_value
*ucontrol
)
2551 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2552 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
2556 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
2557 struct snd_ctl_elem_value
*ucontrol
)
2559 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2560 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
2564 static const struct snd_kcontrol_new spdif_share_sw
= {
2565 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2566 .name
= "IEC958 Default PCM Playback Switch",
2567 .info
= snd_ctl_boolean_mono_info
,
2568 .get
= spdif_share_sw_get
,
2569 .put
= spdif_share_sw_put
,
2573 * snd_hda_create_spdif_share_sw - create Default PCM switch
2574 * @codec: the HDA codec
2575 * @mout: multi-out instance
2577 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
2578 struct hda_multi_out
*mout
)
2580 struct snd_kcontrol
*kctl
;
2582 if (!mout
->dig_out_nid
)
2585 kctl
= snd_ctl_new1(&spdif_share_sw
, mout
);
2588 /* ATTENTION: here mout is passed as private_data, instead of codec */
2589 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
, kctl
);
2591 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw
);
2597 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2599 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
2600 struct snd_ctl_elem_value
*ucontrol
)
2602 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2604 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
2608 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
2609 struct snd_ctl_elem_value
*ucontrol
)
2611 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2612 hda_nid_t nid
= kcontrol
->private_value
;
2613 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
2616 mutex_lock(&codec
->spdif_mutex
);
2617 change
= codec
->spdif_in_enable
!= val
;
2619 codec
->spdif_in_enable
= val
;
2620 snd_hdac_regmap_write(&codec
->core
, nid
,
2621 AC_VERB_SET_DIGI_CONVERT_1
, val
);
2623 mutex_unlock(&codec
->spdif_mutex
);
2627 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
2628 struct snd_ctl_elem_value
*ucontrol
)
2630 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2631 hda_nid_t nid
= kcontrol
->private_value
;
2635 snd_hdac_regmap_read(&codec
->core
, nid
,
2636 AC_VERB_GET_DIGI_CONVERT_1
, &val
);
2637 sbits
= convert_to_spdif_status(val
);
2638 ucontrol
->value
.iec958
.status
[0] = sbits
;
2639 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
2640 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
2641 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
2645 static struct snd_kcontrol_new dig_in_ctls
[] = {
2647 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2648 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
2649 .info
= snd_hda_spdif_in_switch_info
,
2650 .get
= snd_hda_spdif_in_switch_get
,
2651 .put
= snd_hda_spdif_in_switch_put
,
2654 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2655 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2656 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
2657 .info
= snd_hda_spdif_mask_info
,
2658 .get
= snd_hda_spdif_in_status_get
,
2664 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2665 * @codec: the HDA codec
2666 * @nid: audio in widget NID
2668 * Creates controls related with the SPDIF input.
2669 * Called from each patch supporting the SPDIF in.
2671 * Returns 0 if successful, or a negative error code.
2673 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
2676 struct snd_kcontrol
*kctl
;
2677 struct snd_kcontrol_new
*dig_mix
;
2680 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
2682 codec_err(codec
, "too many IEC958 inputs\n");
2685 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
2686 kctl
= snd_ctl_new1(dig_mix
, codec
);
2689 kctl
->private_value
= nid
;
2690 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
2694 codec
->spdif_in_enable
=
2695 snd_hda_codec_read(codec
, nid
, 0,
2696 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
2700 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls
);
2703 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2704 * @codec: the HDA codec
2705 * @fg: function group (not used now)
2706 * @power_state: the power state to set (AC_PWRST_*)
2708 * Set the given power state to all widgets that have the power control.
2709 * If the codec has power_filter set, it evaluates the power state and
2710 * filter out if it's unchanged as D3.
2712 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
2713 unsigned int power_state
)
2717 for_each_hda_codec_node(nid
, codec
) {
2718 unsigned int wcaps
= get_wcaps(codec
, nid
);
2719 unsigned int state
= power_state
;
2720 if (!(wcaps
& AC_WCAP_POWER
))
2722 if (codec
->power_filter
) {
2723 state
= codec
->power_filter(codec
, nid
, power_state
);
2724 if (state
!= power_state
&& power_state
== AC_PWRST_D3
)
2727 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
2731 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all
);
2734 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2735 * @codec: the HDA codec
2737 * @power_state: power state to evalue
2739 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2740 * This can be used a codec power_filter callback.
2742 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec
*codec
,
2744 unsigned int power_state
)
2746 if (nid
== codec
->core
.afg
|| nid
== codec
->core
.mfg
)
2748 if (power_state
== AC_PWRST_D3
&&
2749 get_wcaps_type(get_wcaps(codec
, nid
)) == AC_WID_PIN
&&
2750 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
2751 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
2752 AC_VERB_GET_EAPD_BTLENABLE
, 0);
2758 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter
);
2761 * set power state of the codec, and return the power state
2763 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
2764 unsigned int power_state
)
2766 hda_nid_t fg
= codec
->core
.afg
? codec
->core
.afg
: codec
->core
.mfg
;
2771 /* this delay seems necessary to avoid click noise at power-down */
2772 if (power_state
== AC_PWRST_D3
) {
2773 if (codec
->depop_delay
< 0)
2774 msleep(codec_has_epss(codec
) ? 10 : 100);
2775 else if (codec
->depop_delay
> 0)
2776 msleep(codec
->depop_delay
);
2777 flags
= HDA_RW_NO_RESPONSE_FALLBACK
;
2780 /* repeat power states setting at most 10 times*/
2781 for (count
= 0; count
< 10; count
++) {
2782 if (codec
->patch_ops
.set_power_state
)
2783 codec
->patch_ops
.set_power_state(codec
, fg
,
2786 state
= power_state
;
2787 if (codec
->power_filter
)
2788 state
= codec
->power_filter(codec
, fg
, state
);
2789 if (state
== power_state
|| power_state
!= AC_PWRST_D3
)
2790 snd_hda_codec_read(codec
, fg
, flags
,
2791 AC_VERB_SET_POWER_STATE
,
2793 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
);
2795 state
= snd_hda_sync_power_state(codec
, fg
, power_state
);
2796 if (!(state
& AC_PWRST_ERROR
))
2803 /* sync power states of all widgets;
2804 * this is called at the end of codec parsing
2806 static void sync_power_up_states(struct hda_codec
*codec
)
2810 /* don't care if no filter is used */
2811 if (!codec
->power_filter
)
2814 for_each_hda_codec_node(nid
, codec
) {
2815 unsigned int wcaps
= get_wcaps(codec
, nid
);
2816 unsigned int target
;
2817 if (!(wcaps
& AC_WCAP_POWER
))
2819 target
= codec
->power_filter(codec
, nid
, AC_PWRST_D0
);
2820 if (target
== AC_PWRST_D0
)
2822 if (!snd_hda_check_power_state(codec
, nid
, target
))
2823 snd_hda_codec_write(codec
, nid
, 0,
2824 AC_VERB_SET_POWER_STATE
, target
);
2828 #ifdef CONFIG_SND_HDA_RECONFIG
2829 /* execute additional init verbs */
2830 static void hda_exec_init_verbs(struct hda_codec
*codec
)
2832 if (codec
->init_verbs
.list
)
2833 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
2836 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
2840 /* update the power on/off account with the current jiffies */
2841 static void update_power_acct(struct hda_codec
*codec
, bool on
)
2843 unsigned long delta
= jiffies
- codec
->power_jiffies
;
2846 codec
->power_on_acct
+= delta
;
2848 codec
->power_off_acct
+= delta
;
2849 codec
->power_jiffies
+= delta
;
2852 void snd_hda_update_power_acct(struct hda_codec
*codec
)
2854 update_power_acct(codec
, hda_codec_is_power_on(codec
));
2858 * call suspend and power-down; used both from PM and power-save
2859 * this function returns the power state in the end
2861 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
)
2865 snd_hdac_enter_pm(&codec
->core
);
2866 if (codec
->patch_ops
.suspend
)
2867 codec
->patch_ops
.suspend(codec
);
2868 hda_cleanup_all_streams(codec
);
2869 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
2870 update_power_acct(codec
, true);
2871 snd_hdac_leave_pm(&codec
->core
);
2876 * kick up codec; used both from PM and power-save
2878 static void hda_call_codec_resume(struct hda_codec
*codec
)
2880 snd_hdac_enter_pm(&codec
->core
);
2881 if (codec
->core
.regmap
)
2882 regcache_mark_dirty(codec
->core
.regmap
);
2884 codec
->power_jiffies
= jiffies
;
2886 hda_set_power_state(codec
, AC_PWRST_D0
);
2887 restore_shutup_pins(codec
);
2888 hda_exec_init_verbs(codec
);
2889 snd_hda_jack_set_dirty_all(codec
);
2890 if (codec
->patch_ops
.resume
)
2891 codec
->patch_ops
.resume(codec
);
2893 if (codec
->patch_ops
.init
)
2894 codec
->patch_ops
.init(codec
);
2895 if (codec
->core
.regmap
)
2896 regcache_sync(codec
->core
.regmap
);
2899 if (codec
->jackpoll_interval
)
2900 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
2902 snd_hda_jack_report_sync(codec
);
2903 codec
->core
.dev
.power
.power_state
= PMSG_ON
;
2904 snd_hdac_leave_pm(&codec
->core
);
2907 static int hda_codec_runtime_suspend(struct device
*dev
)
2909 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
2912 cancel_delayed_work_sync(&codec
->jackpoll_work
);
2913 state
= hda_call_codec_suspend(codec
);
2914 if (codec
->link_down_at_suspend
||
2915 (codec_has_clkstop(codec
) && codec_has_epss(codec
) &&
2916 (state
& AC_PWRST_CLK_STOP_OK
)))
2917 snd_hdac_codec_link_down(&codec
->core
);
2918 codec_display_power(codec
, false);
2922 static int hda_codec_runtime_resume(struct device
*dev
)
2924 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
2926 codec_display_power(codec
, true);
2927 snd_hdac_codec_link_up(&codec
->core
);
2928 hda_call_codec_resume(codec
);
2929 pm_runtime_mark_last_busy(dev
);
2932 #endif /* CONFIG_PM */
2934 #ifdef CONFIG_PM_SLEEP
2935 static int hda_codec_force_resume(struct device
*dev
)
2939 /* The get/put pair below enforces the runtime resume even if the
2940 * device hasn't been used at suspend time. This trick is needed to
2941 * update the jack state change during the sleep.
2943 pm_runtime_get_noresume(dev
);
2944 ret
= pm_runtime_force_resume(dev
);
2945 pm_runtime_put(dev
);
2949 static int hda_codec_pm_suspend(struct device
*dev
)
2951 dev
->power
.power_state
= PMSG_SUSPEND
;
2952 return pm_runtime_force_suspend(dev
);
2955 static int hda_codec_pm_resume(struct device
*dev
)
2957 dev
->power
.power_state
= PMSG_RESUME
;
2958 return hda_codec_force_resume(dev
);
2961 static int hda_codec_pm_freeze(struct device
*dev
)
2963 dev
->power
.power_state
= PMSG_FREEZE
;
2964 return pm_runtime_force_suspend(dev
);
2967 static int hda_codec_pm_thaw(struct device
*dev
)
2969 dev
->power
.power_state
= PMSG_THAW
;
2970 return hda_codec_force_resume(dev
);
2973 static int hda_codec_pm_restore(struct device
*dev
)
2975 dev
->power
.power_state
= PMSG_RESTORE
;
2976 return hda_codec_force_resume(dev
);
2978 #endif /* CONFIG_PM_SLEEP */
2980 /* referred in hda_bind.c */
2981 const struct dev_pm_ops hda_codec_driver_pm
= {
2982 #ifdef CONFIG_PM_SLEEP
2983 .suspend
= hda_codec_pm_suspend
,
2984 .resume
= hda_codec_pm_resume
,
2985 .freeze
= hda_codec_pm_freeze
,
2986 .thaw
= hda_codec_pm_thaw
,
2987 .poweroff
= hda_codec_pm_suspend
,
2988 .restore
= hda_codec_pm_restore
,
2989 #endif /* CONFIG_PM_SLEEP */
2990 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend
, hda_codec_runtime_resume
,
2995 * add standard channel maps if not specified
2997 static int add_std_chmaps(struct hda_codec
*codec
)
2999 struct hda_pcm
*pcm
;
3002 list_for_each_entry(pcm
, &codec
->pcm_list_head
, list
) {
3003 for (str
= 0; str
< 2; str
++) {
3004 struct hda_pcm_stream
*hinfo
= &pcm
->stream
[str
];
3005 struct snd_pcm_chmap
*chmap
;
3006 const struct snd_pcm_chmap_elem
*elem
;
3008 if (!pcm
->pcm
|| pcm
->own_chmap
|| !hinfo
->substreams
)
3010 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
3011 err
= snd_pcm_add_chmap_ctls(pcm
->pcm
, str
, elem
,
3012 hinfo
->channels_max
,
3016 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
3022 /* default channel maps for 2.1 speakers;
3023 * since HD-audio supports only stereo, odd number channels are omitted
3025 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
3027 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
3029 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
3030 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
3033 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
3035 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
3038 hda_exec_init_verbs(codec
);
3039 /* continue to initialize... */
3040 if (codec
->patch_ops
.init
)
3041 err
= codec
->patch_ops
.init(codec
);
3042 if (!err
&& codec
->patch_ops
.build_controls
)
3043 err
= codec
->patch_ops
.build_controls(codec
);
3047 /* we create chmaps here instead of build_pcms */
3048 err
= add_std_chmaps(codec
);
3052 if (codec
->jackpoll_interval
)
3053 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3055 snd_hda_jack_report_sync(codec
); /* call at the last init point */
3056 sync_power_up_states(codec
);
3059 EXPORT_SYMBOL_GPL(snd_hda_codec_build_controls
);
3064 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
3065 struct hda_codec
*codec
,
3066 struct snd_pcm_substream
*substream
)
3071 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
3072 struct hda_codec
*codec
,
3073 unsigned int stream_tag
,
3074 unsigned int format
,
3075 struct snd_pcm_substream
*substream
)
3077 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3081 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
3082 struct hda_codec
*codec
,
3083 struct snd_pcm_substream
*substream
)
3085 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3089 static int set_pcm_default_values(struct hda_codec
*codec
,
3090 struct hda_pcm_stream
*info
)
3094 /* query support PCM information from the given NID */
3095 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
3096 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
3097 info
->rates
? NULL
: &info
->rates
,
3098 info
->formats
? NULL
: &info
->formats
,
3099 info
->maxbps
? NULL
: &info
->maxbps
);
3103 if (info
->ops
.open
== NULL
)
3104 info
->ops
.open
= hda_pcm_default_open_close
;
3105 if (info
->ops
.close
== NULL
)
3106 info
->ops
.close
= hda_pcm_default_open_close
;
3107 if (info
->ops
.prepare
== NULL
) {
3108 if (snd_BUG_ON(!info
->nid
))
3110 info
->ops
.prepare
= hda_pcm_default_prepare
;
3112 if (info
->ops
.cleanup
== NULL
) {
3113 if (snd_BUG_ON(!info
->nid
))
3115 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
3121 * codec prepare/cleanup entries
3124 * snd_hda_codec_prepare - Prepare a stream
3125 * @codec: the HDA codec
3126 * @hinfo: PCM information
3127 * @stream: stream tag to assign
3128 * @format: format id to assign
3129 * @substream: PCM substream to assign
3131 * Calls the prepare callback set by the codec with the given arguments.
3132 * Clean up the inactive streams when successful.
3134 int snd_hda_codec_prepare(struct hda_codec
*codec
,
3135 struct hda_pcm_stream
*hinfo
,
3136 unsigned int stream
,
3137 unsigned int format
,
3138 struct snd_pcm_substream
*substream
)
3141 mutex_lock(&codec
->bus
->prepare_mutex
);
3142 if (hinfo
->ops
.prepare
)
3143 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
,
3148 purify_inactive_streams(codec
);
3149 mutex_unlock(&codec
->bus
->prepare_mutex
);
3152 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare
);
3155 * snd_hda_codec_cleanup - Prepare a stream
3156 * @codec: the HDA codec
3157 * @hinfo: PCM information
3158 * @substream: PCM substream
3160 * Calls the cleanup callback set by the codec with the given arguments.
3162 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
3163 struct hda_pcm_stream
*hinfo
,
3164 struct snd_pcm_substream
*substream
)
3166 mutex_lock(&codec
->bus
->prepare_mutex
);
3167 if (hinfo
->ops
.cleanup
)
3168 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
3169 mutex_unlock(&codec
->bus
->prepare_mutex
);
3171 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup
);
3174 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
3175 "Audio", "SPDIF", "HDMI", "Modem"
3179 * get the empty PCM device number to assign
3181 static int get_empty_pcm_device(struct hda_bus
*bus
, unsigned int type
)
3183 /* audio device indices; not linear to keep compatibility */
3184 /* assigned to static slots up to dev#10; if more needed, assign
3185 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3187 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
3188 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
3189 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
3190 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
3191 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
3195 if (type
>= HDA_PCM_NTYPES
) {
3196 dev_err(bus
->card
->dev
, "Invalid PCM type %d\n", type
);
3200 for (i
= 0; audio_idx
[type
][i
] >= 0; i
++) {
3201 #ifndef CONFIG_SND_DYNAMIC_MINORS
3202 if (audio_idx
[type
][i
] >= 8)
3205 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
3206 return audio_idx
[type
][i
];
3209 #ifdef CONFIG_SND_DYNAMIC_MINORS
3210 /* non-fixed slots starting from 10 */
3211 for (i
= 10; i
< 32; i
++) {
3212 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
3217 dev_warn(bus
->card
->dev
, "Too many %s devices\n",
3218 snd_hda_pcm_type_name
[type
]);
3219 #ifndef CONFIG_SND_DYNAMIC_MINORS
3220 dev_warn(bus
->card
->dev
,
3221 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3226 /* call build_pcms ops of the given codec and set up the default parameters */
3227 int snd_hda_codec_parse_pcms(struct hda_codec
*codec
)
3229 struct hda_pcm
*cpcm
;
3232 if (!list_empty(&codec
->pcm_list_head
))
3233 return 0; /* already parsed */
3235 if (!codec
->patch_ops
.build_pcms
)
3238 err
= codec
->patch_ops
.build_pcms(codec
);
3240 codec_err(codec
, "cannot build PCMs for #%d (error %d)\n",
3241 codec
->core
.addr
, err
);
3245 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
3248 for (stream
= 0; stream
< 2; stream
++) {
3249 struct hda_pcm_stream
*info
= &cpcm
->stream
[stream
];
3251 if (!info
->substreams
)
3253 err
= set_pcm_default_values(codec
, info
);
3256 "fail to setup default for PCM %s\n",
3265 EXPORT_SYMBOL_GPL(snd_hda_codec_parse_pcms
);
3267 /* assign all PCMs of the given codec */
3268 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
3270 struct hda_bus
*bus
= codec
->bus
;
3271 struct hda_pcm
*cpcm
;
3274 err
= snd_hda_codec_parse_pcms(codec
);
3278 /* attach a new PCM streams */
3279 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
3281 continue; /* already attached */
3282 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
3283 continue; /* no substreams assigned */
3285 dev
= get_empty_pcm_device(bus
, cpcm
->pcm_type
);
3287 cpcm
->device
= SNDRV_PCM_INVALID_DEVICE
;
3288 continue; /* no fatal error */
3291 err
= snd_hda_attach_pcm_stream(bus
, codec
, cpcm
);
3294 "cannot attach PCM stream %d for codec #%d\n",
3295 dev
, codec
->core
.addr
);
3296 continue; /* no fatal error */
3304 * snd_hda_add_new_ctls - create controls from the array
3305 * @codec: the HDA codec
3306 * @knew: the array of struct snd_kcontrol_new
3308 * This helper function creates and add new controls in the given array.
3309 * The array must be terminated with an empty entry as terminator.
3311 * Returns 0 if successful, or a negative error code.
3313 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
3314 const struct snd_kcontrol_new
*knew
)
3318 for (; knew
->name
; knew
++) {
3319 struct snd_kcontrol
*kctl
;
3320 int addr
= 0, idx
= 0;
3321 if (knew
->iface
== (__force snd_ctl_elem_iface_t
)-1)
3322 continue; /* skip this codec private value */
3324 kctl
= snd_ctl_new1(knew
, codec
);
3328 kctl
->id
.device
= addr
;
3330 kctl
->id
.index
= idx
;
3331 err
= snd_hda_ctl_add(codec
, 0, kctl
);
3334 /* try first with another device index corresponding to
3335 * the codec addr; if it still fails (or it's the
3336 * primary codec), then try another control index
3338 if (!addr
&& codec
->core
.addr
)
3339 addr
= codec
->core
.addr
;
3340 else if (!idx
&& !knew
->index
) {
3341 idx
= find_empty_mixer_ctl_idx(codec
,
3351 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls
);
3354 static void codec_set_power_save(struct hda_codec
*codec
, int delay
)
3356 struct device
*dev
= hda_codec_dev(codec
);
3358 if (delay
== 0 && codec
->auto_runtime_pm
)
3362 pm_runtime_set_autosuspend_delay(dev
, delay
);
3363 pm_runtime_use_autosuspend(dev
);
3364 pm_runtime_allow(dev
);
3365 if (!pm_runtime_suspended(dev
))
3366 pm_runtime_mark_last_busy(dev
);
3368 pm_runtime_dont_use_autosuspend(dev
);
3369 pm_runtime_forbid(dev
);
3374 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3375 * @bus: HD-audio bus
3376 * @delay: autosuspend delay in msec, 0 = off
3378 * Synchronize the runtime PM autosuspend state from the power_save option.
3380 void snd_hda_set_power_save(struct hda_bus
*bus
, int delay
)
3382 struct hda_codec
*c
;
3384 list_for_each_codec(c
, bus
)
3385 codec_set_power_save(c
, delay
);
3387 EXPORT_SYMBOL_GPL(snd_hda_set_power_save
);
3390 * snd_hda_check_amp_list_power - Check the amp list and update the power
3391 * @codec: HD-audio codec
3392 * @check: the object containing an AMP list and the status
3393 * @nid: NID to check / update
3395 * Check whether the given NID is in the amp list. If it's in the list,
3396 * check the current AMP status, and update the the power-status according
3397 * to the mute status.
3399 * This function is supposed to be set or called from the check_power_status
3402 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
3403 struct hda_loopback_check
*check
,
3406 const struct hda_amp_list
*p
;
3409 if (!check
->amplist
)
3411 for (p
= check
->amplist
; p
->nid
; p
++) {
3416 return 0; /* nothing changed */
3418 for (p
= check
->amplist
; p
->nid
; p
++) {
3419 for (ch
= 0; ch
< 2; ch
++) {
3420 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
3422 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
3423 if (!check
->power_on
) {
3424 check
->power_on
= 1;
3425 snd_hda_power_up_pm(codec
);
3431 if (check
->power_on
) {
3432 check
->power_on
= 0;
3433 snd_hda_power_down_pm(codec
);
3437 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power
);
3445 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3446 * @imux: imux helper object
3447 * @uinfo: pointer to get/store the data
3449 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
3450 struct snd_ctl_elem_info
*uinfo
)
3454 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
3456 uinfo
->value
.enumerated
.items
= imux
->num_items
;
3457 if (!imux
->num_items
)
3459 index
= uinfo
->value
.enumerated
.item
;
3460 if (index
>= imux
->num_items
)
3461 index
= imux
->num_items
- 1;
3462 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
3465 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info
);
3468 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3469 * @codec: the HDA codec
3470 * @imux: imux helper object
3471 * @ucontrol: pointer to get/store the data
3472 * @nid: input mux NID
3473 * @cur_val: pointer to get/store the current imux value
3475 int snd_hda_input_mux_put(struct hda_codec
*codec
,
3476 const struct hda_input_mux
*imux
,
3477 struct snd_ctl_elem_value
*ucontrol
,
3479 unsigned int *cur_val
)
3483 if (!imux
->num_items
)
3485 idx
= ucontrol
->value
.enumerated
.item
[0];
3486 if (idx
>= imux
->num_items
)
3487 idx
= imux
->num_items
- 1;
3488 if (*cur_val
== idx
)
3490 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
3491 imux
->items
[idx
].index
);
3495 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put
);
3499 * snd_hda_enum_helper_info - Helper for simple enum ctls
3500 * @kcontrol: ctl element
3501 * @uinfo: pointer to get/store the data
3502 * @num_items: number of enum items
3503 * @texts: enum item string array
3505 * process kcontrol info callback of a simple string enum array
3506 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3508 int snd_hda_enum_helper_info(struct snd_kcontrol
*kcontrol
,
3509 struct snd_ctl_elem_info
*uinfo
,
3510 int num_items
, const char * const *texts
)
3512 static const char * const texts_default
[] = {
3513 "Disabled", "Enabled"
3516 if (!texts
|| !num_items
) {
3518 texts
= texts_default
;
3521 return snd_ctl_enum_info(uinfo
, 1, num_items
, texts
);
3523 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info
);
3526 * Multi-channel / digital-out PCM helper functions
3529 /* setup SPDIF output stream */
3530 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
3531 unsigned int stream_tag
, unsigned int format
)
3533 struct hda_spdif_out
*spdif
;
3534 unsigned int curr_fmt
;
3537 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
3538 /* Add sanity check to pass klockwork check.
3539 * This should never happen.
3541 if (WARN_ON(spdif
== NULL
))
3544 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
3545 AC_VERB_GET_STREAM_FORMAT
, 0);
3546 reset
= codec
->spdif_status_reset
&&
3547 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
3550 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3553 set_dig_out_convert(codec
, nid
,
3554 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
3556 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
3557 if (codec
->slave_dig_outs
) {
3559 for (d
= codec
->slave_dig_outs
; *d
; d
++)
3560 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
3563 /* turn on again (if needed) */
3565 set_dig_out_convert(codec
, nid
,
3566 spdif
->ctls
& 0xff, -1);
3569 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
3571 snd_hda_codec_cleanup_stream(codec
, nid
);
3572 if (codec
->slave_dig_outs
) {
3574 for (d
= codec
->slave_dig_outs
; *d
; d
++)
3575 snd_hda_codec_cleanup_stream(codec
, *d
);
3580 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3581 * @codec: the HDA codec
3582 * @mout: hda_multi_out object
3584 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
3585 struct hda_multi_out
*mout
)
3587 mutex_lock(&codec
->spdif_mutex
);
3588 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
3589 /* already opened as analog dup; reset it once */
3590 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3591 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
3592 mutex_unlock(&codec
->spdif_mutex
);
3595 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open
);
3598 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3599 * @codec: the HDA codec
3600 * @mout: hda_multi_out object
3601 * @stream_tag: stream tag to assign
3602 * @format: format id to assign
3603 * @substream: PCM substream to assign
3605 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
3606 struct hda_multi_out
*mout
,
3607 unsigned int stream_tag
,
3608 unsigned int format
,
3609 struct snd_pcm_substream
*substream
)
3611 mutex_lock(&codec
->spdif_mutex
);
3612 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
3613 mutex_unlock(&codec
->spdif_mutex
);
3616 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare
);
3619 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3620 * @codec: the HDA codec
3621 * @mout: hda_multi_out object
3623 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
3624 struct hda_multi_out
*mout
)
3626 mutex_lock(&codec
->spdif_mutex
);
3627 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3628 mutex_unlock(&codec
->spdif_mutex
);
3631 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup
);
3634 * snd_hda_multi_out_dig_close - release the digital out stream
3635 * @codec: the HDA codec
3636 * @mout: hda_multi_out object
3638 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
3639 struct hda_multi_out
*mout
)
3641 mutex_lock(&codec
->spdif_mutex
);
3642 mout
->dig_out_used
= 0;
3643 mutex_unlock(&codec
->spdif_mutex
);
3646 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close
);
3649 * snd_hda_multi_out_analog_open - open analog outputs
3650 * @codec: the HDA codec
3651 * @mout: hda_multi_out object
3652 * @substream: PCM substream to assign
3653 * @hinfo: PCM information to assign
3655 * Open analog outputs and set up the hw-constraints.
3656 * If the digital outputs can be opened as slave, open the digital
3659 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
3660 struct hda_multi_out
*mout
,
3661 struct snd_pcm_substream
*substream
,
3662 struct hda_pcm_stream
*hinfo
)
3664 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3665 runtime
->hw
.channels_max
= mout
->max_channels
;
3666 if (mout
->dig_out_nid
) {
3667 if (!mout
->analog_rates
) {
3668 mout
->analog_rates
= hinfo
->rates
;
3669 mout
->analog_formats
= hinfo
->formats
;
3670 mout
->analog_maxbps
= hinfo
->maxbps
;
3672 runtime
->hw
.rates
= mout
->analog_rates
;
3673 runtime
->hw
.formats
= mout
->analog_formats
;
3674 hinfo
->maxbps
= mout
->analog_maxbps
;
3676 if (!mout
->spdif_rates
) {
3677 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
3679 &mout
->spdif_formats
,
3680 &mout
->spdif_maxbps
);
3682 mutex_lock(&codec
->spdif_mutex
);
3683 if (mout
->share_spdif
) {
3684 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
3685 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
3686 runtime
->hw
.rates
&= mout
->spdif_rates
;
3687 runtime
->hw
.formats
&= mout
->spdif_formats
;
3688 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
3689 hinfo
->maxbps
= mout
->spdif_maxbps
;
3691 mout
->share_spdif
= 0;
3692 /* FIXME: need notify? */
3695 mutex_unlock(&codec
->spdif_mutex
);
3697 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
3698 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
3700 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open
);
3703 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3704 * @codec: the HDA codec
3705 * @mout: hda_multi_out object
3706 * @stream_tag: stream tag to assign
3707 * @format: format id to assign
3708 * @substream: PCM substream to assign
3710 * Set up the i/o for analog out.
3711 * When the digital out is available, copy the front out to digital out, too.
3713 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
3714 struct hda_multi_out
*mout
,
3715 unsigned int stream_tag
,
3716 unsigned int format
,
3717 struct snd_pcm_substream
*substream
)
3719 const hda_nid_t
*nids
= mout
->dac_nids
;
3720 int chs
= substream
->runtime
->channels
;
3721 struct hda_spdif_out
*spdif
;
3724 mutex_lock(&codec
->spdif_mutex
);
3725 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
3726 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
3727 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
3728 if (chs
== 2 && spdif
!= NULL
&&
3729 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
3731 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
3732 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
3733 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
3734 stream_tag
, format
);
3736 mout
->dig_out_used
= 0;
3737 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3740 mutex_unlock(&codec
->spdif_mutex
);
3743 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
3745 if (!mout
->no_share_stream
&&
3746 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
3747 /* headphone out will just decode front left/right (stereo) */
3748 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
3750 /* extra outputs copied from front */
3751 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
3752 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
3753 snd_hda_codec_setup_stream(codec
,
3754 mout
->hp_out_nid
[i
],
3755 stream_tag
, 0, format
);
3758 for (i
= 1; i
< mout
->num_dacs
; i
++) {
3759 if (chs
>= (i
+ 1) * 2) /* independent out */
3760 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
3762 else if (!mout
->no_share_stream
) /* copy front */
3763 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
3767 /* extra surrounds */
3768 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++) {
3770 if (!mout
->extra_out_nid
[i
])
3772 if (chs
>= (i
+ 1) * 2)
3774 else if (!mout
->no_share_stream
)
3776 snd_hda_codec_setup_stream(codec
, mout
->extra_out_nid
[i
],
3777 stream_tag
, ch
, format
);
3782 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare
);
3785 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3786 * @codec: the HDA codec
3787 * @mout: hda_multi_out object
3789 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
3790 struct hda_multi_out
*mout
)
3792 const hda_nid_t
*nids
= mout
->dac_nids
;
3795 for (i
= 0; i
< mout
->num_dacs
; i
++)
3796 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
3798 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
3799 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
3800 if (mout
->hp_out_nid
[i
])
3801 snd_hda_codec_cleanup_stream(codec
,
3802 mout
->hp_out_nid
[i
]);
3803 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
3804 if (mout
->extra_out_nid
[i
])
3805 snd_hda_codec_cleanup_stream(codec
,
3806 mout
->extra_out_nid
[i
]);
3807 mutex_lock(&codec
->spdif_mutex
);
3808 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
3809 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3810 mout
->dig_out_used
= 0;
3812 mutex_unlock(&codec
->spdif_mutex
);
3815 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup
);
3818 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3819 * @codec: the HDA codec
3820 * @pin: referred pin NID
3822 * Guess the suitable VREF pin bits to be set as the pin-control value.
3823 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3825 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
3827 unsigned int pincap
;
3828 unsigned int oldval
;
3829 oldval
= snd_hda_codec_read(codec
, pin
, 0,
3830 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
3831 pincap
= snd_hda_query_pin_caps(codec
, pin
);
3832 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
3833 /* Exception: if the default pin setup is vref50, we give it priority */
3834 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
3835 return AC_PINCTL_VREF_80
;
3836 else if (pincap
& AC_PINCAP_VREF_50
)
3837 return AC_PINCTL_VREF_50
;
3838 else if (pincap
& AC_PINCAP_VREF_100
)
3839 return AC_PINCTL_VREF_100
;
3840 else if (pincap
& AC_PINCAP_VREF_GRD
)
3841 return AC_PINCTL_VREF_GRD
;
3842 return AC_PINCTL_VREF_HIZ
;
3844 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref
);
3847 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3848 * @codec: the HDA codec
3849 * @pin: referred pin NID
3850 * @val: pin ctl value to audit
3852 unsigned int snd_hda_correct_pin_ctl(struct hda_codec
*codec
,
3853 hda_nid_t pin
, unsigned int val
)
3855 static unsigned int cap_lists
[][2] = {
3856 { AC_PINCTL_VREF_100
, AC_PINCAP_VREF_100
},
3857 { AC_PINCTL_VREF_80
, AC_PINCAP_VREF_80
},
3858 { AC_PINCTL_VREF_50
, AC_PINCAP_VREF_50
},
3859 { AC_PINCTL_VREF_GRD
, AC_PINCAP_VREF_GRD
},
3865 cap
= snd_hda_query_pin_caps(codec
, pin
);
3867 return val
; /* don't know what to do... */
3869 if (val
& AC_PINCTL_OUT_EN
) {
3870 if (!(cap
& AC_PINCAP_OUT
))
3871 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
3872 else if ((val
& AC_PINCTL_HP_EN
) && !(cap
& AC_PINCAP_HP_DRV
))
3873 val
&= ~AC_PINCTL_HP_EN
;
3876 if (val
& AC_PINCTL_IN_EN
) {
3877 if (!(cap
& AC_PINCAP_IN
))
3878 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
3880 unsigned int vcap
, vref
;
3882 vcap
= (cap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
3883 vref
= val
& AC_PINCTL_VREFEN
;
3884 for (i
= 0; i
< ARRAY_SIZE(cap_lists
); i
++) {
3885 if (vref
== cap_lists
[i
][0] &&
3886 !(vcap
& cap_lists
[i
][1])) {
3887 if (i
== ARRAY_SIZE(cap_lists
) - 1)
3888 vref
= AC_PINCTL_VREF_HIZ
;
3890 vref
= cap_lists
[i
+ 1][0];
3893 val
&= ~AC_PINCTL_VREFEN
;
3900 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl
);
3903 * _snd_hda_pin_ctl - Helper to set pin ctl value
3904 * @codec: the HDA codec
3905 * @pin: referred pin NID
3906 * @val: pin control value to set
3907 * @cached: access over codec pinctl cache or direct write
3909 * This function is a helper to set a pin ctl value more safely.
3910 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
3911 * value in pin target array via snd_hda_codec_set_pin_target(), then
3912 * actually writes the value via either snd_hda_codec_write_cache() or
3913 * snd_hda_codec_write() depending on @cached flag.
3915 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
3916 unsigned int val
, bool cached
)
3918 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
3919 snd_hda_codec_set_pin_target(codec
, pin
, val
);
3921 return snd_hda_codec_write_cache(codec
, pin
, 0,
3922 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
3924 return snd_hda_codec_write(codec
, pin
, 0,
3925 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
3927 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl
);
3930 * snd_hda_add_imux_item - Add an item to input_mux
3931 * @codec: the HDA codec
3932 * @imux: imux helper object
3933 * @label: the name of imux item to assign
3934 * @index: index number of imux item to assign
3935 * @type_idx: pointer to store the resultant label index
3937 * When the same label is used already in the existing items, the number
3938 * suffix is appended to the label. This label index number is stored
3939 * to type_idx when non-NULL pointer is given.
3941 int snd_hda_add_imux_item(struct hda_codec
*codec
,
3942 struct hda_input_mux
*imux
, const char *label
,
3943 int index
, int *type_idx
)
3945 int i
, label_idx
= 0;
3946 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
3947 codec_err(codec
, "hda_codec: Too many imux items!\n");
3950 for (i
= 0; i
< imux
->num_items
; i
++) {
3951 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
3955 *type_idx
= label_idx
;
3957 snprintf(imux
->items
[imux
->num_items
].label
,
3958 sizeof(imux
->items
[imux
->num_items
].label
),
3959 "%s %d", label
, label_idx
);
3961 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
3962 sizeof(imux
->items
[imux
->num_items
].label
));
3963 imux
->items
[imux
->num_items
].index
= index
;
3967 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item
);
3970 * snd_hda_bus_reset_codecs - Reset the bus
3971 * @bus: HD-audio bus
3973 void snd_hda_bus_reset_codecs(struct hda_bus
*bus
)
3975 struct hda_codec
*codec
;
3977 list_for_each_codec(codec
, bus
) {
3978 /* FIXME: maybe a better way needed for forced reset */
3979 if (current_work() != &codec
->jackpoll_work
.work
)
3980 cancel_delayed_work_sync(&codec
->jackpoll_work
);
3982 if (hda_codec_is_power_on(codec
)) {
3983 hda_call_codec_suspend(codec
);
3984 hda_call_codec_resume(codec
);
3991 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
3992 * @pcm: PCM caps bits
3993 * @buf: the string buffer to write
3994 * @buflen: the max buffer length
3996 * used by hda_proc.c and hda_eld.c
3998 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
4000 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
4003 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
4004 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
4005 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
4007 buf
[j
] = '\0'; /* necessary when j == 0 */
4009 EXPORT_SYMBOL_GPL(snd_print_pcm_bits
);
4011 MODULE_DESCRIPTION("HDA codec core");
4012 MODULE_LICENSE("GPL");