1 | // SPDX-License-Identifier: GPL-2.0-only |
---|---|
2 | /* |
3 | * Copyright (C) 2023 Axis Communications AB |
4 | * |
5 | * Driver for Texas Instruments TPS6287x PMIC. |
6 | * Datasheet: https://www.ti.com/lit/ds/symlink/tps62873.pdf |
7 | */ |
8 | |
9 | #include <linux/err.h> |
10 | #include <linux/i2c.h> |
11 | #include <linux/mod_devicetable.h> |
12 | #include <linux/module.h> |
13 | #include <linux/regmap.h> |
14 | #include <linux/regulator/of_regulator.h> |
15 | #include <linux/regulator/machine.h> |
16 | #include <linux/regulator/driver.h> |
17 | #include <linux/bitfield.h> |
18 | #include <linux/linear_range.h> |
19 | |
20 | #define TPS6287X_VSET 0x00 |
21 | #define TPS6287X_CTRL1 0x01 |
22 | #define TPS6287X_CTRL1_VRAMP GENMASK(1, 0) |
23 | #define TPS6287X_CTRL1_FPWMEN BIT(4) |
24 | #define TPS6287X_CTRL1_SWEN BIT(5) |
25 | #define TPS6287X_CTRL2 0x02 |
26 | #define TPS6287X_CTRL2_VRANGE GENMASK(3, 2) |
27 | #define TPS6287X_CTRL3 0x03 |
28 | #define TPS6287X_STATUS 0x04 |
29 | |
30 | static const struct regmap_config tps6287x_regmap_config = { |
31 | .reg_bits = 8, |
32 | .val_bits = 8, |
33 | .max_register = TPS6287X_STATUS, |
34 | }; |
35 | |
36 | static const struct linear_range tps6287x_voltage_ranges[] = { |
37 | LINEAR_RANGE(400000, 0, 0xFF, 1250), |
38 | LINEAR_RANGE(400000, 0, 0xFF, 2500), |
39 | LINEAR_RANGE(400000, 0, 0xFF, 5000), |
40 | LINEAR_RANGE(800000, 0, 0xFF, 10000), |
41 | }; |
42 | |
43 | static const unsigned int tps6287x_voltage_range_sel[] = { |
44 | 0x0, 0x1, 0x2, 0x3 |
45 | }; |
46 | |
47 | static const unsigned int tps6287x_voltage_range_prefix[] = { |
48 | 0x000, 0x100, 0x200, 0x300 |
49 | }; |
50 | |
51 | static const unsigned int tps6287x_ramp_table[] = { |
52 | 10000, 5000, 1250, 500 |
53 | }; |
54 | |
55 | struct tps6287x_reg_data { |
56 | int range; |
57 | }; |
58 | |
59 | static int tps6287x_best_range(struct regulator_config *config, const struct regulator_desc *desc) |
60 | { |
61 | const struct linear_range *r; |
62 | int i; |
63 | |
64 | if (!config->init_data->constraints.apply_uV) |
65 | return -1; |
66 | |
67 | for (i = 0; i < desc->n_linear_ranges; i++) { |
68 | r = &desc->linear_ranges[i]; |
69 | if (r->min <= config->init_data->constraints.min_uV && |
70 | config->init_data->constraints.max_uV <= linear_range_get_max_value(r)) |
71 | return i; |
72 | } |
73 | return -1; |
74 | } |
75 | |
76 | static int tps6287x_set_mode(struct regulator_dev *rdev, unsigned int mode) |
77 | { |
78 | unsigned int val; |
79 | |
80 | switch (mode) { |
81 | case REGULATOR_MODE_NORMAL: |
82 | val = 0; |
83 | break; |
84 | case REGULATOR_MODE_FAST: |
85 | val = TPS6287X_CTRL1_FPWMEN; |
86 | break; |
87 | default: |
88 | return -EINVAL; |
89 | } |
90 | |
91 | return regmap_update_bits(map: rdev->regmap, TPS6287X_CTRL1, |
92 | TPS6287X_CTRL1_FPWMEN, val); |
93 | } |
94 | |
95 | static unsigned int tps6287x_get_mode(struct regulator_dev *rdev) |
96 | { |
97 | unsigned int val; |
98 | int ret; |
99 | |
100 | ret = regmap_read(map: rdev->regmap, TPS6287X_CTRL1, val: &val); |
101 | if (ret < 0) |
102 | return 0; |
103 | |
104 | return (val & TPS6287X_CTRL1_FPWMEN) ? REGULATOR_MODE_FAST : |
105 | REGULATOR_MODE_NORMAL; |
106 | } |
107 | |
108 | static unsigned int tps6287x_of_map_mode(unsigned int mode) |
109 | { |
110 | switch (mode) { |
111 | case REGULATOR_MODE_NORMAL: |
112 | case REGULATOR_MODE_FAST: |
113 | return mode; |
114 | default: |
115 | return REGULATOR_MODE_INVALID; |
116 | } |
117 | } |
118 | |
119 | static int tps6287x_map_voltage(struct regulator_dev *rdev, int min_uV, int max_uV) |
120 | { |
121 | struct tps6287x_reg_data *data = (struct tps6287x_reg_data *)rdev->reg_data; |
122 | struct linear_range selected_range; |
123 | int selector, voltage; |
124 | |
125 | if (!data || data->range == -1) |
126 | return regulator_map_voltage_pickable_linear_range(rdev, min_uV, max_uV); |
127 | |
128 | selected_range = rdev->desc->linear_ranges[data->range]; |
129 | selector = DIV_ROUND_UP(min_uV - selected_range.min, selected_range.step); |
130 | if (selector < selected_range.min_sel || selector > selected_range.max_sel) |
131 | return -EINVAL; |
132 | |
133 | selector |= tps6287x_voltage_range_prefix[data->range]; |
134 | voltage = rdev->desc->ops->list_voltage(rdev, selector); |
135 | if (voltage < min_uV || voltage > max_uV) |
136 | return -EINVAL; |
137 | |
138 | return selector; |
139 | } |
140 | |
141 | static const struct regulator_ops tps6287x_regulator_ops = { |
142 | .enable = regulator_enable_regmap, |
143 | .disable = regulator_disable_regmap, |
144 | .set_mode = tps6287x_set_mode, |
145 | .get_mode = tps6287x_get_mode, |
146 | .is_enabled = regulator_is_enabled_regmap, |
147 | .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap, |
148 | .set_voltage_sel = regulator_set_voltage_sel_pickable_regmap, |
149 | .list_voltage = regulator_list_voltage_pickable_linear_range, |
150 | .map_voltage = tps6287x_map_voltage, |
151 | .set_ramp_delay = regulator_set_ramp_delay_regmap, |
152 | }; |
153 | |
154 | static const struct regulator_desc tps6287x_reg = { |
155 | .name = "tps6287x", |
156 | .owner = THIS_MODULE, |
157 | .ops = &tps6287x_regulator_ops, |
158 | .of_map_mode = tps6287x_of_map_mode, |
159 | .type = REGULATOR_VOLTAGE, |
160 | .enable_reg = TPS6287X_CTRL1, |
161 | .enable_mask = TPS6287X_CTRL1_SWEN, |
162 | .vsel_reg = TPS6287X_VSET, |
163 | .vsel_mask = 0xFF, |
164 | .vsel_range_reg = TPS6287X_CTRL2, |
165 | .vsel_range_mask = TPS6287X_CTRL2_VRANGE, |
166 | .range_applied_by_vsel = true, |
167 | .ramp_reg = TPS6287X_CTRL1, |
168 | .ramp_mask = TPS6287X_CTRL1_VRAMP, |
169 | .ramp_delay_table = tps6287x_ramp_table, |
170 | .n_ramp_values = ARRAY_SIZE(tps6287x_ramp_table), |
171 | .n_voltages = 256 * ARRAY_SIZE(tps6287x_voltage_ranges), |
172 | .linear_ranges = tps6287x_voltage_ranges, |
173 | .n_linear_ranges = ARRAY_SIZE(tps6287x_voltage_ranges), |
174 | .linear_range_selectors_bitfield = tps6287x_voltage_range_sel, |
175 | }; |
176 | |
177 | static int tps6287x_i2c_probe(struct i2c_client *i2c) |
178 | { |
179 | struct device *dev = &i2c->dev; |
180 | struct regulator_config config = {}; |
181 | struct tps6287x_reg_data *reg_data; |
182 | struct regulator_dev *rdev; |
183 | |
184 | reg_data = devm_kzalloc(dev, size: sizeof(struct tps6287x_reg_data), GFP_KERNEL); |
185 | |
186 | if (!reg_data) |
187 | return -ENOMEM; |
188 | |
189 | config.regmap = devm_regmap_init_i2c(i2c, &tps6287x_regmap_config); |
190 | if (IS_ERR(ptr: config.regmap)) { |
191 | dev_err(dev, "Failed to init i2c\n"); |
192 | return PTR_ERR(ptr: config.regmap); |
193 | } |
194 | |
195 | config.dev = dev; |
196 | config.of_node = dev->of_node; |
197 | config.init_data = of_get_regulator_init_data(dev, node: dev->of_node, |
198 | desc: &tps6287x_reg); |
199 | |
200 | reg_data->range = tps6287x_best_range(config: &config, desc: &tps6287x_reg); |
201 | |
202 | rdev = devm_regulator_register(dev, regulator_desc: &tps6287x_reg, config: &config); |
203 | if (IS_ERR(ptr: rdev)) { |
204 | dev_err(dev, "Failed to register regulator\n"); |
205 | return PTR_ERR(ptr: rdev); |
206 | } |
207 | |
208 | rdev->reg_data = (void *)reg_data; |
209 | dev_dbg(dev, "Probed regulator\n"); |
210 | |
211 | return 0; |
212 | } |
213 | |
214 | static const struct of_device_id tps6287x_dt_ids[] = { |
215 | { .compatible = "ti,tps62870", }, |
216 | { .compatible = "ti,tps62871", }, |
217 | { .compatible = "ti,tps62872", }, |
218 | { .compatible = "ti,tps62873", }, |
219 | { } |
220 | }; |
221 | |
222 | MODULE_DEVICE_TABLE(of, tps6287x_dt_ids); |
223 | |
224 | static const struct i2c_device_id tps6287x_i2c_id[] = { |
225 | { "tps62870"}, |
226 | { "tps62871"}, |
227 | { "tps62872"}, |
228 | { "tps62873"}, |
229 | {} |
230 | }; |
231 | |
232 | MODULE_DEVICE_TABLE(i2c, tps6287x_i2c_id); |
233 | |
234 | static struct i2c_driver tps6287x_regulator_driver = { |
235 | .driver = { |
236 | .name = "tps6287x", |
237 | .of_match_table = tps6287x_dt_ids, |
238 | }, |
239 | .probe = tps6287x_i2c_probe, |
240 | .id_table = tps6287x_i2c_id, |
241 | }; |
242 | |
243 | module_i2c_driver(tps6287x_regulator_driver); |
244 | |
245 | MODULE_AUTHOR("Mårten Lindahl <marten.lindahl@axis.com>"); |
246 | MODULE_DESCRIPTION("Regulator driver for TI TPS6287X PMIC"); |
247 | MODULE_LICENSE("GPL"); |
248 |
Definitions
- tps6287x_regmap_config
- tps6287x_voltage_ranges
- tps6287x_voltage_range_sel
- tps6287x_voltage_range_prefix
- tps6287x_ramp_table
- tps6287x_reg_data
- tps6287x_best_range
- tps6287x_set_mode
- tps6287x_get_mode
- tps6287x_of_map_mode
- tps6287x_map_voltage
- tps6287x_regulator_ops
- tps6287x_reg
- tps6287x_i2c_probe
- tps6287x_dt_ids
- tps6287x_i2c_id
Improve your Profiling and Debugging skills
Find out more