1 /*
2  * Copyright 2022 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_DRIVERS_I3C_H_
8 #define ZEPHYR_INCLUDE_DRIVERS_I3C_H_
9 
10 /**
11  * @brief I3C Interface
12  * @defgroup i3c_interface I3C Interface
13  * @ingroup io_interfaces
14  * @{
15  */
16 
17 #include <zephyr/types.h>
18 #include <zephyr/device.h>
19 
20 #include <zephyr/drivers/i3c/addresses.h>
21 #include <zephyr/drivers/i3c/ccc.h>
22 #include <zephyr/drivers/i3c/devicetree.h>
23 #include <zephyr/drivers/i3c/ibi.h>
24 #include <zephyr/drivers/i2c.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * @name Bus Characteristic Register (BCR)
32  *
33  * - BCR[7:6]: Device Role
34  *   - 0: I3C Target
35  *   - 1: I3C Controller capable
36  *   - 2: Reserved
37  *   - 3: Reserved
38  * - BCR[5]: Advanced Capabilities
39  *   - 0: Does not support optional advanced capabilities.
40  *   - 1: Supports optional advanced capabilities which
41  *        can be viewed via GETCAPS CCC.
42  * - BCR[4}: Virtual Target Support
43  *   - 0: Is not a virtual target.
44  *   - 1: Is a virtual target.
45  * - BCR[3]: Offline Capable
46  *   - 0: Will always response to I3C commands.
47  *   - 1: Will not always response to I3C commands.
48  * - BCR[2]: IBI Payload
49  *   - 0: No data bytes following the accepted IBI.
50  *   - 1: One data byte (MDB, Mandatory Data Byte) follows
51  *        the accepted IBI. Additional data bytes may also
52  *        follows.
53  * - BCR[1]: IBI Request Capable
54  *   - 0: Not capable
55  *   - 1: Capable
56  * - BCR[0]: Max Data Speed Limitation
57  *   - 0: No Limitation
58  *   - 1: Limitation obtained via GETMXDS CCC.
59  *
60  * @{
61  */
62 
63 /**
64  * @brief Max Data Speed Limitation bit.
65  *
66  * 0 - No Limitation.
67  * 1 - Limitation obtained via GETMXDS CCC.
68  */
69 #define I3C_BCR_MAX_DATA_SPEED_LIMIT			BIT(0)
70 
71 /** @brief IBI Request Capable bit. */
72 #define I3C_BCR_IBI_REQUEST_CAPABLE			BIT(1)
73 
74 /**
75  * @brief IBI Payload bit.
76  *
77  * 0 - No data bytes following the accepted IBI.
78  * 1 - One data byte (MDB, Mandatory Data Byte) follows the accepted IBI.
79  *     Additional data bytes may also follows.
80  */
81 #define I3C_BCR_IBI_PAYLOAD_HAS_DATA_BYTE		BIT(2)
82 
83 /**
84  * @brief Offline Capable bit.
85  *
86  * 0 - Will always respond to I3C commands.
87  * 1 - Will not always respond to I3C commands.
88  */
89 #define I3C_BCR_OFFLINE_CAPABLE				BIT(3)
90 
91 /**
92  * @brief Virtual Target Support bit.
93  *
94  * 0 - Is not a virtual target.
95  * 1 - Is a virtual target.
96  */
97 #define I3C_BCR_VIRTUAL_TARGET				BIT(4)
98 
99 /**
100  * @brief Advanced Capabilities bit.
101  *
102  * 0 - Does not support optional advanced capabilities.
103  * 1 - Supports optional advanced capabilities which can be viewed via
104  *     GETCAPS CCC.
105  */
106 #define I3C_BCR_ADV_CAPABILITIES			BIT(5)
107 
108 /** Device Role - I3C Target. */
109 #define I3C_BCR_DEVICE_ROLE_I3C_TARGET			0U
110 
111 /** Device Role - I3C Controller Capable. */
112 #define I3C_BCR_DEVICE_ROLE_I3C_CONTROLLER_CAPABLE	1U
113 
114 /** Device Role bit shift value. */
115 #define I3C_BCR_DEVICE_ROLE_SHIFT			6U
116 
117 /** Device Role bit shift mask. */
118 #define I3C_BCR_DEVICE_ROLE_MASK			(0x03U << I3C_BCR_DEVICE_ROLE_SHIFT)
119 
120 /**
121  * @brief Device Role
122  *
123  * Obtain Device Role value from the BCR value obtained via GETBCR.
124  *
125  * @param bcr BCR value
126  */
127 #define I3C_BCR_DEVICE_ROLE(bcr)			\
128 	(((bcr) & I3C_BCR_DEVICE_ROLE_MASK) >> I3C_BCR_DEVICE_ROLE_SHIFT)
129 
130 /** @} */
131 
132 /**
133  * @name Device Characteristic Register (DCR)
134  *
135  * Legacy Virtual Register (LVR)
136  * - LVR[7:5]: I2C device index:
137  *   - 0: I2C device has a 50 ns spike filter where
138  *        it is not affected by high frequency on SCL.
139  *   - 1: I2C device does not have a 50 ns spike filter
140  *        but can work with high frequency on SCL.
141  *   - 2: I2C device does not have a 50 ns spike filter
142  *        and cannot work with high frequency on SCL.
143  * - LVR[4]: I2C mode indicator:
144  *   - 0: FM+ mode
145  *   - 1: FM mode
146  * - LVR[3:0]: Reserved.
147  *
148  * @{
149  */
150 
151 /** I2C FM+ Mode. */
152 #define I3C_DCR_I2C_FM_PLUS_MODE			0
153 
154 /** I2C FM Mode. */
155 #define I3C_DCR_I2C_FM_MODE				1
156 
157 /** I2C Mode Indicator bit shift value. */
158 #define I3C_DCR_I2C_MODE_SHIFT				4
159 
160 /** I2C Mode Indicator bitmask. */
161 #define I3C_DCR_I2C_MODE_MASK				BIT(4)
162 
163 /**
164  * @brief I2C Mode
165  *
166  * Obtain I2C Mode value from the DCR value obtained via GETDCR.
167  *
168  * @param dcr DCR value
169  */
170 #define I3C_DCR_I2C_MODE(dcr)				\
171 	(((mode) & I3C_DCR_I2C_MODE_MASK) >> I3C_DCR_I2C_MODE_SHIFT)
172 
173 /**
174  * @brief I2C Device Index 0.
175  *
176  * I2C device has a 50 ns spike filter where it is not affected by high
177  * frequency on SCL.
178  */
179 #define I3C_DCR_I2C_DEV_IDX_0				0
180 
181 /**
182  * @brief I2C Device Index 1.
183  *
184  * I2C device does not have a 50 ns spike filter but can work with high
185  * frequency on SCL.
186  */
187 #define I3C_DCR_I2C_DEV_IDX_1				1
188 
189 /**
190  * @brief I2C Device Index 2.
191  *
192  * I2C device does not have a 50 ns spike filter and cannot work with high
193  * frequency on SCL.
194  */
195 #define I3C_DCR_I2C_DEV_IDX_2				2
196 
197 /** I2C Device Index bit shift value. */
198 #define I3C_DCR_I2C_DEV_IDX_SHIFT			5
199 
200 /** I2C Device Index bitmask. */
201 #define I3C_DCR_I2C_DEV_IDX_MASK			(0x07U << I3C_DCR_I2C_DEV_IDX_SHIFT)
202 
203 /**
204  * @brief I2C Device Index
205  *
206  * Obtain I2C Device Index value from the DCR value obtained via GETDCR.
207  *
208  * @param dcr DCR value
209  */
210 #define I3C_DCR_I2C_DEV_IDX(dcr)			\
211 	(((dcr) & I3C_DCR_I2C_DEV_IDX_MASK) >> I3C_DCR_I2C_DEV_IDX_SHIFT)
212 
213 /** @} */
214 
215 /**
216  * @brief I3C bus mode
217  */
218 enum i3c_bus_mode {
219 	/** Only I3C devices are on the bus. */
220 	I3C_BUS_MODE_PURE,
221 
222 	/**
223 	 * Both I3C and legacy I2C devices are on the bus.
224 	 * The I2C devices have 50ns spike filter on SCL.
225 	 */
226 	I3C_BUS_MODE_MIXED_FAST,
227 
228 	/**
229 	 * Both I3C and legacy I2C devices are on the bus.
230 	 * The I2C devices do not have 50ns spike filter on SCL
231 	 * and can tolerate maximum SDR SCL clock frequency.
232 	 */
233 	I3C_BUS_MODE_MIXED_LIMITED,
234 
235 	/**
236 	 * Both I3C and legacy I2C devices are on the bus.
237 	 * The I2C devices do not have 50ns spike filter on SCL
238 	 * but cannot tolerate maximum SDR SCL clock frequency.
239 	 */
240 	I3C_BUS_MODE_MIXED_SLOW,
241 
242 	I3C_BUS_MODE_MAX = I3C_BUS_MODE_MIXED_SLOW,
243 	I3C_BUS_MODE_INVALID,
244 };
245 
246 /**
247  * @brief I2C bus speed under I3C bus.
248  *
249  * Only FM and FM+ modes are supported for I2C devices under I3C bus.
250  */
251 enum i3c_i2c_speed_type {
252 	/** I2C FM mode */
253 	I3C_I2C_SPEED_FM,
254 
255 	/** I2C FM+ mode */
256 	I3C_I2C_SPEED_FMPLUS,
257 
258 	I3C_I2C_SPEED_MAX = I3C_I2C_SPEED_FMPLUS,
259 	I3C_I2C_SPEED_INVALID,
260 };
261 
262 /**
263  * @brief I3C data rate
264  *
265  * I3C data transfer rate defined by the I3C specification.
266  */
267 enum i3c_data_rate {
268 	/** Single Data Rate messaging */
269 	I3C_DATA_RATE_SDR,
270 
271 	/** High Data Rate - Double Data Rate messaging */
272 	I3C_DATA_RATE_HDR_DDR,
273 
274 	/** High Data Rate - Ternary Symbol Legacy-inclusive-Bus */
275 	I3C_DATA_RATE_HDR_TSL,
276 
277 	/** High Data Rate - Ternary Symbol for Pure Bus */
278 	I3C_DATA_RATE_HDR_TSP,
279 
280 	/** High Data Rate - Bulk Transport */
281 	I3C_DATA_RATE_HDR_BT,
282 
283 	I3C_DATA_RATE_MAX = I3C_DATA_RATE_HDR_BT,
284 	I3C_DATA_RATE_INVALID,
285 };
286 
287 /**
288  * @brief I3C SDR Controller Error Codes
289  *
290  * These are error codes defined by the I3C specification.
291  *
292  * @c I3C_ERROR_CE_UNKNOWN and @c I3C_ERROR_CE_NONE are not
293  * official error codes according to the specification.
294  * These are there simply to aid in error handling during
295  * interactions with the I3C drivers and subsystem.
296  */
297 enum i3c_sdr_controller_error_codes {
298 	/** Transaction after sending CCC */
299 	I3C_ERROR_CE0,
300 
301 	/** Monitoring Error */
302 	I3C_ERROR_CE1,
303 
304 	/** No response to broadcast address (0x7E) */
305 	I3C_ERROR_CE2,
306 
307 	/** Failed Controller Handoff */
308 	I3C_ERROR_CE3,
309 
310 	/** Unknown error (not official error code) */
311 	I3C_ERROR_CE_UNKNOWN,
312 
313 	/** No error (not official error code) */
314 	I3C_ERROR_CE_NONE,
315 
316 	I3C_ERROR_CE_MAX = I3C_ERROR_CE_UNKNOWN,
317 	I3C_ERROR_CE_INVALID,
318 };
319 
320 /**
321  * @brief I3C SDR Target Error Codes
322  *
323  * These are error codes defined by the I3C specification.
324  *
325  * @c I3C_ERROR_TE_UNKNOWN and @c I3C_ERROR_TE_NONE are not
326  * official error codes according to the specification.
327  * These are there simply to aid in error handling during
328  * interactions with the I3C drivers and subsystem.
329  */
330 enum i3c_sdr_target_error_codes {
331 	/**
332 	 * Invalid Broadcast Address or
333 	 * Dynamic Address after DA assignment
334 	 */
335 	I3C_ERROR_TE0,
336 
337 	/** CCC Code */
338 	I3C_ERROR_TE1,
339 
340 	/** Write Data */
341 	I3C_ERROR_TE2,
342 
343 	/** Assigned Address during Dynamic Address Arbitration */
344 	I3C_ERROR_TE3,
345 
346 	/** 0x7E/R missing after RESTART during Dynamic Address Arbitration */
347 	I3C_ERROR_TE4,
348 
349 	/** Transaction after detecting CCC */
350 	I3C_ERROR_TE5,
351 
352 	/** Monitoring Error */
353 	I3C_ERROR_TE6,
354 
355 	/** Dead Bus Recovery */
356 	I3C_ERROR_DBR,
357 
358 	/** Unknown error (not official error code) */
359 	I3C_ERROR_TE_UNKNOWN,
360 
361 	/** No error (not official error code) */
362 	I3C_ERROR_TE_NONE,
363 
364 	I3C_ERROR_TE_MAX = I3C_ERROR_TE_UNKNOWN,
365 	I3C_ERROR_TE_INVALID,
366 };
367 
368 /**
369  * @brief I3C Transfer API
370  * @defgroup i3c_transfer_api I3C Transfer API
371  * @{
372  */
373 
374 /*
375  * I3C_MSG_* are I3C Message flags.
376  */
377 
378 /** Write message to I3C bus. */
379 #define I3C_MSG_WRITE			(0U << 0U)
380 
381 /** Read message from I3C bus. */
382 #define I3C_MSG_READ			BIT(0)
383 
384 /** @cond INTERNAL_HIDDEN */
385 #define I3C_MSG_RW_MASK			BIT(0)
386 /** @endcond  */
387 
388 /** Send STOP after this message. */
389 #define I3C_MSG_STOP			BIT(1)
390 
391 /**
392  * RESTART I3C transaction for this message.
393  *
394  * @note Not all I3C drivers have or require explicit support for this
395  * feature. Some drivers require this be present on a read message
396  * that follows a write, or vice-versa.  Some drivers will merge
397  * adjacent fragments into a single transaction using this flag; some
398  * will not.
399  */
400 #define I3C_MSG_RESTART			BIT(2)
401 
402 /** Transfer use HDR mode */
403 #define I3C_MSG_HDR			BIT(3)
404 
405 /** Skip I3C broadcast header. Private Transfers only. */
406 #define I3C_MSG_NBCH			BIT(4)
407 
408 /** I3C HDR Mode 0 */
409 #define I3C_MSG_HDR_MODE0		BIT(0)
410 
411 /** I3C HDR Mode 1 */
412 #define I3C_MSG_HDR_MODE1		BIT(1)
413 
414 /** I3C HDR Mode 2 */
415 #define I3C_MSG_HDR_MODE2		BIT(2)
416 
417 /** I3C HDR Mode 3 */
418 #define I3C_MSG_HDR_MODE3		BIT(3)
419 
420 /** I3C HDR Mode 4 */
421 #define I3C_MSG_HDR_MODE4		BIT(4)
422 
423 /** I3C HDR Mode 5 */
424 #define I3C_MSG_HDR_MODE5		BIT(5)
425 
426 /** I3C HDR Mode 6 */
427 #define I3C_MSG_HDR_MODE6		BIT(6)
428 
429 /** I3C HDR Mode 7 */
430 #define I3C_MSG_HDR_MODE7		BIT(7)
431 
432 /** I3C HDR-DDR (Double Data Rate) */
433 #define I3C_MSG_HDR_DDR			I3C_MSG_HDR_MODE0
434 
435 /** I3C HDR-TSP (Ternary Symbol Pure-bus) */
436 #define I3C_MSG_HDR_TSP			I3C_MSG_HDR_MODE1
437 
438 /** I3C HDR-TSL (Ternary Symbol Legacy-inclusive-bus) */
439 #define I3C_MSG_HDR_TSL			I3C_MSG_HDR_MODE2
440 
441 /** I3C HDR-BT (Bulk Transport) */
442 #define I3C_MSG_HDR_BT			I3C_MSG_HDR_MODE3
443 
444 /** @} */
445 
446 /**
447  * @addtogroup i3c_transfer_api
448  * @{
449  */
450 
451 /**
452  * @brief One I3C Message.
453  *
454  * This defines one I3C message to transact on the I3C bus.
455  *
456  * @note Some of the configurations supported by this API may not be
457  * supported by specific SoC I3C hardware implementations, in
458  * particular features related to bus transactions intended to read or
459  * write data from different buffers within a single transaction.
460  * Invocations of i3c_transfer() may not indicate an error when an
461  * unsupported configuration is encountered.  In some cases drivers
462  * will generate separate transactions for each message fragment, with
463  * or without presence of @ref I3C_MSG_RESTART in #flags.
464  */
465 struct i3c_msg {
466 	/** Data buffer in bytes */
467 	uint8_t			*buf;
468 
469 	/** Length of buffer in bytes */
470 	uint32_t		len;
471 
472 	/** Flags for this message */
473 	uint8_t			flags;
474 
475 	/**
476 	 * HDR mode (@c I3C_MSG_HDR_MODE*) for transfer
477 	 * if any @c I3C_MSG_HDR_* is set in @c flags.
478 	 *
479 	 * Use SDR mode if none is set.
480 	 */
481 	uint8_t			hdr_mode;
482 };
483 
484 /** @} */
485 
486 /**
487  * @brief Type of configuration being passed to configure function.
488  */
489 enum i3c_config_type {
490 	I3C_CONFIG_CONTROLLER,
491 	I3C_CONFIG_TARGET,
492 	I3C_CONFIG_CUSTOM,
493 };
494 
495 /**
496  * @brief Configuration parameters for I3C hardware to act as controller.
497  */
498 struct i3c_config_controller {
499 	/**
500 	 * True if the controller is to be the secondary controller
501 	 * of the bus. False to be the primary controller.
502 	 */
503 	bool is_secondary;
504 
505 	struct {
506 		/** SCL frequency (in Hz) for I3C transfers. */
507 		uint32_t i3c;
508 
509 		/** SCL frequency (in Hz) for I2C transfers. */
510 		uint32_t i2c;
511 	} scl;
512 
513 	/**
514 	 * Bit mask of supported HDR modes (0 - 7).
515 	 *
516 	 * This can be used to enable or disable HDR mode
517 	 * supported by the hardware at runtime.
518 	 */
519 	uint8_t supported_hdr;
520 };
521 
522 /**
523  * @brief Custom I3C configuration parameters.
524  *
525  * This can be used to configure the I3C hardware on parameters
526  * not covered by @see i3c_config_controller or @see i3c_config_target.
527  * Mostly used to configure vendor specific parameters of the I3C
528  * hardware.
529  */
530 struct i3c_config_custom {
531 	/** ID of the configuration parameter. */
532 	uint32_t id;
533 
534 	union {
535 		/** Value of configuration parameter. */
536 		uintptr_t val;
537 
538 		/**
539 		 * Pointer to configuration parameter.
540 		 *
541 		 * Mainly used to pointer to a struct that
542 		 * the device driver understands.
543 		 */
544 		void *ptr;
545 	};
546 };
547 
548 /**
549  * @cond INTERNAL_HIDDEN
550  *
551  * These are for internal use only, so skip these in
552  * public documentation.
553  */
554 struct i3c_device_desc;
555 struct i3c_device_id;
556 struct i3c_i2c_device_desc;
557 struct i3c_target_config;
558 
559 __subsystem struct i3c_driver_api {
560 	/**
561 	 * For backward compatibility to I2C API.
562 	 *
563 	 * @see i2c_driver_api for more information.
564 	 *
565 	 * (DO NOT MOVE! Must be at the beginning.)
566 	 */
567 	struct i2c_driver_api i2c_api;
568 
569 	/**
570 	 * Configure the I3C hardware.
571 	 *
572 	 * @see i3c_configure
573 	 *
574 	 * @param dev Pointer to controller device driver instance.
575 	 * @param type Type of configuration parameters being passed
576 	 *             in @p config.
577 	 * @param config Pointer to the configuration parameters.
578 	 *
579 	 * @return @see i3c_configure
580 	 */
581 	int (*configure)(const struct device *dev,
582 			 enum i3c_config_type type, void *config);
583 
584 	/**
585 	 * Get configuration of the I3C hardware.
586 	 *
587 	 * @see i3c_config_get
588 	 *
589 	 * @param[in] dev Pointer to controller device driver instance.
590 	 * @param[in] type Type of configuration parameters being passed
591 	 *                 in @p config.
592 	 * @param[in, out] config Pointer to the configuration parameters.
593 	 *
594 	 * @return @see i3c_config_get
595 	 */
596 	int (*config_get)(const struct device *dev,
597 			  enum i3c_config_type type, void *config);
598 
599 	/**
600 	 * Perform bus recovery
601 	 *
602 	 * Controller only API.
603 	 *
604 	 * @see i3c_recover_bus
605 	 *
606 	 * @param dev Pointer to controller device driver instance.
607 	 *
608 	 * @return @see i3c_recover_bus
609 	 */
610 	int (*recover_bus)(const struct device *dev);
611 
612 	/**
613 	 * I3C Device Attach
614 	 *
615 	 * Optional API.
616 	 *
617 	 * @see i3c_attach_i3c_device
618 	 *
619 	 * @param dev Pointer to controller device driver instance.
620 	 * @param target Pointer to target device descriptor.
621 	 * @param addr Address to attach with
622 	 *
623 	 * @return @see i3c_attach_i3c_device
624 	 */
625 	int (*attach_i3c_device)(const struct device *dev,
626 			struct i3c_device_desc *target,
627 			uint8_t addr);
628 
629 	/**
630 	 * I3C Address Update
631 	 *
632 	 * Optional API.
633 	 *
634 	 * @see i3c_reattach_i3c_device
635 	 *
636 	 * @param dev Pointer to controller device driver instance.
637 	 * @param target Pointer to target device descriptor.
638 	 * @param old_dyn_addr Old dynamic address
639 	 *
640 	 * @return @see i3c_reattach_i3c_device
641 	 */
642 	int (*reattach_i3c_device)(const struct device *dev,
643 			struct i3c_device_desc *target,
644 			uint8_t old_dyn_addr);
645 
646 	/**
647 	 * I3C Device Detach
648 	 *
649 	 * Optional API.
650 	 *
651 	 * @see i3c_detach_i3c_device
652 	 *
653 	 * @param dev Pointer to controller device driver instance.
654 	 * @param target Pointer to target device descriptor.
655 	 *
656 	 * @return @see i3c_detach_i3c_device
657 	 */
658 	int (*detach_i3c_device)(const struct device *dev,
659 			struct i3c_device_desc *target);
660 
661 	/**
662 	 * I2C Device Attach
663 	 *
664 	 * Optional API.
665 	 *
666 	 * @see i3c_attach_i2c_device
667 	 *
668 	 * @param dev Pointer to controller device driver instance.
669 	 * @param target Pointer to target device descriptor.
670 	 *
671 	 * @return @see i3c_attach_i2c_device
672 	 */
673 	int (*attach_i2c_device)(const struct device *dev,
674 			struct i3c_i2c_device_desc *target);
675 
676 	/**
677 	 * I2C Device Detach
678 	 *
679 	 * Optional API.
680 	 *
681 	 * @see i3c_detach_i2c_device
682 	 *
683 	 * @param dev Pointer to controller device driver instance.
684 	 * @param target Pointer to target device descriptor.
685 	 *
686 	 * @return @see i3c_detach_i2c_device
687 	 */
688 	int (*detach_i2c_device)(const struct device *dev,
689 			struct i3c_i2c_device_desc *target);
690 
691 	/**
692 	 * Perform Dynamic Address Assignment via ENTDAA.
693 	 *
694 	 * Controller only API.
695 	 *
696 	 * @see i3c_do_daa
697 	 *
698 	 * @param dev Pointer to controller device driver instance.
699 	 *
700 	 * @return @see i3c_do_daa
701 	 */
702 	int (*do_daa)(const struct device *dev);
703 
704 	/**
705 	 * Send Common Command Code (CCC).
706 	 *
707 	 * Controller only API.
708 	 *
709 	 * @see i3c_do_ccc
710 	 *
711 	 * @param dev Pointer to controller device driver instance.
712 	 * @param payload Pointer to the CCC payload.
713 	 *
714 	 * @return @see i3c_do_ccc
715 	 */
716 	int (*do_ccc)(const struct device *dev,
717 		      struct i3c_ccc_payload *payload);
718 
719 	/**
720 	 * Transfer messages in I3C mode.
721 	 *
722 	 * @see i3c_transfer
723 	 *
724 	 * @param dev Pointer to controller device driver instance.
725 	 * @param target Pointer to target device descriptor.
726 	 * @param msg Pointer to I3C messages.
727 	 * @param num_msgs Number of messages to transfer.
728 	 *
729 	 * @return @see i3c_transfer
730 	 */
731 	int (*i3c_xfers)(const struct device *dev,
732 			 struct i3c_device_desc *target,
733 			 struct i3c_msg *msgs,
734 			 uint8_t num_msgs);
735 
736 	/**
737 	 * Find a registered I3C target device.
738 	 *
739 	 * Controller only API.
740 	 *
741 	 * This returns the I3C device descriptor of the I3C device
742 	 * matching the incoming @p id.
743 	 *
744 	 * @param dev Pointer to controller device driver instance.
745 	 * @param id Pointer to I3C device ID.
746 	 *
747 	 * @return @see i3c_device_find.
748 	 */
749 	struct i3c_device_desc *(*i3c_device_find)(const struct device *dev,
750 						   const struct i3c_device_id *id);
751 
752 	/**
753 	 * Raise In-Band Interrupt (IBI).
754 	 *
755 	 * Target device only API.
756 	 *
757 	 * @see i3c_ibi_request
758 	 *
759 	 * @param dev Pointer to controller device driver instance.
760 	 * @param request Pointer to IBI request struct.
761 	 *
762 	 * @return @see i3c_ibi_request
763 	 */
764 	int (*ibi_raise)(const struct device *dev,
765 			 struct i3c_ibi *request);
766 
767 	/**
768 	 * Enable receiving IBI from a target.
769 	 *
770 	 * Controller only API.
771 	 *
772 	 * @see i3c_ibi_enable
773 	 *
774 	 * @param dev Pointer to controller device driver instance.
775 	 * @param target Pointer to target device descriptor.
776 	 *
777 	 * @return @see i3c_ibi_enable
778 	 */
779 	int (*ibi_enable)(const struct device *dev,
780 			  struct i3c_device_desc *target);
781 
782 	/**
783 	 * Disable receiving IBI from a target.
784 	 *
785 	 * Controller only API.
786 	 *
787 	 * @see i3c_ibi_disable
788 	 *
789 	 * @param dev Pointer to controller device driver instance.
790 	 * @param target Pointer to target device descriptor.
791 	 *
792 	 * @return @see i3c_ibi_disable
793 	 */
794 	int (*ibi_disable)(const struct device *dev,
795 			   struct i3c_device_desc *target);
796 
797 	/**
798 	 * Register config as target device of a controller.
799 	 *
800 	 * This tells the controller to act as a target device
801 	 * on the I3C bus.
802 	 *
803 	 * Target device only API.
804 	 *
805 	 * @see i3c_target_register
806 	 *
807 	 * @param dev Pointer to the controller device driver instance.
808 	 * @param cfg I3C target device configuration
809 	 *
810 	 * @return @see i3c_target_register
811 	 */
812 	int (*target_register)(const struct device *dev,
813 			       struct i3c_target_config *cfg);
814 
815 	/**
816 	 * Unregister config as target device of a controller.
817 	 *
818 	 * This tells the controller to stop acting as a target device
819 	 * on the I3C bus.
820 	 *
821 	 * Target device only API.
822 	 *
823 	 * @see i3c_target_unregister
824 	 *
825 	 * @param dev Pointer to the controller device driver instance.
826 	 * @param cfg I3C target device configuration
827 	 *
828 	 * @return @see i3c_target_unregister
829 	 */
830 	int (*target_unregister)(const struct device *dev,
831 				 struct i3c_target_config *cfg);
832 
833 	/**
834 	 * Write to the TX FIFO
835 	 *
836 	 * This writes to the target tx fifo
837 	 *
838 	 * Target device only API.
839 	 *
840 	 * @see i3c_target_tx_write
841 	 *
842 	 * @param dev Pointer to the controller device driver instance.
843 	 * @param buf Pointer to the buffer
844 	 * @param len Length of the buffer
845 	 *
846 	 * @return @see i3c_target_tx_write
847 	 */
848 	int (*target_tx_write)(const struct device *dev,
849 				 uint8_t *buf, uint16_t len);
850 };
851 
852 /**
853  * @endcond
854  */
855 
856 /**
857  * @brief Structure used for matching I3C devices.
858  */
859 struct i3c_device_id {
860 	/** Device Provisioned ID */
861 	const uint64_t pid:48;
862 };
863 
864 /**
865  * @brief Structure initializer for i3c_device_id from PID
866  *
867  * This helper macro expands to a static initializer for a <tt>struct
868  * i3c_device_id</tt> by populating the PID (Provisioned ID) field.
869  *
870  * @param pid Provisioned ID.
871  */
872 #define I3C_DEVICE_ID(pid)						\
873 	{								\
874 		.pid = pid						\
875 	}
876 
877 /**
878  * @brief Structure describing a I3C target device.
879  *
880  * Instances of this are passed to the I3C controller device APIs,
881  * for example:
882  * - i3c_device_register() to tell the controller of a target device.
883  * - i3c_transfers() to initiate data transfers between controller and
884  *   target device.
885  *
886  * Fields @c bus, @c pid and @c static_addr must be initialized by
887  * the module that implements the target device behavior prior to
888  * passing the object reference to I3C controller device APIs.
889  * @c static_addr can be zero if target device does not have static
890  * address.
891  *
892  * Field @c node should not be initialized or modified manually.
893  */
894 struct i3c_device_desc {
895 	/**
896 	 * Used to attach this node onto a linked list.
897 	 *
898 	 * @cond INTERNAL_HIDDEN
899 	 */
900 	sys_snode_t node;
901 	/** @endcond */
902 
903 	/** I3C bus to which this target device is attached */
904 	const struct device * const bus;
905 
906 	/** Device driver instance of the I3C device */
907 	const struct device * const dev;
908 
909 	/** Device Provisioned ID */
910 	const uint64_t pid:48;
911 
912 	/**
913 	 * Static address for this target device.
914 	 *
915 	 * 0 if static address is not being used, and only dynamic
916 	 * address is used. This means that the target device must
917 	 * go through ENTDAA (Dynamic Address Assignment) to get
918 	 * a dynamic address before it can communicate with
919 	 * the controller. This means SETAASA and SETDASA CCC
920 	 * cannot be used to set dynamic address on the target
921 	 * device (as both are to tell target device to use static
922 	 * address as dynamic address).
923 	 */
924 	const uint8_t static_addr;
925 
926 	/**
927 	 * Initial dynamic address.
928 	 *
929 	 * This is specified in the device tree property "assigned-address"
930 	 * to indicate the desired dynamic address during address
931 	 * assignment (SETDASA and ENTDAA).
932 	 *
933 	 * 0 if there is no preference.
934 	 */
935 	const uint8_t init_dynamic_addr;
936 
937 	/**
938 	 * Dynamic Address for this target device used for communication.
939 	 *
940 	 * This is to be set by the controller driver in one of
941 	 * the following situations:
942 	 * - During Dynamic Address Assignment (during ENTDAA)
943 	 * - Reset Dynamic Address Assignment (RSTDAA)
944 	 * - Set All Addresses to Static Addresses (SETAASA)
945 	 * - Set New Dynamic Address (SETNEWDA)
946 	 * - Set Dynamic Address from Static Address (SETDASA)
947 	 *
948 	 * 0 if address has not been assigned.
949 	 */
950 	uint8_t dynamic_addr;
951 
952 #if defined(CONFIG_I3C_USE_GROUP_ADDR) || defined(__DOXYGEN__)
953 	/**
954 	 * Group address for this target device. Set during:
955 	 * - Reset Group Address(es) (RSTGRPA)
956 	 * - Set Group Address (SETGRPA)
957 	 *
958 	 * 0 if group address has not been assigned.
959 	 */
960 	uint8_t group_addr;
961 #endif /* CONFIG_I3C_USE_GROUP_ADDR */
962 
963 	/**
964 	 * Bus Characteristic Register (BCR)
965 	 * - BCR[7:6]: Device Role
966 	 *   - 0: I3C Target
967 	 *   - 1: I3C Controller capable
968 	 *   - 2: Reserved
969 	 *   - 3: Reserved
970 	 * - BCR[5]: Advanced Capabilities
971 	 *   - 0: Does not support optional advanced capabilities.
972 	 *   - 1: Supports optional advanced capabilities which
973 	 *        can be viewed via GETCAPS CCC.
974 	 * - BCR[4}: Virtual Target Support
975 	 *   - 0: Is not a virtual target.
976 	 *   - 1: Is a virtual target.
977 	 * - BCR[3]: Offline Capable
978 	 *   - 0: Will always response to I3C commands.
979 	 *   - 1: Will not always response to I3C commands.
980 	 * - BCR[2]: IBI Payload
981 	 *   - 0: No data bytes following the accepted IBI.
982 	 *   - 1: One data byte (MDB, Mandatory Data Byte) follows
983 	 *        the accepted IBI. Additional data bytes may also
984 	 *        follows.
985 	 * - BCR[1]: IBI Request Capable
986 	 *   - 0: Not capable
987 	 *   - 1: Capable
988 	 * - BCR[0]: Max Data Speed Limitation
989 	 *   - 0: No Limitation
990 	 *   - 1: Limitation obtained via GETMXDS CCC.
991 	 */
992 	uint8_t bcr;
993 
994 	/**
995 	 * Device Characteristic Register (DCR)
996 	 *
997 	 * Describes the type of device. Refer to official documentation
998 	 * on what this number means.
999 	 */
1000 	uint8_t dcr;
1001 
1002 	struct {
1003 		/** Maximum Read Speed */
1004 		uint8_t maxrd;
1005 
1006 		/** Maximum Write Speed */
1007 		uint8_t maxwr;
1008 
1009 		/** Maximum Read turnaround time in microseconds. */
1010 		uint32_t max_read_turnaround;
1011 	} data_speed;
1012 
1013 	struct {
1014 		/** Maximum Read Length */
1015 		uint16_t mrl;
1016 
1017 		/** Maximum Write Length */
1018 		uint16_t mwl;
1019 
1020 		/** Maximum IBI Payload Size. Valid only if BCR[2] is 1. */
1021 		uint8_t max_ibi;
1022 	} data_length;
1023 
1024 	/**
1025 	 * Private data by the controller to aid in transactions. Do not modify.
1026 	 *
1027 	 * @cond INTERNAL_HIDDEN
1028 	 */
1029 	void *controller_priv;
1030 	/** @endcond */
1031 
1032 #if defined(CONFIG_I3C_USE_IBI) || defined(__DOXYGEN__)
1033 	/**
1034 	 * In-Band Interrupt (IBI) callback.
1035 	 */
1036 	i3c_target_ibi_cb_t ibi_cb;
1037 #endif /* CONFIG_I3C_USE_IBI */
1038 };
1039 
1040 /**
1041  * @brief Structure describing a I2C device on I3C bus.
1042  *
1043  * Instances of this are passed to the I3C controller device APIs,
1044  * for example:
1045  * () i3c_i2c_device_register() to tell the controller of an I2C device.
1046  * () i3c_i2c_transfers() to initiate data transfers between controller
1047  *    and I2C device.
1048  *
1049  * Fields other than @c node must be initialized by the module that
1050  * implements the device behavior prior to passing the object
1051  * reference to I3C controller device APIs.
1052  */
1053 struct i3c_i2c_device_desc {
1054 	/**
1055 	 * Used to attach this node onto a linked list.
1056 	 *
1057 	 * @cond INTERNAL_HIDDEN
1058 	 */
1059 	sys_snode_t node;
1060 	/** @endcond */
1061 
1062 	/** I3C bus to which this I2C device is attached */
1063 	const struct device *bus;
1064 
1065 	/** Static address for this I2C device. */
1066 	const uint16_t addr;
1067 
1068 	/**
1069 	 * Legacy Virtual Register (LVR)
1070 	 * - LVR[7:5]: I2C device index:
1071 	 *   - 0: I2C device has a 50 ns spike filter where
1072 	 *        it is not affected by high frequency on SCL.
1073 	 *   - 1: I2C device does not have a 50 ns spike filter
1074 	 *        but can work with high frequency on SCL.
1075 	 *   - 2: I2C device does not have a 50 ns spike filter
1076 	 *        and cannot work with high frequency on SCL.
1077 	 * - LVR[4]: I2C mode indicator:
1078 	 *   - 0: FM+ mode
1079 	 *   - 1: FM mode
1080 	 * - LVR[3:0]: Reserved.
1081 	 */
1082 	const uint8_t lvr;
1083 
1084 	/**
1085 	 * Private data by the controller to aid in transactions. Do not modify.
1086 	 *
1087 	 * @cond INTERNAL_HIDDEN
1088 	 */
1089 	void *controller_priv;
1090 	/** @endcond */
1091 };
1092 
1093 /**
1094  * @brief Structure for describing attached devices for a controller.
1095  *
1096  * This contains slists of attached I3C and I2C devices.
1097  *
1098  * This is a helper struct that can be used by controller device
1099  * driver to aid in device management.
1100  */
1101 struct i3c_dev_attached_list {
1102 	/**
1103 	 * Address slots:
1104 	 * - Aid in dynamic address assignment.
1105 	 * - Quick way to find out if a target address is
1106 	 *   a I3C or I2C device.
1107 	 */
1108 	struct i3c_addr_slots addr_slots;
1109 
1110 	struct {
1111 		/**
1112 		 * Linked list of attached I3C devices.
1113 		 */
1114 		sys_slist_t i3c;
1115 
1116 		/**
1117 		 * Linked list of attached I2C devices.
1118 		 */
1119 		sys_slist_t i2c;
1120 	} devices;
1121 };
1122 
1123 /**
1124  * @brief Structure for describing known devices for a controller.
1125  *
1126  * This contains arrays of known I3C and I2C devices.
1127  *
1128  * This is a helper struct that can be used by controller device
1129  * driver to aid in device management.
1130  */
1131 struct i3c_dev_list {
1132 	/**
1133 	 * Pointer to array of known I3C devices.
1134 	 */
1135 	struct i3c_device_desc * const i3c;
1136 
1137 	/**
1138 	 * Pointer to array of known I2C devices.
1139 	 */
1140 	struct i3c_i2c_device_desc * const i2c;
1141 
1142 	/**
1143 	 * Number of I3C devices in array.
1144 	 */
1145 	const uint8_t num_i3c;
1146 
1147 	/**
1148 	 * Number of I2C devices in array.
1149 	 */
1150 	const uint8_t num_i2c;
1151 };
1152 
1153 /**
1154  * This structure is common to all I3C drivers and is expected to be
1155  * the first element in the object pointed to by the config field
1156  * in the device structure.
1157  */
1158 struct i3c_driver_config {
1159 	/** I3C/I2C device list struct. */
1160 	struct i3c_dev_list dev_list;
1161 };
1162 
1163 /**
1164  * This structure is common to all I3C drivers and is expected to be the first
1165  * element in the driver's struct driver_data declaration.
1166  */
1167 struct i3c_driver_data {
1168 	/** Controller Configuration */
1169 	struct i3c_config_controller ctrl_config;
1170 
1171 	/** Attached I3C/I2C devices and addresses */
1172 	struct i3c_dev_attached_list attached_dev;
1173 };
1174 
1175 /**
1176  * @brief Find a I3C target device descriptor by ID.
1177  *
1178  * This finds the I3C target device descriptor in the device list
1179  * matching the provided ID struct (@p id).
1180  *
1181  * @param dev_list Pointer to the device list struct.
1182  * @param id Pointer to I3C device ID struct.
1183  *
1184  * @return Pointer the the I3C target device descriptor, or
1185  *         NULL if none is found.
1186  */
1187 struct i3c_device_desc *i3c_dev_list_find(const struct i3c_dev_list *dev_list,
1188 					  const struct i3c_device_id *id);
1189 
1190 /**
1191  * @brief Find a I3C target device descriptor by dynamic address.
1192  *
1193  * This finds the I3C target device descriptor in the attached
1194  * device list matching the dynamic address (@p addr)
1195  *
1196  * @param dev_list Pointer to the device list struct.
1197  * @param addr Dynamic address to be matched.
1198  *
1199  * @return Pointer the the I3C target device descriptor, or
1200  *         NULL if none is found.
1201  */
1202 struct i3c_device_desc *i3c_dev_list_i3c_addr_find(struct i3c_dev_attached_list *dev_list,
1203 						   uint8_t addr);
1204 
1205 /**
1206  * @brief Find a I2C target device descriptor by address.
1207  *
1208  * This finds the I2C target device descriptor in the attached
1209  * device list matching the address (@p addr)
1210  *
1211  * @param dev_list Pointer to the device list struct.
1212  * @param addr Address to be matched.
1213  *
1214  * @return Pointer the the I2C target device descriptor, or
1215  *         NULL if none is found.
1216  */
1217 struct i3c_i2c_device_desc *i3c_dev_list_i2c_addr_find(struct i3c_dev_attached_list *dev_list,
1218 							   uint16_t addr);
1219 
1220 /**
1221  * @brief Helper function to find the default address an i3c device is attached with
1222  *
1223  * This is a helper function to find the default address the
1224  * device will be loaded with. This could be either it's static
1225  * address, a requested dynamic address, or just a dynamic address
1226  * that is available
1227  * @param[in] target The pointer of the device descriptor
1228  * @param[out] addr Address to be assigned to target device.
1229  *
1230  * @retval 0 if successful.
1231  * @retval -EINVAL if the expected default address is already in use
1232  */
1233 int i3c_determine_default_addr(struct i3c_device_desc *target, uint8_t *addr);
1234 
1235 /**
1236  * @brief Helper function to find a usable address during ENTDAA.
1237  *
1238  * This is a helper function to find a usable address during
1239  * Dynamic Address Assignment. Given the PID (@p pid), it will
1240  * search through the device list for the matching device
1241  * descriptor. If the device descriptor indicates that there is
1242  * a preferred address (i.e. assigned-address in device tree,
1243  * @see i3c_device_desc::init_dynamic_addr), this preferred
1244  * address will be returned if this address is still available.
1245  * If it is not available, another free address will be returned.
1246  *
1247  * If @p must_match is true, the PID (@p pid) must match
1248  * one of the device in the device list.
1249  *
1250  * If @p must_match is false, this will return an arbitrary
1251  * address. This is useful when not all devices are described in
1252  * device tree. Or else, the DAA process cannot proceed since
1253  * there is no address to be assigned.
1254  *
1255  * If @p assigned_okay is true, it will return the same address
1256  * already assigned to the device
1257  * (@see i3c_device_desc::dynamic_addr). If no address has been
1258  * assigned, it behaves as if @p assigned_okay is false.
1259  * This is useful for assigning the same address to the same
1260  * device (for example, hot-join after device coming back from
1261  * suspend).
1262  *
1263  * If @p assigned_okay is false, the device cannot have an address
1264  * assigned already (that @see i3c_device_desc::dynamic_addr is not
1265  * zero). This is mainly used during the initial DAA.
1266  *
1267  * @param[in] addr_slots Pointer to address slots struct.
1268  * @param[in] dev_list Pointer to the device list struct.
1269  * @param[in] pid Provisioned ID of device to be assigned address.
1270  * @param[in] must_match True if PID must match devices in
1271  *			 the device list. False otherwise.
1272  * @param[in] assigned_okay True if it is okay to return the
1273  *                          address already assigned to the target
1274  *                          matching the PID (@p pid).
1275  * @param[out] target Store the pointer of the device descriptor
1276  *                    if it matches the incoming PID (@p pid).
1277  * @param[out] addr Address to be assigned to target device.
1278  *
1279  * @retval 0 if successful.
1280  * @retval -ENODEV if no device matches the PID (@p pid) in
1281  *                 the device list and @p must_match is true.
1282  * @retval -EINVAL if the device matching PID (@p pid) already
1283  *                 has an address assigned or invalid function
1284  *                 arguments.
1285  */
1286 int i3c_dev_list_daa_addr_helper(struct i3c_addr_slots *addr_slots,
1287 				 const struct i3c_dev_list *dev_list,
1288 				 uint64_t pid, bool must_match,
1289 				 bool assigned_okay,
1290 				 struct i3c_device_desc **target,
1291 				 uint8_t *addr);
1292 
1293 /**
1294  * @brief Configure the I3C hardware.
1295  *
1296  * @param dev Pointer to controller device driver instance.
1297  * @param type Type of configuration parameters being passed
1298  *             in @p config.
1299  * @param config Pointer to the configuration parameters.
1300  *
1301  * @retval 0 If successful.
1302  * @retval -EINVAL If invalid configure parameters.
1303  * @retval -EIO General Input/Output errors.
1304  * @retval -ENOSYS If not implemented.
1305  */
i3c_configure(const struct device * dev,enum i3c_config_type type,void * config)1306 static inline int i3c_configure(const struct device *dev,
1307 				enum i3c_config_type type, void *config)
1308 {
1309 	const struct i3c_driver_api *api =
1310 		(const struct i3c_driver_api *)dev->api;
1311 
1312 	if (api->configure == NULL) {
1313 		return -ENOSYS;
1314 	}
1315 
1316 	return api->configure(dev, type, config);
1317 }
1318 
1319 /**
1320  * @brief Get configuration of the I3C hardware.
1321  *
1322  * This provides a way to get the current configuration of the I3C hardware.
1323  *
1324  * This can return cached config or probed hardware parameters, but it has to
1325  * be up to date with current configuration.
1326  *
1327  * @param[in] dev Pointer to controller device driver instance.
1328  * @param[in] type Type of configuration parameters being passed
1329  *                 in @p config.
1330  * @param[in,out] config Pointer to the configuration parameters.
1331  *
1332  * Note that if @p type is @c I3C_CONFIG_CUSTOM, @p config must contain
1333  * the ID of the parameter to be retrieved.
1334  *
1335  * @retval 0 If successful.
1336  * @retval -EIO General Input/Output errors.
1337  * @retval -ENOSYS If not implemented.
1338  */
i3c_config_get(const struct device * dev,enum i3c_config_type type,void * config)1339 static inline int i3c_config_get(const struct device *dev,
1340 				 enum i3c_config_type type, void *config)
1341 {
1342 	const struct i3c_driver_api *api =
1343 		(const struct i3c_driver_api *)dev->api;
1344 
1345 	if (api->config_get == NULL) {
1346 		return -ENOSYS;
1347 	}
1348 
1349 	return api->config_get(dev, type, config);
1350 }
1351 
1352 /**
1353  * @brief Attempt bus recovery on the I3C bus.
1354  *
1355  * This routine asks the controller to attempt bus recovery.
1356  *
1357  * @retval 0 If successful.
1358  * @retval -EBUSY If bus recovery fails.
1359  * @retval -EIO General input / output error.
1360  * @retval -ENOSYS Bus recovery is not supported by the controller driver.
1361  */
i3c_recover_bus(const struct device * dev)1362 static inline int i3c_recover_bus(const struct device *dev)
1363 {
1364 	const struct i3c_driver_api *api =
1365 		(const struct i3c_driver_api *)dev->api;
1366 
1367 	if (api->recover_bus == NULL) {
1368 		return -ENOSYS;
1369 	}
1370 
1371 	return api->recover_bus(dev);
1372 }
1373 
1374 /**
1375  * @brief Attach an I3C device
1376  *
1377  * Called to attach a I3C device to the addresses. This is
1378  * typically called before a SETDASA or ENTDAA to reserve
1379  * the addresses. This will also call the optional api to
1380  * update any registers within the driver if implemented.
1381  *
1382  * @warning
1383  * Use cases involving multiple writers to the i3c/i2c devices must prevent
1384  * concurrent write operations, either by preventing all writers from
1385  * being preempted or by using a mutex to govern writes to the i3c/i2c devices.
1386  *
1387  * @param target Pointer to the target device descriptor
1388  *
1389  * @retval 0 If successful.
1390  * @retval -EINVAL If address is not available or if the device
1391  *     has already been attached before
1392  */
1393 int i3c_attach_i3c_device(struct i3c_device_desc *target);
1394 
1395 /**
1396  * @brief Reattach I3C device
1397  *
1398  * called after every time an I3C device has its address
1399  * changed. It can be because the device has been powered
1400  * down and has lost its address, or it can happen when a
1401  * device had a static address and has been assigned a
1402  * dynamic address with SETDASA or a dynamic address has
1403  * been updated with SETNEWDA. This will also call the
1404  * optional api to update any registers within the driver
1405  * if implemented.
1406  *
1407  * @warning
1408  * Use cases involving multiple writers to the i3c/i2c devices must prevent
1409  * concurrent write operations, either by preventing all writers from
1410  * being preempted or by using a mutex to govern writes to the i3c/i2c devices.
1411  *
1412  * @param target Pointer to the target device descriptor
1413  * @param old_dyn_addr The old dynamic address of target device, 0 if
1414  *            there was no old dynamic address
1415  *
1416  * @retval 0 If successful.
1417  * @retval -EINVAL If address is not available
1418  */
1419 int i3c_reattach_i3c_device(struct i3c_device_desc *target, uint8_t old_dyn_addr);
1420 
1421 /**
1422  * @brief Detach I3C Device
1423  *
1424  * called to remove an I3C device and to free up the address
1425  * that it used. If it's dynamic address was not set, then it
1426  * assumed that SETDASA failed and will free it's static addr.
1427  * This will also call the optional api to update any registers
1428  * within the driver if implemented.
1429  *
1430  * @warning
1431  * Use cases involving multiple writers to the i3c/i2c devices must prevent
1432  * concurrent write operations, either by preventing all writers from
1433  * being preempted or by using a mutex to govern writes to the i3c/i2c devices.
1434  *
1435  * @param target Pointer to the target device descriptor
1436  *
1437  * @retval 0 If successful.
1438  * @retval -EINVAL If device is already detached
1439  */
1440 int i3c_detach_i3c_device(struct i3c_device_desc *target);
1441 
1442 /**
1443  * @brief Attach an I2C device
1444  *
1445  * Called to attach a I2C device to the addresses. This will
1446  * also call the optional api to update any registers within
1447  * the driver if implemented.
1448  *
1449  * @warning
1450  * Use cases involving multiple writers to the i3c/i2c devices must prevent
1451  * concurrent write operations, either by preventing all writers from
1452  * being preempted or by using a mutex to govern writes to the i3c/i2c devices.
1453  *
1454  * @param target Pointer to the target device descriptor
1455  *
1456  * @retval 0 If successful.
1457  * @retval -EINVAL If address is not available or if the device
1458  *     has already been attached before
1459  */
1460 int i3c_attach_i2c_device(struct i3c_i2c_device_desc *target);
1461 
1462 /**
1463  * @brief Detach I2C Device
1464  *
1465  * called to remove an I2C device and to free up the address
1466  * that it used. This will also call the optional api to
1467  * update any registers within the driver if implemented.
1468  *
1469  * @warning
1470  * Use cases involving multiple writers to the i3c/i2c devices must prevent
1471  * concurrent write operations, either by preventing all writers from
1472  * being preempted or by using a mutex to govern writes to the i3c/i2c devices.
1473  *
1474  * @param target Pointer to the target device descriptor
1475  *
1476  * @retval 0 If successful.
1477  * @retval -EINVAL If device is already detached
1478  */
1479 int i3c_detach_i2c_device(struct i3c_i2c_device_desc *target);
1480 
1481 /**
1482  * @brief Perform Dynamic Address Assignment on the I3C bus.
1483  *
1484  * This routine asks the controller to perform dynamic address assignment
1485  * where the controller belongs. Only the active controller of the bus
1486  * should do this.
1487  *
1488  * @note For controller driver implementation, the controller should perform
1489  * SETDASA to allow static addresses to be the dynamic addresses before
1490  * actually doing ENTDAA.
1491  *
1492  * @param dev Pointer to the device structure for the controller driver
1493  *            instance.
1494  *
1495  * @retval 0 If successful.
1496  * @retval -EBUSY Bus is busy.
1497  * @retval -EIO General input / output error.
1498  * @retval -ENODEV If a provisioned ID does not match to any target devices
1499  *                 in the registered device list.
1500  * @retval -ENOSPC No more free addresses can be assigned to target.
1501  * @retval -ENOSYS Dynamic address assignment is not supported by
1502  *                 the controller driver.
1503  */
i3c_do_daa(const struct device * dev)1504 static inline int i3c_do_daa(const struct device *dev)
1505 {
1506 	const struct i3c_driver_api *api =
1507 		(const struct i3c_driver_api *)dev->api;
1508 
1509 	if (api->do_daa == NULL) {
1510 		return -ENOSYS;
1511 	}
1512 
1513 	return api->do_daa(dev);
1514 }
1515 
1516 /**
1517  * @brief Send CCC to the bus.
1518  *
1519  * @param dev Pointer to the device structure for the controller driver
1520  *            instance.
1521  * @param payload Pointer to the structure describing the CCC payload.
1522  *
1523  * @retval 0 If successful.
1524  * @retval -EBUSY Bus is busy.
1525  * @retval -EIO General Input / output error.
1526  * @retval -EINVAL Invalid valid set in the payload structure.
1527  * @retval -ENOSYS Not implemented.
1528  */
1529 __syscall int i3c_do_ccc(const struct device *dev,
1530 			 struct i3c_ccc_payload *payload);
1531 
z_impl_i3c_do_ccc(const struct device * dev,struct i3c_ccc_payload * payload)1532 static inline int z_impl_i3c_do_ccc(const struct device *dev,
1533 				    struct i3c_ccc_payload *payload)
1534 {
1535 	const struct i3c_driver_api *api =
1536 		(const struct i3c_driver_api *)dev->api;
1537 
1538 	if (api->do_ccc == NULL) {
1539 		return -ENOSYS;
1540 	}
1541 
1542 	return api->do_ccc(dev, payload);
1543 }
1544 
1545 /**
1546  * @addtogroup i3c_transfer_api
1547  * @{
1548  */
1549 
1550 /**
1551  * @brief Perform data transfer from the controller to a I3C target device.
1552  *
1553  * This routine provides a generic interface to perform data transfer
1554  * to a target device synchronously. Use i3c_read()/i3c_write()
1555  * for simple read or write.
1556  *
1557  * The array of message @a msgs must not be NULL.  The number of
1558  * message @a num_msgs may be zero, in which case no transfer occurs.
1559  *
1560  * @note Not all scatter/gather transactions can be supported by all
1561  * drivers.  As an example, a gather write (multiple consecutive
1562  * `i3c_msg` buffers all configured for `I3C_MSG_WRITE`) may be packed
1563  * into a single transaction by some drivers, but others may emit each
1564  * fragment as a distinct write transaction, which will not produce
1565  * the same behavior.  See the documentation of `struct i3c_msg` for
1566  * limitations on support for multi-message bus transactions.
1567  *
1568  * @param target I3C target device descriptor.
1569  * @param msgs Array of messages to transfer.
1570  * @param num_msgs Number of messages to transfer.
1571  *
1572  * @retval 0 If successful.
1573  * @retval -EBUSY Bus is busy.
1574  * @retval -EIO General input / output error.
1575  */
1576 __syscall int i3c_transfer(struct i3c_device_desc *target,
1577 			   struct i3c_msg *msgs, uint8_t num_msgs);
1578 
z_impl_i3c_transfer(struct i3c_device_desc * target,struct i3c_msg * msgs,uint8_t num_msgs)1579 static inline int z_impl_i3c_transfer(struct i3c_device_desc *target,
1580 				      struct i3c_msg *msgs, uint8_t num_msgs)
1581 {
1582 	const struct i3c_driver_api *api =
1583 		(const struct i3c_driver_api *)target->bus->api;
1584 
1585 	return api->i3c_xfers(target->bus, target, msgs, num_msgs);
1586 }
1587 
1588 /** @} */
1589 
1590 /**
1591  * Find a registered I3C target device.
1592  *
1593  * Controller only API.
1594  *
1595  * This returns the I3C device descriptor of the I3C device
1596  * matching the incoming @p id.
1597  *
1598  * @param dev Pointer to controller device driver instance.
1599  * @param id Pointer to I3C device ID.
1600  *
1601  * @return Pointer to I3C device descriptor, or NULL if
1602  *         no I3C device found matching incoming @p id.
1603  */
1604 static inline
i3c_device_find(const struct device * dev,const struct i3c_device_id * id)1605 struct i3c_device_desc *i3c_device_find(const struct device *dev,
1606 					const struct i3c_device_id *id)
1607 {
1608 	const struct i3c_driver_api *api =
1609 		(const struct i3c_driver_api *)dev->api;
1610 
1611 	if (api->i3c_device_find == NULL) {
1612 		return NULL;
1613 	}
1614 
1615 	return api->i3c_device_find(dev, id);
1616 }
1617 
1618 /**
1619  * @addtogroup i3c_ibi
1620  * @{
1621  */
1622 
1623 /**
1624  * @brief Raise an In-Band Interrupt (IBI).
1625  *
1626  * This raises an In-Band Interrupt (IBI) to the active controller.
1627  *
1628  * @param dev Pointer to controller device driver instance.
1629  * @param request Pointer to the IBI request struct.
1630  *
1631  * @retval 0 if operation is successful.
1632  * @retval -EIO General input / output error.
1633  */
i3c_ibi_raise(const struct device * dev,struct i3c_ibi * request)1634 static inline int i3c_ibi_raise(const struct device *dev,
1635 				struct i3c_ibi *request)
1636 {
1637 	const struct i3c_driver_api *api =
1638 		(const struct i3c_driver_api *)dev->api;
1639 
1640 	if (api->ibi_raise == NULL) {
1641 		return -ENOSYS;
1642 	}
1643 
1644 	return api->ibi_raise(dev, request);
1645 }
1646 
1647 /**
1648  * @brief Enable IBI of a target device.
1649  *
1650  * This enables IBI of a target device where the IBI has already been
1651  * request.
1652  *
1653  * @param target I3C target device descriptor.
1654  *
1655  * @retval 0 If successful.
1656  * @retval -EIO General Input / output error.
1657  * @retval -ENOMEM If these is no more empty entries in
1658  *                 the controller's IBI table (if the controller
1659  *                 uses such table).
1660  */
i3c_ibi_enable(struct i3c_device_desc * target)1661 static inline int i3c_ibi_enable(struct i3c_device_desc *target)
1662 {
1663 	const struct i3c_driver_api *api =
1664 		(const struct i3c_driver_api *)target->bus->api;
1665 
1666 	if (api->ibi_enable == NULL) {
1667 		return -ENOSYS;
1668 	}
1669 
1670 	return api->ibi_enable(target->bus, target);
1671 }
1672 
1673 /**
1674  * @brief Disable IBI of a target device.
1675  *
1676  * This enables IBI of a target device where the IBI has already been
1677  * request.
1678  *
1679  * @param target I3C target device descriptor.
1680  *
1681  * @retval 0 If successful.
1682  * @retval -EIO General Input / output error.
1683  * @retval -ENODEV If IBI is not previously enabled for @p target.
1684  */
i3c_ibi_disable(struct i3c_device_desc * target)1685 static inline int i3c_ibi_disable(struct i3c_device_desc *target)
1686 {
1687 	const struct i3c_driver_api *api =
1688 		(const struct i3c_driver_api *)target->bus->api;
1689 
1690 	if (api->ibi_disable == NULL) {
1691 		return -ENOSYS;
1692 	}
1693 
1694 	return api->ibi_disable(target->bus, target);
1695 }
1696 
1697 /**
1698  * @brief Check if target's IBI has payload.
1699  *
1700  * This reads the BCR from the device descriptor struct to determine
1701  * whether IBI from device has payload.
1702  *
1703  * Note that BCR must have been obtained from device and
1704  * @see i3c_device_desc::bcr must be set.
1705  *
1706  * @return True if IBI has payload, false otherwise.
1707  */
i3c_ibi_has_payload(struct i3c_device_desc * target)1708 static inline int i3c_ibi_has_payload(struct i3c_device_desc *target)
1709 {
1710 	return (target->bcr & I3C_BCR_IBI_PAYLOAD_HAS_DATA_BYTE)
1711 		== I3C_BCR_IBI_PAYLOAD_HAS_DATA_BYTE;
1712 }
1713 
1714 /**
1715  * @brief Check if device is IBI capable.
1716  *
1717  * This reads the BCR from the device descriptor struct to determine
1718  * whether device is capable of IBI.
1719  *
1720  * Note that BCR must have been obtained from device and
1721  * @see i3c_device_desc::bcr must be set.
1722  *
1723  * @return True if IBI has payload, false otherwise.
1724  */
i3c_device_is_ibi_capable(struct i3c_device_desc * target)1725 static inline int i3c_device_is_ibi_capable(struct i3c_device_desc *target)
1726 {
1727 	return (target->bcr & I3C_BCR_IBI_REQUEST_CAPABLE)
1728 		== I3C_BCR_IBI_REQUEST_CAPABLE;
1729 }
1730 
1731 /** @} */
1732 
1733 /**
1734  * @addtogroup i3c_transfer_api
1735  * @{
1736  */
1737 
1738 /**
1739  * @brief Write a set amount of data to an I3C target device.
1740  *
1741  * This routine writes a set amount of data synchronously.
1742  *
1743  * @param target I3C target device descriptor.
1744  * @param buf Memory pool from which the data is transferred.
1745  * @param num_bytes Number of bytes to write.
1746  *
1747  * @retval 0 If successful.
1748  * @retval -EBUSY Bus is busy.
1749  * @retval -EIO General input / output error.
1750  */
i3c_write(struct i3c_device_desc * target,const uint8_t * buf,uint32_t num_bytes)1751 static inline int i3c_write(struct i3c_device_desc *target,
1752 			    const uint8_t *buf, uint32_t num_bytes)
1753 {
1754 	struct i3c_msg msg;
1755 
1756 	msg.buf = (uint8_t *)buf;
1757 	msg.len = num_bytes;
1758 	msg.flags = I3C_MSG_WRITE | I3C_MSG_STOP;
1759 
1760 	return i3c_transfer(target, &msg, 1);
1761 }
1762 
1763 /**
1764  * @brief Read a set amount of data from an I3C target device.
1765  *
1766  * This routine reads a set amount of data synchronously.
1767  *
1768  * @param target I3C target device descriptor.
1769  * @param buf Memory pool that stores the retrieved data.
1770  * @param num_bytes Number of bytes to read.
1771  *
1772  * @retval 0 If successful.
1773  * @retval -EBUSY Bus is busy.
1774  * @retval -EIO General input / output error.
1775  */
i3c_read(struct i3c_device_desc * target,uint8_t * buf,uint32_t num_bytes)1776 static inline int i3c_read(struct i3c_device_desc *target,
1777 			   uint8_t *buf, uint32_t num_bytes)
1778 {
1779 	struct i3c_msg msg;
1780 
1781 	msg.buf = buf;
1782 	msg.len = num_bytes;
1783 	msg.flags = I3C_MSG_READ | I3C_MSG_STOP;
1784 
1785 	return i3c_transfer(target, &msg, 1);
1786 }
1787 
1788 /**
1789  * @brief Write then read data from an I3C target device.
1790  *
1791  * This supports the common operation "this is what I want", "now give
1792  * it to me" transaction pair through a combined write-then-read bus
1793  * transaction.
1794  *
1795  * @param target I3C target device descriptor.
1796  * @param write_buf Pointer to the data to be written
1797  * @param num_write Number of bytes to write
1798  * @param read_buf Pointer to storage for read data
1799  * @param num_read Number of bytes to read
1800  *
1801  * @retval 0 if successful
1802  * @retval -EBUSY Bus is busy.
1803  * @retval -EIO General input / output error.
1804  */
i3c_write_read(struct i3c_device_desc * target,const void * write_buf,size_t num_write,void * read_buf,size_t num_read)1805 static inline int i3c_write_read(struct i3c_device_desc *target,
1806 				 const void *write_buf, size_t num_write,
1807 				 void *read_buf, size_t num_read)
1808 {
1809 	struct i3c_msg msg[2];
1810 
1811 	msg[0].buf = (uint8_t *)write_buf;
1812 	msg[0].len = num_write;
1813 	msg[0].flags = I3C_MSG_WRITE;
1814 
1815 	msg[1].buf = (uint8_t *)read_buf;
1816 	msg[1].len = num_read;
1817 	msg[1].flags = I3C_MSG_RESTART | I3C_MSG_READ | I3C_MSG_STOP;
1818 
1819 	return i3c_transfer(target, msg, 2);
1820 }
1821 
1822 /**
1823  * @brief Read multiple bytes from an internal address of an I3C target device.
1824  *
1825  * This routine reads multiple bytes from an internal address of an
1826  * I3C target device synchronously.
1827  *
1828  * Instances of this may be replaced by i3c_write_read().
1829  *
1830  * @param target I3C target device descriptor,
1831  * @param start_addr Internal address from which the data is being read.
1832  * @param buf Memory pool that stores the retrieved data.
1833  * @param num_bytes Number of bytes being read.
1834  *
1835  * @retval 0 If successful.
1836  * @retval -EBUSY Bus is busy.
1837  * @retval -EIO General input / output error.
1838  */
i3c_burst_read(struct i3c_device_desc * target,uint8_t start_addr,uint8_t * buf,uint32_t num_bytes)1839 static inline int i3c_burst_read(struct i3c_device_desc *target,
1840 				 uint8_t start_addr,
1841 				 uint8_t *buf,
1842 				 uint32_t num_bytes)
1843 {
1844 	return i3c_write_read(target,
1845 			      &start_addr, sizeof(start_addr),
1846 			      buf, num_bytes);
1847 }
1848 
1849 /**
1850  * @brief Write multiple bytes to an internal address of an I3C target device.
1851  *
1852  * This routine writes multiple bytes to an internal address of an
1853  * I3C target device synchronously.
1854  *
1855  * @warning The combined write synthesized by this API may not be
1856  * supported on all I3C devices.  Uses of this API may be made more
1857  * portable by replacing them with calls to i3c_write() passing a
1858  * buffer containing the combined address and data.
1859  *
1860  * @param target I3C target device descriptor.
1861  * @param start_addr Internal address to which the data is being written.
1862  * @param buf Memory pool from which the data is transferred.
1863  * @param num_bytes Number of bytes being written.
1864  *
1865  * @retval 0 If successful.
1866  * @retval -EBUSY Bus is busy.
1867  * @retval -EIO General input / output error.
1868  */
i3c_burst_write(struct i3c_device_desc * target,uint8_t start_addr,const uint8_t * buf,uint32_t num_bytes)1869 static inline int i3c_burst_write(struct i3c_device_desc *target,
1870 				  uint8_t start_addr,
1871 				  const uint8_t *buf,
1872 				  uint32_t num_bytes)
1873 {
1874 	struct i3c_msg msg[2];
1875 
1876 	msg[0].buf = &start_addr;
1877 	msg[0].len = 1U;
1878 	msg[0].flags = I3C_MSG_WRITE;
1879 
1880 	msg[1].buf = (uint8_t *)buf;
1881 	msg[1].len = num_bytes;
1882 	msg[1].flags = I3C_MSG_WRITE | I3C_MSG_STOP;
1883 
1884 	return i3c_transfer(target, msg, 2);
1885 }
1886 
1887 /**
1888  * @brief Read internal register of an I3C target device.
1889  *
1890  * This routine reads the value of an 8-bit internal register of an I3C target
1891  * device synchronously.
1892  *
1893  * @param target I3C target device descriptor.
1894  * @param reg_addr Address of the internal register being read.
1895  * @param value Memory pool that stores the retrieved register value.
1896  *
1897  * @retval 0 If successful.
1898  * @retval -EBUSY Bus is busy.
1899  * @retval -EIO General input / output error.
1900  */
i3c_reg_read_byte(struct i3c_device_desc * target,uint8_t reg_addr,uint8_t * value)1901 static inline int i3c_reg_read_byte(struct i3c_device_desc *target,
1902 				    uint8_t reg_addr, uint8_t *value)
1903 {
1904 	return i3c_write_read(target,
1905 			      &reg_addr, sizeof(reg_addr),
1906 			      value, sizeof(*value));
1907 }
1908 
1909 /**
1910  * @brief Write internal register of an I3C target device.
1911  *
1912  * This routine writes a value to an 8-bit internal register of an I3C target
1913  * device synchronously.
1914  *
1915  * @note This function internally combines the register and value into
1916  * a single bus transaction.
1917  *
1918  * @param target I3C target device descriptor.
1919  * @param reg_addr Address of the internal register being written.
1920  * @param value Value to be written to internal register.
1921  *
1922  * @retval 0 If successful.
1923  * @retval -EBUSY Bus is busy.
1924  * @retval -EIO General input / output error.
1925  */
i3c_reg_write_byte(struct i3c_device_desc * target,uint8_t reg_addr,uint8_t value)1926 static inline int i3c_reg_write_byte(struct i3c_device_desc *target,
1927 				     uint8_t reg_addr, uint8_t value)
1928 {
1929 	uint8_t tx_buf[2] = {reg_addr, value};
1930 
1931 	return i3c_write(target, tx_buf, 2);
1932 }
1933 
1934 /**
1935  * @brief Update internal register of an I3C target device.
1936  *
1937  * This routine updates the value of a set of bits from an 8-bit internal
1938  * register of an I3C target device synchronously.
1939  *
1940  * @note If the calculated new register value matches the value that
1941  * was read this function will not generate a write operation.
1942  *
1943  * @param target I3C target device descriptor.
1944  * @param reg_addr Address of the internal register being updated.
1945  * @param mask Bitmask for updating internal register.
1946  * @param value Value for updating internal register.
1947  *
1948  * @retval 0 If successful.
1949  * @retval -EBUSY Bus is busy.
1950  * @retval -EIO General input / output error.
1951  */
i3c_reg_update_byte(struct i3c_device_desc * target,uint8_t reg_addr,uint8_t mask,uint8_t value)1952 static inline int i3c_reg_update_byte(struct i3c_device_desc *target,
1953 				      uint8_t reg_addr, uint8_t mask,
1954 				      uint8_t value)
1955 {
1956 	uint8_t old_value, new_value;
1957 	int rc;
1958 
1959 	rc = i3c_reg_read_byte(target, reg_addr, &old_value);
1960 	if (rc != 0) {
1961 		return rc;
1962 	}
1963 
1964 	new_value = (old_value & ~mask) | (value & mask);
1965 	if (new_value == old_value) {
1966 		return 0;
1967 	}
1968 
1969 	return i3c_reg_write_byte(target, reg_addr, new_value);
1970 }
1971 
1972 /**
1973  * @brief Dump out an I3C message
1974  *
1975  * Dumps out a list of I3C messages. For any that are writes (W), the data is
1976  * displayed in hex.
1977  *
1978  * It looks something like this (with name "testing"):
1979  *
1980  * @code
1981  * D: I3C msg: testing, addr=56
1982  * D:    W len=01:
1983  * D: contents:
1984  * D: 06                      |.
1985  * D:    W len=0e:
1986  * D: contents:
1987  * D: 00 01 02 03 04 05 06 07 |........
1988  * D: 08 09 0a 0b 0c 0d       |......
1989  * @endcode
1990  *
1991  * @param name Name of this dump, displayed at the top.
1992  * @param msgs Array of messages to dump.
1993  * @param num_msgs Number of messages to dump.
1994  * @param target I3C target device descriptor.
1995  */
1996 void i3c_dump_msgs(const char *name, const struct i3c_msg *msgs,
1997 		   uint8_t num_msgs, struct i3c_device_desc *target);
1998 
1999 /** @} */
2000 
2001 /**
2002  * @brief Generic helper function to perform bus initialization.
2003  *
2004  * @param dev Pointer to controller device driver instance.
2005  * @param i3c_dev_list Pointer to I3C device list.
2006  *
2007  * @retval 0 If successful.
2008  * @retval -EBUSY Bus is busy.
2009  * @retval -EIO General input / output error.
2010  * @retval -ENODEV If a provisioned ID does not match to any target devices
2011  *                 in the registered device list.
2012  * @retval -ENOSPC No more free addresses can be assigned to target.
2013  * @retval -ENOSYS Dynamic address assignment is not supported by
2014  *                 the controller driver.
2015  */
2016 int i3c_bus_init(const struct device *dev,
2017 		 const struct i3c_dev_list *i3c_dev_list);
2018 
2019 /**
2020  * @brief Get basic information from device and update device descriptor.
2021  *
2022  * This retrieves some basic information:
2023  *   * Bus Characteristics Register (GETBCR)
2024  *   * Device Characteristics Register (GETDCR)
2025  *   * Max Read Length (GETMRL)
2026  *   * Max Write Length (GETMWL)
2027  * from the device and update the corresponding fields of the device
2028  * descriptor.
2029  *
2030  * This only updates the field(s) in device descriptor
2031  * only if CCC operations succeed.
2032  *
2033  * @param[in,out] target I3C target device descriptor.
2034  *
2035  * @retval 0 if successful.
2036  * @retval -EIO General Input/Output error.
2037  */
2038 int i3c_device_basic_info_get(struct i3c_device_desc *target);
2039 
2040 /*
2041  * This needs to be after declaration of struct i3c_driver_api,
2042  * or else compiler complains about undefined type inside
2043  * the static inline API wrappers.
2044  */
2045 #include <zephyr/drivers/i3c/target_device.h>
2046 
2047 #ifdef __cplusplus
2048 }
2049 #endif
2050 
2051 /**
2052  * @}
2053  */
2054 
2055 #include <syscalls/i3c.h>
2056 
2057 #endif /* ZEPHYR_INCLUDE_DRIVERS_I3C_H_ */
2058