0001
0002
0003
0004
0005
0006 #include <linux/init.h>
0007 #include <linux/delay.h>
0008 #include <linux/device.h>
0009 #include <linux/slab.h>
0010 #include <linux/module.h>
0011 #include <linux/export.h>
0012 #include <linux/pm_runtime.h>
0013 #include <sound/hdaudio.h>
0014 #include <sound/hda_regmap.h>
0015 #include <sound/pcm.h>
0016 #include "local.h"
0017
0018 static void setup_fg_nodes(struct hdac_device *codec);
0019 static int get_codec_vendor_name(struct hdac_device *codec);
0020
0021 static void default_release(struct device *dev)
0022 {
0023 snd_hdac_device_exit(dev_to_hdac_dev(dev));
0024 }
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
0041 const char *name, unsigned int addr)
0042 {
0043 struct device *dev;
0044 hda_nid_t fg;
0045 int err;
0046
0047 dev = &codec->dev;
0048 device_initialize(dev);
0049 dev->parent = bus->dev;
0050 dev->bus = &snd_hda_bus_type;
0051 dev->release = default_release;
0052 dev->groups = hdac_dev_attr_groups;
0053 dev_set_name(dev, "%s", name);
0054 device_enable_async_suspend(dev);
0055
0056 codec->bus = bus;
0057 codec->addr = addr;
0058 codec->type = HDA_DEV_CORE;
0059 mutex_init(&codec->widget_lock);
0060 mutex_init(&codec->regmap_lock);
0061 pm_runtime_set_active(&codec->dev);
0062 pm_runtime_get_noresume(&codec->dev);
0063 atomic_set(&codec->in_pm, 0);
0064
0065 err = snd_hdac_bus_add_device(bus, codec);
0066 if (err < 0)
0067 goto error;
0068
0069
0070 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
0071 AC_PAR_VENDOR_ID);
0072 if (codec->vendor_id == -1) {
0073
0074
0075
0076 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
0077 AC_PAR_VENDOR_ID);
0078 }
0079
0080 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
0081 AC_PAR_SUBSYSTEM_ID);
0082 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
0083 AC_PAR_REV_ID);
0084
0085 setup_fg_nodes(codec);
0086 if (!codec->afg && !codec->mfg) {
0087 dev_err(dev, "no AFG or MFG node found\n");
0088 err = -ENODEV;
0089 goto error;
0090 }
0091
0092 fg = codec->afg ? codec->afg : codec->mfg;
0093
0094 err = snd_hdac_refresh_widgets(codec);
0095 if (err < 0)
0096 goto error;
0097
0098 codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
0099
0100 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
0101 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
0102 &codec->subsystem_id);
0103
0104 err = get_codec_vendor_name(codec);
0105 if (err < 0)
0106 goto error;
0107
0108 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
0109 codec->vendor_id & 0xffff);
0110 if (!codec->chip_name) {
0111 err = -ENOMEM;
0112 goto error;
0113 }
0114
0115 return 0;
0116
0117 error:
0118 put_device(&codec->dev);
0119 return err;
0120 }
0121 EXPORT_SYMBOL_GPL(snd_hdac_device_init);
0122
0123
0124
0125
0126
0127 void snd_hdac_device_exit(struct hdac_device *codec)
0128 {
0129 pm_runtime_put_noidle(&codec->dev);
0130
0131 pm_runtime_set_suspended(&codec->dev);
0132 snd_hdac_bus_remove_device(codec->bus, codec);
0133 kfree(codec->vendor_name);
0134 kfree(codec->chip_name);
0135 }
0136 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
0137
0138
0139
0140
0141
0142 int snd_hdac_device_register(struct hdac_device *codec)
0143 {
0144 int err;
0145
0146 err = device_add(&codec->dev);
0147 if (err < 0)
0148 return err;
0149 mutex_lock(&codec->widget_lock);
0150 err = hda_widget_sysfs_init(codec);
0151 mutex_unlock(&codec->widget_lock);
0152 if (err < 0) {
0153 device_del(&codec->dev);
0154 return err;
0155 }
0156
0157 return 0;
0158 }
0159 EXPORT_SYMBOL_GPL(snd_hdac_device_register);
0160
0161
0162
0163
0164
0165 void snd_hdac_device_unregister(struct hdac_device *codec)
0166 {
0167 if (device_is_registered(&codec->dev)) {
0168 mutex_lock(&codec->widget_lock);
0169 hda_widget_sysfs_exit(codec);
0170 mutex_unlock(&codec->widget_lock);
0171 device_del(&codec->dev);
0172 snd_hdac_bus_remove_device(codec->bus, codec);
0173 }
0174 }
0175 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
0176
0177
0178
0179
0180
0181
0182
0183
0184 int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
0185 {
0186 char *newname;
0187
0188 if (!name)
0189 return 0;
0190 newname = kstrdup(name, GFP_KERNEL);
0191 if (!newname)
0192 return -ENOMEM;
0193 kfree(codec->chip_name);
0194 codec->chip_name = newname;
0195 return 0;
0196 }
0197 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207 int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
0208 {
0209 return scnprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
0210 codec->vendor_id, codec->revision_id, codec->type);
0211 }
0212 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224 static unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
0225 unsigned int verb, unsigned int parm)
0226 {
0227 u32 val, addr;
0228
0229 addr = codec->addr;
0230 if ((addr & ~0xf) || (nid & ~0x7f) ||
0231 (verb & ~0xfff) || (parm & ~0xffff)) {
0232 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
0233 addr, nid, verb, parm);
0234 return -1;
0235 }
0236
0237 val = addr << 28;
0238 val |= (u32)nid << 20;
0239 val |= verb << 8;
0240 val |= parm;
0241 return val;
0242 }
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
0257 unsigned int flags, unsigned int *res)
0258 {
0259 if (codec->exec_verb)
0260 return codec->exec_verb(codec, cmd, flags, res);
0261 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
0262 }
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
0276 unsigned int verb, unsigned int parm, unsigned int *res)
0277 {
0278 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
0279
0280 return snd_hdac_exec_verb(codec, cmd, 0, res);
0281 }
0282 EXPORT_SYMBOL_GPL(snd_hdac_read);
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
0294 unsigned int *res)
0295 {
0296 unsigned int cmd;
0297
0298 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
0299 return snd_hdac_regmap_read_raw(codec, cmd, res);
0300 }
0301 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
0313 int parm)
0314 {
0315 unsigned int cmd, val;
0316
0317 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
0318 if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0)
0319 return -1;
0320 return val;
0321 }
0322 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
0323
0324
0325
0326
0327
0328
0329
0330
0331 int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
0332 unsigned int parm, unsigned int val)
0333 {
0334 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
0335 int err;
0336
0337 if (!codec->regmap)
0338 return -EINVAL;
0339
0340 codec->caps_overwriting = true;
0341 err = snd_hdac_regmap_write_raw(codec, verb, val);
0342 codec->caps_overwriting = false;
0343 return err;
0344 }
0345 EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
0357 hda_nid_t *start_id)
0358 {
0359 unsigned int parm;
0360
0361 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
0362 if (parm == -1) {
0363 *start_id = 0;
0364 return 0;
0365 }
0366 *start_id = (parm >> 16) & 0x7fff;
0367 return (int)(parm & 0x7fff);
0368 }
0369 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
0370
0371
0372
0373
0374 static void setup_fg_nodes(struct hdac_device *codec)
0375 {
0376 int i, total_nodes, function_id;
0377 hda_nid_t nid;
0378
0379 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
0380 for (i = 0; i < total_nodes; i++, nid++) {
0381 function_id = snd_hdac_read_parm(codec, nid,
0382 AC_PAR_FUNCTION_TYPE);
0383 switch (function_id & 0xff) {
0384 case AC_GRP_AUDIO_FUNCTION:
0385 codec->afg = nid;
0386 codec->afg_function_id = function_id & 0xff;
0387 codec->afg_unsol = (function_id >> 8) & 1;
0388 break;
0389 case AC_GRP_MODEM_FUNCTION:
0390 codec->mfg = nid;
0391 codec->mfg_function_id = function_id & 0xff;
0392 codec->mfg_unsol = (function_id >> 8) & 1;
0393 break;
0394 default:
0395 break;
0396 }
0397 }
0398 }
0399
0400
0401
0402
0403
0404 int snd_hdac_refresh_widgets(struct hdac_device *codec)
0405 {
0406 hda_nid_t start_nid;
0407 int nums, err = 0;
0408
0409
0410
0411
0412
0413 mutex_lock(&codec->widget_lock);
0414 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
0415 if (!start_nid || nums <= 0 || nums >= 0xff) {
0416 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
0417 codec->afg);
0418 err = -EINVAL;
0419 goto unlock;
0420 }
0421
0422 err = hda_widget_sysfs_reinit(codec, start_nid, nums);
0423 if (err < 0)
0424 goto unlock;
0425
0426 codec->num_nodes = nums;
0427 codec->start_nid = start_nid;
0428 codec->end_nid = start_nid + nums;
0429 unlock:
0430 mutex_unlock(&codec->widget_lock);
0431 return err;
0432 }
0433 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
0434
0435
0436 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
0437 {
0438 unsigned int wcaps = get_wcaps(codec, nid);
0439 unsigned int parm;
0440
0441 if (!(wcaps & AC_WCAP_CONN_LIST) &&
0442 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
0443 return 0;
0444
0445 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
0446 if (parm == -1)
0447 parm = 0;
0448 return parm;
0449 }
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
0465 hda_nid_t *conn_list, int max_conns)
0466 {
0467 unsigned int parm;
0468 int i, conn_len, conns, err;
0469 unsigned int shift, num_elems, mask;
0470 hda_nid_t prev_nid;
0471 int null_count = 0;
0472
0473 parm = get_num_conns(codec, nid);
0474 if (!parm)
0475 return 0;
0476
0477 if (parm & AC_CLIST_LONG) {
0478
0479 shift = 16;
0480 num_elems = 2;
0481 } else {
0482
0483 shift = 8;
0484 num_elems = 4;
0485 }
0486 conn_len = parm & AC_CLIST_LENGTH;
0487 mask = (1 << (shift-1)) - 1;
0488
0489 if (!conn_len)
0490 return 0;
0491
0492 if (conn_len == 1) {
0493
0494 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
0495 &parm);
0496 if (err < 0)
0497 return err;
0498 if (conn_list)
0499 conn_list[0] = parm & mask;
0500 return 1;
0501 }
0502
0503
0504 conns = 0;
0505 prev_nid = 0;
0506 for (i = 0; i < conn_len; i++) {
0507 int range_val;
0508 hda_nid_t val, n;
0509
0510 if (i % num_elems == 0) {
0511 err = snd_hdac_read(codec, nid,
0512 AC_VERB_GET_CONNECT_LIST, i,
0513 &parm);
0514 if (err < 0)
0515 return -EIO;
0516 }
0517 range_val = !!(parm & (1 << (shift-1)));
0518 val = parm & mask;
0519 if (val == 0 && null_count++) {
0520 dev_dbg(&codec->dev,
0521 "invalid CONNECT_LIST verb %x[%i]:%x\n",
0522 nid, i, parm);
0523 return 0;
0524 }
0525 parm >>= shift;
0526 if (range_val) {
0527
0528 if (!prev_nid || prev_nid >= val) {
0529 dev_warn(&codec->dev,
0530 "invalid dep_range_val %x:%x\n",
0531 prev_nid, val);
0532 continue;
0533 }
0534 for (n = prev_nid + 1; n <= val; n++) {
0535 if (conn_list) {
0536 if (conns >= max_conns)
0537 return -ENOSPC;
0538 conn_list[conns] = n;
0539 }
0540 conns++;
0541 }
0542 } else {
0543 if (conn_list) {
0544 if (conns >= max_conns)
0545 return -ENOSPC;
0546 conn_list[conns] = val;
0547 }
0548 conns++;
0549 }
0550 prev_nid = val;
0551 }
0552 return conns;
0553 }
0554 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
0555
0556 #ifdef CONFIG_PM
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567 int snd_hdac_power_up(struct hdac_device *codec)
0568 {
0569 return pm_runtime_get_sync(&codec->dev);
0570 }
0571 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
0572
0573
0574
0575
0576
0577
0578
0579 int snd_hdac_power_down(struct hdac_device *codec)
0580 {
0581 struct device *dev = &codec->dev;
0582
0583 pm_runtime_mark_last_busy(dev);
0584 return pm_runtime_put_autosuspend(dev);
0585 }
0586 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599 int snd_hdac_power_up_pm(struct hdac_device *codec)
0600 {
0601 if (!atomic_inc_not_zero(&codec->in_pm))
0602 return snd_hdac_power_up(codec);
0603 return 0;
0604 }
0605 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
0606
0607
0608
0609
0610
0611 int snd_hdac_keep_power_up(struct hdac_device *codec)
0612 {
0613 if (!atomic_inc_not_zero(&codec->in_pm)) {
0614 int ret = pm_runtime_get_if_in_use(&codec->dev);
0615 if (!ret)
0616 return -1;
0617 if (ret < 0)
0618 return 0;
0619 }
0620 return 1;
0621 }
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632 int snd_hdac_power_down_pm(struct hdac_device *codec)
0633 {
0634 if (atomic_dec_if_positive(&codec->in_pm) < 0)
0635 return snd_hdac_power_down(codec);
0636 return 0;
0637 }
0638 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
0639 #endif
0640
0641
0642 struct hda_vendor_id {
0643 unsigned int id;
0644 const char *name;
0645 };
0646
0647 static const struct hda_vendor_id hda_vendor_ids[] = {
0648 { 0x1002, "ATI" },
0649 { 0x1013, "Cirrus Logic" },
0650 { 0x1057, "Motorola" },
0651 { 0x1095, "Silicon Image" },
0652 { 0x10de, "Nvidia" },
0653 { 0x10ec, "Realtek" },
0654 { 0x1102, "Creative" },
0655 { 0x1106, "VIA" },
0656 { 0x111d, "IDT" },
0657 { 0x11c1, "LSI" },
0658 { 0x11d4, "Analog Devices" },
0659 { 0x13f6, "C-Media" },
0660 { 0x14f1, "Conexant" },
0661 { 0x17e8, "Chrontel" },
0662 { 0x1854, "LG" },
0663 { 0x19e5, "Huawei" },
0664 { 0x1aec, "Wolfson Microelectronics" },
0665 { 0x1af4, "QEMU" },
0666 { 0x434d, "C-Media" },
0667 { 0x8086, "Intel" },
0668 { 0x8384, "SigmaTel" },
0669 {}
0670 };
0671
0672
0673 static int get_codec_vendor_name(struct hdac_device *codec)
0674 {
0675 const struct hda_vendor_id *c;
0676 u16 vendor_id = codec->vendor_id >> 16;
0677
0678 for (c = hda_vendor_ids; c->id; c++) {
0679 if (c->id == vendor_id) {
0680 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
0681 return codec->vendor_name ? 0 : -ENOMEM;
0682 }
0683 }
0684
0685 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
0686 return codec->vendor_name ? 0 : -ENOMEM;
0687 }
0688
0689
0690
0691
0692 struct hda_rate_tbl {
0693 unsigned int hz;
0694 unsigned int alsa_bits;
0695 unsigned int hda_fmt;
0696 };
0697
0698
0699 #define HDA_RATE(base, mult, div) \
0700 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
0701 (((div) - 1) << AC_FMT_DIV_SHIFT))
0702
0703 static const struct hda_rate_tbl rate_bits[] = {
0704
0705
0706
0707 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
0708 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
0709 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
0710 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
0711 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
0712 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
0713 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
0714 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
0715 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
0716 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
0717 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
0718 #define AC_PAR_PCM_RATE_BITS 11
0719
0720
0721
0722 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
0723
0724 { 0 }
0725 };
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
0740 unsigned int channels,
0741 snd_pcm_format_t format,
0742 unsigned int maxbps,
0743 unsigned short spdif_ctls)
0744 {
0745 int i;
0746 unsigned int val = 0;
0747
0748 for (i = 0; rate_bits[i].hz; i++)
0749 if (rate_bits[i].hz == rate) {
0750 val = rate_bits[i].hda_fmt;
0751 break;
0752 }
0753 if (!rate_bits[i].hz)
0754 return 0;
0755
0756 if (channels == 0 || channels > 8)
0757 return 0;
0758 val |= channels - 1;
0759
0760 switch (snd_pcm_format_width(format)) {
0761 case 8:
0762 val |= AC_FMT_BITS_8;
0763 break;
0764 case 16:
0765 val |= AC_FMT_BITS_16;
0766 break;
0767 case 20:
0768 case 24:
0769 case 32:
0770 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
0771 val |= AC_FMT_BITS_32;
0772 else if (maxbps >= 24)
0773 val |= AC_FMT_BITS_24;
0774 else
0775 val |= AC_FMT_BITS_20;
0776 break;
0777 default:
0778 return 0;
0779 }
0780
0781 if (spdif_ctls & AC_DIG1_NONAUDIO)
0782 val |= AC_FMT_TYPE_NON_PCM;
0783
0784 return val;
0785 }
0786 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
0787
0788 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
0789 {
0790 unsigned int val = 0;
0791
0792 if (nid != codec->afg &&
0793 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
0794 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
0795 if (!val || val == -1)
0796 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
0797 if (!val || val == -1)
0798 return 0;
0799 return val;
0800 }
0801
0802 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
0803 {
0804 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
0805
0806 if (!streams || streams == -1)
0807 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
0808 if (!streams || streams == -1)
0809 return 0;
0810 return streams;
0811 }
0812
0813
0814
0815
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825
0826 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
0827 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
0828 {
0829 unsigned int i, val, wcaps;
0830
0831 wcaps = get_wcaps(codec, nid);
0832 val = query_pcm_param(codec, nid);
0833
0834 if (ratesp) {
0835 u32 rates = 0;
0836 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
0837 if (val & (1 << i))
0838 rates |= rate_bits[i].alsa_bits;
0839 }
0840 if (rates == 0) {
0841 dev_err(&codec->dev,
0842 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
0843 nid, val,
0844 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
0845 return -EIO;
0846 }
0847 *ratesp = rates;
0848 }
0849
0850 if (formatsp || bpsp) {
0851 u64 formats = 0;
0852 unsigned int streams, bps;
0853
0854 streams = query_stream_param(codec, nid);
0855 if (!streams)
0856 return -EIO;
0857
0858 bps = 0;
0859 if (streams & AC_SUPFMT_PCM) {
0860 if (val & AC_SUPPCM_BITS_8) {
0861 formats |= SNDRV_PCM_FMTBIT_U8;
0862 bps = 8;
0863 }
0864 if (val & AC_SUPPCM_BITS_16) {
0865 formats |= SNDRV_PCM_FMTBIT_S16_LE;
0866 bps = 16;
0867 }
0868 if (wcaps & AC_WCAP_DIGITAL) {
0869 if (val & AC_SUPPCM_BITS_32)
0870 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
0871 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
0872 formats |= SNDRV_PCM_FMTBIT_S32_LE;
0873 if (val & AC_SUPPCM_BITS_24)
0874 bps = 24;
0875 else if (val & AC_SUPPCM_BITS_20)
0876 bps = 20;
0877 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
0878 AC_SUPPCM_BITS_32)) {
0879 formats |= SNDRV_PCM_FMTBIT_S32_LE;
0880 if (val & AC_SUPPCM_BITS_32)
0881 bps = 32;
0882 else if (val & AC_SUPPCM_BITS_24)
0883 bps = 24;
0884 else if (val & AC_SUPPCM_BITS_20)
0885 bps = 20;
0886 }
0887 }
0888 #if 0
0889 if (streams & AC_SUPFMT_FLOAT32) {
0890 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
0891 if (!bps)
0892 bps = 32;
0893 }
0894 #endif
0895 if (streams == AC_SUPFMT_AC3) {
0896
0897
0898
0899
0900 formats |= SNDRV_PCM_FMTBIT_U8;
0901 bps = 8;
0902 }
0903 if (formats == 0) {
0904 dev_err(&codec->dev,
0905 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
0906 nid, val,
0907 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
0908 streams);
0909 return -EIO;
0910 }
0911 if (formatsp)
0912 *formatsp = formats;
0913 if (bpsp)
0914 *bpsp = bps;
0915 }
0916
0917 return 0;
0918 }
0919 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929
0930
0931 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
0932 unsigned int format)
0933 {
0934 int i;
0935 unsigned int val = 0, rate, stream;
0936
0937 val = query_pcm_param(codec, nid);
0938 if (!val)
0939 return false;
0940
0941 rate = format & 0xff00;
0942 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
0943 if (rate_bits[i].hda_fmt == rate) {
0944 if (val & (1 << i))
0945 break;
0946 return false;
0947 }
0948 if (i >= AC_PAR_PCM_RATE_BITS)
0949 return false;
0950
0951 stream = query_stream_param(codec, nid);
0952 if (!stream)
0953 return false;
0954
0955 if (stream & AC_SUPFMT_PCM) {
0956 switch (format & 0xf0) {
0957 case 0x00:
0958 if (!(val & AC_SUPPCM_BITS_8))
0959 return false;
0960 break;
0961 case 0x10:
0962 if (!(val & AC_SUPPCM_BITS_16))
0963 return false;
0964 break;
0965 case 0x20:
0966 if (!(val & AC_SUPPCM_BITS_20))
0967 return false;
0968 break;
0969 case 0x30:
0970 if (!(val & AC_SUPPCM_BITS_24))
0971 return false;
0972 break;
0973 case 0x40:
0974 if (!(val & AC_SUPPCM_BITS_32))
0975 return false;
0976 break;
0977 default:
0978 return false;
0979 }
0980 } else {
0981
0982 }
0983
0984 return true;
0985 }
0986 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
0987
0988 static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
0989 int flags, unsigned int verb, unsigned int parm)
0990 {
0991 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
0992 unsigned int res;
0993
0994 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
0995 return -1;
0996
0997 return res;
0998 }
0999
1000 static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
1001 int flags, unsigned int verb, unsigned int parm)
1002 {
1003 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1004
1005 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1006 }
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020 int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1021 int flags, unsigned int verb, unsigned int parm)
1022 {
1023 return codec_read(hdac, nid, flags, verb, parm);
1024 }
1025 EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039 int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1040 int flags, unsigned int verb, unsigned int parm)
1041 {
1042 return codec_write(hdac, nid, flags, verb, parm);
1043 }
1044 EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056 bool snd_hdac_check_power_state(struct hdac_device *hdac,
1057 hda_nid_t nid, unsigned int target_state)
1058 {
1059 unsigned int state = codec_read(hdac, nid, 0,
1060 AC_VERB_GET_POWER_STATE, 0);
1061
1062 if (state & AC_PWRST_ERROR)
1063 return true;
1064 state = (state >> 4) & 0x0f;
1065 return (state == target_state);
1066 }
1067 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078 unsigned int snd_hdac_sync_power_state(struct hdac_device *codec,
1079 hda_nid_t nid, unsigned int power_state)
1080 {
1081 unsigned long end_time = jiffies + msecs_to_jiffies(500);
1082 unsigned int state, actual_state, count;
1083
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) {
1088 msleep(20);
1089 break;
1090 }
1091 actual_state = (state >> 4) & 0x0f;
1092 if (actual_state == power_state)
1093 break;
1094 if (time_after_eq(jiffies, end_time))
1095 break;
1096
1097 msleep(1);
1098 }
1099 return state;
1100 }
1101 EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state);