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);
262 if (is_power_reg(reg)) {
263 regval = clamp_val(regval, 0, 0xffffff);
264 regbuf[0] = regval >> 16;
265 regbuf[1] = (regval >> 8) & 0xff;
269 regval = clamp_val(regval, 0, 0xfff) << 4;
270 regbuf[0] = regval >> 8;
271 regbuf[1] = regval & 0xff;
274 ret = regmap_bulk_write(regmap, reg, regbuf, num_regs);
275 return ret < 0 ? ret : count;
278 static ssize_t ltc2945_reset_history(struct device *dev,
279 struct device_attribute *da,
280 const char *buf, size_t count)
282 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
283 struct regmap *regmap = dev_get_drvdata(dev);
284 u8 reg = attr->index;
285 int num_regs = is_power_reg(reg) ? 3 : 2;
286 u8 buf_min[3] = { 0xff, 0xff, 0xff };
287 u8 buf_max[3] = { 0, 0, 0 };
291 ret = kstrtoul(buf, 10, &val);
297 ret = regmap_update_bits(regmap, LTC2945_CONTROL, CONTROL_TEST_MODE,
301 ret = regmap_bulk_write(regmap, reg, buf_min, num_regs);
306 case LTC2945_MIN_POWER_H:
307 reg = LTC2945_MAX_POWER_H;
309 case LTC2945_MIN_SENSE_H:
310 reg = LTC2945_MAX_SENSE_H;
312 case LTC2945_MIN_VIN_H:
313 reg = LTC2945_MAX_VIN_H;
315 case LTC2945_MIN_ADIN_H:
316 reg = LTC2945_MAX_ADIN_H;
319 WARN_ONCE(1, "Bad register: 0x%x\n", reg);
323 ret = regmap_bulk_write(regmap, reg, buf_max, num_regs);
325 /* Try resetting test mode even if there was an error */
326 regmap_update_bits(regmap, LTC2945_CONTROL, CONTROL_TEST_MODE, 0);
328 return ret ? : count;
331 static ssize_t ltc2945_show_bool(struct device *dev,
332 struct device_attribute *da, char *buf)
334 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
335 struct regmap *regmap = dev_get_drvdata(dev);
339 ret = regmap_read(regmap, LTC2945_FAULT, &fault);
343 fault &= attr->index;
344 if (fault) /* Clear reported faults in chip register */
345 regmap_update_bits(regmap, LTC2945_FAULT, attr->index, 0);
347 return snprintf(buf, PAGE_SIZE, "%d\n", !!fault);
352 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc2945_show_value, NULL,
354 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR, ltc2945_show_value,
355 ltc2945_set_value, LTC2945_MIN_VIN_THRES_H);
356 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR, ltc2945_show_value,
357 ltc2945_set_value, LTC2945_MAX_VIN_THRES_H);
358 static SENSOR_DEVICE_ATTR(in1_lowest, S_IRUGO, ltc2945_show_value, NULL,
360 static SENSOR_DEVICE_ATTR(in1_highest, S_IRUGO, ltc2945_show_value, NULL,
362 static SENSOR_DEVICE_ATTR(in1_reset_history, S_IWUSR, NULL,
363 ltc2945_reset_history, LTC2945_MIN_VIN_H);
365 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc2945_show_value, NULL,
367 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR, ltc2945_show_value,
368 ltc2945_set_value, LTC2945_MIN_ADIN_THRES_H);
369 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR, ltc2945_show_value,
370 ltc2945_set_value, LTC2945_MAX_ADIN_THRES_H);
371 static SENSOR_DEVICE_ATTR(in2_lowest, S_IRUGO, ltc2945_show_value, NULL,
373 static SENSOR_DEVICE_ATTR(in2_highest, S_IRUGO, ltc2945_show_value, NULL,
375 static SENSOR_DEVICE_ATTR(in2_reset_history, S_IWUSR, NULL,
376 ltc2945_reset_history, LTC2945_MIN_ADIN_H);
380 static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ltc2945_show_bool, NULL,
382 static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ltc2945_show_bool, NULL,
384 static SENSOR_DEVICE_ATTR(in2_min_alarm, S_IRUGO, ltc2945_show_bool, NULL,
386 static SENSOR_DEVICE_ATTR(in2_max_alarm, S_IRUGO, ltc2945_show_bool, NULL,
389 /* Currents (via sense resistor) */
391 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc2945_show_value, NULL,
393 static SENSOR_DEVICE_ATTR(curr1_min, S_IRUGO | S_IWUSR, ltc2945_show_value,
394 ltc2945_set_value, LTC2945_MIN_SENSE_THRES_H);
395 static SENSOR_DEVICE_ATTR(curr1_max, S_IRUGO | S_IWUSR, ltc2945_show_value,
396 ltc2945_set_value, LTC2945_MAX_SENSE_THRES_H);
397 static SENSOR_DEVICE_ATTR(curr1_lowest, S_IRUGO, ltc2945_show_value, NULL,
398 LTC2945_MIN_SENSE_H);
399 static SENSOR_DEVICE_ATTR(curr1_highest, S_IRUGO, ltc2945_show_value, NULL,
400 LTC2945_MAX_SENSE_H);
401 static SENSOR_DEVICE_ATTR(curr1_reset_history, S_IWUSR, NULL,
402 ltc2945_reset_history, LTC2945_MIN_SENSE_H);
406 static SENSOR_DEVICE_ATTR(curr1_min_alarm, S_IRUGO, ltc2945_show_bool, NULL,
408 static SENSOR_DEVICE_ATTR(curr1_max_alarm, S_IRUGO, ltc2945_show_bool, NULL,
413 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc2945_show_value, NULL,
415 static SENSOR_DEVICE_ATTR(power1_min, S_IRUGO | S_IWUSR, ltc2945_show_value,
416 ltc2945_set_value, LTC2945_MIN_POWER_THRES_H);
417 static SENSOR_DEVICE_ATTR(power1_max, S_IRUGO | S_IWUSR, ltc2945_show_value,
418 ltc2945_set_value, LTC2945_MAX_POWER_THRES_H);
419 static SENSOR_DEVICE_ATTR(power1_input_lowest, S_IRUGO, ltc2945_show_value,
420 NULL, LTC2945_MIN_POWER_H);
421 static SENSOR_DEVICE_ATTR(power1_input_highest, S_IRUGO, ltc2945_show_value,
422 NULL, LTC2945_MAX_POWER_H);
423 static SENSOR_DEVICE_ATTR(power1_reset_history, S_IWUSR, NULL,
424 ltc2945_reset_history, LTC2945_MIN_POWER_H);
428 static SENSOR_DEVICE_ATTR(power1_min_alarm, S_IRUGO, ltc2945_show_bool, NULL,
430 static SENSOR_DEVICE_ATTR(power1_max_alarm, S_IRUGO, ltc2945_show_bool, NULL,
433 static struct attribute *ltc2945_attrs[] = {
434 &sensor_dev_attr_in1_input.dev_attr.attr,
435 &sensor_dev_attr_in1_min.dev_attr.attr,
436 &sensor_dev_attr_in1_max.dev_attr.attr,
437 &sensor_dev_attr_in1_lowest.dev_attr.attr,
438 &sensor_dev_attr_in1_highest.dev_attr.attr,
439 &sensor_dev_attr_in1_reset_history.dev_attr.attr,
440 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
441 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
443 &sensor_dev_attr_in2_input.dev_attr.attr,
444 &sensor_dev_attr_in2_min.dev_attr.attr,
445 &sensor_dev_attr_in2_max.dev_attr.attr,
446 &sensor_dev_attr_in2_lowest.dev_attr.attr,
447 &sensor_dev_attr_in2_highest.dev_attr.attr,
448 &sensor_dev_attr_in2_reset_history.dev_attr.attr,
449 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
450 &sensor_dev_attr_in2_max_alarm.dev_attr.attr,
452 &sensor_dev_attr_curr1_input.dev_attr.attr,
453 &sensor_dev_attr_curr1_min.dev_attr.attr,
454 &sensor_dev_attr_curr1_max.dev_attr.attr,
455 &sensor_dev_attr_curr1_lowest.dev_attr.attr,
456 &sensor_dev_attr_curr1_highest.dev_attr.attr,
457 &sensor_dev_attr_curr1_reset_history.dev_attr.attr,
458 &sensor_dev_attr_curr1_min_alarm.dev_attr.attr,
459 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
461 &sensor_dev_attr_power1_input.dev_attr.attr,
462 &sensor_dev_attr_power1_min.dev_attr.attr,
463 &sensor_dev_attr_power1_max.dev_attr.attr,
464 &sensor_dev_attr_power1_input_lowest.dev_attr.attr,
465 &sensor_dev_attr_power1_input_highest.dev_attr.attr,
466 &sensor_dev_attr_power1_reset_history.dev_attr.attr,
467 &sensor_dev_attr_power1_min_alarm.dev_attr.attr,
468 &sensor_dev_attr_power1_max_alarm.dev_attr.attr,
472 ATTRIBUTE_GROUPS(ltc2945);
474 static const struct regmap_config ltc2945_regmap_config = {
477 .max_register = LTC2945_MIN_ADIN_THRES_L,
480 static int ltc2945_probe(struct i2c_client *client,
481 const struct i2c_device_id *id)
483 struct device *dev = &client->dev;
484 struct device *hwmon_dev;
485 struct regmap *regmap;
487 regmap = devm_regmap_init_i2c(client, <c2945_regmap_config);
488 if (IS_ERR(regmap)) {
489 dev_err(dev, "failed to allocate register map\n");
490 return PTR_ERR(regmap);
494 regmap_write(regmap, LTC2945_FAULT, 0x00);
496 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
499 return PTR_ERR_OR_ZERO(hwmon_dev);
502 static const struct i2c_device_id ltc2945_id[] = {
507 MODULE_DEVICE_TABLE(i2c, ltc2945_id);
509 static struct i2c_driver ltc2945_driver = {
513 .probe = ltc2945_probe,
514 .id_table = ltc2945_id,
517 module_i2c_driver(ltc2945_driver);
519 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
520 MODULE_DESCRIPTION("LTC2945 driver");
521 MODULE_LICENSE("GPL");