]> err.no Git - linux-2.6/blob - drivers/hwmon/gl520sm.c
[PATCH] I2C hwmon: add hwmon sysfs class to drivers
[linux-2.6] / drivers / hwmon / gl520sm.c
1 /*
2     gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware
3                 monitoring
4     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>,
5                               Kyösti Mälkki <kmalkki@cc.hut.fi>
6     Copyright (c) 2005        Maarten Deprez <maartendeprez@users.sourceforge.net>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23
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>
30 #include <linux/i2c-vid.h>
31 #include <linux/hwmon.h>
32 #include <linux/err.h>
33
34 /* Type of the extra sensor */
35 static unsigned short extra_sensor_type;
36 module_param(extra_sensor_type, ushort, 0);
37 MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
38
39 /* Addresses to scan */
40 static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
41 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
42
43 /* Insmod parameters */
44 SENSORS_INSMOD_1(gl520sm);
45
46 /* Many GL520 constants specified below 
47 One of the inputs can be configured as either temp or voltage.
48 That's why _TEMP2 and _IN4 access the same register 
49 */
50
51 /* The GL520 registers */
52 #define GL520_REG_CHIP_ID               0x00
53 #define GL520_REG_REVISION              0x01
54 #define GL520_REG_CONF                  0x03
55 #define GL520_REG_MASK                  0x11
56
57 #define GL520_REG_VID_INPUT             0x02
58
59 #define GL520_REG_IN0_INPUT             0x15
60 #define GL520_REG_IN0_LIMIT             0x0c
61 #define GL520_REG_IN0_MIN               GL520_REG_IN0_LIMIT
62 #define GL520_REG_IN0_MAX               GL520_REG_IN0_LIMIT
63
64 #define GL520_REG_IN1_INPUT             0x14
65 #define GL520_REG_IN1_LIMIT             0x09
66 #define GL520_REG_IN1_MIN               GL520_REG_IN1_LIMIT
67 #define GL520_REG_IN1_MAX               GL520_REG_IN1_LIMIT
68
69 #define GL520_REG_IN2_INPUT             0x13
70 #define GL520_REG_IN2_LIMIT             0x0a
71 #define GL520_REG_IN2_MIN               GL520_REG_IN2_LIMIT
72 #define GL520_REG_IN2_MAX               GL520_REG_IN2_LIMIT
73
74 #define GL520_REG_IN3_INPUT             0x0d
75 #define GL520_REG_IN3_LIMIT             0x0b
76 #define GL520_REG_IN3_MIN               GL520_REG_IN3_LIMIT
77 #define GL520_REG_IN3_MAX               GL520_REG_IN3_LIMIT
78
79 #define GL520_REG_IN4_INPUT             0x0e
80 #define GL520_REG_IN4_MAX               0x17
81 #define GL520_REG_IN4_MIN               0x18
82
83 #define GL520_REG_TEMP1_INPUT           0x04
84 #define GL520_REG_TEMP1_MAX             0x05
85 #define GL520_REG_TEMP1_MAX_HYST        0x06
86
87 #define GL520_REG_TEMP2_INPUT           0x0e
88 #define GL520_REG_TEMP2_MAX             0x17
89 #define GL520_REG_TEMP2_MAX_HYST        0x18
90
91 #define GL520_REG_FAN_INPUT             0x07
92 #define GL520_REG_FAN_MIN               0x08
93 #define GL520_REG_FAN_DIV               0x0f
94 #define GL520_REG_FAN_OFF               GL520_REG_FAN_DIV
95
96 #define GL520_REG_ALARMS                0x12
97 #define GL520_REG_BEEP_MASK             0x10
98 #define GL520_REG_BEEP_ENABLE           GL520_REG_CONF
99
100 /*
101  * Function declarations
102  */
103
104 static int gl520_attach_adapter(struct i2c_adapter *adapter);
105 static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
106 static void gl520_init_client(struct i2c_client *client);
107 static int gl520_detach_client(struct i2c_client *client);
108 static int gl520_read_value(struct i2c_client *client, u8 reg);
109 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
110 static struct gl520_data *gl520_update_device(struct device *dev);
111
112 /* Driver data */
113 static struct i2c_driver gl520_driver = {
114         .owner          = THIS_MODULE,
115         .name           = "gl520sm",
116         .id             = I2C_DRIVERID_GL520,
117         .flags          = I2C_DF_NOTIFY,
118         .attach_adapter = gl520_attach_adapter,
119         .detach_client  = gl520_detach_client,
120 };
121
122 /* Client data */
123 struct gl520_data {
124         struct i2c_client client;
125         struct class_device *class_dev;
126         struct semaphore update_lock;
127         char valid;             /* zero until the following fields are valid */
128         unsigned long last_updated;     /* in jiffies */
129
130         u8 vid;
131         u8 vrm;
132         u8 in_input[5];         /* [0] = VVD */
133         u8 in_min[5];           /* [0] = VDD */
134         u8 in_max[5];           /* [0] = VDD */
135         u8 fan_input[2];
136         u8 fan_min[2];
137         u8 fan_div[2];
138         u8 fan_off;
139         u8 temp_input[2];
140         u8 temp_max[2];
141         u8 temp_max_hyst[2];
142         u8 alarms;
143         u8 beep_enable;
144         u8 beep_mask;
145         u8 alarm_mask;
146         u8 two_temps;
147 };
148
149 /*
150  * Sysfs stuff
151  */
152
153 #define sysfs_r(type, n, item, reg) \
154 static ssize_t get_##type##item (struct gl520_data *, char *, int); \
155 static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \
156 static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \
157 { \
158         struct gl520_data *data = gl520_update_device(dev); \
159         return get_##type##item(data, buf, (n)); \
160 }
161
162 #define sysfs_w(type, n, item, reg) \
163 static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \
164 static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \
165 static ssize_t set_##type##n##item (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
166 { \
167         struct i2c_client *client = to_i2c_client(dev); \
168         struct gl520_data *data = i2c_get_clientdata(client); \
169         return set_##type##item(client, data, buf, count, (n), reg); \
170 }
171
172 #define sysfs_rw_n(type, n, item, reg) \
173 sysfs_r(type, n, item, reg) \
174 sysfs_w(type, n, item, reg) \
175 static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item);
176
177 #define sysfs_ro_n(type, n, item, reg) \
178 sysfs_r(type, n, item, reg) \
179 static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL);
180
181 #define sysfs_rw(type, item, reg) \
182 sysfs_r(type, 0, item, reg) \
183 sysfs_w(type, 0, item, reg) \
184 static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item);
185
186 #define sysfs_ro(type, item, reg) \
187 sysfs_r(type, 0, item, reg) \
188 static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL);
189
190
191 #define sysfs_vid(n) \
192 sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT)
193
194 #define device_create_file_vid(client, n) \
195 device_create_file(&client->dev, &dev_attr_cpu##n##_vid)
196
197 #define sysfs_in(n) \
198 sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \
199 sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \
200 sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX) \
201
202 #define device_create_file_in(client, n) \
203 ({device_create_file(&client->dev, &dev_attr_in##n##_input); \
204 device_create_file(&client->dev, &dev_attr_in##n##_min); \
205 device_create_file(&client->dev, &dev_attr_in##n##_max);})
206
207 #define sysfs_fan(n) \
208 sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \
209 sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \
210 sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV)
211
212 #define device_create_file_fan(client, n) \
213 ({device_create_file(&client->dev, &dev_attr_fan##n##_input); \
214 device_create_file(&client->dev, &dev_attr_fan##n##_min); \
215 device_create_file(&client->dev, &dev_attr_fan##n##_div);})
216
217 #define sysfs_fan_off(n) \
218 sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) \
219
220 #define device_create_file_fan_off(client, n) \
221 device_create_file(&client->dev, &dev_attr_fan##n##_off)
222
223 #define sysfs_temp(n) \
224 sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \
225 sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \
226 sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST)
227
228 #define device_create_file_temp(client, n) \
229 ({device_create_file(&client->dev, &dev_attr_temp##n##_input); \
230 device_create_file(&client->dev, &dev_attr_temp##n##_max); \
231 device_create_file(&client->dev, &dev_attr_temp##n##_max_hyst);})
232
233 #define sysfs_alarms() \
234 sysfs_ro(alarms, , GL520_REG_ALARMS) \
235 sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \
236 sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK)
237
238 #define device_create_file_alarms(client) \
239 ({device_create_file(&client->dev, &dev_attr_alarms); \
240 device_create_file(&client->dev, &dev_attr_beep_enable); \
241 device_create_file(&client->dev, &dev_attr_beep_mask);})
242
243
244 sysfs_vid(0)
245
246 sysfs_in(0)
247 sysfs_in(1)
248 sysfs_in(2)
249 sysfs_in(3)
250 sysfs_in(4)
251
252 sysfs_fan(1)
253 sysfs_fan(2)
254 sysfs_fan_off(1)
255
256 sysfs_temp(1)
257 sysfs_temp(2)
258
259 sysfs_alarms()
260
261
262 static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n)
263 {
264         return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
265 }
266
267 #define VDD_FROM_REG(val) (((val)*95+2)/4)
268 #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
269
270 #define IN_FROM_REG(val) ((val)*19)
271 #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
272
273 static ssize_t get_in_input(struct gl520_data *data, char *buf, int n)
274 {
275         u8 r = data->in_input[n];
276
277         if (n == 0)
278                 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
279         else
280                 return sprintf(buf, "%d\n", IN_FROM_REG(r));
281 }
282
283 static ssize_t get_in_min(struct gl520_data *data, char *buf, int n)
284 {
285         u8 r = data->in_min[n];
286
287         if (n == 0)
288                 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
289         else
290                 return sprintf(buf, "%d\n", IN_FROM_REG(r));
291 }
292
293 static ssize_t get_in_max(struct gl520_data *data, char *buf, int n)
294 {
295         u8 r = data->in_max[n];
296
297         if (n == 0)
298                 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
299         else
300                 return sprintf(buf, "%d\n", IN_FROM_REG(r));
301 }
302
303 static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
304 {
305         long v = simple_strtol(buf, NULL, 10);
306         u8 r;
307
308         down(&data->update_lock);
309
310         if (n == 0)
311                 r = VDD_TO_REG(v);
312         else
313                 r = IN_TO_REG(v);
314
315         data->in_min[n] = r;
316
317         if (n < 4)
318                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
319         else
320                 gl520_write_value(client, reg, r);
321
322         up(&data->update_lock);
323         return count;
324 }
325
326 static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
327 {
328         long v = simple_strtol(buf, NULL, 10);
329         u8 r;
330
331         if (n == 0)
332                 r = VDD_TO_REG(v);
333         else
334                 r = IN_TO_REG(v);
335
336         down(&data->update_lock);
337
338         data->in_max[n] = r;
339
340         if (n < 4)
341                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
342         else
343                 gl520_write_value(client, reg, r);
344
345         up(&data->update_lock);
346         return count;
347 }
348
349 #define DIV_FROM_REG(val) (1 << (val))
350 #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
351 #define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
352
353 static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n)
354 {
355         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1]));
356 }
357
358 static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n)
359 {
360         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1]));
361 }
362
363 static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n)
364 {
365         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1]));
366 }
367
368 static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n)
369 {
370         return sprintf(buf, "%d\n", data->fan_off);
371 }
372
373 static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
374 {
375         unsigned long v = simple_strtoul(buf, NULL, 10);
376         u8 r;
377
378         down(&data->update_lock);
379         r = FAN_TO_REG(v, data->fan_div[n - 1]);
380         data->fan_min[n - 1] = r;
381
382         if (n == 1)
383                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
384         else
385                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
386
387         data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
388         if (data->fan_min[n - 1] == 0)
389                 data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40;
390         else
391                 data->alarm_mask |= (n == 1) ? 0x20 : 0x40;
392         data->beep_mask &= data->alarm_mask;
393         gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
394
395         up(&data->update_lock);
396         return count;
397 }
398
399 static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
400 {
401         unsigned long v = simple_strtoul(buf, NULL, 10);
402         u8 r;
403
404         switch (v) {
405         case 1: r = 0; break;
406         case 2: r = 1; break;
407         case 4: r = 2; break;
408         case 8: r = 3; break;
409         default:
410                 dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
411                 return -EINVAL;
412         }
413
414         down(&data->update_lock);
415         data->fan_div[n - 1] = r;
416
417         if (n == 1)
418                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
419         else
420                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
421
422         up(&data->update_lock);
423         return count;
424 }
425
426 static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
427 {
428         u8 r = simple_strtoul(buf, NULL, 10)?1:0;
429
430         down(&data->update_lock);
431         data->fan_off = r;
432         gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
433         up(&data->update_lock);
434         return count;
435 }
436
437 #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
438 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
439
440 static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n)
441 {
442         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1]));
443 }
444
445 static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n)
446 {
447         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1]));
448 }
449
450 static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n)
451 {
452         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1]));
453 }
454
455 static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
456 {
457         long v = simple_strtol(buf, NULL, 10);
458
459         down(&data->update_lock);
460         data->temp_max[n - 1] = TEMP_TO_REG(v);;
461         gl520_write_value(client, reg, data->temp_max[n - 1]);
462         up(&data->update_lock);
463         return count;
464 }
465
466 static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
467 {
468         long v = simple_strtol(buf, NULL, 10);
469
470         down(&data->update_lock);
471         data->temp_max_hyst[n - 1] = TEMP_TO_REG(v);
472         gl520_write_value(client, reg, data->temp_max_hyst[n - 1]);
473         up(&data->update_lock);
474         return count;
475 }
476
477 static ssize_t get_alarms(struct gl520_data *data, char *buf, int n)
478 {
479         return sprintf(buf, "%d\n", data->alarms);
480 }
481
482 static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n)
483 {
484         return sprintf(buf, "%d\n", data->beep_enable);
485 }
486
487 static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n)
488 {
489         return sprintf(buf, "%d\n", data->beep_mask);
490 }
491
492 static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
493 {
494         u8 r = simple_strtoul(buf, NULL, 10)?0:1;
495
496         down(&data->update_lock);
497         data->beep_enable = !r;
498         gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
499         up(&data->update_lock);
500         return count;
501 }
502
503 static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
504 {
505         u8 r = simple_strtoul(buf, NULL, 10);
506         
507         down(&data->update_lock);
508         r &= data->alarm_mask;
509         data->beep_mask = r;
510         gl520_write_value(client, reg, r);
511         up(&data->update_lock);
512         return count;
513 }
514
515
516 /*
517  * Real code
518  */
519
520 static int gl520_attach_adapter(struct i2c_adapter *adapter)
521 {
522         if (!(adapter->class & I2C_CLASS_HWMON))
523                 return 0;
524         return i2c_detect(adapter, &addr_data, gl520_detect);
525 }
526
527 static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
528 {
529         struct i2c_client *new_client;
530         struct gl520_data *data;
531         int err = 0;
532
533         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
534                                      I2C_FUNC_SMBUS_WORD_DATA))
535                 goto exit;
536
537         /* OK. For now, we presume we have a valid client. We now create the
538            client structure, even though we cannot fill it completely yet.
539            But it allows us to access gl520_{read,write}_value. */
540
541         if (!(data = kmalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
542                 err = -ENOMEM;
543                 goto exit;
544         }
545         memset(data, 0, sizeof(struct gl520_data));
546
547         new_client = &data->client;
548         i2c_set_clientdata(new_client, data);
549         new_client->addr = address;
550         new_client->adapter = adapter;
551         new_client->driver = &gl520_driver;
552         new_client->flags = 0;
553
554         /* Determine the chip type. */
555         if (kind < 0) {
556                 if ((gl520_read_value(new_client, GL520_REG_CHIP_ID) != 0x20) ||
557                     ((gl520_read_value(new_client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
558                     ((gl520_read_value(new_client, GL520_REG_CONF) & 0x80) != 0x00)) {
559                         dev_dbg(&new_client->dev, "Unknown chip type, skipping\n");
560                         goto exit_free;
561                 }
562         }
563
564         /* Fill in the remaining client fields */
565         strlcpy(new_client->name, "gl520sm", I2C_NAME_SIZE);
566         data->valid = 0;
567         init_MUTEX(&data->update_lock);
568
569         /* Tell the I2C layer a new client has arrived */
570         if ((err = i2c_attach_client(new_client)))
571                 goto exit_free;
572
573         /* Initialize the GL520SM chip */
574         gl520_init_client(new_client);
575
576         /* Register sysfs hooks */
577         data->class_dev = hwmon_device_register(&new_client->dev);
578         if (IS_ERR(data->class_dev)) {
579                 err = PTR_ERR(data->class_dev);
580                 goto exit_detach;
581         }
582
583         device_create_file_vid(new_client, 0);
584
585         device_create_file_in(new_client, 0);
586         device_create_file_in(new_client, 1);
587         device_create_file_in(new_client, 2);
588         device_create_file_in(new_client, 3);
589         if (!data->two_temps)
590                 device_create_file_in(new_client, 4);
591
592         device_create_file_fan(new_client, 1);
593         device_create_file_fan(new_client, 2);
594         device_create_file_fan_off(new_client, 1);
595
596         device_create_file_temp(new_client, 1);
597         if (data->two_temps)
598                 device_create_file_temp(new_client, 2);
599
600         device_create_file_alarms(new_client);
601
602         return 0;
603
604 exit_detach:
605         i2c_detach_client(new_client);
606 exit_free:
607         kfree(data);
608 exit:
609         return err;
610 }
611
612
613 /* Called when we have found a new GL520SM. */
614 static void gl520_init_client(struct i2c_client *client)
615 {
616         struct gl520_data *data = i2c_get_clientdata(client);
617         u8 oldconf, conf;
618
619         conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
620
621         data->alarm_mask = 0xff;
622         data->vrm = i2c_which_vrm();
623
624         if (extra_sensor_type == 1)
625                 conf &= ~0x10;
626         else if (extra_sensor_type == 2)
627                 conf |= 0x10;
628         data->two_temps = !(conf & 0x10);
629
630         /* If IRQ# is disabled, we can safely force comparator mode */
631         if (!(conf & 0x20))
632                 conf &= 0xf7;
633
634         /* Enable monitoring if needed */
635         conf |= 0x40;
636
637         if (conf != oldconf)
638                 gl520_write_value(client, GL520_REG_CONF, conf);
639
640         gl520_update_device(&(client->dev));
641
642         if (data->fan_min[0] == 0)
643                 data->alarm_mask &= ~0x20;
644         if (data->fan_min[1] == 0)
645                 data->alarm_mask &= ~0x40;
646
647         data->beep_mask &= data->alarm_mask;
648         gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
649 }
650
651 static int gl520_detach_client(struct i2c_client *client)
652 {
653         struct gl520_data *data = i2c_get_clientdata(client);
654         int err;
655
656         hwmon_device_unregister(data->class_dev);
657
658         if ((err = i2c_detach_client(client))) {
659                 dev_err(&client->dev, "Client deregistration failed, "
660                         "client not detached.\n");
661                 return err;
662         }
663
664         kfree(data);
665         return 0;
666 }
667
668
669 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized 
670    GL520 uses a high-byte first convention */
671 static int gl520_read_value(struct i2c_client *client, u8 reg)
672 {
673         if ((reg >= 0x07) && (reg <= 0x0c))
674                 return swab16(i2c_smbus_read_word_data(client, reg));
675         else
676                 return i2c_smbus_read_byte_data(client, reg);
677 }
678
679 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
680 {
681         if ((reg >= 0x07) && (reg <= 0x0c))
682                 return i2c_smbus_write_word_data(client, reg, swab16(value));
683         else
684                 return i2c_smbus_write_byte_data(client, reg, value);
685 }
686
687
688 static struct gl520_data *gl520_update_device(struct device *dev)
689 {
690         struct i2c_client *client = to_i2c_client(dev);
691         struct gl520_data *data = i2c_get_clientdata(client);
692         int val;
693
694         down(&data->update_lock);
695
696         if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
697
698                 dev_dbg(&client->dev, "Starting gl520sm update\n");
699
700                 data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
701                 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
702                 data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
703
704                 val = gl520_read_value(client, GL520_REG_IN0_LIMIT);
705                 data->in_min[0] = val & 0xff;
706                 data->in_max[0] = (val >> 8) & 0xff;
707                 val = gl520_read_value(client, GL520_REG_IN1_LIMIT);
708                 data->in_min[1] = val & 0xff;
709                 data->in_max[1] = (val >> 8) & 0xff;
710                 val = gl520_read_value(client, GL520_REG_IN2_LIMIT);
711                 data->in_min[2] = val & 0xff;
712                 data->in_max[2] = (val >> 8) & 0xff;
713                 val = gl520_read_value(client, GL520_REG_IN3_LIMIT);
714                 data->in_min[3] = val & 0xff;
715                 data->in_max[3] = (val >> 8) & 0xff;
716
717                 val = gl520_read_value(client, GL520_REG_FAN_INPUT);
718                 data->fan_input[0] = (val >> 8) & 0xff;
719                 data->fan_input[1] = val & 0xff;
720
721                 val = gl520_read_value(client, GL520_REG_FAN_MIN);
722                 data->fan_min[0] = (val >> 8) & 0xff;
723                 data->fan_min[1] = val & 0xff;
724
725                 data->temp_input[0] = gl520_read_value(client, GL520_REG_TEMP1_INPUT);
726                 data->temp_max[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX);
727                 data->temp_max_hyst[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX_HYST);
728
729                 val = gl520_read_value(client, GL520_REG_FAN_DIV);
730                 data->fan_div[0] = (val >> 6) & 0x03;
731                 data->fan_div[1] = (val >> 4) & 0x03;
732                 data->fan_off = (val >> 2) & 0x01;
733
734                 data->alarms &= data->alarm_mask;
735
736                 val = gl520_read_value(client, GL520_REG_CONF);
737                 data->beep_enable = !((val >> 2) & 1);
738
739                 data->in_input[0] = gl520_read_value(client, GL520_REG_IN0_INPUT);
740                 data->in_input[1] = gl520_read_value(client, GL520_REG_IN1_INPUT);
741                 data->in_input[2] = gl520_read_value(client, GL520_REG_IN2_INPUT);
742                 data->in_input[3] = gl520_read_value(client, GL520_REG_IN3_INPUT);
743
744                 /* Temp1 and Vin4 are the same input */
745                 if (data->two_temps) {
746                         data->temp_input[1] = gl520_read_value(client, GL520_REG_TEMP2_INPUT);
747                         data->temp_max[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX);
748                         data->temp_max_hyst[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX_HYST);
749                 } else {
750                         data->in_input[4] = gl520_read_value(client, GL520_REG_IN4_INPUT);
751                         data->in_min[4] = gl520_read_value(client, GL520_REG_IN4_MIN);
752                         data->in_max[4] = gl520_read_value(client, GL520_REG_IN4_MAX);
753                 }
754
755                 data->last_updated = jiffies;
756                 data->valid = 1;
757         }
758
759         up(&data->update_lock);
760
761         return data;
762 }
763
764
765 static int __init sensors_gl520sm_init(void)
766 {
767         return i2c_add_driver(&gl520_driver);
768 }
769
770 static void __exit sensors_gl520sm_exit(void)
771 {
772         i2c_del_driver(&gl520_driver);
773 }
774
775
776 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
777         "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
778         "Maarten Deprez <maartendeprez@users.sourceforge.net>");
779 MODULE_DESCRIPTION("GL520SM driver");
780 MODULE_LICENSE("GPL");
781
782 module_init(sensors_gl520sm_init);
783 module_exit(sensors_gl520sm_exit);