1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Cadence Design Systems Inc.
5 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
8 #include <linux/atomic.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/workqueue.h>
20 #include "internals.h"
22 static DEFINE_IDR(i3c_bus_idr);
23 static DEFINE_MUTEX(i3c_core_lock);
24 static int __i3c_first_dynamic_bus_num;
27 * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation
28 * @bus: I3C bus to take the lock on
30 * This function takes the bus lock so that no other operations can occur on
31 * the bus. This is needed for all kind of bus maintenance operation, like
32 * - enabling/disabling slave events
34 * - changing the dynamic address of a device
35 * - relinquishing mastership
38 * The reason for this kind of locking is that we don't want drivers and core
39 * logic to rely on I3C device information that could be changed behind their
42 static void i3c_bus_maintenance_lock(struct i3c_bus *bus)
44 down_write(&bus->lock);
48 * i3c_bus_maintenance_unlock - Release the bus lock after a maintenance
50 * @bus: I3C bus to release the lock on
52 * Should be called when the bus maintenance operation is done. See
53 * i3c_bus_maintenance_lock() for more details on what these maintenance
56 static void i3c_bus_maintenance_unlock(struct i3c_bus *bus)
62 * i3c_bus_normaluse_lock - Lock the bus for a normal operation
63 * @bus: I3C bus to take the lock on
65 * This function takes the bus lock for any operation that is not a maintenance
66 * operation (see i3c_bus_maintenance_lock() for a non-exhaustive list of
67 * maintenance operations). Basically all communications with I3C devices are
68 * normal operations (HDR, SDR transfers or CCC commands that do not change bus
69 * state or I3C dynamic address).
71 * Note that this lock is not guaranteeing serialization of normal operations.
72 * In other words, transfer requests passed to the I3C master can be submitted
73 * in parallel and I3C master drivers have to use their own locking to make
74 * sure two different communications are not inter-mixed, or access to the
75 * output/input queue is not done while the engine is busy.
77 void i3c_bus_normaluse_lock(struct i3c_bus *bus)
79 down_read(&bus->lock);
83 * i3c_bus_normaluse_unlock - Release the bus lock after a normal operation
84 * @bus: I3C bus to release the lock on
86 * Should be called when a normal operation is done. See
87 * i3c_bus_normaluse_lock() for more details on what these normal operations
90 void i3c_bus_normaluse_unlock(struct i3c_bus *bus)
95 static struct i3c_master_controller *
96 i3c_bus_to_i3c_master(struct i3c_bus *i3cbus)
98 return container_of(i3cbus, struct i3c_master_controller, bus);
101 static struct i3c_master_controller *dev_to_i3cmaster(struct device *dev)
103 return container_of(dev, struct i3c_master_controller, dev);
106 static const struct device_type i3c_device_type;
108 static struct i3c_bus *dev_to_i3cbus(struct device *dev)
110 struct i3c_master_controller *master;
112 if (dev->type == &i3c_device_type)
113 return dev_to_i3cdev(dev)->bus;
115 master = dev_to_i3cmaster(dev);
120 static struct i3c_dev_desc *dev_to_i3cdesc(struct device *dev)
122 struct i3c_master_controller *master;
124 if (dev->type == &i3c_device_type)
125 return dev_to_i3cdev(dev)->desc;
127 master = dev_to_i3cmaster(dev);
132 static ssize_t bcr_show(struct device *dev,
133 struct device_attribute *da,
136 struct i3c_bus *bus = dev_to_i3cbus(dev);
137 struct i3c_dev_desc *desc;
140 i3c_bus_normaluse_lock(bus);
141 desc = dev_to_i3cdesc(dev);
142 ret = sprintf(buf, "%x\n", desc->info.bcr);
143 i3c_bus_normaluse_unlock(bus);
147 static DEVICE_ATTR_RO(bcr);
149 static ssize_t dcr_show(struct device *dev,
150 struct device_attribute *da,
153 struct i3c_bus *bus = dev_to_i3cbus(dev);
154 struct i3c_dev_desc *desc;
157 i3c_bus_normaluse_lock(bus);
158 desc = dev_to_i3cdesc(dev);
159 ret = sprintf(buf, "%x\n", desc->info.dcr);
160 i3c_bus_normaluse_unlock(bus);
164 static DEVICE_ATTR_RO(dcr);
166 static ssize_t pid_show(struct device *dev,
167 struct device_attribute *da,
170 struct i3c_bus *bus = dev_to_i3cbus(dev);
171 struct i3c_dev_desc *desc;
174 i3c_bus_normaluse_lock(bus);
175 desc = dev_to_i3cdesc(dev);
176 ret = sprintf(buf, "%llx\n", desc->info.pid);
177 i3c_bus_normaluse_unlock(bus);
181 static DEVICE_ATTR_RO(pid);
183 static ssize_t dynamic_address_show(struct device *dev,
184 struct device_attribute *da,
187 struct i3c_bus *bus = dev_to_i3cbus(dev);
188 struct i3c_dev_desc *desc;
191 i3c_bus_normaluse_lock(bus);
192 desc = dev_to_i3cdesc(dev);
193 ret = sprintf(buf, "%02x\n", desc->info.dyn_addr);
194 i3c_bus_normaluse_unlock(bus);
198 static DEVICE_ATTR_RO(dynamic_address);
200 static const char * const hdrcap_strings[] = {
201 "hdr-ddr", "hdr-tsp", "hdr-tsl",
204 static ssize_t hdrcap_show(struct device *dev,
205 struct device_attribute *da,
208 struct i3c_bus *bus = dev_to_i3cbus(dev);
209 struct i3c_dev_desc *desc;
210 ssize_t offset = 0, ret;
214 i3c_bus_normaluse_lock(bus);
215 desc = dev_to_i3cdesc(dev);
216 caps = desc->info.hdr_cap;
217 for_each_set_bit(mode, &caps, 8) {
218 if (mode >= ARRAY_SIZE(hdrcap_strings))
221 if (!hdrcap_strings[mode])
224 ret = sprintf(buf + offset, offset ? " %s" : "%s",
225 hdrcap_strings[mode]);
232 ret = sprintf(buf + offset, "\n");
239 i3c_bus_normaluse_unlock(bus);
243 static DEVICE_ATTR_RO(hdrcap);
245 static ssize_t modalias_show(struct device *dev,
246 struct device_attribute *da, char *buf)
248 struct i3c_device *i3c = dev_to_i3cdev(dev);
249 struct i3c_device_info devinfo;
250 u16 manuf, part, ext;
252 i3c_device_get_info(i3c, &devinfo);
253 manuf = I3C_PID_MANUF_ID(devinfo.pid);
254 part = I3C_PID_PART_ID(devinfo.pid);
255 ext = I3C_PID_EXTRA_INFO(devinfo.pid);
257 if (I3C_PID_RND_LOWER_32BITS(devinfo.pid))
258 return sprintf(buf, "i3c:dcr%02Xmanuf%04X", devinfo.dcr,
261 return sprintf(buf, "i3c:dcr%02Xmanuf%04Xpart%04Xext%04X",
262 devinfo.dcr, manuf, part, ext);
264 static DEVICE_ATTR_RO(modalias);
266 static struct attribute *i3c_device_attrs[] = {
270 &dev_attr_dynamic_address.attr,
271 &dev_attr_hdrcap.attr,
272 &dev_attr_modalias.attr,
275 ATTRIBUTE_GROUPS(i3c_device);
277 static int i3c_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
279 const struct i3c_device *i3cdev = dev_to_i3cdev(dev);
280 struct i3c_device_info devinfo;
281 u16 manuf, part, ext;
283 i3c_device_get_info(i3cdev, &devinfo);
284 manuf = I3C_PID_MANUF_ID(devinfo.pid);
285 part = I3C_PID_PART_ID(devinfo.pid);
286 ext = I3C_PID_EXTRA_INFO(devinfo.pid);
288 if (I3C_PID_RND_LOWER_32BITS(devinfo.pid))
289 return add_uevent_var(env, "MODALIAS=i3c:dcr%02Xmanuf%04X",
292 return add_uevent_var(env,
293 "MODALIAS=i3c:dcr%02Xmanuf%04Xpart%04Xext%04X",
294 devinfo.dcr, manuf, part, ext);
297 static const struct device_type i3c_device_type = {
298 .groups = i3c_device_groups,
299 .uevent = i3c_device_uevent,
302 static int i3c_device_match(struct device *dev, struct device_driver *drv)
304 struct i3c_device *i3cdev;
305 struct i3c_driver *i3cdrv;
307 if (dev->type != &i3c_device_type)
310 i3cdev = dev_to_i3cdev(dev);
311 i3cdrv = drv_to_i3cdrv(drv);
312 if (i3c_device_match_id(i3cdev, i3cdrv->id_table))
318 static int i3c_device_probe(struct device *dev)
320 struct i3c_device *i3cdev = dev_to_i3cdev(dev);
321 struct i3c_driver *driver = drv_to_i3cdrv(dev->driver);
323 return driver->probe(i3cdev);
326 static void i3c_device_remove(struct device *dev)
328 struct i3c_device *i3cdev = dev_to_i3cdev(dev);
329 struct i3c_driver *driver = drv_to_i3cdrv(dev->driver);
332 driver->remove(i3cdev);
334 i3c_device_free_ibi(i3cdev);
337 struct bus_type i3c_bus_type = {
339 .match = i3c_device_match,
340 .probe = i3c_device_probe,
341 .remove = i3c_device_remove,
344 static enum i3c_addr_slot_status
345 i3c_bus_get_addr_slot_status(struct i3c_bus *bus, u16 addr)
347 unsigned long status;
348 int bitpos = addr * 2;
350 if (addr > I2C_MAX_ADDR)
351 return I3C_ADDR_SLOT_RSVD;
353 status = bus->addrslots[bitpos / BITS_PER_LONG];
354 status >>= bitpos % BITS_PER_LONG;
356 return status & I3C_ADDR_SLOT_STATUS_MASK;
359 static void i3c_bus_set_addr_slot_status(struct i3c_bus *bus, u16 addr,
360 enum i3c_addr_slot_status status)
362 int bitpos = addr * 2;
365 if (addr > I2C_MAX_ADDR)
368 ptr = bus->addrslots + (bitpos / BITS_PER_LONG);
369 *ptr &= ~((unsigned long)I3C_ADDR_SLOT_STATUS_MASK <<
370 (bitpos % BITS_PER_LONG));
371 *ptr |= (unsigned long)status << (bitpos % BITS_PER_LONG);
374 static bool i3c_bus_dev_addr_is_avail(struct i3c_bus *bus, u8 addr)
376 enum i3c_addr_slot_status status;
378 status = i3c_bus_get_addr_slot_status(bus, addr);
380 return status == I3C_ADDR_SLOT_FREE;
383 static int i3c_bus_get_free_addr(struct i3c_bus *bus, u8 start_addr)
385 enum i3c_addr_slot_status status;
388 for (addr = start_addr; addr < I3C_MAX_ADDR; addr++) {
389 status = i3c_bus_get_addr_slot_status(bus, addr);
390 if (status == I3C_ADDR_SLOT_FREE)
397 static void i3c_bus_init_addrslots(struct i3c_bus *bus)
401 /* Addresses 0 to 7 are reserved. */
402 for (i = 0; i < 8; i++)
403 i3c_bus_set_addr_slot_status(bus, i, I3C_ADDR_SLOT_RSVD);
406 * Reserve broadcast address and all addresses that might collide
407 * with the broadcast address when facing a single bit error.
409 i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR,
411 for (i = 0; i < 7; i++)
412 i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR ^ BIT(i),
416 static void i3c_bus_cleanup(struct i3c_bus *i3cbus)
418 mutex_lock(&i3c_core_lock);
419 idr_remove(&i3c_bus_idr, i3cbus->id);
420 mutex_unlock(&i3c_core_lock);
423 static int i3c_bus_init(struct i3c_bus *i3cbus, struct device_node *np)
425 int ret, start, end, id = -1;
427 init_rwsem(&i3cbus->lock);
428 INIT_LIST_HEAD(&i3cbus->devs.i2c);
429 INIT_LIST_HEAD(&i3cbus->devs.i3c);
430 i3c_bus_init_addrslots(i3cbus);
431 i3cbus->mode = I3C_BUS_MODE_PURE;
434 id = of_alias_get_id(np, "i3c");
436 mutex_lock(&i3c_core_lock);
441 start = __i3c_first_dynamic_bus_num;
445 ret = idr_alloc(&i3c_bus_idr, i3cbus, start, end, GFP_KERNEL);
446 mutex_unlock(&i3c_core_lock);
456 static const char * const i3c_bus_mode_strings[] = {
457 [I3C_BUS_MODE_PURE] = "pure",
458 [I3C_BUS_MODE_MIXED_FAST] = "mixed-fast",
459 [I3C_BUS_MODE_MIXED_LIMITED] = "mixed-limited",
460 [I3C_BUS_MODE_MIXED_SLOW] = "mixed-slow",
463 static ssize_t mode_show(struct device *dev,
464 struct device_attribute *da,
467 struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
470 i3c_bus_normaluse_lock(i3cbus);
471 if (i3cbus->mode < 0 ||
472 i3cbus->mode >= ARRAY_SIZE(i3c_bus_mode_strings) ||
473 !i3c_bus_mode_strings[i3cbus->mode])
474 ret = sprintf(buf, "unknown\n");
476 ret = sprintf(buf, "%s\n", i3c_bus_mode_strings[i3cbus->mode]);
477 i3c_bus_normaluse_unlock(i3cbus);
481 static DEVICE_ATTR_RO(mode);
483 static ssize_t current_master_show(struct device *dev,
484 struct device_attribute *da,
487 struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
490 i3c_bus_normaluse_lock(i3cbus);
491 ret = sprintf(buf, "%d-%llx\n", i3cbus->id,
492 i3cbus->cur_master->info.pid);
493 i3c_bus_normaluse_unlock(i3cbus);
497 static DEVICE_ATTR_RO(current_master);
499 static ssize_t i3c_scl_frequency_show(struct device *dev,
500 struct device_attribute *da,
503 struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
506 i3c_bus_normaluse_lock(i3cbus);
507 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i3c);
508 i3c_bus_normaluse_unlock(i3cbus);
512 static DEVICE_ATTR_RO(i3c_scl_frequency);
514 static ssize_t i2c_scl_frequency_show(struct device *dev,
515 struct device_attribute *da,
518 struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
521 i3c_bus_normaluse_lock(i3cbus);
522 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i2c);
523 i3c_bus_normaluse_unlock(i3cbus);
527 static DEVICE_ATTR_RO(i2c_scl_frequency);
529 static struct attribute *i3c_masterdev_attrs[] = {
531 &dev_attr_current_master.attr,
532 &dev_attr_i3c_scl_frequency.attr,
533 &dev_attr_i2c_scl_frequency.attr,
537 &dev_attr_dynamic_address.attr,
538 &dev_attr_hdrcap.attr,
541 ATTRIBUTE_GROUPS(i3c_masterdev);
543 static void i3c_masterdev_release(struct device *dev)
545 struct i3c_master_controller *master = dev_to_i3cmaster(dev);
546 struct i3c_bus *bus = dev_to_i3cbus(dev);
549 destroy_workqueue(master->wq);
551 WARN_ON(!list_empty(&bus->devs.i2c) || !list_empty(&bus->devs.i3c));
552 i3c_bus_cleanup(bus);
554 of_node_put(dev->of_node);
557 static const struct device_type i3c_masterdev_type = {
558 .groups = i3c_masterdev_groups,
561 static int i3c_bus_set_mode(struct i3c_bus *i3cbus, enum i3c_bus_mode mode,
562 unsigned long max_i2c_scl_rate)
564 struct i3c_master_controller *master = i3c_bus_to_i3c_master(i3cbus);
568 switch (i3cbus->mode) {
569 case I3C_BUS_MODE_PURE:
570 if (!i3cbus->scl_rate.i3c)
571 i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE;
573 case I3C_BUS_MODE_MIXED_FAST:
574 case I3C_BUS_MODE_MIXED_LIMITED:
575 if (!i3cbus->scl_rate.i3c)
576 i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE;
577 if (!i3cbus->scl_rate.i2c)
578 i3cbus->scl_rate.i2c = max_i2c_scl_rate;
580 case I3C_BUS_MODE_MIXED_SLOW:
581 if (!i3cbus->scl_rate.i2c)
582 i3cbus->scl_rate.i2c = max_i2c_scl_rate;
583 if (!i3cbus->scl_rate.i3c ||
584 i3cbus->scl_rate.i3c > i3cbus->scl_rate.i2c)
585 i3cbus->scl_rate.i3c = i3cbus->scl_rate.i2c;
591 dev_dbg(&master->dev, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n",
592 i3cbus->scl_rate.i2c, i3cbus->scl_rate.i3c);
595 * I3C/I2C frequency may have been overridden, check that user-provided
596 * values are not exceeding max possible frequency.
598 if (i3cbus->scl_rate.i3c > I3C_BUS_MAX_I3C_SCL_RATE ||
599 i3cbus->scl_rate.i2c > I3C_BUS_I2C_FM_PLUS_SCL_RATE)
605 static struct i3c_master_controller *
606 i2c_adapter_to_i3c_master(struct i2c_adapter *adap)
608 return container_of(adap, struct i3c_master_controller, i2c);
611 static struct i2c_adapter *
612 i3c_master_to_i2c_adapter(struct i3c_master_controller *master)
617 static void i3c_master_free_i2c_dev(struct i2c_dev_desc *dev)
622 static struct i2c_dev_desc *
623 i3c_master_alloc_i2c_dev(struct i3c_master_controller *master,
626 struct i2c_dev_desc *dev;
628 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
630 return ERR_PTR(-ENOMEM);
632 dev->common.master = master;
639 static void *i3c_ccc_cmd_dest_init(struct i3c_ccc_cmd_dest *dest, u8 addr,
643 dest->payload.len = payloadlen;
645 dest->payload.data = kzalloc(payloadlen, GFP_KERNEL);
647 dest->payload.data = NULL;
649 return dest->payload.data;
652 static void i3c_ccc_cmd_dest_cleanup(struct i3c_ccc_cmd_dest *dest)
654 kfree(dest->payload.data);
657 static void i3c_ccc_cmd_init(struct i3c_ccc_cmd *cmd, bool rnw, u8 id,
658 struct i3c_ccc_cmd_dest *dests,
661 cmd->rnw = rnw ? 1 : 0;
664 cmd->ndests = ndests;
665 cmd->err = I3C_ERROR_UNKNOWN;
668 static int i3c_master_send_ccc_cmd_locked(struct i3c_master_controller *master,
669 struct i3c_ccc_cmd *cmd)
676 if (WARN_ON(master->init_done &&
677 !rwsem_is_locked(&master->bus.lock)))
680 if (!master->ops->send_ccc_cmd)
683 if ((cmd->id & I3C_CCC_DIRECT) && (!cmd->dests || !cmd->ndests))
686 if (master->ops->supports_ccc_cmd &&
687 !master->ops->supports_ccc_cmd(master, cmd))
690 ret = master->ops->send_ccc_cmd(master, cmd);
692 if (cmd->err != I3C_ERROR_UNKNOWN)
701 static struct i2c_dev_desc *
702 i3c_master_find_i2c_dev_by_addr(const struct i3c_master_controller *master,
705 struct i2c_dev_desc *dev;
707 i3c_bus_for_each_i2cdev(&master->bus, dev) {
708 if (dev->addr == addr)
716 * i3c_master_get_free_addr() - get a free address on the bus
717 * @master: I3C master object
718 * @start_addr: where to start searching
720 * This function must be called with the bus lock held in write mode.
722 * Return: the first free address starting at @start_addr (included) or -ENOMEM
723 * if there's no more address available.
725 int i3c_master_get_free_addr(struct i3c_master_controller *master,
728 return i3c_bus_get_free_addr(&master->bus, start_addr);
730 EXPORT_SYMBOL_GPL(i3c_master_get_free_addr);
732 static void i3c_device_release(struct device *dev)
734 struct i3c_device *i3cdev = dev_to_i3cdev(dev);
736 WARN_ON(i3cdev->desc);
738 of_node_put(i3cdev->dev.of_node);
742 static void i3c_master_free_i3c_dev(struct i3c_dev_desc *dev)
747 static struct i3c_dev_desc *
748 i3c_master_alloc_i3c_dev(struct i3c_master_controller *master,
749 const struct i3c_device_info *info)
751 struct i3c_dev_desc *dev;
753 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
755 return ERR_PTR(-ENOMEM);
757 dev->common.master = master;
759 mutex_init(&dev->ibi_lock);
764 static int i3c_master_rstdaa_locked(struct i3c_master_controller *master,
767 enum i3c_addr_slot_status addrstat;
768 struct i3c_ccc_cmd_dest dest;
769 struct i3c_ccc_cmd cmd;
775 addrstat = i3c_bus_get_addr_slot_status(&master->bus, addr);
776 if (addr != I3C_BROADCAST_ADDR && addrstat != I3C_ADDR_SLOT_I3C_DEV)
779 i3c_ccc_cmd_dest_init(&dest, addr, 0);
780 i3c_ccc_cmd_init(&cmd, false,
781 I3C_CCC_RSTDAA(addr == I3C_BROADCAST_ADDR),
783 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
784 i3c_ccc_cmd_dest_cleanup(&dest);
790 * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment)
792 * @master: master used to send frames on the bus
794 * Send a ENTDAA CCC command to start a DAA procedure.
796 * Note that this function only sends the ENTDAA CCC command, all the logic
797 * behind dynamic address assignment has to be handled in the I3C master
800 * This function must be called with the bus lock held in write mode.
802 * Return: 0 in case of success, a positive I3C error code if the error is
803 * one of the official Mx error codes, and a negative error code otherwise.
805 int i3c_master_entdaa_locked(struct i3c_master_controller *master)
807 struct i3c_ccc_cmd_dest dest;
808 struct i3c_ccc_cmd cmd;
811 i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR, 0);
812 i3c_ccc_cmd_init(&cmd, false, I3C_CCC_ENTDAA, &dest, 1);
813 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
814 i3c_ccc_cmd_dest_cleanup(&dest);
818 EXPORT_SYMBOL_GPL(i3c_master_entdaa_locked);
820 static int i3c_master_enec_disec_locked(struct i3c_master_controller *master,
821 u8 addr, bool enable, u8 evts)
823 struct i3c_ccc_events *events;
824 struct i3c_ccc_cmd_dest dest;
825 struct i3c_ccc_cmd cmd;
828 events = i3c_ccc_cmd_dest_init(&dest, addr, sizeof(*events));
832 events->events = evts;
833 i3c_ccc_cmd_init(&cmd, false,
835 I3C_CCC_ENEC(addr == I3C_BROADCAST_ADDR) :
836 I3C_CCC_DISEC(addr == I3C_BROADCAST_ADDR),
838 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
839 i3c_ccc_cmd_dest_cleanup(&dest);
845 * i3c_master_disec_locked() - send a DISEC CCC command
846 * @master: master used to send frames on the bus
847 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
848 * @evts: events to disable
850 * Send a DISEC CCC command to disable some or all events coming from a
851 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
853 * This function must be called with the bus lock held in write mode.
855 * Return: 0 in case of success, a positive I3C error code if the error is
856 * one of the official Mx error codes, and a negative error code otherwise.
858 int i3c_master_disec_locked(struct i3c_master_controller *master, u8 addr,
861 return i3c_master_enec_disec_locked(master, addr, false, evts);
863 EXPORT_SYMBOL_GPL(i3c_master_disec_locked);
866 * i3c_master_enec_locked() - send an ENEC CCC command
867 * @master: master used to send frames on the bus
868 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
869 * @evts: events to disable
871 * Sends an ENEC CCC command to enable some or all events coming from a
872 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
874 * This function must be called with the bus lock held in write mode.
876 * Return: 0 in case of success, a positive I3C error code if the error is
877 * one of the official Mx error codes, and a negative error code otherwise.
879 int i3c_master_enec_locked(struct i3c_master_controller *master, u8 addr,
882 return i3c_master_enec_disec_locked(master, addr, true, evts);
884 EXPORT_SYMBOL_GPL(i3c_master_enec_locked);
887 * i3c_master_defslvs_locked() - send a DEFSLVS CCC command
888 * @master: master used to send frames on the bus
890 * Send a DEFSLVS CCC command containing all the devices known to the @master.
891 * This is useful when you have secondary masters on the bus to propagate
892 * device information.
894 * This should be called after all I3C devices have been discovered (in other
895 * words, after the DAA procedure has finished) and instantiated in
896 * &i3c_master_controller_ops->bus_init().
897 * It should also be called if a master ACKed an Hot-Join request and assigned
898 * a dynamic address to the device joining the bus.
900 * This function must be called with the bus lock held in write mode.
902 * Return: 0 in case of success, a positive I3C error code if the error is
903 * one of the official Mx error codes, and a negative error code otherwise.
905 int i3c_master_defslvs_locked(struct i3c_master_controller *master)
907 struct i3c_ccc_defslvs *defslvs;
908 struct i3c_ccc_dev_desc *desc;
909 struct i3c_ccc_cmd_dest dest;
910 struct i3c_dev_desc *i3cdev;
911 struct i2c_dev_desc *i2cdev;
912 struct i3c_ccc_cmd cmd;
920 bus = i3c_master_get_bus(master);
921 i3c_bus_for_each_i3cdev(bus, i3cdev) {
924 if (i3cdev == master->this)
927 if (I3C_BCR_DEVICE_ROLE(i3cdev->info.bcr) ==
932 /* No other master on the bus, skip DEFSLVS. */
936 i3c_bus_for_each_i2cdev(bus, i2cdev)
939 defslvs = i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR,
940 struct_size(defslvs, slaves,
945 defslvs->count = ndevs;
946 defslvs->master.bcr = master->this->info.bcr;
947 defslvs->master.dcr = master->this->info.dcr;
948 defslvs->master.dyn_addr = master->this->info.dyn_addr << 1;
949 defslvs->master.static_addr = I3C_BROADCAST_ADDR << 1;
951 desc = defslvs->slaves;
952 i3c_bus_for_each_i2cdev(bus, i2cdev) {
953 desc->lvr = i2cdev->lvr;
954 desc->static_addr = i2cdev->addr << 1;
958 i3c_bus_for_each_i3cdev(bus, i3cdev) {
959 /* Skip the I3C dev representing this master. */
960 if (i3cdev == master->this)
963 desc->bcr = i3cdev->info.bcr;
964 desc->dcr = i3cdev->info.dcr;
965 desc->dyn_addr = i3cdev->info.dyn_addr << 1;
966 desc->static_addr = i3cdev->info.static_addr << 1;
970 i3c_ccc_cmd_init(&cmd, false, I3C_CCC_DEFSLVS, &dest, 1);
971 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
972 i3c_ccc_cmd_dest_cleanup(&dest);
976 EXPORT_SYMBOL_GPL(i3c_master_defslvs_locked);
978 static int i3c_master_setda_locked(struct i3c_master_controller *master,
979 u8 oldaddr, u8 newaddr, bool setdasa)
981 struct i3c_ccc_cmd_dest dest;
982 struct i3c_ccc_setda *setda;
983 struct i3c_ccc_cmd cmd;
986 if (!oldaddr || !newaddr)
989 setda = i3c_ccc_cmd_dest_init(&dest, oldaddr, sizeof(*setda));
993 setda->addr = newaddr << 1;
994 i3c_ccc_cmd_init(&cmd, false,
995 setdasa ? I3C_CCC_SETDASA : I3C_CCC_SETNEWDA,
997 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
998 i3c_ccc_cmd_dest_cleanup(&dest);
1003 static int i3c_master_setdasa_locked(struct i3c_master_controller *master,
1004 u8 static_addr, u8 dyn_addr)
1006 return i3c_master_setda_locked(master, static_addr, dyn_addr, true);
1009 static int i3c_master_setnewda_locked(struct i3c_master_controller *master,
1010 u8 oldaddr, u8 newaddr)
1012 return i3c_master_setda_locked(master, oldaddr, newaddr, false);
1015 static int i3c_master_getmrl_locked(struct i3c_master_controller *master,
1016 struct i3c_device_info *info)
1018 struct i3c_ccc_cmd_dest dest;
1019 struct i3c_ccc_mrl *mrl;
1020 struct i3c_ccc_cmd cmd;
1023 mrl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mrl));
1028 * When the device does not have IBI payload GETMRL only returns 2
1031 if (!(info->bcr & I3C_BCR_IBI_PAYLOAD))
1032 dest.payload.len -= 1;
1034 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMRL, &dest, 1);
1035 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1039 switch (dest.payload.len) {
1041 info->max_ibi_len = mrl->ibi_len;
1044 info->max_read_len = be16_to_cpu(mrl->read_len);
1052 i3c_ccc_cmd_dest_cleanup(&dest);
1057 static int i3c_master_getmwl_locked(struct i3c_master_controller *master,
1058 struct i3c_device_info *info)
1060 struct i3c_ccc_cmd_dest dest;
1061 struct i3c_ccc_mwl *mwl;
1062 struct i3c_ccc_cmd cmd;
1065 mwl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mwl));
1069 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMWL, &dest, 1);
1070 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1074 if (dest.payload.len != sizeof(*mwl)) {
1079 info->max_write_len = be16_to_cpu(mwl->len);
1082 i3c_ccc_cmd_dest_cleanup(&dest);
1087 static int i3c_master_getmxds_locked(struct i3c_master_controller *master,
1088 struct i3c_device_info *info)
1090 struct i3c_ccc_getmxds *getmaxds;
1091 struct i3c_ccc_cmd_dest dest;
1092 struct i3c_ccc_cmd cmd;
1095 getmaxds = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr,
1100 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMXDS, &dest, 1);
1101 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1105 if (dest.payload.len != 2 && dest.payload.len != 5) {
1110 info->max_read_ds = getmaxds->maxrd;
1111 info->max_write_ds = getmaxds->maxwr;
1112 if (dest.payload.len == 5)
1113 info->max_read_turnaround = getmaxds->maxrdturn[0] |
1114 ((u32)getmaxds->maxrdturn[1] << 8) |
1115 ((u32)getmaxds->maxrdturn[2] << 16);
1118 i3c_ccc_cmd_dest_cleanup(&dest);
1123 static int i3c_master_gethdrcap_locked(struct i3c_master_controller *master,
1124 struct i3c_device_info *info)
1126 struct i3c_ccc_gethdrcap *gethdrcap;
1127 struct i3c_ccc_cmd_dest dest;
1128 struct i3c_ccc_cmd cmd;
1131 gethdrcap = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr,
1132 sizeof(*gethdrcap));
1136 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETHDRCAP, &dest, 1);
1137 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1141 if (dest.payload.len != 1) {
1146 info->hdr_cap = gethdrcap->modes;
1149 i3c_ccc_cmd_dest_cleanup(&dest);
1154 static int i3c_master_getpid_locked(struct i3c_master_controller *master,
1155 struct i3c_device_info *info)
1157 struct i3c_ccc_getpid *getpid;
1158 struct i3c_ccc_cmd_dest dest;
1159 struct i3c_ccc_cmd cmd;
1162 getpid = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getpid));
1166 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETPID, &dest, 1);
1167 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1172 for (i = 0; i < sizeof(getpid->pid); i++) {
1173 int sft = (sizeof(getpid->pid) - i - 1) * 8;
1175 info->pid |= (u64)getpid->pid[i] << sft;
1179 i3c_ccc_cmd_dest_cleanup(&dest);
1184 static int i3c_master_getbcr_locked(struct i3c_master_controller *master,
1185 struct i3c_device_info *info)
1187 struct i3c_ccc_getbcr *getbcr;
1188 struct i3c_ccc_cmd_dest dest;
1189 struct i3c_ccc_cmd cmd;
1192 getbcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getbcr));
1196 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETBCR, &dest, 1);
1197 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1201 info->bcr = getbcr->bcr;
1204 i3c_ccc_cmd_dest_cleanup(&dest);
1209 static int i3c_master_getdcr_locked(struct i3c_master_controller *master,
1210 struct i3c_device_info *info)
1212 struct i3c_ccc_getdcr *getdcr;
1213 struct i3c_ccc_cmd_dest dest;
1214 struct i3c_ccc_cmd cmd;
1217 getdcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getdcr));
1221 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETDCR, &dest, 1);
1222 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1226 info->dcr = getdcr->dcr;
1229 i3c_ccc_cmd_dest_cleanup(&dest);
1234 static int i3c_master_retrieve_dev_info(struct i3c_dev_desc *dev)
1236 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1237 enum i3c_addr_slot_status slot_status;
1240 if (!dev->info.dyn_addr)
1243 slot_status = i3c_bus_get_addr_slot_status(&master->bus,
1244 dev->info.dyn_addr);
1245 if (slot_status == I3C_ADDR_SLOT_RSVD ||
1246 slot_status == I3C_ADDR_SLOT_I2C_DEV)
1249 ret = i3c_master_getpid_locked(master, &dev->info);
1253 ret = i3c_master_getbcr_locked(master, &dev->info);
1257 ret = i3c_master_getdcr_locked(master, &dev->info);
1261 if (dev->info.bcr & I3C_BCR_MAX_DATA_SPEED_LIM) {
1262 ret = i3c_master_getmxds_locked(master, &dev->info);
1267 if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD)
1268 dev->info.max_ibi_len = 1;
1270 i3c_master_getmrl_locked(master, &dev->info);
1271 i3c_master_getmwl_locked(master, &dev->info);
1273 if (dev->info.bcr & I3C_BCR_HDR_CAP) {
1274 ret = i3c_master_gethdrcap_locked(master, &dev->info);
1282 static void i3c_master_put_i3c_addrs(struct i3c_dev_desc *dev)
1284 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1286 if (dev->info.static_addr)
1287 i3c_bus_set_addr_slot_status(&master->bus,
1288 dev->info.static_addr,
1289 I3C_ADDR_SLOT_FREE);
1291 if (dev->info.dyn_addr)
1292 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1293 I3C_ADDR_SLOT_FREE);
1295 if (dev->boardinfo && dev->boardinfo->init_dyn_addr)
1296 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1297 I3C_ADDR_SLOT_FREE);
1300 static int i3c_master_get_i3c_addrs(struct i3c_dev_desc *dev)
1302 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1303 enum i3c_addr_slot_status status;
1305 if (!dev->info.static_addr && !dev->info.dyn_addr)
1308 if (dev->info.static_addr) {
1309 status = i3c_bus_get_addr_slot_status(&master->bus,
1310 dev->info.static_addr);
1311 /* Since static address and assigned dynamic address can be
1312 * equal, allow this case to pass.
1314 if (status != I3C_ADDR_SLOT_FREE &&
1315 dev->info.static_addr != dev->boardinfo->init_dyn_addr)
1318 i3c_bus_set_addr_slot_status(&master->bus,
1319 dev->info.static_addr,
1320 I3C_ADDR_SLOT_I3C_DEV);
1324 * ->init_dyn_addr should have been reserved before that, so, if we're
1325 * trying to apply a pre-reserved dynamic address, we should not try
1326 * to reserve the address slot a second time.
1328 if (dev->info.dyn_addr &&
1330 dev->boardinfo->init_dyn_addr != dev->info.dyn_addr)) {
1331 status = i3c_bus_get_addr_slot_status(&master->bus,
1332 dev->info.dyn_addr);
1333 if (status != I3C_ADDR_SLOT_FREE)
1334 goto err_release_static_addr;
1336 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1337 I3C_ADDR_SLOT_I3C_DEV);
1342 err_release_static_addr:
1343 if (dev->info.static_addr)
1344 i3c_bus_set_addr_slot_status(&master->bus,
1345 dev->info.static_addr,
1346 I3C_ADDR_SLOT_FREE);
1351 static int i3c_master_attach_i3c_dev(struct i3c_master_controller *master,
1352 struct i3c_dev_desc *dev)
1357 * We don't attach devices to the controller until they are
1358 * addressable on the bus.
1360 if (!dev->info.static_addr && !dev->info.dyn_addr)
1363 ret = i3c_master_get_i3c_addrs(dev);
1367 /* Do not attach the master device itself. */
1368 if (master->this != dev && master->ops->attach_i3c_dev) {
1369 ret = master->ops->attach_i3c_dev(dev);
1371 i3c_master_put_i3c_addrs(dev);
1376 list_add_tail(&dev->common.node, &master->bus.devs.i3c);
1381 static int i3c_master_reattach_i3c_dev(struct i3c_dev_desc *dev,
1384 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1385 enum i3c_addr_slot_status status;
1388 if (dev->info.dyn_addr != old_dyn_addr &&
1390 dev->info.dyn_addr != dev->boardinfo->init_dyn_addr)) {
1391 status = i3c_bus_get_addr_slot_status(&master->bus,
1392 dev->info.dyn_addr);
1393 if (status != I3C_ADDR_SLOT_FREE)
1395 i3c_bus_set_addr_slot_status(&master->bus,
1397 I3C_ADDR_SLOT_I3C_DEV);
1399 i3c_bus_set_addr_slot_status(&master->bus, old_dyn_addr,
1400 I3C_ADDR_SLOT_FREE);
1403 if (master->ops->reattach_i3c_dev) {
1404 ret = master->ops->reattach_i3c_dev(dev, old_dyn_addr);
1406 i3c_master_put_i3c_addrs(dev);
1414 static void i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
1416 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1418 /* Do not detach the master device itself. */
1419 if (master->this != dev && master->ops->detach_i3c_dev)
1420 master->ops->detach_i3c_dev(dev);
1422 i3c_master_put_i3c_addrs(dev);
1423 list_del(&dev->common.node);
1426 static int i3c_master_attach_i2c_dev(struct i3c_master_controller *master,
1427 struct i2c_dev_desc *dev)
1431 if (master->ops->attach_i2c_dev) {
1432 ret = master->ops->attach_i2c_dev(dev);
1437 list_add_tail(&dev->common.node, &master->bus.devs.i2c);
1442 static void i3c_master_detach_i2c_dev(struct i2c_dev_desc *dev)
1444 struct i3c_master_controller *master = i2c_dev_get_master(dev);
1446 list_del(&dev->common.node);
1448 if (master->ops->detach_i2c_dev)
1449 master->ops->detach_i2c_dev(dev);
1452 static int i3c_master_early_i3c_dev_add(struct i3c_master_controller *master,
1453 struct i3c_dev_boardinfo *boardinfo)
1455 struct i3c_device_info info = {
1456 .static_addr = boardinfo->static_addr,
1457 .pid = boardinfo->pid,
1459 struct i3c_dev_desc *i3cdev;
1462 i3cdev = i3c_master_alloc_i3c_dev(master, &info);
1466 i3cdev->boardinfo = boardinfo;
1468 ret = i3c_master_attach_i3c_dev(master, i3cdev);
1472 ret = i3c_master_setdasa_locked(master, i3cdev->info.static_addr,
1473 i3cdev->boardinfo->init_dyn_addr);
1475 goto err_detach_dev;
1477 i3cdev->info.dyn_addr = i3cdev->boardinfo->init_dyn_addr;
1478 ret = i3c_master_reattach_i3c_dev(i3cdev, 0);
1482 ret = i3c_master_retrieve_dev_info(i3cdev);
1489 i3c_master_rstdaa_locked(master, i3cdev->boardinfo->init_dyn_addr);
1491 i3c_master_detach_i3c_dev(i3cdev);
1493 i3c_master_free_i3c_dev(i3cdev);
1499 i3c_master_register_new_i3c_devs(struct i3c_master_controller *master)
1501 struct i3c_dev_desc *desc;
1504 if (!master->init_done)
1507 i3c_bus_for_each_i3cdev(&master->bus, desc) {
1508 if (desc->dev || !desc->info.dyn_addr || desc == master->this)
1511 desc->dev = kzalloc(sizeof(*desc->dev), GFP_KERNEL);
1515 desc->dev->bus = &master->bus;
1516 desc->dev->desc = desc;
1517 desc->dev->dev.parent = &master->dev;
1518 desc->dev->dev.type = &i3c_device_type;
1519 desc->dev->dev.bus = &i3c_bus_type;
1520 desc->dev->dev.release = i3c_device_release;
1521 dev_set_name(&desc->dev->dev, "%d-%llx", master->bus.id,
1524 if (desc->boardinfo)
1525 desc->dev->dev.of_node = desc->boardinfo->of_node;
1527 ret = device_register(&desc->dev->dev);
1529 dev_err(&master->dev,
1530 "Failed to add I3C device (err = %d)\n", ret);
1535 * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment)
1536 * @master: master doing the DAA
1538 * This function is instantiating an I3C device object and adding it to the
1539 * I3C device list. All device information are automatically retrieved using
1540 * standard CCC commands.
1542 * The I3C device object is returned in case the master wants to attach
1543 * private data to it using i3c_dev_set_master_data().
1545 * This function must be called with the bus lock held in write mode.
1547 * Return: a 0 in case of success, an negative error code otherwise.
1549 int i3c_master_do_daa(struct i3c_master_controller *master)
1553 i3c_bus_maintenance_lock(&master->bus);
1554 ret = master->ops->do_daa(master);
1555 i3c_bus_maintenance_unlock(&master->bus);
1560 i3c_bus_normaluse_lock(&master->bus);
1561 i3c_master_register_new_i3c_devs(master);
1562 i3c_bus_normaluse_unlock(&master->bus);
1566 EXPORT_SYMBOL_GPL(i3c_master_do_daa);
1569 * i3c_master_set_info() - set master device information
1570 * @master: master used to send frames on the bus
1571 * @info: I3C device information
1573 * Set master device info. This should be called from
1574 * &i3c_master_controller_ops->bus_init().
1576 * Not all &i3c_device_info fields are meaningful for a master device.
1577 * Here is a list of fields that should be properly filled:
1579 * - &i3c_device_info->dyn_addr
1580 * - &i3c_device_info->bcr
1581 * - &i3c_device_info->dcr
1582 * - &i3c_device_info->pid
1583 * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in
1584 * &i3c_device_info->bcr
1586 * This function must be called with the bus lock held in maintenance mode.
1588 * Return: 0 if @info contains valid information (not every piece of
1589 * information can be checked, but we can at least make sure @info->dyn_addr
1590 * and @info->bcr are correct), -EINVAL otherwise.
1592 int i3c_master_set_info(struct i3c_master_controller *master,
1593 const struct i3c_device_info *info)
1595 struct i3c_dev_desc *i3cdev;
1598 if (!i3c_bus_dev_addr_is_avail(&master->bus, info->dyn_addr))
1601 if (I3C_BCR_DEVICE_ROLE(info->bcr) == I3C_BCR_I3C_MASTER &&
1608 i3cdev = i3c_master_alloc_i3c_dev(master, info);
1610 return PTR_ERR(i3cdev);
1612 master->this = i3cdev;
1613 master->bus.cur_master = master->this;
1615 ret = i3c_master_attach_i3c_dev(master, i3cdev);
1622 i3c_master_free_i3c_dev(i3cdev);
1626 EXPORT_SYMBOL_GPL(i3c_master_set_info);
1628 static void i3c_master_detach_free_devs(struct i3c_master_controller *master)
1630 struct i3c_dev_desc *i3cdev, *i3ctmp;
1631 struct i2c_dev_desc *i2cdev, *i2ctmp;
1633 list_for_each_entry_safe(i3cdev, i3ctmp, &master->bus.devs.i3c,
1635 i3c_master_detach_i3c_dev(i3cdev);
1637 if (i3cdev->boardinfo && i3cdev->boardinfo->init_dyn_addr)
1638 i3c_bus_set_addr_slot_status(&master->bus,
1639 i3cdev->boardinfo->init_dyn_addr,
1640 I3C_ADDR_SLOT_FREE);
1642 i3c_master_free_i3c_dev(i3cdev);
1645 list_for_each_entry_safe(i2cdev, i2ctmp, &master->bus.devs.i2c,
1647 i3c_master_detach_i2c_dev(i2cdev);
1648 i3c_bus_set_addr_slot_status(&master->bus,
1650 I3C_ADDR_SLOT_FREE);
1651 i3c_master_free_i2c_dev(i2cdev);
1656 * i3c_master_bus_init() - initialize an I3C bus
1657 * @master: main master initializing the bus
1659 * This function is following all initialisation steps described in the I3C
1662 * 1. Attach I2C devs to the master so that the master can fill its internal
1663 * device table appropriately
1665 * 2. Call &i3c_master_controller_ops->bus_init() method to initialize
1666 * the master controller. That's usually where the bus mode is selected
1667 * (pure bus or mixed fast/slow bus)
1669 * 3. Instruct all devices on the bus to drop their dynamic address. This is
1670 * particularly important when the bus was previously configured by someone
1671 * else (for example the bootloader)
1673 * 4. Disable all slave events.
1675 * 5. Reserve address slots for I3C devices with init_dyn_addr. And if devices
1676 * also have static_addr, try to pre-assign dynamic addresses requested by
1677 * the FW with SETDASA and attach corresponding statically defined I3C
1678 * devices to the master.
1680 * 6. Do a DAA (Dynamic Address Assignment) to assign dynamic addresses to all
1681 * remaining I3C devices
1683 * Once this is done, all I3C and I2C devices should be usable.
1685 * Return: a 0 in case of success, an negative error code otherwise.
1687 static int i3c_master_bus_init(struct i3c_master_controller *master)
1689 enum i3c_addr_slot_status status;
1690 struct i2c_dev_boardinfo *i2cboardinfo;
1691 struct i3c_dev_boardinfo *i3cboardinfo;
1692 struct i2c_dev_desc *i2cdev;
1696 * First attach all devices with static definitions provided by the
1699 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) {
1700 status = i3c_bus_get_addr_slot_status(&master->bus,
1701 i2cboardinfo->base.addr);
1702 if (status != I3C_ADDR_SLOT_FREE) {
1704 goto err_detach_devs;
1707 i3c_bus_set_addr_slot_status(&master->bus,
1708 i2cboardinfo->base.addr,
1709 I3C_ADDR_SLOT_I2C_DEV);
1711 i2cdev = i3c_master_alloc_i2c_dev(master,
1712 i2cboardinfo->base.addr,
1714 if (IS_ERR(i2cdev)) {
1715 ret = PTR_ERR(i2cdev);
1716 goto err_detach_devs;
1719 ret = i3c_master_attach_i2c_dev(master, i2cdev);
1721 i3c_master_free_i2c_dev(i2cdev);
1722 goto err_detach_devs;
1727 * Now execute the controller specific ->bus_init() routine, which
1728 * might configure its internal logic to match the bus limitations.
1730 ret = master->ops->bus_init(master);
1732 goto err_detach_devs;
1735 * The master device should have been instantiated in ->bus_init(),
1736 * complain if this was not the case.
1738 if (!master->this) {
1739 dev_err(&master->dev,
1740 "master_set_info() was not called in ->bus_init()\n");
1742 goto err_bus_cleanup;
1746 * Reset all dynamic address that may have been assigned before
1747 * (assigned by the bootloader for example).
1749 ret = i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
1750 if (ret && ret != I3C_ERROR_M2)
1751 goto err_bus_cleanup;
1753 /* Disable all slave events before starting DAA. */
1754 ret = i3c_master_disec_locked(master, I3C_BROADCAST_ADDR,
1755 I3C_CCC_EVENT_SIR | I3C_CCC_EVENT_MR |
1757 if (ret && ret != I3C_ERROR_M2)
1758 goto err_bus_cleanup;
1761 * Reserve init_dyn_addr first, and then try to pre-assign dynamic
1762 * address and retrieve device information if needed.
1763 * In case pre-assign dynamic address fails, setting dynamic address to
1764 * the requested init_dyn_addr is retried after DAA is done in
1765 * i3c_master_add_i3c_dev_locked().
1767 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) {
1770 * We don't reserve a dynamic address for devices that
1771 * don't explicitly request one.
1773 if (!i3cboardinfo->init_dyn_addr)
1776 ret = i3c_bus_get_addr_slot_status(&master->bus,
1777 i3cboardinfo->init_dyn_addr);
1778 if (ret != I3C_ADDR_SLOT_FREE) {
1783 i3c_bus_set_addr_slot_status(&master->bus,
1784 i3cboardinfo->init_dyn_addr,
1785 I3C_ADDR_SLOT_I3C_DEV);
1788 * Only try to create/attach devices that have a static
1789 * address. Other devices will be created/attached when
1790 * DAA happens, and the requested dynamic address will
1791 * be set using SETNEWDA once those devices become
1795 if (i3cboardinfo->static_addr)
1796 i3c_master_early_i3c_dev_add(master, i3cboardinfo);
1799 ret = i3c_master_do_daa(master);
1806 i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
1809 if (master->ops->bus_cleanup)
1810 master->ops->bus_cleanup(master);
1813 i3c_master_detach_free_devs(master);
1818 static void i3c_master_bus_cleanup(struct i3c_master_controller *master)
1820 if (master->ops->bus_cleanup)
1821 master->ops->bus_cleanup(master);
1823 i3c_master_detach_free_devs(master);
1826 static void i3c_master_attach_boardinfo(struct i3c_dev_desc *i3cdev)
1828 struct i3c_master_controller *master = i3cdev->common.master;
1829 struct i3c_dev_boardinfo *i3cboardinfo;
1831 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) {
1832 if (i3cdev->info.pid != i3cboardinfo->pid)
1835 i3cdev->boardinfo = i3cboardinfo;
1836 i3cdev->info.static_addr = i3cboardinfo->static_addr;
1841 static struct i3c_dev_desc *
1842 i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc *refdev)
1844 struct i3c_master_controller *master = i3c_dev_get_master(refdev);
1845 struct i3c_dev_desc *i3cdev;
1847 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) {
1848 if (i3cdev != refdev && i3cdev->info.pid == refdev->info.pid)
1856 * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus
1857 * @master: master used to send frames on the bus
1858 * @addr: I3C slave dynamic address assigned to the device
1860 * This function is instantiating an I3C device object and adding it to the
1861 * I3C device list. All device information are automatically retrieved using
1862 * standard CCC commands.
1864 * The I3C device object is returned in case the master wants to attach
1865 * private data to it using i3c_dev_set_master_data().
1867 * This function must be called with the bus lock held in write mode.
1869 * Return: a 0 in case of success, an negative error code otherwise.
1871 int i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master,
1874 struct i3c_device_info info = { .dyn_addr = addr };
1875 struct i3c_dev_desc *newdev, *olddev;
1876 u8 old_dyn_addr = addr, expected_dyn_addr;
1877 struct i3c_ibi_setup ibireq = { };
1878 bool enable_ibi = false;
1884 newdev = i3c_master_alloc_i3c_dev(master, &info);
1886 return PTR_ERR(newdev);
1888 ret = i3c_master_attach_i3c_dev(master, newdev);
1892 ret = i3c_master_retrieve_dev_info(newdev);
1894 goto err_detach_dev;
1896 i3c_master_attach_boardinfo(newdev);
1898 olddev = i3c_master_search_i3c_dev_duplicate(newdev);
1900 newdev->dev = olddev->dev;
1902 newdev->dev->desc = newdev;
1905 * We need to restore the IBI state too, so let's save the
1906 * IBI information and try to restore them after olddev has
1907 * been detached+released and its IBI has been stopped and
1908 * the associated resources have been freed.
1910 mutex_lock(&olddev->ibi_lock);
1912 ibireq.handler = olddev->ibi->handler;
1913 ibireq.max_payload_len = olddev->ibi->max_payload_len;
1914 ibireq.num_slots = olddev->ibi->num_slots;
1916 if (olddev->ibi->enabled) {
1918 i3c_dev_disable_ibi_locked(olddev);
1921 i3c_dev_free_ibi_locked(olddev);
1923 mutex_unlock(&olddev->ibi_lock);
1925 old_dyn_addr = olddev->info.dyn_addr;
1927 i3c_master_detach_i3c_dev(olddev);
1928 i3c_master_free_i3c_dev(olddev);
1932 * Depending on our previous state, the expected dynamic address might
1934 * - if the device already had a dynamic address assigned, let's try to
1936 * - if the device did not have a dynamic address and the firmware
1937 * requested a specific address, pick this one
1938 * - in any other case, keep the address automatically assigned by the
1941 if (old_dyn_addr && old_dyn_addr != newdev->info.dyn_addr)
1942 expected_dyn_addr = old_dyn_addr;
1943 else if (newdev->boardinfo && newdev->boardinfo->init_dyn_addr)
1944 expected_dyn_addr = newdev->boardinfo->init_dyn_addr;
1946 expected_dyn_addr = newdev->info.dyn_addr;
1948 if (newdev->info.dyn_addr != expected_dyn_addr) {
1950 * Try to apply the expected dynamic address. If it fails, keep
1951 * the address assigned by the master.
1953 ret = i3c_master_setnewda_locked(master,
1954 newdev->info.dyn_addr,
1957 old_dyn_addr = newdev->info.dyn_addr;
1958 newdev->info.dyn_addr = expected_dyn_addr;
1959 i3c_master_reattach_i3c_dev(newdev, old_dyn_addr);
1961 dev_err(&master->dev,
1962 "Failed to assign reserved/old address to device %d%llx",
1963 master->bus.id, newdev->info.pid);
1968 * Now is time to try to restore the IBI setup. If we're lucky,
1969 * everything works as before, otherwise, all we can do is complain.
1970 * FIXME: maybe we should add callback to inform the driver that it
1971 * should request the IBI again instead of trying to hide that from
1974 if (ibireq.handler) {
1975 mutex_lock(&newdev->ibi_lock);
1976 ret = i3c_dev_request_ibi_locked(newdev, &ibireq);
1978 dev_err(&master->dev,
1979 "Failed to request IBI on device %d-%llx",
1980 master->bus.id, newdev->info.pid);
1981 } else if (enable_ibi) {
1982 ret = i3c_dev_enable_ibi_locked(newdev);
1984 dev_err(&master->dev,
1985 "Failed to re-enable IBI on device %d-%llx",
1986 master->bus.id, newdev->info.pid);
1988 mutex_unlock(&newdev->ibi_lock);
1994 if (newdev->dev && newdev->dev->desc)
1995 newdev->dev->desc = NULL;
1997 i3c_master_detach_i3c_dev(newdev);
2000 i3c_master_free_i3c_dev(newdev);
2004 EXPORT_SYMBOL_GPL(i3c_master_add_i3c_dev_locked);
2006 #define OF_I3C_REG1_IS_I2C_DEV BIT(31)
2009 of_i3c_master_add_i2c_boardinfo(struct i3c_master_controller *master,
2010 struct device_node *node, u32 *reg)
2012 struct i2c_dev_boardinfo *boardinfo;
2013 struct device *dev = &master->dev;
2016 boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL);
2020 ret = of_i2c_get_board_info(dev, node, &boardinfo->base);
2025 * The I3C Specification does not clearly say I2C devices with 10-bit
2026 * address are supported. These devices can't be passed properly through
2029 if (boardinfo->base.flags & I2C_CLIENT_TEN) {
2030 dev_err(dev, "I2C device with 10 bit address not supported.");
2034 /* LVR is encoded in reg[2]. */
2035 boardinfo->lvr = reg[2];
2037 list_add_tail(&boardinfo->node, &master->boardinfo.i2c);
2044 of_i3c_master_add_i3c_boardinfo(struct i3c_master_controller *master,
2045 struct device_node *node, u32 *reg)
2047 struct i3c_dev_boardinfo *boardinfo;
2048 struct device *dev = &master->dev;
2049 enum i3c_addr_slot_status addrstatus;
2050 u32 init_dyn_addr = 0;
2052 boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL);
2057 if (reg[0] > I3C_MAX_ADDR)
2060 addrstatus = i3c_bus_get_addr_slot_status(&master->bus,
2062 if (addrstatus != I3C_ADDR_SLOT_FREE)
2066 boardinfo->static_addr = reg[0];
2068 if (!of_property_read_u32(node, "assigned-address", &init_dyn_addr)) {
2069 if (init_dyn_addr > I3C_MAX_ADDR)
2072 addrstatus = i3c_bus_get_addr_slot_status(&master->bus,
2074 if (addrstatus != I3C_ADDR_SLOT_FREE)
2078 boardinfo->pid = ((u64)reg[1] << 32) | reg[2];
2080 if ((boardinfo->pid & GENMASK_ULL(63, 48)) ||
2081 I3C_PID_RND_LOWER_32BITS(boardinfo->pid))
2084 boardinfo->init_dyn_addr = init_dyn_addr;
2085 boardinfo->of_node = of_node_get(node);
2086 list_add_tail(&boardinfo->node, &master->boardinfo.i3c);
2091 static int of_i3c_master_add_dev(struct i3c_master_controller *master,
2092 struct device_node *node)
2097 if (!master || !node)
2100 ret = of_property_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg));
2105 * The manufacturer ID can't be 0. If reg[1] == 0 that means we're
2106 * dealing with an I2C device.
2109 ret = of_i3c_master_add_i2c_boardinfo(master, node, reg);
2111 ret = of_i3c_master_add_i3c_boardinfo(master, node, reg);
2116 static int of_populate_i3c_bus(struct i3c_master_controller *master)
2118 struct device *dev = &master->dev;
2119 struct device_node *i3cbus_np = dev->of_node;
2120 struct device_node *node;
2127 for_each_available_child_of_node(i3cbus_np, node) {
2128 ret = of_i3c_master_add_dev(master, node);
2136 * The user might want to limit I2C and I3C speed in case some devices
2137 * on the bus are not supporting typical rates, or if the bus topology
2138 * prevents it from using max possible rate.
2140 if (!of_property_read_u32(i3cbus_np, "i2c-scl-hz", &val))
2141 master->bus.scl_rate.i2c = val;
2143 if (!of_property_read_u32(i3cbus_np, "i3c-scl-hz", &val))
2144 master->bus.scl_rate.i3c = val;
2149 static int i3c_master_i2c_adapter_xfer(struct i2c_adapter *adap,
2150 struct i2c_msg *xfers, int nxfers)
2152 struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2153 struct i2c_dev_desc *dev;
2157 if (!xfers || !master || nxfers <= 0)
2160 if (!master->ops->i2c_xfers)
2163 /* Doing transfers to different devices is not supported. */
2164 addr = xfers[0].addr;
2165 for (i = 1; i < nxfers; i++) {
2166 if (addr != xfers[i].addr)
2170 i3c_bus_normaluse_lock(&master->bus);
2171 dev = i3c_master_find_i2c_dev_by_addr(master, addr);
2175 ret = master->ops->i2c_xfers(dev, xfers, nxfers);
2176 i3c_bus_normaluse_unlock(&master->bus);
2178 return ret ? ret : nxfers;
2181 static u32 i3c_master_i2c_funcs(struct i2c_adapter *adapter)
2183 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
2186 static u8 i3c_master_i2c_get_lvr(struct i2c_client *client)
2188 /* Fall back to no spike filters and FM bus mode. */
2189 u8 lvr = I3C_LVR_I2C_INDEX(2) | I3C_LVR_I2C_FM_MODE;
2191 if (client->dev.of_node) {
2194 if (!of_property_read_u32_array(client->dev.of_node, "reg",
2195 reg, ARRAY_SIZE(reg)))
2202 static int i3c_master_i2c_attach(struct i2c_adapter *adap, struct i2c_client *client)
2204 struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2205 enum i3c_addr_slot_status status;
2206 struct i2c_dev_desc *i2cdev;
2209 /* Already added by board info? */
2210 if (i3c_master_find_i2c_dev_by_addr(master, client->addr))
2213 status = i3c_bus_get_addr_slot_status(&master->bus, client->addr);
2214 if (status != I3C_ADDR_SLOT_FREE)
2217 i3c_bus_set_addr_slot_status(&master->bus, client->addr,
2218 I3C_ADDR_SLOT_I2C_DEV);
2220 i2cdev = i3c_master_alloc_i2c_dev(master, client->addr,
2221 i3c_master_i2c_get_lvr(client));
2222 if (IS_ERR(i2cdev)) {
2223 ret = PTR_ERR(i2cdev);
2224 goto out_clear_status;
2227 ret = i3c_master_attach_i2c_dev(master, i2cdev);
2234 i3c_master_free_i2c_dev(i2cdev);
2236 i3c_bus_set_addr_slot_status(&master->bus, client->addr,
2237 I3C_ADDR_SLOT_FREE);
2242 static int i3c_master_i2c_detach(struct i2c_adapter *adap, struct i2c_client *client)
2244 struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2245 struct i2c_dev_desc *dev;
2247 dev = i3c_master_find_i2c_dev_by_addr(master, client->addr);
2251 i3c_master_detach_i2c_dev(dev);
2252 i3c_bus_set_addr_slot_status(&master->bus, dev->addr,
2253 I3C_ADDR_SLOT_FREE);
2254 i3c_master_free_i2c_dev(dev);
2259 static const struct i2c_algorithm i3c_master_i2c_algo = {
2260 .master_xfer = i3c_master_i2c_adapter_xfer,
2261 .functionality = i3c_master_i2c_funcs,
2264 static int i3c_i2c_notifier_call(struct notifier_block *nb, unsigned long action,
2267 struct i2c_adapter *adap;
2268 struct i2c_client *client;
2269 struct device *dev = data;
2270 struct i3c_master_controller *master;
2273 if (dev->type != &i2c_client_type)
2276 client = to_i2c_client(dev);
2277 adap = client->adapter;
2279 if (adap->algo != &i3c_master_i2c_algo)
2282 master = i2c_adapter_to_i3c_master(adap);
2284 i3c_bus_maintenance_lock(&master->bus);
2286 case BUS_NOTIFY_ADD_DEVICE:
2287 ret = i3c_master_i2c_attach(adap, client);
2289 case BUS_NOTIFY_DEL_DEVICE:
2290 ret = i3c_master_i2c_detach(adap, client);
2293 i3c_bus_maintenance_unlock(&master->bus);
2298 static struct notifier_block i2cdev_notifier = {
2299 .notifier_call = i3c_i2c_notifier_call,
2302 static int i3c_master_i2c_adapter_init(struct i3c_master_controller *master)
2304 struct i2c_adapter *adap = i3c_master_to_i2c_adapter(master);
2305 struct i2c_dev_desc *i2cdev;
2306 struct i2c_dev_boardinfo *i2cboardinfo;
2309 adap->dev.parent = master->dev.parent;
2310 adap->owner = master->dev.parent->driver->owner;
2311 adap->algo = &i3c_master_i2c_algo;
2312 strncpy(adap->name, dev_name(master->dev.parent), sizeof(adap->name));
2314 /* FIXME: Should we allow i3c masters to override these values? */
2315 adap->timeout = 1000;
2318 ret = i2c_add_adapter(adap);
2323 * We silently ignore failures here. The bus should keep working
2324 * correctly even if one or more i2c devices are not registered.
2326 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) {
2327 i2cdev = i3c_master_find_i2c_dev_by_addr(master,
2328 i2cboardinfo->base.addr);
2329 if (WARN_ON(!i2cdev))
2331 i2cdev->dev = i2c_new_client_device(adap, &i2cboardinfo->base);
2337 static void i3c_master_i2c_adapter_cleanup(struct i3c_master_controller *master)
2339 struct i2c_dev_desc *i2cdev;
2341 i2c_del_adapter(&master->i2c);
2343 i3c_bus_for_each_i2cdev(&master->bus, i2cdev)
2347 static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master)
2349 struct i3c_dev_desc *i3cdev;
2351 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) {
2355 i3cdev->dev->desc = NULL;
2356 if (device_is_registered(&i3cdev->dev->dev))
2357 device_unregister(&i3cdev->dev->dev);
2359 put_device(&i3cdev->dev->dev);
2365 * i3c_master_queue_ibi() - Queue an IBI
2366 * @dev: the device this IBI is coming from
2367 * @slot: the IBI slot used to store the payload
2369 * Queue an IBI to the controller workqueue. The IBI handler attached to
2370 * the dev will be called from a workqueue context.
2372 void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot)
2374 atomic_inc(&dev->ibi->pending_ibis);
2375 queue_work(dev->common.master->wq, &slot->work);
2377 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi);
2379 static void i3c_master_handle_ibi(struct work_struct *work)
2381 struct i3c_ibi_slot *slot = container_of(work, struct i3c_ibi_slot,
2383 struct i3c_dev_desc *dev = slot->dev;
2384 struct i3c_master_controller *master = i3c_dev_get_master(dev);
2385 struct i3c_ibi_payload payload;
2387 payload.data = slot->data;
2388 payload.len = slot->len;
2391 dev->ibi->handler(dev->dev, &payload);
2393 master->ops->recycle_ibi_slot(dev, slot);
2394 if (atomic_dec_and_test(&dev->ibi->pending_ibis))
2395 complete(&dev->ibi->all_ibis_handled);
2398 static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
2399 struct i3c_ibi_slot *slot)
2402 INIT_WORK(&slot->work, i3c_master_handle_ibi);
2405 struct i3c_generic_ibi_slot {
2406 struct list_head node;
2407 struct i3c_ibi_slot base;
2410 struct i3c_generic_ibi_pool {
2412 unsigned int num_slots;
2413 struct i3c_generic_ibi_slot *slots;
2415 struct list_head free_slots;
2416 struct list_head pending;
2420 * i3c_generic_ibi_free_pool() - Free a generic IBI pool
2421 * @pool: the IBI pool to free
2423 * Free all IBI slots allated by a generic IBI pool.
2425 void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
2427 struct i3c_generic_ibi_slot *slot;
2428 unsigned int nslots = 0;
2430 while (!list_empty(&pool->free_slots)) {
2431 slot = list_first_entry(&pool->free_slots,
2432 struct i3c_generic_ibi_slot, node);
2433 list_del(&slot->node);
2438 * If the number of freed slots is not equal to the number of allocated
2439 * slots we have a leak somewhere.
2441 WARN_ON(nslots != pool->num_slots);
2443 kfree(pool->payload_buf);
2447 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool);
2450 * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool
2451 * @dev: the device this pool will be used for
2452 * @req: IBI setup request describing what the device driver expects
2454 * Create a generic IBI pool based on the information provided in @req.
2456 * Return: a valid IBI pool in case of success, an ERR_PTR() otherwise.
2458 struct i3c_generic_ibi_pool *
2459 i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
2460 const struct i3c_ibi_setup *req)
2462 struct i3c_generic_ibi_pool *pool;
2463 struct i3c_generic_ibi_slot *slot;
2467 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
2469 return ERR_PTR(-ENOMEM);
2471 spin_lock_init(&pool->lock);
2472 INIT_LIST_HEAD(&pool->free_slots);
2473 INIT_LIST_HEAD(&pool->pending);
2475 pool->slots = kcalloc(req->num_slots, sizeof(*slot), GFP_KERNEL);
2481 if (req->max_payload_len) {
2482 pool->payload_buf = kcalloc(req->num_slots,
2483 req->max_payload_len, GFP_KERNEL);
2484 if (!pool->payload_buf) {
2490 for (i = 0; i < req->num_slots; i++) {
2491 slot = &pool->slots[i];
2492 i3c_master_init_ibi_slot(dev, &slot->base);
2494 if (req->max_payload_len)
2495 slot->base.data = pool->payload_buf +
2496 (i * req->max_payload_len);
2498 list_add_tail(&slot->node, &pool->free_slots);
2505 i3c_generic_ibi_free_pool(pool);
2506 return ERR_PTR(ret);
2508 EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool);
2511 * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool
2512 * @pool: the pool to query an IBI slot on
2514 * Search for a free slot in a generic IBI pool.
2515 * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot()
2516 * when it's no longer needed.
2518 * Return: a pointer to a free slot, or NULL if there's no free slot available.
2520 struct i3c_ibi_slot *
2521 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool)
2523 struct i3c_generic_ibi_slot *slot;
2524 unsigned long flags;
2526 spin_lock_irqsave(&pool->lock, flags);
2527 slot = list_first_entry_or_null(&pool->free_slots,
2528 struct i3c_generic_ibi_slot, node);
2530 list_del(&slot->node);
2531 spin_unlock_irqrestore(&pool->lock, flags);
2533 return slot ? &slot->base : NULL;
2535 EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot);
2538 * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool
2539 * @pool: the pool to return the IBI slot to
2540 * @s: IBI slot to recycle
2542 * Add an IBI slot back to its generic IBI pool. Should be called from the
2543 * master driver struct_master_controller_ops->recycle_ibi() method.
2545 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
2546 struct i3c_ibi_slot *s)
2548 struct i3c_generic_ibi_slot *slot;
2549 unsigned long flags;
2554 slot = container_of(s, struct i3c_generic_ibi_slot, base);
2555 spin_lock_irqsave(&pool->lock, flags);
2556 list_add_tail(&slot->node, &pool->free_slots);
2557 spin_unlock_irqrestore(&pool->lock, flags);
2559 EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot);
2561 static int i3c_master_check_ops(const struct i3c_master_controller_ops *ops)
2563 if (!ops || !ops->bus_init || !ops->priv_xfers ||
2564 !ops->send_ccc_cmd || !ops->do_daa || !ops->i2c_xfers)
2567 if (ops->request_ibi &&
2568 (!ops->enable_ibi || !ops->disable_ibi || !ops->free_ibi ||
2569 !ops->recycle_ibi_slot))
2576 * i3c_master_register() - register an I3C master
2577 * @master: master used to send frames on the bus
2578 * @parent: the parent device (the one that provides this I3C master
2580 * @ops: the master controller operations
2581 * @secondary: true if you are registering a secondary master. Will return
2582 * -ENOTSUPP if set to true since secondary masters are not yet
2585 * This function takes care of everything for you:
2587 * - creates and initializes the I3C bus
2588 * - populates the bus with static I2C devs if @parent->of_node is not
2590 * - registers all I3C devices added by the controller during bus
2592 * - registers the I2C adapter and all I2C devices
2594 * Return: 0 in case of success, a negative error code otherwise.
2596 int i3c_master_register(struct i3c_master_controller *master,
2597 struct device *parent,
2598 const struct i3c_master_controller_ops *ops,
2601 unsigned long i2c_scl_rate = I3C_BUS_I2C_FM_PLUS_SCL_RATE;
2602 struct i3c_bus *i3cbus = i3c_master_get_bus(master);
2603 enum i3c_bus_mode mode = I3C_BUS_MODE_PURE;
2604 struct i2c_dev_boardinfo *i2cbi;
2607 /* We do not support secondary masters yet. */
2611 ret = i3c_master_check_ops(ops);
2615 master->dev.parent = parent;
2616 master->dev.of_node = of_node_get(parent->of_node);
2617 master->dev.bus = &i3c_bus_type;
2618 master->dev.type = &i3c_masterdev_type;
2619 master->dev.release = i3c_masterdev_release;
2621 master->secondary = secondary;
2622 INIT_LIST_HEAD(&master->boardinfo.i2c);
2623 INIT_LIST_HEAD(&master->boardinfo.i3c);
2625 ret = i3c_bus_init(i3cbus, master->dev.of_node);
2629 device_initialize(&master->dev);
2630 dev_set_name(&master->dev, "i3c-%d", i3cbus->id);
2632 ret = of_populate_i3c_bus(master);
2636 list_for_each_entry(i2cbi, &master->boardinfo.i2c, node) {
2637 switch (i2cbi->lvr & I3C_LVR_I2C_INDEX_MASK) {
2638 case I3C_LVR_I2C_INDEX(0):
2639 if (mode < I3C_BUS_MODE_MIXED_FAST)
2640 mode = I3C_BUS_MODE_MIXED_FAST;
2642 case I3C_LVR_I2C_INDEX(1):
2643 if (mode < I3C_BUS_MODE_MIXED_LIMITED)
2644 mode = I3C_BUS_MODE_MIXED_LIMITED;
2646 case I3C_LVR_I2C_INDEX(2):
2647 if (mode < I3C_BUS_MODE_MIXED_SLOW)
2648 mode = I3C_BUS_MODE_MIXED_SLOW;
2655 if (i2cbi->lvr & I3C_LVR_I2C_FM_MODE)
2656 i2c_scl_rate = I3C_BUS_I2C_FM_SCL_RATE;
2659 ret = i3c_bus_set_mode(i3cbus, mode, i2c_scl_rate);
2663 master->wq = alloc_workqueue("%s", 0, 0, dev_name(parent));
2669 ret = i3c_master_bus_init(master);
2673 ret = device_add(&master->dev);
2675 goto err_cleanup_bus;
2678 * Expose our I3C bus as an I2C adapter so that I2C devices are exposed
2679 * through the I2C subsystem.
2681 ret = i3c_master_i2c_adapter_init(master);
2686 * We're done initializing the bus and the controller, we can now
2687 * register I3C devices discovered during the initial DAA.
2689 master->init_done = true;
2690 i3c_bus_normaluse_lock(&master->bus);
2691 i3c_master_register_new_i3c_devs(master);
2692 i3c_bus_normaluse_unlock(&master->bus);
2697 device_del(&master->dev);
2700 i3c_master_bus_cleanup(master);
2703 put_device(&master->dev);
2707 EXPORT_SYMBOL_GPL(i3c_master_register);
2710 * i3c_master_unregister() - unregister an I3C master
2711 * @master: master used to send frames on the bus
2713 * Basically undo everything done in i3c_master_register().
2715 void i3c_master_unregister(struct i3c_master_controller *master)
2717 i3c_master_i2c_adapter_cleanup(master);
2718 i3c_master_unregister_i3c_devs(master);
2719 i3c_master_bus_cleanup(master);
2720 device_unregister(&master->dev);
2722 EXPORT_SYMBOL_GPL(i3c_master_unregister);
2724 int i3c_dev_setdasa_locked(struct i3c_dev_desc *dev)
2726 struct i3c_master_controller *master;
2731 master = i3c_dev_get_master(dev);
2735 if (!dev->boardinfo || !dev->boardinfo->init_dyn_addr ||
2736 !dev->boardinfo->static_addr)
2739 return i3c_master_setdasa_locked(master, dev->info.static_addr,
2740 dev->boardinfo->init_dyn_addr);
2743 int i3c_dev_do_priv_xfers_locked(struct i3c_dev_desc *dev,
2744 struct i3c_priv_xfer *xfers,
2747 struct i3c_master_controller *master;
2752 master = i3c_dev_get_master(dev);
2753 if (!master || !xfers)
2756 if (!master->ops->priv_xfers)
2759 return master->ops->priv_xfers(dev, xfers, nxfers);
2762 int i3c_dev_disable_ibi_locked(struct i3c_dev_desc *dev)
2764 struct i3c_master_controller *master;
2770 master = i3c_dev_get_master(dev);
2771 ret = master->ops->disable_ibi(dev);
2775 reinit_completion(&dev->ibi->all_ibis_handled);
2776 if (atomic_read(&dev->ibi->pending_ibis))
2777 wait_for_completion(&dev->ibi->all_ibis_handled);
2779 dev->ibi->enabled = false;
2784 int i3c_dev_enable_ibi_locked(struct i3c_dev_desc *dev)
2786 struct i3c_master_controller *master = i3c_dev_get_master(dev);
2792 ret = master->ops->enable_ibi(dev);
2794 dev->ibi->enabled = true;
2799 int i3c_dev_request_ibi_locked(struct i3c_dev_desc *dev,
2800 const struct i3c_ibi_setup *req)
2802 struct i3c_master_controller *master = i3c_dev_get_master(dev);
2803 struct i3c_device_ibi_info *ibi;
2806 if (!master->ops->request_ibi)
2812 ibi = kzalloc(sizeof(*ibi), GFP_KERNEL);
2816 atomic_set(&ibi->pending_ibis, 0);
2817 init_completion(&ibi->all_ibis_handled);
2818 ibi->handler = req->handler;
2819 ibi->max_payload_len = req->max_payload_len;
2820 ibi->num_slots = req->num_slots;
2823 ret = master->ops->request_ibi(dev, req);
2832 void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev)
2834 struct i3c_master_controller *master = i3c_dev_get_master(dev);
2839 if (WARN_ON(dev->ibi->enabled))
2840 WARN_ON(i3c_dev_disable_ibi_locked(dev));
2842 master->ops->free_ibi(dev);
2847 static int __init i3c_init(void)
2851 res = of_alias_get_highest_id("i3c");
2853 mutex_lock(&i3c_core_lock);
2854 __i3c_first_dynamic_bus_num = res + 1;
2855 mutex_unlock(&i3c_core_lock);
2858 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
2862 res = bus_register(&i3c_bus_type);
2864 goto out_unreg_notifier;
2869 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
2873 subsys_initcall(i3c_init);
2875 static void __exit i3c_exit(void)
2877 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
2878 idr_destroy(&i3c_bus_idr);
2879 bus_unregister(&i3c_bus_type);
2881 module_exit(i3c_exit);
2883 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
2884 MODULE_DESCRIPTION("I3C core");
2885 MODULE_LICENSE("GPL v2");