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>
16 #include <sound/pcm_params.h>
19 static void setup_fg_nodes(struct hdac_device
*codec
);
20 static int get_codec_vendor_name(struct hdac_device
*codec
);
22 static void default_release(struct device
*dev
)
24 snd_hdac_device_exit(dev_to_hdac_dev(dev
));
28 * snd_hdac_device_init - initialize the HD-audio codec base device
29 * @codec: device to initialize
31 * @name: device name string
32 * @addr: codec address
34 * Returns zero for success or a negative error code.
36 * This function increments the runtime PM counter and marks it active.
37 * The caller needs to turn it off appropriately later.
39 * The caller needs to set the device's release op properly by itself.
41 int snd_hdac_device_init(struct hdac_device
*codec
, struct hdac_bus
*bus
,
42 const char *name
, unsigned int addr
)
49 device_initialize(dev
);
50 dev
->parent
= bus
->dev
;
51 dev
->bus
= &snd_hda_bus_type
;
52 dev
->release
= default_release
;
53 dev
->groups
= hdac_dev_attr_groups
;
54 dev_set_name(dev
, "%s", name
);
55 device_enable_async_suspend(dev
);
59 codec
->type
= HDA_DEV_CORE
;
60 mutex_init(&codec
->widget_lock
);
61 mutex_init(&codec
->regmap_lock
);
62 pm_runtime_set_active(&codec
->dev
);
63 pm_runtime_get_noresume(&codec
->dev
);
64 atomic_set(&codec
->in_pm
, 0);
66 err
= snd_hdac_bus_add_device(bus
, codec
);
71 codec
->vendor_id
= snd_hdac_read_parm(codec
, AC_NODE_ROOT
,
73 if (codec
->vendor_id
== -1) {
74 /* read again, hopefully the access method was corrected
77 codec
->vendor_id
= snd_hdac_read_parm(codec
, AC_NODE_ROOT
,
81 codec
->subsystem_id
= snd_hdac_read_parm(codec
, AC_NODE_ROOT
,
83 codec
->revision_id
= snd_hdac_read_parm(codec
, AC_NODE_ROOT
,
86 setup_fg_nodes(codec
);
87 if (!codec
->afg
&& !codec
->mfg
) {
88 dev_err(dev
, "no AFG or MFG node found\n");
93 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
95 err
= snd_hdac_refresh_widgets(codec
);
99 codec
->power_caps
= snd_hdac_read_parm(codec
, fg
, AC_PAR_POWER_STATE
);
100 /* reread ssid if not set by parameter */
101 if (codec
->subsystem_id
== -1 || codec
->subsystem_id
== 0)
102 snd_hdac_read(codec
, fg
, AC_VERB_GET_SUBSYSTEM_ID
, 0,
103 &codec
->subsystem_id
);
105 err
= get_codec_vendor_name(codec
);
109 codec
->chip_name
= kasprintf(GFP_KERNEL
, "ID %x",
110 codec
->vendor_id
& 0xffff);
111 if (!codec
->chip_name
) {
119 put_device(&codec
->dev
);
122 EXPORT_SYMBOL_GPL(snd_hdac_device_init
);
125 * snd_hdac_device_exit - clean up the HD-audio codec base device
126 * @codec: device to clean up
128 void snd_hdac_device_exit(struct hdac_device
*codec
)
130 pm_runtime_put_noidle(&codec
->dev
);
131 /* keep balance of runtime PM child_count in parent device */
132 pm_runtime_set_suspended(&codec
->dev
);
133 snd_hdac_bus_remove_device(codec
->bus
, codec
);
134 kfree(codec
->vendor_name
);
135 kfree(codec
->chip_name
);
137 EXPORT_SYMBOL_GPL(snd_hdac_device_exit
);
140 * snd_hdac_device_register - register the hd-audio codec base device
141 * @codec: the device to register
143 int snd_hdac_device_register(struct hdac_device
*codec
)
147 err
= device_add(&codec
->dev
);
150 mutex_lock(&codec
->widget_lock
);
151 err
= hda_widget_sysfs_init(codec
);
152 mutex_unlock(&codec
->widget_lock
);
154 device_del(&codec
->dev
);
160 EXPORT_SYMBOL_GPL(snd_hdac_device_register
);
163 * snd_hdac_device_unregister - unregister the hd-audio codec base device
164 * @codec: the device to unregister
166 void snd_hdac_device_unregister(struct hdac_device
*codec
)
168 if (device_is_registered(&codec
->dev
)) {
169 mutex_lock(&codec
->widget_lock
);
170 hda_widget_sysfs_exit(codec
);
171 mutex_unlock(&codec
->widget_lock
);
172 device_del(&codec
->dev
);
173 snd_hdac_bus_remove_device(codec
->bus
, codec
);
176 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister
);
179 * snd_hdac_device_set_chip_name - set/update the codec name
180 * @codec: the HDAC device
181 * @name: name string to set
183 * Returns 0 if the name is set or updated, or a negative error code.
185 int snd_hdac_device_set_chip_name(struct hdac_device
*codec
, const char *name
)
191 newname
= kstrdup(name
, GFP_KERNEL
);
194 kfree(codec
->chip_name
);
195 codec
->chip_name
= newname
;
198 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name
);
201 * snd_hdac_codec_modalias - give the module alias name
202 * @codec: HDAC device
203 * @buf: string buffer to store
204 * @size: string buffer size
206 * Returns the size of string, like snprintf(), or a negative error code.
208 int snd_hdac_codec_modalias(const struct hdac_device
*codec
, char *buf
, size_t size
)
210 return scnprintf(buf
, size
, "hdaudio:v%08Xr%08Xa%02X\n",
211 codec
->vendor_id
, codec
->revision_id
, codec
->type
);
213 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias
);
216 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
217 * HD-audio controller
218 * @codec: the codec object
219 * @nid: NID to encode
220 * @verb: verb to encode
221 * @parm: parameter to encode
223 * Return an encoded command verb or -1 for error.
225 static unsigned int snd_hdac_make_cmd(struct hdac_device
*codec
, hda_nid_t nid
,
226 unsigned int verb
, unsigned int parm
)
231 if ((addr
& ~0xf) || (nid
& ~0x7f) ||
232 (verb
& ~0xfff) || (parm
& ~0xffff)) {
233 dev_err(&codec
->dev
, "out of range cmd %x:%x:%x:%x\n",
234 addr
, nid
, verb
, parm
);
239 val
|= (u32
)nid
<< 20;
246 * snd_hdac_exec_verb - execute an encoded verb
247 * @codec: the codec object
248 * @cmd: encoded verb to execute
249 * @flags: optional flags, pass zero for default
250 * @res: the pointer to store the result, NULL if running async
252 * Returns zero if successful, or a negative error code.
254 * This calls the exec_verb op when set in hdac_codec. If not,
255 * call the default snd_hdac_bus_exec_verb().
257 int snd_hdac_exec_verb(struct hdac_device
*codec
, unsigned int cmd
,
258 unsigned int flags
, unsigned int *res
)
260 if (codec
->exec_verb
)
261 return codec
->exec_verb(codec
, cmd
, flags
, res
);
262 return snd_hdac_bus_exec_verb(codec
->bus
, codec
->addr
, cmd
, res
);
267 * snd_hdac_read - execute a verb
268 * @codec: the codec object
269 * @nid: NID to execute a verb
270 * @verb: verb to execute
271 * @parm: parameter for a verb
272 * @res: the pointer to store the result, NULL if running async
274 * Returns zero if successful, or a negative error code.
276 int snd_hdac_read(struct hdac_device
*codec
, hda_nid_t nid
,
277 unsigned int verb
, unsigned int parm
, unsigned int *res
)
279 unsigned int cmd
= snd_hdac_make_cmd(codec
, nid
, verb
, parm
);
281 return snd_hdac_exec_verb(codec
, cmd
, 0, res
);
283 EXPORT_SYMBOL_GPL(snd_hdac_read
);
286 * _snd_hdac_read_parm - read a parmeter
287 * @codec: the codec object
288 * @nid: NID to read a parameter
289 * @parm: parameter to read
290 * @res: pointer to store the read value
292 * This function returns zero or an error unlike snd_hdac_read_parm().
294 int _snd_hdac_read_parm(struct hdac_device
*codec
, hda_nid_t nid
, int parm
,
299 cmd
= snd_hdac_regmap_encode_verb(nid
, AC_VERB_PARAMETERS
) | parm
;
300 return snd_hdac_regmap_read_raw(codec
, cmd
, res
);
302 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm
);
305 * snd_hdac_read_parm_uncached - read a codec parameter without caching
306 * @codec: the codec object
307 * @nid: NID to read a parameter
308 * @parm: parameter to read
310 * Returns -1 for error. If you need to distinguish the error more
311 * strictly, use snd_hdac_read() directly.
313 int snd_hdac_read_parm_uncached(struct hdac_device
*codec
, hda_nid_t nid
,
316 unsigned int cmd
, val
;
318 cmd
= snd_hdac_regmap_encode_verb(nid
, AC_VERB_PARAMETERS
) | parm
;
319 if (snd_hdac_regmap_read_raw_uncached(codec
, cmd
, &val
) < 0)
323 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached
);
326 * snd_hdac_override_parm - override read-only parameters
327 * @codec: the codec object
328 * @nid: NID for the parameter
329 * @parm: the parameter to change
330 * @val: the parameter value to overwrite
332 int snd_hdac_override_parm(struct hdac_device
*codec
, hda_nid_t nid
,
333 unsigned int parm
, unsigned int val
)
335 unsigned int verb
= (AC_VERB_PARAMETERS
<< 8) | (nid
<< 20) | parm
;
341 codec
->caps_overwriting
= true;
342 err
= snd_hdac_regmap_write_raw(codec
, verb
, val
);
343 codec
->caps_overwriting
= false;
346 EXPORT_SYMBOL_GPL(snd_hdac_override_parm
);
349 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
350 * @codec: the codec object
351 * @nid: NID to inspect
352 * @start_id: the pointer to store the starting NID
354 * Returns the number of subtree nodes or zero if not found.
355 * This function reads parameters always without caching.
357 int snd_hdac_get_sub_nodes(struct hdac_device
*codec
, hda_nid_t nid
,
362 parm
= snd_hdac_read_parm_uncached(codec
, nid
, AC_PAR_NODE_COUNT
);
367 *start_id
= (parm
>> 16) & 0x7fff;
368 return (int)(parm
& 0x7fff);
370 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes
);
373 * look for an AFG and MFG nodes
375 static void setup_fg_nodes(struct hdac_device
*codec
)
377 int i
, total_nodes
, function_id
;
380 total_nodes
= snd_hdac_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
381 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
382 function_id
= snd_hdac_read_parm(codec
, nid
,
383 AC_PAR_FUNCTION_TYPE
);
384 switch (function_id
& 0xff) {
385 case AC_GRP_AUDIO_FUNCTION
:
387 codec
->afg_function_id
= function_id
& 0xff;
388 codec
->afg_unsol
= (function_id
>> 8) & 1;
390 case AC_GRP_MODEM_FUNCTION
:
392 codec
->mfg_function_id
= function_id
& 0xff;
393 codec
->mfg_unsol
= (function_id
>> 8) & 1;
402 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
403 * @codec: the codec object
405 int snd_hdac_refresh_widgets(struct hdac_device
*codec
)
411 * Serialize against multiple threads trying to update the sysfs
414 mutex_lock(&codec
->widget_lock
);
415 nums
= snd_hdac_get_sub_nodes(codec
, codec
->afg
, &start_nid
);
416 if (!start_nid
|| nums
<= 0 || nums
>= 0xff) {
417 dev_err(&codec
->dev
, "cannot read sub nodes for FG 0x%02x\n",
423 err
= hda_widget_sysfs_reinit(codec
, start_nid
, nums
);
427 codec
->num_nodes
= nums
;
428 codec
->start_nid
= start_nid
;
429 codec
->end_nid
= start_nid
+ nums
;
431 mutex_unlock(&codec
->widget_lock
);
434 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets
);
436 /* return CONNLIST_LEN parameter of the given widget */
437 static unsigned int get_num_conns(struct hdac_device
*codec
, hda_nid_t nid
)
439 unsigned int wcaps
= get_wcaps(codec
, nid
);
442 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
443 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
446 parm
= snd_hdac_read_parm(codec
, nid
, AC_PAR_CONNLIST_LEN
);
453 * snd_hdac_get_connections - get a widget connection list
454 * @codec: the codec object
456 * @conn_list: the array to store the results, can be NULL
457 * @max_conns: the max size of the given array
459 * Returns the number of connected widgets, zero for no connection, or a
460 * negative error code. When the number of elements don't fit with the
461 * given array size, it returns -ENOSPC.
463 * When @conn_list is NULL, it just checks the number of connections.
465 int snd_hdac_get_connections(struct hdac_device
*codec
, hda_nid_t nid
,
466 hda_nid_t
*conn_list
, int max_conns
)
469 int i
, conn_len
, conns
, err
;
470 unsigned int shift
, num_elems
, mask
;
474 parm
= get_num_conns(codec
, nid
);
478 if (parm
& AC_CLIST_LONG
) {
487 conn_len
= parm
& AC_CLIST_LENGTH
;
488 mask
= (1 << (shift
-1)) - 1;
491 return 0; /* no connection */
494 /* single connection */
495 err
= snd_hdac_read(codec
, nid
, AC_VERB_GET_CONNECT_LIST
, 0,
500 conn_list
[0] = parm
& mask
;
504 /* multi connection */
507 for (i
= 0; i
< conn_len
; i
++) {
511 if (i
% num_elems
== 0) {
512 err
= snd_hdac_read(codec
, nid
,
513 AC_VERB_GET_CONNECT_LIST
, i
,
518 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
520 if (val
== 0 && null_count
++) { /* no second chance */
522 "invalid CONNECT_LIST verb %x[%i]:%x\n",
528 /* ranges between the previous and this one */
529 if (!prev_nid
|| prev_nid
>= val
) {
530 dev_warn(&codec
->dev
,
531 "invalid dep_range_val %x:%x\n",
535 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
537 if (conns
>= max_conns
)
539 conn_list
[conns
] = n
;
545 if (conns
>= max_conns
)
547 conn_list
[conns
] = val
;
555 EXPORT_SYMBOL_GPL(snd_hdac_get_connections
);
559 * snd_hdac_power_up - power up the codec
560 * @codec: the codec object
562 * This function calls the runtime PM helper to power up the given codec.
563 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
564 * path that isn't included in PM path. Otherwise it gets stuck.
566 * Returns zero if successful, or a negative error code.
568 int snd_hdac_power_up(struct hdac_device
*codec
)
570 return pm_runtime_get_sync(&codec
->dev
);
572 EXPORT_SYMBOL_GPL(snd_hdac_power_up
);
575 * snd_hdac_power_down - power down the codec
576 * @codec: the codec object
578 * Returns zero if successful, or a negative error code.
580 int snd_hdac_power_down(struct hdac_device
*codec
)
582 struct device
*dev
= &codec
->dev
;
584 pm_runtime_mark_last_busy(dev
);
585 return pm_runtime_put_autosuspend(dev
);
587 EXPORT_SYMBOL_GPL(snd_hdac_power_down
);
590 * snd_hdac_power_up_pm - power up the codec
591 * @codec: the codec object
593 * This function can be called in a recursive code path like init code
594 * which may be called by PM suspend/resume again. OTOH, if a power-up
595 * call must wake up the sleeper (e.g. in a kctl callback), use
596 * snd_hdac_power_up() instead.
598 * Returns zero if successful, or a negative error code.
600 int snd_hdac_power_up_pm(struct hdac_device
*codec
)
602 if (!atomic_inc_not_zero(&codec
->in_pm
))
603 return snd_hdac_power_up(codec
);
606 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm
);
608 /* like snd_hdac_power_up_pm(), but only increment the pm count when
609 * already powered up. Returns -1 if not powered up, 1 if incremented
610 * or 0 if unchanged. Only used in hdac_regmap.c
612 int snd_hdac_keep_power_up(struct hdac_device
*codec
)
614 if (!atomic_inc_not_zero(&codec
->in_pm
)) {
615 int ret
= pm_runtime_get_if_active(&codec
->dev
);
625 * snd_hdac_power_down_pm - power down the codec
626 * @codec: the codec object
628 * Like snd_hdac_power_up_pm(), this function is used in a recursive
629 * code path like init code which may be called by PM suspend/resume again.
631 * Returns zero if successful, or a negative error code.
633 int snd_hdac_power_down_pm(struct hdac_device
*codec
)
635 if (atomic_dec_if_positive(&codec
->in_pm
) < 0)
636 return snd_hdac_power_down(codec
);
639 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm
);
642 /* codec vendor labels */
643 struct hda_vendor_id
{
648 static const struct hda_vendor_id hda_vendor_ids
[] = {
649 { 0x0014, "Loongson" },
651 { 0x1013, "Cirrus Logic" },
652 { 0x1057, "Motorola" },
653 { 0x1095, "Silicon Image" },
654 { 0x10de, "Nvidia" },
655 { 0x10ec, "Realtek" },
656 { 0x1102, "Creative" },
660 { 0x11d4, "Analog Devices" },
661 { 0x13f6, "C-Media" },
662 { 0x14f1, "Conexant" },
663 { 0x17e8, "Chrontel" },
665 { 0x19e5, "Huawei" },
666 { 0x1aec, "Wolfson Microelectronics" },
668 { 0x1fa8, "Senarytech" },
669 { 0x434d, "C-Media" },
671 { 0x8384, "SigmaTel" },
675 /* store the codec vendor name */
676 static int get_codec_vendor_name(struct hdac_device
*codec
)
678 const struct hda_vendor_id
*c
;
679 u16 vendor_id
= codec
->vendor_id
>> 16;
681 for (c
= hda_vendor_ids
; c
->id
; c
++) {
682 if (c
->id
== vendor_id
) {
683 codec
->vendor_name
= kstrdup(c
->name
, GFP_KERNEL
);
684 return codec
->vendor_name
? 0 : -ENOMEM
;
688 codec
->vendor_name
= kasprintf(GFP_KERNEL
, "Generic %04x", vendor_id
);
689 return codec
->vendor_name
? 0 : -ENOMEM
;
695 struct hda_rate_tbl
{
697 unsigned int alsa_bits
;
698 unsigned int hda_fmt
;
701 /* rate = base * mult / div */
702 #define HDA_RATE(base, mult, div) \
703 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
704 (((div) - 1) << AC_FMT_DIV_SHIFT))
706 static const struct hda_rate_tbl rate_bits
[] = {
707 /* rate in Hz, ALSA rate bitmask, HDA format value */
709 /* autodetected value used in snd_hda_query_supported_pcm */
710 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
711 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
712 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
713 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
714 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
715 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
716 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
717 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
718 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
719 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
720 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
721 #define AC_PAR_PCM_RATE_BITS 11
722 /* up to bits 10, 384kHZ isn't supported properly */
724 /* not autodetected value */
725 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
727 { 0 } /* terminator */
730 static snd_pcm_format_t
snd_hdac_format_normalize(snd_pcm_format_t format
)
733 case SNDRV_PCM_FORMAT_S20_LE
:
734 case SNDRV_PCM_FORMAT_S24_LE
:
735 return SNDRV_PCM_FORMAT_S32_LE
;
737 case SNDRV_PCM_FORMAT_U20_LE
:
738 case SNDRV_PCM_FORMAT_U24_LE
:
739 return SNDRV_PCM_FORMAT_U32_LE
;
741 case SNDRV_PCM_FORMAT_S20_BE
:
742 case SNDRV_PCM_FORMAT_S24_BE
:
743 return SNDRV_PCM_FORMAT_S32_BE
;
745 case SNDRV_PCM_FORMAT_U20_BE
:
746 case SNDRV_PCM_FORMAT_U24_BE
:
747 return SNDRV_PCM_FORMAT_U32_BE
;
755 * snd_hdac_stream_format_bits - obtain bits per sample value.
756 * @format: the PCM format.
757 * @subformat: the PCM subformat.
758 * @maxbits: the maximum bits per sample.
760 * Return: The number of bits per sample.
762 unsigned int snd_hdac_stream_format_bits(snd_pcm_format_t format
, snd_pcm_subformat_t subformat
,
763 unsigned int maxbits
)
765 struct snd_pcm_hw_params params
;
768 memset(¶ms
, 0, sizeof(params
));
770 params_set_format(¶ms
, snd_hdac_format_normalize(format
));
771 snd_mask_set(hw_param_mask(¶ms
, SNDRV_PCM_HW_PARAM_SUBFORMAT
),
772 (__force
unsigned int)subformat
);
774 bits
= snd_pcm_hw_params_bits(¶ms
);
776 return min(bits
, maxbits
);
779 EXPORT_SYMBOL_GPL(snd_hdac_stream_format_bits
);
782 * snd_hdac_stream_format - convert format parameters to SDxFMT value.
783 * @channels: the number of channels.
784 * @bits: bits per sample.
785 * @rate: the sample rate.
787 * Return: The format bitset or zero if invalid.
789 unsigned int snd_hdac_stream_format(unsigned int channels
, unsigned int bits
, unsigned int rate
)
791 unsigned int val
= 0;
794 for (i
= 0; rate_bits
[i
].hz
; i
++) {
795 if (rate_bits
[i
].hz
== rate
) {
796 val
= rate_bits
[i
].hda_fmt
;
801 if (!rate_bits
[i
].hz
)
804 if (channels
== 0 || channels
> 8)
810 val
|= AC_FMT_BITS_8
;
813 val
|= AC_FMT_BITS_16
;
816 val
|= AC_FMT_BITS_20
;
819 val
|= AC_FMT_BITS_24
;
822 val
|= AC_FMT_BITS_32
;
830 EXPORT_SYMBOL_GPL(snd_hdac_stream_format
);
833 * snd_hdac_spdif_stream_format - convert format parameters to SDxFMT value.
834 * @channels: the number of channels.
835 * @bits: bits per sample.
836 * @rate: the sample rate.
837 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant).
839 * Return: The format bitset or zero if invalid.
841 unsigned int snd_hdac_spdif_stream_format(unsigned int channels
, unsigned int bits
,
842 unsigned int rate
, unsigned short spdif_ctls
)
844 unsigned int val
= snd_hdac_stream_format(channels
, bits
, rate
);
846 if (val
&& spdif_ctls
& AC_DIG1_NONAUDIO
)
847 val
|= AC_FMT_TYPE_NON_PCM
;
851 EXPORT_SYMBOL_GPL(snd_hdac_spdif_stream_format
);
853 static unsigned int query_pcm_param(struct hdac_device
*codec
, hda_nid_t nid
)
855 unsigned int val
= 0;
857 if (nid
!= codec
->afg
&&
858 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
859 val
= snd_hdac_read_parm(codec
, nid
, AC_PAR_PCM
);
860 if (!val
|| val
== -1)
861 val
= snd_hdac_read_parm(codec
, codec
->afg
, AC_PAR_PCM
);
862 if (!val
|| val
== -1)
867 static unsigned int query_stream_param(struct hdac_device
*codec
, hda_nid_t nid
)
869 unsigned int streams
= snd_hdac_read_parm(codec
, nid
, AC_PAR_STREAM
);
871 if (!streams
|| streams
== -1)
872 streams
= snd_hdac_read_parm(codec
, codec
->afg
, AC_PAR_STREAM
);
873 if (!streams
|| streams
== -1)
879 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
880 * @codec: the codec object
882 * @ratesp: the pointer to store the detected rate bitflags
883 * @formatsp: the pointer to store the detected formats
884 * @subformatsp: the pointer to store the detected subformats for S32_LE format
885 * @bpsp: the pointer to store the detected format widths
887 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp,
888 * @subformatsp or @bpsp argument is ignored.
890 * Returns 0 if successful, otherwise a negative error code.
892 int snd_hdac_query_supported_pcm(struct hdac_device
*codec
, hda_nid_t nid
,
893 u32
*ratesp
, u64
*formatsp
, u32
*subformatsp
,
896 unsigned int i
, val
, wcaps
;
898 wcaps
= get_wcaps(codec
, nid
);
899 val
= query_pcm_param(codec
, nid
);
903 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
905 rates
|= rate_bits
[i
].alsa_bits
;
909 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
911 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
917 if (formatsp
|| subformatsp
|| bpsp
) {
918 unsigned int streams
, bps
;
922 streams
= query_stream_param(codec
, nid
);
927 if (streams
& AC_SUPFMT_PCM
) {
928 if (val
& AC_SUPPCM_BITS_8
) {
929 formats
|= SNDRV_PCM_FMTBIT_U8
;
932 if (val
& AC_SUPPCM_BITS_16
) {
933 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
936 if (val
& AC_SUPPCM_BITS_20
) {
937 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
938 subformats
|= SNDRV_PCM_SUBFMTBIT_MSBITS_20
;
941 if (val
& AC_SUPPCM_BITS_24
) {
942 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
943 subformats
|= SNDRV_PCM_SUBFMTBIT_MSBITS_24
;
946 if (val
& AC_SUPPCM_BITS_32
) {
947 if (wcaps
& AC_WCAP_DIGITAL
) {
948 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
950 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
951 subformats
|= SNDRV_PCM_SUBFMTBIT_MSBITS_MAX
;
956 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
957 if (streams
& AC_SUPFMT_FLOAT32
) {
958 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
963 if (streams
== AC_SUPFMT_AC3
) {
964 /* should be exclusive */
965 /* temporary hack: we have still no proper support
966 * for the direct AC3 stream...
968 formats
|= SNDRV_PCM_FMTBIT_U8
;
973 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
975 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
982 *subformatsp
= subformats
;
989 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm
);
992 * snd_hdac_is_supported_format - Check the validity of the format
993 * @codec: the codec object
995 * @format: the HD-audio format value to check
997 * Check whether the given node supports the format value.
999 * Returns true if supported, false if not.
1001 bool snd_hdac_is_supported_format(struct hdac_device
*codec
, hda_nid_t nid
,
1002 unsigned int format
)
1005 unsigned int val
= 0, rate
, stream
;
1007 val
= query_pcm_param(codec
, nid
);
1011 rate
= format
& 0xff00;
1012 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
1013 if (rate_bits
[i
].hda_fmt
== rate
) {
1018 if (i
>= AC_PAR_PCM_RATE_BITS
)
1021 stream
= query_stream_param(codec
, nid
);
1025 if (stream
& AC_SUPFMT_PCM
) {
1026 switch (format
& 0xf0) {
1028 if (!(val
& AC_SUPPCM_BITS_8
))
1032 if (!(val
& AC_SUPPCM_BITS_16
))
1036 if (!(val
& AC_SUPPCM_BITS_20
))
1040 if (!(val
& AC_SUPPCM_BITS_24
))
1044 if (!(val
& AC_SUPPCM_BITS_32
))
1051 /* FIXME: check for float32 and AC3? */
1056 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format
);
1058 static unsigned int codec_read(struct hdac_device
*hdac
, hda_nid_t nid
,
1059 int flags
, unsigned int verb
, unsigned int parm
)
1061 unsigned int cmd
= snd_hdac_make_cmd(hdac
, nid
, verb
, parm
);
1064 if (snd_hdac_exec_verb(hdac
, cmd
, flags
, &res
))
1070 static int codec_write(struct hdac_device
*hdac
, hda_nid_t nid
,
1071 int flags
, unsigned int verb
, unsigned int parm
)
1073 unsigned int cmd
= snd_hdac_make_cmd(hdac
, nid
, verb
, parm
);
1075 return snd_hdac_exec_verb(hdac
, cmd
, flags
, NULL
);
1079 * snd_hdac_codec_read - send a command and get the response
1080 * @hdac: the HDAC device
1081 * @nid: NID to send the command
1082 * @flags: optional bit flags
1083 * @verb: the verb to send
1084 * @parm: the parameter for the verb
1086 * Send a single command and read the corresponding response.
1088 * Returns the obtained response value, or -1 for an error.
1090 int snd_hdac_codec_read(struct hdac_device
*hdac
, hda_nid_t nid
,
1091 int flags
, unsigned int verb
, unsigned int parm
)
1093 return codec_read(hdac
, nid
, flags
, verb
, parm
);
1095 EXPORT_SYMBOL_GPL(snd_hdac_codec_read
);
1098 * snd_hdac_codec_write - send a single command without waiting for response
1099 * @hdac: the HDAC device
1100 * @nid: NID to send the command
1101 * @flags: optional bit flags
1102 * @verb: the verb to send
1103 * @parm: the parameter for the verb
1105 * Send a single command without waiting for response.
1107 * Returns 0 if successful, or a negative error code.
1109 int snd_hdac_codec_write(struct hdac_device
*hdac
, hda_nid_t nid
,
1110 int flags
, unsigned int verb
, unsigned int parm
)
1112 return codec_write(hdac
, nid
, flags
, verb
, parm
);
1114 EXPORT_SYMBOL_GPL(snd_hdac_codec_write
);
1117 * snd_hdac_check_power_state - check whether the actual power state matches
1118 * with the target state
1120 * @hdac: the HDAC device
1121 * @nid: NID to send the command
1122 * @target_state: target state to check for
1124 * Return true if state matches, false if not
1126 bool snd_hdac_check_power_state(struct hdac_device
*hdac
,
1127 hda_nid_t nid
, unsigned int target_state
)
1129 unsigned int state
= codec_read(hdac
, nid
, 0,
1130 AC_VERB_GET_POWER_STATE
, 0);
1132 if (state
& AC_PWRST_ERROR
)
1134 state
= (state
>> 4) & 0x0f;
1135 return (state
== target_state
);
1137 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state
);
1139 * snd_hdac_sync_power_state - wait until actual power state matches
1140 * with the target state
1142 * @codec: the HDAC device
1143 * @nid: NID to send the command
1144 * @power_state: target power state to wait for
1146 * Return power state or PS_ERROR if codec rejects GET verb.
1148 unsigned int snd_hdac_sync_power_state(struct hdac_device
*codec
,
1149 hda_nid_t nid
, unsigned int power_state
)
1151 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
1152 unsigned int state
, actual_state
, count
;
1154 for (count
= 0; count
< 500; count
++) {
1155 state
= snd_hdac_codec_read(codec
, nid
, 0,
1156 AC_VERB_GET_POWER_STATE
, 0);
1157 if (state
& AC_PWRST_ERROR
) {
1161 actual_state
= (state
>> 4) & 0x0f;
1162 if (actual_state
== power_state
)
1164 if (time_after_eq(jiffies
, end_time
))
1166 /* wait until the codec reachs to the target state */
1171 EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state
);