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