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/hwmon.h>
14 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/machine.h>
21 #include <linux/regulator/of_regulator.h>
22 #include <linux/platform_device.h>
24 #include <linux/mfd/max5970.h>
26 struct max5970_regulator
{
27 int num_switches
, mon_rng
, irng
, shunt_micro_ohms
, lim_uA
;
28 struct regmap
*regmap
;
31 enum max597x_regulator_id
{
36 static int max5970_read_adc(struct regmap
*regmap
, int reg
, long *val
)
41 ret
= regmap_bulk_read(regmap
, reg
, ®_data
[0], 2);
45 *val
= (reg_data
[0] << 2) | (reg_data
[1] & 3);
50 static int max5970_read(struct device
*dev
, enum hwmon_sensor_types type
,
51 u32 attr
, int channel
, long *val
)
53 struct regulator_dev
**rdevs
= dev_get_drvdata(dev
);
54 struct max5970_regulator
*ddata
= rdev_get_drvdata(rdevs
[channel
]);
55 struct regmap
*regmap
= ddata
->regmap
;
61 case hwmon_curr_input
:
62 ret
= max5970_read_adc(regmap
, MAX5970_REG_CURRENT_H(channel
), val
);
66 * Calculate current from ADC value, IRNG range & shunt resistor value.
67 * ddata->irng holds the voltage corresponding to the maximum value the
68 * 10-bit ADC can measure.
69 * To obtain the output, multiply the ADC value by the IRNG range (in
70 * millivolts) and then divide it by the maximum value of the 10-bit ADC.
72 *val
= (*val
* ddata
->irng
) >> 10;
73 /* Convert the voltage measurement across shunt resistor to current */
74 *val
= (*val
* 1000) / ddata
->shunt_micro_ohms
;
83 ret
= max5970_read_adc(regmap
, MAX5970_REG_VOLTAGE_H(channel
), val
);
87 * Calculate voltage from ADC value and MON range.
88 * ddata->mon_rng holds the voltage corresponding to the maximum value the
89 * 10-bit ADC can measure.
90 * To obtain the output, multiply the ADC value by the MON range (in
91 * microvolts) and then divide it by the maximum value of the 10-bit ADC.
93 *val
= mul_u64_u32_shr(*val
, ddata
->mon_rng
, 10);
105 static umode_t
max5970_is_visible(const void *data
,
106 enum hwmon_sensor_types type
,
107 u32 attr
, int channel
)
109 struct regulator_dev
**rdevs
= (struct regulator_dev
**)data
;
110 struct max5970_regulator
*ddata
;
112 if (channel
>= MAX5970_NUM_SWITCHES
|| !rdevs
[channel
])
115 ddata
= rdev_get_drvdata(rdevs
[channel
]);
117 if (channel
>= ddata
->num_switches
)
131 case hwmon_curr_input
:
132 /* Current measurement requires knowledge of the shunt resistor value. */
133 if (ddata
->shunt_micro_ohms
)
146 static const struct hwmon_ops max5970_hwmon_ops
= {
147 .is_visible
= max5970_is_visible
,
148 .read
= max5970_read
,
151 static const struct hwmon_channel_info
*max5970_info
[] = {
152 HWMON_CHANNEL_INFO(in
, HWMON_I_INPUT
, HWMON_I_INPUT
),
153 HWMON_CHANNEL_INFO(curr
, HWMON_C_INPUT
, HWMON_C_INPUT
),
157 static const struct hwmon_chip_info max5970_chip_info
= {
158 .ops
= &max5970_hwmon_ops
,
159 .info
= max5970_info
,
162 static int max597x_uvp_ovp_check_mode(struct regulator_dev
*rdev
, int severity
)
166 /* Status1 register contains the soft strap values sampled at POR */
167 ret
= regmap_read(rdev
->regmap
, MAX5970_REG_STATUS1
, ®
);
171 /* Check soft straps match requested mode */
172 if (severity
== REGULATOR_SEVERITY_PROT
) {
173 if (STATUS1_PROT(reg
) != STATUS1_PROT_SHUTDOWN
)
178 if (STATUS1_PROT(reg
) == STATUS1_PROT_SHUTDOWN
)
184 static int max597x_set_vp(struct regulator_dev
*rdev
, int lim_uV
, int severity
,
185 bool enable
, bool overvoltage
)
187 int off_h
, off_l
, reg
, ret
;
188 struct max5970_regulator
*data
= rdev_get_drvdata(rdev
);
189 int channel
= rdev_get_id(rdev
);
192 if (severity
== REGULATOR_SEVERITY_WARN
) {
193 off_h
= MAX5970_REG_CH_OV_WARN_H(channel
);
194 off_l
= MAX5970_REG_CH_OV_WARN_L(channel
);
196 off_h
= MAX5970_REG_CH_OV_CRIT_H(channel
);
197 off_l
= MAX5970_REG_CH_OV_CRIT_L(channel
);
200 if (severity
== REGULATOR_SEVERITY_WARN
) {
201 off_h
= MAX5970_REG_CH_UV_WARN_H(channel
);
202 off_l
= MAX5970_REG_CH_UV_WARN_L(channel
);
204 off_h
= MAX5970_REG_CH_UV_CRIT_H(channel
);
205 off_l
= MAX5970_REG_CH_UV_CRIT_L(channel
);
210 /* reg = ADC_MASK * (lim_uV / 1000000) / (data->mon_rng / 1000000) */
211 reg
= ADC_MASK
* lim_uV
/ data
->mon_rng
;
215 ret
= regmap_write(rdev
->regmap
, off_h
, MAX5970_VAL2REG_H(reg
));
219 ret
= regmap_write(rdev
->regmap
, off_l
, MAX5970_VAL2REG_L(reg
));
226 static int max597x_set_uvp(struct regulator_dev
*rdev
, int lim_uV
, int severity
,
232 * MAX5970 has enable control as a special value in limit reg. Can't
233 * set limit but keep feature disabled or enable W/O given limit.
235 if ((lim_uV
&& !enable
) || (!lim_uV
&& enable
))
238 ret
= max597x_uvp_ovp_check_mode(rdev
, severity
);
242 return max597x_set_vp(rdev
, lim_uV
, severity
, enable
, false);
245 static int max597x_set_ovp(struct regulator_dev
*rdev
, int lim_uV
, int severity
,
251 * MAX5970 has enable control as a special value in limit reg. Can't
252 * set limit but keep feature disabled or enable W/O given limit.
254 if ((lim_uV
&& !enable
) || (!lim_uV
&& enable
))
257 ret
= max597x_uvp_ovp_check_mode(rdev
, severity
);
261 return max597x_set_vp(rdev
, lim_uV
, severity
, enable
, true);
264 static int max597x_set_ocp(struct regulator_dev
*rdev
, int lim_uA
,
265 int severity
, bool enable
)
268 unsigned int vthst
, vthfst
;
270 struct max5970_regulator
*data
= rdev_get_drvdata(rdev
);
271 int rdev_id
= rdev_get_id(rdev
);
273 * MAX5970 doesn't has enable control for ocp.
274 * If limit is specified but enable is not set then hold the value in
275 * variable & later use it when ocp needs to be enabled.
277 if (lim_uA
!= 0 && lim_uA
!= data
->lim_uA
)
278 data
->lim_uA
= lim_uA
;
280 if (severity
!= REGULATOR_SEVERITY_PROT
)
285 /* Calc Vtrip threshold in uV. */
287 div_u64(mul_u32_u32(data
->shunt_micro_ohms
, data
->lim_uA
),
291 * As recommended in datasheed, add 20% margin to avoid
292 * spurious event & passive component tolerance.
294 vthst
= div_u64(mul_u32_u32(vthst
, 120), 100);
296 /* Calc fast Vtrip threshold in uV */
297 vthfst
= vthst
* (MAX5970_FAST2SLOW_RATIO
/ 100);
299 if (vthfst
> data
->irng
) {
300 dev_err(&rdev
->dev
, "Current limit out of range\n");
303 /* Fast trip threshold to be programmed */
304 val
= div_u64(mul_u32_u32(0xFF, vthfst
), data
->irng
);
307 * Since there is no option to disable ocp, set limit to max
312 reg
= MAX5970_REG_DAC_FAST(rdev_id
);
314 return regmap_write(rdev
->regmap
, reg
, val
);
317 static int max597x_get_status(struct regulator_dev
*rdev
)
321 ret
= regmap_read(rdev
->regmap
, MAX5970_REG_STATUS3
, &val
);
325 if (val
& MAX5970_STATUS3_ALERT
)
326 return REGULATOR_STATUS_ERROR
;
328 ret
= regulator_is_enabled_regmap(rdev
);
333 return REGULATOR_STATUS_ON
;
335 return REGULATOR_STATUS_OFF
;
338 static const struct regulator_ops max597x_switch_ops
= {
339 .enable
= regulator_enable_regmap
,
340 .disable
= regulator_disable_regmap
,
341 .is_enabled
= regulator_is_enabled_regmap
,
342 .get_status
= max597x_get_status
,
343 .set_over_voltage_protection
= max597x_set_ovp
,
344 .set_under_voltage_protection
= max597x_set_uvp
,
345 .set_over_current_protection
= max597x_set_ocp
,
348 static int max597x_dt_parse(struct device_node
*np
,
349 const struct regulator_desc
*desc
,
350 struct regulator_config
*cfg
)
352 struct max5970_regulator
*data
= cfg
->driver_data
;
356 of_property_read_u32(np
, "shunt-resistor-micro-ohms",
357 &data
->shunt_micro_ohms
);
360 "property 'shunt-resistor-micro-ohms' not found, err %d\n",
366 #define MAX597X_SWITCH(_ID, _ereg, _chan, _supply) { \
368 .of_match = of_match_ptr(#_ID), \
369 .ops = &max597x_switch_ops, \
370 .regulators_node = of_match_ptr("regulators"), \
371 .type = REGULATOR_VOLTAGE, \
372 .id = MAX597X_##_ID, \
373 .owner = THIS_MODULE, \
374 .supply_name = _supply, \
375 .enable_reg = _ereg, \
376 .enable_mask = CHXEN((_chan)), \
377 .of_parse_cb = max597x_dt_parse, \
380 static const struct regulator_desc regulators
[] = {
381 MAX597X_SWITCH(sw0
, MAX5970_REG_CHXEN
, 0, "vss1"),
382 MAX597X_SWITCH(sw1
, MAX5970_REG_CHXEN
, 1, "vss2"),
385 static int max597x_regmap_read_clear(struct regmap
*map
, unsigned int reg
,
390 ret
= regmap_read(map
, reg
, val
);
395 return regmap_write(map
, reg
, 0);
400 static int max597x_irq_handler(int irq
, struct regulator_irq_data
*rid
,
401 unsigned long *dev_mask
)
403 struct regulator_err_state
*stat
;
404 struct max5970_regulator
*d
= (struct max5970_regulator
*)rid
->data
;
407 ret
= max597x_regmap_read_clear(d
->regmap
, MAX5970_REG_FAULT0
, &val
);
409 return REGULATOR_FAILED_RETRY
;
412 for (i
= 0; i
< d
->num_switches
; i
++) {
413 stat
= &rid
->states
[i
];
418 for (i
= 0; i
< d
->num_switches
; i
++) {
419 stat
= &rid
->states
[i
];
421 if (val
& UV_STATUS_CRIT(i
)) {
423 stat
->notifs
|= REGULATOR_EVENT_UNDER_VOLTAGE
;
424 stat
->errors
|= REGULATOR_ERROR_UNDER_VOLTAGE
;
425 } else if (val
& UV_STATUS_WARN(i
)) {
427 stat
->notifs
|= REGULATOR_EVENT_UNDER_VOLTAGE_WARN
;
428 stat
->errors
|= REGULATOR_ERROR_UNDER_VOLTAGE_WARN
;
432 ret
= max597x_regmap_read_clear(d
->regmap
, MAX5970_REG_FAULT1
, &val
);
434 return REGULATOR_FAILED_RETRY
;
436 for (i
= 0; i
< d
->num_switches
; i
++) {
437 stat
= &rid
->states
[i
];
439 if (val
& OV_STATUS_CRIT(i
)) {
441 stat
->notifs
|= REGULATOR_EVENT_REGULATION_OUT
;
442 stat
->errors
|= REGULATOR_ERROR_REGULATION_OUT
;
443 } else if (val
& OV_STATUS_WARN(i
)) {
445 stat
->notifs
|= REGULATOR_EVENT_OVER_VOLTAGE_WARN
;
446 stat
->errors
|= REGULATOR_ERROR_OVER_VOLTAGE_WARN
;
450 ret
= max597x_regmap_read_clear(d
->regmap
, MAX5970_REG_FAULT2
, &val
);
452 return REGULATOR_FAILED_RETRY
;
454 for (i
= 0; i
< d
->num_switches
; i
++) {
455 stat
= &rid
->states
[i
];
457 if (val
& OC_STATUS_WARN(i
)) {
459 stat
->notifs
|= REGULATOR_EVENT_OVER_CURRENT_WARN
;
460 stat
->errors
|= REGULATOR_ERROR_OVER_CURRENT_WARN
;
464 ret
= regmap_read(d
->regmap
, MAX5970_REG_STATUS0
, &val
);
466 return REGULATOR_FAILED_RETRY
;
468 for (i
= 0; i
< d
->num_switches
; i
++) {
469 stat
= &rid
->states
[i
];
471 if ((val
& MAX5970_CB_IFAULTF(i
))
472 || (val
& MAX5970_CB_IFAULTS(i
))) {
475 REGULATOR_EVENT_OVER_CURRENT
|
476 REGULATOR_EVENT_DISABLE
;
478 REGULATOR_ERROR_OVER_CURRENT
| REGULATOR_ERROR_FAIL
;
480 /* Clear the sub-IRQ status */
481 regulator_disable_regmap(stat
->rdev
);
487 static int max597x_adc_range(struct regmap
*regmap
, const int ch
,
488 int *irng
, int *mon_rng
)
493 /* Decode current ADC range */
494 ret
= regmap_read(regmap
, MAX5970_REG_STATUS2
, ®
);
497 switch (MAX5970_IRNG(reg
, ch
)) {
499 *irng
= 100000; /* 100 mV */
502 *irng
= 50000; /* 50 mV */
505 *irng
= 25000; /* 25 mV */
511 /* Decode current voltage monitor range */
512 ret
= regmap_read(regmap
, MAX5970_REG_MON_RANGE
, ®
);
516 *mon_rng
= MAX5970_MON_MAX_RANGE_UV
>> MAX5970_MON(reg
, ch
);
521 static int max597x_setup_irq(struct device
*dev
,
523 struct regulator_dev
*rdevs
[MAX5970_NUM_SWITCHES
],
524 int num_switches
, struct max5970_regulator
*data
)
526 struct regulator_irq_desc max597x_notif
= {
527 .name
= "max597x-irq",
528 .map_event
= max597x_irq_handler
,
531 int errs
= REGULATOR_ERROR_UNDER_VOLTAGE
|
532 REGULATOR_ERROR_UNDER_VOLTAGE_WARN
|
533 REGULATOR_ERROR_OVER_VOLTAGE_WARN
|
534 REGULATOR_ERROR_REGULATION_OUT
|
535 REGULATOR_ERROR_OVER_CURRENT
|
536 REGULATOR_ERROR_OVER_CURRENT_WARN
| REGULATOR_ERROR_FAIL
;
539 /* Register notifiers - can fail if IRQ is not given */
540 irq_helper
= devm_regulator_irq_helper(dev
, &max597x_notif
,
542 &rdevs
[0], num_switches
);
543 if (IS_ERR(irq_helper
)) {
544 if (PTR_ERR(irq_helper
) == -EPROBE_DEFER
)
545 return -EPROBE_DEFER
;
547 dev_warn(dev
, "IRQ disabled %pe\n", irq_helper
);
553 static int max597x_regulator_probe(struct platform_device
*pdev
)
555 struct regmap
*regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
556 struct max5970_regulator
*data
;
557 struct i2c_client
*i2c
= to_i2c_client(pdev
->dev
.parent
);
558 struct regulator_config config
= { };
559 struct regulator_dev
*rdev
;
560 struct regulator_dev
**rdevs
= NULL
;
561 struct device
*hwmon_dev
;
566 return -EPROBE_DEFER
;
568 rdevs
= devm_kcalloc(&i2c
->dev
, MAX5970_NUM_SWITCHES
, sizeof(struct regulator_dev
*),
573 if (of_device_is_compatible(i2c
->dev
.of_node
, "maxim,max5978"))
574 num_switches
= MAX5978_NUM_SWITCHES
;
575 else if (of_device_is_compatible(i2c
->dev
.of_node
, "maxim,max5970"))
576 num_switches
= MAX5970_NUM_SWITCHES
;
580 for (i
= 0; i
< num_switches
; i
++) {
582 devm_kzalloc(&i2c
->dev
, sizeof(struct max5970_regulator
),
587 data
->num_switches
= num_switches
;
588 data
->regmap
= regmap
;
590 ret
= max597x_adc_range(regmap
, i
, &data
->irng
, &data
->mon_rng
);
594 config
.dev
= &i2c
->dev
;
595 config
.driver_data
= (void *)data
;
596 config
.regmap
= data
->regmap
;
597 rdev
= devm_regulator_register(&i2c
->dev
,
598 ®ulators
[i
], &config
);
600 dev_err(&i2c
->dev
, "failed to register regulator %s\n",
602 return PTR_ERR(rdev
);
607 if (IS_REACHABLE(CONFIG_HWMON
)) {
608 hwmon_dev
= devm_hwmon_device_register_with_info(&i2c
->dev
, "max5970", rdevs
,
609 &max5970_chip_info
, NULL
);
610 if (IS_ERR(hwmon_dev
)) {
611 return dev_err_probe(&i2c
->dev
, PTR_ERR(hwmon_dev
),
612 "Unable to register hwmon device\n");
618 max597x_setup_irq(&i2c
->dev
, i2c
->irq
, rdevs
, num_switches
,
621 dev_err(&i2c
->dev
, "IRQ setup failed");
629 static struct platform_driver max597x_regulator_driver
= {
631 .name
= "max5970-regulator",
632 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
634 .probe
= max597x_regulator_probe
,
637 module_platform_driver(max597x_regulator_driver
);
640 MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>");
641 MODULE_DESCRIPTION("MAX5970_hot-swap controller driver");
642 MODULE_LICENSE("GPL v2");