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