|
| 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) |
0 commit comments