drivers: regulator: add fake driver

Add FFF-based fake regulator driver. This driver can be used as a stub
or mock in testing.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-12-19 12:00:38 +01:00 committed by Fabio Baltieri
parent 63c005d3b4
commit 1080afa680
6 changed files with 184 additions and 0 deletions

View file

@ -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)

View file

@ -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"

View file

@ -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

View file

@ -0,0 +1,102 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT zephyr_fake_regulator
#include <zephyr/devicetree.h>
#include <zephyr/drivers/regulator.h>
#include <zephyr/drivers/regulator/fake.h>
#include <zephyr/fff.h>
#include <zephyr/toolchain.h>
/* 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)

View file

@ -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

View file

@ -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 <zephyr/drivers/regulator.h>
#include <zephyr/fff.h>
#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_ */