Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / lm80.c
blob4a10cbe403d37300b1ff08d5651aa81594f6f0c9
1 /*
2 * lm80.c - From lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 * and Philip Edelbrock <phil@netroedge.com>
7 * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
34 /* Addresses to scan */
35 <<<<<<< HEAD:drivers/hwmon/lm80.c
36 static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c,
37 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
38 =======
39 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
40 0x2e, 0x2f, I2C_CLIENT_END };
41 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/lm80.c
43 /* Insmod parameters */
44 I2C_CLIENT_INSMOD_1(lm80);
46 /* Many LM80 constants specified below */
48 /* The LM80 registers */
49 #define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
50 #define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
51 #define LM80_REG_IN(nr) (0x20 + (nr))
53 #define LM80_REG_FAN1 0x28
54 #define LM80_REG_FAN2 0x29
55 #define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
57 #define LM80_REG_TEMP 0x27
58 #define LM80_REG_TEMP_HOT_MAX 0x38
59 #define LM80_REG_TEMP_HOT_HYST 0x39
60 #define LM80_REG_TEMP_OS_MAX 0x3a
61 #define LM80_REG_TEMP_OS_HYST 0x3b
63 #define LM80_REG_CONFIG 0x00
64 #define LM80_REG_ALARM1 0x01
65 #define LM80_REG_ALARM2 0x02
66 #define LM80_REG_MASK1 0x03
67 #define LM80_REG_MASK2 0x04
68 #define LM80_REG_FANDIV 0x05
69 #define LM80_REG_RES 0x06
72 /* Conversions. Rounding and limit checking is only done on the TO_REG
73 variants. Note that you should be a bit careful with which arguments
74 these macros are called: arguments may be evaluated more than once.
75 Fixing this is just not worth it. */
77 #define IN_TO_REG(val) (SENSORS_LIMIT(((val)+5)/10,0,255))
78 #define IN_FROM_REG(val) ((val)*10)
80 static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
82 if (rpm == 0)
83 return 255;
84 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
85 return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254);
88 #define FAN_FROM_REG(val,div) ((val)==0?-1:\
89 (val)==255?0:1350000/((div)*(val)))
91 static inline long TEMP_FROM_REG(u16 temp)
93 long res;
95 temp >>= 4;
96 if (temp < 0x0800)
97 res = 625 * (long) temp;
98 else
99 res = ((long) temp - 0x01000) * 625;
101 return res / 10;
104 #define TEMP_LIMIT_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
106 #define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val)<0?\
107 ((val)-500)/1000:((val)+500)/1000,0,255)
109 #define DIV_FROM_REG(val) (1 << (val))
112 * Client data (each client gets its own)
115 struct lm80_data {
116 struct i2c_client client;
117 struct device *hwmon_dev;
118 struct mutex update_lock;
119 char valid; /* !=0 if following fields are valid */
120 unsigned long last_updated; /* In jiffies */
122 u8 in[7]; /* Register value */
123 u8 in_max[7]; /* Register value */
124 u8 in_min[7]; /* Register value */
125 u8 fan[2]; /* Register value */
126 u8 fan_min[2]; /* Register value */
127 u8 fan_div[2]; /* Register encoding, shifted right */
128 u16 temp; /* Register values, shifted right */
129 u8 temp_hot_max; /* Register value */
130 u8 temp_hot_hyst; /* Register value */
131 u8 temp_os_max; /* Register value */
132 u8 temp_os_hyst; /* Register value */
133 u16 alarms; /* Register encoding, combined */
137 * Functions declaration
140 static int lm80_attach_adapter(struct i2c_adapter *adapter);
141 static int lm80_detect(struct i2c_adapter *adapter, int address, int kind);
142 static void lm80_init_client(struct i2c_client *client);
143 static int lm80_detach_client(struct i2c_client *client);
144 static struct lm80_data *lm80_update_device(struct device *dev);
145 static int lm80_read_value(struct i2c_client *client, u8 reg);
146 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
149 * Driver data (common to all clients)
152 static struct i2c_driver lm80_driver = {
153 .driver = {
154 .name = "lm80",
156 .attach_adapter = lm80_attach_adapter,
157 .detach_client = lm80_detach_client,
161 * Sysfs stuff
164 #define show_in(suffix, value) \
165 static ssize_t show_in_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
167 int nr = to_sensor_dev_attr(attr)->index; \
168 struct lm80_data *data = lm80_update_device(dev); \
169 return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
171 show_in(min, in_min)
172 show_in(max, in_max)
173 show_in(input, in)
175 #define set_in(suffix, value, reg) \
176 static ssize_t set_in_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
177 size_t count) \
179 int nr = to_sensor_dev_attr(attr)->index; \
180 struct i2c_client *client = to_i2c_client(dev); \
181 struct lm80_data *data = i2c_get_clientdata(client); \
182 long val = simple_strtol(buf, NULL, 10); \
184 mutex_lock(&data->update_lock);\
185 data->value[nr] = IN_TO_REG(val); \
186 lm80_write_value(client, reg(nr), data->value[nr]); \
187 mutex_unlock(&data->update_lock);\
188 return count; \
190 set_in(min, in_min, LM80_REG_IN_MIN)
191 set_in(max, in_max, LM80_REG_IN_MAX)
193 #define show_fan(suffix, value) \
194 static ssize_t show_fan_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
196 int nr = to_sensor_dev_attr(attr)->index; \
197 struct lm80_data *data = lm80_update_device(dev); \
198 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
199 DIV_FROM_REG(data->fan_div[nr]))); \
201 show_fan(min, fan_min)
202 show_fan(input, fan)
204 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
205 char *buf)
207 int nr = to_sensor_dev_attr(attr)->index;
208 struct lm80_data *data = lm80_update_device(dev);
209 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
212 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
213 const char *buf, size_t count)
215 int nr = to_sensor_dev_attr(attr)->index;
216 struct i2c_client *client = to_i2c_client(dev);
217 struct lm80_data *data = i2c_get_clientdata(client);
218 long val = simple_strtoul(buf, NULL, 10);
220 mutex_lock(&data->update_lock);
221 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
222 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
223 mutex_unlock(&data->update_lock);
224 return count;
227 /* Note: we save and restore the fan minimum here, because its value is
228 determined in part by the fan divisor. This follows the principle of
229 least surprise; the user doesn't expect the fan minimum to change just
230 because the divisor changed. */
231 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
232 const char *buf, size_t count)
234 int nr = to_sensor_dev_attr(attr)->index;
235 struct i2c_client *client = to_i2c_client(dev);
236 struct lm80_data *data = i2c_get_clientdata(client);
237 unsigned long min, val = simple_strtoul(buf, NULL, 10);
238 u8 reg;
240 /* Save fan_min */
241 mutex_lock(&data->update_lock);
242 min = FAN_FROM_REG(data->fan_min[nr],
243 DIV_FROM_REG(data->fan_div[nr]));
245 switch (val) {
246 case 1: data->fan_div[nr] = 0; break;
247 case 2: data->fan_div[nr] = 1; break;
248 case 4: data->fan_div[nr] = 2; break;
249 case 8: data->fan_div[nr] = 3; break;
250 default:
251 dev_err(&client->dev, "fan_div value %ld not "
252 "supported. Choose one of 1, 2, 4 or 8!\n", val);
253 mutex_unlock(&data->update_lock);
254 return -EINVAL;
257 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
258 | (data->fan_div[nr] << (2 * (nr + 1)));
259 lm80_write_value(client, LM80_REG_FANDIV, reg);
261 /* Restore fan_min */
262 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
263 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
264 mutex_unlock(&data->update_lock);
266 return count;
269 static ssize_t show_temp_input1(struct device *dev, struct device_attribute *attr, char *buf)
271 struct lm80_data *data = lm80_update_device(dev);
272 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
275 #define show_temp(suffix, value) \
276 static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
278 struct lm80_data *data = lm80_update_device(dev); \
279 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
281 show_temp(hot_max, temp_hot_max);
282 show_temp(hot_hyst, temp_hot_hyst);
283 show_temp(os_max, temp_os_max);
284 show_temp(os_hyst, temp_os_hyst);
286 #define set_temp(suffix, value, reg) \
287 static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
288 size_t count) \
290 struct i2c_client *client = to_i2c_client(dev); \
291 struct lm80_data *data = i2c_get_clientdata(client); \
292 long val = simple_strtoul(buf, NULL, 10); \
294 mutex_lock(&data->update_lock); \
295 data->value = TEMP_LIMIT_TO_REG(val); \
296 lm80_write_value(client, reg, data->value); \
297 mutex_unlock(&data->update_lock); \
298 return count; \
300 set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
301 set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
302 set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
303 set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
305 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
306 char *buf)
308 struct lm80_data *data = lm80_update_device(dev);
309 return sprintf(buf, "%u\n", data->alarms);
312 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
313 char *buf)
315 int bitnr = to_sensor_dev_attr(attr)->index;
316 struct lm80_data *data = lm80_update_device(dev);
317 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
320 static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
321 show_in_min, set_in_min, 0);
322 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
323 show_in_min, set_in_min, 1);
324 static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
325 show_in_min, set_in_min, 2);
326 static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
327 show_in_min, set_in_min, 3);
328 static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
329 show_in_min, set_in_min, 4);
330 static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
331 show_in_min, set_in_min, 5);
332 static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
333 show_in_min, set_in_min, 6);
334 static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO,
335 show_in_max, set_in_max, 0);
336 static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
337 show_in_max, set_in_max, 1);
338 static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
339 show_in_max, set_in_max, 2);
340 static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
341 show_in_max, set_in_max, 3);
342 static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
343 show_in_max, set_in_max, 4);
344 static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
345 show_in_max, set_in_max, 5);
346 static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
347 show_in_max, set_in_max, 6);
348 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0);
349 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1);
350 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2);
351 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3);
352 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4);
353 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5);
354 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6);
355 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
356 show_fan_min, set_fan_min, 0);
357 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
358 show_fan_min, set_fan_min, 1);
359 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
360 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
361 static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
362 show_fan_div, set_fan_div, 0);
363 static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
364 show_fan_div, set_fan_div, 1);
365 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
366 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
367 set_temp_hot_max);
368 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
369 set_temp_hot_hyst);
370 static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
371 set_temp_os_max);
372 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
373 set_temp_os_hyst);
374 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
375 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
376 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
377 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
378 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
379 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
380 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
381 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
382 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
383 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
384 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
385 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
388 * Real code
391 static int lm80_attach_adapter(struct i2c_adapter *adapter)
393 if (!(adapter->class & I2C_CLASS_HWMON))
394 return 0;
395 return i2c_probe(adapter, &addr_data, lm80_detect);
398 static struct attribute *lm80_attributes[] = {
399 &sensor_dev_attr_in0_min.dev_attr.attr,
400 &sensor_dev_attr_in1_min.dev_attr.attr,
401 &sensor_dev_attr_in2_min.dev_attr.attr,
402 &sensor_dev_attr_in3_min.dev_attr.attr,
403 &sensor_dev_attr_in4_min.dev_attr.attr,
404 &sensor_dev_attr_in5_min.dev_attr.attr,
405 &sensor_dev_attr_in6_min.dev_attr.attr,
406 &sensor_dev_attr_in0_max.dev_attr.attr,
407 &sensor_dev_attr_in1_max.dev_attr.attr,
408 &sensor_dev_attr_in2_max.dev_attr.attr,
409 &sensor_dev_attr_in3_max.dev_attr.attr,
410 &sensor_dev_attr_in4_max.dev_attr.attr,
411 &sensor_dev_attr_in5_max.dev_attr.attr,
412 &sensor_dev_attr_in6_max.dev_attr.attr,
413 &sensor_dev_attr_in0_input.dev_attr.attr,
414 &sensor_dev_attr_in1_input.dev_attr.attr,
415 &sensor_dev_attr_in2_input.dev_attr.attr,
416 &sensor_dev_attr_in3_input.dev_attr.attr,
417 &sensor_dev_attr_in4_input.dev_attr.attr,
418 &sensor_dev_attr_in5_input.dev_attr.attr,
419 &sensor_dev_attr_in6_input.dev_attr.attr,
420 &sensor_dev_attr_fan1_min.dev_attr.attr,
421 &sensor_dev_attr_fan2_min.dev_attr.attr,
422 &sensor_dev_attr_fan1_input.dev_attr.attr,
423 &sensor_dev_attr_fan2_input.dev_attr.attr,
424 &sensor_dev_attr_fan1_div.dev_attr.attr,
425 &sensor_dev_attr_fan2_div.dev_attr.attr,
426 &dev_attr_temp1_input.attr,
427 &dev_attr_temp1_max.attr,
428 &dev_attr_temp1_max_hyst.attr,
429 &dev_attr_temp1_crit.attr,
430 &dev_attr_temp1_crit_hyst.attr,
431 &dev_attr_alarms.attr,
432 &sensor_dev_attr_in0_alarm.dev_attr.attr,
433 &sensor_dev_attr_in1_alarm.dev_attr.attr,
434 &sensor_dev_attr_in2_alarm.dev_attr.attr,
435 &sensor_dev_attr_in3_alarm.dev_attr.attr,
436 &sensor_dev_attr_in4_alarm.dev_attr.attr,
437 &sensor_dev_attr_in5_alarm.dev_attr.attr,
438 &sensor_dev_attr_in6_alarm.dev_attr.attr,
439 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
440 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
441 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
442 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
443 NULL
446 static const struct attribute_group lm80_group = {
447 .attrs = lm80_attributes,
450 static int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
452 int i, cur;
453 struct i2c_client *client;
454 struct lm80_data *data;
455 int err = 0;
456 const char *name;
458 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
459 goto exit;
461 /* OK. For now, we presume we have a valid client. We now create the
462 client structure, even though we cannot fill it completely yet.
463 But it allows us to access lm80_{read,write}_value. */
464 if (!(data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL))) {
465 err = -ENOMEM;
466 goto exit;
469 client = &data->client;
470 i2c_set_clientdata(client, data);
471 client->addr = address;
472 client->adapter = adapter;
473 client->driver = &lm80_driver;
475 /* Now, we do the remaining detection. It is lousy. */
476 if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
477 goto error_free;
478 for (i = 0x2a; i <= 0x3d; i++) {
479 cur = i2c_smbus_read_byte_data(client, i);
480 if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
481 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
482 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
483 goto error_free;
486 /* Determine the chip type - only one kind supported! */
487 kind = lm80;
488 name = "lm80";
490 /* Fill in the remaining client fields */
491 strlcpy(client->name, name, I2C_NAME_SIZE);
492 mutex_init(&data->update_lock);
494 /* Tell the I2C layer a new client has arrived */
495 if ((err = i2c_attach_client(client)))
496 goto error_free;
498 /* Initialize the LM80 chip */
499 lm80_init_client(client);
501 /* A few vars need to be filled upon startup */
502 data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
503 data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
505 /* Register sysfs hooks */
506 if ((err = sysfs_create_group(&client->dev.kobj, &lm80_group)))
507 goto error_detach;
509 data->hwmon_dev = hwmon_device_register(&client->dev);
510 if (IS_ERR(data->hwmon_dev)) {
511 err = PTR_ERR(data->hwmon_dev);
512 goto error_remove;
515 return 0;
517 error_remove:
518 sysfs_remove_group(&client->dev.kobj, &lm80_group);
519 error_detach:
520 i2c_detach_client(client);
521 error_free:
522 kfree(data);
523 exit:
524 return err;
527 static int lm80_detach_client(struct i2c_client *client)
529 struct lm80_data *data = i2c_get_clientdata(client);
530 int err;
532 hwmon_device_unregister(data->hwmon_dev);
533 sysfs_remove_group(&client->dev.kobj, &lm80_group);
534 if ((err = i2c_detach_client(client)))
535 return err;
537 kfree(data);
538 return 0;
541 static int lm80_read_value(struct i2c_client *client, u8 reg)
543 return i2c_smbus_read_byte_data(client, reg);
546 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
548 return i2c_smbus_write_byte_data(client, reg, value);
551 /* Called when we have found a new LM80. */
552 static void lm80_init_client(struct i2c_client *client)
554 /* Reset all except Watchdog values and last conversion values
555 This sets fan-divs to 2, among others. This makes most other
556 initializations unnecessary */
557 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
558 /* Set 11-bit temperature resolution */
559 lm80_write_value(client, LM80_REG_RES, 0x08);
561 /* Start monitoring */
562 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
565 static struct lm80_data *lm80_update_device(struct device *dev)
567 struct i2c_client *client = to_i2c_client(dev);
568 struct lm80_data *data = i2c_get_clientdata(client);
569 int i;
571 mutex_lock(&data->update_lock);
573 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
574 dev_dbg(&client->dev, "Starting lm80 update\n");
575 for (i = 0; i <= 6; i++) {
576 data->in[i] =
577 lm80_read_value(client, LM80_REG_IN(i));
578 data->in_min[i] =
579 lm80_read_value(client, LM80_REG_IN_MIN(i));
580 data->in_max[i] =
581 lm80_read_value(client, LM80_REG_IN_MAX(i));
583 data->fan[0] = lm80_read_value(client, LM80_REG_FAN1);
584 data->fan_min[0] =
585 lm80_read_value(client, LM80_REG_FAN_MIN(1));
586 data->fan[1] = lm80_read_value(client, LM80_REG_FAN2);
587 data->fan_min[1] =
588 lm80_read_value(client, LM80_REG_FAN_MIN(2));
590 data->temp =
591 (lm80_read_value(client, LM80_REG_TEMP) << 8) |
592 (lm80_read_value(client, LM80_REG_RES) & 0xf0);
593 data->temp_os_max =
594 lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
595 data->temp_os_hyst =
596 lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
597 data->temp_hot_max =
598 lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
599 data->temp_hot_hyst =
600 lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
602 i = lm80_read_value(client, LM80_REG_FANDIV);
603 data->fan_div[0] = (i >> 2) & 0x03;
604 data->fan_div[1] = (i >> 4) & 0x03;
605 data->alarms = lm80_read_value(client, LM80_REG_ALARM1) +
606 (lm80_read_value(client, LM80_REG_ALARM2) << 8);
607 data->last_updated = jiffies;
608 data->valid = 1;
611 mutex_unlock(&data->update_lock);
613 return data;
616 static int __init sensors_lm80_init(void)
618 return i2c_add_driver(&lm80_driver);
621 static void __exit sensors_lm80_exit(void)
623 i2c_del_driver(&lm80_driver);
626 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
627 "Philip Edelbrock <phil@netroedge.com>");
628 MODULE_DESCRIPTION("LM80 driver");
629 MODULE_LICENSE("GPL");
631 module_init(sensors_lm80_init);
632 module_exit(sensors_lm80_exit);