1 // SPDX-License-Identifier: GPL-2.0-only
3 * HD-audio codec core device
6 #include <linux/init.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/export.h>
12 #include <linux/pm_runtime.h>
13 #include <sound/hdaudio.h>
14 #include <sound/hda_regmap.h>
15 #include <sound/pcm.h>
18 static void setup_fg_nodes(struct hdac_device
*codec
);
19 static int get_codec_vendor_name(struct hdac_device
*codec
);
21 static void default_release(struct device
*dev
)
23 snd_hdac_device_exit(container_of(dev
, struct hdac_device
, dev
));
27 * snd_hdac_device_init - initialize the HD-audio codec base device
28 * @codec: device to initialize
30 * @name: device name string
31 * @addr: codec address
33 * Returns zero for success or a negative error code.
35 * This function increments the runtime PM counter and marks it active.
36 * The caller needs to turn it off appropriately later.
38 * The caller needs to set the device's release op properly by itself.
40 int snd_hdac_device_init(struct hdac_device
*codec
, struct hdac_bus
*bus
,
41 const char *name
, unsigned int addr
)
48 device_initialize(dev
);
49 dev
->parent
= bus
->dev
;
50 dev
->bus
= &snd_hda_bus_type
;
51 dev
->release
= default_release
;
52 dev
->groups
= hdac_dev_attr_groups
;
53 dev_set_name(dev
, "%s", name
);
54 device_enable_async_suspend(dev
);
58 codec
->type
= HDA_DEV_CORE
;
59 mutex_init(&codec
->widget_lock
);
60 mutex_init(&codec
->regmap_lock
);
61 pm_runtime_set_active(&codec
->dev
);
62 pm_runtime_get_noresume(&codec
->dev
);
63 atomic_set(&codec
->in_pm
, 0);
65 err
= snd_hdac_bus_add_device(bus
, codec
);
70 codec
->vendor_id
= snd_hdac_read_parm(codec
, AC_NODE_ROOT
,
72 if (codec
->vendor_id
== -1) {
73 /* read again, hopefully the access method was corrected
76 codec
->vendor_id
= snd_hdac_read_parm(codec
, AC_NODE_ROOT
,
80 codec
->subsystem_id
= snd_hdac_read_parm(codec
, AC_NODE_ROOT
,
82 codec
->revision_id
= snd_hdac_read_parm(codec
, AC_NODE_ROOT
,
85 setup_fg_nodes(codec
);
86 if (!codec
->afg
&& !codec
->mfg
) {
87 dev_err(dev
, "no AFG or MFG node found\n");
92 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
94 err
= snd_hdac_refresh_widgets(codec
);
98 codec
->power_caps
= snd_hdac_read_parm(codec
, fg
, AC_PAR_POWER_STATE
);
99 /* reread ssid if not set by parameter */
100 if (codec
->subsystem_id
== -1 || codec
->subsystem_id
== 0)
101 snd_hdac_read(codec
, fg
, AC_VERB_GET_SUBSYSTEM_ID
, 0,
102 &codec
->subsystem_id
);
104 err
= get_codec_vendor_name(codec
);
108 codec
->chip_name
= kasprintf(GFP_KERNEL
, "ID %x",
109 codec
->vendor_id
& 0xffff);
110 if (!codec
->chip_name
) {
118 put_device(&codec
->dev
);
121 EXPORT_SYMBOL_GPL(snd_hdac_device_init
);
124 * snd_hdac_device_exit - clean up the HD-audio codec base device
125 * @codec: device to clean up
127 void snd_hdac_device_exit(struct hdac_device
*codec
)
129 pm_runtime_put_noidle(&codec
->dev
);
130 snd_hdac_bus_remove_device(codec
->bus
, codec
);
131 kfree(codec
->vendor_name
);
132 kfree(codec
->chip_name
);
134 EXPORT_SYMBOL_GPL(snd_hdac_device_exit
);
137 * snd_hdac_device_register - register the hd-audio codec base device
138 * @codec: the device to register
140 int snd_hdac_device_register(struct hdac_device
*codec
)
144 err
= device_add(&codec
->dev
);
147 mutex_lock(&codec
->widget_lock
);
148 err
= hda_widget_sysfs_init(codec
);
149 mutex_unlock(&codec
->widget_lock
);
151 device_del(&codec
->dev
);
157 EXPORT_SYMBOL_GPL(snd_hdac_device_register
);
160 * snd_hdac_device_unregister - unregister the hd-audio codec base device
161 * @codec: the device to unregister
163 void snd_hdac_device_unregister(struct hdac_device
*codec
)
165 if (device_is_registered(&codec
->dev
)) {
166 mutex_lock(&codec
->widget_lock
);
167 hda_widget_sysfs_exit(codec
);
168 mutex_unlock(&codec
->widget_lock
);
169 device_del(&codec
->dev
);
170 snd_hdac_bus_remove_device(codec
->bus
, codec
);
173 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister
);
176 * snd_hdac_device_set_chip_name - set/update the codec name
177 * @codec: the HDAC device
178 * @name: name string to set
180 * Returns 0 if the name is set or updated, or a negative error code.
182 int snd_hdac_device_set_chip_name(struct hdac_device
*codec
, const char *name
)
188 newname
= kstrdup(name
, GFP_KERNEL
);
191 kfree(codec
->chip_name
);
192 codec
->chip_name
= newname
;
195 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name
);
198 * snd_hdac_codec_modalias - give the module alias name
199 * @codec: HDAC device
200 * @buf: string buffer to store
201 * @size: string buffer size
203 * Returns the size of string, like snprintf(), or a negative error code.
205 int snd_hdac_codec_modalias(struct hdac_device
*codec
, char *buf
, size_t size
)
207 return scnprintf(buf
, size
, "hdaudio:v%08Xr%08Xa%02X\n",
208 codec
->vendor_id
, codec
->revision_id
, codec
->type
);
210 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias
);
213 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
214 * HD-audio controller
215 * @codec: the codec object
216 * @nid: NID to encode
217 * @verb: verb to encode
218 * @parm: parameter to encode
220 * Return an encoded command verb or -1 for error.
222 static unsigned int snd_hdac_make_cmd(struct hdac_device
*codec
, hda_nid_t nid
,
223 unsigned int verb
, unsigned int parm
)
228 if ((addr
& ~0xf) || (nid
& ~0x7f) ||
229 (verb
& ~0xfff) || (parm
& ~0xffff)) {
230 dev_err(&codec
->dev
, "out of range cmd %x:%x:%x:%x\n",
231 addr
, nid
, verb
, parm
);
236 val
|= (u32
)nid
<< 20;
243 * snd_hdac_exec_verb - execute an encoded verb
244 * @codec: the codec object
245 * @cmd: encoded verb to execute
246 * @flags: optional flags, pass zero for default
247 * @res: the pointer to store the result, NULL if running async
249 * Returns zero if successful, or a negative error code.
251 * This calls the exec_verb op when set in hdac_codec. If not,
252 * call the default snd_hdac_bus_exec_verb().
254 int snd_hdac_exec_verb(struct hdac_device
*codec
, unsigned int cmd
,
255 unsigned int flags
, unsigned int *res
)
257 if (codec
->exec_verb
)
258 return codec
->exec_verb(codec
, cmd
, flags
, res
);
259 return snd_hdac_bus_exec_verb(codec
->bus
, codec
->addr
, cmd
, res
);
264 * snd_hdac_read - execute a verb
265 * @codec: the codec object
266 * @nid: NID to execute a verb
267 * @verb: verb to execute
268 * @parm: parameter for a verb
269 * @res: the pointer to store the result, NULL if running async
271 * Returns zero if successful, or a negative error code.
273 int snd_hdac_read(struct hdac_device
*codec
, hda_nid_t nid
,
274 unsigned int verb
, unsigned int parm
, unsigned int *res
)
276 unsigned int cmd
= snd_hdac_make_cmd(codec
, nid
, verb
, parm
);
278 return snd_hdac_exec_verb(codec
, cmd
, 0, res
);
280 EXPORT_SYMBOL_GPL(snd_hdac_read
);
283 * _snd_hdac_read_parm - read a parmeter
284 * @codec: the codec object
285 * @nid: NID to read a parameter
286 * @parm: parameter to read
287 * @res: pointer to store the read value
289 * This function returns zero or an error unlike snd_hdac_read_parm().
291 int _snd_hdac_read_parm(struct hdac_device
*codec
, hda_nid_t nid
, int parm
,
296 cmd
= snd_hdac_regmap_encode_verb(nid
, AC_VERB_PARAMETERS
) | parm
;
297 return snd_hdac_regmap_read_raw(codec
, cmd
, res
);
299 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm
);
302 * snd_hdac_read_parm_uncached - read a codec parameter without caching
303 * @codec: the codec object
304 * @nid: NID to read a parameter
305 * @parm: parameter to read
307 * Returns -1 for error. If you need to distinguish the error more
308 * strictly, use snd_hdac_read() directly.
310 int snd_hdac_read_parm_uncached(struct hdac_device
*codec
, hda_nid_t nid
,
313 unsigned int cmd
, val
;
315 cmd
= snd_hdac_regmap_encode_verb(nid
, AC_VERB_PARAMETERS
) | parm
;
316 if (snd_hdac_regmap_read_raw_uncached(codec
, cmd
, &val
) < 0)
320 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached
);
323 * snd_hdac_override_parm - override read-only parameters
324 * @codec: the codec object
325 * @nid: NID for the parameter
326 * @parm: the parameter to change
327 * @val: the parameter value to overwrite
329 int snd_hdac_override_parm(struct hdac_device
*codec
, hda_nid_t nid
,
330 unsigned int parm
, unsigned int val
)
332 unsigned int verb
= (AC_VERB_PARAMETERS
<< 8) | (nid
<< 20) | parm
;
338 codec
->caps_overwriting
= true;
339 err
= snd_hdac_regmap_write_raw(codec
, verb
, val
);
340 codec
->caps_overwriting
= false;
343 EXPORT_SYMBOL_GPL(snd_hdac_override_parm
);
346 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
347 * @codec: the codec object
348 * @nid: NID to inspect
349 * @start_id: the pointer to store the starting NID
351 * Returns the number of subtree nodes or zero if not found.
352 * This function reads parameters always without caching.
354 int snd_hdac_get_sub_nodes(struct hdac_device
*codec
, hda_nid_t nid
,
359 parm
= snd_hdac_read_parm_uncached(codec
, nid
, AC_PAR_NODE_COUNT
);
364 *start_id
= (parm
>> 16) & 0x7fff;
365 return (int)(parm
& 0x7fff);
367 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes
);
370 * look for an AFG and MFG nodes
372 static void setup_fg_nodes(struct hdac_device
*codec
)
374 int i
, total_nodes
, function_id
;
377 total_nodes
= snd_hdac_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
378 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
379 function_id
= snd_hdac_read_parm(codec
, nid
,
380 AC_PAR_FUNCTION_TYPE
);
381 switch (function_id
& 0xff) {
382 case AC_GRP_AUDIO_FUNCTION
:
384 codec
->afg_function_id
= function_id
& 0xff;
385 codec
->afg_unsol
= (function_id
>> 8) & 1;
387 case AC_GRP_MODEM_FUNCTION
:
389 codec
->mfg_function_id
= function_id
& 0xff;
390 codec
->mfg_unsol
= (function_id
>> 8) & 1;
399 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
400 * @codec: the codec object
402 int snd_hdac_refresh_widgets(struct hdac_device
*codec
)
408 * Serialize against multiple threads trying to update the sysfs
411 mutex_lock(&codec
->widget_lock
);
412 nums
= snd_hdac_get_sub_nodes(codec
, codec
->afg
, &start_nid
);
413 if (!start_nid
|| nums
<= 0 || nums
>= 0xff) {
414 dev_err(&codec
->dev
, "cannot read sub nodes for FG 0x%02x\n",
420 err
= hda_widget_sysfs_reinit(codec
, start_nid
, nums
);
424 codec
->num_nodes
= nums
;
425 codec
->start_nid
= start_nid
;
426 codec
->end_nid
= start_nid
+ nums
;
428 mutex_unlock(&codec
->widget_lock
);
431 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets
);
433 /* return CONNLIST_LEN parameter of the given widget */
434 static unsigned int get_num_conns(struct hdac_device
*codec
, hda_nid_t nid
)
436 unsigned int wcaps
= get_wcaps(codec
, nid
);
439 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
440 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
443 parm
= snd_hdac_read_parm(codec
, nid
, AC_PAR_CONNLIST_LEN
);
450 * snd_hdac_get_connections - get a widget connection list
451 * @codec: the codec object
453 * @conn_list: the array to store the results, can be NULL
454 * @max_conns: the max size of the given array
456 * Returns the number of connected widgets, zero for no connection, or a
457 * negative error code. When the number of elements don't fit with the
458 * given array size, it returns -ENOSPC.
460 * When @conn_list is NULL, it just checks the number of connections.
462 int snd_hdac_get_connections(struct hdac_device
*codec
, hda_nid_t nid
,
463 hda_nid_t
*conn_list
, int max_conns
)
466 int i
, conn_len
, conns
, err
;
467 unsigned int shift
, num_elems
, mask
;
471 parm
= get_num_conns(codec
, nid
);
475 if (parm
& AC_CLIST_LONG
) {
484 conn_len
= parm
& AC_CLIST_LENGTH
;
485 mask
= (1 << (shift
-1)) - 1;
488 return 0; /* no connection */
491 /* single connection */
492 err
= snd_hdac_read(codec
, nid
, AC_VERB_GET_CONNECT_LIST
, 0,
497 conn_list
[0] = parm
& mask
;
501 /* multi connection */
504 for (i
= 0; i
< conn_len
; i
++) {
508 if (i
% num_elems
== 0) {
509 err
= snd_hdac_read(codec
, nid
,
510 AC_VERB_GET_CONNECT_LIST
, i
,
515 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
517 if (val
== 0 && null_count
++) { /* no second chance */
519 "invalid CONNECT_LIST verb %x[%i]:%x\n",
525 /* ranges between the previous and this one */
526 if (!prev_nid
|| prev_nid
>= val
) {
527 dev_warn(&codec
->dev
,
528 "invalid dep_range_val %x:%x\n",
532 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
534 if (conns
>= max_conns
)
536 conn_list
[conns
] = n
;
542 if (conns
>= max_conns
)
544 conn_list
[conns
] = val
;
552 EXPORT_SYMBOL_GPL(snd_hdac_get_connections
);
556 * snd_hdac_power_up - power up the codec
557 * @codec: the codec object
559 * This function calls the runtime PM helper to power up the given codec.
560 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
561 * path that isn't included in PM path. Otherwise it gets stuck.
563 * Returns zero if successful, or a negative error code.
565 int snd_hdac_power_up(struct hdac_device
*codec
)
567 return pm_runtime_get_sync(&codec
->dev
);
569 EXPORT_SYMBOL_GPL(snd_hdac_power_up
);
572 * snd_hdac_power_down - power down the codec
573 * @codec: the codec object
575 * Returns zero if successful, or a negative error code.
577 int snd_hdac_power_down(struct hdac_device
*codec
)
579 struct device
*dev
= &codec
->dev
;
581 pm_runtime_mark_last_busy(dev
);
582 return pm_runtime_put_autosuspend(dev
);
584 EXPORT_SYMBOL_GPL(snd_hdac_power_down
);
587 * snd_hdac_power_up_pm - power up the codec
588 * @codec: the codec object
590 * This function can be called in a recursive code path like init code
591 * which may be called by PM suspend/resume again. OTOH, if a power-up
592 * call must wake up the sleeper (e.g. in a kctl callback), use
593 * snd_hdac_power_up() instead.
595 * Returns zero if successful, or a negative error code.
597 int snd_hdac_power_up_pm(struct hdac_device
*codec
)
599 if (!atomic_inc_not_zero(&codec
->in_pm
))
600 return snd_hdac_power_up(codec
);
603 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm
);
605 /* like snd_hdac_power_up_pm(), but only increment the pm count when
606 * already powered up. Returns -1 if not powered up, 1 if incremented
607 * or 0 if unchanged. Only used in hdac_regmap.c
609 int snd_hdac_keep_power_up(struct hdac_device
*codec
)
611 if (!atomic_inc_not_zero(&codec
->in_pm
)) {
612 int ret
= pm_runtime_get_if_in_use(&codec
->dev
);
622 * snd_hdac_power_down_pm - power down the codec
623 * @codec: the codec object
625 * Like snd_hdac_power_up_pm(), this function is used in a recursive
626 * code path like init code which may be called by PM suspend/resume again.
628 * Returns zero if successful, or a negative error code.
630 int snd_hdac_power_down_pm(struct hdac_device
*codec
)
632 if (atomic_dec_if_positive(&codec
->in_pm
) < 0)
633 return snd_hdac_power_down(codec
);
636 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm
);
639 /* codec vendor labels */
640 struct hda_vendor_id
{
645 static const struct hda_vendor_id hda_vendor_ids
[] = {
647 { 0x1013, "Cirrus Logic" },
648 { 0x1057, "Motorola" },
649 { 0x1095, "Silicon Image" },
650 { 0x10de, "Nvidia" },
651 { 0x10ec, "Realtek" },
652 { 0x1102, "Creative" },
656 { 0x11d4, "Analog Devices" },
657 { 0x13f6, "C-Media" },
658 { 0x14f1, "Conexant" },
659 { 0x17e8, "Chrontel" },
661 { 0x1aec, "Wolfson Microelectronics" },
663 { 0x434d, "C-Media" },
665 { 0x8384, "SigmaTel" },
669 /* store the codec vendor name */
670 static int get_codec_vendor_name(struct hdac_device
*codec
)
672 const struct hda_vendor_id
*c
;
673 u16 vendor_id
= codec
->vendor_id
>> 16;
675 for (c
= hda_vendor_ids
; c
->id
; c
++) {
676 if (c
->id
== vendor_id
) {
677 codec
->vendor_name
= kstrdup(c
->name
, GFP_KERNEL
);
678 return codec
->vendor_name
? 0 : -ENOMEM
;
682 codec
->vendor_name
= kasprintf(GFP_KERNEL
, "Generic %04x", vendor_id
);
683 return codec
->vendor_name
? 0 : -ENOMEM
;
689 struct hda_rate_tbl
{
691 unsigned int alsa_bits
;
692 unsigned int hda_fmt
;
695 /* rate = base * mult / div */
696 #define HDA_RATE(base, mult, div) \
697 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
698 (((div) - 1) << AC_FMT_DIV_SHIFT))
700 static const struct hda_rate_tbl rate_bits
[] = {
701 /* rate in Hz, ALSA rate bitmask, HDA format value */
703 /* autodetected value used in snd_hda_query_supported_pcm */
704 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
705 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
706 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
707 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
708 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
709 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
710 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
711 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
712 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
713 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
714 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
715 #define AC_PAR_PCM_RATE_BITS 11
716 /* up to bits 10, 384kHZ isn't supported properly */
718 /* not autodetected value */
719 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
721 { 0 } /* terminator */
725 * snd_hdac_calc_stream_format - calculate the format bitset
726 * @rate: the sample rate
727 * @channels: the number of channels
728 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
729 * @maxbps: the max. bps
730 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
732 * Calculate the format bitset from the given rate, channels and th PCM format.
734 * Return zero if invalid.
736 unsigned int snd_hdac_calc_stream_format(unsigned int rate
,
737 unsigned int channels
,
738 snd_pcm_format_t format
,
740 unsigned short spdif_ctls
)
743 unsigned int val
= 0;
745 for (i
= 0; rate_bits
[i
].hz
; i
++)
746 if (rate_bits
[i
].hz
== rate
) {
747 val
= rate_bits
[i
].hda_fmt
;
750 if (!rate_bits
[i
].hz
)
753 if (channels
== 0 || channels
> 8)
757 switch (snd_pcm_format_width(format
)) {
759 val
|= AC_FMT_BITS_8
;
762 val
|= AC_FMT_BITS_16
;
767 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
768 val
|= AC_FMT_BITS_32
;
769 else if (maxbps
>= 24)
770 val
|= AC_FMT_BITS_24
;
772 val
|= AC_FMT_BITS_20
;
778 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
779 val
|= AC_FMT_TYPE_NON_PCM
;
783 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format
);
785 static unsigned int query_pcm_param(struct hdac_device
*codec
, hda_nid_t nid
)
787 unsigned int val
= 0;
789 if (nid
!= codec
->afg
&&
790 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
791 val
= snd_hdac_read_parm(codec
, nid
, AC_PAR_PCM
);
792 if (!val
|| val
== -1)
793 val
= snd_hdac_read_parm(codec
, codec
->afg
, AC_PAR_PCM
);
794 if (!val
|| val
== -1)
799 static unsigned int query_stream_param(struct hdac_device
*codec
, hda_nid_t nid
)
801 unsigned int streams
= snd_hdac_read_parm(codec
, nid
, AC_PAR_STREAM
);
803 if (!streams
|| streams
== -1)
804 streams
= snd_hdac_read_parm(codec
, codec
->afg
, AC_PAR_STREAM
);
805 if (!streams
|| streams
== -1)
811 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
812 * @codec: the codec object
814 * @ratesp: the pointer to store the detected rate bitflags
815 * @formatsp: the pointer to store the detected formats
816 * @bpsp: the pointer to store the detected format widths
818 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
819 * or @bsps argument is ignored.
821 * Returns 0 if successful, otherwise a negative error code.
823 int snd_hdac_query_supported_pcm(struct hdac_device
*codec
, hda_nid_t nid
,
824 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
826 unsigned int i
, val
, wcaps
;
828 wcaps
= get_wcaps(codec
, nid
);
829 val
= query_pcm_param(codec
, nid
);
833 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
835 rates
|= rate_bits
[i
].alsa_bits
;
839 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
841 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
847 if (formatsp
|| bpsp
) {
849 unsigned int streams
, bps
;
851 streams
= query_stream_param(codec
, nid
);
856 if (streams
& AC_SUPFMT_PCM
) {
857 if (val
& AC_SUPPCM_BITS_8
) {
858 formats
|= SNDRV_PCM_FMTBIT_U8
;
861 if (val
& AC_SUPPCM_BITS_16
) {
862 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
865 if (wcaps
& AC_WCAP_DIGITAL
) {
866 if (val
& AC_SUPPCM_BITS_32
)
867 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
868 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
869 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
870 if (val
& AC_SUPPCM_BITS_24
)
872 else if (val
& AC_SUPPCM_BITS_20
)
874 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
875 AC_SUPPCM_BITS_32
)) {
876 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
877 if (val
& AC_SUPPCM_BITS_32
)
879 else if (val
& AC_SUPPCM_BITS_24
)
881 else if (val
& AC_SUPPCM_BITS_20
)
885 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
886 if (streams
& AC_SUPFMT_FLOAT32
) {
887 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
892 if (streams
== AC_SUPFMT_AC3
) {
893 /* should be exclusive */
894 /* temporary hack: we have still no proper support
895 * for the direct AC3 stream...
897 formats
|= SNDRV_PCM_FMTBIT_U8
;
902 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
904 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
916 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm
);
919 * snd_hdac_is_supported_format - Check the validity of the format
920 * @codec: the codec object
922 * @format: the HD-audio format value to check
924 * Check whether the given node supports the format value.
926 * Returns true if supported, false if not.
928 bool snd_hdac_is_supported_format(struct hdac_device
*codec
, hda_nid_t nid
,
932 unsigned int val
= 0, rate
, stream
;
934 val
= query_pcm_param(codec
, nid
);
938 rate
= format
& 0xff00;
939 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
940 if (rate_bits
[i
].hda_fmt
== rate
) {
945 if (i
>= AC_PAR_PCM_RATE_BITS
)
948 stream
= query_stream_param(codec
, nid
);
952 if (stream
& AC_SUPFMT_PCM
) {
953 switch (format
& 0xf0) {
955 if (!(val
& AC_SUPPCM_BITS_8
))
959 if (!(val
& AC_SUPPCM_BITS_16
))
963 if (!(val
& AC_SUPPCM_BITS_20
))
967 if (!(val
& AC_SUPPCM_BITS_24
))
971 if (!(val
& AC_SUPPCM_BITS_32
))
978 /* FIXME: check for float32 and AC3? */
983 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format
);
985 static unsigned int codec_read(struct hdac_device
*hdac
, hda_nid_t nid
,
986 int flags
, unsigned int verb
, unsigned int parm
)
988 unsigned int cmd
= snd_hdac_make_cmd(hdac
, nid
, verb
, parm
);
991 if (snd_hdac_exec_verb(hdac
, cmd
, flags
, &res
))
997 static int codec_write(struct hdac_device
*hdac
, hda_nid_t nid
,
998 int flags
, unsigned int verb
, unsigned int parm
)
1000 unsigned int cmd
= snd_hdac_make_cmd(hdac
, nid
, verb
, parm
);
1002 return snd_hdac_exec_verb(hdac
, cmd
, flags
, NULL
);
1006 * snd_hdac_codec_read - send a command and get the response
1007 * @hdac: the HDAC device
1008 * @nid: NID to send the command
1009 * @flags: optional bit flags
1010 * @verb: the verb to send
1011 * @parm: the parameter for the verb
1013 * Send a single command and read the corresponding response.
1015 * Returns the obtained response value, or -1 for an error.
1017 int snd_hdac_codec_read(struct hdac_device
*hdac
, hda_nid_t nid
,
1018 int flags
, unsigned int verb
, unsigned int parm
)
1020 return codec_read(hdac
, nid
, flags
, verb
, parm
);
1022 EXPORT_SYMBOL_GPL(snd_hdac_codec_read
);
1025 * snd_hdac_codec_write - send a single command without waiting for response
1026 * @hdac: the HDAC device
1027 * @nid: NID to send the command
1028 * @flags: optional bit flags
1029 * @verb: the verb to send
1030 * @parm: the parameter for the verb
1032 * Send a single command without waiting for response.
1034 * Returns 0 if successful, or a negative error code.
1036 int snd_hdac_codec_write(struct hdac_device
*hdac
, hda_nid_t nid
,
1037 int flags
, unsigned int verb
, unsigned int parm
)
1039 return codec_write(hdac
, nid
, flags
, verb
, parm
);
1041 EXPORT_SYMBOL_GPL(snd_hdac_codec_write
);
1044 * snd_hdac_check_power_state - check whether the actual power state matches
1045 * with the target state
1047 * @hdac: the HDAC device
1048 * @nid: NID to send the command
1049 * @target_state: target state to check for
1051 * Return true if state matches, false if not
1053 bool snd_hdac_check_power_state(struct hdac_device
*hdac
,
1054 hda_nid_t nid
, unsigned int target_state
)
1056 unsigned int state
= codec_read(hdac
, nid
, 0,
1057 AC_VERB_GET_POWER_STATE
, 0);
1059 if (state
& AC_PWRST_ERROR
)
1061 state
= (state
>> 4) & 0x0f;
1062 return (state
== target_state
);
1064 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state
);
1066 * snd_hdac_sync_power_state - wait until actual power state matches
1067 * with the target state
1069 * @codec: the HDAC device
1070 * @nid: NID to send the command
1071 * @power_state: target power state to wait for
1073 * Return power state or PS_ERROR if codec rejects GET verb.
1075 unsigned int snd_hdac_sync_power_state(struct hdac_device
*codec
,
1076 hda_nid_t nid
, unsigned int power_state
)
1078 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
1079 unsigned int state
, actual_state
, count
;
1081 for (count
= 0; count
< 500; count
++) {
1082 state
= snd_hdac_codec_read(codec
, nid
, 0,
1083 AC_VERB_GET_POWER_STATE
, 0);
1084 if (state
& AC_PWRST_ERROR
) {
1088 actual_state
= (state
>> 4) & 0x0f;
1089 if (actual_state
== power_state
)
1091 if (time_after_eq(jiffies
, end_time
))
1093 /* wait until the codec reachs to the target state */
1098 EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state
);