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