From d9fe261f8eae2ca8b999a35852024ecf2ec3dc2a Mon Sep 17 00:00:00 2001 From: Mykola Kvach Date: Thu, 10 Aug 2023 11:56:21 +0300 Subject: [PATCH] drivers: regulator-fixed: extend api of driver (list/count voltages) Allow properties 'regulator-min-microvolt' and 'regulator-max-microvolt' for fixed regulators: Note: they should be equal. Add simple functions for getting list of allowed and count of voltages. Signed-off-by: Mykola Kvach --- drivers/regulator/regulator_fixed.c | 53 ++++++++++++++++----- dts/bindings/regulator/regulator-fixed.yaml | 2 + include/zephyr/drivers/regulator.h | 23 +++++++++ 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/drivers/regulator/regulator_fixed.c b/drivers/regulator/regulator_fixed.c index 8c9b1f372f6..641198bfd8c 100644 --- a/drivers/regulator/regulator_fixed.c +++ b/drivers/regulator/regulator_fixed.c @@ -59,9 +59,33 @@ static int regulator_fixed_disable(const struct device *dev) return gpio_pin_set_dt(&cfg->enable, 0); } +static unsigned int regulator_fixed_count_voltages(const struct device *dev) +{ + int32_t min_uv; + + return (regulator_common_get_min_voltage(dev, &min_uv) < 0) ? 0U : 1U; +} + +static int regulator_fixed_list_voltage(const struct device *dev, + unsigned int idx, + int32_t *volt_uv) +{ + if (idx != 0) { + return -EINVAL; + } + + if (regulator_common_get_min_voltage(dev, volt_uv) < 0) { + return -EINVAL; + } + + return 0; +} + static const struct regulator_driver_api regulator_fixed_api = { .enable = regulator_fixed_enable, .disable = regulator_fixed_disable, + .count_voltages = regulator_fixed_count_voltages, + .list_voltage = regulator_fixed_list_voltage, }; static int regulator_fixed_init(const struct device *dev) @@ -98,19 +122,22 @@ static int regulator_fixed_init(const struct device *dev) return regulator_common_init(dev, init_enabled); } -#define REGULATOR_FIXED_DEFINE(inst) \ - static struct regulator_fixed_data data##inst; \ - \ - static const struct regulator_fixed_config config##inst = { \ - .common = REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst), \ - .startup_delay_us = DT_INST_PROP(inst, startup_delay_us), \ - .off_on_delay_us = DT_INST_PROP(inst, off_on_delay_us), \ - .enable = GPIO_DT_SPEC_INST_GET_OR(inst, enable_gpios, {0}), \ - }; \ - \ - DEVICE_DT_INST_DEFINE(inst, regulator_fixed_init, NULL, &data##inst, \ - &config##inst, POST_KERNEL, \ - CONFIG_REGULATOR_FIXED_INIT_PRIORITY, \ +#define REGULATOR_FIXED_DEFINE(inst) \ + BUILD_ASSERT(DT_INST_PROP_OR(inst, regulator_min_microvolt, 0) == \ + DT_INST_PROP_OR(inst, regulator_max_microvolt, 0), \ + "Regulator requires fixed voltages"); \ + static struct regulator_fixed_data data##inst; \ + \ + static const struct regulator_fixed_config config##inst = { \ + .common = REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst), \ + .startup_delay_us = DT_INST_PROP(inst, startup_delay_us), \ + .off_on_delay_us = DT_INST_PROP(inst, off_on_delay_us), \ + .enable = GPIO_DT_SPEC_INST_GET_OR(inst, enable_gpios, {0}), \ + }; \ + \ + DEVICE_DT_INST_DEFINE(inst, regulator_fixed_init, NULL, &data##inst, \ + &config##inst, POST_KERNEL, \ + CONFIG_REGULATOR_FIXED_INIT_PRIORITY, \ ®ulator_fixed_api); DT_INST_FOREACH_STATUS_OKAY(REGULATOR_FIXED_DEFINE) diff --git a/dts/bindings/regulator/regulator-fixed.yaml b/dts/bindings/regulator/regulator-fixed.yaml index 0e1d222972d..1033d333f08 100644 --- a/dts/bindings/regulator/regulator-fixed.yaml +++ b/dts/bindings/regulator/regulator-fixed.yaml @@ -11,6 +11,8 @@ include: - regulator-name - regulator-boot-on - regulator-always-on + - regulator-min-microvolt + - regulator-max-microvolt compatible: "regulator-fixed" diff --git a/include/zephyr/drivers/regulator.h b/include/zephyr/drivers/regulator.h index 8669180e65d..a2e28440591 100644 --- a/include/zephyr/drivers/regulator.h +++ b/include/zephyr/drivers/regulator.h @@ -2,6 +2,7 @@ * Copyright (c) 2019-2020 Peter Bigot Consulting, LLC * Copyright (c) 2021 NXP * Copyright (c) 2022 Nordic Semiconductor ASA + * Copyright (c) 2023 EPAM Systems * SPDX-License-Identifier: Apache-2.0 */ @@ -242,6 +243,28 @@ static inline bool regulator_common_is_init_enabled(const struct device *dev) return (config->flags & REGULATOR_INIT_ENABLED) != 0U; } +/** + * @brief Get minimum supported voltage. + * + * @param dev Regulator device instance. + * @param min_uv Where minimum voltage will be stored, in microvolts. + * + * @retval 0 If successful + * @retval -ENOENT If minimum voltage is not specified. + */ +static inline int regulator_common_get_min_voltage(const struct device *dev, int32_t *min_uv) +{ + const struct regulator_common_config *config = + (const struct regulator_common_config *)dev->config; + + if (config->min_uv == INT32_MIN) { + return -ENOENT; + } + + *min_uv = config->min_uv; + return 0; +} + /** @endcond */ /**