1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) 2 // Copyright(c) 2015-17 Intel Corporation. 3 4 #include <linux/acpi.h> 5 #include <linux/delay.h> 6 #include <linux/mod_devicetable.h> 7 #include <linux/pm_runtime.h> 8 #include <linux/soundwire/sdw_registers.h> 9 #include <linux/soundwire/sdw.h> 10 #include "bus.h" 11 #include "sysfs_local.h" 12 13 static DEFINE_IDA(sdw_ida); 14 15 static int sdw_get_id(struct sdw_bus *bus) 16 { 17 int rc = ida_alloc(&sdw_ida, GFP_KERNEL); 18 19 if (rc < 0) 20 return rc; 21 22 bus->id = rc; 23 return 0; 24 } 25 26 /** 27 * sdw_bus_master_add() - add a bus Master instance 28 * @bus: bus instance 29 * @parent: parent device 30 * @fwnode: firmware node handle 31 * 32 * Initializes the bus instance, read properties and create child 33 * devices. 34 */ 35 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent, 36 struct fwnode_handle *fwnode) 37 { 38 struct sdw_master_prop *prop = NULL; 39 int ret; 40 41 if (!parent) { 42 pr_err("SoundWire parent device is not set\n"); 43 return -ENODEV; 44 } 45 46 ret = sdw_get_id(bus); 47 if (ret < 0) { 48 dev_err(parent, "Failed to get bus id\n"); 49 return ret; 50 } 51 52 ret = sdw_master_device_add(bus, parent, fwnode); 53 if (ret < 0) { 54 dev_err(parent, "Failed to add master device at link %d\n", 55 bus->link_id); 56 return ret; 57 } 58 59 if (!bus->ops) { 60 dev_err(bus->dev, "SoundWire Bus ops are not set\n"); 61 return -EINVAL; 62 } 63 64 if (!bus->compute_params) { 65 dev_err(bus->dev, 66 "Bandwidth allocation not configured, compute_params no set\n"); 67 return -EINVAL; 68 } 69 70 mutex_init(&bus->msg_lock); 71 mutex_init(&bus->bus_lock); 72 INIT_LIST_HEAD(&bus->slaves); 73 INIT_LIST_HEAD(&bus->m_rt_list); 74 75 /* 76 * Initialize multi_link flag 77 * TODO: populate this flag by reading property from FW node 78 */ 79 bus->multi_link = false; 80 if (bus->ops->read_prop) { 81 ret = bus->ops->read_prop(bus); 82 if (ret < 0) { 83 dev_err(bus->dev, 84 "Bus read properties failed:%d\n", ret); 85 return ret; 86 } 87 } 88 89 sdw_bus_debugfs_init(bus); 90 91 /* 92 * Device numbers in SoundWire are 0 through 15. Enumeration device 93 * number (0), Broadcast device number (15), Group numbers (12 and 94 * 13) and Master device number (14) are not used for assignment so 95 * mask these and other higher bits. 96 */ 97 98 /* Set higher order bits */ 99 *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM); 100 101 /* Set enumuration device number and broadcast device number */ 102 set_bit(SDW_ENUM_DEV_NUM, bus->assigned); 103 set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned); 104 105 /* Set group device numbers and master device number */ 106 set_bit(SDW_GROUP12_DEV_NUM, bus->assigned); 107 set_bit(SDW_GROUP13_DEV_NUM, bus->assigned); 108 set_bit(SDW_MASTER_DEV_NUM, bus->assigned); 109 110 /* 111 * SDW is an enumerable bus, but devices can be powered off. So, 112 * they won't be able to report as present. 113 * 114 * Create Slave devices based on Slaves described in 115 * the respective firmware (ACPI/DT) 116 */ 117 if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev)) 118 ret = sdw_acpi_find_slaves(bus); 119 else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node) 120 ret = sdw_of_find_slaves(bus); 121 else 122 ret = -ENOTSUPP; /* No ACPI/DT so error out */ 123 124 if (ret < 0) { 125 dev_err(bus->dev, "Finding slaves failed:%d\n", ret); 126 return ret; 127 } 128 129 /* 130 * Initialize clock values based on Master properties. The max 131 * frequency is read from max_clk_freq property. Current assumption 132 * is that the bus will start at highest clock frequency when 133 * powered on. 134 * 135 * Default active bank will be 0 as out of reset the Slaves have 136 * to start with bank 0 (Table 40 of Spec) 137 */ 138 prop = &bus->prop; 139 bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR; 140 bus->params.curr_dr_freq = bus->params.max_dr_freq; 141 bus->params.curr_bank = SDW_BANK0; 142 bus->params.next_bank = SDW_BANK1; 143 144 return 0; 145 } 146 EXPORT_SYMBOL(sdw_bus_master_add); 147 148 static int sdw_delete_slave(struct device *dev, void *data) 149 { 150 struct sdw_slave *slave = dev_to_sdw_dev(dev); 151 struct sdw_bus *bus = slave->bus; 152 153 pm_runtime_disable(dev); 154 155 sdw_slave_debugfs_exit(slave); 156 157 mutex_lock(&bus->bus_lock); 158 159 if (slave->dev_num) /* clear dev_num if assigned */ 160 clear_bit(slave->dev_num, bus->assigned); 161 162 list_del_init(&slave->node); 163 mutex_unlock(&bus->bus_lock); 164 165 device_unregister(dev); 166 return 0; 167 } 168 169 /** 170 * sdw_bus_master_delete() - delete the bus master instance 171 * @bus: bus to be deleted 172 * 173 * Remove the instance, delete the child devices. 174 */ 175 void sdw_bus_master_delete(struct sdw_bus *bus) 176 { 177 device_for_each_child(bus->dev, NULL, sdw_delete_slave); 178 sdw_master_device_del(bus); 179 180 sdw_bus_debugfs_exit(bus); 181 ida_free(&sdw_ida, bus->id); 182 } 183 EXPORT_SYMBOL(sdw_bus_master_delete); 184 185 /* 186 * SDW IO Calls 187 */ 188 189 static inline int find_response_code(enum sdw_command_response resp) 190 { 191 switch (resp) { 192 case SDW_CMD_OK: 193 return 0; 194 195 case SDW_CMD_IGNORED: 196 return -ENODATA; 197 198 case SDW_CMD_TIMEOUT: 199 return -ETIMEDOUT; 200 201 default: 202 return -EIO; 203 } 204 } 205 206 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg) 207 { 208 int retry = bus->prop.err_threshold; 209 enum sdw_command_response resp; 210 int ret = 0, i; 211 212 for (i = 0; i <= retry; i++) { 213 resp = bus->ops->xfer_msg(bus, msg); 214 ret = find_response_code(resp); 215 216 /* if cmd is ok or ignored return */ 217 if (ret == 0 || ret == -ENODATA) 218 return ret; 219 } 220 221 return ret; 222 } 223 224 static inline int do_transfer_defer(struct sdw_bus *bus, 225 struct sdw_msg *msg, 226 struct sdw_defer *defer) 227 { 228 int retry = bus->prop.err_threshold; 229 enum sdw_command_response resp; 230 int ret = 0, i; 231 232 defer->msg = msg; 233 defer->length = msg->len; 234 init_completion(&defer->complete); 235 236 for (i = 0; i <= retry; i++) { 237 resp = bus->ops->xfer_msg_defer(bus, msg, defer); 238 ret = find_response_code(resp); 239 /* if cmd is ok or ignored return */ 240 if (ret == 0 || ret == -ENODATA) 241 return ret; 242 } 243 244 return ret; 245 } 246 247 static int sdw_reset_page(struct sdw_bus *bus, u16 dev_num) 248 { 249 int retry = bus->prop.err_threshold; 250 enum sdw_command_response resp; 251 int ret = 0, i; 252 253 for (i = 0; i <= retry; i++) { 254 resp = bus->ops->reset_page_addr(bus, dev_num); 255 ret = find_response_code(resp); 256 /* if cmd is ok or ignored return */ 257 if (ret == 0 || ret == -ENODATA) 258 return ret; 259 } 260 261 return ret; 262 } 263 264 static int sdw_transfer_unlocked(struct sdw_bus *bus, struct sdw_msg *msg) 265 { 266 int ret; 267 268 ret = do_transfer(bus, msg); 269 if (ret != 0 && ret != -ENODATA) 270 dev_err(bus->dev, "trf on Slave %d failed:%d %s addr %x count %d\n", 271 msg->dev_num, ret, 272 (msg->flags & SDW_MSG_FLAG_WRITE) ? "write" : "read", 273 msg->addr, msg->len); 274 275 if (msg->page) 276 sdw_reset_page(bus, msg->dev_num); 277 278 return ret; 279 } 280 281 /** 282 * sdw_transfer() - Synchronous transfer message to a SDW Slave device 283 * @bus: SDW bus 284 * @msg: SDW message to be xfered 285 */ 286 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg) 287 { 288 int ret; 289 290 mutex_lock(&bus->msg_lock); 291 292 ret = sdw_transfer_unlocked(bus, msg); 293 294 mutex_unlock(&bus->msg_lock); 295 296 return ret; 297 } 298 299 /** 300 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device 301 * @bus: SDW bus 302 * @msg: SDW message to be xfered 303 * @defer: Defer block for signal completion 304 * 305 * Caller needs to hold the msg_lock lock while calling this 306 */ 307 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg, 308 struct sdw_defer *defer) 309 { 310 int ret; 311 312 if (!bus->ops->xfer_msg_defer) 313 return -ENOTSUPP; 314 315 ret = do_transfer_defer(bus, msg, defer); 316 if (ret != 0 && ret != -ENODATA) 317 dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n", 318 msg->dev_num, ret); 319 320 if (msg->page) 321 sdw_reset_page(bus, msg->dev_num); 322 323 return ret; 324 } 325 326 int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave, 327 u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf) 328 { 329 memset(msg, 0, sizeof(*msg)); 330 msg->addr = addr; /* addr is 16 bit and truncated here */ 331 msg->len = count; 332 msg->dev_num = dev_num; 333 msg->flags = flags; 334 msg->buf = buf; 335 336 if (addr < SDW_REG_NO_PAGE) /* no paging area */ 337 return 0; 338 339 if (addr >= SDW_REG_MAX) { /* illegal addr */ 340 pr_err("SDW: Invalid address %x passed\n", addr); 341 return -EINVAL; 342 } 343 344 if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */ 345 if (slave && !slave->prop.paging_support) 346 return 0; 347 /* no need for else as that will fall-through to paging */ 348 } 349 350 /* paging mandatory */ 351 if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) { 352 pr_err("SDW: Invalid device for paging :%d\n", dev_num); 353 return -EINVAL; 354 } 355 356 if (!slave) { 357 pr_err("SDW: No slave for paging addr\n"); 358 return -EINVAL; 359 } 360 361 if (!slave->prop.paging_support) { 362 dev_err(&slave->dev, 363 "address %x needs paging but no support\n", addr); 364 return -EINVAL; 365 } 366 367 msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr); 368 msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr); 369 msg->addr |= BIT(15); 370 msg->page = true; 371 372 return 0; 373 } 374 375 /* 376 * Read/Write IO functions. 377 * no_pm versions can only be called by the bus, e.g. while enumerating or 378 * handling suspend-resume sequences. 379 * all clients need to use the pm versions 380 */ 381 382 static int 383 sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val) 384 { 385 struct sdw_msg msg; 386 int ret; 387 388 ret = sdw_fill_msg(&msg, slave, addr, count, 389 slave->dev_num, SDW_MSG_FLAG_READ, val); 390 if (ret < 0) 391 return ret; 392 393 return sdw_transfer(slave->bus, &msg); 394 } 395 396 static int 397 sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val) 398 { 399 struct sdw_msg msg; 400 int ret; 401 402 ret = sdw_fill_msg(&msg, slave, addr, count, 403 slave->dev_num, SDW_MSG_FLAG_WRITE, (u8 *)val); 404 if (ret < 0) 405 return ret; 406 407 return sdw_transfer(slave->bus, &msg); 408 } 409 410 int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value) 411 { 412 return sdw_nwrite_no_pm(slave, addr, 1, &value); 413 } 414 EXPORT_SYMBOL(sdw_write_no_pm); 415 416 static int 417 sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr) 418 { 419 struct sdw_msg msg; 420 u8 buf; 421 int ret; 422 423 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num, 424 SDW_MSG_FLAG_READ, &buf); 425 if (ret < 0) 426 return ret; 427 428 ret = sdw_transfer(bus, &msg); 429 if (ret < 0) 430 return ret; 431 432 return buf; 433 } 434 435 static int 436 sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value) 437 { 438 struct sdw_msg msg; 439 int ret; 440 441 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num, 442 SDW_MSG_FLAG_WRITE, &value); 443 if (ret < 0) 444 return ret; 445 446 return sdw_transfer(bus, &msg); 447 } 448 449 int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr) 450 { 451 struct sdw_msg msg; 452 u8 buf; 453 int ret; 454 455 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num, 456 SDW_MSG_FLAG_READ, &buf); 457 if (ret < 0) 458 return ret; 459 460 ret = sdw_transfer_unlocked(bus, &msg); 461 if (ret < 0) 462 return ret; 463 464 return buf; 465 } 466 EXPORT_SYMBOL(sdw_bread_no_pm_unlocked); 467 468 int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value) 469 { 470 struct sdw_msg msg; 471 int ret; 472 473 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num, 474 SDW_MSG_FLAG_WRITE, &value); 475 if (ret < 0) 476 return ret; 477 478 return sdw_transfer_unlocked(bus, &msg); 479 } 480 EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked); 481 482 int sdw_read_no_pm(struct sdw_slave *slave, u32 addr) 483 { 484 u8 buf; 485 int ret; 486 487 ret = sdw_nread_no_pm(slave, addr, 1, &buf); 488 if (ret < 0) 489 return ret; 490 else 491 return buf; 492 } 493 EXPORT_SYMBOL(sdw_read_no_pm); 494 495 int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val) 496 { 497 int tmp; 498 499 tmp = sdw_read_no_pm(slave, addr); 500 if (tmp < 0) 501 return tmp; 502 503 tmp = (tmp & ~mask) | val; 504 return sdw_write_no_pm(slave, addr, tmp); 505 } 506 EXPORT_SYMBOL(sdw_update_no_pm); 507 508 /* Read-Modify-Write Slave register */ 509 int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val) 510 { 511 int tmp; 512 513 tmp = sdw_read(slave, addr); 514 if (tmp < 0) 515 return tmp; 516 517 tmp = (tmp & ~mask) | val; 518 return sdw_write(slave, addr, tmp); 519 } 520 EXPORT_SYMBOL(sdw_update); 521 522 /** 523 * sdw_nread() - Read "n" contiguous SDW Slave registers 524 * @slave: SDW Slave 525 * @addr: Register address 526 * @count: length 527 * @val: Buffer for values to be read 528 */ 529 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val) 530 { 531 int ret; 532 533 ret = pm_runtime_get_sync(&slave->dev); 534 if (ret < 0 && ret != -EACCES) { 535 pm_runtime_put_noidle(&slave->dev); 536 return ret; 537 } 538 539 ret = sdw_nread_no_pm(slave, addr, count, val); 540 541 pm_runtime_mark_last_busy(&slave->dev); 542 pm_runtime_put(&slave->dev); 543 544 return ret; 545 } 546 EXPORT_SYMBOL(sdw_nread); 547 548 /** 549 * sdw_nwrite() - Write "n" contiguous SDW Slave registers 550 * @slave: SDW Slave 551 * @addr: Register address 552 * @count: length 553 * @val: Buffer for values to be written 554 */ 555 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val) 556 { 557 int ret; 558 559 ret = pm_runtime_get_sync(&slave->dev); 560 if (ret < 0 && ret != -EACCES) { 561 pm_runtime_put_noidle(&slave->dev); 562 return ret; 563 } 564 565 ret = sdw_nwrite_no_pm(slave, addr, count, val); 566 567 pm_runtime_mark_last_busy(&slave->dev); 568 pm_runtime_put(&slave->dev); 569 570 return ret; 571 } 572 EXPORT_SYMBOL(sdw_nwrite); 573 574 /** 575 * sdw_read() - Read a SDW Slave register 576 * @slave: SDW Slave 577 * @addr: Register address 578 */ 579 int sdw_read(struct sdw_slave *slave, u32 addr) 580 { 581 u8 buf; 582 int ret; 583 584 ret = sdw_nread(slave, addr, 1, &buf); 585 if (ret < 0) 586 return ret; 587 588 return buf; 589 } 590 EXPORT_SYMBOL(sdw_read); 591 592 /** 593 * sdw_write() - Write a SDW Slave register 594 * @slave: SDW Slave 595 * @addr: Register address 596 * @value: Register value 597 */ 598 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value) 599 { 600 return sdw_nwrite(slave, addr, 1, &value); 601 } 602 EXPORT_SYMBOL(sdw_write); 603 604 /* 605 * SDW alert handling 606 */ 607 608 /* called with bus_lock held */ 609 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i) 610 { 611 struct sdw_slave *slave; 612 613 list_for_each_entry(slave, &bus->slaves, node) { 614 if (slave->dev_num == i) 615 return slave; 616 } 617 618 return NULL; 619 } 620 621 int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id) 622 { 623 if (slave->id.mfg_id != id.mfg_id || 624 slave->id.part_id != id.part_id || 625 slave->id.class_id != id.class_id || 626 (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID && 627 slave->id.unique_id != id.unique_id)) 628 return -ENODEV; 629 630 return 0; 631 } 632 EXPORT_SYMBOL(sdw_compare_devid); 633 634 /* called with bus_lock held */ 635 static int sdw_get_device_num(struct sdw_slave *slave) 636 { 637 int bit; 638 639 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES); 640 if (bit == SDW_MAX_DEVICES) { 641 bit = -ENODEV; 642 goto err; 643 } 644 645 /* 646 * Do not update dev_num in Slave data structure here, 647 * Update once program dev_num is successful 648 */ 649 set_bit(bit, slave->bus->assigned); 650 651 err: 652 return bit; 653 } 654 655 static int sdw_assign_device_num(struct sdw_slave *slave) 656 { 657 struct sdw_bus *bus = slave->bus; 658 int ret, dev_num; 659 bool new_device = false; 660 661 /* check first if device number is assigned, if so reuse that */ 662 if (!slave->dev_num) { 663 if (!slave->dev_num_sticky) { 664 mutex_lock(&slave->bus->bus_lock); 665 dev_num = sdw_get_device_num(slave); 666 mutex_unlock(&slave->bus->bus_lock); 667 if (dev_num < 0) { 668 dev_err(bus->dev, "Get dev_num failed: %d\n", 669 dev_num); 670 return dev_num; 671 } 672 slave->dev_num = dev_num; 673 slave->dev_num_sticky = dev_num; 674 new_device = true; 675 } else { 676 slave->dev_num = slave->dev_num_sticky; 677 } 678 } 679 680 if (!new_device) 681 dev_dbg(bus->dev, 682 "Slave already registered, reusing dev_num:%d\n", 683 slave->dev_num); 684 685 /* Clear the slave->dev_num to transfer message on device 0 */ 686 dev_num = slave->dev_num; 687 slave->dev_num = 0; 688 689 ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num); 690 if (ret < 0) { 691 dev_err(bus->dev, "Program device_num %d failed: %d\n", 692 dev_num, ret); 693 return ret; 694 } 695 696 /* After xfer of msg, restore dev_num */ 697 slave->dev_num = slave->dev_num_sticky; 698 699 return 0; 700 } 701 702 void sdw_extract_slave_id(struct sdw_bus *bus, 703 u64 addr, struct sdw_slave_id *id) 704 { 705 dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr); 706 707 id->sdw_version = SDW_VERSION(addr); 708 id->unique_id = SDW_UNIQUE_ID(addr); 709 id->mfg_id = SDW_MFG_ID(addr); 710 id->part_id = SDW_PART_ID(addr); 711 id->class_id = SDW_CLASS_ID(addr); 712 713 dev_dbg(bus->dev, 714 "SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n", 715 id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version); 716 } 717 EXPORT_SYMBOL(sdw_extract_slave_id); 718 719 static int sdw_program_device_num(struct sdw_bus *bus) 720 { 721 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0}; 722 struct sdw_slave *slave, *_s; 723 struct sdw_slave_id id; 724 struct sdw_msg msg; 725 bool found; 726 int count = 0, ret; 727 u64 addr; 728 729 /* No Slave, so use raw xfer api */ 730 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0, 731 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf); 732 if (ret < 0) 733 return ret; 734 735 do { 736 ret = sdw_transfer(bus, &msg); 737 if (ret == -ENODATA) { /* end of device id reads */ 738 dev_dbg(bus->dev, "No more devices to enumerate\n"); 739 ret = 0; 740 break; 741 } 742 if (ret < 0) { 743 dev_err(bus->dev, "DEVID read fail:%d\n", ret); 744 break; 745 } 746 747 /* 748 * Construct the addr and extract. Cast the higher shift 749 * bits to avoid truncation due to size limit. 750 */ 751 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) | 752 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) | 753 ((u64)buf[0] << 40); 754 755 sdw_extract_slave_id(bus, addr, &id); 756 757 found = false; 758 /* Now compare with entries */ 759 list_for_each_entry_safe(slave, _s, &bus->slaves, node) { 760 if (sdw_compare_devid(slave, id) == 0) { 761 found = true; 762 763 /* 764 * Assign a new dev_num to this Slave and 765 * not mark it present. It will be marked 766 * present after it reports ATTACHED on new 767 * dev_num 768 */ 769 ret = sdw_assign_device_num(slave); 770 if (ret < 0) { 771 dev_err(bus->dev, 772 "Assign dev_num failed:%d\n", 773 ret); 774 return ret; 775 } 776 777 break; 778 } 779 } 780 781 if (!found) { 782 /* TODO: Park this device in Group 13 */ 783 784 /* 785 * add Slave device even if there is no platform 786 * firmware description. There will be no driver probe 787 * but the user/integration will be able to see the 788 * device, enumeration status and device number in sysfs 789 */ 790 sdw_slave_add(bus, &id, NULL); 791 792 dev_err(bus->dev, "Slave Entry not found\n"); 793 } 794 795 count++; 796 797 /* 798 * Check till error out or retry (count) exhausts. 799 * Device can drop off and rejoin during enumeration 800 * so count till twice the bound. 801 */ 802 803 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2)); 804 805 return ret; 806 } 807 808 static void sdw_modify_slave_status(struct sdw_slave *slave, 809 enum sdw_slave_status status) 810 { 811 struct sdw_bus *bus = slave->bus; 812 813 mutex_lock(&bus->bus_lock); 814 815 dev_vdbg(bus->dev, 816 "%s: changing status slave %d status %d new status %d\n", 817 __func__, slave->dev_num, slave->status, status); 818 819 if (status == SDW_SLAVE_UNATTACHED) { 820 dev_dbg(&slave->dev, 821 "%s: initializing enumeration and init completion for Slave %d\n", 822 __func__, slave->dev_num); 823 824 init_completion(&slave->enumeration_complete); 825 init_completion(&slave->initialization_complete); 826 827 } else if ((status == SDW_SLAVE_ATTACHED) && 828 (slave->status == SDW_SLAVE_UNATTACHED)) { 829 dev_dbg(&slave->dev, 830 "%s: signaling enumeration completion for Slave %d\n", 831 __func__, slave->dev_num); 832 833 complete(&slave->enumeration_complete); 834 } 835 slave->status = status; 836 mutex_unlock(&bus->bus_lock); 837 } 838 839 static int sdw_slave_clk_stop_callback(struct sdw_slave *slave, 840 enum sdw_clk_stop_mode mode, 841 enum sdw_clk_stop_type type) 842 { 843 int ret; 844 845 if (slave->ops && slave->ops->clk_stop) { 846 ret = slave->ops->clk_stop(slave, mode, type); 847 if (ret < 0) 848 return ret; 849 } 850 851 return 0; 852 } 853 854 static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave, 855 enum sdw_clk_stop_mode mode, 856 bool prepare) 857 { 858 bool wake_en; 859 u32 val = 0; 860 int ret; 861 862 wake_en = slave->prop.wake_capable; 863 864 if (prepare) { 865 val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP; 866 867 if (mode == SDW_CLK_STOP_MODE1) 868 val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1; 869 870 if (wake_en) 871 val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN; 872 } else { 873 ret = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL); 874 if (ret < 0) { 875 if (ret != -ENODATA) 876 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL read failed:%d\n", ret); 877 return ret; 878 } 879 val = ret; 880 val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP); 881 } 882 883 ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val); 884 885 if (ret < 0 && ret != -ENODATA) 886 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL write failed:%d\n", ret); 887 888 return ret; 889 } 890 891 static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num) 892 { 893 int retry = bus->clk_stop_timeout; 894 int val; 895 896 do { 897 val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT); 898 if (val < 0) { 899 dev_err(bus->dev, "SDW_SCP_STAT bread failed:%d\n", val); 900 return val; 901 } 902 val &= SDW_SCP_STAT_CLK_STP_NF; 903 if (!val) { 904 dev_dbg(bus->dev, "clock stop prep/de-prep done slave:%d\n", 905 dev_num); 906 return 0; 907 } 908 909 usleep_range(1000, 1500); 910 retry--; 911 } while (retry); 912 913 dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d\n", 914 dev_num); 915 916 return -ETIMEDOUT; 917 } 918 919 /** 920 * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop 921 * 922 * @bus: SDW bus instance 923 * 924 * Query Slave for clock stop mode and prepare for that mode. 925 */ 926 int sdw_bus_prep_clk_stop(struct sdw_bus *bus) 927 { 928 bool simple_clk_stop = true; 929 struct sdw_slave *slave; 930 bool is_slave = false; 931 int ret = 0; 932 933 /* 934 * In order to save on transition time, prepare 935 * each Slave and then wait for all Slave(s) to be 936 * prepared for clock stop. 937 * If one of the Slave devices has lost sync and 938 * replies with Command Ignored/-ENODATA, we continue 939 * the loop 940 */ 941 list_for_each_entry(slave, &bus->slaves, node) { 942 if (!slave->dev_num) 943 continue; 944 945 if (slave->status != SDW_SLAVE_ATTACHED && 946 slave->status != SDW_SLAVE_ALERT) 947 continue; 948 949 /* Identify if Slave(s) are available on Bus */ 950 is_slave = true; 951 952 ret = sdw_slave_clk_stop_callback(slave, 953 SDW_CLK_STOP_MODE0, 954 SDW_CLK_PRE_PREPARE); 955 if (ret < 0 && ret != -ENODATA) { 956 dev_err(&slave->dev, "clock stop pre-prepare cb failed:%d\n", ret); 957 return ret; 958 } 959 960 /* Only prepare a Slave device if needed */ 961 if (!slave->prop.simple_clk_stop_capable) { 962 simple_clk_stop = false; 963 964 ret = sdw_slave_clk_stop_prepare(slave, 965 SDW_CLK_STOP_MODE0, 966 true); 967 if (ret < 0 && ret != -ENODATA) { 968 dev_err(&slave->dev, "clock stop prepare failed:%d\n", ret); 969 return ret; 970 } 971 } 972 } 973 974 /* Skip remaining clock stop preparation if no Slave is attached */ 975 if (!is_slave) 976 return 0; 977 978 /* 979 * Don't wait for all Slaves to be ready if they follow the simple 980 * state machine 981 */ 982 if (!simple_clk_stop) { 983 ret = sdw_bus_wait_for_clk_prep_deprep(bus, 984 SDW_BROADCAST_DEV_NUM); 985 /* 986 * if there are no Slave devices present and the reply is 987 * Command_Ignored/-ENODATA, we don't need to continue with the 988 * flow and can just return here. The error code is not modified 989 * and its handling left as an exercise for the caller. 990 */ 991 if (ret < 0) 992 return ret; 993 } 994 995 /* Inform slaves that prep is done */ 996 list_for_each_entry(slave, &bus->slaves, node) { 997 if (!slave->dev_num) 998 continue; 999 1000 if (slave->status != SDW_SLAVE_ATTACHED && 1001 slave->status != SDW_SLAVE_ALERT) 1002 continue; 1003 1004 ret = sdw_slave_clk_stop_callback(slave, 1005 SDW_CLK_STOP_MODE0, 1006 SDW_CLK_POST_PREPARE); 1007 1008 if (ret < 0 && ret != -ENODATA) { 1009 dev_err(&slave->dev, "clock stop post-prepare cb failed:%d\n", ret); 1010 return ret; 1011 } 1012 } 1013 1014 return 0; 1015 } 1016 EXPORT_SYMBOL(sdw_bus_prep_clk_stop); 1017 1018 /** 1019 * sdw_bus_clk_stop: stop bus clock 1020 * 1021 * @bus: SDW bus instance 1022 * 1023 * After preparing the Slaves for clock stop, stop the clock by broadcasting 1024 * write to SCP_CTRL register. 1025 */ 1026 int sdw_bus_clk_stop(struct sdw_bus *bus) 1027 { 1028 int ret; 1029 1030 /* 1031 * broadcast clock stop now, attached Slaves will ACK this, 1032 * unattached will ignore 1033 */ 1034 ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM, 1035 SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW); 1036 if (ret < 0) { 1037 if (ret != -ENODATA) 1038 dev_err(bus->dev, "ClockStopNow Broadcast msg failed %d\n", ret); 1039 return ret; 1040 } 1041 1042 return 0; 1043 } 1044 EXPORT_SYMBOL(sdw_bus_clk_stop); 1045 1046 /** 1047 * sdw_bus_exit_clk_stop: Exit clock stop mode 1048 * 1049 * @bus: SDW bus instance 1050 * 1051 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves 1052 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate 1053 * back. 1054 */ 1055 int sdw_bus_exit_clk_stop(struct sdw_bus *bus) 1056 { 1057 bool simple_clk_stop = true; 1058 struct sdw_slave *slave; 1059 bool is_slave = false; 1060 int ret; 1061 1062 /* 1063 * In order to save on transition time, de-prepare 1064 * each Slave and then wait for all Slave(s) to be 1065 * de-prepared after clock resume. 1066 */ 1067 list_for_each_entry(slave, &bus->slaves, node) { 1068 if (!slave->dev_num) 1069 continue; 1070 1071 if (slave->status != SDW_SLAVE_ATTACHED && 1072 slave->status != SDW_SLAVE_ALERT) 1073 continue; 1074 1075 /* Identify if Slave(s) are available on Bus */ 1076 is_slave = true; 1077 1078 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0, 1079 SDW_CLK_PRE_DEPREPARE); 1080 if (ret < 0) 1081 dev_warn(&slave->dev, "clock stop pre-deprepare cb failed:%d\n", ret); 1082 1083 /* Only de-prepare a Slave device if needed */ 1084 if (!slave->prop.simple_clk_stop_capable) { 1085 simple_clk_stop = false; 1086 1087 ret = sdw_slave_clk_stop_prepare(slave, SDW_CLK_STOP_MODE0, 1088 false); 1089 1090 if (ret < 0) 1091 dev_warn(&slave->dev, "clock stop deprepare failed:%d\n", ret); 1092 } 1093 } 1094 1095 /* Skip remaining clock stop de-preparation if no Slave is attached */ 1096 if (!is_slave) 1097 return 0; 1098 1099 /* 1100 * Don't wait for all Slaves to be ready if they follow the simple 1101 * state machine 1102 */ 1103 if (!simple_clk_stop) { 1104 ret = sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM); 1105 if (ret < 0) 1106 dev_warn(&slave->dev, "clock stop deprepare wait failed:%d\n", ret); 1107 } 1108 1109 list_for_each_entry(slave, &bus->slaves, node) { 1110 if (!slave->dev_num) 1111 continue; 1112 1113 if (slave->status != SDW_SLAVE_ATTACHED && 1114 slave->status != SDW_SLAVE_ALERT) 1115 continue; 1116 1117 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0, 1118 SDW_CLK_POST_DEPREPARE); 1119 if (ret < 0) 1120 dev_warn(&slave->dev, "clock stop post-deprepare cb failed:%d\n", ret); 1121 } 1122 1123 return 0; 1124 } 1125 EXPORT_SYMBOL(sdw_bus_exit_clk_stop); 1126 1127 int sdw_configure_dpn_intr(struct sdw_slave *slave, 1128 int port, bool enable, int mask) 1129 { 1130 u32 addr; 1131 int ret; 1132 u8 val = 0; 1133 1134 if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) { 1135 dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n", 1136 enable ? "on" : "off"); 1137 mask |= SDW_DPN_INT_TEST_FAIL; 1138 } 1139 1140 addr = SDW_DPN_INTMASK(port); 1141 1142 /* Set/Clear port ready interrupt mask */ 1143 if (enable) { 1144 val |= mask; 1145 val |= SDW_DPN_INT_PORT_READY; 1146 } else { 1147 val &= ~(mask); 1148 val &= ~SDW_DPN_INT_PORT_READY; 1149 } 1150 1151 ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val); 1152 if (ret < 0) 1153 dev_err(&slave->dev, 1154 "SDW_DPN_INTMASK write failed:%d\n", val); 1155 1156 return ret; 1157 } 1158 1159 static int sdw_slave_set_frequency(struct sdw_slave *slave) 1160 { 1161 u32 mclk_freq = slave->bus->prop.mclk_freq; 1162 u32 curr_freq = slave->bus->params.curr_dr_freq >> 1; 1163 unsigned int scale; 1164 u8 scale_index; 1165 u8 base; 1166 int ret; 1167 1168 /* 1169 * frequency base and scale registers are required for SDCA 1170 * devices. They may also be used for 1.2+/non-SDCA devices, 1171 * but we will need a DisCo property to cover this case 1172 */ 1173 if (!slave->id.class_id) 1174 return 0; 1175 1176 if (!mclk_freq) { 1177 dev_err(&slave->dev, 1178 "no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n"); 1179 return -EINVAL; 1180 } 1181 1182 /* 1183 * map base frequency using Table 89 of SoundWire 1.2 spec. 1184 * The order of the tests just follows the specification, this 1185 * is not a selection between possible values or a search for 1186 * the best value but just a mapping. Only one case per platform 1187 * is relevant. 1188 * Some BIOS have inconsistent values for mclk_freq but a 1189 * correct root so we force the mclk_freq to avoid variations. 1190 */ 1191 if (!(19200000 % mclk_freq)) { 1192 mclk_freq = 19200000; 1193 base = SDW_SCP_BASE_CLOCK_19200000_HZ; 1194 } else if (!(24000000 % mclk_freq)) { 1195 mclk_freq = 24000000; 1196 base = SDW_SCP_BASE_CLOCK_24000000_HZ; 1197 } else if (!(24576000 % mclk_freq)) { 1198 mclk_freq = 24576000; 1199 base = SDW_SCP_BASE_CLOCK_24576000_HZ; 1200 } else if (!(22579200 % mclk_freq)) { 1201 mclk_freq = 22579200; 1202 base = SDW_SCP_BASE_CLOCK_22579200_HZ; 1203 } else if (!(32000000 % mclk_freq)) { 1204 mclk_freq = 32000000; 1205 base = SDW_SCP_BASE_CLOCK_32000000_HZ; 1206 } else { 1207 dev_err(&slave->dev, 1208 "Unsupported clock base, mclk %d\n", 1209 mclk_freq); 1210 return -EINVAL; 1211 } 1212 1213 if (mclk_freq % curr_freq) { 1214 dev_err(&slave->dev, 1215 "mclk %d is not multiple of bus curr_freq %d\n", 1216 mclk_freq, curr_freq); 1217 return -EINVAL; 1218 } 1219 1220 scale = mclk_freq / curr_freq; 1221 1222 /* 1223 * map scale to Table 90 of SoundWire 1.2 spec - and check 1224 * that the scale is a power of two and maximum 64 1225 */ 1226 scale_index = ilog2(scale); 1227 1228 if (BIT(scale_index) != scale || scale_index > 6) { 1229 dev_err(&slave->dev, 1230 "No match found for scale %d, bus mclk %d curr_freq %d\n", 1231 scale, mclk_freq, curr_freq); 1232 return -EINVAL; 1233 } 1234 scale_index++; 1235 1236 ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base); 1237 if (ret < 0) { 1238 dev_err(&slave->dev, 1239 "SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret); 1240 return ret; 1241 } 1242 1243 /* initialize scale for both banks */ 1244 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index); 1245 if (ret < 0) { 1246 dev_err(&slave->dev, 1247 "SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret); 1248 return ret; 1249 } 1250 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index); 1251 if (ret < 0) 1252 dev_err(&slave->dev, 1253 "SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret); 1254 1255 dev_dbg(&slave->dev, 1256 "Configured bus base %d, scale %d, mclk %d, curr_freq %d\n", 1257 base, scale_index, mclk_freq, curr_freq); 1258 1259 return ret; 1260 } 1261 1262 static int sdw_initialize_slave(struct sdw_slave *slave) 1263 { 1264 struct sdw_slave_prop *prop = &slave->prop; 1265 int status; 1266 int ret; 1267 u8 val; 1268 1269 ret = sdw_slave_set_frequency(slave); 1270 if (ret < 0) 1271 return ret; 1272 1273 if (slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH) { 1274 /* Clear bus clash interrupt before enabling interrupt mask */ 1275 status = sdw_read_no_pm(slave, SDW_SCP_INT1); 1276 if (status < 0) { 1277 dev_err(&slave->dev, 1278 "SDW_SCP_INT1 (BUS_CLASH) read failed:%d\n", status); 1279 return status; 1280 } 1281 if (status & SDW_SCP_INT1_BUS_CLASH) { 1282 dev_warn(&slave->dev, "Bus clash detected before INT mask is enabled\n"); 1283 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_BUS_CLASH); 1284 if (ret < 0) { 1285 dev_err(&slave->dev, 1286 "SDW_SCP_INT1 (BUS_CLASH) write failed:%d\n", ret); 1287 return ret; 1288 } 1289 } 1290 } 1291 if ((slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY) && 1292 !(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY)) { 1293 /* Clear parity interrupt before enabling interrupt mask */ 1294 status = sdw_read_no_pm(slave, SDW_SCP_INT1); 1295 if (status < 0) { 1296 dev_err(&slave->dev, 1297 "SDW_SCP_INT1 (PARITY) read failed:%d\n", status); 1298 return status; 1299 } 1300 if (status & SDW_SCP_INT1_PARITY) { 1301 dev_warn(&slave->dev, "PARITY error detected before INT mask is enabled\n"); 1302 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_PARITY); 1303 if (ret < 0) { 1304 dev_err(&slave->dev, 1305 "SDW_SCP_INT1 (PARITY) write failed:%d\n", ret); 1306 return ret; 1307 } 1308 } 1309 } 1310 1311 /* 1312 * Set SCP_INT1_MASK register, typically bus clash and 1313 * implementation-defined interrupt mask. The Parity detection 1314 * may not always be correct on startup so its use is 1315 * device-dependent, it might e.g. only be enabled in 1316 * steady-state after a couple of frames. 1317 */ 1318 val = slave->prop.scp_int1_mask; 1319 1320 /* Enable SCP interrupts */ 1321 ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val); 1322 if (ret < 0) { 1323 dev_err(&slave->dev, 1324 "SDW_SCP_INTMASK1 write failed:%d\n", ret); 1325 return ret; 1326 } 1327 1328 /* No need to continue if DP0 is not present */ 1329 if (!slave->prop.dp0_prop) 1330 return 0; 1331 1332 /* Enable DP0 interrupts */ 1333 val = prop->dp0_prop->imp_def_interrupts; 1334 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE; 1335 1336 ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val); 1337 if (ret < 0) 1338 dev_err(&slave->dev, 1339 "SDW_DP0_INTMASK read failed:%d\n", ret); 1340 return ret; 1341 } 1342 1343 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status) 1344 { 1345 u8 clear, impl_int_mask; 1346 int status, status2, ret, count = 0; 1347 1348 status = sdw_read_no_pm(slave, SDW_DP0_INT); 1349 if (status < 0) { 1350 dev_err(&slave->dev, 1351 "SDW_DP0_INT read failed:%d\n", status); 1352 return status; 1353 } 1354 1355 do { 1356 clear = status & ~SDW_DP0_INTERRUPTS; 1357 1358 if (status & SDW_DP0_INT_TEST_FAIL) { 1359 dev_err(&slave->dev, "Test fail for port 0\n"); 1360 clear |= SDW_DP0_INT_TEST_FAIL; 1361 } 1362 1363 /* 1364 * Assumption: PORT_READY interrupt will be received only for 1365 * ports implementing Channel Prepare state machine (CP_SM) 1366 */ 1367 1368 if (status & SDW_DP0_INT_PORT_READY) { 1369 complete(&slave->port_ready[0]); 1370 clear |= SDW_DP0_INT_PORT_READY; 1371 } 1372 1373 if (status & SDW_DP0_INT_BRA_FAILURE) { 1374 dev_err(&slave->dev, "BRA failed\n"); 1375 clear |= SDW_DP0_INT_BRA_FAILURE; 1376 } 1377 1378 impl_int_mask = SDW_DP0_INT_IMPDEF1 | 1379 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3; 1380 1381 if (status & impl_int_mask) { 1382 clear |= impl_int_mask; 1383 *slave_status = clear; 1384 } 1385 1386 /* clear the interrupts but don't touch reserved and SDCA_CASCADE fields */ 1387 ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear); 1388 if (ret < 0) { 1389 dev_err(&slave->dev, 1390 "SDW_DP0_INT write failed:%d\n", ret); 1391 return ret; 1392 } 1393 1394 /* Read DP0 interrupt again */ 1395 status2 = sdw_read_no_pm(slave, SDW_DP0_INT); 1396 if (status2 < 0) { 1397 dev_err(&slave->dev, 1398 "SDW_DP0_INT read failed:%d\n", status2); 1399 return status2; 1400 } 1401 /* filter to limit loop to interrupts identified in the first status read */ 1402 status &= status2; 1403 1404 count++; 1405 1406 /* we can get alerts while processing so keep retrying */ 1407 } while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY)); 1408 1409 if (count == SDW_READ_INTR_CLEAR_RETRY) 1410 dev_warn(&slave->dev, "Reached MAX_RETRY on DP0 read\n"); 1411 1412 return ret; 1413 } 1414 1415 static int sdw_handle_port_interrupt(struct sdw_slave *slave, 1416 int port, u8 *slave_status) 1417 { 1418 u8 clear, impl_int_mask; 1419 int status, status2, ret, count = 0; 1420 u32 addr; 1421 1422 if (port == 0) 1423 return sdw_handle_dp0_interrupt(slave, slave_status); 1424 1425 addr = SDW_DPN_INT(port); 1426 status = sdw_read_no_pm(slave, addr); 1427 if (status < 0) { 1428 dev_err(&slave->dev, 1429 "SDW_DPN_INT read failed:%d\n", status); 1430 1431 return status; 1432 } 1433 1434 do { 1435 clear = status & ~SDW_DPN_INTERRUPTS; 1436 1437 if (status & SDW_DPN_INT_TEST_FAIL) { 1438 dev_err(&slave->dev, "Test fail for port:%d\n", port); 1439 clear |= SDW_DPN_INT_TEST_FAIL; 1440 } 1441 1442 /* 1443 * Assumption: PORT_READY interrupt will be received only 1444 * for ports implementing CP_SM. 1445 */ 1446 if (status & SDW_DPN_INT_PORT_READY) { 1447 complete(&slave->port_ready[port]); 1448 clear |= SDW_DPN_INT_PORT_READY; 1449 } 1450 1451 impl_int_mask = SDW_DPN_INT_IMPDEF1 | 1452 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3; 1453 1454 if (status & impl_int_mask) { 1455 clear |= impl_int_mask; 1456 *slave_status = clear; 1457 } 1458 1459 /* clear the interrupt but don't touch reserved fields */ 1460 ret = sdw_write_no_pm(slave, addr, clear); 1461 if (ret < 0) { 1462 dev_err(&slave->dev, 1463 "SDW_DPN_INT write failed:%d\n", ret); 1464 return ret; 1465 } 1466 1467 /* Read DPN interrupt again */ 1468 status2 = sdw_read_no_pm(slave, addr); 1469 if (status2 < 0) { 1470 dev_err(&slave->dev, 1471 "SDW_DPN_INT read failed:%d\n", status2); 1472 return status2; 1473 } 1474 /* filter to limit loop to interrupts identified in the first status read */ 1475 status &= status2; 1476 1477 count++; 1478 1479 /* we can get alerts while processing so keep retrying */ 1480 } while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY)); 1481 1482 if (count == SDW_READ_INTR_CLEAR_RETRY) 1483 dev_warn(&slave->dev, "Reached MAX_RETRY on port read"); 1484 1485 return ret; 1486 } 1487 1488 static int sdw_handle_slave_alerts(struct sdw_slave *slave) 1489 { 1490 struct sdw_slave_intr_status slave_intr; 1491 u8 clear = 0, bit, port_status[15] = {0}; 1492 int port_num, stat, ret, count = 0; 1493 unsigned long port; 1494 bool slave_notify; 1495 u8 sdca_cascade = 0; 1496 u8 buf, buf2[2], _buf, _buf2[2]; 1497 bool parity_check; 1498 bool parity_quirk; 1499 1500 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT); 1501 1502 ret = pm_runtime_get_sync(&slave->dev); 1503 if (ret < 0 && ret != -EACCES) { 1504 dev_err(&slave->dev, "Failed to resume device: %d\n", ret); 1505 pm_runtime_put_noidle(&slave->dev); 1506 return ret; 1507 } 1508 1509 /* Read Intstat 1, Intstat 2 and Intstat 3 registers */ 1510 ret = sdw_read_no_pm(slave, SDW_SCP_INT1); 1511 if (ret < 0) { 1512 dev_err(&slave->dev, 1513 "SDW_SCP_INT1 read failed:%d\n", ret); 1514 goto io_err; 1515 } 1516 buf = ret; 1517 1518 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2); 1519 if (ret < 0) { 1520 dev_err(&slave->dev, 1521 "SDW_SCP_INT2/3 read failed:%d\n", ret); 1522 goto io_err; 1523 } 1524 1525 if (slave->prop.is_sdca) { 1526 ret = sdw_read_no_pm(slave, SDW_DP0_INT); 1527 if (ret < 0) { 1528 dev_err(&slave->dev, 1529 "SDW_DP0_INT read failed:%d\n", ret); 1530 goto io_err; 1531 } 1532 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE; 1533 } 1534 1535 do { 1536 slave_notify = false; 1537 1538 /* 1539 * Check parity, bus clash and Slave (impl defined) 1540 * interrupt 1541 */ 1542 if (buf & SDW_SCP_INT1_PARITY) { 1543 parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY; 1544 parity_quirk = !slave->first_interrupt_done && 1545 (slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY); 1546 1547 if (parity_check && !parity_quirk) 1548 dev_err(&slave->dev, "Parity error detected\n"); 1549 clear |= SDW_SCP_INT1_PARITY; 1550 } 1551 1552 if (buf & SDW_SCP_INT1_BUS_CLASH) { 1553 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH) 1554 dev_err(&slave->dev, "Bus clash detected\n"); 1555 clear |= SDW_SCP_INT1_BUS_CLASH; 1556 } 1557 1558 /* 1559 * When bus clash or parity errors are detected, such errors 1560 * are unlikely to be recoverable errors. 1561 * TODO: In such scenario, reset bus. Make this configurable 1562 * via sysfs property with bus reset being the default. 1563 */ 1564 1565 if (buf & SDW_SCP_INT1_IMPL_DEF) { 1566 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) { 1567 dev_dbg(&slave->dev, "Slave impl defined interrupt\n"); 1568 slave_notify = true; 1569 } 1570 clear |= SDW_SCP_INT1_IMPL_DEF; 1571 } 1572 1573 /* the SDCA interrupts are cleared in the codec driver .interrupt_callback() */ 1574 if (sdca_cascade) 1575 slave_notify = true; 1576 1577 /* Check port 0 - 3 interrupts */ 1578 port = buf & SDW_SCP_INT1_PORT0_3; 1579 1580 /* To get port number corresponding to bits, shift it */ 1581 port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port); 1582 for_each_set_bit(bit, &port, 8) { 1583 sdw_handle_port_interrupt(slave, bit, 1584 &port_status[bit]); 1585 } 1586 1587 /* Check if cascade 2 interrupt is present */ 1588 if (buf & SDW_SCP_INT1_SCP2_CASCADE) { 1589 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10; 1590 for_each_set_bit(bit, &port, 8) { 1591 /* scp2 ports start from 4 */ 1592 port_num = bit + 3; 1593 sdw_handle_port_interrupt(slave, 1594 port_num, 1595 &port_status[port_num]); 1596 } 1597 } 1598 1599 /* now check last cascade */ 1600 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) { 1601 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14; 1602 for_each_set_bit(bit, &port, 8) { 1603 /* scp3 ports start from 11 */ 1604 port_num = bit + 10; 1605 sdw_handle_port_interrupt(slave, 1606 port_num, 1607 &port_status[port_num]); 1608 } 1609 } 1610 1611 /* Update the Slave driver */ 1612 if (slave_notify && slave->ops && 1613 slave->ops->interrupt_callback) { 1614 slave_intr.sdca_cascade = sdca_cascade; 1615 slave_intr.control_port = clear; 1616 memcpy(slave_intr.port, &port_status, 1617 sizeof(slave_intr.port)); 1618 1619 slave->ops->interrupt_callback(slave, &slave_intr); 1620 } 1621 1622 /* Ack interrupt */ 1623 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear); 1624 if (ret < 0) { 1625 dev_err(&slave->dev, 1626 "SDW_SCP_INT1 write failed:%d\n", ret); 1627 goto io_err; 1628 } 1629 1630 /* at this point all initial interrupt sources were handled */ 1631 slave->first_interrupt_done = true; 1632 1633 /* 1634 * Read status again to ensure no new interrupts arrived 1635 * while servicing interrupts. 1636 */ 1637 ret = sdw_read_no_pm(slave, SDW_SCP_INT1); 1638 if (ret < 0) { 1639 dev_err(&slave->dev, 1640 "SDW_SCP_INT1 recheck read failed:%d\n", ret); 1641 goto io_err; 1642 } 1643 _buf = ret; 1644 1645 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, _buf2); 1646 if (ret < 0) { 1647 dev_err(&slave->dev, 1648 "SDW_SCP_INT2/3 recheck read failed:%d\n", ret); 1649 goto io_err; 1650 } 1651 1652 if (slave->prop.is_sdca) { 1653 ret = sdw_read_no_pm(slave, SDW_DP0_INT); 1654 if (ret < 0) { 1655 dev_err(&slave->dev, 1656 "SDW_DP0_INT recheck read failed:%d\n", ret); 1657 goto io_err; 1658 } 1659 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE; 1660 } 1661 1662 /* 1663 * Make sure no interrupts are pending, but filter to limit loop 1664 * to interrupts identified in the first status read 1665 */ 1666 buf &= _buf; 1667 buf2[0] &= _buf2[0]; 1668 buf2[1] &= _buf2[1]; 1669 stat = buf || buf2[0] || buf2[1] || sdca_cascade; 1670 1671 /* 1672 * Exit loop if Slave is continuously in ALERT state even 1673 * after servicing the interrupt multiple times. 1674 */ 1675 count++; 1676 1677 /* we can get alerts while processing so keep retrying */ 1678 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY); 1679 1680 if (count == SDW_READ_INTR_CLEAR_RETRY) 1681 dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n"); 1682 1683 io_err: 1684 pm_runtime_mark_last_busy(&slave->dev); 1685 pm_runtime_put_autosuspend(&slave->dev); 1686 1687 return ret; 1688 } 1689 1690 static int sdw_update_slave_status(struct sdw_slave *slave, 1691 enum sdw_slave_status status) 1692 { 1693 unsigned long time; 1694 1695 if (!slave->probed) { 1696 /* 1697 * the slave status update is typically handled in an 1698 * interrupt thread, which can race with the driver 1699 * probe, e.g. when a module needs to be loaded. 1700 * 1701 * make sure the probe is complete before updating 1702 * status. 1703 */ 1704 time = wait_for_completion_timeout(&slave->probe_complete, 1705 msecs_to_jiffies(DEFAULT_PROBE_TIMEOUT)); 1706 if (!time) { 1707 dev_err(&slave->dev, "Probe not complete, timed out\n"); 1708 return -ETIMEDOUT; 1709 } 1710 } 1711 1712 if (!slave->ops || !slave->ops->update_status) 1713 return 0; 1714 1715 return slave->ops->update_status(slave, status); 1716 } 1717 1718 /** 1719 * sdw_handle_slave_status() - Handle Slave status 1720 * @bus: SDW bus instance 1721 * @status: Status for all Slave(s) 1722 */ 1723 int sdw_handle_slave_status(struct sdw_bus *bus, 1724 enum sdw_slave_status status[]) 1725 { 1726 enum sdw_slave_status prev_status; 1727 struct sdw_slave *slave; 1728 bool attached_initializing; 1729 int i, ret = 0; 1730 1731 /* first check if any Slaves fell off the bus */ 1732 for (i = 1; i <= SDW_MAX_DEVICES; i++) { 1733 mutex_lock(&bus->bus_lock); 1734 if (test_bit(i, bus->assigned) == false) { 1735 mutex_unlock(&bus->bus_lock); 1736 continue; 1737 } 1738 mutex_unlock(&bus->bus_lock); 1739 1740 slave = sdw_get_slave(bus, i); 1741 if (!slave) 1742 continue; 1743 1744 if (status[i] == SDW_SLAVE_UNATTACHED && 1745 slave->status != SDW_SLAVE_UNATTACHED) 1746 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED); 1747 } 1748 1749 if (status[0] == SDW_SLAVE_ATTACHED) { 1750 dev_dbg(bus->dev, "Slave attached, programming device number\n"); 1751 ret = sdw_program_device_num(bus); 1752 if (ret < 0) 1753 dev_err(bus->dev, "Slave attach failed: %d\n", ret); 1754 /* 1755 * programming a device number will have side effects, 1756 * so we deal with other devices at a later time 1757 */ 1758 return ret; 1759 } 1760 1761 /* Continue to check other slave statuses */ 1762 for (i = 1; i <= SDW_MAX_DEVICES; i++) { 1763 mutex_lock(&bus->bus_lock); 1764 if (test_bit(i, bus->assigned) == false) { 1765 mutex_unlock(&bus->bus_lock); 1766 continue; 1767 } 1768 mutex_unlock(&bus->bus_lock); 1769 1770 slave = sdw_get_slave(bus, i); 1771 if (!slave) 1772 continue; 1773 1774 attached_initializing = false; 1775 1776 switch (status[i]) { 1777 case SDW_SLAVE_UNATTACHED: 1778 if (slave->status == SDW_SLAVE_UNATTACHED) 1779 break; 1780 1781 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED); 1782 break; 1783 1784 case SDW_SLAVE_ALERT: 1785 ret = sdw_handle_slave_alerts(slave); 1786 if (ret < 0) 1787 dev_err(&slave->dev, 1788 "Slave %d alert handling failed: %d\n", 1789 i, ret); 1790 break; 1791 1792 case SDW_SLAVE_ATTACHED: 1793 if (slave->status == SDW_SLAVE_ATTACHED) 1794 break; 1795 1796 prev_status = slave->status; 1797 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED); 1798 1799 if (prev_status == SDW_SLAVE_ALERT) 1800 break; 1801 1802 attached_initializing = true; 1803 1804 ret = sdw_initialize_slave(slave); 1805 if (ret < 0) 1806 dev_err(&slave->dev, 1807 "Slave %d initialization failed: %d\n", 1808 i, ret); 1809 1810 break; 1811 1812 default: 1813 dev_err(&slave->dev, "Invalid slave %d status:%d\n", 1814 i, status[i]); 1815 break; 1816 } 1817 1818 ret = sdw_update_slave_status(slave, status[i]); 1819 if (ret < 0) 1820 dev_err(&slave->dev, 1821 "Update Slave status failed:%d\n", ret); 1822 if (attached_initializing) { 1823 dev_dbg(&slave->dev, 1824 "%s: signaling initialization completion for Slave %d\n", 1825 __func__, slave->dev_num); 1826 1827 complete(&slave->initialization_complete); 1828 } 1829 } 1830 1831 return ret; 1832 } 1833 EXPORT_SYMBOL(sdw_handle_slave_status); 1834 1835 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request) 1836 { 1837 struct sdw_slave *slave; 1838 int i; 1839 1840 /* Check all non-zero devices */ 1841 for (i = 1; i <= SDW_MAX_DEVICES; i++) { 1842 mutex_lock(&bus->bus_lock); 1843 if (test_bit(i, bus->assigned) == false) { 1844 mutex_unlock(&bus->bus_lock); 1845 continue; 1846 } 1847 mutex_unlock(&bus->bus_lock); 1848 1849 slave = sdw_get_slave(bus, i); 1850 if (!slave) 1851 continue; 1852 1853 if (slave->status != SDW_SLAVE_UNATTACHED) { 1854 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED); 1855 slave->first_interrupt_done = false; 1856 } 1857 1858 /* keep track of request, used in pm_runtime resume */ 1859 slave->unattach_request = request; 1860 } 1861 } 1862 EXPORT_SYMBOL(sdw_clear_slave_status); 1863