1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2023 Analog Devices, Inc.
4 * ADI Regulator driver for the MAX77857
5 * MAX77859 and MAX77831.
7 #include <linux/bitfield.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/regmap.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/regulator/machine.h>
14 #include <linux/regulator/of_regulator.h>
15 #include <linux/util_macros.h>
17 #define MAX77857_REG_INT_SRC 0x10
18 #define MAX77857_REG_INT_MASK 0x11
19 #define MAX77857_REG_CONT1 0x12
20 #define MAX77857_REG_CONT2 0x13
21 #define MAX77857_REG_CONT3 0x14
23 #define MAX77857_INT_SRC_OCP BIT(0)
24 #define MAX77857_INT_SRC_THS BIT(1)
25 #define MAX77857_INT_SRC_HARDSHORT BIT(2)
26 #define MAX77857_INT_SRC_OVP BIT(3)
27 #define MAX77857_INT_SRC_POK BIT(4)
29 #define MAX77857_ILIM_MASK GENMASK(2, 0)
30 #define MAX77857_CONT1_FREQ GENMASK(4, 3)
31 #define MAX77857_CONT3_FPWM BIT(5)
33 #define MAX77859_REG_INT_SRC 0x11
34 #define MAX77859_REG_CONT1 0x13
35 #define MAX77859_REG_CONT2 0x14
36 #define MAX77859_REG_CONT3 0x15
37 #define MAX77859_REG_CONT5 0x17
38 #define MAX77859_CONT2_FPWM BIT(2)
39 #define MAX77859_CONT2_INTB BIT(3)
40 #define MAX77859_CONT3_DVS_START BIT(2)
41 #define MAX77859_VOLTAGE_SEL_MASK GENMASK(9, 0)
43 #define MAX77859_CURRENT_MIN 1000000
44 #define MAX77859_CURRENT_MAX 5000000
45 #define MAX77859_CURRENT_STEP 50000
54 static bool max77857_volatile_reg(struct device *dev, unsigned int reg)
56 enum max77857_id id = (uintptr_t)dev_get_drvdata(dev);
61 return reg == MAX77857_REG_INT_SRC;
64 return reg == MAX77859_REG_INT_SRC;
70 static struct regmap_config max77857_regmap_config = {
73 .cache_type = REGCACHE_MAPLE,
74 .volatile_reg = max77857_volatile_reg,
77 static int max77857_get_status(struct regulator_dev *rdev)
82 ret = regmap_read(rdev->regmap, MAX77857_REG_INT_SRC, &val);
86 if (FIELD_GET(MAX77857_INT_SRC_POK, val))
87 return REGULATOR_STATUS_ON;
89 return REGULATOR_STATUS_ERROR;
92 static unsigned int max77857_get_mode(struct regulator_dev *rdev)
94 enum max77857_id id = (uintptr_t)rdev_get_drvdata(rdev);
101 ret = regmap_read(rdev->regmap, MAX77857_REG_CONT3, ®val);
105 if (FIELD_GET(MAX77857_CONT3_FPWM, regval))
106 return REGULATOR_MODE_FAST;
111 ret = regmap_read(rdev->regmap, MAX77859_REG_CONT2, ®val);
115 if (FIELD_GET(MAX77859_CONT2_FPWM, regval))
116 return REGULATOR_MODE_FAST;
123 return REGULATOR_MODE_NORMAL;
126 static int max77857_set_mode(struct regulator_dev *rdev, unsigned int mode)
128 enum max77857_id id = (uintptr_t)rdev_get_drvdata(rdev);
129 unsigned int reg, val;
134 reg = MAX77857_REG_CONT3;
135 val = MAX77857_CONT3_FPWM;
139 reg = MAX77859_REG_CONT2;
140 val = MAX77859_CONT2_FPWM;
147 case REGULATOR_MODE_FAST:
148 return regmap_set_bits(rdev->regmap, reg, val);
149 case REGULATOR_MODE_NORMAL:
150 return regmap_clear_bits(rdev->regmap, reg, val);
156 static int max77857_get_error_flags(struct regulator_dev *rdev,
162 ret = regmap_read(rdev->regmap, MAX77857_REG_INT_SRC, &val);
168 if (FIELD_GET(MAX77857_INT_SRC_OVP, val))
169 *flags |= REGULATOR_ERROR_OVER_VOLTAGE_WARN;
171 if (FIELD_GET(MAX77857_INT_SRC_OCP, val) ||
172 FIELD_GET(MAX77857_INT_SRC_HARDSHORT, val))
173 *flags |= REGULATOR_ERROR_OVER_CURRENT;
175 if (FIELD_GET(MAX77857_INT_SRC_THS, val))
176 *flags |= REGULATOR_ERROR_OVER_TEMP;
178 if (!FIELD_GET(MAX77857_INT_SRC_POK, val))
179 *flags |= REGULATOR_ERROR_FAIL;
184 static struct linear_range max77859_lin_ranges[] = {
185 REGULATOR_LINEAR_RANGE(3200000, 0x0A0, 0x320, 20000)
188 static const unsigned int max77859_ramp_table[4] = {
192 static int max77859_set_voltage_sel(struct regulator_dev *rdev,
198 reg = cpu_to_be16(sel);
200 ret = regmap_bulk_write(rdev->regmap, MAX77859_REG_CONT3, ®, 2);
204 /* actually apply new voltage */
205 return regmap_set_bits(rdev->regmap, MAX77859_REG_CONT3,
206 MAX77859_CONT3_DVS_START);
209 static int max77859_get_voltage_sel(struct regulator_dev *rdev)
214 ret = regmap_bulk_read(rdev->regmap, MAX77859_REG_CONT3, ®, 2);
218 return FIELD_GET(MAX77859_VOLTAGE_SEL_MASK, __be16_to_cpu(reg));
221 static int max77859_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA)
225 if (max_uA < MAX77859_CURRENT_MIN)
228 selector = 0x12 + (max_uA - MAX77859_CURRENT_MIN) / MAX77859_CURRENT_STEP;
230 selector = clamp_val(selector, 0x00, 0x7F);
232 return regmap_write(rdev->regmap, MAX77859_REG_CONT5, selector);
235 static int max77859_get_current_limit(struct regulator_dev *rdev)
240 ret = regmap_read(rdev->regmap, MAX77859_REG_CONT5, &selector);
244 if (selector <= 0x12)
245 return MAX77859_CURRENT_MIN;
247 if (selector >= 0x64)
248 return MAX77859_CURRENT_MAX;
250 return MAX77859_CURRENT_MIN + (selector - 0x12) * MAX77859_CURRENT_STEP;
253 static const struct regulator_ops max77859_regulator_ops = {
254 .list_voltage = regulator_list_voltage_linear_range,
255 .set_voltage_sel = max77859_set_voltage_sel,
256 .get_voltage_sel = max77859_get_voltage_sel,
257 .set_ramp_delay = regulator_set_ramp_delay_regmap,
258 .get_status = max77857_get_status,
259 .set_mode = max77857_set_mode,
260 .get_mode = max77857_get_mode,
261 .get_error_flags = max77857_get_error_flags,
264 static const struct regulator_ops max77859a_regulator_ops = {
265 .list_voltage = regulator_list_voltage_linear_range,
266 .set_voltage_sel = max77859_set_voltage_sel,
267 .get_voltage_sel = max77859_get_voltage_sel,
268 .set_current_limit = max77859_set_current_limit,
269 .get_current_limit = max77859_get_current_limit,
270 .set_ramp_delay = regulator_set_ramp_delay_regmap,
271 .get_status = max77857_get_status,
272 .set_mode = max77857_set_mode,
273 .get_mode = max77857_get_mode,
274 .get_error_flags = max77857_get_error_flags,
277 static const struct regulator_ops max77857_regulator_ops = {
278 .list_voltage = regulator_list_voltage_linear_range,
279 .set_voltage_sel = regulator_set_voltage_sel_regmap,
280 .get_voltage_sel = regulator_get_voltage_sel_regmap,
281 .set_ramp_delay = regulator_set_ramp_delay_regmap,
282 .get_status = max77857_get_status,
283 .set_mode = max77857_set_mode,
284 .get_mode = max77857_get_mode,
285 .get_error_flags = max77857_get_error_flags,
288 static struct linear_range max77857_lin_ranges[] = {
289 REGULATOR_LINEAR_RANGE(4485000, 0x3D, 0xCC, 73500)
292 static const unsigned int max77857_switch_freq[] = {
293 1200000, 1500000, 1800000, 2100000
296 #define RAMAP_DELAY_INIT_VAL 1333
298 static const unsigned int max77857_ramp_table[2][4] = {
299 { RAMAP_DELAY_INIT_VAL, 667, 333, 227 }, /* when switch freq is 1.8MHz or 2.1MHz */
300 { 1166, 667, 333, 167 }, /* when switch freq is 1.2MHz or 1.5MHz */
303 static struct regulator_desc max77857_regulator_desc = {
304 .ops = &max77857_regulator_ops,
306 .linear_ranges = max77857_lin_ranges,
307 .n_linear_ranges = ARRAY_SIZE(max77857_lin_ranges),
309 .vsel_reg = MAX77857_REG_CONT2,
310 .ramp_delay_table = max77857_ramp_table[0],
311 .n_ramp_values = ARRAY_SIZE(max77857_ramp_table[0]),
312 .ramp_reg = MAX77857_REG_CONT3,
313 .ramp_mask = GENMASK(1, 0),
314 .ramp_delay = RAMAP_DELAY_INIT_VAL,
315 .owner = THIS_MODULE,
318 static void max77857_calc_range(struct device *dev, enum max77857_id id)
320 struct linear_range *range;
321 unsigned long vref_step;
325 device_property_read_u32(dev, "adi,rtop-ohms", &rtop);
326 device_property_read_u32(dev, "adi,rbot-ohms", &rbot);
334 range = max77857_lin_ranges;
339 range = max77859_lin_ranges;
344 range->step = DIV_ROUND_CLOSEST(vref_step * (rbot + rtop), rbot);
345 range->min = range->step * range->min_sel;
348 static int max77857_probe(struct i2c_client *client)
350 const struct i2c_device_id *i2c_id;
351 struct device *dev = &client->dev;
352 struct regulator_config cfg = { };
353 struct regulator_dev *rdev;
354 struct regmap *regmap;
359 i2c_id = i2c_client_get_device_id(client);
363 id = i2c_id->driver_data;
365 dev_set_drvdata(dev, (void *)id);
367 if (id == ID_MAX77859 || id == ID_MAX77859A) {
368 max77857_regulator_desc.ops = &max77859_regulator_ops;
369 max77857_regulator_desc.linear_ranges = max77859_lin_ranges;
370 max77857_regulator_desc.ramp_delay_table = max77859_ramp_table;
371 max77857_regulator_desc.ramp_delay = max77859_ramp_table[0];
374 if (id == ID_MAX77859A)
375 max77857_regulator_desc.ops = &max77859a_regulator_ops;
377 max77857_calc_range(dev, id);
379 regmap = devm_regmap_init_i2c(client, &max77857_regmap_config);
381 return dev_err_probe(dev, PTR_ERR(regmap),
382 "cannot initialize regmap\n");
384 device_property_read_u32(dev, "adi,switch-frequency-hz", &switch_freq);
386 switch_freq = find_closest(switch_freq, max77857_switch_freq,
387 ARRAY_SIZE(max77857_switch_freq));
389 if (id == ID_MAX77831 && switch_freq == 3)
395 ret = regmap_update_bits(regmap, MAX77857_REG_CONT1,
396 MAX77857_CONT1_FREQ, switch_freq);
398 if (switch_freq >= 2)
401 max77857_regulator_desc.ramp_delay_table = max77857_ramp_table[1];
402 max77857_regulator_desc.ramp_delay = max77857_ramp_table[1][0];
406 ret = regmap_update_bits(regmap, MAX77859_REG_CONT1,
407 MAX77857_CONT1_FREQ, switch_freq);
415 cfg.driver_data = (void *)id;
417 cfg.init_data = of_get_regulator_init_data(dev, dev->of_node,
418 &max77857_regulator_desc);
422 rdev = devm_regulator_register(dev, &max77857_regulator_desc, &cfg);
424 return dev_err_probe(dev, PTR_ERR(rdev),
425 "cannot register regulator\n");
430 const struct i2c_device_id max77857_id[] = {
431 { "max77831", ID_MAX77831 },
432 { "max77857", ID_MAX77857 },
433 { "max77859", ID_MAX77859 },
434 { "max77859a", ID_MAX77859A },
437 MODULE_DEVICE_TABLE(i2c, max77857_id);
439 static const struct of_device_id max77857_of_id[] = {
440 { .compatible = "adi,max77831", .data = (void *)ID_MAX77831 },
441 { .compatible = "adi,max77857", .data = (void *)ID_MAX77857 },
442 { .compatible = "adi,max77859", .data = (void *)ID_MAX77859 },
443 { .compatible = "adi,max77859a", .data = (void *)ID_MAX77859A },
446 MODULE_DEVICE_TABLE(of, max77857_of_id);
448 static struct i2c_driver max77857_driver = {
451 .of_match_table = max77857_of_id,
453 .id_table = max77857_id,
454 .probe = max77857_probe,
456 module_i2c_driver(max77857_driver);
458 MODULE_DESCRIPTION("Analog Devices MAX77857 Buck-Boost Converter Driver");
459 MODULE_AUTHOR("Ibrahim Tilki <Ibrahim.Tilki@analog.com>");
460 MODULE_AUTHOR("Okan Sahin <Okan.Sahin@analog.com>");
461 MODULE_LICENSE("GPL");