[PATCH] I2C hwmon: add hwmon sysfs class to drivers
[deliverable/linux.git] / drivers / hwmon / lm80.c
CommitLineData
1da177e4
LT
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>
6 *
7 * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
8 *
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.
13 *
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.
18 *
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.
22 */
23
1da177e4
LT
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/i2c-sensor.h>
943b0830
MH
30#include <linux/hwmon.h>
31#include <linux/err.h>
1da177e4
LT
32
33/* Addresses to scan */
34static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c,
35 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
36static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
37
38/* Insmod parameters */
39SENSORS_INSMOD_1(lm80);
40
41/* Many LM80 constants specified below */
42
43/* The LM80 registers */
44#define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
45#define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
46#define LM80_REG_IN(nr) (0x20 + (nr))
47
48#define LM80_REG_FAN1 0x28
49#define LM80_REG_FAN2 0x29
50#define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
51
52#define LM80_REG_TEMP 0x27
53#define LM80_REG_TEMP_HOT_MAX 0x38
54#define LM80_REG_TEMP_HOT_HYST 0x39
55#define LM80_REG_TEMP_OS_MAX 0x3a
56#define LM80_REG_TEMP_OS_HYST 0x3b
57
58#define LM80_REG_CONFIG 0x00
59#define LM80_REG_ALARM1 0x01
60#define LM80_REG_ALARM2 0x02
61#define LM80_REG_MASK1 0x03
62#define LM80_REG_MASK2 0x04
63#define LM80_REG_FANDIV 0x05
64#define LM80_REG_RES 0x06
65
66
67/* Conversions. Rounding and limit checking is only done on the TO_REG
68 variants. Note that you should be a bit careful with which arguments
69 these macros are called: arguments may be evaluated more than once.
70 Fixing this is just not worth it. */
71
72#define IN_TO_REG(val) (SENSORS_LIMIT(((val)+5)/10,0,255))
73#define IN_FROM_REG(val) ((val)*10)
74
75static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
76{
77 if (rpm == 0)
78 return 255;
79 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
80 return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254);
81}
82
83#define FAN_FROM_REG(val,div) ((val)==0?-1:\
84 (val)==255?0:1350000/((div)*(val)))
85
86static inline long TEMP_FROM_REG(u16 temp)
87{
88 long res;
89
90 temp >>= 4;
91 if (temp < 0x0800)
92 res = 625 * (long) temp;
93 else
94 res = ((long) temp - 0x01000) * 625;
95
96 return res / 10;
97}
98
99#define TEMP_LIMIT_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
100
101#define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val)<0?\
102 ((val)-500)/1000:((val)+500)/1000,0,255)
103
104#define DIV_FROM_REG(val) (1 << (val))
105
106/*
107 * Client data (each client gets its own)
108 */
109
110struct lm80_data {
111 struct i2c_client client;
943b0830 112 struct class_device *class_dev;
1da177e4
LT
113 struct semaphore update_lock;
114 char valid; /* !=0 if following fields are valid */
115 unsigned long last_updated; /* In jiffies */
116
117 u8 in[7]; /* Register value */
118 u8 in_max[7]; /* Register value */
119 u8 in_min[7]; /* Register value */
120 u8 fan[2]; /* Register value */
121 u8 fan_min[2]; /* Register value */
122 u8 fan_div[2]; /* Register encoding, shifted right */
123 u16 temp; /* Register values, shifted right */
124 u8 temp_hot_max; /* Register value */
125 u8 temp_hot_hyst; /* Register value */
126 u8 temp_os_max; /* Register value */
127 u8 temp_os_hyst; /* Register value */
128 u16 alarms; /* Register encoding, combined */
129};
130
131/*
132 * Functions declaration
133 */
134
135static int lm80_attach_adapter(struct i2c_adapter *adapter);
136static int lm80_detect(struct i2c_adapter *adapter, int address, int kind);
137static void lm80_init_client(struct i2c_client *client);
138static int lm80_detach_client(struct i2c_client *client);
139static struct lm80_data *lm80_update_device(struct device *dev);
140static int lm80_read_value(struct i2c_client *client, u8 reg);
141static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
142
143/*
144 * Driver data (common to all clients)
145 */
146
147static struct i2c_driver lm80_driver = {
148 .owner = THIS_MODULE,
149 .name = "lm80",
150 .id = I2C_DRIVERID_LM80,
151 .flags = I2C_DF_NOTIFY,
152 .attach_adapter = lm80_attach_adapter,
153 .detach_client = lm80_detach_client,
154};
155
156/*
157 * Sysfs stuff
158 */
159
160#define show_in(suffix, value) \
8627f9ba 161static ssize_t show_in_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
162{ \
163 struct lm80_data *data = lm80_update_device(dev); \
164 return sprintf(buf, "%d\n", IN_FROM_REG(data->value)); \
165}
166show_in(min0, in_min[0]);
167show_in(min1, in_min[1]);
168show_in(min2, in_min[2]);
169show_in(min3, in_min[3]);
170show_in(min4, in_min[4]);
171show_in(min5, in_min[5]);
172show_in(min6, in_min[6]);
173show_in(max0, in_max[0]);
174show_in(max1, in_max[1]);
175show_in(max2, in_max[2]);
176show_in(max3, in_max[3]);
177show_in(max4, in_max[4]);
178show_in(max5, in_max[5]);
179show_in(max6, in_max[6]);
180show_in(input0, in[0]);
181show_in(input1, in[1]);
182show_in(input2, in[2]);
183show_in(input3, in[3]);
184show_in(input4, in[4]);
185show_in(input5, in[5]);
186show_in(input6, in[6]);
187
188#define set_in(suffix, value, reg) \
8627f9ba 189static ssize_t set_in_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
190 size_t count) \
191{ \
192 struct i2c_client *client = to_i2c_client(dev); \
193 struct lm80_data *data = i2c_get_clientdata(client); \
194 long val = simple_strtol(buf, NULL, 10); \
195 \
196 down(&data->update_lock);\
197 data->value = IN_TO_REG(val); \
198 lm80_write_value(client, reg, data->value); \
199 up(&data->update_lock);\
200 return count; \
201}
202set_in(min0, in_min[0], LM80_REG_IN_MIN(0));
203set_in(min1, in_min[1], LM80_REG_IN_MIN(1));
204set_in(min2, in_min[2], LM80_REG_IN_MIN(2));
205set_in(min3, in_min[3], LM80_REG_IN_MIN(3));
206set_in(min4, in_min[4], LM80_REG_IN_MIN(4));
207set_in(min5, in_min[5], LM80_REG_IN_MIN(5));
208set_in(min6, in_min[6], LM80_REG_IN_MIN(6));
209set_in(max0, in_max[0], LM80_REG_IN_MAX(0));
210set_in(max1, in_max[1], LM80_REG_IN_MAX(1));
211set_in(max2, in_max[2], LM80_REG_IN_MAX(2));
212set_in(max3, in_max[3], LM80_REG_IN_MAX(3));
213set_in(max4, in_max[4], LM80_REG_IN_MAX(4));
214set_in(max5, in_max[5], LM80_REG_IN_MAX(5));
215set_in(max6, in_max[6], LM80_REG_IN_MAX(6));
216
217#define show_fan(suffix, value, div) \
8627f9ba 218static ssize_t show_fan_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
219{ \
220 struct lm80_data *data = lm80_update_device(dev); \
221 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value, \
222 DIV_FROM_REG(data->div))); \
223}
224show_fan(min1, fan_min[0], fan_div[0]);
225show_fan(min2, fan_min[1], fan_div[1]);
226show_fan(input1, fan[0], fan_div[0]);
227show_fan(input2, fan[1], fan_div[1]);
228
229#define show_fan_div(suffix, value) \
8627f9ba 230static ssize_t show_fan_div##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
231{ \
232 struct lm80_data *data = lm80_update_device(dev); \
233 return sprintf(buf, "%d\n", DIV_FROM_REG(data->value)); \
234}
235show_fan_div(1, fan_div[0]);
236show_fan_div(2, fan_div[1]);
237
238#define set_fan(suffix, value, reg, div) \
8627f9ba 239static ssize_t set_fan_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
240 size_t count) \
241{ \
242 struct i2c_client *client = to_i2c_client(dev); \
243 struct lm80_data *data = i2c_get_clientdata(client); \
244 long val = simple_strtoul(buf, NULL, 10); \
245 \
246 down(&data->update_lock);\
247 data->value = FAN_TO_REG(val, DIV_FROM_REG(data->div)); \
248 lm80_write_value(client, reg, data->value); \
249 up(&data->update_lock);\
250 return count; \
251}
252set_fan(min1, fan_min[0], LM80_REG_FAN_MIN(1), fan_div[0]);
253set_fan(min2, fan_min[1], LM80_REG_FAN_MIN(2), fan_div[1]);
254
255/* Note: we save and restore the fan minimum here, because its value is
256 determined in part by the fan divisor. This follows the principle of
257 least suprise; the user doesn't expect the fan minimum to change just
258 because the divisor changed. */
259static ssize_t set_fan_div(struct device *dev, const char *buf,
260 size_t count, int nr)
261{
262 struct i2c_client *client = to_i2c_client(dev);
263 struct lm80_data *data = i2c_get_clientdata(client);
264 unsigned long min, val = simple_strtoul(buf, NULL, 10);
265 u8 reg;
266
267 /* Save fan_min */
268 down(&data->update_lock);
269 min = FAN_FROM_REG(data->fan_min[nr],
270 DIV_FROM_REG(data->fan_div[nr]));
271
272 switch (val) {
273 case 1: data->fan_div[nr] = 0; break;
274 case 2: data->fan_div[nr] = 1; break;
275 case 4: data->fan_div[nr] = 2; break;
276 case 8: data->fan_div[nr] = 3; break;
277 default:
278 dev_err(&client->dev, "fan_div value %ld not "
279 "supported. Choose one of 1, 2, 4 or 8!\n", val);
280 up(&data->update_lock);
281 return -EINVAL;
282 }
283
284 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
285 | (data->fan_div[nr] << (2 * (nr + 1)));
286 lm80_write_value(client, LM80_REG_FANDIV, reg);
287
288 /* Restore fan_min */
289 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
290 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
291 up(&data->update_lock);
292
293 return count;
294}
295
296#define set_fan_div(number) \
8627f9ba 297static ssize_t set_fan_div##number(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
298 size_t count) \
299{ \
300 return set_fan_div(dev, buf, count, number - 1); \
301}
302set_fan_div(1);
303set_fan_div(2);
304
8627f9ba 305static ssize_t show_temp_input1(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
306{
307 struct lm80_data *data = lm80_update_device(dev);
308 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
309}
310
311#define show_temp(suffix, value) \
8627f9ba 312static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
313{ \
314 struct lm80_data *data = lm80_update_device(dev); \
315 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
316}
317show_temp(hot_max, temp_hot_max);
318show_temp(hot_hyst, temp_hot_hyst);
319show_temp(os_max, temp_os_max);
320show_temp(os_hyst, temp_os_hyst);
321
322#define set_temp(suffix, value, reg) \
8627f9ba 323static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
324 size_t count) \
325{ \
326 struct i2c_client *client = to_i2c_client(dev); \
327 struct lm80_data *data = i2c_get_clientdata(client); \
328 long val = simple_strtoul(buf, NULL, 10); \
329 \
330 down(&data->update_lock); \
331 data->value = TEMP_LIMIT_TO_REG(val); \
332 lm80_write_value(client, reg, data->value); \
333 up(&data->update_lock); \
334 return count; \
335}
336set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
337set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
338set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
339set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
340
8627f9ba 341static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
342{
343 struct lm80_data *data = lm80_update_device(dev);
344 return sprintf(buf, "%u\n", data->alarms);
345}
346
347static DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min0, set_in_min0);
348static DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min1, set_in_min1);
349static DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min2, set_in_min2);
350static DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min3, set_in_min3);
351static DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min4, set_in_min4);
352static DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min5, set_in_min5);
353static DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min6, set_in_min6);
354static DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max0, set_in_max0);
355static DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max1, set_in_max1);
356static DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max2, set_in_max2);
357static DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max3, set_in_max3);
358static DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max4, set_in_max4);
359static DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max5, set_in_max5);
360static DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max6, set_in_max6);
361static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
362static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
363static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
364static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
365static DEVICE_ATTR(in4_input, S_IRUGO, show_in_input4, NULL);
366static DEVICE_ATTR(in5_input, S_IRUGO, show_in_input5, NULL);
367static DEVICE_ATTR(in6_input, S_IRUGO, show_in_input6, NULL);
368static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min1,
369 set_fan_min1);
370static DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min2,
371 set_fan_min2);
372static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
373static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
374static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div1, set_fan_div1);
375static DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div2, set_fan_div2);
376static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
377static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
378 set_temp_hot_max);
379static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
380 set_temp_hot_hyst);
381static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
382 set_temp_os_max);
383static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
384 set_temp_os_hyst);
385static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
386
387/*
388 * Real code
389 */
390
391static int lm80_attach_adapter(struct i2c_adapter *adapter)
392{
393 if (!(adapter->class & I2C_CLASS_HWMON))
394 return 0;
395 return i2c_detect(adapter, &addr_data, lm80_detect);
396}
397
398int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
399{
400 int i, cur;
401 struct i2c_client *new_client;
402 struct lm80_data *data;
403 int err = 0;
404 const char *name;
405
406 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
407 goto exit;
408
409 /* OK. For now, we presume we have a valid client. We now create the
410 client structure, even though we cannot fill it completely yet.
411 But it allows us to access lm80_{read,write}_value. */
412 if (!(data = kmalloc(sizeof(struct lm80_data), GFP_KERNEL))) {
413 err = -ENOMEM;
414 goto exit;
415 }
416 memset(data, 0, sizeof(struct lm80_data));
417
418 new_client = &data->client;
419 i2c_set_clientdata(new_client, data);
420 new_client->addr = address;
421 new_client->adapter = adapter;
422 new_client->driver = &lm80_driver;
423 new_client->flags = 0;
424
425 /* Now, we do the remaining detection. It is lousy. */
426 if (lm80_read_value(new_client, LM80_REG_ALARM2) & 0xc0)
427 goto error_free;
428 for (i = 0x2a; i <= 0x3d; i++) {
429 cur = i2c_smbus_read_byte_data(new_client, i);
430 if ((i2c_smbus_read_byte_data(new_client, i + 0x40) != cur)
431 || (i2c_smbus_read_byte_data(new_client, i + 0x80) != cur)
432 || (i2c_smbus_read_byte_data(new_client, i + 0xc0) != cur))
433 goto error_free;
434 }
435
436 /* Determine the chip type - only one kind supported! */
437 kind = lm80;
438 name = "lm80";
439
440 /* Fill in the remaining client fields and put it into the global list */
441 strlcpy(new_client->name, name, I2C_NAME_SIZE);
442 data->valid = 0;
443 init_MUTEX(&data->update_lock);
444
445 /* Tell the I2C layer a new client has arrived */
446 if ((err = i2c_attach_client(new_client)))
447 goto error_free;
448
449 /* Initialize the LM80 chip */
450 lm80_init_client(new_client);
451
452 /* A few vars need to be filled upon startup */
453 data->fan_min[0] = lm80_read_value(new_client, LM80_REG_FAN_MIN(1));
454 data->fan_min[1] = lm80_read_value(new_client, LM80_REG_FAN_MIN(2));
455
456 /* Register sysfs hooks */
943b0830
MH
457 data->class_dev = hwmon_device_register(&new_client->dev);
458 if (IS_ERR(data->class_dev)) {
459 err = PTR_ERR(data->class_dev);
460 goto error_detach;
461 }
462
1da177e4
LT
463 device_create_file(&new_client->dev, &dev_attr_in0_min);
464 device_create_file(&new_client->dev, &dev_attr_in1_min);
465 device_create_file(&new_client->dev, &dev_attr_in2_min);
466 device_create_file(&new_client->dev, &dev_attr_in3_min);
467 device_create_file(&new_client->dev, &dev_attr_in4_min);
468 device_create_file(&new_client->dev, &dev_attr_in5_min);
469 device_create_file(&new_client->dev, &dev_attr_in6_min);
470 device_create_file(&new_client->dev, &dev_attr_in0_max);
471 device_create_file(&new_client->dev, &dev_attr_in1_max);
472 device_create_file(&new_client->dev, &dev_attr_in2_max);
473 device_create_file(&new_client->dev, &dev_attr_in3_max);
474 device_create_file(&new_client->dev, &dev_attr_in4_max);
475 device_create_file(&new_client->dev, &dev_attr_in5_max);
476 device_create_file(&new_client->dev, &dev_attr_in6_max);
477 device_create_file(&new_client->dev, &dev_attr_in0_input);
478 device_create_file(&new_client->dev, &dev_attr_in1_input);
479 device_create_file(&new_client->dev, &dev_attr_in2_input);
480 device_create_file(&new_client->dev, &dev_attr_in3_input);
481 device_create_file(&new_client->dev, &dev_attr_in4_input);
482 device_create_file(&new_client->dev, &dev_attr_in5_input);
483 device_create_file(&new_client->dev, &dev_attr_in6_input);
484 device_create_file(&new_client->dev, &dev_attr_fan1_min);
485 device_create_file(&new_client->dev, &dev_attr_fan2_min);
486 device_create_file(&new_client->dev, &dev_attr_fan1_input);
487 device_create_file(&new_client->dev, &dev_attr_fan2_input);
488 device_create_file(&new_client->dev, &dev_attr_fan1_div);
489 device_create_file(&new_client->dev, &dev_attr_fan2_div);
490 device_create_file(&new_client->dev, &dev_attr_temp1_input);
491 device_create_file(&new_client->dev, &dev_attr_temp1_max);
492 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
493 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
494 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
495 device_create_file(&new_client->dev, &dev_attr_alarms);
496
497 return 0;
498
943b0830
MH
499error_detach:
500 i2c_detach_client(new_client);
1da177e4
LT
501error_free:
502 kfree(data);
503exit:
504 return err;
505}
506
507static int lm80_detach_client(struct i2c_client *client)
508{
943b0830 509 struct lm80_data *data = i2c_get_clientdata(client);
1da177e4
LT
510 int err;
511
943b0830
MH
512 hwmon_device_unregister(data->class_dev);
513
1da177e4
LT
514 if ((err = i2c_detach_client(client))) {
515 dev_err(&client->dev, "Client deregistration failed, "
516 "client not detached.\n");
517 return err;
518 }
519
943b0830 520 kfree(data);
1da177e4
LT
521 return 0;
522}
523
524static int lm80_read_value(struct i2c_client *client, u8 reg)
525{
526 return i2c_smbus_read_byte_data(client, reg);
527}
528
529static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
530{
531 return i2c_smbus_write_byte_data(client, reg, value);
532}
533
534/* Called when we have found a new LM80. */
535static void lm80_init_client(struct i2c_client *client)
536{
537 /* Reset all except Watchdog values and last conversion values
538 This sets fan-divs to 2, among others. This makes most other
539 initializations unnecessary */
540 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
541 /* Set 11-bit temperature resolution */
542 lm80_write_value(client, LM80_REG_RES, 0x08);
543
544 /* Start monitoring */
545 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
546}
547
548static struct lm80_data *lm80_update_device(struct device *dev)
549{
550 struct i2c_client *client = to_i2c_client(dev);
551 struct lm80_data *data = i2c_get_clientdata(client);
552 int i;
553
554 down(&data->update_lock);
555
556 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
557 dev_dbg(&client->dev, "Starting lm80 update\n");
558 for (i = 0; i <= 6; i++) {
559 data->in[i] =
560 lm80_read_value(client, LM80_REG_IN(i));
561 data->in_min[i] =
562 lm80_read_value(client, LM80_REG_IN_MIN(i));
563 data->in_max[i] =
564 lm80_read_value(client, LM80_REG_IN_MAX(i));
565 }
566 data->fan[0] = lm80_read_value(client, LM80_REG_FAN1);
567 data->fan_min[0] =
568 lm80_read_value(client, LM80_REG_FAN_MIN(1));
569 data->fan[1] = lm80_read_value(client, LM80_REG_FAN2);
570 data->fan_min[1] =
571 lm80_read_value(client, LM80_REG_FAN_MIN(2));
572
573 data->temp =
574 (lm80_read_value(client, LM80_REG_TEMP) << 8) |
575 (lm80_read_value(client, LM80_REG_RES) & 0xf0);
576 data->temp_os_max =
577 lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
578 data->temp_os_hyst =
579 lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
580 data->temp_hot_max =
581 lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
582 data->temp_hot_hyst =
583 lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
584
585 i = lm80_read_value(client, LM80_REG_FANDIV);
586 data->fan_div[0] = (i >> 2) & 0x03;
587 data->fan_div[1] = (i >> 4) & 0x03;
588 data->alarms = lm80_read_value(client, LM80_REG_ALARM1) +
589 (lm80_read_value(client, LM80_REG_ALARM2) << 8);
590 data->last_updated = jiffies;
591 data->valid = 1;
592 }
593
594 up(&data->update_lock);
595
596 return data;
597}
598
599static int __init sensors_lm80_init(void)
600{
601 return i2c_add_driver(&lm80_driver);
602}
603
604static void __exit sensors_lm80_exit(void)
605{
606 i2c_del_driver(&lm80_driver);
607}
608
609MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
610 "Philip Edelbrock <phil@netroedge.com>");
611MODULE_DESCRIPTION("LM80 driver");
612MODULE_LICENSE("GPL");
613
614module_init(sensors_lm80_init);
615module_exit(sensors_lm80_exit);
This page took 0.084942 seconds and 5 git commands to generate.