diff --git a/drivers/regulator/CMakeLists.txt b/drivers/regulator/CMakeLists.txt index a1099c64974..8e9f3e7ef29 100644 --- a/drivers/regulator/CMakeLists.txt +++ b/drivers/regulator/CMakeLists.txt @@ -4,6 +4,7 @@ zephyr_library() zephyr_library_sources(regulator_common.c) +zephyr_library_sources_ifdef(CONFIG_REGULATOR_FAKE regulator_fake.c) zephyr_library_sources_ifdef(CONFIG_REGULATOR_FIXED regulator_fixed.c) zephyr_library_sources_ifdef(CONFIG_REGULATOR_NPM6001 regulator_npm6001.c) zephyr_library_sources_ifdef(CONFIG_REGULATOR_PCA9420 regulator_pca9420.c) diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig index e3624faafd5..64d325c831a 100644 --- a/drivers/regulator/Kconfig +++ b/drivers/regulator/Kconfig @@ -21,6 +21,7 @@ module = REGULATOR module-str = regulator source "subsys/logging/Kconfig.template.log_config" +source "drivers/regulator/Kconfig.fake" source "drivers/regulator/Kconfig.fixed" source "drivers/regulator/Kconfig.npm6001" source "drivers/regulator/Kconfig.pca9420" diff --git a/drivers/regulator/Kconfig.fake b/drivers/regulator/Kconfig.fake new file mode 100644 index 00000000000..5c6da974cc2 --- /dev/null +++ b/drivers/regulator/Kconfig.fake @@ -0,0 +1,26 @@ +# Copyright (c) 2022 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +config REGULATOR_FAKE + bool "Fake regulator driver" + default y + depends on DT_HAS_ZEPHYR_FAKE_REGULATOR_ENABLED + help + Enable support for the FFF-based fake regulator driver. + +if REGULATOR_FAKE + +config REGULATOR_FAKE_COMMON_INIT_PRIORITY + int "Fake regulator driver init priority (common part)" + default 75 + help + Init priority for the fake regulator driver (common part). + +config REGULATOR_FAKE_INIT_PRIORITY + int "Fake regulator driver init priority" + default 76 + help + Init priority for the fake regulator driver. It must be + greater than REGULATOR_FAKE_COMMON_INIT_PRIORITY. + +endif diff --git a/drivers/regulator/regulator_fake.c b/drivers/regulator/regulator_fake.c new file mode 100644 index 00000000000..0bba43ee9e3 --- /dev/null +++ b/drivers/regulator/regulator_fake.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2022 Nordic Semiconductor ASA + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT zephyr_fake_regulator + +#include +#include +#include +#include +#include + +/* regulator */ + +struct regulator_fake_config { + struct regulator_common_config common; +}; + +struct regulator_fake_data { + struct regulator_common_data data; +}; + +DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_enable, const struct device *); +DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_disable, const struct device *); +DEFINE_FAKE_VALUE_FUNC(unsigned int, regulator_fake_count_voltages, + const struct device *); +DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_list_voltage, const struct device *, + unsigned int, int32_t *); +DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_set_voltage, const struct device *, + int32_t, int32_t); +DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_voltage, const struct device *, + int32_t *); +DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_set_current_limit, + const struct device *, int32_t, int32_t); +DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_current_limit, + const struct device *, int32_t *); +DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_set_mode, const struct device *, + regulator_mode_t); +DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_error_flags, + const struct device *, regulator_error_flags_t *); + +static struct regulator_driver_api api = { + .enable = regulator_fake_enable, + .disable = regulator_fake_disable, + .count_voltages = regulator_fake_count_voltages, + .list_voltage = regulator_fake_list_voltage, + .set_voltage = regulator_fake_set_voltage, + .get_voltage = regulator_fake_get_voltage, + .set_current_limit = regulator_fake_set_current_limit, + .get_current_limit = regulator_fake_get_current_limit, + .set_mode = regulator_fake_set_mode, + .get_error_flags = regulator_fake_get_error_flags, +}; + +static int regulator_fake_init(const struct device *dev) +{ + regulator_common_data_init(dev); + + return regulator_common_init_enable(dev); +} + +/* parent regulator */ + +DEFINE_FAKE_VALUE_FUNC(int, regulator_parent_fake_dvs_state_set, + const struct device *, regulator_dvs_state_t); + +static struct regulator_parent_driver_api parent_api = { + .dvs_state_set = regulator_parent_fake_dvs_state_set, +}; + +static int regulator_fake_common_init(const struct device *dev) +{ + ARG_UNUSED(dev); + + return 0; +} + +#define FAKE_DATA_NAME(node_id) _CONCAT(data_, DT_DEP_ORD(node_id)) +#define FAKE_CONF_NAME(node_id) _CONCAT(config_, DT_DEP_ORD(node_id)) + +#define REGULATOR_FAKE_DEFINE(node_id) \ + static struct regulator_fake_data FAKE_DATA_NAME(node_id); \ + \ + static const struct regulator_fake_config FAKE_CONF_NAME(node_id) = { \ + .common = REGULATOR_DT_COMMON_CONFIG_INIT(node_id), \ + }; \ + \ + DEVICE_DT_DEFINE(node_id, regulator_fake_init, NULL, \ + &FAKE_DATA_NAME(node_id), &FAKE_CONF_NAME(node_id), \ + POST_KERNEL, CONFIG_REGULATOR_FAKE_INIT_PRIORITY, \ + &api); + +#define REGULATOR_FAKE_DEFINE_ALL(inst) \ + DEVICE_DT_INST_DEFINE(inst, regulator_fake_common_init, NULL, NULL, \ + NULL, POST_KERNEL, \ + CONFIG_REGULATOR_FAKE_COMMON_INIT_PRIORITY, \ + &parent_api); \ + \ + DT_INST_FOREACH_CHILD(inst, REGULATOR_FAKE_DEFINE) + +DT_INST_FOREACH_STATUS_OKAY(REGULATOR_FAKE_DEFINE_ALL) diff --git a/dts/bindings/regulator/zephyr,fake-regulator.yaml b/dts/bindings/regulator/zephyr,fake-regulator.yaml new file mode 100644 index 00000000000..7a6894948b2 --- /dev/null +++ b/dts/bindings/regulator/zephyr,fake-regulator.yaml @@ -0,0 +1,12 @@ +# Copyright (c) 2022 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +description: | + Fake regulator, to be used as a mock or stub in tests. + + An arbitrary number of children can be defined. + +compatible: "zephyr,fake-regulator" + +child-binding: + include: regulator.yaml diff --git a/include/zephyr/drivers/regulator/fake.h b/include/zephyr/drivers/regulator/fake.h new file mode 100644 index 00000000000..fe8b161c3c7 --- /dev/null +++ b/include/zephyr/drivers/regulator/fake.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Nordic Semiconductor ASA + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_DRIVERS_REGULATOR_FAKE_H_ +#define ZEPHYR_DRIVERS_REGULATOR_FAKE_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_enable, const struct device *); +DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_disable, const struct device *); +DECLARE_FAKE_VALUE_FUNC(unsigned int, regulator_fake_count_voltages, + const struct device *); +DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_list_voltage, const struct device *, + unsigned int, int32_t *); +DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_set_voltage, const struct device *, + int32_t, int32_t); +DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_get_voltage, const struct device *, + int32_t *); +DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_set_current_limit, + const struct device *, int32_t, int32_t); +DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_get_current_limit, + const struct device *, int32_t *); +DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_set_mode, const struct device *, + regulator_mode_t); +DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_get_error_flags, + const struct device *, regulator_error_flags_t *); + +DECLARE_FAKE_VALUE_FUNC(int, regulator_parent_fake_dvs_state_set, + const struct device *, regulator_dvs_state_t); + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_TESTS_DRIVERS_CAN_SHELL_FAKE_CAN_H_ */