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 /* keep balance of runtime PM child_count in parent device */
127 pm_runtime_set_suspended(&codec->dev);
128 snd_hdac_bus_remove_device(codec->bus, codec);
129 kfree(codec->vendor_name);
130 kfree(codec->chip_name);
132 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
135 * snd_hdac_device_register - register the hd-audio codec base device
136 * codec: the device to register
138 int snd_hdac_device_register(struct hdac_device *codec)
142 err = device_add(&codec->dev);
145 err = hda_widget_sysfs_init(codec);
147 device_del(&codec->dev);
153 EXPORT_SYMBOL_GPL(snd_hdac_device_register);
156 * snd_hdac_device_unregister - unregister the hd-audio codec base device
157 * codec: the device to unregister
159 void snd_hdac_device_unregister(struct hdac_device *codec)
161 if (device_is_registered(&codec->dev)) {
162 hda_widget_sysfs_exit(codec);
163 device_del(&codec->dev);
166 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
169 * snd_hdac_device_set_chip_name - set/update the codec name
170 * @codec: the HDAC device
171 * @name: name string to set
173 * Returns 0 if the name is set or updated, or a negative error code.
175 int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
181 newname = kstrdup(name, GFP_KERNEL);
184 kfree(codec->chip_name);
185 codec->chip_name = newname;
188 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
191 * snd_hdac_codec_modalias - give the module alias name
192 * @codec: HDAC device
193 * @buf: string buffer to store
194 * @size: string buffer size
196 * Returns the size of string, like snprintf(), or a negative error code.
198 int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
200 return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
201 codec->vendor_id, codec->revision_id, codec->type);
203 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
206 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
207 * HD-audio controller
208 * @codec: the codec object
209 * @nid: NID to encode
210 * @verb: verb to encode
211 * @parm: parameter to encode
213 * Return an encoded command verb or -1 for error.
215 unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
216 unsigned int verb, unsigned int parm)
221 if ((addr & ~0xf) || (nid & ~0x7f) ||
222 (verb & ~0xfff) || (parm & ~0xffff)) {
223 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
224 addr, nid, verb, parm);
229 val |= (u32)nid << 20;
234 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
237 * snd_hdac_exec_verb - execute an encoded verb
238 * @codec: the codec object
239 * @cmd: encoded verb to execute
240 * @flags: optional flags, pass zero for default
241 * @res: the pointer to store the result, NULL if running async
243 * Returns zero if successful, or a negative error code.
245 * This calls the exec_verb op when set in hdac_codec. If not,
246 * call the default snd_hdac_bus_exec_verb().
248 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
249 unsigned int flags, unsigned int *res)
251 if (codec->exec_verb)
252 return codec->exec_verb(codec, cmd, flags, res);
253 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
255 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
259 * snd_hdac_read - execute a verb
260 * @codec: the codec object
261 * @nid: NID to execute a verb
262 * @verb: verb to execute
263 * @parm: parameter for a verb
264 * @res: the pointer to store the result, NULL if running async
266 * Returns zero if successful, or a negative error code.
268 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
269 unsigned int verb, unsigned int parm, unsigned int *res)
271 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
273 return snd_hdac_exec_verb(codec, cmd, 0, res);
275 EXPORT_SYMBOL_GPL(snd_hdac_read);
278 * _snd_hdac_read_parm - read a parmeter
280 * This function returns zero or an error unlike snd_hdac_read_parm().
282 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
287 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
288 return snd_hdac_regmap_read_raw(codec, cmd, res);
290 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
293 * snd_hdac_read_parm_uncached - read a codec parameter without caching
294 * @codec: the codec object
295 * @nid: NID to read a parameter
296 * @parm: parameter to read
298 * Returns -1 for error. If you need to distinguish the error more
299 * strictly, use snd_hdac_read() directly.
301 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
307 regcache_cache_bypass(codec->regmap, true);
308 val = snd_hdac_read_parm(codec, nid, parm);
310 regcache_cache_bypass(codec->regmap, false);
313 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
316 * snd_hdac_override_parm - override read-only parameters
317 * @codec: the codec object
318 * @nid: NID for the parameter
319 * @parm: the parameter to change
320 * @val: the parameter value to overwrite
322 int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
323 unsigned int parm, unsigned int val)
325 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
331 codec->caps_overwriting = true;
332 err = snd_hdac_regmap_write_raw(codec, verb, val);
333 codec->caps_overwriting = false;
336 EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
339 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
340 * @codec: the codec object
341 * @nid: NID to inspect
342 * @start_id: the pointer to store the starting NID
344 * Returns the number of subtree nodes or zero if not found.
345 * This function reads parameters always without caching.
347 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
352 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
357 *start_id = (parm >> 16) & 0x7fff;
358 return (int)(parm & 0x7fff);
360 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
363 * look for an AFG and MFG nodes
365 static void setup_fg_nodes(struct hdac_device *codec)
367 int i, total_nodes, function_id;
370 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
371 for (i = 0; i < total_nodes; i++, nid++) {
372 function_id = snd_hdac_read_parm(codec, nid,
373 AC_PAR_FUNCTION_TYPE);
374 switch (function_id & 0xff) {
375 case AC_GRP_AUDIO_FUNCTION:
377 codec->afg_function_id = function_id & 0xff;
378 codec->afg_unsol = (function_id >> 8) & 1;
380 case AC_GRP_MODEM_FUNCTION:
382 codec->mfg_function_id = function_id & 0xff;
383 codec->mfg_unsol = (function_id >> 8) & 1;
392 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
393 * @codec: the codec object
395 int snd_hdac_refresh_widgets(struct hdac_device *codec)
400 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
401 if (!start_nid || nums <= 0 || nums >= 0xff) {
402 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
407 codec->num_nodes = nums;
408 codec->start_nid = start_nid;
409 codec->end_nid = start_nid + nums;
412 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
415 * snd_hdac_refresh_widget_sysfs - Reset the codec widgets and reinit the
417 * @codec: the codec object
419 * first we need to remove sysfs, then refresh widgets and lastly
422 int snd_hdac_refresh_widget_sysfs(struct hdac_device *codec)
426 if (device_is_registered(&codec->dev))
427 hda_widget_sysfs_exit(codec);
428 ret = snd_hdac_refresh_widgets(codec);
430 dev_err(&codec->dev, "failed to refresh widget: %d\n", ret);
433 if (device_is_registered(&codec->dev)) {
434 ret = hda_widget_sysfs_init(codec);
436 dev_err(&codec->dev, "failed to init sysfs: %d\n", ret);
442 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widget_sysfs);
444 /* return CONNLIST_LEN parameter of the given widget */
445 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
447 unsigned int wcaps = get_wcaps(codec, nid);
450 if (!(wcaps & AC_WCAP_CONN_LIST) &&
451 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
454 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
461 * snd_hdac_get_connections - get a widget connection list
462 * @codec: the codec object
464 * @conn_list: the array to store the results, can be NULL
465 * @max_conns: the max size of the given array
467 * Returns the number of connected widgets, zero for no connection, or a
468 * negative error code. When the number of elements don't fit with the
469 * given array size, it returns -ENOSPC.
471 * When @conn_list is NULL, it just checks the number of connections.
473 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
474 hda_nid_t *conn_list, int max_conns)
477 int i, conn_len, conns, err;
478 unsigned int shift, num_elems, mask;
482 parm = get_num_conns(codec, nid);
486 if (parm & AC_CLIST_LONG) {
495 conn_len = parm & AC_CLIST_LENGTH;
496 mask = (1 << (shift-1)) - 1;
499 return 0; /* no connection */
502 /* single connection */
503 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
508 conn_list[0] = parm & mask;
512 /* multi connection */
515 for (i = 0; i < conn_len; i++) {
519 if (i % num_elems == 0) {
520 err = snd_hdac_read(codec, nid,
521 AC_VERB_GET_CONNECT_LIST, i,
526 range_val = !!(parm & (1 << (shift-1))); /* ranges */
528 if (val == 0 && null_count++) { /* no second chance */
530 "invalid CONNECT_LIST verb %x[%i]:%x\n",
536 /* ranges between the previous and this one */
537 if (!prev_nid || prev_nid >= val) {
538 dev_warn(&codec->dev,
539 "invalid dep_range_val %x:%x\n",
543 for (n = prev_nid + 1; n <= val; n++) {
545 if (conns >= max_conns)
547 conn_list[conns] = n;
553 if (conns >= max_conns)
555 conn_list[conns] = val;
563 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
567 * snd_hdac_power_up - power up the codec
568 * @codec: the codec object
570 * This function calls the runtime PM helper to power up the given codec.
571 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
572 * path that isn't included in PM path. Otherwise it gets stuck.
574 * Returns zero if successful, or a negative error code.
576 int snd_hdac_power_up(struct hdac_device *codec)
578 return pm_runtime_get_sync(&codec->dev);
580 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
583 * snd_hdac_power_down - power down the codec
584 * @codec: the codec object
586 * Returns zero if successful, or a negative error code.
588 int snd_hdac_power_down(struct hdac_device *codec)
590 struct device *dev = &codec->dev;
592 pm_runtime_mark_last_busy(dev);
593 return pm_runtime_put_autosuspend(dev);
595 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
598 * snd_hdac_power_up_pm - power up the codec
599 * @codec: the codec object
601 * This function can be called in a recursive code path like init code
602 * which may be called by PM suspend/resume again. OTOH, if a power-up
603 * call must wake up the sleeper (e.g. in a kctl callback), use
604 * snd_hdac_power_up() instead.
606 * Returns zero if successful, or a negative error code.
608 int snd_hdac_power_up_pm(struct hdac_device *codec)
610 if (!atomic_inc_not_zero(&codec->in_pm))
611 return snd_hdac_power_up(codec);
614 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
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);
635 * snd_hdac_link_power - Enable/disable the link power for a codec
636 * @codec: the codec object
637 * @bool: enable or disable the link power
639 int snd_hdac_link_power(struct hdac_device *codec, bool enable)
641 if (!codec->link_power_control)
644 if (codec->bus->ops->link_power)
645 return codec->bus->ops->link_power(codec->bus, enable);
649 EXPORT_SYMBOL_GPL(snd_hdac_link_power);
651 /* codec vendor labels */
652 struct hda_vendor_id {
657 static struct hda_vendor_id hda_vendor_ids[] = {
659 { 0x1013, "Cirrus Logic" },
660 { 0x1057, "Motorola" },
661 { 0x1095, "Silicon Image" },
662 { 0x10de, "Nvidia" },
663 { 0x10ec, "Realtek" },
664 { 0x1102, "Creative" },
668 { 0x11d4, "Analog Devices" },
669 { 0x13f6, "C-Media" },
670 { 0x14f1, "Conexant" },
671 { 0x17e8, "Chrontel" },
673 { 0x1aec, "Wolfson Microelectronics" },
675 { 0x434d, "C-Media" },
677 { 0x8384, "SigmaTel" },
681 /* store the codec vendor name */
682 static int get_codec_vendor_name(struct hdac_device *codec)
684 const struct hda_vendor_id *c;
685 u16 vendor_id = codec->vendor_id >> 16;
687 for (c = hda_vendor_ids; c->id; c++) {
688 if (c->id == vendor_id) {
689 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
690 return codec->vendor_name ? 0 : -ENOMEM;
694 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
695 return codec->vendor_name ? 0 : -ENOMEM;
701 struct hda_rate_tbl {
703 unsigned int alsa_bits;
704 unsigned int hda_fmt;
707 /* rate = base * mult / div */
708 #define HDA_RATE(base, mult, div) \
709 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
710 (((div) - 1) << AC_FMT_DIV_SHIFT))
712 static struct hda_rate_tbl rate_bits[] = {
713 /* rate in Hz, ALSA rate bitmask, HDA format value */
715 /* autodetected value used in snd_hda_query_supported_pcm */
716 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
717 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
718 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
719 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
720 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
721 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
722 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
723 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
724 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
725 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
726 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
727 #define AC_PAR_PCM_RATE_BITS 11
728 /* up to bits 10, 384kHZ isn't supported properly */
730 /* not autodetected value */
731 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
733 { 0 } /* terminator */
737 * snd_hdac_calc_stream_format - calculate the format bitset
738 * @rate: the sample rate
739 * @channels: the number of channels
740 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
741 * @maxbps: the max. bps
742 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
744 * Calculate the format bitset from the given rate, channels and th PCM format.
746 * Return zero if invalid.
748 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
749 unsigned int channels,
752 unsigned short spdif_ctls)
755 unsigned int val = 0;
757 for (i = 0; rate_bits[i].hz; i++)
758 if (rate_bits[i].hz == rate) {
759 val = rate_bits[i].hda_fmt;
762 if (!rate_bits[i].hz)
765 if (channels == 0 || channels > 8)
769 switch (snd_pcm_format_width(format)) {
771 val |= AC_FMT_BITS_8;
774 val |= AC_FMT_BITS_16;
779 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
780 val |= AC_FMT_BITS_32;
781 else if (maxbps >= 24)
782 val |= AC_FMT_BITS_24;
784 val |= AC_FMT_BITS_20;
790 if (spdif_ctls & AC_DIG1_NONAUDIO)
791 val |= AC_FMT_TYPE_NON_PCM;
795 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
797 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
799 unsigned int val = 0;
801 if (nid != codec->afg &&
802 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
803 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
804 if (!val || val == -1)
805 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
806 if (!val || val == -1)
811 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
813 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
815 if (!streams || streams == -1)
816 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
817 if (!streams || streams == -1)
823 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
824 * @codec: the codec object
826 * @ratesp: the pointer to store the detected rate bitflags
827 * @formatsp: the pointer to store the detected formats
828 * @bpsp: the pointer to store the detected format widths
830 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
831 * or @bsps argument is ignored.
833 * Returns 0 if successful, otherwise a negative error code.
835 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
836 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
838 unsigned int i, val, wcaps;
840 wcaps = get_wcaps(codec, nid);
841 val = query_pcm_param(codec, nid);
845 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
847 rates |= rate_bits[i].alsa_bits;
851 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
853 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
859 if (formatsp || bpsp) {
861 unsigned int streams, bps;
863 streams = query_stream_param(codec, nid);
868 if (streams & AC_SUPFMT_PCM) {
869 if (val & AC_SUPPCM_BITS_8) {
870 formats |= SNDRV_PCM_FMTBIT_U8;
873 if (val & AC_SUPPCM_BITS_16) {
874 formats |= SNDRV_PCM_FMTBIT_S16_LE;
877 if (wcaps & AC_WCAP_DIGITAL) {
878 if (val & AC_SUPPCM_BITS_32)
879 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
880 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
881 formats |= SNDRV_PCM_FMTBIT_S32_LE;
882 if (val & AC_SUPPCM_BITS_24)
884 else if (val & AC_SUPPCM_BITS_20)
886 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
887 AC_SUPPCM_BITS_32)) {
888 formats |= SNDRV_PCM_FMTBIT_S32_LE;
889 if (val & AC_SUPPCM_BITS_32)
891 else if (val & AC_SUPPCM_BITS_24)
893 else if (val & AC_SUPPCM_BITS_20)
897 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
898 if (streams & AC_SUPFMT_FLOAT32) {
899 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
904 if (streams == AC_SUPFMT_AC3) {
905 /* should be exclusive */
906 /* temporary hack: we have still no proper support
907 * for the direct AC3 stream...
909 formats |= SNDRV_PCM_FMTBIT_U8;
914 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
916 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
928 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
931 * snd_hdac_is_supported_format - Check the validity of the format
932 * @codec: the codec object
934 * @format: the HD-audio format value to check
936 * Check whether the given node supports the format value.
938 * Returns true if supported, false if not.
940 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
944 unsigned int val = 0, rate, stream;
946 val = query_pcm_param(codec, nid);
950 rate = format & 0xff00;
951 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
952 if (rate_bits[i].hda_fmt == rate) {
957 if (i >= AC_PAR_PCM_RATE_BITS)
960 stream = query_stream_param(codec, nid);
964 if (stream & AC_SUPFMT_PCM) {
965 switch (format & 0xf0) {
967 if (!(val & AC_SUPPCM_BITS_8))
971 if (!(val & AC_SUPPCM_BITS_16))
975 if (!(val & AC_SUPPCM_BITS_20))
979 if (!(val & AC_SUPPCM_BITS_24))
983 if (!(val & AC_SUPPCM_BITS_32))
990 /* FIXME: check for float32 and AC3? */
995 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
997 static unsigned int codec_read(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);
1003 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
1009 static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
1010 int flags, unsigned int verb, unsigned int parm)
1012 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1014 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1018 * snd_hdac_codec_read - send a command and get the response
1019 * @hdac: the HDAC device
1020 * @nid: NID to send the command
1021 * @flags: optional bit flags
1022 * @verb: the verb to send
1023 * @parm: the parameter for the verb
1025 * Send a single command and read the corresponding response.
1027 * Returns the obtained response value, or -1 for an error.
1029 int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1030 int flags, unsigned int verb, unsigned int parm)
1032 return codec_read(hdac, nid, flags, verb, parm);
1034 EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1037 * snd_hdac_codec_write - send a single command without waiting for response
1038 * @hdac: the HDAC device
1039 * @nid: NID to send the command
1040 * @flags: optional bit flags
1041 * @verb: the verb to send
1042 * @parm: the parameter for the verb
1044 * Send a single command without waiting for response.
1046 * Returns 0 if successful, or a negative error code.
1048 int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1049 int flags, unsigned int verb, unsigned int parm)
1051 return codec_write(hdac, nid, flags, verb, parm);
1053 EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1056 * snd_hdac_check_power_state - check whether the actual power state matches
1057 * with the target state
1059 * @hdac: the HDAC device
1060 * @nid: NID to send the command
1061 * @target_state: target state to check for
1063 * Return true if state matches, false if not
1065 bool snd_hdac_check_power_state(struct hdac_device *hdac,
1066 hda_nid_t nid, unsigned int target_state)
1068 unsigned int state = codec_read(hdac, nid, 0,
1069 AC_VERB_GET_POWER_STATE, 0);
1071 if (state & AC_PWRST_ERROR)
1073 state = (state >> 4) & 0x0f;
1074 return (state == target_state);
1076 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);