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