1 /*
2 * Copyright (c) 2022 Vestas Wind Systems A/S
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/eeprom.h>
9 #include <zephyr/drivers/eeprom/eeprom_fake.h>
10 #include <zephyr/fff.h>
11
12 #ifdef CONFIG_ZTEST
13 #include <zephyr/ztest.h>
14 #endif /* CONFIG_ZTEST */
15
16 #define DT_DRV_COMPAT zephyr_fake_eeprom
17
18 struct fake_eeprom_config {
19 size_t size;
20 };
21
22 DEFINE_FAKE_VALUE_FUNC(int, fake_eeprom_read, const struct device *, off_t, void *, size_t);
23
24 DEFINE_FAKE_VALUE_FUNC(int, fake_eeprom_write, const struct device *, off_t, const void *, size_t);
25
26 DEFINE_FAKE_VALUE_FUNC(size_t, fake_eeprom_size, const struct device *);
27
fake_eeprom_size_delegate(const struct device * dev)28 size_t fake_eeprom_size_delegate(const struct device *dev)
29 {
30 const struct fake_eeprom_config *config = dev->config;
31
32 return config->size;
33 }
34
35 #ifdef CONFIG_ZTEST
fake_eeprom_reset_rule_before(const struct ztest_unit_test * test,void * fixture)36 static void fake_eeprom_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
37 {
38 ARG_UNUSED(test);
39 ARG_UNUSED(fixture);
40
41 RESET_FAKE(fake_eeprom_read);
42 RESET_FAKE(fake_eeprom_write);
43 RESET_FAKE(fake_eeprom_size);
44
45 /* Re-install default delegate for reporting the EEPROM size */
46 fake_eeprom_size_fake.custom_fake = fake_eeprom_size_delegate;
47 }
48
49 ZTEST_RULE(fake_eeprom_reset_rule, fake_eeprom_reset_rule_before, NULL);
50 #endif /* CONFIG_ZTEST */
51
52 static const struct eeprom_driver_api fake_eeprom_driver_api = {
53 .read = fake_eeprom_read,
54 .write = fake_eeprom_write,
55 .size = fake_eeprom_size,
56 };
57
fake_eeprom_init(const struct device * dev)58 static int fake_eeprom_init(const struct device *dev)
59 {
60 ARG_UNUSED(dev);
61
62 /* Install default delegate for reporting the EEPROM size */
63 fake_eeprom_size_fake.custom_fake = fake_eeprom_size_delegate;
64
65 return 0;
66 }
67
68 #define FAKE_EEPROM_INIT(inst) \
69 static const struct fake_eeprom_config fake_eeprom_config_##inst = { \
70 .size = DT_INST_PROP(inst, size), \
71 }; \
72 \
73 DEVICE_DT_INST_DEFINE(inst, &fake_eeprom_init, NULL, NULL, \
74 &fake_eeprom_config_##inst, \
75 POST_KERNEL, CONFIG_EEPROM_INIT_PRIORITY, \
76 &fake_eeprom_driver_api);
77
78 DT_INST_FOREACH_STATUS_OKAY(FAKE_EEPROM_INIT)
79