1 /**
2  * @file
3  *
4  * @brief Public APIs for the I2C emulation drivers.
5  */
6 
7 /*
8  * Copyright 2020 Google LLC
9  * Copyright (c) 2020 Nordic Semiconductor ASA
10  *
11  * SPDX-License-Identifier: Apache-2.0
12  */
13 #ifndef ZEPHYR_INCLUDE_DRIVERS_I2C_I2C_EMUL_H_
14 #define ZEPHYR_INCLUDE_DRIVERS_I2C_I2C_EMUL_H_
15 
16 /**
17  * @brief I2C Emulation Interface
18  * @defgroup i2c_emul_interface I2C Emulation Interface
19  * @ingroup io_emulators
20  * @{
21  */
22 
23 #include <zephyr/types.h>
24 #include <device.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 struct i2c_msg;
31 struct i2c_emul_api;
32 
33 /** Node in a linked list of emulators for I2C devices */
34 struct i2c_emul {
35 	sys_snode_t node;
36 
37 	/* API provided for this device */
38 	const struct i2c_emul_api *api;
39 
40 	/* I2C address of the emulated device */
41 	uint16_t addr;
42 };
43 
44 /**
45  * Passes I2C messages to the emulator. The emulator updates the data with what
46  * was read back.
47  *
48  * @param emul Emulator instance
49  * @param msgs Array of messages to transfer. For 'read' messages, this function
50  *	updates the 'buf' member with the data that was read
51  * @param num_msgs Number of messages to transfer.
52  * @param addr Address of the I2C target device.
53  *
54  * @retval 0 If successful.
55  * @retval -EIO General input / output error.
56  */
57 typedef int (*i2c_emul_transfer_t)(struct i2c_emul *emul, struct i2c_msg *msgs,
58 				   int num_msgs, int addr);
59 
60 /**
61  * Register an emulated device on the controller
62  *
63  * @param dev Device that will use the emulator
64  * @param name User-friendly name for this emulator
65  * @param emul I2C emulator to use
66  * @return 0 indicating success (always)
67  */
68 int i2c_emul_register(const struct device *dev, const char *name,
69 		      struct i2c_emul *emul);
70 
71 /** Definition of the emulator API */
72 struct i2c_emul_api {
73 	i2c_emul_transfer_t transfer;
74 };
75 
76 /**
77  * Back door to allow an emulator to retrieve the host configuration.
78  *
79  * @param dev I2C device associated with the emulator
80  * @return Bit-packed 32-bit value containing the device's runtime configuration
81  */
82 uint32_t i2c_emul_get_config(const struct device *dev);
83 
84 #ifdef __cplusplus
85 }
86 #endif
87 
88 /**
89  * @}
90  */
91 
92 #endif /* ZEPHYR_INCLUDE_DRIVERS_I2C_I2C_EMUL_H_ */
93