Skip to content

Commit 5aa1d7d

Browse files
committed
regulator: add ti tps55287
add ti tps55287 regulator Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
1 parent 7acc119 commit 5aa1d7d

File tree

6 files changed

+220
-0
lines changed

6 files changed

+220
-0
lines changed

drivers/regulator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ zephyr_library_sources_ifdef(CONFIG_REGULATOR_PCA9420 regulator_pca9420.c)
2020
zephyr_library_sources_ifdef(CONFIG_REGULATOR_PCA9422 regulator_pca9422.c)
2121
zephyr_library_sources_ifdef(CONFIG_REGULATOR_PF1550 regulator_pf1550.c)
2222
zephyr_library_sources_ifdef(CONFIG_REGULATOR_SHELL regulator_shell.c)
23+
zephyr_library_sources_ifdef(CONFIG_REGULATOR_TPS55287 regulator_tps55287.c)
2324
zephyr_library_sources_ifdef(CONFIG_REGULATOR_RPI_PICO regulator_rpi_pico.c)
2425
zephyr_library_sources_ifdef(CONFIG_REGULATOR_NXP_VREF regulator_nxp_vref.c)
2526
zephyr_library_sources_ifdef(CONFIG_REGULATOR_NXP_VREFV1 regulator_nxp_vrefv1.c)

drivers/regulator/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ source "drivers/regulator/Kconfig.pca9420"
4242
source "drivers/regulator/Kconfig.pca9422"
4343
source "drivers/regulator/Kconfig.pf1550"
4444
source "drivers/regulator/Kconfig.rpi_pico"
45+
source "drivers/regulator/Kconfig.tps55287"
4546
source "drivers/regulator/Kconfig.nxp_vref"
4647
source "drivers/regulator/Kconfig.nxp_vrefv1"
4748
source "drivers/regulator/Kconfig.mpm54304"

