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 pm_runtime_set_active(&codec->dev);
61 pm_runtime_get_noresume(&codec->dev);
62 atomic_set(&codec->in_pm, 0);
64 err = snd_hdac_bus_add_device(bus, codec);
69 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
71 if (codec->vendor_id == -1) {
72 /* read again, hopefully the access method was corrected
75 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
79 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
81 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
84 setup_fg_nodes(codec);
85 if (!codec->afg && !codec->mfg) {
86 dev_err(dev, "no AFG or MFG node found\n");
91 fg = codec->afg ? codec->afg : codec->mfg;
93 err = snd_hdac_refresh_widgets(codec);
97 codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
98 /* reread ssid if not set by parameter */
99 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
100 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
101 &codec->subsystem_id);
103 err = get_codec_vendor_name(codec);
107 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
108 codec->vendor_id & 0xffff);
109 if (!codec->chip_name) {
117 put_device(&codec->dev);
120 EXPORT_SYMBOL_GPL(snd_hdac_device_init);
123 * snd_hdac_device_exit - clean up the HD-audio codec base device
124 * @codec: device to clean up
126 void snd_hdac_device_exit(struct hdac_device *codec)
128 pm_runtime_put_noidle(&codec->dev);
129 snd_hdac_bus_remove_device(codec->bus, codec);
130 kfree(codec->vendor_name);
131 kfree(codec->chip_name);
133 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
136 * snd_hdac_device_register - register the hd-audio codec base device
137 * codec: the device to register
139 int snd_hdac_device_register(struct hdac_device *codec)
143 err = device_add(&codec->dev);
146 mutex_lock(&codec->widget_lock);
147 err = hda_widget_sysfs_init(codec);
148 mutex_unlock(&codec->widget_lock);
150 device_del(&codec->dev);
156 EXPORT_SYMBOL_GPL(snd_hdac_device_register);
159 * snd_hdac_device_unregister - unregister the hd-audio codec base device
160 * codec: the device to unregister
162 void snd_hdac_device_unregister(struct hdac_device *codec)
164 if (device_is_registered(&codec->dev)) {
165 mutex_lock(&codec->widget_lock);
166 hda_widget_sysfs_exit(codec);
167 mutex_unlock(&codec->widget_lock);
168 device_del(&codec->dev);
169 snd_hdac_bus_remove_device(codec->bus, codec);
172 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
175 * snd_hdac_device_set_chip_name - set/update the codec name
176 * @codec: the HDAC device
177 * @name: name string to set
179 * Returns 0 if the name is set or updated, or a negative error code.
181 int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
187 newname = kstrdup(name, GFP_KERNEL);
190 kfree(codec->chip_name);
191 codec->chip_name = newname;
194 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
197 * snd_hdac_codec_modalias - give the module alias name
198 * @codec: HDAC device
199 * @buf: string buffer to store
200 * @size: string buffer size
202 * Returns the size of string, like snprintf(), or a negative error code.
204 int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
206 return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
207 codec->vendor_id, codec->revision_id, codec->type);
209 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
212 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
213 * HD-audio controller
214 * @codec: the codec object
215 * @nid: NID to encode
216 * @verb: verb to encode
217 * @parm: parameter to encode
219 * Return an encoded command verb or -1 for error.
221 static unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
222 unsigned int verb, unsigned int parm)
227 if ((addr & ~0xf) || (nid & ~0x7f) ||
228 (verb & ~0xfff) || (parm & ~0xffff)) {
229 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
230 addr, nid, verb, parm);
235 val |= (u32)nid << 20;
242 * snd_hdac_exec_verb - execute an encoded verb
243 * @codec: the codec object
244 * @cmd: encoded verb to execute
245 * @flags: optional flags, pass zero for default
246 * @res: the pointer to store the result, NULL if running async
248 * Returns zero if successful, or a negative error code.
250 * This calls the exec_verb op when set in hdac_codec. If not,
251 * call the default snd_hdac_bus_exec_verb().
253 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
254 unsigned int flags, unsigned int *res)
256 if (codec->exec_verb)
257 return codec->exec_verb(codec, cmd, flags, res);
258 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
263 * snd_hdac_read - execute a verb
264 * @codec: the codec object
265 * @nid: NID to execute a verb
266 * @verb: verb to execute
267 * @parm: parameter for a verb
268 * @res: the pointer to store the result, NULL if running async
270 * Returns zero if successful, or a negative error code.
272 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
273 unsigned int verb, unsigned int parm, unsigned int *res)
275 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
277 return snd_hdac_exec_verb(codec, cmd, 0, res);
279 EXPORT_SYMBOL_GPL(snd_hdac_read);
282 * _snd_hdac_read_parm - read a parmeter
284 * This function returns zero or an error unlike snd_hdac_read_parm().
286 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
291 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
292 return snd_hdac_regmap_read_raw(codec, cmd, res);
294 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
297 * snd_hdac_read_parm_uncached - read a codec parameter without caching
298 * @codec: the codec object
299 * @nid: NID to read a parameter
300 * @parm: parameter to read
302 * Returns -1 for error. If you need to distinguish the error more
303 * strictly, use snd_hdac_read() directly.
305 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
308 unsigned int cmd, val;
310 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
311 if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0)
315 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
318 * snd_hdac_override_parm - override read-only parameters
319 * @codec: the codec object
320 * @nid: NID for the parameter
321 * @parm: the parameter to change
322 * @val: the parameter value to overwrite
324 int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
325 unsigned int parm, unsigned int val)
327 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
333 codec->caps_overwriting = true;
334 err = snd_hdac_regmap_write_raw(codec, verb, val);
335 codec->caps_overwriting = false;
338 EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
341 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
342 * @codec: the codec object
343 * @nid: NID to inspect
344 * @start_id: the pointer to store the starting NID
346 * Returns the number of subtree nodes or zero if not found.
347 * This function reads parameters always without caching.
349 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
354 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
359 *start_id = (parm >> 16) & 0x7fff;
360 return (int)(parm & 0x7fff);
362 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
365 * look for an AFG and MFG nodes
367 static void setup_fg_nodes(struct hdac_device *codec)
369 int i, total_nodes, function_id;
372 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
373 for (i = 0; i < total_nodes; i++, nid++) {
374 function_id = snd_hdac_read_parm(codec, nid,
375 AC_PAR_FUNCTION_TYPE);
376 switch (function_id & 0xff) {
377 case AC_GRP_AUDIO_FUNCTION:
379 codec->afg_function_id = function_id & 0xff;
380 codec->afg_unsol = (function_id >> 8) & 1;
382 case AC_GRP_MODEM_FUNCTION:
384 codec->mfg_function_id = function_id & 0xff;
385 codec->mfg_unsol = (function_id >> 8) & 1;
394 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
395 * @codec: the codec object
397 int snd_hdac_refresh_widgets(struct hdac_device *codec)
403 * Serialize against multiple threads trying to update the sysfs
406 mutex_lock(&codec->widget_lock);
407 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
408 if (!start_nid || nums <= 0 || nums >= 0xff) {
409 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
415 err = hda_widget_sysfs_reinit(codec, start_nid, nums);
419 codec->num_nodes = nums;
420 codec->start_nid = start_nid;
421 codec->end_nid = start_nid + nums;
423 mutex_unlock(&codec->widget_lock);
426 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
428 /* return CONNLIST_LEN parameter of the given widget */
429 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
431 unsigned int wcaps = get_wcaps(codec, nid);
434 if (!(wcaps & AC_WCAP_CONN_LIST) &&
435 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
438 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
445 * snd_hdac_get_connections - get a widget connection list
446 * @codec: the codec object
448 * @conn_list: the array to store the results, can be NULL
449 * @max_conns: the max size of the given array
451 * Returns the number of connected widgets, zero for no connection, or a
452 * negative error code. When the number of elements don't fit with the
453 * given array size, it returns -ENOSPC.
455 * When @conn_list is NULL, it just checks the number of connections.
457 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
458 hda_nid_t *conn_list, int max_conns)
461 int i, conn_len, conns, err;
462 unsigned int shift, num_elems, mask;
466 parm = get_num_conns(codec, nid);
470 if (parm & AC_CLIST_LONG) {
479 conn_len = parm & AC_CLIST_LENGTH;
480 mask = (1 << (shift-1)) - 1;
483 return 0; /* no connection */
486 /* single connection */
487 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
492 conn_list[0] = parm & mask;
496 /* multi connection */
499 for (i = 0; i < conn_len; i++) {
503 if (i % num_elems == 0) {
504 err = snd_hdac_read(codec, nid,
505 AC_VERB_GET_CONNECT_LIST, i,
510 range_val = !!(parm & (1 << (shift-1))); /* ranges */
512 if (val == 0 && null_count++) { /* no second chance */
514 "invalid CONNECT_LIST verb %x[%i]:%x\n",
520 /* ranges between the previous and this one */
521 if (!prev_nid || prev_nid >= val) {
522 dev_warn(&codec->dev,
523 "invalid dep_range_val %x:%x\n",
527 for (n = prev_nid + 1; n <= val; n++) {
529 if (conns >= max_conns)
531 conn_list[conns] = n;
537 if (conns >= max_conns)
539 conn_list[conns] = val;
547 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
551 * snd_hdac_power_up - power up the codec
552 * @codec: the codec object
554 * This function calls the runtime PM helper to power up the given codec.
555 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
556 * path that isn't included in PM path. Otherwise it gets stuck.
558 * Returns zero if successful, or a negative error code.
560 int snd_hdac_power_up(struct hdac_device *codec)
562 return pm_runtime_get_sync(&codec->dev);
564 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
567 * snd_hdac_power_down - power down the codec
568 * @codec: the codec object
570 * Returns zero if successful, or a negative error code.
572 int snd_hdac_power_down(struct hdac_device *codec)
574 struct device *dev = &codec->dev;
576 pm_runtime_mark_last_busy(dev);
577 return pm_runtime_put_autosuspend(dev);
579 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
582 * snd_hdac_power_up_pm - power up the codec
583 * @codec: the codec object
585 * This function can be called in a recursive code path like init code
586 * which may be called by PM suspend/resume again. OTOH, if a power-up
587 * call must wake up the sleeper (e.g. in a kctl callback), use
588 * snd_hdac_power_up() instead.
590 * Returns zero if successful, or a negative error code.
592 int snd_hdac_power_up_pm(struct hdac_device *codec)
594 if (!atomic_inc_not_zero(&codec->in_pm))
595 return snd_hdac_power_up(codec);
598 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
600 /* like snd_hdac_power_up_pm(), but only increment the pm count when
601 * already powered up. Returns -1 if not powered up, 1 if incremented
602 * or 0 if unchanged. Only used in hdac_regmap.c
604 int snd_hdac_keep_power_up(struct hdac_device *codec)
606 if (!atomic_inc_not_zero(&codec->in_pm)) {
607 int ret = pm_runtime_get_if_in_use(&codec->dev);
617 * snd_hdac_power_down_pm - power down the codec
618 * @codec: the codec object
620 * Like snd_hdac_power_up_pm(), this function is used in a recursive
621 * code path like init code which may be called by PM suspend/resume again.
623 * Returns zero if successful, or a negative error code.
625 int snd_hdac_power_down_pm(struct hdac_device *codec)
627 if (atomic_dec_if_positive(&codec->in_pm) < 0)
628 return snd_hdac_power_down(codec);
631 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
634 /* codec vendor labels */
635 struct hda_vendor_id {
640 static struct hda_vendor_id hda_vendor_ids[] = {
642 { 0x1013, "Cirrus Logic" },
643 { 0x1057, "Motorola" },
644 { 0x1095, "Silicon Image" },
645 { 0x10de, "Nvidia" },
646 { 0x10ec, "Realtek" },
647 { 0x1102, "Creative" },
651 { 0x11d4, "Analog Devices" },
652 { 0x13f6, "C-Media" },
653 { 0x14f1, "Conexant" },
654 { 0x17e8, "Chrontel" },
656 { 0x1aec, "Wolfson Microelectronics" },
658 { 0x434d, "C-Media" },
660 { 0x8384, "SigmaTel" },
664 /* store the codec vendor name */
665 static int get_codec_vendor_name(struct hdac_device *codec)
667 const struct hda_vendor_id *c;
668 u16 vendor_id = codec->vendor_id >> 16;
670 for (c = hda_vendor_ids; c->id; c++) {
671 if (c->id == vendor_id) {
672 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
673 return codec->vendor_name ? 0 : -ENOMEM;
677 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
678 return codec->vendor_name ? 0 : -ENOMEM;
684 struct hda_rate_tbl {
686 unsigned int alsa_bits;
687 unsigned int hda_fmt;
690 /* rate = base * mult / div */
691 #define HDA_RATE(base, mult, div) \
692 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
693 (((div) - 1) << AC_FMT_DIV_SHIFT))
695 static struct hda_rate_tbl rate_bits[] = {
696 /* rate in Hz, ALSA rate bitmask, HDA format value */
698 /* autodetected value used in snd_hda_query_supported_pcm */
699 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
700 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
701 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
702 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
703 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
704 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
705 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
706 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
707 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
708 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
709 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
710 #define AC_PAR_PCM_RATE_BITS 11
711 /* up to bits 10, 384kHZ isn't supported properly */
713 /* not autodetected value */
714 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
716 { 0 } /* terminator */
720 * snd_hdac_calc_stream_format - calculate the format bitset
721 * @rate: the sample rate
722 * @channels: the number of channels
723 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
724 * @maxbps: the max. bps
725 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
727 * Calculate the format bitset from the given rate, channels and th PCM format.
729 * Return zero if invalid.
731 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
732 unsigned int channels,
733 snd_pcm_format_t format,
735 unsigned short spdif_ctls)
738 unsigned int val = 0;
740 for (i = 0; rate_bits[i].hz; i++)
741 if (rate_bits[i].hz == rate) {
742 val = rate_bits[i].hda_fmt;
745 if (!rate_bits[i].hz)
748 if (channels == 0 || channels > 8)
752 switch (snd_pcm_format_width(format)) {
754 val |= AC_FMT_BITS_8;
757 val |= AC_FMT_BITS_16;
762 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
763 val |= AC_FMT_BITS_32;
764 else if (maxbps >= 24)
765 val |= AC_FMT_BITS_24;
767 val |= AC_FMT_BITS_20;
773 if (spdif_ctls & AC_DIG1_NONAUDIO)
774 val |= AC_FMT_TYPE_NON_PCM;
778 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
780 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
782 unsigned int val = 0;
784 if (nid != codec->afg &&
785 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
786 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
787 if (!val || val == -1)
788 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
789 if (!val || val == -1)
794 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
796 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
798 if (!streams || streams == -1)
799 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
800 if (!streams || streams == -1)
806 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
807 * @codec: the codec object
809 * @ratesp: the pointer to store the detected rate bitflags
810 * @formatsp: the pointer to store the detected formats
811 * @bpsp: the pointer to store the detected format widths
813 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
814 * or @bsps argument is ignored.
816 * Returns 0 if successful, otherwise a negative error code.
818 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
819 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
821 unsigned int i, val, wcaps;
823 wcaps = get_wcaps(codec, nid);
824 val = query_pcm_param(codec, nid);
828 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
830 rates |= rate_bits[i].alsa_bits;
834 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
836 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
842 if (formatsp || bpsp) {
844 unsigned int streams, bps;
846 streams = query_stream_param(codec, nid);
851 if (streams & AC_SUPFMT_PCM) {
852 if (val & AC_SUPPCM_BITS_8) {
853 formats |= SNDRV_PCM_FMTBIT_U8;
856 if (val & AC_SUPPCM_BITS_16) {
857 formats |= SNDRV_PCM_FMTBIT_S16_LE;
860 if (wcaps & AC_WCAP_DIGITAL) {
861 if (val & AC_SUPPCM_BITS_32)
862 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
863 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
864 formats |= SNDRV_PCM_FMTBIT_S32_LE;
865 if (val & AC_SUPPCM_BITS_24)
867 else if (val & AC_SUPPCM_BITS_20)
869 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
870 AC_SUPPCM_BITS_32)) {
871 formats |= SNDRV_PCM_FMTBIT_S32_LE;
872 if (val & AC_SUPPCM_BITS_32)
874 else if (val & AC_SUPPCM_BITS_24)
876 else if (val & AC_SUPPCM_BITS_20)
880 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
881 if (streams & AC_SUPFMT_FLOAT32) {
882 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
887 if (streams == AC_SUPFMT_AC3) {
888 /* should be exclusive */
889 /* temporary hack: we have still no proper support
890 * for the direct AC3 stream...
892 formats |= SNDRV_PCM_FMTBIT_U8;
897 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
899 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
911 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
914 * snd_hdac_is_supported_format - Check the validity of the format
915 * @codec: the codec object
917 * @format: the HD-audio format value to check
919 * Check whether the given node supports the format value.
921 * Returns true if supported, false if not.
923 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
927 unsigned int val = 0, rate, stream;
929 val = query_pcm_param(codec, nid);
933 rate = format & 0xff00;
934 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
935 if (rate_bits[i].hda_fmt == rate) {
940 if (i >= AC_PAR_PCM_RATE_BITS)
943 stream = query_stream_param(codec, nid);
947 if (stream & AC_SUPFMT_PCM) {
948 switch (format & 0xf0) {
950 if (!(val & AC_SUPPCM_BITS_8))
954 if (!(val & AC_SUPPCM_BITS_16))
958 if (!(val & AC_SUPPCM_BITS_20))
962 if (!(val & AC_SUPPCM_BITS_24))
966 if (!(val & AC_SUPPCM_BITS_32))
973 /* FIXME: check for float32 and AC3? */
978 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
980 static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
981 int flags, unsigned int verb, unsigned int parm)
983 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
986 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
992 static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
993 int flags, unsigned int verb, unsigned int parm)
995 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
997 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1001 * snd_hdac_codec_read - send a command and get the response
1002 * @hdac: the HDAC device
1003 * @nid: NID to send the command
1004 * @flags: optional bit flags
1005 * @verb: the verb to send
1006 * @parm: the parameter for the verb
1008 * Send a single command and read the corresponding response.
1010 * Returns the obtained response value, or -1 for an error.
1012 int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1013 int flags, unsigned int verb, unsigned int parm)
1015 return codec_read(hdac, nid, flags, verb, parm);
1017 EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1020 * snd_hdac_codec_write - send a single command without waiting for response
1021 * @hdac: the HDAC device
1022 * @nid: NID to send the command
1023 * @flags: optional bit flags
1024 * @verb: the verb to send
1025 * @parm: the parameter for the verb
1027 * Send a single command without waiting for response.
1029 * Returns 0 if successful, or a negative error code.
1031 int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1032 int flags, unsigned int verb, unsigned int parm)
1034 return codec_write(hdac, nid, flags, verb, parm);
1036 EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1039 * snd_hdac_check_power_state - check whether the actual power state matches
1040 * with the target state
1042 * @hdac: the HDAC device
1043 * @nid: NID to send the command
1044 * @target_state: target state to check for
1046 * Return true if state matches, false if not
1048 bool snd_hdac_check_power_state(struct hdac_device *hdac,
1049 hda_nid_t nid, unsigned int target_state)
1051 unsigned int state = codec_read(hdac, nid, 0,
1052 AC_VERB_GET_POWER_STATE, 0);
1054 if (state & AC_PWRST_ERROR)
1056 state = (state >> 4) & 0x0f;
1057 return (state == target_state);
1059 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);
1061 * snd_hdac_sync_power_state - wait until actual power state matches
1062 * with the target state
1064 * @hdac: the HDAC device
1065 * @nid: NID to send the command
1066 * @target_state: target state to check for
1068 * Return power state or PS_ERROR if codec rejects GET verb.
1070 unsigned int snd_hdac_sync_power_state(struct hdac_device *codec,
1071 hda_nid_t nid, unsigned int power_state)
1073 unsigned long end_time = jiffies + msecs_to_jiffies(500);
1074 unsigned int state, actual_state, count;
1076 for (count = 0; count < 500; count++) {
1077 state = snd_hdac_codec_read(codec, nid, 0,
1078 AC_VERB_GET_POWER_STATE, 0);
1079 if (state & AC_PWRST_ERROR) {
1083 actual_state = (state >> 4) & 0x0f;
1084 if (actual_state == power_state)
1086 if (time_after_eq(jiffies, end_time))
1088 /* wait until the codec reachs to the target state */
1093 EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state);