1 /*
2  * Copyright 2023 Google LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "emul_tester.h"
8 
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/emul.h>
11 #include <zephyr/drivers/emul_stub_device.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/drivers/i2c_emul.h>
14 
15 #define DT_DRV_COMPAT vnd_emul_tester
16 
17 struct emul_tester_cfg {
18 	int scale;
19 };
20 
21 struct emul_tester_data {
22 	int action;
23 };
24 
emul_tester_set_action(const struct emul * target,int action)25 static int emul_tester_set_action(const struct emul *target, int action)
26 {
27 	const struct emul_tester_cfg *cfg = target->cfg;
28 	struct emul_tester_data *data = target->data;
29 
30 	data->action = action * cfg->scale;
31 
32 	return 0;
33 }
34 
emul_tester_get_action(const struct emul * target,int * action)35 static int emul_tester_get_action(const struct emul *target, int *action)
36 {
37 	struct emul_tester_data *data = target->data;
38 
39 	*action = data->action;
40 
41 	return 0;
42 }
43 
emul_tester_transfer(const struct emul * target,struct i2c_msg * msgs,int num_msgs,int addr)44 static int emul_tester_transfer(const struct emul *target, struct i2c_msg *msgs, int num_msgs,
45 				int addr)
46 {
47 	ARG_UNUSED(target);
48 	ARG_UNUSED(msgs);
49 	ARG_UNUSED(num_msgs);
50 	ARG_UNUSED(addr);
51 
52 	return -ENOTSUP;
53 }
54 
55 static struct i2c_emul_api bus_api = {
56 	.transfer = emul_tester_transfer,
57 };
58 
59 static const struct emul_tester_backend_api emul_tester_backend_api = {
60 	.set_action = emul_tester_set_action,
61 	.get_action = emul_tester_get_action,
62 };
63 
emul_tester_init(const struct emul * target,const struct device * parent)64 static int emul_tester_init(const struct emul *target, const struct device *parent)
65 {
66 	ARG_UNUSED(target);
67 	ARG_UNUSED(parent);
68 
69 	return 0;
70 }
71 
72 #define EMUL_TESTER(n)                                                                             \
73 	static struct emul_tester_data emul_tester_data_##n;                                       \
74 	static const struct emul_tester_cfg emul_tester_cfg_##n = {                                \
75 		.scale = DT_INST_PROP(n, scale),                                                   \
76 	};                                                                                         \
77 	EMUL_DT_INST_DEFINE(n, emul_tester_init, &emul_tester_data_##n, &emul_tester_cfg_##n,      \
78 			    &bus_api, &emul_tester_backend_api);
79 
80 DT_INST_FOREACH_STATUS_OKAY(EMUL_TESTER)
81 
82 DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE)
83