Commit 5a125a61 authored by Ryan McClelland's avatar Ryan McClelland Committed by Henrik Brix Andersen
Browse files

regulator: add active-discharge api



Add an active discharge api for regulators. This uses the already
existing but previously unused regulator-active-discharge
property.

Signed-off-by: default avatarRyan McClelland <ryanmcclelland@meta.com>
parent 2d9b56d7
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 Nordic Semiconductor ASA
 * Copyright 2023 Meta Platforms
 * SPDX-License-Identifier: Apache-2.0
 */

@@ -42,6 +43,15 @@ int regulator_common_init(const struct device *dev, bool is_enabled)
		}
	}

	if (REGULATOR_ACTIVE_DISCHARGE_GET_BITS(config->flags) !=
	    REGULATOR_ACTIVE_DISCHARGE_DEFAULT) {
		ret = regulator_set_active_discharge(dev,
		    (bool)REGULATOR_ACTIVE_DISCHARGE_GET_BITS(config->flags));
		if (ret < 0) {
			return ret;
		}
	}

	if (config->init_uv > INT32_MIN) {
		ret = regulator_set_voltage(dev, config->init_uv, config->init_uv);
		if (ret < 0) {
+76 −4
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
 * Copyright (c) 2021 NXP
 * Copyright (c) 2022 Nordic Semiconductor ASA
 * Copyright (c) 2023 EPAM Systems
 * Copyright (c) 2023 Meta Platforms
 * SPDX-License-Identifier: Apache-2.0
 */

@@ -87,6 +88,10 @@ typedef int (*regulator_set_mode_t)(const struct device *dev,
				    regulator_mode_t mode);
typedef int (*regulator_get_mode_t)(const struct device *dev,
				    regulator_mode_t *mode);
typedef int (*regulator_set_active_discharge_t)(const struct device *dev,
				    bool active_discharge);
typedef int (*regulator_get_active_discharge_t)(const struct device *dev,
				    bool *active_discharge);
typedef int (*regulator_get_error_flags_t)(
	const struct device *dev, regulator_error_flags_t *flags);

@@ -104,6 +109,8 @@ __subsystem struct regulator_driver_api {
	regulator_get_current_limit_t get_current_limit;
	regulator_set_mode_t set_mode;
	regulator_get_mode_t get_mode;
	regulator_set_active_discharge_t set_active_discharge;
	regulator_get_active_discharge_t get_active_discharge;
	regulator_get_error_flags_t get_error_flags;
};

@@ -118,6 +125,22 @@ __subsystem struct regulator_driver_api {
#define REGULATOR_BOOT_ON	           BIT(1)
/** Indicates if regulator must be enabled when initialized */
#define REGULATOR_INIT_ENABLED             (REGULATOR_ALWAYS_ON | REGULATOR_BOOT_ON)
/** Regulator active discharge state mask */
#define REGULATOR_ACTIVE_DISCHARGE_MASK    GENMASK(3, 2)
/** Regulator active discharge state flag position*/
#define REGULATOR_ACTIVE_DISCHARGE_POS     2
/** Disable regulator active discharge */
#define REGULATOR_ACTIVE_DISCHARGE_DISABLE 0
/** Enable regulator active discharge */
#define REGULATOR_ACTIVE_DISCHARGE_ENABLE  1
/** Leave regulator active discharge state as default */
#define REGULATOR_ACTIVE_DISCHARGE_DEFAULT 2
/** Regulator active discharge set bits */
#define REGULATOR_ACTIVE_DISCHARGE_SET_BITS(x) \
	(((x) << REGULATOR_ACTIVE_DISCHARGE_POS) & REGULATOR_ACTIVE_DISCHARGE_MASK)
/** Regulator active discharge get bits */
#define REGULATOR_ACTIVE_DISCHARGE_GET_BITS(x) \
	(((x) & REGULATOR_ACTIVE_DISCHARGE_MASK) >> REGULATOR_ACTIVE_DISCHARGE_POS)

/** @} */

@@ -186,7 +209,10 @@ struct regulator_common_config {
		.flags = ((DT_PROP_OR(node_id, regulator_always_on, 0U) *      \
			   REGULATOR_ALWAYS_ON) |                              \
			  (DT_PROP_OR(node_id, regulator_boot_on, 0U) *        \
			   REGULATOR_BOOT_ON)),                                \
			   REGULATOR_BOOT_ON) |                                \
			  (REGULATOR_ACTIVE_DISCHARGE_SET_BITS(                \
			   DT_PROP_OR(node_id, regulator_active_discharge,     \
			   REGULATOR_ACTIVE_DISCHARGE_DEFAULT)))),             \
	}

/**
@@ -635,6 +661,52 @@ static inline int regulator_get_mode(const struct device *dev,
	return api->get_mode(dev, mode);
}

/**
 * @brief Set active discharge setting.
 *
 * @param dev Regulator device instance.
 * @param active_discharge Active discharge enable or disable.
 *
 * @retval 0 If successful.
 * @retval -ENOSYS If function is not implemented.
 * @retval -errno In case of any other error.
 */
static inline int regulator_set_active_discharge(const struct device *dev,
				     bool active_discharge)
{
	const struct regulator_driver_api *api =
		(const struct regulator_driver_api *)dev->api;

	if (api->set_active_discharge == NULL) {
		return -ENOSYS;
	}

	return api->set_active_discharge(dev, active_discharge);
}

/**
 * @brief Get active discharge setting.
 *
 * @param dev Regulator device instance.
 * @param[out] active_discharge Where active discharge will be stored.
 *
 * @retval 0 If successful.
 * @retval -ENOSYS If function is not implemented.
 * @retval -errno In case of any other error.
 */
static inline int regulator_get_active_discharge(const struct device *dev,
				     bool *active_discharge)
{
	const struct regulator_driver_api *api =
		(const struct regulator_driver_api *)dev->api;

	if (api->get_active_discharge == NULL) {
		return -ENOSYS;
	}

	return api->get_active_discharge(dev, active_discharge);
}

/**
 * @brief Get active error flags.
 *