1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2018 Cadence Design Systems Inc. 4 * 5 * Author: Boris Brezillon <boris.brezillon@bootlin.com> 6 */ 7 8 #include <linux/atomic.h> 9 #include <linux/bug.h> 10 #include <linux/device.h> 11 #include <linux/dma-mapping.h> 12 #include <linux/err.h> 13 #include <linux/export.h> 14 #include <linux/kernel.h> 15 #include <linux/list.h> 16 #include <linux/of.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/slab.h> 19 #include <linux/spinlock.h> 20 #include <linux/workqueue.h> 21 22 #include "internals.h" 23 24 static DEFINE_IDR(i3c_bus_idr); 25 static DEFINE_MUTEX(i3c_core_lock); 26 static int __i3c_first_dynamic_bus_num; 27 static BLOCKING_NOTIFIER_HEAD(i3c_bus_notifier); 28 29 /** 30 * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation 31 * @bus: I3C bus to take the lock on 32 * 33 * This function takes the bus lock so that no other operations can occur on 34 * the bus. This is needed for all kind of bus maintenance operation, like 35 * - enabling/disabling slave events 36 * - re-triggering DAA 37 * - changing the dynamic address of a device 38 * - relinquishing mastership 39 * - ... 40 * 41 * The reason for this kind of locking is that we don't want drivers and core 42 * logic to rely on I3C device information that could be changed behind their 43 * back. 44 */ 45 static void i3c_bus_maintenance_lock(struct i3c_bus *bus) 46 { 47 down_write(&bus->lock); 48 } 49 50 /** 51 * i3c_bus_maintenance_unlock - Release the bus lock after a maintenance 52 * operation 53 * @bus: I3C bus to release the lock on 54 * 55 * Should be called when the bus maintenance operation is done. See 56 * i3c_bus_maintenance_lock() for more details on what these maintenance 57 * operations are. 58 */ 59 static void i3c_bus_maintenance_unlock(struct i3c_bus *bus) 60 { 61 up_write(&bus->lock); 62 } 63 64 /** 65 * i3c_bus_normaluse_lock - Lock the bus for a normal operation 66 * @bus: I3C bus to take the lock on 67 * 68 * This function takes the bus lock for any operation that is not a maintenance 69 * operation (see i3c_bus_maintenance_lock() for a non-exhaustive list of 70 * maintenance operations). Basically all communications with I3C devices are 71 * normal operations (HDR, SDR transfers or CCC commands that do not change bus 72 * state or I3C dynamic address). 73 * 74 * Note that this lock is not guaranteeing serialization of normal operations. 75 * In other words, transfer requests passed to the I3C master can be submitted 76 * in parallel and I3C master drivers have to use their own locking to make 77 * sure two different communications are not inter-mixed, or access to the 78 * output/input queue is not done while the engine is busy. 79 */ 80 void i3c_bus_normaluse_lock(struct i3c_bus *bus) 81 { 82 down_read(&bus->lock); 83 } 84 85 /** 86 * i3c_bus_normaluse_unlock - Release the bus lock after a normal operation 87 * @bus: I3C bus to release the lock on 88 * 89 * Should be called when a normal operation is done. See 90 * i3c_bus_normaluse_lock() for more details on what these normal operations 91 * are. 92 */ 93 void i3c_bus_normaluse_unlock(struct i3c_bus *bus) 94 { 95 up_read(&bus->lock); 96 } 97 98 static struct i3c_master_controller * 99 i3c_bus_to_i3c_master(struct i3c_bus *i3cbus) 100 { 101 return container_of(i3cbus, struct i3c_master_controller, bus); 102 } 103 104 static struct i3c_master_controller *dev_to_i3cmaster(struct device *dev) 105 { 106 return container_of(dev, struct i3c_master_controller, dev); 107 } 108 109 static int __must_check i3c_master_rpm_get(struct i3c_master_controller *master) 110 { 111 int ret = master->rpm_allowed ? pm_runtime_resume_and_get(master->dev.parent) : 0; 112 113 if (ret < 0) { 114 dev_err(master->dev.parent, "runtime resume failed, error %d\n", ret); 115 return ret; 116 } 117 return 0; 118 } 119 120 static void i3c_master_rpm_put(struct i3c_master_controller *master) 121 { 122 if (master->rpm_allowed) 123 pm_runtime_put_autosuspend(master->dev.parent); 124 } 125 126 int i3c_bus_rpm_get(struct i3c_bus *bus) 127 { 128 return i3c_master_rpm_get(i3c_bus_to_i3c_master(bus)); 129 } 130 131 void i3c_bus_rpm_put(struct i3c_bus *bus) 132 { 133 i3c_master_rpm_put(i3c_bus_to_i3c_master(bus)); 134 } 135 136 bool i3c_bus_rpm_ibi_allowed(struct i3c_bus *bus) 137 { 138 return i3c_bus_to_i3c_master(bus)->rpm_ibi_allowed; 139 } 140 141 static const struct device_type i3c_device_type; 142 143 static struct i3c_bus *dev_to_i3cbus(struct device *dev) 144 { 145 struct i3c_master_controller *master; 146 147 if (dev->type == &i3c_device_type) 148 return dev_to_i3cdev(dev)->bus; 149 150 master = dev_to_i3cmaster(dev); 151 152 return &master->bus; 153 } 154 155 static struct i3c_dev_desc *dev_to_i3cdesc(struct device *dev) 156 { 157 struct i3c_master_controller *master; 158 159 if (dev->type == &i3c_device_type) 160 return dev_to_i3cdev(dev)->desc; 161 162 master = dev_to_i3cmaster(dev); 163 164 return master->this; 165 } 166 167 static ssize_t bcr_show(struct device *dev, 168 struct device_attribute *da, 169 char *buf) 170 { 171 struct i3c_bus *bus = dev_to_i3cbus(dev); 172 struct i3c_dev_desc *desc; 173 ssize_t ret; 174 175 i3c_bus_normaluse_lock(bus); 176 desc = dev_to_i3cdesc(dev); 177 ret = sprintf(buf, "0x%02x\n", desc->info.bcr); 178 i3c_bus_normaluse_unlock(bus); 179 180 return ret; 181 } 182 static DEVICE_ATTR_RO(bcr); 183 184 static ssize_t dcr_show(struct device *dev, 185 struct device_attribute *da, 186 char *buf) 187 { 188 struct i3c_bus *bus = dev_to_i3cbus(dev); 189 struct i3c_dev_desc *desc; 190 ssize_t ret; 191 192 i3c_bus_normaluse_lock(bus); 193 desc = dev_to_i3cdesc(dev); 194 ret = sprintf(buf, "0x%02x\n", desc->info.dcr); 195 i3c_bus_normaluse_unlock(bus); 196 197 return ret; 198 } 199 static DEVICE_ATTR_RO(dcr); 200 201 static ssize_t pid_show(struct device *dev, 202 struct device_attribute *da, 203 char *buf) 204 { 205 struct i3c_bus *bus = dev_to_i3cbus(dev); 206 struct i3c_dev_desc *desc; 207 ssize_t ret; 208 209 i3c_bus_normaluse_lock(bus); 210 desc = dev_to_i3cdesc(dev); 211 ret = sprintf(buf, "%llx\n", desc->info.pid); 212 i3c_bus_normaluse_unlock(bus); 213 214 return ret; 215 } 216 static DEVICE_ATTR_RO(pid); 217 218 static ssize_t dynamic_address_show(struct device *dev, 219 struct device_attribute *da, 220 char *buf) 221 { 222 struct i3c_bus *bus = dev_to_i3cbus(dev); 223 struct i3c_dev_desc *desc; 224 ssize_t ret; 225 226 i3c_bus_normaluse_lock(bus); 227 desc = dev_to_i3cdesc(dev); 228 ret = sprintf(buf, "%02x\n", desc->info.dyn_addr); 229 i3c_bus_normaluse_unlock(bus); 230 231 return ret; 232 } 233 static DEVICE_ATTR_RO(dynamic_address); 234 235 static const char * const hdrcap_strings[] = { 236 "hdr-ddr", "hdr-tsp", "hdr-tsl", 237 }; 238 239 static ssize_t hdrcap_show(struct device *dev, 240 struct device_attribute *da, 241 char *buf) 242 { 243 struct i3c_bus *bus = dev_to_i3cbus(dev); 244 struct i3c_dev_desc *desc; 245 ssize_t offset = 0, ret; 246 unsigned long caps; 247 int mode; 248 249 i3c_bus_normaluse_lock(bus); 250 desc = dev_to_i3cdesc(dev); 251 caps = desc->info.hdr_cap; 252 for_each_set_bit(mode, &caps, 8) { 253 if (mode >= ARRAY_SIZE(hdrcap_strings)) 254 break; 255 256 if (!hdrcap_strings[mode]) 257 continue; 258 259 ret = sprintf(buf + offset, offset ? " %s" : "%s", 260 hdrcap_strings[mode]); 261 if (ret < 0) 262 goto out; 263 264 offset += ret; 265 } 266 267 ret = sprintf(buf + offset, "\n"); 268 if (ret < 0) 269 goto out; 270 271 ret = offset + ret; 272 273 out: 274 i3c_bus_normaluse_unlock(bus); 275 276 return ret; 277 } 278 static DEVICE_ATTR_RO(hdrcap); 279 280 static ssize_t modalias_show(struct device *dev, 281 struct device_attribute *da, char *buf) 282 { 283 struct i3c_device *i3c = dev_to_i3cdev(dev); 284 struct i3c_device_info devinfo; 285 u16 manuf, part, ext; 286 287 i3c_device_get_info(i3c, &devinfo); 288 manuf = I3C_PID_MANUF_ID(devinfo.pid); 289 part = I3C_PID_PART_ID(devinfo.pid); 290 ext = I3C_PID_EXTRA_INFO(devinfo.pid); 291 292 if (I3C_PID_RND_LOWER_32BITS(devinfo.pid)) 293 return sprintf(buf, "i3c:dcr%02Xmanuf%04X", devinfo.dcr, 294 manuf); 295 296 return sprintf(buf, "i3c:dcr%02Xmanuf%04Xpart%04Xext%04X", 297 devinfo.dcr, manuf, part, ext); 298 } 299 static DEVICE_ATTR_RO(modalias); 300 301 static struct attribute *i3c_device_attrs[] = { 302 &dev_attr_bcr.attr, 303 &dev_attr_dcr.attr, 304 &dev_attr_pid.attr, 305 &dev_attr_dynamic_address.attr, 306 &dev_attr_hdrcap.attr, 307 &dev_attr_modalias.attr, 308 NULL, 309 }; 310 ATTRIBUTE_GROUPS(i3c_device); 311 312 static int i3c_device_uevent(const struct device *dev, struct kobj_uevent_env *env) 313 { 314 const struct i3c_device *i3cdev = dev_to_i3cdev(dev); 315 struct i3c_device_info devinfo; 316 u16 manuf, part, ext; 317 318 if (i3cdev->desc) 319 devinfo = i3cdev->desc->info; 320 manuf = I3C_PID_MANUF_ID(devinfo.pid); 321 part = I3C_PID_PART_ID(devinfo.pid); 322 ext = I3C_PID_EXTRA_INFO(devinfo.pid); 323 324 if (I3C_PID_RND_LOWER_32BITS(devinfo.pid)) 325 return add_uevent_var(env, "MODALIAS=i3c:dcr%02Xmanuf%04X", 326 devinfo.dcr, manuf); 327 328 return add_uevent_var(env, 329 "MODALIAS=i3c:dcr%02Xmanuf%04Xpart%04Xext%04X", 330 devinfo.dcr, manuf, part, ext); 331 } 332 333 static const struct device_type i3c_device_type = { 334 .groups = i3c_device_groups, 335 .uevent = i3c_device_uevent, 336 }; 337 338 static int i3c_device_match(struct device *dev, const struct device_driver *drv) 339 { 340 struct i3c_device *i3cdev; 341 const struct i3c_driver *i3cdrv; 342 343 if (dev->type != &i3c_device_type) 344 return 0; 345 346 i3cdev = dev_to_i3cdev(dev); 347 i3cdrv = drv_to_i3cdrv(drv); 348 if (i3c_device_match_id(i3cdev, i3cdrv->id_table)) 349 return 1; 350 351 return 0; 352 } 353 354 static int i3c_device_probe(struct device *dev) 355 { 356 struct i3c_device *i3cdev = dev_to_i3cdev(dev); 357 struct i3c_driver *driver = drv_to_i3cdrv(dev->driver); 358 359 return driver->probe(i3cdev); 360 } 361 362 static void i3c_device_remove(struct device *dev) 363 { 364 struct i3c_device *i3cdev = dev_to_i3cdev(dev); 365 struct i3c_driver *driver = drv_to_i3cdrv(dev->driver); 366 367 if (driver->remove) 368 driver->remove(i3cdev); 369 } 370 371 const struct bus_type i3c_bus_type = { 372 .name = "i3c", 373 .match = i3c_device_match, 374 .probe = i3c_device_probe, 375 .remove = i3c_device_remove, 376 }; 377 EXPORT_SYMBOL_GPL(i3c_bus_type); 378 379 static enum i3c_addr_slot_status 380 i3c_bus_get_addr_slot_status_mask(struct i3c_bus *bus, u16 addr, u32 mask) 381 { 382 unsigned long status; 383 int bitpos = addr * I3C_ADDR_SLOT_STATUS_BITS; 384 385 if (addr > I2C_MAX_ADDR) 386 return I3C_ADDR_SLOT_RSVD; 387 388 status = bus->addrslots[bitpos / BITS_PER_LONG]; 389 status >>= bitpos % BITS_PER_LONG; 390 391 return status & mask; 392 } 393 394 static enum i3c_addr_slot_status 395 i3c_bus_get_addr_slot_status(struct i3c_bus *bus, u16 addr) 396 { 397 return i3c_bus_get_addr_slot_status_mask(bus, addr, I3C_ADDR_SLOT_STATUS_MASK); 398 } 399 400 static void i3c_bus_set_addr_slot_status_mask(struct i3c_bus *bus, u16 addr, 401 enum i3c_addr_slot_status status, u32 mask) 402 { 403 int bitpos = addr * I3C_ADDR_SLOT_STATUS_BITS; 404 unsigned long *ptr; 405 406 if (addr > I2C_MAX_ADDR) 407 return; 408 409 ptr = bus->addrslots + (bitpos / BITS_PER_LONG); 410 *ptr &= ~((unsigned long)mask << (bitpos % BITS_PER_LONG)); 411 *ptr |= ((unsigned long)status & mask) << (bitpos % BITS_PER_LONG); 412 } 413 414 static void i3c_bus_set_addr_slot_status(struct i3c_bus *bus, u16 addr, 415 enum i3c_addr_slot_status status) 416 { 417 i3c_bus_set_addr_slot_status_mask(bus, addr, status, I3C_ADDR_SLOT_STATUS_MASK); 418 } 419 420 static bool i3c_bus_dev_addr_is_avail(struct i3c_bus *bus, u8 addr) 421 { 422 enum i3c_addr_slot_status status; 423 424 status = i3c_bus_get_addr_slot_status(bus, addr); 425 426 return status == I3C_ADDR_SLOT_FREE; 427 } 428 429 /* 430 * ┌────┬─────────────┬───┬─────────┬───┐ 431 * │S/Sr│ 7'h7E RnW=0 │ACK│ ENTDAA │ T ├────┐ 432 * └────┴─────────────┴───┴─────────┴───┘ │ 433 * ┌─────────────────────────────────────────┘ 434 * │ ┌──┬─────────────┬───┬─────────────────┬────────────────┬───┬─────────┐ 435 * └─►│Sr│7'h7E RnW=1 │ACK│48bit UID BCR DCR│Assign 7bit Addr│PAR│ ACK/NACK│ 436 * └──┴─────────────┴───┴─────────────────┴────────────────┴───┴─────────┘ 437 * Some master controllers (such as HCI) need to prepare the entire above transaction before 438 * sending it out to the I3C bus. This means that a 7-bit dynamic address needs to be allocated 439 * before knowing the target device's UID information. 440 * 441 * However, some I3C targets may request specific addresses (called as "init_dyn_addr"), which is 442 * typically specified by the DT-'s assigned-address property. Lower addresses having higher IBI 443 * priority. If it is available, i3c_bus_get_free_addr() preferably return a free address that is 444 * not in the list of desired addresses (called as "init_dyn_addr"). This allows the device with 445 * the "init_dyn_addr" to switch to its "init_dyn_addr" when it hot-joins the I3C bus. Otherwise, 446 * if the "init_dyn_addr" is already in use by another I3C device, the target device will not be 447 * able to switch to its desired address. 448 * 449 * If the previous step fails, fallback returning one of the remaining unassigned address, 450 * regardless of its state in the desired list. 451 */ 452 static int i3c_bus_get_free_addr(struct i3c_bus *bus, u8 start_addr) 453 { 454 enum i3c_addr_slot_status status; 455 u8 addr; 456 457 for (addr = start_addr; addr < I3C_MAX_ADDR; addr++) { 458 status = i3c_bus_get_addr_slot_status_mask(bus, addr, 459 I3C_ADDR_SLOT_EXT_STATUS_MASK); 460 if (status == I3C_ADDR_SLOT_FREE) 461 return addr; 462 } 463 464 for (addr = start_addr; addr < I3C_MAX_ADDR; addr++) { 465 status = i3c_bus_get_addr_slot_status_mask(bus, addr, 466 I3C_ADDR_SLOT_STATUS_MASK); 467 if (status == I3C_ADDR_SLOT_FREE) 468 return addr; 469 } 470 471 return -ENOMEM; 472 } 473 474 static void i3c_bus_init_addrslots(struct i3c_bus *bus) 475 { 476 int i; 477 478 /* Addresses 0 to 7 are reserved. */ 479 for (i = 0; i < 8; i++) 480 i3c_bus_set_addr_slot_status(bus, i, I3C_ADDR_SLOT_RSVD); 481 482 /* 483 * Reserve broadcast address and all addresses that might collide 484 * with the broadcast address when facing a single bit error. 485 */ 486 i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR, 487 I3C_ADDR_SLOT_RSVD); 488 for (i = 0; i < 7; i++) 489 i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR ^ BIT(i), 490 I3C_ADDR_SLOT_RSVD); 491 } 492 493 static void i3c_bus_cleanup(struct i3c_bus *i3cbus) 494 { 495 mutex_lock(&i3c_core_lock); 496 idr_remove(&i3c_bus_idr, i3cbus->id); 497 mutex_unlock(&i3c_core_lock); 498 } 499 500 static int i3c_bus_init(struct i3c_bus *i3cbus, struct device_node *np) 501 { 502 int ret, start, end, id = -1; 503 504 init_rwsem(&i3cbus->lock); 505 INIT_LIST_HEAD(&i3cbus->devs.i2c); 506 INIT_LIST_HEAD(&i3cbus->devs.i3c); 507 i3c_bus_init_addrslots(i3cbus); 508 i3cbus->mode = I3C_BUS_MODE_PURE; 509 510 if (np) 511 id = of_alias_get_id(np, "i3c"); 512 513 mutex_lock(&i3c_core_lock); 514 if (id >= 0) { 515 start = id; 516 end = start + 1; 517 } else { 518 start = __i3c_first_dynamic_bus_num; 519 end = 0; 520 } 521 522 ret = idr_alloc(&i3c_bus_idr, i3cbus, start, end, GFP_KERNEL); 523 mutex_unlock(&i3c_core_lock); 524 525 if (ret < 0) 526 return ret; 527 528 i3cbus->id = ret; 529 530 return 0; 531 } 532 533 void i3c_for_each_bus_locked(int (*fn)(struct i3c_bus *bus, void *data), 534 void *data) 535 { 536 struct i3c_bus *bus; 537 int id; 538 539 mutex_lock(&i3c_core_lock); 540 idr_for_each_entry(&i3c_bus_idr, bus, id) 541 fn(bus, data); 542 mutex_unlock(&i3c_core_lock); 543 } 544 EXPORT_SYMBOL_GPL(i3c_for_each_bus_locked); 545 546 int i3c_register_notifier(struct notifier_block *nb) 547 { 548 return blocking_notifier_chain_register(&i3c_bus_notifier, nb); 549 } 550 EXPORT_SYMBOL_GPL(i3c_register_notifier); 551 552 int i3c_unregister_notifier(struct notifier_block *nb) 553 { 554 return blocking_notifier_chain_unregister(&i3c_bus_notifier, nb); 555 } 556 EXPORT_SYMBOL_GPL(i3c_unregister_notifier); 557 558 static void i3c_bus_notify(struct i3c_bus *bus, unsigned int action) 559 { 560 blocking_notifier_call_chain(&i3c_bus_notifier, action, bus); 561 } 562 563 static const char * const i3c_bus_mode_strings[] = { 564 [I3C_BUS_MODE_PURE] = "pure", 565 [I3C_BUS_MODE_MIXED_FAST] = "mixed-fast", 566 [I3C_BUS_MODE_MIXED_LIMITED] = "mixed-limited", 567 [I3C_BUS_MODE_MIXED_SLOW] = "mixed-slow", 568 }; 569 570 static ssize_t mode_show(struct device *dev, 571 struct device_attribute *da, 572 char *buf) 573 { 574 struct i3c_bus *i3cbus = dev_to_i3cbus(dev); 575 ssize_t ret; 576 577 i3c_bus_normaluse_lock(i3cbus); 578 if (i3cbus->mode < 0 || 579 i3cbus->mode >= ARRAY_SIZE(i3c_bus_mode_strings) || 580 !i3c_bus_mode_strings[i3cbus->mode]) 581 ret = sprintf(buf, "unknown\n"); 582 else 583 ret = sprintf(buf, "%s\n", i3c_bus_mode_strings[i3cbus->mode]); 584 i3c_bus_normaluse_unlock(i3cbus); 585 586 return ret; 587 } 588 static DEVICE_ATTR_RO(mode); 589 590 static ssize_t current_master_show(struct device *dev, 591 struct device_attribute *da, 592 char *buf) 593 { 594 struct i3c_bus *i3cbus = dev_to_i3cbus(dev); 595 ssize_t ret; 596 597 i3c_bus_normaluse_lock(i3cbus); 598 ret = sprintf(buf, "%d-%llx\n", i3cbus->id, 599 i3cbus->cur_master->info.pid); 600 i3c_bus_normaluse_unlock(i3cbus); 601 602 return ret; 603 } 604 static DEVICE_ATTR_RO(current_master); 605 606 static ssize_t i3c_scl_frequency_show(struct device *dev, 607 struct device_attribute *da, 608 char *buf) 609 { 610 struct i3c_bus *i3cbus = dev_to_i3cbus(dev); 611 ssize_t ret; 612 613 i3c_bus_normaluse_lock(i3cbus); 614 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i3c); 615 i3c_bus_normaluse_unlock(i3cbus); 616 617 return ret; 618 } 619 static DEVICE_ATTR_RO(i3c_scl_frequency); 620 621 static ssize_t i2c_scl_frequency_show(struct device *dev, 622 struct device_attribute *da, 623 char *buf) 624 { 625 struct i3c_bus *i3cbus = dev_to_i3cbus(dev); 626 ssize_t ret; 627 628 i3c_bus_normaluse_lock(i3cbus); 629 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i2c); 630 i3c_bus_normaluse_unlock(i3cbus); 631 632 return ret; 633 } 634 static DEVICE_ATTR_RO(i2c_scl_frequency); 635 636 static int i3c_set_hotjoin(struct i3c_master_controller *master, bool enable) 637 { 638 int ret; 639 640 if (!master || !master->ops) 641 return -EINVAL; 642 643 if (!master->ops->enable_hotjoin || !master->ops->disable_hotjoin) 644 return -EINVAL; 645 646 if (enable || master->rpm_ibi_allowed) { 647 ret = i3c_master_rpm_get(master); 648 if (ret) 649 return ret; 650 } 651 652 i3c_bus_normaluse_lock(&master->bus); 653 654 if (enable) 655 ret = master->ops->enable_hotjoin(master); 656 else 657 ret = master->ops->disable_hotjoin(master); 658 659 if (!ret) 660 master->hotjoin = enable; 661 662 i3c_bus_normaluse_unlock(&master->bus); 663 664 if ((enable && ret) || (!enable && !ret) || master->rpm_ibi_allowed) 665 i3c_master_rpm_put(master); 666 667 return ret; 668 } 669 670 static ssize_t hotjoin_store(struct device *dev, struct device_attribute *attr, 671 const char *buf, size_t count) 672 { 673 struct i3c_bus *i3cbus = dev_to_i3cbus(dev); 674 int ret; 675 bool res; 676 677 if (!i3cbus->cur_master) 678 return -EINVAL; 679 680 if (kstrtobool(buf, &res)) 681 return -EINVAL; 682 683 ret = i3c_set_hotjoin(i3cbus->cur_master->common.master, res); 684 if (ret) 685 return ret; 686 687 return count; 688 } 689 690 /* 691 * i3c_master_enable_hotjoin - Enable hotjoin 692 * @master: I3C master object 693 * 694 * Return: a 0 in case of success, an negative error code otherwise. 695 */ 696 int i3c_master_enable_hotjoin(struct i3c_master_controller *master) 697 { 698 return i3c_set_hotjoin(master, true); 699 } 700 EXPORT_SYMBOL_GPL(i3c_master_enable_hotjoin); 701 702 /* 703 * i3c_master_disable_hotjoin - Disable hotjoin 704 * @master: I3C master object 705 * 706 * Return: a 0 in case of success, an negative error code otherwise. 707 */ 708 int i3c_master_disable_hotjoin(struct i3c_master_controller *master) 709 { 710 return i3c_set_hotjoin(master, false); 711 } 712 EXPORT_SYMBOL_GPL(i3c_master_disable_hotjoin); 713 714 static ssize_t hotjoin_show(struct device *dev, struct device_attribute *da, char *buf) 715 { 716 struct i3c_bus *i3cbus = dev_to_i3cbus(dev); 717 ssize_t ret; 718 719 i3c_bus_normaluse_lock(i3cbus); 720 ret = sysfs_emit(buf, "%d\n", i3cbus->cur_master->common.master->hotjoin); 721 i3c_bus_normaluse_unlock(i3cbus); 722 723 return ret; 724 } 725 726 static DEVICE_ATTR_RW(hotjoin); 727 728 static ssize_t dev_nack_retry_count_show(struct device *dev, 729 struct device_attribute *attr, char *buf) 730 { 731 return sysfs_emit(buf, "%u\n", dev_to_i3cmaster(dev)->dev_nack_retry_count); 732 } 733 734 static ssize_t dev_nack_retry_count_store(struct device *dev, 735 struct device_attribute *attr, 736 const char *buf, size_t count) 737 { 738 struct i3c_bus *i3cbus = dev_to_i3cbus(dev); 739 struct i3c_master_controller *master = dev_to_i3cmaster(dev); 740 unsigned long val; 741 int ret; 742 743 ret = kstrtoul(buf, 0, &val); 744 if (ret) 745 return ret; 746 747 i3c_bus_maintenance_lock(i3cbus); 748 ret = master->ops->set_dev_nack_retry(master, val); 749 i3c_bus_maintenance_unlock(i3cbus); 750 751 if (ret) 752 return ret; 753 754 master->dev_nack_retry_count = val; 755 756 return count; 757 } 758 759 static DEVICE_ATTR_RW(dev_nack_retry_count); 760 761 static struct attribute *i3c_masterdev_attrs[] = { 762 &dev_attr_mode.attr, 763 &dev_attr_current_master.attr, 764 &dev_attr_i3c_scl_frequency.attr, 765 &dev_attr_i2c_scl_frequency.attr, 766 &dev_attr_bcr.attr, 767 &dev_attr_dcr.attr, 768 &dev_attr_pid.attr, 769 &dev_attr_dynamic_address.attr, 770 &dev_attr_hdrcap.attr, 771 &dev_attr_hotjoin.attr, 772 NULL, 773 }; 774 ATTRIBUTE_GROUPS(i3c_masterdev); 775 776 static void i3c_masterdev_release(struct device *dev) 777 { 778 struct i3c_master_controller *master = dev_to_i3cmaster(dev); 779 struct i3c_bus *bus = dev_to_i3cbus(dev); 780 781 if (master->wq) 782 destroy_workqueue(master->wq); 783 784 WARN_ON(!list_empty(&bus->devs.i2c) || !list_empty(&bus->devs.i3c)); 785 i3c_bus_cleanup(bus); 786 787 of_node_put(dev->of_node); 788 } 789 790 static const struct device_type i3c_masterdev_type = { 791 .groups = i3c_masterdev_groups, 792 }; 793 794 static int i3c_bus_set_mode(struct i3c_bus *i3cbus, enum i3c_bus_mode mode, 795 unsigned long max_i2c_scl_rate) 796 { 797 struct i3c_master_controller *master = i3c_bus_to_i3c_master(i3cbus); 798 799 i3cbus->mode = mode; 800 801 switch (i3cbus->mode) { 802 case I3C_BUS_MODE_PURE: 803 if (!i3cbus->scl_rate.i3c) 804 i3cbus->scl_rate.i3c = I3C_BUS_I3C_SCL_TYP_RATE; 805 break; 806 case I3C_BUS_MODE_MIXED_FAST: 807 case I3C_BUS_MODE_MIXED_LIMITED: 808 if (!i3cbus->scl_rate.i3c) 809 i3cbus->scl_rate.i3c = I3C_BUS_I3C_SCL_TYP_RATE; 810 if (!i3cbus->scl_rate.i2c) 811 i3cbus->scl_rate.i2c = max_i2c_scl_rate; 812 break; 813 case I3C_BUS_MODE_MIXED_SLOW: 814 if (!i3cbus->scl_rate.i2c) 815 i3cbus->scl_rate.i2c = max_i2c_scl_rate; 816 if (!i3cbus->scl_rate.i3c || 817 i3cbus->scl_rate.i3c > i3cbus->scl_rate.i2c) 818 i3cbus->scl_rate.i3c = i3cbus->scl_rate.i2c; 819 break; 820 default: 821 return -EINVAL; 822 } 823 824 dev_dbg(&master->dev, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n", 825 i3cbus->scl_rate.i2c, i3cbus->scl_rate.i3c); 826 827 /* 828 * I3C/I2C frequency may have been overridden, check that user-provided 829 * values are not exceeding max possible frequency. 830 */ 831 if (i3cbus->scl_rate.i3c > I3C_BUS_I3C_SCL_MAX_RATE || 832 i3cbus->scl_rate.i2c > I3C_BUS_I2C_FM_PLUS_SCL_MAX_RATE) 833 return -EINVAL; 834 835 return 0; 836 } 837 838 static struct i3c_master_controller * 839 i2c_adapter_to_i3c_master(struct i2c_adapter *adap) 840 { 841 return container_of(adap, struct i3c_master_controller, i2c); 842 } 843 844 static struct i2c_adapter * 845 i3c_master_to_i2c_adapter(struct i3c_master_controller *master) 846 { 847 return &master->i2c; 848 } 849 850 static void i3c_master_free_i2c_dev(struct i2c_dev_desc *dev) 851 { 852 kfree(dev); 853 } 854 855 static struct i2c_dev_desc * 856 i3c_master_alloc_i2c_dev(struct i3c_master_controller *master, 857 u16 addr, u8 lvr) 858 { 859 struct i2c_dev_desc *dev; 860 861 dev = kzalloc_obj(*dev, GFP_KERNEL); 862 if (!dev) 863 return ERR_PTR(-ENOMEM); 864 865 dev->common.master = master; 866 dev->addr = addr; 867 dev->lvr = lvr; 868 869 return dev; 870 } 871 872 static void *i3c_ccc_cmd_dest_init(struct i3c_ccc_cmd_dest *dest, u8 addr, 873 u16 payloadlen) 874 { 875 dest->addr = addr; 876 dest->payload.len = payloadlen; 877 if (payloadlen) 878 dest->payload.data = kzalloc(payloadlen, GFP_KERNEL); 879 else 880 dest->payload.data = NULL; 881 882 return dest->payload.data; 883 } 884 885 static void i3c_ccc_cmd_dest_cleanup(struct i3c_ccc_cmd_dest *dest) 886 { 887 kfree(dest->payload.data); 888 } 889 890 static void i3c_ccc_cmd_init(struct i3c_ccc_cmd *cmd, bool rnw, u8 id, 891 struct i3c_ccc_cmd_dest *dests, 892 unsigned int ndests) 893 { 894 cmd->rnw = rnw ? 1 : 0; 895 cmd->id = id; 896 cmd->dests = dests; 897 cmd->ndests = ndests; 898 cmd->err = I3C_ERROR_UNKNOWN; 899 } 900 901 static int i3c_master_send_ccc_cmd_locked(struct i3c_master_controller *master, 902 struct i3c_ccc_cmd *cmd) 903 { 904 int ret; 905 906 if (!cmd || !master) 907 return -EINVAL; 908 909 if (WARN_ON(master->init_done && 910 !rwsem_is_locked(&master->bus.lock))) 911 return -EINVAL; 912 913 if (!master->ops->send_ccc_cmd) 914 return -EOPNOTSUPP; 915 916 if ((cmd->id & I3C_CCC_DIRECT) && (!cmd->dests || !cmd->ndests)) 917 return -EINVAL; 918 919 if (master->ops->supports_ccc_cmd && 920 !master->ops->supports_ccc_cmd(master, cmd)) 921 return -EOPNOTSUPP; 922 923 ret = master->ops->send_ccc_cmd(master, cmd); 924 if (ret) { 925 if (cmd->err != I3C_ERROR_UNKNOWN) 926 return cmd->err; 927 928 return ret; 929 } 930 931 return 0; 932 } 933 934 static struct i2c_dev_desc * 935 i3c_master_find_i2c_dev_by_addr(const struct i3c_master_controller *master, 936 u16 addr) 937 { 938 struct i2c_dev_desc *dev; 939 940 i3c_bus_for_each_i2cdev(&master->bus, dev) { 941 if (dev->addr == addr) 942 return dev; 943 } 944 945 return NULL; 946 } 947 948 /** 949 * i3c_master_get_free_addr() - get a free address on the bus 950 * @master: I3C master object 951 * @start_addr: where to start searching 952 * 953 * This function must be called with the bus lock held in write mode. 954 * 955 * Return: the first free address starting at @start_addr (included) or -ENOMEM 956 * if there's no more address available. 957 */ 958 int i3c_master_get_free_addr(struct i3c_master_controller *master, 959 u8 start_addr) 960 { 961 return i3c_bus_get_free_addr(&master->bus, start_addr); 962 } 963 EXPORT_SYMBOL_GPL(i3c_master_get_free_addr); 964 965 static void i3c_device_release(struct device *dev) 966 { 967 struct i3c_device *i3cdev = dev_to_i3cdev(dev); 968 969 WARN_ON(i3cdev->desc); 970 971 of_node_put(i3cdev->dev.of_node); 972 kfree(i3cdev); 973 } 974 975 static void i3c_master_free_i3c_dev(struct i3c_dev_desc *dev) 976 { 977 kfree(dev); 978 } 979 980 static struct i3c_dev_desc * 981 i3c_master_alloc_i3c_dev(struct i3c_master_controller *master, 982 const struct i3c_device_info *info) 983 { 984 struct i3c_dev_desc *dev; 985 986 dev = kzalloc_obj(*dev, GFP_KERNEL); 987 if (!dev) 988 return ERR_PTR(-ENOMEM); 989 990 dev->common.master = master; 991 dev->info = *info; 992 mutex_init(&dev->ibi_lock); 993 994 return dev; 995 } 996 997 static int i3c_master_rstdaa_locked(struct i3c_master_controller *master, 998 u8 addr) 999 { 1000 enum i3c_addr_slot_status addrstat; 1001 struct i3c_ccc_cmd_dest dest; 1002 struct i3c_ccc_cmd cmd; 1003 int ret; 1004 1005 if (!master) 1006 return -EINVAL; 1007 1008 addrstat = i3c_bus_get_addr_slot_status(&master->bus, addr); 1009 if (addr != I3C_BROADCAST_ADDR && addrstat != I3C_ADDR_SLOT_I3C_DEV) 1010 return -EINVAL; 1011 1012 i3c_ccc_cmd_dest_init(&dest, addr, 0); 1013 i3c_ccc_cmd_init(&cmd, false, 1014 I3C_CCC_RSTDAA(addr == I3C_BROADCAST_ADDR), 1015 &dest, 1); 1016 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1017 i3c_ccc_cmd_dest_cleanup(&dest); 1018 1019 return ret; 1020 } 1021 1022 /** 1023 * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment) 1024 * procedure 1025 * @master: master used to send frames on the bus 1026 * 1027 * Send a ENTDAA CCC command to start a DAA procedure. 1028 * 1029 * Note that this function only sends the ENTDAA CCC command, all the logic 1030 * behind dynamic address assignment has to be handled in the I3C master 1031 * driver. 1032 * 1033 * This function must be called with the bus lock held in write mode. 1034 * 1035 * Return: 0 in case of success, a positive I3C error code if the error is 1036 * one of the official Mx error codes, and a negative error code otherwise. 1037 */ 1038 int i3c_master_entdaa_locked(struct i3c_master_controller *master) 1039 { 1040 struct i3c_ccc_cmd_dest dest; 1041 struct i3c_ccc_cmd cmd; 1042 int ret; 1043 1044 i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR, 0); 1045 i3c_ccc_cmd_init(&cmd, false, I3C_CCC_ENTDAA, &dest, 1); 1046 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1047 i3c_ccc_cmd_dest_cleanup(&dest); 1048 1049 return ret; 1050 } 1051 EXPORT_SYMBOL_GPL(i3c_master_entdaa_locked); 1052 1053 static int i3c_master_enec_disec_locked(struct i3c_master_controller *master, 1054 u8 addr, bool enable, u8 evts) 1055 { 1056 struct i3c_ccc_events *events; 1057 struct i3c_ccc_cmd_dest dest; 1058 struct i3c_ccc_cmd cmd; 1059 int ret; 1060 1061 events = i3c_ccc_cmd_dest_init(&dest, addr, sizeof(*events)); 1062 if (!events) 1063 return -ENOMEM; 1064 1065 events->events = evts; 1066 i3c_ccc_cmd_init(&cmd, false, 1067 enable ? 1068 I3C_CCC_ENEC(addr == I3C_BROADCAST_ADDR) : 1069 I3C_CCC_DISEC(addr == I3C_BROADCAST_ADDR), 1070 &dest, 1); 1071 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1072 i3c_ccc_cmd_dest_cleanup(&dest); 1073 1074 return ret; 1075 } 1076 1077 /** 1078 * i3c_master_disec_locked() - send a DISEC CCC command 1079 * @master: master used to send frames on the bus 1080 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR 1081 * @evts: events to disable 1082 * 1083 * Send a DISEC CCC command to disable some or all events coming from a 1084 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR. 1085 * 1086 * This function must be called with the bus lock held in write mode. 1087 * 1088 * Return: 0 in case of success, a positive I3C error code if the error is 1089 * one of the official Mx error codes, and a negative error code otherwise. 1090 */ 1091 int i3c_master_disec_locked(struct i3c_master_controller *master, u8 addr, 1092 u8 evts) 1093 { 1094 return i3c_master_enec_disec_locked(master, addr, false, evts); 1095 } 1096 EXPORT_SYMBOL_GPL(i3c_master_disec_locked); 1097 1098 /** 1099 * i3c_master_enec_locked() - send an ENEC CCC command 1100 * @master: master used to send frames on the bus 1101 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR 1102 * @evts: events to disable 1103 * 1104 * Sends an ENEC CCC command to enable some or all events coming from a 1105 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR. 1106 * 1107 * This function must be called with the bus lock held in write mode. 1108 * 1109 * Return: 0 in case of success, a positive I3C error code if the error is 1110 * one of the official Mx error codes, and a negative error code otherwise. 1111 */ 1112 int i3c_master_enec_locked(struct i3c_master_controller *master, u8 addr, 1113 u8 evts) 1114 { 1115 return i3c_master_enec_disec_locked(master, addr, true, evts); 1116 } 1117 EXPORT_SYMBOL_GPL(i3c_master_enec_locked); 1118 1119 /** 1120 * i3c_master_defslvs_locked() - send a DEFSLVS CCC command 1121 * @master: master used to send frames on the bus 1122 * 1123 * Send a DEFSLVS CCC command containing all the devices known to the @master. 1124 * This is useful when you have secondary masters on the bus to propagate 1125 * device information. 1126 * 1127 * This should be called after all I3C devices have been discovered (in other 1128 * words, after the DAA procedure has finished) and instantiated in 1129 * &i3c_master_controller_ops->bus_init(). 1130 * It should also be called if a master ACKed an Hot-Join request and assigned 1131 * a dynamic address to the device joining the bus. 1132 * 1133 * This function must be called with the bus lock held in write mode. 1134 * 1135 * Return: 0 in case of success, a positive I3C error code if the error is 1136 * one of the official Mx error codes, and a negative error code otherwise. 1137 */ 1138 int i3c_master_defslvs_locked(struct i3c_master_controller *master) 1139 { 1140 struct i3c_ccc_defslvs *defslvs; 1141 struct i3c_ccc_dev_desc *desc; 1142 struct i3c_ccc_cmd_dest dest; 1143 struct i3c_dev_desc *i3cdev; 1144 struct i2c_dev_desc *i2cdev; 1145 struct i3c_ccc_cmd cmd; 1146 struct i3c_bus *bus; 1147 bool send = false; 1148 int ndevs = 0, ret; 1149 1150 if (!master) 1151 return -EINVAL; 1152 1153 bus = i3c_master_get_bus(master); 1154 i3c_bus_for_each_i3cdev(bus, i3cdev) { 1155 ndevs++; 1156 1157 if (i3cdev == master->this) 1158 continue; 1159 1160 if (I3C_BCR_DEVICE_ROLE(i3cdev->info.bcr) == 1161 I3C_BCR_I3C_MASTER) 1162 send = true; 1163 } 1164 1165 /* No other master on the bus, skip DEFSLVS. */ 1166 if (!send) 1167 return 0; 1168 1169 i3c_bus_for_each_i2cdev(bus, i2cdev) 1170 ndevs++; 1171 1172 defslvs = i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR, 1173 struct_size(defslvs, slaves, 1174 ndevs - 1)); 1175 if (!defslvs) 1176 return -ENOMEM; 1177 1178 defslvs->count = ndevs; 1179 defslvs->master.bcr = master->this->info.bcr; 1180 defslvs->master.dcr = master->this->info.dcr; 1181 defslvs->master.dyn_addr = master->this->info.dyn_addr << 1; 1182 defslvs->master.static_addr = I3C_BROADCAST_ADDR << 1; 1183 1184 desc = defslvs->slaves; 1185 i3c_bus_for_each_i2cdev(bus, i2cdev) { 1186 desc->lvr = i2cdev->lvr; 1187 desc->static_addr = i2cdev->addr << 1; 1188 desc++; 1189 } 1190 1191 i3c_bus_for_each_i3cdev(bus, i3cdev) { 1192 /* Skip the I3C dev representing this master. */ 1193 if (i3cdev == master->this) 1194 continue; 1195 1196 desc->bcr = i3cdev->info.bcr; 1197 desc->dcr = i3cdev->info.dcr; 1198 desc->dyn_addr = i3cdev->info.dyn_addr << 1; 1199 desc->static_addr = i3cdev->info.static_addr << 1; 1200 desc++; 1201 } 1202 1203 i3c_ccc_cmd_init(&cmd, false, I3C_CCC_DEFSLVS, &dest, 1); 1204 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1205 i3c_ccc_cmd_dest_cleanup(&dest); 1206 1207 return ret; 1208 } 1209 EXPORT_SYMBOL_GPL(i3c_master_defslvs_locked); 1210 1211 static int i3c_master_setda_locked(struct i3c_master_controller *master, 1212 u8 oldaddr, u8 newaddr, bool setdasa) 1213 { 1214 struct i3c_ccc_cmd_dest dest; 1215 struct i3c_ccc_setda *setda; 1216 struct i3c_ccc_cmd cmd; 1217 int ret; 1218 1219 if (!oldaddr || !newaddr) 1220 return -EINVAL; 1221 1222 setda = i3c_ccc_cmd_dest_init(&dest, oldaddr, sizeof(*setda)); 1223 if (!setda) 1224 return -ENOMEM; 1225 1226 setda->addr = newaddr << 1; 1227 i3c_ccc_cmd_init(&cmd, false, 1228 setdasa ? I3C_CCC_SETDASA : I3C_CCC_SETNEWDA, 1229 &dest, 1); 1230 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1231 i3c_ccc_cmd_dest_cleanup(&dest); 1232 1233 return ret; 1234 } 1235 1236 static int i3c_master_setdasa_locked(struct i3c_master_controller *master, 1237 u8 static_addr, u8 dyn_addr) 1238 { 1239 return i3c_master_setda_locked(master, static_addr, dyn_addr, true); 1240 } 1241 1242 static int i3c_master_setnewda_locked(struct i3c_master_controller *master, 1243 u8 oldaddr, u8 newaddr) 1244 { 1245 return i3c_master_setda_locked(master, oldaddr, newaddr, false); 1246 } 1247 1248 static int i3c_master_getmrl_locked(struct i3c_master_controller *master, 1249 struct i3c_device_info *info) 1250 { 1251 struct i3c_ccc_cmd_dest dest; 1252 struct i3c_ccc_mrl *mrl; 1253 struct i3c_ccc_cmd cmd; 1254 int ret; 1255 1256 mrl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mrl)); 1257 if (!mrl) 1258 return -ENOMEM; 1259 1260 /* 1261 * When the device does not have IBI payload GETMRL only returns 2 1262 * bytes of data. 1263 */ 1264 if (!(info->bcr & I3C_BCR_IBI_PAYLOAD)) 1265 dest.payload.len -= 1; 1266 1267 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMRL, &dest, 1); 1268 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1269 if (ret) 1270 goto out; 1271 1272 switch (dest.payload.len) { 1273 case 3: 1274 info->max_ibi_len = mrl->ibi_len; 1275 fallthrough; 1276 case 2: 1277 info->max_read_len = be16_to_cpu(mrl->read_len); 1278 break; 1279 default: 1280 ret = -EIO; 1281 goto out; 1282 } 1283 1284 out: 1285 i3c_ccc_cmd_dest_cleanup(&dest); 1286 1287 return ret; 1288 } 1289 1290 static int i3c_master_getmwl_locked(struct i3c_master_controller *master, 1291 struct i3c_device_info *info) 1292 { 1293 struct i3c_ccc_cmd_dest dest; 1294 struct i3c_ccc_mwl *mwl; 1295 struct i3c_ccc_cmd cmd; 1296 int ret; 1297 1298 mwl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mwl)); 1299 if (!mwl) 1300 return -ENOMEM; 1301 1302 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMWL, &dest, 1); 1303 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1304 if (ret) 1305 goto out; 1306 1307 if (dest.payload.len != sizeof(*mwl)) { 1308 ret = -EIO; 1309 goto out; 1310 } 1311 1312 info->max_write_len = be16_to_cpu(mwl->len); 1313 1314 out: 1315 i3c_ccc_cmd_dest_cleanup(&dest); 1316 1317 return ret; 1318 } 1319 1320 static int i3c_master_getmxds_locked(struct i3c_master_controller *master, 1321 struct i3c_device_info *info) 1322 { 1323 struct i3c_ccc_getmxds *getmaxds; 1324 struct i3c_ccc_cmd_dest dest; 1325 struct i3c_ccc_cmd cmd; 1326 int ret; 1327 1328 getmaxds = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, 1329 sizeof(*getmaxds)); 1330 if (!getmaxds) 1331 return -ENOMEM; 1332 1333 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMXDS, &dest, 1); 1334 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1335 if (ret) { 1336 /* 1337 * Retry when the device does not support max read turnaround 1338 * while expecting shorter length from this CCC command. 1339 */ 1340 dest.payload.len -= 3; 1341 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1342 if (ret) 1343 goto out; 1344 } 1345 1346 if (dest.payload.len != 2 && dest.payload.len != 5) { 1347 ret = -EIO; 1348 goto out; 1349 } 1350 1351 info->max_read_ds = getmaxds->maxrd; 1352 info->max_write_ds = getmaxds->maxwr; 1353 if (dest.payload.len == 5) 1354 info->max_read_turnaround = getmaxds->maxrdturn[0] | 1355 ((u32)getmaxds->maxrdturn[1] << 8) | 1356 ((u32)getmaxds->maxrdturn[2] << 16); 1357 1358 out: 1359 i3c_ccc_cmd_dest_cleanup(&dest); 1360 1361 return ret; 1362 } 1363 1364 static int i3c_master_gethdrcap_locked(struct i3c_master_controller *master, 1365 struct i3c_device_info *info) 1366 { 1367 struct i3c_ccc_gethdrcap *gethdrcap; 1368 struct i3c_ccc_cmd_dest dest; 1369 struct i3c_ccc_cmd cmd; 1370 int ret; 1371 1372 gethdrcap = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, 1373 sizeof(*gethdrcap)); 1374 if (!gethdrcap) 1375 return -ENOMEM; 1376 1377 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETHDRCAP, &dest, 1); 1378 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1379 if (ret) 1380 goto out; 1381 1382 if (dest.payload.len != 1) { 1383 ret = -EIO; 1384 goto out; 1385 } 1386 1387 info->hdr_cap = gethdrcap->modes; 1388 1389 out: 1390 i3c_ccc_cmd_dest_cleanup(&dest); 1391 1392 return ret; 1393 } 1394 1395 static int i3c_master_getpid_locked(struct i3c_master_controller *master, 1396 struct i3c_device_info *info) 1397 { 1398 struct i3c_ccc_getpid *getpid; 1399 struct i3c_ccc_cmd_dest dest; 1400 struct i3c_ccc_cmd cmd; 1401 int ret, i; 1402 1403 getpid = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getpid)); 1404 if (!getpid) 1405 return -ENOMEM; 1406 1407 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETPID, &dest, 1); 1408 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1409 if (ret) 1410 goto out; 1411 1412 info->pid = 0; 1413 for (i = 0; i < sizeof(getpid->pid); i++) { 1414 int sft = (sizeof(getpid->pid) - i - 1) * 8; 1415 1416 info->pid |= (u64)getpid->pid[i] << sft; 1417 } 1418 1419 out: 1420 i3c_ccc_cmd_dest_cleanup(&dest); 1421 1422 return ret; 1423 } 1424 1425 static int i3c_master_getbcr_locked(struct i3c_master_controller *master, 1426 struct i3c_device_info *info) 1427 { 1428 struct i3c_ccc_getbcr *getbcr; 1429 struct i3c_ccc_cmd_dest dest; 1430 struct i3c_ccc_cmd cmd; 1431 int ret; 1432 1433 getbcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getbcr)); 1434 if (!getbcr) 1435 return -ENOMEM; 1436 1437 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETBCR, &dest, 1); 1438 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1439 if (ret) 1440 goto out; 1441 1442 info->bcr = getbcr->bcr; 1443 1444 out: 1445 i3c_ccc_cmd_dest_cleanup(&dest); 1446 1447 return ret; 1448 } 1449 1450 static int i3c_master_getdcr_locked(struct i3c_master_controller *master, 1451 struct i3c_device_info *info) 1452 { 1453 struct i3c_ccc_getdcr *getdcr; 1454 struct i3c_ccc_cmd_dest dest; 1455 struct i3c_ccc_cmd cmd; 1456 int ret; 1457 1458 getdcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getdcr)); 1459 if (!getdcr) 1460 return -ENOMEM; 1461 1462 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETDCR, &dest, 1); 1463 ret = i3c_master_send_ccc_cmd_locked(master, &cmd); 1464 if (ret) 1465 goto out; 1466 1467 info->dcr = getdcr->dcr; 1468 1469 out: 1470 i3c_ccc_cmd_dest_cleanup(&dest); 1471 1472 return ret; 1473 } 1474 1475 static int i3c_master_retrieve_dev_info(struct i3c_dev_desc *dev) 1476 { 1477 struct i3c_master_controller *master = i3c_dev_get_master(dev); 1478 enum i3c_addr_slot_status slot_status; 1479 int ret; 1480 1481 if (!dev->info.dyn_addr) 1482 return -EINVAL; 1483 1484 slot_status = i3c_bus_get_addr_slot_status(&master->bus, 1485 dev->info.dyn_addr); 1486 if (slot_status == I3C_ADDR_SLOT_RSVD || 1487 slot_status == I3C_ADDR_SLOT_I2C_DEV) 1488 return -EINVAL; 1489 1490 ret = i3c_master_getpid_locked(master, &dev->info); 1491 if (ret) 1492 return ret; 1493 1494 ret = i3c_master_getbcr_locked(master, &dev->info); 1495 if (ret) 1496 return ret; 1497 1498 ret = i3c_master_getdcr_locked(master, &dev->info); 1499 if (ret) 1500 return ret; 1501 1502 if (dev->info.bcr & I3C_BCR_MAX_DATA_SPEED_LIM) { 1503 ret = i3c_master_getmxds_locked(master, &dev->info); 1504 if (ret) 1505 return ret; 1506 } 1507 1508 if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD) 1509 dev->info.max_ibi_len = 1; 1510 1511 i3c_master_getmrl_locked(master, &dev->info); 1512 i3c_master_getmwl_locked(master, &dev->info); 1513 1514 if (dev->info.bcr & I3C_BCR_HDR_CAP) { 1515 ret = i3c_master_gethdrcap_locked(master, &dev->info); 1516 if (ret && ret != -EOPNOTSUPP) 1517 return ret; 1518 } 1519 1520 return 0; 1521 } 1522 1523 static void i3c_master_put_i3c_addrs(struct i3c_dev_desc *dev) 1524 { 1525 struct i3c_master_controller *master = i3c_dev_get_master(dev); 1526 1527 if (dev->info.static_addr) 1528 i3c_bus_set_addr_slot_status(&master->bus, 1529 dev->info.static_addr, 1530 I3C_ADDR_SLOT_FREE); 1531 1532 if (dev->info.dyn_addr) 1533 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr, 1534 I3C_ADDR_SLOT_FREE); 1535 1536 if (dev->boardinfo && dev->boardinfo->init_dyn_addr) 1537 i3c_bus_set_addr_slot_status(&master->bus, dev->boardinfo->init_dyn_addr, 1538 I3C_ADDR_SLOT_FREE); 1539 } 1540 1541 static int i3c_master_get_i3c_addrs(struct i3c_dev_desc *dev) 1542 { 1543 struct i3c_master_controller *master = i3c_dev_get_master(dev); 1544 enum i3c_addr_slot_status status; 1545 1546 if (!dev->info.static_addr && !dev->info.dyn_addr) 1547 return 0; 1548 1549 if (dev->info.static_addr) { 1550 status = i3c_bus_get_addr_slot_status(&master->bus, 1551 dev->info.static_addr); 1552 /* Since static address and assigned dynamic address can be 1553 * equal, allow this case to pass. 1554 */ 1555 if (status != I3C_ADDR_SLOT_FREE && 1556 dev->info.static_addr != dev->boardinfo->init_dyn_addr) 1557 return -EBUSY; 1558 1559 i3c_bus_set_addr_slot_status(&master->bus, 1560 dev->info.static_addr, 1561 I3C_ADDR_SLOT_I3C_DEV); 1562 } 1563 1564 /* 1565 * ->init_dyn_addr should have been reserved before that, so, if we're 1566 * trying to apply a pre-reserved dynamic address, we should not try 1567 * to reserve the address slot a second time. 1568 */ 1569 if (dev->info.dyn_addr && 1570 (!dev->boardinfo || 1571 dev->boardinfo->init_dyn_addr != dev->info.dyn_addr)) { 1572 status = i3c_bus_get_addr_slot_status(&master->bus, 1573 dev->info.dyn_addr); 1574 if (status != I3C_ADDR_SLOT_FREE) 1575 goto err_release_static_addr; 1576 1577 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr, 1578 I3C_ADDR_SLOT_I3C_DEV); 1579 } 1580 1581 return 0; 1582 1583 err_release_static_addr: 1584 if (dev->info.static_addr) 1585 i3c_bus_set_addr_slot_status(&master->bus, 1586 dev->info.static_addr, 1587 I3C_ADDR_SLOT_FREE); 1588 1589 return -EBUSY; 1590 } 1591 1592 static int i3c_master_attach_i3c_dev(struct i3c_master_controller *master, 1593 struct i3c_dev_desc *dev) 1594 { 1595 int ret; 1596 1597 /* 1598 * We don't attach devices to the controller until they are 1599 * addressable on the bus. 1600 */ 1601 if (!dev->info.static_addr && !dev->info.dyn_addr) 1602 return 0; 1603 1604 ret = i3c_master_get_i3c_addrs(dev); 1605 if (ret) 1606 return ret; 1607 1608 /* Do not attach the master device itself. */ 1609 if (master->this != dev && master->ops->attach_i3c_dev) { 1610 ret = master->ops->attach_i3c_dev(dev); 1611 if (ret) { 1612 i3c_master_put_i3c_addrs(dev); 1613 return ret; 1614 } 1615 } 1616 1617 list_add_tail(&dev->common.node, &master->bus.devs.i3c); 1618 1619 return 0; 1620 } 1621 1622 static int i3c_master_reattach_i3c_dev(struct i3c_dev_desc *dev, 1623 u8 old_dyn_addr) 1624 { 1625 struct i3c_master_controller *master = i3c_dev_get_master(dev); 1626 int ret; 1627 1628 if (dev->info.dyn_addr != old_dyn_addr) { 1629 i3c_bus_set_addr_slot_status(&master->bus, 1630 dev->info.dyn_addr, 1631 I3C_ADDR_SLOT_I3C_DEV); 1632 if (old_dyn_addr) 1633 i3c_bus_set_addr_slot_status(&master->bus, old_dyn_addr, 1634 I3C_ADDR_SLOT_FREE); 1635 } 1636 1637 if (master->ops->reattach_i3c_dev) { 1638 ret = master->ops->reattach_i3c_dev(dev, old_dyn_addr); 1639 if (ret) { 1640 i3c_master_put_i3c_addrs(dev); 1641 return ret; 1642 } 1643 } 1644 1645 return 0; 1646 } 1647 1648 static void i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev) 1649 { 1650 struct i3c_master_controller *master = i3c_dev_get_master(dev); 1651 1652 /* Do not detach the master device itself. */ 1653 if (master->this != dev && master->ops->detach_i3c_dev) 1654 master->ops->detach_i3c_dev(dev); 1655 1656 i3c_master_put_i3c_addrs(dev); 1657 list_del(&dev->common.node); 1658 } 1659 1660 static int i3c_master_attach_i2c_dev(struct i3c_master_controller *master, 1661 struct i2c_dev_desc *dev) 1662 { 1663 int ret; 1664 1665 if (master->ops->attach_i2c_dev) { 1666 ret = master->ops->attach_i2c_dev(dev); 1667 if (ret) 1668 return ret; 1669 } 1670 1671 list_add_tail(&dev->common.node, &master->bus.devs.i2c); 1672 1673 return 0; 1674 } 1675 1676 static void i3c_master_detach_i2c_dev(struct i2c_dev_desc *dev) 1677 { 1678 struct i3c_master_controller *master = i2c_dev_get_master(dev); 1679 1680 list_del(&dev->common.node); 1681 1682 if (master->ops->detach_i2c_dev) 1683 master->ops->detach_i2c_dev(dev); 1684 } 1685 1686 static int i3c_master_early_i3c_dev_add(struct i3c_master_controller *master, 1687 struct i3c_dev_boardinfo *boardinfo) 1688 { 1689 struct i3c_device_info info = { 1690 .static_addr = boardinfo->static_addr, 1691 .pid = boardinfo->pid, 1692 }; 1693 struct i3c_dev_desc *i3cdev; 1694 int ret; 1695 1696 i3cdev = i3c_master_alloc_i3c_dev(master, &info); 1697 if (IS_ERR(i3cdev)) 1698 return -ENOMEM; 1699 1700 i3cdev->boardinfo = boardinfo; 1701 1702 ret = i3c_master_attach_i3c_dev(master, i3cdev); 1703 if (ret) 1704 goto err_free_dev; 1705 1706 ret = i3c_master_setdasa_locked(master, i3cdev->info.static_addr, 1707 i3cdev->boardinfo->init_dyn_addr); 1708 if (ret) 1709 goto err_detach_dev; 1710 1711 i3cdev->info.dyn_addr = i3cdev->boardinfo->init_dyn_addr; 1712 ret = i3c_master_reattach_i3c_dev(i3cdev, 0); 1713 if (ret) 1714 goto err_rstdaa; 1715 1716 ret = i3c_master_retrieve_dev_info(i3cdev); 1717 if (ret) 1718 goto err_rstdaa; 1719 1720 return 0; 1721 1722 err_rstdaa: 1723 i3c_master_rstdaa_locked(master, i3cdev->boardinfo->init_dyn_addr); 1724 err_detach_dev: 1725 i3c_master_detach_i3c_dev(i3cdev); 1726 err_free_dev: 1727 i3c_master_free_i3c_dev(i3cdev); 1728 1729 return ret; 1730 } 1731 1732 static void 1733 i3c_master_register_new_i3c_devs(struct i3c_master_controller *master) 1734 { 1735 struct i3c_dev_desc *desc; 1736 int ret; 1737 1738 if (!master->init_done) 1739 return; 1740 1741 i3c_bus_for_each_i3cdev(&master->bus, desc) { 1742 if (desc->dev || !desc->info.dyn_addr || desc == master->this) 1743 continue; 1744 1745 desc->dev = kzalloc_obj(*desc->dev, GFP_KERNEL); 1746 if (!desc->dev) 1747 continue; 1748 1749 desc->dev->bus = &master->bus; 1750 desc->dev->desc = desc; 1751 desc->dev->dev.parent = &master->dev; 1752 desc->dev->dev.type = &i3c_device_type; 1753 desc->dev->dev.bus = &i3c_bus_type; 1754 desc->dev->dev.release = i3c_device_release; 1755 dev_set_name(&desc->dev->dev, "%d-%llx", master->bus.id, 1756 desc->info.pid); 1757 1758 if (desc->boardinfo) 1759 desc->dev->dev.of_node = desc->boardinfo->of_node; 1760 1761 ret = device_register(&desc->dev->dev); 1762 if (ret) { 1763 dev_err(&master->dev, 1764 "Failed to add I3C device (err = %d)\n", ret); 1765 put_device(&desc->dev->dev); 1766 } 1767 } 1768 } 1769 1770 /** 1771 * i3c_master_do_daa_ext() - Dynamic Address Assignment (extended version) 1772 * @master: controller 1773 * @rstdaa: whether to first perform Reset of Dynamic Addresses (RSTDAA) 1774 * 1775 * Perform Dynamic Address Assignment with optional support for System 1776 * Hibernation (@rstdaa is true). 1777 * 1778 * After System Hibernation, Dynamic Addresses can have been reassigned at boot 1779 * time to different values. A simple strategy is followed to handle that. 1780 * Perform a Reset of Dynamic Addresses (RSTDAA) followed by the normal DAA 1781 * procedure which has provision for reassigning addresses that differ from the 1782 * previously recorded addresses. 1783 * 1784 * Return: a 0 in case of success, an negative error code otherwise. 1785 */ 1786 int i3c_master_do_daa_ext(struct i3c_master_controller *master, bool rstdaa) 1787 { 1788 int rstret = 0; 1789 int ret; 1790 1791 ret = i3c_master_rpm_get(master); 1792 if (ret) 1793 return ret; 1794 1795 i3c_bus_maintenance_lock(&master->bus); 1796 1797 if (rstdaa) { 1798 rstret = i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR); 1799 if (rstret == I3C_ERROR_M2) 1800 rstret = 0; 1801 } 1802 1803 ret = master->ops->do_daa(master); 1804 1805 i3c_bus_maintenance_unlock(&master->bus); 1806 1807 if (ret) 1808 goto out; 1809 1810 i3c_bus_normaluse_lock(&master->bus); 1811 i3c_master_register_new_i3c_devs(master); 1812 i3c_bus_normaluse_unlock(&master->bus); 1813 out: 1814 i3c_master_rpm_put(master); 1815 1816 return rstret ?: ret; 1817 } 1818 EXPORT_SYMBOL_GPL(i3c_master_do_daa_ext); 1819 1820 /** 1821 * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment) 1822 * @master: master doing the DAA 1823 * 1824 * This function instantiates I3C device objects and adds them to the 1825 * I3C device list. All device information is automatically retrieved using 1826 * standard CCC commands. 1827 * 1828 * Return: a 0 in case of success, an negative error code otherwise. 1829 */ 1830 int i3c_master_do_daa(struct i3c_master_controller *master) 1831 { 1832 return i3c_master_do_daa_ext(master, false); 1833 } 1834 EXPORT_SYMBOL_GPL(i3c_master_do_daa); 1835 1836 /** 1837 * i3c_master_dma_map_single() - Map buffer for single DMA transfer 1838 * @dev: device object of a device doing DMA 1839 * @buf: destination/source buffer for DMA 1840 * @len: length of transfer 1841 * @force_bounce: true, force to use a bounce buffer, 1842 * false, function will auto check is a bounce buffer required 1843 * @dir: DMA direction 1844 * 1845 * Map buffer for a DMA transfer and allocate a bounce buffer if required. 1846 * 1847 * Return: I3C DMA transfer descriptor or NULL in case of error. 1848 */ 1849 struct i3c_dma *i3c_master_dma_map_single(struct device *dev, void *buf, 1850 size_t len, bool force_bounce, enum dma_data_direction dir) 1851 { 1852 void *bounce __free(kfree) = NULL; 1853 void *dma_buf = buf; 1854 1855 struct i3c_dma *dma_xfer __free(kfree) = kzalloc_obj(*dma_xfer, 1856 GFP_KERNEL); 1857 if (!dma_xfer) 1858 return NULL; 1859 1860 dma_xfer->dev = dev; 1861 dma_xfer->buf = buf; 1862 dma_xfer->dir = dir; 1863 dma_xfer->len = len; 1864 dma_xfer->map_len = len; 1865 1866 if (is_vmalloc_addr(buf)) 1867 force_bounce = true; 1868 1869 if (force_bounce) { 1870 dma_xfer->map_len = ALIGN(len, cache_line_size()); 1871 if (dir == DMA_FROM_DEVICE) 1872 bounce = kzalloc(dma_xfer->map_len, GFP_KERNEL); 1873 else 1874 bounce = kmemdup(buf, dma_xfer->map_len, GFP_KERNEL); 1875 if (!bounce) 1876 return NULL; 1877 dma_buf = bounce; 1878 } 1879 1880 dma_xfer->addr = dma_map_single(dev, dma_buf, dma_xfer->map_len, dir); 1881 if (dma_mapping_error(dev, dma_xfer->addr)) 1882 return NULL; 1883 1884 dma_xfer->bounce_buf = no_free_ptr(bounce); 1885 return no_free_ptr(dma_xfer); 1886 } 1887 EXPORT_SYMBOL_GPL(i3c_master_dma_map_single); 1888 1889 /** 1890 * i3c_master_dma_unmap_single() - Unmap buffer after DMA 1891 * @dma_xfer: DMA transfer and mapping descriptor 1892 * 1893 * Unmap buffer and cleanup DMA transfer descriptor. 1894 */ 1895 void i3c_master_dma_unmap_single(struct i3c_dma *dma_xfer) 1896 { 1897 dma_unmap_single(dma_xfer->dev, dma_xfer->addr, 1898 dma_xfer->map_len, dma_xfer->dir); 1899 if (dma_xfer->bounce_buf) { 1900 if (dma_xfer->dir == DMA_FROM_DEVICE) 1901 memcpy(dma_xfer->buf, dma_xfer->bounce_buf, 1902 dma_xfer->len); 1903 kfree(dma_xfer->bounce_buf); 1904 } 1905 kfree(dma_xfer); 1906 } 1907 EXPORT_SYMBOL_GPL(i3c_master_dma_unmap_single); 1908 1909 /** 1910 * i3c_master_set_info() - set master device information 1911 * @master: master used to send frames on the bus 1912 * @info: I3C device information 1913 * 1914 * Set master device info. This should be called from 1915 * &i3c_master_controller_ops->bus_init(). 1916 * 1917 * Not all &i3c_device_info fields are meaningful for a master device. 1918 * Here is a list of fields that should be properly filled: 1919 * 1920 * - &i3c_device_info->dyn_addr 1921 * - &i3c_device_info->bcr 1922 * - &i3c_device_info->dcr 1923 * - &i3c_device_info->pid 1924 * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in 1925 * &i3c_device_info->bcr 1926 * 1927 * This function must be called with the bus lock held in maintenance mode. 1928 * 1929 * Return: 0 if @info contains valid information (not every piece of 1930 * information can be checked, but we can at least make sure @info->dyn_addr 1931 * and @info->bcr are correct), -EINVAL otherwise. 1932 */ 1933 int i3c_master_set_info(struct i3c_master_controller *master, 1934 const struct i3c_device_info *info) 1935 { 1936 struct i3c_dev_desc *i3cdev; 1937 int ret; 1938 1939 if (!i3c_bus_dev_addr_is_avail(&master->bus, info->dyn_addr)) 1940 return -EINVAL; 1941 1942 if (I3C_BCR_DEVICE_ROLE(info->bcr) == I3C_BCR_I3C_MASTER && 1943 master->secondary) 1944 return -EINVAL; 1945 1946 if (master->this) 1947 return -EINVAL; 1948 1949 i3cdev = i3c_master_alloc_i3c_dev(master, info); 1950 if (IS_ERR(i3cdev)) 1951 return PTR_ERR(i3cdev); 1952 1953 master->this = i3cdev; 1954 master->bus.cur_master = master->this; 1955 1956 ret = i3c_master_attach_i3c_dev(master, i3cdev); 1957 if (ret) 1958 goto err_free_dev; 1959 1960 return 0; 1961 1962 err_free_dev: 1963 i3c_master_free_i3c_dev(i3cdev); 1964 1965 return ret; 1966 } 1967 EXPORT_SYMBOL_GPL(i3c_master_set_info); 1968 1969 static void i3c_master_detach_free_devs(struct i3c_master_controller *master) 1970 { 1971 struct i3c_dev_desc *i3cdev, *i3ctmp; 1972 struct i2c_dev_desc *i2cdev, *i2ctmp; 1973 1974 list_for_each_entry_safe(i3cdev, i3ctmp, &master->bus.devs.i3c, 1975 common.node) { 1976 i3c_master_detach_i3c_dev(i3cdev); 1977 1978 if (i3cdev->boardinfo && i3cdev->boardinfo->init_dyn_addr) 1979 i3c_bus_set_addr_slot_status(&master->bus, 1980 i3cdev->boardinfo->init_dyn_addr, 1981 I3C_ADDR_SLOT_FREE); 1982 1983 i3c_master_free_i3c_dev(i3cdev); 1984 } 1985 1986 list_for_each_entry_safe(i2cdev, i2ctmp, &master->bus.devs.i2c, 1987 common.node) { 1988 i3c_master_detach_i2c_dev(i2cdev); 1989 i3c_bus_set_addr_slot_status(&master->bus, 1990 i2cdev->addr, 1991 I3C_ADDR_SLOT_FREE); 1992 i3c_master_free_i2c_dev(i2cdev); 1993 } 1994 } 1995 1996 /** 1997 * i3c_master_bus_init() - initialize an I3C bus 1998 * @master: main master initializing the bus 1999 * 2000 * This function is following all initialisation steps described in the I3C 2001 * specification: 2002 * 2003 * 1. Attach I2C devs to the master so that the master can fill its internal 2004 * device table appropriately 2005 * 2006 * 2. Call &i3c_master_controller_ops->bus_init() method to initialize 2007 * the master controller. That's usually where the bus mode is selected 2008 * (pure bus or mixed fast/slow bus) 2009 * 2010 * 3. Instruct all devices on the bus to drop their dynamic address. This is 2011 * particularly important when the bus was previously configured by someone 2012 * else (for example the bootloader) 2013 * 2014 * 4. Disable all slave events. 2015 * 2016 * 5. Reserve address slots for I3C devices with init_dyn_addr. And if devices 2017 * also have static_addr, try to pre-assign dynamic addresses requested by 2018 * the FW with SETDASA and attach corresponding statically defined I3C 2019 * devices to the master. 2020 * 2021 * 6. Do a DAA (Dynamic Address Assignment) to assign dynamic addresses to all 2022 * remaining I3C devices 2023 * 2024 * Once this is done, all I3C and I2C devices should be usable. 2025 * 2026 * Return: a 0 in case of success, an negative error code otherwise. 2027 */ 2028 static int i3c_master_bus_init(struct i3c_master_controller *master) 2029 { 2030 enum i3c_addr_slot_status status; 2031 struct i2c_dev_boardinfo *i2cboardinfo; 2032 struct i3c_dev_boardinfo *i3cboardinfo; 2033 struct i2c_dev_desc *i2cdev; 2034 int ret; 2035 2036 /* 2037 * First attach all devices with static definitions provided by the 2038 * FW. 2039 */ 2040 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) { 2041 status = i3c_bus_get_addr_slot_status(&master->bus, 2042 i2cboardinfo->base.addr); 2043 if (status != I3C_ADDR_SLOT_FREE) { 2044 ret = -EBUSY; 2045 goto err_detach_devs; 2046 } 2047 2048 i3c_bus_set_addr_slot_status(&master->bus, 2049 i2cboardinfo->base.addr, 2050 I3C_ADDR_SLOT_I2C_DEV); 2051 2052 i2cdev = i3c_master_alloc_i2c_dev(master, 2053 i2cboardinfo->base.addr, 2054 i2cboardinfo->lvr); 2055 if (IS_ERR(i2cdev)) { 2056 ret = PTR_ERR(i2cdev); 2057 goto err_detach_devs; 2058 } 2059 2060 ret = i3c_master_attach_i2c_dev(master, i2cdev); 2061 if (ret) { 2062 i3c_master_free_i2c_dev(i2cdev); 2063 goto err_detach_devs; 2064 } 2065 } 2066 2067 /* 2068 * Now execute the controller specific ->bus_init() routine, which 2069 * might configure its internal logic to match the bus limitations. 2070 */ 2071 ret = master->ops->bus_init(master); 2072 if (ret) 2073 goto err_detach_devs; 2074 2075 /* 2076 * The master device should have been instantiated in ->bus_init(), 2077 * complain if this was not the case. 2078 */ 2079 if (!master->this) { 2080 dev_err(&master->dev, 2081 "master_set_info() was not called in ->bus_init()\n"); 2082 ret = -EINVAL; 2083 goto err_bus_cleanup; 2084 } 2085 2086 if (master->ops->set_speed) { 2087 ret = master->ops->set_speed(master, I3C_OPEN_DRAIN_SLOW_SPEED); 2088 if (ret) 2089 goto err_bus_cleanup; 2090 } 2091 2092 /* 2093 * Reset all dynamic address that may have been assigned before 2094 * (assigned by the bootloader for example). 2095 */ 2096 ret = i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR); 2097 if (ret && ret != I3C_ERROR_M2) 2098 goto err_bus_cleanup; 2099 2100 if (master->ops->set_speed) { 2101 ret = master->ops->set_speed(master, I3C_OPEN_DRAIN_NORMAL_SPEED); 2102 if (ret) 2103 goto err_bus_cleanup; 2104 } 2105 2106 /* Disable all slave events before starting DAA. */ 2107 ret = i3c_master_disec_locked(master, I3C_BROADCAST_ADDR, 2108 I3C_CCC_EVENT_SIR | I3C_CCC_EVENT_MR | 2109 I3C_CCC_EVENT_HJ); 2110 if (ret && ret != I3C_ERROR_M2) 2111 goto err_bus_cleanup; 2112 2113 /* 2114 * Reserve init_dyn_addr first, and then try to pre-assign dynamic 2115 * address and retrieve device information if needed. 2116 * In case pre-assign dynamic address fails, setting dynamic address to 2117 * the requested init_dyn_addr is retried after DAA is done in 2118 * i3c_master_add_i3c_dev_locked(). 2119 */ 2120 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) { 2121 2122 /* 2123 * We don't reserve a dynamic address for devices that 2124 * don't explicitly request one. 2125 */ 2126 if (!i3cboardinfo->init_dyn_addr) 2127 continue; 2128 2129 ret = i3c_bus_get_addr_slot_status(&master->bus, 2130 i3cboardinfo->init_dyn_addr); 2131 if (ret != I3C_ADDR_SLOT_FREE) { 2132 ret = -EBUSY; 2133 goto err_rstdaa; 2134 } 2135 2136 /* Do not mark as occupied until real device exist in bus */ 2137 i3c_bus_set_addr_slot_status_mask(&master->bus, 2138 i3cboardinfo->init_dyn_addr, 2139 I3C_ADDR_SLOT_EXT_DESIRED, 2140 I3C_ADDR_SLOT_EXT_STATUS_MASK); 2141 2142 /* 2143 * Only try to create/attach devices that have a static 2144 * address. Other devices will be created/attached when 2145 * DAA happens, and the requested dynamic address will 2146 * be set using SETNEWDA once those devices become 2147 * addressable. 2148 */ 2149 2150 if (i3cboardinfo->static_addr) 2151 i3c_master_early_i3c_dev_add(master, i3cboardinfo); 2152 } 2153 2154 ret = i3c_master_do_daa(master); 2155 if (ret) 2156 goto err_rstdaa; 2157 2158 return 0; 2159 2160 err_rstdaa: 2161 i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR); 2162 2163 err_bus_cleanup: 2164 if (master->ops->bus_cleanup) 2165 master->ops->bus_cleanup(master); 2166 2167 err_detach_devs: 2168 i3c_master_detach_free_devs(master); 2169 2170 return ret; 2171 } 2172 2173 static void i3c_master_bus_cleanup(struct i3c_master_controller *master) 2174 { 2175 if (master->ops->bus_cleanup) { 2176 int ret = i3c_master_rpm_get(master); 2177 2178 if (ret) { 2179 dev_err(&master->dev, 2180 "runtime resume error: master bus_cleanup() not done\n"); 2181 } else { 2182 master->ops->bus_cleanup(master); 2183 i3c_master_rpm_put(master); 2184 } 2185 } 2186 2187 i3c_master_detach_free_devs(master); 2188 } 2189 2190 static void i3c_master_attach_boardinfo(struct i3c_dev_desc *i3cdev) 2191 { 2192 struct i3c_master_controller *master = i3cdev->common.master; 2193 struct i3c_dev_boardinfo *i3cboardinfo; 2194 2195 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) { 2196 if (i3cdev->info.pid != i3cboardinfo->pid) 2197 continue; 2198 2199 i3cdev->boardinfo = i3cboardinfo; 2200 i3cdev->info.static_addr = i3cboardinfo->static_addr; 2201 return; 2202 } 2203 } 2204 2205 static struct i3c_dev_desc * 2206 i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc *refdev) 2207 { 2208 struct i3c_master_controller *master = i3c_dev_get_master(refdev); 2209 struct i3c_dev_desc *i3cdev; 2210 2211 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) { 2212 if (i3cdev != refdev && i3cdev->info.pid == refdev->info.pid) 2213 return i3cdev; 2214 } 2215 2216 return NULL; 2217 } 2218 2219 /** 2220 * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus 2221 * @master: master used to send frames on the bus 2222 * @addr: I3C slave dynamic address assigned to the device 2223 * 2224 * This function is instantiating an I3C device object and adding it to the 2225 * I3C device list. All device information are automatically retrieved using 2226 * standard CCC commands. 2227 * 2228 * The I3C device object is returned in case the master wants to attach 2229 * private data to it using i3c_dev_set_master_data(). 2230 * 2231 * This function must be called with the bus lock held in write mode. 2232 * 2233 * Return: a 0 in case of success, an negative error code otherwise. 2234 */ 2235 int i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master, 2236 u8 addr) 2237 { 2238 struct i3c_device_info info = { .dyn_addr = addr }; 2239 struct i3c_dev_desc *newdev, *olddev; 2240 u8 old_dyn_addr = addr, expected_dyn_addr; 2241 struct i3c_ibi_setup ibireq = { }; 2242 bool enable_ibi = false; 2243 int ret; 2244 2245 if (!master) 2246 return -EINVAL; 2247 2248 newdev = i3c_master_alloc_i3c_dev(master, &info); 2249 if (IS_ERR(newdev)) 2250 return PTR_ERR(newdev); 2251 2252 ret = i3c_master_attach_i3c_dev(master, newdev); 2253 if (ret) 2254 goto err_free_dev; 2255 2256 ret = i3c_master_retrieve_dev_info(newdev); 2257 if (ret) 2258 goto err_detach_dev; 2259 2260 i3c_master_attach_boardinfo(newdev); 2261 2262 olddev = i3c_master_search_i3c_dev_duplicate(newdev); 2263 if (olddev) { 2264 newdev->dev = olddev->dev; 2265 if (newdev->dev) 2266 newdev->dev->desc = newdev; 2267 2268 /* 2269 * We need to restore the IBI state too, so let's save the 2270 * IBI information and try to restore them after olddev has 2271 * been detached+released and its IBI has been stopped and 2272 * the associated resources have been freed. 2273 */ 2274 mutex_lock(&olddev->ibi_lock); 2275 if (olddev->ibi) { 2276 ibireq.handler = olddev->ibi->handler; 2277 ibireq.max_payload_len = olddev->ibi->max_payload_len; 2278 ibireq.num_slots = olddev->ibi->num_slots; 2279 2280 if (olddev->ibi->enabled) 2281 enable_ibi = true; 2282 /* 2283 * The olddev should not receive any commands on the 2284 * i3c bus as it does not exist and has been assigned 2285 * a new address. This will result in NACK or timeout. 2286 * So, update the olddev->ibi->enabled flag to false 2287 * to avoid DISEC with OldAddr. 2288 */ 2289 olddev->ibi->enabled = false; 2290 i3c_dev_free_ibi_locked(olddev); 2291 } 2292 mutex_unlock(&olddev->ibi_lock); 2293 2294 old_dyn_addr = olddev->info.dyn_addr; 2295 2296 i3c_master_detach_i3c_dev(olddev); 2297 i3c_master_free_i3c_dev(olddev); 2298 } 2299 2300 /* 2301 * Depending on our previous state, the expected dynamic address might 2302 * differ: 2303 * - if the device already had a dynamic address assigned, let's try to 2304 * re-apply this one 2305 * - if the device did not have a dynamic address and the firmware 2306 * requested a specific address, pick this one 2307 * - in any other case, keep the address automatically assigned by the 2308 * master 2309 */ 2310 if (old_dyn_addr && old_dyn_addr != newdev->info.dyn_addr) 2311 expected_dyn_addr = old_dyn_addr; 2312 else if (newdev->boardinfo && newdev->boardinfo->init_dyn_addr) 2313 expected_dyn_addr = newdev->boardinfo->init_dyn_addr; 2314 else 2315 expected_dyn_addr = newdev->info.dyn_addr; 2316 2317 if (newdev->info.dyn_addr != expected_dyn_addr && 2318 i3c_bus_get_addr_slot_status(&master->bus, expected_dyn_addr) == I3C_ADDR_SLOT_FREE) { 2319 /* 2320 * Try to apply the expected dynamic address. If it fails, keep 2321 * the address assigned by the master. 2322 */ 2323 ret = i3c_master_setnewda_locked(master, 2324 newdev->info.dyn_addr, 2325 expected_dyn_addr); 2326 if (!ret) { 2327 old_dyn_addr = newdev->info.dyn_addr; 2328 newdev->info.dyn_addr = expected_dyn_addr; 2329 i3c_master_reattach_i3c_dev(newdev, old_dyn_addr); 2330 } else { 2331 dev_err(&master->dev, 2332 "Failed to assign reserved/old address to device %d%llx", 2333 master->bus.id, newdev->info.pid); 2334 } 2335 } 2336 2337 /* 2338 * Now is time to try to restore the IBI setup. If we're lucky, 2339 * everything works as before, otherwise, all we can do is complain. 2340 * FIXME: maybe we should add callback to inform the driver that it 2341 * should request the IBI again instead of trying to hide that from 2342 * him. 2343 */ 2344 if (ibireq.handler) { 2345 mutex_lock(&newdev->ibi_lock); 2346 ret = i3c_dev_request_ibi_locked(newdev, &ibireq); 2347 if (ret) { 2348 dev_err(&master->dev, 2349 "Failed to request IBI on device %d-%llx", 2350 master->bus.id, newdev->info.pid); 2351 } else if (enable_ibi) { 2352 ret = i3c_dev_enable_ibi_locked(newdev); 2353 if (ret) 2354 dev_err(&master->dev, 2355 "Failed to re-enable IBI on device %d-%llx", 2356 master->bus.id, newdev->info.pid); 2357 } 2358 mutex_unlock(&newdev->ibi_lock); 2359 } 2360 2361 return 0; 2362 2363 err_detach_dev: 2364 if (newdev->dev && newdev->dev->desc) 2365 newdev->dev->desc = NULL; 2366 2367 i3c_master_detach_i3c_dev(newdev); 2368 2369 err_free_dev: 2370 i3c_master_free_i3c_dev(newdev); 2371 2372 return ret; 2373 } 2374 EXPORT_SYMBOL_GPL(i3c_master_add_i3c_dev_locked); 2375 2376 #define OF_I3C_REG1_IS_I2C_DEV BIT(31) 2377 2378 static int 2379 of_i3c_master_add_i2c_boardinfo(struct i3c_master_controller *master, 2380 struct device_node *node, u32 *reg) 2381 { 2382 struct i2c_dev_boardinfo *boardinfo; 2383 struct device *dev = &master->dev; 2384 int ret; 2385 2386 boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL); 2387 if (!boardinfo) 2388 return -ENOMEM; 2389 2390 ret = of_i2c_get_board_info(dev, node, &boardinfo->base); 2391 if (ret) 2392 return ret; 2393 2394 /* 2395 * The I3C Specification does not clearly say I2C devices with 10-bit 2396 * address are supported. These devices can't be passed properly through 2397 * DEFSLVS command. 2398 */ 2399 if (boardinfo->base.flags & I2C_CLIENT_TEN) { 2400 dev_err(dev, "I2C device with 10 bit address not supported."); 2401 return -EOPNOTSUPP; 2402 } 2403 2404 /* LVR is encoded in reg[2]. */ 2405 boardinfo->lvr = reg[2]; 2406 2407 list_add_tail(&boardinfo->node, &master->boardinfo.i2c); 2408 of_node_get(node); 2409 2410 return 0; 2411 } 2412 2413 static int 2414 of_i3c_master_add_i3c_boardinfo(struct i3c_master_controller *master, 2415 struct device_node *node, u32 *reg) 2416 { 2417 struct i3c_dev_boardinfo *boardinfo; 2418 struct device *dev = &master->dev; 2419 enum i3c_addr_slot_status addrstatus; 2420 u32 init_dyn_addr = 0; 2421 2422 boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL); 2423 if (!boardinfo) 2424 return -ENOMEM; 2425 2426 if (reg[0]) { 2427 if (reg[0] > I3C_MAX_ADDR) 2428 return -EINVAL; 2429 2430 addrstatus = i3c_bus_get_addr_slot_status(&master->bus, 2431 reg[0]); 2432 if (addrstatus != I3C_ADDR_SLOT_FREE) 2433 return -EINVAL; 2434 } 2435 2436 boardinfo->static_addr = reg[0]; 2437 2438 if (!of_property_read_u32(node, "assigned-address", &init_dyn_addr)) { 2439 if (init_dyn_addr > I3C_MAX_ADDR) 2440 return -EINVAL; 2441 2442 addrstatus = i3c_bus_get_addr_slot_status(&master->bus, 2443 init_dyn_addr); 2444 if (addrstatus != I3C_ADDR_SLOT_FREE) 2445 return -EINVAL; 2446 } 2447 2448 boardinfo->pid = ((u64)reg[1] << 32) | reg[2]; 2449 2450 if ((boardinfo->pid & GENMASK_ULL(63, 48)) || 2451 I3C_PID_RND_LOWER_32BITS(boardinfo->pid)) 2452 return -EINVAL; 2453 2454 boardinfo->init_dyn_addr = init_dyn_addr; 2455 boardinfo->of_node = of_node_get(node); 2456 list_add_tail(&boardinfo->node, &master->boardinfo.i3c); 2457 2458 return 0; 2459 } 2460 2461 static int of_i3c_master_add_dev(struct i3c_master_controller *master, 2462 struct device_node *node) 2463 { 2464 u32 reg[3]; 2465 int ret; 2466 2467 if (!master) 2468 return -EINVAL; 2469 2470 ret = of_property_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg)); 2471 if (ret) 2472 return ret; 2473 2474 /* 2475 * The manufacturer ID can't be 0. If reg[1] == 0 that means we're 2476 * dealing with an I2C device. 2477 */ 2478 if (!reg[1]) 2479 ret = of_i3c_master_add_i2c_boardinfo(master, node, reg); 2480 else 2481 ret = of_i3c_master_add_i3c_boardinfo(master, node, reg); 2482 2483 return ret; 2484 } 2485 2486 static int of_populate_i3c_bus(struct i3c_master_controller *master) 2487 { 2488 struct device *dev = &master->dev; 2489 struct device_node *i3cbus_np = dev->of_node; 2490 int ret; 2491 u32 val; 2492 2493 if (!i3cbus_np) 2494 return 0; 2495 2496 for_each_available_child_of_node_scoped(i3cbus_np, node) { 2497 ret = of_i3c_master_add_dev(master, node); 2498 if (ret) 2499 return ret; 2500 } 2501 2502 /* 2503 * The user might want to limit I2C and I3C speed in case some devices 2504 * on the bus are not supporting typical rates, or if the bus topology 2505 * prevents it from using max possible rate. 2506 */ 2507 if (!of_property_read_u32(i3cbus_np, "i2c-scl-hz", &val)) 2508 master->bus.scl_rate.i2c = val; 2509 2510 if (!of_property_read_u32(i3cbus_np, "i3c-scl-hz", &val)) 2511 master->bus.scl_rate.i3c = val; 2512 2513 return 0; 2514 } 2515 2516 static int i3c_master_i2c_adapter_xfer(struct i2c_adapter *adap, 2517 struct i2c_msg *xfers, int nxfers) 2518 { 2519 struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap); 2520 struct i2c_dev_desc *dev; 2521 int i, ret; 2522 u16 addr; 2523 2524 if (!xfers || !master || nxfers <= 0) 2525 return -EINVAL; 2526 2527 if (!master->ops->i2c_xfers) 2528 return -EOPNOTSUPP; 2529 2530 /* Doing transfers to different devices is not supported. */ 2531 addr = xfers[0].addr; 2532 for (i = 1; i < nxfers; i++) { 2533 if (addr != xfers[i].addr) 2534 return -EOPNOTSUPP; 2535 } 2536 2537 ret = i3c_master_rpm_get(master); 2538 if (ret) 2539 return ret; 2540 2541 i3c_bus_normaluse_lock(&master->bus); 2542 dev = i3c_master_find_i2c_dev_by_addr(master, addr); 2543 if (!dev) 2544 ret = -ENOENT; 2545 else 2546 ret = master->ops->i2c_xfers(dev, xfers, nxfers); 2547 i3c_bus_normaluse_unlock(&master->bus); 2548 2549 i3c_master_rpm_put(master); 2550 2551 return ret ? ret : nxfers; 2552 } 2553 2554 static u32 i3c_master_i2c_funcs(struct i2c_adapter *adapter) 2555 { 2556 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C; 2557 } 2558 2559 static u8 i3c_master_i2c_get_lvr(struct i2c_client *client) 2560 { 2561 /* Fall back to no spike filters and FM bus mode. */ 2562 u8 lvr = I3C_LVR_I2C_INDEX(2) | I3C_LVR_I2C_FM_MODE; 2563 u32 reg[3]; 2564 2565 if (!of_property_read_u32_array(client->dev.of_node, "reg", reg, ARRAY_SIZE(reg))) 2566 lvr = reg[2]; 2567 2568 return lvr; 2569 } 2570 2571 static int i3c_master_i2c_attach(struct i2c_adapter *adap, struct i2c_client *client) 2572 { 2573 struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap); 2574 enum i3c_addr_slot_status status; 2575 struct i2c_dev_desc *i2cdev; 2576 int ret; 2577 2578 /* Already added by board info? */ 2579 if (i3c_master_find_i2c_dev_by_addr(master, client->addr)) 2580 return 0; 2581 2582 status = i3c_bus_get_addr_slot_status(&master->bus, client->addr); 2583 if (status != I3C_ADDR_SLOT_FREE) 2584 return -EBUSY; 2585 2586 i3c_bus_set_addr_slot_status(&master->bus, client->addr, 2587 I3C_ADDR_SLOT_I2C_DEV); 2588 2589 i2cdev = i3c_master_alloc_i2c_dev(master, client->addr, 2590 i3c_master_i2c_get_lvr(client)); 2591 if (IS_ERR(i2cdev)) { 2592 ret = PTR_ERR(i2cdev); 2593 goto out_clear_status; 2594 } 2595 2596 ret = i3c_master_attach_i2c_dev(master, i2cdev); 2597 if (ret) 2598 goto out_free_dev; 2599 2600 return 0; 2601 2602 out_free_dev: 2603 i3c_master_free_i2c_dev(i2cdev); 2604 out_clear_status: 2605 i3c_bus_set_addr_slot_status(&master->bus, client->addr, 2606 I3C_ADDR_SLOT_FREE); 2607 2608 return ret; 2609 } 2610 2611 static int i3c_master_i2c_detach(struct i2c_adapter *adap, struct i2c_client *client) 2612 { 2613 struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap); 2614 struct i2c_dev_desc *dev; 2615 2616 dev = i3c_master_find_i2c_dev_by_addr(master, client->addr); 2617 if (!dev) 2618 return -ENODEV; 2619 2620 i3c_master_detach_i2c_dev(dev); 2621 i3c_bus_set_addr_slot_status(&master->bus, dev->addr, 2622 I3C_ADDR_SLOT_FREE); 2623 i3c_master_free_i2c_dev(dev); 2624 2625 return 0; 2626 } 2627 2628 static const struct i2c_algorithm i3c_master_i2c_algo = { 2629 .master_xfer = i3c_master_i2c_adapter_xfer, 2630 .functionality = i3c_master_i2c_funcs, 2631 }; 2632 2633 static int i3c_i2c_notifier_call(struct notifier_block *nb, unsigned long action, 2634 void *data) 2635 { 2636 struct i2c_adapter *adap; 2637 struct i2c_client *client; 2638 struct device *dev = data; 2639 struct i3c_master_controller *master; 2640 int ret; 2641 2642 if (dev->type != &i2c_client_type) 2643 return 0; 2644 2645 client = to_i2c_client(dev); 2646 adap = client->adapter; 2647 2648 if (adap->algo != &i3c_master_i2c_algo) 2649 return 0; 2650 2651 master = i2c_adapter_to_i3c_master(adap); 2652 2653 ret = i3c_master_rpm_get(master); 2654 if (ret) 2655 return ret; 2656 2657 i3c_bus_maintenance_lock(&master->bus); 2658 switch (action) { 2659 case BUS_NOTIFY_ADD_DEVICE: 2660 ret = i3c_master_i2c_attach(adap, client); 2661 break; 2662 case BUS_NOTIFY_DEL_DEVICE: 2663 ret = i3c_master_i2c_detach(adap, client); 2664 break; 2665 default: 2666 ret = -EINVAL; 2667 } 2668 i3c_bus_maintenance_unlock(&master->bus); 2669 2670 i3c_master_rpm_put(master); 2671 2672 return ret; 2673 } 2674 2675 static struct notifier_block i2cdev_notifier = { 2676 .notifier_call = i3c_i2c_notifier_call, 2677 }; 2678 2679 static int i3c_master_i2c_adapter_init(struct i3c_master_controller *master) 2680 { 2681 struct i2c_adapter *adap = i3c_master_to_i2c_adapter(master); 2682 struct i2c_dev_desc *i2cdev; 2683 struct i2c_dev_boardinfo *i2cboardinfo; 2684 int ret, id; 2685 2686 adap->dev.parent = master->dev.parent; 2687 adap->owner = master->dev.parent->driver->owner; 2688 adap->algo = &i3c_master_i2c_algo; 2689 strscpy(adap->name, dev_name(master->dev.parent), sizeof(adap->name)); 2690 adap->timeout = HZ; 2691 adap->retries = 3; 2692 2693 id = of_alias_get_id(master->dev.of_node, "i2c"); 2694 if (id >= 0) { 2695 adap->nr = id; 2696 ret = i2c_add_numbered_adapter(adap); 2697 } else { 2698 ret = i2c_add_adapter(adap); 2699 } 2700 if (ret) 2701 return ret; 2702 2703 /* 2704 * We silently ignore failures here. The bus should keep working 2705 * correctly even if one or more i2c devices are not registered. 2706 */ 2707 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) { 2708 i2cdev = i3c_master_find_i2c_dev_by_addr(master, 2709 i2cboardinfo->base.addr); 2710 if (WARN_ON(!i2cdev)) 2711 continue; 2712 i2cdev->dev = i2c_new_client_device(adap, &i2cboardinfo->base); 2713 } 2714 2715 return 0; 2716 } 2717 2718 static void i3c_master_i2c_adapter_cleanup(struct i3c_master_controller *master) 2719 { 2720 struct i2c_dev_desc *i2cdev; 2721 2722 i2c_del_adapter(&master->i2c); 2723 2724 i3c_bus_for_each_i2cdev(&master->bus, i2cdev) 2725 i2cdev->dev = NULL; 2726 } 2727 2728 static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master) 2729 { 2730 struct i3c_dev_desc *i3cdev; 2731 2732 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) { 2733 if (!i3cdev->dev) 2734 continue; 2735 2736 i3cdev->dev->desc = NULL; 2737 if (device_is_registered(&i3cdev->dev->dev)) 2738 device_unregister(&i3cdev->dev->dev); 2739 else 2740 put_device(&i3cdev->dev->dev); 2741 i3cdev->dev = NULL; 2742 } 2743 } 2744 2745 /** 2746 * i3c_master_queue_ibi() - Queue an IBI 2747 * @dev: the device this IBI is coming from 2748 * @slot: the IBI slot used to store the payload 2749 * 2750 * Queue an IBI to the controller workqueue. The IBI handler attached to 2751 * the dev will be called from a workqueue context. 2752 */ 2753 void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot) 2754 { 2755 if (!dev->ibi || !slot) 2756 return; 2757 2758 atomic_inc(&dev->ibi->pending_ibis); 2759 queue_work(dev->ibi->wq, &slot->work); 2760 } 2761 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi); 2762 2763 static void i3c_master_handle_ibi(struct work_struct *work) 2764 { 2765 struct i3c_ibi_slot *slot = container_of(work, struct i3c_ibi_slot, 2766 work); 2767 struct i3c_dev_desc *dev = slot->dev; 2768 struct i3c_master_controller *master = i3c_dev_get_master(dev); 2769 struct i3c_ibi_payload payload; 2770 2771 payload.data = slot->data; 2772 payload.len = slot->len; 2773 2774 if (dev->dev) 2775 dev->ibi->handler(dev->dev, &payload); 2776 2777 master->ops->recycle_ibi_slot(dev, slot); 2778 if (atomic_dec_and_test(&dev->ibi->pending_ibis)) 2779 complete(&dev->ibi->all_ibis_handled); 2780 } 2781 2782 static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev, 2783 struct i3c_ibi_slot *slot) 2784 { 2785 slot->dev = dev; 2786 INIT_WORK(&slot->work, i3c_master_handle_ibi); 2787 } 2788 2789 struct i3c_generic_ibi_slot { 2790 struct list_head node; 2791 struct i3c_ibi_slot base; 2792 }; 2793 2794 struct i3c_generic_ibi_pool { 2795 spinlock_t lock; 2796 unsigned int num_slots; 2797 struct i3c_generic_ibi_slot *slots; 2798 void *payload_buf; 2799 struct list_head free_slots; 2800 struct list_head pending; 2801 }; 2802 2803 /** 2804 * i3c_generic_ibi_free_pool() - Free a generic IBI pool 2805 * @pool: the IBI pool to free 2806 * 2807 * Free all IBI slots allated by a generic IBI pool. 2808 */ 2809 void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool) 2810 { 2811 struct i3c_generic_ibi_slot *slot; 2812 unsigned int nslots = 0; 2813 2814 while (!list_empty(&pool->free_slots)) { 2815 slot = list_first_entry(&pool->free_slots, 2816 struct i3c_generic_ibi_slot, node); 2817 list_del(&slot->node); 2818 nslots++; 2819 } 2820 2821 /* 2822 * If the number of freed slots is not equal to the number of allocated 2823 * slots we have a leak somewhere. 2824 */ 2825 WARN_ON(nslots != pool->num_slots); 2826 2827 kfree(pool->payload_buf); 2828 kfree(pool->slots); 2829 kfree(pool); 2830 } 2831 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool); 2832 2833 /** 2834 * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool 2835 * @dev: the device this pool will be used for 2836 * @req: IBI setup request describing what the device driver expects 2837 * 2838 * Create a generic IBI pool based on the information provided in @req. 2839 * 2840 * Return: a valid IBI pool in case of success, an ERR_PTR() otherwise. 2841 */ 2842 struct i3c_generic_ibi_pool * 2843 i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev, 2844 const struct i3c_ibi_setup *req) 2845 { 2846 struct i3c_generic_ibi_pool *pool; 2847 struct i3c_generic_ibi_slot *slot; 2848 unsigned int i; 2849 int ret; 2850 2851 pool = kzalloc_obj(*pool, GFP_KERNEL); 2852 if (!pool) 2853 return ERR_PTR(-ENOMEM); 2854 2855 spin_lock_init(&pool->lock); 2856 INIT_LIST_HEAD(&pool->free_slots); 2857 INIT_LIST_HEAD(&pool->pending); 2858 2859 pool->slots = kzalloc_objs(*slot, req->num_slots, GFP_KERNEL); 2860 if (!pool->slots) { 2861 ret = -ENOMEM; 2862 goto err_free_pool; 2863 } 2864 2865 if (req->max_payload_len) { 2866 pool->payload_buf = kcalloc(req->num_slots, 2867 req->max_payload_len, GFP_KERNEL); 2868 if (!pool->payload_buf) { 2869 ret = -ENOMEM; 2870 goto err_free_pool; 2871 } 2872 } 2873 2874 for (i = 0; i < req->num_slots; i++) { 2875 slot = &pool->slots[i]; 2876 i3c_master_init_ibi_slot(dev, &slot->base); 2877 2878 if (req->max_payload_len) 2879 slot->base.data = pool->payload_buf + 2880 (i * req->max_payload_len); 2881 2882 list_add_tail(&slot->node, &pool->free_slots); 2883 pool->num_slots++; 2884 } 2885 2886 return pool; 2887 2888 err_free_pool: 2889 i3c_generic_ibi_free_pool(pool); 2890 return ERR_PTR(ret); 2891 } 2892 EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool); 2893 2894 /** 2895 * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool 2896 * @pool: the pool to query an IBI slot on 2897 * 2898 * Search for a free slot in a generic IBI pool. 2899 * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot() 2900 * when it's no longer needed. 2901 * 2902 * Return: a pointer to a free slot, or NULL if there's no free slot available. 2903 */ 2904 struct i3c_ibi_slot * 2905 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool) 2906 { 2907 struct i3c_generic_ibi_slot *slot; 2908 unsigned long flags; 2909 2910 spin_lock_irqsave(&pool->lock, flags); 2911 slot = list_first_entry_or_null(&pool->free_slots, 2912 struct i3c_generic_ibi_slot, node); 2913 if (slot) 2914 list_del(&slot->node); 2915 spin_unlock_irqrestore(&pool->lock, flags); 2916 2917 return slot ? &slot->base : NULL; 2918 } 2919 EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot); 2920 2921 /** 2922 * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool 2923 * @pool: the pool to return the IBI slot to 2924 * @s: IBI slot to recycle 2925 * 2926 * Add an IBI slot back to its generic IBI pool. Should be called from the 2927 * master driver struct_master_controller_ops->recycle_ibi() method. 2928 */ 2929 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool, 2930 struct i3c_ibi_slot *s) 2931 { 2932 struct i3c_generic_ibi_slot *slot; 2933 unsigned long flags; 2934 2935 if (!s) 2936 return; 2937 2938 slot = container_of(s, struct i3c_generic_ibi_slot, base); 2939 spin_lock_irqsave(&pool->lock, flags); 2940 list_add_tail(&slot->node, &pool->free_slots); 2941 spin_unlock_irqrestore(&pool->lock, flags); 2942 } 2943 EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot); 2944 2945 static int i3c_master_check_ops(const struct i3c_master_controller_ops *ops) 2946 { 2947 if (!ops || !ops->bus_init || !ops->i3c_xfers || 2948 !ops->send_ccc_cmd || !ops->do_daa || !ops->i2c_xfers) 2949 return -EINVAL; 2950 2951 if (ops->request_ibi && 2952 (!ops->enable_ibi || !ops->disable_ibi || !ops->free_ibi || 2953 !ops->recycle_ibi_slot)) 2954 return -EINVAL; 2955 2956 return 0; 2957 } 2958 2959 /** 2960 * i3c_master_register() - register an I3C master 2961 * @master: master used to send frames on the bus 2962 * @parent: the parent device (the one that provides this I3C master 2963 * controller) 2964 * @ops: the master controller operations 2965 * @secondary: true if you are registering a secondary master. Will return 2966 * -EOPNOTSUPP if set to true since secondary masters are not yet 2967 * supported 2968 * 2969 * This function takes care of everything for you: 2970 * 2971 * - creates and initializes the I3C bus 2972 * - populates the bus with static I2C devs if @parent->of_node is not 2973 * NULL 2974 * - registers all I3C devices added by the controller during bus 2975 * initialization 2976 * - registers the I2C adapter and all I2C devices 2977 * 2978 * Return: 0 in case of success, a negative error code otherwise. 2979 */ 2980 int i3c_master_register(struct i3c_master_controller *master, 2981 struct device *parent, 2982 const struct i3c_master_controller_ops *ops, 2983 bool secondary) 2984 { 2985 unsigned long i2c_scl_rate = I3C_BUS_I2C_FM_PLUS_SCL_MAX_RATE; 2986 struct i3c_bus *i3cbus = i3c_master_get_bus(master); 2987 enum i3c_bus_mode mode = I3C_BUS_MODE_PURE; 2988 struct i2c_dev_boardinfo *i2cbi; 2989 int ret; 2990 2991 /* We do not support secondary masters yet. */ 2992 if (secondary) 2993 return -EOPNOTSUPP; 2994 2995 ret = i3c_master_check_ops(ops); 2996 if (ret) 2997 return ret; 2998 2999 master->dev.parent = parent; 3000 master->dev.of_node = of_node_get(parent->of_node); 3001 master->dev.bus = &i3c_bus_type; 3002 master->dev.type = &i3c_masterdev_type; 3003 master->dev.release = i3c_masterdev_release; 3004 master->ops = ops; 3005 master->secondary = secondary; 3006 INIT_LIST_HEAD(&master->boardinfo.i2c); 3007 INIT_LIST_HEAD(&master->boardinfo.i3c); 3008 3009 ret = i3c_master_rpm_get(master); 3010 if (ret) 3011 return ret; 3012 3013 device_initialize(&master->dev); 3014 3015 master->dev.dma_mask = parent->dma_mask; 3016 master->dev.coherent_dma_mask = parent->coherent_dma_mask; 3017 master->dev.dma_parms = parent->dma_parms; 3018 3019 ret = i3c_bus_init(i3cbus, master->dev.of_node); 3020 if (ret) 3021 goto err_put_dev; 3022 3023 dev_set_name(&master->dev, "i3c-%d", i3cbus->id); 3024 3025 ret = of_populate_i3c_bus(master); 3026 if (ret) 3027 goto err_put_dev; 3028 3029 list_for_each_entry(i2cbi, &master->boardinfo.i2c, node) { 3030 switch (i2cbi->lvr & I3C_LVR_I2C_INDEX_MASK) { 3031 case I3C_LVR_I2C_INDEX(0): 3032 if (mode < I3C_BUS_MODE_MIXED_FAST) 3033 mode = I3C_BUS_MODE_MIXED_FAST; 3034 break; 3035 case I3C_LVR_I2C_INDEX(1): 3036 if (mode < I3C_BUS_MODE_MIXED_LIMITED) 3037 mode = I3C_BUS_MODE_MIXED_LIMITED; 3038 break; 3039 case I3C_LVR_I2C_INDEX(2): 3040 if (mode < I3C_BUS_MODE_MIXED_SLOW) 3041 mode = I3C_BUS_MODE_MIXED_SLOW; 3042 break; 3043 default: 3044 ret = -EINVAL; 3045 goto err_put_dev; 3046 } 3047 3048 if (i2cbi->lvr & I3C_LVR_I2C_FM_MODE) 3049 i2c_scl_rate = I3C_BUS_I2C_FM_SCL_MAX_RATE; 3050 } 3051 3052 ret = i3c_bus_set_mode(i3cbus, mode, i2c_scl_rate); 3053 if (ret) 3054 goto err_put_dev; 3055 3056 master->wq = alloc_workqueue("%s", WQ_PERCPU, 0, dev_name(parent)); 3057 if (!master->wq) { 3058 ret = -ENOMEM; 3059 goto err_put_dev; 3060 } 3061 3062 ret = i3c_master_bus_init(master); 3063 if (ret) 3064 goto err_put_dev; 3065 3066 ret = device_add(&master->dev); 3067 if (ret) 3068 goto err_cleanup_bus; 3069 3070 /* 3071 * Expose our I3C bus as an I2C adapter so that I2C devices are exposed 3072 * through the I2C subsystem. 3073 */ 3074 ret = i3c_master_i2c_adapter_init(master); 3075 if (ret) 3076 goto err_del_dev; 3077 3078 i3c_bus_notify(i3cbus, I3C_NOTIFY_BUS_ADD); 3079 3080 pm_runtime_no_callbacks(&master->dev); 3081 pm_suspend_ignore_children(&master->dev, true); 3082 pm_runtime_enable(&master->dev); 3083 3084 /* 3085 * We're done initializing the bus and the controller, we can now 3086 * register I3C devices discovered during the initial DAA. 3087 */ 3088 master->init_done = true; 3089 i3c_bus_normaluse_lock(&master->bus); 3090 i3c_master_register_new_i3c_devs(master); 3091 i3c_bus_normaluse_unlock(&master->bus); 3092 3093 if (master->ops->set_dev_nack_retry) 3094 device_create_file(&master->dev, &dev_attr_dev_nack_retry_count); 3095 3096 i3c_master_rpm_put(master); 3097 3098 return 0; 3099 3100 err_del_dev: 3101 device_del(&master->dev); 3102 3103 err_cleanup_bus: 3104 i3c_master_bus_cleanup(master); 3105 3106 err_put_dev: 3107 i3c_master_rpm_put(master); 3108 put_device(&master->dev); 3109 3110 return ret; 3111 } 3112 EXPORT_SYMBOL_GPL(i3c_master_register); 3113 3114 /** 3115 * i3c_master_unregister() - unregister an I3C master 3116 * @master: master used to send frames on the bus 3117 * 3118 * Basically undo everything done in i3c_master_register(). 3119 */ 3120 void i3c_master_unregister(struct i3c_master_controller *master) 3121 { 3122 i3c_bus_notify(&master->bus, I3C_NOTIFY_BUS_REMOVE); 3123 3124 if (master->ops->set_dev_nack_retry) 3125 device_remove_file(&master->dev, &dev_attr_dev_nack_retry_count); 3126 3127 i3c_master_i2c_adapter_cleanup(master); 3128 i3c_master_unregister_i3c_devs(master); 3129 i3c_master_bus_cleanup(master); 3130 pm_runtime_disable(&master->dev); 3131 device_unregister(&master->dev); 3132 } 3133 EXPORT_SYMBOL_GPL(i3c_master_unregister); 3134 3135 int i3c_dev_setdasa_locked(struct i3c_dev_desc *dev) 3136 { 3137 struct i3c_master_controller *master; 3138 3139 if (!dev) 3140 return -ENOENT; 3141 3142 master = i3c_dev_get_master(dev); 3143 if (!master) 3144 return -EINVAL; 3145 3146 if (!dev->boardinfo || !dev->boardinfo->init_dyn_addr || 3147 !dev->boardinfo->static_addr) 3148 return -EINVAL; 3149 3150 return i3c_master_setdasa_locked(master, dev->info.static_addr, 3151 dev->boardinfo->init_dyn_addr); 3152 } 3153 3154 int i3c_dev_do_xfers_locked(struct i3c_dev_desc *dev, struct i3c_xfer *xfers, 3155 int nxfers, enum i3c_xfer_mode mode) 3156 { 3157 struct i3c_master_controller *master; 3158 3159 if (!dev) 3160 return -ENOENT; 3161 3162 master = i3c_dev_get_master(dev); 3163 if (!master || !xfers) 3164 return -EINVAL; 3165 3166 if (mode != I3C_SDR && !(master->this->info.hdr_cap & BIT(mode))) 3167 return -EOPNOTSUPP; 3168 3169 return master->ops->i3c_xfers(dev, xfers, nxfers, mode); 3170 } 3171 3172 int i3c_dev_disable_ibi_locked(struct i3c_dev_desc *dev) 3173 { 3174 struct i3c_master_controller *master; 3175 int ret; 3176 3177 if (!dev->ibi) 3178 return -EINVAL; 3179 3180 master = i3c_dev_get_master(dev); 3181 ret = master->ops->disable_ibi(dev); 3182 if (ret) 3183 return ret; 3184 3185 reinit_completion(&dev->ibi->all_ibis_handled); 3186 if (atomic_read(&dev->ibi->pending_ibis)) 3187 wait_for_completion(&dev->ibi->all_ibis_handled); 3188 3189 dev->ibi->enabled = false; 3190 3191 return 0; 3192 } 3193 3194 int i3c_dev_enable_ibi_locked(struct i3c_dev_desc *dev) 3195 { 3196 struct i3c_master_controller *master = i3c_dev_get_master(dev); 3197 int ret; 3198 3199 if (!dev->ibi) 3200 return -EINVAL; 3201 3202 ret = master->ops->enable_ibi(dev); 3203 if (!ret) 3204 dev->ibi->enabled = true; 3205 3206 return ret; 3207 } 3208 3209 int i3c_dev_request_ibi_locked(struct i3c_dev_desc *dev, 3210 const struct i3c_ibi_setup *req) 3211 { 3212 struct i3c_master_controller *master = i3c_dev_get_master(dev); 3213 struct i3c_device_ibi_info *ibi; 3214 int ret; 3215 3216 if (!master->ops->request_ibi) 3217 return -EOPNOTSUPP; 3218 3219 if (dev->ibi) 3220 return -EBUSY; 3221 3222 ibi = kzalloc_obj(*ibi, GFP_KERNEL); 3223 if (!ibi) 3224 return -ENOMEM; 3225 3226 ibi->wq = alloc_ordered_workqueue(dev_name(i3cdev_to_dev(dev->dev)), WQ_MEM_RECLAIM); 3227 if (!ibi->wq) { 3228 kfree(ibi); 3229 return -ENOMEM; 3230 } 3231 3232 atomic_set(&ibi->pending_ibis, 0); 3233 init_completion(&ibi->all_ibis_handled); 3234 ibi->handler = req->handler; 3235 ibi->max_payload_len = req->max_payload_len; 3236 ibi->num_slots = req->num_slots; 3237 3238 dev->ibi = ibi; 3239 ret = master->ops->request_ibi(dev, req); 3240 if (ret) { 3241 kfree(ibi); 3242 dev->ibi = NULL; 3243 } 3244 3245 return ret; 3246 } 3247 3248 void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev) 3249 { 3250 struct i3c_master_controller *master = i3c_dev_get_master(dev); 3251 3252 if (!dev->ibi) 3253 return; 3254 3255 if (dev->ibi->enabled) { 3256 int ret; 3257 3258 dev_err(&master->dev, "Freeing IBI that is still enabled\n"); 3259 ret = i3c_master_rpm_get(master); 3260 if (!ret) { 3261 ret = i3c_dev_disable_ibi_locked(dev); 3262 i3c_master_rpm_put(master); 3263 } 3264 if (ret) 3265 dev_err(&master->dev, "Failed to disable IBI before freeing\n"); 3266 } 3267 3268 master->ops->free_ibi(dev); 3269 3270 if (dev->ibi->wq) { 3271 destroy_workqueue(dev->ibi->wq); 3272 dev->ibi->wq = NULL; 3273 } 3274 3275 kfree(dev->ibi); 3276 dev->ibi = NULL; 3277 } 3278 3279 static int __init i3c_init(void) 3280 { 3281 int res; 3282 3283 res = of_alias_get_highest_id("i3c"); 3284 if (res >= 0) { 3285 mutex_lock(&i3c_core_lock); 3286 __i3c_first_dynamic_bus_num = res + 1; 3287 mutex_unlock(&i3c_core_lock); 3288 } 3289 3290 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier); 3291 if (res) 3292 return res; 3293 3294 res = bus_register(&i3c_bus_type); 3295 if (res) 3296 goto out_unreg_notifier; 3297 3298 return 0; 3299 3300 out_unreg_notifier: 3301 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier); 3302 3303 return res; 3304 } 3305 subsys_initcall(i3c_init); 3306 3307 static void __exit i3c_exit(void) 3308 { 3309 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier); 3310 idr_destroy(&i3c_bus_idr); 3311 bus_unregister(&i3c_bus_type); 3312 } 3313 module_exit(i3c_exit); 3314 3315 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>"); 3316 MODULE_DESCRIPTION("I3C core"); 3317 MODULE_LICENSE("GPL v2"); 3318