1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Linear Technology LTC2945 I2C Power Monitor
5 * Copyright (c) 2014 Guenter Roeck
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/err.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/hwmon.h>
14 #include <linux/hwmon-sysfs.h>
15 #include <linux/jiffies.h>
16 #include <linux/regmap.h>
19 #define LTC2945_CONTROL 0x00
20 #define LTC2945_ALERT 0x01
21 #define LTC2945_STATUS 0x02
22 #define LTC2945_FAULT 0x03
23 #define LTC2945_POWER_H 0x05
24 #define LTC2945_MAX_POWER_H 0x08
25 #define LTC2945_MIN_POWER_H 0x0b
26 #define LTC2945_MAX_POWER_THRES_H 0x0e
27 #define LTC2945_MIN_POWER_THRES_H 0x11
28 #define LTC2945_SENSE_H 0x14
29 #define LTC2945_MAX_SENSE_H 0x16
30 #define LTC2945_MIN_SENSE_H 0x18
31 #define LTC2945_MAX_SENSE_THRES_H 0x1a
32 #define LTC2945_MIN_SENSE_THRES_H 0x1c
33 #define LTC2945_VIN_H 0x1e
34 #define LTC2945_MAX_VIN_H 0x20
35 #define LTC2945_MIN_VIN_H 0x22
36 #define LTC2945_MAX_VIN_THRES_H 0x24
37 #define LTC2945_MIN_VIN_THRES_H 0x26
38 #define LTC2945_ADIN_H 0x28
39 #define LTC2945_MAX_ADIN_H 0x2a
40 #define LTC2945_MIN_ADIN_H 0x2c
41 #define LTC2945_MAX_ADIN_THRES_H 0x2e
42 #define LTC2945_MIN_ADIN_THRES_H 0x30
43 #define LTC2945_MIN_ADIN_THRES_L 0x31
45 /* Fault register bits */
47 #define FAULT_ADIN_UV (1 << 0)
48 #define FAULT_ADIN_OV (1 << 1)
49 #define FAULT_VIN_UV (1 << 2)
50 #define FAULT_VIN_OV (1 << 3)
51 #define FAULT_SENSE_UV (1 << 4)
52 #define FAULT_SENSE_OV (1 << 5)
53 #define FAULT_POWER_UV (1 << 6)
54 #define FAULT_POWER_OV (1 << 7)
56 /* Control register bits */
58 #define CONTROL_MULT_SELECT (1 << 0)
59 #define CONTROL_TEST_MODE (1 << 4)
61 static inline bool is_power_reg(u8 reg
)
63 return reg
< LTC2945_SENSE_H
;
66 /* Return the value from the given register in uW, mV, or mA */
67 static long long ltc2945_reg_to_val(struct device
*dev
, u8 reg
)
69 struct regmap
*regmap
= dev_get_drvdata(dev
);
75 ret
= regmap_bulk_read(regmap
, reg
, buf
,
76 is_power_reg(reg
) ? 3 : 2);
80 if (is_power_reg(reg
)) {
82 val
= (buf
[0] << 16) + (buf
[1] << 8) + buf
[2];
84 /* current, voltage */
85 val
= (buf
[0] << 4) + (buf
[1] >> 4);
90 case LTC2945_MAX_POWER_H
:
91 case LTC2945_MIN_POWER_H
:
92 case LTC2945_MAX_POWER_THRES_H
:
93 case LTC2945_MIN_POWER_THRES_H
:
95 * Convert to uW by assuming current is measured with
96 * an 1mOhm sense resistor, similar to current
98 * Control register bit 0 selects if voltage at SENSE+/VDD
99 * or voltage at ADIN is used to measure power.
101 ret
= regmap_read(regmap
, LTC2945_CONTROL
, &control
);
104 if (control
& CONTROL_MULT_SELECT
) {
105 /* 25 mV * 25 uV = 0.625 uV resolution. */
108 /* 0.5 mV * 25 uV = 0.0125 uV resolution. */
109 val
= (val
* 25LL) >> 1;
113 case LTC2945_MAX_VIN_H
:
114 case LTC2945_MIN_VIN_H
:
115 case LTC2945_MAX_VIN_THRES_H
:
116 case LTC2945_MIN_VIN_THRES_H
:
117 /* 25 mV resolution. Convert to mV. */
121 case LTC2945_MAX_ADIN_H
:
122 case LTC2945_MIN_ADIN_THRES_H
:
123 case LTC2945_MAX_ADIN_THRES_H
:
124 case LTC2945_MIN_ADIN_H
:
125 /* 0.5mV resolution. Convert to mV. */
128 case LTC2945_SENSE_H
:
129 case LTC2945_MAX_SENSE_H
:
130 case LTC2945_MIN_SENSE_H
:
131 case LTC2945_MAX_SENSE_THRES_H
:
132 case LTC2945_MIN_SENSE_THRES_H
:
134 * 25 uV resolution. Convert to current as measured with
135 * an 1 mOhm sense resistor, in mA. If a different sense
136 * resistor is installed, calculate the actual current by
137 * dividing the reported current by the sense resistor value
148 static int ltc2945_val_to_reg(struct device
*dev
, u8 reg
,
151 struct regmap
*regmap
= dev_get_drvdata(dev
);
152 unsigned int control
;
156 case LTC2945_POWER_H
:
157 case LTC2945_MAX_POWER_H
:
158 case LTC2945_MIN_POWER_H
:
159 case LTC2945_MAX_POWER_THRES_H
:
160 case LTC2945_MIN_POWER_THRES_H
:
162 * Convert to register value by assuming current is measured
163 * with an 1mOhm sense resistor, similar to current
165 * Control register bit 0 selects if voltage at SENSE+/VDD
166 * or voltage at ADIN is used to measure power, which in turn
167 * determines register calculations.
169 ret
= regmap_read(regmap
, LTC2945_CONTROL
, &control
);
172 if (control
& CONTROL_MULT_SELECT
) {
173 /* 25 mV * 25 uV = 0.625 uV resolution. */
174 val
= DIV_ROUND_CLOSEST(val
, 625);
177 * 0.5 mV * 25 uV = 0.0125 uV resolution.
178 * Divide first to avoid overflow;
179 * accept loss of accuracy.
181 val
= DIV_ROUND_CLOSEST(val
, 25) * 2;
185 case LTC2945_MAX_VIN_H
:
186 case LTC2945_MIN_VIN_H
:
187 case LTC2945_MAX_VIN_THRES_H
:
188 case LTC2945_MIN_VIN_THRES_H
:
189 /* 25 mV resolution. */
193 case LTC2945_MAX_ADIN_H
:
194 case LTC2945_MIN_ADIN_THRES_H
:
195 case LTC2945_MAX_ADIN_THRES_H
:
196 case LTC2945_MIN_ADIN_H
:
197 /* 0.5mV resolution. */
200 case LTC2945_SENSE_H
:
201 case LTC2945_MAX_SENSE_H
:
202 case LTC2945_MIN_SENSE_H
:
203 case LTC2945_MAX_SENSE_THRES_H
:
204 case LTC2945_MIN_SENSE_THRES_H
:
206 * 25 uV resolution. Convert to current as measured with
207 * an 1 mOhm sense resistor, in mA. If a different sense
208 * resistor is installed, calculate the actual current by
209 * dividing the reported current by the sense resistor value
212 val
= DIV_ROUND_CLOSEST(val
, 25);
220 static ssize_t
ltc2945_value_show(struct device
*dev
,
221 struct device_attribute
*da
, char *buf
)
223 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
226 value
= ltc2945_reg_to_val(dev
, attr
->index
);
229 return snprintf(buf
, PAGE_SIZE
, "%lld\n", value
);
232 static ssize_t
ltc2945_value_store(struct device
*dev
,
233 struct device_attribute
*da
,
234 const char *buf
, size_t count
)
236 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
237 struct regmap
*regmap
= dev_get_drvdata(dev
);
238 u8 reg
= attr
->index
;
245 ret
= kstrtoul(buf
, 10, &val
);
249 /* convert to register value, then clamp and write result */
250 regval
= ltc2945_val_to_reg(dev
, reg
, val
);
251 if (is_power_reg(reg
)) {
252 regval
= clamp_val(regval
, 0, 0xffffff);
253 regbuf
[0] = regval
>> 16;
254 regbuf
[1] = (regval
>> 8) & 0xff;
258 regval
= clamp_val(regval
, 0, 0xfff) << 4;
259 regbuf
[0] = regval
>> 8;
260 regbuf
[1] = regval
& 0xff;
263 ret
= regmap_bulk_write(regmap
, reg
, regbuf
, num_regs
);
264 return ret
< 0 ? ret
: count
;
267 static ssize_t
ltc2945_history_store(struct device
*dev
,
268 struct device_attribute
*da
,
269 const char *buf
, size_t count
)
271 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
272 struct regmap
*regmap
= dev_get_drvdata(dev
);
273 u8 reg
= attr
->index
;
274 int num_regs
= is_power_reg(reg
) ? 3 : 2;
275 u8 buf_min
[3] = { 0xff, 0xff, 0xff };
276 u8 buf_max
[3] = { 0, 0, 0 };
280 ret
= kstrtoul(buf
, 10, &val
);
286 ret
= regmap_update_bits(regmap
, LTC2945_CONTROL
, CONTROL_TEST_MODE
,
290 ret
= regmap_bulk_write(regmap
, reg
, buf_min
, num_regs
);
295 case LTC2945_MIN_POWER_H
:
296 reg
= LTC2945_MAX_POWER_H
;
298 case LTC2945_MIN_SENSE_H
:
299 reg
= LTC2945_MAX_SENSE_H
;
301 case LTC2945_MIN_VIN_H
:
302 reg
= LTC2945_MAX_VIN_H
;
304 case LTC2945_MIN_ADIN_H
:
305 reg
= LTC2945_MAX_ADIN_H
;
308 WARN_ONCE(1, "Bad register: 0x%x\n", reg
);
312 ret
= regmap_bulk_write(regmap
, reg
, buf_max
, num_regs
);
314 /* Try resetting test mode even if there was an error */
315 regmap_update_bits(regmap
, LTC2945_CONTROL
, CONTROL_TEST_MODE
, 0);
317 return ret
? : count
;
320 static ssize_t
ltc2945_bool_show(struct device
*dev
,
321 struct device_attribute
*da
, char *buf
)
323 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
324 struct regmap
*regmap
= dev_get_drvdata(dev
);
328 ret
= regmap_read(regmap
, LTC2945_FAULT
, &fault
);
332 fault
&= attr
->index
;
333 if (fault
) /* Clear reported faults in chip register */
334 regmap_update_bits(regmap
, LTC2945_FAULT
, attr
->index
, 0);
336 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!fault
);
341 static SENSOR_DEVICE_ATTR_RO(in1_input
, ltc2945_value
, LTC2945_VIN_H
);
342 static SENSOR_DEVICE_ATTR_RW(in1_min
, ltc2945_value
, LTC2945_MIN_VIN_THRES_H
);
343 static SENSOR_DEVICE_ATTR_RW(in1_max
, ltc2945_value
, LTC2945_MAX_VIN_THRES_H
);
344 static SENSOR_DEVICE_ATTR_RO(in1_lowest
, ltc2945_value
, LTC2945_MIN_VIN_H
);
345 static SENSOR_DEVICE_ATTR_RO(in1_highest
, ltc2945_value
, LTC2945_MAX_VIN_H
);
346 static SENSOR_DEVICE_ATTR_WO(in1_reset_history
, ltc2945_history
,
349 static SENSOR_DEVICE_ATTR_RO(in2_input
, ltc2945_value
, LTC2945_ADIN_H
);
350 static SENSOR_DEVICE_ATTR_RW(in2_min
, ltc2945_value
, LTC2945_MIN_ADIN_THRES_H
);
351 static SENSOR_DEVICE_ATTR_RW(in2_max
, ltc2945_value
, LTC2945_MAX_ADIN_THRES_H
);
352 static SENSOR_DEVICE_ATTR_RO(in2_lowest
, ltc2945_value
, LTC2945_MIN_ADIN_H
);
353 static SENSOR_DEVICE_ATTR_RO(in2_highest
, ltc2945_value
, LTC2945_MAX_ADIN_H
);
354 static SENSOR_DEVICE_ATTR_WO(in2_reset_history
, ltc2945_history
,
359 static SENSOR_DEVICE_ATTR_RO(in1_min_alarm
, ltc2945_bool
, FAULT_VIN_UV
);
360 static SENSOR_DEVICE_ATTR_RO(in1_max_alarm
, ltc2945_bool
, FAULT_VIN_OV
);
361 static SENSOR_DEVICE_ATTR_RO(in2_min_alarm
, ltc2945_bool
, FAULT_ADIN_UV
);
362 static SENSOR_DEVICE_ATTR_RO(in2_max_alarm
, ltc2945_bool
, FAULT_ADIN_OV
);
364 /* Currents (via sense resistor) */
366 static SENSOR_DEVICE_ATTR_RO(curr1_input
, ltc2945_value
, LTC2945_SENSE_H
);
367 static SENSOR_DEVICE_ATTR_RW(curr1_min
, ltc2945_value
,
368 LTC2945_MIN_SENSE_THRES_H
);
369 static SENSOR_DEVICE_ATTR_RW(curr1_max
, ltc2945_value
,
370 LTC2945_MAX_SENSE_THRES_H
);
371 static SENSOR_DEVICE_ATTR_RO(curr1_lowest
, ltc2945_value
, LTC2945_MIN_SENSE_H
);
372 static SENSOR_DEVICE_ATTR_RO(curr1_highest
, ltc2945_value
,
373 LTC2945_MAX_SENSE_H
);
374 static SENSOR_DEVICE_ATTR_WO(curr1_reset_history
, ltc2945_history
,
375 LTC2945_MIN_SENSE_H
);
379 static SENSOR_DEVICE_ATTR_RO(curr1_min_alarm
, ltc2945_bool
, FAULT_SENSE_UV
);
380 static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm
, ltc2945_bool
, FAULT_SENSE_OV
);
384 static SENSOR_DEVICE_ATTR_RO(power1_input
, ltc2945_value
, LTC2945_POWER_H
);
385 static SENSOR_DEVICE_ATTR_RW(power1_min
, ltc2945_value
,
386 LTC2945_MIN_POWER_THRES_H
);
387 static SENSOR_DEVICE_ATTR_RW(power1_max
, ltc2945_value
,
388 LTC2945_MAX_POWER_THRES_H
);
389 static SENSOR_DEVICE_ATTR_RO(power1_input_lowest
, ltc2945_value
,
390 LTC2945_MIN_POWER_H
);
391 static SENSOR_DEVICE_ATTR_RO(power1_input_highest
, ltc2945_value
,
392 LTC2945_MAX_POWER_H
);
393 static SENSOR_DEVICE_ATTR_WO(power1_reset_history
, ltc2945_history
,
394 LTC2945_MIN_POWER_H
);
398 static SENSOR_DEVICE_ATTR_RO(power1_min_alarm
, ltc2945_bool
, FAULT_POWER_UV
);
399 static SENSOR_DEVICE_ATTR_RO(power1_max_alarm
, ltc2945_bool
, FAULT_POWER_OV
);
401 static struct attribute
*ltc2945_attrs
[] = {
402 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
403 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
404 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
405 &sensor_dev_attr_in1_lowest
.dev_attr
.attr
,
406 &sensor_dev_attr_in1_highest
.dev_attr
.attr
,
407 &sensor_dev_attr_in1_reset_history
.dev_attr
.attr
,
408 &sensor_dev_attr_in1_min_alarm
.dev_attr
.attr
,
409 &sensor_dev_attr_in1_max_alarm
.dev_attr
.attr
,
411 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
412 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
413 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
414 &sensor_dev_attr_in2_lowest
.dev_attr
.attr
,
415 &sensor_dev_attr_in2_highest
.dev_attr
.attr
,
416 &sensor_dev_attr_in2_reset_history
.dev_attr
.attr
,
417 &sensor_dev_attr_in2_min_alarm
.dev_attr
.attr
,
418 &sensor_dev_attr_in2_max_alarm
.dev_attr
.attr
,
420 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
421 &sensor_dev_attr_curr1_min
.dev_attr
.attr
,
422 &sensor_dev_attr_curr1_max
.dev_attr
.attr
,
423 &sensor_dev_attr_curr1_lowest
.dev_attr
.attr
,
424 &sensor_dev_attr_curr1_highest
.dev_attr
.attr
,
425 &sensor_dev_attr_curr1_reset_history
.dev_attr
.attr
,
426 &sensor_dev_attr_curr1_min_alarm
.dev_attr
.attr
,
427 &sensor_dev_attr_curr1_max_alarm
.dev_attr
.attr
,
429 &sensor_dev_attr_power1_input
.dev_attr
.attr
,
430 &sensor_dev_attr_power1_min
.dev_attr
.attr
,
431 &sensor_dev_attr_power1_max
.dev_attr
.attr
,
432 &sensor_dev_attr_power1_input_lowest
.dev_attr
.attr
,
433 &sensor_dev_attr_power1_input_highest
.dev_attr
.attr
,
434 &sensor_dev_attr_power1_reset_history
.dev_attr
.attr
,
435 &sensor_dev_attr_power1_min_alarm
.dev_attr
.attr
,
436 &sensor_dev_attr_power1_max_alarm
.dev_attr
.attr
,
440 ATTRIBUTE_GROUPS(ltc2945
);
442 static const struct regmap_config ltc2945_regmap_config
= {
445 .max_register
= LTC2945_MIN_ADIN_THRES_L
,
448 static int ltc2945_probe(struct i2c_client
*client
)
450 struct device
*dev
= &client
->dev
;
451 struct device
*hwmon_dev
;
452 struct regmap
*regmap
;
454 regmap
= devm_regmap_init_i2c(client
, <c2945_regmap_config
);
455 if (IS_ERR(regmap
)) {
456 dev_err(dev
, "failed to allocate register map\n");
457 return PTR_ERR(regmap
);
461 regmap_write(regmap
, LTC2945_FAULT
, 0x00);
463 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
466 return PTR_ERR_OR_ZERO(hwmon_dev
);
469 static const struct i2c_device_id ltc2945_id
[] = {
474 MODULE_DEVICE_TABLE(i2c
, ltc2945_id
);
476 static struct i2c_driver ltc2945_driver
= {
480 .probe_new
= ltc2945_probe
,
481 .id_table
= ltc2945_id
,
484 module_i2c_driver(ltc2945_driver
);
486 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
487 MODULE_DESCRIPTION("LTC2945 driver");
488 MODULE_LICENSE("GPL");