2 * HD-audio codec core device
5 #include <linux/init.h>
6 #include <linux/delay.h>
7 #include <linux/device.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/export.h>
11 #include <linux/pm_runtime.h>
12 #include <sound/hdaudio.h>
13 #include <sound/hda_regmap.h>
14 #include <sound/pcm.h>
17 static void setup_fg_nodes(struct hdac_device *codec);
18 static int get_codec_vendor_name(struct hdac_device *codec);
20 static void default_release(struct device *dev)
22 snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
26 * snd_hdac_device_init - initialize the HD-audio codec base device
27 * @codec: device to initialize
29 * @name: device name string
30 * @addr: codec address
32 * Returns zero for success or a negative error code.
34 * This function increments the runtime PM counter and marks it active.
35 * The caller needs to turn it off appropriately later.
37 * The caller needs to set the device's release op properly by itself.
39 int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
40 const char *name, unsigned int addr)
47 device_initialize(dev);
48 dev->parent = bus->dev;
49 dev->bus = &snd_hda_bus_type;
50 dev->release = default_release;
51 dev->groups = hdac_dev_attr_groups;
52 dev_set_name(dev, "%s", name);
53 device_enable_async_suspend(dev);
57 codec->type = HDA_DEV_CORE;
58 pm_runtime_set_active(&codec->dev);
59 pm_runtime_get_noresume(&codec->dev);
60 atomic_set(&codec->in_pm, 0);
62 err = snd_hdac_bus_add_device(bus, codec);
67 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
69 if (codec->vendor_id == -1) {
70 /* read again, hopefully the access method was corrected
73 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
77 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
79 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
82 setup_fg_nodes(codec);
83 if (!codec->afg && !codec->mfg) {
84 dev_err(dev, "no AFG or MFG node found\n");
89 fg = codec->afg ? codec->afg : codec->mfg;
91 err = snd_hdac_refresh_widgets(codec, false);
95 codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
96 /* reread ssid if not set by parameter */
97 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
98 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
99 &codec->subsystem_id);
101 err = get_codec_vendor_name(codec);
105 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
106 codec->vendor_id & 0xffff);
107 if (!codec->chip_name) {
115 put_device(&codec->dev);
118 EXPORT_SYMBOL_GPL(snd_hdac_device_init);
121 * snd_hdac_device_exit - clean up the HD-audio codec base device
122 * @codec: device to clean up
124 void snd_hdac_device_exit(struct hdac_device *codec)
126 pm_runtime_put_noidle(&codec->dev);
127 snd_hdac_bus_remove_device(codec->bus, codec);
128 kfree(codec->vendor_name);
129 kfree(codec->chip_name);
131 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
134 * snd_hdac_device_register - register the hd-audio codec base device
135 * codec: the device to register
137 int snd_hdac_device_register(struct hdac_device *codec)
141 err = device_add(&codec->dev);
144 err = hda_widget_sysfs_init(codec);
146 device_del(&codec->dev);
152 EXPORT_SYMBOL_GPL(snd_hdac_device_register);
155 * snd_hdac_device_unregister - unregister the hd-audio codec base device
156 * codec: the device to unregister
158 void snd_hdac_device_unregister(struct hdac_device *codec)
160 if (device_is_registered(&codec->dev)) {
161 hda_widget_sysfs_exit(codec);
162 device_del(&codec->dev);
163 snd_hdac_bus_remove_device(codec->bus, codec);
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,
304 unsigned int cmd, val;
306 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
307 if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0)
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
392 * @sysfs: re-initialize sysfs tree, too
394 int snd_hdac_refresh_widgets(struct hdac_device *codec, bool sysfs)
399 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
400 if (!start_nid || nums <= 0 || nums >= 0xff) {
401 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
407 err = hda_widget_sysfs_reinit(codec, start_nid, nums);
412 codec->num_nodes = nums;
413 codec->start_nid = start_nid;
414 codec->end_nid = start_nid + nums;
417 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
419 /* return CONNLIST_LEN parameter of the given widget */
420 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
422 unsigned int wcaps = get_wcaps(codec, nid);
425 if (!(wcaps & AC_WCAP_CONN_LIST) &&
426 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
429 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
436 * snd_hdac_get_connections - get a widget connection list
437 * @codec: the codec object
439 * @conn_list: the array to store the results, can be NULL
440 * @max_conns: the max size of the given array
442 * Returns the number of connected widgets, zero for no connection, or a
443 * negative error code. When the number of elements don't fit with the
444 * given array size, it returns -ENOSPC.
446 * When @conn_list is NULL, it just checks the number of connections.
448 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
449 hda_nid_t *conn_list, int max_conns)
452 int i, conn_len, conns, err;
453 unsigned int shift, num_elems, mask;
457 parm = get_num_conns(codec, nid);
461 if (parm & AC_CLIST_LONG) {
470 conn_len = parm & AC_CLIST_LENGTH;
471 mask = (1 << (shift-1)) - 1;
474 return 0; /* no connection */
477 /* single connection */
478 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
483 conn_list[0] = parm & mask;
487 /* multi connection */
490 for (i = 0; i < conn_len; i++) {
494 if (i % num_elems == 0) {
495 err = snd_hdac_read(codec, nid,
496 AC_VERB_GET_CONNECT_LIST, i,
501 range_val = !!(parm & (1 << (shift-1))); /* ranges */
503 if (val == 0 && null_count++) { /* no second chance */
505 "invalid CONNECT_LIST verb %x[%i]:%x\n",
511 /* ranges between the previous and this one */
512 if (!prev_nid || prev_nid >= val) {
513 dev_warn(&codec->dev,
514 "invalid dep_range_val %x:%x\n",
518 for (n = prev_nid + 1; n <= val; n++) {
520 if (conns >= max_conns)
522 conn_list[conns] = n;
528 if (conns >= max_conns)
530 conn_list[conns] = val;
538 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
542 * snd_hdac_power_up - power up the codec
543 * @codec: the codec object
545 * This function calls the runtime PM helper to power up the given codec.
546 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
547 * path that isn't included in PM path. Otherwise it gets stuck.
549 * Returns zero if successful, or a negative error code.
551 int snd_hdac_power_up(struct hdac_device *codec)
553 return pm_runtime_get_sync(&codec->dev);
555 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
558 * snd_hdac_power_down - power down the codec
559 * @codec: the codec object
561 * Returns zero if successful, or a negative error code.
563 int snd_hdac_power_down(struct hdac_device *codec)
565 struct device *dev = &codec->dev;
567 pm_runtime_mark_last_busy(dev);
568 return pm_runtime_put_autosuspend(dev);
570 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
573 * snd_hdac_power_up_pm - power up the codec
574 * @codec: the codec object
576 * This function can be called in a recursive code path like init code
577 * which may be called by PM suspend/resume again. OTOH, if a power-up
578 * call must wake up the sleeper (e.g. in a kctl callback), use
579 * snd_hdac_power_up() instead.
581 * Returns zero if successful, or a negative error code.
583 int snd_hdac_power_up_pm(struct hdac_device *codec)
585 if (!atomic_inc_not_zero(&codec->in_pm))
586 return snd_hdac_power_up(codec);
589 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
591 /* like snd_hdac_power_up_pm(), but only increment the pm count when
592 * already powered up. Returns -1 if not powered up, 1 if incremented
593 * or 0 if unchanged. Only used in hdac_regmap.c
595 int snd_hdac_keep_power_up(struct hdac_device *codec)
597 if (!atomic_inc_not_zero(&codec->in_pm)) {
598 int ret = pm_runtime_get_if_in_use(&codec->dev);
608 * snd_hdac_power_down_pm - power down the codec
609 * @codec: the codec object
611 * Like snd_hdac_power_up_pm(), this function is used in a recursive
612 * code path like init code which may be called by PM suspend/resume again.
614 * Returns zero if successful, or a negative error code.
616 int snd_hdac_power_down_pm(struct hdac_device *codec)
618 if (atomic_dec_if_positive(&codec->in_pm) < 0)
619 return snd_hdac_power_down(codec);
622 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
626 * snd_hdac_link_power - Enable/disable the link power for a codec
627 * @codec: the codec object
628 * @bool: enable or disable the link power
630 int snd_hdac_link_power(struct hdac_device *codec, bool enable)
632 if (!codec->link_power_control)
635 if (codec->bus->ops->link_power)
636 return codec->bus->ops->link_power(codec->bus, enable);
640 EXPORT_SYMBOL_GPL(snd_hdac_link_power);
642 /* codec vendor labels */
643 struct hda_vendor_id {
648 static struct hda_vendor_id hda_vendor_ids[] = {
650 { 0x1013, "Cirrus Logic" },
651 { 0x1057, "Motorola" },
652 { 0x1095, "Silicon Image" },
653 { 0x10de, "Nvidia" },
654 { 0x10ec, "Realtek" },
655 { 0x1102, "Creative" },
659 { 0x11d4, "Analog Devices" },
660 { 0x13f6, "C-Media" },
661 { 0x14f1, "Conexant" },
662 { 0x17e8, "Chrontel" },
664 { 0x1aec, "Wolfson Microelectronics" },
666 { 0x434d, "C-Media" },
668 { 0x8384, "SigmaTel" },
672 /* store the codec vendor name */
673 static int get_codec_vendor_name(struct hdac_device *codec)
675 const struct hda_vendor_id *c;
676 u16 vendor_id = codec->vendor_id >> 16;
678 for (c = hda_vendor_ids; c->id; c++) {
679 if (c->id == vendor_id) {
680 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
681 return codec->vendor_name ? 0 : -ENOMEM;
685 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
686 return codec->vendor_name ? 0 : -ENOMEM;
692 struct hda_rate_tbl {
694 unsigned int alsa_bits;
695 unsigned int hda_fmt;
698 /* rate = base * mult / div */
699 #define HDA_RATE(base, mult, div) \
700 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
701 (((div) - 1) << AC_FMT_DIV_SHIFT))
703 static struct hda_rate_tbl rate_bits[] = {
704 /* rate in Hz, ALSA rate bitmask, HDA format value */
706 /* autodetected value used in snd_hda_query_supported_pcm */
707 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
708 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
709 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
710 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
711 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
712 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
713 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
714 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
715 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
716 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
717 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
718 #define AC_PAR_PCM_RATE_BITS 11
719 /* up to bits 10, 384kHZ isn't supported properly */
721 /* not autodetected value */
722 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
724 { 0 } /* terminator */
728 * snd_hdac_calc_stream_format - calculate the format bitset
729 * @rate: the sample rate
730 * @channels: the number of channels
731 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
732 * @maxbps: the max. bps
733 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
735 * Calculate the format bitset from the given rate, channels and th PCM format.
737 * Return zero if invalid.
739 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
740 unsigned int channels,
741 snd_pcm_format_t format,
743 unsigned short spdif_ctls)
746 unsigned int val = 0;
748 for (i = 0; rate_bits[i].hz; i++)
749 if (rate_bits[i].hz == rate) {
750 val = rate_bits[i].hda_fmt;
753 if (!rate_bits[i].hz)
756 if (channels == 0 || channels > 8)
760 switch (snd_pcm_format_width(format)) {
762 val |= AC_FMT_BITS_8;
765 val |= AC_FMT_BITS_16;
770 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
771 val |= AC_FMT_BITS_32;
772 else if (maxbps >= 24)
773 val |= AC_FMT_BITS_24;
775 val |= AC_FMT_BITS_20;
781 if (spdif_ctls & AC_DIG1_NONAUDIO)
782 val |= AC_FMT_TYPE_NON_PCM;
786 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
788 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
790 unsigned int val = 0;
792 if (nid != codec->afg &&
793 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
794 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
795 if (!val || val == -1)
796 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
797 if (!val || val == -1)
802 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
804 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
806 if (!streams || streams == -1)
807 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
808 if (!streams || streams == -1)
814 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
815 * @codec: the codec object
817 * @ratesp: the pointer to store the detected rate bitflags
818 * @formatsp: the pointer to store the detected formats
819 * @bpsp: the pointer to store the detected format widths
821 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
822 * or @bsps argument is ignored.
824 * Returns 0 if successful, otherwise a negative error code.
826 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
827 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
829 unsigned int i, val, wcaps;
831 wcaps = get_wcaps(codec, nid);
832 val = query_pcm_param(codec, nid);
836 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
838 rates |= rate_bits[i].alsa_bits;
842 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
844 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
850 if (formatsp || bpsp) {
852 unsigned int streams, bps;
854 streams = query_stream_param(codec, nid);
859 if (streams & AC_SUPFMT_PCM) {
860 if (val & AC_SUPPCM_BITS_8) {
861 formats |= SNDRV_PCM_FMTBIT_U8;
864 if (val & AC_SUPPCM_BITS_16) {
865 formats |= SNDRV_PCM_FMTBIT_S16_LE;
868 if (wcaps & AC_WCAP_DIGITAL) {
869 if (val & AC_SUPPCM_BITS_32)
870 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
871 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
872 formats |= SNDRV_PCM_FMTBIT_S32_LE;
873 if (val & AC_SUPPCM_BITS_24)
875 else if (val & AC_SUPPCM_BITS_20)
877 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
878 AC_SUPPCM_BITS_32)) {
879 formats |= SNDRV_PCM_FMTBIT_S32_LE;
880 if (val & AC_SUPPCM_BITS_32)
882 else if (val & AC_SUPPCM_BITS_24)
884 else if (val & AC_SUPPCM_BITS_20)
888 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
889 if (streams & AC_SUPFMT_FLOAT32) {
890 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
895 if (streams == AC_SUPFMT_AC3) {
896 /* should be exclusive */
897 /* temporary hack: we have still no proper support
898 * for the direct AC3 stream...
900 formats |= SNDRV_PCM_FMTBIT_U8;
905 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
907 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
919 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
922 * snd_hdac_is_supported_format - Check the validity of the format
923 * @codec: the codec object
925 * @format: the HD-audio format value to check
927 * Check whether the given node supports the format value.
929 * Returns true if supported, false if not.
931 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
935 unsigned int val = 0, rate, stream;
937 val = query_pcm_param(codec, nid);
941 rate = format & 0xff00;
942 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
943 if (rate_bits[i].hda_fmt == rate) {
948 if (i >= AC_PAR_PCM_RATE_BITS)
951 stream = query_stream_param(codec, nid);
955 if (stream & AC_SUPFMT_PCM) {
956 switch (format & 0xf0) {
958 if (!(val & AC_SUPPCM_BITS_8))
962 if (!(val & AC_SUPPCM_BITS_16))
966 if (!(val & AC_SUPPCM_BITS_20))
970 if (!(val & AC_SUPPCM_BITS_24))
974 if (!(val & AC_SUPPCM_BITS_32))
981 /* FIXME: check for float32 and AC3? */
986 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
988 static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
989 int flags, unsigned int verb, unsigned int parm)
991 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
994 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
1000 static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
1001 int flags, unsigned int verb, unsigned int parm)
1003 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1005 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1009 * snd_hdac_codec_read - send a command and get the response
1010 * @hdac: the HDAC device
1011 * @nid: NID to send the command
1012 * @flags: optional bit flags
1013 * @verb: the verb to send
1014 * @parm: the parameter for the verb
1016 * Send a single command and read the corresponding response.
1018 * Returns the obtained response value, or -1 for an error.
1020 int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1021 int flags, unsigned int verb, unsigned int parm)
1023 return codec_read(hdac, nid, flags, verb, parm);
1025 EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1028 * snd_hdac_codec_write - send a single command without waiting for response
1029 * @hdac: the HDAC device
1030 * @nid: NID to send the command
1031 * @flags: optional bit flags
1032 * @verb: the verb to send
1033 * @parm: the parameter for the verb
1035 * Send a single command without waiting for response.
1037 * Returns 0 if successful, or a negative error code.
1039 int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1040 int flags, unsigned int verb, unsigned int parm)
1042 return codec_write(hdac, nid, flags, verb, parm);
1044 EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1047 * snd_hdac_check_power_state - check whether the actual power state matches
1048 * with the target state
1050 * @hdac: the HDAC device
1051 * @nid: NID to send the command
1052 * @target_state: target state to check for
1054 * Return true if state matches, false if not
1056 bool snd_hdac_check_power_state(struct hdac_device *hdac,
1057 hda_nid_t nid, unsigned int target_state)
1059 unsigned int state = codec_read(hdac, nid, 0,
1060 AC_VERB_GET_POWER_STATE, 0);
1062 if (state & AC_PWRST_ERROR)
1064 state = (state >> 4) & 0x0f;
1065 return (state == target_state);
1067 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);
1069 * snd_hdac_sync_power_state - wait until actual power state matches
1070 * with the target state
1072 * @hdac: the HDAC device
1073 * @nid: NID to send the command
1074 * @target_state: target state to check for
1076 * Return power state or PS_ERROR if codec rejects GET verb.
1078 unsigned int snd_hdac_sync_power_state(struct hdac_device *codec,
1079 hda_nid_t nid, unsigned int power_state)
1081 unsigned long end_time = jiffies + msecs_to_jiffies(500);
1082 unsigned int state, actual_state, count;
1084 for (count = 0; count < 500; count++) {
1085 state = snd_hdac_codec_read(codec, nid, 0,
1086 AC_VERB_GET_POWER_STATE, 0);
1087 if (state & AC_PWRST_ERROR) {
1091 actual_state = (state >> 4) & 0x0f;
1092 if (actual_state == power_state)
1094 if (time_after_eq(jiffies, end_time))
1096 /* wait until the codec reachs to the target state */
1101 EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state);