1 /*
2  * This driver creates fake I2C buses which can contain emulated devices,
3  * implemented by a separate emulation driver. The API between this driver and
4  * its emulators is defined by struct i2c_emul_driver_api.
5  *
6  * Copyright 2020 Google LLC
7  * Copyright (c) 2020 Nordic Semiconductor ASA
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #define DT_DRV_COMPAT zephyr_i2c_emul_controller
13 
14 #define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_REGISTER(i2c_emul_ctlr);
17 
18 #include <zephyr/device.h>
19 #include <zephyr/drivers/emul.h>
20 #include <zephyr/drivers/i2c.h>
21 #include <zephyr/drivers/i2c_emul.h>
22 
23 #include "i2c-priv.h"
24 
25 /** Working data for the device */
26 struct i2c_emul_data {
27 	/* List of struct i2c_emul associated with the device */
28 	sys_slist_t emuls;
29 	/* I2C host configuration */
30 	uint32_t config;
31 	uint32_t bitrate;
32 };
33 
34 /**
35  * Find an emulator by its I2C address
36  *
37  * @param dev I2C emulation controller device
38  * @param addr I2C address of that device
39  * @return emulator ro use
40  * @return NULL if not found
41  */
i2c_emul_find(const struct device * dev,int addr)42 static struct i2c_emul *i2c_emul_find(const struct device *dev, int addr)
43 {
44 	struct i2c_emul_data *data = dev->data;
45 	sys_snode_t *node;
46 
47 	SYS_SLIST_FOR_EACH_NODE(&data->emuls, node) {
48 		struct i2c_emul *emul = NULL;
49 
50 		emul = CONTAINER_OF(node, struct i2c_emul, node);
51 		if (emul->addr == addr) {
52 			return emul;
53 		}
54 	}
55 
56 	return NULL;
57 }
58 
i2c_emul_configure(const struct device * dev,uint32_t dev_config)59 static int i2c_emul_configure(const struct device *dev, uint32_t dev_config)
60 {
61 	struct i2c_emul_data *data = dev->data;
62 
63 	data->config = dev_config;
64 
65 	return 0;
66 }
67 
i2c_emul_get_config(const struct device * dev,uint32_t * dev_config)68 static int i2c_emul_get_config(const struct device *dev, uint32_t *dev_config)
69 {
70 	struct i2c_emul_data *data = dev->data;
71 
72 	*dev_config = data->config;
73 
74 	return 0;
75 }
76 
i2c_emul_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)77 static int i2c_emul_transfer(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs,
78 			     uint16_t addr)
79 {
80 	struct i2c_emul *emul;
81 	const struct i2c_emul_api *api;
82 	int ret;
83 
84 	emul = i2c_emul_find(dev, addr);
85 	if (!emul) {
86 		return -EIO;
87 	}
88 
89 	api = emul->api;
90 	__ASSERT_NO_MSG(emul->api);
91 	__ASSERT_NO_MSG(emul->api->transfer);
92 
93 	if (emul->mock_api != NULL && emul->mock_api->transfer != NULL) {
94 		ret = emul->mock_api->transfer(emul->target, msgs, num_msgs, addr);
95 		if (ret != -ENOSYS) {
96 			return ret;
97 		}
98 	}
99 
100 	return api->transfer(emul->target, msgs, num_msgs, addr);
101 }
102 
103 /**
104  * Set up a new emulator and add it to the list
105  *
106  * @param dev I2C emulation controller device
107  */
i2c_emul_init(const struct device * dev)108 static int i2c_emul_init(const struct device *dev)
109 {
110 	struct i2c_emul_data *data = dev->data;
111 	int rc;
112 
113 	sys_slist_init(&data->emuls);
114 
115 	rc = emul_init_for_bus(dev);
116 
117 	/* Set config to an uninitialized state */
118 	data->config = (I2C_MODE_CONTROLLER | i2c_map_dt_bitrate(data->bitrate));
119 
120 	return rc;
121 }
122 
i2c_emul_register(const struct device * dev,struct i2c_emul * emul)123 int i2c_emul_register(const struct device *dev, struct i2c_emul *emul)
124 {
125 	struct i2c_emul_data *data = dev->data;
126 	const char *name = emul->target->dev->name;
127 
128 	sys_slist_append(&data->emuls, &emul->node);
129 
130 	LOG_INF("Register emulator '%s' at I2C addr %02x", name, emul->addr);
131 
132 	return 0;
133 }
134 
135 /* Device instantiation */
136 
137 static const struct i2c_driver_api i2c_emul_api = {
138 	.configure = i2c_emul_configure,
139 	.get_config = i2c_emul_get_config,
140 	.transfer = i2c_emul_transfer,
141 };
142 
143 #define EMUL_LINK_AND_COMMA(node_id)                                                               \
144 	{                                                                                          \
145 		.dev = DEVICE_DT_GET(node_id),                                                     \
146 	},
147 
148 #define I2C_EMUL_INIT(n)                                                                           \
149 	static const struct emul_link_for_bus emuls_##n[] = {                                      \
150 		DT_FOREACH_CHILD_STATUS_OKAY(DT_DRV_INST(n), EMUL_LINK_AND_COMMA)};                \
151 	static struct emul_list_for_bus i2c_emul_cfg_##n = {                                       \
152 		.children = emuls_##n,                                                             \
153 		.num_children = ARRAY_SIZE(emuls_##n),                                             \
154 	};                                                                                         \
155 	static struct i2c_emul_data i2c_emul_data_##n = {                                          \
156 		.bitrate = DT_INST_PROP(n, clock_frequency),                                       \
157 	};                                                                                         \
158 	I2C_DEVICE_DT_INST_DEFINE(n, i2c_emul_init, NULL, &i2c_emul_data_##n, &i2c_emul_cfg_##n,   \
159 				  POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, &i2c_emul_api);
160 
161 DT_INST_FOREACH_STATUS_OKAY(I2C_EMUL_INIT)
162