Lines Matching +full:i3c +full:- +full:scl +full:- +full:hz

1 // SPDX-License-Identifier: GPL-2.0
28 * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation
29 * @bus: I3C bus to take the lock on
33 * - enabling/disabling slave events
34 * - re-triggering DAA
35 * - changing the dynamic address of a device
36 * - relinquishing mastership
37 * - ...
40 * logic to rely on I3C device information that could be changed behind their
45 down_write(&bus->lock); in i3c_bus_maintenance_lock()
49 * i3c_bus_maintenance_unlock - Release the bus lock after a maintenance
51 * @bus: I3C bus to release the lock on
59 up_write(&bus->lock); in i3c_bus_maintenance_unlock()
63 * i3c_bus_normaluse_lock - Lock the bus for a normal operation
64 * @bus: I3C bus to take the lock on
67 * operation (see i3c_bus_maintenance_lock() for a non-exhaustive list of
68 * maintenance operations). Basically all communications with I3C devices are
70 * state or I3C dynamic address).
73 * In other words, transfer requests passed to the I3C master can be submitted
74 * in parallel and I3C master drivers have to use their own locking to make
75 * sure two different communications are not inter-mixed, or access to the
80 down_read(&bus->lock); in i3c_bus_normaluse_lock()
84 * i3c_bus_normaluse_unlock - Release the bus lock after a normal operation
85 * @bus: I3C bus to release the lock on
93 up_read(&bus->lock); in i3c_bus_normaluse_unlock()
113 if (dev->type == &i3c_device_type) in dev_to_i3cbus()
114 return dev_to_i3cdev(dev)->bus; in dev_to_i3cbus()
118 return &master->bus; in dev_to_i3cbus()
125 if (dev->type == &i3c_device_type) in dev_to_i3cdesc()
126 return dev_to_i3cdev(dev)->desc; in dev_to_i3cdesc()
130 return master->this; in dev_to_i3cdesc()
143 ret = sprintf(buf, "%x\n", desc->info.bcr); in bcr_show()
160 ret = sprintf(buf, "%x\n", desc->info.dcr); in dcr_show()
177 ret = sprintf(buf, "%llx\n", desc->info.pid); in pid_show()
194 ret = sprintf(buf, "%02x\n", desc->info.dyn_addr); in dynamic_address_show()
202 "hdr-ddr", "hdr-tsp", "hdr-tsl",
217 caps = desc->info.hdr_cap; in hdrcap_show()
249 struct i3c_device *i3c = dev_to_i3cdev(dev); in modalias_show() local
253 i3c_device_get_info(i3c, &devinfo); in modalias_show()
259 return sprintf(buf, "i3c:dcr%02Xmanuf%04X", devinfo.dcr, in modalias_show()
262 return sprintf(buf, "i3c:dcr%02Xmanuf%04Xpart%04Xext%04X", in modalias_show()
284 if (i3cdev->desc) in i3c_device_uevent()
285 devinfo = i3cdev->desc->info; in i3c_device_uevent()
291 return add_uevent_var(env, "MODALIAS=i3c:dcr%02Xmanuf%04X", in i3c_device_uevent()
295 "MODALIAS=i3c:dcr%02Xmanuf%04Xpart%04Xext%04X", in i3c_device_uevent()
309 if (dev->type != &i3c_device_type) in i3c_device_match()
314 if (i3c_device_match_id(i3cdev, i3cdrv->id_table)) in i3c_device_match()
323 struct i3c_driver *driver = drv_to_i3cdrv(dev->driver); in i3c_device_probe()
325 return driver->probe(i3cdev); in i3c_device_probe()
331 struct i3c_driver *driver = drv_to_i3cdrv(dev->driver); in i3c_device_remove()
333 if (driver->remove) in i3c_device_remove()
334 driver->remove(i3cdev); in i3c_device_remove()
340 .name = "i3c",
355 status = bus->addrslots[bitpos / BITS_PER_LONG]; in i3c_bus_get_addr_slot_status_mask()
376 ptr = bus->addrslots + (bitpos / BITS_PER_LONG); in i3c_bus_set_addr_slot_status_mask()
405 * sending it out to the I3C bus. This means that a 7-bit dynamic address needs to be allocated
408 * However, some I3C targets may request specific addresses (called as "init_dyn_addr"), which is
409 * typically specified by the DT-'s assigned-address property. Lower addresses having higher IBI
412 * the "init_dyn_addr" to switch to its "init_dyn_addr" when it hot-joins the I3C bus. Otherwise,
413 * if the "init_dyn_addr" is already in use by another I3C device, the target device will not be
438 return -ENOMEM; in i3c_bus_get_free_addr()
463 idr_remove(&i3c_bus_idr, i3cbus->id); in i3c_bus_cleanup()
469 int ret, start, end, id = -1; in i3c_bus_init()
471 init_rwsem(&i3cbus->lock); in i3c_bus_init()
472 INIT_LIST_HEAD(&i3cbus->devs.i2c); in i3c_bus_init()
473 INIT_LIST_HEAD(&i3cbus->devs.i3c); in i3c_bus_init()
475 i3cbus->mode = I3C_BUS_MODE_PURE; in i3c_bus_init()
478 id = of_alias_get_id(np, "i3c"); in i3c_bus_init()
495 i3cbus->id = ret; in i3c_bus_init()
532 [I3C_BUS_MODE_MIXED_FAST] = "mixed-fast",
533 [I3C_BUS_MODE_MIXED_LIMITED] = "mixed-limited",
534 [I3C_BUS_MODE_MIXED_SLOW] = "mixed-slow",
545 if (i3cbus->mode < 0 || in mode_show()
546 i3cbus->mode >= ARRAY_SIZE(i3c_bus_mode_strings) || in mode_show()
547 !i3c_bus_mode_strings[i3cbus->mode]) in mode_show()
550 ret = sprintf(buf, "%s\n", i3c_bus_mode_strings[i3cbus->mode]); in mode_show()
565 ret = sprintf(buf, "%d-%llx\n", i3cbus->id, in current_master_show()
566 i3cbus->cur_master->info.pid); in current_master_show()
581 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i3c); in i3c_scl_frequency_show()
596 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i2c); in i2c_scl_frequency_show()
607 if (!master || !master->ops) in i3c_set_hotjoin()
608 return -EINVAL; in i3c_set_hotjoin()
610 if (!master->ops->enable_hotjoin || !master->ops->disable_hotjoin) in i3c_set_hotjoin()
611 return -EINVAL; in i3c_set_hotjoin()
613 i3c_bus_normaluse_lock(&master->bus); in i3c_set_hotjoin()
616 ret = master->ops->enable_hotjoin(master); in i3c_set_hotjoin()
618 ret = master->ops->disable_hotjoin(master); in i3c_set_hotjoin()
620 master->hotjoin = enable; in i3c_set_hotjoin()
622 i3c_bus_normaluse_unlock(&master->bus); in i3c_set_hotjoin()
634 if (!i3cbus->cur_master) in hotjoin_store()
635 return -EINVAL; in hotjoin_store()
638 return -EINVAL; in hotjoin_store()
640 ret = i3c_set_hotjoin(i3cbus->cur_master->common.master, res); in hotjoin_store()
648 * i3c_master_enable_hotjoin - Enable hotjoin
649 * @master: I3C master object
660 * i3c_master_disable_hotjoin - Disable hotjoin
661 * @master: I3C master object
677 ret = sysfs_emit(buf, "%d\n", i3cbus->cur_master->common.master->hotjoin); in hotjoin_show()
705 if (master->wq) in i3c_masterdev_release()
706 destroy_workqueue(master->wq); in i3c_masterdev_release()
708 WARN_ON(!list_empty(&bus->devs.i2c) || !list_empty(&bus->devs.i3c)); in i3c_masterdev_release()
711 of_node_put(dev->of_node); in i3c_masterdev_release()
723 i3cbus->mode = mode; in i3c_bus_set_mode()
725 switch (i3cbus->mode) { in i3c_bus_set_mode()
727 if (!i3cbus->scl_rate.i3c) in i3c_bus_set_mode()
728 i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE; in i3c_bus_set_mode()
732 if (!i3cbus->scl_rate.i3c) in i3c_bus_set_mode()
733 i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE; in i3c_bus_set_mode()
734 if (!i3cbus->scl_rate.i2c) in i3c_bus_set_mode()
735 i3cbus->scl_rate.i2c = max_i2c_scl_rate; in i3c_bus_set_mode()
738 if (!i3cbus->scl_rate.i2c) in i3c_bus_set_mode()
739 i3cbus->scl_rate.i2c = max_i2c_scl_rate; in i3c_bus_set_mode()
740 if (!i3cbus->scl_rate.i3c || in i3c_bus_set_mode()
741 i3cbus->scl_rate.i3c > i3cbus->scl_rate.i2c) in i3c_bus_set_mode()
742 i3cbus->scl_rate.i3c = i3cbus->scl_rate.i2c; in i3c_bus_set_mode()
745 return -EINVAL; in i3c_bus_set_mode()
748 dev_dbg(&master->dev, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n", in i3c_bus_set_mode()
749 i3cbus->scl_rate.i2c, i3cbus->scl_rate.i3c); in i3c_bus_set_mode()
752 * I3C/I2C frequency may have been overridden, check that user-provided in i3c_bus_set_mode()
755 if (i3cbus->scl_rate.i3c > I3C_BUS_MAX_I3C_SCL_RATE || in i3c_bus_set_mode()
756 i3cbus->scl_rate.i2c > I3C_BUS_I2C_FM_PLUS_SCL_RATE) in i3c_bus_set_mode()
757 return -EINVAL; in i3c_bus_set_mode()
771 return &master->i2c; in i3c_master_to_i2c_adapter()
787 return ERR_PTR(-ENOMEM); in i3c_master_alloc_i2c_dev()
789 dev->common.master = master; in i3c_master_alloc_i2c_dev()
790 dev->addr = addr; in i3c_master_alloc_i2c_dev()
791 dev->lvr = lvr; in i3c_master_alloc_i2c_dev()
799 dest->addr = addr; in i3c_ccc_cmd_dest_init()
800 dest->payload.len = payloadlen; in i3c_ccc_cmd_dest_init()
802 dest->payload.data = kzalloc(payloadlen, GFP_KERNEL); in i3c_ccc_cmd_dest_init()
804 dest->payload.data = NULL; in i3c_ccc_cmd_dest_init()
806 return dest->payload.data; in i3c_ccc_cmd_dest_init()
811 kfree(dest->payload.data); in i3c_ccc_cmd_dest_cleanup()
818 cmd->rnw = rnw ? 1 : 0; in i3c_ccc_cmd_init()
819 cmd->id = id; in i3c_ccc_cmd_init()
820 cmd->dests = dests; in i3c_ccc_cmd_init()
821 cmd->ndests = ndests; in i3c_ccc_cmd_init()
822 cmd->err = I3C_ERROR_UNKNOWN; in i3c_ccc_cmd_init()
831 return -EINVAL; in i3c_master_send_ccc_cmd_locked()
833 if (WARN_ON(master->init_done && in i3c_master_send_ccc_cmd_locked()
834 !rwsem_is_locked(&master->bus.lock))) in i3c_master_send_ccc_cmd_locked()
835 return -EINVAL; in i3c_master_send_ccc_cmd_locked()
837 if (!master->ops->send_ccc_cmd) in i3c_master_send_ccc_cmd_locked()
838 return -ENOTSUPP; in i3c_master_send_ccc_cmd_locked()
840 if ((cmd->id & I3C_CCC_DIRECT) && (!cmd->dests || !cmd->ndests)) in i3c_master_send_ccc_cmd_locked()
841 return -EINVAL; in i3c_master_send_ccc_cmd_locked()
843 if (master->ops->supports_ccc_cmd && in i3c_master_send_ccc_cmd_locked()
844 !master->ops->supports_ccc_cmd(master, cmd)) in i3c_master_send_ccc_cmd_locked()
845 return -ENOTSUPP; in i3c_master_send_ccc_cmd_locked()
847 ret = master->ops->send_ccc_cmd(master, cmd); in i3c_master_send_ccc_cmd_locked()
849 if (cmd->err != I3C_ERROR_UNKNOWN) in i3c_master_send_ccc_cmd_locked()
850 return cmd->err; in i3c_master_send_ccc_cmd_locked()
864 i3c_bus_for_each_i2cdev(&master->bus, dev) { in i3c_master_find_i2c_dev_by_addr()
865 if (dev->addr == addr) in i3c_master_find_i2c_dev_by_addr()
873 * i3c_master_get_free_addr() - get a free address on the bus
874 * @master: I3C master object
879 * Return: the first free address starting at @start_addr (included) or -ENOMEM
885 return i3c_bus_get_free_addr(&master->bus, start_addr); in i3c_master_get_free_addr()
893 WARN_ON(i3cdev->desc); in i3c_device_release()
895 of_node_put(i3cdev->dev.of_node); in i3c_device_release()
912 return ERR_PTR(-ENOMEM); in i3c_master_alloc_i3c_dev()
914 dev->common.master = master; in i3c_master_alloc_i3c_dev()
915 dev->info = *info; in i3c_master_alloc_i3c_dev()
916 mutex_init(&dev->ibi_lock); in i3c_master_alloc_i3c_dev()
930 return -EINVAL; in i3c_master_rstdaa_locked()
932 addrstat = i3c_bus_get_addr_slot_status(&master->bus, addr); in i3c_master_rstdaa_locked()
934 return -EINVAL; in i3c_master_rstdaa_locked()
947 * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment)
954 * behind dynamic address assignment has to be handled in the I3C master
959 * Return: 0 in case of success, a positive I3C error code if the error is
987 return -ENOMEM; in i3c_master_enec_disec_locked()
989 events->events = evts; in i3c_master_enec_disec_locked()
1002 * i3c_master_disec_locked() - send a DISEC CCC command
1004 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
1012 * Return: 0 in case of success, a positive I3C error code if the error is
1023 * i3c_master_enec_locked() - send an ENEC CCC command
1025 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
1033 * Return: 0 in case of success, a positive I3C error code if the error is
1044 * i3c_master_defslvs_locked() - send a DEFSLVS CCC command
1051 * This should be called after all I3C devices have been discovered (in other
1053 * &i3c_master_controller_ops->bus_init().
1054 * It should also be called if a master ACKed an Hot-Join request and assigned
1059 * Return: 0 in case of success, a positive I3C error code if the error is
1075 return -EINVAL; in i3c_master_defslvs_locked()
1081 if (i3cdev == master->this) in i3c_master_defslvs_locked()
1084 if (I3C_BCR_DEVICE_ROLE(i3cdev->info.bcr) == in i3c_master_defslvs_locked()
1098 ndevs - 1)); in i3c_master_defslvs_locked()
1100 return -ENOMEM; in i3c_master_defslvs_locked()
1102 defslvs->count = ndevs; in i3c_master_defslvs_locked()
1103 defslvs->master.bcr = master->this->info.bcr; in i3c_master_defslvs_locked()
1104 defslvs->master.dcr = master->this->info.dcr; in i3c_master_defslvs_locked()
1105 defslvs->master.dyn_addr = master->this->info.dyn_addr << 1; in i3c_master_defslvs_locked()
1106 defslvs->master.static_addr = I3C_BROADCAST_ADDR << 1; in i3c_master_defslvs_locked()
1108 desc = defslvs->slaves; in i3c_master_defslvs_locked()
1110 desc->lvr = i2cdev->lvr; in i3c_master_defslvs_locked()
1111 desc->static_addr = i2cdev->addr << 1; in i3c_master_defslvs_locked()
1116 /* Skip the I3C dev representing this master. */ in i3c_master_defslvs_locked()
1117 if (i3cdev == master->this) in i3c_master_defslvs_locked()
1120 desc->bcr = i3cdev->info.bcr; in i3c_master_defslvs_locked()
1121 desc->dcr = i3cdev->info.dcr; in i3c_master_defslvs_locked()
1122 desc->dyn_addr = i3cdev->info.dyn_addr << 1; in i3c_master_defslvs_locked()
1123 desc->static_addr = i3cdev->info.static_addr << 1; in i3c_master_defslvs_locked()
1144 return -EINVAL; in i3c_master_setda_locked()
1148 return -ENOMEM; in i3c_master_setda_locked()
1150 setda->addr = newaddr << 1; in i3c_master_setda_locked()
1180 mrl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mrl)); in i3c_master_getmrl_locked()
1182 return -ENOMEM; in i3c_master_getmrl_locked()
1188 if (!(info->bcr & I3C_BCR_IBI_PAYLOAD)) in i3c_master_getmrl_locked()
1189 dest.payload.len -= 1; in i3c_master_getmrl_locked()
1198 info->max_ibi_len = mrl->ibi_len; in i3c_master_getmrl_locked()
1201 info->max_read_len = be16_to_cpu(mrl->read_len); in i3c_master_getmrl_locked()
1204 ret = -EIO; in i3c_master_getmrl_locked()
1222 mwl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mwl)); in i3c_master_getmwl_locked()
1224 return -ENOMEM; in i3c_master_getmwl_locked()
1232 ret = -EIO; in i3c_master_getmwl_locked()
1236 info->max_write_len = be16_to_cpu(mwl->len); in i3c_master_getmwl_locked()
1252 getmaxds = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, in i3c_master_getmxds_locked()
1255 return -ENOMEM; in i3c_master_getmxds_locked()
1263 ret = -EIO; in i3c_master_getmxds_locked()
1267 info->max_read_ds = getmaxds->maxrd; in i3c_master_getmxds_locked()
1268 info->max_write_ds = getmaxds->maxwr; in i3c_master_getmxds_locked()
1270 info->max_read_turnaround = getmaxds->maxrdturn[0] | in i3c_master_getmxds_locked()
1271 ((u32)getmaxds->maxrdturn[1] << 8) | in i3c_master_getmxds_locked()
1272 ((u32)getmaxds->maxrdturn[2] << 16); in i3c_master_getmxds_locked()
1288 gethdrcap = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, in i3c_master_gethdrcap_locked()
1291 return -ENOMEM; in i3c_master_gethdrcap_locked()
1299 ret = -EIO; in i3c_master_gethdrcap_locked()
1303 info->hdr_cap = gethdrcap->modes; in i3c_master_gethdrcap_locked()
1319 getpid = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getpid)); in i3c_master_getpid_locked()
1321 return -ENOMEM; in i3c_master_getpid_locked()
1328 info->pid = 0; in i3c_master_getpid_locked()
1329 for (i = 0; i < sizeof(getpid->pid); i++) { in i3c_master_getpid_locked()
1330 int sft = (sizeof(getpid->pid) - i - 1) * 8; in i3c_master_getpid_locked()
1332 info->pid |= (u64)getpid->pid[i] << sft; in i3c_master_getpid_locked()
1349 getbcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getbcr)); in i3c_master_getbcr_locked()
1351 return -ENOMEM; in i3c_master_getbcr_locked()
1358 info->bcr = getbcr->bcr; in i3c_master_getbcr_locked()
1374 getdcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getdcr)); in i3c_master_getdcr_locked()
1376 return -ENOMEM; in i3c_master_getdcr_locked()
1383 info->dcr = getdcr->dcr; in i3c_master_getdcr_locked()
1397 if (!dev->info.dyn_addr) in i3c_master_retrieve_dev_info()
1398 return -EINVAL; in i3c_master_retrieve_dev_info()
1400 slot_status = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_retrieve_dev_info()
1401 dev->info.dyn_addr); in i3c_master_retrieve_dev_info()
1404 return -EINVAL; in i3c_master_retrieve_dev_info()
1406 ret = i3c_master_getpid_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1410 ret = i3c_master_getbcr_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1414 ret = i3c_master_getdcr_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1418 if (dev->info.bcr & I3C_BCR_MAX_DATA_SPEED_LIM) { in i3c_master_retrieve_dev_info()
1419 ret = i3c_master_getmxds_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1424 if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD) in i3c_master_retrieve_dev_info()
1425 dev->info.max_ibi_len = 1; in i3c_master_retrieve_dev_info()
1427 i3c_master_getmrl_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1428 i3c_master_getmwl_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1430 if (dev->info.bcr & I3C_BCR_HDR_CAP) { in i3c_master_retrieve_dev_info()
1431 ret = i3c_master_gethdrcap_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1443 if (dev->info.static_addr) in i3c_master_put_i3c_addrs()
1444 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_put_i3c_addrs()
1445 dev->info.static_addr, in i3c_master_put_i3c_addrs()
1448 if (dev->info.dyn_addr) in i3c_master_put_i3c_addrs()
1449 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr, in i3c_master_put_i3c_addrs()
1452 if (dev->boardinfo && dev->boardinfo->init_dyn_addr) in i3c_master_put_i3c_addrs()
1453 i3c_bus_set_addr_slot_status(&master->bus, dev->boardinfo->init_dyn_addr, in i3c_master_put_i3c_addrs()
1462 if (!dev->info.static_addr && !dev->info.dyn_addr) in i3c_master_get_i3c_addrs()
1465 if (dev->info.static_addr) { in i3c_master_get_i3c_addrs()
1466 status = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_get_i3c_addrs()
1467 dev->info.static_addr); in i3c_master_get_i3c_addrs()
1472 dev->info.static_addr != dev->boardinfo->init_dyn_addr) in i3c_master_get_i3c_addrs()
1473 return -EBUSY; in i3c_master_get_i3c_addrs()
1475 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_get_i3c_addrs()
1476 dev->info.static_addr, in i3c_master_get_i3c_addrs()
1481 * ->init_dyn_addr should have been reserved before that, so, if we're in i3c_master_get_i3c_addrs()
1482 * trying to apply a pre-reserved dynamic address, we should not try in i3c_master_get_i3c_addrs()
1485 if (dev->info.dyn_addr && in i3c_master_get_i3c_addrs()
1486 (!dev->boardinfo || in i3c_master_get_i3c_addrs()
1487 dev->boardinfo->init_dyn_addr != dev->info.dyn_addr)) { in i3c_master_get_i3c_addrs()
1488 status = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_get_i3c_addrs()
1489 dev->info.dyn_addr); in i3c_master_get_i3c_addrs()
1493 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr, in i3c_master_get_i3c_addrs()
1500 if (dev->info.static_addr) in i3c_master_get_i3c_addrs()
1501 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_get_i3c_addrs()
1502 dev->info.static_addr, in i3c_master_get_i3c_addrs()
1505 return -EBUSY; in i3c_master_get_i3c_addrs()
1517 if (!dev->info.static_addr && !dev->info.dyn_addr) in i3c_master_attach_i3c_dev()
1525 if (master->this != dev && master->ops->attach_i3c_dev) { in i3c_master_attach_i3c_dev()
1526 ret = master->ops->attach_i3c_dev(dev); in i3c_master_attach_i3c_dev()
1533 list_add_tail(&dev->common.node, &master->bus.devs.i3c); in i3c_master_attach_i3c_dev()
1544 if (dev->info.dyn_addr != old_dyn_addr) { in i3c_master_reattach_i3c_dev()
1545 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_reattach_i3c_dev()
1546 dev->info.dyn_addr, in i3c_master_reattach_i3c_dev()
1549 i3c_bus_set_addr_slot_status(&master->bus, old_dyn_addr, in i3c_master_reattach_i3c_dev()
1553 if (master->ops->reattach_i3c_dev) { in i3c_master_reattach_i3c_dev()
1554 ret = master->ops->reattach_i3c_dev(dev, old_dyn_addr); in i3c_master_reattach_i3c_dev()
1569 if (master->this != dev && master->ops->detach_i3c_dev) in i3c_master_detach_i3c_dev()
1570 master->ops->detach_i3c_dev(dev); in i3c_master_detach_i3c_dev()
1573 list_del(&dev->common.node); in i3c_master_detach_i3c_dev()
1581 if (master->ops->attach_i2c_dev) { in i3c_master_attach_i2c_dev()
1582 ret = master->ops->attach_i2c_dev(dev); in i3c_master_attach_i2c_dev()
1587 list_add_tail(&dev->common.node, &master->bus.devs.i2c); in i3c_master_attach_i2c_dev()
1596 list_del(&dev->common.node); in i3c_master_detach_i2c_dev()
1598 if (master->ops->detach_i2c_dev) in i3c_master_detach_i2c_dev()
1599 master->ops->detach_i2c_dev(dev); in i3c_master_detach_i2c_dev()
1606 .static_addr = boardinfo->static_addr, in i3c_master_early_i3c_dev_add()
1607 .pid = boardinfo->pid, in i3c_master_early_i3c_dev_add()
1614 return -ENOMEM; in i3c_master_early_i3c_dev_add()
1616 i3cdev->boardinfo = boardinfo; in i3c_master_early_i3c_dev_add()
1622 ret = i3c_master_setdasa_locked(master, i3cdev->info.static_addr, in i3c_master_early_i3c_dev_add()
1623 i3cdev->boardinfo->init_dyn_addr); in i3c_master_early_i3c_dev_add()
1627 i3cdev->info.dyn_addr = i3cdev->boardinfo->init_dyn_addr; in i3c_master_early_i3c_dev_add()
1639 i3c_master_rstdaa_locked(master, i3cdev->boardinfo->init_dyn_addr); in i3c_master_early_i3c_dev_add()
1654 if (!master->init_done) in i3c_master_register_new_i3c_devs()
1657 i3c_bus_for_each_i3cdev(&master->bus, desc) { in i3c_master_register_new_i3c_devs()
1658 if (desc->dev || !desc->info.dyn_addr || desc == master->this) in i3c_master_register_new_i3c_devs()
1661 desc->dev = kzalloc(sizeof(*desc->dev), GFP_KERNEL); in i3c_master_register_new_i3c_devs()
1662 if (!desc->dev) in i3c_master_register_new_i3c_devs()
1665 desc->dev->bus = &master->bus; in i3c_master_register_new_i3c_devs()
1666 desc->dev->desc = desc; in i3c_master_register_new_i3c_devs()
1667 desc->dev->dev.parent = &master->dev; in i3c_master_register_new_i3c_devs()
1668 desc->dev->dev.type = &i3c_device_type; in i3c_master_register_new_i3c_devs()
1669 desc->dev->dev.bus = &i3c_bus_type; in i3c_master_register_new_i3c_devs()
1670 desc->dev->dev.release = i3c_device_release; in i3c_master_register_new_i3c_devs()
1671 dev_set_name(&desc->dev->dev, "%d-%llx", master->bus.id, in i3c_master_register_new_i3c_devs()
1672 desc->info.pid); in i3c_master_register_new_i3c_devs()
1674 if (desc->boardinfo) in i3c_master_register_new_i3c_devs()
1675 desc->dev->dev.of_node = desc->boardinfo->of_node; in i3c_master_register_new_i3c_devs()
1677 ret = device_register(&desc->dev->dev); in i3c_master_register_new_i3c_devs()
1679 dev_err(&master->dev, in i3c_master_register_new_i3c_devs()
1680 "Failed to add I3C device (err = %d)\n", ret); in i3c_master_register_new_i3c_devs()
1681 put_device(&desc->dev->dev); in i3c_master_register_new_i3c_devs()
1687 * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment)
1690 * This function is instantiating an I3C device object and adding it to the
1691 * I3C device list. All device information are automatically retrieved using
1694 * The I3C device object is returned in case the master wants to attach
1705 i3c_bus_maintenance_lock(&master->bus); in i3c_master_do_daa()
1706 ret = master->ops->do_daa(master); in i3c_master_do_daa()
1707 i3c_bus_maintenance_unlock(&master->bus); in i3c_master_do_daa()
1712 i3c_bus_normaluse_lock(&master->bus); in i3c_master_do_daa()
1714 i3c_bus_normaluse_unlock(&master->bus); in i3c_master_do_daa()
1721 * i3c_master_set_info() - set master device information
1723 * @info: I3C device information
1726 * &i3c_master_controller_ops->bus_init().
1731 * - &i3c_device_info->dyn_addr
1732 * - &i3c_device_info->bcr
1733 * - &i3c_device_info->dcr
1734 * - &i3c_device_info->pid
1735 * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in
1736 * &i3c_device_info->bcr
1741 * information can be checked, but we can at least make sure @info->dyn_addr
1742 * and @info->bcr are correct), -EINVAL otherwise.
1750 if (!i3c_bus_dev_addr_is_avail(&master->bus, info->dyn_addr)) in i3c_master_set_info()
1751 return -EINVAL; in i3c_master_set_info()
1753 if (I3C_BCR_DEVICE_ROLE(info->bcr) == I3C_BCR_I3C_MASTER && in i3c_master_set_info()
1754 master->secondary) in i3c_master_set_info()
1755 return -EINVAL; in i3c_master_set_info()
1757 if (master->this) in i3c_master_set_info()
1758 return -EINVAL; in i3c_master_set_info()
1764 master->this = i3cdev; in i3c_master_set_info()
1765 master->bus.cur_master = master->this; in i3c_master_set_info()
1785 list_for_each_entry_safe(i3cdev, i3ctmp, &master->bus.devs.i3c, in i3c_master_detach_free_devs()
1789 if (i3cdev->boardinfo && i3cdev->boardinfo->init_dyn_addr) in i3c_master_detach_free_devs()
1790 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_detach_free_devs()
1791 i3cdev->boardinfo->init_dyn_addr, in i3c_master_detach_free_devs()
1797 list_for_each_entry_safe(i2cdev, i2ctmp, &master->bus.devs.i2c, in i3c_master_detach_free_devs()
1800 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_detach_free_devs()
1801 i2cdev->addr, in i3c_master_detach_free_devs()
1808 * i3c_master_bus_init() - initialize an I3C bus
1811 * This function is following all initialisation steps described in the I3C
1817 * 2. Call &i3c_master_controller_ops->bus_init() method to initialize
1827 * 5. Reserve address slots for I3C devices with init_dyn_addr. And if devices
1828 * also have static_addr, try to pre-assign dynamic addresses requested by
1829 * the FW with SETDASA and attach corresponding statically defined I3C
1833 * remaining I3C devices
1835 * Once this is done, all I3C and I2C devices should be usable.
1851 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) { in i3c_master_bus_init()
1852 status = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_bus_init()
1853 i2cboardinfo->base.addr); in i3c_master_bus_init()
1855 ret = -EBUSY; in i3c_master_bus_init()
1859 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_bus_init()
1860 i2cboardinfo->base.addr, in i3c_master_bus_init()
1864 i2cboardinfo->base.addr, in i3c_master_bus_init()
1865 i2cboardinfo->lvr); in i3c_master_bus_init()
1879 * Now execute the controller specific ->bus_init() routine, which in i3c_master_bus_init()
1882 ret = master->ops->bus_init(master); in i3c_master_bus_init()
1887 * The master device should have been instantiated in ->bus_init(), in i3c_master_bus_init()
1890 if (!master->this) { in i3c_master_bus_init()
1891 dev_err(&master->dev, in i3c_master_bus_init()
1892 "master_set_info() was not called in ->bus_init()\n"); in i3c_master_bus_init()
1893 ret = -EINVAL; in i3c_master_bus_init()
1897 if (master->ops->set_speed) { in i3c_master_bus_init()
1898 ret = master->ops->set_speed(master, I3C_OPEN_DRAIN_SLOW_SPEED); in i3c_master_bus_init()
1911 if (master->ops->set_speed) { in i3c_master_bus_init()
1912 master->ops->set_speed(master, I3C_OPEN_DRAIN_NORMAL_SPEED); in i3c_master_bus_init()
1925 * Reserve init_dyn_addr first, and then try to pre-assign dynamic in i3c_master_bus_init()
1927 * In case pre-assign dynamic address fails, setting dynamic address to in i3c_master_bus_init()
1931 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) { in i3c_master_bus_init()
1937 if (!i3cboardinfo->init_dyn_addr) in i3c_master_bus_init()
1940 ret = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_bus_init()
1941 i3cboardinfo->init_dyn_addr); in i3c_master_bus_init()
1943 ret = -EBUSY; in i3c_master_bus_init()
1948 i3c_bus_set_addr_slot_status_mask(&master->bus, in i3c_master_bus_init()
1949 i3cboardinfo->init_dyn_addr, in i3c_master_bus_init()
1961 if (i3cboardinfo->static_addr) in i3c_master_bus_init()
1975 if (master->ops->bus_cleanup) in i3c_master_bus_init()
1976 master->ops->bus_cleanup(master); in i3c_master_bus_init()
1986 if (master->ops->bus_cleanup) in i3c_master_bus_cleanup()
1987 master->ops->bus_cleanup(master); in i3c_master_bus_cleanup()
1994 struct i3c_master_controller *master = i3cdev->common.master; in i3c_master_attach_boardinfo()
1997 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) { in i3c_master_attach_boardinfo()
1998 if (i3cdev->info.pid != i3cboardinfo->pid) in i3c_master_attach_boardinfo()
2001 i3cdev->boardinfo = i3cboardinfo; in i3c_master_attach_boardinfo()
2002 i3cdev->info.static_addr = i3cboardinfo->static_addr; in i3c_master_attach_boardinfo()
2013 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) { in i3c_master_search_i3c_dev_duplicate()
2014 if (i3cdev != refdev && i3cdev->info.pid == refdev->info.pid) in i3c_master_search_i3c_dev_duplicate()
2022 * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus
2024 * @addr: I3C slave dynamic address assigned to the device
2026 * This function is instantiating an I3C device object and adding it to the
2027 * I3C device list. All device information are automatically retrieved using
2030 * The I3C device object is returned in case the master wants to attach
2048 return -EINVAL; in i3c_master_add_i3c_dev_locked()
2066 newdev->dev = olddev->dev; in i3c_master_add_i3c_dev_locked()
2067 if (newdev->dev) in i3c_master_add_i3c_dev_locked()
2068 newdev->dev->desc = newdev; in i3c_master_add_i3c_dev_locked()
2076 mutex_lock(&olddev->ibi_lock); in i3c_master_add_i3c_dev_locked()
2077 if (olddev->ibi) { in i3c_master_add_i3c_dev_locked()
2078 ibireq.handler = olddev->ibi->handler; in i3c_master_add_i3c_dev_locked()
2079 ibireq.max_payload_len = olddev->ibi->max_payload_len; in i3c_master_add_i3c_dev_locked()
2080 ibireq.num_slots = olddev->ibi->num_slots; in i3c_master_add_i3c_dev_locked()
2082 if (olddev->ibi->enabled) in i3c_master_add_i3c_dev_locked()
2086 * i3c bus as it does not exist and has been assigned in i3c_master_add_i3c_dev_locked()
2088 * So, update the olddev->ibi->enabled flag to false in i3c_master_add_i3c_dev_locked()
2091 olddev->ibi->enabled = false; in i3c_master_add_i3c_dev_locked()
2094 mutex_unlock(&olddev->ibi_lock); in i3c_master_add_i3c_dev_locked()
2096 old_dyn_addr = olddev->info.dyn_addr; in i3c_master_add_i3c_dev_locked()
2105 * - if the device already had a dynamic address assigned, let's try to in i3c_master_add_i3c_dev_locked()
2106 * re-apply this one in i3c_master_add_i3c_dev_locked()
2107 * - if the device did not have a dynamic address and the firmware in i3c_master_add_i3c_dev_locked()
2109 * - in any other case, keep the address automatically assigned by the in i3c_master_add_i3c_dev_locked()
2112 if (old_dyn_addr && old_dyn_addr != newdev->info.dyn_addr) in i3c_master_add_i3c_dev_locked()
2114 else if (newdev->boardinfo && newdev->boardinfo->init_dyn_addr) in i3c_master_add_i3c_dev_locked()
2115 expected_dyn_addr = newdev->boardinfo->init_dyn_addr; in i3c_master_add_i3c_dev_locked()
2117 expected_dyn_addr = newdev->info.dyn_addr; in i3c_master_add_i3c_dev_locked()
2119 if (newdev->info.dyn_addr != expected_dyn_addr && in i3c_master_add_i3c_dev_locked()
2120 i3c_bus_get_addr_slot_status(&master->bus, expected_dyn_addr) == I3C_ADDR_SLOT_FREE) { in i3c_master_add_i3c_dev_locked()
2126 newdev->info.dyn_addr, in i3c_master_add_i3c_dev_locked()
2129 old_dyn_addr = newdev->info.dyn_addr; in i3c_master_add_i3c_dev_locked()
2130 newdev->info.dyn_addr = expected_dyn_addr; in i3c_master_add_i3c_dev_locked()
2133 dev_err(&master->dev, in i3c_master_add_i3c_dev_locked()
2135 master->bus.id, newdev->info.pid); in i3c_master_add_i3c_dev_locked()
2147 mutex_lock(&newdev->ibi_lock); in i3c_master_add_i3c_dev_locked()
2150 dev_err(&master->dev, in i3c_master_add_i3c_dev_locked()
2151 "Failed to request IBI on device %d-%llx", in i3c_master_add_i3c_dev_locked()
2152 master->bus.id, newdev->info.pid); in i3c_master_add_i3c_dev_locked()
2156 dev_err(&master->dev, in i3c_master_add_i3c_dev_locked()
2157 "Failed to re-enable IBI on device %d-%llx", in i3c_master_add_i3c_dev_locked()
2158 master->bus.id, newdev->info.pid); in i3c_master_add_i3c_dev_locked()
2160 mutex_unlock(&newdev->ibi_lock); in i3c_master_add_i3c_dev_locked()
2166 if (newdev->dev && newdev->dev->desc) in i3c_master_add_i3c_dev_locked()
2167 newdev->dev->desc = NULL; in i3c_master_add_i3c_dev_locked()
2185 struct device *dev = &master->dev; in of_i3c_master_add_i2c_boardinfo()
2190 return -ENOMEM; in of_i3c_master_add_i2c_boardinfo()
2192 ret = of_i2c_get_board_info(dev, node, &boardinfo->base); in of_i3c_master_add_i2c_boardinfo()
2197 * The I3C Specification does not clearly say I2C devices with 10-bit in of_i3c_master_add_i2c_boardinfo()
2201 if (boardinfo->base.flags & I2C_CLIENT_TEN) { in of_i3c_master_add_i2c_boardinfo()
2203 return -ENOTSUPP; in of_i3c_master_add_i2c_boardinfo()
2207 boardinfo->lvr = reg[2]; in of_i3c_master_add_i2c_boardinfo()
2209 list_add_tail(&boardinfo->node, &master->boardinfo.i2c); in of_i3c_master_add_i2c_boardinfo()
2220 struct device *dev = &master->dev; in of_i3c_master_add_i3c_boardinfo()
2226 return -ENOMEM; in of_i3c_master_add_i3c_boardinfo()
2230 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2232 addrstatus = i3c_bus_get_addr_slot_status(&master->bus, in of_i3c_master_add_i3c_boardinfo()
2235 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2238 boardinfo->static_addr = reg[0]; in of_i3c_master_add_i3c_boardinfo()
2240 if (!of_property_read_u32(node, "assigned-address", &init_dyn_addr)) { in of_i3c_master_add_i3c_boardinfo()
2242 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2244 addrstatus = i3c_bus_get_addr_slot_status(&master->bus, in of_i3c_master_add_i3c_boardinfo()
2247 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2250 boardinfo->pid = ((u64)reg[1] << 32) | reg[2]; in of_i3c_master_add_i3c_boardinfo()
2252 if ((boardinfo->pid & GENMASK_ULL(63, 48)) || in of_i3c_master_add_i3c_boardinfo()
2253 I3C_PID_RND_LOWER_32BITS(boardinfo->pid)) in of_i3c_master_add_i3c_boardinfo()
2254 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2256 boardinfo->init_dyn_addr = init_dyn_addr; in of_i3c_master_add_i3c_boardinfo()
2257 boardinfo->of_node = of_node_get(node); in of_i3c_master_add_i3c_boardinfo()
2258 list_add_tail(&boardinfo->node, &master->boardinfo.i3c); in of_i3c_master_add_i3c_boardinfo()
2270 return -EINVAL; in of_i3c_master_add_dev()
2290 struct device *dev = &master->dev; in of_populate_i3c_bus()
2291 struct device_node *i3cbus_np = dev->of_node; in of_populate_i3c_bus()
2308 * The user might want to limit I2C and I3C speed in case some devices in of_populate_i3c_bus()
2312 if (!of_property_read_u32(i3cbus_np, "i2c-scl-hz", &val)) in of_populate_i3c_bus()
2313 master->bus.scl_rate.i2c = val; in of_populate_i3c_bus()
2315 if (!of_property_read_u32(i3cbus_np, "i3c-scl-hz", &val)) in of_populate_i3c_bus()
2316 master->bus.scl_rate.i3c = val; in of_populate_i3c_bus()
2330 return -EINVAL; in i3c_master_i2c_adapter_xfer()
2332 if (!master->ops->i2c_xfers) in i3c_master_i2c_adapter_xfer()
2333 return -ENOTSUPP; in i3c_master_i2c_adapter_xfer()
2339 return -ENOTSUPP; in i3c_master_i2c_adapter_xfer()
2342 i3c_bus_normaluse_lock(&master->bus); in i3c_master_i2c_adapter_xfer()
2345 ret = -ENOENT; in i3c_master_i2c_adapter_xfer()
2347 ret = master->ops->i2c_xfers(dev, xfers, nxfers); in i3c_master_i2c_adapter_xfer()
2348 i3c_bus_normaluse_unlock(&master->bus); in i3c_master_i2c_adapter_xfer()
2363 if (client->dev.of_node) { in i3c_master_i2c_get_lvr()
2366 if (!of_property_read_u32_array(client->dev.of_node, "reg", in i3c_master_i2c_get_lvr()
2382 if (i3c_master_find_i2c_dev_by_addr(master, client->addr)) in i3c_master_i2c_attach()
2385 status = i3c_bus_get_addr_slot_status(&master->bus, client->addr); in i3c_master_i2c_attach()
2387 return -EBUSY; in i3c_master_i2c_attach()
2389 i3c_bus_set_addr_slot_status(&master->bus, client->addr, in i3c_master_i2c_attach()
2392 i2cdev = i3c_master_alloc_i2c_dev(master, client->addr, in i3c_master_i2c_attach()
2408 i3c_bus_set_addr_slot_status(&master->bus, client->addr, in i3c_master_i2c_attach()
2419 dev = i3c_master_find_i2c_dev_by_addr(master, client->addr); in i3c_master_i2c_detach()
2421 return -ENODEV; in i3c_master_i2c_detach()
2424 i3c_bus_set_addr_slot_status(&master->bus, dev->addr, in i3c_master_i2c_detach()
2445 if (dev->type != &i2c_client_type) in i3c_i2c_notifier_call()
2449 adap = client->adapter; in i3c_i2c_notifier_call()
2451 if (adap->algo != &i3c_master_i2c_algo) in i3c_i2c_notifier_call()
2456 i3c_bus_maintenance_lock(&master->bus); in i3c_i2c_notifier_call()
2465 i3c_bus_maintenance_unlock(&master->bus); in i3c_i2c_notifier_call()
2481 adap->dev.parent = master->dev.parent; in i3c_master_i2c_adapter_init()
2482 adap->owner = master->dev.parent->driver->owner; in i3c_master_i2c_adapter_init()
2483 adap->algo = &i3c_master_i2c_algo; in i3c_master_i2c_adapter_init()
2484 strncpy(adap->name, dev_name(master->dev.parent), sizeof(adap->name)); in i3c_master_i2c_adapter_init()
2486 /* FIXME: Should we allow i3c masters to override these values? */ in i3c_master_i2c_adapter_init()
2487 adap->timeout = 1000; in i3c_master_i2c_adapter_init()
2488 adap->retries = 3; in i3c_master_i2c_adapter_init()
2498 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) { in i3c_master_i2c_adapter_init()
2500 i2cboardinfo->base.addr); in i3c_master_i2c_adapter_init()
2503 i2cdev->dev = i2c_new_client_device(adap, &i2cboardinfo->base); in i3c_master_i2c_adapter_init()
2513 i2c_del_adapter(&master->i2c); in i3c_master_i2c_adapter_cleanup()
2515 i3c_bus_for_each_i2cdev(&master->bus, i2cdev) in i3c_master_i2c_adapter_cleanup()
2516 i2cdev->dev = NULL; in i3c_master_i2c_adapter_cleanup()
2523 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) { in i3c_master_unregister_i3c_devs()
2524 if (!i3cdev->dev) in i3c_master_unregister_i3c_devs()
2527 i3cdev->dev->desc = NULL; in i3c_master_unregister_i3c_devs()
2528 if (device_is_registered(&i3cdev->dev->dev)) in i3c_master_unregister_i3c_devs()
2529 device_unregister(&i3cdev->dev->dev); in i3c_master_unregister_i3c_devs()
2531 put_device(&i3cdev->dev->dev); in i3c_master_unregister_i3c_devs()
2532 i3cdev->dev = NULL; in i3c_master_unregister_i3c_devs()
2537 * i3c_master_queue_ibi() - Queue an IBI
2546 atomic_inc(&dev->ibi->pending_ibis); in i3c_master_queue_ibi()
2547 queue_work(dev->common.master->wq, &slot->work); in i3c_master_queue_ibi()
2555 struct i3c_dev_desc *dev = slot->dev; in i3c_master_handle_ibi()
2559 payload.data = slot->data; in i3c_master_handle_ibi()
2560 payload.len = slot->len; in i3c_master_handle_ibi()
2562 if (dev->dev) in i3c_master_handle_ibi()
2563 dev->ibi->handler(dev->dev, &payload); in i3c_master_handle_ibi()
2565 master->ops->recycle_ibi_slot(dev, slot); in i3c_master_handle_ibi()
2566 if (atomic_dec_and_test(&dev->ibi->pending_ibis)) in i3c_master_handle_ibi()
2567 complete(&dev->ibi->all_ibis_handled); in i3c_master_handle_ibi()
2573 slot->dev = dev; in i3c_master_init_ibi_slot()
2574 INIT_WORK(&slot->work, i3c_master_handle_ibi); in i3c_master_init_ibi_slot()
2592 * i3c_generic_ibi_free_pool() - Free a generic IBI pool
2602 while (!list_empty(&pool->free_slots)) { in i3c_generic_ibi_free_pool()
2603 slot = list_first_entry(&pool->free_slots, in i3c_generic_ibi_free_pool()
2605 list_del(&slot->node); in i3c_generic_ibi_free_pool()
2613 WARN_ON(nslots != pool->num_slots); in i3c_generic_ibi_free_pool()
2615 kfree(pool->payload_buf); in i3c_generic_ibi_free_pool()
2616 kfree(pool->slots); in i3c_generic_ibi_free_pool()
2622 * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool
2641 return ERR_PTR(-ENOMEM); in i3c_generic_ibi_alloc_pool()
2643 spin_lock_init(&pool->lock); in i3c_generic_ibi_alloc_pool()
2644 INIT_LIST_HEAD(&pool->free_slots); in i3c_generic_ibi_alloc_pool()
2645 INIT_LIST_HEAD(&pool->pending); in i3c_generic_ibi_alloc_pool()
2647 pool->slots = kcalloc(req->num_slots, sizeof(*slot), GFP_KERNEL); in i3c_generic_ibi_alloc_pool()
2648 if (!pool->slots) { in i3c_generic_ibi_alloc_pool()
2649 ret = -ENOMEM; in i3c_generic_ibi_alloc_pool()
2653 if (req->max_payload_len) { in i3c_generic_ibi_alloc_pool()
2654 pool->payload_buf = kcalloc(req->num_slots, in i3c_generic_ibi_alloc_pool()
2655 req->max_payload_len, GFP_KERNEL); in i3c_generic_ibi_alloc_pool()
2656 if (!pool->payload_buf) { in i3c_generic_ibi_alloc_pool()
2657 ret = -ENOMEM; in i3c_generic_ibi_alloc_pool()
2662 for (i = 0; i < req->num_slots; i++) { in i3c_generic_ibi_alloc_pool()
2663 slot = &pool->slots[i]; in i3c_generic_ibi_alloc_pool()
2664 i3c_master_init_ibi_slot(dev, &slot->base); in i3c_generic_ibi_alloc_pool()
2666 if (req->max_payload_len) in i3c_generic_ibi_alloc_pool()
2667 slot->base.data = pool->payload_buf + in i3c_generic_ibi_alloc_pool()
2668 (i * req->max_payload_len); in i3c_generic_ibi_alloc_pool()
2670 list_add_tail(&slot->node, &pool->free_slots); in i3c_generic_ibi_alloc_pool()
2671 pool->num_slots++; in i3c_generic_ibi_alloc_pool()
2683 * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool
2698 spin_lock_irqsave(&pool->lock, flags); in i3c_generic_ibi_get_free_slot()
2699 slot = list_first_entry_or_null(&pool->free_slots, in i3c_generic_ibi_get_free_slot()
2702 list_del(&slot->node); in i3c_generic_ibi_get_free_slot()
2703 spin_unlock_irqrestore(&pool->lock, flags); in i3c_generic_ibi_get_free_slot()
2705 return slot ? &slot->base : NULL; in i3c_generic_ibi_get_free_slot()
2710 * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool
2715 * master driver struct_master_controller_ops->recycle_ibi() method.
2727 spin_lock_irqsave(&pool->lock, flags); in i3c_generic_ibi_recycle_slot()
2728 list_add_tail(&slot->node, &pool->free_slots); in i3c_generic_ibi_recycle_slot()
2729 spin_unlock_irqrestore(&pool->lock, flags); in i3c_generic_ibi_recycle_slot()
2735 if (!ops || !ops->bus_init || !ops->priv_xfers || in i3c_master_check_ops()
2736 !ops->send_ccc_cmd || !ops->do_daa || !ops->i2c_xfers) in i3c_master_check_ops()
2737 return -EINVAL; in i3c_master_check_ops()
2739 if (ops->request_ibi && in i3c_master_check_ops()
2740 (!ops->enable_ibi || !ops->disable_ibi || !ops->free_ibi || in i3c_master_check_ops()
2741 !ops->recycle_ibi_slot)) in i3c_master_check_ops()
2742 return -EINVAL; in i3c_master_check_ops()
2748 * i3c_master_register() - register an I3C master
2750 * @parent: the parent device (the one that provides this I3C master
2754 * -ENOTSUPP if set to true since secondary masters are not yet
2759 * - creates and initializes the I3C bus
2760 * - populates the bus with static I2C devs if @parent->of_node is not
2762 * - registers all I3C devices added by the controller during bus
2764 * - registers the I2C adapter and all I2C devices
2781 return -ENOTSUPP; in i3c_master_register()
2787 master->dev.parent = parent; in i3c_master_register()
2788 master->dev.of_node = of_node_get(parent->of_node); in i3c_master_register()
2789 master->dev.bus = &i3c_bus_type; in i3c_master_register()
2790 master->dev.type = &i3c_masterdev_type; in i3c_master_register()
2791 master->dev.release = i3c_masterdev_release; in i3c_master_register()
2792 master->ops = ops; in i3c_master_register()
2793 master->secondary = secondary; in i3c_master_register()
2794 INIT_LIST_HEAD(&master->boardinfo.i2c); in i3c_master_register()
2795 INIT_LIST_HEAD(&master->boardinfo.i3c); in i3c_master_register()
2797 ret = i3c_bus_init(i3cbus, master->dev.of_node); in i3c_master_register()
2801 device_initialize(&master->dev); in i3c_master_register()
2802 dev_set_name(&master->dev, "i3c-%d", i3cbus->id); in i3c_master_register()
2808 list_for_each_entry(i2cbi, &master->boardinfo.i2c, node) { in i3c_master_register()
2809 switch (i2cbi->lvr & I3C_LVR_I2C_INDEX_MASK) { in i3c_master_register()
2823 ret = -EINVAL; in i3c_master_register()
2827 if (i2cbi->lvr & I3C_LVR_I2C_FM_MODE) in i3c_master_register()
2835 master->wq = alloc_workqueue("%s", 0, 0, dev_name(parent)); in i3c_master_register()
2836 if (!master->wq) { in i3c_master_register()
2837 ret = -ENOMEM; in i3c_master_register()
2845 ret = device_add(&master->dev); in i3c_master_register()
2850 * Expose our I3C bus as an I2C adapter so that I2C devices are exposed in i3c_master_register()
2861 * register I3C devices discovered during the initial DAA. in i3c_master_register()
2863 master->init_done = true; in i3c_master_register()
2864 i3c_bus_normaluse_lock(&master->bus); in i3c_master_register()
2866 i3c_bus_normaluse_unlock(&master->bus); in i3c_master_register()
2871 device_del(&master->dev); in i3c_master_register()
2877 put_device(&master->dev); in i3c_master_register()
2884 * i3c_master_unregister() - unregister an I3C master
2891 i3c_bus_notify(&master->bus, I3C_NOTIFY_BUS_REMOVE); in i3c_master_unregister()
2896 device_unregister(&master->dev); in i3c_master_unregister()
2905 return -ENOENT; in i3c_dev_setdasa_locked()
2909 return -EINVAL; in i3c_dev_setdasa_locked()
2911 if (!dev->boardinfo || !dev->boardinfo->init_dyn_addr || in i3c_dev_setdasa_locked()
2912 !dev->boardinfo->static_addr) in i3c_dev_setdasa_locked()
2913 return -EINVAL; in i3c_dev_setdasa_locked()
2915 return i3c_master_setdasa_locked(master, dev->info.static_addr, in i3c_dev_setdasa_locked()
2916 dev->boardinfo->init_dyn_addr); in i3c_dev_setdasa_locked()
2926 return -ENOENT; in i3c_dev_do_priv_xfers_locked()
2930 return -EINVAL; in i3c_dev_do_priv_xfers_locked()
2932 if (!master->ops->priv_xfers) in i3c_dev_do_priv_xfers_locked()
2933 return -ENOTSUPP; in i3c_dev_do_priv_xfers_locked()
2935 return master->ops->priv_xfers(dev, xfers, nxfers); in i3c_dev_do_priv_xfers_locked()
2943 if (!dev->ibi) in i3c_dev_disable_ibi_locked()
2944 return -EINVAL; in i3c_dev_disable_ibi_locked()
2947 ret = master->ops->disable_ibi(dev); in i3c_dev_disable_ibi_locked()
2951 reinit_completion(&dev->ibi->all_ibis_handled); in i3c_dev_disable_ibi_locked()
2952 if (atomic_read(&dev->ibi->pending_ibis)) in i3c_dev_disable_ibi_locked()
2953 wait_for_completion(&dev->ibi->all_ibis_handled); in i3c_dev_disable_ibi_locked()
2955 dev->ibi->enabled = false; in i3c_dev_disable_ibi_locked()
2965 if (!dev->ibi) in i3c_dev_enable_ibi_locked()
2966 return -EINVAL; in i3c_dev_enable_ibi_locked()
2968 ret = master->ops->enable_ibi(dev); in i3c_dev_enable_ibi_locked()
2970 dev->ibi->enabled = true; in i3c_dev_enable_ibi_locked()
2982 if (!master->ops->request_ibi) in i3c_dev_request_ibi_locked()
2983 return -ENOTSUPP; in i3c_dev_request_ibi_locked()
2985 if (dev->ibi) in i3c_dev_request_ibi_locked()
2986 return -EBUSY; in i3c_dev_request_ibi_locked()
2990 return -ENOMEM; in i3c_dev_request_ibi_locked()
2992 atomic_set(&ibi->pending_ibis, 0); in i3c_dev_request_ibi_locked()
2993 init_completion(&ibi->all_ibis_handled); in i3c_dev_request_ibi_locked()
2994 ibi->handler = req->handler; in i3c_dev_request_ibi_locked()
2995 ibi->max_payload_len = req->max_payload_len; in i3c_dev_request_ibi_locked()
2996 ibi->num_slots = req->num_slots; in i3c_dev_request_ibi_locked()
2998 dev->ibi = ibi; in i3c_dev_request_ibi_locked()
2999 ret = master->ops->request_ibi(dev, req); in i3c_dev_request_ibi_locked()
3002 dev->ibi = NULL; in i3c_dev_request_ibi_locked()
3012 if (!dev->ibi) in i3c_dev_free_ibi_locked()
3015 if (WARN_ON(dev->ibi->enabled)) in i3c_dev_free_ibi_locked()
3018 master->ops->free_ibi(dev); in i3c_dev_free_ibi_locked()
3019 kfree(dev->ibi); in i3c_dev_free_ibi_locked()
3020 dev->ibi = NULL; in i3c_dev_free_ibi_locked()
3027 res = of_alias_get_highest_id("i3c"); in i3c_init()
3060 MODULE_DESCRIPTION("I3C core");