1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT zephyr_fake_regulator
7
8 #include <zephyr/devicetree.h>
9 #include <zephyr/drivers/regulator.h>
10 #include <zephyr/drivers/regulator/fake.h>
11 #include <zephyr/fff.h>
12 #include <zephyr/toolchain.h>
13
14 /* regulator */
15
16 struct regulator_fake_config {
17 struct regulator_common_config common;
18 bool is_enabled;
19 };
20
21 struct regulator_fake_data {
22 struct regulator_common_data data;
23 };
24
25 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_enable, const struct device *);
26 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_disable, const struct device *);
27 DEFINE_FAKE_VALUE_FUNC(unsigned int, regulator_fake_count_voltages,
28 const struct device *);
29 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_list_voltage, const struct device *,
30 unsigned int, int32_t *);
31 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_set_voltage, const struct device *,
32 int32_t, int32_t);
33 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_voltage, const struct device *,
34 int32_t *);
35 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_set_current_limit,
36 const struct device *, int32_t, int32_t);
37 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_current_limit,
38 const struct device *, int32_t *);
39 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_set_mode, const struct device *,
40 regulator_mode_t);
41 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_mode, const struct device *,
42 regulator_mode_t *);
43 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_set_active_discharge, const struct device *,
44 bool);
45 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_active_discharge, const struct device *,
46 bool *);
47 DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_error_flags,
48 const struct device *, regulator_error_flags_t *);
49
50 static DEVICE_API(regulator, api) = {
51 .enable = regulator_fake_enable,
52 .disable = regulator_fake_disable,
53 .count_voltages = regulator_fake_count_voltages,
54 .list_voltage = regulator_fake_list_voltage,
55 .set_voltage = regulator_fake_set_voltage,
56 .get_voltage = regulator_fake_get_voltage,
57 .set_current_limit = regulator_fake_set_current_limit,
58 .get_current_limit = regulator_fake_get_current_limit,
59 .set_mode = regulator_fake_set_mode,
60 .get_mode = regulator_fake_get_mode,
61 .set_active_discharge = regulator_fake_set_active_discharge,
62 .get_active_discharge = regulator_fake_get_active_discharge,
63 .get_error_flags = regulator_fake_get_error_flags,
64 };
65
regulator_fake_init(const struct device * dev)66 static int regulator_fake_init(const struct device *dev)
67 {
68 const struct regulator_fake_config *config = dev->config;
69
70 regulator_common_data_init(dev);
71
72 return regulator_common_init(dev, config->is_enabled);
73 }
74
75 /* parent regulator */
76
77 DEFINE_FAKE_VALUE_FUNC(int, regulator_parent_fake_dvs_state_set,
78 const struct device *, regulator_dvs_state_t);
79
80 DEFINE_FAKE_VALUE_FUNC(int, regulator_parent_fake_ship_mode,
81 const struct device *);
82
83 static DEVICE_API(regulator_parent, parent_api) = {
84 .dvs_state_set = regulator_parent_fake_dvs_state_set,
85 .ship_mode = regulator_parent_fake_ship_mode,
86 };
87
88 #define FAKE_DATA_NAME(node_id) _CONCAT(data_, DT_DEP_ORD(node_id))
89 #define FAKE_CONF_NAME(node_id) _CONCAT(config_, DT_DEP_ORD(node_id))
90
91 #define REGULATOR_FAKE_DEFINE(node_id) \
92 static struct regulator_fake_data FAKE_DATA_NAME(node_id); \
93 \
94 static const struct regulator_fake_config FAKE_CONF_NAME(node_id) = { \
95 .common = REGULATOR_DT_COMMON_CONFIG_INIT(node_id), \
96 .is_enabled = DT_PROP(node_id, fake_is_enabled_in_hardware), \
97 }; \
98 \
99 DEVICE_DT_DEFINE(node_id, regulator_fake_init, NULL, \
100 &FAKE_DATA_NAME(node_id), &FAKE_CONF_NAME(node_id), \
101 POST_KERNEL, CONFIG_REGULATOR_FAKE_INIT_PRIORITY, \
102 &api);
103
104 #define REGULATOR_FAKE_DEFINE_ALL(inst) \
105 DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, NULL, POST_KERNEL, \
106 CONFIG_REGULATOR_FAKE_COMMON_INIT_PRIORITY, \
107 &parent_api); \
108 \
109 DT_INST_FOREACH_CHILD(inst, REGULATOR_FAKE_DEFINE)
110
111 DT_INST_FOREACH_STATUS_OKAY(REGULATOR_FAKE_DEFINE_ALL)
112