1 // SPDX-License-Identifier: GPL-2.0
3 * Device driver for regulators in MAX5970 and MAX5978 IC
5 * Copyright (c) 2022 9elements GmbH
7 * Author: Patrick Rudolph <patrick.rudolph@9elements.com>
10 #include <linux/bitops.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/of_regulator.h>
21 #include <linux/platform_device.h>
23 #include <linux/mfd/max597x.h>
25 struct max597x_regulator {
26 int num_switches, mon_rng, irng, shunt_micro_ohms, lim_uA;
27 struct regmap *regmap;
30 enum max597x_regulator_id {
35 static int max597x_uvp_ovp_check_mode(struct regulator_dev *rdev, int severity)
39 /* Status1 register contains the soft strap values sampled at POR */
40 ret = regmap_read(rdev->regmap, MAX5970_REG_STATUS1, ®);
44 /* Check soft straps match requested mode */
45 if (severity == REGULATOR_SEVERITY_PROT) {
46 if (STATUS1_PROT(reg) != STATUS1_PROT_SHUTDOWN)
51 if (STATUS1_PROT(reg) == STATUS1_PROT_SHUTDOWN)
57 static int max597x_set_vp(struct regulator_dev *rdev, int lim_uV, int severity,
58 bool enable, bool overvoltage)
60 int off_h, off_l, reg, ret;
61 struct max597x_regulator *data = rdev_get_drvdata(rdev);
62 int channel = rdev_get_id(rdev);
65 if (severity == REGULATOR_SEVERITY_WARN) {
66 off_h = MAX5970_REG_CH_OV_WARN_H(channel);
67 off_l = MAX5970_REG_CH_OV_WARN_L(channel);
69 off_h = MAX5970_REG_CH_OV_CRIT_H(channel);
70 off_l = MAX5970_REG_CH_OV_CRIT_L(channel);
73 if (severity == REGULATOR_SEVERITY_WARN) {
74 off_h = MAX5970_REG_CH_UV_WARN_H(channel);
75 off_l = MAX5970_REG_CH_UV_WARN_L(channel);
77 off_h = MAX5970_REG_CH_UV_CRIT_H(channel);
78 off_l = MAX5970_REG_CH_UV_CRIT_L(channel);
83 /* reg = ADC_MASK * (lim_uV / 1000000) / (data->mon_rng / 1000000) */
84 reg = ADC_MASK * lim_uV / data->mon_rng;
88 ret = regmap_write(rdev->regmap, off_h, MAX5970_VAL2REG_H(reg));
92 ret = regmap_write(rdev->regmap, off_l, MAX5970_VAL2REG_L(reg));
99 static int max597x_set_uvp(struct regulator_dev *rdev, int lim_uV, int severity,
105 * MAX5970 has enable control as a special value in limit reg. Can't
106 * set limit but keep feature disabled or enable W/O given limit.
108 if ((lim_uV && !enable) || (!lim_uV && enable))
111 ret = max597x_uvp_ovp_check_mode(rdev, severity);
115 return max597x_set_vp(rdev, lim_uV, severity, enable, false);
118 static int max597x_set_ovp(struct regulator_dev *rdev, int lim_uV, int severity,
124 * MAX5970 has enable control as a special value in limit reg. Can't
125 * set limit but keep feature disabled or enable W/O given limit.
127 if ((lim_uV && !enable) || (!lim_uV && enable))
130 ret = max597x_uvp_ovp_check_mode(rdev, severity);
134 return max597x_set_vp(rdev, lim_uV, severity, enable, true);
137 static int max597x_set_ocp(struct regulator_dev *rdev, int lim_uA,
138 int severity, bool enable)
141 unsigned int vthst, vthfst;
143 struct max597x_regulator *data = rdev_get_drvdata(rdev);
144 int rdev_id = rdev_get_id(rdev);
146 * MAX5970 doesn't has enable control for ocp.
147 * If limit is specified but enable is not set then hold the value in
148 * variable & later use it when ocp needs to be enabled.
150 if (lim_uA != 0 && lim_uA != data->lim_uA)
151 data->lim_uA = lim_uA;
153 if (severity != REGULATOR_SEVERITY_PROT)
158 /* Calc Vtrip threshold in uV. */
160 div_u64(mul_u32_u32(data->shunt_micro_ohms, data->lim_uA),
164 * As recommended in datasheed, add 20% margin to avoid
165 * spurious event & passive component tolerance.
167 vthst = div_u64(mul_u32_u32(vthst, 120), 100);
169 /* Calc fast Vtrip threshold in uV */
170 vthfst = vthst * (MAX5970_FAST2SLOW_RATIO / 100);
172 if (vthfst > data->irng) {
173 dev_err(&rdev->dev, "Current limit out of range\n");
176 /* Fast trip threshold to be programmed */
177 val = div_u64(mul_u32_u32(0xFF, vthfst), data->irng);
180 * Since there is no option to disable ocp, set limit to max
185 reg = MAX5970_REG_DAC_FAST(rdev_id);
187 return regmap_write(rdev->regmap, reg, val);
190 static int max597x_get_status(struct regulator_dev *rdev)
194 ret = regmap_read(rdev->regmap, MAX5970_REG_STATUS3, &val);
196 return REGULATOR_FAILED_RETRY;
198 if (val & MAX5970_STATUS3_ALERT)
199 return REGULATOR_STATUS_ERROR;
201 ret = regulator_is_enabled_regmap(rdev);
206 return REGULATOR_STATUS_ON;
208 return REGULATOR_STATUS_OFF;
211 static const struct regulator_ops max597x_switch_ops = {
212 .enable = regulator_enable_regmap,
213 .disable = regulator_disable_regmap,
214 .is_enabled = regulator_is_enabled_regmap,
215 .get_status = max597x_get_status,
216 .set_over_voltage_protection = max597x_set_ovp,
217 .set_under_voltage_protection = max597x_set_uvp,
218 .set_over_current_protection = max597x_set_ocp,
221 static int max597x_dt_parse(struct device_node *np,
222 const struct regulator_desc *desc,
223 struct regulator_config *cfg)
225 struct max597x_regulator *data = cfg->driver_data;
229 of_property_read_u32(np, "shunt-resistor-micro-ohms",
230 &data->shunt_micro_ohms);
233 "property 'shunt-resistor-micro-ohms' not found, err %d\n",
239 #define MAX597X_SWITCH(_ID, _ereg, _chan, _supply) { \
241 .of_match = of_match_ptr(#_ID), \
242 .ops = &max597x_switch_ops, \
243 .regulators_node = of_match_ptr("regulators"), \
244 .type = REGULATOR_VOLTAGE, \
245 .id = MAX597X_##_ID, \
246 .owner = THIS_MODULE, \
247 .supply_name = _supply, \
248 .enable_reg = _ereg, \
249 .enable_mask = CHXEN((_chan)), \
250 .of_parse_cb = max597x_dt_parse, \
253 static const struct regulator_desc regulators[] = {
254 MAX597X_SWITCH(SW0, MAX5970_REG_CHXEN, 0, "vss1"),
255 MAX597X_SWITCH(SW1, MAX5970_REG_CHXEN, 1, "vss2"),
258 static int max597x_regmap_read_clear(struct regmap *map, unsigned int reg,
263 ret = regmap_read(map, reg, val);
268 return regmap_write(map, reg, *val);
273 static int max597x_irq_handler(int irq, struct regulator_irq_data *rid,
274 unsigned long *dev_mask)
276 struct regulator_err_state *stat;
277 struct max597x_regulator *d = (struct max597x_regulator *)rid->data;
280 ret = max597x_regmap_read_clear(d->regmap, MAX5970_REG_FAULT0, &val);
282 return REGULATOR_FAILED_RETRY;
285 for (i = 0; i < d->num_switches; i++) {
286 stat = &rid->states[i];
291 for (i = 0; i < d->num_switches; i++) {
292 stat = &rid->states[i];
294 if (val & UV_STATUS_CRIT(i)) {
296 stat->notifs |= REGULATOR_EVENT_UNDER_VOLTAGE;
297 stat->errors |= REGULATOR_ERROR_UNDER_VOLTAGE;
298 } else if (val & UV_STATUS_WARN(i)) {
300 stat->notifs |= REGULATOR_EVENT_UNDER_VOLTAGE_WARN;
301 stat->errors |= REGULATOR_ERROR_UNDER_VOLTAGE_WARN;
305 ret = max597x_regmap_read_clear(d->regmap, MAX5970_REG_FAULT1, &val);
307 return REGULATOR_FAILED_RETRY;
309 for (i = 0; i < d->num_switches; i++) {
310 stat = &rid->states[i];
312 if (val & OV_STATUS_CRIT(i)) {
314 stat->notifs |= REGULATOR_EVENT_REGULATION_OUT;
315 stat->errors |= REGULATOR_ERROR_REGULATION_OUT;
316 } else if (val & OV_STATUS_WARN(i)) {
318 stat->notifs |= REGULATOR_EVENT_OVER_VOLTAGE_WARN;
319 stat->errors |= REGULATOR_ERROR_OVER_VOLTAGE_WARN;
323 ret = max597x_regmap_read_clear(d->regmap, MAX5970_REG_FAULT2, &val);
325 return REGULATOR_FAILED_RETRY;
327 for (i = 0; i < d->num_switches; i++) {
328 stat = &rid->states[i];
330 if (val & OC_STATUS_WARN(i)) {
332 stat->notifs |= REGULATOR_EVENT_OVER_CURRENT_WARN;
333 stat->errors |= REGULATOR_ERROR_OVER_CURRENT_WARN;
337 ret = regmap_read(d->regmap, MAX5970_REG_STATUS0, &val);
339 return REGULATOR_FAILED_RETRY;
341 for (i = 0; i < d->num_switches; i++) {
342 stat = &rid->states[i];
344 if ((val & MAX5970_CB_IFAULTF(i))
345 || (val & MAX5970_CB_IFAULTS(i))) {
348 REGULATOR_EVENT_OVER_CURRENT |
349 REGULATOR_EVENT_DISABLE;
351 REGULATOR_ERROR_OVER_CURRENT | REGULATOR_ERROR_FAIL;
353 /* Clear the sub-IRQ status */
354 regulator_disable_regmap(stat->rdev);
360 static const struct regmap_config max597x_regmap_config = {
363 .max_register = MAX_REGISTERS,
366 static int max597x_adc_range(struct regmap *regmap, const int ch,
367 u32 *irng, u32 *mon_rng)
372 /* Decode current ADC range */
373 ret = regmap_read(regmap, MAX5970_REG_STATUS2, ®);
376 switch (MAX5970_IRNG(reg, ch)) {
378 *irng = 100000; /* 100 mV */
381 *irng = 50000; /* 50 mV */
384 *irng = 25000; /* 25 mV */
390 /* Decode current voltage monitor range */
391 ret = regmap_read(regmap, MAX5970_REG_MON_RANGE, ®);
395 *mon_rng = MAX5970_MON_MAX_RANGE_UV >> MAX5970_MON(reg, ch);
400 static int max597x_setup_irq(struct device *dev,
402 struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES],
403 int num_switches, struct max597x_regulator *data)
405 struct regulator_irq_desc max597x_notif = {
406 .name = "max597x-irq",
407 .map_event = max597x_irq_handler,
410 int errs = REGULATOR_ERROR_UNDER_VOLTAGE |
411 REGULATOR_ERROR_UNDER_VOLTAGE_WARN |
412 REGULATOR_ERROR_OVER_VOLTAGE_WARN |
413 REGULATOR_ERROR_REGULATION_OUT |
414 REGULATOR_ERROR_OVER_CURRENT |
415 REGULATOR_ERROR_OVER_CURRENT_WARN | REGULATOR_ERROR_FAIL;
418 /* Register notifiers - can fail if IRQ is not given */
419 irq_helper = devm_regulator_irq_helper(dev, &max597x_notif,
421 &rdevs[0], num_switches);
422 if (IS_ERR(irq_helper)) {
423 if (PTR_ERR(irq_helper) == -EPROBE_DEFER)
424 return -EPROBE_DEFER;
426 dev_warn(dev, "IRQ disabled %pe\n", irq_helper);
432 static int max597x_regulator_probe(struct platform_device *pdev)
436 struct max597x_data *max597x = dev_get_drvdata(pdev->dev.parent);
437 struct max597x_regulator *data;
439 struct regulator_config config = { };
440 struct regulator_dev *rdev;
441 struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES];
442 int num_switches = max597x->num_switches;
445 for (i = 0; i < num_switches; i++) {
447 devm_kzalloc(max597x->dev, sizeof(struct max597x_regulator),
452 data->num_switches = num_switches;
453 data->regmap = max597x->regmap;
455 ret = max597x_adc_range(data->regmap, i, &max597x->irng[i], &max597x->mon_rng[i]);
459 data->irng = max597x->irng[i];
460 data->mon_rng = max597x->mon_rng[i];
462 config.dev = max597x->dev;
463 config.driver_data = (void *)data;
464 config.regmap = data->regmap;
465 rdev = devm_regulator_register(max597x->dev,
466 ®ulators[i], &config);
468 dev_err(max597x->dev, "failed to register regulator %s\n",
470 return PTR_ERR(rdev);
473 max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms;
478 max597x_setup_irq(max597x->dev, max597x->irq, rdevs, num_switches,
481 dev_err(max597x->dev, "IRQ setup failed");
489 static struct platform_driver max597x_regulator_driver = {
491 .name = "max597x-regulator",
493 .probe = max597x_regulator_probe,
496 module_platform_driver(max597x_regulator_driver);
499 MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>");
500 MODULE_DESCRIPTION("MAX5970_hot-swap controller driver");
501 MODULE_LICENSE("GPL v2");