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
);
26 * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation
27 * @bus: I3C bus to take the lock on
29 * This function takes the bus lock so that no other operations can occur on
30 * the bus. This is needed for all kind of bus maintenance operation, like
31 * - enabling/disabling slave events
33 * - changing the dynamic address of a device
34 * - relinquishing mastership
37 * The reason for this kind of locking is that we don't want drivers and core
38 * logic to rely on I3C device information that could be changed behind their
41 static void i3c_bus_maintenance_lock(struct i3c_bus
*bus
)
43 down_write(&bus
->lock
);
47 * i3c_bus_maintenance_unlock - Release the bus lock after a maintenance
49 * @bus: I3C bus to release the lock on
51 * Should be called when the bus maintenance operation is done. See
52 * i3c_bus_maintenance_lock() for more details on what these maintenance
55 static void i3c_bus_maintenance_unlock(struct i3c_bus
*bus
)
61 * i3c_bus_normaluse_lock - Lock the bus for a normal operation
62 * @bus: I3C bus to take the lock on
64 * This function takes the bus lock for any operation that is not a maintenance
65 * operation (see i3c_bus_maintenance_lock() for a non-exhaustive list of
66 * maintenance operations). Basically all communications with I3C devices are
67 * normal operations (HDR, SDR transfers or CCC commands that do not change bus
68 * state or I3C dynamic address).
70 * Note that this lock is not guaranteeing serialization of normal operations.
71 * In other words, transfer requests passed to the I3C master can be submitted
72 * in parallel and I3C master drivers have to use their own locking to make
73 * sure two different communications are not inter-mixed, or access to the
74 * output/input queue is not done while the engine is busy.
76 void i3c_bus_normaluse_lock(struct i3c_bus
*bus
)
78 down_read(&bus
->lock
);
82 * i3c_bus_normaluse_unlock - Release the bus lock after a normal operation
83 * @bus: I3C bus to release the lock on
85 * Should be called when a normal operation is done. See
86 * i3c_bus_normaluse_lock() for more details on what these normal operations
89 void i3c_bus_normaluse_unlock(struct i3c_bus
*bus
)
94 static struct i3c_master_controller
*
95 i3c_bus_to_i3c_master(struct i3c_bus
*i3cbus
)
97 return container_of(i3cbus
, struct i3c_master_controller
, bus
);
100 static struct i3c_master_controller
*dev_to_i3cmaster(struct device
*dev
)
102 return container_of(dev
, struct i3c_master_controller
, dev
);
105 static const struct device_type i3c_device_type
;
107 static struct i3c_bus
*dev_to_i3cbus(struct device
*dev
)
109 struct i3c_master_controller
*master
;
111 if (dev
->type
== &i3c_device_type
)
112 return dev_to_i3cdev(dev
)->bus
;
114 master
= dev_to_i3cmaster(dev
);
119 static struct i3c_dev_desc
*dev_to_i3cdesc(struct device
*dev
)
121 struct i3c_master_controller
*master
;
123 if (dev
->type
== &i3c_device_type
)
124 return dev_to_i3cdev(dev
)->desc
;
126 master
= dev_to_i3cmaster(dev
);
131 static ssize_t
bcr_show(struct device
*dev
,
132 struct device_attribute
*da
,
135 struct i3c_bus
*bus
= dev_to_i3cbus(dev
);
136 struct i3c_dev_desc
*desc
;
139 i3c_bus_normaluse_lock(bus
);
140 desc
= dev_to_i3cdesc(dev
);
141 ret
= sprintf(buf
, "%x\n", desc
->info
.bcr
);
142 i3c_bus_normaluse_unlock(bus
);
146 static DEVICE_ATTR_RO(bcr
);
148 static ssize_t
dcr_show(struct device
*dev
,
149 struct device_attribute
*da
,
152 struct i3c_bus
*bus
= dev_to_i3cbus(dev
);
153 struct i3c_dev_desc
*desc
;
156 i3c_bus_normaluse_lock(bus
);
157 desc
= dev_to_i3cdesc(dev
);
158 ret
= sprintf(buf
, "%x\n", desc
->info
.dcr
);
159 i3c_bus_normaluse_unlock(bus
);
163 static DEVICE_ATTR_RO(dcr
);
165 static ssize_t
pid_show(struct device
*dev
,
166 struct device_attribute
*da
,
169 struct i3c_bus
*bus
= dev_to_i3cbus(dev
);
170 struct i3c_dev_desc
*desc
;
173 i3c_bus_normaluse_lock(bus
);
174 desc
= dev_to_i3cdesc(dev
);
175 ret
= sprintf(buf
, "%llx\n", desc
->info
.pid
);
176 i3c_bus_normaluse_unlock(bus
);
180 static DEVICE_ATTR_RO(pid
);
182 static ssize_t
dynamic_address_show(struct device
*dev
,
183 struct device_attribute
*da
,
186 struct i3c_bus
*bus
= dev_to_i3cbus(dev
);
187 struct i3c_dev_desc
*desc
;
190 i3c_bus_normaluse_lock(bus
);
191 desc
= dev_to_i3cdesc(dev
);
192 ret
= sprintf(buf
, "%02x\n", desc
->info
.dyn_addr
);
193 i3c_bus_normaluse_unlock(bus
);
197 static DEVICE_ATTR_RO(dynamic_address
);
199 static const char * const hdrcap_strings
[] = {
200 "hdr-ddr", "hdr-tsp", "hdr-tsl",
203 static ssize_t
hdrcap_show(struct device
*dev
,
204 struct device_attribute
*da
,
207 struct i3c_bus
*bus
= dev_to_i3cbus(dev
);
208 struct i3c_dev_desc
*desc
;
209 ssize_t offset
= 0, ret
;
213 i3c_bus_normaluse_lock(bus
);
214 desc
= dev_to_i3cdesc(dev
);
215 caps
= desc
->info
.hdr_cap
;
216 for_each_set_bit(mode
, &caps
, 8) {
217 if (mode
>= ARRAY_SIZE(hdrcap_strings
))
220 if (!hdrcap_strings
[mode
])
223 ret
= sprintf(buf
+ offset
, offset
? " %s" : "%s",
224 hdrcap_strings
[mode
]);
231 ret
= sprintf(buf
+ offset
, "\n");
238 i3c_bus_normaluse_unlock(bus
);
242 static DEVICE_ATTR_RO(hdrcap
);
244 static ssize_t
modalias_show(struct device
*dev
,
245 struct device_attribute
*da
, char *buf
)
247 struct i3c_device
*i3c
= dev_to_i3cdev(dev
);
248 struct i3c_device_info devinfo
;
249 u16 manuf
, part
, ext
;
251 i3c_device_get_info(i3c
, &devinfo
);
252 manuf
= I3C_PID_MANUF_ID(devinfo
.pid
);
253 part
= I3C_PID_PART_ID(devinfo
.pid
);
254 ext
= I3C_PID_EXTRA_INFO(devinfo
.pid
);
256 if (I3C_PID_RND_LOWER_32BITS(devinfo
.pid
))
257 return sprintf(buf
, "i3c:dcr%02Xmanuf%04X", devinfo
.dcr
,
260 return sprintf(buf
, "i3c:dcr%02Xmanuf%04Xpart%04Xext%04X",
261 devinfo
.dcr
, manuf
, part
, ext
);
263 static DEVICE_ATTR_RO(modalias
);
265 static struct attribute
*i3c_device_attrs
[] = {
269 &dev_attr_dynamic_address
.attr
,
270 &dev_attr_hdrcap
.attr
,
271 &dev_attr_modalias
.attr
,
274 ATTRIBUTE_GROUPS(i3c_device
);
276 static int i3c_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
278 struct i3c_device
*i3cdev
= dev_to_i3cdev(dev
);
279 struct i3c_device_info devinfo
;
280 u16 manuf
, part
, ext
;
282 i3c_device_get_info(i3cdev
, &devinfo
);
283 manuf
= I3C_PID_MANUF_ID(devinfo
.pid
);
284 part
= I3C_PID_PART_ID(devinfo
.pid
);
285 ext
= I3C_PID_EXTRA_INFO(devinfo
.pid
);
287 if (I3C_PID_RND_LOWER_32BITS(devinfo
.pid
))
288 return add_uevent_var(env
, "MODALIAS=i3c:dcr%02Xmanuf%04X",
291 return add_uevent_var(env
,
292 "MODALIAS=i3c:dcr%02Xmanuf%04Xpart%04Xext%04X",
293 devinfo
.dcr
, manuf
, part
, ext
);
296 static const struct device_type i3c_device_type
= {
297 .groups
= i3c_device_groups
,
298 .uevent
= i3c_device_uevent
,
301 static int i3c_device_match(struct device
*dev
, struct device_driver
*drv
)
303 struct i3c_device
*i3cdev
;
304 struct i3c_driver
*i3cdrv
;
306 if (dev
->type
!= &i3c_device_type
)
309 i3cdev
= dev_to_i3cdev(dev
);
310 i3cdrv
= drv_to_i3cdrv(drv
);
311 if (i3c_device_match_id(i3cdev
, i3cdrv
->id_table
))
317 static int i3c_device_probe(struct device
*dev
)
319 struct i3c_device
*i3cdev
= dev_to_i3cdev(dev
);
320 struct i3c_driver
*driver
= drv_to_i3cdrv(dev
->driver
);
322 return driver
->probe(i3cdev
);
325 static int i3c_device_remove(struct device
*dev
)
327 struct i3c_device
*i3cdev
= dev_to_i3cdev(dev
);
328 struct i3c_driver
*driver
= drv_to_i3cdrv(dev
->driver
);
331 ret
= driver
->remove(i3cdev
);
335 i3c_device_free_ibi(i3cdev
);
340 struct bus_type i3c_bus_type
= {
342 .match
= i3c_device_match
,
343 .probe
= i3c_device_probe
,
344 .remove
= i3c_device_remove
,
347 static enum i3c_addr_slot_status
348 i3c_bus_get_addr_slot_status(struct i3c_bus
*bus
, u16 addr
)
350 int status
, bitpos
= addr
* 2;
352 if (addr
> I2C_MAX_ADDR
)
353 return I3C_ADDR_SLOT_RSVD
;
355 status
= bus
->addrslots
[bitpos
/ BITS_PER_LONG
];
356 status
>>= bitpos
% BITS_PER_LONG
;
358 return status
& I3C_ADDR_SLOT_STATUS_MASK
;
361 static void i3c_bus_set_addr_slot_status(struct i3c_bus
*bus
, u16 addr
,
362 enum i3c_addr_slot_status status
)
364 int bitpos
= addr
* 2;
367 if (addr
> I2C_MAX_ADDR
)
370 ptr
= bus
->addrslots
+ (bitpos
/ BITS_PER_LONG
);
371 *ptr
&= ~((unsigned long)I3C_ADDR_SLOT_STATUS_MASK
<<
372 (bitpos
% BITS_PER_LONG
));
373 *ptr
|= (unsigned long)status
<< (bitpos
% BITS_PER_LONG
);
376 static bool i3c_bus_dev_addr_is_avail(struct i3c_bus
*bus
, u8 addr
)
378 enum i3c_addr_slot_status status
;
380 status
= i3c_bus_get_addr_slot_status(bus
, addr
);
382 return status
== I3C_ADDR_SLOT_FREE
;
385 static int i3c_bus_get_free_addr(struct i3c_bus
*bus
, u8 start_addr
)
387 enum i3c_addr_slot_status status
;
390 for (addr
= start_addr
; addr
< I3C_MAX_ADDR
; addr
++) {
391 status
= i3c_bus_get_addr_slot_status(bus
, addr
);
392 if (status
== I3C_ADDR_SLOT_FREE
)
399 static void i3c_bus_init_addrslots(struct i3c_bus
*bus
)
403 /* Addresses 0 to 7 are reserved. */
404 for (i
= 0; i
< 8; i
++)
405 i3c_bus_set_addr_slot_status(bus
, i
, I3C_ADDR_SLOT_RSVD
);
408 * Reserve broadcast address and all addresses that might collide
409 * with the broadcast address when facing a single bit error.
411 i3c_bus_set_addr_slot_status(bus
, I3C_BROADCAST_ADDR
,
413 for (i
= 0; i
< 7; i
++)
414 i3c_bus_set_addr_slot_status(bus
, I3C_BROADCAST_ADDR
^ BIT(i
),
418 static void i3c_bus_cleanup(struct i3c_bus
*i3cbus
)
420 mutex_lock(&i3c_core_lock
);
421 idr_remove(&i3c_bus_idr
, i3cbus
->id
);
422 mutex_unlock(&i3c_core_lock
);
425 static int i3c_bus_init(struct i3c_bus
*i3cbus
)
429 init_rwsem(&i3cbus
->lock
);
430 INIT_LIST_HEAD(&i3cbus
->devs
.i2c
);
431 INIT_LIST_HEAD(&i3cbus
->devs
.i3c
);
432 i3c_bus_init_addrslots(i3cbus
);
433 i3cbus
->mode
= I3C_BUS_MODE_PURE
;
435 mutex_lock(&i3c_core_lock
);
436 ret
= idr_alloc(&i3c_bus_idr
, i3cbus
, 0, 0, GFP_KERNEL
);
437 mutex_unlock(&i3c_core_lock
);
447 static const char * const i3c_bus_mode_strings
[] = {
448 [I3C_BUS_MODE_PURE
] = "pure",
449 [I3C_BUS_MODE_MIXED_FAST
] = "mixed-fast",
450 [I3C_BUS_MODE_MIXED_LIMITED
] = "mixed-limited",
451 [I3C_BUS_MODE_MIXED_SLOW
] = "mixed-slow",
454 static ssize_t
mode_show(struct device
*dev
,
455 struct device_attribute
*da
,
458 struct i3c_bus
*i3cbus
= dev_to_i3cbus(dev
);
461 i3c_bus_normaluse_lock(i3cbus
);
462 if (i3cbus
->mode
< 0 ||
463 i3cbus
->mode
>= ARRAY_SIZE(i3c_bus_mode_strings
) ||
464 !i3c_bus_mode_strings
[i3cbus
->mode
])
465 ret
= sprintf(buf
, "unknown\n");
467 ret
= sprintf(buf
, "%s\n", i3c_bus_mode_strings
[i3cbus
->mode
]);
468 i3c_bus_normaluse_unlock(i3cbus
);
472 static DEVICE_ATTR_RO(mode
);
474 static ssize_t
current_master_show(struct device
*dev
,
475 struct device_attribute
*da
,
478 struct i3c_bus
*i3cbus
= dev_to_i3cbus(dev
);
481 i3c_bus_normaluse_lock(i3cbus
);
482 ret
= sprintf(buf
, "%d-%llx\n", i3cbus
->id
,
483 i3cbus
->cur_master
->info
.pid
);
484 i3c_bus_normaluse_unlock(i3cbus
);
488 static DEVICE_ATTR_RO(current_master
);
490 static ssize_t
i3c_scl_frequency_show(struct device
*dev
,
491 struct device_attribute
*da
,
494 struct i3c_bus
*i3cbus
= dev_to_i3cbus(dev
);
497 i3c_bus_normaluse_lock(i3cbus
);
498 ret
= sprintf(buf
, "%ld\n", i3cbus
->scl_rate
.i3c
);
499 i3c_bus_normaluse_unlock(i3cbus
);
503 static DEVICE_ATTR_RO(i3c_scl_frequency
);
505 static ssize_t
i2c_scl_frequency_show(struct device
*dev
,
506 struct device_attribute
*da
,
509 struct i3c_bus
*i3cbus
= dev_to_i3cbus(dev
);
512 i3c_bus_normaluse_lock(i3cbus
);
513 ret
= sprintf(buf
, "%ld\n", i3cbus
->scl_rate
.i2c
);
514 i3c_bus_normaluse_unlock(i3cbus
);
518 static DEVICE_ATTR_RO(i2c_scl_frequency
);
520 static struct attribute
*i3c_masterdev_attrs
[] = {
522 &dev_attr_current_master
.attr
,
523 &dev_attr_i3c_scl_frequency
.attr
,
524 &dev_attr_i2c_scl_frequency
.attr
,
528 &dev_attr_dynamic_address
.attr
,
529 &dev_attr_hdrcap
.attr
,
532 ATTRIBUTE_GROUPS(i3c_masterdev
);
534 static void i3c_masterdev_release(struct device
*dev
)
536 struct i3c_master_controller
*master
= dev_to_i3cmaster(dev
);
537 struct i3c_bus
*bus
= dev_to_i3cbus(dev
);
540 destroy_workqueue(master
->wq
);
542 WARN_ON(!list_empty(&bus
->devs
.i2c
) || !list_empty(&bus
->devs
.i3c
));
543 i3c_bus_cleanup(bus
);
545 of_node_put(dev
->of_node
);
548 static const struct device_type i3c_masterdev_type
= {
549 .groups
= i3c_masterdev_groups
,
552 static int i3c_bus_set_mode(struct i3c_bus
*i3cbus
, enum i3c_bus_mode mode
,
553 unsigned long max_i2c_scl_rate
)
555 struct i3c_master_controller
*master
= i3c_bus_to_i3c_master(i3cbus
);
559 switch (i3cbus
->mode
) {
560 case I3C_BUS_MODE_PURE
:
561 if (!i3cbus
->scl_rate
.i3c
)
562 i3cbus
->scl_rate
.i3c
= I3C_BUS_TYP_I3C_SCL_RATE
;
564 case I3C_BUS_MODE_MIXED_FAST
:
565 case I3C_BUS_MODE_MIXED_LIMITED
:
566 if (!i3cbus
->scl_rate
.i3c
)
567 i3cbus
->scl_rate
.i3c
= I3C_BUS_TYP_I3C_SCL_RATE
;
568 if (!i3cbus
->scl_rate
.i2c
)
569 i3cbus
->scl_rate
.i2c
= max_i2c_scl_rate
;
571 case I3C_BUS_MODE_MIXED_SLOW
:
572 if (!i3cbus
->scl_rate
.i2c
)
573 i3cbus
->scl_rate
.i2c
= max_i2c_scl_rate
;
574 if (!i3cbus
->scl_rate
.i3c
||
575 i3cbus
->scl_rate
.i3c
> i3cbus
->scl_rate
.i2c
)
576 i3cbus
->scl_rate
.i3c
= i3cbus
->scl_rate
.i2c
;
582 dev_dbg(&master
->dev
, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n",
583 i3cbus
->scl_rate
.i2c
, i3cbus
->scl_rate
.i3c
);
586 * I3C/I2C frequency may have been overridden, check that user-provided
587 * values are not exceeding max possible frequency.
589 if (i3cbus
->scl_rate
.i3c
> I3C_BUS_MAX_I3C_SCL_RATE
||
590 i3cbus
->scl_rate
.i2c
> I3C_BUS_I2C_FM_PLUS_SCL_RATE
)
596 static struct i3c_master_controller
*
597 i2c_adapter_to_i3c_master(struct i2c_adapter
*adap
)
599 return container_of(adap
, struct i3c_master_controller
, i2c
);
602 static struct i2c_adapter
*
603 i3c_master_to_i2c_adapter(struct i3c_master_controller
*master
)
608 static void i3c_master_free_i2c_dev(struct i2c_dev_desc
*dev
)
613 static struct i2c_dev_desc
*
614 i3c_master_alloc_i2c_dev(struct i3c_master_controller
*master
,
615 const struct i2c_dev_boardinfo
*boardinfo
)
617 struct i2c_dev_desc
*dev
;
619 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
621 return ERR_PTR(-ENOMEM
);
623 dev
->common
.master
= master
;
624 dev
->boardinfo
= boardinfo
;
625 dev
->addr
= boardinfo
->base
.addr
;
626 dev
->lvr
= boardinfo
->lvr
;
631 static void *i3c_ccc_cmd_dest_init(struct i3c_ccc_cmd_dest
*dest
, u8 addr
,
635 dest
->payload
.len
= payloadlen
;
637 dest
->payload
.data
= kzalloc(payloadlen
, GFP_KERNEL
);
639 dest
->payload
.data
= NULL
;
641 return dest
->payload
.data
;
644 static void i3c_ccc_cmd_dest_cleanup(struct i3c_ccc_cmd_dest
*dest
)
646 kfree(dest
->payload
.data
);
649 static void i3c_ccc_cmd_init(struct i3c_ccc_cmd
*cmd
, bool rnw
, u8 id
,
650 struct i3c_ccc_cmd_dest
*dests
,
653 cmd
->rnw
= rnw
? 1 : 0;
656 cmd
->ndests
= ndests
;
657 cmd
->err
= I3C_ERROR_UNKNOWN
;
660 static int i3c_master_send_ccc_cmd_locked(struct i3c_master_controller
*master
,
661 struct i3c_ccc_cmd
*cmd
)
668 if (WARN_ON(master
->init_done
&&
669 !rwsem_is_locked(&master
->bus
.lock
)))
672 if (!master
->ops
->send_ccc_cmd
)
675 if ((cmd
->id
& I3C_CCC_DIRECT
) && (!cmd
->dests
|| !cmd
->ndests
))
678 if (master
->ops
->supports_ccc_cmd
&&
679 !master
->ops
->supports_ccc_cmd(master
, cmd
))
682 ret
= master
->ops
->send_ccc_cmd(master
, cmd
);
684 if (cmd
->err
!= I3C_ERROR_UNKNOWN
)
693 static struct i2c_dev_desc
*
694 i3c_master_find_i2c_dev_by_addr(const struct i3c_master_controller
*master
,
697 struct i2c_dev_desc
*dev
;
699 i3c_bus_for_each_i2cdev(&master
->bus
, dev
) {
700 if (dev
->boardinfo
->base
.addr
== addr
)
708 * i3c_master_get_free_addr() - get a free address on the bus
709 * @master: I3C master object
710 * @start_addr: where to start searching
712 * This function must be called with the bus lock held in write mode.
714 * Return: the first free address starting at @start_addr (included) or -ENOMEM
715 * if there's no more address available.
717 int i3c_master_get_free_addr(struct i3c_master_controller
*master
,
720 return i3c_bus_get_free_addr(&master
->bus
, start_addr
);
722 EXPORT_SYMBOL_GPL(i3c_master_get_free_addr
);
724 static void i3c_device_release(struct device
*dev
)
726 struct i3c_device
*i3cdev
= dev_to_i3cdev(dev
);
728 WARN_ON(i3cdev
->desc
);
730 of_node_put(i3cdev
->dev
.of_node
);
734 static void i3c_master_free_i3c_dev(struct i3c_dev_desc
*dev
)
739 static struct i3c_dev_desc
*
740 i3c_master_alloc_i3c_dev(struct i3c_master_controller
*master
,
741 const struct i3c_device_info
*info
)
743 struct i3c_dev_desc
*dev
;
745 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
747 return ERR_PTR(-ENOMEM
);
749 dev
->common
.master
= master
;
751 mutex_init(&dev
->ibi_lock
);
756 static int i3c_master_rstdaa_locked(struct i3c_master_controller
*master
,
759 enum i3c_addr_slot_status addrstat
;
760 struct i3c_ccc_cmd_dest dest
;
761 struct i3c_ccc_cmd cmd
;
767 addrstat
= i3c_bus_get_addr_slot_status(&master
->bus
, addr
);
768 if (addr
!= I3C_BROADCAST_ADDR
&& addrstat
!= I3C_ADDR_SLOT_I3C_DEV
)
771 i3c_ccc_cmd_dest_init(&dest
, addr
, 0);
772 i3c_ccc_cmd_init(&cmd
, false,
773 I3C_CCC_RSTDAA(addr
== I3C_BROADCAST_ADDR
),
775 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
776 i3c_ccc_cmd_dest_cleanup(&dest
);
782 * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment)
784 * @master: master used to send frames on the bus
786 * Send a ENTDAA CCC command to start a DAA procedure.
788 * Note that this function only sends the ENTDAA CCC command, all the logic
789 * behind dynamic address assignment has to be handled in the I3C master
792 * This function must be called with the bus lock held in write mode.
794 * Return: 0 in case of success, a positive I3C error code if the error is
795 * one of the official Mx error codes, and a negative error code otherwise.
797 int i3c_master_entdaa_locked(struct i3c_master_controller
*master
)
799 struct i3c_ccc_cmd_dest dest
;
800 struct i3c_ccc_cmd cmd
;
803 i3c_ccc_cmd_dest_init(&dest
, I3C_BROADCAST_ADDR
, 0);
804 i3c_ccc_cmd_init(&cmd
, false, I3C_CCC_ENTDAA
, &dest
, 1);
805 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
806 i3c_ccc_cmd_dest_cleanup(&dest
);
810 EXPORT_SYMBOL_GPL(i3c_master_entdaa_locked
);
812 static int i3c_master_enec_disec_locked(struct i3c_master_controller
*master
,
813 u8 addr
, bool enable
, u8 evts
)
815 struct i3c_ccc_events
*events
;
816 struct i3c_ccc_cmd_dest dest
;
817 struct i3c_ccc_cmd cmd
;
820 events
= i3c_ccc_cmd_dest_init(&dest
, addr
, sizeof(*events
));
824 events
->events
= evts
;
825 i3c_ccc_cmd_init(&cmd
, false,
827 I3C_CCC_ENEC(addr
== I3C_BROADCAST_ADDR
) :
828 I3C_CCC_DISEC(addr
== I3C_BROADCAST_ADDR
),
830 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
831 i3c_ccc_cmd_dest_cleanup(&dest
);
837 * i3c_master_disec_locked() - send a DISEC CCC command
838 * @master: master used to send frames on the bus
839 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
840 * @evts: events to disable
842 * Send a DISEC CCC command to disable some or all events coming from a
843 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
845 * This function must be called with the bus lock held in write mode.
847 * Return: 0 in case of success, a positive I3C error code if the error is
848 * one of the official Mx error codes, and a negative error code otherwise.
850 int i3c_master_disec_locked(struct i3c_master_controller
*master
, u8 addr
,
853 return i3c_master_enec_disec_locked(master
, addr
, false, evts
);
855 EXPORT_SYMBOL_GPL(i3c_master_disec_locked
);
858 * i3c_master_enec_locked() - send an ENEC CCC command
859 * @master: master used to send frames on the bus
860 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
861 * @evts: events to disable
863 * Sends an ENEC CCC command to enable some or all events coming from a
864 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
866 * This function must be called with the bus lock held in write mode.
868 * Return: 0 in case of success, a positive I3C error code if the error is
869 * one of the official Mx error codes, and a negative error code otherwise.
871 int i3c_master_enec_locked(struct i3c_master_controller
*master
, u8 addr
,
874 return i3c_master_enec_disec_locked(master
, addr
, true, evts
);
876 EXPORT_SYMBOL_GPL(i3c_master_enec_locked
);
879 * i3c_master_defslvs_locked() - send a DEFSLVS CCC command
880 * @master: master used to send frames on the bus
882 * Send a DEFSLVS CCC command containing all the devices known to the @master.
883 * This is useful when you have secondary masters on the bus to propagate
884 * device information.
886 * This should be called after all I3C devices have been discovered (in other
887 * words, after the DAA procedure has finished) and instantiated in
888 * &i3c_master_controller_ops->bus_init().
889 * It should also be called if a master ACKed an Hot-Join request and assigned
890 * a dynamic address to the device joining the bus.
892 * This function must be called with the bus lock held in write mode.
894 * Return: 0 in case of success, a positive I3C error code if the error is
895 * one of the official Mx error codes, and a negative error code otherwise.
897 int i3c_master_defslvs_locked(struct i3c_master_controller
*master
)
899 struct i3c_ccc_defslvs
*defslvs
;
900 struct i3c_ccc_dev_desc
*desc
;
901 struct i3c_ccc_cmd_dest dest
;
902 struct i3c_dev_desc
*i3cdev
;
903 struct i2c_dev_desc
*i2cdev
;
904 struct i3c_ccc_cmd cmd
;
912 bus
= i3c_master_get_bus(master
);
913 i3c_bus_for_each_i3cdev(bus
, i3cdev
) {
916 if (i3cdev
== master
->this)
919 if (I3C_BCR_DEVICE_ROLE(i3cdev
->info
.bcr
) ==
924 /* No other master on the bus, skip DEFSLVS. */
928 i3c_bus_for_each_i2cdev(bus
, i2cdev
)
931 defslvs
= i3c_ccc_cmd_dest_init(&dest
, I3C_BROADCAST_ADDR
,
932 struct_size(defslvs
, slaves
,
937 defslvs
->count
= ndevs
;
938 defslvs
->master
.bcr
= master
->this->info
.bcr
;
939 defslvs
->master
.dcr
= master
->this->info
.dcr
;
940 defslvs
->master
.dyn_addr
= master
->this->info
.dyn_addr
<< 1;
941 defslvs
->master
.static_addr
= I3C_BROADCAST_ADDR
<< 1;
943 desc
= defslvs
->slaves
;
944 i3c_bus_for_each_i2cdev(bus
, i2cdev
) {
945 desc
->lvr
= i2cdev
->lvr
;
946 desc
->static_addr
= i2cdev
->addr
<< 1;
950 i3c_bus_for_each_i3cdev(bus
, i3cdev
) {
951 /* Skip the I3C dev representing this master. */
952 if (i3cdev
== master
->this)
955 desc
->bcr
= i3cdev
->info
.bcr
;
956 desc
->dcr
= i3cdev
->info
.dcr
;
957 desc
->dyn_addr
= i3cdev
->info
.dyn_addr
<< 1;
958 desc
->static_addr
= i3cdev
->info
.static_addr
<< 1;
962 i3c_ccc_cmd_init(&cmd
, false, I3C_CCC_DEFSLVS
, &dest
, 1);
963 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
964 i3c_ccc_cmd_dest_cleanup(&dest
);
968 EXPORT_SYMBOL_GPL(i3c_master_defslvs_locked
);
970 static int i3c_master_setda_locked(struct i3c_master_controller
*master
,
971 u8 oldaddr
, u8 newaddr
, bool setdasa
)
973 struct i3c_ccc_cmd_dest dest
;
974 struct i3c_ccc_setda
*setda
;
975 struct i3c_ccc_cmd cmd
;
978 if (!oldaddr
|| !newaddr
)
981 setda
= i3c_ccc_cmd_dest_init(&dest
, oldaddr
, sizeof(*setda
));
985 setda
->addr
= newaddr
<< 1;
986 i3c_ccc_cmd_init(&cmd
, false,
987 setdasa
? I3C_CCC_SETDASA
: I3C_CCC_SETNEWDA
,
989 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
990 i3c_ccc_cmd_dest_cleanup(&dest
);
995 static int i3c_master_setdasa_locked(struct i3c_master_controller
*master
,
996 u8 static_addr
, u8 dyn_addr
)
998 return i3c_master_setda_locked(master
, static_addr
, dyn_addr
, true);
1001 static int i3c_master_setnewda_locked(struct i3c_master_controller
*master
,
1002 u8 oldaddr
, u8 newaddr
)
1004 return i3c_master_setda_locked(master
, oldaddr
, newaddr
, false);
1007 static int i3c_master_getmrl_locked(struct i3c_master_controller
*master
,
1008 struct i3c_device_info
*info
)
1010 struct i3c_ccc_cmd_dest dest
;
1011 unsigned int expected_len
;
1012 struct i3c_ccc_mrl
*mrl
;
1013 struct i3c_ccc_cmd cmd
;
1016 mrl
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*mrl
));
1021 * When the device does not have IBI payload GETMRL only returns 2
1024 if (!(info
->bcr
& I3C_BCR_IBI_PAYLOAD
))
1025 dest
.payload
.len
-= 1;
1027 expected_len
= dest
.payload
.len
;
1028 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETMRL
, &dest
, 1);
1029 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1033 if (dest
.payload
.len
!= expected_len
) {
1038 info
->max_read_len
= be16_to_cpu(mrl
->read_len
);
1040 if (info
->bcr
& I3C_BCR_IBI_PAYLOAD
)
1041 info
->max_ibi_len
= mrl
->ibi_len
;
1044 i3c_ccc_cmd_dest_cleanup(&dest
);
1049 static int i3c_master_getmwl_locked(struct i3c_master_controller
*master
,
1050 struct i3c_device_info
*info
)
1052 struct i3c_ccc_cmd_dest dest
;
1053 struct i3c_ccc_mwl
*mwl
;
1054 struct i3c_ccc_cmd cmd
;
1057 mwl
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*mwl
));
1061 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETMWL
, &dest
, 1);
1062 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1066 if (dest
.payload
.len
!= sizeof(*mwl
)) {
1071 info
->max_write_len
= be16_to_cpu(mwl
->len
);
1074 i3c_ccc_cmd_dest_cleanup(&dest
);
1079 static int i3c_master_getmxds_locked(struct i3c_master_controller
*master
,
1080 struct i3c_device_info
*info
)
1082 struct i3c_ccc_getmxds
*getmaxds
;
1083 struct i3c_ccc_cmd_dest dest
;
1084 struct i3c_ccc_cmd cmd
;
1087 getmaxds
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
,
1092 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETMXDS
, &dest
, 1);
1093 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1097 if (dest
.payload
.len
!= 2 && dest
.payload
.len
!= 5) {
1102 info
->max_read_ds
= getmaxds
->maxrd
;
1103 info
->max_write_ds
= getmaxds
->maxwr
;
1104 if (dest
.payload
.len
== 5)
1105 info
->max_read_turnaround
= getmaxds
->maxrdturn
[0] |
1106 ((u32
)getmaxds
->maxrdturn
[1] << 8) |
1107 ((u32
)getmaxds
->maxrdturn
[2] << 16);
1110 i3c_ccc_cmd_dest_cleanup(&dest
);
1115 static int i3c_master_gethdrcap_locked(struct i3c_master_controller
*master
,
1116 struct i3c_device_info
*info
)
1118 struct i3c_ccc_gethdrcap
*gethdrcap
;
1119 struct i3c_ccc_cmd_dest dest
;
1120 struct i3c_ccc_cmd cmd
;
1123 gethdrcap
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
,
1124 sizeof(*gethdrcap
));
1128 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETHDRCAP
, &dest
, 1);
1129 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1133 if (dest
.payload
.len
!= 1) {
1138 info
->hdr_cap
= gethdrcap
->modes
;
1141 i3c_ccc_cmd_dest_cleanup(&dest
);
1146 static int i3c_master_getpid_locked(struct i3c_master_controller
*master
,
1147 struct i3c_device_info
*info
)
1149 struct i3c_ccc_getpid
*getpid
;
1150 struct i3c_ccc_cmd_dest dest
;
1151 struct i3c_ccc_cmd cmd
;
1154 getpid
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*getpid
));
1158 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETPID
, &dest
, 1);
1159 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1164 for (i
= 0; i
< sizeof(getpid
->pid
); i
++) {
1165 int sft
= (sizeof(getpid
->pid
) - i
- 1) * 8;
1167 info
->pid
|= (u64
)getpid
->pid
[i
] << sft
;
1171 i3c_ccc_cmd_dest_cleanup(&dest
);
1176 static int i3c_master_getbcr_locked(struct i3c_master_controller
*master
,
1177 struct i3c_device_info
*info
)
1179 struct i3c_ccc_getbcr
*getbcr
;
1180 struct i3c_ccc_cmd_dest dest
;
1181 struct i3c_ccc_cmd cmd
;
1184 getbcr
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*getbcr
));
1188 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETBCR
, &dest
, 1);
1189 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1193 info
->bcr
= getbcr
->bcr
;
1196 i3c_ccc_cmd_dest_cleanup(&dest
);
1201 static int i3c_master_getdcr_locked(struct i3c_master_controller
*master
,
1202 struct i3c_device_info
*info
)
1204 struct i3c_ccc_getdcr
*getdcr
;
1205 struct i3c_ccc_cmd_dest dest
;
1206 struct i3c_ccc_cmd cmd
;
1209 getdcr
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*getdcr
));
1213 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETDCR
, &dest
, 1);
1214 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1218 info
->dcr
= getdcr
->dcr
;
1221 i3c_ccc_cmd_dest_cleanup(&dest
);
1226 static int i3c_master_retrieve_dev_info(struct i3c_dev_desc
*dev
)
1228 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1229 enum i3c_addr_slot_status slot_status
;
1232 if (!dev
->info
.dyn_addr
)
1235 slot_status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1236 dev
->info
.dyn_addr
);
1237 if (slot_status
== I3C_ADDR_SLOT_RSVD
||
1238 slot_status
== I3C_ADDR_SLOT_I2C_DEV
)
1241 ret
= i3c_master_getpid_locked(master
, &dev
->info
);
1245 ret
= i3c_master_getbcr_locked(master
, &dev
->info
);
1249 ret
= i3c_master_getdcr_locked(master
, &dev
->info
);
1253 if (dev
->info
.bcr
& I3C_BCR_MAX_DATA_SPEED_LIM
) {
1254 ret
= i3c_master_getmxds_locked(master
, &dev
->info
);
1259 if (dev
->info
.bcr
& I3C_BCR_IBI_PAYLOAD
)
1260 dev
->info
.max_ibi_len
= 1;
1262 i3c_master_getmrl_locked(master
, &dev
->info
);
1263 i3c_master_getmwl_locked(master
, &dev
->info
);
1265 if (dev
->info
.bcr
& I3C_BCR_HDR_CAP
) {
1266 ret
= i3c_master_gethdrcap_locked(master
, &dev
->info
);
1274 static void i3c_master_put_i3c_addrs(struct i3c_dev_desc
*dev
)
1276 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1278 if (dev
->info
.static_addr
)
1279 i3c_bus_set_addr_slot_status(&master
->bus
,
1280 dev
->info
.static_addr
,
1281 I3C_ADDR_SLOT_FREE
);
1283 if (dev
->info
.dyn_addr
)
1284 i3c_bus_set_addr_slot_status(&master
->bus
, dev
->info
.dyn_addr
,
1285 I3C_ADDR_SLOT_FREE
);
1287 if (dev
->boardinfo
&& dev
->boardinfo
->init_dyn_addr
)
1288 i3c_bus_set_addr_slot_status(&master
->bus
, dev
->info
.dyn_addr
,
1289 I3C_ADDR_SLOT_FREE
);
1292 static int i3c_master_get_i3c_addrs(struct i3c_dev_desc
*dev
)
1294 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1295 enum i3c_addr_slot_status status
;
1297 if (!dev
->info
.static_addr
&& !dev
->info
.dyn_addr
)
1300 if (dev
->info
.static_addr
) {
1301 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1302 dev
->info
.static_addr
);
1303 if (status
!= I3C_ADDR_SLOT_FREE
)
1306 i3c_bus_set_addr_slot_status(&master
->bus
,
1307 dev
->info
.static_addr
,
1308 I3C_ADDR_SLOT_I3C_DEV
);
1312 * ->init_dyn_addr should have been reserved before that, so, if we're
1313 * trying to apply a pre-reserved dynamic address, we should not try
1314 * to reserve the address slot a second time.
1316 if (dev
->info
.dyn_addr
&&
1318 dev
->boardinfo
->init_dyn_addr
!= dev
->info
.dyn_addr
)) {
1319 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1320 dev
->info
.dyn_addr
);
1321 if (status
!= I3C_ADDR_SLOT_FREE
)
1322 goto err_release_static_addr
;
1324 i3c_bus_set_addr_slot_status(&master
->bus
, dev
->info
.dyn_addr
,
1325 I3C_ADDR_SLOT_I3C_DEV
);
1330 err_release_static_addr
:
1331 if (dev
->info
.static_addr
)
1332 i3c_bus_set_addr_slot_status(&master
->bus
,
1333 dev
->info
.static_addr
,
1334 I3C_ADDR_SLOT_FREE
);
1339 static int i3c_master_attach_i3c_dev(struct i3c_master_controller
*master
,
1340 struct i3c_dev_desc
*dev
)
1345 * We don't attach devices to the controller until they are
1346 * addressable on the bus.
1348 if (!dev
->info
.static_addr
&& !dev
->info
.dyn_addr
)
1351 ret
= i3c_master_get_i3c_addrs(dev
);
1355 /* Do not attach the master device itself. */
1356 if (master
->this != dev
&& master
->ops
->attach_i3c_dev
) {
1357 ret
= master
->ops
->attach_i3c_dev(dev
);
1359 i3c_master_put_i3c_addrs(dev
);
1364 list_add_tail(&dev
->common
.node
, &master
->bus
.devs
.i3c
);
1369 static int i3c_master_reattach_i3c_dev(struct i3c_dev_desc
*dev
,
1372 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1373 enum i3c_addr_slot_status status
;
1376 if (dev
->info
.dyn_addr
!= old_dyn_addr
) {
1377 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1378 dev
->info
.dyn_addr
);
1379 if (status
!= I3C_ADDR_SLOT_FREE
)
1381 i3c_bus_set_addr_slot_status(&master
->bus
,
1383 I3C_ADDR_SLOT_I3C_DEV
);
1386 if (master
->ops
->reattach_i3c_dev
) {
1387 ret
= master
->ops
->reattach_i3c_dev(dev
, old_dyn_addr
);
1389 i3c_master_put_i3c_addrs(dev
);
1397 static void i3c_master_detach_i3c_dev(struct i3c_dev_desc
*dev
)
1399 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1401 /* Do not detach the master device itself. */
1402 if (master
->this != dev
&& master
->ops
->detach_i3c_dev
)
1403 master
->ops
->detach_i3c_dev(dev
);
1405 i3c_master_put_i3c_addrs(dev
);
1406 list_del(&dev
->common
.node
);
1409 static int i3c_master_attach_i2c_dev(struct i3c_master_controller
*master
,
1410 struct i2c_dev_desc
*dev
)
1414 if (master
->ops
->attach_i2c_dev
) {
1415 ret
= master
->ops
->attach_i2c_dev(dev
);
1420 list_add_tail(&dev
->common
.node
, &master
->bus
.devs
.i2c
);
1425 static void i3c_master_detach_i2c_dev(struct i2c_dev_desc
*dev
)
1427 struct i3c_master_controller
*master
= i2c_dev_get_master(dev
);
1429 list_del(&dev
->common
.node
);
1431 if (master
->ops
->detach_i2c_dev
)
1432 master
->ops
->detach_i2c_dev(dev
);
1435 static void i3c_master_pre_assign_dyn_addr(struct i3c_dev_desc
*dev
)
1437 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1440 if (!dev
->boardinfo
|| !dev
->boardinfo
->init_dyn_addr
||
1441 !dev
->boardinfo
->static_addr
)
1444 ret
= i3c_master_setdasa_locked(master
, dev
->info
.static_addr
,
1445 dev
->boardinfo
->init_dyn_addr
);
1449 dev
->info
.dyn_addr
= dev
->boardinfo
->init_dyn_addr
;
1450 ret
= i3c_master_reattach_i3c_dev(dev
, 0);
1454 ret
= i3c_master_retrieve_dev_info(dev
);
1461 i3c_master_rstdaa_locked(master
, dev
->boardinfo
->init_dyn_addr
);
1465 i3c_master_register_new_i3c_devs(struct i3c_master_controller
*master
)
1467 struct i3c_dev_desc
*desc
;
1470 if (!master
->init_done
)
1473 i3c_bus_for_each_i3cdev(&master
->bus
, desc
) {
1474 if (desc
->dev
|| !desc
->info
.dyn_addr
|| desc
== master
->this)
1477 desc
->dev
= kzalloc(sizeof(*desc
->dev
), GFP_KERNEL
);
1481 desc
->dev
->bus
= &master
->bus
;
1482 desc
->dev
->desc
= desc
;
1483 desc
->dev
->dev
.parent
= &master
->dev
;
1484 desc
->dev
->dev
.type
= &i3c_device_type
;
1485 desc
->dev
->dev
.bus
= &i3c_bus_type
;
1486 desc
->dev
->dev
.release
= i3c_device_release
;
1487 dev_set_name(&desc
->dev
->dev
, "%d-%llx", master
->bus
.id
,
1490 if (desc
->boardinfo
)
1491 desc
->dev
->dev
.of_node
= desc
->boardinfo
->of_node
;
1493 ret
= device_register(&desc
->dev
->dev
);
1495 dev_err(&master
->dev
,
1496 "Failed to add I3C device (err = %d)\n", ret
);
1501 * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment)
1502 * @master: master doing the DAA
1504 * This function is instantiating an I3C device object and adding it to the
1505 * I3C device list. All device information are automatically retrieved using
1506 * standard CCC commands.
1508 * The I3C device object is returned in case the master wants to attach
1509 * private data to it using i3c_dev_set_master_data().
1511 * This function must be called with the bus lock held in write mode.
1513 * Return: a 0 in case of success, an negative error code otherwise.
1515 int i3c_master_do_daa(struct i3c_master_controller
*master
)
1519 i3c_bus_maintenance_lock(&master
->bus
);
1520 ret
= master
->ops
->do_daa(master
);
1521 i3c_bus_maintenance_unlock(&master
->bus
);
1526 i3c_bus_normaluse_lock(&master
->bus
);
1527 i3c_master_register_new_i3c_devs(master
);
1528 i3c_bus_normaluse_unlock(&master
->bus
);
1532 EXPORT_SYMBOL_GPL(i3c_master_do_daa
);
1535 * i3c_master_set_info() - set master device information
1536 * @master: master used to send frames on the bus
1537 * @info: I3C device information
1539 * Set master device info. This should be called from
1540 * &i3c_master_controller_ops->bus_init().
1542 * Not all &i3c_device_info fields are meaningful for a master device.
1543 * Here is a list of fields that should be properly filled:
1545 * - &i3c_device_info->dyn_addr
1546 * - &i3c_device_info->bcr
1547 * - &i3c_device_info->dcr
1548 * - &i3c_device_info->pid
1549 * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in
1550 * &i3c_device_info->bcr
1552 * This function must be called with the bus lock held in maintenance mode.
1554 * Return: 0 if @info contains valid information (not every piece of
1555 * information can be checked, but we can at least make sure @info->dyn_addr
1556 * and @info->bcr are correct), -EINVAL otherwise.
1558 int i3c_master_set_info(struct i3c_master_controller
*master
,
1559 const struct i3c_device_info
*info
)
1561 struct i3c_dev_desc
*i3cdev
;
1564 if (!i3c_bus_dev_addr_is_avail(&master
->bus
, info
->dyn_addr
))
1567 if (I3C_BCR_DEVICE_ROLE(info
->bcr
) == I3C_BCR_I3C_MASTER
&&
1574 i3cdev
= i3c_master_alloc_i3c_dev(master
, info
);
1576 return PTR_ERR(i3cdev
);
1578 master
->this = i3cdev
;
1579 master
->bus
.cur_master
= master
->this;
1581 ret
= i3c_master_attach_i3c_dev(master
, i3cdev
);
1588 i3c_master_free_i3c_dev(i3cdev
);
1592 EXPORT_SYMBOL_GPL(i3c_master_set_info
);
1594 static void i3c_master_detach_free_devs(struct i3c_master_controller
*master
)
1596 struct i3c_dev_desc
*i3cdev
, *i3ctmp
;
1597 struct i2c_dev_desc
*i2cdev
, *i2ctmp
;
1599 list_for_each_entry_safe(i3cdev
, i3ctmp
, &master
->bus
.devs
.i3c
,
1601 i3c_master_detach_i3c_dev(i3cdev
);
1603 if (i3cdev
->boardinfo
&& i3cdev
->boardinfo
->init_dyn_addr
)
1604 i3c_bus_set_addr_slot_status(&master
->bus
,
1605 i3cdev
->boardinfo
->init_dyn_addr
,
1606 I3C_ADDR_SLOT_FREE
);
1608 i3c_master_free_i3c_dev(i3cdev
);
1611 list_for_each_entry_safe(i2cdev
, i2ctmp
, &master
->bus
.devs
.i2c
,
1613 i3c_master_detach_i2c_dev(i2cdev
);
1614 i3c_bus_set_addr_slot_status(&master
->bus
,
1616 I3C_ADDR_SLOT_FREE
);
1617 i3c_master_free_i2c_dev(i2cdev
);
1622 * i3c_master_bus_init() - initialize an I3C bus
1623 * @master: main master initializing the bus
1625 * This function is following all initialisation steps described in the I3C
1628 * 1. Attach I2C and statically defined I3C devs to the master so that the
1629 * master can fill its internal device table appropriately
1631 * 2. Call &i3c_master_controller_ops->bus_init() method to initialize
1632 * the master controller. That's usually where the bus mode is selected
1633 * (pure bus or mixed fast/slow bus)
1635 * 3. Instruct all devices on the bus to drop their dynamic address. This is
1636 * particularly important when the bus was previously configured by someone
1637 * else (for example the bootloader)
1639 * 4. Disable all slave events.
1641 * 5. Pre-assign dynamic addresses requested by the FW with SETDASA for I3C
1642 * devices that have a static address
1644 * 6. Do a DAA (Dynamic Address Assignment) to assign dynamic addresses to all
1645 * remaining I3C devices
1647 * Once this is done, all I3C and I2C devices should be usable.
1649 * Return: a 0 in case of success, an negative error code otherwise.
1651 static int i3c_master_bus_init(struct i3c_master_controller
*master
)
1653 enum i3c_addr_slot_status status
;
1654 struct i2c_dev_boardinfo
*i2cboardinfo
;
1655 struct i3c_dev_boardinfo
*i3cboardinfo
;
1656 struct i3c_dev_desc
*i3cdev
;
1657 struct i2c_dev_desc
*i2cdev
;
1661 * First attach all devices with static definitions provided by the
1664 list_for_each_entry(i2cboardinfo
, &master
->boardinfo
.i2c
, node
) {
1665 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1666 i2cboardinfo
->base
.addr
);
1667 if (status
!= I3C_ADDR_SLOT_FREE
) {
1669 goto err_detach_devs
;
1672 i3c_bus_set_addr_slot_status(&master
->bus
,
1673 i2cboardinfo
->base
.addr
,
1674 I3C_ADDR_SLOT_I2C_DEV
);
1676 i2cdev
= i3c_master_alloc_i2c_dev(master
, i2cboardinfo
);
1677 if (IS_ERR(i2cdev
)) {
1678 ret
= PTR_ERR(i2cdev
);
1679 goto err_detach_devs
;
1682 ret
= i3c_master_attach_i2c_dev(master
, i2cdev
);
1684 i3c_master_free_i2c_dev(i2cdev
);
1685 goto err_detach_devs
;
1688 list_for_each_entry(i3cboardinfo
, &master
->boardinfo
.i3c
, node
) {
1689 struct i3c_device_info info
= {
1690 .static_addr
= i3cboardinfo
->static_addr
,
1693 if (i3cboardinfo
->init_dyn_addr
) {
1694 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1695 i3cboardinfo
->init_dyn_addr
);
1696 if (status
!= I3C_ADDR_SLOT_FREE
) {
1698 goto err_detach_devs
;
1702 i3cdev
= i3c_master_alloc_i3c_dev(master
, &info
);
1703 if (IS_ERR(i3cdev
)) {
1704 ret
= PTR_ERR(i3cdev
);
1705 goto err_detach_devs
;
1708 i3cdev
->boardinfo
= i3cboardinfo
;
1710 ret
= i3c_master_attach_i3c_dev(master
, i3cdev
);
1712 i3c_master_free_i3c_dev(i3cdev
);
1713 goto err_detach_devs
;
1718 * Now execute the controller specific ->bus_init() routine, which
1719 * might configure its internal logic to match the bus limitations.
1721 ret
= master
->ops
->bus_init(master
);
1723 goto err_detach_devs
;
1726 * The master device should have been instantiated in ->bus_init(),
1727 * complain if this was not the case.
1729 if (!master
->this) {
1730 dev_err(&master
->dev
,
1731 "master_set_info() was not called in ->bus_init()\n");
1733 goto err_bus_cleanup
;
1737 * Reset all dynamic address that may have been assigned before
1738 * (assigned by the bootloader for example).
1740 ret
= i3c_master_rstdaa_locked(master
, I3C_BROADCAST_ADDR
);
1741 if (ret
&& ret
!= I3C_ERROR_M2
)
1742 goto err_bus_cleanup
;
1744 /* Disable all slave events before starting DAA. */
1745 ret
= i3c_master_disec_locked(master
, I3C_BROADCAST_ADDR
,
1746 I3C_CCC_EVENT_SIR
| I3C_CCC_EVENT_MR
|
1748 if (ret
&& ret
!= I3C_ERROR_M2
)
1749 goto err_bus_cleanup
;
1752 * Pre-assign dynamic address and retrieve device information if
1755 i3c_bus_for_each_i3cdev(&master
->bus
, i3cdev
)
1756 i3c_master_pre_assign_dyn_addr(i3cdev
);
1758 ret
= i3c_master_do_daa(master
);
1765 i3c_master_rstdaa_locked(master
, I3C_BROADCAST_ADDR
);
1768 if (master
->ops
->bus_cleanup
)
1769 master
->ops
->bus_cleanup(master
);
1772 i3c_master_detach_free_devs(master
);
1777 static void i3c_master_bus_cleanup(struct i3c_master_controller
*master
)
1779 if (master
->ops
->bus_cleanup
)
1780 master
->ops
->bus_cleanup(master
);
1782 i3c_master_detach_free_devs(master
);
1785 static struct i3c_dev_desc
*
1786 i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc
*refdev
)
1788 struct i3c_master_controller
*master
= i3c_dev_get_master(refdev
);
1789 struct i3c_dev_desc
*i3cdev
;
1791 i3c_bus_for_each_i3cdev(&master
->bus
, i3cdev
) {
1792 if (i3cdev
!= refdev
&& i3cdev
->info
.pid
== refdev
->info
.pid
)
1800 * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus
1801 * @master: master used to send frames on the bus
1802 * @addr: I3C slave dynamic address assigned to the device
1804 * This function is instantiating an I3C device object and adding it to the
1805 * I3C device list. All device information are automatically retrieved using
1806 * standard CCC commands.
1808 * The I3C device object is returned in case the master wants to attach
1809 * private data to it using i3c_dev_set_master_data().
1811 * This function must be called with the bus lock held in write mode.
1813 * Return: a 0 in case of success, an negative error code otherwise.
1815 int i3c_master_add_i3c_dev_locked(struct i3c_master_controller
*master
,
1818 struct i3c_device_info info
= { .dyn_addr
= addr
};
1819 struct i3c_dev_desc
*newdev
, *olddev
;
1820 u8 old_dyn_addr
= addr
, expected_dyn_addr
;
1821 struct i3c_ibi_setup ibireq
= { };
1822 bool enable_ibi
= false;
1828 newdev
= i3c_master_alloc_i3c_dev(master
, &info
);
1830 return PTR_ERR(newdev
);
1832 ret
= i3c_master_attach_i3c_dev(master
, newdev
);
1836 ret
= i3c_master_retrieve_dev_info(newdev
);
1838 goto err_detach_dev
;
1840 olddev
= i3c_master_search_i3c_dev_duplicate(newdev
);
1842 newdev
->boardinfo
= olddev
->boardinfo
;
1843 newdev
->info
.static_addr
= olddev
->info
.static_addr
;
1844 newdev
->dev
= olddev
->dev
;
1846 newdev
->dev
->desc
= newdev
;
1849 * We need to restore the IBI state too, so let's save the
1850 * IBI information and try to restore them after olddev has
1851 * been detached+released and its IBI has been stopped and
1852 * the associated resources have been freed.
1854 mutex_lock(&olddev
->ibi_lock
);
1856 ibireq
.handler
= olddev
->ibi
->handler
;
1857 ibireq
.max_payload_len
= olddev
->ibi
->max_payload_len
;
1858 ibireq
.num_slots
= olddev
->ibi
->num_slots
;
1860 if (olddev
->ibi
->enabled
) {
1862 i3c_dev_disable_ibi_locked(olddev
);
1865 i3c_dev_free_ibi_locked(olddev
);
1867 mutex_unlock(&olddev
->ibi_lock
);
1869 old_dyn_addr
= olddev
->info
.dyn_addr
;
1871 i3c_master_detach_i3c_dev(olddev
);
1872 i3c_master_free_i3c_dev(olddev
);
1875 ret
= i3c_master_reattach_i3c_dev(newdev
, old_dyn_addr
);
1877 goto err_detach_dev
;
1880 * Depending on our previous state, the expected dynamic address might
1882 * - if the device already had a dynamic address assigned, let's try to
1884 * - if the device did not have a dynamic address and the firmware
1885 * requested a specific address, pick this one
1886 * - in any other case, keep the address automatically assigned by the
1889 if (old_dyn_addr
&& old_dyn_addr
!= newdev
->info
.dyn_addr
)
1890 expected_dyn_addr
= old_dyn_addr
;
1891 else if (newdev
->boardinfo
&& newdev
->boardinfo
->init_dyn_addr
)
1892 expected_dyn_addr
= newdev
->boardinfo
->init_dyn_addr
;
1894 expected_dyn_addr
= newdev
->info
.dyn_addr
;
1896 if (newdev
->info
.dyn_addr
!= expected_dyn_addr
) {
1898 * Try to apply the expected dynamic address. If it fails, keep
1899 * the address assigned by the master.
1901 ret
= i3c_master_setnewda_locked(master
,
1902 newdev
->info
.dyn_addr
,
1905 old_dyn_addr
= newdev
->info
.dyn_addr
;
1906 newdev
->info
.dyn_addr
= expected_dyn_addr
;
1907 i3c_master_reattach_i3c_dev(newdev
, old_dyn_addr
);
1909 dev_err(&master
->dev
,
1910 "Failed to assign reserved/old address to device %d%llx",
1911 master
->bus
.id
, newdev
->info
.pid
);
1916 * Now is time to try to restore the IBI setup. If we're lucky,
1917 * everything works as before, otherwise, all we can do is complain.
1918 * FIXME: maybe we should add callback to inform the driver that it
1919 * should request the IBI again instead of trying to hide that from
1922 if (ibireq
.handler
) {
1923 mutex_lock(&newdev
->ibi_lock
);
1924 ret
= i3c_dev_request_ibi_locked(newdev
, &ibireq
);
1926 dev_err(&master
->dev
,
1927 "Failed to request IBI on device %d-%llx",
1928 master
->bus
.id
, newdev
->info
.pid
);
1929 } else if (enable_ibi
) {
1930 ret
= i3c_dev_enable_ibi_locked(newdev
);
1932 dev_err(&master
->dev
,
1933 "Failed to re-enable IBI on device %d-%llx",
1934 master
->bus
.id
, newdev
->info
.pid
);
1936 mutex_unlock(&newdev
->ibi_lock
);
1942 if (newdev
->dev
&& newdev
->dev
->desc
)
1943 newdev
->dev
->desc
= NULL
;
1945 i3c_master_detach_i3c_dev(newdev
);
1948 i3c_master_free_i3c_dev(newdev
);
1952 EXPORT_SYMBOL_GPL(i3c_master_add_i3c_dev_locked
);
1954 #define OF_I3C_REG1_IS_I2C_DEV BIT(31)
1957 of_i3c_master_add_i2c_boardinfo(struct i3c_master_controller
*master
,
1958 struct device_node
*node
, u32
*reg
)
1960 struct i2c_dev_boardinfo
*boardinfo
;
1961 struct device
*dev
= &master
->dev
;
1964 boardinfo
= devm_kzalloc(dev
, sizeof(*boardinfo
), GFP_KERNEL
);
1968 ret
= of_i2c_get_board_info(dev
, node
, &boardinfo
->base
);
1973 * The I3C Specification does not clearly say I2C devices with 10-bit
1974 * address are supported. These devices can't be passed properly through
1977 if (boardinfo
->base
.flags
& I2C_CLIENT_TEN
) {
1978 dev_err(dev
, "I2C device with 10 bit address not supported.");
1982 /* LVR is encoded in reg[2]. */
1983 boardinfo
->lvr
= reg
[2];
1985 list_add_tail(&boardinfo
->node
, &master
->boardinfo
.i2c
);
1992 of_i3c_master_add_i3c_boardinfo(struct i3c_master_controller
*master
,
1993 struct device_node
*node
, u32
*reg
)
1995 struct i3c_dev_boardinfo
*boardinfo
;
1996 struct device
*dev
= &master
->dev
;
1997 enum i3c_addr_slot_status addrstatus
;
1998 u32 init_dyn_addr
= 0;
2000 boardinfo
= devm_kzalloc(dev
, sizeof(*boardinfo
), GFP_KERNEL
);
2005 if (reg
[0] > I3C_MAX_ADDR
)
2008 addrstatus
= i3c_bus_get_addr_slot_status(&master
->bus
,
2010 if (addrstatus
!= I3C_ADDR_SLOT_FREE
)
2014 boardinfo
->static_addr
= reg
[0];
2016 if (!of_property_read_u32(node
, "assigned-address", &init_dyn_addr
)) {
2017 if (init_dyn_addr
> I3C_MAX_ADDR
)
2020 addrstatus
= i3c_bus_get_addr_slot_status(&master
->bus
,
2022 if (addrstatus
!= I3C_ADDR_SLOT_FREE
)
2026 boardinfo
->pid
= ((u64
)reg
[1] << 32) | reg
[2];
2028 if ((boardinfo
->pid
& GENMASK_ULL(63, 48)) ||
2029 I3C_PID_RND_LOWER_32BITS(boardinfo
->pid
))
2032 boardinfo
->init_dyn_addr
= init_dyn_addr
;
2033 boardinfo
->of_node
= of_node_get(node
);
2034 list_add_tail(&boardinfo
->node
, &master
->boardinfo
.i3c
);
2039 static int of_i3c_master_add_dev(struct i3c_master_controller
*master
,
2040 struct device_node
*node
)
2045 if (!master
|| !node
)
2048 ret
= of_property_read_u32_array(node
, "reg", reg
, ARRAY_SIZE(reg
));
2053 * The manufacturer ID can't be 0. If reg[1] == 0 that means we're
2054 * dealing with an I2C device.
2057 ret
= of_i3c_master_add_i2c_boardinfo(master
, node
, reg
);
2059 ret
= of_i3c_master_add_i3c_boardinfo(master
, node
, reg
);
2064 static int of_populate_i3c_bus(struct i3c_master_controller
*master
)
2066 struct device
*dev
= &master
->dev
;
2067 struct device_node
*i3cbus_np
= dev
->of_node
;
2068 struct device_node
*node
;
2075 for_each_available_child_of_node(i3cbus_np
, node
) {
2076 ret
= of_i3c_master_add_dev(master
, node
);
2084 * The user might want to limit I2C and I3C speed in case some devices
2085 * on the bus are not supporting typical rates, or if the bus topology
2086 * prevents it from using max possible rate.
2088 if (!of_property_read_u32(i3cbus_np
, "i2c-scl-hz", &val
))
2089 master
->bus
.scl_rate
.i2c
= val
;
2091 if (!of_property_read_u32(i3cbus_np
, "i3c-scl-hz", &val
))
2092 master
->bus
.scl_rate
.i3c
= val
;
2097 static int i3c_master_i2c_adapter_xfer(struct i2c_adapter
*adap
,
2098 struct i2c_msg
*xfers
, int nxfers
)
2100 struct i3c_master_controller
*master
= i2c_adapter_to_i3c_master(adap
);
2101 struct i2c_dev_desc
*dev
;
2105 if (!xfers
|| !master
|| nxfers
<= 0)
2108 if (!master
->ops
->i2c_xfers
)
2111 /* Doing transfers to different devices is not supported. */
2112 addr
= xfers
[0].addr
;
2113 for (i
= 1; i
< nxfers
; i
++) {
2114 if (addr
!= xfers
[i
].addr
)
2118 i3c_bus_normaluse_lock(&master
->bus
);
2119 dev
= i3c_master_find_i2c_dev_by_addr(master
, addr
);
2123 ret
= master
->ops
->i2c_xfers(dev
, xfers
, nxfers
);
2124 i3c_bus_normaluse_unlock(&master
->bus
);
2126 return ret
? ret
: nxfers
;
2129 static u32
i3c_master_i2c_funcs(struct i2c_adapter
*adapter
)
2131 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
;
2134 static const struct i2c_algorithm i3c_master_i2c_algo
= {
2135 .master_xfer
= i3c_master_i2c_adapter_xfer
,
2136 .functionality
= i3c_master_i2c_funcs
,
2139 static int i3c_master_i2c_adapter_init(struct i3c_master_controller
*master
)
2141 struct i2c_adapter
*adap
= i3c_master_to_i2c_adapter(master
);
2142 struct i2c_dev_desc
*i2cdev
;
2145 adap
->dev
.parent
= master
->dev
.parent
;
2146 adap
->owner
= master
->dev
.parent
->driver
->owner
;
2147 adap
->algo
= &i3c_master_i2c_algo
;
2148 strncpy(adap
->name
, dev_name(master
->dev
.parent
), sizeof(adap
->name
));
2150 /* FIXME: Should we allow i3c masters to override these values? */
2151 adap
->timeout
= 1000;
2154 ret
= i2c_add_adapter(adap
);
2159 * We silently ignore failures here. The bus should keep working
2160 * correctly even if one or more i2c devices are not registered.
2162 i3c_bus_for_each_i2cdev(&master
->bus
, i2cdev
)
2163 i2cdev
->dev
= i2c_new_device(adap
, &i2cdev
->boardinfo
->base
);
2168 static void i3c_master_i2c_adapter_cleanup(struct i3c_master_controller
*master
)
2170 struct i2c_dev_desc
*i2cdev
;
2172 i2c_del_adapter(&master
->i2c
);
2174 i3c_bus_for_each_i2cdev(&master
->bus
, i2cdev
)
2178 static void i3c_master_unregister_i3c_devs(struct i3c_master_controller
*master
)
2180 struct i3c_dev_desc
*i3cdev
;
2182 i3c_bus_for_each_i3cdev(&master
->bus
, i3cdev
) {
2186 i3cdev
->dev
->desc
= NULL
;
2187 if (device_is_registered(&i3cdev
->dev
->dev
))
2188 device_unregister(&i3cdev
->dev
->dev
);
2190 put_device(&i3cdev
->dev
->dev
);
2196 * i3c_master_queue_ibi() - Queue an IBI
2197 * @dev: the device this IBI is coming from
2198 * @slot: the IBI slot used to store the payload
2200 * Queue an IBI to the controller workqueue. The IBI handler attached to
2201 * the dev will be called from a workqueue context.
2203 void i3c_master_queue_ibi(struct i3c_dev_desc
*dev
, struct i3c_ibi_slot
*slot
)
2205 atomic_inc(&dev
->ibi
->pending_ibis
);
2206 queue_work(dev
->common
.master
->wq
, &slot
->work
);
2208 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi
);
2210 static void i3c_master_handle_ibi(struct work_struct
*work
)
2212 struct i3c_ibi_slot
*slot
= container_of(work
, struct i3c_ibi_slot
,
2214 struct i3c_dev_desc
*dev
= slot
->dev
;
2215 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
2216 struct i3c_ibi_payload payload
;
2218 payload
.data
= slot
->data
;
2219 payload
.len
= slot
->len
;
2222 dev
->ibi
->handler(dev
->dev
, &payload
);
2224 master
->ops
->recycle_ibi_slot(dev
, slot
);
2225 if (atomic_dec_and_test(&dev
->ibi
->pending_ibis
))
2226 complete(&dev
->ibi
->all_ibis_handled
);
2229 static void i3c_master_init_ibi_slot(struct i3c_dev_desc
*dev
,
2230 struct i3c_ibi_slot
*slot
)
2233 INIT_WORK(&slot
->work
, i3c_master_handle_ibi
);
2236 struct i3c_generic_ibi_slot
{
2237 struct list_head node
;
2238 struct i3c_ibi_slot base
;
2241 struct i3c_generic_ibi_pool
{
2243 unsigned int num_slots
;
2244 struct i3c_generic_ibi_slot
*slots
;
2246 struct list_head free_slots
;
2247 struct list_head pending
;
2251 * i3c_generic_ibi_free_pool() - Free a generic IBI pool
2252 * @pool: the IBI pool to free
2254 * Free all IBI slots allated by a generic IBI pool.
2256 void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool
*pool
)
2258 struct i3c_generic_ibi_slot
*slot
;
2259 unsigned int nslots
= 0;
2261 while (!list_empty(&pool
->free_slots
)) {
2262 slot
= list_first_entry(&pool
->free_slots
,
2263 struct i3c_generic_ibi_slot
, node
);
2264 list_del(&slot
->node
);
2269 * If the number of freed slots is not equal to the number of allocated
2270 * slots we have a leak somewhere.
2272 WARN_ON(nslots
!= pool
->num_slots
);
2274 kfree(pool
->payload_buf
);
2278 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool
);
2281 * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool
2282 * @dev: the device this pool will be used for
2283 * @req: IBI setup request describing what the device driver expects
2285 * Create a generic IBI pool based on the information provided in @req.
2287 * Return: a valid IBI pool in case of success, an ERR_PTR() otherwise.
2289 struct i3c_generic_ibi_pool
*
2290 i3c_generic_ibi_alloc_pool(struct i3c_dev_desc
*dev
,
2291 const struct i3c_ibi_setup
*req
)
2293 struct i3c_generic_ibi_pool
*pool
;
2294 struct i3c_generic_ibi_slot
*slot
;
2298 pool
= kzalloc(sizeof(*pool
), GFP_KERNEL
);
2300 return ERR_PTR(-ENOMEM
);
2302 spin_lock_init(&pool
->lock
);
2303 INIT_LIST_HEAD(&pool
->free_slots
);
2304 INIT_LIST_HEAD(&pool
->pending
);
2306 pool
->slots
= kcalloc(req
->num_slots
, sizeof(*slot
), GFP_KERNEL
);
2312 if (req
->max_payload_len
) {
2313 pool
->payload_buf
= kcalloc(req
->num_slots
,
2314 req
->max_payload_len
, GFP_KERNEL
);
2315 if (!pool
->payload_buf
) {
2321 for (i
= 0; i
< req
->num_slots
; i
++) {
2322 slot
= &pool
->slots
[i
];
2323 i3c_master_init_ibi_slot(dev
, &slot
->base
);
2325 if (req
->max_payload_len
)
2326 slot
->base
.data
= pool
->payload_buf
+
2327 (i
* req
->max_payload_len
);
2329 list_add_tail(&slot
->node
, &pool
->free_slots
);
2336 i3c_generic_ibi_free_pool(pool
);
2337 return ERR_PTR(ret
);
2339 EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool
);
2342 * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool
2343 * @pool: the pool to query an IBI slot on
2345 * Search for a free slot in a generic IBI pool.
2346 * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot()
2347 * when it's no longer needed.
2349 * Return: a pointer to a free slot, or NULL if there's no free slot available.
2351 struct i3c_ibi_slot
*
2352 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool
*pool
)
2354 struct i3c_generic_ibi_slot
*slot
;
2355 unsigned long flags
;
2357 spin_lock_irqsave(&pool
->lock
, flags
);
2358 slot
= list_first_entry_or_null(&pool
->free_slots
,
2359 struct i3c_generic_ibi_slot
, node
);
2361 list_del(&slot
->node
);
2362 spin_unlock_irqrestore(&pool
->lock
, flags
);
2364 return slot
? &slot
->base
: NULL
;
2366 EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot
);
2369 * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool
2370 * @pool: the pool to return the IBI slot to
2371 * @s: IBI slot to recycle
2373 * Add an IBI slot back to its generic IBI pool. Should be called from the
2374 * master driver struct_master_controller_ops->recycle_ibi() method.
2376 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool
*pool
,
2377 struct i3c_ibi_slot
*s
)
2379 struct i3c_generic_ibi_slot
*slot
;
2380 unsigned long flags
;
2385 slot
= container_of(s
, struct i3c_generic_ibi_slot
, base
);
2386 spin_lock_irqsave(&pool
->lock
, flags
);
2387 list_add_tail(&slot
->node
, &pool
->free_slots
);
2388 spin_unlock_irqrestore(&pool
->lock
, flags
);
2390 EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot
);
2392 static int i3c_master_check_ops(const struct i3c_master_controller_ops
*ops
)
2394 if (!ops
|| !ops
->bus_init
|| !ops
->priv_xfers
||
2395 !ops
->send_ccc_cmd
|| !ops
->do_daa
|| !ops
->i2c_xfers
)
2398 if (ops
->request_ibi
&&
2399 (!ops
->enable_ibi
|| !ops
->disable_ibi
|| !ops
->free_ibi
||
2400 !ops
->recycle_ibi_slot
))
2407 * i3c_master_register() - register an I3C master
2408 * @master: master used to send frames on the bus
2409 * @parent: the parent device (the one that provides this I3C master
2411 * @ops: the master controller operations
2412 * @secondary: true if you are registering a secondary master. Will return
2413 * -ENOTSUPP if set to true since secondary masters are not yet
2416 * This function takes care of everything for you:
2418 * - creates and initializes the I3C bus
2419 * - populates the bus with static I2C devs if @parent->of_node is not
2421 * - registers all I3C devices added by the controller during bus
2423 * - registers the I2C adapter and all I2C devices
2425 * Return: 0 in case of success, a negative error code otherwise.
2427 int i3c_master_register(struct i3c_master_controller
*master
,
2428 struct device
*parent
,
2429 const struct i3c_master_controller_ops
*ops
,
2432 unsigned long i2c_scl_rate
= I3C_BUS_I2C_FM_PLUS_SCL_RATE
;
2433 struct i3c_bus
*i3cbus
= i3c_master_get_bus(master
);
2434 enum i3c_bus_mode mode
= I3C_BUS_MODE_PURE
;
2435 struct i2c_dev_boardinfo
*i2cbi
;
2438 /* We do not support secondary masters yet. */
2442 ret
= i3c_master_check_ops(ops
);
2446 master
->dev
.parent
= parent
;
2447 master
->dev
.of_node
= of_node_get(parent
->of_node
);
2448 master
->dev
.bus
= &i3c_bus_type
;
2449 master
->dev
.type
= &i3c_masterdev_type
;
2450 master
->dev
.release
= i3c_masterdev_release
;
2452 master
->secondary
= secondary
;
2453 INIT_LIST_HEAD(&master
->boardinfo
.i2c
);
2454 INIT_LIST_HEAD(&master
->boardinfo
.i3c
);
2456 ret
= i3c_bus_init(i3cbus
);
2460 device_initialize(&master
->dev
);
2461 dev_set_name(&master
->dev
, "i3c-%d", i3cbus
->id
);
2463 ret
= of_populate_i3c_bus(master
);
2467 list_for_each_entry(i2cbi
, &master
->boardinfo
.i2c
, node
) {
2468 switch (i2cbi
->lvr
& I3C_LVR_I2C_INDEX_MASK
) {
2469 case I3C_LVR_I2C_INDEX(0):
2470 if (mode
< I3C_BUS_MODE_MIXED_FAST
)
2471 mode
= I3C_BUS_MODE_MIXED_FAST
;
2473 case I3C_LVR_I2C_INDEX(1):
2474 if (mode
< I3C_BUS_MODE_MIXED_LIMITED
)
2475 mode
= I3C_BUS_MODE_MIXED_LIMITED
;
2477 case I3C_LVR_I2C_INDEX(2):
2478 if (mode
< I3C_BUS_MODE_MIXED_SLOW
)
2479 mode
= I3C_BUS_MODE_MIXED_SLOW
;
2486 if (i2cbi
->lvr
& I3C_LVR_I2C_FM_MODE
)
2487 i2c_scl_rate
= I3C_BUS_I2C_FM_SCL_RATE
;
2490 ret
= i3c_bus_set_mode(i3cbus
, mode
, i2c_scl_rate
);
2494 master
->wq
= alloc_workqueue("%s", 0, 0, dev_name(parent
));
2500 ret
= i3c_master_bus_init(master
);
2504 ret
= device_add(&master
->dev
);
2506 goto err_cleanup_bus
;
2509 * Expose our I3C bus as an I2C adapter so that I2C devices are exposed
2510 * through the I2C subsystem.
2512 ret
= i3c_master_i2c_adapter_init(master
);
2517 * We're done initializing the bus and the controller, we can now
2518 * register I3C devices discovered during the initial DAA.
2520 master
->init_done
= true;
2521 i3c_bus_normaluse_lock(&master
->bus
);
2522 i3c_master_register_new_i3c_devs(master
);
2523 i3c_bus_normaluse_unlock(&master
->bus
);
2528 device_del(&master
->dev
);
2531 i3c_master_bus_cleanup(master
);
2534 put_device(&master
->dev
);
2538 EXPORT_SYMBOL_GPL(i3c_master_register
);
2541 * i3c_master_unregister() - unregister an I3C master
2542 * @master: master used to send frames on the bus
2544 * Basically undo everything done in i3c_master_register().
2546 * Return: 0 in case of success, a negative error code otherwise.
2548 int i3c_master_unregister(struct i3c_master_controller
*master
)
2550 i3c_master_i2c_adapter_cleanup(master
);
2551 i3c_master_unregister_i3c_devs(master
);
2552 i3c_master_bus_cleanup(master
);
2553 device_unregister(&master
->dev
);
2557 EXPORT_SYMBOL_GPL(i3c_master_unregister
);
2559 int i3c_dev_do_priv_xfers_locked(struct i3c_dev_desc
*dev
,
2560 struct i3c_priv_xfer
*xfers
,
2563 struct i3c_master_controller
*master
;
2568 master
= i3c_dev_get_master(dev
);
2569 if (!master
|| !xfers
)
2572 if (!master
->ops
->priv_xfers
)
2575 return master
->ops
->priv_xfers(dev
, xfers
, nxfers
);
2578 int i3c_dev_disable_ibi_locked(struct i3c_dev_desc
*dev
)
2580 struct i3c_master_controller
*master
;
2586 master
= i3c_dev_get_master(dev
);
2587 ret
= master
->ops
->disable_ibi(dev
);
2591 reinit_completion(&dev
->ibi
->all_ibis_handled
);
2592 if (atomic_read(&dev
->ibi
->pending_ibis
))
2593 wait_for_completion(&dev
->ibi
->all_ibis_handled
);
2595 dev
->ibi
->enabled
= false;
2600 int i3c_dev_enable_ibi_locked(struct i3c_dev_desc
*dev
)
2602 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
2608 ret
= master
->ops
->enable_ibi(dev
);
2610 dev
->ibi
->enabled
= true;
2615 int i3c_dev_request_ibi_locked(struct i3c_dev_desc
*dev
,
2616 const struct i3c_ibi_setup
*req
)
2618 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
2619 struct i3c_device_ibi_info
*ibi
;
2622 if (!master
->ops
->request_ibi
)
2628 ibi
= kzalloc(sizeof(*ibi
), GFP_KERNEL
);
2632 atomic_set(&ibi
->pending_ibis
, 0);
2633 init_completion(&ibi
->all_ibis_handled
);
2634 ibi
->handler
= req
->handler
;
2635 ibi
->max_payload_len
= req
->max_payload_len
;
2636 ibi
->num_slots
= req
->num_slots
;
2639 ret
= master
->ops
->request_ibi(dev
, req
);
2648 void i3c_dev_free_ibi_locked(struct i3c_dev_desc
*dev
)
2650 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
2655 if (WARN_ON(dev
->ibi
->enabled
))
2656 WARN_ON(i3c_dev_disable_ibi_locked(dev
));
2658 master
->ops
->free_ibi(dev
);
2663 static int __init
i3c_init(void)
2665 return bus_register(&i3c_bus_type
);
2667 subsys_initcall(i3c_init
);
2669 static void __exit
i3c_exit(void)
2671 idr_destroy(&i3c_bus_idr
);
2672 bus_unregister(&i3c_bus_type
);
2674 module_exit(i3c_exit
);
2676 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
2677 MODULE_DESCRIPTION("I3C core");
2678 MODULE_LICENSE("GPL v2");