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