xref: /openbmc/linux/drivers/hwmon/sch5627.c (revision e15a5365)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3  *   Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>           *
4  *                                                                         *
5  ***************************************************************************/
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/jiffies.h>
13 #include <linux/platform_device.h>
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include "sch56xx-common.h"
19 
20 #define DRVNAME "sch5627"
21 #define DEVNAME DRVNAME /* We only support one model */
22 
23 #define SCH5627_HWMON_ID		0xa5
24 #define SCH5627_COMPANY_ID		0x5c
25 #define SCH5627_PRIMARY_ID		0xa0
26 
27 #define SCH5627_REG_BUILD_CODE		0x39
28 #define SCH5627_REG_BUILD_ID		0x3a
29 #define SCH5627_REG_HWMON_ID		0x3c
30 #define SCH5627_REG_HWMON_REV		0x3d
31 #define SCH5627_REG_COMPANY_ID		0x3e
32 #define SCH5627_REG_PRIMARY_ID		0x3f
33 #define SCH5627_REG_CTRL		0x40
34 
35 #define SCH5627_NO_TEMPS		8
36 #define SCH5627_NO_FANS			4
37 #define SCH5627_NO_IN			5
38 
39 static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = {
40 	0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 };
41 static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = {
42 	0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 };
43 static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = {
44 	0, 0, 1, 1, 0, 0, 0, 1 };
45 static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = {
46 	0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 };
47 static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = {
48 	0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 };
49 
50 static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = {
51 	0x2C, 0x2E, 0x30, 0x32 };
52 static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = {
53 	0x62, 0x64, 0x66, 0x68 };
54 
55 static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = {
56 	0x22, 0x23, 0x24, 0x25, 0x189 };
57 static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = {
58 	0xE4, 0xE4, 0xE3, 0xE3, 0x18A };
59 static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = {
60 	1, 0, 1, 0, 1 };
61 static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = {
62 	10745, 3660, 9765, 10745, 3660 };
63 static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = {
64 	"VCC", "VTT", "VBAT", "VTR", "V_IN" };
65 
66 struct sch5627_data {
67 	unsigned short addr;
68 	struct device *hwmon_dev;
69 	struct sch56xx_watchdog_data *watchdog;
70 	u8 control;
71 	u8 temp_max[SCH5627_NO_TEMPS];
72 	u8 temp_crit[SCH5627_NO_TEMPS];
73 	u16 fan_min[SCH5627_NO_FANS];
74 
75 	struct mutex update_lock;
76 	unsigned long last_battery;	/* In jiffies */
77 	char valid;			/* !=0 if following fields are valid */
78 	unsigned long last_updated;	/* In jiffies */
79 	u16 temp[SCH5627_NO_TEMPS];
80 	u16 fan[SCH5627_NO_FANS];
81 	u16 in[SCH5627_NO_IN];
82 };
83 
84 static struct sch5627_data *sch5627_update_device(struct device *dev)
85 {
86 	struct sch5627_data *data = dev_get_drvdata(dev);
87 	struct sch5627_data *ret = data;
88 	int i, val;
89 
90 	mutex_lock(&data->update_lock);
91 
92 	/* Trigger a Vbat voltage measurement every 5 minutes */
93 	if (time_after(jiffies, data->last_battery + 300 * HZ)) {
94 		sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL,
95 					  data->control | 0x10);
96 		data->last_battery = jiffies;
97 	}
98 
99 	/* Cache the values for 1 second */
100 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
101 		for (i = 0; i < SCH5627_NO_TEMPS; i++) {
102 			val = sch56xx_read_virtual_reg12(data->addr,
103 				SCH5627_REG_TEMP_MSB[i],
104 				SCH5627_REG_TEMP_LSN[i],
105 				SCH5627_REG_TEMP_HIGH_NIBBLE[i]);
106 			if (unlikely(val < 0)) {
107 				ret = ERR_PTR(val);
108 				goto abort;
109 			}
110 			data->temp[i] = val;
111 		}
112 
113 		for (i = 0; i < SCH5627_NO_FANS; i++) {
114 			val = sch56xx_read_virtual_reg16(data->addr,
115 							 SCH5627_REG_FAN[i]);
116 			if (unlikely(val < 0)) {
117 				ret = ERR_PTR(val);
118 				goto abort;
119 			}
120 			data->fan[i] = val;
121 		}
122 
123 		for (i = 0; i < SCH5627_NO_IN; i++) {
124 			val = sch56xx_read_virtual_reg12(data->addr,
125 				SCH5627_REG_IN_MSB[i],
126 				SCH5627_REG_IN_LSN[i],
127 				SCH5627_REG_IN_HIGH_NIBBLE[i]);
128 			if (unlikely(val < 0)) {
129 				ret = ERR_PTR(val);
130 				goto abort;
131 			}
132 			data->in[i] = val;
133 		}
134 
135 		data->last_updated = jiffies;
136 		data->valid = 1;
137 	}
138 abort:
139 	mutex_unlock(&data->update_lock);
140 	return ret;
141 }
142 
143 static int sch5627_read_limits(struct sch5627_data *data)
144 {
145 	int i, val;
146 
147 	for (i = 0; i < SCH5627_NO_TEMPS; i++) {
148 		/*
149 		 * Note what SMSC calls ABS, is what lm_sensors calls max
150 		 * (aka high), and HIGH is what lm_sensors calls crit.
151 		 */
152 		val = sch56xx_read_virtual_reg(data->addr,
153 					       SCH5627_REG_TEMP_ABS[i]);
154 		if (val < 0)
155 			return val;
156 		data->temp_max[i] = val;
157 
158 		val = sch56xx_read_virtual_reg(data->addr,
159 					       SCH5627_REG_TEMP_HIGH[i]);
160 		if (val < 0)
161 			return val;
162 		data->temp_crit[i] = val;
163 	}
164 	for (i = 0; i < SCH5627_NO_FANS; i++) {
165 		val = sch56xx_read_virtual_reg16(data->addr,
166 						 SCH5627_REG_FAN_MIN[i]);
167 		if (val < 0)
168 			return val;
169 		data->fan_min[i] = val;
170 	}
171 
172 	return 0;
173 }
174 
175 static int reg_to_temp(u16 reg)
176 {
177 	return (reg * 625) / 10 - 64000;
178 }
179 
180 static int reg_to_temp_limit(u8 reg)
181 {
182 	return (reg - 64) * 1000;
183 }
184 
185 static int reg_to_rpm(u16 reg)
186 {
187 	if (reg == 0)
188 		return -EIO;
189 	if (reg == 0xffff)
190 		return 0;
191 
192 	return 5400540 / reg;
193 }
194 
195 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
196 	char *buf)
197 {
198 	return snprintf(buf, PAGE_SIZE, "%s\n", DEVNAME);
199 }
200 
201 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
202 			 char *buf)
203 {
204 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
205 	struct sch5627_data *data = sch5627_update_device(dev);
206 	int val;
207 
208 	if (IS_ERR(data))
209 		return PTR_ERR(data);
210 
211 	val = reg_to_temp(data->temp[attr->index]);
212 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
213 }
214 
215 static ssize_t temp_fault_show(struct device *dev,
216 			       struct device_attribute *devattr, char *buf)
217 {
218 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
219 	struct sch5627_data *data = sch5627_update_device(dev);
220 
221 	if (IS_ERR(data))
222 		return PTR_ERR(data);
223 
224 	return snprintf(buf, PAGE_SIZE, "%d\n", data->temp[attr->index] == 0);
225 }
226 
227 static ssize_t temp_max_show(struct device *dev,
228 			     struct device_attribute *devattr, char *buf)
229 {
230 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
231 	struct sch5627_data *data = dev_get_drvdata(dev);
232 	int val;
233 
234 	val = reg_to_temp_limit(data->temp_max[attr->index]);
235 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
236 }
237 
238 static ssize_t temp_crit_show(struct device *dev,
239 			      struct device_attribute *devattr, char *buf)
240 {
241 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
242 	struct sch5627_data *data = dev_get_drvdata(dev);
243 	int val;
244 
245 	val = reg_to_temp_limit(data->temp_crit[attr->index]);
246 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
247 }
248 
249 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
250 			char *buf)
251 {
252 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
253 	struct sch5627_data *data = sch5627_update_device(dev);
254 	int val;
255 
256 	if (IS_ERR(data))
257 		return PTR_ERR(data);
258 
259 	val = reg_to_rpm(data->fan[attr->index]);
260 	if (val < 0)
261 		return val;
262 
263 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
264 }
265 
266 static ssize_t fan_fault_show(struct device *dev,
267 			      struct device_attribute *devattr, char *buf)
268 {
269 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
270 	struct sch5627_data *data = sch5627_update_device(dev);
271 
272 	if (IS_ERR(data))
273 		return PTR_ERR(data);
274 
275 	return snprintf(buf, PAGE_SIZE, "%d\n",
276 			data->fan[attr->index] == 0xffff);
277 }
278 
279 static ssize_t fan_min_show(struct device *dev,
280 			    struct device_attribute *devattr, char *buf)
281 {
282 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
283 	struct sch5627_data *data = dev_get_drvdata(dev);
284 	int val = reg_to_rpm(data->fan_min[attr->index]);
285 	if (val < 0)
286 		return val;
287 
288 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
289 }
290 
291 static ssize_t in_show(struct device *dev, struct device_attribute *devattr,
292 		       char *buf)
293 {
294 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
295 	struct sch5627_data *data = sch5627_update_device(dev);
296 	int val;
297 
298 	if (IS_ERR(data))
299 		return PTR_ERR(data);
300 
301 	val = DIV_ROUND_CLOSEST(
302 		data->in[attr->index] * SCH5627_REG_IN_FACTOR[attr->index],
303 		10000);
304 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
305 }
306 
307 static ssize_t in_label_show(struct device *dev,
308 			     struct device_attribute *devattr, char *buf)
309 {
310 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
311 
312 	return snprintf(buf, PAGE_SIZE, "%s\n",
313 			SCH5627_IN_LABELS[attr->index]);
314 }
315 
316 static DEVICE_ATTR_RO(name);
317 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
318 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
319 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
320 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
321 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
322 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
323 static SENSOR_DEVICE_ATTR_RO(temp7_input, temp, 6);
324 static SENSOR_DEVICE_ATTR_RO(temp8_input, temp, 7);
325 static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
326 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
327 static SENSOR_DEVICE_ATTR_RO(temp3_fault, temp_fault, 2);
328 static SENSOR_DEVICE_ATTR_RO(temp4_fault, temp_fault, 3);
329 static SENSOR_DEVICE_ATTR_RO(temp5_fault, temp_fault, 4);
330 static SENSOR_DEVICE_ATTR_RO(temp6_fault, temp_fault, 5);
331 static SENSOR_DEVICE_ATTR_RO(temp7_fault, temp_fault, 6);
332 static SENSOR_DEVICE_ATTR_RO(temp8_fault, temp_fault, 7);
333 static SENSOR_DEVICE_ATTR_RO(temp1_max, temp_max, 0);
334 static SENSOR_DEVICE_ATTR_RO(temp2_max, temp_max, 1);
335 static SENSOR_DEVICE_ATTR_RO(temp3_max, temp_max, 2);
336 static SENSOR_DEVICE_ATTR_RO(temp4_max, temp_max, 3);
337 static SENSOR_DEVICE_ATTR_RO(temp5_max, temp_max, 4);
338 static SENSOR_DEVICE_ATTR_RO(temp6_max, temp_max, 5);
339 static SENSOR_DEVICE_ATTR_RO(temp7_max, temp_max, 6);
340 static SENSOR_DEVICE_ATTR_RO(temp8_max, temp_max, 7);
341 static SENSOR_DEVICE_ATTR_RO(temp1_crit, temp_crit, 0);
342 static SENSOR_DEVICE_ATTR_RO(temp2_crit, temp_crit, 1);
343 static SENSOR_DEVICE_ATTR_RO(temp3_crit, temp_crit, 2);
344 static SENSOR_DEVICE_ATTR_RO(temp4_crit, temp_crit, 3);
345 static SENSOR_DEVICE_ATTR_RO(temp5_crit, temp_crit, 4);
346 static SENSOR_DEVICE_ATTR_RO(temp6_crit, temp_crit, 5);
347 static SENSOR_DEVICE_ATTR_RO(temp7_crit, temp_crit, 6);
348 static SENSOR_DEVICE_ATTR_RO(temp8_crit, temp_crit, 7);
349 
350 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
351 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
352 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
353 static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
354 static SENSOR_DEVICE_ATTR_RO(fan1_fault, fan_fault, 0);
355 static SENSOR_DEVICE_ATTR_RO(fan2_fault, fan_fault, 1);
356 static SENSOR_DEVICE_ATTR_RO(fan3_fault, fan_fault, 2);
357 static SENSOR_DEVICE_ATTR_RO(fan4_fault, fan_fault, 3);
358 static SENSOR_DEVICE_ATTR_RO(fan1_min, fan_min, 0);
359 static SENSOR_DEVICE_ATTR_RO(fan2_min, fan_min, 1);
360 static SENSOR_DEVICE_ATTR_RO(fan3_min, fan_min, 2);
361 static SENSOR_DEVICE_ATTR_RO(fan4_min, fan_min, 3);
362 
363 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
364 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
365 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
366 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
367 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
368 static SENSOR_DEVICE_ATTR_RO(in0_label, in_label, 0);
369 static SENSOR_DEVICE_ATTR_RO(in1_label, in_label, 1);
370 static SENSOR_DEVICE_ATTR_RO(in2_label, in_label, 2);
371 static SENSOR_DEVICE_ATTR_RO(in3_label, in_label, 3);
372 
373 static struct attribute *sch5627_attributes[] = {
374 	&dev_attr_name.attr,
375 
376 	&sensor_dev_attr_temp1_input.dev_attr.attr,
377 	&sensor_dev_attr_temp2_input.dev_attr.attr,
378 	&sensor_dev_attr_temp3_input.dev_attr.attr,
379 	&sensor_dev_attr_temp4_input.dev_attr.attr,
380 	&sensor_dev_attr_temp5_input.dev_attr.attr,
381 	&sensor_dev_attr_temp6_input.dev_attr.attr,
382 	&sensor_dev_attr_temp7_input.dev_attr.attr,
383 	&sensor_dev_attr_temp8_input.dev_attr.attr,
384 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
385 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
386 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
387 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
388 	&sensor_dev_attr_temp5_fault.dev_attr.attr,
389 	&sensor_dev_attr_temp6_fault.dev_attr.attr,
390 	&sensor_dev_attr_temp7_fault.dev_attr.attr,
391 	&sensor_dev_attr_temp8_fault.dev_attr.attr,
392 	&sensor_dev_attr_temp1_max.dev_attr.attr,
393 	&sensor_dev_attr_temp2_max.dev_attr.attr,
394 	&sensor_dev_attr_temp3_max.dev_attr.attr,
395 	&sensor_dev_attr_temp4_max.dev_attr.attr,
396 	&sensor_dev_attr_temp5_max.dev_attr.attr,
397 	&sensor_dev_attr_temp6_max.dev_attr.attr,
398 	&sensor_dev_attr_temp7_max.dev_attr.attr,
399 	&sensor_dev_attr_temp8_max.dev_attr.attr,
400 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
401 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
402 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
403 	&sensor_dev_attr_temp4_crit.dev_attr.attr,
404 	&sensor_dev_attr_temp5_crit.dev_attr.attr,
405 	&sensor_dev_attr_temp6_crit.dev_attr.attr,
406 	&sensor_dev_attr_temp7_crit.dev_attr.attr,
407 	&sensor_dev_attr_temp8_crit.dev_attr.attr,
408 
409 	&sensor_dev_attr_fan1_input.dev_attr.attr,
410 	&sensor_dev_attr_fan2_input.dev_attr.attr,
411 	&sensor_dev_attr_fan3_input.dev_attr.attr,
412 	&sensor_dev_attr_fan4_input.dev_attr.attr,
413 	&sensor_dev_attr_fan1_fault.dev_attr.attr,
414 	&sensor_dev_attr_fan2_fault.dev_attr.attr,
415 	&sensor_dev_attr_fan3_fault.dev_attr.attr,
416 	&sensor_dev_attr_fan4_fault.dev_attr.attr,
417 	&sensor_dev_attr_fan1_min.dev_attr.attr,
418 	&sensor_dev_attr_fan2_min.dev_attr.attr,
419 	&sensor_dev_attr_fan3_min.dev_attr.attr,
420 	&sensor_dev_attr_fan4_min.dev_attr.attr,
421 
422 	&sensor_dev_attr_in0_input.dev_attr.attr,
423 	&sensor_dev_attr_in1_input.dev_attr.attr,
424 	&sensor_dev_attr_in2_input.dev_attr.attr,
425 	&sensor_dev_attr_in3_input.dev_attr.attr,
426 	&sensor_dev_attr_in4_input.dev_attr.attr,
427 	&sensor_dev_attr_in0_label.dev_attr.attr,
428 	&sensor_dev_attr_in1_label.dev_attr.attr,
429 	&sensor_dev_attr_in2_label.dev_attr.attr,
430 	&sensor_dev_attr_in3_label.dev_attr.attr,
431 	/* No in4_label as in4 is a generic input pin */
432 
433 	NULL
434 };
435 
436 static const struct attribute_group sch5627_group = {
437 	.attrs = sch5627_attributes,
438 };
439 
440 static int sch5627_remove(struct platform_device *pdev)
441 {
442 	struct sch5627_data *data = platform_get_drvdata(pdev);
443 
444 	if (data->watchdog)
445 		sch56xx_watchdog_unregister(data->watchdog);
446 
447 	if (data->hwmon_dev)
448 		hwmon_device_unregister(data->hwmon_dev);
449 
450 	sysfs_remove_group(&pdev->dev.kobj, &sch5627_group);
451 
452 	return 0;
453 }
454 
455 static int sch5627_probe(struct platform_device *pdev)
456 {
457 	struct sch5627_data *data;
458 	int err, build_code, build_id, hwmon_rev, val;
459 
460 	data = devm_kzalloc(&pdev->dev, sizeof(struct sch5627_data),
461 			    GFP_KERNEL);
462 	if (!data)
463 		return -ENOMEM;
464 
465 	data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
466 	mutex_init(&data->update_lock);
467 	platform_set_drvdata(pdev, data);
468 
469 	val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_HWMON_ID);
470 	if (val < 0) {
471 		err = val;
472 		goto error;
473 	}
474 	if (val != SCH5627_HWMON_ID) {
475 		pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon",
476 		       val, SCH5627_HWMON_ID);
477 		err = -ENODEV;
478 		goto error;
479 	}
480 
481 	val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_COMPANY_ID);
482 	if (val < 0) {
483 		err = val;
484 		goto error;
485 	}
486 	if (val != SCH5627_COMPANY_ID) {
487 		pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company",
488 		       val, SCH5627_COMPANY_ID);
489 		err = -ENODEV;
490 		goto error;
491 	}
492 
493 	val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_PRIMARY_ID);
494 	if (val < 0) {
495 		err = val;
496 		goto error;
497 	}
498 	if (val != SCH5627_PRIMARY_ID) {
499 		pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary",
500 		       val, SCH5627_PRIMARY_ID);
501 		err = -ENODEV;
502 		goto error;
503 	}
504 
505 	build_code = sch56xx_read_virtual_reg(data->addr,
506 					      SCH5627_REG_BUILD_CODE);
507 	if (build_code < 0) {
508 		err = build_code;
509 		goto error;
510 	}
511 
512 	build_id = sch56xx_read_virtual_reg16(data->addr,
513 					      SCH5627_REG_BUILD_ID);
514 	if (build_id < 0) {
515 		err = build_id;
516 		goto error;
517 	}
518 
519 	hwmon_rev = sch56xx_read_virtual_reg(data->addr,
520 					     SCH5627_REG_HWMON_REV);
521 	if (hwmon_rev < 0) {
522 		err = hwmon_rev;
523 		goto error;
524 	}
525 
526 	val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_CTRL);
527 	if (val < 0) {
528 		err = val;
529 		goto error;
530 	}
531 	data->control = val;
532 	if (!(data->control & 0x01)) {
533 		pr_err("hardware monitoring not enabled\n");
534 		err = -ENODEV;
535 		goto error;
536 	}
537 	/* Trigger a Vbat voltage measurement, so that we get a valid reading
538 	   the first time we read Vbat */
539 	sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL,
540 				  data->control | 0x10);
541 	data->last_battery = jiffies;
542 
543 	/*
544 	 * Read limits, we do this only once as reading a register on
545 	 * the sch5627 is quite expensive (and they don't change).
546 	 */
547 	err = sch5627_read_limits(data);
548 	if (err)
549 		goto error;
550 
551 	pr_info("found %s chip at %#hx\n", DEVNAME, data->addr);
552 	pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n",
553 		build_code, build_id, hwmon_rev);
554 
555 	/* Register sysfs interface files */
556 	err = sysfs_create_group(&pdev->dev.kobj, &sch5627_group);
557 	if (err)
558 		goto error;
559 
560 	data->hwmon_dev = hwmon_device_register(&pdev->dev);
561 	if (IS_ERR(data->hwmon_dev)) {
562 		err = PTR_ERR(data->hwmon_dev);
563 		data->hwmon_dev = NULL;
564 		goto error;
565 	}
566 
567 	/* Note failing to register the watchdog is not a fatal error */
568 	data->watchdog = sch56xx_watchdog_register(&pdev->dev, data->addr,
569 			(build_code << 24) | (build_id << 8) | hwmon_rev,
570 			&data->update_lock, 1);
571 
572 	return 0;
573 
574 error:
575 	sch5627_remove(pdev);
576 	return err;
577 }
578 
579 static struct platform_driver sch5627_driver = {
580 	.driver = {
581 		.name	= DRVNAME,
582 	},
583 	.probe		= sch5627_probe,
584 	.remove		= sch5627_remove,
585 };
586 
587 module_platform_driver(sch5627_driver);
588 
589 MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver");
590 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
591 MODULE_LICENSE("GPL");
592