1 /*
2 * Copyright (c) 2023 Grinn
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT maxim_max20335_regulator
7
8 #include <zephyr/drivers/i2c.h>
9 #include <zephyr/drivers/regulator.h>
10 #include <zephyr/dt-bindings/regulator/max20335.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/sys/linear_range.h>
13
14 #define MAX20335_BUCK1_CFG 0x0DU
15 #define MAX20335_BUCK1_VSET 0x0EU
16 #define MAX20335_BUCK2_CFG 0x0FU
17 #define MAX20335_BUCK2_VSET 0x10U
18 #define MAX20335_BUCK12_CSET 0x11U
19 #define MAX20335_PWR_CMD 0x1FU
20 #define MAX20335_BUCK1_CSET_MASK 0xF0U
21 #define MAX20335_BUCK2_CSET_MASK 0x0FU
22 #define MAX20335_BUCK2_CSET_SHIFT 4
23 #define MAX20335_BUCK_EN BIT(3)
24 #define MAX20335_BUCK_EN_MASK GENMASK(4, 3)
25
26 #define MAX20335_LDO1_CFG 0x12U
27 #define MAX20335_LDO1_VSET 0x13U
28 #define MAX20335_LDO2_CFG 0x14U
29 #define MAX20335_LDO2_VSET 0x15U
30 #define MAX20335_LDO3_CFG 0x16U
31 #define MAX20335_LDO3_VSET 0x17U
32 #define MAX20335_LDO_MODE_MASK BIT(0)
33 #define MAX20335_LDO_EN BIT(1)
34 #define MAX20335_LDO_EN_MASK GENMASK(2, 1)
35
36 #define MAX20335_OFF_MODE 0xB2U
37
38 enum max20335_pmic_sources {
39 MAX20335_PMIC_SOURCE_BUCK1,
40 MAX20335_PMIC_SOURCE_BUCK2,
41 MAX20335_PMIC_SOURCE_LDO1,
42 MAX20335_PMIC_SOURCE_LDO2,
43 MAX20335_PMIC_SOURCE_LDO3,
44 };
45
46 struct regulator_max20335_desc {
47 uint8_t vsel_reg;
48 uint8_t enable_mask;
49 uint8_t enable_val;
50 uint8_t cfg_reg;
51 const struct linear_range *uv_range;
52 const struct linear_range *ua_range;
53 };
54
55 struct regulator_max20335_common_config {
56 struct i2c_dt_spec bus;
57 };
58
59 struct regulator_max20335_config {
60 struct regulator_common_config common;
61 struct i2c_dt_spec bus;
62 const struct regulator_max20335_desc *desc;
63 uint8_t source;
64 };
65
66 struct regulator_max20335_data {
67 struct regulator_common_data common;
68 };
69
70 static const struct linear_range buck1_range = LINEAR_RANGE_INIT(700000, 25000U, 0x0U, 0x3FU);
71 static const struct linear_range buck2_range = LINEAR_RANGE_INIT(700000, 50000U, 0x0U, 0x3FU);
72 static const struct linear_range buck12_current_limit_range =
73 LINEAR_RANGE_INIT(50000, 25000U, 0x02U, 0x0FU);
74 static const struct linear_range ldo1_range = LINEAR_RANGE_INIT(800000, 100000U, 0x0U, 0x1CU);
75 static const struct linear_range ldo23_range = LINEAR_RANGE_INIT(900000, 100000U, 0x0U, 0x1FU);
76
77 static const struct regulator_max20335_desc __maybe_unused buck1_desc = {
78 .vsel_reg = MAX20335_BUCK1_VSET,
79 .enable_mask = MAX20335_BUCK_EN_MASK,
80 .enable_val = MAX20335_BUCK_EN,
81 .cfg_reg = MAX20335_BUCK1_CFG,
82 .uv_range = &buck1_range,
83 .ua_range = &buck12_current_limit_range,
84 };
85
86 static const struct regulator_max20335_desc __maybe_unused buck2_desc = {
87 .vsel_reg = MAX20335_BUCK2_VSET,
88 .enable_mask = MAX20335_BUCK_EN_MASK,
89 .enable_val = MAX20335_BUCK_EN,
90 .cfg_reg = MAX20335_BUCK2_CFG,
91 .uv_range = &buck2_range,
92 .ua_range = &buck12_current_limit_range,
93 };
94
95 static const struct regulator_max20335_desc __maybe_unused ldo1_desc = {
96 .vsel_reg = MAX20335_LDO1_VSET,
97 .enable_mask = MAX20335_LDO_EN_MASK,
98 .enable_val = MAX20335_LDO_EN,
99 .cfg_reg = MAX20335_LDO1_CFG,
100 .uv_range = &ldo1_range,
101 };
102
103 static const struct regulator_max20335_desc __maybe_unused ldo2_desc = {
104 .vsel_reg = MAX20335_LDO2_VSET,
105 .enable_mask = MAX20335_LDO_EN_MASK,
106 .enable_val = MAX20335_LDO_EN,
107 .cfg_reg = MAX20335_LDO2_CFG,
108 .uv_range = &ldo23_range,
109 };
110
111 static const struct regulator_max20335_desc __maybe_unused ldo3_desc = {
112 .vsel_reg = MAX20335_LDO3_VSET,
113 .enable_mask = MAX20335_LDO_EN_MASK,
114 .enable_val = MAX20335_LDO_EN,
115 .cfg_reg = MAX20335_LDO3_CFG,
116 .uv_range = &ldo23_range,
117 };
118
regulator_max20335_set_enable(const struct device * dev,bool enable)119 static int regulator_max20335_set_enable(const struct device *dev, bool enable)
120 {
121 const struct regulator_max20335_config *config = dev->config;
122
123 return i2c_reg_update_byte_dt(&config->bus,
124 config->desc->cfg_reg,
125 config->desc->enable_mask,
126 enable ? config->desc->enable_val : 0);
127 }
128
regulator_max20335_enable(const struct device * dev)129 static int regulator_max20335_enable(const struct device *dev)
130 {
131 return regulator_max20335_set_enable(dev, true);
132 }
133
regulator_max20335_disable(const struct device * dev)134 static int regulator_max20335_disable(const struct device *dev)
135 {
136 return regulator_max20335_set_enable(dev, false);
137 }
138
regulator_max20335_set_mode(const struct device * dev,regulator_mode_t mode)139 static int regulator_max20335_set_mode(const struct device *dev, regulator_mode_t mode)
140 {
141 const struct regulator_max20335_config *config = dev->config;
142
143 if (mode > MAX20335_LOAD_SWITCH_MODE) {
144 return -ENOTSUP;
145 }
146
147 switch (config->source) {
148 case MAX20335_PMIC_SOURCE_LDO1:
149 __fallthrough;
150 case MAX20335_PMIC_SOURCE_LDO2:
151 __fallthrough;
152 case MAX20335_PMIC_SOURCE_LDO3:
153 return i2c_reg_update_byte_dt(&config->bus,
154 config->desc->cfg_reg,
155 MAX20335_LDO_MODE_MASK,
156 mode);
157 default:
158 return -ENOTSUP;
159 }
160 }
161
regulator_max20335_count_voltages(const struct device * dev)162 static unsigned int regulator_max20335_count_voltages(const struct device *dev)
163 {
164 const struct regulator_max20335_config *config = dev->config;
165
166 return linear_range_values_count(config->desc->uv_range);
167 }
168
regulator_max20335_list_voltage(const struct device * dev,unsigned int idx,int32_t * volt_uv)169 static int regulator_max20335_list_voltage(const struct device *dev, unsigned int idx,
170 int32_t *volt_uv)
171 {
172 const struct regulator_max20335_config *config = dev->config;
173
174 return linear_range_get_value(config->desc->uv_range, idx, volt_uv);
175 }
176
regulator_max20335_set_buck_ldo_voltage(const struct device * dev,int32_t min_uv,int32_t max_uv,const struct linear_range * range,uint8_t vout_reg)177 static int regulator_max20335_set_buck_ldo_voltage(const struct device *dev, int32_t min_uv,
178 int32_t max_uv, const struct linear_range *range,
179 uint8_t vout_reg)
180 {
181 const struct regulator_max20335_config *config = dev->config;
182 uint16_t idx;
183 int ret;
184
185 ret = linear_range_get_win_index(range, min_uv, max_uv, &idx);
186 if (ret == -EINVAL) {
187 return ret;
188 }
189
190 return i2c_reg_write_byte_dt(&config->bus, vout_reg, (uint8_t)idx);
191 }
192
regulator_max20335_buck12_ldo123_get_voltage(const struct device * dev,const struct linear_range * range,uint8_t vout_reg,int32_t * volt_uv)193 static int regulator_max20335_buck12_ldo123_get_voltage(const struct device *dev,
194 const struct linear_range *range,
195 uint8_t vout_reg, int32_t *volt_uv)
196 {
197 const struct regulator_max20335_config *config = dev->config;
198 uint8_t idx;
199 int ret;
200
201 ret = i2c_reg_read_byte_dt(&config->bus, vout_reg, &idx);
202 if (ret < 0) {
203 return ret;
204 }
205
206 return linear_range_get_value(range, idx, volt_uv);
207 }
208
regulator_max20335_get_voltage(const struct device * dev,int32_t * volt_uv)209 static int regulator_max20335_get_voltage(const struct device *dev, int32_t *volt_uv)
210 {
211 const struct regulator_max20335_config *config = dev->config;
212
213 return regulator_max20335_buck12_ldo123_get_voltage(dev,
214 config->desc->uv_range,
215 config->desc->vsel_reg,
216 volt_uv);
217 }
218
regulator_max20335_set_voltage(const struct device * dev,int32_t min_uv,int32_t max_uv)219 static int regulator_max20335_set_voltage(const struct device *dev, int32_t min_uv, int32_t max_uv)
220 {
221 const struct regulator_max20335_config *config = dev->config;
222
223 return regulator_max20335_set_buck_ldo_voltage(dev,
224 min_uv,
225 max_uv,
226 config->desc->uv_range,
227 config->desc->vsel_reg);
228 }
229
regulator_max20335_count_current_limits(const struct device * dev)230 static unsigned int regulator_max20335_count_current_limits(const struct device *dev)
231 {
232 const struct regulator_max20335_config *config = dev->config;
233
234 if (config->source != MAX20335_PMIC_SOURCE_BUCK1 &&
235 config->source != MAX20335_PMIC_SOURCE_BUCK2) {
236 return -ENOTSUP;
237 }
238
239 return linear_range_values_count(config->desc->ua_range);
240 }
241
regulator_max20335_list_current_limit(const struct device * dev,unsigned int idx,int32_t * current_ua)242 static int regulator_max20335_list_current_limit(const struct device *dev, unsigned int idx,
243 int32_t *current_ua)
244 {
245 const struct regulator_max20335_config *config = dev->config;
246
247 if (config->source != MAX20335_PMIC_SOURCE_BUCK1 &&
248 config->source != MAX20335_PMIC_SOURCE_BUCK2) {
249 return -ENOTSUP;
250 }
251
252 return linear_range_get_value(config->desc->ua_range, idx, current_ua);
253 }
254
regulator_max20335_set_current_limit(const struct device * dev,int32_t min_ua,int32_t max_ua)255 static int regulator_max20335_set_current_limit(const struct device *dev,
256 int32_t min_ua,
257 int32_t max_ua)
258 {
259 const struct regulator_max20335_config *config = dev->config;
260 uint8_t val;
261 uint16_t idx;
262 int ret;
263
264 if (config->source != MAX20335_PMIC_SOURCE_BUCK1 &&
265 config->source != MAX20335_PMIC_SOURCE_BUCK2) {
266 return -ENOTSUP;
267 }
268
269 ret = i2c_reg_read_byte_dt(&config->bus, MAX20335_BUCK12_CSET, &val);
270 if (ret < 0) {
271 return ret;
272 }
273
274 ret = linear_range_get_win_index(config->desc->ua_range, min_ua, max_ua, &idx);
275 if (ret == -EINVAL) {
276 return ret;
277 }
278
279 switch (config->source) {
280 case MAX20335_PMIC_SOURCE_BUCK1:
281 val = idx | (val & MAX20335_BUCK1_CSET_MASK);
282 break;
283 case MAX20335_PMIC_SOURCE_BUCK2:
284 val = (idx << MAX20335_BUCK2_CSET_SHIFT) | (val & MAX20335_BUCK2_CSET_MASK);
285 break;
286 default:
287 return -ENOTSUP;
288 }
289
290 return i2c_reg_write_byte_dt(&config->bus, MAX20335_BUCK12_CSET, val);
291 }
292
regulator_max20335_power_off(const struct device * dev)293 static int regulator_max20335_power_off(const struct device *dev)
294 {
295 const struct regulator_max20335_common_config *common_config = dev->config;
296
297 return i2c_reg_write_byte_dt(&common_config->bus, MAX20335_PWR_CMD, MAX20335_OFF_MODE);
298 }
299
regulator_max20335_init(const struct device * dev)300 static int regulator_max20335_init(const struct device *dev)
301 {
302 const struct regulator_max20335_config *config = dev->config;
303
304 if (!i2c_is_ready_dt(&config->bus)) {
305 return -ENODEV;
306 }
307
308 regulator_common_data_init(dev);
309
310 return regulator_common_init(dev, false);
311 }
312
regulator_max20335_common_init(const struct device * dev)313 static int regulator_max20335_common_init(const struct device *dev)
314 {
315 const struct regulator_max20335_common_config *common_config = dev->config;
316
317 if (!i2c_is_ready_dt(&common_config->bus)) {
318 return -ENODEV;
319 }
320
321 return 0;
322 }
323
324 static const struct regulator_parent_driver_api parent_api = {
325 .ship_mode = regulator_max20335_power_off,
326 };
327
328 static const struct regulator_driver_api api = {
329 .enable = regulator_max20335_enable,
330 .disable = regulator_max20335_disable,
331 .set_mode = regulator_max20335_set_mode,
332 .count_voltages = regulator_max20335_count_voltages,
333 .list_voltage = regulator_max20335_list_voltage,
334 .set_voltage = regulator_max20335_set_voltage,
335 .get_voltage = regulator_max20335_get_voltage,
336 .count_current_limits = regulator_max20335_count_current_limits,
337 .list_current_limit = regulator_max20335_list_current_limit,
338 .set_current_limit = regulator_max20335_set_current_limit,
339 };
340
341 #define REGULATOR_MAX20335_DEFINE(node_id, id, child_name, _source) \
342 static const struct regulator_max20335_config regulator_max20335_config_##id = { \
343 .common = REGULATOR_DT_COMMON_CONFIG_INIT(node_id), \
344 .bus = I2C_DT_SPEC_GET(DT_GPARENT(node_id)), \
345 .desc = &child_name##_desc, \
346 .source = _source, \
347 }; \
348 \
349 static struct regulator_max20335_data regulator_max20335_data_##id; \
350 DEVICE_DT_DEFINE(node_id, regulator_max20335_init, NULL, \
351 ®ulator_max20335_data_##id, \
352 ®ulator_max20335_config_##id, \
353 POST_KERNEL, \
354 CONFIG_REGULATOR_MAXIM_MAX20335_INIT_PRIORITY, \
355 &api);
356
357 #define REGULATOR_MAX20335_DEFINE_COND(inst, child, source) \
358 COND_CODE_1(DT_NODE_EXISTS(DT_INST_CHILD(inst, child)), \
359 (REGULATOR_MAX20335_DEFINE(DT_INST_CHILD(inst, child), \
360 child##inst, child, source)), \
361 ())
362
363 #define REGULATOR_MAX20335_DEFINE_ALL(inst) \
364 static const struct regulator_max20335_common_config common_config_##inst = { \
365 .bus = I2C_DT_SPEC_GET(DT_INST_PARENT(inst)), \
366 }; \
367 \
368 DEVICE_DT_INST_DEFINE(inst, regulator_max20335_common_init, \
369 NULL, NULL, &common_config_##inst, POST_KERNEL, \
370 CONFIG_REGULATOR_MAXIM_MAX20335_COMMON_INIT_PRIORITY, \
371 &parent_api); \
372 \
373 REGULATOR_MAX20335_DEFINE_COND(inst, buck1, MAX20335_PMIC_SOURCE_BUCK1) \
374 REGULATOR_MAX20335_DEFINE_COND(inst, buck2, MAX20335_PMIC_SOURCE_BUCK2) \
375 REGULATOR_MAX20335_DEFINE_COND(inst, ldo1, MAX20335_PMIC_SOURCE_LDO1) \
376 REGULATOR_MAX20335_DEFINE_COND(inst, ldo2, MAX20335_PMIC_SOURCE_LDO2) \
377 REGULATOR_MAX20335_DEFINE_COND(inst, ldo3, MAX20335_PMIC_SOURCE_LDO3)
378
379 DT_INST_FOREACH_STATUS_OKAY(REGULATOR_MAX20335_DEFINE_ALL)
380