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