2 * Driver for Linear Technology LTC2945 I2C Power Monitor
4 * Copyright (c) 2014 Guenter Roeck
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/i2c.h>
22 #include <linux/hwmon.h>
23 #include <linux/hwmon-sysfs.h>
24 #include <linux/jiffies.h>
25 #include <linux/regmap.h>
28 #define LTC2945_CONTROL 0x00
29 #define LTC2945_ALERT 0x01
30 #define LTC2945_STATUS 0x02
31 #define LTC2945_FAULT 0x03
32 #define LTC2945_POWER_H 0x05
33 #define LTC2945_MAX_POWER_H 0x08
34 #define LTC2945_MIN_POWER_H 0x0b
35 #define LTC2945_MAX_POWER_THRES_H 0x0e
36 #define LTC2945_MIN_POWER_THRES_H 0x11
37 #define LTC2945_SENSE_H 0x14
38 #define LTC2945_MAX_SENSE_H 0x16
39 #define LTC2945_MIN_SENSE_H 0x18
40 #define LTC2945_MAX_SENSE_THRES_H 0x1a
41 #define LTC2945_MIN_SENSE_THRES_H 0x1c
42 #define LTC2945_VIN_H 0x1e
43 #define LTC2945_MAX_VIN_H 0x20
44 #define LTC2945_MIN_VIN_H 0x22
45 #define LTC2945_MAX_VIN_THRES_H 0x24
46 #define LTC2945_MIN_VIN_THRES_H 0x26
47 #define LTC2945_ADIN_H 0x28
48 #define LTC2945_MAX_ADIN_H 0x2a
49 #define LTC2945_MIN_ADIN_H 0x2c
50 #define LTC2945_MAX_ADIN_THRES_H 0x2e
51 #define LTC2945_MIN_ADIN_THRES_H 0x30
52 #define LTC2945_MIN_ADIN_THRES_L 0x31
54 /* Fault register bits */
56 #define FAULT_ADIN_UV (1 << 0)
57 #define FAULT_ADIN_OV (1 << 1)
58 #define FAULT_VIN_UV (1 << 2)
59 #define FAULT_VIN_OV (1 << 3)
60 #define FAULT_SENSE_UV (1 << 4)
61 #define FAULT_SENSE_OV (1 << 5)
62 #define FAULT_POWER_UV (1 << 6)
63 #define FAULT_POWER_OV (1 << 7)
65 /* Control register bits */
67 #define CONTROL_MULT_SELECT (1 << 0)
68 #define CONTROL_TEST_MODE (1 << 4)
70 static inline bool is_power_reg(u8 reg
)
72 return reg
< LTC2945_SENSE_H
;
75 /* Return the value from the given register in uW, mV, or mA */
76 static long long ltc2945_reg_to_val(struct device
*dev
, u8 reg
)
78 struct regmap
*regmap
= dev_get_drvdata(dev
);
84 ret
= regmap_bulk_read(regmap
, reg
, buf
,
85 is_power_reg(reg
) ? 3 : 2);
89 if (is_power_reg(reg
)) {
91 val
= (buf
[0] << 16) + (buf
[1] << 8) + buf
[2];
93 /* current, voltage */
94 val
= (buf
[0] << 4) + (buf
[1] >> 4);
99 case LTC2945_MAX_POWER_H
:
100 case LTC2945_MIN_POWER_H
:
101 case LTC2945_MAX_POWER_THRES_H
:
102 case LTC2945_MIN_POWER_THRES_H
:
104 * Convert to uW by assuming current is measured with
105 * an 1mOhm sense resistor, similar to current
107 * Control register bit 0 selects if voltage at SENSE+/VDD
108 * or voltage at ADIN is used to measure power.
110 ret
= regmap_read(regmap
, LTC2945_CONTROL
, &control
);
113 if (control
& CONTROL_MULT_SELECT
) {
114 /* 25 mV * 25 uV = 0.625 uV resolution. */
117 /* 0.5 mV * 25 uV = 0.0125 uV resolution. */
118 val
= (val
* 25LL) >> 1;
122 case LTC2945_MAX_VIN_H
:
123 case LTC2945_MIN_VIN_H
:
124 case LTC2945_MAX_VIN_THRES_H
:
125 case LTC2945_MIN_VIN_THRES_H
:
126 /* 25 mV resolution. Convert to mV. */
130 case LTC2945_MAX_ADIN_H
:
131 case LTC2945_MIN_ADIN_THRES_H
:
132 case LTC2945_MAX_ADIN_THRES_H
:
133 case LTC2945_MIN_ADIN_H
:
134 /* 0.5mV resolution. Convert to mV. */
137 case LTC2945_SENSE_H
:
138 case LTC2945_MAX_SENSE_H
:
139 case LTC2945_MIN_SENSE_H
:
140 case LTC2945_MAX_SENSE_THRES_H
:
141 case LTC2945_MIN_SENSE_THRES_H
:
143 * 25 uV resolution. Convert to current as measured with
144 * an 1 mOhm sense resistor, in mA. If a different sense
145 * resistor is installed, calculate the actual current by
146 * dividing the reported current by the sense resistor value
157 static int ltc2945_val_to_reg(struct device
*dev
, u8 reg
,
160 struct regmap
*regmap
= dev_get_drvdata(dev
);
161 unsigned int control
;
165 case LTC2945_POWER_H
:
166 case LTC2945_MAX_POWER_H
:
167 case LTC2945_MIN_POWER_H
:
168 case LTC2945_MAX_POWER_THRES_H
:
169 case LTC2945_MIN_POWER_THRES_H
:
171 * Convert to register value by assuming current is measured
172 * with an 1mOhm sense resistor, similar to current
174 * Control register bit 0 selects if voltage at SENSE+/VDD
175 * or voltage at ADIN is used to measure power, which in turn
176 * determines register calculations.
178 ret
= regmap_read(regmap
, LTC2945_CONTROL
, &control
);
181 if (control
& CONTROL_MULT_SELECT
) {
182 /* 25 mV * 25 uV = 0.625 uV resolution. */
183 val
= DIV_ROUND_CLOSEST(val
, 625);
186 * 0.5 mV * 25 uV = 0.0125 uV resolution.
187 * Divide first to avoid overflow;
188 * accept loss of accuracy.
190 val
= DIV_ROUND_CLOSEST(val
, 25) * 2;
194 case LTC2945_MAX_VIN_H
:
195 case LTC2945_MIN_VIN_H
:
196 case LTC2945_MAX_VIN_THRES_H
:
197 case LTC2945_MIN_VIN_THRES_H
:
198 /* 25 mV resolution. */
202 case LTC2945_MAX_ADIN_H
:
203 case LTC2945_MIN_ADIN_THRES_H
:
204 case LTC2945_MAX_ADIN_THRES_H
:
205 case LTC2945_MIN_ADIN_H
:
206 /* 0.5mV resolution. */
209 case LTC2945_SENSE_H
:
210 case LTC2945_MAX_SENSE_H
:
211 case LTC2945_MIN_SENSE_H
:
212 case LTC2945_MAX_SENSE_THRES_H
:
213 case LTC2945_MIN_SENSE_THRES_H
:
215 * 25 uV resolution. Convert to current as measured with
216 * an 1 mOhm sense resistor, in mA. If a different sense
217 * resistor is installed, calculate the actual current by
218 * dividing the reported current by the sense resistor value
221 val
= DIV_ROUND_CLOSEST(val
, 25);
229 static ssize_t
ltc2945_show_value(struct device
*dev
,
230 struct device_attribute
*da
, char *buf
)
232 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
235 value
= ltc2945_reg_to_val(dev
, attr
->index
);
238 return snprintf(buf
, PAGE_SIZE
, "%lld\n", value
);
241 static ssize_t
ltc2945_set_value(struct device
*dev
,
242 struct device_attribute
*da
,
243 const char *buf
, size_t count
)
245 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
246 struct regmap
*regmap
= dev_get_drvdata(dev
);
247 u8 reg
= attr
->index
;
254 ret
= kstrtoul(buf
, 10, &val
);
258 /* convert to register value, then clamp and write result */
259 regval
= ltc2945_val_to_reg(dev
, reg
, val
);
260 if (is_power_reg(reg
)) {
261 regval
= clamp_val(regval
, 0, 0xffffff);
262 regbuf
[0] = regval
>> 16;
263 regbuf
[1] = (regval
>> 8) & 0xff;
267 regval
= clamp_val(regval
, 0, 0xfff) << 4;
268 regbuf
[0] = regval
>> 8;
269 regbuf
[1] = regval
& 0xff;
272 ret
= regmap_bulk_write(regmap
, reg
, regbuf
, num_regs
);
273 return ret
< 0 ? ret
: count
;
276 static ssize_t
ltc2945_reset_history(struct device
*dev
,
277 struct device_attribute
*da
,
278 const char *buf
, size_t count
)
280 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
281 struct regmap
*regmap
= dev_get_drvdata(dev
);
282 u8 reg
= attr
->index
;
283 int num_regs
= is_power_reg(reg
) ? 3 : 2;
284 u8 buf_min
[3] = { 0xff, 0xff, 0xff };
285 u8 buf_max
[3] = { 0, 0, 0 };
289 ret
= kstrtoul(buf
, 10, &val
);
295 ret
= regmap_update_bits(regmap
, LTC2945_CONTROL
, CONTROL_TEST_MODE
,
299 ret
= regmap_bulk_write(regmap
, reg
, buf_min
, num_regs
);
304 case LTC2945_MIN_POWER_H
:
305 reg
= LTC2945_MAX_POWER_H
;
307 case LTC2945_MIN_SENSE_H
:
308 reg
= LTC2945_MAX_SENSE_H
;
310 case LTC2945_MIN_VIN_H
:
311 reg
= LTC2945_MAX_VIN_H
;
313 case LTC2945_MIN_ADIN_H
:
314 reg
= LTC2945_MAX_ADIN_H
;
317 WARN_ONCE(1, "Bad register: 0x%x\n", reg
);
321 ret
= regmap_bulk_write(regmap
, reg
, buf_max
, num_regs
);
323 /* Try resetting test mode even if there was an error */
324 regmap_update_bits(regmap
, LTC2945_CONTROL
, CONTROL_TEST_MODE
, 0);
326 return ret
? : count
;
329 static ssize_t
ltc2945_show_bool(struct device
*dev
,
330 struct device_attribute
*da
, char *buf
)
332 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
333 struct regmap
*regmap
= dev_get_drvdata(dev
);
337 ret
= regmap_read(regmap
, LTC2945_FAULT
, &fault
);
341 fault
&= attr
->index
;
342 if (fault
) /* Clear reported faults in chip register */
343 regmap_update_bits(regmap
, LTC2945_FAULT
, attr
->index
, 0);
345 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!fault
);
350 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, ltc2945_show_value
, NULL
,
352 static SENSOR_DEVICE_ATTR(in1_min
, S_IRUGO
| S_IWUSR
, ltc2945_show_value
,
353 ltc2945_set_value
, LTC2945_MIN_VIN_THRES_H
);
354 static SENSOR_DEVICE_ATTR(in1_max
, S_IRUGO
| S_IWUSR
, ltc2945_show_value
,
355 ltc2945_set_value
, LTC2945_MAX_VIN_THRES_H
);
356 static SENSOR_DEVICE_ATTR(in1_lowest
, S_IRUGO
, ltc2945_show_value
, NULL
,
358 static SENSOR_DEVICE_ATTR(in1_highest
, S_IRUGO
, ltc2945_show_value
, NULL
,
360 static SENSOR_DEVICE_ATTR(in1_reset_history
, S_IWUSR
, NULL
,
361 ltc2945_reset_history
, LTC2945_MIN_VIN_H
);
363 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, ltc2945_show_value
, NULL
,
365 static SENSOR_DEVICE_ATTR(in2_min
, S_IRUGO
| S_IWUSR
, ltc2945_show_value
,
366 ltc2945_set_value
, LTC2945_MIN_ADIN_THRES_H
);
367 static SENSOR_DEVICE_ATTR(in2_max
, S_IRUGO
| S_IWUSR
, ltc2945_show_value
,
368 ltc2945_set_value
, LTC2945_MAX_ADIN_THRES_H
);
369 static SENSOR_DEVICE_ATTR(in2_lowest
, S_IRUGO
, ltc2945_show_value
, NULL
,
371 static SENSOR_DEVICE_ATTR(in2_highest
, S_IRUGO
, ltc2945_show_value
, NULL
,
373 static SENSOR_DEVICE_ATTR(in2_reset_history
, S_IWUSR
, NULL
,
374 ltc2945_reset_history
, LTC2945_MIN_ADIN_H
);
378 static SENSOR_DEVICE_ATTR(in1_min_alarm
, S_IRUGO
, ltc2945_show_bool
, NULL
,
380 static SENSOR_DEVICE_ATTR(in1_max_alarm
, S_IRUGO
, ltc2945_show_bool
, NULL
,
382 static SENSOR_DEVICE_ATTR(in2_min_alarm
, S_IRUGO
, ltc2945_show_bool
, NULL
,
384 static SENSOR_DEVICE_ATTR(in2_max_alarm
, S_IRUGO
, ltc2945_show_bool
, NULL
,
387 /* Currents (via sense resistor) */
389 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, ltc2945_show_value
, NULL
,
391 static SENSOR_DEVICE_ATTR(curr1_min
, S_IRUGO
| S_IWUSR
, ltc2945_show_value
,
392 ltc2945_set_value
, LTC2945_MIN_SENSE_THRES_H
);
393 static SENSOR_DEVICE_ATTR(curr1_max
, S_IRUGO
| S_IWUSR
, ltc2945_show_value
,
394 ltc2945_set_value
, LTC2945_MAX_SENSE_THRES_H
);
395 static SENSOR_DEVICE_ATTR(curr1_lowest
, S_IRUGO
, ltc2945_show_value
, NULL
,
396 LTC2945_MIN_SENSE_H
);
397 static SENSOR_DEVICE_ATTR(curr1_highest
, S_IRUGO
, ltc2945_show_value
, NULL
,
398 LTC2945_MAX_SENSE_H
);
399 static SENSOR_DEVICE_ATTR(curr1_reset_history
, S_IWUSR
, NULL
,
400 ltc2945_reset_history
, LTC2945_MIN_SENSE_H
);
404 static SENSOR_DEVICE_ATTR(curr1_min_alarm
, S_IRUGO
, ltc2945_show_bool
, NULL
,
406 static SENSOR_DEVICE_ATTR(curr1_max_alarm
, S_IRUGO
, ltc2945_show_bool
, NULL
,
411 static SENSOR_DEVICE_ATTR(power1_input
, S_IRUGO
, ltc2945_show_value
, NULL
,
413 static SENSOR_DEVICE_ATTR(power1_min
, S_IRUGO
| S_IWUSR
, ltc2945_show_value
,
414 ltc2945_set_value
, LTC2945_MIN_POWER_THRES_H
);
415 static SENSOR_DEVICE_ATTR(power1_max
, S_IRUGO
| S_IWUSR
, ltc2945_show_value
,
416 ltc2945_set_value
, LTC2945_MAX_POWER_THRES_H
);
417 static SENSOR_DEVICE_ATTR(power1_input_lowest
, S_IRUGO
, ltc2945_show_value
,
418 NULL
, LTC2945_MIN_POWER_H
);
419 static SENSOR_DEVICE_ATTR(power1_input_highest
, S_IRUGO
, ltc2945_show_value
,
420 NULL
, LTC2945_MAX_POWER_H
);
421 static SENSOR_DEVICE_ATTR(power1_reset_history
, S_IWUSR
, NULL
,
422 ltc2945_reset_history
, LTC2945_MIN_POWER_H
);
426 static SENSOR_DEVICE_ATTR(power1_min_alarm
, S_IRUGO
, ltc2945_show_bool
, NULL
,
428 static SENSOR_DEVICE_ATTR(power1_max_alarm
, S_IRUGO
, ltc2945_show_bool
, NULL
,
431 static struct attribute
*ltc2945_attrs
[] = {
432 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
433 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
434 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
435 &sensor_dev_attr_in1_lowest
.dev_attr
.attr
,
436 &sensor_dev_attr_in1_highest
.dev_attr
.attr
,
437 &sensor_dev_attr_in1_reset_history
.dev_attr
.attr
,
438 &sensor_dev_attr_in1_min_alarm
.dev_attr
.attr
,
439 &sensor_dev_attr_in1_max_alarm
.dev_attr
.attr
,
441 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
442 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
443 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
444 &sensor_dev_attr_in2_lowest
.dev_attr
.attr
,
445 &sensor_dev_attr_in2_highest
.dev_attr
.attr
,
446 &sensor_dev_attr_in2_reset_history
.dev_attr
.attr
,
447 &sensor_dev_attr_in2_min_alarm
.dev_attr
.attr
,
448 &sensor_dev_attr_in2_max_alarm
.dev_attr
.attr
,
450 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
451 &sensor_dev_attr_curr1_min
.dev_attr
.attr
,
452 &sensor_dev_attr_curr1_max
.dev_attr
.attr
,
453 &sensor_dev_attr_curr1_lowest
.dev_attr
.attr
,
454 &sensor_dev_attr_curr1_highest
.dev_attr
.attr
,
455 &sensor_dev_attr_curr1_reset_history
.dev_attr
.attr
,
456 &sensor_dev_attr_curr1_min_alarm
.dev_attr
.attr
,
457 &sensor_dev_attr_curr1_max_alarm
.dev_attr
.attr
,
459 &sensor_dev_attr_power1_input
.dev_attr
.attr
,
460 &sensor_dev_attr_power1_min
.dev_attr
.attr
,
461 &sensor_dev_attr_power1_max
.dev_attr
.attr
,
462 &sensor_dev_attr_power1_input_lowest
.dev_attr
.attr
,
463 &sensor_dev_attr_power1_input_highest
.dev_attr
.attr
,
464 &sensor_dev_attr_power1_reset_history
.dev_attr
.attr
,
465 &sensor_dev_attr_power1_min_alarm
.dev_attr
.attr
,
466 &sensor_dev_attr_power1_max_alarm
.dev_attr
.attr
,
470 ATTRIBUTE_GROUPS(ltc2945
);
472 static const struct regmap_config ltc2945_regmap_config
= {
475 .max_register
= LTC2945_MIN_ADIN_THRES_L
,
478 static int ltc2945_probe(struct i2c_client
*client
,
479 const struct i2c_device_id
*id
)
481 struct device
*dev
= &client
->dev
;
482 struct device
*hwmon_dev
;
483 struct regmap
*regmap
;
485 regmap
= devm_regmap_init_i2c(client
, <c2945_regmap_config
);
486 if (IS_ERR(regmap
)) {
487 dev_err(dev
, "failed to allocate register map\n");
488 return PTR_ERR(regmap
);
492 regmap_write(regmap
, LTC2945_FAULT
, 0x00);
494 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
497 return PTR_ERR_OR_ZERO(hwmon_dev
);
500 static const struct i2c_device_id ltc2945_id
[] = {
505 MODULE_DEVICE_TABLE(i2c
, ltc2945_id
);
507 static struct i2c_driver ltc2945_driver
= {
511 .probe
= ltc2945_probe
,
512 .id_table
= ltc2945_id
,
515 module_i2c_driver(ltc2945_driver
);
517 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
518 MODULE_DESCRIPTION("LTC2945 driver");
519 MODULE_LICENSE("GPL");