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