Skip to content

Commit 1795b6e

Browse files
committed
regulator: add ti tps55287
add ti tps55287 regulator Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
1 parent 2526911 commit 1795b6e

File tree

6 files changed

+200
-0
lines changed

6 files changed

+200
-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: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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/sys/byteorder.h>
11+
#include <zephyr/sys/linear_range.h>
12+
#include <zephyr/sys/util.h>
13+
14+
#define TPS55287_REG_REF 0x00U
15+
#define TPS55287_REG_VOUT_FS 0x04U
16+
#define TPS55287_REG_MODE 0x06U
17+
18+
#define TPS55287_REG_MODE_OE BIT(7)
19+
20+
static const struct linear_range core_ranges[] = {
21+
LINEAR_RANGE_INIT(800000u, 2500u, 0xf0, BIT_MASK(11)),
22+
LINEAR_RANGE_INIT(800000u, 5000u, 0x50, BIT_MASK(11)),
23+
LINEAR_RANGE_INIT(800000u, 7500u, 0x1a, BIT_MASK(11)),
24+
LINEAR_RANGE_INIT(800000u, 10000u, 0x00, BIT_MASK(11)),
25+
};
26+
27+
struct regulator_tps55287_config {
28+
struct regulator_common_config common;
29+
struct i2c_dt_spec i2c;
30+
};
31+
32+
struct regulator_tps55287_data {
33+
struct regulator_common_data data;
34+
};
35+
36+
static unsigned int regulator_tps55287_count_voltages(const struct device *dev)
37+
{
38+
return linear_range_group_values_count(core_ranges, ARRAY_SIZE(core_ranges));
39+
}
40+
41+
static int regulator_tps55287_list_voltage(const struct device *dev, unsigned int idx,
42+
int32_t *volt_uv)
43+
{
44+
for (uint8_t i = 0U; i < ARRAY_SIZE(core_ranges); i++) {
45+
if (linear_range_get_value(&core_ranges[i], idx, volt_uv) == 0) {
46+
return 0;
47+
}
48+
idx -= linear_range_values_count(&core_ranges[i]);
49+
}
50+
51+
return -EINVAL;
52+
}
53+
54+
static int regulator_tps55287_set_voltage(const struct device *dev, int32_t min_uv, int32_t max_uv)
55+
{
56+
const struct regulator_tps55287_config *config = dev->config;
57+
uint8_t buf[3] = {0};
58+
uint16_t idx;
59+
uint8_t vout_fs_reg;
60+
int ret;
61+
62+
ret = i2c_reg_read_byte_dt(&config->i2c, TPS55287_REG_VOUT_FS, &vout_fs_reg);
63+
if (ret < 0) {
64+
return ret;
65+
}
66+
67+
vout_fs_reg &= 0x03;
68+
69+
ret = linear_range_get_win_index(&core_ranges[vout_fs_reg], min_uv, max_uv, &idx);
70+
71+
/* If we couldn't find a matching voltage in the current range, check the other ranges */
72+
if (ret < 0) {
73+
vout_fs_reg = ARRAY_SIZE(core_ranges);
74+
/* We start with the highest voltage range and work our way down */
75+
do {
76+
vout_fs_reg--;
77+
78+
ret = linear_range_get_win_index(&core_ranges[vout_fs_reg], min_uv, max_uv,
79+
&idx);
80+
} while ((ret < 0) && (vout_fs_reg > 0U));
81+
82+
if (ret < 0) {
83+
return ret;
84+
}
85+
86+
ret = i2c_reg_write_byte_dt(&config->i2c, TPS55287_REG_VOUT_FS, vout_fs_reg);
87+
if (ret < 0) {
88+
return ret;
89+
}
90+
}
91+
92+
buf[0] = TPS55287_REG_REF;
93+
94+
sys_put_be16(idx, &buf[1]);
95+
96+
return i2c_write_dt(&config->i2c, buf, sizeof(buf));
97+
}
98+
99+
static int regulator_tps55287_get_voltage(const struct device *dev, int32_t *volt_uv)
100+
{
101+
const struct regulator_tps55287_config *config = dev->config;
102+
uint8_t vout_fs_reg = 0;
103+
uint8_t buf[2] = {0};
104+
int ret;
105+
106+
ret = i2c_reg_read_byte_dt(&config->i2c, TPS55287_REG_VOUT_FS, &vout_fs_reg);
107+
if (ret < 0) {
108+
return ret;
109+
}
110+
111+
ret = i2c_burst_read_dt(&config->i2c, TPS55287_REG_REF, buf, sizeof(buf));
112+
if (ret < 0) {
113+
return ret;
114+
}
115+
116+
return linear_range_get_value(&core_ranges[vout_fs_reg & 0x03], sys_get_be16(buf), volt_uv);
117+
}
118+
119+
static int regulator_tps55287_enable(const struct device *dev)
120+
{
121+
const struct regulator_tps55287_config *config = dev->config;
122+
123+
return i2c_reg_update_byte_dt(&config->i2c, TPS55287_REG_MODE, TPS55287_REG_MODE_OE,
124+
TPS55287_REG_MODE_OE);
125+
}
126+
127+
static int regulator_tps55287_disable(const struct device *dev)
128+
{
129+
const struct regulator_tps55287_config *config = dev->config;
130+
131+
return i2c_reg_update_byte_dt(&config->i2c, TPS55287_REG_MODE, TPS55287_REG_MODE_OE, 0);
132+
}
133+
134+
static int regulator_tps55287_init(const struct device *dev)
135+
{
136+
regulator_common_data_init(dev);
137+
138+
return regulator_common_init(dev, false);
139+
}
140+
141+
static DEVICE_API(regulator, api) = {
142+
.enable = regulator_tps55287_enable,
143+
.disable = regulator_tps55287_disable,
144+
.count_voltages = regulator_tps55287_count_voltages,
145+
.list_voltage = regulator_tps55287_list_voltage,
146+
.set_voltage = regulator_tps55287_set_voltage,
147+
.get_voltage = regulator_tps55287_get_voltage,
148+
};
149+
150+
#define REGULATOR_TPS55287_DEFINE_ALL(inst) \
151+
static struct regulator_tps55287_data data_##inst; \
152+
\
153+
static const struct regulator_tps55287_config config_##inst = { \
154+
.common = REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst), \
155+
.i2c = I2C_DT_SPEC_INST_GET(inst), \
156+
}; \
157+
\
158+
DEVICE_DT_INST_DEFINE(inst, regulator_tps55287_init, NULL, &data_##inst, &config_##inst, \
159+
POST_KERNEL, CONFIG_REGULATOR_TPS55287_INIT_PRIORITY, &api);
160+
161+
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
@@ -158,3 +158,8 @@ npm1304@9 {
158158
LDO2 {};
159159
};
160160
};
161+
162+
tps55287@a {
163+
compatible = "ti,tps55287";
164+
reg = <0xa>;
165+
};

0 commit comments

Comments
 (0)