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);
164 snd_hdac_bus_remove_device(codec->bus, codec);
167 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
170 * snd_hdac_device_set_chip_name - set/update the codec name
171 * @codec: the HDAC device
172 * @name: name string to set
174 * Returns 0 if the name is set or updated, or a negative error code.
176 int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
182 newname = kstrdup(name, GFP_KERNEL);
185 kfree(codec->chip_name);
186 codec->chip_name = newname;
189 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
192 * snd_hdac_codec_modalias - give the module alias name
193 * @codec: HDAC device
194 * @buf: string buffer to store
195 * @size: string buffer size
197 * Returns the size of string, like snprintf(), or a negative error code.
199 int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
201 return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
202 codec->vendor_id, codec->revision_id, codec->type);
204 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
207 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
208 * HD-audio controller
209 * @codec: the codec object
210 * @nid: NID to encode
211 * @verb: verb to encode
212 * @parm: parameter to encode
214 * Return an encoded command verb or -1 for error.
216 unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
217 unsigned int verb, unsigned int parm)
222 if ((addr & ~0xf) || (nid & ~0x7f) ||
223 (verb & ~0xfff) || (parm & ~0xffff)) {
224 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
225 addr, nid, verb, parm);
230 val |= (u32)nid << 20;
235 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
238 * snd_hdac_exec_verb - execute an encoded verb
239 * @codec: the codec object
240 * @cmd: encoded verb to execute
241 * @flags: optional flags, pass zero for default
242 * @res: the pointer to store the result, NULL if running async
244 * Returns zero if successful, or a negative error code.
246 * This calls the exec_verb op when set in hdac_codec. If not,
247 * call the default snd_hdac_bus_exec_verb().
249 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
250 unsigned int flags, unsigned int *res)
252 if (codec->exec_verb)
253 return codec->exec_verb(codec, cmd, flags, res);
254 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
256 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
260 * snd_hdac_read - execute a verb
261 * @codec: the codec object
262 * @nid: NID to execute a verb
263 * @verb: verb to execute
264 * @parm: parameter for a verb
265 * @res: the pointer to store the result, NULL if running async
267 * Returns zero if successful, or a negative error code.
269 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
270 unsigned int verb, unsigned int parm, unsigned int *res)
272 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
274 return snd_hdac_exec_verb(codec, cmd, 0, res);
276 EXPORT_SYMBOL_GPL(snd_hdac_read);
279 * _snd_hdac_read_parm - read a parmeter
281 * This function returns zero or an error unlike snd_hdac_read_parm().
283 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
288 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
289 return snd_hdac_regmap_read_raw(codec, cmd, res);
291 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
294 * snd_hdac_read_parm_uncached - read a codec parameter without caching
295 * @codec: the codec object
296 * @nid: NID to read a parameter
297 * @parm: parameter to read
299 * Returns -1 for error. If you need to distinguish the error more
300 * strictly, use snd_hdac_read() directly.
302 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
305 unsigned int cmd, val;
307 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
308 if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0)
312 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
315 * snd_hdac_override_parm - override read-only parameters
316 * @codec: the codec object
317 * @nid: NID for the parameter
318 * @parm: the parameter to change
319 * @val: the parameter value to overwrite
321 int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
322 unsigned int parm, unsigned int val)
324 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
330 codec->caps_overwriting = true;
331 err = snd_hdac_regmap_write_raw(codec, verb, val);
332 codec->caps_overwriting = false;
335 EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
338 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
339 * @codec: the codec object
340 * @nid: NID to inspect
341 * @start_id: the pointer to store the starting NID
343 * Returns the number of subtree nodes or zero if not found.
344 * This function reads parameters always without caching.
346 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
351 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
356 *start_id = (parm >> 16) & 0x7fff;
357 return (int)(parm & 0x7fff);
359 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
362 * look for an AFG and MFG nodes
364 static void setup_fg_nodes(struct hdac_device *codec)
366 int i, total_nodes, function_id;
369 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
370 for (i = 0; i < total_nodes; i++, nid++) {
371 function_id = snd_hdac_read_parm(codec, nid,
372 AC_PAR_FUNCTION_TYPE);
373 switch (function_id & 0xff) {
374 case AC_GRP_AUDIO_FUNCTION:
376 codec->afg_function_id = function_id & 0xff;
377 codec->afg_unsol = (function_id >> 8) & 1;
379 case AC_GRP_MODEM_FUNCTION:
381 codec->mfg_function_id = function_id & 0xff;
382 codec->mfg_unsol = (function_id >> 8) & 1;
391 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
392 * @codec: the codec object
394 int snd_hdac_refresh_widgets(struct hdac_device *codec)
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",
406 codec->num_nodes = nums;
407 codec->start_nid = start_nid;
408 codec->end_nid = start_nid + nums;
411 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
414 * snd_hdac_refresh_widget_sysfs - Reset the codec widgets and reinit the
416 * @codec: the codec object
418 * first we need to remove sysfs, then refresh widgets and lastly
421 int snd_hdac_refresh_widget_sysfs(struct hdac_device *codec)
425 if (device_is_registered(&codec->dev))
426 hda_widget_sysfs_exit(codec);
427 ret = snd_hdac_refresh_widgets(codec);
429 dev_err(&codec->dev, "failed to refresh widget: %d\n", ret);
432 if (device_is_registered(&codec->dev)) {
433 ret = hda_widget_sysfs_init(codec);
435 dev_err(&codec->dev, "failed to init sysfs: %d\n", ret);
441 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widget_sysfs);
443 /* return CONNLIST_LEN parameter of the given widget */
444 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
446 unsigned int wcaps = get_wcaps(codec, nid);
449 if (!(wcaps & AC_WCAP_CONN_LIST) &&
450 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
453 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
460 * snd_hdac_get_connections - get a widget connection list
461 * @codec: the codec object
463 * @conn_list: the array to store the results, can be NULL
464 * @max_conns: the max size of the given array
466 * Returns the number of connected widgets, zero for no connection, or a
467 * negative error code. When the number of elements don't fit with the
468 * given array size, it returns -ENOSPC.
470 * When @conn_list is NULL, it just checks the number of connections.
472 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
473 hda_nid_t *conn_list, int max_conns)
476 int i, conn_len, conns, err;
477 unsigned int shift, num_elems, mask;
481 parm = get_num_conns(codec, nid);
485 if (parm & AC_CLIST_LONG) {
494 conn_len = parm & AC_CLIST_LENGTH;
495 mask = (1 << (shift-1)) - 1;
498 return 0; /* no connection */
501 /* single connection */
502 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
507 conn_list[0] = parm & mask;
511 /* multi connection */
514 for (i = 0; i < conn_len; i++) {
518 if (i % num_elems == 0) {
519 err = snd_hdac_read(codec, nid,
520 AC_VERB_GET_CONNECT_LIST, i,
525 range_val = !!(parm & (1 << (shift-1))); /* ranges */
527 if (val == 0 && null_count++) { /* no second chance */
529 "invalid CONNECT_LIST verb %x[%i]:%x\n",
535 /* ranges between the previous and this one */
536 if (!prev_nid || prev_nid >= val) {
537 dev_warn(&codec->dev,
538 "invalid dep_range_val %x:%x\n",
542 for (n = prev_nid + 1; n <= val; n++) {
544 if (conns >= max_conns)
546 conn_list[conns] = n;
552 if (conns >= max_conns)
554 conn_list[conns] = val;
562 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
566 * snd_hdac_power_up - power up the codec
567 * @codec: the codec object
569 * This function calls the runtime PM helper to power up the given codec.
570 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
571 * path that isn't included in PM path. Otherwise it gets stuck.
573 * Returns zero if successful, or a negative error code.
575 int snd_hdac_power_up(struct hdac_device *codec)
577 return pm_runtime_get_sync(&codec->dev);
579 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
582 * snd_hdac_power_down - power down the codec
583 * @codec: the codec object
585 * Returns zero if successful, or a negative error code.
587 int snd_hdac_power_down(struct hdac_device *codec)
589 struct device *dev = &codec->dev;
591 pm_runtime_mark_last_busy(dev);
592 return pm_runtime_put_autosuspend(dev);
594 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
597 * snd_hdac_power_up_pm - power up the codec
598 * @codec: the codec object
600 * This function can be called in a recursive code path like init code
601 * which may be called by PM suspend/resume again. OTOH, if a power-up
602 * call must wake up the sleeper (e.g. in a kctl callback), use
603 * snd_hdac_power_up() instead.
605 * Returns zero if successful, or a negative error code.
607 int snd_hdac_power_up_pm(struct hdac_device *codec)
609 if (!atomic_inc_not_zero(&codec->in_pm))
610 return snd_hdac_power_up(codec);
613 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
615 /* like snd_hdac_power_up_pm(), but only increment the pm count when
616 * already powered up. Returns -1 if not powered up, 1 if incremented
617 * or 0 if unchanged. Only used in hdac_regmap.c
619 int snd_hdac_keep_power_up(struct hdac_device *codec)
621 if (!atomic_inc_not_zero(&codec->in_pm)) {
622 int ret = pm_runtime_get_if_in_use(&codec->dev);
632 * snd_hdac_power_down_pm - power down the codec
633 * @codec: the codec object
635 * Like snd_hdac_power_up_pm(), this function is used in a recursive
636 * code path like init code which may be called by PM suspend/resume again.
638 * Returns zero if successful, or a negative error code.
640 int snd_hdac_power_down_pm(struct hdac_device *codec)
642 if (atomic_dec_if_positive(&codec->in_pm) < 0)
643 return snd_hdac_power_down(codec);
646 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
650 * snd_hdac_link_power - Enable/disable the link power for a codec
651 * @codec: the codec object
652 * @bool: enable or disable the link power
654 int snd_hdac_link_power(struct hdac_device *codec, bool enable)
656 if (!codec->link_power_control)
659 if (codec->bus->ops->link_power)
660 return codec->bus->ops->link_power(codec->bus, enable);
664 EXPORT_SYMBOL_GPL(snd_hdac_link_power);
666 /* codec vendor labels */
667 struct hda_vendor_id {
672 static struct hda_vendor_id hda_vendor_ids[] = {
674 { 0x1013, "Cirrus Logic" },
675 { 0x1057, "Motorola" },
676 { 0x1095, "Silicon Image" },
677 { 0x10de, "Nvidia" },
678 { 0x10ec, "Realtek" },
679 { 0x1102, "Creative" },
683 { 0x11d4, "Analog Devices" },
684 { 0x13f6, "C-Media" },
685 { 0x14f1, "Conexant" },
686 { 0x17e8, "Chrontel" },
688 { 0x1aec, "Wolfson Microelectronics" },
690 { 0x434d, "C-Media" },
692 { 0x8384, "SigmaTel" },
696 /* store the codec vendor name */
697 static int get_codec_vendor_name(struct hdac_device *codec)
699 const struct hda_vendor_id *c;
700 u16 vendor_id = codec->vendor_id >> 16;
702 for (c = hda_vendor_ids; c->id; c++) {
703 if (c->id == vendor_id) {
704 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
705 return codec->vendor_name ? 0 : -ENOMEM;
709 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
710 return codec->vendor_name ? 0 : -ENOMEM;
716 struct hda_rate_tbl {
718 unsigned int alsa_bits;
719 unsigned int hda_fmt;
722 /* rate = base * mult / div */
723 #define HDA_RATE(base, mult, div) \
724 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
725 (((div) - 1) << AC_FMT_DIV_SHIFT))
727 static struct hda_rate_tbl rate_bits[] = {
728 /* rate in Hz, ALSA rate bitmask, HDA format value */
730 /* autodetected value used in snd_hda_query_supported_pcm */
731 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
732 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
733 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
734 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
735 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
736 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
737 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
738 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
739 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
740 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
741 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
742 #define AC_PAR_PCM_RATE_BITS 11
743 /* up to bits 10, 384kHZ isn't supported properly */
745 /* not autodetected value */
746 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
748 { 0 } /* terminator */
752 * snd_hdac_calc_stream_format - calculate the format bitset
753 * @rate: the sample rate
754 * @channels: the number of channels
755 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
756 * @maxbps: the max. bps
757 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
759 * Calculate the format bitset from the given rate, channels and th PCM format.
761 * Return zero if invalid.
763 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
764 unsigned int channels,
767 unsigned short spdif_ctls)
770 unsigned int val = 0;
772 for (i = 0; rate_bits[i].hz; i++)
773 if (rate_bits[i].hz == rate) {
774 val = rate_bits[i].hda_fmt;
777 if (!rate_bits[i].hz)
780 if (channels == 0 || channels > 8)
784 switch (snd_pcm_format_width(format)) {
786 val |= AC_FMT_BITS_8;
789 val |= AC_FMT_BITS_16;
794 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
795 val |= AC_FMT_BITS_32;
796 else if (maxbps >= 24)
797 val |= AC_FMT_BITS_24;
799 val |= AC_FMT_BITS_20;
805 if (spdif_ctls & AC_DIG1_NONAUDIO)
806 val |= AC_FMT_TYPE_NON_PCM;
810 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
812 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
814 unsigned int val = 0;
816 if (nid != codec->afg &&
817 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
818 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
819 if (!val || val == -1)
820 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
821 if (!val || val == -1)
826 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
828 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
830 if (!streams || streams == -1)
831 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
832 if (!streams || streams == -1)
838 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
839 * @codec: the codec object
841 * @ratesp: the pointer to store the detected rate bitflags
842 * @formatsp: the pointer to store the detected formats
843 * @bpsp: the pointer to store the detected format widths
845 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
846 * or @bsps argument is ignored.
848 * Returns 0 if successful, otherwise a negative error code.
850 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
851 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
853 unsigned int i, val, wcaps;
855 wcaps = get_wcaps(codec, nid);
856 val = query_pcm_param(codec, nid);
860 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
862 rates |= rate_bits[i].alsa_bits;
866 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
868 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
874 if (formatsp || bpsp) {
876 unsigned int streams, bps;
878 streams = query_stream_param(codec, nid);
883 if (streams & AC_SUPFMT_PCM) {
884 if (val & AC_SUPPCM_BITS_8) {
885 formats |= SNDRV_PCM_FMTBIT_U8;
888 if (val & AC_SUPPCM_BITS_16) {
889 formats |= SNDRV_PCM_FMTBIT_S16_LE;
892 if (wcaps & AC_WCAP_DIGITAL) {
893 if (val & AC_SUPPCM_BITS_32)
894 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
895 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
896 formats |= SNDRV_PCM_FMTBIT_S32_LE;
897 if (val & AC_SUPPCM_BITS_24)
899 else if (val & AC_SUPPCM_BITS_20)
901 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
902 AC_SUPPCM_BITS_32)) {
903 formats |= SNDRV_PCM_FMTBIT_S32_LE;
904 if (val & AC_SUPPCM_BITS_32)
906 else if (val & AC_SUPPCM_BITS_24)
908 else if (val & AC_SUPPCM_BITS_20)
912 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
913 if (streams & AC_SUPFMT_FLOAT32) {
914 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
919 if (streams == AC_SUPFMT_AC3) {
920 /* should be exclusive */
921 /* temporary hack: we have still no proper support
922 * for the direct AC3 stream...
924 formats |= SNDRV_PCM_FMTBIT_U8;
929 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
931 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
943 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
946 * snd_hdac_is_supported_format - Check the validity of the format
947 * @codec: the codec object
949 * @format: the HD-audio format value to check
951 * Check whether the given node supports the format value.
953 * Returns true if supported, false if not.
955 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
959 unsigned int val = 0, rate, stream;
961 val = query_pcm_param(codec, nid);
965 rate = format & 0xff00;
966 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
967 if (rate_bits[i].hda_fmt == rate) {
972 if (i >= AC_PAR_PCM_RATE_BITS)
975 stream = query_stream_param(codec, nid);
979 if (stream & AC_SUPFMT_PCM) {
980 switch (format & 0xf0) {
982 if (!(val & AC_SUPPCM_BITS_8))
986 if (!(val & AC_SUPPCM_BITS_16))
990 if (!(val & AC_SUPPCM_BITS_20))
994 if (!(val & AC_SUPPCM_BITS_24))
998 if (!(val & AC_SUPPCM_BITS_32))
1005 /* FIXME: check for float32 and AC3? */
1010 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
1012 static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
1013 int flags, unsigned int verb, unsigned int parm)
1015 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1018 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
1024 static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
1025 int flags, unsigned int verb, unsigned int parm)
1027 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1029 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1033 * snd_hdac_codec_read - send a command and get the response
1034 * @hdac: the HDAC device
1035 * @nid: NID to send the command
1036 * @flags: optional bit flags
1037 * @verb: the verb to send
1038 * @parm: the parameter for the verb
1040 * Send a single command and read the corresponding response.
1042 * Returns the obtained response value, or -1 for an error.
1044 int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1045 int flags, unsigned int verb, unsigned int parm)
1047 return codec_read(hdac, nid, flags, verb, parm);
1049 EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1052 * snd_hdac_codec_write - send a single command without waiting for response
1053 * @hdac: the HDAC device
1054 * @nid: NID to send the command
1055 * @flags: optional bit flags
1056 * @verb: the verb to send
1057 * @parm: the parameter for the verb
1059 * Send a single command without waiting for response.
1061 * Returns 0 if successful, or a negative error code.
1063 int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1064 int flags, unsigned int verb, unsigned int parm)
1066 return codec_write(hdac, nid, flags, verb, parm);
1068 EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1071 * snd_hdac_check_power_state - check whether the actual power state matches
1072 * with the target state
1074 * @hdac: the HDAC device
1075 * @nid: NID to send the command
1076 * @target_state: target state to check for
1078 * Return true if state matches, false if not
1080 bool snd_hdac_check_power_state(struct hdac_device *hdac,
1081 hda_nid_t nid, unsigned int target_state)
1083 unsigned int state = codec_read(hdac, nid, 0,
1084 AC_VERB_GET_POWER_STATE, 0);
1086 if (state & AC_PWRST_ERROR)
1088 state = (state >> 4) & 0x0f;
1089 return (state == target_state);
1091 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);