b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2018 Cadence Design Systems Inc. |
| 4 | * |
| 5 | * Author: Boris Brezillon <boris.brezillon@bootlin.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/atomic.h> |
| 9 | #include <linux/bug.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/export.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/workqueue.h> |
| 19 | |
| 20 | #include "internals.h" |
| 21 | |
| 22 | static DEFINE_IDR(i3c_bus_idr); |
| 23 | static DEFINE_MUTEX(i3c_core_lock); |
| 24 | |
| 25 | /** |
| 26 | * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation |
| 27 | * @bus: I3C bus to take the lock on |
| 28 | * |
| 29 | * This function takes the bus lock so that no other operations can occur on |
| 30 | * the bus. This is needed for all kind of bus maintenance operation, like |
| 31 | * - enabling/disabling slave events |
| 32 | * - re-triggering DAA |
| 33 | * - changing the dynamic address of a device |
| 34 | * - relinquishing mastership |
| 35 | * - ... |
| 36 | * |
| 37 | * The reason for this kind of locking is that we don't want drivers and core |
| 38 | * logic to rely on I3C device information that could be changed behind their |
| 39 | * back. |
| 40 | */ |
| 41 | static void i3c_bus_maintenance_lock(struct i3c_bus *bus) |
| 42 | { |
| 43 | down_write(&bus->lock); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * i3c_bus_maintenance_unlock - Release the bus lock after a maintenance |
| 48 | * operation |
| 49 | * @bus: I3C bus to release the lock on |
| 50 | * |
| 51 | * Should be called when the bus maintenance operation is done. See |
| 52 | * i3c_bus_maintenance_lock() for more details on what these maintenance |
| 53 | * operations are. |
| 54 | */ |
| 55 | static void i3c_bus_maintenance_unlock(struct i3c_bus *bus) |
| 56 | { |
| 57 | up_write(&bus->lock); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * i3c_bus_normaluse_lock - Lock the bus for a normal operation |
| 62 | * @bus: I3C bus to take the lock on |
| 63 | * |
| 64 | * This function takes the bus lock for any operation that is not a maintenance |
| 65 | * operation (see i3c_bus_maintenance_lock() for a non-exhaustive list of |
| 66 | * maintenance operations). Basically all communications with I3C devices are |
| 67 | * normal operations (HDR, SDR transfers or CCC commands that do not change bus |
| 68 | * state or I3C dynamic address). |
| 69 | * |
| 70 | * Note that this lock is not guaranteeing serialization of normal operations. |
| 71 | * In other words, transfer requests passed to the I3C master can be submitted |
| 72 | * in parallel and I3C master drivers have to use their own locking to make |
| 73 | * sure two different communications are not inter-mixed, or access to the |
| 74 | * output/input queue is not done while the engine is busy. |
| 75 | */ |
| 76 | void i3c_bus_normaluse_lock(struct i3c_bus *bus) |
| 77 | { |
| 78 | down_read(&bus->lock); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * i3c_bus_normaluse_unlock - Release the bus lock after a normal operation |
| 83 | * @bus: I3C bus to release the lock on |
| 84 | * |
| 85 | * Should be called when a normal operation is done. See |
| 86 | * i3c_bus_normaluse_lock() for more details on what these normal operations |
| 87 | * are. |
| 88 | */ |
| 89 | void i3c_bus_normaluse_unlock(struct i3c_bus *bus) |
| 90 | { |
| 91 | up_read(&bus->lock); |
| 92 | } |
| 93 | |
| 94 | static struct i3c_master_controller * |
| 95 | i3c_bus_to_i3c_master(struct i3c_bus *i3cbus) |
| 96 | { |
| 97 | return container_of(i3cbus, struct i3c_master_controller, bus); |
| 98 | } |
| 99 | |
| 100 | static struct i3c_master_controller *dev_to_i3cmaster(struct device *dev) |
| 101 | { |
| 102 | return container_of(dev, struct i3c_master_controller, dev); |
| 103 | } |
| 104 | |
| 105 | static const struct device_type i3c_device_type; |
| 106 | |
| 107 | static struct i3c_bus *dev_to_i3cbus(struct device *dev) |
| 108 | { |
| 109 | struct i3c_master_controller *master; |
| 110 | |
| 111 | if (dev->type == &i3c_device_type) |
| 112 | return dev_to_i3cdev(dev)->bus; |
| 113 | |
| 114 | master = dev_to_i3cmaster(dev); |
| 115 | |
| 116 | return &master->bus; |
| 117 | } |
| 118 | |
| 119 | static struct i3c_dev_desc *dev_to_i3cdesc(struct device *dev) |
| 120 | { |
| 121 | struct i3c_master_controller *master; |
| 122 | |
| 123 | if (dev->type == &i3c_device_type) |
| 124 | return dev_to_i3cdev(dev)->desc; |
| 125 | |
| 126 | master = dev_to_i3cmaster(dev); |
| 127 | |
| 128 | return master->this; |
| 129 | } |
| 130 | |
| 131 | static ssize_t bcr_show(struct device *dev, |
| 132 | struct device_attribute *da, |
| 133 | char *buf) |
| 134 | { |
| 135 | struct i3c_bus *bus = dev_to_i3cbus(dev); |
| 136 | struct i3c_dev_desc *desc; |
| 137 | ssize_t ret; |
| 138 | |
| 139 | i3c_bus_normaluse_lock(bus); |
| 140 | desc = dev_to_i3cdesc(dev); |
| 141 | ret = sprintf(buf, "%x\n", desc->info.bcr); |
| 142 | i3c_bus_normaluse_unlock(bus); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | static DEVICE_ATTR_RO(bcr); |
| 147 | |
| 148 | static ssize_t dcr_show(struct device *dev, |
| 149 | struct device_attribute *da, |
| 150 | char *buf) |
| 151 | { |
| 152 | struct i3c_bus *bus = dev_to_i3cbus(dev); |
| 153 | struct i3c_dev_desc *desc; |
| 154 | ssize_t ret; |
| 155 | |
| 156 | i3c_bus_normaluse_lock(bus); |
| 157 | desc = dev_to_i3cdesc(dev); |
| 158 | ret = sprintf(buf, "%x\n", desc->info.dcr); |
| 159 | i3c_bus_normaluse_unlock(bus); |
| 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | static DEVICE_ATTR_RO(dcr); |
| 164 | |
| 165 | static ssize_t pid_show(struct device *dev, |
| 166 | struct device_attribute *da, |
| 167 | char *buf) |
| 168 | { |
| 169 | struct i3c_bus *bus = dev_to_i3cbus(dev); |
| 170 | struct i3c_dev_desc *desc; |
| 171 | ssize_t ret; |
| 172 | |
| 173 | i3c_bus_normaluse_lock(bus); |
| 174 | desc = dev_to_i3cdesc(dev); |
| 175 | ret = sprintf(buf, "%llx\n", desc->info.pid); |
| 176 | i3c_bus_normaluse_unlock(bus); |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | static DEVICE_ATTR_RO(pid); |
| 181 | |
| 182 | static ssize_t dynamic_address_show(struct device *dev, |
| 183 | struct device_attribute *da, |
| 184 | char *buf) |
| 185 | { |
| 186 | struct i3c_bus *bus = dev_to_i3cbus(dev); |
| 187 | struct i3c_dev_desc *desc; |
| 188 | ssize_t ret; |
| 189 | |
| 190 | i3c_bus_normaluse_lock(bus); |
| 191 | desc = dev_to_i3cdesc(dev); |
| 192 | ret = sprintf(buf, "%02x\n", desc->info.dyn_addr); |
| 193 | i3c_bus_normaluse_unlock(bus); |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | static DEVICE_ATTR_RO(dynamic_address); |
| 198 | |
| 199 | static const char * const hdrcap_strings[] = { |
| 200 | "hdr-ddr", "hdr-tsp", "hdr-tsl", |
| 201 | }; |
| 202 | |
| 203 | static ssize_t hdrcap_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 offset = 0, ret; |
| 210 | unsigned long caps; |
| 211 | int mode; |
| 212 | |
| 213 | i3c_bus_normaluse_lock(bus); |
| 214 | desc = dev_to_i3cdesc(dev); |
| 215 | caps = desc->info.hdr_cap; |
| 216 | for_each_set_bit(mode, &caps, 8) { |
| 217 | if (mode >= ARRAY_SIZE(hdrcap_strings)) |
| 218 | break; |
| 219 | |
| 220 | if (!hdrcap_strings[mode]) |
| 221 | continue; |
| 222 | |
| 223 | ret = sprintf(buf + offset, offset ? " %s" : "%s", |
| 224 | hdrcap_strings[mode]); |
| 225 | if (ret < 0) |
| 226 | goto out; |
| 227 | |
| 228 | offset += ret; |
| 229 | } |
| 230 | |
| 231 | ret = sprintf(buf + offset, "\n"); |
| 232 | if (ret < 0) |
| 233 | goto out; |
| 234 | |
| 235 | ret = offset + ret; |
| 236 | |
| 237 | out: |
| 238 | i3c_bus_normaluse_unlock(bus); |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | static DEVICE_ATTR_RO(hdrcap); |
| 243 | |
| 244 | static struct attribute *i3c_device_attrs[] = { |
| 245 | &dev_attr_bcr.attr, |
| 246 | &dev_attr_dcr.attr, |
| 247 | &dev_attr_pid.attr, |
| 248 | &dev_attr_dynamic_address.attr, |
| 249 | &dev_attr_hdrcap.attr, |
| 250 | NULL, |
| 251 | }; |
| 252 | ATTRIBUTE_GROUPS(i3c_device); |
| 253 | |
| 254 | static int i3c_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 255 | { |
| 256 | struct i3c_device *i3cdev = dev_to_i3cdev(dev); |
| 257 | struct i3c_device_info devinfo; |
| 258 | u16 manuf, part, ext; |
| 259 | |
| 260 | if (i3cdev->desc) |
| 261 | devinfo = i3cdev->desc->info; |
| 262 | manuf = I3C_PID_MANUF_ID(devinfo.pid); |
| 263 | part = I3C_PID_PART_ID(devinfo.pid); |
| 264 | ext = I3C_PID_EXTRA_INFO(devinfo.pid); |
| 265 | |
| 266 | if (I3C_PID_RND_LOWER_32BITS(devinfo.pid)) |
| 267 | return add_uevent_var(env, "MODALIAS=i3c:dcr%02Xmanuf%04X", |
| 268 | devinfo.dcr, manuf); |
| 269 | |
| 270 | return add_uevent_var(env, |
| 271 | "MODALIAS=i3c:dcr%02Xmanuf%04Xpart%04xext%04x", |
| 272 | devinfo.dcr, manuf, part, ext); |
| 273 | } |
| 274 | |
| 275 | static const struct device_type i3c_device_type = { |
| 276 | .groups = i3c_device_groups, |
| 277 | .uevent = i3c_device_uevent, |
| 278 | }; |
| 279 | |
| 280 | static int i3c_device_match(struct device *dev, struct device_driver *drv) |
| 281 | { |
| 282 | struct i3c_device *i3cdev; |
| 283 | struct i3c_driver *i3cdrv; |
| 284 | |
| 285 | if (dev->type != &i3c_device_type) |
| 286 | return 0; |
| 287 | |
| 288 | i3cdev = dev_to_i3cdev(dev); |
| 289 | i3cdrv = drv_to_i3cdrv(drv); |
| 290 | if (i3c_device_match_id(i3cdev, i3cdrv->id_table)) |
| 291 | return 1; |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int i3c_device_probe(struct device *dev) |
| 297 | { |
| 298 | struct i3c_device *i3cdev = dev_to_i3cdev(dev); |
| 299 | struct i3c_driver *driver = drv_to_i3cdrv(dev->driver); |
| 300 | |
| 301 | return driver->probe(i3cdev); |
| 302 | } |
| 303 | |
| 304 | static int i3c_device_remove(struct device *dev) |
| 305 | { |
| 306 | struct i3c_device *i3cdev = dev_to_i3cdev(dev); |
| 307 | struct i3c_driver *driver = drv_to_i3cdrv(dev->driver); |
| 308 | int ret; |
| 309 | |
| 310 | ret = driver->remove(i3cdev); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | |
| 314 | i3c_device_free_ibi(i3cdev); |
| 315 | |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | struct bus_type i3c_bus_type = { |
| 320 | .name = "i3c", |
| 321 | .match = i3c_device_match, |
| 322 | .probe = i3c_device_probe, |
| 323 | .remove = i3c_device_remove, |
| 324 | }; |
| 325 | |
| 326 | static enum i3c_addr_slot_status |
| 327 | i3c_bus_get_addr_slot_status(struct i3c_bus *bus, u16 addr) |
| 328 | { |
| 329 | int status, bitpos = addr * 2; |
| 330 | |
| 331 | if (addr > I2C_MAX_ADDR) |
| 332 | return I3C_ADDR_SLOT_RSVD; |
| 333 | |
| 334 | status = bus->addrslots[bitpos / BITS_PER_LONG]; |
| 335 | status >>= bitpos % BITS_PER_LONG; |
| 336 | |
| 337 | return status & I3C_ADDR_SLOT_STATUS_MASK; |
| 338 | } |
| 339 | |
| 340 | static void i3c_bus_set_addr_slot_status(struct i3c_bus *bus, u16 addr, |
| 341 | enum i3c_addr_slot_status status) |
| 342 | { |
| 343 | int bitpos = addr * 2; |
| 344 | unsigned long *ptr; |
| 345 | |
| 346 | if (addr > I2C_MAX_ADDR) |
| 347 | return; |
| 348 | |
| 349 | ptr = bus->addrslots + (bitpos / BITS_PER_LONG); |
| 350 | *ptr &= ~((unsigned long)I3C_ADDR_SLOT_STATUS_MASK << |
| 351 | (bitpos % BITS_PER_LONG)); |
| 352 | *ptr |= (unsigned long)status << (bitpos % BITS_PER_LONG); |
| 353 | } |
| 354 | |
| 355 | static bool i3c_bus_dev_addr_is_avail(struct i3c_bus *bus, u8 addr) |
| 356 | { |
| 357 | enum i3c_addr_slot_status status; |
| 358 | |
| 359 | status = i3c_bus_get_addr_slot_status(bus, addr); |
| 360 | |
| 361 | return status == I3C_ADDR_SLOT_FREE; |
| 362 | } |
| 363 | |
| 364 | static int i3c_bus_get_free_addr(struct i3c_bus *bus, u8 start_addr) |
| 365 | { |
| 366 | enum i3c_addr_slot_status status; |
| 367 | u8 addr; |
| 368 | |
| 369 | for (addr = start_addr; addr < I3C_MAX_ADDR; addr++) { |
| 370 | status = i3c_bus_get_addr_slot_status(bus, addr); |
| 371 | if (status == I3C_ADDR_SLOT_FREE) |
| 372 | return addr; |
| 373 | } |
| 374 | |
| 375 | return -ENOMEM; |
| 376 | } |
| 377 | |
| 378 | static void i3c_bus_init_addrslots(struct i3c_bus *bus) |
| 379 | { |
| 380 | int i; |
| 381 | |
| 382 | /* Addresses 0 to 7 are reserved. */ |
| 383 | for (i = 0; i < 8; i++) |
| 384 | i3c_bus_set_addr_slot_status(bus, i, I3C_ADDR_SLOT_RSVD); |
| 385 | |
| 386 | /* |
| 387 | * Reserve broadcast address and all addresses that might collide |
| 388 | * with the broadcast address when facing a single bit error. |
| 389 | */ |
| 390 | i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR, |
| 391 | I3C_ADDR_SLOT_RSVD); |
| 392 | for (i = 0; i < 7; i++) |
| 393 | i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR ^ BIT(i), |
| 394 | I3C_ADDR_SLOT_RSVD); |
| 395 | } |
| 396 | |
| 397 | static void i3c_bus_cleanup(struct i3c_bus *i3cbus) |
| 398 | { |
| 399 | mutex_lock(&i3c_core_lock); |
| 400 | idr_remove(&i3c_bus_idr, i3cbus->id); |
| 401 | mutex_unlock(&i3c_core_lock); |
| 402 | } |
| 403 | |
| 404 | static int i3c_bus_init(struct i3c_bus *i3cbus) |
| 405 | { |
| 406 | int ret; |
| 407 | |
| 408 | init_rwsem(&i3cbus->lock); |
| 409 | INIT_LIST_HEAD(&i3cbus->devs.i2c); |
| 410 | INIT_LIST_HEAD(&i3cbus->devs.i3c); |
| 411 | i3c_bus_init_addrslots(i3cbus); |
| 412 | i3cbus->mode = I3C_BUS_MODE_PURE; |
| 413 | |
| 414 | mutex_lock(&i3c_core_lock); |
| 415 | ret = idr_alloc(&i3c_bus_idr, i3cbus, 0, 0, GFP_KERNEL); |
| 416 | mutex_unlock(&i3c_core_lock); |
| 417 | |
| 418 | if (ret < 0) |
| 419 | return ret; |
| 420 | |
| 421 | i3cbus->id = ret; |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static const char * const i3c_bus_mode_strings[] = { |
| 427 | [I3C_BUS_MODE_PURE] = "pure", |
| 428 | [I3C_BUS_MODE_MIXED_FAST] = "mixed-fast", |
| 429 | [I3C_BUS_MODE_MIXED_LIMITED] = "mixed-limited", |
| 430 | [I3C_BUS_MODE_MIXED_SLOW] = "mixed-slow", |
| 431 | }; |
| 432 | |
| 433 | static ssize_t mode_show(struct device *dev, |
| 434 | struct device_attribute *da, |
| 435 | char *buf) |
| 436 | { |
| 437 | struct i3c_bus *i3cbus = dev_to_i3cbus(dev); |
| 438 | ssize_t ret; |
| 439 | |
| 440 | i3c_bus_normaluse_lock(i3cbus); |
| 441 | if (i3cbus->mode < 0 || |
| 442 | i3cbus->mode >= ARRAY_SIZE(i3c_bus_mode_strings) || |
| 443 | !i3c_bus_mode_strings[i3cbus->mode]) |
| 444 | ret = sprintf(buf, "unknown\n"); |
| 445 | else |
| 446 | ret = sprintf(buf, "%s\n", i3c_bus_mode_strings[i3cbus->mode]); |
| 447 | i3c_bus_normaluse_unlock(i3cbus); |
| 448 | |
| 449 | return ret; |
| 450 | } |
| 451 | static DEVICE_ATTR_RO(mode); |
| 452 | |
| 453 | static ssize_t current_master_show(struct device *dev, |
| 454 | struct device_attribute *da, |
| 455 | char *buf) |
| 456 | { |
| 457 | struct i3c_bus *i3cbus = dev_to_i3cbus(dev); |
| 458 | ssize_t ret; |
| 459 | |
| 460 | i3c_bus_normaluse_lock(i3cbus); |
| 461 | ret = sprintf(buf, "%d-%llx\n", i3cbus->id, |
| 462 | i3cbus->cur_master->info.pid); |
| 463 | i3c_bus_normaluse_unlock(i3cbus); |
| 464 | |
| 465 | return ret; |
| 466 | } |
| 467 | static DEVICE_ATTR_RO(current_master); |
| 468 | |
| 469 | static ssize_t i3c_scl_frequency_show(struct device *dev, |
| 470 | struct device_attribute *da, |
| 471 | char *buf) |
| 472 | { |
| 473 | struct i3c_bus *i3cbus = dev_to_i3cbus(dev); |
| 474 | ssize_t ret; |
| 475 | |
| 476 | i3c_bus_normaluse_lock(i3cbus); |
| 477 | ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i3c); |
| 478 | i3c_bus_normaluse_unlock(i3cbus); |
| 479 | |
| 480 | return ret; |
| 481 | } |
| 482 | static DEVICE_ATTR_RO(i3c_scl_frequency); |
| 483 | |
| 484 | static ssize_t i2c_scl_frequency_show(struct device *dev, |
| 485 | struct device_attribute *da, |
| 486 | char *buf) |
| 487 | { |
| 488 | struct i3c_bus *i3cbus = dev_to_i3cbus(dev); |
| 489 | ssize_t ret; |
| 490 | |
| 491 | i3c_bus_normaluse_lock(i3cbus); |
| 492 | ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i2c); |
| 493 | i3c_bus_normaluse_unlock(i3cbus); |
| 494 | |
| 495 | return ret; |
| 496 | } |
| 497 | static DEVICE_ATTR_RO(i2c_scl_frequency); |
| 498 | |
| 499 | static struct attribute *i3c_masterdev_attrs[] = { |
| 500 | &dev_attr_mode.attr, |
| 501 | &dev_attr_current_master.attr, |
| 502 | &dev_attr_i3c_scl_frequency.attr, |
| 503 | &dev_attr_i2c_scl_frequency.attr, |
| 504 | &dev_attr_bcr.attr, |
| 505 | &dev_attr_dcr.attr, |
| 506 | &dev_attr_pid.attr, |
| 507 | &dev_attr_dynamic_address.attr, |
| 508 | &dev_attr_hdrcap.attr, |
| 509 | NULL, |
| 510 | }; |
| 511 | ATTRIBUTE_GROUPS(i3c_masterdev); |
| 512 | |
| 513 | static void i3c_masterdev_release(struct device *dev) |
| 514 | { |
| 515 | struct i3c_master_controller *master = dev_to_i3cmaster(dev); |
| 516 | struct i3c_bus *bus = dev_to_i3cbus(dev); |
| 517 | |
| 518 | if (master->wq) |
| 519 | destroy_workqueue(master->wq); |
| 520 | |
| 521 | WARN_ON(!list_empty(&bus->devs.i2c) || !list_empty(&bus->devs.i3c)); |
| 522 | i3c_bus_cleanup(bus); |
| 523 | |
| 524 | of_node_put(dev->of_node); |
| 525 | } |
| 526 | |
| 527 | static const struct device_type i3c_masterdev_type = { |
| 528 | .groups = i3c_masterdev_groups, |
| 529 | }; |
| 530 | |
| 531 | int i3c_bus_set_mode(struct i3c_bus *i3cbus, enum i3c_bus_mode mode, |
| 532 | unsigned long max_i2c_scl_rate) |
| 533 | { |
| 534 | struct i3c_master_controller *master = i3c_bus_to_i3c_master(i3cbus); |
| 535 | |
| 536 | i3cbus->mode = mode; |
| 537 | |
| 538 | switch (i3cbus->mode) { |
| 539 | case I3C_BUS_MODE_PURE: |
| 540 | if (!i3cbus->scl_rate.i3c) |
| 541 | i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE; |
| 542 | break; |
| 543 | case I3C_BUS_MODE_MIXED_FAST: |
| 544 | case I3C_BUS_MODE_MIXED_LIMITED: |
| 545 | if (!i3cbus->scl_rate.i3c) |
| 546 | i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE; |
| 547 | if (!i3cbus->scl_rate.i2c) |
| 548 | i3cbus->scl_rate.i2c = max_i2c_scl_rate; |
| 549 | break; |
| 550 | case I3C_BUS_MODE_MIXED_SLOW: |
| 551 | if (!i3cbus->scl_rate.i2c) |
| 552 | i3cbus->scl_rate.i2c = max_i2c_scl_rate; |
| 553 | if (!i3cbus->scl_rate.i3c || |
| 554 | i3cbus->scl_rate.i3c > i3cbus->scl_rate.i2c) |
| 555 | i3cbus->scl_rate.i3c = i3cbus->scl_rate.i2c; |
| 556 | break; |
| 557 | default: |
| 558 | return -EINVAL; |
| 559 | } |
| 560 | |
| 561 | dev_dbg(&master->dev, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n", |
| 562 | i3cbus->scl_rate.i2c, i3cbus->scl_rate.i3c); |
| 563 | |
| 564 | /* |
| 565 | * I3C/I2C frequency may have been overridden, check that user-provided |
| 566 | * values are not exceeding max possible frequency. |
| 567 | */ |
| 568 | if (i3cbus->scl_rate.i3c > I3C_BUS_MAX_I3C_SCL_RATE || |
| 569 | i3cbus->scl_rate.i2c > I3C_BUS_I2C_FM_PLUS_SCL_RATE) |
| 570 | return -EINVAL; |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static struct i3c_master_controller * |
| 576 | i2c_adapter_to_i3c_master(struct i2c_adapter *adap) |
| 577 | { |
| 578 | return container_of(adap, struct i3c_master_controller, i2c); |
| 579 | } |
| 580 | |
| 581 | static struct i2c_adapter * |
| 582 | i3c_master_to_i2c_adapter(struct i3c_master_controller *master) |
| 583 | { |
| 584 | return &master->i2c; |
| 585 | } |
| 586 | |
| 587 | static void i3c_master_free_i2c_dev(struct i2c_dev_desc *dev) |
| 588 | { |
| 589 | kfree(dev); |
| 590 | } |
| 591 | |
| 592 | static struct i2c_dev_desc * |
| 593 | i3c_master_alloc_i2c_dev(struct i3c_master_controller *master, |
| 594 | const struct i2c_dev_boardinfo *boardinfo) |
| 595 | { |
| 596 | struct i2c_dev_desc *dev; |
| 597 | |
| 598 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 599 | if (!dev) |
| 600 | return ERR_PTR(-ENOMEM); |
| 601 | |
| 602 | dev->common.master = master; |
| 603 | dev->boardinfo = boardinfo; |
| 604 | dev->addr = boardinfo->base.addr; |
| 605 | dev->lvr = boardinfo->lvr; |
| 606 | |
| 607 | return dev; |
| 608 | } |
| 609 | |
| 610 | static void *i3c_ccc_cmd_dest_init(struct i3c_ccc_cmd_dest *dest, u8 addr, |
| 611 | u16 payloadlen) |
| 612 | { |
| 613 | dest->addr = addr; |
| 614 | dest->payload.len = payloadlen; |
| 615 | if (payloadlen) |
| 616 | dest->payload.data = kzalloc(payloadlen, GFP_KERNEL); |
| 617 | else |
| 618 | dest->payload.data = NULL; |
| 619 | |
| 620 | return dest->payload.data; |
| 621 | } |
| 622 | |
| 623 | static void i3c_ccc_cmd_dest_cleanup(struct i3c_ccc_cmd_dest *dest) |
| 624 | { |
| 625 | kfree(dest->payload.data); |
| 626 | } |
| 627 | |
| 628 | static void i3c_ccc_cmd_init(struct i3c_ccc_cmd *cmd, bool rnw, u8 id, |
| 629 | struct i3c_ccc_cmd_dest *dests, |
| 630 | unsigned int ndests) |
| 631 | { |
| 632 | cmd->rnw = rnw ? 1 : 0; |
| 633 | cmd->id = id; |
| 634 | cmd->dests = dests; |
| 635 | cmd->ndests = ndests; |
| 636 | cmd->err = I3C_ERROR_UNKNOWN; |
| 637 | } |
| 638 | |
| 639 | static int i3c_master_send_ccc_cmd_locked(struct i3c_master_controller *master, |
| 640 | struct i3c_ccc_cmd *cmd) |
| 641 | { |
| 642 | int ret; |
| 643 | |
| 644 | if (!cmd || !master) |
| 645 | return -EINVAL; |
| 646 | |
| 647 | if (WARN_ON(master->init_done && |
| 648 | !rwsem_is_locked(&master->bus.lock))) |
| 649 | return -EINVAL; |
| 650 | |
| 651 | if (!master->ops->send_ccc_cmd) |
| 652 | return -ENOTSUPP; |
| 653 | |
| 654 | if ((cmd->id & I3C_CCC_DIRECT) && (!cmd->dests || !cmd->ndests)) |
| 655 | return -EINVAL; |
| 656 | |
| 657 | if (master->ops->supports_ccc_cmd && |
| 658 | !master->ops->supports_ccc_cmd(master, cmd)) |
| 659 | return -ENOTSUPP; |
| 660 | |
| 661 | ret = master->ops->send_ccc_cmd(master, cmd); |
| 662 | if (ret) { |
| 663 | if (cmd->err != I3C_ERROR_UNKNOWN) |
| 664 | return cmd->err; |
| 665 | |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static struct i2c_dev_desc * |
| 673 | i3c_master_find_i2c_dev_by_addr(const struct i3c_master_controller *master, |
| 674 | u16 addr) |
| 675 | { |
| 676 | struct i2c_dev_desc *dev; |
| 677 | |
| 678 | i3c_bus_for_each_i2cdev(&master->bus, dev) { |
| 679 | if (dev->boardinfo->base.addr == addr) |
| 680 | return dev; |
| 681 | } |
| 682 | |
| 683 | return NULL; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * i3c_master_get_free_addr() - get a free address on the bus |
| 688 | * @master: I3C master object |
| 689 | * @start_addr: where to start searching |
| 690 | * |
| 691 | * This function must be called with the bus lock held in write mode. |
| 692 | * |
| 693 | * Return: the first free address starting at @start_addr (included) or -ENOMEM |
| 694 | * if there's no more address available. |
| 695 | */ |
| 696 | int i3c_master_get_free_addr(struct i3c_master_controller *master, |
| 697 | u8 start_addr) |
| 698 | { |
| 699 | return i3c_bus_get_free_addr(&master->bus, start_addr); |
| 700 | } |
| 701 | EXPORT_SYMBOL_GPL(i3c_master_get_free_addr); |
| 702 | |
| 703 | static void i3c_device_release(struct device *dev) |
| 704 | { |
| 705 | struct i3c_device *i3cdev = dev_to_i3cdev(dev); |
| 706 | |
| 707 | WARN_ON(i3cdev->desc); |
| 708 | |
| 709 | of_node_put(i3cdev->dev.of_node); |
| 710 | kfree(i3cdev); |
| 711 | } |
| 712 | |
| 713 | static void i3c_master_free_i3c_dev(struct i3c_dev_desc *dev) |
| 714 | { |
| 715 | kfree(dev); |
| 716 | } |
| 717 | |
| 718 | static struct i3c_dev_desc * |
| 719 | i3c_master_alloc_i3c_dev(struct i3c_master_controller *master, |
| 720 | const struct i3c_device_info *info) |
| 721 | { |
| 722 | struct i3c_dev_desc *dev; |
| 723 | |
| 724 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 725 | if (!dev) |
| 726 | return ERR_PTR(-ENOMEM); |
| 727 | |
| 728 | dev->common.master = master; |
| 729 | dev->info = *info; |
| 730 | mutex_init(&dev->ibi_lock); |
| 731 | |
| 732 | return dev; |
| 733 | } |
| 734 | |
| 735 | static int i3c_master_rstdaa_locked(struct i3c_master_controller *master, |
| 736 | u8 addr) |
| 737 | { |
| 738 | enum i3c_addr_slot_status addrstat; |
| 739 | struct i3c_ccc_cmd_dest dest; |
| 740 | struct i3c_ccc_cmd cmd; |
| 741 | int ret; |
| 742 | |
| 743 | if (!master) |
| 744 | return -EINVAL; |
| 745 | |
| 746 | addrstat = i3c_bus_get_addr_slot_status(&master->bus, addr); |
| 747 | if (addr != I3C_BROADCAST_ADDR && addrstat != I3C_ADDR_SLOT_I3C_DEV) |
| 748 | return -EINVAL; |
| 749 | |
| 750 | i3c_ccc_cmd_dest_init(&dest, addr, 0); |
| 751 | i3c_ccc_cmd_init(&cmd, false, |
| 752 | I3C_CCC_RSTDAA(addr == I3C_BROADCAST_ADDR), |
| 753 | &dest, 1); |
| 754 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 755 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 756 | |
| 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment) |
| 762 | * procedure |
| 763 | * @master: master used to send frames on the bus |
| 764 | * |
| 765 | * Send a ENTDAA CCC command to start a DAA procedure. |
| 766 | * |
| 767 | * Note that this function only sends the ENTDAA CCC command, all the logic |
| 768 | * behind dynamic address assignment has to be handled in the I3C master |
| 769 | * driver. |
| 770 | * |
| 771 | * This function must be called with the bus lock held in write mode. |
| 772 | * |
| 773 | * Return: 0 in case of success, a positive I3C error code if the error is |
| 774 | * one of the official Mx error codes, and a negative error code otherwise. |
| 775 | */ |
| 776 | int i3c_master_entdaa_locked(struct i3c_master_controller *master) |
| 777 | { |
| 778 | struct i3c_ccc_cmd_dest dest; |
| 779 | struct i3c_ccc_cmd cmd; |
| 780 | int ret; |
| 781 | |
| 782 | i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR, 0); |
| 783 | i3c_ccc_cmd_init(&cmd, false, I3C_CCC_ENTDAA, &dest, 1); |
| 784 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 785 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 786 | |
| 787 | return ret; |
| 788 | } |
| 789 | EXPORT_SYMBOL_GPL(i3c_master_entdaa_locked); |
| 790 | |
| 791 | static int i3c_master_enec_disec_locked(struct i3c_master_controller *master, |
| 792 | u8 addr, bool enable, u8 evts) |
| 793 | { |
| 794 | struct i3c_ccc_events *events; |
| 795 | struct i3c_ccc_cmd_dest dest; |
| 796 | struct i3c_ccc_cmd cmd; |
| 797 | int ret; |
| 798 | |
| 799 | events = i3c_ccc_cmd_dest_init(&dest, addr, sizeof(*events)); |
| 800 | if (!events) |
| 801 | return -ENOMEM; |
| 802 | |
| 803 | events->events = evts; |
| 804 | i3c_ccc_cmd_init(&cmd, false, |
| 805 | enable ? |
| 806 | I3C_CCC_ENEC(addr == I3C_BROADCAST_ADDR) : |
| 807 | I3C_CCC_DISEC(addr == I3C_BROADCAST_ADDR), |
| 808 | &dest, 1); |
| 809 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 810 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 811 | |
| 812 | return ret; |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * i3c_master_disec_locked() - send a DISEC CCC command |
| 817 | * @master: master used to send frames on the bus |
| 818 | * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR |
| 819 | * @evts: events to disable |
| 820 | * |
| 821 | * Send a DISEC CCC command to disable some or all events coming from a |
| 822 | * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR. |
| 823 | * |
| 824 | * This function must be called with the bus lock held in write mode. |
| 825 | * |
| 826 | * Return: 0 in case of success, a positive I3C error code if the error is |
| 827 | * one of the official Mx error codes, and a negative error code otherwise. |
| 828 | */ |
| 829 | int i3c_master_disec_locked(struct i3c_master_controller *master, u8 addr, |
| 830 | u8 evts) |
| 831 | { |
| 832 | return i3c_master_enec_disec_locked(master, addr, false, evts); |
| 833 | } |
| 834 | EXPORT_SYMBOL_GPL(i3c_master_disec_locked); |
| 835 | |
| 836 | /** |
| 837 | * i3c_master_enec_locked() - send an ENEC CCC command |
| 838 | * @master: master used to send frames on the bus |
| 839 | * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR |
| 840 | * @evts: events to disable |
| 841 | * |
| 842 | * Sends an ENEC CCC command to enable some or all events coming from a |
| 843 | * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR. |
| 844 | * |
| 845 | * This function must be called with the bus lock held in write mode. |
| 846 | * |
| 847 | * Return: 0 in case of success, a positive I3C error code if the error is |
| 848 | * one of the official Mx error codes, and a negative error code otherwise. |
| 849 | */ |
| 850 | int i3c_master_enec_locked(struct i3c_master_controller *master, u8 addr, |
| 851 | u8 evts) |
| 852 | { |
| 853 | return i3c_master_enec_disec_locked(master, addr, true, evts); |
| 854 | } |
| 855 | EXPORT_SYMBOL_GPL(i3c_master_enec_locked); |
| 856 | |
| 857 | /** |
| 858 | * i3c_master_defslvs_locked() - send a DEFSLVS CCC command |
| 859 | * @master: master used to send frames on the bus |
| 860 | * |
| 861 | * Send a DEFSLVS CCC command containing all the devices known to the @master. |
| 862 | * This is useful when you have secondary masters on the bus to propagate |
| 863 | * device information. |
| 864 | * |
| 865 | * This should be called after all I3C devices have been discovered (in other |
| 866 | * words, after the DAA procedure has finished) and instantiated in |
| 867 | * &i3c_master_controller_ops->bus_init(). |
| 868 | * It should also be called if a master ACKed an Hot-Join request and assigned |
| 869 | * a dynamic address to the device joining the bus. |
| 870 | * |
| 871 | * This function must be called with the bus lock held in write mode. |
| 872 | * |
| 873 | * Return: 0 in case of success, a positive I3C error code if the error is |
| 874 | * one of the official Mx error codes, and a negative error code otherwise. |
| 875 | */ |
| 876 | int i3c_master_defslvs_locked(struct i3c_master_controller *master) |
| 877 | { |
| 878 | struct i3c_ccc_defslvs *defslvs; |
| 879 | struct i3c_ccc_dev_desc *desc; |
| 880 | struct i3c_ccc_cmd_dest dest; |
| 881 | struct i3c_dev_desc *i3cdev; |
| 882 | struct i2c_dev_desc *i2cdev; |
| 883 | struct i3c_ccc_cmd cmd; |
| 884 | struct i3c_bus *bus; |
| 885 | bool send = false; |
| 886 | int ndevs = 0, ret; |
| 887 | |
| 888 | if (!master) |
| 889 | return -EINVAL; |
| 890 | |
| 891 | bus = i3c_master_get_bus(master); |
| 892 | i3c_bus_for_each_i3cdev(bus, i3cdev) { |
| 893 | ndevs++; |
| 894 | |
| 895 | if (i3cdev == master->this) |
| 896 | continue; |
| 897 | |
| 898 | if (I3C_BCR_DEVICE_ROLE(i3cdev->info.bcr) == |
| 899 | I3C_BCR_I3C_MASTER) |
| 900 | send = true; |
| 901 | } |
| 902 | |
| 903 | /* No other master on the bus, skip DEFSLVS. */ |
| 904 | if (!send) |
| 905 | return 0; |
| 906 | |
| 907 | i3c_bus_for_each_i2cdev(bus, i2cdev) |
| 908 | ndevs++; |
| 909 | |
| 910 | defslvs = i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR, |
| 911 | struct_size(defslvs, slaves, |
| 912 | ndevs - 1)); |
| 913 | if (!defslvs) |
| 914 | return -ENOMEM; |
| 915 | |
| 916 | defslvs->count = ndevs; |
| 917 | defslvs->master.bcr = master->this->info.bcr; |
| 918 | defslvs->master.dcr = master->this->info.dcr; |
| 919 | defslvs->master.dyn_addr = master->this->info.dyn_addr << 1; |
| 920 | defslvs->master.static_addr = I3C_BROADCAST_ADDR << 1; |
| 921 | |
| 922 | desc = defslvs->slaves; |
| 923 | i3c_bus_for_each_i2cdev(bus, i2cdev) { |
| 924 | desc->lvr = i2cdev->lvr; |
| 925 | desc->static_addr = i2cdev->addr << 1; |
| 926 | desc++; |
| 927 | } |
| 928 | |
| 929 | i3c_bus_for_each_i3cdev(bus, i3cdev) { |
| 930 | /* Skip the I3C dev representing this master. */ |
| 931 | if (i3cdev == master->this) |
| 932 | continue; |
| 933 | |
| 934 | desc->bcr = i3cdev->info.bcr; |
| 935 | desc->dcr = i3cdev->info.dcr; |
| 936 | desc->dyn_addr = i3cdev->info.dyn_addr << 1; |
| 937 | desc->static_addr = i3cdev->info.static_addr << 1; |
| 938 | desc++; |
| 939 | } |
| 940 | |
| 941 | i3c_ccc_cmd_init(&cmd, false, I3C_CCC_DEFSLVS, &dest, 1); |
| 942 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 943 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 944 | |
| 945 | return ret; |
| 946 | } |
| 947 | EXPORT_SYMBOL_GPL(i3c_master_defslvs_locked); |
| 948 | |
| 949 | static int i3c_master_setda_locked(struct i3c_master_controller *master, |
| 950 | u8 oldaddr, u8 newaddr, bool setdasa) |
| 951 | { |
| 952 | struct i3c_ccc_cmd_dest dest; |
| 953 | struct i3c_ccc_setda *setda; |
| 954 | struct i3c_ccc_cmd cmd; |
| 955 | int ret; |
| 956 | |
| 957 | if (!oldaddr || !newaddr) |
| 958 | return -EINVAL; |
| 959 | |
| 960 | setda = i3c_ccc_cmd_dest_init(&dest, oldaddr, sizeof(*setda)); |
| 961 | if (!setda) |
| 962 | return -ENOMEM; |
| 963 | |
| 964 | setda->addr = newaddr << 1; |
| 965 | i3c_ccc_cmd_init(&cmd, false, |
| 966 | setdasa ? I3C_CCC_SETDASA : I3C_CCC_SETNEWDA, |
| 967 | &dest, 1); |
| 968 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 969 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 970 | |
| 971 | return ret; |
| 972 | } |
| 973 | |
| 974 | static int i3c_master_setdasa_locked(struct i3c_master_controller *master, |
| 975 | u8 static_addr, u8 dyn_addr) |
| 976 | { |
| 977 | return i3c_master_setda_locked(master, static_addr, dyn_addr, true); |
| 978 | } |
| 979 | |
| 980 | static int i3c_master_setnewda_locked(struct i3c_master_controller *master, |
| 981 | u8 oldaddr, u8 newaddr) |
| 982 | { |
| 983 | return i3c_master_setda_locked(master, oldaddr, newaddr, false); |
| 984 | } |
| 985 | |
| 986 | static int i3c_master_getmrl_locked(struct i3c_master_controller *master, |
| 987 | struct i3c_device_info *info) |
| 988 | { |
| 989 | struct i3c_ccc_cmd_dest dest; |
| 990 | unsigned int expected_len; |
| 991 | struct i3c_ccc_mrl *mrl; |
| 992 | struct i3c_ccc_cmd cmd; |
| 993 | int ret; |
| 994 | |
| 995 | mrl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mrl)); |
| 996 | if (!mrl) |
| 997 | return -ENOMEM; |
| 998 | |
| 999 | /* |
| 1000 | * When the device does not have IBI payload GETMRL only returns 2 |
| 1001 | * bytes of data. |
| 1002 | */ |
| 1003 | if (!(info->bcr & I3C_BCR_IBI_PAYLOAD)) |
| 1004 | dest.payload.len -= 1; |
| 1005 | |
| 1006 | expected_len = dest.payload.len; |
| 1007 | i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMRL, &dest, 1); |
| 1008 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 1009 | if (ret) |
| 1010 | goto out; |
| 1011 | |
| 1012 | if (dest.payload.len != expected_len) { |
| 1013 | ret = -EIO; |
| 1014 | goto out; |
| 1015 | } |
| 1016 | |
| 1017 | info->max_read_len = be16_to_cpu(mrl->read_len); |
| 1018 | |
| 1019 | if (info->bcr & I3C_BCR_IBI_PAYLOAD) |
| 1020 | info->max_ibi_len = mrl->ibi_len; |
| 1021 | |
| 1022 | out: |
| 1023 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 1024 | |
| 1025 | return ret; |
| 1026 | } |
| 1027 | |
| 1028 | static int i3c_master_getmwl_locked(struct i3c_master_controller *master, |
| 1029 | struct i3c_device_info *info) |
| 1030 | { |
| 1031 | struct i3c_ccc_cmd_dest dest; |
| 1032 | struct i3c_ccc_mwl *mwl; |
| 1033 | struct i3c_ccc_cmd cmd; |
| 1034 | int ret; |
| 1035 | |
| 1036 | mwl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mwl)); |
| 1037 | if (!mwl) |
| 1038 | return -ENOMEM; |
| 1039 | |
| 1040 | i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMWL, &dest, 1); |
| 1041 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 1042 | if (ret) |
| 1043 | goto out; |
| 1044 | |
| 1045 | if (dest.payload.len != sizeof(*mwl)) { |
| 1046 | ret = -EIO; |
| 1047 | goto out; |
| 1048 | } |
| 1049 | |
| 1050 | info->max_write_len = be16_to_cpu(mwl->len); |
| 1051 | |
| 1052 | out: |
| 1053 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 1054 | |
| 1055 | return ret; |
| 1056 | } |
| 1057 | |
| 1058 | static int i3c_master_getmxds_locked(struct i3c_master_controller *master, |
| 1059 | struct i3c_device_info *info) |
| 1060 | { |
| 1061 | struct i3c_ccc_getmxds *getmaxds; |
| 1062 | struct i3c_ccc_cmd_dest dest; |
| 1063 | struct i3c_ccc_cmd cmd; |
| 1064 | int ret; |
| 1065 | |
| 1066 | getmaxds = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, |
| 1067 | sizeof(*getmaxds)); |
| 1068 | if (!getmaxds) |
| 1069 | return -ENOMEM; |
| 1070 | |
| 1071 | i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMXDS, &dest, 1); |
| 1072 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 1073 | if (ret) |
| 1074 | goto out; |
| 1075 | |
| 1076 | if (dest.payload.len != 2 && dest.payload.len != 5) { |
| 1077 | ret = -EIO; |
| 1078 | goto out; |
| 1079 | } |
| 1080 | |
| 1081 | info->max_read_ds = getmaxds->maxrd; |
| 1082 | info->max_write_ds = getmaxds->maxwr; |
| 1083 | if (dest.payload.len == 5) |
| 1084 | info->max_read_turnaround = getmaxds->maxrdturn[0] | |
| 1085 | ((u32)getmaxds->maxrdturn[1] << 8) | |
| 1086 | ((u32)getmaxds->maxrdturn[2] << 16); |
| 1087 | |
| 1088 | out: |
| 1089 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 1090 | |
| 1091 | return ret; |
| 1092 | } |
| 1093 | |
| 1094 | static int i3c_master_gethdrcap_locked(struct i3c_master_controller *master, |
| 1095 | struct i3c_device_info *info) |
| 1096 | { |
| 1097 | struct i3c_ccc_gethdrcap *gethdrcap; |
| 1098 | struct i3c_ccc_cmd_dest dest; |
| 1099 | struct i3c_ccc_cmd cmd; |
| 1100 | int ret; |
| 1101 | |
| 1102 | gethdrcap = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, |
| 1103 | sizeof(*gethdrcap)); |
| 1104 | if (!gethdrcap) |
| 1105 | return -ENOMEM; |
| 1106 | |
| 1107 | i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETHDRCAP, &dest, 1); |
| 1108 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 1109 | if (ret) |
| 1110 | goto out; |
| 1111 | |
| 1112 | if (dest.payload.len != 1) { |
| 1113 | ret = -EIO; |
| 1114 | goto out; |
| 1115 | } |
| 1116 | |
| 1117 | info->hdr_cap = gethdrcap->modes; |
| 1118 | |
| 1119 | out: |
| 1120 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 1121 | |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
| 1125 | static int i3c_master_getpid_locked(struct i3c_master_controller *master, |
| 1126 | struct i3c_device_info *info) |
| 1127 | { |
| 1128 | struct i3c_ccc_getpid *getpid; |
| 1129 | struct i3c_ccc_cmd_dest dest; |
| 1130 | struct i3c_ccc_cmd cmd; |
| 1131 | int ret, i; |
| 1132 | |
| 1133 | getpid = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getpid)); |
| 1134 | if (!getpid) |
| 1135 | return -ENOMEM; |
| 1136 | |
| 1137 | i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETPID, &dest, 1); |
| 1138 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 1139 | if (ret) |
| 1140 | goto out; |
| 1141 | |
| 1142 | info->pid = 0; |
| 1143 | for (i = 0; i < sizeof(getpid->pid); i++) { |
| 1144 | int sft = (sizeof(getpid->pid) - i - 1) * 8; |
| 1145 | |
| 1146 | info->pid |= (u64)getpid->pid[i] << sft; |
| 1147 | } |
| 1148 | |
| 1149 | out: |
| 1150 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 1151 | |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
| 1155 | static int i3c_master_getbcr_locked(struct i3c_master_controller *master, |
| 1156 | struct i3c_device_info *info) |
| 1157 | { |
| 1158 | struct i3c_ccc_getbcr *getbcr; |
| 1159 | struct i3c_ccc_cmd_dest dest; |
| 1160 | struct i3c_ccc_cmd cmd; |
| 1161 | int ret; |
| 1162 | |
| 1163 | getbcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getbcr)); |
| 1164 | if (!getbcr) |
| 1165 | return -ENOMEM; |
| 1166 | |
| 1167 | i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETBCR, &dest, 1); |
| 1168 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 1169 | if (ret) |
| 1170 | goto out; |
| 1171 | |
| 1172 | info->bcr = getbcr->bcr; |
| 1173 | |
| 1174 | out: |
| 1175 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 1176 | |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
| 1180 | static int i3c_master_getdcr_locked(struct i3c_master_controller *master, |
| 1181 | struct i3c_device_info *info) |
| 1182 | { |
| 1183 | struct i3c_ccc_getdcr *getdcr; |
| 1184 | struct i3c_ccc_cmd_dest dest; |
| 1185 | struct i3c_ccc_cmd cmd; |
| 1186 | int ret; |
| 1187 | |
| 1188 | getdcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getdcr)); |
| 1189 | if (!getdcr) |
| 1190 | return -ENOMEM; |
| 1191 | |
| 1192 | i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETDCR, &dest, 1); |
| 1193 | ret = i3c_master_send_ccc_cmd_locked(master, &cmd); |
| 1194 | if (ret) |
| 1195 | goto out; |
| 1196 | |
| 1197 | info->dcr = getdcr->dcr; |
| 1198 | |
| 1199 | out: |
| 1200 | i3c_ccc_cmd_dest_cleanup(&dest); |
| 1201 | |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
| 1205 | static int i3c_master_retrieve_dev_info(struct i3c_dev_desc *dev) |
| 1206 | { |
| 1207 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 1208 | enum i3c_addr_slot_status slot_status; |
| 1209 | int ret; |
| 1210 | |
| 1211 | if (!dev->info.dyn_addr) |
| 1212 | return -EINVAL; |
| 1213 | |
| 1214 | slot_status = i3c_bus_get_addr_slot_status(&master->bus, |
| 1215 | dev->info.dyn_addr); |
| 1216 | if (slot_status == I3C_ADDR_SLOT_RSVD || |
| 1217 | slot_status == I3C_ADDR_SLOT_I2C_DEV) |
| 1218 | return -EINVAL; |
| 1219 | |
| 1220 | ret = i3c_master_getpid_locked(master, &dev->info); |
| 1221 | if (ret) |
| 1222 | return ret; |
| 1223 | |
| 1224 | ret = i3c_master_getbcr_locked(master, &dev->info); |
| 1225 | if (ret) |
| 1226 | return ret; |
| 1227 | |
| 1228 | ret = i3c_master_getdcr_locked(master, &dev->info); |
| 1229 | if (ret) |
| 1230 | return ret; |
| 1231 | |
| 1232 | if (dev->info.bcr & I3C_BCR_MAX_DATA_SPEED_LIM) { |
| 1233 | ret = i3c_master_getmxds_locked(master, &dev->info); |
| 1234 | if (ret) |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
| 1238 | if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD) |
| 1239 | dev->info.max_ibi_len = 1; |
| 1240 | |
| 1241 | i3c_master_getmrl_locked(master, &dev->info); |
| 1242 | i3c_master_getmwl_locked(master, &dev->info); |
| 1243 | |
| 1244 | if (dev->info.bcr & I3C_BCR_HDR_CAP) { |
| 1245 | ret = i3c_master_gethdrcap_locked(master, &dev->info); |
| 1246 | if (ret) |
| 1247 | return ret; |
| 1248 | } |
| 1249 | |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | static void i3c_master_put_i3c_addrs(struct i3c_dev_desc *dev) |
| 1254 | { |
| 1255 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 1256 | |
| 1257 | if (dev->info.static_addr) |
| 1258 | i3c_bus_set_addr_slot_status(&master->bus, |
| 1259 | dev->info.static_addr, |
| 1260 | I3C_ADDR_SLOT_FREE); |
| 1261 | |
| 1262 | if (dev->info.dyn_addr) |
| 1263 | i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr, |
| 1264 | I3C_ADDR_SLOT_FREE); |
| 1265 | |
| 1266 | if (dev->boardinfo && dev->boardinfo->init_dyn_addr) |
| 1267 | i3c_bus_set_addr_slot_status(&master->bus, dev->boardinfo->init_dyn_addr, |
| 1268 | I3C_ADDR_SLOT_FREE); |
| 1269 | } |
| 1270 | |
| 1271 | static int i3c_master_get_i3c_addrs(struct i3c_dev_desc *dev) |
| 1272 | { |
| 1273 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 1274 | enum i3c_addr_slot_status status; |
| 1275 | |
| 1276 | if (!dev->info.static_addr && !dev->info.dyn_addr) |
| 1277 | return 0; |
| 1278 | |
| 1279 | if (dev->info.static_addr) { |
| 1280 | status = i3c_bus_get_addr_slot_status(&master->bus, |
| 1281 | dev->info.static_addr); |
| 1282 | if (status != I3C_ADDR_SLOT_FREE) |
| 1283 | return -EBUSY; |
| 1284 | |
| 1285 | i3c_bus_set_addr_slot_status(&master->bus, |
| 1286 | dev->info.static_addr, |
| 1287 | I3C_ADDR_SLOT_I3C_DEV); |
| 1288 | } |
| 1289 | |
| 1290 | /* |
| 1291 | * ->init_dyn_addr should have been reserved before that, so, if we're |
| 1292 | * trying to apply a pre-reserved dynamic address, we should not try |
| 1293 | * to reserve the address slot a second time. |
| 1294 | */ |
| 1295 | if (dev->info.dyn_addr && |
| 1296 | (!dev->boardinfo || |
| 1297 | dev->boardinfo->init_dyn_addr != dev->info.dyn_addr)) { |
| 1298 | status = i3c_bus_get_addr_slot_status(&master->bus, |
| 1299 | dev->info.dyn_addr); |
| 1300 | if (status != I3C_ADDR_SLOT_FREE) |
| 1301 | goto err_release_static_addr; |
| 1302 | |
| 1303 | i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr, |
| 1304 | I3C_ADDR_SLOT_I3C_DEV); |
| 1305 | } |
| 1306 | |
| 1307 | return 0; |
| 1308 | |
| 1309 | err_release_static_addr: |
| 1310 | if (dev->info.static_addr) |
| 1311 | i3c_bus_set_addr_slot_status(&master->bus, |
| 1312 | dev->info.static_addr, |
| 1313 | I3C_ADDR_SLOT_FREE); |
| 1314 | |
| 1315 | return -EBUSY; |
| 1316 | } |
| 1317 | |
| 1318 | static int i3c_master_attach_i3c_dev(struct i3c_master_controller *master, |
| 1319 | struct i3c_dev_desc *dev) |
| 1320 | { |
| 1321 | int ret; |
| 1322 | |
| 1323 | /* |
| 1324 | * We don't attach devices to the controller until they are |
| 1325 | * addressable on the bus. |
| 1326 | */ |
| 1327 | if (!dev->info.static_addr && !dev->info.dyn_addr) |
| 1328 | return 0; |
| 1329 | |
| 1330 | ret = i3c_master_get_i3c_addrs(dev); |
| 1331 | if (ret) |
| 1332 | return ret; |
| 1333 | |
| 1334 | /* Do not attach the master device itself. */ |
| 1335 | if (master->this != dev && master->ops->attach_i3c_dev) { |
| 1336 | ret = master->ops->attach_i3c_dev(dev); |
| 1337 | if (ret) { |
| 1338 | i3c_master_put_i3c_addrs(dev); |
| 1339 | return ret; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | list_add_tail(&dev->common.node, &master->bus.devs.i3c); |
| 1344 | |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
| 1348 | static int i3c_master_reattach_i3c_dev(struct i3c_dev_desc *dev, |
| 1349 | u8 old_dyn_addr) |
| 1350 | { |
| 1351 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 1352 | enum i3c_addr_slot_status status; |
| 1353 | int ret; |
| 1354 | |
| 1355 | if (dev->info.dyn_addr != old_dyn_addr) { |
| 1356 | status = i3c_bus_get_addr_slot_status(&master->bus, |
| 1357 | dev->info.dyn_addr); |
| 1358 | if (status != I3C_ADDR_SLOT_FREE) |
| 1359 | return -EBUSY; |
| 1360 | i3c_bus_set_addr_slot_status(&master->bus, |
| 1361 | dev->info.dyn_addr, |
| 1362 | I3C_ADDR_SLOT_I3C_DEV); |
| 1363 | } |
| 1364 | |
| 1365 | if (master->ops->reattach_i3c_dev) { |
| 1366 | ret = master->ops->reattach_i3c_dev(dev, old_dyn_addr); |
| 1367 | if (ret) { |
| 1368 | i3c_master_put_i3c_addrs(dev); |
| 1369 | return ret; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
| 1376 | static void i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev) |
| 1377 | { |
| 1378 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 1379 | |
| 1380 | /* Do not detach the master device itself. */ |
| 1381 | if (master->this != dev && master->ops->detach_i3c_dev) |
| 1382 | master->ops->detach_i3c_dev(dev); |
| 1383 | |
| 1384 | i3c_master_put_i3c_addrs(dev); |
| 1385 | list_del(&dev->common.node); |
| 1386 | } |
| 1387 | |
| 1388 | static int i3c_master_attach_i2c_dev(struct i3c_master_controller *master, |
| 1389 | struct i2c_dev_desc *dev) |
| 1390 | { |
| 1391 | int ret; |
| 1392 | |
| 1393 | if (master->ops->attach_i2c_dev) { |
| 1394 | ret = master->ops->attach_i2c_dev(dev); |
| 1395 | if (ret) |
| 1396 | return ret; |
| 1397 | } |
| 1398 | |
| 1399 | list_add_tail(&dev->common.node, &master->bus.devs.i2c); |
| 1400 | |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | static void i3c_master_detach_i2c_dev(struct i2c_dev_desc *dev) |
| 1405 | { |
| 1406 | struct i3c_master_controller *master = i2c_dev_get_master(dev); |
| 1407 | |
| 1408 | list_del(&dev->common.node); |
| 1409 | |
| 1410 | if (master->ops->detach_i2c_dev) |
| 1411 | master->ops->detach_i2c_dev(dev); |
| 1412 | } |
| 1413 | |
| 1414 | static void i3c_master_pre_assign_dyn_addr(struct i3c_dev_desc *dev) |
| 1415 | { |
| 1416 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 1417 | int ret; |
| 1418 | |
| 1419 | if (!dev->boardinfo || !dev->boardinfo->init_dyn_addr || |
| 1420 | !dev->boardinfo->static_addr) |
| 1421 | return; |
| 1422 | |
| 1423 | ret = i3c_master_setdasa_locked(master, dev->info.static_addr, |
| 1424 | dev->boardinfo->init_dyn_addr); |
| 1425 | if (ret) |
| 1426 | return; |
| 1427 | |
| 1428 | dev->info.dyn_addr = dev->boardinfo->init_dyn_addr; |
| 1429 | ret = i3c_master_reattach_i3c_dev(dev, 0); |
| 1430 | if (ret) |
| 1431 | goto err_rstdaa; |
| 1432 | |
| 1433 | ret = i3c_master_retrieve_dev_info(dev); |
| 1434 | if (ret) |
| 1435 | goto err_rstdaa; |
| 1436 | |
| 1437 | return; |
| 1438 | |
| 1439 | err_rstdaa: |
| 1440 | i3c_master_rstdaa_locked(master, dev->boardinfo->init_dyn_addr); |
| 1441 | } |
| 1442 | |
| 1443 | static void |
| 1444 | i3c_master_register_new_i3c_devs(struct i3c_master_controller *master) |
| 1445 | { |
| 1446 | struct i3c_dev_desc *desc; |
| 1447 | int ret; |
| 1448 | |
| 1449 | if (!master->init_done) |
| 1450 | return; |
| 1451 | |
| 1452 | i3c_bus_for_each_i3cdev(&master->bus, desc) { |
| 1453 | if (desc->dev || !desc->info.dyn_addr || desc == master->this) |
| 1454 | continue; |
| 1455 | |
| 1456 | desc->dev = kzalloc(sizeof(*desc->dev), GFP_KERNEL); |
| 1457 | if (!desc->dev) |
| 1458 | continue; |
| 1459 | |
| 1460 | desc->dev->bus = &master->bus; |
| 1461 | desc->dev->desc = desc; |
| 1462 | desc->dev->dev.parent = &master->dev; |
| 1463 | desc->dev->dev.type = &i3c_device_type; |
| 1464 | desc->dev->dev.bus = &i3c_bus_type; |
| 1465 | desc->dev->dev.release = i3c_device_release; |
| 1466 | dev_set_name(&desc->dev->dev, "%d-%llx", master->bus.id, |
| 1467 | desc->info.pid); |
| 1468 | |
| 1469 | if (desc->boardinfo) |
| 1470 | desc->dev->dev.of_node = desc->boardinfo->of_node; |
| 1471 | |
| 1472 | ret = device_register(&desc->dev->dev); |
| 1473 | if (ret) { |
| 1474 | dev_err(&master->dev, |
| 1475 | "Failed to add I3C device (err = %d)\n", ret); |
| 1476 | put_device(&desc->dev->dev); |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment) |
| 1483 | * @master: master doing the DAA |
| 1484 | * |
| 1485 | * This function is instantiating an I3C device object and adding it to the |
| 1486 | * I3C device list. All device information are automatically retrieved using |
| 1487 | * standard CCC commands. |
| 1488 | * |
| 1489 | * The I3C device object is returned in case the master wants to attach |
| 1490 | * private data to it using i3c_dev_set_master_data(). |
| 1491 | * |
| 1492 | * This function must be called with the bus lock held in write mode. |
| 1493 | * |
| 1494 | * Return: a 0 in case of success, an negative error code otherwise. |
| 1495 | */ |
| 1496 | int i3c_master_do_daa(struct i3c_master_controller *master) |
| 1497 | { |
| 1498 | int ret; |
| 1499 | |
| 1500 | i3c_bus_maintenance_lock(&master->bus); |
| 1501 | ret = master->ops->do_daa(master); |
| 1502 | i3c_bus_maintenance_unlock(&master->bus); |
| 1503 | |
| 1504 | if (ret) |
| 1505 | return ret; |
| 1506 | |
| 1507 | i3c_bus_normaluse_lock(&master->bus); |
| 1508 | i3c_master_register_new_i3c_devs(master); |
| 1509 | i3c_bus_normaluse_unlock(&master->bus); |
| 1510 | |
| 1511 | return 0; |
| 1512 | } |
| 1513 | EXPORT_SYMBOL_GPL(i3c_master_do_daa); |
| 1514 | |
| 1515 | /** |
| 1516 | * i3c_master_set_info() - set master device information |
| 1517 | * @master: master used to send frames on the bus |
| 1518 | * @info: I3C device information |
| 1519 | * |
| 1520 | * Set master device info. This should be called from |
| 1521 | * &i3c_master_controller_ops->bus_init(). |
| 1522 | * |
| 1523 | * Not all &i3c_device_info fields are meaningful for a master device. |
| 1524 | * Here is a list of fields that should be properly filled: |
| 1525 | * |
| 1526 | * - &i3c_device_info->dyn_addr |
| 1527 | * - &i3c_device_info->bcr |
| 1528 | * - &i3c_device_info->dcr |
| 1529 | * - &i3c_device_info->pid |
| 1530 | * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in |
| 1531 | * &i3c_device_info->bcr |
| 1532 | * |
| 1533 | * This function must be called with the bus lock held in maintenance mode. |
| 1534 | * |
| 1535 | * Return: 0 if @info contains valid information (not every piece of |
| 1536 | * information can be checked, but we can at least make sure @info->dyn_addr |
| 1537 | * and @info->bcr are correct), -EINVAL otherwise. |
| 1538 | */ |
| 1539 | int i3c_master_set_info(struct i3c_master_controller *master, |
| 1540 | const struct i3c_device_info *info) |
| 1541 | { |
| 1542 | struct i3c_dev_desc *i3cdev; |
| 1543 | int ret; |
| 1544 | |
| 1545 | if (!i3c_bus_dev_addr_is_avail(&master->bus, info->dyn_addr)) |
| 1546 | return -EINVAL; |
| 1547 | |
| 1548 | if (I3C_BCR_DEVICE_ROLE(info->bcr) == I3C_BCR_I3C_MASTER && |
| 1549 | master->secondary) |
| 1550 | return -EINVAL; |
| 1551 | |
| 1552 | if (master->this) |
| 1553 | return -EINVAL; |
| 1554 | |
| 1555 | i3cdev = i3c_master_alloc_i3c_dev(master, info); |
| 1556 | if (IS_ERR(i3cdev)) |
| 1557 | return PTR_ERR(i3cdev); |
| 1558 | |
| 1559 | master->this = i3cdev; |
| 1560 | master->bus.cur_master = master->this; |
| 1561 | |
| 1562 | ret = i3c_master_attach_i3c_dev(master, i3cdev); |
| 1563 | if (ret) |
| 1564 | goto err_free_dev; |
| 1565 | |
| 1566 | return 0; |
| 1567 | |
| 1568 | err_free_dev: |
| 1569 | i3c_master_free_i3c_dev(i3cdev); |
| 1570 | |
| 1571 | return ret; |
| 1572 | } |
| 1573 | EXPORT_SYMBOL_GPL(i3c_master_set_info); |
| 1574 | |
| 1575 | static void i3c_master_detach_free_devs(struct i3c_master_controller *master) |
| 1576 | { |
| 1577 | struct i3c_dev_desc *i3cdev, *i3ctmp; |
| 1578 | struct i2c_dev_desc *i2cdev, *i2ctmp; |
| 1579 | |
| 1580 | list_for_each_entry_safe(i3cdev, i3ctmp, &master->bus.devs.i3c, |
| 1581 | common.node) { |
| 1582 | i3c_master_detach_i3c_dev(i3cdev); |
| 1583 | |
| 1584 | if (i3cdev->boardinfo && i3cdev->boardinfo->init_dyn_addr) |
| 1585 | i3c_bus_set_addr_slot_status(&master->bus, |
| 1586 | i3cdev->boardinfo->init_dyn_addr, |
| 1587 | I3C_ADDR_SLOT_FREE); |
| 1588 | |
| 1589 | i3c_master_free_i3c_dev(i3cdev); |
| 1590 | } |
| 1591 | |
| 1592 | list_for_each_entry_safe(i2cdev, i2ctmp, &master->bus.devs.i2c, |
| 1593 | common.node) { |
| 1594 | i3c_master_detach_i2c_dev(i2cdev); |
| 1595 | i3c_bus_set_addr_slot_status(&master->bus, |
| 1596 | i2cdev->addr, |
| 1597 | I3C_ADDR_SLOT_FREE); |
| 1598 | i3c_master_free_i2c_dev(i2cdev); |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | /** |
| 1603 | * i3c_master_bus_init() - initialize an I3C bus |
| 1604 | * @master: main master initializing the bus |
| 1605 | * |
| 1606 | * This function is following all initialisation steps described in the I3C |
| 1607 | * specification: |
| 1608 | * |
| 1609 | * 1. Attach I2C and statically defined I3C devs to the master so that the |
| 1610 | * master can fill its internal device table appropriately |
| 1611 | * |
| 1612 | * 2. Call &i3c_master_controller_ops->bus_init() method to initialize |
| 1613 | * the master controller. That's usually where the bus mode is selected |
| 1614 | * (pure bus or mixed fast/slow bus) |
| 1615 | * |
| 1616 | * 3. Instruct all devices on the bus to drop their dynamic address. This is |
| 1617 | * particularly important when the bus was previously configured by someone |
| 1618 | * else (for example the bootloader) |
| 1619 | * |
| 1620 | * 4. Disable all slave events. |
| 1621 | * |
| 1622 | * 5. Pre-assign dynamic addresses requested by the FW with SETDASA for I3C |
| 1623 | * devices that have a static address |
| 1624 | * |
| 1625 | * 6. Do a DAA (Dynamic Address Assignment) to assign dynamic addresses to all |
| 1626 | * remaining I3C devices |
| 1627 | * |
| 1628 | * Once this is done, all I3C and I2C devices should be usable. |
| 1629 | * |
| 1630 | * Return: a 0 in case of success, an negative error code otherwise. |
| 1631 | */ |
| 1632 | static int i3c_master_bus_init(struct i3c_master_controller *master) |
| 1633 | { |
| 1634 | enum i3c_addr_slot_status status; |
| 1635 | struct i2c_dev_boardinfo *i2cboardinfo; |
| 1636 | struct i3c_dev_boardinfo *i3cboardinfo; |
| 1637 | struct i3c_dev_desc *i3cdev; |
| 1638 | struct i2c_dev_desc *i2cdev; |
| 1639 | int ret; |
| 1640 | |
| 1641 | /* |
| 1642 | * First attach all devices with static definitions provided by the |
| 1643 | * FW. |
| 1644 | */ |
| 1645 | list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) { |
| 1646 | status = i3c_bus_get_addr_slot_status(&master->bus, |
| 1647 | i2cboardinfo->base.addr); |
| 1648 | if (status != I3C_ADDR_SLOT_FREE) { |
| 1649 | ret = -EBUSY; |
| 1650 | goto err_detach_devs; |
| 1651 | } |
| 1652 | |
| 1653 | i3c_bus_set_addr_slot_status(&master->bus, |
| 1654 | i2cboardinfo->base.addr, |
| 1655 | I3C_ADDR_SLOT_I2C_DEV); |
| 1656 | |
| 1657 | i2cdev = i3c_master_alloc_i2c_dev(master, i2cboardinfo); |
| 1658 | if (IS_ERR(i2cdev)) { |
| 1659 | ret = PTR_ERR(i2cdev); |
| 1660 | goto err_detach_devs; |
| 1661 | } |
| 1662 | |
| 1663 | ret = i3c_master_attach_i2c_dev(master, i2cdev); |
| 1664 | if (ret) { |
| 1665 | i3c_master_free_i2c_dev(i2cdev); |
| 1666 | goto err_detach_devs; |
| 1667 | } |
| 1668 | } |
| 1669 | list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) { |
| 1670 | struct i3c_device_info info = { |
| 1671 | .static_addr = i3cboardinfo->static_addr, |
| 1672 | }; |
| 1673 | |
| 1674 | if (i3cboardinfo->init_dyn_addr) { |
| 1675 | status = i3c_bus_get_addr_slot_status(&master->bus, |
| 1676 | i3cboardinfo->init_dyn_addr); |
| 1677 | if (status != I3C_ADDR_SLOT_FREE) { |
| 1678 | ret = -EBUSY; |
| 1679 | goto err_detach_devs; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | i3cdev = i3c_master_alloc_i3c_dev(master, &info); |
| 1684 | if (IS_ERR(i3cdev)) { |
| 1685 | ret = PTR_ERR(i3cdev); |
| 1686 | goto err_detach_devs; |
| 1687 | } |
| 1688 | |
| 1689 | i3cdev->boardinfo = i3cboardinfo; |
| 1690 | |
| 1691 | ret = i3c_master_attach_i3c_dev(master, i3cdev); |
| 1692 | if (ret) { |
| 1693 | i3c_master_free_i3c_dev(i3cdev); |
| 1694 | goto err_detach_devs; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * Now execute the controller specific ->bus_init() routine, which |
| 1700 | * might configure its internal logic to match the bus limitations. |
| 1701 | */ |
| 1702 | ret = master->ops->bus_init(master); |
| 1703 | if (ret) |
| 1704 | goto err_detach_devs; |
| 1705 | |
| 1706 | /* |
| 1707 | * The master device should have been instantiated in ->bus_init(), |
| 1708 | * complain if this was not the case. |
| 1709 | */ |
| 1710 | if (!master->this) { |
| 1711 | dev_err(&master->dev, |
| 1712 | "master_set_info() was not called in ->bus_init()\n"); |
| 1713 | ret = -EINVAL; |
| 1714 | goto err_bus_cleanup; |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * Reset all dynamic address that may have been assigned before |
| 1719 | * (assigned by the bootloader for example). |
| 1720 | */ |
| 1721 | ret = i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR); |
| 1722 | if (ret && ret != I3C_ERROR_M2) |
| 1723 | goto err_bus_cleanup; |
| 1724 | |
| 1725 | /* Disable all slave events before starting DAA. */ |
| 1726 | ret = i3c_master_disec_locked(master, I3C_BROADCAST_ADDR, |
| 1727 | I3C_CCC_EVENT_SIR | I3C_CCC_EVENT_MR | |
| 1728 | I3C_CCC_EVENT_HJ); |
| 1729 | if (ret && ret != I3C_ERROR_M2) |
| 1730 | goto err_bus_cleanup; |
| 1731 | |
| 1732 | /* |
| 1733 | * Pre-assign dynamic address and retrieve device information if |
| 1734 | * needed. |
| 1735 | */ |
| 1736 | i3c_bus_for_each_i3cdev(&master->bus, i3cdev) |
| 1737 | i3c_master_pre_assign_dyn_addr(i3cdev); |
| 1738 | |
| 1739 | ret = i3c_master_do_daa(master); |
| 1740 | if (ret) |
| 1741 | goto err_rstdaa; |
| 1742 | |
| 1743 | return 0; |
| 1744 | |
| 1745 | err_rstdaa: |
| 1746 | i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR); |
| 1747 | |
| 1748 | err_bus_cleanup: |
| 1749 | if (master->ops->bus_cleanup) |
| 1750 | master->ops->bus_cleanup(master); |
| 1751 | |
| 1752 | err_detach_devs: |
| 1753 | i3c_master_detach_free_devs(master); |
| 1754 | |
| 1755 | return ret; |
| 1756 | } |
| 1757 | |
| 1758 | static void i3c_master_bus_cleanup(struct i3c_master_controller *master) |
| 1759 | { |
| 1760 | if (master->ops->bus_cleanup) |
| 1761 | master->ops->bus_cleanup(master); |
| 1762 | |
| 1763 | i3c_master_detach_free_devs(master); |
| 1764 | } |
| 1765 | |
| 1766 | static void i3c_master_attach_boardinfo(struct i3c_dev_desc *i3cdev) |
| 1767 | { |
| 1768 | struct i3c_master_controller *master = i3cdev->common.master; |
| 1769 | struct i3c_dev_boardinfo *i3cboardinfo; |
| 1770 | |
| 1771 | list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) { |
| 1772 | if (i3cdev->info.pid != i3cboardinfo->pid) |
| 1773 | continue; |
| 1774 | |
| 1775 | i3cdev->boardinfo = i3cboardinfo; |
| 1776 | i3cdev->info.static_addr = i3cboardinfo->static_addr; |
| 1777 | return; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | static struct i3c_dev_desc * |
| 1782 | i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc *refdev) |
| 1783 | { |
| 1784 | struct i3c_master_controller *master = refdev->common.master; |
| 1785 | struct i3c_dev_desc *i3cdev; |
| 1786 | |
| 1787 | i3c_bus_for_each_i3cdev(&master->bus, i3cdev) { |
| 1788 | if (i3cdev != refdev && i3cdev->info.pid == refdev->info.pid) |
| 1789 | return i3cdev; |
| 1790 | } |
| 1791 | |
| 1792 | return NULL; |
| 1793 | } |
| 1794 | |
| 1795 | /** |
| 1796 | * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus |
| 1797 | * @master: master used to send frames on the bus |
| 1798 | * @addr: I3C slave dynamic address assigned to the device |
| 1799 | * |
| 1800 | * This function is instantiating an I3C device object and adding it to the |
| 1801 | * I3C device list. All device information are automatically retrieved using |
| 1802 | * standard CCC commands. |
| 1803 | * |
| 1804 | * The I3C device object is returned in case the master wants to attach |
| 1805 | * private data to it using i3c_dev_set_master_data(). |
| 1806 | * |
| 1807 | * This function must be called with the bus lock held in write mode. |
| 1808 | * |
| 1809 | * Return: a 0 in case of success, an negative error code otherwise. |
| 1810 | */ |
| 1811 | int i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master, |
| 1812 | u8 addr) |
| 1813 | { |
| 1814 | struct i3c_device_info info = { .dyn_addr = addr }; |
| 1815 | struct i3c_dev_desc *newdev, *olddev; |
| 1816 | u8 old_dyn_addr = addr, expected_dyn_addr; |
| 1817 | struct i3c_ibi_setup ibireq = { }; |
| 1818 | bool enable_ibi = false; |
| 1819 | int ret; |
| 1820 | |
| 1821 | if (!master) |
| 1822 | return -EINVAL; |
| 1823 | |
| 1824 | newdev = i3c_master_alloc_i3c_dev(master, &info); |
| 1825 | if (IS_ERR(newdev)) |
| 1826 | return PTR_ERR(newdev); |
| 1827 | |
| 1828 | ret = i3c_master_attach_i3c_dev(master, newdev); |
| 1829 | if (ret) |
| 1830 | goto err_free_dev; |
| 1831 | |
| 1832 | ret = i3c_master_retrieve_dev_info(newdev); |
| 1833 | if (ret) |
| 1834 | goto err_detach_dev; |
| 1835 | |
| 1836 | i3c_master_attach_boardinfo(newdev); |
| 1837 | |
| 1838 | olddev = i3c_master_search_i3c_dev_duplicate(newdev); |
| 1839 | if (olddev) { |
| 1840 | newdev->dev = olddev->dev; |
| 1841 | if (newdev->dev) |
| 1842 | newdev->dev->desc = newdev; |
| 1843 | |
| 1844 | /* |
| 1845 | * We need to restore the IBI state too, so let's save the |
| 1846 | * IBI information and try to restore them after olddev has |
| 1847 | * been detached+released and its IBI has been stopped and |
| 1848 | * the associated resources have been freed. |
| 1849 | */ |
| 1850 | mutex_lock(&olddev->ibi_lock); |
| 1851 | if (olddev->ibi) { |
| 1852 | ibireq.handler = olddev->ibi->handler; |
| 1853 | ibireq.max_payload_len = olddev->ibi->max_payload_len; |
| 1854 | ibireq.num_slots = olddev->ibi->num_slots; |
| 1855 | |
| 1856 | if (olddev->ibi->enabled) { |
| 1857 | enable_ibi = true; |
| 1858 | i3c_dev_disable_ibi_locked(olddev); |
| 1859 | } |
| 1860 | |
| 1861 | i3c_dev_free_ibi_locked(olddev); |
| 1862 | } |
| 1863 | mutex_unlock(&olddev->ibi_lock); |
| 1864 | |
| 1865 | old_dyn_addr = olddev->info.dyn_addr; |
| 1866 | |
| 1867 | i3c_master_detach_i3c_dev(olddev); |
| 1868 | i3c_master_free_i3c_dev(olddev); |
| 1869 | } |
| 1870 | |
| 1871 | ret = i3c_master_reattach_i3c_dev(newdev, old_dyn_addr); |
| 1872 | if (ret) |
| 1873 | goto err_detach_dev; |
| 1874 | |
| 1875 | /* |
| 1876 | * Depending on our previous state, the expected dynamic address might |
| 1877 | * differ: |
| 1878 | * - if the device already had a dynamic address assigned, let's try to |
| 1879 | * re-apply this one |
| 1880 | * - if the device did not have a dynamic address and the firmware |
| 1881 | * requested a specific address, pick this one |
| 1882 | * - in any other case, keep the address automatically assigned by the |
| 1883 | * master |
| 1884 | */ |
| 1885 | if (old_dyn_addr && old_dyn_addr != newdev->info.dyn_addr) |
| 1886 | expected_dyn_addr = old_dyn_addr; |
| 1887 | else if (newdev->boardinfo && newdev->boardinfo->init_dyn_addr) |
| 1888 | expected_dyn_addr = newdev->boardinfo->init_dyn_addr; |
| 1889 | else |
| 1890 | expected_dyn_addr = newdev->info.dyn_addr; |
| 1891 | |
| 1892 | if (newdev->info.dyn_addr != expected_dyn_addr) { |
| 1893 | /* |
| 1894 | * Try to apply the expected dynamic address. If it fails, keep |
| 1895 | * the address assigned by the master. |
| 1896 | */ |
| 1897 | ret = i3c_master_setnewda_locked(master, |
| 1898 | newdev->info.dyn_addr, |
| 1899 | expected_dyn_addr); |
| 1900 | if (!ret) { |
| 1901 | old_dyn_addr = newdev->info.dyn_addr; |
| 1902 | newdev->info.dyn_addr = expected_dyn_addr; |
| 1903 | i3c_master_reattach_i3c_dev(newdev, old_dyn_addr); |
| 1904 | } else { |
| 1905 | dev_err(&master->dev, |
| 1906 | "Failed to assign reserved/old address to device %d%llx", |
| 1907 | master->bus.id, newdev->info.pid); |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * Now is time to try to restore the IBI setup. If we're lucky, |
| 1913 | * everything works as before, otherwise, all we can do is complain. |
| 1914 | * FIXME: maybe we should add callback to inform the driver that it |
| 1915 | * should request the IBI again instead of trying to hide that from |
| 1916 | * him. |
| 1917 | */ |
| 1918 | if (ibireq.handler) { |
| 1919 | mutex_lock(&newdev->ibi_lock); |
| 1920 | ret = i3c_dev_request_ibi_locked(newdev, &ibireq); |
| 1921 | if (ret) { |
| 1922 | dev_err(&master->dev, |
| 1923 | "Failed to request IBI on device %d-%llx", |
| 1924 | master->bus.id, newdev->info.pid); |
| 1925 | } else if (enable_ibi) { |
| 1926 | ret = i3c_dev_enable_ibi_locked(newdev); |
| 1927 | if (ret) |
| 1928 | dev_err(&master->dev, |
| 1929 | "Failed to re-enable IBI on device %d-%llx", |
| 1930 | master->bus.id, newdev->info.pid); |
| 1931 | } |
| 1932 | mutex_unlock(&newdev->ibi_lock); |
| 1933 | } |
| 1934 | |
| 1935 | return 0; |
| 1936 | |
| 1937 | err_detach_dev: |
| 1938 | if (newdev->dev && newdev->dev->desc) |
| 1939 | newdev->dev->desc = NULL; |
| 1940 | |
| 1941 | i3c_master_detach_i3c_dev(newdev); |
| 1942 | |
| 1943 | err_free_dev: |
| 1944 | i3c_master_free_i3c_dev(newdev); |
| 1945 | |
| 1946 | return ret; |
| 1947 | } |
| 1948 | EXPORT_SYMBOL_GPL(i3c_master_add_i3c_dev_locked); |
| 1949 | |
| 1950 | #define OF_I3C_REG1_IS_I2C_DEV BIT(31) |
| 1951 | |
| 1952 | static int |
| 1953 | of_i3c_master_add_i2c_boardinfo(struct i3c_master_controller *master, |
| 1954 | struct device_node *node, u32 *reg) |
| 1955 | { |
| 1956 | struct i2c_dev_boardinfo *boardinfo; |
| 1957 | struct device *dev = &master->dev; |
| 1958 | int ret; |
| 1959 | |
| 1960 | boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL); |
| 1961 | if (!boardinfo) |
| 1962 | return -ENOMEM; |
| 1963 | |
| 1964 | ret = of_i2c_get_board_info(dev, node, &boardinfo->base); |
| 1965 | if (ret) |
| 1966 | return ret; |
| 1967 | |
| 1968 | /* |
| 1969 | * The I3C Specification does not clearly say I2C devices with 10-bit |
| 1970 | * address are supported. These devices can't be passed properly through |
| 1971 | * DEFSLVS command. |
| 1972 | */ |
| 1973 | if (boardinfo->base.flags & I2C_CLIENT_TEN) { |
| 1974 | dev_err(&master->dev, "I2C device with 10 bit address not supported."); |
| 1975 | return -ENOTSUPP; |
| 1976 | } |
| 1977 | |
| 1978 | /* LVR is encoded in reg[2]. */ |
| 1979 | boardinfo->lvr = reg[2]; |
| 1980 | |
| 1981 | list_add_tail(&boardinfo->node, &master->boardinfo.i2c); |
| 1982 | of_node_get(node); |
| 1983 | |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | static int |
| 1988 | of_i3c_master_add_i3c_boardinfo(struct i3c_master_controller *master, |
| 1989 | struct device_node *node, u32 *reg) |
| 1990 | { |
| 1991 | struct i3c_dev_boardinfo *boardinfo; |
| 1992 | struct device *dev = &master->dev; |
| 1993 | enum i3c_addr_slot_status addrstatus; |
| 1994 | u32 init_dyn_addr = 0; |
| 1995 | |
| 1996 | boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL); |
| 1997 | if (!boardinfo) |
| 1998 | return -ENOMEM; |
| 1999 | |
| 2000 | if (reg[0]) { |
| 2001 | if (reg[0] > I3C_MAX_ADDR) |
| 2002 | return -EINVAL; |
| 2003 | |
| 2004 | addrstatus = i3c_bus_get_addr_slot_status(&master->bus, |
| 2005 | reg[0]); |
| 2006 | if (addrstatus != I3C_ADDR_SLOT_FREE) |
| 2007 | return -EINVAL; |
| 2008 | } |
| 2009 | |
| 2010 | boardinfo->static_addr = reg[0]; |
| 2011 | |
| 2012 | if (!of_property_read_u32(node, "assigned-address", &init_dyn_addr)) { |
| 2013 | if (init_dyn_addr > I3C_MAX_ADDR) |
| 2014 | return -EINVAL; |
| 2015 | |
| 2016 | addrstatus = i3c_bus_get_addr_slot_status(&master->bus, |
| 2017 | init_dyn_addr); |
| 2018 | if (addrstatus != I3C_ADDR_SLOT_FREE) |
| 2019 | return -EINVAL; |
| 2020 | } |
| 2021 | |
| 2022 | boardinfo->pid = ((u64)reg[1] << 32) | reg[2]; |
| 2023 | |
| 2024 | if ((boardinfo->pid & GENMASK_ULL(63, 48)) || |
| 2025 | I3C_PID_RND_LOWER_32BITS(boardinfo->pid)) |
| 2026 | return -EINVAL; |
| 2027 | |
| 2028 | boardinfo->init_dyn_addr = init_dyn_addr; |
| 2029 | boardinfo->of_node = of_node_get(node); |
| 2030 | list_add_tail(&boardinfo->node, &master->boardinfo.i3c); |
| 2031 | |
| 2032 | return 0; |
| 2033 | } |
| 2034 | |
| 2035 | static int of_i3c_master_add_dev(struct i3c_master_controller *master, |
| 2036 | struct device_node *node) |
| 2037 | { |
| 2038 | u32 reg[3]; |
| 2039 | int ret; |
| 2040 | |
| 2041 | if (!master || !node) |
| 2042 | return -EINVAL; |
| 2043 | |
| 2044 | ret = of_property_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg)); |
| 2045 | if (ret) |
| 2046 | return ret; |
| 2047 | |
| 2048 | /* |
| 2049 | * The manufacturer ID can't be 0. If reg[1] == 0 that means we're |
| 2050 | * dealing with an I2C device. |
| 2051 | */ |
| 2052 | if (!reg[1]) |
| 2053 | ret = of_i3c_master_add_i2c_boardinfo(master, node, reg); |
| 2054 | else |
| 2055 | ret = of_i3c_master_add_i3c_boardinfo(master, node, reg); |
| 2056 | |
| 2057 | return ret; |
| 2058 | } |
| 2059 | |
| 2060 | static int of_populate_i3c_bus(struct i3c_master_controller *master) |
| 2061 | { |
| 2062 | struct device *dev = &master->dev; |
| 2063 | struct device_node *i3cbus_np = dev->of_node; |
| 2064 | struct device_node *node; |
| 2065 | int ret; |
| 2066 | u32 val; |
| 2067 | |
| 2068 | if (!i3cbus_np) |
| 2069 | return 0; |
| 2070 | |
| 2071 | for_each_available_child_of_node(i3cbus_np, node) { |
| 2072 | ret = of_i3c_master_add_dev(master, node); |
| 2073 | if (ret) { |
| 2074 | of_node_put(node); |
| 2075 | return ret; |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | /* |
| 2080 | * The user might want to limit I2C and I3C speed in case some devices |
| 2081 | * on the bus are not supporting typical rates, or if the bus topology |
| 2082 | * prevents it from using max possible rate. |
| 2083 | */ |
| 2084 | if (!of_property_read_u32(i3cbus_np, "i2c-scl-hz", &val)) |
| 2085 | master->bus.scl_rate.i2c = val; |
| 2086 | |
| 2087 | if (!of_property_read_u32(i3cbus_np, "i3c-scl-hz", &val)) |
| 2088 | master->bus.scl_rate.i3c = val; |
| 2089 | |
| 2090 | return 0; |
| 2091 | } |
| 2092 | |
| 2093 | static int i3c_master_i2c_adapter_xfer(struct i2c_adapter *adap, |
| 2094 | struct i2c_msg *xfers, int nxfers) |
| 2095 | { |
| 2096 | struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap); |
| 2097 | struct i2c_dev_desc *dev; |
| 2098 | int i, ret; |
| 2099 | u16 addr; |
| 2100 | |
| 2101 | if (!xfers || !master || nxfers <= 0) |
| 2102 | return -EINVAL; |
| 2103 | |
| 2104 | if (!master->ops->i2c_xfers) |
| 2105 | return -ENOTSUPP; |
| 2106 | |
| 2107 | /* Doing transfers to different devices is not supported. */ |
| 2108 | addr = xfers[0].addr; |
| 2109 | for (i = 1; i < nxfers; i++) { |
| 2110 | if (addr != xfers[i].addr) |
| 2111 | return -ENOTSUPP; |
| 2112 | } |
| 2113 | |
| 2114 | i3c_bus_normaluse_lock(&master->bus); |
| 2115 | dev = i3c_master_find_i2c_dev_by_addr(master, addr); |
| 2116 | if (!dev) |
| 2117 | ret = -ENOENT; |
| 2118 | else |
| 2119 | ret = master->ops->i2c_xfers(dev, xfers, nxfers); |
| 2120 | i3c_bus_normaluse_unlock(&master->bus); |
| 2121 | |
| 2122 | return ret ? ret : nxfers; |
| 2123 | } |
| 2124 | |
| 2125 | static u32 i3c_master_i2c_funcs(struct i2c_adapter *adapter) |
| 2126 | { |
| 2127 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C; |
| 2128 | } |
| 2129 | |
| 2130 | static const struct i2c_algorithm i3c_master_i2c_algo = { |
| 2131 | .master_xfer = i3c_master_i2c_adapter_xfer, |
| 2132 | .functionality = i3c_master_i2c_funcs, |
| 2133 | }; |
| 2134 | |
| 2135 | static int i3c_master_i2c_adapter_init(struct i3c_master_controller *master) |
| 2136 | { |
| 2137 | struct i2c_adapter *adap = i3c_master_to_i2c_adapter(master); |
| 2138 | struct i2c_dev_desc *i2cdev; |
| 2139 | int ret; |
| 2140 | |
| 2141 | adap->dev.parent = master->dev.parent; |
| 2142 | adap->owner = master->dev.parent->driver->owner; |
| 2143 | adap->algo = &i3c_master_i2c_algo; |
| 2144 | strncpy(adap->name, dev_name(master->dev.parent), sizeof(adap->name)); |
| 2145 | |
| 2146 | /* FIXME: Should we allow i3c masters to override these values? */ |
| 2147 | adap->timeout = 1000; |
| 2148 | adap->retries = 3; |
| 2149 | |
| 2150 | ret = i2c_add_adapter(adap); |
| 2151 | if (ret) |
| 2152 | return ret; |
| 2153 | |
| 2154 | /* |
| 2155 | * We silently ignore failures here. The bus should keep working |
| 2156 | * correctly even if one or more i2c devices are not registered. |
| 2157 | */ |
| 2158 | i3c_bus_for_each_i2cdev(&master->bus, i2cdev) |
| 2159 | i2cdev->dev = i2c_new_device(adap, &i2cdev->boardinfo->base); |
| 2160 | |
| 2161 | return 0; |
| 2162 | } |
| 2163 | |
| 2164 | static void i3c_master_i2c_adapter_cleanup(struct i3c_master_controller *master) |
| 2165 | { |
| 2166 | struct i2c_dev_desc *i2cdev; |
| 2167 | |
| 2168 | i2c_del_adapter(&master->i2c); |
| 2169 | |
| 2170 | i3c_bus_for_each_i2cdev(&master->bus, i2cdev) |
| 2171 | i2cdev->dev = NULL; |
| 2172 | } |
| 2173 | |
| 2174 | static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master) |
| 2175 | { |
| 2176 | struct i3c_dev_desc *i3cdev; |
| 2177 | |
| 2178 | i3c_bus_for_each_i3cdev(&master->bus, i3cdev) { |
| 2179 | if (!i3cdev->dev) |
| 2180 | continue; |
| 2181 | |
| 2182 | i3cdev->dev->desc = NULL; |
| 2183 | if (device_is_registered(&i3cdev->dev->dev)) |
| 2184 | device_unregister(&i3cdev->dev->dev); |
| 2185 | else |
| 2186 | put_device(&i3cdev->dev->dev); |
| 2187 | i3cdev->dev = NULL; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | /** |
| 2192 | * i3c_master_queue_ibi() - Queue an IBI |
| 2193 | * @dev: the device this IBI is coming from |
| 2194 | * @slot: the IBI slot used to store the payload |
| 2195 | * |
| 2196 | * Queue an IBI to the controller workqueue. The IBI handler attached to |
| 2197 | * the dev will be called from a workqueue context. |
| 2198 | */ |
| 2199 | void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot) |
| 2200 | { |
| 2201 | atomic_inc(&dev->ibi->pending_ibis); |
| 2202 | queue_work(dev->common.master->wq, &slot->work); |
| 2203 | } |
| 2204 | EXPORT_SYMBOL_GPL(i3c_master_queue_ibi); |
| 2205 | |
| 2206 | static void i3c_master_handle_ibi(struct work_struct *work) |
| 2207 | { |
| 2208 | struct i3c_ibi_slot *slot = container_of(work, struct i3c_ibi_slot, |
| 2209 | work); |
| 2210 | struct i3c_dev_desc *dev = slot->dev; |
| 2211 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 2212 | struct i3c_ibi_payload payload; |
| 2213 | |
| 2214 | payload.data = slot->data; |
| 2215 | payload.len = slot->len; |
| 2216 | |
| 2217 | if (dev->dev) |
| 2218 | dev->ibi->handler(dev->dev, &payload); |
| 2219 | |
| 2220 | master->ops->recycle_ibi_slot(dev, slot); |
| 2221 | if (atomic_dec_and_test(&dev->ibi->pending_ibis)) |
| 2222 | complete(&dev->ibi->all_ibis_handled); |
| 2223 | } |
| 2224 | |
| 2225 | static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev, |
| 2226 | struct i3c_ibi_slot *slot) |
| 2227 | { |
| 2228 | slot->dev = dev; |
| 2229 | INIT_WORK(&slot->work, i3c_master_handle_ibi); |
| 2230 | } |
| 2231 | |
| 2232 | struct i3c_generic_ibi_slot { |
| 2233 | struct list_head node; |
| 2234 | struct i3c_ibi_slot base; |
| 2235 | }; |
| 2236 | |
| 2237 | struct i3c_generic_ibi_pool { |
| 2238 | spinlock_t lock; |
| 2239 | unsigned int num_slots; |
| 2240 | struct i3c_generic_ibi_slot *slots; |
| 2241 | void *payload_buf; |
| 2242 | struct list_head free_slots; |
| 2243 | struct list_head pending; |
| 2244 | }; |
| 2245 | |
| 2246 | /** |
| 2247 | * i3c_generic_ibi_free_pool() - Free a generic IBI pool |
| 2248 | * @pool: the IBI pool to free |
| 2249 | * |
| 2250 | * Free all IBI slots allated by a generic IBI pool. |
| 2251 | */ |
| 2252 | void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool) |
| 2253 | { |
| 2254 | struct i3c_generic_ibi_slot *slot; |
| 2255 | unsigned int nslots = 0; |
| 2256 | |
| 2257 | while (!list_empty(&pool->free_slots)) { |
| 2258 | slot = list_first_entry(&pool->free_slots, |
| 2259 | struct i3c_generic_ibi_slot, node); |
| 2260 | list_del(&slot->node); |
| 2261 | nslots++; |
| 2262 | } |
| 2263 | |
| 2264 | /* |
| 2265 | * If the number of freed slots is not equal to the number of allocated |
| 2266 | * slots we have a leak somewhere. |
| 2267 | */ |
| 2268 | WARN_ON(nslots != pool->num_slots); |
| 2269 | |
| 2270 | kfree(pool->payload_buf); |
| 2271 | kfree(pool->slots); |
| 2272 | kfree(pool); |
| 2273 | } |
| 2274 | EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool); |
| 2275 | |
| 2276 | /** |
| 2277 | * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool |
| 2278 | * @dev: the device this pool will be used for |
| 2279 | * @req: IBI setup request describing what the device driver expects |
| 2280 | * |
| 2281 | * Create a generic IBI pool based on the information provided in @req. |
| 2282 | * |
| 2283 | * Return: a valid IBI pool in case of success, an ERR_PTR() otherwise. |
| 2284 | */ |
| 2285 | struct i3c_generic_ibi_pool * |
| 2286 | i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev, |
| 2287 | const struct i3c_ibi_setup *req) |
| 2288 | { |
| 2289 | struct i3c_generic_ibi_pool *pool; |
| 2290 | struct i3c_generic_ibi_slot *slot; |
| 2291 | unsigned int i; |
| 2292 | int ret; |
| 2293 | |
| 2294 | pool = kzalloc(sizeof(*pool), GFP_KERNEL); |
| 2295 | if (!pool) |
| 2296 | return ERR_PTR(-ENOMEM); |
| 2297 | |
| 2298 | spin_lock_init(&pool->lock); |
| 2299 | INIT_LIST_HEAD(&pool->free_slots); |
| 2300 | INIT_LIST_HEAD(&pool->pending); |
| 2301 | |
| 2302 | pool->slots = kcalloc(req->num_slots, sizeof(*slot), GFP_KERNEL); |
| 2303 | if (!pool->slots) { |
| 2304 | ret = -ENOMEM; |
| 2305 | goto err_free_pool; |
| 2306 | } |
| 2307 | |
| 2308 | if (req->max_payload_len) { |
| 2309 | pool->payload_buf = kcalloc(req->num_slots, |
| 2310 | req->max_payload_len, GFP_KERNEL); |
| 2311 | if (!pool->payload_buf) { |
| 2312 | ret = -ENOMEM; |
| 2313 | goto err_free_pool; |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | for (i = 0; i < req->num_slots; i++) { |
| 2318 | slot = &pool->slots[i]; |
| 2319 | i3c_master_init_ibi_slot(dev, &slot->base); |
| 2320 | |
| 2321 | if (req->max_payload_len) |
| 2322 | slot->base.data = pool->payload_buf + |
| 2323 | (i * req->max_payload_len); |
| 2324 | |
| 2325 | list_add_tail(&slot->node, &pool->free_slots); |
| 2326 | pool->num_slots++; |
| 2327 | } |
| 2328 | |
| 2329 | return pool; |
| 2330 | |
| 2331 | err_free_pool: |
| 2332 | i3c_generic_ibi_free_pool(pool); |
| 2333 | return ERR_PTR(ret); |
| 2334 | } |
| 2335 | EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool); |
| 2336 | |
| 2337 | /** |
| 2338 | * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool |
| 2339 | * @pool: the pool to query an IBI slot on |
| 2340 | * |
| 2341 | * Search for a free slot in a generic IBI pool. |
| 2342 | * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot() |
| 2343 | * when it's no longer needed. |
| 2344 | * |
| 2345 | * Return: a pointer to a free slot, or NULL if there's no free slot available. |
| 2346 | */ |
| 2347 | struct i3c_ibi_slot * |
| 2348 | i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool) |
| 2349 | { |
| 2350 | struct i3c_generic_ibi_slot *slot; |
| 2351 | unsigned long flags; |
| 2352 | |
| 2353 | spin_lock_irqsave(&pool->lock, flags); |
| 2354 | slot = list_first_entry_or_null(&pool->free_slots, |
| 2355 | struct i3c_generic_ibi_slot, node); |
| 2356 | if (slot) |
| 2357 | list_del(&slot->node); |
| 2358 | spin_unlock_irqrestore(&pool->lock, flags); |
| 2359 | |
| 2360 | return slot ? &slot->base : NULL; |
| 2361 | } |
| 2362 | EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot); |
| 2363 | |
| 2364 | /** |
| 2365 | * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool |
| 2366 | * @pool: the pool to return the IBI slot to |
| 2367 | * @s: IBI slot to recycle |
| 2368 | * |
| 2369 | * Add an IBI slot back to its generic IBI pool. Should be called from the |
| 2370 | * master driver struct_master_controller_ops->recycle_ibi() method. |
| 2371 | */ |
| 2372 | void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool, |
| 2373 | struct i3c_ibi_slot *s) |
| 2374 | { |
| 2375 | struct i3c_generic_ibi_slot *slot; |
| 2376 | unsigned long flags; |
| 2377 | |
| 2378 | if (!s) |
| 2379 | return; |
| 2380 | |
| 2381 | slot = container_of(s, struct i3c_generic_ibi_slot, base); |
| 2382 | spin_lock_irqsave(&pool->lock, flags); |
| 2383 | list_add_tail(&slot->node, &pool->free_slots); |
| 2384 | spin_unlock_irqrestore(&pool->lock, flags); |
| 2385 | } |
| 2386 | EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot); |
| 2387 | |
| 2388 | static int i3c_master_check_ops(const struct i3c_master_controller_ops *ops) |
| 2389 | { |
| 2390 | if (!ops || !ops->bus_init || !ops->priv_xfers || |
| 2391 | !ops->send_ccc_cmd || !ops->do_daa || !ops->i2c_xfers) |
| 2392 | return -EINVAL; |
| 2393 | |
| 2394 | if (ops->request_ibi && |
| 2395 | (!ops->enable_ibi || !ops->disable_ibi || !ops->free_ibi || |
| 2396 | !ops->recycle_ibi_slot)) |
| 2397 | return -EINVAL; |
| 2398 | |
| 2399 | return 0; |
| 2400 | } |
| 2401 | |
| 2402 | /** |
| 2403 | * i3c_master_register() - register an I3C master |
| 2404 | * @master: master used to send frames on the bus |
| 2405 | * @parent: the parent device (the one that provides this I3C master |
| 2406 | * controller) |
| 2407 | * @ops: the master controller operations |
| 2408 | * @secondary: true if you are registering a secondary master. Will return |
| 2409 | * -ENOTSUPP if set to true since secondary masters are not yet |
| 2410 | * supported |
| 2411 | * |
| 2412 | * This function takes care of everything for you: |
| 2413 | * |
| 2414 | * - creates and initializes the I3C bus |
| 2415 | * - populates the bus with static I2C devs if @parent->of_node is not |
| 2416 | * NULL |
| 2417 | * - registers all I3C devices added by the controller during bus |
| 2418 | * initialization |
| 2419 | * - registers the I2C adapter and all I2C devices |
| 2420 | * |
| 2421 | * Return: 0 in case of success, a negative error code otherwise. |
| 2422 | */ |
| 2423 | int i3c_master_register(struct i3c_master_controller *master, |
| 2424 | struct device *parent, |
| 2425 | const struct i3c_master_controller_ops *ops, |
| 2426 | bool secondary) |
| 2427 | { |
| 2428 | unsigned long i2c_scl_rate = I3C_BUS_I2C_FM_PLUS_SCL_RATE; |
| 2429 | struct i3c_bus *i3cbus = i3c_master_get_bus(master); |
| 2430 | enum i3c_bus_mode mode = I3C_BUS_MODE_PURE; |
| 2431 | struct i2c_dev_boardinfo *i2cbi; |
| 2432 | int ret; |
| 2433 | |
| 2434 | /* We do not support secondary masters yet. */ |
| 2435 | if (secondary) |
| 2436 | return -ENOTSUPP; |
| 2437 | |
| 2438 | ret = i3c_master_check_ops(ops); |
| 2439 | if (ret) |
| 2440 | return ret; |
| 2441 | |
| 2442 | master->dev.parent = parent; |
| 2443 | master->dev.of_node = of_node_get(parent->of_node); |
| 2444 | master->dev.bus = &i3c_bus_type; |
| 2445 | master->dev.type = &i3c_masterdev_type; |
| 2446 | master->dev.release = i3c_masterdev_release; |
| 2447 | master->ops = ops; |
| 2448 | master->secondary = secondary; |
| 2449 | INIT_LIST_HEAD(&master->boardinfo.i2c); |
| 2450 | INIT_LIST_HEAD(&master->boardinfo.i3c); |
| 2451 | |
| 2452 | ret = i3c_bus_init(i3cbus); |
| 2453 | if (ret) |
| 2454 | return ret; |
| 2455 | |
| 2456 | device_initialize(&master->dev); |
| 2457 | dev_set_name(&master->dev, "i3c-%d", i3cbus->id); |
| 2458 | |
| 2459 | ret = of_populate_i3c_bus(master); |
| 2460 | if (ret) |
| 2461 | goto err_put_dev; |
| 2462 | |
| 2463 | list_for_each_entry(i2cbi, &master->boardinfo.i2c, node) { |
| 2464 | switch (i2cbi->lvr & I3C_LVR_I2C_INDEX_MASK) { |
| 2465 | case I3C_LVR_I2C_INDEX(0): |
| 2466 | if (mode < I3C_BUS_MODE_MIXED_FAST) |
| 2467 | mode = I3C_BUS_MODE_MIXED_FAST; |
| 2468 | break; |
| 2469 | case I3C_LVR_I2C_INDEX(1): |
| 2470 | if (mode < I3C_BUS_MODE_MIXED_LIMITED) |
| 2471 | mode = I3C_BUS_MODE_MIXED_LIMITED; |
| 2472 | break; |
| 2473 | case I3C_LVR_I2C_INDEX(2): |
| 2474 | if (mode < I3C_BUS_MODE_MIXED_SLOW) |
| 2475 | mode = I3C_BUS_MODE_MIXED_SLOW; |
| 2476 | break; |
| 2477 | default: |
| 2478 | ret = -EINVAL; |
| 2479 | goto err_put_dev; |
| 2480 | } |
| 2481 | |
| 2482 | if (i2cbi->lvr & I3C_LVR_I2C_FM_MODE) |
| 2483 | i2c_scl_rate = I3C_BUS_I2C_FM_SCL_RATE; |
| 2484 | } |
| 2485 | |
| 2486 | ret = i3c_bus_set_mode(i3cbus, mode, i2c_scl_rate); |
| 2487 | if (ret) |
| 2488 | goto err_put_dev; |
| 2489 | |
| 2490 | master->wq = alloc_workqueue("%s", 0, 0, dev_name(parent)); |
| 2491 | if (!master->wq) { |
| 2492 | ret = -ENOMEM; |
| 2493 | goto err_put_dev; |
| 2494 | } |
| 2495 | |
| 2496 | ret = i3c_master_bus_init(master); |
| 2497 | if (ret) |
| 2498 | goto err_put_dev; |
| 2499 | |
| 2500 | ret = device_add(&master->dev); |
| 2501 | if (ret) |
| 2502 | goto err_cleanup_bus; |
| 2503 | |
| 2504 | /* |
| 2505 | * Expose our I3C bus as an I2C adapter so that I2C devices are exposed |
| 2506 | * through the I2C subsystem. |
| 2507 | */ |
| 2508 | ret = i3c_master_i2c_adapter_init(master); |
| 2509 | if (ret) |
| 2510 | goto err_del_dev; |
| 2511 | |
| 2512 | /* |
| 2513 | * We're done initializing the bus and the controller, we can now |
| 2514 | * register I3C devices dicovered during the initial DAA. |
| 2515 | */ |
| 2516 | master->init_done = true; |
| 2517 | i3c_bus_normaluse_lock(&master->bus); |
| 2518 | i3c_master_register_new_i3c_devs(master); |
| 2519 | i3c_bus_normaluse_unlock(&master->bus); |
| 2520 | |
| 2521 | return 0; |
| 2522 | |
| 2523 | err_del_dev: |
| 2524 | device_del(&master->dev); |
| 2525 | |
| 2526 | err_cleanup_bus: |
| 2527 | i3c_master_bus_cleanup(master); |
| 2528 | |
| 2529 | err_put_dev: |
| 2530 | put_device(&master->dev); |
| 2531 | |
| 2532 | return ret; |
| 2533 | } |
| 2534 | EXPORT_SYMBOL_GPL(i3c_master_register); |
| 2535 | |
| 2536 | /** |
| 2537 | * i3c_master_unregister() - unregister an I3C master |
| 2538 | * @master: master used to send frames on the bus |
| 2539 | * |
| 2540 | * Basically undo everything done in i3c_master_register(). |
| 2541 | * |
| 2542 | * Return: 0 in case of success, a negative error code otherwise. |
| 2543 | */ |
| 2544 | int i3c_master_unregister(struct i3c_master_controller *master) |
| 2545 | { |
| 2546 | i3c_master_i2c_adapter_cleanup(master); |
| 2547 | i3c_master_unregister_i3c_devs(master); |
| 2548 | i3c_master_bus_cleanup(master); |
| 2549 | device_unregister(&master->dev); |
| 2550 | |
| 2551 | return 0; |
| 2552 | } |
| 2553 | EXPORT_SYMBOL_GPL(i3c_master_unregister); |
| 2554 | |
| 2555 | int i3c_dev_do_priv_xfers_locked(struct i3c_dev_desc *dev, |
| 2556 | struct i3c_priv_xfer *xfers, |
| 2557 | int nxfers) |
| 2558 | { |
| 2559 | struct i3c_master_controller *master; |
| 2560 | |
| 2561 | if (!dev) |
| 2562 | return -ENOENT; |
| 2563 | |
| 2564 | master = i3c_dev_get_master(dev); |
| 2565 | if (!master || !xfers) |
| 2566 | return -EINVAL; |
| 2567 | |
| 2568 | if (!master->ops->priv_xfers) |
| 2569 | return -ENOTSUPP; |
| 2570 | |
| 2571 | return master->ops->priv_xfers(dev, xfers, nxfers); |
| 2572 | } |
| 2573 | |
| 2574 | int i3c_dev_disable_ibi_locked(struct i3c_dev_desc *dev) |
| 2575 | { |
| 2576 | struct i3c_master_controller *master; |
| 2577 | int ret; |
| 2578 | |
| 2579 | if (!dev->ibi) |
| 2580 | return -EINVAL; |
| 2581 | |
| 2582 | master = i3c_dev_get_master(dev); |
| 2583 | ret = master->ops->disable_ibi(dev); |
| 2584 | if (ret) |
| 2585 | return ret; |
| 2586 | |
| 2587 | reinit_completion(&dev->ibi->all_ibis_handled); |
| 2588 | if (atomic_read(&dev->ibi->pending_ibis)) |
| 2589 | wait_for_completion(&dev->ibi->all_ibis_handled); |
| 2590 | |
| 2591 | dev->ibi->enabled = false; |
| 2592 | |
| 2593 | return 0; |
| 2594 | } |
| 2595 | |
| 2596 | int i3c_dev_enable_ibi_locked(struct i3c_dev_desc *dev) |
| 2597 | { |
| 2598 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 2599 | int ret; |
| 2600 | |
| 2601 | if (!dev->ibi) |
| 2602 | return -EINVAL; |
| 2603 | |
| 2604 | ret = master->ops->enable_ibi(dev); |
| 2605 | if (!ret) |
| 2606 | dev->ibi->enabled = true; |
| 2607 | |
| 2608 | return ret; |
| 2609 | } |
| 2610 | |
| 2611 | int i3c_dev_request_ibi_locked(struct i3c_dev_desc *dev, |
| 2612 | const struct i3c_ibi_setup *req) |
| 2613 | { |
| 2614 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 2615 | struct i3c_device_ibi_info *ibi; |
| 2616 | int ret; |
| 2617 | |
| 2618 | if (!master->ops->request_ibi) |
| 2619 | return -ENOTSUPP; |
| 2620 | |
| 2621 | if (dev->ibi) |
| 2622 | return -EBUSY; |
| 2623 | |
| 2624 | ibi = kzalloc(sizeof(*ibi), GFP_KERNEL); |
| 2625 | if (!ibi) |
| 2626 | return -ENOMEM; |
| 2627 | |
| 2628 | atomic_set(&ibi->pending_ibis, 0); |
| 2629 | init_completion(&ibi->all_ibis_handled); |
| 2630 | ibi->handler = req->handler; |
| 2631 | ibi->max_payload_len = req->max_payload_len; |
| 2632 | ibi->num_slots = req->num_slots; |
| 2633 | |
| 2634 | dev->ibi = ibi; |
| 2635 | ret = master->ops->request_ibi(dev, req); |
| 2636 | if (ret) { |
| 2637 | kfree(ibi); |
| 2638 | dev->ibi = NULL; |
| 2639 | } |
| 2640 | |
| 2641 | return ret; |
| 2642 | } |
| 2643 | |
| 2644 | void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev) |
| 2645 | { |
| 2646 | struct i3c_master_controller *master = i3c_dev_get_master(dev); |
| 2647 | |
| 2648 | if (!dev->ibi) |
| 2649 | return; |
| 2650 | |
| 2651 | if (WARN_ON(dev->ibi->enabled)) |
| 2652 | WARN_ON(i3c_dev_disable_ibi_locked(dev)); |
| 2653 | |
| 2654 | master->ops->free_ibi(dev); |
| 2655 | kfree(dev->ibi); |
| 2656 | dev->ibi = NULL; |
| 2657 | } |
| 2658 | |
| 2659 | static int __init i3c_init(void) |
| 2660 | { |
| 2661 | return bus_register(&i3c_bus_type); |
| 2662 | } |
| 2663 | subsys_initcall(i3c_init); |
| 2664 | |
| 2665 | static void __exit i3c_exit(void) |
| 2666 | { |
| 2667 | idr_destroy(&i3c_bus_idr); |
| 2668 | bus_unregister(&i3c_bus_type); |
| 2669 | } |
| 2670 | module_exit(i3c_exit); |
| 2671 | |
| 2672 | MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>"); |
| 2673 | MODULE_DESCRIPTION("I3C core"); |
| 2674 | MODULE_LICENSE("GPL v2"); |