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 	ret = api->transfer(emul->target, msgs, num_msgs, addr);
94 	if (ret) {
95 		return ret;
96 	}
97 
98 	return 0;
99 }
100 
101 /**
102  * Set up a new emulator and add it to the list
103  *
104  * @param dev I2C emulation controller device
105  */
i2c_emul_init(const struct device * dev)106 static int i2c_emul_init(const struct device *dev)
107 {
108 	struct i2c_emul_data *data = dev->data;
109 	int rc;
110 
111 	sys_slist_init(&data->emuls);
112 
113 	rc = emul_init_for_bus(dev);
114 
115 	/* Set config to an uninitialized state */
116 	data->config = (I2C_MODE_CONTROLLER | i2c_map_dt_bitrate(data->bitrate));
117 
118 	return rc;
119 }
120 
i2c_emul_register(const struct device * dev,struct i2c_emul * emul)121 int i2c_emul_register(const struct device *dev, struct i2c_emul *emul)
122 {
123 	struct i2c_emul_data *data = dev->data;
124 	const char *name = emul->target->dev->name;
125 
126 	sys_slist_append(&data->emuls, &emul->node);
127 
128 	LOG_INF("Register emulator '%s' at I2C addr %02x\n", name, emul->addr);
129 
130 	return 0;
131 }
132 
133 /* Device instantiation */
134 
135 static struct i2c_driver_api i2c_emul_api = {
136 	.configure = i2c_emul_configure,
137 	.get_config = i2c_emul_get_config,
138 	.transfer = i2c_emul_transfer,
139 };
140 
141 #define EMUL_LINK_AND_COMMA(node_id)                                                               \
142 	{                                                                                          \
143 		.dev = DEVICE_DT_GET(node_id),                                                     \
144 	},
145 
146 #define I2C_EMUL_INIT(n)                                                                           \
147 	static const struct emul_link_for_bus emuls_##n[] = {                                      \
148 		DT_FOREACH_CHILD_STATUS_OKAY(DT_DRV_INST(n), EMUL_LINK_AND_COMMA)};                \
149 	static struct emul_list_for_bus i2c_emul_cfg_##n = {                                       \
150 		.children = emuls_##n,                                                             \
151 		.num_children = ARRAY_SIZE(emuls_##n),                                             \
152 	};                                                                                         \
153 	static struct i2c_emul_data i2c_emul_data_##n = {                                          \
154 		.bitrate = DT_INST_PROP(n, clock_frequency),                                       \
155 	};                                                                                         \
156 	I2C_DEVICE_DT_INST_DEFINE(n, i2c_emul_init, NULL, &i2c_emul_data_##n, &i2c_emul_cfg_##n,   \
157 				  POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, &i2c_emul_api);
158 
159 DT_INST_FOREACH_STATUS_OKAY(I2C_EMUL_INIT)
160