drivers/regulator/Kconfig.tps55287

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config REGULATOR_TPS55287
5+
bool "TPS55287 regulator driver"
6+
default y
7+
depends on DT_HAS_TI_TPS55287_ENABLED
8+
select I2C
9+
help
10+
Enable support for the TPS55287 regulator.
11+
12+
if REGULATOR_TPS55287
13+
14+
config REGULATOR_TPS55287_INIT_PRIORITY
15+
int "TPS55287 regulator driver init priority"
16+
default 51
17+
help
18+
Init priority for the TPS55287 regulator driver. This must be higher
19+
than the I2C init priority.
20+
21+
endif
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#define DT_DRV_COMPAT ti_tps55287
7+
8+
#include <zephyr/drivers/i2c.h>
9+
#include <zephyr/drivers/regulator.h>
10+
#include <zephyr/logging/log.h>
11+
#include <zephyr/sys/byteorder.h>
12+
#include <zephyr/sys/linear_range.h>
13+
#include <zephyr/sys/util.h>
14+
15+
LOG_MODULE_REGISTER(tps55287, CONFIG_REGULATOR_LOG_LEVEL);
16+
17+
#define TPS55287_REG_REF 0x00U
18+
#define TPS55287_REG_VOUT_FS 0x04U
19+
#define TPS55287_REG_MODE 0x06U
20+
21+
#define TPS55287_REG_MODE_OE BIT(7)
22+
23+
static const struct linear_range core_ranges[] = {
24+
LINEAR_RANGE_INIT(800000u, 2500u, 0xf0, BIT_MASK(11)),
25+
LINEAR_RANGE_INIT(800000u, 5000u, 0x50, BIT_MASK(11)),
26+
LINEAR_RANGE_INIT(800000u, 7500u, 0x1a, BIT_MASK(11)),
27+
LINEAR_RANGE_INIT(800000u, 10000u, 0x00, BIT_MASK(11)),
28+
};
29+
30+
struct regulator_tps55287_config {
31+
struct regulator_common_config common;
32+
struct i2c_dt_spec i2c;
33+
};
34+
35+
struct regulator_tps55287_data {
36+
struct regulator_common_data data;
37+
};
38+
39+
static unsigned int regulator_tps55287_count_voltages(const struct device *dev)
40+
{
41+
return linear_range_group_values_count(core_ranges, ARRAY_SIZE(core_ranges));
42+
}
43+
44+
static int regulator_tps55287_list_voltage(const struct device *dev, unsigned int idx,
45+
int32_t *volt_uv)
46+
{
47+
for (uint8_t i = 0U; i < ARRAY_SIZE(core_ranges); i++) {
48+
if (linear_range_get_value(&core_ranges[i], idx, volt_uv) == 0) {
49+
return 0;
50+
}
51+
idx -= linear_range_values_count(&core_ranges[i]);
52+
}
53+
54+
return -EINVAL;
55+
}
56+
57+
static int regulator_tps55287_set_voltage(const struct device *dev, int32_t min_uv, int32_t max_uv)
58+
{
59+
const struct regulator_tps55287_config *config = dev->config;
60+
uint8_t buf[3] = {0};
61+
uint16_t idx;
62+
uint8_t vout_fs_reg;
63+
int ret;
64+
65+
ret = i2c_reg_read_byte_dt(&config->i2c, TPS55287_REG_VOUT_FS, &vout_fs_reg);
66+
if (ret < 0) {
67+
return ret;
68+
}
69+
70+
vout_fs_reg &= 0x03;
71+
72+
ret = linear_range_get_win_index(&core_ranges[vout_fs_reg], min_uv, max_uv, &idx);
73+
74+
/* If we couldn't find a matching voltage in the current range, check the other ranges */
75+
if (ret < 0) {
76+
vout_fs_reg = ARRAY_SIZE(core_ranges);
77+
/* We start with the highest voltage range and work our way down */
78+
do {
79+
vout_fs_reg--;
80+
81+
ret = linear_range_get_win_index(&core_ranges[vout_fs_reg], min_uv, max_uv,
82+
&idx);
83+
} while ((ret < 0) && (vout_fs_reg > 0U));
84+
85+
if (ret < 0) {
86+
return ret;
87+
}
88+
89+
ret = i2c_reg_write_byte_dt(&config->i2c, TPS55287_REG_VOUT_FS, vout_fs_reg);
90+
if (ret < 0) {
91+
return ret;
92+
}
93+
}
94+
95+
LOG_DBG("%s: Setting voltage to range %u, index %u", dev->name, vout_fs_reg, idx);
96+
97+
buf[0] = TPS55287_REG_REF;
98+
99+
sys_put_be16(idx, &buf[1]);
100+
101+
return i2c_write_dt(&config->i2c, buf, sizeof(buf));
102+
}
103+
104+
static int regulator_tps55287_get_voltage(const struct device *dev, int32_t *volt_uv)
105+
{
106+
const struct regulator_tps55287_config *config = dev->config;
107+
uint8_t vout_fs_reg = 0;
108+
uint8_t buf[2] = {0};
109+
uint16_t idx;
110+
int ret;
111+
112+
ret = i2c_reg_read_byte_dt(&config->i2c, TPS55287_REG_VOUT_FS, &vout_fs_reg);
113+
if (ret < 0) {
114+
return ret;
115+
}
116+
117+
ret = i2c_burst_read_dt(&config->i2c, TPS55287_REG_REF, buf, sizeof(buf));
118+
if (ret < 0) {
119+
return ret;
120+
}
121+
122+
vout_fs_reg &= 0x03;
123+
idx = sys_get_be16(buf);
124+
125+
ret = linear_range_get_value(&core_ranges[vout_fs_reg], idx, volt_uv);
126+
127+
LOG_DBG("%s: Got voltage: %d uV (range %u, index %u)", dev->name, *volt_uv, vout_fs_reg,
128+
idx);
129+
130+
return ret;
131+
}
132+
133+
static int regulator_tps55287_enable(const struct device *dev)
134+
{
135+
const struct regulator_tps55287_config *config = dev->config;
136+
137+
return i2c_reg_update_byte_dt(&config->i2c, TPS55287_REG_MODE, TPS55287_REG_MODE_OE,
138+
TPS55287_REG_MODE_OE);
139+
}
140+
141+
static int regulator_tps55287_disable(const struct device *dev)
142+
{
143+
const struct regulator_tps55287_config *config = dev->config;
144+
145+
return i2c_reg_update_byte_dt(&config->i2c, TPS55287_REG_MODE, TPS55287_REG_MODE_OE, 0);
146+
}
147+
148+
static int regulator_tps55287_init(const struct device *dev)
149+
{
150+
int ret;
151+
152+
regulator_common_data_init(dev);
153+
154+
ret = regulator_common_init(dev, false);
155+
if (ret < 0) {
156+
LOG_ERR("%s: Failed to initialize regulator: %d", dev->name, ret);
157+
}
158+
return ret;
159+
}
160+
161+
static DEVICE_API(regulator, api) = {
162+
.enable = regulator_tps55287_enable,
163+
.disable = regulator_tps55287_disable,
164+
.count_voltages = regulator_tps55287_count_voltages,
165+
.list_voltage = regulator_tps55287_list_voltage,
166+
.set_voltage = regulator_tps55287_set_voltage,
167+
.get_voltage = regulator_tps55287_get_voltage,
168+
};
169+
170+
#define REGULATOR_TPS55287_DEFINE_ALL(inst) \
171+
static struct regulator_tps55287_data data_##inst; \
172+
\
173+
static const struct regulator_tps55287_config config_##inst = { \
174+
.common = REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst), \
175+
.i2c = I2C_DT_SPEC_INST_GET(inst), \
176+
}; \
177+
\
178+
DEVICE_DT_INST_DEFINE(inst, regulator_tps55287_init, NULL, &data_##inst, &config_##inst, \
179+
POST_KERNEL, CONFIG_REGULATOR_TPS55287_INIT_PRIORITY, &api);
180+
181+
DT_INST_FOREACH_STATUS_OKAY(REGULATOR_TPS55287_DEFINE_ALL)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
TPS55287 core supply regulator
6+
7+
compatible: "ti,tps55287"
8+
9+
include:
10+
- name: i2c-device.yaml
11+
- name: regulator.yaml

tests/drivers/build_all/regulator/i2c.dtsi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,8 @@ npm1304@9 {
200200
LDO2 {};
201201
};
202202
};
203+
204+
tps55287@a {
205+
compatible = "ti,tps55287";
206+
reg = <0xa>;
207+
};

0 commit comments

Comments
 (0)