xref: /openbmc/linux/drivers/hwmon/lm63.c (revision a09d2831)
1 /*
2  * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3  *          with integrated fan control
4  * Copyright (C) 2004-2008  Jean Delvare <khali@linux-fr.org>
5  * Based on the lm90 driver.
6  *
7  * The LM63 is a sensor chip made by National Semiconductor. It measures
8  * two temperatures (its own and one external one) and the speed of one
9  * fan, those speed it can additionally control. Complete datasheet can be
10  * obtained from National's website at:
11  *   http://www.national.com/pf/LM/LM63.html
12  *
13  * The LM63 is basically an LM86 with fan speed monitoring and control
14  * capabilities added. It misses some of the LM86 features though:
15  *  - No low limit for local temperature.
16  *  - No critical limit for local temperature.
17  *  - Critical limit for remote temperature can be changed only once. We
18  *    will consider that the critical limit is read-only.
19  *
20  * The datasheet isn't very clear about what the tachometer reading is.
21  * I had a explanation from National Semiconductor though. The two lower
22  * bits of the read value have to be masked out. The value is still 16 bit
23  * in width.
24  *
25  * This program is free software; you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation; either version 2 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38  */
39 
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/jiffies.h>
44 #include <linux/i2c.h>
45 #include <linux/hwmon-sysfs.h>
46 #include <linux/hwmon.h>
47 #include <linux/err.h>
48 #include <linux/mutex.h>
49 #include <linux/sysfs.h>
50 
51 /*
52  * Addresses to scan
53  * Address is fully defined internally and cannot be changed.
54  */
55 
56 static const unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
57 
58 /*
59  * The LM63 registers
60  */
61 
62 #define LM63_REG_CONFIG1		0x03
63 #define LM63_REG_CONFIG2		0xBF
64 #define LM63_REG_CONFIG_FAN		0x4A
65 
66 #define LM63_REG_TACH_COUNT_MSB		0x47
67 #define LM63_REG_TACH_COUNT_LSB		0x46
68 #define LM63_REG_TACH_LIMIT_MSB		0x49
69 #define LM63_REG_TACH_LIMIT_LSB		0x48
70 
71 #define LM63_REG_PWM_VALUE		0x4C
72 #define LM63_REG_PWM_FREQ		0x4D
73 
74 #define LM63_REG_LOCAL_TEMP		0x00
75 #define LM63_REG_LOCAL_HIGH		0x05
76 
77 #define LM63_REG_REMOTE_TEMP_MSB	0x01
78 #define LM63_REG_REMOTE_TEMP_LSB	0x10
79 #define LM63_REG_REMOTE_OFFSET_MSB	0x11
80 #define LM63_REG_REMOTE_OFFSET_LSB	0x12
81 #define LM63_REG_REMOTE_HIGH_MSB	0x07
82 #define LM63_REG_REMOTE_HIGH_LSB	0x13
83 #define LM63_REG_REMOTE_LOW_MSB		0x08
84 #define LM63_REG_REMOTE_LOW_LSB		0x14
85 #define LM63_REG_REMOTE_TCRIT		0x19
86 #define LM63_REG_REMOTE_TCRIT_HYST	0x21
87 
88 #define LM63_REG_ALERT_STATUS		0x02
89 #define LM63_REG_ALERT_MASK		0x16
90 
91 #define LM63_REG_MAN_ID			0xFE
92 #define LM63_REG_CHIP_ID		0xFF
93 
94 /*
95  * Conversions and various macros
96  * For tachometer counts, the LM63 uses 16-bit values.
97  * For local temperature and high limit, remote critical limit and hysteresis
98  * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
99  * For remote temperature, low and high limits, it uses signed 11-bit values
100  * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
101  */
102 
103 #define FAN_FROM_REG(reg)	((reg) == 0xFFFC || (reg) == 0 ? 0 : \
104 				 5400000 / (reg))
105 #define FAN_TO_REG(val)		((val) <= 82 ? 0xFFFC : \
106 				 (5400000 / (val)) & 0xFFFC)
107 #define TEMP8_FROM_REG(reg)	((reg) * 1000)
108 #define TEMP8_TO_REG(val)	((val) <= -128000 ? -128 : \
109 				 (val) >= 127000 ? 127 : \
110 				 (val) < 0 ? ((val) - 500) / 1000 : \
111 				 ((val) + 500) / 1000)
112 #define TEMP11_FROM_REG(reg)	((reg) / 32 * 125)
113 #define TEMP11_TO_REG(val)	((val) <= -128000 ? 0x8000 : \
114 				 (val) >= 127875 ? 0x7FE0 : \
115 				 (val) < 0 ? ((val) - 62) / 125 * 32 : \
116 				 ((val) + 62) / 125 * 32)
117 #define HYST_TO_REG(val)	((val) <= 0 ? 0 : \
118 				 (val) >= 127000 ? 127 : \
119 				 ((val) + 500) / 1000)
120 
121 /*
122  * Functions declaration
123  */
124 
125 static int lm63_probe(struct i2c_client *client,
126 		      const struct i2c_device_id *id);
127 static int lm63_remove(struct i2c_client *client);
128 
129 static struct lm63_data *lm63_update_device(struct device *dev);
130 
131 static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info);
132 static void lm63_init_client(struct i2c_client *client);
133 
134 /*
135  * Driver data (common to all clients)
136  */
137 
138 static const struct i2c_device_id lm63_id[] = {
139 	{ "lm63", 0 },
140 	{ }
141 };
142 MODULE_DEVICE_TABLE(i2c, lm63_id);
143 
144 static struct i2c_driver lm63_driver = {
145 	.class		= I2C_CLASS_HWMON,
146 	.driver = {
147 		.name	= "lm63",
148 	},
149 	.probe		= lm63_probe,
150 	.remove		= lm63_remove,
151 	.id_table	= lm63_id,
152 	.detect		= lm63_detect,
153 	.address_list	= normal_i2c,
154 };
155 
156 /*
157  * Client data (each client gets its own)
158  */
159 
160 struct lm63_data {
161 	struct device *hwmon_dev;
162 	struct mutex update_lock;
163 	char valid; /* zero until following fields are valid */
164 	unsigned long last_updated; /* in jiffies */
165 
166 	/* registers values */
167 	u8 config, config_fan;
168 	u16 fan[2];	/* 0: input
169 			   1: low limit */
170 	u8 pwm1_freq;
171 	u8 pwm1_value;
172 	s8 temp8[3];	/* 0: local input
173 			   1: local high limit
174 			   2: remote critical limit */
175 	s16 temp11[3];	/* 0: remote input
176 			   1: remote low limit
177 			   2: remote high limit */
178 	u8 temp2_crit_hyst;
179 	u8 alarms;
180 };
181 
182 /*
183  * Sysfs callback functions and files
184  */
185 
186 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
187 			char *buf)
188 {
189 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
190 	struct lm63_data *data = lm63_update_device(dev);
191 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
192 }
193 
194 static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
195 		       const char *buf, size_t count)
196 {
197 	struct i2c_client *client = to_i2c_client(dev);
198 	struct lm63_data *data = i2c_get_clientdata(client);
199 	unsigned long val = simple_strtoul(buf, NULL, 10);
200 
201 	mutex_lock(&data->update_lock);
202 	data->fan[1] = FAN_TO_REG(val);
203 	i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
204 				  data->fan[1] & 0xFF);
205 	i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
206 				  data->fan[1] >> 8);
207 	mutex_unlock(&data->update_lock);
208 	return count;
209 }
210 
211 static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
212 			 char *buf)
213 {
214 	struct lm63_data *data = lm63_update_device(dev);
215 	return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
216 		       255 : (data->pwm1_value * 255 + data->pwm1_freq) /
217 		       (2 * data->pwm1_freq));
218 }
219 
220 static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
221 			const char *buf, size_t count)
222 {
223 	struct i2c_client *client = to_i2c_client(dev);
224 	struct lm63_data *data = i2c_get_clientdata(client);
225 	unsigned long val;
226 
227 	if (!(data->config_fan & 0x20)) /* register is read-only */
228 		return -EPERM;
229 
230 	val = simple_strtoul(buf, NULL, 10);
231 	mutex_lock(&data->update_lock);
232 	data->pwm1_value = val <= 0 ? 0 :
233 			   val >= 255 ? 2 * data->pwm1_freq :
234 			   (val * data->pwm1_freq * 2 + 127) / 255;
235 	i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
236 	mutex_unlock(&data->update_lock);
237 	return count;
238 }
239 
240 static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy,
241 				char *buf)
242 {
243 	struct lm63_data *data = lm63_update_device(dev);
244 	return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
245 }
246 
247 static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
248 			  char *buf)
249 {
250 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
251 	struct lm63_data *data = lm63_update_device(dev);
252 	return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
253 }
254 
255 static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy,
256 			 const char *buf, size_t count)
257 {
258 	struct i2c_client *client = to_i2c_client(dev);
259 	struct lm63_data *data = i2c_get_clientdata(client);
260 	long val = simple_strtol(buf, NULL, 10);
261 
262 	mutex_lock(&data->update_lock);
263 	data->temp8[1] = TEMP8_TO_REG(val);
264 	i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]);
265 	mutex_unlock(&data->update_lock);
266 	return count;
267 }
268 
269 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
270 			   char *buf)
271 {
272 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
273 	struct lm63_data *data = lm63_update_device(dev);
274 	return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index]));
275 }
276 
277 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
278 			  const char *buf, size_t count)
279 {
280 	static const u8 reg[4] = {
281 		LM63_REG_REMOTE_LOW_MSB,
282 		LM63_REG_REMOTE_LOW_LSB,
283 		LM63_REG_REMOTE_HIGH_MSB,
284 		LM63_REG_REMOTE_HIGH_LSB,
285 	};
286 
287 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
288 	struct i2c_client *client = to_i2c_client(dev);
289 	struct lm63_data *data = i2c_get_clientdata(client);
290 	long val = simple_strtol(buf, NULL, 10);
291 	int nr = attr->index;
292 
293 	mutex_lock(&data->update_lock);
294 	data->temp11[nr] = TEMP11_TO_REG(val);
295 	i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
296 				  data->temp11[nr] >> 8);
297 	i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
298 				  data->temp11[nr] & 0xff);
299 	mutex_unlock(&data->update_lock);
300 	return count;
301 }
302 
303 /* Hysteresis register holds a relative value, while we want to present
304    an absolute to user-space */
305 static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
306 				    char *buf)
307 {
308 	struct lm63_data *data = lm63_update_device(dev);
309 	return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2])
310 		       - TEMP8_FROM_REG(data->temp2_crit_hyst));
311 }
312 
313 /* And now the other way around, user-space provides an absolute
314    hysteresis value and we have to store a relative one */
315 static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
316 				   const char *buf, size_t count)
317 {
318 	struct i2c_client *client = to_i2c_client(dev);
319 	struct lm63_data *data = i2c_get_clientdata(client);
320 	long val = simple_strtol(buf, NULL, 10);
321 	long hyst;
322 
323 	mutex_lock(&data->update_lock);
324 	hyst = TEMP8_FROM_REG(data->temp8[2]) - val;
325 	i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
326 				  HYST_TO_REG(hyst));
327 	mutex_unlock(&data->update_lock);
328 	return count;
329 }
330 
331 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
332 			   char *buf)
333 {
334 	struct lm63_data *data = lm63_update_device(dev);
335 	return sprintf(buf, "%u\n", data->alarms);
336 }
337 
338 static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
339 			  char *buf)
340 {
341 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
342 	struct lm63_data *data = lm63_update_device(dev);
343 	int bitnr = attr->index;
344 
345 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
346 }
347 
348 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
349 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
350 	set_fan, 1);
351 
352 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
353 static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
354 
355 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
356 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
357 	set_temp8, 1);
358 
359 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
360 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
361 	set_temp11, 1);
362 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
363 	set_temp11, 2);
364 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2);
365 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
366 	set_temp2_crit_hyst);
367 
368 /* Individual alarm files */
369 static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
370 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
371 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
372 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
373 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
374 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
375 /* Raw alarm file for compatibility */
376 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
377 
378 static struct attribute *lm63_attributes[] = {
379 	&dev_attr_pwm1.attr,
380 	&dev_attr_pwm1_enable.attr,
381 	&sensor_dev_attr_temp1_input.dev_attr.attr,
382 	&sensor_dev_attr_temp2_input.dev_attr.attr,
383 	&sensor_dev_attr_temp2_min.dev_attr.attr,
384 	&sensor_dev_attr_temp1_max.dev_attr.attr,
385 	&sensor_dev_attr_temp2_max.dev_attr.attr,
386 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
387 	&dev_attr_temp2_crit_hyst.attr,
388 
389 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
390 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
391 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
392 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
393 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
394 	&dev_attr_alarms.attr,
395 	NULL
396 };
397 
398 static const struct attribute_group lm63_group = {
399 	.attrs = lm63_attributes,
400 };
401 
402 static struct attribute *lm63_attributes_fan1[] = {
403 	&sensor_dev_attr_fan1_input.dev_attr.attr,
404 	&sensor_dev_attr_fan1_min.dev_attr.attr,
405 
406 	&sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
407 	NULL
408 };
409 
410 static const struct attribute_group lm63_group_fan1 = {
411 	.attrs = lm63_attributes_fan1,
412 };
413 
414 /*
415  * Real code
416  */
417 
418 /* Return 0 if detection is successful, -ENODEV otherwise */
419 static int lm63_detect(struct i2c_client *new_client,
420 		       struct i2c_board_info *info)
421 {
422 	struct i2c_adapter *adapter = new_client->adapter;
423 	u8 man_id, chip_id, reg_config1, reg_config2;
424 	u8 reg_alert_status, reg_alert_mask;
425 
426 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
427 		return -ENODEV;
428 
429 	man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
430 	chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
431 
432 	reg_config1 = i2c_smbus_read_byte_data(new_client,
433 		      LM63_REG_CONFIG1);
434 	reg_config2 = i2c_smbus_read_byte_data(new_client,
435 		      LM63_REG_CONFIG2);
436 	reg_alert_status = i2c_smbus_read_byte_data(new_client,
437 			   LM63_REG_ALERT_STATUS);
438 	reg_alert_mask = i2c_smbus_read_byte_data(new_client,
439 			 LM63_REG_ALERT_MASK);
440 
441 	if (man_id != 0x01 /* National Semiconductor */
442 	 || chip_id != 0x41 /* LM63 */
443 	 || (reg_config1 & 0x18) != 0x00
444 	 || (reg_config2 & 0xF8) != 0x00
445 	 || (reg_alert_status & 0x20) != 0x00
446 	 || (reg_alert_mask & 0xA4) != 0xA4) {
447 		dev_dbg(&adapter->dev,
448 			"Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
449 			man_id, chip_id);
450 		return -ENODEV;
451 	}
452 
453 	strlcpy(info->type, "lm63", I2C_NAME_SIZE);
454 
455 	return 0;
456 }
457 
458 static int lm63_probe(struct i2c_client *new_client,
459 		      const struct i2c_device_id *id)
460 {
461 	struct lm63_data *data;
462 	int err;
463 
464 	data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
465 	if (!data) {
466 		err = -ENOMEM;
467 		goto exit;
468 	}
469 
470 	i2c_set_clientdata(new_client, data);
471 	data->valid = 0;
472 	mutex_init(&data->update_lock);
473 
474 	/* Initialize the LM63 chip */
475 	lm63_init_client(new_client);
476 
477 	/* Register sysfs hooks */
478 	if ((err = sysfs_create_group(&new_client->dev.kobj,
479 				      &lm63_group)))
480 		goto exit_free;
481 	if (data->config & 0x04) { /* tachometer enabled */
482 		if ((err = sysfs_create_group(&new_client->dev.kobj,
483 					      &lm63_group_fan1)))
484 			goto exit_remove_files;
485 	}
486 
487 	data->hwmon_dev = hwmon_device_register(&new_client->dev);
488 	if (IS_ERR(data->hwmon_dev)) {
489 		err = PTR_ERR(data->hwmon_dev);
490 		goto exit_remove_files;
491 	}
492 
493 	return 0;
494 
495 exit_remove_files:
496 	sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
497 	sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
498 exit_free:
499 	kfree(data);
500 exit:
501 	return err;
502 }
503 
504 /* Idealy we shouldn't have to initialize anything, since the BIOS
505    should have taken care of everything */
506 static void lm63_init_client(struct i2c_client *client)
507 {
508 	struct lm63_data *data = i2c_get_clientdata(client);
509 
510 	data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
511 	data->config_fan = i2c_smbus_read_byte_data(client,
512 						    LM63_REG_CONFIG_FAN);
513 
514 	/* Start converting if needed */
515 	if (data->config & 0x40) { /* standby */
516 		dev_dbg(&client->dev, "Switching to operational mode\n");
517 		data->config &= 0xA7;
518 		i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
519 					  data->config);
520 	}
521 
522 	/* We may need pwm1_freq before ever updating the client data */
523 	data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
524 	if (data->pwm1_freq == 0)
525 		data->pwm1_freq = 1;
526 
527 	/* Show some debug info about the LM63 configuration */
528 	dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
529 		(data->config & 0x04) ? "tachometer input" :
530 		"alert output");
531 	dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
532 		(data->config_fan & 0x08) ? "1.4" : "360",
533 		((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
534 	dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
535 		(data->config_fan & 0x10) ? "low" : "high",
536 		(data->config_fan & 0x20) ? "manual" : "auto");
537 }
538 
539 static int lm63_remove(struct i2c_client *client)
540 {
541 	struct lm63_data *data = i2c_get_clientdata(client);
542 
543 	hwmon_device_unregister(data->hwmon_dev);
544 	sysfs_remove_group(&client->dev.kobj, &lm63_group);
545 	sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
546 
547 	kfree(data);
548 	return 0;
549 }
550 
551 static struct lm63_data *lm63_update_device(struct device *dev)
552 {
553 	struct i2c_client *client = to_i2c_client(dev);
554 	struct lm63_data *data = i2c_get_clientdata(client);
555 
556 	mutex_lock(&data->update_lock);
557 
558 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
559 		if (data->config & 0x04) { /* tachometer enabled  */
560 			/* order matters for fan1_input */
561 			data->fan[0] = i2c_smbus_read_byte_data(client,
562 				       LM63_REG_TACH_COUNT_LSB) & 0xFC;
563 			data->fan[0] |= i2c_smbus_read_byte_data(client,
564 					LM63_REG_TACH_COUNT_MSB) << 8;
565 			data->fan[1] = (i2c_smbus_read_byte_data(client,
566 					LM63_REG_TACH_LIMIT_LSB) & 0xFC)
567 				     | (i2c_smbus_read_byte_data(client,
568 					LM63_REG_TACH_LIMIT_MSB) << 8);
569 		}
570 
571 		data->pwm1_freq = i2c_smbus_read_byte_data(client,
572 				  LM63_REG_PWM_FREQ);
573 		if (data->pwm1_freq == 0)
574 			data->pwm1_freq = 1;
575 		data->pwm1_value = i2c_smbus_read_byte_data(client,
576 				   LM63_REG_PWM_VALUE);
577 
578 		data->temp8[0] = i2c_smbus_read_byte_data(client,
579 				 LM63_REG_LOCAL_TEMP);
580 		data->temp8[1] = i2c_smbus_read_byte_data(client,
581 				 LM63_REG_LOCAL_HIGH);
582 
583 		/* order matters for temp2_input */
584 		data->temp11[0] = i2c_smbus_read_byte_data(client,
585 				  LM63_REG_REMOTE_TEMP_MSB) << 8;
586 		data->temp11[0] |= i2c_smbus_read_byte_data(client,
587 				   LM63_REG_REMOTE_TEMP_LSB);
588 		data->temp11[1] = (i2c_smbus_read_byte_data(client,
589 				  LM63_REG_REMOTE_LOW_MSB) << 8)
590 				| i2c_smbus_read_byte_data(client,
591 				  LM63_REG_REMOTE_LOW_LSB);
592 		data->temp11[2] = (i2c_smbus_read_byte_data(client,
593 				  LM63_REG_REMOTE_HIGH_MSB) << 8)
594 				| i2c_smbus_read_byte_data(client,
595 				  LM63_REG_REMOTE_HIGH_LSB);
596 		data->temp8[2] = i2c_smbus_read_byte_data(client,
597 				 LM63_REG_REMOTE_TCRIT);
598 		data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
599 					LM63_REG_REMOTE_TCRIT_HYST);
600 
601 		data->alarms = i2c_smbus_read_byte_data(client,
602 			       LM63_REG_ALERT_STATUS) & 0x7F;
603 
604 		data->last_updated = jiffies;
605 		data->valid = 1;
606 	}
607 
608 	mutex_unlock(&data->update_lock);
609 
610 	return data;
611 }
612 
613 static int __init sensors_lm63_init(void)
614 {
615 	return i2c_add_driver(&lm63_driver);
616 }
617 
618 static void __exit sensors_lm63_exit(void)
619 {
620 	i2c_del_driver(&lm63_driver);
621 }
622 
623 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
624 MODULE_DESCRIPTION("LM63 driver");
625 MODULE_LICENSE("GPL");
626 
627 module_init(sensors_lm63_init);
628 module_exit(sensors_lm63_exit);
629