]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/i2c/chips/gl518sm.c
Linux-2.6.12-rc2
[mirror_ubuntu-bionic-kernel.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/jiffies.h>
44 #include <linux/i2c.h>
45 #include <linux/i2c-sensor.h>
46
47 /* Addresses to scan */
48 static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
49 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
50
51 /* Insmod parameters */
52 SENSORS_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
96 static 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 */
119 struct gl518_data {
120 struct i2c_client client;
121 enum chips type;
122
123 struct semaphore update_lock;
124 char valid; /* !=0 if following fields are valid */
125 unsigned long last_updated; /* In jiffies */
126
127 u8 voltage_in[4]; /* Register values; [0] = VDD */
128 u8 voltage_min[4]; /* Register values; [0] = VDD */
129 u8 voltage_max[4]; /* Register values; [0] = VDD */
130 u8 iter_voltage_in[4]; /* Register values; [0] = VDD */
131 u8 fan_in[2];
132 u8 fan_min[2];
133 u8 fan_div[2]; /* Register encoding, shifted right */
134 u8 fan_auto1; /* Boolean */
135 u8 temp_in; /* Register values */
136 u8 temp_max; /* Register values */
137 u8 temp_hyst; /* Register values */
138 u8 alarms; /* Register value */
139 u8 alarm_mask; /* Register value */
140 u8 beep_mask; /* Register value */
141 u8 beep_enable; /* Boolean */
142 };
143
144 static int gl518_attach_adapter(struct i2c_adapter *adapter);
145 static int gl518_detect(struct i2c_adapter *adapter, int address, int kind);
146 static void gl518_init_client(struct i2c_client *client);
147 static int gl518_detach_client(struct i2c_client *client);
148 static int gl518_read_value(struct i2c_client *client, u8 reg);
149 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
150 static struct gl518_data *gl518_update_device(struct device *dev);
151
152 /* This is the driver that will be inserted */
153 static struct i2c_driver gl518_driver = {
154 .owner = THIS_MODULE,
155 .name = "gl518sm",
156 .id = I2C_DRIVERID_GL518,
157 .flags = I2C_DF_NOTIFY,
158 .attach_adapter = gl518_attach_adapter,
159 .detach_client = gl518_detach_client,
160 };
161
162 /*
163 * Sysfs stuff
164 */
165
166 #define show(type, suffix, value) \
167 static ssize_t show_##suffix(struct device *dev, char *buf) \
168 { \
169 struct gl518_data *data = gl518_update_device(dev); \
170 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
171 }
172
173 #define show_fan(suffix, value, index) \
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", FAN_FROM_REG(data->value[index], \
178 DIV_FROM_REG(data->fan_div[index]))); \
179 }
180
181 show(TEMP, temp_input1, temp_in);
182 show(TEMP, temp_max1, temp_max);
183 show(TEMP, temp_hyst1, temp_hyst);
184 show(BOOL, fan_auto1, fan_auto1);
185 show_fan(fan_input1, fan_in, 0);
186 show_fan(fan_input2, fan_in, 1);
187 show_fan(fan_min1, fan_min, 0);
188 show_fan(fan_min2, fan_min, 1);
189 show(DIV, fan_div1, fan_div[0]);
190 show(DIV, fan_div2, fan_div[1]);
191 show(VDD, in_input0, voltage_in[0]);
192 show(IN, in_input1, voltage_in[1]);
193 show(IN, in_input2, voltage_in[2]);
194 show(IN, in_input3, voltage_in[3]);
195 show(VDD, in_min0, voltage_min[0]);
196 show(IN, in_min1, voltage_min[1]);
197 show(IN, in_min2, voltage_min[2]);
198 show(IN, in_min3, voltage_min[3]);
199 show(VDD, in_max0, voltage_max[0]);
200 show(IN, in_max1, voltage_max[1]);
201 show(IN, in_max2, voltage_max[2]);
202 show(IN, in_max3, voltage_max[3]);
203 show(RAW, alarms, alarms);
204 show(BOOL, beep_enable, beep_enable);
205 show(BEEP_MASK, beep_mask, beep_mask);
206
207 #define set(type, suffix, value, reg) \
208 static ssize_t set_##suffix(struct device *dev, const char *buf, \
209 size_t count) \
210 { \
211 struct i2c_client *client = to_i2c_client(dev); \
212 struct gl518_data *data = i2c_get_clientdata(client); \
213 long val = simple_strtol(buf, NULL, 10); \
214 \
215 down(&data->update_lock); \
216 data->value = type##_TO_REG(val); \
217 gl518_write_value(client, reg, data->value); \
218 up(&data->update_lock); \
219 return count; \
220 }
221
222 #define set_bits(type, suffix, value, reg, mask, shift) \
223 static ssize_t set_##suffix(struct device *dev, const char *buf, \
224 size_t count) \
225 { \
226 struct i2c_client *client = to_i2c_client(dev); \
227 struct gl518_data *data = i2c_get_clientdata(client); \
228 int regvalue; \
229 unsigned long val = simple_strtoul(buf, NULL, 10); \
230 \
231 down(&data->update_lock); \
232 regvalue = gl518_read_value(client, reg); \
233 data->value = type##_TO_REG(val); \
234 regvalue = (regvalue & ~mask) | (data->value << shift); \
235 gl518_write_value(client, reg, regvalue); \
236 up(&data->update_lock); \
237 return count; \
238 }
239
240 #define set_low(type, suffix, value, reg) \
241 set_bits(type, suffix, value, reg, 0x00ff, 0)
242 #define set_high(type, suffix, value, reg) \
243 set_bits(type, suffix, value, reg, 0xff00, 8)
244
245 set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
246 set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
247 set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
248 set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0xc0, 6);
249 set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0x30, 4);
250 set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
251 set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
252 set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
253 set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
254 set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
255 set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
256 set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
257 set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
258 set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
259 set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
260
261 static ssize_t set_fan_min1(struct device *dev, const char *buf, size_t count)
262 {
263 struct i2c_client *client = to_i2c_client(dev);
264 struct gl518_data *data = i2c_get_clientdata(client);
265 int regvalue;
266 unsigned long val = simple_strtoul(buf, NULL, 10);
267
268 down(&data->update_lock);
269 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
270 data->fan_min[0] = FAN_TO_REG(val,
271 DIV_FROM_REG(data->fan_div[0]));
272 regvalue = (regvalue & 0x00ff) | (data->fan_min[0] << 8);
273 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
274
275 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
276 if (data->fan_min[0] == 0)
277 data->alarm_mask &= ~0x20;
278 else
279 data->alarm_mask |= 0x20;
280 data->beep_mask &= data->alarm_mask;
281 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
282
283 up(&data->update_lock);
284 return count;
285 }
286
287 static ssize_t set_fan_min2(struct device *dev, const char *buf, size_t count)
288 {
289 struct i2c_client *client = to_i2c_client(dev);
290 struct gl518_data *data = i2c_get_clientdata(client);
291 int regvalue;
292 unsigned long val = simple_strtoul(buf, NULL, 10);
293
294 down(&data->update_lock);
295 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
296 data->fan_min[1] = FAN_TO_REG(val,
297 DIV_FROM_REG(data->fan_div[1]));
298 regvalue = (regvalue & 0xff00) | data->fan_min[1];
299 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
300
301 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
302 if (data->fan_min[1] == 0)
303 data->alarm_mask &= ~0x40;
304 else
305 data->alarm_mask |= 0x40;
306 data->beep_mask &= data->alarm_mask;
307 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
308
309 up(&data->update_lock);
310 return count;
311 }
312
313 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
314 static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
315 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
316 show_temp_hyst1, set_temp_hyst1);
317 static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
318 static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
319 static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
320 static DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1);
321 static DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2);
322 static DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1);
323 static DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2);
324 static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
325 static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
326 static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
327 static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
328 static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
329 static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
330 static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
331 static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
332 static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
333 static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
334 static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
335 static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
336 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
337 static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
338 show_beep_enable, set_beep_enable);
339 static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
340 show_beep_mask, set_beep_mask);
341
342 /*
343 * Real code
344 */
345
346 static int gl518_attach_adapter(struct i2c_adapter *adapter)
347 {
348 if (!(adapter->class & I2C_CLASS_HWMON))
349 return 0;
350 return i2c_detect(adapter, &addr_data, gl518_detect);
351 }
352
353 static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
354 {
355 int i;
356 struct i2c_client *new_client;
357 struct gl518_data *data;
358 int err = 0;
359
360 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
361 I2C_FUNC_SMBUS_WORD_DATA))
362 goto exit;
363
364 /* OK. For now, we presume we have a valid client. We now create the
365 client structure, even though we cannot fill it completely yet.
366 But it allows us to access gl518_{read,write}_value. */
367
368 if (!(data = kmalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
369 err = -ENOMEM;
370 goto exit;
371 }
372 memset(data, 0, sizeof(struct gl518_data));
373
374 new_client = &data->client;
375 i2c_set_clientdata(new_client, data);
376
377 new_client->addr = address;
378 new_client->adapter = adapter;
379 new_client->driver = &gl518_driver;
380 new_client->flags = 0;
381
382 /* Now, we do the remaining detection. */
383
384 if (kind < 0) {
385 if ((gl518_read_value(new_client, GL518_REG_CHIP_ID) != 0x80)
386 || (gl518_read_value(new_client, GL518_REG_CONF) & 0x80))
387 goto exit_free;
388 }
389
390 /* Determine the chip type. */
391 if (kind <= 0) {
392 i = gl518_read_value(new_client, GL518_REG_REVISION);
393 if (i == 0x00) {
394 kind = gl518sm_r00;
395 } else if (i == 0x80) {
396 kind = gl518sm_r80;
397 } else {
398 if (kind <= 0)
399 dev_info(&adapter->dev,
400 "Ignoring 'force' parameter for unknown "
401 "chip at adapter %d, address 0x%02x\n",
402 i2c_adapter_id(adapter), address);
403 goto exit_free;
404 }
405 }
406
407 /* Fill in the remaining client fields */
408 strlcpy(new_client->name, "gl518sm", I2C_NAME_SIZE);
409 data->type = kind;
410 data->valid = 0;
411 init_MUTEX(&data->update_lock);
412
413 /* Tell the I2C layer a new client has arrived */
414 if ((err = i2c_attach_client(new_client)))
415 goto exit_free;
416
417 /* Initialize the GL518SM chip */
418 data->alarm_mask = 0xff;
419 data->voltage_in[0]=data->voltage_in[1]=data->voltage_in[2]=0;
420 gl518_init_client((struct i2c_client *) new_client);
421
422 /* Register sysfs hooks */
423 device_create_file(&new_client->dev, &dev_attr_in0_input);
424 device_create_file(&new_client->dev, &dev_attr_in1_input);
425 device_create_file(&new_client->dev, &dev_attr_in2_input);
426 device_create_file(&new_client->dev, &dev_attr_in3_input);
427 device_create_file(&new_client->dev, &dev_attr_in0_min);
428 device_create_file(&new_client->dev, &dev_attr_in1_min);
429 device_create_file(&new_client->dev, &dev_attr_in2_min);
430 device_create_file(&new_client->dev, &dev_attr_in3_min);
431 device_create_file(&new_client->dev, &dev_attr_in0_max);
432 device_create_file(&new_client->dev, &dev_attr_in1_max);
433 device_create_file(&new_client->dev, &dev_attr_in2_max);
434 device_create_file(&new_client->dev, &dev_attr_in3_max);
435 device_create_file(&new_client->dev, &dev_attr_fan1_auto);
436 device_create_file(&new_client->dev, &dev_attr_fan1_input);
437 device_create_file(&new_client->dev, &dev_attr_fan2_input);
438 device_create_file(&new_client->dev, &dev_attr_fan1_min);
439 device_create_file(&new_client->dev, &dev_attr_fan2_min);
440 device_create_file(&new_client->dev, &dev_attr_fan1_div);
441 device_create_file(&new_client->dev, &dev_attr_fan2_div);
442 device_create_file(&new_client->dev, &dev_attr_temp1_input);
443 device_create_file(&new_client->dev, &dev_attr_temp1_max);
444 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
445 device_create_file(&new_client->dev, &dev_attr_alarms);
446 device_create_file(&new_client->dev, &dev_attr_beep_enable);
447 device_create_file(&new_client->dev, &dev_attr_beep_mask);
448
449 return 0;
450
451 /* OK, this is not exactly good programming practice, usually. But it is
452 very code-efficient in this case. */
453
454 exit_free:
455 kfree(data);
456 exit:
457 return err;
458 }
459
460
461 /* Called when we have found a new GL518SM.
462 Note that we preserve D4:NoFan2 and D2:beep_enable. */
463 static void gl518_init_client(struct i2c_client *client)
464 {
465 /* Make sure we leave D7:Reset untouched */
466 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
467
468 /* Comparator mode (D3=0), standby mode (D6=0) */
469 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
470
471 /* Never interrupts */
472 gl518_write_value(client, GL518_REG_MASK, 0x00);
473
474 /* Clear status register (D5=1), start (D6=1) */
475 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
476 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
477 }
478
479 static int gl518_detach_client(struct i2c_client *client)
480 {
481 int err;
482
483 if ((err = i2c_detach_client(client))) {
484 dev_err(&client->dev, "Client deregistration failed, "
485 "client not detached.\n");
486 return err;
487 }
488
489 kfree(i2c_get_clientdata(client));
490
491 return 0;
492 }
493
494 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
495 GL518 uses a high-byte first convention, which is exactly opposite to
496 the usual practice. */
497 static int gl518_read_value(struct i2c_client *client, u8 reg)
498 {
499 if ((reg >= 0x07) && (reg <= 0x0c))
500 return swab16(i2c_smbus_read_word_data(client, reg));
501 else
502 return i2c_smbus_read_byte_data(client, reg);
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. */
508 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
509 {
510 if ((reg >= 0x07) && (reg <= 0x0c))
511 return i2c_smbus_write_word_data(client, reg, swab16(value));
512 else
513 return i2c_smbus_write_byte_data(client, reg, value);
514 }
515
516 static struct gl518_data *gl518_update_device(struct device *dev)
517 {
518 struct i2c_client *client = to_i2c_client(dev);
519 struct gl518_data *data = i2c_get_clientdata(client);
520 int val;
521
522 down(&data->update_lock);
523
524 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
525 || !data->valid) {
526 dev_dbg(&client->dev, "Starting gl518 update\n");
527
528 data->alarms = gl518_read_value(client, GL518_REG_INT);
529 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
530
531 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
532 data->voltage_min[0] = val & 0xff;
533 data->voltage_max[0] = (val >> 8) & 0xff;
534 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
535 data->voltage_min[1] = val & 0xff;
536 data->voltage_max[1] = (val >> 8) & 0xff;
537 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
538 data->voltage_min[2] = val & 0xff;
539 data->voltage_max[2] = (val >> 8) & 0xff;
540 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
541 data->voltage_min[3] = val & 0xff;
542 data->voltage_max[3] = (val >> 8) & 0xff;
543
544 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
545 data->fan_in[0] = (val >> 8) & 0xff;
546 data->fan_in[1] = val & 0xff;
547
548 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
549 data->fan_min[0] = (val >> 8) & 0xff;
550 data->fan_min[1] = val & 0xff;
551
552 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
553 data->temp_max =
554 gl518_read_value(client, GL518_REG_TEMP_MAX);
555 data->temp_hyst =
556 gl518_read_value(client, GL518_REG_TEMP_HYST);
557
558 val = gl518_read_value(client, GL518_REG_MISC);
559 data->fan_div[0] = (val >> 6) & 0x03;
560 data->fan_div[1] = (val >> 4) & 0x03;
561 data->fan_auto1 = (val >> 3) & 0x01;
562
563 data->alarms &= data->alarm_mask;
564
565 val = gl518_read_value(client, GL518_REG_CONF);
566 data->beep_enable = (val >> 2) & 1;
567
568 if (data->type != gl518sm_r00) {
569 data->voltage_in[0] =
570 gl518_read_value(client, GL518_REG_VDD);
571 data->voltage_in[1] =
572 gl518_read_value(client, GL518_REG_VIN1);
573 data->voltage_in[2] =
574 gl518_read_value(client, GL518_REG_VIN2);
575 }
576 data->voltage_in[3] =
577 gl518_read_value(client, GL518_REG_VIN3);
578
579 data->last_updated = jiffies;
580 data->valid = 1;
581 }
582
583 up(&data->update_lock);
584
585 return data;
586 }
587
588 static int __init sensors_gl518sm_init(void)
589 {
590 return i2c_add_driver(&gl518_driver);
591 }
592
593 static void __exit sensors_gl518sm_exit(void)
594 {
595 i2c_del_driver(&gl518_driver);
596 }
597
598 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
599 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
600 "Hong-Gunn Chew <hglinux@gunnet.org>");
601 MODULE_DESCRIPTION("GL518SM driver");
602 MODULE_LICENSE("GPL");
603
604 module_init(sensors_gl518sm_init);
605 module_exit(sensors_gl518sm_exit);