GK SDK 源码库: XMIPCLinuxV100R005C00SPC030 (kernel/tools/open_source excluded)

This commit is contained in:
lai
2026-09-06 03:52:57 +08:00
commit b1928b41c0
21813 changed files with 4413081 additions and 0 deletions
@@ -0,0 +1,526 @@
/*
* Copyright (c) 2017-2022, STMicroelectronics - All Rights Reserved
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <assert.h>
#include <errno.h>
#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <drivers/st/regulator.h>
#include <drivers/st/stm32_i2c.h>
#include <drivers/st/stm32mp_pmic.h>
#include <drivers/st/stpmic1.h>
#include <lib/mmio.h>
#include <lib/utils_def.h>
#include <libfdt.h>
#include <platform_def.h>
#define PMIC_NODE_NOT_FOUND 1
static struct i2c_handle_s i2c_handle;
static uint32_t pmic_i2c_addr;
static int register_pmic(void);
static int dt_get_pmic_node(void *fdt)
{
static int node = -FDT_ERR_BADOFFSET;
if (node == -FDT_ERR_BADOFFSET) {
node = fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
}
return node;
}
int dt_pmic_status(void)
{
static int status = -FDT_ERR_BADVALUE;
int node;
void *fdt;
if (status != -FDT_ERR_BADVALUE) {
return status;
}
if (fdt_get_address(&fdt) == 0) {
return -ENOENT;
}
node = dt_get_pmic_node(fdt);
if (node <= 0) {
status = -FDT_ERR_NOTFOUND;
return status;
}
status = (int)fdt_get_status(node);
return status;
}
static bool dt_pmic_is_secure(void)
{
int status = dt_pmic_status();
return (status >= 0) &&
(status == DT_SECURE) &&
(i2c_handle.dt_status == DT_SECURE);
}
/*
* Get PMIC and its I2C bus configuration from the device tree.
* Return 0 on success, negative on error, 1 if no PMIC node is defined.
*/
static int dt_pmic_i2c_config(struct dt_node_info *i2c_info,
struct stm32_i2c_init_s *init)
{
static int i2c_node = -FDT_ERR_NOTFOUND;
void *fdt;
if (fdt_get_address(&fdt) == 0) {
return -FDT_ERR_NOTFOUND;
}
if (i2c_node == -FDT_ERR_NOTFOUND) {
int pmic_node;
const fdt32_t *cuint;
pmic_node = dt_get_pmic_node(fdt);
if (pmic_node < 0) {
return PMIC_NODE_NOT_FOUND;
}
cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
if (cuint == NULL) {
return -FDT_ERR_NOTFOUND;
}
pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1;
if (pmic_i2c_addr > UINT16_MAX) {
return -FDT_ERR_BADVALUE;
}
i2c_node = fdt_parent_offset(fdt, pmic_node);
if (i2c_node < 0) {
return -FDT_ERR_NOTFOUND;
}
}
dt_fill_device_info(i2c_info, i2c_node);
if (i2c_info->base == 0U) {
return -FDT_ERR_NOTFOUND;
}
return stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init);
}
bool initialize_pmic_i2c(void)
{
int ret;
struct dt_node_info i2c_info;
struct i2c_handle_s *i2c = &i2c_handle;
struct stm32_i2c_init_s i2c_init;
ret = dt_pmic_i2c_config(&i2c_info, &i2c_init);
if (ret < 0) {
ERROR("I2C configuration failed %d\n", ret);
panic();
}
if (ret != 0) {
return false;
}
/* Initialize PMIC I2C */
i2c->i2c_base_addr = i2c_info.base;
i2c->dt_status = i2c_info.status;
i2c->clock = i2c_info.clock;
i2c->i2c_state = I2C_STATE_RESET;
i2c_init.own_address1 = pmic_i2c_addr;
i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT;
i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE;
i2c_init.own_address2 = 0;
i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK;
i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE;
i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE;
i2c_init.analog_filter = 1;
i2c_init.digital_filter_coef = 0;
ret = stm32_i2c_init(i2c, &i2c_init);
if (ret != 0) {
ERROR("Cannot initialize I2C %x (%d)\n",
i2c->i2c_base_addr, ret);
panic();
}
if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, 1,
I2C_TIMEOUT_BUSY_MS)) {
ERROR("I2C device not ready\n");
panic();
}
stpmic1_bind_i2c(i2c, (uint16_t)pmic_i2c_addr);
return true;
}
static void register_pmic_shared_peripherals(void)
{
uintptr_t i2c_base = i2c_handle.i2c_base_addr;
if (dt_pmic_is_secure()) {
stm32mp_register_secure_periph_iomem(i2c_base);
} else {
if (i2c_base != 0U) {
stm32mp_register_non_secure_periph_iomem(i2c_base);
}
}
}
void initialize_pmic(void)
{
if (!initialize_pmic_i2c()) {
VERBOSE("No PMIC\n");
return;
}
register_pmic_shared_peripherals();
if (register_pmic() < 0) {
panic();
}
if (stpmic1_powerctrl_on() < 0) {
panic();
}
}
#if DEBUG
void print_pmic_info_and_debug(void)
{
unsigned long pmic_version;
if (stpmic1_get_version(&pmic_version) != 0) {
ERROR("Failed to access PMIC\n");
panic();
}
INFO("PMIC version = 0x%02lx\n", pmic_version);
}
#endif
int pmic_ddr_power_init(enum ddr_type ddr_type)
{
int status;
uint16_t buck3_min_mv;
struct rdev *buck2, *buck3, *vref;
struct rdev *ldo3 __unused;
buck2 = regulator_get_by_name("buck2");
if (buck2 == NULL) {
return -ENOENT;
}
#if STM32MP15
ldo3 = regulator_get_by_name("ldo3");
if (ldo3 == NULL) {
return -ENOENT;
}
#endif
vref = regulator_get_by_name("vref_ddr");
if (vref == NULL) {
return -ENOENT;
}
switch (ddr_type) {
case STM32MP_DDR3:
#if STM32MP15
status = regulator_set_flag(ldo3, REGUL_SINK_SOURCE);
if (status != 0) {
return status;
}
#endif
status = regulator_set_min_voltage(buck2);
if (status != 0) {
return status;
}
status = regulator_enable(buck2);
if (status != 0) {
return status;
}
status = regulator_enable(vref);
if (status != 0) {
return status;
}
#if STM32MP15
status = regulator_enable(ldo3);
if (status != 0) {
return status;
}
#endif
break;
case STM32MP_LPDDR2:
case STM32MP_LPDDR3:
/*
* Set LDO3 to 1.8V
* Set LDO3 to bypass mode if BUCK3 = 1.8V
* Set LDO3 to normal mode if BUCK3 != 1.8V
*/
buck3 = regulator_get_by_name("buck3");
if (buck3 == NULL) {
return -ENOENT;
}
regulator_get_range(buck3, &buck3_min_mv, NULL);
#if STM32MP15
if (buck3_min_mv != 1800) {
status = regulator_set_min_voltage(ldo3);
if (status != 0) {
return status;
}
} else {
status = regulator_set_flag(ldo3, REGUL_ENABLE_BYPASS);
if (status != 0) {
return status;
}
}
#endif
status = regulator_set_min_voltage(buck2);
if (status != 0) {
return status;
}
#if STM32MP15
status = regulator_enable(ldo3);
if (status != 0) {
return status;
}
#endif
status = regulator_enable(buck2);
if (status != 0) {
return status;
}
status = regulator_enable(vref);
if (status != 0) {
return status;
}
break;
default:
break;
};
return 0;
}
int pmic_voltages_init(void)
{
#if STM32MP13
struct rdev *buck1, *buck4;
int status;
buck1 = regulator_get_by_name("buck1");
if (buck1 == NULL) {
return -ENOENT;
}
buck4 = regulator_get_by_name("buck4");
if (buck4 == NULL) {
return -ENOENT;
}
status = regulator_set_min_voltage(buck1);
if (status != 0) {
return status;
}
status = regulator_set_min_voltage(buck4);
if (status != 0) {
return status;
}
#endif
return 0;
}
enum {
STPMIC1_BUCK1 = 0,
STPMIC1_BUCK2,
STPMIC1_BUCK3,
STPMIC1_BUCK4,
STPMIC1_LDO1,
STPMIC1_LDO2,
STPMIC1_LDO3,
STPMIC1_LDO4,
STPMIC1_LDO5,
STPMIC1_LDO6,
STPMIC1_VREF_DDR,
STPMIC1_BOOST,
STPMIC1_VBUS_OTG,
STPMIC1_SW_OUT,
};
static int pmic_set_state(const struct regul_description *desc, bool enable)
{
VERBOSE("%s: set state to %d\n", desc->node_name, enable);
if (enable == STATE_ENABLE) {
return stpmic1_regulator_enable(desc->node_name);
} else {
return stpmic1_regulator_disable(desc->node_name);
}
}
static int pmic_get_state(const struct regul_description *desc)
{
VERBOSE("%s: get state\n", desc->node_name);
return stpmic1_is_regulator_enabled(desc->node_name);
}
static int pmic_get_voltage(const struct regul_description *desc)
{
VERBOSE("%s: get volt\n", desc->node_name);
return stpmic1_regulator_voltage_get(desc->node_name);
}
static int pmic_set_voltage(const struct regul_description *desc, uint16_t mv)
{
VERBOSE("%s: get volt\n", desc->node_name);
return stpmic1_regulator_voltage_set(desc->node_name, mv);
}
static int pmic_list_voltages(const struct regul_description *desc,
const uint16_t **levels, size_t *count)
{
VERBOSE("%s: list volt\n", desc->node_name);
return stpmic1_regulator_levels_mv(desc->node_name, levels, count);
}
static int pmic_set_flag(const struct regul_description *desc, uint16_t flag)
{
VERBOSE("%s: set_flag 0x%x\n", desc->node_name, flag);
switch (flag) {
case REGUL_OCP:
return stpmic1_regulator_icc_set(desc->node_name);
case REGUL_ACTIVE_DISCHARGE:
return stpmic1_active_discharge_mode_set(desc->node_name);
case REGUL_PULL_DOWN:
return stpmic1_regulator_pull_down_set(desc->node_name);
case REGUL_MASK_RESET:
return stpmic1_regulator_mask_reset_set(desc->node_name);
case REGUL_SINK_SOURCE:
return stpmic1_regulator_sink_mode_set(desc->node_name);
case REGUL_ENABLE_BYPASS:
return stpmic1_regulator_bypass_mode_set(desc->node_name);
default:
return -EINVAL;
}
}
static const struct regul_ops pmic_ops = {
.set_state = pmic_set_state,
.get_state = pmic_get_state,
.set_voltage = pmic_set_voltage,
.get_voltage = pmic_get_voltage,
.list_voltages = pmic_list_voltages,
.set_flag = pmic_set_flag,
};
#define DEFINE_REGU(name) { \
.node_name = name, \
.ops = &pmic_ops, \
.driver_data = NULL, \
.enable_ramp_delay = 1000, \
}
static const struct regul_description pmic_regs[] = {
[STPMIC1_BUCK1] = DEFINE_REGU("buck1"),
[STPMIC1_BUCK2] = DEFINE_REGU("buck2"),
[STPMIC1_BUCK3] = DEFINE_REGU("buck3"),
[STPMIC1_BUCK4] = DEFINE_REGU("buck4"),
[STPMIC1_LDO1] = DEFINE_REGU("ldo1"),
[STPMIC1_LDO2] = DEFINE_REGU("ldo2"),
[STPMIC1_LDO3] = DEFINE_REGU("ldo3"),
[STPMIC1_LDO4] = DEFINE_REGU("ldo4"),
[STPMIC1_LDO5] = DEFINE_REGU("ldo5"),
[STPMIC1_LDO6] = DEFINE_REGU("ldo6"),
[STPMIC1_VREF_DDR] = DEFINE_REGU("vref_ddr"),
[STPMIC1_BOOST] = DEFINE_REGU("boost"),
[STPMIC1_VBUS_OTG] = DEFINE_REGU("pwr_sw1"),
[STPMIC1_SW_OUT] = DEFINE_REGU("pwr_sw2"),
};
#define NB_REG ARRAY_SIZE(pmic_regs)
static int register_pmic(void)
{
void *fdt;
int pmic_node, regulators_node, subnode;
VERBOSE("Register pmic\n");
if (fdt_get_address(&fdt) == 0) {
return -FDT_ERR_NOTFOUND;
}
pmic_node = dt_get_pmic_node(fdt);
if (pmic_node < 0) {
return pmic_node;
}
regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");
if (regulators_node < 0) {
return -ENOENT;
}
fdt_for_each_subnode(subnode, fdt, regulators_node) {
const char *reg_name = fdt_get_name(fdt, subnode, NULL);
const struct regul_description *desc;
unsigned int i;
int ret;
for (i = 0; i < NB_REG; i++) {
desc = &pmic_regs[i];
if (strcmp(desc->node_name, reg_name) == 0) {
break;
}
}
assert(i < NB_REG);
ret = regulator_register(desc, subnode);
if (ret != 0) {
WARN("%s:%d failed to register %s\n", __func__,
__LINE__, reg_name);
return ret;
}
}
return 0;
}
@@ -0,0 +1,937 @@
/*
* Copyright (c) 2016-2021, STMicroelectronics - All Rights Reserved
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <errno.h>
#include <string.h>
#include <common/debug.h>
#include <drivers/st/stpmic1.h>
#define I2C_TIMEOUT_MS 25
struct regul_struct {
const char *dt_node_name;
const uint16_t *voltage_table;
uint8_t voltage_table_size;
uint8_t control_reg;
uint8_t enable_mask;
uint8_t low_power_reg;
uint8_t pull_down_reg;
uint8_t pull_down;
uint8_t mask_reset_reg;
uint8_t mask_reset;
uint8_t icc_reg;
uint8_t icc_mask;
};
static struct i2c_handle_s *pmic_i2c_handle;
static uint16_t pmic_i2c_addr;
/*
* Special mode corresponds to LDO3 in sink source mode or in bypass mode.
* LDO3 doesn't switch back from special to normal mode.
*/
static bool ldo3_special_mode;
/* Voltage tables in mV */
static const uint16_t buck1_voltage_table[] = {
725,
725,
725,
725,
725,
725,
750,
775,
800,
825,
850,
875,
900,
925,
950,
975,
1000,
1025,
1050,
1075,
1100,
1125,
1150,
1175,
1200,
1225,
1250,
1275,
1300,
1325,
1350,
1375,
1400,
1425,
1450,
1475,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
1500,
};
static const uint16_t buck2_voltage_table[] = {
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1050,
1050,
1100,
1100,
1150,
1150,
1200,
1200,
1250,
1250,
1300,
1300,
1350,
1350,
1400,
1400,
1450,
1450,
1500,
};
static const uint16_t buck3_voltage_table[] = {
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1100,
1100,
1100,
1100,
1200,
1200,
1200,
1200,
1300,
1300,
1300,
1300,
1400,
1400,
1400,
1400,
1500,
1600,
1700,
1800,
1900,
2000,
2100,
2200,
2300,
2400,
2500,
2600,
2700,
2800,
2900,
3000,
3100,
3200,
3300,
3400,
};
static const uint16_t buck4_voltage_table[] = {
600,
625,
650,
675,
700,
725,
750,
775,
800,
825,
850,
875,
900,
925,
950,
975,
1000,
1025,
1050,
1075,
1100,
1125,
1150,
1175,
1200,
1225,
1250,
1275,
1300,
1300,
1350,
1350,
1400,
1400,
1450,
1450,
1500,
1600,
1700,
1800,
1900,
2000,
2100,
2200,
2300,
2400,
2500,
2600,
2700,
2800,
2900,
3000,
3100,
3200,
3300,
3400,
3500,
3600,
3700,
3800,
3900,
};
static const uint16_t ldo1_voltage_table[] = {
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1800,
1900,
2000,
2100,
2200,
2300,
2400,
2500,
2600,
2700,
2800,
2900,
3000,
3100,
3200,
3300,
};
static const uint16_t ldo2_voltage_table[] = {
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1800,
1900,
2000,
2100,
2200,
2300,
2400,
2500,
2600,
2700,
2800,
2900,
3000,
3100,
3200,
3300,
};
static const uint16_t ldo3_voltage_table[] = {
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1800,
1900,
2000,
2100,
2200,
2300,
2400,
2500,
2600,
2700,
2800,
2900,
3000,
3100,
3200,
3300,
3300,
3300,
3300,
3300,
3300,
3300,
};
/* Special mode table is used for sink source OR bypass mode */
static const uint16_t ldo3_special_mode_table[] = {
0,
};
static const uint16_t ldo5_voltage_table[] = {
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1700,
1800,
1900,
2000,
2100,
2200,
2300,
2400,
2500,
2600,
2700,
2800,
2900,
3000,
3100,
3200,
3300,
3400,
3500,
3600,
3700,
3800,
3900,
};
static const uint16_t ldo6_voltage_table[] = {
900,
1000,
1100,
1200,
1300,
1400,
1500,
1600,
1700,
1800,
1900,
2000,
2100,
2200,
2300,
2400,
2500,
2600,
2700,
2800,
2900,
3000,
3100,
3200,
3300,
};
static const uint16_t ldo4_voltage_table[] = {
3300,
};
static const uint16_t vref_ddr_voltage_table[] = {
3300,
};
static const uint16_t fixed_5v_voltage_table[] = {
5000,
};
/* Table of Regulators in PMIC SoC */
static const struct regul_struct regulators_table[] = {
{
.dt_node_name = "buck1",
.voltage_table = buck1_voltage_table,
.voltage_table_size = ARRAY_SIZE(buck1_voltage_table),
.control_reg = BUCK1_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = BUCK1_PWRCTRL_REG,
.pull_down_reg = BUCK_PULL_DOWN_REG,
.pull_down = BUCK1_PULL_DOWN_SHIFT,
.mask_reset_reg = MASK_RESET_BUCK_REG,
.mask_reset = BUCK1_MASK_RESET,
.icc_reg = BUCK_ICC_TURNOFF_REG,
.icc_mask = BUCK1_ICC_SHIFT,
},
{
.dt_node_name = "buck2",
.voltage_table = buck2_voltage_table,
.voltage_table_size = ARRAY_SIZE(buck2_voltage_table),
.control_reg = BUCK2_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = BUCK2_PWRCTRL_REG,
.pull_down_reg = BUCK_PULL_DOWN_REG,
.pull_down = BUCK2_PULL_DOWN_SHIFT,
.mask_reset_reg = MASK_RESET_BUCK_REG,
.mask_reset = BUCK2_MASK_RESET,
.icc_reg = BUCK_ICC_TURNOFF_REG,
.icc_mask = BUCK2_ICC_SHIFT,
},
{
.dt_node_name = "buck3",
.voltage_table = buck3_voltage_table,
.voltage_table_size = ARRAY_SIZE(buck3_voltage_table),
.control_reg = BUCK3_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = BUCK3_PWRCTRL_REG,
.pull_down_reg = BUCK_PULL_DOWN_REG,
.pull_down = BUCK3_PULL_DOWN_SHIFT,
.mask_reset_reg = MASK_RESET_BUCK_REG,
.mask_reset = BUCK3_MASK_RESET,
.icc_reg = BUCK_ICC_TURNOFF_REG,
.icc_mask = BUCK3_ICC_SHIFT,
},
{
.dt_node_name = "buck4",
.voltage_table = buck4_voltage_table,
.voltage_table_size = ARRAY_SIZE(buck4_voltage_table),
.control_reg = BUCK4_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = BUCK4_PWRCTRL_REG,
.pull_down_reg = BUCK_PULL_DOWN_REG,
.pull_down = BUCK4_PULL_DOWN_SHIFT,
.mask_reset_reg = MASK_RESET_BUCK_REG,
.mask_reset = BUCK4_MASK_RESET,
.icc_reg = BUCK_ICC_TURNOFF_REG,
.icc_mask = BUCK4_ICC_SHIFT,
},
{
.dt_node_name = "ldo1",
.voltage_table = ldo1_voltage_table,
.voltage_table_size = ARRAY_SIZE(ldo1_voltage_table),
.control_reg = LDO1_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = LDO1_PWRCTRL_REG,
.mask_reset_reg = MASK_RESET_LDO_REG,
.mask_reset = LDO1_MASK_RESET,
.icc_reg = LDO_ICC_TURNOFF_REG,
.icc_mask = LDO1_ICC_SHIFT,
},
{
.dt_node_name = "ldo2",
.voltage_table = ldo2_voltage_table,
.voltage_table_size = ARRAY_SIZE(ldo2_voltage_table),
.control_reg = LDO2_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = LDO2_PWRCTRL_REG,
.mask_reset_reg = MASK_RESET_LDO_REG,
.mask_reset = LDO2_MASK_RESET,
.icc_reg = LDO_ICC_TURNOFF_REG,
.icc_mask = LDO2_ICC_SHIFT,
},
{
.dt_node_name = "ldo3",
.voltage_table = ldo3_voltage_table,
.voltage_table_size = ARRAY_SIZE(ldo3_voltage_table),
.control_reg = LDO3_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = LDO3_PWRCTRL_REG,
.mask_reset_reg = MASK_RESET_LDO_REG,
.mask_reset = LDO3_MASK_RESET,
.icc_reg = LDO_ICC_TURNOFF_REG,
.icc_mask = LDO3_ICC_SHIFT,
},
{
.dt_node_name = "ldo4",
.voltage_table = ldo4_voltage_table,
.voltage_table_size = ARRAY_SIZE(ldo4_voltage_table),
.control_reg = LDO4_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = LDO4_PWRCTRL_REG,
.mask_reset_reg = MASK_RESET_LDO_REG,
.mask_reset = LDO4_MASK_RESET,
.icc_reg = LDO_ICC_TURNOFF_REG,
.icc_mask = LDO4_ICC_SHIFT,
},
{
.dt_node_name = "ldo5",
.voltage_table = ldo5_voltage_table,
.voltage_table_size = ARRAY_SIZE(ldo5_voltage_table),
.control_reg = LDO5_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = LDO5_PWRCTRL_REG,
.mask_reset_reg = MASK_RESET_LDO_REG,
.mask_reset = LDO5_MASK_RESET,
.icc_reg = LDO_ICC_TURNOFF_REG,
.icc_mask = LDO5_ICC_SHIFT,
},
{
.dt_node_name = "ldo6",
.voltage_table = ldo6_voltage_table,
.voltage_table_size = ARRAY_SIZE(ldo6_voltage_table),
.control_reg = LDO6_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = LDO6_PWRCTRL_REG,
.mask_reset_reg = MASK_RESET_LDO_REG,
.mask_reset = LDO6_MASK_RESET,
.icc_reg = LDO_ICC_TURNOFF_REG,
.icc_mask = LDO6_ICC_SHIFT,
},
{
.dt_node_name = "vref_ddr",
.voltage_table = vref_ddr_voltage_table,
.voltage_table_size = ARRAY_SIZE(vref_ddr_voltage_table),
.control_reg = VREF_DDR_CONTROL_REG,
.enable_mask = LDO_BUCK_ENABLE_MASK,
.low_power_reg = VREF_DDR_PWRCTRL_REG,
.mask_reset_reg = MASK_RESET_LDO_REG,
.mask_reset = VREF_DDR_MASK_RESET,
},
{
.dt_node_name = "boost",
.voltage_table = fixed_5v_voltage_table,
.voltage_table_size = ARRAY_SIZE(fixed_5v_voltage_table),
.control_reg = USB_CONTROL_REG,
.enable_mask = BOOST_ENABLED,
.icc_reg = BUCK_ICC_TURNOFF_REG,
.icc_mask = BOOST_ICC_SHIFT,
},
{
.dt_node_name = "pwr_sw1",
.voltage_table = fixed_5v_voltage_table,
.voltage_table_size = ARRAY_SIZE(fixed_5v_voltage_table),
.control_reg = USB_CONTROL_REG,
.enable_mask = USBSW_OTG_SWITCH_ENABLED,
.icc_reg = BUCK_ICC_TURNOFF_REG,
.icc_mask = PWR_SW1_ICC_SHIFT,
},
{
.dt_node_name = "pwr_sw2",
.voltage_table = fixed_5v_voltage_table,
.voltage_table_size = ARRAY_SIZE(fixed_5v_voltage_table),
.control_reg = USB_CONTROL_REG,
.enable_mask = SWIN_SWOUT_ENABLED,
.icc_reg = BUCK_ICC_TURNOFF_REG,
.icc_mask = PWR_SW2_ICC_SHIFT,
},
};
#define MAX_REGUL ARRAY_SIZE(regulators_table)
static const struct regul_struct *get_regulator_data(const char *name)
{
uint8_t i;
for (i = 0 ; i < MAX_REGUL ; i++) {
if (strncmp(name, regulators_table[i].dt_node_name,
strlen(regulators_table[i].dt_node_name)) == 0) {
return &regulators_table[i];
}
}
/* Regulator not found */
panic();
return NULL;
}
static uint8_t voltage_to_index(const char *name, uint16_t millivolts)
{
const struct regul_struct *regul = get_regulator_data(name);
uint8_t i;
for (i = 0 ; i < regul->voltage_table_size ; i++) {
if (regul->voltage_table[i] == millivolts) {
return i;
}
}
/* Voltage not found */
panic();
return 0;
}
int stpmic1_powerctrl_on(void)
{
return stpmic1_register_update(MAIN_CONTROL_REG, PWRCTRL_PIN_VALID,
PWRCTRL_PIN_VALID);
}
int stpmic1_switch_off(void)
{
return stpmic1_register_update(MAIN_CONTROL_REG, 1,
SOFTWARE_SWITCH_OFF_ENABLED);
}
int stpmic1_regulator_enable(const char *name)
{
const struct regul_struct *regul = get_regulator_data(name);
return stpmic1_register_update(regul->control_reg, regul->enable_mask,
regul->enable_mask);
}
int stpmic1_regulator_disable(const char *name)
{
const struct regul_struct *regul = get_regulator_data(name);
return stpmic1_register_update(regul->control_reg, 0,
regul->enable_mask);
}
bool stpmic1_is_regulator_enabled(const char *name)
{
uint8_t val;
const struct regul_struct *regul = get_regulator_data(name);
if (stpmic1_register_read(regul->control_reg, &val) != 0) {
panic();
}
return (val & regul->enable_mask) == regul->enable_mask;
}
int stpmic1_regulator_voltage_set(const char *name, uint16_t millivolts)
{
uint8_t voltage_index = voltage_to_index(name, millivolts);
const struct regul_struct *regul = get_regulator_data(name);
uint8_t mask;
if ((strncmp(name, "ldo3", 5) == 0) && ldo3_special_mode) {
/*
* when the LDO3 is in special mode, we do not change voltage,
* because by setting voltage, the LDO would leaves sink-source
* mode. There is obviously no reason to leave sink-source mode
* at runtime.
*/
return 0;
}
/* Voltage can be set for buck<N> or ldo<N> (except ldo4) regulators */
if (strncmp(name, "buck", 4) == 0) {
mask = BUCK_VOLTAGE_MASK;
} else if ((strncmp(name, "ldo", 3) == 0) &&
(strncmp(name, "ldo4", 5) != 0)) {
mask = LDO_VOLTAGE_MASK;
} else {
return 0;
}
return stpmic1_register_update(regul->control_reg,
voltage_index << LDO_BUCK_VOLTAGE_SHIFT,
mask);
}
int stpmic1_regulator_pull_down_set(const char *name)
{
const struct regul_struct *regul = get_regulator_data(name);
if (regul->pull_down_reg != 0) {
return stpmic1_register_update(regul->pull_down_reg,
BIT(regul->pull_down),
LDO_BUCK_PULL_DOWN_MASK <<
regul->pull_down);
}
return 0;
}
int stpmic1_regulator_mask_reset_set(const char *name)
{
const struct regul_struct *regul = get_regulator_data(name);
if (regul->mask_reset_reg == 0U) {
return -EPERM;
}
return stpmic1_register_update(regul->mask_reset_reg,
BIT(regul->mask_reset),
LDO_BUCK_RESET_MASK <<
regul->mask_reset);
}
int stpmic1_regulator_icc_set(const char *name)
{
const struct regul_struct *regul = get_regulator_data(name);
if (regul->mask_reset_reg == 0U) {
return -EPERM;
}
return stpmic1_register_update(regul->icc_reg,
BIT(regul->icc_mask),
BIT(regul->icc_mask));
}
int stpmic1_regulator_sink_mode_set(const char *name)
{
if (strncmp(name, "ldo3", 5) != 0) {
return -EPERM;
}
ldo3_special_mode = true;
/* disable bypass mode, enable sink mode */
return stpmic1_register_update(LDO3_CONTROL_REG,
LDO3_DDR_SEL << LDO_BUCK_VOLTAGE_SHIFT,
LDO3_BYPASS | LDO_VOLTAGE_MASK);
}
int stpmic1_regulator_bypass_mode_set(const char *name)
{
if (strncmp(name, "ldo3", 5) != 0) {
return -EPERM;
}
ldo3_special_mode = true;
/* enable bypass mode, disable sink mode */
return stpmic1_register_update(LDO3_CONTROL_REG,
LDO3_BYPASS,
LDO3_BYPASS | LDO_VOLTAGE_MASK);
}
int stpmic1_active_discharge_mode_set(const char *name)
{
if (strncmp(name, "pwr_sw1", 8) == 0) {
return stpmic1_register_update(USB_CONTROL_REG,
VBUS_OTG_DISCHARGE,
VBUS_OTG_DISCHARGE);
}
if (strncmp(name, "pwr_sw2", 8) == 0) {
return stpmic1_register_update(USB_CONTROL_REG,
SW_OUT_DISCHARGE,
SW_OUT_DISCHARGE);
}
return -EPERM;
}
int stpmic1_regulator_levels_mv(const char *name, const uint16_t **levels,
size_t *levels_count)
{
const struct regul_struct *regul = get_regulator_data(name);
if ((strncmp(name, "ldo3", 5) == 0) && ldo3_special_mode) {
*levels_count = ARRAY_SIZE(ldo3_special_mode_table);
*levels = ldo3_special_mode_table;
} else {
*levels_count = regul->voltage_table_size;
*levels = regul->voltage_table;
}
return 0;
}
int stpmic1_regulator_voltage_get(const char *name)
{
const struct regul_struct *regul = get_regulator_data(name);
uint8_t value;
uint8_t mask;
int status;
if ((strncmp(name, "ldo3", 5) == 0) && ldo3_special_mode) {
return 0;
}
/* Voltage can be set for buck<N> or ldo<N> (except ldo4) regulators */
if (strncmp(name, "buck", 4) == 0) {
mask = BUCK_VOLTAGE_MASK;
} else if ((strncmp(name, "ldo", 3) == 0) &&
(strncmp(name, "ldo4", 5) != 0)) {
mask = LDO_VOLTAGE_MASK;
} else {
return 0;
}
status = stpmic1_register_read(regul->control_reg, &value);
if (status < 0) {
return status;
}
value = (value & mask) >> LDO_BUCK_VOLTAGE_SHIFT;
if (value > regul->voltage_table_size) {
return -ERANGE;
}
return (int)regul->voltage_table[value];
}
int stpmic1_register_read(uint8_t register_id, uint8_t *value)
{
return stm32_i2c_mem_read(pmic_i2c_handle, pmic_i2c_addr,
(uint16_t)register_id,
I2C_MEMADD_SIZE_8BIT, value,
1, I2C_TIMEOUT_MS);
}
int stpmic1_register_write(uint8_t register_id, uint8_t value)
{
int status;
status = stm32_i2c_mem_write(pmic_i2c_handle, pmic_i2c_addr,
(uint16_t)register_id,
I2C_MEMADD_SIZE_8BIT, &value,
1, I2C_TIMEOUT_MS);
#if ENABLE_ASSERTIONS
if (status != 0) {
return status;
}
if ((register_id != WATCHDOG_CONTROL_REG) && (register_id <= 0x40U)) {
uint8_t readval;
status = stpmic1_register_read(register_id, &readval);
if (status != 0) {
return status;
}
if (readval != value) {
return -EIO;
}
}
#endif
return status;
}
int stpmic1_register_update(uint8_t register_id, uint8_t value, uint8_t mask)
{
int status;
uint8_t val;
status = stpmic1_register_read(register_id, &val);
if (status != 0) {
return status;
}
val = (val & ~mask) | (value & mask);
return stpmic1_register_write(register_id, val);
}
void stpmic1_bind_i2c(struct i2c_handle_s *i2c_handle, uint16_t i2c_addr)
{
pmic_i2c_handle = i2c_handle;
pmic_i2c_addr = i2c_addr;
}
void stpmic1_dump_regulators(void)
{
uint32_t i;
for (i = 0U; i < MAX_REGUL; i++) {
const char *name __unused = regulators_table[i].dt_node_name;
VERBOSE("PMIC regul %s: %sable, %dmV",
name,
stpmic1_is_regulator_enabled(name) ? "en" : "dis",
stpmic1_regulator_voltage_get(name));
}
}
int stpmic1_get_version(unsigned long *version)
{
uint8_t read_val;
int status;
status = stpmic1_register_read(VERSION_STATUS_REG, &read_val);
if (status < 0) {
return status;
}
*version = (unsigned long)read_val;
return 0;
}