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 struct attribute
*i3c_device_attrs
[] = {
248 &dev_attr_dynamic_address
.attr
,
249 &dev_attr_hdrcap
.attr
,
252 ATTRIBUTE_GROUPS(i3c_device
);
254 static int i3c_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
256 struct i3c_device
*i3cdev
= dev_to_i3cdev(dev
);
257 struct i3c_device_info devinfo
;
258 u16 manuf
, part
, ext
;
260 i3c_device_get_info(i3cdev
, &devinfo
);
261 manuf
= I3C_PID_MANUF_ID(devinfo
.pid
);
262 part
= I3C_PID_PART_ID(devinfo
.pid
);
263 ext
= I3C_PID_EXTRA_INFO(devinfo
.pid
);
265 if (I3C_PID_RND_LOWER_32BITS(devinfo
.pid
))
266 return add_uevent_var(env
, "MODALIAS=i3c:dcr%02Xmanuf%04X",
269 return add_uevent_var(env
,
270 "MODALIAS=i3c:dcr%02Xmanuf%04Xpart%04xext%04x",
271 devinfo
.dcr
, manuf
, part
, ext
);
274 static const struct device_type i3c_device_type
= {
275 .groups
= i3c_device_groups
,
276 .uevent
= i3c_device_uevent
,
279 static int i3c_device_match(struct device
*dev
, struct device_driver
*drv
)
281 struct i3c_device
*i3cdev
;
282 struct i3c_driver
*i3cdrv
;
284 if (dev
->type
!= &i3c_device_type
)
287 i3cdev
= dev_to_i3cdev(dev
);
288 i3cdrv
= drv_to_i3cdrv(drv
);
289 if (i3c_device_match_id(i3cdev
, i3cdrv
->id_table
))
295 static int i3c_device_probe(struct device
*dev
)
297 struct i3c_device
*i3cdev
= dev_to_i3cdev(dev
);
298 struct i3c_driver
*driver
= drv_to_i3cdrv(dev
->driver
);
300 return driver
->probe(i3cdev
);
303 static int i3c_device_remove(struct device
*dev
)
305 struct i3c_device
*i3cdev
= dev_to_i3cdev(dev
);
306 struct i3c_driver
*driver
= drv_to_i3cdrv(dev
->driver
);
309 ret
= driver
->remove(i3cdev
);
313 i3c_device_free_ibi(i3cdev
);
318 struct bus_type i3c_bus_type
= {
320 .match
= i3c_device_match
,
321 .probe
= i3c_device_probe
,
322 .remove
= i3c_device_remove
,
325 static enum i3c_addr_slot_status
326 i3c_bus_get_addr_slot_status(struct i3c_bus
*bus
, u16 addr
)
328 int status
, bitpos
= addr
* 2;
330 if (addr
> I2C_MAX_ADDR
)
331 return I3C_ADDR_SLOT_RSVD
;
333 status
= bus
->addrslots
[bitpos
/ BITS_PER_LONG
];
334 status
>>= bitpos
% BITS_PER_LONG
;
336 return status
& I3C_ADDR_SLOT_STATUS_MASK
;
339 static void i3c_bus_set_addr_slot_status(struct i3c_bus
*bus
, u16 addr
,
340 enum i3c_addr_slot_status status
)
342 int bitpos
= addr
* 2;
345 if (addr
> I2C_MAX_ADDR
)
348 ptr
= bus
->addrslots
+ (bitpos
/ BITS_PER_LONG
);
349 *ptr
&= ~((unsigned long)I3C_ADDR_SLOT_STATUS_MASK
<<
350 (bitpos
% BITS_PER_LONG
));
351 *ptr
|= (unsigned long)status
<< (bitpos
% BITS_PER_LONG
);
354 static bool i3c_bus_dev_addr_is_avail(struct i3c_bus
*bus
, u8 addr
)
356 enum i3c_addr_slot_status status
;
358 status
= i3c_bus_get_addr_slot_status(bus
, addr
);
360 return status
== I3C_ADDR_SLOT_FREE
;
363 static int i3c_bus_get_free_addr(struct i3c_bus
*bus
, u8 start_addr
)
365 enum i3c_addr_slot_status status
;
368 for (addr
= start_addr
; addr
< I3C_MAX_ADDR
; addr
++) {
369 status
= i3c_bus_get_addr_slot_status(bus
, addr
);
370 if (status
== I3C_ADDR_SLOT_FREE
)
377 static void i3c_bus_init_addrslots(struct i3c_bus
*bus
)
381 /* Addresses 0 to 7 are reserved. */
382 for (i
= 0; i
< 8; i
++)
383 i3c_bus_set_addr_slot_status(bus
, i
, I3C_ADDR_SLOT_RSVD
);
386 * Reserve broadcast address and all addresses that might collide
387 * with the broadcast address when facing a single bit error.
389 i3c_bus_set_addr_slot_status(bus
, I3C_BROADCAST_ADDR
,
391 for (i
= 0; i
< 7; i
++)
392 i3c_bus_set_addr_slot_status(bus
, I3C_BROADCAST_ADDR
^ BIT(i
),
396 static void i3c_bus_cleanup(struct i3c_bus
*i3cbus
)
398 mutex_lock(&i3c_core_lock
);
399 idr_remove(&i3c_bus_idr
, i3cbus
->id
);
400 mutex_unlock(&i3c_core_lock
);
403 static int i3c_bus_init(struct i3c_bus
*i3cbus
)
407 init_rwsem(&i3cbus
->lock
);
408 INIT_LIST_HEAD(&i3cbus
->devs
.i2c
);
409 INIT_LIST_HEAD(&i3cbus
->devs
.i3c
);
410 i3c_bus_init_addrslots(i3cbus
);
411 i3cbus
->mode
= I3C_BUS_MODE_PURE
;
413 mutex_lock(&i3c_core_lock
);
414 ret
= idr_alloc(&i3c_bus_idr
, i3cbus
, 0, 0, GFP_KERNEL
);
415 mutex_unlock(&i3c_core_lock
);
425 static const char * const i3c_bus_mode_strings
[] = {
426 [I3C_BUS_MODE_PURE
] = "pure",
427 [I3C_BUS_MODE_MIXED_FAST
] = "mixed-fast",
428 [I3C_BUS_MODE_MIXED_LIMITED
] = "mixed-limited",
429 [I3C_BUS_MODE_MIXED_SLOW
] = "mixed-slow",
432 static ssize_t
mode_show(struct device
*dev
,
433 struct device_attribute
*da
,
436 struct i3c_bus
*i3cbus
= dev_to_i3cbus(dev
);
439 i3c_bus_normaluse_lock(i3cbus
);
440 if (i3cbus
->mode
< 0 ||
441 i3cbus
->mode
>= ARRAY_SIZE(i3c_bus_mode_strings
) ||
442 !i3c_bus_mode_strings
[i3cbus
->mode
])
443 ret
= sprintf(buf
, "unknown\n");
445 ret
= sprintf(buf
, "%s\n", i3c_bus_mode_strings
[i3cbus
->mode
]);
446 i3c_bus_normaluse_unlock(i3cbus
);
450 static DEVICE_ATTR_RO(mode
);
452 static ssize_t
current_master_show(struct device
*dev
,
453 struct device_attribute
*da
,
456 struct i3c_bus
*i3cbus
= dev_to_i3cbus(dev
);
459 i3c_bus_normaluse_lock(i3cbus
);
460 ret
= sprintf(buf
, "%d-%llx\n", i3cbus
->id
,
461 i3cbus
->cur_master
->info
.pid
);
462 i3c_bus_normaluse_unlock(i3cbus
);
466 static DEVICE_ATTR_RO(current_master
);
468 static ssize_t
i3c_scl_frequency_show(struct device
*dev
,
469 struct device_attribute
*da
,
472 struct i3c_bus
*i3cbus
= dev_to_i3cbus(dev
);
475 i3c_bus_normaluse_lock(i3cbus
);
476 ret
= sprintf(buf
, "%ld\n", i3cbus
->scl_rate
.i3c
);
477 i3c_bus_normaluse_unlock(i3cbus
);
481 static DEVICE_ATTR_RO(i3c_scl_frequency
);
483 static ssize_t
i2c_scl_frequency_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
, "%ld\n", i3cbus
->scl_rate
.i2c
);
492 i3c_bus_normaluse_unlock(i3cbus
);
496 static DEVICE_ATTR_RO(i2c_scl_frequency
);
498 static struct attribute
*i3c_masterdev_attrs
[] = {
500 &dev_attr_current_master
.attr
,
501 &dev_attr_i3c_scl_frequency
.attr
,
502 &dev_attr_i2c_scl_frequency
.attr
,
506 &dev_attr_dynamic_address
.attr
,
507 &dev_attr_hdrcap
.attr
,
510 ATTRIBUTE_GROUPS(i3c_masterdev
);
512 static void i3c_masterdev_release(struct device
*dev
)
514 struct i3c_master_controller
*master
= dev_to_i3cmaster(dev
);
515 struct i3c_bus
*bus
= dev_to_i3cbus(dev
);
518 destroy_workqueue(master
->wq
);
520 WARN_ON(!list_empty(&bus
->devs
.i2c
) || !list_empty(&bus
->devs
.i3c
));
521 i3c_bus_cleanup(bus
);
523 of_node_put(dev
->of_node
);
526 static const struct device_type i3c_masterdev_type
= {
527 .groups
= i3c_masterdev_groups
,
530 static int i3c_bus_set_mode(struct i3c_bus
*i3cbus
, enum i3c_bus_mode mode
,
531 unsigned long max_i2c_scl_rate
)
533 struct i3c_master_controller
*master
= i3c_bus_to_i3c_master(i3cbus
);
537 switch (i3cbus
->mode
) {
538 case I3C_BUS_MODE_PURE
:
539 if (!i3cbus
->scl_rate
.i3c
)
540 i3cbus
->scl_rate
.i3c
= I3C_BUS_TYP_I3C_SCL_RATE
;
542 case I3C_BUS_MODE_MIXED_FAST
:
543 case I3C_BUS_MODE_MIXED_LIMITED
:
544 if (!i3cbus
->scl_rate
.i3c
)
545 i3cbus
->scl_rate
.i3c
= I3C_BUS_TYP_I3C_SCL_RATE
;
546 if (!i3cbus
->scl_rate
.i2c
)
547 i3cbus
->scl_rate
.i2c
= max_i2c_scl_rate
;
549 case I3C_BUS_MODE_MIXED_SLOW
:
550 if (!i3cbus
->scl_rate
.i2c
)
551 i3cbus
->scl_rate
.i2c
= max_i2c_scl_rate
;
552 if (!i3cbus
->scl_rate
.i3c
||
553 i3cbus
->scl_rate
.i3c
> i3cbus
->scl_rate
.i2c
)
554 i3cbus
->scl_rate
.i3c
= i3cbus
->scl_rate
.i2c
;
560 dev_dbg(&master
->dev
, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n",
561 i3cbus
->scl_rate
.i2c
, i3cbus
->scl_rate
.i3c
);
564 * I3C/I2C frequency may have been overridden, check that user-provided
565 * values are not exceeding max possible frequency.
567 if (i3cbus
->scl_rate
.i3c
> I3C_BUS_MAX_I3C_SCL_RATE
||
568 i3cbus
->scl_rate
.i2c
> I3C_BUS_I2C_FM_PLUS_SCL_RATE
)
574 static struct i3c_master_controller
*
575 i2c_adapter_to_i3c_master(struct i2c_adapter
*adap
)
577 return container_of(adap
, struct i3c_master_controller
, i2c
);
580 static struct i2c_adapter
*
581 i3c_master_to_i2c_adapter(struct i3c_master_controller
*master
)
586 static void i3c_master_free_i2c_dev(struct i2c_dev_desc
*dev
)
591 static struct i2c_dev_desc
*
592 i3c_master_alloc_i2c_dev(struct i3c_master_controller
*master
,
593 const struct i2c_dev_boardinfo
*boardinfo
)
595 struct i2c_dev_desc
*dev
;
597 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
599 return ERR_PTR(-ENOMEM
);
601 dev
->common
.master
= master
;
602 dev
->boardinfo
= boardinfo
;
603 dev
->addr
= boardinfo
->base
.addr
;
604 dev
->lvr
= boardinfo
->lvr
;
609 static void *i3c_ccc_cmd_dest_init(struct i3c_ccc_cmd_dest
*dest
, u8 addr
,
613 dest
->payload
.len
= payloadlen
;
615 dest
->payload
.data
= kzalloc(payloadlen
, GFP_KERNEL
);
617 dest
->payload
.data
= NULL
;
619 return dest
->payload
.data
;
622 static void i3c_ccc_cmd_dest_cleanup(struct i3c_ccc_cmd_dest
*dest
)
624 kfree(dest
->payload
.data
);
627 static void i3c_ccc_cmd_init(struct i3c_ccc_cmd
*cmd
, bool rnw
, u8 id
,
628 struct i3c_ccc_cmd_dest
*dests
,
631 cmd
->rnw
= rnw
? 1 : 0;
634 cmd
->ndests
= ndests
;
635 cmd
->err
= I3C_ERROR_UNKNOWN
;
638 static int i3c_master_send_ccc_cmd_locked(struct i3c_master_controller
*master
,
639 struct i3c_ccc_cmd
*cmd
)
646 if (WARN_ON(master
->init_done
&&
647 !rwsem_is_locked(&master
->bus
.lock
)))
650 if (!master
->ops
->send_ccc_cmd
)
653 if ((cmd
->id
& I3C_CCC_DIRECT
) && (!cmd
->dests
|| !cmd
->ndests
))
656 if (master
->ops
->supports_ccc_cmd
&&
657 !master
->ops
->supports_ccc_cmd(master
, cmd
))
660 ret
= master
->ops
->send_ccc_cmd(master
, cmd
);
662 if (cmd
->err
!= I3C_ERROR_UNKNOWN
)
671 static struct i2c_dev_desc
*
672 i3c_master_find_i2c_dev_by_addr(const struct i3c_master_controller
*master
,
675 struct i2c_dev_desc
*dev
;
677 i3c_bus_for_each_i2cdev(&master
->bus
, dev
) {
678 if (dev
->boardinfo
->base
.addr
== addr
)
686 * i3c_master_get_free_addr() - get a free address on the bus
687 * @master: I3C master object
688 * @start_addr: where to start searching
690 * This function must be called with the bus lock held in write mode.
692 * Return: the first free address starting at @start_addr (included) or -ENOMEM
693 * if there's no more address available.
695 int i3c_master_get_free_addr(struct i3c_master_controller
*master
,
698 return i3c_bus_get_free_addr(&master
->bus
, start_addr
);
700 EXPORT_SYMBOL_GPL(i3c_master_get_free_addr
);
702 static void i3c_device_release(struct device
*dev
)
704 struct i3c_device
*i3cdev
= dev_to_i3cdev(dev
);
706 WARN_ON(i3cdev
->desc
);
708 of_node_put(i3cdev
->dev
.of_node
);
712 static void i3c_master_free_i3c_dev(struct i3c_dev_desc
*dev
)
717 static struct i3c_dev_desc
*
718 i3c_master_alloc_i3c_dev(struct i3c_master_controller
*master
,
719 const struct i3c_device_info
*info
)
721 struct i3c_dev_desc
*dev
;
723 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
725 return ERR_PTR(-ENOMEM
);
727 dev
->common
.master
= master
;
729 mutex_init(&dev
->ibi_lock
);
734 static int i3c_master_rstdaa_locked(struct i3c_master_controller
*master
,
737 enum i3c_addr_slot_status addrstat
;
738 struct i3c_ccc_cmd_dest dest
;
739 struct i3c_ccc_cmd cmd
;
745 addrstat
= i3c_bus_get_addr_slot_status(&master
->bus
, addr
);
746 if (addr
!= I3C_BROADCAST_ADDR
&& addrstat
!= I3C_ADDR_SLOT_I3C_DEV
)
749 i3c_ccc_cmd_dest_init(&dest
, addr
, 0);
750 i3c_ccc_cmd_init(&cmd
, false,
751 I3C_CCC_RSTDAA(addr
== I3C_BROADCAST_ADDR
),
753 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
754 i3c_ccc_cmd_dest_cleanup(&dest
);
760 * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment)
762 * @master: master used to send frames on the bus
764 * Send a ENTDAA CCC command to start a DAA procedure.
766 * Note that this function only sends the ENTDAA CCC command, all the logic
767 * behind dynamic address assignment has to be handled in the I3C master
770 * This function must be called with the bus lock held in write mode.
772 * Return: 0 in case of success, a positive I3C error code if the error is
773 * one of the official Mx error codes, and a negative error code otherwise.
775 int i3c_master_entdaa_locked(struct i3c_master_controller
*master
)
777 struct i3c_ccc_cmd_dest dest
;
778 struct i3c_ccc_cmd cmd
;
781 i3c_ccc_cmd_dest_init(&dest
, I3C_BROADCAST_ADDR
, 0);
782 i3c_ccc_cmd_init(&cmd
, false, I3C_CCC_ENTDAA
, &dest
, 1);
783 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
784 i3c_ccc_cmd_dest_cleanup(&dest
);
788 EXPORT_SYMBOL_GPL(i3c_master_entdaa_locked
);
790 static int i3c_master_enec_disec_locked(struct i3c_master_controller
*master
,
791 u8 addr
, bool enable
, u8 evts
)
793 struct i3c_ccc_events
*events
;
794 struct i3c_ccc_cmd_dest dest
;
795 struct i3c_ccc_cmd cmd
;
798 events
= i3c_ccc_cmd_dest_init(&dest
, addr
, sizeof(*events
));
802 events
->events
= evts
;
803 i3c_ccc_cmd_init(&cmd
, false,
805 I3C_CCC_ENEC(addr
== I3C_BROADCAST_ADDR
) :
806 I3C_CCC_DISEC(addr
== I3C_BROADCAST_ADDR
),
808 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
809 i3c_ccc_cmd_dest_cleanup(&dest
);
815 * i3c_master_disec_locked() - send a DISEC CCC command
816 * @master: master used to send frames on the bus
817 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
818 * @evts: events to disable
820 * Send a DISEC CCC command to disable some or all events coming from a
821 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
823 * This function must be called with the bus lock held in write mode.
825 * Return: 0 in case of success, a positive I3C error code if the error is
826 * one of the official Mx error codes, and a negative error code otherwise.
828 int i3c_master_disec_locked(struct i3c_master_controller
*master
, u8 addr
,
831 return i3c_master_enec_disec_locked(master
, addr
, false, evts
);
833 EXPORT_SYMBOL_GPL(i3c_master_disec_locked
);
836 * i3c_master_enec_locked() - send an ENEC CCC command
837 * @master: master used to send frames on the bus
838 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
839 * @evts: events to disable
841 * Sends an ENEC CCC command to enable some or all events coming from a
842 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
844 * This function must be called with the bus lock held in write mode.
846 * Return: 0 in case of success, a positive I3C error code if the error is
847 * one of the official Mx error codes, and a negative error code otherwise.
849 int i3c_master_enec_locked(struct i3c_master_controller
*master
, u8 addr
,
852 return i3c_master_enec_disec_locked(master
, addr
, true, evts
);
854 EXPORT_SYMBOL_GPL(i3c_master_enec_locked
);
857 * i3c_master_defslvs_locked() - send a DEFSLVS CCC command
858 * @master: master used to send frames on the bus
860 * Send a DEFSLVS CCC command containing all the devices known to the @master.
861 * This is useful when you have secondary masters on the bus to propagate
862 * device information.
864 * This should be called after all I3C devices have been discovered (in other
865 * words, after the DAA procedure has finished) and instantiated in
866 * &i3c_master_controller_ops->bus_init().
867 * It should also be called if a master ACKed an Hot-Join request and assigned
868 * a dynamic address to the device joining the bus.
870 * This function must be called with the bus lock held in write mode.
872 * Return: 0 in case of success, a positive I3C error code if the error is
873 * one of the official Mx error codes, and a negative error code otherwise.
875 int i3c_master_defslvs_locked(struct i3c_master_controller
*master
)
877 struct i3c_ccc_defslvs
*defslvs
;
878 struct i3c_ccc_dev_desc
*desc
;
879 struct i3c_ccc_cmd_dest dest
;
880 struct i3c_dev_desc
*i3cdev
;
881 struct i2c_dev_desc
*i2cdev
;
882 struct i3c_ccc_cmd cmd
;
890 bus
= i3c_master_get_bus(master
);
891 i3c_bus_for_each_i3cdev(bus
, i3cdev
) {
894 if (i3cdev
== master
->this)
897 if (I3C_BCR_DEVICE_ROLE(i3cdev
->info
.bcr
) ==
902 /* No other master on the bus, skip DEFSLVS. */
906 i3c_bus_for_each_i2cdev(bus
, i2cdev
)
909 defslvs
= i3c_ccc_cmd_dest_init(&dest
, I3C_BROADCAST_ADDR
,
910 struct_size(defslvs
, slaves
,
915 defslvs
->count
= ndevs
;
916 defslvs
->master
.bcr
= master
->this->info
.bcr
;
917 defslvs
->master
.dcr
= master
->this->info
.dcr
;
918 defslvs
->master
.dyn_addr
= master
->this->info
.dyn_addr
<< 1;
919 defslvs
->master
.static_addr
= I3C_BROADCAST_ADDR
<< 1;
921 desc
= defslvs
->slaves
;
922 i3c_bus_for_each_i2cdev(bus
, i2cdev
) {
923 desc
->lvr
= i2cdev
->lvr
;
924 desc
->static_addr
= i2cdev
->addr
<< 1;
928 i3c_bus_for_each_i3cdev(bus
, i3cdev
) {
929 /* Skip the I3C dev representing this master. */
930 if (i3cdev
== master
->this)
933 desc
->bcr
= i3cdev
->info
.bcr
;
934 desc
->dcr
= i3cdev
->info
.dcr
;
935 desc
->dyn_addr
= i3cdev
->info
.dyn_addr
<< 1;
936 desc
->static_addr
= i3cdev
->info
.static_addr
<< 1;
940 i3c_ccc_cmd_init(&cmd
, false, I3C_CCC_DEFSLVS
, &dest
, 1);
941 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
942 i3c_ccc_cmd_dest_cleanup(&dest
);
946 EXPORT_SYMBOL_GPL(i3c_master_defslvs_locked
);
948 static int i3c_master_setda_locked(struct i3c_master_controller
*master
,
949 u8 oldaddr
, u8 newaddr
, bool setdasa
)
951 struct i3c_ccc_cmd_dest dest
;
952 struct i3c_ccc_setda
*setda
;
953 struct i3c_ccc_cmd cmd
;
956 if (!oldaddr
|| !newaddr
)
959 setda
= i3c_ccc_cmd_dest_init(&dest
, oldaddr
, sizeof(*setda
));
963 setda
->addr
= newaddr
<< 1;
964 i3c_ccc_cmd_init(&cmd
, false,
965 setdasa
? I3C_CCC_SETDASA
: I3C_CCC_SETNEWDA
,
967 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
968 i3c_ccc_cmd_dest_cleanup(&dest
);
973 static int i3c_master_setdasa_locked(struct i3c_master_controller
*master
,
974 u8 static_addr
, u8 dyn_addr
)
976 return i3c_master_setda_locked(master
, static_addr
, dyn_addr
, true);
979 static int i3c_master_setnewda_locked(struct i3c_master_controller
*master
,
980 u8 oldaddr
, u8 newaddr
)
982 return i3c_master_setda_locked(master
, oldaddr
, newaddr
, false);
985 static int i3c_master_getmrl_locked(struct i3c_master_controller
*master
,
986 struct i3c_device_info
*info
)
988 struct i3c_ccc_cmd_dest dest
;
989 unsigned int expected_len
;
990 struct i3c_ccc_mrl
*mrl
;
991 struct i3c_ccc_cmd cmd
;
994 mrl
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*mrl
));
999 * When the device does not have IBI payload GETMRL only returns 2
1002 if (!(info
->bcr
& I3C_BCR_IBI_PAYLOAD
))
1003 dest
.payload
.len
-= 1;
1005 expected_len
= dest
.payload
.len
;
1006 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETMRL
, &dest
, 1);
1007 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1011 if (dest
.payload
.len
!= expected_len
) {
1016 info
->max_read_len
= be16_to_cpu(mrl
->read_len
);
1018 if (info
->bcr
& I3C_BCR_IBI_PAYLOAD
)
1019 info
->max_ibi_len
= mrl
->ibi_len
;
1022 i3c_ccc_cmd_dest_cleanup(&dest
);
1027 static int i3c_master_getmwl_locked(struct i3c_master_controller
*master
,
1028 struct i3c_device_info
*info
)
1030 struct i3c_ccc_cmd_dest dest
;
1031 struct i3c_ccc_mwl
*mwl
;
1032 struct i3c_ccc_cmd cmd
;
1035 mwl
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*mwl
));
1039 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETMWL
, &dest
, 1);
1040 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1044 if (dest
.payload
.len
!= sizeof(*mwl
)) {
1049 info
->max_write_len
= be16_to_cpu(mwl
->len
);
1052 i3c_ccc_cmd_dest_cleanup(&dest
);
1057 static int i3c_master_getmxds_locked(struct i3c_master_controller
*master
,
1058 struct i3c_device_info
*info
)
1060 struct i3c_ccc_getmxds
*getmaxds
;
1061 struct i3c_ccc_cmd_dest dest
;
1062 struct i3c_ccc_cmd cmd
;
1065 getmaxds
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
,
1070 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETMXDS
, &dest
, 1);
1071 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1075 if (dest
.payload
.len
!= 2 && dest
.payload
.len
!= 5) {
1080 info
->max_read_ds
= getmaxds
->maxrd
;
1081 info
->max_write_ds
= getmaxds
->maxwr
;
1082 if (dest
.payload
.len
== 5)
1083 info
->max_read_turnaround
= getmaxds
->maxrdturn
[0] |
1084 ((u32
)getmaxds
->maxrdturn
[1] << 8) |
1085 ((u32
)getmaxds
->maxrdturn
[2] << 16);
1088 i3c_ccc_cmd_dest_cleanup(&dest
);
1093 static int i3c_master_gethdrcap_locked(struct i3c_master_controller
*master
,
1094 struct i3c_device_info
*info
)
1096 struct i3c_ccc_gethdrcap
*gethdrcap
;
1097 struct i3c_ccc_cmd_dest dest
;
1098 struct i3c_ccc_cmd cmd
;
1101 gethdrcap
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
,
1102 sizeof(*gethdrcap
));
1106 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETHDRCAP
, &dest
, 1);
1107 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1111 if (dest
.payload
.len
!= 1) {
1116 info
->hdr_cap
= gethdrcap
->modes
;
1119 i3c_ccc_cmd_dest_cleanup(&dest
);
1124 static int i3c_master_getpid_locked(struct i3c_master_controller
*master
,
1125 struct i3c_device_info
*info
)
1127 struct i3c_ccc_getpid
*getpid
;
1128 struct i3c_ccc_cmd_dest dest
;
1129 struct i3c_ccc_cmd cmd
;
1132 getpid
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*getpid
));
1136 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETPID
, &dest
, 1);
1137 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1142 for (i
= 0; i
< sizeof(getpid
->pid
); i
++) {
1143 int sft
= (sizeof(getpid
->pid
) - i
- 1) * 8;
1145 info
->pid
|= (u64
)getpid
->pid
[i
] << sft
;
1149 i3c_ccc_cmd_dest_cleanup(&dest
);
1154 static int i3c_master_getbcr_locked(struct i3c_master_controller
*master
,
1155 struct i3c_device_info
*info
)
1157 struct i3c_ccc_getbcr
*getbcr
;
1158 struct i3c_ccc_cmd_dest dest
;
1159 struct i3c_ccc_cmd cmd
;
1162 getbcr
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*getbcr
));
1166 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETBCR
, &dest
, 1);
1167 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1171 info
->bcr
= getbcr
->bcr
;
1174 i3c_ccc_cmd_dest_cleanup(&dest
);
1179 static int i3c_master_getdcr_locked(struct i3c_master_controller
*master
,
1180 struct i3c_device_info
*info
)
1182 struct i3c_ccc_getdcr
*getdcr
;
1183 struct i3c_ccc_cmd_dest dest
;
1184 struct i3c_ccc_cmd cmd
;
1187 getdcr
= i3c_ccc_cmd_dest_init(&dest
, info
->dyn_addr
, sizeof(*getdcr
));
1191 i3c_ccc_cmd_init(&cmd
, true, I3C_CCC_GETDCR
, &dest
, 1);
1192 ret
= i3c_master_send_ccc_cmd_locked(master
, &cmd
);
1196 info
->dcr
= getdcr
->dcr
;
1199 i3c_ccc_cmd_dest_cleanup(&dest
);
1204 static int i3c_master_retrieve_dev_info(struct i3c_dev_desc
*dev
)
1206 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1207 enum i3c_addr_slot_status slot_status
;
1210 if (!dev
->info
.dyn_addr
)
1213 slot_status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1214 dev
->info
.dyn_addr
);
1215 if (slot_status
== I3C_ADDR_SLOT_RSVD
||
1216 slot_status
== I3C_ADDR_SLOT_I2C_DEV
)
1219 ret
= i3c_master_getpid_locked(master
, &dev
->info
);
1223 ret
= i3c_master_getbcr_locked(master
, &dev
->info
);
1227 ret
= i3c_master_getdcr_locked(master
, &dev
->info
);
1231 if (dev
->info
.bcr
& I3C_BCR_MAX_DATA_SPEED_LIM
) {
1232 ret
= i3c_master_getmxds_locked(master
, &dev
->info
);
1237 if (dev
->info
.bcr
& I3C_BCR_IBI_PAYLOAD
)
1238 dev
->info
.max_ibi_len
= 1;
1240 i3c_master_getmrl_locked(master
, &dev
->info
);
1241 i3c_master_getmwl_locked(master
, &dev
->info
);
1243 if (dev
->info
.bcr
& I3C_BCR_HDR_CAP
) {
1244 ret
= i3c_master_gethdrcap_locked(master
, &dev
->info
);
1252 static void i3c_master_put_i3c_addrs(struct i3c_dev_desc
*dev
)
1254 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1256 if (dev
->info
.static_addr
)
1257 i3c_bus_set_addr_slot_status(&master
->bus
,
1258 dev
->info
.static_addr
,
1259 I3C_ADDR_SLOT_FREE
);
1261 if (dev
->info
.dyn_addr
)
1262 i3c_bus_set_addr_slot_status(&master
->bus
, dev
->info
.dyn_addr
,
1263 I3C_ADDR_SLOT_FREE
);
1265 if (dev
->boardinfo
&& dev
->boardinfo
->init_dyn_addr
)
1266 i3c_bus_set_addr_slot_status(&master
->bus
, dev
->info
.dyn_addr
,
1267 I3C_ADDR_SLOT_FREE
);
1270 static int i3c_master_get_i3c_addrs(struct i3c_dev_desc
*dev
)
1272 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1273 enum i3c_addr_slot_status status
;
1275 if (!dev
->info
.static_addr
&& !dev
->info
.dyn_addr
)
1278 if (dev
->info
.static_addr
) {
1279 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1280 dev
->info
.static_addr
);
1281 if (status
!= I3C_ADDR_SLOT_FREE
)
1284 i3c_bus_set_addr_slot_status(&master
->bus
,
1285 dev
->info
.static_addr
,
1286 I3C_ADDR_SLOT_I3C_DEV
);
1290 * ->init_dyn_addr should have been reserved before that, so, if we're
1291 * trying to apply a pre-reserved dynamic address, we should not try
1292 * to reserve the address slot a second time.
1294 if (dev
->info
.dyn_addr
&&
1296 dev
->boardinfo
->init_dyn_addr
!= dev
->info
.dyn_addr
)) {
1297 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1298 dev
->info
.dyn_addr
);
1299 if (status
!= I3C_ADDR_SLOT_FREE
)
1300 goto err_release_static_addr
;
1302 i3c_bus_set_addr_slot_status(&master
->bus
, dev
->info
.dyn_addr
,
1303 I3C_ADDR_SLOT_I3C_DEV
);
1308 err_release_static_addr
:
1309 if (dev
->info
.static_addr
)
1310 i3c_bus_set_addr_slot_status(&master
->bus
,
1311 dev
->info
.static_addr
,
1312 I3C_ADDR_SLOT_FREE
);
1317 static int i3c_master_attach_i3c_dev(struct i3c_master_controller
*master
,
1318 struct i3c_dev_desc
*dev
)
1323 * We don't attach devices to the controller until they are
1324 * addressable on the bus.
1326 if (!dev
->info
.static_addr
&& !dev
->info
.dyn_addr
)
1329 ret
= i3c_master_get_i3c_addrs(dev
);
1333 /* Do not attach the master device itself. */
1334 if (master
->this != dev
&& master
->ops
->attach_i3c_dev
) {
1335 ret
= master
->ops
->attach_i3c_dev(dev
);
1337 i3c_master_put_i3c_addrs(dev
);
1342 list_add_tail(&dev
->common
.node
, &master
->bus
.devs
.i3c
);
1347 static int i3c_master_reattach_i3c_dev(struct i3c_dev_desc
*dev
,
1350 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1351 enum i3c_addr_slot_status status
;
1354 if (dev
->info
.dyn_addr
!= old_dyn_addr
) {
1355 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1356 dev
->info
.dyn_addr
);
1357 if (status
!= I3C_ADDR_SLOT_FREE
)
1359 i3c_bus_set_addr_slot_status(&master
->bus
,
1361 I3C_ADDR_SLOT_I3C_DEV
);
1364 if (master
->ops
->reattach_i3c_dev
) {
1365 ret
= master
->ops
->reattach_i3c_dev(dev
, old_dyn_addr
);
1367 i3c_master_put_i3c_addrs(dev
);
1375 static void i3c_master_detach_i3c_dev(struct i3c_dev_desc
*dev
)
1377 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1379 /* Do not detach the master device itself. */
1380 if (master
->this != dev
&& master
->ops
->detach_i3c_dev
)
1381 master
->ops
->detach_i3c_dev(dev
);
1383 i3c_master_put_i3c_addrs(dev
);
1384 list_del(&dev
->common
.node
);
1387 static int i3c_master_attach_i2c_dev(struct i3c_master_controller
*master
,
1388 struct i2c_dev_desc
*dev
)
1392 if (master
->ops
->attach_i2c_dev
) {
1393 ret
= master
->ops
->attach_i2c_dev(dev
);
1398 list_add_tail(&dev
->common
.node
, &master
->bus
.devs
.i2c
);
1403 static void i3c_master_detach_i2c_dev(struct i2c_dev_desc
*dev
)
1405 struct i3c_master_controller
*master
= i2c_dev_get_master(dev
);
1407 list_del(&dev
->common
.node
);
1409 if (master
->ops
->detach_i2c_dev
)
1410 master
->ops
->detach_i2c_dev(dev
);
1413 static void i3c_master_pre_assign_dyn_addr(struct i3c_dev_desc
*dev
)
1415 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
1418 if (!dev
->boardinfo
|| !dev
->boardinfo
->init_dyn_addr
||
1419 !dev
->boardinfo
->static_addr
)
1422 ret
= i3c_master_setdasa_locked(master
, dev
->info
.static_addr
,
1423 dev
->boardinfo
->init_dyn_addr
);
1427 dev
->info
.dyn_addr
= dev
->boardinfo
->init_dyn_addr
;
1428 ret
= i3c_master_reattach_i3c_dev(dev
, 0);
1432 ret
= i3c_master_retrieve_dev_info(dev
);
1439 i3c_master_rstdaa_locked(master
, dev
->boardinfo
->init_dyn_addr
);
1443 i3c_master_register_new_i3c_devs(struct i3c_master_controller
*master
)
1445 struct i3c_dev_desc
*desc
;
1448 if (!master
->init_done
)
1451 i3c_bus_for_each_i3cdev(&master
->bus
, desc
) {
1452 if (desc
->dev
|| !desc
->info
.dyn_addr
|| desc
== master
->this)
1455 desc
->dev
= kzalloc(sizeof(*desc
->dev
), GFP_KERNEL
);
1459 desc
->dev
->bus
= &master
->bus
;
1460 desc
->dev
->desc
= desc
;
1461 desc
->dev
->dev
.parent
= &master
->dev
;
1462 desc
->dev
->dev
.type
= &i3c_device_type
;
1463 desc
->dev
->dev
.bus
= &i3c_bus_type
;
1464 desc
->dev
->dev
.release
= i3c_device_release
;
1465 dev_set_name(&desc
->dev
->dev
, "%d-%llx", master
->bus
.id
,
1468 if (desc
->boardinfo
)
1469 desc
->dev
->dev
.of_node
= desc
->boardinfo
->of_node
;
1471 ret
= device_register(&desc
->dev
->dev
);
1473 dev_err(&master
->dev
,
1474 "Failed to add I3C device (err = %d)\n", ret
);
1479 * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment)
1480 * @master: master doing the DAA
1482 * This function is instantiating an I3C device object and adding it to the
1483 * I3C device list. All device information are automatically retrieved using
1484 * standard CCC commands.
1486 * The I3C device object is returned in case the master wants to attach
1487 * private data to it using i3c_dev_set_master_data().
1489 * This function must be called with the bus lock held in write mode.
1491 * Return: a 0 in case of success, an negative error code otherwise.
1493 int i3c_master_do_daa(struct i3c_master_controller
*master
)
1497 i3c_bus_maintenance_lock(&master
->bus
);
1498 ret
= master
->ops
->do_daa(master
);
1499 i3c_bus_maintenance_unlock(&master
->bus
);
1504 i3c_bus_normaluse_lock(&master
->bus
);
1505 i3c_master_register_new_i3c_devs(master
);
1506 i3c_bus_normaluse_unlock(&master
->bus
);
1510 EXPORT_SYMBOL_GPL(i3c_master_do_daa
);
1513 * i3c_master_set_info() - set master device information
1514 * @master: master used to send frames on the bus
1515 * @info: I3C device information
1517 * Set master device info. This should be called from
1518 * &i3c_master_controller_ops->bus_init().
1520 * Not all &i3c_device_info fields are meaningful for a master device.
1521 * Here is a list of fields that should be properly filled:
1523 * - &i3c_device_info->dyn_addr
1524 * - &i3c_device_info->bcr
1525 * - &i3c_device_info->dcr
1526 * - &i3c_device_info->pid
1527 * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in
1528 * &i3c_device_info->bcr
1530 * This function must be called with the bus lock held in maintenance mode.
1532 * Return: 0 if @info contains valid information (not every piece of
1533 * information can be checked, but we can at least make sure @info->dyn_addr
1534 * and @info->bcr are correct), -EINVAL otherwise.
1536 int i3c_master_set_info(struct i3c_master_controller
*master
,
1537 const struct i3c_device_info
*info
)
1539 struct i3c_dev_desc
*i3cdev
;
1542 if (!i3c_bus_dev_addr_is_avail(&master
->bus
, info
->dyn_addr
))
1545 if (I3C_BCR_DEVICE_ROLE(info
->bcr
) == I3C_BCR_I3C_MASTER
&&
1552 i3cdev
= i3c_master_alloc_i3c_dev(master
, info
);
1554 return PTR_ERR(i3cdev
);
1556 master
->this = i3cdev
;
1557 master
->bus
.cur_master
= master
->this;
1559 ret
= i3c_master_attach_i3c_dev(master
, i3cdev
);
1566 i3c_master_free_i3c_dev(i3cdev
);
1570 EXPORT_SYMBOL_GPL(i3c_master_set_info
);
1572 static void i3c_master_detach_free_devs(struct i3c_master_controller
*master
)
1574 struct i3c_dev_desc
*i3cdev
, *i3ctmp
;
1575 struct i2c_dev_desc
*i2cdev
, *i2ctmp
;
1577 list_for_each_entry_safe(i3cdev
, i3ctmp
, &master
->bus
.devs
.i3c
,
1579 i3c_master_detach_i3c_dev(i3cdev
);
1581 if (i3cdev
->boardinfo
&& i3cdev
->boardinfo
->init_dyn_addr
)
1582 i3c_bus_set_addr_slot_status(&master
->bus
,
1583 i3cdev
->boardinfo
->init_dyn_addr
,
1584 I3C_ADDR_SLOT_FREE
);
1586 i3c_master_free_i3c_dev(i3cdev
);
1589 list_for_each_entry_safe(i2cdev
, i2ctmp
, &master
->bus
.devs
.i2c
,
1591 i3c_master_detach_i2c_dev(i2cdev
);
1592 i3c_bus_set_addr_slot_status(&master
->bus
,
1594 I3C_ADDR_SLOT_FREE
);
1595 i3c_master_free_i2c_dev(i2cdev
);
1600 * i3c_master_bus_init() - initialize an I3C bus
1601 * @master: main master initializing the bus
1603 * This function is following all initialisation steps described in the I3C
1606 * 1. Attach I2C and statically defined I3C devs to the master so that the
1607 * master can fill its internal device table appropriately
1609 * 2. Call &i3c_master_controller_ops->bus_init() method to initialize
1610 * the master controller. That's usually where the bus mode is selected
1611 * (pure bus or mixed fast/slow bus)
1613 * 3. Instruct all devices on the bus to drop their dynamic address. This is
1614 * particularly important when the bus was previously configured by someone
1615 * else (for example the bootloader)
1617 * 4. Disable all slave events.
1619 * 5. Pre-assign dynamic addresses requested by the FW with SETDASA for I3C
1620 * devices that have a static address
1622 * 6. Do a DAA (Dynamic Address Assignment) to assign dynamic addresses to all
1623 * remaining I3C devices
1625 * Once this is done, all I3C and I2C devices should be usable.
1627 * Return: a 0 in case of success, an negative error code otherwise.
1629 static int i3c_master_bus_init(struct i3c_master_controller
*master
)
1631 enum i3c_addr_slot_status status
;
1632 struct i2c_dev_boardinfo
*i2cboardinfo
;
1633 struct i3c_dev_boardinfo
*i3cboardinfo
;
1634 struct i3c_dev_desc
*i3cdev
;
1635 struct i2c_dev_desc
*i2cdev
;
1639 * First attach all devices with static definitions provided by the
1642 list_for_each_entry(i2cboardinfo
, &master
->boardinfo
.i2c
, node
) {
1643 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1644 i2cboardinfo
->base
.addr
);
1645 if (status
!= I3C_ADDR_SLOT_FREE
) {
1647 goto err_detach_devs
;
1650 i3c_bus_set_addr_slot_status(&master
->bus
,
1651 i2cboardinfo
->base
.addr
,
1652 I3C_ADDR_SLOT_I2C_DEV
);
1654 i2cdev
= i3c_master_alloc_i2c_dev(master
, i2cboardinfo
);
1655 if (IS_ERR(i2cdev
)) {
1656 ret
= PTR_ERR(i2cdev
);
1657 goto err_detach_devs
;
1660 ret
= i3c_master_attach_i2c_dev(master
, i2cdev
);
1662 i3c_master_free_i2c_dev(i2cdev
);
1663 goto err_detach_devs
;
1666 list_for_each_entry(i3cboardinfo
, &master
->boardinfo
.i3c
, node
) {
1667 struct i3c_device_info info
= {
1668 .static_addr
= i3cboardinfo
->static_addr
,
1671 if (i3cboardinfo
->init_dyn_addr
) {
1672 status
= i3c_bus_get_addr_slot_status(&master
->bus
,
1673 i3cboardinfo
->init_dyn_addr
);
1674 if (status
!= I3C_ADDR_SLOT_FREE
) {
1676 goto err_detach_devs
;
1680 i3cdev
= i3c_master_alloc_i3c_dev(master
, &info
);
1681 if (IS_ERR(i3cdev
)) {
1682 ret
= PTR_ERR(i3cdev
);
1683 goto err_detach_devs
;
1686 i3cdev
->boardinfo
= i3cboardinfo
;
1688 ret
= i3c_master_attach_i3c_dev(master
, i3cdev
);
1690 i3c_master_free_i3c_dev(i3cdev
);
1691 goto err_detach_devs
;
1696 * Now execute the controller specific ->bus_init() routine, which
1697 * might configure its internal logic to match the bus limitations.
1699 ret
= master
->ops
->bus_init(master
);
1701 goto err_detach_devs
;
1704 * The master device should have been instantiated in ->bus_init(),
1705 * complain if this was not the case.
1707 if (!master
->this) {
1708 dev_err(&master
->dev
,
1709 "master_set_info() was not called in ->bus_init()\n");
1711 goto err_bus_cleanup
;
1715 * Reset all dynamic address that may have been assigned before
1716 * (assigned by the bootloader for example).
1718 ret
= i3c_master_rstdaa_locked(master
, I3C_BROADCAST_ADDR
);
1719 if (ret
&& ret
!= I3C_ERROR_M2
)
1720 goto err_bus_cleanup
;
1722 /* Disable all slave events before starting DAA. */
1723 ret
= i3c_master_disec_locked(master
, I3C_BROADCAST_ADDR
,
1724 I3C_CCC_EVENT_SIR
| I3C_CCC_EVENT_MR
|
1726 if (ret
&& ret
!= I3C_ERROR_M2
)
1727 goto err_bus_cleanup
;
1730 * Pre-assign dynamic address and retrieve device information if
1733 i3c_bus_for_each_i3cdev(&master
->bus
, i3cdev
)
1734 i3c_master_pre_assign_dyn_addr(i3cdev
);
1736 ret
= i3c_master_do_daa(master
);
1743 i3c_master_rstdaa_locked(master
, I3C_BROADCAST_ADDR
);
1746 if (master
->ops
->bus_cleanup
)
1747 master
->ops
->bus_cleanup(master
);
1750 i3c_master_detach_free_devs(master
);
1755 static void i3c_master_bus_cleanup(struct i3c_master_controller
*master
)
1757 if (master
->ops
->bus_cleanup
)
1758 master
->ops
->bus_cleanup(master
);
1760 i3c_master_detach_free_devs(master
);
1763 static struct i3c_dev_desc
*
1764 i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc
*refdev
)
1766 struct i3c_master_controller
*master
= i3c_dev_get_master(refdev
);
1767 struct i3c_dev_desc
*i3cdev
;
1769 i3c_bus_for_each_i3cdev(&master
->bus
, i3cdev
) {
1770 if (i3cdev
!= refdev
&& i3cdev
->info
.pid
== refdev
->info
.pid
)
1778 * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus
1779 * @master: master used to send frames on the bus
1780 * @addr: I3C slave dynamic address assigned to the device
1782 * This function is instantiating an I3C device object and adding it to the
1783 * I3C device list. All device information are automatically retrieved using
1784 * standard CCC commands.
1786 * The I3C device object is returned in case the master wants to attach
1787 * private data to it using i3c_dev_set_master_data().
1789 * This function must be called with the bus lock held in write mode.
1791 * Return: a 0 in case of success, an negative error code otherwise.
1793 int i3c_master_add_i3c_dev_locked(struct i3c_master_controller
*master
,
1796 struct i3c_device_info info
= { .dyn_addr
= addr
};
1797 struct i3c_dev_desc
*newdev
, *olddev
;
1798 u8 old_dyn_addr
= addr
, expected_dyn_addr
;
1799 struct i3c_ibi_setup ibireq
= { };
1800 bool enable_ibi
= false;
1806 newdev
= i3c_master_alloc_i3c_dev(master
, &info
);
1808 return PTR_ERR(newdev
);
1810 ret
= i3c_master_attach_i3c_dev(master
, newdev
);
1814 ret
= i3c_master_retrieve_dev_info(newdev
);
1816 goto err_detach_dev
;
1818 olddev
= i3c_master_search_i3c_dev_duplicate(newdev
);
1820 newdev
->boardinfo
= olddev
->boardinfo
;
1821 newdev
->info
.static_addr
= olddev
->info
.static_addr
;
1822 newdev
->dev
= olddev
->dev
;
1824 newdev
->dev
->desc
= newdev
;
1827 * We need to restore the IBI state too, so let's save the
1828 * IBI information and try to restore them after olddev has
1829 * been detached+released and its IBI has been stopped and
1830 * the associated resources have been freed.
1832 mutex_lock(&olddev
->ibi_lock
);
1834 ibireq
.handler
= olddev
->ibi
->handler
;
1835 ibireq
.max_payload_len
= olddev
->ibi
->max_payload_len
;
1836 ibireq
.num_slots
= olddev
->ibi
->num_slots
;
1838 if (olddev
->ibi
->enabled
) {
1840 i3c_dev_disable_ibi_locked(olddev
);
1843 i3c_dev_free_ibi_locked(olddev
);
1845 mutex_unlock(&olddev
->ibi_lock
);
1847 old_dyn_addr
= olddev
->info
.dyn_addr
;
1849 i3c_master_detach_i3c_dev(olddev
);
1850 i3c_master_free_i3c_dev(olddev
);
1853 ret
= i3c_master_reattach_i3c_dev(newdev
, old_dyn_addr
);
1855 goto err_detach_dev
;
1858 * Depending on our previous state, the expected dynamic address might
1860 * - if the device already had a dynamic address assigned, let's try to
1862 * - if the device did not have a dynamic address and the firmware
1863 * requested a specific address, pick this one
1864 * - in any other case, keep the address automatically assigned by the
1867 if (old_dyn_addr
&& old_dyn_addr
!= newdev
->info
.dyn_addr
)
1868 expected_dyn_addr
= old_dyn_addr
;
1869 else if (newdev
->boardinfo
&& newdev
->boardinfo
->init_dyn_addr
)
1870 expected_dyn_addr
= newdev
->boardinfo
->init_dyn_addr
;
1872 expected_dyn_addr
= newdev
->info
.dyn_addr
;
1874 if (newdev
->info
.dyn_addr
!= expected_dyn_addr
) {
1876 * Try to apply the expected dynamic address. If it fails, keep
1877 * the address assigned by the master.
1879 ret
= i3c_master_setnewda_locked(master
,
1880 newdev
->info
.dyn_addr
,
1883 old_dyn_addr
= newdev
->info
.dyn_addr
;
1884 newdev
->info
.dyn_addr
= expected_dyn_addr
;
1885 i3c_master_reattach_i3c_dev(newdev
, old_dyn_addr
);
1887 dev_err(&master
->dev
,
1888 "Failed to assign reserved/old address to device %d%llx",
1889 master
->bus
.id
, newdev
->info
.pid
);
1894 * Now is time to try to restore the IBI setup. If we're lucky,
1895 * everything works as before, otherwise, all we can do is complain.
1896 * FIXME: maybe we should add callback to inform the driver that it
1897 * should request the IBI again instead of trying to hide that from
1900 if (ibireq
.handler
) {
1901 mutex_lock(&newdev
->ibi_lock
);
1902 ret
= i3c_dev_request_ibi_locked(newdev
, &ibireq
);
1904 dev_err(&master
->dev
,
1905 "Failed to request IBI on device %d-%llx",
1906 master
->bus
.id
, newdev
->info
.pid
);
1907 } else if (enable_ibi
) {
1908 ret
= i3c_dev_enable_ibi_locked(newdev
);
1910 dev_err(&master
->dev
,
1911 "Failed to re-enable IBI on device %d-%llx",
1912 master
->bus
.id
, newdev
->info
.pid
);
1914 mutex_unlock(&newdev
->ibi_lock
);
1920 if (newdev
->dev
&& newdev
->dev
->desc
)
1921 newdev
->dev
->desc
= NULL
;
1923 i3c_master_detach_i3c_dev(newdev
);
1926 i3c_master_free_i3c_dev(newdev
);
1930 EXPORT_SYMBOL_GPL(i3c_master_add_i3c_dev_locked
);
1932 #define OF_I3C_REG1_IS_I2C_DEV BIT(31)
1935 of_i3c_master_add_i2c_boardinfo(struct i3c_master_controller
*master
,
1936 struct device_node
*node
, u32
*reg
)
1938 struct i2c_dev_boardinfo
*boardinfo
;
1939 struct device
*dev
= &master
->dev
;
1942 boardinfo
= devm_kzalloc(dev
, sizeof(*boardinfo
), GFP_KERNEL
);
1946 ret
= of_i2c_get_board_info(dev
, node
, &boardinfo
->base
);
1951 * The I3C Specification does not clearly say I2C devices with 10-bit
1952 * address are supported. These devices can't be passed properly through
1955 if (boardinfo
->base
.flags
& I2C_CLIENT_TEN
) {
1956 dev_err(&master
->dev
, "I2C device with 10 bit address not supported.");
1960 /* LVR is encoded in reg[2]. */
1961 boardinfo
->lvr
= reg
[2];
1963 list_add_tail(&boardinfo
->node
, &master
->boardinfo
.i2c
);
1970 of_i3c_master_add_i3c_boardinfo(struct i3c_master_controller
*master
,
1971 struct device_node
*node
, u32
*reg
)
1973 struct i3c_dev_boardinfo
*boardinfo
;
1974 struct device
*dev
= &master
->dev
;
1975 enum i3c_addr_slot_status addrstatus
;
1976 u32 init_dyn_addr
= 0;
1978 boardinfo
= devm_kzalloc(dev
, sizeof(*boardinfo
), GFP_KERNEL
);
1983 if (reg
[0] > I3C_MAX_ADDR
)
1986 addrstatus
= i3c_bus_get_addr_slot_status(&master
->bus
,
1988 if (addrstatus
!= I3C_ADDR_SLOT_FREE
)
1992 boardinfo
->static_addr
= reg
[0];
1994 if (!of_property_read_u32(node
, "assigned-address", &init_dyn_addr
)) {
1995 if (init_dyn_addr
> I3C_MAX_ADDR
)
1998 addrstatus
= i3c_bus_get_addr_slot_status(&master
->bus
,
2000 if (addrstatus
!= I3C_ADDR_SLOT_FREE
)
2004 boardinfo
->pid
= ((u64
)reg
[1] << 32) | reg
[2];
2006 if ((boardinfo
->pid
& GENMASK_ULL(63, 48)) ||
2007 I3C_PID_RND_LOWER_32BITS(boardinfo
->pid
))
2010 boardinfo
->init_dyn_addr
= init_dyn_addr
;
2011 boardinfo
->of_node
= of_node_get(node
);
2012 list_add_tail(&boardinfo
->node
, &master
->boardinfo
.i3c
);
2017 static int of_i3c_master_add_dev(struct i3c_master_controller
*master
,
2018 struct device_node
*node
)
2023 if (!master
|| !node
)
2026 ret
= of_property_read_u32_array(node
, "reg", reg
, ARRAY_SIZE(reg
));
2031 * The manufacturer ID can't be 0. If reg[1] == 0 that means we're
2032 * dealing with an I2C device.
2035 ret
= of_i3c_master_add_i2c_boardinfo(master
, node
, reg
);
2037 ret
= of_i3c_master_add_i3c_boardinfo(master
, node
, reg
);
2042 static int of_populate_i3c_bus(struct i3c_master_controller
*master
)
2044 struct device
*dev
= &master
->dev
;
2045 struct device_node
*i3cbus_np
= dev
->of_node
;
2046 struct device_node
*node
;
2053 for_each_available_child_of_node(i3cbus_np
, node
) {
2054 ret
= of_i3c_master_add_dev(master
, node
);
2062 * The user might want to limit I2C and I3C speed in case some devices
2063 * on the bus are not supporting typical rates, or if the bus topology
2064 * prevents it from using max possible rate.
2066 if (!of_property_read_u32(i3cbus_np
, "i2c-scl-hz", &val
))
2067 master
->bus
.scl_rate
.i2c
= val
;
2069 if (!of_property_read_u32(i3cbus_np
, "i3c-scl-hz", &val
))
2070 master
->bus
.scl_rate
.i3c
= val
;
2075 static int i3c_master_i2c_adapter_xfer(struct i2c_adapter
*adap
,
2076 struct i2c_msg
*xfers
, int nxfers
)
2078 struct i3c_master_controller
*master
= i2c_adapter_to_i3c_master(adap
);
2079 struct i2c_dev_desc
*dev
;
2083 if (!xfers
|| !master
|| nxfers
<= 0)
2086 if (!master
->ops
->i2c_xfers
)
2089 /* Doing transfers to different devices is not supported. */
2090 addr
= xfers
[0].addr
;
2091 for (i
= 1; i
< nxfers
; i
++) {
2092 if (addr
!= xfers
[i
].addr
)
2096 i3c_bus_normaluse_lock(&master
->bus
);
2097 dev
= i3c_master_find_i2c_dev_by_addr(master
, addr
);
2101 ret
= master
->ops
->i2c_xfers(dev
, xfers
, nxfers
);
2102 i3c_bus_normaluse_unlock(&master
->bus
);
2104 return ret
? ret
: nxfers
;
2107 static u32
i3c_master_i2c_funcs(struct i2c_adapter
*adapter
)
2109 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
;
2112 static const struct i2c_algorithm i3c_master_i2c_algo
= {
2113 .master_xfer
= i3c_master_i2c_adapter_xfer
,
2114 .functionality
= i3c_master_i2c_funcs
,
2117 static int i3c_master_i2c_adapter_init(struct i3c_master_controller
*master
)
2119 struct i2c_adapter
*adap
= i3c_master_to_i2c_adapter(master
);
2120 struct i2c_dev_desc
*i2cdev
;
2123 adap
->dev
.parent
= master
->dev
.parent
;
2124 adap
->owner
= master
->dev
.parent
->driver
->owner
;
2125 adap
->algo
= &i3c_master_i2c_algo
;
2126 strncpy(adap
->name
, dev_name(master
->dev
.parent
), sizeof(adap
->name
));
2128 /* FIXME: Should we allow i3c masters to override these values? */
2129 adap
->timeout
= 1000;
2132 ret
= i2c_add_adapter(adap
);
2137 * We silently ignore failures here. The bus should keep working
2138 * correctly even if one or more i2c devices are not registered.
2140 i3c_bus_for_each_i2cdev(&master
->bus
, i2cdev
)
2141 i2cdev
->dev
= i2c_new_device(adap
, &i2cdev
->boardinfo
->base
);
2146 static void i3c_master_i2c_adapter_cleanup(struct i3c_master_controller
*master
)
2148 struct i2c_dev_desc
*i2cdev
;
2150 i2c_del_adapter(&master
->i2c
);
2152 i3c_bus_for_each_i2cdev(&master
->bus
, i2cdev
)
2156 static void i3c_master_unregister_i3c_devs(struct i3c_master_controller
*master
)
2158 struct i3c_dev_desc
*i3cdev
;
2160 i3c_bus_for_each_i3cdev(&master
->bus
, i3cdev
) {
2164 i3cdev
->dev
->desc
= NULL
;
2165 if (device_is_registered(&i3cdev
->dev
->dev
))
2166 device_unregister(&i3cdev
->dev
->dev
);
2168 put_device(&i3cdev
->dev
->dev
);
2174 * i3c_master_queue_ibi() - Queue an IBI
2175 * @dev: the device this IBI is coming from
2176 * @slot: the IBI slot used to store the payload
2178 * Queue an IBI to the controller workqueue. The IBI handler attached to
2179 * the dev will be called from a workqueue context.
2181 void i3c_master_queue_ibi(struct i3c_dev_desc
*dev
, struct i3c_ibi_slot
*slot
)
2183 atomic_inc(&dev
->ibi
->pending_ibis
);
2184 queue_work(dev
->common
.master
->wq
, &slot
->work
);
2186 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi
);
2188 static void i3c_master_handle_ibi(struct work_struct
*work
)
2190 struct i3c_ibi_slot
*slot
= container_of(work
, struct i3c_ibi_slot
,
2192 struct i3c_dev_desc
*dev
= slot
->dev
;
2193 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
2194 struct i3c_ibi_payload payload
;
2196 payload
.data
= slot
->data
;
2197 payload
.len
= slot
->len
;
2200 dev
->ibi
->handler(dev
->dev
, &payload
);
2202 master
->ops
->recycle_ibi_slot(dev
, slot
);
2203 if (atomic_dec_and_test(&dev
->ibi
->pending_ibis
))
2204 complete(&dev
->ibi
->all_ibis_handled
);
2207 static void i3c_master_init_ibi_slot(struct i3c_dev_desc
*dev
,
2208 struct i3c_ibi_slot
*slot
)
2211 INIT_WORK(&slot
->work
, i3c_master_handle_ibi
);
2214 struct i3c_generic_ibi_slot
{
2215 struct list_head node
;
2216 struct i3c_ibi_slot base
;
2219 struct i3c_generic_ibi_pool
{
2221 unsigned int num_slots
;
2222 struct i3c_generic_ibi_slot
*slots
;
2224 struct list_head free_slots
;
2225 struct list_head pending
;
2229 * i3c_generic_ibi_free_pool() - Free a generic IBI pool
2230 * @pool: the IBI pool to free
2232 * Free all IBI slots allated by a generic IBI pool.
2234 void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool
*pool
)
2236 struct i3c_generic_ibi_slot
*slot
;
2237 unsigned int nslots
= 0;
2239 while (!list_empty(&pool
->free_slots
)) {
2240 slot
= list_first_entry(&pool
->free_slots
,
2241 struct i3c_generic_ibi_slot
, node
);
2242 list_del(&slot
->node
);
2247 * If the number of freed slots is not equal to the number of allocated
2248 * slots we have a leak somewhere.
2250 WARN_ON(nslots
!= pool
->num_slots
);
2252 kfree(pool
->payload_buf
);
2256 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool
);
2259 * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool
2260 * @dev: the device this pool will be used for
2261 * @req: IBI setup request describing what the device driver expects
2263 * Create a generic IBI pool based on the information provided in @req.
2265 * Return: a valid IBI pool in case of success, an ERR_PTR() otherwise.
2267 struct i3c_generic_ibi_pool
*
2268 i3c_generic_ibi_alloc_pool(struct i3c_dev_desc
*dev
,
2269 const struct i3c_ibi_setup
*req
)
2271 struct i3c_generic_ibi_pool
*pool
;
2272 struct i3c_generic_ibi_slot
*slot
;
2276 pool
= kzalloc(sizeof(*pool
), GFP_KERNEL
);
2278 return ERR_PTR(-ENOMEM
);
2280 spin_lock_init(&pool
->lock
);
2281 INIT_LIST_HEAD(&pool
->free_slots
);
2282 INIT_LIST_HEAD(&pool
->pending
);
2284 pool
->slots
= kcalloc(req
->num_slots
, sizeof(*slot
), GFP_KERNEL
);
2290 if (req
->max_payload_len
) {
2291 pool
->payload_buf
= kcalloc(req
->num_slots
,
2292 req
->max_payload_len
, GFP_KERNEL
);
2293 if (!pool
->payload_buf
) {
2299 for (i
= 0; i
< req
->num_slots
; i
++) {
2300 slot
= &pool
->slots
[i
];
2301 i3c_master_init_ibi_slot(dev
, &slot
->base
);
2303 if (req
->max_payload_len
)
2304 slot
->base
.data
= pool
->payload_buf
+
2305 (i
* req
->max_payload_len
);
2307 list_add_tail(&slot
->node
, &pool
->free_slots
);
2314 i3c_generic_ibi_free_pool(pool
);
2315 return ERR_PTR(ret
);
2317 EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool
);
2320 * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool
2321 * @pool: the pool to query an IBI slot on
2323 * Search for a free slot in a generic IBI pool.
2324 * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot()
2325 * when it's no longer needed.
2327 * Return: a pointer to a free slot, or NULL if there's no free slot available.
2329 struct i3c_ibi_slot
*
2330 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool
*pool
)
2332 struct i3c_generic_ibi_slot
*slot
;
2333 unsigned long flags
;
2335 spin_lock_irqsave(&pool
->lock
, flags
);
2336 slot
= list_first_entry_or_null(&pool
->free_slots
,
2337 struct i3c_generic_ibi_slot
, node
);
2339 list_del(&slot
->node
);
2340 spin_unlock_irqrestore(&pool
->lock
, flags
);
2342 return slot
? &slot
->base
: NULL
;
2344 EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot
);
2347 * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool
2348 * @pool: the pool to return the IBI slot to
2349 * @s: IBI slot to recycle
2351 * Add an IBI slot back to its generic IBI pool. Should be called from the
2352 * master driver struct_master_controller_ops->recycle_ibi() method.
2354 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool
*pool
,
2355 struct i3c_ibi_slot
*s
)
2357 struct i3c_generic_ibi_slot
*slot
;
2358 unsigned long flags
;
2363 slot
= container_of(s
, struct i3c_generic_ibi_slot
, base
);
2364 spin_lock_irqsave(&pool
->lock
, flags
);
2365 list_add_tail(&slot
->node
, &pool
->free_slots
);
2366 spin_unlock_irqrestore(&pool
->lock
, flags
);
2368 EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot
);
2370 static int i3c_master_check_ops(const struct i3c_master_controller_ops
*ops
)
2372 if (!ops
|| !ops
->bus_init
|| !ops
->priv_xfers
||
2373 !ops
->send_ccc_cmd
|| !ops
->do_daa
|| !ops
->i2c_xfers
)
2376 if (ops
->request_ibi
&&
2377 (!ops
->enable_ibi
|| !ops
->disable_ibi
|| !ops
->free_ibi
||
2378 !ops
->recycle_ibi_slot
))
2385 * i3c_master_register() - register an I3C master
2386 * @master: master used to send frames on the bus
2387 * @parent: the parent device (the one that provides this I3C master
2389 * @ops: the master controller operations
2390 * @secondary: true if you are registering a secondary master. Will return
2391 * -ENOTSUPP if set to true since secondary masters are not yet
2394 * This function takes care of everything for you:
2396 * - creates and initializes the I3C bus
2397 * - populates the bus with static I2C devs if @parent->of_node is not
2399 * - registers all I3C devices added by the controller during bus
2401 * - registers the I2C adapter and all I2C devices
2403 * Return: 0 in case of success, a negative error code otherwise.
2405 int i3c_master_register(struct i3c_master_controller
*master
,
2406 struct device
*parent
,
2407 const struct i3c_master_controller_ops
*ops
,
2410 unsigned long i2c_scl_rate
= I3C_BUS_I2C_FM_PLUS_SCL_RATE
;
2411 struct i3c_bus
*i3cbus
= i3c_master_get_bus(master
);
2412 enum i3c_bus_mode mode
= I3C_BUS_MODE_PURE
;
2413 struct i2c_dev_boardinfo
*i2cbi
;
2416 /* We do not support secondary masters yet. */
2420 ret
= i3c_master_check_ops(ops
);
2424 master
->dev
.parent
= parent
;
2425 master
->dev
.of_node
= of_node_get(parent
->of_node
);
2426 master
->dev
.bus
= &i3c_bus_type
;
2427 master
->dev
.type
= &i3c_masterdev_type
;
2428 master
->dev
.release
= i3c_masterdev_release
;
2430 master
->secondary
= secondary
;
2431 INIT_LIST_HEAD(&master
->boardinfo
.i2c
);
2432 INIT_LIST_HEAD(&master
->boardinfo
.i3c
);
2434 ret
= i3c_bus_init(i3cbus
);
2438 device_initialize(&master
->dev
);
2439 dev_set_name(&master
->dev
, "i3c-%d", i3cbus
->id
);
2441 ret
= of_populate_i3c_bus(master
);
2445 list_for_each_entry(i2cbi
, &master
->boardinfo
.i2c
, node
) {
2446 switch (i2cbi
->lvr
& I3C_LVR_I2C_INDEX_MASK
) {
2447 case I3C_LVR_I2C_INDEX(0):
2448 if (mode
< I3C_BUS_MODE_MIXED_FAST
)
2449 mode
= I3C_BUS_MODE_MIXED_FAST
;
2451 case I3C_LVR_I2C_INDEX(1):
2452 if (mode
< I3C_BUS_MODE_MIXED_LIMITED
)
2453 mode
= I3C_BUS_MODE_MIXED_LIMITED
;
2455 case I3C_LVR_I2C_INDEX(2):
2456 if (mode
< I3C_BUS_MODE_MIXED_SLOW
)
2457 mode
= I3C_BUS_MODE_MIXED_SLOW
;
2464 if (i2cbi
->lvr
& I3C_LVR_I2C_FM_MODE
)
2465 i2c_scl_rate
= I3C_BUS_I2C_FM_SCL_RATE
;
2468 ret
= i3c_bus_set_mode(i3cbus
, mode
, i2c_scl_rate
);
2472 master
->wq
= alloc_workqueue("%s", 0, 0, dev_name(parent
));
2478 ret
= i3c_master_bus_init(master
);
2482 ret
= device_add(&master
->dev
);
2484 goto err_cleanup_bus
;
2487 * Expose our I3C bus as an I2C adapter so that I2C devices are exposed
2488 * through the I2C subsystem.
2490 ret
= i3c_master_i2c_adapter_init(master
);
2495 * We're done initializing the bus and the controller, we can now
2496 * register I3C devices discovered during the initial DAA.
2498 master
->init_done
= true;
2499 i3c_bus_normaluse_lock(&master
->bus
);
2500 i3c_master_register_new_i3c_devs(master
);
2501 i3c_bus_normaluse_unlock(&master
->bus
);
2506 device_del(&master
->dev
);
2509 i3c_master_bus_cleanup(master
);
2512 put_device(&master
->dev
);
2516 EXPORT_SYMBOL_GPL(i3c_master_register
);
2519 * i3c_master_unregister() - unregister an I3C master
2520 * @master: master used to send frames on the bus
2522 * Basically undo everything done in i3c_master_register().
2524 * Return: 0 in case of success, a negative error code otherwise.
2526 int i3c_master_unregister(struct i3c_master_controller
*master
)
2528 i3c_master_i2c_adapter_cleanup(master
);
2529 i3c_master_unregister_i3c_devs(master
);
2530 i3c_master_bus_cleanup(master
);
2531 device_unregister(&master
->dev
);
2535 EXPORT_SYMBOL_GPL(i3c_master_unregister
);
2537 int i3c_dev_do_priv_xfers_locked(struct i3c_dev_desc
*dev
,
2538 struct i3c_priv_xfer
*xfers
,
2541 struct i3c_master_controller
*master
;
2546 master
= i3c_dev_get_master(dev
);
2547 if (!master
|| !xfers
)
2550 if (!master
->ops
->priv_xfers
)
2553 return master
->ops
->priv_xfers(dev
, xfers
, nxfers
);
2556 int i3c_dev_disable_ibi_locked(struct i3c_dev_desc
*dev
)
2558 struct i3c_master_controller
*master
;
2564 master
= i3c_dev_get_master(dev
);
2565 ret
= master
->ops
->disable_ibi(dev
);
2569 reinit_completion(&dev
->ibi
->all_ibis_handled
);
2570 if (atomic_read(&dev
->ibi
->pending_ibis
))
2571 wait_for_completion(&dev
->ibi
->all_ibis_handled
);
2573 dev
->ibi
->enabled
= false;
2578 int i3c_dev_enable_ibi_locked(struct i3c_dev_desc
*dev
)
2580 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
2586 ret
= master
->ops
->enable_ibi(dev
);
2588 dev
->ibi
->enabled
= true;
2593 int i3c_dev_request_ibi_locked(struct i3c_dev_desc
*dev
,
2594 const struct i3c_ibi_setup
*req
)
2596 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
2597 struct i3c_device_ibi_info
*ibi
;
2600 if (!master
->ops
->request_ibi
)
2606 ibi
= kzalloc(sizeof(*ibi
), GFP_KERNEL
);
2610 atomic_set(&ibi
->pending_ibis
, 0);
2611 init_completion(&ibi
->all_ibis_handled
);
2612 ibi
->handler
= req
->handler
;
2613 ibi
->max_payload_len
= req
->max_payload_len
;
2614 ibi
->num_slots
= req
->num_slots
;
2617 ret
= master
->ops
->request_ibi(dev
, req
);
2626 void i3c_dev_free_ibi_locked(struct i3c_dev_desc
*dev
)
2628 struct i3c_master_controller
*master
= i3c_dev_get_master(dev
);
2633 if (WARN_ON(dev
->ibi
->enabled
))
2634 WARN_ON(i3c_dev_disable_ibi_locked(dev
));
2636 master
->ops
->free_ibi(dev
);
2641 static int __init
i3c_init(void)
2643 return bus_register(&i3c_bus_type
);
2645 subsys_initcall(i3c_init
);
2647 static void __exit
i3c_exit(void)
2649 idr_destroy(&i3c_bus_idr
);
2650 bus_unregister(&i3c_bus_type
);
2652 module_exit(i3c_exit
);
2654 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
2655 MODULE_DESCRIPTION("I3C core");
2656 MODULE_LICENSE("GPL v2");