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 const struct of_device_id __maybe_unused ltc2945_of_match[] = {
62 { .compatible = "adi,ltc2945" },
65 MODULE_DEVICE_TABLE(of, ltc2945_of_match);
68 * struct ltc2945_data - LTC2945 device data
69 * @regmap: regmap device
70 * @shunt_resistor: shunt resistor value in micro ohms (1000 by default)
73 struct regmap *regmap;
77 static inline bool is_power_reg(u8 reg)
79 return reg < LTC2945_SENSE_H;
82 /* Return the value from the given register in uW, mV, or mA */
83 static long long ltc2945_reg_to_val(struct device *dev, u8 reg)
85 struct ltc2945_data *data = dev_get_drvdata(dev);
86 struct regmap *regmap = data->regmap;
87 u32 shunt_resistor = data->shunt_resistor;
93 ret = regmap_bulk_read(regmap, reg, buf,
94 is_power_reg(reg) ? 3 : 2);
98 if (is_power_reg(reg)) {
100 val = (buf[0] << 16) + (buf[1] << 8) + buf[2];
102 /* 12-bit current, voltage */
103 val = (buf[0] << 4) + (buf[1] >> 4);
107 case LTC2945_POWER_H:
108 case LTC2945_MAX_POWER_H:
109 case LTC2945_MIN_POWER_H:
110 case LTC2945_MAX_POWER_THRES_H:
111 case LTC2945_MIN_POWER_THRES_H:
114 * Control register bit 0 selects if voltage at SENSE+/VDD
115 * or voltage at ADIN is used to measure power.
117 ret = regmap_read(regmap, LTC2945_CONTROL, &control);
120 if (control & CONTROL_MULT_SELECT) {
121 /* 25 mV * 25 uV = 0.625 uV resolution. */
124 /* 0.5 mV * 25 uV = 0.0125 uV resolution. */
125 val = (val * 25LL) >> 1;
128 /* Overflow check: Assuming max 24-bit power, val is at most 53 bits right now. */
129 val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor);
131 * Overflow check: After division, depending on shunt resistor,
132 * val can still be > 32 bits so returning long long makes sense
137 case LTC2945_MAX_VIN_H:
138 case LTC2945_MIN_VIN_H:
139 case LTC2945_MAX_VIN_THRES_H:
140 case LTC2945_MIN_VIN_THRES_H:
141 /* 25 mV resolution. Convert to mV. */
145 case LTC2945_MAX_ADIN_H:
146 case LTC2945_MIN_ADIN_THRES_H:
147 case LTC2945_MAX_ADIN_THRES_H:
148 case LTC2945_MIN_ADIN_H:
149 /* 0.5mV resolution. Convert to mV. */
152 case LTC2945_SENSE_H:
153 case LTC2945_MAX_SENSE_H:
154 case LTC2945_MIN_SENSE_H:
155 case LTC2945_MAX_SENSE_THRES_H:
156 case LTC2945_MIN_SENSE_THRES_H:
157 /* 25 uV resolution. Convert to mA. */
159 /* Overflow check: Assuming max 12-bit sense, val is at most 27 bits right now */
160 val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor);
161 /* Overflow check: After division, <= 27 bits */
169 static long long ltc2945_val_to_reg(struct device *dev, u8 reg,
170 unsigned long long val)
172 struct ltc2945_data *data = dev_get_drvdata(dev);
173 struct regmap *regmap = data->regmap;
174 u32 shunt_resistor = data->shunt_resistor;
175 unsigned int control;
178 /* Ensure we don't overflow */
179 val = clamp_val(val, 0, U32_MAX);
182 case LTC2945_POWER_H:
183 case LTC2945_MAX_POWER_H:
184 case LTC2945_MIN_POWER_H:
185 case LTC2945_MAX_POWER_THRES_H:
186 case LTC2945_MIN_POWER_THRES_H:
188 * Control register bit 0 selects if voltage at SENSE+/VDD
189 * or voltage at ADIN is used to measure power, which in turn
190 * determines register calculations.
192 ret = regmap_read(regmap, LTC2945_CONTROL, &control);
195 if (control & CONTROL_MULT_SELECT) {
196 /* 25 mV * 25 uV = 0.625 uV resolution. */
197 val *= shunt_resistor;
198 /* Overflow check: Assuming 32-bit val and shunt resistor, val <= 64bits */
199 val = DIV_ROUND_CLOSEST_ULL(val, 625 * 1000);
200 /* Overflow check: val is now <= 44 bits */
202 /* 0.5 mV * 25 uV = 0.0125 uV resolution. */
203 val *= shunt_resistor;
204 /* Overflow check: Assuming 32-bit val and shunt resistor, val <= 64bits */
205 val = DIV_ROUND_CLOSEST_ULL(val, 25 * 1000) * 2;
206 /* Overflow check: val is now <= 51 bits */
210 case LTC2945_MAX_VIN_H:
211 case LTC2945_MIN_VIN_H:
212 case LTC2945_MAX_VIN_THRES_H:
213 case LTC2945_MIN_VIN_THRES_H:
214 /* 25 mV resolution. */
215 val = DIV_ROUND_CLOSEST_ULL(val, 25);
218 case LTC2945_MAX_ADIN_H:
219 case LTC2945_MIN_ADIN_THRES_H:
220 case LTC2945_MAX_ADIN_THRES_H:
221 case LTC2945_MIN_ADIN_H:
222 /* 0.5mV resolution. */
225 case LTC2945_SENSE_H:
226 case LTC2945_MAX_SENSE_H:
227 case LTC2945_MIN_SENSE_H:
228 case LTC2945_MAX_SENSE_THRES_H:
229 case LTC2945_MIN_SENSE_THRES_H:
230 /* 25 uV resolution. Convert to mA. */
231 val *= shunt_resistor;
232 /* Overflow check: Assuming 32-bit val and 32-bit shunt resistor, val is 64bits */
233 val = DIV_ROUND_CLOSEST_ULL(val, 25 * 1000);
234 /* Overflow check: val is now <= 50 bits */
242 static ssize_t ltc2945_value_show(struct device *dev,
243 struct device_attribute *da, char *buf)
245 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
248 value = ltc2945_reg_to_val(dev, attr->index);
251 return sysfs_emit(buf, "%lld\n", value);
254 static ssize_t ltc2945_value_store(struct device *dev,
255 struct device_attribute *da,
256 const char *buf, size_t count)
258 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
259 struct ltc2945_data *data = dev_get_drvdata(dev);
260 struct regmap *regmap = data->regmap;
261 u8 reg = attr->index;
268 ret = kstrtouint(buf, 10, &val);
272 /* convert to register value, then clamp and write result */
273 regval = ltc2945_val_to_reg(dev, reg, val);
276 if (is_power_reg(reg)) {
277 regval = clamp_val(regval, 0, 0xffffff);
278 regbuf[0] = regval >> 16;
279 regbuf[1] = (regval >> 8) & 0xff;
283 regval = clamp_val(regval, 0, 0xfff) << 4;
284 regbuf[0] = regval >> 8;
285 regbuf[1] = regval & 0xff;
288 ret = regmap_bulk_write(regmap, reg, regbuf, num_regs);
289 return ret < 0 ? ret : count;
292 static ssize_t ltc2945_history_store(struct device *dev,
293 struct device_attribute *da,
294 const char *buf, size_t count)
296 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
297 struct ltc2945_data *data = dev_get_drvdata(dev);
298 struct regmap *regmap = data->regmap;
299 u8 reg = attr->index;
300 int num_regs = is_power_reg(reg) ? 3 : 2;
301 u8 buf_min[3] = { 0xff, 0xff, 0xff };
302 u8 buf_max[3] = { 0, 0, 0 };
306 ret = kstrtoul(buf, 10, &val);
312 ret = regmap_update_bits(regmap, LTC2945_CONTROL, CONTROL_TEST_MODE,
316 ret = regmap_bulk_write(regmap, reg, buf_min, num_regs);
321 case LTC2945_MIN_POWER_H:
322 reg = LTC2945_MAX_POWER_H;
324 case LTC2945_MIN_SENSE_H:
325 reg = LTC2945_MAX_SENSE_H;
327 case LTC2945_MIN_VIN_H:
328 reg = LTC2945_MAX_VIN_H;
330 case LTC2945_MIN_ADIN_H:
331 reg = LTC2945_MAX_ADIN_H;
334 WARN_ONCE(1, "Bad register: 0x%x\n", reg);
338 ret = regmap_bulk_write(regmap, reg, buf_max, num_regs);
340 /* Try resetting test mode even if there was an error */
341 regmap_update_bits(regmap, LTC2945_CONTROL, CONTROL_TEST_MODE, 0);
343 return ret ? : count;
346 static ssize_t ltc2945_bool_show(struct device *dev,
347 struct device_attribute *da, char *buf)
349 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
350 struct ltc2945_data *data = dev_get_drvdata(dev);
351 struct regmap *regmap = data->regmap;
355 ret = regmap_read(regmap, LTC2945_FAULT, &fault);
359 fault &= attr->index;
360 if (fault) /* Clear reported faults in chip register */
361 regmap_update_bits(regmap, LTC2945_FAULT, attr->index, 0);
363 return sysfs_emit(buf, "%d\n", !!fault);
368 static SENSOR_DEVICE_ATTR_RO(in1_input, ltc2945_value, LTC2945_VIN_H);
369 static SENSOR_DEVICE_ATTR_RW(in1_min, ltc2945_value, LTC2945_MIN_VIN_THRES_H);
370 static SENSOR_DEVICE_ATTR_RW(in1_max, ltc2945_value, LTC2945_MAX_VIN_THRES_H);
371 static SENSOR_DEVICE_ATTR_RO(in1_lowest, ltc2945_value, LTC2945_MIN_VIN_H);
372 static SENSOR_DEVICE_ATTR_RO(in1_highest, ltc2945_value, LTC2945_MAX_VIN_H);
373 static SENSOR_DEVICE_ATTR_WO(in1_reset_history, ltc2945_history,
376 static SENSOR_DEVICE_ATTR_RO(in2_input, ltc2945_value, LTC2945_ADIN_H);
377 static SENSOR_DEVICE_ATTR_RW(in2_min, ltc2945_value, LTC2945_MIN_ADIN_THRES_H);
378 static SENSOR_DEVICE_ATTR_RW(in2_max, ltc2945_value, LTC2945_MAX_ADIN_THRES_H);
379 static SENSOR_DEVICE_ATTR_RO(in2_lowest, ltc2945_value, LTC2945_MIN_ADIN_H);
380 static SENSOR_DEVICE_ATTR_RO(in2_highest, ltc2945_value, LTC2945_MAX_ADIN_H);
381 static SENSOR_DEVICE_ATTR_WO(in2_reset_history, ltc2945_history,
386 static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc2945_bool, FAULT_VIN_UV);
387 static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc2945_bool, FAULT_VIN_OV);
388 static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc2945_bool, FAULT_ADIN_UV);
389 static SENSOR_DEVICE_ATTR_RO(in2_max_alarm, ltc2945_bool, FAULT_ADIN_OV);
391 /* Currents (via sense resistor) */
393 static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc2945_value, LTC2945_SENSE_H);
394 static SENSOR_DEVICE_ATTR_RW(curr1_min, ltc2945_value,
395 LTC2945_MIN_SENSE_THRES_H);
396 static SENSOR_DEVICE_ATTR_RW(curr1_max, ltc2945_value,
397 LTC2945_MAX_SENSE_THRES_H);
398 static SENSOR_DEVICE_ATTR_RO(curr1_lowest, ltc2945_value, LTC2945_MIN_SENSE_H);
399 static SENSOR_DEVICE_ATTR_RO(curr1_highest, ltc2945_value,
400 LTC2945_MAX_SENSE_H);
401 static SENSOR_DEVICE_ATTR_WO(curr1_reset_history, ltc2945_history,
402 LTC2945_MIN_SENSE_H);
406 static SENSOR_DEVICE_ATTR_RO(curr1_min_alarm, ltc2945_bool, FAULT_SENSE_UV);
407 static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc2945_bool, FAULT_SENSE_OV);
411 static SENSOR_DEVICE_ATTR_RO(power1_input, ltc2945_value, LTC2945_POWER_H);
412 static SENSOR_DEVICE_ATTR_RW(power1_min, ltc2945_value,
413 LTC2945_MIN_POWER_THRES_H);
414 static SENSOR_DEVICE_ATTR_RW(power1_max, ltc2945_value,
415 LTC2945_MAX_POWER_THRES_H);
416 static SENSOR_DEVICE_ATTR_RO(power1_input_lowest, ltc2945_value,
417 LTC2945_MIN_POWER_H);
418 static SENSOR_DEVICE_ATTR_RO(power1_input_highest, ltc2945_value,
419 LTC2945_MAX_POWER_H);
420 static SENSOR_DEVICE_ATTR_WO(power1_reset_history, ltc2945_history,
421 LTC2945_MIN_POWER_H);
425 static SENSOR_DEVICE_ATTR_RO(power1_min_alarm, ltc2945_bool, FAULT_POWER_UV);
426 static SENSOR_DEVICE_ATTR_RO(power1_max_alarm, ltc2945_bool, FAULT_POWER_OV);
428 static struct attribute *ltc2945_attrs[] = {
429 &sensor_dev_attr_in1_input.dev_attr.attr,
430 &sensor_dev_attr_in1_min.dev_attr.attr,
431 &sensor_dev_attr_in1_max.dev_attr.attr,
432 &sensor_dev_attr_in1_lowest.dev_attr.attr,
433 &sensor_dev_attr_in1_highest.dev_attr.attr,
434 &sensor_dev_attr_in1_reset_history.dev_attr.attr,
435 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
436 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
438 &sensor_dev_attr_in2_input.dev_attr.attr,
439 &sensor_dev_attr_in2_min.dev_attr.attr,
440 &sensor_dev_attr_in2_max.dev_attr.attr,
441 &sensor_dev_attr_in2_lowest.dev_attr.attr,
442 &sensor_dev_attr_in2_highest.dev_attr.attr,
443 &sensor_dev_attr_in2_reset_history.dev_attr.attr,
444 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
445 &sensor_dev_attr_in2_max_alarm.dev_attr.attr,
447 &sensor_dev_attr_curr1_input.dev_attr.attr,
448 &sensor_dev_attr_curr1_min.dev_attr.attr,
449 &sensor_dev_attr_curr1_max.dev_attr.attr,
450 &sensor_dev_attr_curr1_lowest.dev_attr.attr,
451 &sensor_dev_attr_curr1_highest.dev_attr.attr,
452 &sensor_dev_attr_curr1_reset_history.dev_attr.attr,
453 &sensor_dev_attr_curr1_min_alarm.dev_attr.attr,
454 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
456 &sensor_dev_attr_power1_input.dev_attr.attr,
457 &sensor_dev_attr_power1_min.dev_attr.attr,
458 &sensor_dev_attr_power1_max.dev_attr.attr,
459 &sensor_dev_attr_power1_input_lowest.dev_attr.attr,
460 &sensor_dev_attr_power1_input_highest.dev_attr.attr,
461 &sensor_dev_attr_power1_reset_history.dev_attr.attr,
462 &sensor_dev_attr_power1_min_alarm.dev_attr.attr,
463 &sensor_dev_attr_power1_max_alarm.dev_attr.attr,
467 ATTRIBUTE_GROUPS(ltc2945);
469 static const struct regmap_config ltc2945_regmap_config = {
472 .max_register = LTC2945_MIN_ADIN_THRES_L,
475 static int ltc2945_probe(struct i2c_client *client)
477 struct device *dev = &client->dev;
478 struct device *hwmon_dev;
479 struct regmap *regmap;
480 struct ltc2945_data *data;
482 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
485 dev_set_drvdata(dev, data);
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);
493 data->regmap = regmap;
494 if (device_property_read_u32(dev, "shunt-resistor-micro-ohms",
495 &data->shunt_resistor))
496 data->shunt_resistor = 1000;
498 if (data->shunt_resistor == 0)
502 regmap_write(regmap, LTC2945_FAULT, 0x00);
504 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
507 return PTR_ERR_OR_ZERO(hwmon_dev);
510 static const struct i2c_device_id ltc2945_id[] = {
515 MODULE_DEVICE_TABLE(i2c, ltc2945_id);
517 static struct i2c_driver ltc2945_driver = {
520 .of_match_table = of_match_ptr(ltc2945_of_match),
522 .probe = ltc2945_probe,
523 .id_table = ltc2945_id,
526 module_i2c_driver(ltc2945_driver);
528 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
529 MODULE_DESCRIPTION("LTC2945 driver");
530 MODULE_LICENSE("GPL");