xref: /openbmc/linux/drivers/hwmon/ltc2945.c (revision f3539c12)
1 /*
2  * Driver for Linear Technology LTC2945 I2C Power Monitor
3  *
4  * Copyright (c) 2014 Guenter Roeck
5  *
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.
10  *
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.
15  */
16 
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>
26 
27 /* chip registers */
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
53 
54 /* Fault register bits */
55 
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)
64 
65 /* Control register bits */
66 
67 #define CONTROL_MULT_SELECT	(1 << 0)
68 #define CONTROL_TEST_MODE	(1 << 4)
69 
70 static inline bool is_power_reg(u8 reg)
71 {
72 	return reg < LTC2945_SENSE_H;
73 }
74 
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)
77 {
78 	struct regmap *regmap = dev_get_drvdata(dev);
79 	unsigned int control;
80 	u8 buf[3];
81 	long long val;
82 	int ret;
83 
84 	ret = regmap_bulk_read(regmap, reg, buf,
85 			       is_power_reg(reg) ? 3 : 2);
86 	if (ret < 0)
87 		return ret;
88 
89 	if (is_power_reg(reg)) {
90 		/* power */
91 		val = (buf[0] << 16) + (buf[1] << 8) + buf[2];
92 	} else {
93 		/* current, voltage */
94 		val = (buf[0] << 4) + (buf[1] >> 4);
95 	}
96 
97 	switch (reg) {
98 	case LTC2945_POWER_H:
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:
103 		/*
104 		 * Convert to uW by assuming current is measured with
105 		 * an 1mOhm sense resistor, similar to current
106 		 * measurements.
107 		 * Control register bit 0 selects if voltage at SENSE+/VDD
108 		 * or voltage at ADIN is used to measure power.
109 		 */
110 		ret = regmap_read(regmap, LTC2945_CONTROL, &control);
111 		if (ret < 0)
112 			return ret;
113 		if (control & CONTROL_MULT_SELECT) {
114 			/* 25 mV * 25 uV = 0.625 uV resolution. */
115 			val *= 625LL;
116 		} else {
117 			/* 0.5 mV * 25 uV = 0.0125 uV resolution. */
118 			val = (val * 25LL) >> 1;
119 		}
120 		break;
121 	case LTC2945_VIN_H:
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. */
127 		val *= 25;
128 		break;
129 	case LTC2945_ADIN_H:
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. */
135 		val = val >> 1;
136 		break;
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:
142 		/*
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
147 		 * in mOhm.
148 		 */
149 		val *= 25;
150 		break;
151 	default:
152 		return -EINVAL;
153 	}
154 	return val;
155 }
156 
157 static int ltc2945_val_to_reg(struct device *dev, u8 reg,
158 			      unsigned long val)
159 {
160 	struct regmap *regmap = dev_get_drvdata(dev);
161 	unsigned int control;
162 	int ret;
163 
164 	switch (reg) {
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:
170 		/*
171 		 * Convert to register value by assuming current is measured
172 		 * with an 1mOhm sense resistor, similar to current
173 		 * measurements.
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.
177 		 */
178 		ret = regmap_read(regmap, LTC2945_CONTROL, &control);
179 		if (ret < 0)
180 			return ret;
181 		if (control & CONTROL_MULT_SELECT) {
182 			/* 25 mV * 25 uV = 0.625 uV resolution. */
183 			val = DIV_ROUND_CLOSEST(val, 625);
184 		} else {
185 			/*
186 			 * 0.5 mV * 25 uV = 0.0125 uV resolution.
187 			 * Divide first to avoid overflow;
188 			 * accept loss of accuracy.
189 			 */
190 			val = DIV_ROUND_CLOSEST(val, 25) * 2;
191 		}
192 		break;
193 	case LTC2945_VIN_H:
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. */
199 		val /= 25;
200 		break;
201 	case LTC2945_ADIN_H:
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. */
207 		val *= 2;
208 		break;
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:
214 		/*
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
219 		 * in mOhm.
220 		 */
221 		val = DIV_ROUND_CLOSEST(val, 25);
222 		break;
223 	default:
224 		return -EINVAL;
225 	}
226 	return val;
227 }
228 
229 static ssize_t ltc2945_show_value(struct device *dev,
230 				  struct device_attribute *da, char *buf)
231 {
232 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
233 	long long value;
234 
235 	value = ltc2945_reg_to_val(dev, attr->index);
236 	if (value < 0)
237 		return value;
238 	return snprintf(buf, PAGE_SIZE, "%lld\n", value);
239 }
240 
241 static ssize_t ltc2945_set_value(struct device *dev,
242 				     struct device_attribute *da,
243 				     const char *buf, size_t count)
244 {
245 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
246 	struct regmap *regmap = dev_get_drvdata(dev);
247 	u8 reg = attr->index;
248 	unsigned long val;
249 	u8 regbuf[3];
250 	int num_regs;
251 	int regval;
252 	int ret;
253 
254 	ret = kstrtoul(buf, 10, &val);
255 	if (ret)
256 		return ret;
257 
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;
264 		regbuf[2] = regval;
265 		num_regs = 3;
266 	} else {
267 		regval = clamp_val(regval, 0, 0xfff) << 4;
268 		regbuf[0] = regval >> 8;
269 		regbuf[1] = regval & 0xff;
270 		num_regs = 2;
271 	}
272 	ret = regmap_bulk_write(regmap, reg, regbuf, num_regs);
273 	return ret < 0 ? ret : count;
274 }
275 
276 static ssize_t ltc2945_reset_history(struct device *dev,
277 				     struct device_attribute *da,
278 				     const char *buf, size_t count)
279 {
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 };
286 	unsigned long val;
287 	int ret;
288 
289 	ret = kstrtoul(buf, 10, &val);
290 	if (ret)
291 		return ret;
292 	if (val != 1)
293 		return -EINVAL;
294 
295 	ret = regmap_update_bits(regmap, LTC2945_CONTROL, CONTROL_TEST_MODE,
296 				 CONTROL_TEST_MODE);
297 
298 	/* Reset minimum */
299 	ret = regmap_bulk_write(regmap, reg, buf_min, num_regs);
300 	if (ret)
301 		return ret;
302 
303 	switch (reg) {
304 	case LTC2945_MIN_POWER_H:
305 		reg = LTC2945_MAX_POWER_H;
306 		break;
307 	case LTC2945_MIN_SENSE_H:
308 		reg = LTC2945_MAX_SENSE_H;
309 		break;
310 	case LTC2945_MIN_VIN_H:
311 		reg = LTC2945_MAX_VIN_H;
312 		break;
313 	case LTC2945_MIN_ADIN_H:
314 		reg = LTC2945_MAX_ADIN_H;
315 		break;
316 	default:
317 		WARN_ONCE(1, "Bad register: 0x%x\n", reg);
318 		return -EINVAL;
319 	}
320 	/* Reset maximum */
321 	ret = regmap_bulk_write(regmap, reg, buf_max, num_regs);
322 
323 	/* Try resetting test mode even if there was an error */
324 	regmap_update_bits(regmap, LTC2945_CONTROL, CONTROL_TEST_MODE, 0);
325 
326 	return ret ? : count;
327 }
328 
329 static ssize_t ltc2945_show_bool(struct device *dev,
330 				 struct device_attribute *da, char *buf)
331 {
332 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
333 	struct regmap *regmap = dev_get_drvdata(dev);
334 	unsigned int fault;
335 	int ret;
336 
337 	ret = regmap_read(regmap, LTC2945_FAULT, &fault);
338 	if (ret < 0)
339 		return ret;
340 
341 	fault &= attr->index;
342 	if (fault)		/* Clear reported faults in chip register */
343 		regmap_update_bits(regmap, LTC2945_FAULT, attr->index, 0);
344 
345 	return snprintf(buf, PAGE_SIZE, "%d\n", !!fault);
346 }
347 
348 /* Input voltages */
349 
350 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc2945_show_value, NULL,
351 			  LTC2945_VIN_H);
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,
357 			  LTC2945_MIN_VIN_H);
358 static SENSOR_DEVICE_ATTR(in1_highest, S_IRUGO, ltc2945_show_value, NULL,
359 			  LTC2945_MAX_VIN_H);
360 static SENSOR_DEVICE_ATTR(in1_reset_history, S_IWUSR, NULL,
361 			  ltc2945_reset_history, LTC2945_MIN_VIN_H);
362 
363 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc2945_show_value, NULL,
364 			  LTC2945_ADIN_H);
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,
370 			  LTC2945_MIN_ADIN_H);
371 static SENSOR_DEVICE_ATTR(in2_highest, S_IRUGO, ltc2945_show_value, NULL,
372 			  LTC2945_MAX_ADIN_H);
373 static SENSOR_DEVICE_ATTR(in2_reset_history, S_IWUSR, NULL,
374 			  ltc2945_reset_history, LTC2945_MIN_ADIN_H);
375 
376 /* Voltage alarms */
377 
378 static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ltc2945_show_bool, NULL,
379 			  FAULT_VIN_UV);
380 static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ltc2945_show_bool, NULL,
381 			  FAULT_VIN_OV);
382 static SENSOR_DEVICE_ATTR(in2_min_alarm, S_IRUGO, ltc2945_show_bool, NULL,
383 			  FAULT_ADIN_UV);
384 static SENSOR_DEVICE_ATTR(in2_max_alarm, S_IRUGO, ltc2945_show_bool, NULL,
385 			  FAULT_ADIN_OV);
386 
387 /* Currents (via sense resistor) */
388 
389 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc2945_show_value, NULL,
390 			  LTC2945_SENSE_H);
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);
401 
402 /* Current alarms */
403 
404 static SENSOR_DEVICE_ATTR(curr1_min_alarm, S_IRUGO, ltc2945_show_bool, NULL,
405 			  FAULT_SENSE_UV);
406 static SENSOR_DEVICE_ATTR(curr1_max_alarm, S_IRUGO, ltc2945_show_bool, NULL,
407 			  FAULT_SENSE_OV);
408 
409 /* Power */
410 
411 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc2945_show_value, NULL,
412 			  LTC2945_POWER_H);
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);
423 
424 /* Power alarms */
425 
426 static SENSOR_DEVICE_ATTR(power1_min_alarm, S_IRUGO, ltc2945_show_bool, NULL,
427 			  FAULT_POWER_UV);
428 static SENSOR_DEVICE_ATTR(power1_max_alarm, S_IRUGO, ltc2945_show_bool, NULL,
429 			  FAULT_POWER_OV);
430 
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,
440 
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,
449 
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,
458 
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,
467 
468 	NULL,
469 };
470 ATTRIBUTE_GROUPS(ltc2945);
471 
472 static const struct regmap_config ltc2945_regmap_config = {
473 	.reg_bits = 8,
474 	.val_bits = 8,
475 	.max_register = LTC2945_MIN_ADIN_THRES_L,
476 };
477 
478 static int ltc2945_probe(struct i2c_client *client,
479 			 const struct i2c_device_id *id)
480 {
481 	struct device *dev = &client->dev;
482 	struct device *hwmon_dev;
483 	struct regmap *regmap;
484 
485 	regmap = devm_regmap_init_i2c(client, &ltc2945_regmap_config);
486 	if (IS_ERR(regmap)) {
487 		dev_err(dev, "failed to allocate register map\n");
488 		return PTR_ERR(regmap);
489 	}
490 
491 	/* Clear faults */
492 	regmap_write(regmap, LTC2945_FAULT, 0x00);
493 
494 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
495 							   regmap,
496 							   ltc2945_groups);
497 	return PTR_ERR_OR_ZERO(hwmon_dev);
498 }
499 
500 static const struct i2c_device_id ltc2945_id[] = {
501 	{"ltc2945", 0},
502 	{ }
503 };
504 
505 MODULE_DEVICE_TABLE(i2c, ltc2945_id);
506 
507 static struct i2c_driver ltc2945_driver = {
508 	.driver = {
509 		   .name = "ltc2945",
510 		   },
511 	.probe = ltc2945_probe,
512 	.id_table = ltc2945_id,
513 };
514 
515 module_i2c_driver(ltc2945_driver);
516 
517 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
518 MODULE_DESCRIPTION("LTC2945 driver");
519 MODULE_LICENSE("GPL");
520