2018-04-26 15:13:09 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2018, STMicroelectronics - All Rights Reserved
|
|
|
|
* Author: Christophe Kerello <christophe.kerello@st.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2018-04-26 15:13:09 +00:00
|
|
|
#include <power/pmic.h>
|
|
|
|
#include <power/regulator.h>
|
2019-02-04 10:26:16 +00:00
|
|
|
#include <power/stpmic1.h>
|
2018-04-26 15:13:09 +00:00
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
struct stpmic1_range {
|
2018-04-26 15:13:09 +00:00
|
|
|
int min_uv;
|
|
|
|
int min_sel;
|
|
|
|
int max_sel;
|
|
|
|
int step;
|
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
struct stpmic1_output {
|
|
|
|
const struct stpmic1_range *ranges;
|
2018-04-26 15:13:09 +00:00
|
|
|
int nbranges;
|
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
#define STPMIC1_MODE(_id, _val, _name) { \
|
2018-04-26 15:13:09 +00:00
|
|
|
.id = _id, \
|
|
|
|
.register_value = _val, \
|
|
|
|
.name = _name, \
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
#define STPMIC1_RANGE(_min_uv, _min_sel, _max_sel, _step) { \
|
2018-04-26 15:13:09 +00:00
|
|
|
.min_uv = _min_uv, \
|
|
|
|
.min_sel = _min_sel, \
|
|
|
|
.max_sel = _max_sel, \
|
|
|
|
.step = _step, \
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
#define STPMIC1_OUTPUT(_ranges, _nbranges) { \
|
2018-04-26 15:13:09 +00:00
|
|
|
.ranges = _ranges, \
|
|
|
|
.nbranges = _nbranges, \
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_output_find_uv(int sel,
|
|
|
|
const struct stpmic1_output *output)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
2019-02-04 10:26:17 +00:00
|
|
|
const struct stpmic1_range *range;
|
2018-04-26 15:13:09 +00:00
|
|
|
int i;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
for (i = 0, range = output->ranges;
|
|
|
|
i < output->nbranges; i++, range++) {
|
2018-04-26 15:13:09 +00:00
|
|
|
if (sel >= range->min_sel && sel <= range->max_sel)
|
|
|
|
return range->min_uv +
|
|
|
|
(sel - range->min_sel) * range->step;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_output_find_sel(int uv,
|
|
|
|
const struct stpmic1_output *output)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
2019-02-04 10:26:17 +00:00
|
|
|
const struct stpmic1_range *range;
|
2018-04-26 15:13:09 +00:00
|
|
|
int i;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
for (i = 0, range = output->ranges;
|
|
|
|
i < output->nbranges; i++, range++) {
|
2018-04-26 15:13:09 +00:00
|
|
|
if (uv == range->min_uv && !range->step)
|
|
|
|
return range->min_sel;
|
|
|
|
|
|
|
|
if (uv >= range->min_uv &&
|
|
|
|
uv <= range->min_uv +
|
|
|
|
(range->max_sel - range->min_sel) * range->step)
|
|
|
|
return range->min_sel +
|
|
|
|
(uv - range->min_uv) / range->step;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BUCK regulators
|
|
|
|
*/
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_range buck1_ranges[] = {
|
|
|
|
STPMIC1_RANGE(725000, 0, 4, 0),
|
|
|
|
STPMIC1_RANGE(725000, 5, 36, 25000),
|
|
|
|
STPMIC1_RANGE(1500000, 37, 63, 0),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_range buck2_ranges[] = {
|
|
|
|
STPMIC1_RANGE(1000000, 0, 17, 0),
|
|
|
|
STPMIC1_RANGE(1050000, 18, 19, 0),
|
|
|
|
STPMIC1_RANGE(1100000, 20, 21, 0),
|
|
|
|
STPMIC1_RANGE(1150000, 22, 23, 0),
|
|
|
|
STPMIC1_RANGE(1200000, 24, 25, 0),
|
|
|
|
STPMIC1_RANGE(1250000, 26, 27, 0),
|
|
|
|
STPMIC1_RANGE(1300000, 28, 29, 0),
|
|
|
|
STPMIC1_RANGE(1350000, 30, 31, 0),
|
|
|
|
STPMIC1_RANGE(1400000, 32, 33, 0),
|
|
|
|
STPMIC1_RANGE(1450000, 34, 35, 0),
|
|
|
|
STPMIC1_RANGE(1500000, 36, 63, 0),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_range buck3_ranges[] = {
|
|
|
|
STPMIC1_RANGE(1000000, 0, 19, 0),
|
|
|
|
STPMIC1_RANGE(1100000, 20, 23, 0),
|
|
|
|
STPMIC1_RANGE(1200000, 24, 27, 0),
|
|
|
|
STPMIC1_RANGE(1300000, 28, 31, 0),
|
|
|
|
STPMIC1_RANGE(1400000, 32, 35, 0),
|
|
|
|
STPMIC1_RANGE(1500000, 36, 55, 100000),
|
|
|
|
STPMIC1_RANGE(3400000, 56, 63, 0),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_range buck4_ranges[] = {
|
|
|
|
STPMIC1_RANGE(600000, 0, 27, 25000),
|
|
|
|
STPMIC1_RANGE(1300000, 28, 29, 0),
|
|
|
|
STPMIC1_RANGE(1350000, 30, 31, 0),
|
|
|
|
STPMIC1_RANGE(1400000, 32, 33, 0),
|
|
|
|
STPMIC1_RANGE(1450000, 34, 35, 0),
|
|
|
|
STPMIC1_RANGE(1500000, 36, 60, 100000),
|
|
|
|
STPMIC1_RANGE(3900000, 61, 63, 0),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* BUCK: 1,2,3,4 - voltage ranges */
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_output buck_voltage_range[] = {
|
|
|
|
STPMIC1_OUTPUT(buck1_ranges, ARRAY_SIZE(buck1_ranges)),
|
|
|
|
STPMIC1_OUTPUT(buck2_ranges, ARRAY_SIZE(buck2_ranges)),
|
|
|
|
STPMIC1_OUTPUT(buck3_ranges, ARRAY_SIZE(buck3_ranges)),
|
|
|
|
STPMIC1_OUTPUT(buck4_ranges, ARRAY_SIZE(buck4_ranges)),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* BUCK modes */
|
|
|
|
static const struct dm_regulator_mode buck_modes[] = {
|
2019-02-04 10:26:18 +00:00
|
|
|
STPMIC1_MODE(STPMIC1_PREG_MODE_HP, STPMIC1_PREG_MODE_HP, "HP"),
|
|
|
|
STPMIC1_MODE(STPMIC1_PREG_MODE_LP, STPMIC1_PREG_MODE_LP, "LP"),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_buck_get_uv(struct udevice *dev, int buck)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int sel;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
sel = pmic_reg_read(dev, STPMIC1_BUCKX_MAIN_CR(buck));
|
2018-04-26 15:13:09 +00:00
|
|
|
if (sel < 0)
|
|
|
|
return sel;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
sel &= STPMIC1_BUCK_VOUT_MASK;
|
|
|
|
sel >>= STPMIC1_BUCK_VOUT_SHIFT;
|
2018-04-26 15:13:09 +00:00
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
return stpmic1_output_find_uv(sel, &buck_voltage_range[buck]);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_buck_get_value(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
2019-02-04 10:26:17 +00:00
|
|
|
return stpmic1_buck_get_uv(dev->parent, dev->driver_data - 1);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_buck_set_value(struct udevice *dev, int uv)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int sel, buck = dev->driver_data - 1;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
sel = stpmic1_output_find_sel(uv, &buck_voltage_range[buck]);
|
2018-04-26 15:13:09 +00:00
|
|
|
if (sel < 0)
|
|
|
|
return sel;
|
|
|
|
|
|
|
|
return pmic_clrsetbits(dev->parent,
|
2019-02-04 10:26:18 +00:00
|
|
|
STPMIC1_BUCKX_MAIN_CR(buck),
|
|
|
|
STPMIC1_BUCK_VOUT_MASK,
|
|
|
|
sel << STPMIC1_BUCK_VOUT_SHIFT);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_buck_get_enable(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pmic_reg_read(dev->parent,
|
2019-02-04 10:26:18 +00:00
|
|
|
STPMIC1_BUCKX_MAIN_CR(dev->driver_data - 1));
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
return ret & STPMIC1_BUCK_ENA ? true : false;
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_buck_set_enable(struct udevice *dev, bool enable)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
2019-02-04 10:26:17 +00:00
|
|
|
int delay = enable ? STPMIC1_DEFAULT_START_UP_DELAY_MS :
|
|
|
|
STPMIC1_DEFAULT_STOP_DELAY_MS;
|
2018-04-26 15:13:09 +00:00
|
|
|
int ret, uv;
|
|
|
|
|
|
|
|
/* if regulator is already in the wanted state, nothing to do */
|
2019-02-04 10:26:17 +00:00
|
|
|
if (stpmic1_buck_get_enable(dev) == enable)
|
2018-04-26 15:13:09 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
2019-02-04 10:26:17 +00:00
|
|
|
uv = stpmic1_buck_get_value(dev);
|
|
|
|
if (uv < uc_pdata->min_uV || uv > uc_pdata->max_uV)
|
|
|
|
stpmic1_buck_set_value(dev, uc_pdata->min_uV);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = pmic_clrsetbits(dev->parent,
|
2019-02-04 10:26:18 +00:00
|
|
|
STPMIC1_BUCKX_MAIN_CR(dev->driver_data - 1),
|
|
|
|
STPMIC1_BUCK_ENA, enable ? STPMIC1_BUCK_ENA : 0);
|
2018-06-27 09:59:47 +00:00
|
|
|
mdelay(delay);
|
2018-04-26 15:13:09 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_buck_get_mode(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pmic_reg_read(dev->parent,
|
2019-02-04 10:26:18 +00:00
|
|
|
STPMIC1_BUCKX_MAIN_CR(dev->driver_data - 1));
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
return ret & STPMIC1_BUCK_PREG_MODE ? STPMIC1_PREG_MODE_LP :
|
|
|
|
STPMIC1_PREG_MODE_HP;
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_buck_set_mode(struct udevice *dev, int mode)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
return pmic_clrsetbits(dev->parent,
|
2019-02-04 10:26:18 +00:00
|
|
|
STPMIC1_BUCKX_MAIN_CR(dev->driver_data - 1),
|
|
|
|
STPMIC1_BUCK_PREG_MODE,
|
|
|
|
mode ? STPMIC1_BUCK_PREG_MODE : 0);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_buck_probe(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
if (!dev->driver_data || dev->driver_data > STPMIC1_MAX_BUCK)
|
2018-04-26 15:13:09 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
|
|
|
|
uc_pdata->type = REGULATOR_TYPE_BUCK;
|
|
|
|
uc_pdata->mode = (struct dm_regulator_mode *)buck_modes;
|
|
|
|
uc_pdata->mode_count = ARRAY_SIZE(buck_modes);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct dm_regulator_ops stpmic1_buck_ops = {
|
|
|
|
.get_value = stpmic1_buck_get_value,
|
|
|
|
.set_value = stpmic1_buck_set_value,
|
|
|
|
.get_enable = stpmic1_buck_get_enable,
|
|
|
|
.set_enable = stpmic1_buck_set_enable,
|
|
|
|
.get_mode = stpmic1_buck_get_mode,
|
|
|
|
.set_mode = stpmic1_buck_set_mode,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
U_BOOT_DRIVER(stpmic1_buck) = {
|
|
|
|
.name = "stpmic1_buck",
|
2018-04-26 15:13:09 +00:00
|
|
|
.id = UCLASS_REGULATOR,
|
2019-02-04 10:26:17 +00:00
|
|
|
.ops = &stpmic1_buck_ops,
|
|
|
|
.probe = stpmic1_buck_probe,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LDO regulators
|
|
|
|
*/
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_range ldo12_ranges[] = {
|
|
|
|
STPMIC1_RANGE(1700000, 0, 7, 0),
|
|
|
|
STPMIC1_RANGE(1700000, 8, 24, 100000),
|
|
|
|
STPMIC1_RANGE(3300000, 25, 31, 0),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_range ldo3_ranges[] = {
|
|
|
|
STPMIC1_RANGE(1700000, 0, 7, 0),
|
|
|
|
STPMIC1_RANGE(1700000, 8, 24, 100000),
|
|
|
|
STPMIC1_RANGE(3300000, 25, 30, 0),
|
2018-04-26 15:13:09 +00:00
|
|
|
/* Sel 31 is special case when LDO3 is in mode sync_source (BUCK2/2) */
|
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_range ldo5_ranges[] = {
|
|
|
|
STPMIC1_RANGE(1700000, 0, 7, 0),
|
|
|
|
STPMIC1_RANGE(1700000, 8, 30, 100000),
|
|
|
|
STPMIC1_RANGE(3900000, 31, 31, 0),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_range ldo6_ranges[] = {
|
|
|
|
STPMIC1_RANGE(900000, 0, 24, 100000),
|
|
|
|
STPMIC1_RANGE(3300000, 25, 31, 0),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* LDO: 1,2,3,4,5,6 - voltage ranges */
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct stpmic1_output ldo_voltage_range[] = {
|
|
|
|
STPMIC1_OUTPUT(ldo12_ranges, ARRAY_SIZE(ldo12_ranges)),
|
|
|
|
STPMIC1_OUTPUT(ldo12_ranges, ARRAY_SIZE(ldo12_ranges)),
|
|
|
|
STPMIC1_OUTPUT(ldo3_ranges, ARRAY_SIZE(ldo3_ranges)),
|
|
|
|
STPMIC1_OUTPUT(NULL, 0),
|
|
|
|
STPMIC1_OUTPUT(ldo5_ranges, ARRAY_SIZE(ldo5_ranges)),
|
|
|
|
STPMIC1_OUTPUT(ldo6_ranges, ARRAY_SIZE(ldo6_ranges)),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* LDO modes */
|
|
|
|
static const struct dm_regulator_mode ldo_modes[] = {
|
2019-02-04 10:26:17 +00:00
|
|
|
STPMIC1_MODE(STPMIC1_LDO_MODE_NORMAL,
|
|
|
|
STPMIC1_LDO_MODE_NORMAL, "NORMAL"),
|
|
|
|
STPMIC1_MODE(STPMIC1_LDO_MODE_BYPASS,
|
|
|
|
STPMIC1_LDO_MODE_BYPASS, "BYPASS"),
|
|
|
|
STPMIC1_MODE(STPMIC1_LDO_MODE_SINK_SOURCE,
|
|
|
|
STPMIC1_LDO_MODE_SINK_SOURCE, "SINK SOURCE"),
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_ldo_get_value(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int sel, ldo = dev->driver_data - 1;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
sel = pmic_reg_read(dev->parent, STPMIC1_LDOX_MAIN_CR(ldo));
|
2018-04-26 15:13:09 +00:00
|
|
|
if (sel < 0)
|
|
|
|
return sel;
|
|
|
|
|
|
|
|
/* ldo4 => 3,3V */
|
2019-02-04 10:26:17 +00:00
|
|
|
if (ldo == STPMIC1_LDO4)
|
|
|
|
return STPMIC1_LDO4_UV;
|
2018-04-26 15:13:09 +00:00
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
sel &= STPMIC1_LDO12356_VOUT_MASK;
|
|
|
|
sel >>= STPMIC1_LDO12356_VOUT_SHIFT;
|
2018-04-26 15:13:09 +00:00
|
|
|
|
|
|
|
/* ldo3, sel = 31 => BUCK2/2 */
|
2019-02-04 10:26:17 +00:00
|
|
|
if (ldo == STPMIC1_LDO3 && sel == STPMIC1_LDO3_DDR_SEL)
|
|
|
|
return stpmic1_buck_get_uv(dev->parent, STPMIC1_BUCK2) / 2;
|
2018-04-26 15:13:09 +00:00
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
return stpmic1_output_find_uv(sel, &ldo_voltage_range[ldo]);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_ldo_set_value(struct udevice *dev, int uv)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int sel, ldo = dev->driver_data - 1;
|
|
|
|
|
|
|
|
/* ldo4 => not possible */
|
2019-02-04 10:26:17 +00:00
|
|
|
if (ldo == STPMIC1_LDO4)
|
2018-04-26 15:13:09 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
sel = stpmic1_output_find_sel(uv, &ldo_voltage_range[ldo]);
|
2018-04-26 15:13:09 +00:00
|
|
|
if (sel < 0)
|
|
|
|
return sel;
|
|
|
|
|
|
|
|
return pmic_clrsetbits(dev->parent,
|
2019-02-04 10:26:18 +00:00
|
|
|
STPMIC1_LDOX_MAIN_CR(ldo),
|
|
|
|
STPMIC1_LDO12356_VOUT_MASK,
|
|
|
|
sel << STPMIC1_LDO12356_VOUT_SHIFT);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_ldo_get_enable(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pmic_reg_read(dev->parent,
|
2019-02-04 10:26:18 +00:00
|
|
|
STPMIC1_LDOX_MAIN_CR(dev->driver_data - 1));
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
return ret & STPMIC1_LDO_ENA ? true : false;
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_ldo_set_enable(struct udevice *dev, bool enable)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
2019-02-04 10:26:17 +00:00
|
|
|
int delay = enable ? STPMIC1_DEFAULT_START_UP_DELAY_MS :
|
|
|
|
STPMIC1_DEFAULT_STOP_DELAY_MS;
|
2018-04-26 15:13:09 +00:00
|
|
|
int ret, uv;
|
|
|
|
|
|
|
|
/* if regulator is already in the wanted state, nothing to do */
|
2019-02-04 10:26:17 +00:00
|
|
|
if (stpmic1_ldo_get_enable(dev) == enable)
|
2018-04-26 15:13:09 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
2019-02-04 10:26:17 +00:00
|
|
|
uv = stpmic1_ldo_get_value(dev);
|
|
|
|
if (uv < uc_pdata->min_uV || uv > uc_pdata->max_uV)
|
|
|
|
stpmic1_ldo_set_value(dev, uc_pdata->min_uV);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = pmic_clrsetbits(dev->parent,
|
2019-02-04 10:26:18 +00:00
|
|
|
STPMIC1_LDOX_MAIN_CR(dev->driver_data - 1),
|
|
|
|
STPMIC1_LDO_ENA, enable ? STPMIC1_LDO_ENA : 0);
|
2018-06-27 09:59:47 +00:00
|
|
|
mdelay(delay);
|
2018-04-26 15:13:09 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_ldo_get_mode(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int ret, ldo = dev->driver_data - 1;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
if (ldo != STPMIC1_LDO3)
|
2018-04-26 15:13:09 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_reg_read(dev->parent, STPMIC1_LDOX_MAIN_CR(ldo));
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
if (ret & STPMIC1_LDO3_MODE)
|
|
|
|
return STPMIC1_LDO_MODE_BYPASS;
|
2018-04-26 15:13:09 +00:00
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret &= STPMIC1_LDO12356_VOUT_MASK;
|
|
|
|
ret >>= STPMIC1_LDO12356_VOUT_SHIFT;
|
2018-04-26 15:13:09 +00:00
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
return ret == STPMIC1_LDO3_DDR_SEL ? STPMIC1_LDO_MODE_SINK_SOURCE :
|
|
|
|
STPMIC1_LDO_MODE_NORMAL;
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_ldo_set_mode(struct udevice *dev, int mode)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int ret, ldo = dev->driver_data - 1;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
if (ldo != STPMIC1_LDO3)
|
2018-04-26 15:13:09 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_reg_read(dev->parent, STPMIC1_LDOX_MAIN_CR(ldo));
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (mode) {
|
2019-02-04 10:26:17 +00:00
|
|
|
case STPMIC1_LDO_MODE_SINK_SOURCE:
|
2019-02-04 10:26:18 +00:00
|
|
|
ret &= ~STPMIC1_LDO12356_VOUT_MASK;
|
|
|
|
ret |= STPMIC1_LDO3_DDR_SEL << STPMIC1_LDO12356_VOUT_SHIFT;
|
2019-06-21 13:26:53 +00:00
|
|
|
/* fallthrough */
|
2019-02-04 10:26:17 +00:00
|
|
|
case STPMIC1_LDO_MODE_NORMAL:
|
|
|
|
ret &= ~STPMIC1_LDO3_MODE;
|
2018-04-26 15:13:09 +00:00
|
|
|
break;
|
2019-02-04 10:26:17 +00:00
|
|
|
case STPMIC1_LDO_MODE_BYPASS:
|
|
|
|
ret |= STPMIC1_LDO3_MODE;
|
2018-04-26 15:13:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
return pmic_reg_write(dev->parent, STPMIC1_LDOX_MAIN_CR(ldo), ret);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_ldo_probe(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
if (!dev->driver_data || dev->driver_data > STPMIC1_MAX_LDO)
|
2018-04-26 15:13:09 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
|
|
|
|
uc_pdata->type = REGULATOR_TYPE_LDO;
|
2019-02-04 10:26:17 +00:00
|
|
|
if (dev->driver_data - 1 == STPMIC1_LDO3) {
|
2018-04-26 15:13:09 +00:00
|
|
|
uc_pdata->mode = (struct dm_regulator_mode *)ldo_modes;
|
|
|
|
uc_pdata->mode_count = ARRAY_SIZE(ldo_modes);
|
|
|
|
} else {
|
|
|
|
uc_pdata->mode_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct dm_regulator_ops stpmic1_ldo_ops = {
|
|
|
|
.get_value = stpmic1_ldo_get_value,
|
|
|
|
.set_value = stpmic1_ldo_set_value,
|
|
|
|
.get_enable = stpmic1_ldo_get_enable,
|
|
|
|
.set_enable = stpmic1_ldo_set_enable,
|
|
|
|
.get_mode = stpmic1_ldo_get_mode,
|
|
|
|
.set_mode = stpmic1_ldo_set_mode,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
U_BOOT_DRIVER(stpmic1_ldo) = {
|
|
|
|
.name = "stpmic1_ldo",
|
2018-04-26 15:13:09 +00:00
|
|
|
.id = UCLASS_REGULATOR,
|
2019-02-04 10:26:17 +00:00
|
|
|
.ops = &stpmic1_ldo_ops,
|
|
|
|
.probe = stpmic1_ldo_probe,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* VREF DDR regulator
|
|
|
|
*/
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_vref_ddr_get_value(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
/* BUCK2/2 */
|
2019-02-04 10:26:17 +00:00
|
|
|
return stpmic1_buck_get_uv(dev->parent, STPMIC1_BUCK2) / 2;
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_vref_ddr_get_enable(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_reg_read(dev->parent, STPMIC1_REFDDR_MAIN_CR);
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
return ret & STPMIC1_VREF_ENA ? true : false;
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_vref_ddr_set_enable(struct udevice *dev, bool enable)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
2019-02-04 10:26:17 +00:00
|
|
|
int delay = enable ? STPMIC1_DEFAULT_START_UP_DELAY_MS :
|
|
|
|
STPMIC1_DEFAULT_STOP_DELAY_MS;
|
2018-04-26 15:13:09 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* if regulator is already in the wanted state, nothing to do */
|
2019-02-04 10:26:17 +00:00
|
|
|
if (stpmic1_vref_ddr_get_enable(dev) == enable)
|
2018-04-26 15:13:09 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_clrsetbits(dev->parent, STPMIC1_REFDDR_MAIN_CR,
|
|
|
|
STPMIC1_VREF_ENA, enable ? STPMIC1_VREF_ENA : 0);
|
2018-06-27 09:59:47 +00:00
|
|
|
mdelay(delay);
|
2018-04-26 15:13:09 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_vref_ddr_probe(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
|
|
|
|
uc_pdata->type = REGULATOR_TYPE_FIXED;
|
|
|
|
uc_pdata->mode_count = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct dm_regulator_ops stpmic1_vref_ddr_ops = {
|
|
|
|
.get_value = stpmic1_vref_ddr_get_value,
|
|
|
|
.get_enable = stpmic1_vref_ddr_get_enable,
|
|
|
|
.set_enable = stpmic1_vref_ddr_set_enable,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
U_BOOT_DRIVER(stpmic1_vref_ddr) = {
|
|
|
|
.name = "stpmic1_vref_ddr",
|
2018-04-26 15:13:09 +00:00
|
|
|
.id = UCLASS_REGULATOR,
|
2019-02-04 10:26:17 +00:00
|
|
|
.ops = &stpmic1_vref_ddr_ops,
|
|
|
|
.probe = stpmic1_vref_ddr_probe,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BOOST regulator
|
|
|
|
*/
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_boost_get_enable(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_reg_read(dev->parent, STPMIC1_BST_SW_CR);
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
return ret & STPMIC1_BST_ON ? true : false;
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_boost_set_enable(struct udevice *dev, bool enable)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_reg_read(dev->parent, STPMIC1_BST_SW_CR);
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
if (!enable && ret & STPMIC1_PWR_SW_ON)
|
2018-04-26 15:13:09 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* if regulator is already in the wanted state, nothing to do */
|
2019-02-04 10:26:18 +00:00
|
|
|
if (!!(ret & STPMIC1_BST_ON) == enable)
|
2018-04-26 15:13:09 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_clrsetbits(dev->parent, STPMIC1_BST_SW_CR,
|
|
|
|
STPMIC1_BST_ON,
|
|
|
|
enable ? STPMIC1_BST_ON : 0);
|
2018-04-26 15:13:09 +00:00
|
|
|
if (enable)
|
2019-02-04 10:26:17 +00:00
|
|
|
mdelay(STPMIC1_USB_BOOST_START_UP_DELAY_MS);
|
2018-04-26 15:13:09 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_boost_probe(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
|
|
|
|
uc_pdata->type = REGULATOR_TYPE_FIXED;
|
|
|
|
uc_pdata->mode_count = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct dm_regulator_ops stpmic1_boost_ops = {
|
|
|
|
.get_enable = stpmic1_boost_get_enable,
|
|
|
|
.set_enable = stpmic1_boost_set_enable,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
U_BOOT_DRIVER(stpmic1_boost) = {
|
|
|
|
.name = "stpmic1_boost",
|
2018-04-26 15:13:09 +00:00
|
|
|
.id = UCLASS_REGULATOR,
|
2019-02-04 10:26:17 +00:00
|
|
|
.ops = &stpmic1_boost_ops,
|
|
|
|
.probe = stpmic1_boost_probe,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* USB power switch
|
|
|
|
*/
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_pwr_sw_get_enable(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
uint mask = 1 << dev->driver_data;
|
|
|
|
int ret;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_reg_read(dev->parent, STPMIC1_BST_SW_CR);
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ret & mask ? true : false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_pwr_sw_set_enable(struct udevice *dev, bool enable)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
uint mask = 1 << dev->driver_data;
|
2019-02-04 10:26:17 +00:00
|
|
|
int delay = enable ? STPMIC1_DEFAULT_START_UP_DELAY_MS :
|
|
|
|
STPMIC1_DEFAULT_STOP_DELAY_MS;
|
2018-04-26 15:13:09 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_reg_read(dev->parent, STPMIC1_BST_SW_CR);
|
2018-04-26 15:13:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* if regulator is already in the wanted state, nothing to do */
|
|
|
|
if (!!(ret & mask) == enable)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Boost management */
|
2019-02-04 10:26:18 +00:00
|
|
|
if (enable && !(ret & STPMIC1_BST_ON)) {
|
|
|
|
pmic_clrsetbits(dev->parent, STPMIC1_BST_SW_CR,
|
|
|
|
STPMIC1_BST_ON, STPMIC1_BST_ON);
|
2019-02-04 10:26:17 +00:00
|
|
|
mdelay(STPMIC1_USB_BOOST_START_UP_DELAY_MS);
|
2019-02-04 10:26:18 +00:00
|
|
|
} else if (!enable && ret & STPMIC1_BST_ON &&
|
|
|
|
(ret & STPMIC1_PWR_SW_ON) != STPMIC1_PWR_SW_ON) {
|
|
|
|
pmic_clrsetbits(dev->parent, STPMIC1_BST_SW_CR,
|
|
|
|
STPMIC1_BST_ON, 0);
|
2018-04-26 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:18 +00:00
|
|
|
ret = pmic_clrsetbits(dev->parent, STPMIC1_BST_SW_CR,
|
2018-04-26 15:13:09 +00:00
|
|
|
mask, enable ? mask : 0);
|
2018-06-27 09:59:47 +00:00
|
|
|
mdelay(delay);
|
2018-04-26 15:13:09 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static int stpmic1_pwr_sw_probe(struct udevice *dev)
|
2018-04-26 15:13:09 +00:00
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
if (!dev->driver_data || dev->driver_data > STPMIC1_MAX_PWR_SW)
|
2018-04-26 15:13:09 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
|
|
|
|
uc_pdata->type = REGULATOR_TYPE_FIXED;
|
|
|
|
uc_pdata->mode_count = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
static const struct dm_regulator_ops stpmic1_pwr_sw_ops = {
|
|
|
|
.get_enable = stpmic1_pwr_sw_get_enable,
|
|
|
|
.set_enable = stpmic1_pwr_sw_set_enable,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
2019-02-04 10:26:17 +00:00
|
|
|
U_BOOT_DRIVER(stpmic1_pwr_sw) = {
|
|
|
|
.name = "stpmic1_pwr_sw",
|
2018-04-26 15:13:09 +00:00
|
|
|
.id = UCLASS_REGULATOR,
|
2019-02-04 10:26:17 +00:00
|
|
|
.ops = &stpmic1_pwr_sw_ops,
|
|
|
|
.probe = stpmic1_pwr_sw_probe,
|
2018-04-26 15:13:09 +00:00
|
|
|
};
|