Lines Matching +full:dynamic +full:- +full:mode

1 // SPDX-License-Identifier: GPL-2.0
29 * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation
34 * - enabling/disabling slave events
35 * - re-triggering DAA
36 * - changing the dynamic address of a device
37 * - relinquishing mastership
38 * - ...
46 down_write(&bus->lock); in i3c_bus_maintenance_lock()
50 * i3c_bus_maintenance_unlock - Release the bus lock after a maintenance
60 up_write(&bus->lock); in i3c_bus_maintenance_unlock()
64 * i3c_bus_normaluse_lock - Lock the bus for a normal operation
68 * operation (see i3c_bus_maintenance_lock() for a non-exhaustive list of
71 * state or I3C dynamic address).
76 * sure two different communications are not inter-mixed, or access to the
81 down_read(&bus->lock); in i3c_bus_normaluse_lock()
85 * i3c_bus_normaluse_unlock - Release the bus lock after a normal operation
94 up_read(&bus->lock); in i3c_bus_normaluse_unlock()
114 if (dev->type == &i3c_device_type) in dev_to_i3cbus()
115 return dev_to_i3cdev(dev)->bus; in dev_to_i3cbus()
119 return &master->bus; in dev_to_i3cbus()
126 if (dev->type == &i3c_device_type) in dev_to_i3cdesc()
127 return dev_to_i3cdev(dev)->desc; in dev_to_i3cdesc()
131 return master->this; in dev_to_i3cdesc()
144 ret = sprintf(buf, "%x\n", desc->info.bcr); in bcr_show()
161 ret = sprintf(buf, "%x\n", desc->info.dcr); in dcr_show()
178 ret = sprintf(buf, "%llx\n", desc->info.pid); in pid_show()
195 ret = sprintf(buf, "%02x\n", desc->info.dyn_addr); in dynamic_address_show()
203 "hdr-ddr", "hdr-tsp", "hdr-tsl",
214 int mode; in hdrcap_show() local
218 caps = desc->info.hdr_cap; in hdrcap_show()
219 for_each_set_bit(mode, &caps, 8) { in hdrcap_show()
220 if (mode >= ARRAY_SIZE(hdrcap_strings)) in hdrcap_show()
223 if (!hdrcap_strings[mode]) in hdrcap_show()
227 hdrcap_strings[mode]); in hdrcap_show()
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()
356 status = bus->addrslots[bitpos / BITS_PER_LONG]; in i3c_bus_get_addr_slot_status()
371 ptr = bus->addrslots + (bitpos / BITS_PER_LONG); in i3c_bus_set_addr_slot_status()
397 return -ENOMEM; in i3c_bus_get_free_addr()
422 idr_remove(&i3c_bus_idr, i3cbus->id); in i3c_bus_cleanup()
428 int ret, start, end, id = -1; in i3c_bus_init()
430 init_rwsem(&i3cbus->lock); in i3c_bus_init()
431 INIT_LIST_HEAD(&i3cbus->devs.i2c); in i3c_bus_init()
432 INIT_LIST_HEAD(&i3cbus->devs.i3c); in i3c_bus_init()
434 i3cbus->mode = I3C_BUS_MODE_PURE; in i3c_bus_init()
454 i3cbus->id = ret; in i3c_bus_init()
491 [I3C_BUS_MODE_MIXED_FAST] = "mixed-fast",
492 [I3C_BUS_MODE_MIXED_LIMITED] = "mixed-limited",
493 [I3C_BUS_MODE_MIXED_SLOW] = "mixed-slow",
504 if (i3cbus->mode < 0 || in mode_show()
505 i3cbus->mode >= ARRAY_SIZE(i3c_bus_mode_strings) || in mode_show()
506 !i3c_bus_mode_strings[i3cbus->mode]) in mode_show()
509 ret = sprintf(buf, "%s\n", i3c_bus_mode_strings[i3cbus->mode]); in mode_show()
514 static DEVICE_ATTR_RO(mode);
524 ret = sprintf(buf, "%d-%llx\n", i3cbus->id, in current_master_show()
525 i3cbus->cur_master->info.pid); in current_master_show()
540 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i3c); in i3c_scl_frequency_show()
555 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i2c); in i2c_scl_frequency_show()
566 if (!master || !master->ops) in i3c_set_hotjoin()
567 return -EINVAL; in i3c_set_hotjoin()
569 if (!master->ops->enable_hotjoin || !master->ops->disable_hotjoin) in i3c_set_hotjoin()
570 return -EINVAL; in i3c_set_hotjoin()
572 i3c_bus_normaluse_lock(&master->bus); in i3c_set_hotjoin()
575 ret = master->ops->enable_hotjoin(master); in i3c_set_hotjoin()
577 ret = master->ops->disable_hotjoin(master); in i3c_set_hotjoin()
579 master->hotjoin = enable; in i3c_set_hotjoin()
581 i3c_bus_normaluse_unlock(&master->bus); in i3c_set_hotjoin()
593 if (!i3cbus->cur_master) in hotjoin_store()
594 return -EINVAL; in hotjoin_store()
597 return -EINVAL; in hotjoin_store()
599 ret = i3c_set_hotjoin(i3cbus->cur_master->common.master, res); in hotjoin_store()
607 * i3c_master_enable_hotjoin - Enable hotjoin
619 * i3c_master_disable_hotjoin - Disable hotjoin
636 ret = sysfs_emit(buf, "%d\n", i3cbus->cur_master->common.master->hotjoin); in hotjoin_show()
664 if (master->wq) in i3c_masterdev_release()
665 destroy_workqueue(master->wq); in i3c_masterdev_release()
667 WARN_ON(!list_empty(&bus->devs.i2c) || !list_empty(&bus->devs.i3c)); in i3c_masterdev_release()
670 of_node_put(dev->of_node); in i3c_masterdev_release()
677 static int i3c_bus_set_mode(struct i3c_bus *i3cbus, enum i3c_bus_mode mode, in i3c_bus_set_mode() argument
682 i3cbus->mode = mode; in i3c_bus_set_mode()
684 switch (i3cbus->mode) { in i3c_bus_set_mode()
686 if (!i3cbus->scl_rate.i3c) in i3c_bus_set_mode()
687 i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE; in i3c_bus_set_mode()
691 if (!i3cbus->scl_rate.i3c) in i3c_bus_set_mode()
692 i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE; in i3c_bus_set_mode()
693 if (!i3cbus->scl_rate.i2c) in i3c_bus_set_mode()
694 i3cbus->scl_rate.i2c = max_i2c_scl_rate; in i3c_bus_set_mode()
697 if (!i3cbus->scl_rate.i2c) in i3c_bus_set_mode()
698 i3cbus->scl_rate.i2c = max_i2c_scl_rate; in i3c_bus_set_mode()
699 if (!i3cbus->scl_rate.i3c || in i3c_bus_set_mode()
700 i3cbus->scl_rate.i3c > i3cbus->scl_rate.i2c) in i3c_bus_set_mode()
701 i3cbus->scl_rate.i3c = i3cbus->scl_rate.i2c; in i3c_bus_set_mode()
704 return -EINVAL; in i3c_bus_set_mode()
707 dev_dbg(&master->dev, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n", in i3c_bus_set_mode()
708 i3cbus->scl_rate.i2c, i3cbus->scl_rate.i3c); in i3c_bus_set_mode()
711 * I3C/I2C frequency may have been overridden, check that user-provided in i3c_bus_set_mode()
714 if (i3cbus->scl_rate.i3c > I3C_BUS_MAX_I3C_SCL_RATE || in i3c_bus_set_mode()
715 i3cbus->scl_rate.i2c > I3C_BUS_I2C_FM_PLUS_SCL_RATE) in i3c_bus_set_mode()
716 return -EINVAL; in i3c_bus_set_mode()
730 return &master->i2c; in i3c_master_to_i2c_adapter()
746 return ERR_PTR(-ENOMEM); in i3c_master_alloc_i2c_dev()
748 dev->common.master = master; in i3c_master_alloc_i2c_dev()
749 dev->addr = addr; in i3c_master_alloc_i2c_dev()
750 dev->lvr = lvr; in i3c_master_alloc_i2c_dev()
758 dest->addr = addr; in i3c_ccc_cmd_dest_init()
759 dest->payload.len = payloadlen; in i3c_ccc_cmd_dest_init()
761 dest->payload.data = kzalloc(payloadlen, GFP_KERNEL); in i3c_ccc_cmd_dest_init()
763 dest->payload.data = NULL; in i3c_ccc_cmd_dest_init()
765 return dest->payload.data; in i3c_ccc_cmd_dest_init()
770 kfree(dest->payload.data); in i3c_ccc_cmd_dest_cleanup()
777 cmd->rnw = rnw ? 1 : 0; in i3c_ccc_cmd_init()
778 cmd->id = id; in i3c_ccc_cmd_init()
779 cmd->dests = dests; in i3c_ccc_cmd_init()
780 cmd->ndests = ndests; in i3c_ccc_cmd_init()
781 cmd->err = I3C_ERROR_UNKNOWN; in i3c_ccc_cmd_init()
790 return -EINVAL; in i3c_master_send_ccc_cmd_locked()
792 if (WARN_ON(master->init_done && in i3c_master_send_ccc_cmd_locked()
793 !rwsem_is_locked(&master->bus.lock))) in i3c_master_send_ccc_cmd_locked()
794 return -EINVAL; in i3c_master_send_ccc_cmd_locked()
796 if (!master->ops->send_ccc_cmd) in i3c_master_send_ccc_cmd_locked()
797 return -ENOTSUPP; in i3c_master_send_ccc_cmd_locked()
799 if ((cmd->id & I3C_CCC_DIRECT) && (!cmd->dests || !cmd->ndests)) in i3c_master_send_ccc_cmd_locked()
800 return -EINVAL; in i3c_master_send_ccc_cmd_locked()
802 if (master->ops->supports_ccc_cmd && in i3c_master_send_ccc_cmd_locked()
803 !master->ops->supports_ccc_cmd(master, cmd)) in i3c_master_send_ccc_cmd_locked()
804 return -ENOTSUPP; in i3c_master_send_ccc_cmd_locked()
806 ret = master->ops->send_ccc_cmd(master, cmd); in i3c_master_send_ccc_cmd_locked()
808 if (cmd->err != I3C_ERROR_UNKNOWN) in i3c_master_send_ccc_cmd_locked()
809 return cmd->err; in i3c_master_send_ccc_cmd_locked()
823 i3c_bus_for_each_i2cdev(&master->bus, dev) { in i3c_master_find_i2c_dev_by_addr()
824 if (dev->addr == addr) in i3c_master_find_i2c_dev_by_addr()
832 * i3c_master_get_free_addr() - get a free address on the bus
836 * This function must be called with the bus lock held in write mode.
838 * Return: the first free address starting at @start_addr (included) or -ENOMEM
844 return i3c_bus_get_free_addr(&master->bus, start_addr); in i3c_master_get_free_addr()
852 WARN_ON(i3cdev->desc); in i3c_device_release()
854 of_node_put(i3cdev->dev.of_node); in i3c_device_release()
871 return ERR_PTR(-ENOMEM); in i3c_master_alloc_i3c_dev()
873 dev->common.master = master; in i3c_master_alloc_i3c_dev()
874 dev->info = *info; in i3c_master_alloc_i3c_dev()
875 mutex_init(&dev->ibi_lock); in i3c_master_alloc_i3c_dev()
889 return -EINVAL; in i3c_master_rstdaa_locked()
891 addrstat = i3c_bus_get_addr_slot_status(&master->bus, addr); in i3c_master_rstdaa_locked()
893 return -EINVAL; in i3c_master_rstdaa_locked()
906 * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment)
913 * behind dynamic address assignment has to be handled in the I3C master
916 * This function must be called with the bus lock held in write mode.
946 return -ENOMEM; in i3c_master_enec_disec_locked()
948 events->events = evts; in i3c_master_enec_disec_locked()
961 * i3c_master_disec_locked() - send a DISEC CCC command
969 * This function must be called with the bus lock held in write mode.
982 * i3c_master_enec_locked() - send an ENEC CCC command
990 * This function must be called with the bus lock held in write mode.
1003 * i3c_master_defslvs_locked() - send a DEFSLVS CCC command
1012 * &i3c_master_controller_ops->bus_init().
1013 * It should also be called if a master ACKed an Hot-Join request and assigned
1014 * a dynamic address to the device joining the bus.
1016 * This function must be called with the bus lock held in write mode.
1034 return -EINVAL; in i3c_master_defslvs_locked()
1040 if (i3cdev == master->this) in i3c_master_defslvs_locked()
1043 if (I3C_BCR_DEVICE_ROLE(i3cdev->info.bcr) == in i3c_master_defslvs_locked()
1057 ndevs - 1)); in i3c_master_defslvs_locked()
1059 return -ENOMEM; in i3c_master_defslvs_locked()
1061 defslvs->count = ndevs; in i3c_master_defslvs_locked()
1062 defslvs->master.bcr = master->this->info.bcr; in i3c_master_defslvs_locked()
1063 defslvs->master.dcr = master->this->info.dcr; in i3c_master_defslvs_locked()
1064 defslvs->master.dyn_addr = master->this->info.dyn_addr << 1; in i3c_master_defslvs_locked()
1065 defslvs->master.static_addr = I3C_BROADCAST_ADDR << 1; in i3c_master_defslvs_locked()
1067 desc = defslvs->slaves; in i3c_master_defslvs_locked()
1069 desc->lvr = i2cdev->lvr; in i3c_master_defslvs_locked()
1070 desc->static_addr = i2cdev->addr << 1; in i3c_master_defslvs_locked()
1076 if (i3cdev == master->this) in i3c_master_defslvs_locked()
1079 desc->bcr = i3cdev->info.bcr; in i3c_master_defslvs_locked()
1080 desc->dcr = i3cdev->info.dcr; in i3c_master_defslvs_locked()
1081 desc->dyn_addr = i3cdev->info.dyn_addr << 1; in i3c_master_defslvs_locked()
1082 desc->static_addr = i3cdev->info.static_addr << 1; in i3c_master_defslvs_locked()
1103 return -EINVAL; in i3c_master_setda_locked()
1107 return -ENOMEM; in i3c_master_setda_locked()
1109 setda->addr = newaddr << 1; in i3c_master_setda_locked()
1139 mrl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mrl)); in i3c_master_getmrl_locked()
1141 return -ENOMEM; in i3c_master_getmrl_locked()
1147 if (!(info->bcr & I3C_BCR_IBI_PAYLOAD)) in i3c_master_getmrl_locked()
1148 dest.payload.len -= 1; in i3c_master_getmrl_locked()
1157 info->max_ibi_len = mrl->ibi_len; in i3c_master_getmrl_locked()
1160 info->max_read_len = be16_to_cpu(mrl->read_len); in i3c_master_getmrl_locked()
1163 ret = -EIO; in i3c_master_getmrl_locked()
1181 mwl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mwl)); in i3c_master_getmwl_locked()
1183 return -ENOMEM; in i3c_master_getmwl_locked()
1191 ret = -EIO; in i3c_master_getmwl_locked()
1195 info->max_write_len = be16_to_cpu(mwl->len); in i3c_master_getmwl_locked()
1211 getmaxds = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, in i3c_master_getmxds_locked()
1214 return -ENOMEM; in i3c_master_getmxds_locked()
1223 dest.payload.len -= 3; in i3c_master_getmxds_locked()
1230 ret = -EIO; in i3c_master_getmxds_locked()
1234 info->max_read_ds = getmaxds->maxrd; in i3c_master_getmxds_locked()
1235 info->max_write_ds = getmaxds->maxwr; in i3c_master_getmxds_locked()
1237 info->max_read_turnaround = getmaxds->maxrdturn[0] | in i3c_master_getmxds_locked()
1238 ((u32)getmaxds->maxrdturn[1] << 8) | in i3c_master_getmxds_locked()
1239 ((u32)getmaxds->maxrdturn[2] << 16); in i3c_master_getmxds_locked()
1255 gethdrcap = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, in i3c_master_gethdrcap_locked()
1258 return -ENOMEM; in i3c_master_gethdrcap_locked()
1266 ret = -EIO; in i3c_master_gethdrcap_locked()
1270 info->hdr_cap = gethdrcap->modes; in i3c_master_gethdrcap_locked()
1286 getpid = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getpid)); in i3c_master_getpid_locked()
1288 return -ENOMEM; in i3c_master_getpid_locked()
1295 info->pid = 0; in i3c_master_getpid_locked()
1296 for (i = 0; i < sizeof(getpid->pid); i++) { in i3c_master_getpid_locked()
1297 int sft = (sizeof(getpid->pid) - i - 1) * 8; in i3c_master_getpid_locked()
1299 info->pid |= (u64)getpid->pid[i] << sft; in i3c_master_getpid_locked()
1316 getbcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getbcr)); in i3c_master_getbcr_locked()
1318 return -ENOMEM; in i3c_master_getbcr_locked()
1325 info->bcr = getbcr->bcr; in i3c_master_getbcr_locked()
1341 getdcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getdcr)); in i3c_master_getdcr_locked()
1343 return -ENOMEM; in i3c_master_getdcr_locked()
1350 info->dcr = getdcr->dcr; in i3c_master_getdcr_locked()
1364 if (!dev->info.dyn_addr) in i3c_master_retrieve_dev_info()
1365 return -EINVAL; in i3c_master_retrieve_dev_info()
1367 slot_status = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_retrieve_dev_info()
1368 dev->info.dyn_addr); in i3c_master_retrieve_dev_info()
1371 return -EINVAL; in i3c_master_retrieve_dev_info()
1373 ret = i3c_master_getpid_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1377 ret = i3c_master_getbcr_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1381 ret = i3c_master_getdcr_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1385 if (dev->info.bcr & I3C_BCR_MAX_DATA_SPEED_LIM) { in i3c_master_retrieve_dev_info()
1386 ret = i3c_master_getmxds_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1391 if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD) in i3c_master_retrieve_dev_info()
1392 dev->info.max_ibi_len = 1; in i3c_master_retrieve_dev_info()
1394 i3c_master_getmrl_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1395 i3c_master_getmwl_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1397 if (dev->info.bcr & I3C_BCR_HDR_CAP) { in i3c_master_retrieve_dev_info()
1398 ret = i3c_master_gethdrcap_locked(master, &dev->info); in i3c_master_retrieve_dev_info()
1410 if (dev->info.static_addr) in i3c_master_put_i3c_addrs()
1411 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_put_i3c_addrs()
1412 dev->info.static_addr, in i3c_master_put_i3c_addrs()
1415 if (dev->info.dyn_addr) in i3c_master_put_i3c_addrs()
1416 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr, in i3c_master_put_i3c_addrs()
1419 if (dev->boardinfo && dev->boardinfo->init_dyn_addr) in i3c_master_put_i3c_addrs()
1420 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr, in i3c_master_put_i3c_addrs()
1429 if (!dev->info.static_addr && !dev->info.dyn_addr) in i3c_master_get_i3c_addrs()
1432 if (dev->info.static_addr) { in i3c_master_get_i3c_addrs()
1433 status = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_get_i3c_addrs()
1434 dev->info.static_addr); in i3c_master_get_i3c_addrs()
1435 /* Since static address and assigned dynamic address can be in i3c_master_get_i3c_addrs()
1439 dev->info.static_addr != dev->boardinfo->init_dyn_addr) in i3c_master_get_i3c_addrs()
1440 return -EBUSY; in i3c_master_get_i3c_addrs()
1442 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_get_i3c_addrs()
1443 dev->info.static_addr, in i3c_master_get_i3c_addrs()
1448 * ->init_dyn_addr should have been reserved before that, so, if we're in i3c_master_get_i3c_addrs()
1449 * trying to apply a pre-reserved dynamic address, we should not try in i3c_master_get_i3c_addrs()
1452 if (dev->info.dyn_addr && in i3c_master_get_i3c_addrs()
1453 (!dev->boardinfo || in i3c_master_get_i3c_addrs()
1454 dev->boardinfo->init_dyn_addr != dev->info.dyn_addr)) { in i3c_master_get_i3c_addrs()
1455 status = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_get_i3c_addrs()
1456 dev->info.dyn_addr); in i3c_master_get_i3c_addrs()
1460 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr, in i3c_master_get_i3c_addrs()
1467 if (dev->info.static_addr) in i3c_master_get_i3c_addrs()
1468 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_get_i3c_addrs()
1469 dev->info.static_addr, in i3c_master_get_i3c_addrs()
1472 return -EBUSY; in i3c_master_get_i3c_addrs()
1484 if (!dev->info.static_addr && !dev->info.dyn_addr) in i3c_master_attach_i3c_dev()
1492 if (master->this != dev && master->ops->attach_i3c_dev) { in i3c_master_attach_i3c_dev()
1493 ret = master->ops->attach_i3c_dev(dev); in i3c_master_attach_i3c_dev()
1500 list_add_tail(&dev->common.node, &master->bus.devs.i3c); in i3c_master_attach_i3c_dev()
1512 if (dev->info.dyn_addr != old_dyn_addr && in i3c_master_reattach_i3c_dev()
1513 (!dev->boardinfo || in i3c_master_reattach_i3c_dev()
1514 dev->info.dyn_addr != dev->boardinfo->init_dyn_addr)) { in i3c_master_reattach_i3c_dev()
1515 status = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_reattach_i3c_dev()
1516 dev->info.dyn_addr); in i3c_master_reattach_i3c_dev()
1518 return -EBUSY; in i3c_master_reattach_i3c_dev()
1519 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_reattach_i3c_dev()
1520 dev->info.dyn_addr, in i3c_master_reattach_i3c_dev()
1523 i3c_bus_set_addr_slot_status(&master->bus, old_dyn_addr, in i3c_master_reattach_i3c_dev()
1527 if (master->ops->reattach_i3c_dev) { in i3c_master_reattach_i3c_dev()
1528 ret = master->ops->reattach_i3c_dev(dev, old_dyn_addr); in i3c_master_reattach_i3c_dev()
1543 if (master->this != dev && master->ops->detach_i3c_dev) in i3c_master_detach_i3c_dev()
1544 master->ops->detach_i3c_dev(dev); in i3c_master_detach_i3c_dev()
1547 list_del(&dev->common.node); in i3c_master_detach_i3c_dev()
1555 if (master->ops->attach_i2c_dev) { in i3c_master_attach_i2c_dev()
1556 ret = master->ops->attach_i2c_dev(dev); in i3c_master_attach_i2c_dev()
1561 list_add_tail(&dev->common.node, &master->bus.devs.i2c); in i3c_master_attach_i2c_dev()
1570 list_del(&dev->common.node); in i3c_master_detach_i2c_dev()
1572 if (master->ops->detach_i2c_dev) in i3c_master_detach_i2c_dev()
1573 master->ops->detach_i2c_dev(dev); in i3c_master_detach_i2c_dev()
1580 .static_addr = boardinfo->static_addr, in i3c_master_early_i3c_dev_add()
1581 .pid = boardinfo->pid, in i3c_master_early_i3c_dev_add()
1588 return -ENOMEM; in i3c_master_early_i3c_dev_add()
1590 i3cdev->boardinfo = boardinfo; in i3c_master_early_i3c_dev_add()
1596 ret = i3c_master_setdasa_locked(master, i3cdev->info.static_addr, in i3c_master_early_i3c_dev_add()
1597 i3cdev->boardinfo->init_dyn_addr); in i3c_master_early_i3c_dev_add()
1601 i3cdev->info.dyn_addr = i3cdev->boardinfo->init_dyn_addr; in i3c_master_early_i3c_dev_add()
1613 i3c_master_rstdaa_locked(master, i3cdev->boardinfo->init_dyn_addr); in i3c_master_early_i3c_dev_add()
1628 if (!master->init_done) in i3c_master_register_new_i3c_devs()
1631 i3c_bus_for_each_i3cdev(&master->bus, desc) { in i3c_master_register_new_i3c_devs()
1632 if (desc->dev || !desc->info.dyn_addr || desc == master->this) in i3c_master_register_new_i3c_devs()
1635 desc->dev = kzalloc(sizeof(*desc->dev), GFP_KERNEL); in i3c_master_register_new_i3c_devs()
1636 if (!desc->dev) in i3c_master_register_new_i3c_devs()
1639 desc->dev->bus = &master->bus; in i3c_master_register_new_i3c_devs()
1640 desc->dev->desc = desc; in i3c_master_register_new_i3c_devs()
1641 desc->dev->dev.parent = &master->dev; in i3c_master_register_new_i3c_devs()
1642 desc->dev->dev.type = &i3c_device_type; in i3c_master_register_new_i3c_devs()
1643 desc->dev->dev.bus = &i3c_bus_type; in i3c_master_register_new_i3c_devs()
1644 desc->dev->dev.release = i3c_device_release; in i3c_master_register_new_i3c_devs()
1645 dev_set_name(&desc->dev->dev, "%d-%llx", master->bus.id, in i3c_master_register_new_i3c_devs()
1646 desc->info.pid); in i3c_master_register_new_i3c_devs()
1648 if (desc->boardinfo) in i3c_master_register_new_i3c_devs()
1649 desc->dev->dev.of_node = desc->boardinfo->of_node; in i3c_master_register_new_i3c_devs()
1651 ret = device_register(&desc->dev->dev); in i3c_master_register_new_i3c_devs()
1653 dev_err(&master->dev, in i3c_master_register_new_i3c_devs()
1655 put_device(&desc->dev->dev); in i3c_master_register_new_i3c_devs()
1661 * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment)
1671 * This function must be called with the bus lock held in write mode.
1679 i3c_bus_maintenance_lock(&master->bus); in i3c_master_do_daa()
1680 ret = master->ops->do_daa(master); in i3c_master_do_daa()
1681 i3c_bus_maintenance_unlock(&master->bus); in i3c_master_do_daa()
1686 i3c_bus_normaluse_lock(&master->bus); in i3c_master_do_daa()
1688 i3c_bus_normaluse_unlock(&master->bus); in i3c_master_do_daa()
1695 * i3c_master_set_info() - set master device information
1700 * &i3c_master_controller_ops->bus_init().
1705 * - &i3c_device_info->dyn_addr
1706 * - &i3c_device_info->bcr
1707 * - &i3c_device_info->dcr
1708 * - &i3c_device_info->pid
1709 * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in
1710 * &i3c_device_info->bcr
1712 * This function must be called with the bus lock held in maintenance mode.
1715 * information can be checked, but we can at least make sure @info->dyn_addr
1716 * and @info->bcr are correct), -EINVAL otherwise.
1724 if (!i3c_bus_dev_addr_is_avail(&master->bus, info->dyn_addr)) in i3c_master_set_info()
1725 return -EINVAL; in i3c_master_set_info()
1727 if (I3C_BCR_DEVICE_ROLE(info->bcr) == I3C_BCR_I3C_MASTER && in i3c_master_set_info()
1728 master->secondary) in i3c_master_set_info()
1729 return -EINVAL; in i3c_master_set_info()
1731 if (master->this) in i3c_master_set_info()
1732 return -EINVAL; in i3c_master_set_info()
1738 master->this = i3cdev; in i3c_master_set_info()
1739 master->bus.cur_master = master->this; in i3c_master_set_info()
1759 list_for_each_entry_safe(i3cdev, i3ctmp, &master->bus.devs.i3c, in i3c_master_detach_free_devs()
1763 if (i3cdev->boardinfo && i3cdev->boardinfo->init_dyn_addr) in i3c_master_detach_free_devs()
1764 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_detach_free_devs()
1765 i3cdev->boardinfo->init_dyn_addr, in i3c_master_detach_free_devs()
1771 list_for_each_entry_safe(i2cdev, i2ctmp, &master->bus.devs.i2c, in i3c_master_detach_free_devs()
1774 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_detach_free_devs()
1775 i2cdev->addr, in i3c_master_detach_free_devs()
1782 * i3c_master_bus_init() - initialize an I3C bus
1791 * 2. Call &i3c_master_controller_ops->bus_init() method to initialize
1792 * the master controller. That's usually where the bus mode is selected
1795 * 3. Instruct all devices on the bus to drop their dynamic address. This is
1802 * also have static_addr, try to pre-assign dynamic addresses requested by
1806 * 6. Do a DAA (Dynamic Address Assignment) to assign dynamic addresses to all
1825 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) { in i3c_master_bus_init()
1826 status = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_bus_init()
1827 i2cboardinfo->base.addr); in i3c_master_bus_init()
1829 ret = -EBUSY; in i3c_master_bus_init()
1833 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_bus_init()
1834 i2cboardinfo->base.addr, in i3c_master_bus_init()
1838 i2cboardinfo->base.addr, in i3c_master_bus_init()
1839 i2cboardinfo->lvr); in i3c_master_bus_init()
1853 * Now execute the controller specific ->bus_init() routine, which in i3c_master_bus_init()
1856 ret = master->ops->bus_init(master); in i3c_master_bus_init()
1861 * The master device should have been instantiated in ->bus_init(), in i3c_master_bus_init()
1864 if (!master->this) { in i3c_master_bus_init()
1865 dev_err(&master->dev, in i3c_master_bus_init()
1866 "master_set_info() was not called in ->bus_init()\n"); in i3c_master_bus_init()
1867 ret = -EINVAL; in i3c_master_bus_init()
1871 if (master->ops->set_speed) { in i3c_master_bus_init()
1872 ret = master->ops->set_speed(master, I3C_OPEN_DRAIN_SLOW_SPEED); in i3c_master_bus_init()
1878 * Reset all dynamic address that may have been assigned before in i3c_master_bus_init()
1885 if (master->ops->set_speed) { in i3c_master_bus_init()
1886 master->ops->set_speed(master, I3C_OPEN_DRAIN_NORMAL_SPEED); in i3c_master_bus_init()
1899 * Reserve init_dyn_addr first, and then try to pre-assign dynamic in i3c_master_bus_init()
1901 * In case pre-assign dynamic address fails, setting dynamic address to in i3c_master_bus_init()
1905 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) { in i3c_master_bus_init()
1908 * We don't reserve a dynamic address for devices that in i3c_master_bus_init()
1911 if (!i3cboardinfo->init_dyn_addr) in i3c_master_bus_init()
1914 ret = i3c_bus_get_addr_slot_status(&master->bus, in i3c_master_bus_init()
1915 i3cboardinfo->init_dyn_addr); in i3c_master_bus_init()
1917 ret = -EBUSY; in i3c_master_bus_init()
1921 i3c_bus_set_addr_slot_status(&master->bus, in i3c_master_bus_init()
1922 i3cboardinfo->init_dyn_addr, in i3c_master_bus_init()
1928 * DAA happens, and the requested dynamic address will in i3c_master_bus_init()
1933 if (i3cboardinfo->static_addr) in i3c_master_bus_init()
1947 if (master->ops->bus_cleanup) in i3c_master_bus_init()
1948 master->ops->bus_cleanup(master); in i3c_master_bus_init()
1958 if (master->ops->bus_cleanup) in i3c_master_bus_cleanup()
1959 master->ops->bus_cleanup(master); in i3c_master_bus_cleanup()
1966 struct i3c_master_controller *master = i3cdev->common.master; in i3c_master_attach_boardinfo()
1969 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) { in i3c_master_attach_boardinfo()
1970 if (i3cdev->info.pid != i3cboardinfo->pid) in i3c_master_attach_boardinfo()
1973 i3cdev->boardinfo = i3cboardinfo; in i3c_master_attach_boardinfo()
1974 i3cdev->info.static_addr = i3cboardinfo->static_addr; in i3c_master_attach_boardinfo()
1985 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) { in i3c_master_search_i3c_dev_duplicate()
1986 if (i3cdev != refdev && i3cdev->info.pid == refdev->info.pid) in i3c_master_search_i3c_dev_duplicate()
1994 * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus
1996 * @addr: I3C slave dynamic address assigned to the device
2005 * This function must be called with the bus lock held in write mode.
2020 return -EINVAL; in i3c_master_add_i3c_dev_locked()
2038 newdev->dev = olddev->dev; in i3c_master_add_i3c_dev_locked()
2039 if (newdev->dev) in i3c_master_add_i3c_dev_locked()
2040 newdev->dev->desc = newdev; in i3c_master_add_i3c_dev_locked()
2048 mutex_lock(&olddev->ibi_lock); in i3c_master_add_i3c_dev_locked()
2049 if (olddev->ibi) { in i3c_master_add_i3c_dev_locked()
2050 ibireq.handler = olddev->ibi->handler; in i3c_master_add_i3c_dev_locked()
2051 ibireq.max_payload_len = olddev->ibi->max_payload_len; in i3c_master_add_i3c_dev_locked()
2052 ibireq.num_slots = olddev->ibi->num_slots; in i3c_master_add_i3c_dev_locked()
2054 if (olddev->ibi->enabled) { in i3c_master_add_i3c_dev_locked()
2061 mutex_unlock(&olddev->ibi_lock); in i3c_master_add_i3c_dev_locked()
2063 old_dyn_addr = olddev->info.dyn_addr; in i3c_master_add_i3c_dev_locked()
2070 * Depending on our previous state, the expected dynamic address might in i3c_master_add_i3c_dev_locked()
2072 * - if the device already had a dynamic address assigned, let's try to in i3c_master_add_i3c_dev_locked()
2073 * re-apply this one in i3c_master_add_i3c_dev_locked()
2074 * - if the device did not have a dynamic address and the firmware in i3c_master_add_i3c_dev_locked()
2076 * - in any other case, keep the address automatically assigned by the in i3c_master_add_i3c_dev_locked()
2079 if (old_dyn_addr && old_dyn_addr != newdev->info.dyn_addr) in i3c_master_add_i3c_dev_locked()
2081 else if (newdev->boardinfo && newdev->boardinfo->init_dyn_addr) in i3c_master_add_i3c_dev_locked()
2082 expected_dyn_addr = newdev->boardinfo->init_dyn_addr; in i3c_master_add_i3c_dev_locked()
2084 expected_dyn_addr = newdev->info.dyn_addr; in i3c_master_add_i3c_dev_locked()
2086 if (newdev->info.dyn_addr != expected_dyn_addr) { in i3c_master_add_i3c_dev_locked()
2088 * Try to apply the expected dynamic address. If it fails, keep in i3c_master_add_i3c_dev_locked()
2092 newdev->info.dyn_addr, in i3c_master_add_i3c_dev_locked()
2095 old_dyn_addr = newdev->info.dyn_addr; in i3c_master_add_i3c_dev_locked()
2096 newdev->info.dyn_addr = expected_dyn_addr; in i3c_master_add_i3c_dev_locked()
2099 dev_err(&master->dev, in i3c_master_add_i3c_dev_locked()
2101 master->bus.id, newdev->info.pid); in i3c_master_add_i3c_dev_locked()
2113 mutex_lock(&newdev->ibi_lock); in i3c_master_add_i3c_dev_locked()
2116 dev_err(&master->dev, in i3c_master_add_i3c_dev_locked()
2117 "Failed to request IBI on device %d-%llx", in i3c_master_add_i3c_dev_locked()
2118 master->bus.id, newdev->info.pid); in i3c_master_add_i3c_dev_locked()
2122 dev_err(&master->dev, in i3c_master_add_i3c_dev_locked()
2123 "Failed to re-enable IBI on device %d-%llx", in i3c_master_add_i3c_dev_locked()
2124 master->bus.id, newdev->info.pid); in i3c_master_add_i3c_dev_locked()
2126 mutex_unlock(&newdev->ibi_lock); in i3c_master_add_i3c_dev_locked()
2132 if (newdev->dev && newdev->dev->desc) in i3c_master_add_i3c_dev_locked()
2133 newdev->dev->desc = NULL; in i3c_master_add_i3c_dev_locked()
2151 struct device *dev = &master->dev; in of_i3c_master_add_i2c_boardinfo()
2156 return -ENOMEM; in of_i3c_master_add_i2c_boardinfo()
2158 ret = of_i2c_get_board_info(dev, node, &boardinfo->base); in of_i3c_master_add_i2c_boardinfo()
2163 * The I3C Specification does not clearly say I2C devices with 10-bit in of_i3c_master_add_i2c_boardinfo()
2167 if (boardinfo->base.flags & I2C_CLIENT_TEN) { in of_i3c_master_add_i2c_boardinfo()
2169 return -ENOTSUPP; in of_i3c_master_add_i2c_boardinfo()
2173 boardinfo->lvr = reg[2]; in of_i3c_master_add_i2c_boardinfo()
2175 list_add_tail(&boardinfo->node, &master->boardinfo.i2c); in of_i3c_master_add_i2c_boardinfo()
2186 struct device *dev = &master->dev; in of_i3c_master_add_i3c_boardinfo()
2192 return -ENOMEM; in of_i3c_master_add_i3c_boardinfo()
2196 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2198 addrstatus = i3c_bus_get_addr_slot_status(&master->bus, in of_i3c_master_add_i3c_boardinfo()
2201 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2204 boardinfo->static_addr = reg[0]; in of_i3c_master_add_i3c_boardinfo()
2206 if (!of_property_read_u32(node, "assigned-address", &init_dyn_addr)) { in of_i3c_master_add_i3c_boardinfo()
2208 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2210 addrstatus = i3c_bus_get_addr_slot_status(&master->bus, in of_i3c_master_add_i3c_boardinfo()
2213 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2216 boardinfo->pid = ((u64)reg[1] << 32) | reg[2]; in of_i3c_master_add_i3c_boardinfo()
2218 if ((boardinfo->pid & GENMASK_ULL(63, 48)) || in of_i3c_master_add_i3c_boardinfo()
2219 I3C_PID_RND_LOWER_32BITS(boardinfo->pid)) in of_i3c_master_add_i3c_boardinfo()
2220 return -EINVAL; in of_i3c_master_add_i3c_boardinfo()
2222 boardinfo->init_dyn_addr = init_dyn_addr; in of_i3c_master_add_i3c_boardinfo()
2223 boardinfo->of_node = of_node_get(node); in of_i3c_master_add_i3c_boardinfo()
2224 list_add_tail(&boardinfo->node, &master->boardinfo.i3c); in of_i3c_master_add_i3c_boardinfo()
2236 return -EINVAL; in of_i3c_master_add_dev()
2256 struct device *dev = &master->dev; in of_populate_i3c_bus()
2257 struct device_node *i3cbus_np = dev->of_node; in of_populate_i3c_bus()
2278 if (!of_property_read_u32(i3cbus_np, "i2c-scl-hz", &val)) in of_populate_i3c_bus()
2279 master->bus.scl_rate.i2c = val; in of_populate_i3c_bus()
2281 if (!of_property_read_u32(i3cbus_np, "i3c-scl-hz", &val)) in of_populate_i3c_bus()
2282 master->bus.scl_rate.i3c = val; in of_populate_i3c_bus()
2296 return -EINVAL; in i3c_master_i2c_adapter_xfer()
2298 if (!master->ops->i2c_xfers) in i3c_master_i2c_adapter_xfer()
2299 return -ENOTSUPP; in i3c_master_i2c_adapter_xfer()
2305 return -ENOTSUPP; in i3c_master_i2c_adapter_xfer()
2308 i3c_bus_normaluse_lock(&master->bus); in i3c_master_i2c_adapter_xfer()
2311 ret = -ENOENT; in i3c_master_i2c_adapter_xfer()
2313 ret = master->ops->i2c_xfers(dev, xfers, nxfers); in i3c_master_i2c_adapter_xfer()
2314 i3c_bus_normaluse_unlock(&master->bus); in i3c_master_i2c_adapter_xfer()
2326 /* Fall back to no spike filters and FM bus mode. */ in i3c_master_i2c_get_lvr()
2329 if (client->dev.of_node) { in i3c_master_i2c_get_lvr()
2332 if (!of_property_read_u32_array(client->dev.of_node, "reg", in i3c_master_i2c_get_lvr()
2348 if (i3c_master_find_i2c_dev_by_addr(master, client->addr)) in i3c_master_i2c_attach()
2351 status = i3c_bus_get_addr_slot_status(&master->bus, client->addr); in i3c_master_i2c_attach()
2353 return -EBUSY; in i3c_master_i2c_attach()
2355 i3c_bus_set_addr_slot_status(&master->bus, client->addr, in i3c_master_i2c_attach()
2358 i2cdev = i3c_master_alloc_i2c_dev(master, client->addr, in i3c_master_i2c_attach()
2374 i3c_bus_set_addr_slot_status(&master->bus, client->addr, in i3c_master_i2c_attach()
2385 dev = i3c_master_find_i2c_dev_by_addr(master, client->addr); in i3c_master_i2c_detach()
2387 return -ENODEV; in i3c_master_i2c_detach()
2390 i3c_bus_set_addr_slot_status(&master->bus, dev->addr, in i3c_master_i2c_detach()
2411 if (dev->type != &i2c_client_type) in i3c_i2c_notifier_call()
2415 adap = client->adapter; in i3c_i2c_notifier_call()
2417 if (adap->algo != &i3c_master_i2c_algo) in i3c_i2c_notifier_call()
2422 i3c_bus_maintenance_lock(&master->bus); in i3c_i2c_notifier_call()
2431 i3c_bus_maintenance_unlock(&master->bus); in i3c_i2c_notifier_call()
2447 adap->dev.parent = master->dev.parent; in i3c_master_i2c_adapter_init()
2448 adap->owner = master->dev.parent->driver->owner; in i3c_master_i2c_adapter_init()
2449 adap->algo = &i3c_master_i2c_algo; in i3c_master_i2c_adapter_init()
2450 strscpy(adap->name, dev_name(master->dev.parent), sizeof(adap->name)); in i3c_master_i2c_adapter_init()
2453 adap->timeout = 1000; in i3c_master_i2c_adapter_init()
2454 adap->retries = 3; in i3c_master_i2c_adapter_init()
2464 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) { in i3c_master_i2c_adapter_init()
2466 i2cboardinfo->base.addr); in i3c_master_i2c_adapter_init()
2469 i2cdev->dev = i2c_new_client_device(adap, &i2cboardinfo->base); in i3c_master_i2c_adapter_init()
2479 i2c_del_adapter(&master->i2c); in i3c_master_i2c_adapter_cleanup()
2481 i3c_bus_for_each_i2cdev(&master->bus, i2cdev) in i3c_master_i2c_adapter_cleanup()
2482 i2cdev->dev = NULL; in i3c_master_i2c_adapter_cleanup()
2489 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) { in i3c_master_unregister_i3c_devs()
2490 if (!i3cdev->dev) in i3c_master_unregister_i3c_devs()
2493 i3cdev->dev->desc = NULL; in i3c_master_unregister_i3c_devs()
2494 if (device_is_registered(&i3cdev->dev->dev)) in i3c_master_unregister_i3c_devs()
2495 device_unregister(&i3cdev->dev->dev); in i3c_master_unregister_i3c_devs()
2497 put_device(&i3cdev->dev->dev); in i3c_master_unregister_i3c_devs()
2498 i3cdev->dev = NULL; in i3c_master_unregister_i3c_devs()
2503 * i3c_master_queue_ibi() - Queue an IBI
2512 atomic_inc(&dev->ibi->pending_ibis); in i3c_master_queue_ibi()
2513 queue_work(dev->ibi->wq, &slot->work); in i3c_master_queue_ibi()
2521 struct i3c_dev_desc *dev = slot->dev; in i3c_master_handle_ibi()
2525 payload.data = slot->data; in i3c_master_handle_ibi()
2526 payload.len = slot->len; in i3c_master_handle_ibi()
2528 if (dev->dev) in i3c_master_handle_ibi()
2529 dev->ibi->handler(dev->dev, &payload); in i3c_master_handle_ibi()
2531 master->ops->recycle_ibi_slot(dev, slot); in i3c_master_handle_ibi()
2532 if (atomic_dec_and_test(&dev->ibi->pending_ibis)) in i3c_master_handle_ibi()
2533 complete(&dev->ibi->all_ibis_handled); in i3c_master_handle_ibi()
2539 slot->dev = dev; in i3c_master_init_ibi_slot()
2540 INIT_WORK(&slot->work, i3c_master_handle_ibi); in i3c_master_init_ibi_slot()
2558 * i3c_generic_ibi_free_pool() - Free a generic IBI pool
2568 while (!list_empty(&pool->free_slots)) { in i3c_generic_ibi_free_pool()
2569 slot = list_first_entry(&pool->free_slots, in i3c_generic_ibi_free_pool()
2571 list_del(&slot->node); in i3c_generic_ibi_free_pool()
2579 WARN_ON(nslots != pool->num_slots); in i3c_generic_ibi_free_pool()
2581 kfree(pool->payload_buf); in i3c_generic_ibi_free_pool()
2582 kfree(pool->slots); in i3c_generic_ibi_free_pool()
2588 * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool
2607 return ERR_PTR(-ENOMEM); in i3c_generic_ibi_alloc_pool()
2609 spin_lock_init(&pool->lock); in i3c_generic_ibi_alloc_pool()
2610 INIT_LIST_HEAD(&pool->free_slots); in i3c_generic_ibi_alloc_pool()
2611 INIT_LIST_HEAD(&pool->pending); in i3c_generic_ibi_alloc_pool()
2613 pool->slots = kcalloc(req->num_slots, sizeof(*slot), GFP_KERNEL); in i3c_generic_ibi_alloc_pool()
2614 if (!pool->slots) { in i3c_generic_ibi_alloc_pool()
2615 ret = -ENOMEM; in i3c_generic_ibi_alloc_pool()
2619 if (req->max_payload_len) { in i3c_generic_ibi_alloc_pool()
2620 pool->payload_buf = kcalloc(req->num_slots, in i3c_generic_ibi_alloc_pool()
2621 req->max_payload_len, GFP_KERNEL); in i3c_generic_ibi_alloc_pool()
2622 if (!pool->payload_buf) { in i3c_generic_ibi_alloc_pool()
2623 ret = -ENOMEM; in i3c_generic_ibi_alloc_pool()
2628 for (i = 0; i < req->num_slots; i++) { in i3c_generic_ibi_alloc_pool()
2629 slot = &pool->slots[i]; in i3c_generic_ibi_alloc_pool()
2630 i3c_master_init_ibi_slot(dev, &slot->base); in i3c_generic_ibi_alloc_pool()
2632 if (req->max_payload_len) in i3c_generic_ibi_alloc_pool()
2633 slot->base.data = pool->payload_buf + in i3c_generic_ibi_alloc_pool()
2634 (i * req->max_payload_len); in i3c_generic_ibi_alloc_pool()
2636 list_add_tail(&slot->node, &pool->free_slots); in i3c_generic_ibi_alloc_pool()
2637 pool->num_slots++; in i3c_generic_ibi_alloc_pool()
2649 * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool
2664 spin_lock_irqsave(&pool->lock, flags); in i3c_generic_ibi_get_free_slot()
2665 slot = list_first_entry_or_null(&pool->free_slots, in i3c_generic_ibi_get_free_slot()
2668 list_del(&slot->node); in i3c_generic_ibi_get_free_slot()
2669 spin_unlock_irqrestore(&pool->lock, flags); in i3c_generic_ibi_get_free_slot()
2671 return slot ? &slot->base : NULL; in i3c_generic_ibi_get_free_slot()
2676 * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool
2681 * master driver struct_master_controller_ops->recycle_ibi() method.
2693 spin_lock_irqsave(&pool->lock, flags); in i3c_generic_ibi_recycle_slot()
2694 list_add_tail(&slot->node, &pool->free_slots); in i3c_generic_ibi_recycle_slot()
2695 spin_unlock_irqrestore(&pool->lock, flags); in i3c_generic_ibi_recycle_slot()
2701 if (!ops || !ops->bus_init || !ops->priv_xfers || in i3c_master_check_ops()
2702 !ops->send_ccc_cmd || !ops->do_daa || !ops->i2c_xfers) in i3c_master_check_ops()
2703 return -EINVAL; in i3c_master_check_ops()
2705 if (ops->request_ibi && in i3c_master_check_ops()
2706 (!ops->enable_ibi || !ops->disable_ibi || !ops->free_ibi || in i3c_master_check_ops()
2707 !ops->recycle_ibi_slot)) in i3c_master_check_ops()
2708 return -EINVAL; in i3c_master_check_ops()
2714 * i3c_master_register() - register an I3C master
2720 * -ENOTSUPP if set to true since secondary masters are not yet
2725 * - creates and initializes the I3C bus
2726 * - populates the bus with static I2C devs if @parent->of_node is not
2728 * - registers all I3C devices added by the controller during bus
2730 * - registers the I2C adapter and all I2C devices
2741 enum i3c_bus_mode mode = I3C_BUS_MODE_PURE; in i3c_master_register() local
2747 return -ENOTSUPP; in i3c_master_register()
2753 master->dev.parent = parent; in i3c_master_register()
2754 master->dev.of_node = of_node_get(parent->of_node); in i3c_master_register()
2755 master->dev.bus = &i3c_bus_type; in i3c_master_register()
2756 master->dev.type = &i3c_masterdev_type; in i3c_master_register()
2757 master->dev.release = i3c_masterdev_release; in i3c_master_register()
2758 master->ops = ops; in i3c_master_register()
2759 master->secondary = secondary; in i3c_master_register()
2760 INIT_LIST_HEAD(&master->boardinfo.i2c); in i3c_master_register()
2761 INIT_LIST_HEAD(&master->boardinfo.i3c); in i3c_master_register()
2763 ret = i3c_bus_init(i3cbus, master->dev.of_node); in i3c_master_register()
2767 device_initialize(&master->dev); in i3c_master_register()
2768 dev_set_name(&master->dev, "i3c-%d", i3cbus->id); in i3c_master_register()
2770 master->dev.dma_mask = parent->dma_mask; in i3c_master_register()
2771 master->dev.coherent_dma_mask = parent->coherent_dma_mask; in i3c_master_register()
2772 master->dev.dma_parms = parent->dma_parms; in i3c_master_register()
2778 list_for_each_entry(i2cbi, &master->boardinfo.i2c, node) { in i3c_master_register()
2779 switch (i2cbi->lvr & I3C_LVR_I2C_INDEX_MASK) { in i3c_master_register()
2781 if (mode < I3C_BUS_MODE_MIXED_FAST) in i3c_master_register()
2782 mode = I3C_BUS_MODE_MIXED_FAST; in i3c_master_register()
2785 if (mode < I3C_BUS_MODE_MIXED_LIMITED) in i3c_master_register()
2786 mode = I3C_BUS_MODE_MIXED_LIMITED; in i3c_master_register()
2789 if (mode < I3C_BUS_MODE_MIXED_SLOW) in i3c_master_register()
2790 mode = I3C_BUS_MODE_MIXED_SLOW; in i3c_master_register()
2793 ret = -EINVAL; in i3c_master_register()
2797 if (i2cbi->lvr & I3C_LVR_I2C_FM_MODE) in i3c_master_register()
2801 ret = i3c_bus_set_mode(i3cbus, mode, i2c_scl_rate); in i3c_master_register()
2805 master->wq = alloc_workqueue("%s", 0, 0, dev_name(parent)); in i3c_master_register()
2806 if (!master->wq) { in i3c_master_register()
2807 ret = -ENOMEM; in i3c_master_register()
2815 ret = device_add(&master->dev); in i3c_master_register()
2829 pm_runtime_no_callbacks(&master->dev); in i3c_master_register()
2830 pm_suspend_ignore_children(&master->dev, true); in i3c_master_register()
2831 pm_runtime_enable(&master->dev); in i3c_master_register()
2837 master->init_done = true; in i3c_master_register()
2838 i3c_bus_normaluse_lock(&master->bus); in i3c_master_register()
2840 i3c_bus_normaluse_unlock(&master->bus); in i3c_master_register()
2845 device_del(&master->dev); in i3c_master_register()
2851 put_device(&master->dev); in i3c_master_register()
2858 * i3c_master_unregister() - unregister an I3C master
2865 i3c_bus_notify(&master->bus, I3C_NOTIFY_BUS_REMOVE); in i3c_master_unregister()
2870 pm_runtime_disable(&master->dev); in i3c_master_unregister()
2871 device_unregister(&master->dev); in i3c_master_unregister()
2880 return -ENOENT; in i3c_dev_setdasa_locked()
2884 return -EINVAL; in i3c_dev_setdasa_locked()
2886 if (!dev->boardinfo || !dev->boardinfo->init_dyn_addr || in i3c_dev_setdasa_locked()
2887 !dev->boardinfo->static_addr) in i3c_dev_setdasa_locked()
2888 return -EINVAL; in i3c_dev_setdasa_locked()
2890 return i3c_master_setdasa_locked(master, dev->info.static_addr, in i3c_dev_setdasa_locked()
2891 dev->boardinfo->init_dyn_addr); in i3c_dev_setdasa_locked()
2901 return -ENOENT; in i3c_dev_do_priv_xfers_locked()
2905 return -EINVAL; in i3c_dev_do_priv_xfers_locked()
2907 if (!master->ops->priv_xfers) in i3c_dev_do_priv_xfers_locked()
2908 return -ENOTSUPP; in i3c_dev_do_priv_xfers_locked()
2910 return master->ops->priv_xfers(dev, xfers, nxfers); in i3c_dev_do_priv_xfers_locked()
2918 if (!dev->ibi) in i3c_dev_disable_ibi_locked()
2919 return -EINVAL; in i3c_dev_disable_ibi_locked()
2922 ret = master->ops->disable_ibi(dev); in i3c_dev_disable_ibi_locked()
2926 reinit_completion(&dev->ibi->all_ibis_handled); in i3c_dev_disable_ibi_locked()
2927 if (atomic_read(&dev->ibi->pending_ibis)) in i3c_dev_disable_ibi_locked()
2928 wait_for_completion(&dev->ibi->all_ibis_handled); in i3c_dev_disable_ibi_locked()
2930 dev->ibi->enabled = false; in i3c_dev_disable_ibi_locked()
2940 if (!dev->ibi) in i3c_dev_enable_ibi_locked()
2941 return -EINVAL; in i3c_dev_enable_ibi_locked()
2943 ret = master->ops->enable_ibi(dev); in i3c_dev_enable_ibi_locked()
2945 dev->ibi->enabled = true; in i3c_dev_enable_ibi_locked()
2957 if (!master->ops->request_ibi) in i3c_dev_request_ibi_locked()
2958 return -ENOTSUPP; in i3c_dev_request_ibi_locked()
2960 if (dev->ibi) in i3c_dev_request_ibi_locked()
2961 return -EBUSY; in i3c_dev_request_ibi_locked()
2965 return -ENOMEM; in i3c_dev_request_ibi_locked()
2967 ibi->wq = alloc_ordered_workqueue(dev_name(i3cdev_to_dev(dev->dev)), WQ_MEM_RECLAIM); in i3c_dev_request_ibi_locked()
2968 if (!ibi->wq) { in i3c_dev_request_ibi_locked()
2970 return -ENOMEM; in i3c_dev_request_ibi_locked()
2973 atomic_set(&ibi->pending_ibis, 0); in i3c_dev_request_ibi_locked()
2974 init_completion(&ibi->all_ibis_handled); in i3c_dev_request_ibi_locked()
2975 ibi->handler = req->handler; in i3c_dev_request_ibi_locked()
2976 ibi->max_payload_len = req->max_payload_len; in i3c_dev_request_ibi_locked()
2977 ibi->num_slots = req->num_slots; in i3c_dev_request_ibi_locked()
2979 dev->ibi = ibi; in i3c_dev_request_ibi_locked()
2980 ret = master->ops->request_ibi(dev, req); in i3c_dev_request_ibi_locked()
2983 dev->ibi = NULL; in i3c_dev_request_ibi_locked()
2993 if (!dev->ibi) in i3c_dev_free_ibi_locked()
2996 if (WARN_ON(dev->ibi->enabled)) in i3c_dev_free_ibi_locked()
2999 master->ops->free_ibi(dev); in i3c_dev_free_ibi_locked()
3001 if (dev->ibi->wq) { in i3c_dev_free_ibi_locked()
3002 destroy_workqueue(dev->ibi->wq); in i3c_dev_free_ibi_locked()
3003 dev->ibi->wq = NULL; in i3c_dev_free_ibi_locked()
3006 kfree(dev->ibi); in i3c_dev_free_ibi_locked()
3007 dev->ibi = NULL; in i3c_dev_free_ibi_locked()