2 * HD-audio codec core device
5 #include <linux/init.h>
6 #include <linux/device.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/export.h>
10 #include <linux/pm_runtime.h>
11 #include <sound/hdaudio.h>
12 #include <sound/hda_regmap.h>
13 #include <sound/pcm.h>
16 static void setup_fg_nodes(struct hdac_device *codec);
17 static int get_codec_vendor_name(struct hdac_device *codec);
19 static void default_release(struct device *dev)
21 snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
25 * snd_hdac_device_init - initialize the HD-audio codec base device
26 * @codec: device to initialize
28 * @name: device name string
29 * @addr: codec address
31 * Returns zero for success or a negative error code.
33 * This function increments the runtime PM counter and marks it active.
34 * The caller needs to turn it off appropriately later.
36 * The caller needs to set the device's release op properly by itself.
38 int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
39 const char *name, unsigned int addr)
46 device_initialize(dev);
47 dev->parent = bus->dev;
48 dev->bus = &snd_hda_bus_type;
49 dev->release = default_release;
50 dev->groups = hdac_dev_attr_groups;
51 dev_set_name(dev, "%s", name);
52 device_enable_async_suspend(dev);
56 codec->type = HDA_DEV_CORE;
57 pm_runtime_set_active(&codec->dev);
58 pm_runtime_get_noresume(&codec->dev);
59 atomic_set(&codec->in_pm, 0);
61 err = snd_hdac_bus_add_device(bus, codec);
66 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
68 if (codec->vendor_id == -1) {
69 /* read again, hopefully the access method was corrected
72 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
76 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
78 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
81 setup_fg_nodes(codec);
82 if (!codec->afg && !codec->mfg) {
83 dev_err(dev, "no AFG or MFG node found\n");
88 fg = codec->afg ? codec->afg : codec->mfg;
90 err = snd_hdac_refresh_widgets(codec);
94 codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
95 /* reread ssid if not set by parameter */
96 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
97 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
98 &codec->subsystem_id);
100 err = get_codec_vendor_name(codec);
104 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
105 codec->vendor_id & 0xffff);
106 if (!codec->chip_name) {
114 put_device(&codec->dev);
117 EXPORT_SYMBOL_GPL(snd_hdac_device_init);
120 * snd_hdac_device_exit - clean up the HD-audio codec base device
121 * @codec: device to clean up
123 void snd_hdac_device_exit(struct hdac_device *codec)
125 pm_runtime_put_noidle(&codec->dev);
126 snd_hdac_bus_remove_device(codec->bus, codec);
127 kfree(codec->vendor_name);
128 kfree(codec->chip_name);
130 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
133 * snd_hdac_device_register - register the hd-audio codec base device
134 * codec: the device to register
136 int snd_hdac_device_register(struct hdac_device *codec)
140 err = device_add(&codec->dev);
143 err = hda_widget_sysfs_init(codec);
145 device_del(&codec->dev);
151 EXPORT_SYMBOL_GPL(snd_hdac_device_register);
154 * snd_hdac_device_unregister - unregister the hd-audio codec base device
155 * codec: the device to unregister
157 void snd_hdac_device_unregister(struct hdac_device *codec)
159 if (device_is_registered(&codec->dev)) {
160 hda_widget_sysfs_exit(codec);
161 device_del(&codec->dev);
164 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
167 * snd_hdac_device_set_chip_name - set/update the codec name
168 * @codec: the HDAC device
169 * @name: name string to set
171 * Returns 0 if the name is set or updated, or a negative error code.
173 int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
179 newname = kstrdup(name, GFP_KERNEL);
182 kfree(codec->chip_name);
183 codec->chip_name = newname;
186 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
189 * snd_hdac_codec_modalias - give the module alias name
190 * @codec: HDAC device
191 * @buf: string buffer to store
192 * @size: string buffer size
194 * Returns the size of string, like snprintf(), or a negative error code.
196 int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
198 return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
199 codec->vendor_id, codec->revision_id, codec->type);
201 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
204 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
205 * HD-audio controller
206 * @codec: the codec object
207 * @nid: NID to encode
208 * @verb: verb to encode
209 * @parm: parameter to encode
211 * Return an encoded command verb or -1 for error.
213 unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
214 unsigned int verb, unsigned int parm)
219 if ((addr & ~0xf) || (nid & ~0x7f) ||
220 (verb & ~0xfff) || (parm & ~0xffff)) {
221 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
222 addr, nid, verb, parm);
227 val |= (u32)nid << 20;
232 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
235 * snd_hdac_exec_verb - execute an encoded verb
236 * @codec: the codec object
237 * @cmd: encoded verb to execute
238 * @flags: optional flags, pass zero for default
239 * @res: the pointer to store the result, NULL if running async
241 * Returns zero if successful, or a negative error code.
243 * This calls the exec_verb op when set in hdac_codec. If not,
244 * call the default snd_hdac_bus_exec_verb().
246 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
247 unsigned int flags, unsigned int *res)
249 if (codec->exec_verb)
250 return codec->exec_verb(codec, cmd, flags, res);
251 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
253 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
257 * snd_hdac_read - execute a verb
258 * @codec: the codec object
259 * @nid: NID to execute a verb
260 * @verb: verb to execute
261 * @parm: parameter for a verb
262 * @res: the pointer to store the result, NULL if running async
264 * Returns zero if successful, or a negative error code.
266 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
267 unsigned int verb, unsigned int parm, unsigned int *res)
269 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
271 return snd_hdac_exec_verb(codec, cmd, 0, res);
273 EXPORT_SYMBOL_GPL(snd_hdac_read);
276 * _snd_hdac_read_parm - read a parmeter
278 * This function returns zero or an error unlike snd_hdac_read_parm().
280 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
285 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
286 return snd_hdac_regmap_read_raw(codec, cmd, res);
288 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
291 * snd_hdac_read_parm_uncached - read a codec parameter without caching
292 * @codec: the codec object
293 * @nid: NID to read a parameter
294 * @parm: parameter to read
296 * Returns -1 for error. If you need to distinguish the error more
297 * strictly, use snd_hdac_read() directly.
299 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
305 regcache_cache_bypass(codec->regmap, true);
306 val = snd_hdac_read_parm(codec, nid, parm);
308 regcache_cache_bypass(codec->regmap, false);
311 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
314 * snd_hdac_override_parm - override read-only parameters
315 * @codec: the codec object
316 * @nid: NID for the parameter
317 * @parm: the parameter to change
318 * @val: the parameter value to overwrite
320 int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
321 unsigned int parm, unsigned int val)
323 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
329 codec->caps_overwriting = true;
330 err = snd_hdac_regmap_write_raw(codec, verb, val);
331 codec->caps_overwriting = false;
334 EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
337 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
338 * @codec: the codec object
339 * @nid: NID to inspect
340 * @start_id: the pointer to store the starting NID
342 * Returns the number of subtree nodes or zero if not found.
343 * This function reads parameters always without caching.
345 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
350 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
355 *start_id = (parm >> 16) & 0x7fff;
356 return (int)(parm & 0x7fff);
358 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
361 * look for an AFG and MFG nodes
363 static void setup_fg_nodes(struct hdac_device *codec)
365 int i, total_nodes, function_id;
368 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
369 for (i = 0; i < total_nodes; i++, nid++) {
370 function_id = snd_hdac_read_parm(codec, nid,
371 AC_PAR_FUNCTION_TYPE);
372 switch (function_id & 0xff) {
373 case AC_GRP_AUDIO_FUNCTION:
375 codec->afg_function_id = function_id & 0xff;
376 codec->afg_unsol = (function_id >> 8) & 1;
378 case AC_GRP_MODEM_FUNCTION:
380 codec->mfg_function_id = function_id & 0xff;
381 codec->mfg_unsol = (function_id >> 8) & 1;
390 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
391 * @codec: the codec object
393 int snd_hdac_refresh_widgets(struct hdac_device *codec)
398 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
399 if (!start_nid || nums <= 0 || nums >= 0xff) {
400 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
405 codec->num_nodes = nums;
406 codec->start_nid = start_nid;
407 codec->end_nid = start_nid + nums;
410 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
413 * snd_hdac_refresh_widget_sysfs - Reset the codec widgets and reinit the
415 * @codec: the codec object
417 * first we need to remove sysfs, then refresh widgets and lastly
420 int snd_hdac_refresh_widget_sysfs(struct hdac_device *codec)
424 if (device_is_registered(&codec->dev))
425 hda_widget_sysfs_exit(codec);
426 ret = snd_hdac_refresh_widgets(codec);
428 dev_err(&codec->dev, "failed to refresh widget: %d\n", ret);
431 if (device_is_registered(&codec->dev)) {
432 ret = hda_widget_sysfs_init(codec);
434 dev_err(&codec->dev, "failed to init sysfs: %d\n", ret);
440 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widget_sysfs);
442 /* return CONNLIST_LEN parameter of the given widget */
443 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
445 unsigned int wcaps = get_wcaps(codec, nid);
448 if (!(wcaps & AC_WCAP_CONN_LIST) &&
449 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
452 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
459 * snd_hdac_get_connections - get a widget connection list
460 * @codec: the codec object
462 * @conn_list: the array to store the results, can be NULL
463 * @max_conns: the max size of the given array
465 * Returns the number of connected widgets, zero for no connection, or a
466 * negative error code. When the number of elements don't fit with the
467 * given array size, it returns -ENOSPC.
469 * When @conn_list is NULL, it just checks the number of connections.
471 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
472 hda_nid_t *conn_list, int max_conns)
475 int i, conn_len, conns, err;
476 unsigned int shift, num_elems, mask;
480 parm = get_num_conns(codec, nid);
484 if (parm & AC_CLIST_LONG) {
493 conn_len = parm & AC_CLIST_LENGTH;
494 mask = (1 << (shift-1)) - 1;
497 return 0; /* no connection */
500 /* single connection */
501 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
506 conn_list[0] = parm & mask;
510 /* multi connection */
513 for (i = 0; i < conn_len; i++) {
517 if (i % num_elems == 0) {
518 err = snd_hdac_read(codec, nid,
519 AC_VERB_GET_CONNECT_LIST, i,
524 range_val = !!(parm & (1 << (shift-1))); /* ranges */
526 if (val == 0 && null_count++) { /* no second chance */
528 "invalid CONNECT_LIST verb %x[%i]:%x\n",
534 /* ranges between the previous and this one */
535 if (!prev_nid || prev_nid >= val) {
536 dev_warn(&codec->dev,
537 "invalid dep_range_val %x:%x\n",
541 for (n = prev_nid + 1; n <= val; n++) {
543 if (conns >= max_conns)
545 conn_list[conns] = n;
551 if (conns >= max_conns)
553 conn_list[conns] = val;
561 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
565 * snd_hdac_power_up - power up the codec
566 * @codec: the codec object
568 * This function calls the runtime PM helper to power up the given codec.
569 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
570 * path that isn't included in PM path. Otherwise it gets stuck.
572 * Returns zero if successful, or a negative error code.
574 int snd_hdac_power_up(struct hdac_device *codec)
576 return pm_runtime_get_sync(&codec->dev);
578 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
581 * snd_hdac_power_down - power down the codec
582 * @codec: the codec object
584 * Returns zero if successful, or a negative error code.
586 int snd_hdac_power_down(struct hdac_device *codec)
588 struct device *dev = &codec->dev;
590 pm_runtime_mark_last_busy(dev);
591 return pm_runtime_put_autosuspend(dev);
593 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
596 * snd_hdac_power_up_pm - power up the codec
597 * @codec: the codec object
599 * This function can be called in a recursive code path like init code
600 * which may be called by PM suspend/resume again. OTOH, if a power-up
601 * call must wake up the sleeper (e.g. in a kctl callback), use
602 * snd_hdac_power_up() instead.
604 * Returns zero if successful, or a negative error code.
606 int snd_hdac_power_up_pm(struct hdac_device *codec)
608 if (!atomic_inc_not_zero(&codec->in_pm))
609 return snd_hdac_power_up(codec);
612 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
614 /* like snd_hdac_power_up_pm(), but only increment the pm count when
615 * already powered up. Returns -1 if not powered up, 1 if incremented
616 * or 0 if unchanged. Only used in hdac_regmap.c
618 int snd_hdac_keep_power_up(struct hdac_device *codec)
620 if (!atomic_inc_not_zero(&codec->in_pm)) {
621 int ret = pm_runtime_get_if_in_use(&codec->dev);
631 * snd_hdac_power_down_pm - power down the codec
632 * @codec: the codec object
634 * Like snd_hdac_power_up_pm(), this function is used in a recursive
635 * code path like init code which may be called by PM suspend/resume again.
637 * Returns zero if successful, or a negative error code.
639 int snd_hdac_power_down_pm(struct hdac_device *codec)
641 if (atomic_dec_if_positive(&codec->in_pm) < 0)
642 return snd_hdac_power_down(codec);
645 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
649 * snd_hdac_link_power - Enable/disable the link power for a codec
650 * @codec: the codec object
651 * @bool: enable or disable the link power
653 int snd_hdac_link_power(struct hdac_device *codec, bool enable)
655 if (!codec->link_power_control)
658 if (codec->bus->ops->link_power)
659 return codec->bus->ops->link_power(codec->bus, enable);
663 EXPORT_SYMBOL_GPL(snd_hdac_link_power);
665 /* codec vendor labels */
666 struct hda_vendor_id {
671 static struct hda_vendor_id hda_vendor_ids[] = {
673 { 0x1013, "Cirrus Logic" },
674 { 0x1057, "Motorola" },
675 { 0x1095, "Silicon Image" },
676 { 0x10de, "Nvidia" },
677 { 0x10ec, "Realtek" },
678 { 0x1102, "Creative" },
682 { 0x11d4, "Analog Devices" },
683 { 0x13f6, "C-Media" },
684 { 0x14f1, "Conexant" },
685 { 0x17e8, "Chrontel" },
687 { 0x1aec, "Wolfson Microelectronics" },
689 { 0x434d, "C-Media" },
691 { 0x8384, "SigmaTel" },
695 /* store the codec vendor name */
696 static int get_codec_vendor_name(struct hdac_device *codec)
698 const struct hda_vendor_id *c;
699 u16 vendor_id = codec->vendor_id >> 16;
701 for (c = hda_vendor_ids; c->id; c++) {
702 if (c->id == vendor_id) {
703 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
704 return codec->vendor_name ? 0 : -ENOMEM;
708 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
709 return codec->vendor_name ? 0 : -ENOMEM;
715 struct hda_rate_tbl {
717 unsigned int alsa_bits;
718 unsigned int hda_fmt;
721 /* rate = base * mult / div */
722 #define HDA_RATE(base, mult, div) \
723 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
724 (((div) - 1) << AC_FMT_DIV_SHIFT))
726 static struct hda_rate_tbl rate_bits[] = {
727 /* rate in Hz, ALSA rate bitmask, HDA format value */
729 /* autodetected value used in snd_hda_query_supported_pcm */
730 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
731 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
732 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
733 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
734 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
735 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
736 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
737 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
738 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
739 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
740 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
741 #define AC_PAR_PCM_RATE_BITS 11
742 /* up to bits 10, 384kHZ isn't supported properly */
744 /* not autodetected value */
745 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
747 { 0 } /* terminator */
751 * snd_hdac_calc_stream_format - calculate the format bitset
752 * @rate: the sample rate
753 * @channels: the number of channels
754 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
755 * @maxbps: the max. bps
756 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
758 * Calculate the format bitset from the given rate, channels and th PCM format.
760 * Return zero if invalid.
762 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
763 unsigned int channels,
766 unsigned short spdif_ctls)
769 unsigned int val = 0;
771 for (i = 0; rate_bits[i].hz; i++)
772 if (rate_bits[i].hz == rate) {
773 val = rate_bits[i].hda_fmt;
776 if (!rate_bits[i].hz)
779 if (channels == 0 || channels > 8)
783 switch (snd_pcm_format_width(format)) {
785 val |= AC_FMT_BITS_8;
788 val |= AC_FMT_BITS_16;
793 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
794 val |= AC_FMT_BITS_32;
795 else if (maxbps >= 24)
796 val |= AC_FMT_BITS_24;
798 val |= AC_FMT_BITS_20;
804 if (spdif_ctls & AC_DIG1_NONAUDIO)
805 val |= AC_FMT_TYPE_NON_PCM;
809 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
811 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
813 unsigned int val = 0;
815 if (nid != codec->afg &&
816 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
817 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
818 if (!val || val == -1)
819 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
820 if (!val || val == -1)
825 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
827 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
829 if (!streams || streams == -1)
830 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
831 if (!streams || streams == -1)
837 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
838 * @codec: the codec object
840 * @ratesp: the pointer to store the detected rate bitflags
841 * @formatsp: the pointer to store the detected formats
842 * @bpsp: the pointer to store the detected format widths
844 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
845 * or @bsps argument is ignored.
847 * Returns 0 if successful, otherwise a negative error code.
849 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
850 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
852 unsigned int i, val, wcaps;
854 wcaps = get_wcaps(codec, nid);
855 val = query_pcm_param(codec, nid);
859 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
861 rates |= rate_bits[i].alsa_bits;
865 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
867 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
873 if (formatsp || bpsp) {
875 unsigned int streams, bps;
877 streams = query_stream_param(codec, nid);
882 if (streams & AC_SUPFMT_PCM) {
883 if (val & AC_SUPPCM_BITS_8) {
884 formats |= SNDRV_PCM_FMTBIT_U8;
887 if (val & AC_SUPPCM_BITS_16) {
888 formats |= SNDRV_PCM_FMTBIT_S16_LE;
891 if (wcaps & AC_WCAP_DIGITAL) {
892 if (val & AC_SUPPCM_BITS_32)
893 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
894 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
895 formats |= SNDRV_PCM_FMTBIT_S32_LE;
896 if (val & AC_SUPPCM_BITS_24)
898 else if (val & AC_SUPPCM_BITS_20)
900 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
901 AC_SUPPCM_BITS_32)) {
902 formats |= SNDRV_PCM_FMTBIT_S32_LE;
903 if (val & AC_SUPPCM_BITS_32)
905 else if (val & AC_SUPPCM_BITS_24)
907 else if (val & AC_SUPPCM_BITS_20)
911 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
912 if (streams & AC_SUPFMT_FLOAT32) {
913 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
918 if (streams == AC_SUPFMT_AC3) {
919 /* should be exclusive */
920 /* temporary hack: we have still no proper support
921 * for the direct AC3 stream...
923 formats |= SNDRV_PCM_FMTBIT_U8;
928 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
930 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
942 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
945 * snd_hdac_is_supported_format - Check the validity of the format
946 * @codec: the codec object
948 * @format: the HD-audio format value to check
950 * Check whether the given node supports the format value.
952 * Returns true if supported, false if not.
954 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
958 unsigned int val = 0, rate, stream;
960 val = query_pcm_param(codec, nid);
964 rate = format & 0xff00;
965 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
966 if (rate_bits[i].hda_fmt == rate) {
971 if (i >= AC_PAR_PCM_RATE_BITS)
974 stream = query_stream_param(codec, nid);
978 if (stream & AC_SUPFMT_PCM) {
979 switch (format & 0xf0) {
981 if (!(val & AC_SUPPCM_BITS_8))
985 if (!(val & AC_SUPPCM_BITS_16))
989 if (!(val & AC_SUPPCM_BITS_20))
993 if (!(val & AC_SUPPCM_BITS_24))
997 if (!(val & AC_SUPPCM_BITS_32))
1004 /* FIXME: check for float32 and AC3? */
1009 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
1011 static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
1012 int flags, unsigned int verb, unsigned int parm)
1014 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1017 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
1023 static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
1024 int flags, unsigned int verb, unsigned int parm)
1026 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1028 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1032 * snd_hdac_codec_read - send a command and get the response
1033 * @hdac: the HDAC device
1034 * @nid: NID to send the command
1035 * @flags: optional bit flags
1036 * @verb: the verb to send
1037 * @parm: the parameter for the verb
1039 * Send a single command and read the corresponding response.
1041 * Returns the obtained response value, or -1 for an error.
1043 int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1044 int flags, unsigned int verb, unsigned int parm)
1046 return codec_read(hdac, nid, flags, verb, parm);
1048 EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1051 * snd_hdac_codec_write - send a single command without waiting for response
1052 * @hdac: the HDAC device
1053 * @nid: NID to send the command
1054 * @flags: optional bit flags
1055 * @verb: the verb to send
1056 * @parm: the parameter for the verb
1058 * Send a single command without waiting for response.
1060 * Returns 0 if successful, or a negative error code.
1062 int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1063 int flags, unsigned int verb, unsigned int parm)
1065 return codec_write(hdac, nid, flags, verb, parm);
1067 EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1070 * snd_hdac_check_power_state - check whether the actual power state matches
1071 * with the target state
1073 * @hdac: the HDAC device
1074 * @nid: NID to send the command
1075 * @target_state: target state to check for
1077 * Return true if state matches, false if not
1079 bool snd_hdac_check_power_state(struct hdac_device *hdac,
1080 hda_nid_t nid, unsigned int target_state)
1082 unsigned int state = codec_read(hdac, nid, 0,
1083 AC_VERB_GET_POWER_STATE, 0);
1085 if (state & AC_PWRST_ERROR)
1087 state = (state >> 4) & 0x0f;
1088 return (state == target_state);
1090 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);