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