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 /* Identify if Slave(s) are available on Bus */ 867 is_slave = true; 868 869 if (slave->status != SDW_SLAVE_ATTACHED && 870 slave->status != SDW_SLAVE_ALERT) 871 continue; 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 /* Inform slaves that prep is done */ 904 list_for_each_entry(slave, &bus->slaves, node) { 905 if (!slave->dev_num) 906 continue; 907 908 if (slave->status != SDW_SLAVE_ATTACHED && 909 slave->status != SDW_SLAVE_ALERT) 910 continue; 911 912 slave_mode = slave->curr_clk_stop_mode; 913 914 if (slave_mode == SDW_CLK_STOP_MODE1) { 915 ret = sdw_slave_clk_stop_callback(slave, 916 slave_mode, 917 SDW_CLK_POST_PREPARE); 918 919 if (ret < 0) { 920 dev_err(&slave->dev, 921 "post-prepare failed:%d", ret); 922 } 923 } 924 } 925 926 return ret; 927 } 928 EXPORT_SYMBOL(sdw_bus_prep_clk_stop); 929 930 /** 931 * sdw_bus_clk_stop: stop bus clock 932 * 933 * @bus: SDW bus instance 934 * 935 * After preparing the Slaves for clock stop, stop the clock by broadcasting 936 * write to SCP_CTRL register. 937 */ 938 int sdw_bus_clk_stop(struct sdw_bus *bus) 939 { 940 int ret; 941 942 /* 943 * broadcast clock stop now, attached Slaves will ACK this, 944 * unattached will ignore 945 */ 946 ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM, 947 SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW); 948 if (ret < 0) { 949 if (ret == -ENODATA) 950 dev_dbg(bus->dev, 951 "ClockStopNow Broadcast msg ignored %d", ret); 952 else 953 dev_err(bus->dev, 954 "ClockStopNow Broadcast msg failed %d", ret); 955 return ret; 956 } 957 958 return 0; 959 } 960 EXPORT_SYMBOL(sdw_bus_clk_stop); 961 962 /** 963 * sdw_bus_exit_clk_stop: Exit clock stop mode 964 * 965 * @bus: SDW bus instance 966 * 967 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves 968 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate 969 * back. 970 */ 971 int sdw_bus_exit_clk_stop(struct sdw_bus *bus) 972 { 973 enum sdw_clk_stop_mode mode; 974 bool simple_clk_stop = true; 975 struct sdw_slave *slave; 976 bool is_slave = false; 977 int ret; 978 979 /* 980 * In order to save on transition time, de-prepare 981 * each Slave and then wait for all Slave(s) to be 982 * de-prepared after clock resume. 983 */ 984 list_for_each_entry(slave, &bus->slaves, node) { 985 if (!slave->dev_num) 986 continue; 987 988 /* Identify if Slave(s) are available on Bus */ 989 is_slave = true; 990 991 if (slave->status != SDW_SLAVE_ATTACHED && 992 slave->status != SDW_SLAVE_ALERT) 993 continue; 994 995 mode = slave->curr_clk_stop_mode; 996 997 if (mode == SDW_CLK_STOP_MODE1) { 998 simple_clk_stop = false; 999 continue; 1000 } 1001 1002 ret = sdw_slave_clk_stop_callback(slave, mode, 1003 SDW_CLK_PRE_DEPREPARE); 1004 if (ret < 0) 1005 dev_warn(&slave->dev, 1006 "clk stop deprep failed:%d", ret); 1007 1008 ret = sdw_slave_clk_stop_prepare(slave, mode, 1009 false); 1010 1011 if (ret < 0) 1012 dev_warn(&slave->dev, 1013 "clk stop deprep failed:%d", ret); 1014 } 1015 1016 if (is_slave && !simple_clk_stop) 1017 sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM); 1018 1019 list_for_each_entry(slave, &bus->slaves, node) { 1020 if (!slave->dev_num) 1021 continue; 1022 1023 if (slave->status != SDW_SLAVE_ATTACHED && 1024 slave->status != SDW_SLAVE_ALERT) 1025 continue; 1026 1027 mode = slave->curr_clk_stop_mode; 1028 sdw_slave_clk_stop_callback(slave, mode, 1029 SDW_CLK_POST_DEPREPARE); 1030 } 1031 1032 return 0; 1033 } 1034 EXPORT_SYMBOL(sdw_bus_exit_clk_stop); 1035 1036 int sdw_configure_dpn_intr(struct sdw_slave *slave, 1037 int port, bool enable, int mask) 1038 { 1039 u32 addr; 1040 int ret; 1041 u8 val = 0; 1042 1043 addr = SDW_DPN_INTMASK(port); 1044 1045 /* Set/Clear port ready interrupt mask */ 1046 if (enable) { 1047 val |= mask; 1048 val |= SDW_DPN_INT_PORT_READY; 1049 } else { 1050 val &= ~(mask); 1051 val &= ~SDW_DPN_INT_PORT_READY; 1052 } 1053 1054 ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val); 1055 if (ret < 0) 1056 dev_err(slave->bus->dev, 1057 "SDW_DPN_INTMASK write failed:%d\n", val); 1058 1059 return ret; 1060 } 1061 1062 static int sdw_initialize_slave(struct sdw_slave *slave) 1063 { 1064 struct sdw_slave_prop *prop = &slave->prop; 1065 int ret; 1066 u8 val; 1067 1068 /* 1069 * Set bus clash, parity and SCP implementation 1070 * defined interrupt mask 1071 * TODO: Read implementation defined interrupt mask 1072 * from Slave property 1073 */ 1074 val = SDW_SCP_INT1_IMPL_DEF | SDW_SCP_INT1_BUS_CLASH | 1075 SDW_SCP_INT1_PARITY; 1076 1077 /* Enable SCP interrupts */ 1078 ret = sdw_update(slave, SDW_SCP_INTMASK1, val, val); 1079 if (ret < 0) { 1080 dev_err(slave->bus->dev, 1081 "SDW_SCP_INTMASK1 write failed:%d\n", ret); 1082 return ret; 1083 } 1084 1085 /* No need to continue if DP0 is not present */ 1086 if (!slave->prop.dp0_prop) 1087 return 0; 1088 1089 /* Enable DP0 interrupts */ 1090 val = prop->dp0_prop->imp_def_interrupts; 1091 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE; 1092 1093 ret = sdw_update(slave, SDW_DP0_INTMASK, val, val); 1094 if (ret < 0) 1095 dev_err(slave->bus->dev, 1096 "SDW_DP0_INTMASK read failed:%d\n", ret); 1097 return ret; 1098 } 1099 1100 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status) 1101 { 1102 u8 clear = 0, impl_int_mask; 1103 int status, status2, ret, count = 0; 1104 1105 status = sdw_read(slave, SDW_DP0_INT); 1106 if (status < 0) { 1107 dev_err(slave->bus->dev, 1108 "SDW_DP0_INT read failed:%d\n", status); 1109 return status; 1110 } 1111 1112 do { 1113 if (status & SDW_DP0_INT_TEST_FAIL) { 1114 dev_err(&slave->dev, "Test fail for port 0\n"); 1115 clear |= SDW_DP0_INT_TEST_FAIL; 1116 } 1117 1118 /* 1119 * Assumption: PORT_READY interrupt will be received only for 1120 * ports implementing Channel Prepare state machine (CP_SM) 1121 */ 1122 1123 if (status & SDW_DP0_INT_PORT_READY) { 1124 complete(&slave->port_ready[0]); 1125 clear |= SDW_DP0_INT_PORT_READY; 1126 } 1127 1128 if (status & SDW_DP0_INT_BRA_FAILURE) { 1129 dev_err(&slave->dev, "BRA failed\n"); 1130 clear |= SDW_DP0_INT_BRA_FAILURE; 1131 } 1132 1133 impl_int_mask = SDW_DP0_INT_IMPDEF1 | 1134 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3; 1135 1136 if (status & impl_int_mask) { 1137 clear |= impl_int_mask; 1138 *slave_status = clear; 1139 } 1140 1141 /* clear the interrupt */ 1142 ret = sdw_write(slave, SDW_DP0_INT, clear); 1143 if (ret < 0) { 1144 dev_err(slave->bus->dev, 1145 "SDW_DP0_INT write failed:%d\n", ret); 1146 return ret; 1147 } 1148 1149 /* Read DP0 interrupt again */ 1150 status2 = sdw_read(slave, SDW_DP0_INT); 1151 if (status2 < 0) { 1152 dev_err(slave->bus->dev, 1153 "SDW_DP0_INT read failed:%d\n", status2); 1154 return status2; 1155 } 1156 status &= status2; 1157 1158 count++; 1159 1160 /* we can get alerts while processing so keep retrying */ 1161 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY); 1162 1163 if (count == SDW_READ_INTR_CLEAR_RETRY) 1164 dev_warn(slave->bus->dev, "Reached MAX_RETRY on DP0 read\n"); 1165 1166 return ret; 1167 } 1168 1169 static int sdw_handle_port_interrupt(struct sdw_slave *slave, 1170 int port, u8 *slave_status) 1171 { 1172 u8 clear = 0, impl_int_mask; 1173 int status, status2, ret, count = 0; 1174 u32 addr; 1175 1176 if (port == 0) 1177 return sdw_handle_dp0_interrupt(slave, slave_status); 1178 1179 addr = SDW_DPN_INT(port); 1180 status = sdw_read(slave, addr); 1181 if (status < 0) { 1182 dev_err(slave->bus->dev, 1183 "SDW_DPN_INT read failed:%d\n", status); 1184 1185 return status; 1186 } 1187 1188 do { 1189 if (status & SDW_DPN_INT_TEST_FAIL) { 1190 dev_err(&slave->dev, "Test fail for port:%d\n", port); 1191 clear |= SDW_DPN_INT_TEST_FAIL; 1192 } 1193 1194 /* 1195 * Assumption: PORT_READY interrupt will be received only 1196 * for ports implementing CP_SM. 1197 */ 1198 if (status & SDW_DPN_INT_PORT_READY) { 1199 complete(&slave->port_ready[port]); 1200 clear |= SDW_DPN_INT_PORT_READY; 1201 } 1202 1203 impl_int_mask = SDW_DPN_INT_IMPDEF1 | 1204 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3; 1205 1206 if (status & impl_int_mask) { 1207 clear |= impl_int_mask; 1208 *slave_status = clear; 1209 } 1210 1211 /* clear the interrupt */ 1212 ret = sdw_write(slave, addr, clear); 1213 if (ret < 0) { 1214 dev_err(slave->bus->dev, 1215 "SDW_DPN_INT write failed:%d\n", ret); 1216 return ret; 1217 } 1218 1219 /* Read DPN interrupt again */ 1220 status2 = sdw_read(slave, addr); 1221 if (status2 < 0) { 1222 dev_err(slave->bus->dev, 1223 "SDW_DPN_INT read failed:%d\n", status2); 1224 return status2; 1225 } 1226 status &= status2; 1227 1228 count++; 1229 1230 /* we can get alerts while processing so keep retrying */ 1231 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY); 1232 1233 if (count == SDW_READ_INTR_CLEAR_RETRY) 1234 dev_warn(slave->bus->dev, "Reached MAX_RETRY on port read"); 1235 1236 return ret; 1237 } 1238 1239 static int sdw_handle_slave_alerts(struct sdw_slave *slave) 1240 { 1241 struct sdw_slave_intr_status slave_intr; 1242 u8 clear = 0, bit, port_status[15] = {0}; 1243 int port_num, stat, ret, count = 0; 1244 unsigned long port; 1245 bool slave_notify = false; 1246 u8 buf, buf2[2], _buf, _buf2[2]; 1247 1248 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT); 1249 1250 ret = pm_runtime_get_sync(&slave->dev); 1251 if (ret < 0 && ret != -EACCES) { 1252 dev_err(&slave->dev, "Failed to resume device: %d\n", ret); 1253 pm_runtime_put_noidle(slave->bus->dev); 1254 return ret; 1255 } 1256 1257 /* Read Instat 1, Instat 2 and Instat 3 registers */ 1258 ret = sdw_read(slave, SDW_SCP_INT1); 1259 if (ret < 0) { 1260 dev_err(slave->bus->dev, 1261 "SDW_SCP_INT1 read failed:%d\n", ret); 1262 goto io_err; 1263 } 1264 buf = ret; 1265 1266 ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, buf2); 1267 if (ret < 0) { 1268 dev_err(slave->bus->dev, 1269 "SDW_SCP_INT2/3 read failed:%d\n", ret); 1270 goto io_err; 1271 } 1272 1273 do { 1274 /* 1275 * Check parity, bus clash and Slave (impl defined) 1276 * interrupt 1277 */ 1278 if (buf & SDW_SCP_INT1_PARITY) { 1279 dev_err(&slave->dev, "Parity error detected\n"); 1280 clear |= SDW_SCP_INT1_PARITY; 1281 } 1282 1283 if (buf & SDW_SCP_INT1_BUS_CLASH) { 1284 dev_err(&slave->dev, "Bus clash error detected\n"); 1285 clear |= SDW_SCP_INT1_BUS_CLASH; 1286 } 1287 1288 /* 1289 * When bus clash or parity errors are detected, such errors 1290 * are unlikely to be recoverable errors. 1291 * TODO: In such scenario, reset bus. Make this configurable 1292 * via sysfs property with bus reset being the default. 1293 */ 1294 1295 if (buf & SDW_SCP_INT1_IMPL_DEF) { 1296 dev_dbg(&slave->dev, "Slave impl defined interrupt\n"); 1297 clear |= SDW_SCP_INT1_IMPL_DEF; 1298 slave_notify = true; 1299 } 1300 1301 /* Check port 0 - 3 interrupts */ 1302 port = buf & SDW_SCP_INT1_PORT0_3; 1303 1304 /* To get port number corresponding to bits, shift it */ 1305 port = port >> SDW_REG_SHIFT(SDW_SCP_INT1_PORT0_3); 1306 for_each_set_bit(bit, &port, 8) { 1307 sdw_handle_port_interrupt(slave, bit, 1308 &port_status[bit]); 1309 } 1310 1311 /* Check if cascade 2 interrupt is present */ 1312 if (buf & SDW_SCP_INT1_SCP2_CASCADE) { 1313 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10; 1314 for_each_set_bit(bit, &port, 8) { 1315 /* scp2 ports start from 4 */ 1316 port_num = bit + 3; 1317 sdw_handle_port_interrupt(slave, 1318 port_num, 1319 &port_status[port_num]); 1320 } 1321 } 1322 1323 /* now check last cascade */ 1324 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) { 1325 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14; 1326 for_each_set_bit(bit, &port, 8) { 1327 /* scp3 ports start from 11 */ 1328 port_num = bit + 10; 1329 sdw_handle_port_interrupt(slave, 1330 port_num, 1331 &port_status[port_num]); 1332 } 1333 } 1334 1335 /* Update the Slave driver */ 1336 if (slave_notify && slave->ops && 1337 slave->ops->interrupt_callback) { 1338 slave_intr.control_port = clear; 1339 memcpy(slave_intr.port, &port_status, 1340 sizeof(slave_intr.port)); 1341 1342 slave->ops->interrupt_callback(slave, &slave_intr); 1343 } 1344 1345 /* Ack interrupt */ 1346 ret = sdw_write(slave, SDW_SCP_INT1, clear); 1347 if (ret < 0) { 1348 dev_err(slave->bus->dev, 1349 "SDW_SCP_INT1 write failed:%d\n", ret); 1350 goto io_err; 1351 } 1352 1353 /* 1354 * Read status again to ensure no new interrupts arrived 1355 * while servicing interrupts. 1356 */ 1357 ret = sdw_read(slave, SDW_SCP_INT1); 1358 if (ret < 0) { 1359 dev_err(slave->bus->dev, 1360 "SDW_SCP_INT1 read failed:%d\n", ret); 1361 goto io_err; 1362 } 1363 _buf = ret; 1364 1365 ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, _buf2); 1366 if (ret < 0) { 1367 dev_err(slave->bus->dev, 1368 "SDW_SCP_INT2/3 read failed:%d\n", ret); 1369 goto io_err; 1370 } 1371 1372 /* Make sure no interrupts are pending */ 1373 buf &= _buf; 1374 buf2[0] &= _buf2[0]; 1375 buf2[1] &= _buf2[1]; 1376 stat = buf || buf2[0] || buf2[1]; 1377 1378 /* 1379 * Exit loop if Slave is continuously in ALERT state even 1380 * after servicing the interrupt multiple times. 1381 */ 1382 count++; 1383 1384 /* we can get alerts while processing so keep retrying */ 1385 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY); 1386 1387 if (count == SDW_READ_INTR_CLEAR_RETRY) 1388 dev_warn(slave->bus->dev, "Reached MAX_RETRY on alert read\n"); 1389 1390 io_err: 1391 pm_runtime_mark_last_busy(&slave->dev); 1392 pm_runtime_put_autosuspend(&slave->dev); 1393 1394 return ret; 1395 } 1396 1397 static int sdw_update_slave_status(struct sdw_slave *slave, 1398 enum sdw_slave_status status) 1399 { 1400 unsigned long time; 1401 1402 if (!slave->probed) { 1403 /* 1404 * the slave status update is typically handled in an 1405 * interrupt thread, which can race with the driver 1406 * probe, e.g. when a module needs to be loaded. 1407 * 1408 * make sure the probe is complete before updating 1409 * status. 1410 */ 1411 time = wait_for_completion_timeout(&slave->probe_complete, 1412 msecs_to_jiffies(DEFAULT_PROBE_TIMEOUT)); 1413 if (!time) { 1414 dev_err(&slave->dev, "Probe not complete, timed out\n"); 1415 return -ETIMEDOUT; 1416 } 1417 } 1418 1419 if (!slave->ops || !slave->ops->update_status) 1420 return 0; 1421 1422 return slave->ops->update_status(slave, status); 1423 } 1424 1425 /** 1426 * sdw_handle_slave_status() - Handle Slave status 1427 * @bus: SDW bus instance 1428 * @status: Status for all Slave(s) 1429 */ 1430 int sdw_handle_slave_status(struct sdw_bus *bus, 1431 enum sdw_slave_status status[]) 1432 { 1433 enum sdw_slave_status prev_status; 1434 struct sdw_slave *slave; 1435 bool attached_initializing; 1436 int i, ret = 0; 1437 1438 /* first check if any Slaves fell off the bus */ 1439 for (i = 1; i <= SDW_MAX_DEVICES; i++) { 1440 mutex_lock(&bus->bus_lock); 1441 if (test_bit(i, bus->assigned) == false) { 1442 mutex_unlock(&bus->bus_lock); 1443 continue; 1444 } 1445 mutex_unlock(&bus->bus_lock); 1446 1447 slave = sdw_get_slave(bus, i); 1448 if (!slave) 1449 continue; 1450 1451 if (status[i] == SDW_SLAVE_UNATTACHED && 1452 slave->status != SDW_SLAVE_UNATTACHED) 1453 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED); 1454 } 1455 1456 if (status[0] == SDW_SLAVE_ATTACHED) { 1457 dev_dbg(bus->dev, "Slave attached, programming device number\n"); 1458 ret = sdw_program_device_num(bus); 1459 if (ret) 1460 dev_err(bus->dev, "Slave attach failed: %d\n", ret); 1461 /* 1462 * programming a device number will have side effects, 1463 * so we deal with other devices at a later time 1464 */ 1465 return ret; 1466 } 1467 1468 /* Continue to check other slave statuses */ 1469 for (i = 1; i <= SDW_MAX_DEVICES; i++) { 1470 mutex_lock(&bus->bus_lock); 1471 if (test_bit(i, bus->assigned) == false) { 1472 mutex_unlock(&bus->bus_lock); 1473 continue; 1474 } 1475 mutex_unlock(&bus->bus_lock); 1476 1477 slave = sdw_get_slave(bus, i); 1478 if (!slave) 1479 continue; 1480 1481 attached_initializing = false; 1482 1483 switch (status[i]) { 1484 case SDW_SLAVE_UNATTACHED: 1485 if (slave->status == SDW_SLAVE_UNATTACHED) 1486 break; 1487 1488 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED); 1489 break; 1490 1491 case SDW_SLAVE_ALERT: 1492 ret = sdw_handle_slave_alerts(slave); 1493 if (ret) 1494 dev_err(bus->dev, 1495 "Slave %d alert handling failed: %d\n", 1496 i, ret); 1497 break; 1498 1499 case SDW_SLAVE_ATTACHED: 1500 if (slave->status == SDW_SLAVE_ATTACHED) 1501 break; 1502 1503 prev_status = slave->status; 1504 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED); 1505 1506 if (prev_status == SDW_SLAVE_ALERT) 1507 break; 1508 1509 attached_initializing = true; 1510 1511 ret = sdw_initialize_slave(slave); 1512 if (ret) 1513 dev_err(bus->dev, 1514 "Slave %d initialization failed: %d\n", 1515 i, ret); 1516 1517 break; 1518 1519 default: 1520 dev_err(bus->dev, "Invalid slave %d status:%d\n", 1521 i, status[i]); 1522 break; 1523 } 1524 1525 ret = sdw_update_slave_status(slave, status[i]); 1526 if (ret) 1527 dev_err(slave->bus->dev, 1528 "Update Slave status failed:%d\n", ret); 1529 if (attached_initializing) 1530 complete(&slave->initialization_complete); 1531 } 1532 1533 return ret; 1534 } 1535 EXPORT_SYMBOL(sdw_handle_slave_status); 1536 1537 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request) 1538 { 1539 struct sdw_slave *slave; 1540 int i; 1541 1542 /* Check all non-zero devices */ 1543 for (i = 1; i <= SDW_MAX_DEVICES; i++) { 1544 mutex_lock(&bus->bus_lock); 1545 if (test_bit(i, bus->assigned) == false) { 1546 mutex_unlock(&bus->bus_lock); 1547 continue; 1548 } 1549 mutex_unlock(&bus->bus_lock); 1550 1551 slave = sdw_get_slave(bus, i); 1552 if (!slave) 1553 continue; 1554 1555 if (slave->status != SDW_SLAVE_UNATTACHED) 1556 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED); 1557 1558 /* keep track of request, used in pm_runtime resume */ 1559 slave->unattach_request = request; 1560 } 1561 } 1562 EXPORT_SYMBOL(sdw_clear_slave_status); 1563