1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * HD-audio codec core device 4 */ 5 6 #include <linux/init.h> 7 #include <linux/delay.h> 8 #include <linux/device.h> 9 #include <linux/slab.h> 10 #include <linux/module.h> 11 #include <linux/export.h> 12 #include <linux/pm_runtime.h> 13 #include <sound/hdaudio.h> 14 #include <sound/hda_regmap.h> 15 #include <sound/pcm.h> 16 #include "local.h" 17 18 static void setup_fg_nodes(struct hdac_device *codec); 19 static int get_codec_vendor_name(struct hdac_device *codec); 20 21 static void default_release(struct device *dev) 22 { 23 snd_hdac_device_exit(container_of(dev, struct hdac_device, dev)); 24 } 25 26 /** 27 * snd_hdac_device_init - initialize the HD-audio codec base device 28 * @codec: device to initialize 29 * @bus: but to attach 30 * @name: device name string 31 * @addr: codec address 32 * 33 * Returns zero for success or a negative error code. 34 * 35 * This function increments the runtime PM counter and marks it active. 36 * The caller needs to turn it off appropriately later. 37 * 38 * The caller needs to set the device's release op properly by itself. 39 */ 40 int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus, 41 const char *name, unsigned int addr) 42 { 43 struct device *dev; 44 hda_nid_t fg; 45 int err; 46 47 dev = &codec->dev; 48 device_initialize(dev); 49 dev->parent = bus->dev; 50 dev->bus = &snd_hda_bus_type; 51 dev->release = default_release; 52 dev->groups = hdac_dev_attr_groups; 53 dev_set_name(dev, "%s", name); 54 device_enable_async_suspend(dev); 55 56 codec->bus = bus; 57 codec->addr = addr; 58 codec->type = HDA_DEV_CORE; 59 mutex_init(&codec->widget_lock); 60 pm_runtime_set_active(&codec->dev); 61 pm_runtime_get_noresume(&codec->dev); 62 atomic_set(&codec->in_pm, 0); 63 64 err = snd_hdac_bus_add_device(bus, codec); 65 if (err < 0) 66 goto error; 67 68 /* fill parameters */ 69 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, 70 AC_PAR_VENDOR_ID); 71 if (codec->vendor_id == -1) { 72 /* read again, hopefully the access method was corrected 73 * in the last read... 74 */ 75 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, 76 AC_PAR_VENDOR_ID); 77 } 78 79 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, 80 AC_PAR_SUBSYSTEM_ID); 81 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, 82 AC_PAR_REV_ID); 83 84 setup_fg_nodes(codec); 85 if (!codec->afg && !codec->mfg) { 86 dev_err(dev, "no AFG or MFG node found\n"); 87 err = -ENODEV; 88 goto error; 89 } 90 91 fg = codec->afg ? codec->afg : codec->mfg; 92 93 err = snd_hdac_refresh_widgets(codec); 94 if (err < 0) 95 goto error; 96 97 codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE); 98 /* reread ssid if not set by parameter */ 99 if (codec->subsystem_id == -1 || codec->subsystem_id == 0) 100 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0, 101 &codec->subsystem_id); 102 103 err = get_codec_vendor_name(codec); 104 if (err < 0) 105 goto error; 106 107 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x", 108 codec->vendor_id & 0xffff); 109 if (!codec->chip_name) { 110 err = -ENOMEM; 111 goto error; 112 } 113 114 return 0; 115 116 error: 117 put_device(&codec->dev); 118 return err; 119 } 120 EXPORT_SYMBOL_GPL(snd_hdac_device_init); 121 122 /** 123 * snd_hdac_device_exit - clean up the HD-audio codec base device 124 * @codec: device to clean up 125 */ 126 void snd_hdac_device_exit(struct hdac_device *codec) 127 { 128 pm_runtime_put_noidle(&codec->dev); 129 snd_hdac_bus_remove_device(codec->bus, codec); 130 kfree(codec->vendor_name); 131 kfree(codec->chip_name); 132 } 133 EXPORT_SYMBOL_GPL(snd_hdac_device_exit); 134 135 /** 136 * snd_hdac_device_register - register the hd-audio codec base device 137 * codec: the device to register 138 */ 139 int snd_hdac_device_register(struct hdac_device *codec) 140 { 141 int err; 142 143 err = device_add(&codec->dev); 144 if (err < 0) 145 return err; 146 mutex_lock(&codec->widget_lock); 147 err = hda_widget_sysfs_init(codec); 148 mutex_unlock(&codec->widget_lock); 149 if (err < 0) { 150 device_del(&codec->dev); 151 return err; 152 } 153 154 return 0; 155 } 156 EXPORT_SYMBOL_GPL(snd_hdac_device_register); 157 158 /** 159 * snd_hdac_device_unregister - unregister the hd-audio codec base device 160 * codec: the device to unregister 161 */ 162 void snd_hdac_device_unregister(struct hdac_device *codec) 163 { 164 if (device_is_registered(&codec->dev)) { 165 mutex_lock(&codec->widget_lock); 166 hda_widget_sysfs_exit(codec); 167 mutex_unlock(&codec->widget_lock); 168 device_del(&codec->dev); 169 snd_hdac_bus_remove_device(codec->bus, codec); 170 } 171 } 172 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister); 173 174 /** 175 * snd_hdac_device_set_chip_name - set/update the codec name 176 * @codec: the HDAC device 177 * @name: name string to set 178 * 179 * Returns 0 if the name is set or updated, or a negative error code. 180 */ 181 int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name) 182 { 183 char *newname; 184 185 if (!name) 186 return 0; 187 newname = kstrdup(name, GFP_KERNEL); 188 if (!newname) 189 return -ENOMEM; 190 kfree(codec->chip_name); 191 codec->chip_name = newname; 192 return 0; 193 } 194 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name); 195 196 /** 197 * snd_hdac_codec_modalias - give the module alias name 198 * @codec: HDAC device 199 * @buf: string buffer to store 200 * @size: string buffer size 201 * 202 * Returns the size of string, like snprintf(), or a negative error code. 203 */ 204 int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size) 205 { 206 return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n", 207 codec->vendor_id, codec->revision_id, codec->type); 208 } 209 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias); 210 211 /** 212 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the 213 * HD-audio controller 214 * @codec: the codec object 215 * @nid: NID to encode 216 * @verb: verb to encode 217 * @parm: parameter to encode 218 * 219 * Return an encoded command verb or -1 for error. 220 */ 221 static unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid, 222 unsigned int verb, unsigned int parm) 223 { 224 u32 val, addr; 225 226 addr = codec->addr; 227 if ((addr & ~0xf) || (nid & ~0x7f) || 228 (verb & ~0xfff) || (parm & ~0xffff)) { 229 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n", 230 addr, nid, verb, parm); 231 return -1; 232 } 233 234 val = addr << 28; 235 val |= (u32)nid << 20; 236 val |= verb << 8; 237 val |= parm; 238 return val; 239 } 240 241 /** 242 * snd_hdac_exec_verb - execute an encoded verb 243 * @codec: the codec object 244 * @cmd: encoded verb to execute 245 * @flags: optional flags, pass zero for default 246 * @res: the pointer to store the result, NULL if running async 247 * 248 * Returns zero if successful, or a negative error code. 249 * 250 * This calls the exec_verb op when set in hdac_codec. If not, 251 * call the default snd_hdac_bus_exec_verb(). 252 */ 253 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd, 254 unsigned int flags, unsigned int *res) 255 { 256 if (codec->exec_verb) 257 return codec->exec_verb(codec, cmd, flags, res); 258 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res); 259 } 260 261 262 /** 263 * snd_hdac_read - execute a verb 264 * @codec: the codec object 265 * @nid: NID to execute a verb 266 * @verb: verb to execute 267 * @parm: parameter for a verb 268 * @res: the pointer to store the result, NULL if running async 269 * 270 * Returns zero if successful, or a negative error code. 271 */ 272 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid, 273 unsigned int verb, unsigned int parm, unsigned int *res) 274 { 275 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm); 276 277 return snd_hdac_exec_verb(codec, cmd, 0, res); 278 } 279 EXPORT_SYMBOL_GPL(snd_hdac_read); 280 281 /** 282 * _snd_hdac_read_parm - read a parmeter 283 * 284 * This function returns zero or an error unlike snd_hdac_read_parm(). 285 */ 286 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm, 287 unsigned int *res) 288 { 289 unsigned int cmd; 290 291 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm; 292 return snd_hdac_regmap_read_raw(codec, cmd, res); 293 } 294 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm); 295 296 /** 297 * snd_hdac_read_parm_uncached - read a codec parameter without caching 298 * @codec: the codec object 299 * @nid: NID to read a parameter 300 * @parm: parameter to read 301 * 302 * Returns -1 for error. If you need to distinguish the error more 303 * strictly, use snd_hdac_read() directly. 304 */ 305 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid, 306 int parm) 307 { 308 unsigned int cmd, val; 309 310 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm; 311 if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0) 312 return -1; 313 return val; 314 } 315 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached); 316 317 /** 318 * snd_hdac_override_parm - override read-only parameters 319 * @codec: the codec object 320 * @nid: NID for the parameter 321 * @parm: the parameter to change 322 * @val: the parameter value to overwrite 323 */ 324 int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid, 325 unsigned int parm, unsigned int val) 326 { 327 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm; 328 int err; 329 330 if (!codec->regmap) 331 return -EINVAL; 332 333 codec->caps_overwriting = true; 334 err = snd_hdac_regmap_write_raw(codec, verb, val); 335 codec->caps_overwriting = false; 336 return err; 337 } 338 EXPORT_SYMBOL_GPL(snd_hdac_override_parm); 339 340 /** 341 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes 342 * @codec: the codec object 343 * @nid: NID to inspect 344 * @start_id: the pointer to store the starting NID 345 * 346 * Returns the number of subtree nodes or zero if not found. 347 * This function reads parameters always without caching. 348 */ 349 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid, 350 hda_nid_t *start_id) 351 { 352 unsigned int parm; 353 354 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT); 355 if (parm == -1) { 356 *start_id = 0; 357 return 0; 358 } 359 *start_id = (parm >> 16) & 0x7fff; 360 return (int)(parm & 0x7fff); 361 } 362 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes); 363 364 /* 365 * look for an AFG and MFG nodes 366 */ 367 static void setup_fg_nodes(struct hdac_device *codec) 368 { 369 int i, total_nodes, function_id; 370 hda_nid_t nid; 371 372 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid); 373 for (i = 0; i < total_nodes; i++, nid++) { 374 function_id = snd_hdac_read_parm(codec, nid, 375 AC_PAR_FUNCTION_TYPE); 376 switch (function_id & 0xff) { 377 case AC_GRP_AUDIO_FUNCTION: 378 codec->afg = nid; 379 codec->afg_function_id = function_id & 0xff; 380 codec->afg_unsol = (function_id >> 8) & 1; 381 break; 382 case AC_GRP_MODEM_FUNCTION: 383 codec->mfg = nid; 384 codec->mfg_function_id = function_id & 0xff; 385 codec->mfg_unsol = (function_id >> 8) & 1; 386 break; 387 default: 388 break; 389 } 390 } 391 } 392 393 /** 394 * snd_hdac_refresh_widgets - Reset the widget start/end nodes 395 * @codec: the codec object 396 */ 397 int snd_hdac_refresh_widgets(struct hdac_device *codec) 398 { 399 hda_nid_t start_nid; 400 int nums, err = 0; 401 402 /* 403 * Serialize against multiple threads trying to update the sysfs 404 * widgets array. 405 */ 406 mutex_lock(&codec->widget_lock); 407 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid); 408 if (!start_nid || nums <= 0 || nums >= 0xff) { 409 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n", 410 codec->afg); 411 err = -EINVAL; 412 goto unlock; 413 } 414 415 err = hda_widget_sysfs_reinit(codec, start_nid, nums); 416 if (err < 0) 417 goto unlock; 418 419 codec->num_nodes = nums; 420 codec->start_nid = start_nid; 421 codec->end_nid = start_nid + nums; 422 unlock: 423 mutex_unlock(&codec->widget_lock); 424 return err; 425 } 426 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets); 427 428 /* return CONNLIST_LEN parameter of the given widget */ 429 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid) 430 { 431 unsigned int wcaps = get_wcaps(codec, nid); 432 unsigned int parm; 433 434 if (!(wcaps & AC_WCAP_CONN_LIST) && 435 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) 436 return 0; 437 438 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN); 439 if (parm == -1) 440 parm = 0; 441 return parm; 442 } 443 444 /** 445 * snd_hdac_get_connections - get a widget connection list 446 * @codec: the codec object 447 * @nid: NID 448 * @conn_list: the array to store the results, can be NULL 449 * @max_conns: the max size of the given array 450 * 451 * Returns the number of connected widgets, zero for no connection, or a 452 * negative error code. When the number of elements don't fit with the 453 * given array size, it returns -ENOSPC. 454 * 455 * When @conn_list is NULL, it just checks the number of connections. 456 */ 457 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid, 458 hda_nid_t *conn_list, int max_conns) 459 { 460 unsigned int parm; 461 int i, conn_len, conns, err; 462 unsigned int shift, num_elems, mask; 463 hda_nid_t prev_nid; 464 int null_count = 0; 465 466 parm = get_num_conns(codec, nid); 467 if (!parm) 468 return 0; 469 470 if (parm & AC_CLIST_LONG) { 471 /* long form */ 472 shift = 16; 473 num_elems = 2; 474 } else { 475 /* short form */ 476 shift = 8; 477 num_elems = 4; 478 } 479 conn_len = parm & AC_CLIST_LENGTH; 480 mask = (1 << (shift-1)) - 1; 481 482 if (!conn_len) 483 return 0; /* no connection */ 484 485 if (conn_len == 1) { 486 /* single connection */ 487 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0, 488 &parm); 489 if (err < 0) 490 return err; 491 if (conn_list) 492 conn_list[0] = parm & mask; 493 return 1; 494 } 495 496 /* multi connection */ 497 conns = 0; 498 prev_nid = 0; 499 for (i = 0; i < conn_len; i++) { 500 int range_val; 501 hda_nid_t val, n; 502 503 if (i % num_elems == 0) { 504 err = snd_hdac_read(codec, nid, 505 AC_VERB_GET_CONNECT_LIST, i, 506 &parm); 507 if (err < 0) 508 return -EIO; 509 } 510 range_val = !!(parm & (1 << (shift-1))); /* ranges */ 511 val = parm & mask; 512 if (val == 0 && null_count++) { /* no second chance */ 513 dev_dbg(&codec->dev, 514 "invalid CONNECT_LIST verb %x[%i]:%x\n", 515 nid, i, parm); 516 return 0; 517 } 518 parm >>= shift; 519 if (range_val) { 520 /* ranges between the previous and this one */ 521 if (!prev_nid || prev_nid >= val) { 522 dev_warn(&codec->dev, 523 "invalid dep_range_val %x:%x\n", 524 prev_nid, val); 525 continue; 526 } 527 for (n = prev_nid + 1; n <= val; n++) { 528 if (conn_list) { 529 if (conns >= max_conns) 530 return -ENOSPC; 531 conn_list[conns] = n; 532 } 533 conns++; 534 } 535 } else { 536 if (conn_list) { 537 if (conns >= max_conns) 538 return -ENOSPC; 539 conn_list[conns] = val; 540 } 541 conns++; 542 } 543 prev_nid = val; 544 } 545 return conns; 546 } 547 EXPORT_SYMBOL_GPL(snd_hdac_get_connections); 548 549 #ifdef CONFIG_PM 550 /** 551 * snd_hdac_power_up - power up the codec 552 * @codec: the codec object 553 * 554 * This function calls the runtime PM helper to power up the given codec. 555 * Unlike snd_hdac_power_up_pm(), you should call this only for the code 556 * path that isn't included in PM path. Otherwise it gets stuck. 557 * 558 * Returns zero if successful, or a negative error code. 559 */ 560 int snd_hdac_power_up(struct hdac_device *codec) 561 { 562 return pm_runtime_get_sync(&codec->dev); 563 } 564 EXPORT_SYMBOL_GPL(snd_hdac_power_up); 565 566 /** 567 * snd_hdac_power_down - power down the codec 568 * @codec: the codec object 569 * 570 * Returns zero if successful, or a negative error code. 571 */ 572 int snd_hdac_power_down(struct hdac_device *codec) 573 { 574 struct device *dev = &codec->dev; 575 576 pm_runtime_mark_last_busy(dev); 577 return pm_runtime_put_autosuspend(dev); 578 } 579 EXPORT_SYMBOL_GPL(snd_hdac_power_down); 580 581 /** 582 * snd_hdac_power_up_pm - power up the codec 583 * @codec: the codec object 584 * 585 * This function can be called in a recursive code path like init code 586 * which may be called by PM suspend/resume again. OTOH, if a power-up 587 * call must wake up the sleeper (e.g. in a kctl callback), use 588 * snd_hdac_power_up() instead. 589 * 590 * Returns zero if successful, or a negative error code. 591 */ 592 int snd_hdac_power_up_pm(struct hdac_device *codec) 593 { 594 if (!atomic_inc_not_zero(&codec->in_pm)) 595 return snd_hdac_power_up(codec); 596 return 0; 597 } 598 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm); 599 600 /* like snd_hdac_power_up_pm(), but only increment the pm count when 601 * already powered up. Returns -1 if not powered up, 1 if incremented 602 * or 0 if unchanged. Only used in hdac_regmap.c 603 */ 604 int snd_hdac_keep_power_up(struct hdac_device *codec) 605 { 606 if (!atomic_inc_not_zero(&codec->in_pm)) { 607 int ret = pm_runtime_get_if_in_use(&codec->dev); 608 if (!ret) 609 return -1; 610 if (ret < 0) 611 return 0; 612 } 613 return 1; 614 } 615 616 /** 617 * snd_hdac_power_down_pm - power down the codec 618 * @codec: the codec object 619 * 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. 622 * 623 * Returns zero if successful, or a negative error code. 624 */ 625 int snd_hdac_power_down_pm(struct hdac_device *codec) 626 { 627 if (atomic_dec_if_positive(&codec->in_pm) < 0) 628 return snd_hdac_power_down(codec); 629 return 0; 630 } 631 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm); 632 #endif 633 634 /* codec vendor labels */ 635 struct hda_vendor_id { 636 unsigned int id; 637 const char *name; 638 }; 639 640 static struct hda_vendor_id hda_vendor_ids[] = { 641 { 0x1002, "ATI" }, 642 { 0x1013, "Cirrus Logic" }, 643 { 0x1057, "Motorola" }, 644 { 0x1095, "Silicon Image" }, 645 { 0x10de, "Nvidia" }, 646 { 0x10ec, "Realtek" }, 647 { 0x1102, "Creative" }, 648 { 0x1106, "VIA" }, 649 { 0x111d, "IDT" }, 650 { 0x11c1, "LSI" }, 651 { 0x11d4, "Analog Devices" }, 652 { 0x13f6, "C-Media" }, 653 { 0x14f1, "Conexant" }, 654 { 0x17e8, "Chrontel" }, 655 { 0x1854, "LG" }, 656 { 0x1aec, "Wolfson Microelectronics" }, 657 { 0x1af4, "QEMU" }, 658 { 0x434d, "C-Media" }, 659 { 0x8086, "Intel" }, 660 { 0x8384, "SigmaTel" }, 661 {} /* terminator */ 662 }; 663 664 /* store the codec vendor name */ 665 static int get_codec_vendor_name(struct hdac_device *codec) 666 { 667 const struct hda_vendor_id *c; 668 u16 vendor_id = codec->vendor_id >> 16; 669 670 for (c = hda_vendor_ids; c->id; c++) { 671 if (c->id == vendor_id) { 672 codec->vendor_name = kstrdup(c->name, GFP_KERNEL); 673 return codec->vendor_name ? 0 : -ENOMEM; 674 } 675 } 676 677 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id); 678 return codec->vendor_name ? 0 : -ENOMEM; 679 } 680 681 /* 682 * stream formats 683 */ 684 struct hda_rate_tbl { 685 unsigned int hz; 686 unsigned int alsa_bits; 687 unsigned int hda_fmt; 688 }; 689 690 /* rate = base * mult / div */ 691 #define HDA_RATE(base, mult, div) \ 692 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ 693 (((div) - 1) << AC_FMT_DIV_SHIFT)) 694 695 static struct hda_rate_tbl rate_bits[] = { 696 /* rate in Hz, ALSA rate bitmask, HDA format value */ 697 698 /* autodetected value used in snd_hda_query_supported_pcm */ 699 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, 700 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, 701 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, 702 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, 703 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, 704 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, 705 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, 706 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, 707 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, 708 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, 709 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, 710 #define AC_PAR_PCM_RATE_BITS 11 711 /* up to bits 10, 384kHZ isn't supported properly */ 712 713 /* not autodetected value */ 714 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, 715 716 { 0 } /* terminator */ 717 }; 718 719 /** 720 * snd_hdac_calc_stream_format - calculate the format bitset 721 * @rate: the sample rate 722 * @channels: the number of channels 723 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) 724 * @maxbps: the max. bps 725 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) 726 * 727 * Calculate the format bitset from the given rate, channels and th PCM format. 728 * 729 * Return zero if invalid. 730 */ 731 unsigned int snd_hdac_calc_stream_format(unsigned int rate, 732 unsigned int channels, 733 snd_pcm_format_t format, 734 unsigned int maxbps, 735 unsigned short spdif_ctls) 736 { 737 int i; 738 unsigned int val = 0; 739 740 for (i = 0; rate_bits[i].hz; i++) 741 if (rate_bits[i].hz == rate) { 742 val = rate_bits[i].hda_fmt; 743 break; 744 } 745 if (!rate_bits[i].hz) 746 return 0; 747 748 if (channels == 0 || channels > 8) 749 return 0; 750 val |= channels - 1; 751 752 switch (snd_pcm_format_width(format)) { 753 case 8: 754 val |= AC_FMT_BITS_8; 755 break; 756 case 16: 757 val |= AC_FMT_BITS_16; 758 break; 759 case 20: 760 case 24: 761 case 32: 762 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) 763 val |= AC_FMT_BITS_32; 764 else if (maxbps >= 24) 765 val |= AC_FMT_BITS_24; 766 else 767 val |= AC_FMT_BITS_20; 768 break; 769 default: 770 return 0; 771 } 772 773 if (spdif_ctls & AC_DIG1_NONAUDIO) 774 val |= AC_FMT_TYPE_NON_PCM; 775 776 return val; 777 } 778 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format); 779 780 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid) 781 { 782 unsigned int val = 0; 783 784 if (nid != codec->afg && 785 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) 786 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM); 787 if (!val || val == -1) 788 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM); 789 if (!val || val == -1) 790 return 0; 791 return val; 792 } 793 794 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid) 795 { 796 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM); 797 798 if (!streams || streams == -1) 799 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM); 800 if (!streams || streams == -1) 801 return 0; 802 return streams; 803 } 804 805 /** 806 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats 807 * @codec: the codec object 808 * @nid: NID to query 809 * @ratesp: the pointer to store the detected rate bitflags 810 * @formatsp: the pointer to store the detected formats 811 * @bpsp: the pointer to store the detected format widths 812 * 813 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp 814 * or @bsps argument is ignored. 815 * 816 * Returns 0 if successful, otherwise a negative error code. 817 */ 818 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid, 819 u32 *ratesp, u64 *formatsp, unsigned int *bpsp) 820 { 821 unsigned int i, val, wcaps; 822 823 wcaps = get_wcaps(codec, nid); 824 val = query_pcm_param(codec, nid); 825 826 if (ratesp) { 827 u32 rates = 0; 828 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { 829 if (val & (1 << i)) 830 rates |= rate_bits[i].alsa_bits; 831 } 832 if (rates == 0) { 833 dev_err(&codec->dev, 834 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", 835 nid, val, 836 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); 837 return -EIO; 838 } 839 *ratesp = rates; 840 } 841 842 if (formatsp || bpsp) { 843 u64 formats = 0; 844 unsigned int streams, bps; 845 846 streams = query_stream_param(codec, nid); 847 if (!streams) 848 return -EIO; 849 850 bps = 0; 851 if (streams & AC_SUPFMT_PCM) { 852 if (val & AC_SUPPCM_BITS_8) { 853 formats |= SNDRV_PCM_FMTBIT_U8; 854 bps = 8; 855 } 856 if (val & AC_SUPPCM_BITS_16) { 857 formats |= SNDRV_PCM_FMTBIT_S16_LE; 858 bps = 16; 859 } 860 if (wcaps & AC_WCAP_DIGITAL) { 861 if (val & AC_SUPPCM_BITS_32) 862 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; 863 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) 864 formats |= SNDRV_PCM_FMTBIT_S32_LE; 865 if (val & AC_SUPPCM_BITS_24) 866 bps = 24; 867 else if (val & AC_SUPPCM_BITS_20) 868 bps = 20; 869 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| 870 AC_SUPPCM_BITS_32)) { 871 formats |= SNDRV_PCM_FMTBIT_S32_LE; 872 if (val & AC_SUPPCM_BITS_32) 873 bps = 32; 874 else if (val & AC_SUPPCM_BITS_24) 875 bps = 24; 876 else if (val & AC_SUPPCM_BITS_20) 877 bps = 20; 878 } 879 } 880 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */ 881 if (streams & AC_SUPFMT_FLOAT32) { 882 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; 883 if (!bps) 884 bps = 32; 885 } 886 #endif 887 if (streams == AC_SUPFMT_AC3) { 888 /* should be exclusive */ 889 /* temporary hack: we have still no proper support 890 * for the direct AC3 stream... 891 */ 892 formats |= SNDRV_PCM_FMTBIT_U8; 893 bps = 8; 894 } 895 if (formats == 0) { 896 dev_err(&codec->dev, 897 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", 898 nid, val, 899 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, 900 streams); 901 return -EIO; 902 } 903 if (formatsp) 904 *formatsp = formats; 905 if (bpsp) 906 *bpsp = bps; 907 } 908 909 return 0; 910 } 911 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm); 912 913 /** 914 * snd_hdac_is_supported_format - Check the validity of the format 915 * @codec: the codec object 916 * @nid: NID to check 917 * @format: the HD-audio format value to check 918 * 919 * Check whether the given node supports the format value. 920 * 921 * Returns true if supported, false if not. 922 */ 923 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid, 924 unsigned int format) 925 { 926 int i; 927 unsigned int val = 0, rate, stream; 928 929 val = query_pcm_param(codec, nid); 930 if (!val) 931 return false; 932 933 rate = format & 0xff00; 934 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) 935 if (rate_bits[i].hda_fmt == rate) { 936 if (val & (1 << i)) 937 break; 938 return false; 939 } 940 if (i >= AC_PAR_PCM_RATE_BITS) 941 return false; 942 943 stream = query_stream_param(codec, nid); 944 if (!stream) 945 return false; 946 947 if (stream & AC_SUPFMT_PCM) { 948 switch (format & 0xf0) { 949 case 0x00: 950 if (!(val & AC_SUPPCM_BITS_8)) 951 return false; 952 break; 953 case 0x10: 954 if (!(val & AC_SUPPCM_BITS_16)) 955 return false; 956 break; 957 case 0x20: 958 if (!(val & AC_SUPPCM_BITS_20)) 959 return false; 960 break; 961 case 0x30: 962 if (!(val & AC_SUPPCM_BITS_24)) 963 return false; 964 break; 965 case 0x40: 966 if (!(val & AC_SUPPCM_BITS_32)) 967 return false; 968 break; 969 default: 970 return false; 971 } 972 } else { 973 /* FIXME: check for float32 and AC3? */ 974 } 975 976 return true; 977 } 978 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format); 979 980 static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid, 981 int flags, unsigned int verb, unsigned int parm) 982 { 983 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm); 984 unsigned int res; 985 986 if (snd_hdac_exec_verb(hdac, cmd, flags, &res)) 987 return -1; 988 989 return res; 990 } 991 992 static int codec_write(struct hdac_device *hdac, hda_nid_t nid, 993 int flags, unsigned int verb, unsigned int parm) 994 { 995 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm); 996 997 return snd_hdac_exec_verb(hdac, cmd, flags, NULL); 998 } 999 1000 /** 1001 * snd_hdac_codec_read - send a command and get the response 1002 * @hdac: the HDAC device 1003 * @nid: NID to send the command 1004 * @flags: optional bit flags 1005 * @verb: the verb to send 1006 * @parm: the parameter for the verb 1007 * 1008 * Send a single command and read the corresponding response. 1009 * 1010 * Returns the obtained response value, or -1 for an error. 1011 */ 1012 int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid, 1013 int flags, unsigned int verb, unsigned int parm) 1014 { 1015 return codec_read(hdac, nid, flags, verb, parm); 1016 } 1017 EXPORT_SYMBOL_GPL(snd_hdac_codec_read); 1018 1019 /** 1020 * snd_hdac_codec_write - send a single command without waiting for response 1021 * @hdac: the HDAC device 1022 * @nid: NID to send the command 1023 * @flags: optional bit flags 1024 * @verb: the verb to send 1025 * @parm: the parameter for the verb 1026 * 1027 * Send a single command without waiting for response. 1028 * 1029 * Returns 0 if successful, or a negative error code. 1030 */ 1031 int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid, 1032 int flags, unsigned int verb, unsigned int parm) 1033 { 1034 return codec_write(hdac, nid, flags, verb, parm); 1035 } 1036 EXPORT_SYMBOL_GPL(snd_hdac_codec_write); 1037 1038 /** 1039 * snd_hdac_check_power_state - check whether the actual power state matches 1040 * with the target state 1041 * 1042 * @hdac: the HDAC device 1043 * @nid: NID to send the command 1044 * @target_state: target state to check for 1045 * 1046 * Return true if state matches, false if not 1047 */ 1048 bool snd_hdac_check_power_state(struct hdac_device *hdac, 1049 hda_nid_t nid, unsigned int target_state) 1050 { 1051 unsigned int state = codec_read(hdac, nid, 0, 1052 AC_VERB_GET_POWER_STATE, 0); 1053 1054 if (state & AC_PWRST_ERROR) 1055 return true; 1056 state = (state >> 4) & 0x0f; 1057 return (state == target_state); 1058 } 1059 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state); 1060 /** 1061 * snd_hdac_sync_power_state - wait until actual power state matches 1062 * with the target state 1063 * 1064 * @hdac: the HDAC device 1065 * @nid: NID to send the command 1066 * @target_state: target state to check for 1067 * 1068 * Return power state or PS_ERROR if codec rejects GET verb. 1069 */ 1070 unsigned int snd_hdac_sync_power_state(struct hdac_device *codec, 1071 hda_nid_t nid, unsigned int power_state) 1072 { 1073 unsigned long end_time = jiffies + msecs_to_jiffies(500); 1074 unsigned int state, actual_state, count; 1075 1076 for (count = 0; count < 500; count++) { 1077 state = snd_hdac_codec_read(codec, nid, 0, 1078 AC_VERB_GET_POWER_STATE, 0); 1079 if (state & AC_PWRST_ERROR) { 1080 msleep(20); 1081 break; 1082 } 1083 actual_state = (state >> 4) & 0x0f; 1084 if (actual_state == power_state) 1085 break; 1086 if (time_after_eq(jiffies, end_time)) 1087 break; 1088 /* wait until the codec reachs to the target state */ 1089 msleep(1); 1090 } 1091 return state; 1092 } 1093 EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state); 1094