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