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