xref: /openbmc/linux/drivers/hwmon/ltc2945.c (revision 8f762fe5)
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_value_show(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_value_store(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_history_store(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_bool_show(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_RO(in1_input, ltc2945_value, LTC2945_VIN_H);
351 static SENSOR_DEVICE_ATTR_RW(in1_min, ltc2945_value, LTC2945_MIN_VIN_THRES_H);
352 static SENSOR_DEVICE_ATTR_RW(in1_max, ltc2945_value, LTC2945_MAX_VIN_THRES_H);
353 static SENSOR_DEVICE_ATTR_RO(in1_lowest, ltc2945_value, LTC2945_MIN_VIN_H);
354 static SENSOR_DEVICE_ATTR_RO(in1_highest, ltc2945_value, LTC2945_MAX_VIN_H);
355 static SENSOR_DEVICE_ATTR_WO(in1_reset_history, ltc2945_history,
356 			     LTC2945_MIN_VIN_H);
357 
358 static SENSOR_DEVICE_ATTR_RO(in2_input, ltc2945_value, LTC2945_ADIN_H);
359 static SENSOR_DEVICE_ATTR_RW(in2_min, ltc2945_value, LTC2945_MIN_ADIN_THRES_H);
360 static SENSOR_DEVICE_ATTR_RW(in2_max, ltc2945_value, LTC2945_MAX_ADIN_THRES_H);
361 static SENSOR_DEVICE_ATTR_RO(in2_lowest, ltc2945_value, LTC2945_MIN_ADIN_H);
362 static SENSOR_DEVICE_ATTR_RO(in2_highest, ltc2945_value, LTC2945_MAX_ADIN_H);
363 static SENSOR_DEVICE_ATTR_WO(in2_reset_history, ltc2945_history,
364 			     LTC2945_MIN_ADIN_H);
365 
366 /* Voltage alarms */
367 
368 static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc2945_bool, FAULT_VIN_UV);
369 static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc2945_bool, FAULT_VIN_OV);
370 static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc2945_bool, FAULT_ADIN_UV);
371 static SENSOR_DEVICE_ATTR_RO(in2_max_alarm, ltc2945_bool, FAULT_ADIN_OV);
372 
373 /* Currents (via sense resistor) */
374 
375 static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc2945_value, LTC2945_SENSE_H);
376 static SENSOR_DEVICE_ATTR_RW(curr1_min, ltc2945_value,
377 			     LTC2945_MIN_SENSE_THRES_H);
378 static SENSOR_DEVICE_ATTR_RW(curr1_max, ltc2945_value,
379 			     LTC2945_MAX_SENSE_THRES_H);
380 static SENSOR_DEVICE_ATTR_RO(curr1_lowest, ltc2945_value, LTC2945_MIN_SENSE_H);
381 static SENSOR_DEVICE_ATTR_RO(curr1_highest, ltc2945_value,
382 			     LTC2945_MAX_SENSE_H);
383 static SENSOR_DEVICE_ATTR_WO(curr1_reset_history, ltc2945_history,
384 			     LTC2945_MIN_SENSE_H);
385 
386 /* Current alarms */
387 
388 static SENSOR_DEVICE_ATTR_RO(curr1_min_alarm, ltc2945_bool, FAULT_SENSE_UV);
389 static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc2945_bool, FAULT_SENSE_OV);
390 
391 /* Power */
392 
393 static SENSOR_DEVICE_ATTR_RO(power1_input, ltc2945_value, LTC2945_POWER_H);
394 static SENSOR_DEVICE_ATTR_RW(power1_min, ltc2945_value,
395 			     LTC2945_MIN_POWER_THRES_H);
396 static SENSOR_DEVICE_ATTR_RW(power1_max, ltc2945_value,
397 			     LTC2945_MAX_POWER_THRES_H);
398 static SENSOR_DEVICE_ATTR_RO(power1_input_lowest, ltc2945_value,
399 			     LTC2945_MIN_POWER_H);
400 static SENSOR_DEVICE_ATTR_RO(power1_input_highest, ltc2945_value,
401 			     LTC2945_MAX_POWER_H);
402 static SENSOR_DEVICE_ATTR_WO(power1_reset_history, ltc2945_history,
403 			     LTC2945_MIN_POWER_H);
404 
405 /* Power alarms */
406 
407 static SENSOR_DEVICE_ATTR_RO(power1_min_alarm, ltc2945_bool, FAULT_POWER_UV);
408 static SENSOR_DEVICE_ATTR_RO(power1_max_alarm, ltc2945_bool, FAULT_POWER_OV);
409 
410 static struct attribute *ltc2945_attrs[] = {
411 	&sensor_dev_attr_in1_input.dev_attr.attr,
412 	&sensor_dev_attr_in1_min.dev_attr.attr,
413 	&sensor_dev_attr_in1_max.dev_attr.attr,
414 	&sensor_dev_attr_in1_lowest.dev_attr.attr,
415 	&sensor_dev_attr_in1_highest.dev_attr.attr,
416 	&sensor_dev_attr_in1_reset_history.dev_attr.attr,
417 	&sensor_dev_attr_in1_min_alarm.dev_attr.attr,
418 	&sensor_dev_attr_in1_max_alarm.dev_attr.attr,
419 
420 	&sensor_dev_attr_in2_input.dev_attr.attr,
421 	&sensor_dev_attr_in2_min.dev_attr.attr,
422 	&sensor_dev_attr_in2_max.dev_attr.attr,
423 	&sensor_dev_attr_in2_lowest.dev_attr.attr,
424 	&sensor_dev_attr_in2_highest.dev_attr.attr,
425 	&sensor_dev_attr_in2_reset_history.dev_attr.attr,
426 	&sensor_dev_attr_in2_min_alarm.dev_attr.attr,
427 	&sensor_dev_attr_in2_max_alarm.dev_attr.attr,
428 
429 	&sensor_dev_attr_curr1_input.dev_attr.attr,
430 	&sensor_dev_attr_curr1_min.dev_attr.attr,
431 	&sensor_dev_attr_curr1_max.dev_attr.attr,
432 	&sensor_dev_attr_curr1_lowest.dev_attr.attr,
433 	&sensor_dev_attr_curr1_highest.dev_attr.attr,
434 	&sensor_dev_attr_curr1_reset_history.dev_attr.attr,
435 	&sensor_dev_attr_curr1_min_alarm.dev_attr.attr,
436 	&sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
437 
438 	&sensor_dev_attr_power1_input.dev_attr.attr,
439 	&sensor_dev_attr_power1_min.dev_attr.attr,
440 	&sensor_dev_attr_power1_max.dev_attr.attr,
441 	&sensor_dev_attr_power1_input_lowest.dev_attr.attr,
442 	&sensor_dev_attr_power1_input_highest.dev_attr.attr,
443 	&sensor_dev_attr_power1_reset_history.dev_attr.attr,
444 	&sensor_dev_attr_power1_min_alarm.dev_attr.attr,
445 	&sensor_dev_attr_power1_max_alarm.dev_attr.attr,
446 
447 	NULL,
448 };
449 ATTRIBUTE_GROUPS(ltc2945);
450 
451 static const struct regmap_config ltc2945_regmap_config = {
452 	.reg_bits = 8,
453 	.val_bits = 8,
454 	.max_register = LTC2945_MIN_ADIN_THRES_L,
455 };
456 
457 static int ltc2945_probe(struct i2c_client *client,
458 			 const struct i2c_device_id *id)
459 {
460 	struct device *dev = &client->dev;
461 	struct device *hwmon_dev;
462 	struct regmap *regmap;
463 
464 	regmap = devm_regmap_init_i2c(client, &ltc2945_regmap_config);
465 	if (IS_ERR(regmap)) {
466 		dev_err(dev, "failed to allocate register map\n");
467 		return PTR_ERR(regmap);
468 	}
469 
470 	/* Clear faults */
471 	regmap_write(regmap, LTC2945_FAULT, 0x00);
472 
473 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
474 							   regmap,
475 							   ltc2945_groups);
476 	return PTR_ERR_OR_ZERO(hwmon_dev);
477 }
478 
479 static const struct i2c_device_id ltc2945_id[] = {
480 	{"ltc2945", 0},
481 	{ }
482 };
483 
484 MODULE_DEVICE_TABLE(i2c, ltc2945_id);
485 
486 static struct i2c_driver ltc2945_driver = {
487 	.driver = {
488 		   .name = "ltc2945",
489 		   },
490 	.probe = ltc2945_probe,
491 	.id_table = ltc2945_id,
492 };
493 
494 module_i2c_driver(ltc2945_driver);
495 
496 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
497 MODULE_DESCRIPTION("LTC2945 driver");
498 MODULE_LICENSE("GPL");
499