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