Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion components/drivers/regulator/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ menuconfig RT_USING_REGULATOR
bool "Using Voltage and Current Regulator"
select RT_USING_ADT
select RT_USING_ADT_REF
default n

config RT_REGULATOR_FAN53555
bool "Fairchild FAN53555 / TCS4525 Regulator"
depends on RT_USING_REGULATOR
depends on RT_USING_I2C
depends on RT_USING_DM
default n

config RT_REGULATOR_FIXED
Expand All @@ -27,6 +27,15 @@ config RT_REGULATOR_GPIO
depends on RT_USING_PIN
default y

config RT_REGULATOR_PWM
bool "PWM regulator support"
depends on RT_USING_REGULATOR
depends on RT_USING_DM
depends on RT_USING_PWM
depends on RT_USING_PIN
depends on RT_USING_OFW
default n

config RT_REGULATOR_SCMI
bool "SCMI regulator support"
depends on RT_USING_REGULATOR
Expand Down
3 changes: 3 additions & 0 deletions components/drivers/regulator/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if GetDepend(['RT_REGULATOR_FIXED']):
if GetDepend(['RT_REGULATOR_GPIO']):
src += ['regulator-gpio.c']

if GetDepend(['RT_REGULATOR_PWM']):
src += ['regulator-pwm.c']

if GetDepend(['RT_REGULATOR_SCMI']):
src += ['regulator-scmi.c']

Expand Down
54 changes: 25 additions & 29 deletions components/drivers/regulator/regulator-fixed.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@

struct regulator_fixed
{
struct rt_regulator_node parent;
struct rt_regulator_node parent;
struct rt_regulator_param param;

rt_base_t enable_pin;
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 regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
struct rt_regulator_param *param = &rf->param;

if (rf->enable_pin < 0 || param->always_on)
if (rf->enable_pin < 0)
{
return RT_EOK;
}
Expand All @@ -39,32 +39,31 @@ static rt_err_t regulator_fixed_enable(struct rt_regulator_node *reg_np)

static rt_err_t regulator_fixed_disable(struct rt_regulator_node *reg_np)
{
struct regulator_fixed *rf = raw_to_regulator_fixed(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)
if (rf->enable_pin < 0)
{
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);
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);
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)
if (rf->enable_pin < 0)
{
return RT_TRUE;
}

rt_pin_mode(rf->enable_pin, PIN_MODE_INPUT);
active = rt_pin_read(rf->enable_pin);

if (param->enable_active_high)
Expand All @@ -82,20 +81,19 @@ static int regulator_fixed_get_voltage(struct rt_regulator_node *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,
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));
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)
Expand All @@ -105,11 +103,11 @@ static rt_err_t regulator_fixed_probe(struct rt_platform_device *pdev)

regulator_ofw_parse(dev->ofw_node, &rf->param);

rnp = &rf->parent;
rnp = &rf->parent;
rnp->supply_name = rf->param.name;
rnp->ops = &regulator_fixed_ops;
rnp->param = &rf->param;
rnp->dev = &pdev->parent;
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);

Expand Down Expand Up @@ -148,16 +146,14 @@ static rt_err_t regulator_fixed_probe(struct rt_platform_device *pdev)
return err;
}

static const struct rt_ofw_node_id regulator_fixed_ofw_ids[] =
{
static const struct rt_ofw_node_id regulator_fixed_ofw_ids[] = {
{ .compatible = "regulator-fixed" },
{ /* sentinel */ }
};

static struct rt_platform_driver regulator_fixed_driver =
{
static struct rt_platform_driver regulator_fixed_driver = {
.name = "reg-fixed-voltage",
.ids = regulator_fixed_ofw_ids,
.ids = regulator_fixed_ofw_ids,

.probe = regulator_fixed_probe,
};
Expand Down
78 changes: 30 additions & 48 deletions components/drivers/regulator/regulator-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct regulator_gpio_state

struct regulator_gpio_desc
{
rt_base_t pin;
rt_base_t pin;
rt_uint32_t flags;
};

Expand All @@ -30,32 +30,27 @@ struct regulator_gpio

rt_base_t enable_pin;

rt_size_t pins_nr;
rt_size_t pins_nr;
struct regulator_gpio_desc *pins_desc;

int state;
rt_size_t states_nr;
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;
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 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);
Expand All @@ -67,14 +62,9 @@ static rt_err_t regulator_gpio_enable(struct rt_regulator_node *reg_np)

static rt_err_t regulator_gpio_disable(struct rt_regulator_node *reg_np)
{
struct regulator_gpio *rg = raw_to_regulator_gpio(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);
Expand All @@ -86,14 +76,9 @@ static rt_err_t regulator_gpio_disable(struct rt_regulator_node *reg_np)

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 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;
Expand All @@ -106,9 +91,9 @@ static rt_bool_t regulator_gpio_is_enabled(struct rt_regulator_node *reg_np)
}

static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,
int min_uvolt, int max_uvolt)
int min_uvolt, int max_uvolt)
{
int target = 0, best_val = RT_REGULATOR_UVOLT_INVALID;
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)
Expand All @@ -119,7 +104,7 @@ static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,
state->value >= min_uvolt &&
state->value <= max_uvolt)
{
target = state->gpios;
target = state->gpios;
best_val = state->value;
}
}
Expand All @@ -131,7 +116,7 @@ static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,

for (int i = 0; i < rg->pins_nr; ++i)
{
int state = (target >> i) & 1;
int state = (target >> i) & 1;
struct regulator_gpio_desc *gpiod = &rg->pins_desc[i];

rt_pin_mode(gpiod->pin, PIN_MODE_OUTPUT);
Expand All @@ -158,20 +143,19 @@ static int regulator_gpio_get_voltage(struct rt_regulator_node *reg_np)
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,
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));
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)
Expand All @@ -181,11 +165,11 @@ static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)

regulator_ofw_parse(dev->ofw_node, &rg->param);

rgp = &rg->parent;
rgp = &rg->parent;
rgp->supply_name = rg->param.name;
rgp->ops = &regulator_gpio_ops;
rgp->param = &rg->param;
rgp->dev = &pdev->parent;
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);
Expand Down Expand Up @@ -214,7 +198,7 @@ static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)

for (int i = 0; i < rg->pins_nr; ++i)
{
rt_uint32_t val;
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);
Expand Down Expand Up @@ -286,16 +270,14 @@ static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)
return err;
}

static const struct rt_ofw_node_id regulator_gpio_ofw_ids[] =
{
static const struct rt_ofw_node_id regulator_gpio_ofw_ids[] = {
{ .compatible = "regulator-gpio" },
{ /* sentinel */ }
};

static struct rt_platform_driver regulator_gpio_driver =
{
static struct rt_platform_driver regulator_gpio_driver = {
.name = "regulator-gpio",
.ids = regulator_gpio_ofw_ids,
.ids = regulator_gpio_ofw_ids,

.probe = regulator_gpio_probe,
};
Expand Down
Loading
Loading