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