[DM/REGULATOR] Add populate drivers

1. Fixed regulator.
2. GPIO regulator.

Signed-off-by: GuEe-GUI <2991707448@qq.com>
This commit is contained in:
GuEe-GUI 2024-11-26 13:36:08 +08:00 committed by Rbb666
parent 56614a4cb9
commit 49d18ec0a9
4 changed files with 499 additions and 0 deletions

View File

@ -5,6 +5,19 @@ menuconfig RT_USING_REGULATOR
depends on RT_USING_DM depends on RT_USING_DM
default n default n
config RT_REGULATOR_FIXED
bool "Fixed regulator support"
depends on RT_USING_REGULATOR
depends on RT_USING_PIN
depends on RT_USING_PINCTRL
default y
config RT_REGULATOR_GPIO
bool "GPIO regulator support"
depends on RT_USING_REGULATOR
depends on RT_USING_PIN
default y
if RT_USING_REGULATOR if RT_USING_REGULATOR
osource "$(SOC_DM_REGULATOR_DIR)/Kconfig" osource "$(SOC_DM_REGULATOR_DIR)/Kconfig"
endif endif

View File

@ -10,6 +10,12 @@ CPPPATH = [cwd + '/../include']
src = ['regulator.c', 'regulator_dm.c'] src = ['regulator.c', 'regulator_dm.c']
if GetDepend(['RT_REGULATOR_FIXED']):
src += ['regulator-fixed.c']
if GetDepend(['RT_REGULATOR_GPIO']):
src += ['regulator-gpio.c']
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group') Return('group')

View File

@ -0,0 +1,171 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-09-23 GuEe-GUI first version
*/
#include "regulator_dm.h"
struct regulator_fixed
{
struct rt_regulator_node parent;
struct rt_regulator_param param;
rt_base_t enable_pin;
const char *input_supply;
};
#define raw_to_regulator_fixed(raw) rt_container_of(raw, struct regulator_fixed, parent)
static rt_err_t regulator_fixed_enable(struct rt_regulator_node *reg_np)
{
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
struct rt_regulator_param *param = &rf->param;
if (rf->enable_pin < 0 || param->always_on)
{
return RT_EOK;
}
rt_pin_mode(rf->enable_pin, PIN_MODE_OUTPUT);
rt_pin_write(rf->enable_pin, param->enable_active_high ? PIN_HIGH : PIN_LOW);
return RT_EOK;
}
static rt_err_t regulator_fixed_disable(struct rt_regulator_node *reg_np)
{
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
struct rt_regulator_param *param = &rf->param;
if (rf->enable_pin < 0 || param->always_on)
{
return RT_EOK;
}
rt_pin_mode(rf->enable_pin, PIN_MODE_OUTPUT);
rt_pin_write(rf->enable_pin, param->enable_active_high ? PIN_LOW: PIN_HIGH);
return RT_EOK;
}
static rt_bool_t regulator_fixed_is_enabled(struct rt_regulator_node *reg_np)
{
rt_uint8_t active;
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
struct rt_regulator_param *param = &rf->param;
if (rf->enable_pin < 0 || param->always_on)
{
return RT_TRUE;
}
rt_pin_mode(rf->enable_pin, PIN_MODE_INPUT);
active = rt_pin_read(rf->enable_pin);
if (param->enable_active_high)
{
return active == PIN_HIGH;
}
return active == PIN_LOW;
}
static int regulator_fixed_get_voltage(struct rt_regulator_node *reg_np)
{
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
return rf->param.min_uvolt + (rf->param.max_uvolt - rf->param.min_uvolt) / 2;
}
static const struct rt_regulator_ops regulator_fixed_ops =
{
.enable = regulator_fixed_enable,
.disable = regulator_fixed_disable,
.is_enabled = regulator_fixed_is_enabled,
.get_voltage = regulator_fixed_get_voltage,
};
static rt_err_t regulator_fixed_probe(struct rt_platform_device *pdev)
{
rt_err_t err;
rt_uint32_t val;
struct rt_device *dev = &pdev->parent;
struct regulator_fixed *rf = rt_calloc(1, sizeof(*rf));
struct rt_regulator_node *rnp;
if (!rf)
{
return -RT_ENOMEM;
}
regulator_ofw_parse(dev->ofw_node, &rf->param);
rnp = &rf->parent;
rnp->supply_name = rf->param.name;
rnp->ops = &regulator_fixed_ops;
rnp->param = &rf->param;
rnp->dev = &pdev->parent;
rf->enable_pin = rt_pin_get_named_pin(dev, "enable", 0, RT_NULL, RT_NULL);
if (rf->enable_pin < 0)
{
rf->enable_pin = rt_pin_get_named_pin(dev, RT_NULL, 0, RT_NULL, RT_NULL);
}
if (rf->enable_pin < 0)
{
rf->enable_pin = -1;
}
rt_pin_ctrl_confs_apply(dev, 0);
if (!rt_dm_dev_prop_read_u32(dev, "startup-delay-us", &val))
{
rf->param.enable_delay = val;
}
if (!rt_dm_dev_prop_read_u32(dev, "off-on-delay-us", &val))
{
rf->param.off_on_delay = val;
}
if ((err = rt_regulator_register(rnp)))
{
goto _fail;
}
return RT_EOK;
_fail:
rt_free(rf);
return err;
}
static const struct rt_ofw_node_id regulator_fixed_ofw_ids[] =
{
{ .compatible = "regulator-fixed" },
{ /* sentinel */ }
};
static struct rt_platform_driver regulator_fixed_driver =
{
.name = "reg-fixed-voltage",
.ids = regulator_fixed_ofw_ids,
.probe = regulator_fixed_probe,
};
static int regulator_fixed_register(void)
{
rt_platform_driver_register(&regulator_fixed_driver);
return 0;
}
INIT_SUBSYS_EXPORT(regulator_fixed_register);

