hwmon: (gl520sm) Various cleanups
[safe/jmp/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/hwmon.h>
30 #include <linux/hwmon-vid.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
33 #include <linux/sysfs.h>
34
35 /* Type of the extra sensor */
36 static unsigned short extra_sensor_type;
37 module_param(extra_sensor_type, ushort, 0);
38 MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
39
40 /* Addresses to scan */
41 static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
42
43 /* Insmod parameters */
44 I2C_CLIENT_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         .driver = {
115                 .name   = "gl520sm",
116         },
117         .attach_adapter = gl520_attach_adapter,
118         .detach_client  = gl520_detach_client,
119 };
120
121 /* Client data */
122 struct gl520_data {
123         struct i2c_client client;
124         struct device *hwmon_dev;
125         struct mutex update_lock;
126         char valid;             /* zero until the following fields are valid */
127         unsigned long last_updated;     /* in jiffies */
128
129         u8 vid;
130         u8 vrm;
131         u8 in_input[5];         /* [0] = VVD */
132         u8 in_min[5];           /* [0] = VDD */
133         u8 in_max[5];           /* [0] = VDD */
134         u8 fan_input[2];
135         u8 fan_min[2];
136         u8 fan_div[2];
137         u8 fan_off;
138         u8 temp_input[2];
139         u8 temp_max[2];
140         u8 temp_max_hyst[2];
141         u8 alarms;
142         u8 beep_enable;
143         u8 beep_mask;
144         u8 alarm_mask;
145         u8 two_temps;
146 };
147
148 /*
149  * Sysfs stuff
150  */
151
152 #define sysfs_r(type, n, item, reg) \
153 static ssize_t get_##type##item (struct gl520_data *, char *, int); \
154 static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \
155 static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \
156 { \
157         struct gl520_data *data = gl520_update_device(dev); \
158         return get_##type##item(data, buf, (n)); \
159 }
160
161 #define sysfs_w(type, n, item, reg) \
162 static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \
163 static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \
164 static ssize_t set_##type##n##item (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
165 { \
166         struct i2c_client *client = to_i2c_client(dev); \
167         struct gl520_data *data = i2c_get_clientdata(client); \
168         return set_##type##item(client, data, buf, count, (n), reg); \
169 }
170
171 #define sysfs_rw_n(type, n, item, reg) \
172 sysfs_r(type, n, item, reg) \
173 sysfs_w(type, n, item, reg) \
174 static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item);
175
176 #define sysfs_ro_n(type, n, item, reg) \
177 sysfs_r(type, n, item, reg) \
178 static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL);
179
180 #define sysfs_rw(type, item, reg) \
181 sysfs_r(type, 0, item, reg) \
182 sysfs_w(type, 0, item, reg) \
183 static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item);
184
185 #define sysfs_ro(type, item, reg) \
186 sysfs_r(type, 0, item, reg) \
187 static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL);
188
189
190 #define sysfs_vid(n) \
191 sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT)
192
193 #define sysfs_in(n) \
194 sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \
195 sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \
196 sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX)
197
198 #define sysfs_fan(n) \
199 sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \
200 sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \
201 sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV)
202
203 #define sysfs_fan_off(n) \
204 sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF)
205
206 #define sysfs_temp(n) \
207 sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \
208 sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \
209 sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST)
210
211 #define sysfs_alarms() \
212 sysfs_ro(alarms, , GL520_REG_ALARMS) \
213 sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \
214 sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK)
215
216
217 sysfs_vid(0)
218
219 sysfs_in(0)
220 sysfs_in(1)
221 sysfs_in(2)
222 sysfs_in(3)
223 sysfs_in(4)
224
225 sysfs_fan(1)
226 sysfs_fan(2)
227 sysfs_fan_off(1)
228
229 sysfs_temp(1)
230 sysfs_temp(2)
231
232 sysfs_alarms()
233
234
235 static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n)
236 {
237         return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
238 }
239
240 #define VDD_FROM_REG(val) (((val)*95+2)/4)
241 #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
242
243 #define IN_FROM_REG(val) ((val)*19)
244 #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
245
246 static ssize_t get_in_input(struct gl520_data *data, char *buf, int n)
247 {
248         u8 r = data->in_input[n];
249
250         if (n == 0)
251                 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
252         else
253                 return sprintf(buf, "%d\n", IN_FROM_REG(r));
254 }
255
256 static ssize_t get_in_min(struct gl520_data *data, char *buf, int n)
257 {
258         u8 r = data->in_min[n];
259
260         if (n == 0)
261                 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
262         else
263                 return sprintf(buf, "%d\n", IN_FROM_REG(r));
264 }
265
266 static ssize_t get_in_max(struct gl520_data *data, char *buf, int n)
267 {
268         u8 r = data->in_max[n];
269
270         if (n == 0)
271                 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
272         else
273                 return sprintf(buf, "%d\n", IN_FROM_REG(r));
274 }
275
276 static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
277 {
278         long v = simple_strtol(buf, NULL, 10);
279         u8 r;
280
281         mutex_lock(&data->update_lock);
282
283         if (n == 0)
284                 r = VDD_TO_REG(v);
285         else
286                 r = IN_TO_REG(v);
287
288         data->in_min[n] = r;
289
290         if (n < 4)
291                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
292         else
293                 gl520_write_value(client, reg, r);
294
295         mutex_unlock(&data->update_lock);
296         return count;
297 }
298
299 static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
300 {
301         long v = simple_strtol(buf, NULL, 10);
302         u8 r;
303
304         if (n == 0)
305                 r = VDD_TO_REG(v);
306         else
307                 r = IN_TO_REG(v);
308
309         mutex_lock(&data->update_lock);
310
311         data->in_max[n] = r;
312
313         if (n < 4)
314                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
315         else
316                 gl520_write_value(client, reg, r);
317
318         mutex_unlock(&data->update_lock);
319         return count;
320 }
321
322 #define DIV_FROM_REG(val) (1 << (val))
323 #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
324 #define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
325
326 static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n)
327 {
328         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1]));
329 }
330
331 static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n)
332 {
333         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1]));
334 }
335
336 static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n)
337 {
338         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1]));
339 }
340
341 static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n)
342 {
343         return sprintf(buf, "%d\n", data->fan_off);
344 }
345
346 static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
347 {
348         unsigned long v = simple_strtoul(buf, NULL, 10);
349         u8 r;
350
351         mutex_lock(&data->update_lock);
352         r = FAN_TO_REG(v, data->fan_div[n - 1]);
353         data->fan_min[n - 1] = r;
354
355         if (n == 1)
356                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
357         else
358                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
359
360         data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
361         if (data->fan_min[n - 1] == 0)
362                 data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40;
363         else
364                 data->alarm_mask |= (n == 1) ? 0x20 : 0x40;
365         data->beep_mask &= data->alarm_mask;
366         gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
367
368         mutex_unlock(&data->update_lock);
369         return count;
370 }
371
372 static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
373 {
374         unsigned long v = simple_strtoul(buf, NULL, 10);
375         u8 r;
376
377         switch (v) {
378         case 1: r = 0; break;
379         case 2: r = 1; break;
380         case 4: r = 2; break;
381         case 8: r = 3; break;
382         default:
383                 dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
384                 return -EINVAL;
385         }
386
387         mutex_lock(&data->update_lock);
388         data->fan_div[n - 1] = r;
389
390         if (n == 1)
391                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
392         else
393                 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
394
395         mutex_unlock(&data->update_lock);
396         return count;
397 }
398
399 static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
400 {
401         u8 r = simple_strtoul(buf, NULL, 10)?1:0;
402
403         mutex_lock(&data->update_lock);
404         data->fan_off = r;
405         gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
406         mutex_unlock(&data->update_lock);
407         return count;
408 }
409
410 #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
411 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
412
413 static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n)
414 {
415         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1]));
416 }
417
418 static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n)
419 {
420         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1]));
421 }
422
423 static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n)
424 {
425         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1]));
426 }
427
428 static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
429 {
430         long v = simple_strtol(buf, NULL, 10);
431
432         mutex_lock(&data->update_lock);
433         data->temp_max[n - 1] = TEMP_TO_REG(v);
434         gl520_write_value(client, reg, data->temp_max[n - 1]);
435         mutex_unlock(&data->update_lock);
436         return count;
437 }
438
439 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)
440 {
441         long v = simple_strtol(buf, NULL, 10);
442
443         mutex_lock(&data->update_lock);
444         data->temp_max_hyst[n - 1] = TEMP_TO_REG(v);
445         gl520_write_value(client, reg, data->temp_max_hyst[n - 1]);
446         mutex_unlock(&data->update_lock);
447         return count;
448 }
449
450 static ssize_t get_alarms(struct gl520_data *data, char *buf, int n)
451 {
452         return sprintf(buf, "%d\n", data->alarms);
453 }
454
455 static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n)
456 {
457         return sprintf(buf, "%d\n", data->beep_enable);
458 }
459
460 static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n)
461 {
462         return sprintf(buf, "%d\n", data->beep_mask);
463 }
464
465 static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
466 {
467         u8 r = simple_strtoul(buf, NULL, 10)?0:1;
468
469         mutex_lock(&data->update_lock);
470         data->beep_enable = !r;
471         gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
472         mutex_unlock(&data->update_lock);
473         return count;
474 }
475
476 static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
477 {
478         u8 r = simple_strtoul(buf, NULL, 10);
479
480         mutex_lock(&data->update_lock);
481         r &= data->alarm_mask;
482         data->beep_mask = r;
483         gl520_write_value(client, reg, r);
484         mutex_unlock(&data->update_lock);
485         return count;
486 }
487
488 static struct attribute *gl520_attributes[] = {
489         &dev_attr_cpu0_vid.attr,
490
491         &dev_attr_in0_input.attr,
492         &dev_attr_in0_min.attr,
493         &dev_attr_in0_max.attr,
494         &dev_attr_in1_input.attr,
495         &dev_attr_in1_min.attr,
496         &dev_attr_in1_max.attr,
497         &dev_attr_in2_input.attr,
498         &dev_attr_in2_min.attr,
499         &dev_attr_in2_max.attr,
500         &dev_attr_in3_input.attr,
501         &dev_attr_in3_min.attr,
502         &dev_attr_in3_max.attr,
503
504         &dev_attr_fan1_input.attr,
505         &dev_attr_fan1_min.attr,
506         &dev_attr_fan1_div.attr,
507         &dev_attr_fan1_off.attr,
508         &dev_attr_fan2_input.attr,
509         &dev_attr_fan2_min.attr,
510         &dev_attr_fan2_div.attr,
511
512         &dev_attr_temp1_input.attr,
513         &dev_attr_temp1_max.attr,
514         &dev_attr_temp1_max_hyst.attr,
515
516         &dev_attr_alarms.attr,
517         &dev_attr_beep_enable.attr,
518         &dev_attr_beep_mask.attr,
519         NULL
520 };
521
522 static const struct attribute_group gl520_group = {
523         .attrs = gl520_attributes,
524 };
525
526 static struct attribute *gl520_attributes_opt[] = {
527         &dev_attr_in4_input.attr,
528         &dev_attr_in4_min.attr,
529         &dev_attr_in4_max.attr,
530
531         &dev_attr_temp2_input.attr,
532         &dev_attr_temp2_max.attr,
533         &dev_attr_temp2_max_hyst.attr,
534         NULL
535 };
536
537 static const struct attribute_group gl520_group_opt = {
538         .attrs = gl520_attributes_opt,
539 };
540
541
542 /*
543  * Real code
544  */
545
546 static int gl520_attach_adapter(struct i2c_adapter *adapter)
547 {
548         if (!(adapter->class & I2C_CLASS_HWMON))
549                 return 0;
550         return i2c_probe(adapter, &addr_data, gl520_detect);
551 }
552
553 static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
554 {
555         struct i2c_client *client;
556         struct gl520_data *data;
557         int err = 0;
558
559         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
560                                      I2C_FUNC_SMBUS_WORD_DATA))
561                 goto exit;
562
563         /* OK. For now, we presume we have a valid client. We now create the
564            client structure, even though we cannot fill it completely yet.
565            But it allows us to access gl520_{read,write}_value. */
566
567         if (!(data = kzalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
568                 err = -ENOMEM;
569                 goto exit;
570         }
571
572         client = &data->client;
573         i2c_set_clientdata(client, data);
574         client->addr = address;
575         client->adapter = adapter;
576         client->driver = &gl520_driver;
577
578         /* Determine the chip type. */
579         if (kind < 0) {
580                 if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) ||
581                     ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
582                     ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) {
583                         dev_dbg(&client->dev, "Unknown chip type, skipping\n");
584                         goto exit_free;
585                 }
586         }
587
588         /* Fill in the remaining client fields */
589         strlcpy(client->name, "gl520sm", I2C_NAME_SIZE);
590         mutex_init(&data->update_lock);
591
592         /* Tell the I2C layer a new client has arrived */
593         if ((err = i2c_attach_client(client)))
594                 goto exit_free;
595
596         /* Initialize the GL520SM chip */
597         gl520_init_client(client);
598
599         /* Register sysfs hooks */
600         if ((err = sysfs_create_group(&client->dev.kobj, &gl520_group)))
601                 goto exit_detach;
602
603         if (data->two_temps) {
604                 if ((err = device_create_file(&client->dev,
605                                               &dev_attr_temp2_input))
606                  || (err = device_create_file(&client->dev,
607                                               &dev_attr_temp2_max))
608                  || (err = device_create_file(&client->dev,
609                                               &dev_attr_temp2_max_hyst)))
610                         goto exit_remove_files;
611         } else {
612                 if ((err = device_create_file(&client->dev,
613                                               &dev_attr_in4_input))
614                  || (err = device_create_file(&client->dev,
615                                               &dev_attr_in4_min))
616                  || (err = device_create_file(&client->dev,
617                                               &dev_attr_in4_max)))
618                         goto exit_remove_files;
619         }
620
621
622         data->hwmon_dev = hwmon_device_register(&client->dev);
623         if (IS_ERR(data->hwmon_dev)) {
624                 err = PTR_ERR(data->hwmon_dev);
625                 goto exit_remove_files;
626         }
627
628         return 0;
629
630 exit_remove_files:
631         sysfs_remove_group(&client->dev.kobj, &gl520_group);
632         sysfs_remove_group(&client->dev.kobj, &gl520_group_opt);
633 exit_detach:
634         i2c_detach_client(client);
635 exit_free:
636         kfree(data);
637 exit:
638         return err;
639 }
640
641
642 /* Called when we have found a new GL520SM. */
643 static void gl520_init_client(struct i2c_client *client)
644 {
645         struct gl520_data *data = i2c_get_clientdata(client);
646         u8 oldconf, conf;
647
648         conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
649
650         data->alarm_mask = 0xff;
651         data->vrm = vid_which_vrm();
652
653         if (extra_sensor_type == 1)
654                 conf &= ~0x10;
655         else if (extra_sensor_type == 2)
656                 conf |= 0x10;
657         data->two_temps = !(conf & 0x10);
658
659         /* If IRQ# is disabled, we can safely force comparator mode */
660         if (!(conf & 0x20))
661                 conf &= 0xf7;
662
663         /* Enable monitoring if needed */
664         conf |= 0x40;
665
666         if (conf != oldconf)
667                 gl520_write_value(client, GL520_REG_CONF, conf);
668
669         gl520_update_device(&(client->dev));
670
671         if (data->fan_min[0] == 0)
672                 data->alarm_mask &= ~0x20;
673         if (data->fan_min[1] == 0)
674                 data->alarm_mask &= ~0x40;
675
676         data->beep_mask &= data->alarm_mask;
677         gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
678 }
679
680 static int gl520_detach_client(struct i2c_client *client)
681 {
682         struct gl520_data *data = i2c_get_clientdata(client);
683         int err;
684
685         hwmon_device_unregister(data->hwmon_dev);
686         sysfs_remove_group(&client->dev.kobj, &gl520_group);
687         sysfs_remove_group(&client->dev.kobj, &gl520_group_opt);
688
689         if ((err = i2c_detach_client(client)))
690                 return err;
691
692         kfree(data);
693         return 0;
694 }
695
696
697 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
698    GL520 uses a high-byte first convention */
699 static int gl520_read_value(struct i2c_client *client, u8 reg)
700 {
701         if ((reg >= 0x07) && (reg <= 0x0c))
702                 return swab16(i2c_smbus_read_word_data(client, reg));
703         else
704                 return i2c_smbus_read_byte_data(client, reg);
705 }
706
707 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
708 {
709         if ((reg >= 0x07) && (reg <= 0x0c))
710                 return i2c_smbus_write_word_data(client, reg, swab16(value));
711         else
712                 return i2c_smbus_write_byte_data(client, reg, value);
713 }
714
715
716 static struct gl520_data *gl520_update_device(struct device *dev)
717 {
718         struct i2c_client *client = to_i2c_client(dev);
719         struct gl520_data *data = i2c_get_clientdata(client);
720         int val;
721
722         mutex_lock(&data->update_lock);
723
724         if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
725
726                 dev_dbg(&client->dev, "Starting gl520sm update\n");
727
728                 data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
729                 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
730                 data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
731
732                 val = gl520_read_value(client, GL520_REG_IN0_LIMIT);
733                 data->in_min[0] = val & 0xff;
734                 data->in_max[0] = (val >> 8) & 0xff;
735                 val = gl520_read_value(client, GL520_REG_IN1_LIMIT);
736                 data->in_min[1] = val & 0xff;
737                 data->in_max[1] = (val >> 8) & 0xff;
738                 val = gl520_read_value(client, GL520_REG_IN2_LIMIT);
739                 data->in_min[2] = val & 0xff;
740                 data->in_max[2] = (val >> 8) & 0xff;
741                 val = gl520_read_value(client, GL520_REG_IN3_LIMIT);
742                 data->in_min[3] = val & 0xff;
743                 data->in_max[3] = (val >> 8) & 0xff;
744
745                 val = gl520_read_value(client, GL520_REG_FAN_INPUT);
746                 data->fan_input[0] = (val >> 8) & 0xff;
747                 data->fan_input[1] = val & 0xff;
748
749                 val = gl520_read_value(client, GL520_REG_FAN_MIN);
750                 data->fan_min[0] = (val >> 8) & 0xff;
751                 data->fan_min[1] = val & 0xff;
752
753                 data->temp_input[0] = gl520_read_value(client, GL520_REG_TEMP1_INPUT);
754                 data->temp_max[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX);
755                 data->temp_max_hyst[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX_HYST);
756
757                 val = gl520_read_value(client, GL520_REG_FAN_DIV);
758                 data->fan_div[0] = (val >> 6) & 0x03;
759                 data->fan_div[1] = (val >> 4) & 0x03;
760                 data->fan_off = (val >> 2) & 0x01;
761
762                 data->alarms &= data->alarm_mask;
763
764                 val = gl520_read_value(client, GL520_REG_CONF);
765                 data->beep_enable = !((val >> 2) & 1);
766
767                 data->in_input[0] = gl520_read_value(client, GL520_REG_IN0_INPUT);
768                 data->in_input[1] = gl520_read_value(client, GL520_REG_IN1_INPUT);
769                 data->in_input[2] = gl520_read_value(client, GL520_REG_IN2_INPUT);
770                 data->in_input[3] = gl520_read_value(client, GL520_REG_IN3_INPUT);
771
772                 /* Temp1 and Vin4 are the same input */
773                 if (data->two_temps) {
774                         data->temp_input[1] = gl520_read_value(client, GL520_REG_TEMP2_INPUT);
775                         data->temp_max[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX);
776                         data->temp_max_hyst[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX_HYST);
777                 } else {
778                         data->in_input[4] = gl520_read_value(client, GL520_REG_IN4_INPUT);
779                         data->in_min[4] = gl520_read_value(client, GL520_REG_IN4_MIN);
780                         data->in_max[4] = gl520_read_value(client, GL520_REG_IN4_MAX);
781                 }
782
783                 data->last_updated = jiffies;
784                 data->valid = 1;
785         }
786
787         mutex_unlock(&data->update_lock);
788
789         return data;
790 }
791
792
793 static int __init sensors_gl520sm_init(void)
794 {
795         return i2c_add_driver(&gl520_driver);
796 }
797
798 static void __exit sensors_gl520sm_exit(void)
799 {
800         i2c_del_driver(&gl520_driver);
801 }
802
803
804 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
805         "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
806         "Maarten Deprez <maartendeprez@users.sourceforge.net>");
807 MODULE_DESCRIPTION("GL520SM driver");
808 MODULE_LICENSE("GPL");
809
810 module_init(sensors_gl520sm_init);
811 module_exit(sensors_gl520sm_exit);