patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / chips / gl518sm.c
1 /*
2  * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware
3  *             monitoring
4  * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5  * Kyosti Malkki <kmalkki@cc.hut.fi>
6  * Copyright (C) 2004 Hong-Gunn Chew <hglinux@gunnet.org> and
7  * Jean Delvare <khali@linux-fr.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  * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
24  * and advice of Greg Kroah-Hartman.
25  *
26  * Notes about the port:
27  * Release 0x00 of the GL518SM chipset doesn't support reading of in0,
28  * in1 nor in2. The original driver had an ugly workaround to get them
29  * anyway (changing limits and watching alarms trigger and wear off).
30  * We did not keep that part of the original driver in the Linux 2.6
31  * version, since it was making the driver significantly more complex
32  * with no real benefit.
33  *
34  * History:
35  * 2004-01-28  Original port. (Hong-Gunn Chew)
36  * 2004-01-31  Code review and approval. (Jean Delvare)
37  */
38
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-sensor.h>
45
46 /* Addresses to scan */
47 static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
48 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
49 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
50 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
51
52 /* Insmod parameters */
53 SENSORS_INSMOD_2(gl518sm_r00, gl518sm_r80);
54
55 /* Many GL518 constants specified below */
56
57 /* The GL518 registers */
58 #define GL518_REG_CHIP_ID       0x00
59 #define GL518_REG_REVISION      0x01
60 #define GL518_REG_VENDOR_ID     0x02
61 #define GL518_REG_CONF          0x03
62 #define GL518_REG_TEMP_IN       0x04
63 #define GL518_REG_TEMP_MAX      0x05
64 #define GL518_REG_TEMP_HYST     0x06
65 #define GL518_REG_FAN_COUNT     0x07
66 #define GL518_REG_FAN_LIMIT     0x08
67 #define GL518_REG_VIN1_LIMIT    0x09
68 #define GL518_REG_VIN2_LIMIT    0x0a
69 #define GL518_REG_VIN3_LIMIT    0x0b
70 #define GL518_REG_VDD_LIMIT     0x0c
71 #define GL518_REG_VIN3          0x0d
72 #define GL518_REG_MISC          0x0f
73 #define GL518_REG_ALARM         0x10
74 #define GL518_REG_MASK          0x11
75 #define GL518_REG_INT           0x12
76 #define GL518_REG_VIN2          0x13
77 #define GL518_REG_VIN1          0x14
78 #define GL518_REG_VDD           0x15
79
80
81 /*
82  * Conversions. Rounding and limit checking is only done on the TO_REG
83  * variants. Note that you should be a bit careful with which arguments
84  * these macros are called: arguments may be evaluated more than once.
85  * Fixing this is just not worth it.
86  */
87
88 #define RAW_FROM_REG(val)       val
89
90 #define BOOL_FROM_REG(val)      ((val)?0:1)
91 #define BOOL_TO_REG(val)        ((val)?0:1)
92
93 #define TEMP_TO_REG(val)        (SENSORS_LIMIT(((((val)<0? \
94                                 (val)-500:(val)+500)/1000)+119),0,255))
95 #define TEMP_FROM_REG(val)      (((val) - 119) * 1000)
96
97 static inline u8 FAN_TO_REG(long rpm, int div)
98 {
99         long rpmdiv;
100         if (rpm == 0)
101                 return 0;
102         rpmdiv = SENSORS_LIMIT(rpm, 1, 1920000) * div;
103         return SENSORS_LIMIT((960000 + rpmdiv / 2) / rpmdiv, 1, 255);
104 }
105 #define FAN_FROM_REG(val,div)   ((val)==0 ? 0 : (960000/((val)*(div))))
106
107 #define IN_TO_REG(val)          (SENSORS_LIMIT((((val)+9)/19),0,255))
108 #define IN_FROM_REG(val)        ((val)*19)
109
110 #define VDD_TO_REG(val)         (SENSORS_LIMIT((((val)*4+47)/95),0,255))
111 #define VDD_FROM_REG(val)       (((val)*95+2)/4)
112
113 #define DIV_TO_REG(val)         ((val)==4?2:(val)==2?1:(val)==1?0:3)
114 #define DIV_FROM_REG(val)       (1 << (val))
115
116 #define BEEP_MASK_TO_REG(val)   ((val) & 0x7f & data->alarm_mask)
117 #define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
118
119 /* Each client has this additional data */
120 struct gl518_data {
121         struct i2c_client client;
122         enum chips type;
123
124         struct semaphore update_lock;
125         char valid;             /* !=0 if following fields are valid */
126         unsigned long last_updated;     /* In jiffies */
127
128         u8 voltage_in[4];       /* Register values; [0] = VDD */
129         u8 voltage_min[4];      /* Register values; [0] = VDD */
130         u8 voltage_max[4];      /* Register values; [0] = VDD */
131         u8 iter_voltage_in[4];  /* Register values; [0] = VDD */
132         u8 fan_in[2];
133         u8 fan_min[2];
134         u8 fan_div[2];          /* Register encoding, shifted right */
135         u8 fan_auto1;           /* Boolean */
136         u8 temp_in;             /* Register values */
137         u8 temp_max;            /* Register values */
138         u8 temp_hyst;           /* Register values */
139         u8 alarms;              /* Register value */
140         u8 alarm_mask;          /* Register value */
141         u8 beep_mask;           /* Register value */
142         u8 beep_enable;         /* Boolean */
143 };
144
145 static int gl518_attach_adapter(struct i2c_adapter *adapter);
146 static int gl518_detect(struct i2c_adapter *adapter, int address, int kind);
147 static void gl518_init_client(struct i2c_client *client);
148 static int gl518_detach_client(struct i2c_client *client);
149 static int gl518_read_value(struct i2c_client *client, u8 reg);
150 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
151 static struct gl518_data *gl518_update_device(struct device *dev);
152
153 /* This is the driver that will be inserted */
154 static struct i2c_driver gl518_driver = {
155         .owner          = THIS_MODULE,
156         .name           = "gl518sm",
157         .id             = I2C_DRIVERID_GL518,
158         .flags          = I2C_DF_NOTIFY,
159         .attach_adapter = gl518_attach_adapter,
160         .detach_client  = gl518_detach_client,
161 };
162
163 /*
164  * Internal variables
165  */
166
167 static int gl518_id = 0;
168
169 /*
170  * Sysfs stuff
171  */
172
173 #define show(type, suffix, value)                                       \
174 static ssize_t show_##suffix(struct device *dev, char *buf)             \
175 {                                                                       \
176         struct gl518_data *data = gl518_update_device(dev);             \
177         return sprintf(buf, "%d\n", type##_FROM_REG(data->value));      \
178 }
179
180 #define show_fan(suffix, value, index)                                  \
181 static ssize_t show_##suffix(struct device *dev, char *buf)             \
182 {                                                                       \
183         struct gl518_data *data = gl518_update_device(dev);             \
184         return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[index],    \
185                 DIV_FROM_REG(data->fan_div[index])));                   \
186 }
187
188 show(TEMP, temp_input1, temp_in);
189 show(TEMP, temp_max1, temp_max);
190 show(TEMP, temp_hyst1, temp_hyst);
191 show(BOOL, fan_auto1, fan_auto1);
192 show_fan(fan_input1, fan_in, 0);
193 show_fan(fan_input2, fan_in, 1);
194 show_fan(fan_min1, fan_min, 0);
195 show_fan(fan_min2, fan_min, 1);
196 show(DIV, fan_div1, fan_div[0]);
197 show(DIV, fan_div2, fan_div[1]);
198 show(VDD, in_input0, voltage_in[0]);
199 show(IN, in_input1, voltage_in[1]);
200 show(IN, in_input2, voltage_in[2]);
201 show(IN, in_input3, voltage_in[3]);
202 show(VDD, in_min0, voltage_min[0]);
203 show(IN, in_min1, voltage_min[1]);
204 show(IN, in_min2, voltage_min[2]);
205 show(IN, in_min3, voltage_min[3]);
206 show(VDD, in_max0, voltage_max[0]);
207 show(IN, in_max1, voltage_max[1]);
208 show(IN, in_max2, voltage_max[2]);
209 show(IN, in_max3, voltage_max[3]);
210 show(RAW, alarms, alarms);
211 show(BOOL, beep_enable, beep_enable);
212 show(BEEP_MASK, beep_mask, beep_mask);
213
214 #define set(type, suffix, value, reg)                                   \
215 static ssize_t set_##suffix(struct device *dev, const char *buf,        \
216         size_t count)                                                   \
217 {                                                                       \
218         struct i2c_client *client = to_i2c_client(dev);                 \
219         struct gl518_data *data = i2c_get_clientdata(client);           \
220         data->value = type##_TO_REG(simple_strtol(buf, NULL, 10));      \
221         gl518_write_value(client, reg, data->value);                    \
222         return count;                                                   \
223 }
224
225 #define set_bits(type, suffix, value, reg, mask, shift)                 \
226 static ssize_t set_##suffix(struct device *dev, const char *buf,        \
227         size_t count)                                                   \
228 {                                                                       \
229         struct i2c_client *client = to_i2c_client(dev);                 \
230         struct gl518_data *data = i2c_get_clientdata(client);           \
231         int regvalue = gl518_read_value(client, reg);                   \
232         data->value = type##_TO_REG(simple_strtoul(buf, NULL, 10));     \
233         regvalue = (regvalue & ~mask) | (data->value << shift);         \
234         gl518_write_value(client, reg, regvalue);                       \
235         return count;                                                   \
236 }
237
238 #define set_low(type, suffix, value, reg)                               \
239         set_bits(type, suffix, value, reg, 0x00ff, 0)
240 #define set_high(type, suffix, value, reg)                              \
241         set_bits(type, suffix, value, reg, 0xff00, 8)
242
243 set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
244 set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
245 set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
246 set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0xc0, 6);
247 set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0x30, 4);
248 set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
249 set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
250 set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
251 set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
252 set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
253 set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
254 set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
255 set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
256 set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
257 set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
258
259 static ssize_t set_fan_min1(struct device *dev, const char *buf, size_t count)
260 {
261         struct i2c_client *client = to_i2c_client(dev);
262         struct gl518_data *data = i2c_get_clientdata(client);
263         int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
264
265         data->fan_min[0] = FAN_TO_REG(simple_strtoul(buf, NULL, 10),
266                 DIV_FROM_REG(data->fan_div[0]));
267         regvalue = (regvalue & 0x00ff) | (data->fan_min[0] << 8);
268         gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
269
270         data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
271         if (data->fan_min[0] == 0)
272                 data->alarm_mask &= ~0x20;
273         else
274                 data->alarm_mask |= 0x20;
275         data->beep_mask &= data->alarm_mask;
276         gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
277
278         return count;
279 }
280
281 static ssize_t set_fan_min2(struct device *dev, const char *buf, size_t count)
282 {
283         struct i2c_client *client = to_i2c_client(dev);
284         struct gl518_data *data = i2c_get_clientdata(client);
285         int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
286
287         data->fan_min[1] = FAN_TO_REG(simple_strtoul(buf, NULL, 10),
288                 DIV_FROM_REG(data->fan_div[1]));
289         regvalue = (regvalue & 0xff00) | data->fan_min[1];
290         gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
291
292         data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
293         if (data->fan_min[1] == 0)
294                 data->alarm_mask &= ~0x40;
295         else
296                 data->alarm_mask |= 0x40;
297         data->beep_mask &= data->alarm_mask;
298         gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
299
300         return count;
301 }
302
303 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
304 static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
305 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
306         show_temp_hyst1, set_temp_hyst1);
307 static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
308 static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
309 static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
310 static DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1);
311 static DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2);
312 static DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1);
313 static DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2);
314 static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
315 static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
316 static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
317 static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
318 static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
319 static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
320 static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
321 static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
322 static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
323 static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
324 static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
325 static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
326 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
327 static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
328         show_beep_enable, set_beep_enable);
329 static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
330         show_beep_mask, set_beep_mask);
331
332 /*
333  * Real code
334  */
335
336 static int gl518_attach_adapter(struct i2c_adapter *adapter)
337 {
338         if (!(adapter->class & I2C_CLASS_HWMON))
339                 return 0;
340         return i2c_detect(adapter, &addr_data, gl518_detect);
341 }
342
343 static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
344 {
345         int i;
346         struct i2c_client *new_client;
347         struct gl518_data *data;
348         int err = 0;
349
350         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
351                                      I2C_FUNC_SMBUS_WORD_DATA))
352                 goto exit;
353
354         /* OK. For now, we presume we have a valid client. We now create the
355            client structure, even though we cannot fill it completely yet.
356            But it allows us to access gl518_{read,write}_value. */
357
358         if (!(data = kmalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
359                 err = -ENOMEM;
360                 goto exit;
361         }
362         memset(data, 0, sizeof(struct gl518_data));
363
364         new_client = &data->client;
365         i2c_set_clientdata(new_client, data);
366
367         new_client->addr = address;
368         new_client->adapter = adapter;
369         new_client->driver = &gl518_driver;
370         new_client->flags = 0;
371
372         /* Now, we do the remaining detection. */
373
374         if (kind < 0) {
375                 if ((gl518_read_value(new_client, GL518_REG_CHIP_ID) != 0x80)
376                  || (gl518_read_value(new_client, GL518_REG_CONF) & 0x80))
377                         goto exit_free;
378         }
379
380         /* Determine the chip type. */
381         if (kind <= 0) {
382                 i = gl518_read_value(new_client, GL518_REG_REVISION);
383                 if (i == 0x00) {
384                         kind = gl518sm_r00;
385                 } else if (i == 0x80) {
386                         kind = gl518sm_r80;
387                 } else {
388                         if (kind <= 0)
389                                 dev_info(&adapter->dev,
390                                     "Ignoring 'force' parameter for unknown "
391                                     "chip at adapter %d, address 0x%02x\n",
392                                     i2c_adapter_id(adapter), address);
393                         goto exit_free;
394                 }
395         }
396
397         /* Fill in the remaining client fields */
398         strlcpy(new_client->name, "gl518sm", I2C_NAME_SIZE);
399         new_client->id = gl518_id++;
400         data->type = kind;
401         data->valid = 0;
402         init_MUTEX(&data->update_lock);
403
404         /* Tell the I2C layer a new client has arrived */
405         if ((err = i2c_attach_client(new_client)))
406                 goto exit_free;
407
408         /* Initialize the GL518SM chip */
409         data->alarm_mask = 0xff;
410         data->voltage_in[0]=data->voltage_in[1]=data->voltage_in[2]=0;
411         gl518_init_client((struct i2c_client *) new_client);
412
413         /* Register sysfs hooks */
414         device_create_file(&new_client->dev, &dev_attr_in0_input);
415         device_create_file(&new_client->dev, &dev_attr_in1_input);
416         device_create_file(&new_client->dev, &dev_attr_in2_input);
417         device_create_file(&new_client->dev, &dev_attr_in3_input);
418         device_create_file(&new_client->dev, &dev_attr_in0_min);
419         device_create_file(&new_client->dev, &dev_attr_in1_min);
420         device_create_file(&new_client->dev, &dev_attr_in2_min);
421         device_create_file(&new_client->dev, &dev_attr_in3_min);
422         device_create_file(&new_client->dev, &dev_attr_in0_max);
423         device_create_file(&new_client->dev, &dev_attr_in1_max);
424         device_create_file(&new_client->dev, &dev_attr_in2_max);
425         device_create_file(&new_client->dev, &dev_attr_in3_max);
426         device_create_file(&new_client->dev, &dev_attr_fan1_auto);
427         device_create_file(&new_client->dev, &dev_attr_fan1_input);
428         device_create_file(&new_client->dev, &dev_attr_fan2_input);
429         device_create_file(&new_client->dev, &dev_attr_fan1_min);
430         device_create_file(&new_client->dev, &dev_attr_fan2_min);
431         device_create_file(&new_client->dev, &dev_attr_fan1_div);
432         device_create_file(&new_client->dev, &dev_attr_fan2_div);
433         device_create_file(&new_client->dev, &dev_attr_temp1_input);
434         device_create_file(&new_client->dev, &dev_attr_temp1_max);
435         device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
436         device_create_file(&new_client->dev, &dev_attr_alarms);
437         device_create_file(&new_client->dev, &dev_attr_beep_enable);
438         device_create_file(&new_client->dev, &dev_attr_beep_mask);
439
440         return 0;
441
442 /* OK, this is not exactly good programming practice, usually. But it is
443    very code-efficient in this case. */
444
445 exit_free:
446         kfree(data);
447 exit:
448         return err;
449 }
450
451
452 /* Called when we have found a new GL518SM.
453    Note that we preserve D4:NoFan2 and D2:beep_enable. */
454 static void gl518_init_client(struct i2c_client *client)
455 {
456         /* Make sure we leave D7:Reset untouched */
457         u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
458
459         /* Comparator mode (D3=0), standby mode (D6=0) */
460         gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
461
462         /* Never interrupts */
463         gl518_write_value(client, GL518_REG_MASK, 0x00);
464
465         /* Clear status register (D5=1), start (D6=1) */
466         gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
467         gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
468 }
469
470 static int gl518_detach_client(struct i2c_client *client)
471 {
472         int err;
473
474         if ((err = i2c_detach_client(client))) {
475                 dev_err(&client->dev, "Client deregistration failed, "
476                         "client not detached.\n");
477                 return err;
478         }
479
480         kfree(i2c_get_clientdata(client));
481
482         return 0;
483 }
484
485 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized 
486    GL518 uses a high-byte first convention, which is exactly opposite to
487    the usual practice. */
488 static int gl518_read_value(struct i2c_client *client, u8 reg)
489 {
490         if ((reg >= 0x07) && (reg <= 0x0c))
491                 return swab16(i2c_smbus_read_word_data(client, reg));
492         else
493                 return i2c_smbus_read_byte_data(client, reg);
494 }
495
496 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized 
497    GL518 uses a high-byte first convention, which is exactly opposite to
498    the usual practice. */
499 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
500 {
501         if ((reg >= 0x07) && (reg <= 0x0c))
502                 return i2c_smbus_write_word_data(client, reg, swab16(value));
503         else
504                 return i2c_smbus_write_byte_data(client, reg, value);
505 }
506
507 static struct gl518_data *gl518_update_device(struct device *dev)
508 {
509         struct i2c_client *client = to_i2c_client(dev);
510         struct gl518_data *data = i2c_get_clientdata(client);
511         int val;
512
513         down(&data->update_lock);
514
515         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
516             (jiffies < data->last_updated) || !data->valid) {
517                 dev_dbg(&client->dev, "Starting gl518 update\n");
518
519                 data->alarms = gl518_read_value(client, GL518_REG_INT);
520                 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
521
522                 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
523                 data->voltage_min[0] = val & 0xff;
524                 data->voltage_max[0] = (val >> 8) & 0xff;
525                 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
526                 data->voltage_min[1] = val & 0xff;
527                 data->voltage_max[1] = (val >> 8) & 0xff;
528                 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
529                 data->voltage_min[2] = val & 0xff;
530                 data->voltage_max[2] = (val >> 8) & 0xff;
531                 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
532                 data->voltage_min[3] = val & 0xff;
533                 data->voltage_max[3] = (val >> 8) & 0xff;
534
535                 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
536                 data->fan_in[0] = (val >> 8) & 0xff;
537                 data->fan_in[1] = val & 0xff;
538
539                 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
540                 data->fan_min[0] = (val >> 8) & 0xff;
541                 data->fan_min[1] = val & 0xff;
542
543                 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
544                 data->temp_max =
545                     gl518_read_value(client, GL518_REG_TEMP_MAX);
546                 data->temp_hyst =
547                     gl518_read_value(client, GL518_REG_TEMP_HYST);
548
549                 val = gl518_read_value(client, GL518_REG_MISC);
550                 data->fan_div[0] = (val >> 6) & 0x03;
551                 data->fan_div[1] = (val >> 4) & 0x03;
552                 data->fan_auto1  = (val >> 3) & 0x01;
553
554                 data->alarms &= data->alarm_mask;
555
556                 val = gl518_read_value(client, GL518_REG_CONF);
557                 data->beep_enable = (val >> 2) & 1;
558
559                 if (data->type != gl518sm_r00) {
560                         data->voltage_in[0] =
561                             gl518_read_value(client, GL518_REG_VDD);
562                         data->voltage_in[1] =
563                             gl518_read_value(client, GL518_REG_VIN1);
564                         data->voltage_in[2] =
565                             gl518_read_value(client, GL518_REG_VIN2);
566                 }
567                 data->voltage_in[3] =
568                     gl518_read_value(client, GL518_REG_VIN3);
569
570                 data->last_updated = jiffies;
571                 data->valid = 1;
572         }
573
574         up(&data->update_lock);
575
576         return data;
577 }
578
579 static int __init sensors_gl518sm_init(void)
580 {
581         return i2c_add_driver(&gl518_driver);
582 }
583
584 static void __exit sensors_gl518sm_exit(void)
585 {
586         i2c_del_driver(&gl518_driver);
587 }
588
589 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
590         "Kyosti Malkki <kmalkki@cc.hut.fi> and "
591         "Hong-Gunn Chew <hglinux@gunnet.org>");
592 MODULE_DESCRIPTION("GL518SM driver");
593 MODULE_LICENSE("GPL");
594
595 module_init(sensors_gl518sm_init);
596 module_exit(sensors_gl518sm_exit);