View File

@ -0,0 +1,309 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-09-23 GuEe-GUI first version
*/
#include <dt-bindings/pin/state.h>
#include "regulator_dm.h"
struct regulator_gpio_state
{
rt_uint32_t value;
rt_uint32_t gpios;
};
struct regulator_gpio_desc
{
rt_base_t pin;
rt_uint32_t flags;
};
struct regulator_gpio
{
struct rt_regulator_node parent;
rt_base_t enable_pin;
rt_size_t pins_nr;
struct regulator_gpio_desc *pins_desc;
int state;
rt_size_t states_nr;
struct regulator_gpio_state *states;
const char *input_supply;
rt_uint32_t startup_delay;
rt_uint32_t off_on_delay;
rt_bool_t enabled_at_boot;
struct rt_regulator_param param;
};
#define raw_to_regulator_gpio(raw) rt_container_of(raw, struct regulator_gpio, parent)
static rt_err_t regulator_gpio_enable(struct rt_regulator_node *reg_np)
{
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
struct rt_regulator_param *param = &rg->param;
if (param->always_on)
{
return RT_EOK;
}
if (rg->enable_pin >= 0)
{
rt_pin_mode(rg->enable_pin, PIN_MODE_OUTPUT);
rt_pin_write(rg->enable_pin, param->enable_active_high ? PIN_HIGH : PIN_LOW);
}
return RT_EOK;
}
static rt_err_t regulator_gpio_disable(struct rt_regulator_node *reg_np)
{
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
struct rt_regulator_param *param = &rg->param;
if (param->always_on)
{
return RT_EOK;
}
if (rg->enable_pin >= 0)
{
rt_pin_mode(rg->enable_pin, PIN_MODE_OUTPUT);
rt_pin_write(rg->enable_pin, param->enable_active_high ? PIN_LOW : PIN_HIGH);
}
return RT_EOK;
}
static rt_bool_t regulator_gpio_is_enabled(struct rt_regulator_node *reg_np)
{
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
struct rt_regulator_param *param = &rg->param;
if (param->always_on)
{
return RT_TRUE;
}
if (rg->enable_pin >= 0)
{
rt_uint8_t active_val = param->enable_active_high ? PIN_LOW : PIN_HIGH;
rt_pin_mode(rg->enable_pin, PIN_MODE_INPUT);
return rt_pin_read(rg->enable_pin) == active_val;
}
return RT_TRUE;
}
static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,
int min_uvolt, int max_uvolt)
{
int target = 0, best_val = RT_REGULATOR_UVOLT_INVALID;
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
for (int i = 0; i < rg->states_nr; ++i)
{
struct regulator_gpio_state *state = &rg->states[i];
if (state->value < best_val &&
state->value >= min_uvolt &&
state->value <= max_uvolt)
{
target = state->gpios;
best_val = state->value;
}
}
if (best_val == RT_REGULATOR_UVOLT_INVALID)
{
return -RT_EINVAL;
}
for (int i = 0; i < rg->pins_nr; ++i)
{
int state = (target >> i) & 1;
struct regulator_gpio_desc *gpiod = &rg->pins_desc[i];
rt_pin_mode(gpiod->pin, PIN_MODE_OUTPUT);
rt_pin_write(gpiod->pin, gpiod->flags == PIND_OUT_HIGH ? state : !state);
}
rg->state = target;
return RT_EOK;
}
static int regulator_gpio_get_voltage(struct rt_regulator_node *reg_np)
{
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
for (int i = 0; i < rg->states_nr; ++i)
{
if (rg->states[i].gpios == rg->state)
{
return rg->states[i].value;
}
}
return -RT_EINVAL;
}
static const struct rt_regulator_ops regulator_gpio_ops =
{
.enable = regulator_gpio_enable,
.disable = regulator_gpio_disable,
.is_enabled = regulator_gpio_is_enabled,
.set_voltage = regulator_gpio_set_voltage,
.get_voltage = regulator_gpio_get_voltage,
};
static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)
{
rt_err_t err;
struct rt_device *dev = &pdev->parent;
struct regulator_gpio *rg = rt_calloc(1, sizeof(*rg));
struct rt_regulator_node *rgp;
if (!rg)
{
return -RT_ENOMEM;
}
regulator_ofw_parse(dev->ofw_node, &rg->param);
rgp = &rg->parent;
rgp->supply_name = rg->param.name;
rgp->ops = &regulator_gpio_ops;
rgp->param = &rg->param;
rgp->dev = &pdev->parent;
rt_dm_dev_prop_read_u32(dev, "startup-delay-us", &rg->startup_delay);
rt_dm_dev_prop_read_u32(dev, "off-on-delay-us", &rg->off_on_delay);
/* GPIO flags are ignored, we check by enable-active-high */
rg->enable_pin = rt_pin_get_named_pin(dev, "enable", 0, RT_NULL, RT_NULL);
if (rg->enable_pin < 0 && rg->enable_pin != -RT_EEMPTY)
{
err = rg->enable_pin;
goto _fail;
}
rg->pins_nr = rt_pin_get_named_pin_count(dev, "gpios");
if (rg->pins_nr > 0)
{
rg->pins_desc = rt_malloc(sizeof(*rg->pins_desc) * rg->pins_nr);
if (!rg->pins_desc)
{
err = -RT_ENOMEM;
goto _fail;
}
for (int i = 0; i < rg->pins_nr; ++i)
{
rt_uint32_t val;
struct regulator_gpio_desc *gpiod = &rg->pins_desc[i];
gpiod->pin = rt_pin_get_named_pin(dev, RT_NULL, i, RT_NULL, RT_NULL);
if (gpiod->pin < 0)
{
err = gpiod->pin;
goto _fail;
}
if (rt_dm_dev_prop_read_u32_index(dev, "gpios-states", i, &val) < 0)
{
gpiod->flags = PIND_OUT_HIGH;
}
else
{
gpiod->flags = val ? PIND_OUT_HIGH : PIND_OUT_LOW;
}
if (gpiod->flags == PIND_OUT_HIGH)
{
rg->state |= 1 << i;
}
}
}
rg->states_nr = rt_dm_dev_prop_count_of_u32(dev, "states") / 2;
if (rg->states_nr < 0)
{
err = -RT_EIO;
goto _fail;
}
rg->states = rt_malloc(sizeof(*rg->states) * rg->states_nr);
if (!rg->states)
{
err = -RT_ENOMEM;
goto _fail;
}
for (int i = 0; i < rg->states_nr; ++i)
{
rt_dm_dev_prop_read_u32_index(dev, "states", i * 2, &rg->states[i].value);
rt_dm_dev_prop_read_u32_index(dev, "states", i * 2 + 1, &rg->states[i].gpios);
}
if ((err = rt_regulator_register(rgp)))
{
goto _fail;
}
return RT_EOK;
_fail:
if (rg->pins_desc)
{
rt_free(rg->pins_desc);
}
if (rg->states)
{
rt_free(rg->states);
}
rt_free(rg);
return err;
}
static const struct rt_ofw_node_id regulator_gpio_ofw_ids[] =
{
{ .compatible = "regulator-gpio" },
{ /* sentinel */ }
};
static struct rt_platform_driver regulator_gpio_driver =
{
.name = "regulator-gpio",
.ids = regulator_gpio_ofw_ids,
.probe = regulator_gpio_probe,
};
static int regulator_gpio_register(void)
{
rt_platform_driver_register(&regulator_gpio_driver);
return 0;
}
INIT_SUBSYS_EXPORT(regulator_gpio_register);