RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / hwmon / gl518sm.c
blob212de681853ab33057ac291f6344a93153651586
3 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
6 #include <linux/jiffies.h>
7 #include <linux/i2c.h>
8 #include <linux/hwmon.h>
9 #include <linux/hwmon-sysfs.h>
10 #include <linux/err.h>
11 #include <linux/mutex.h>
12 #include <linux/sysfs.h>
14 /* Addresses to scan */
15 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
17 enum chips { gl518sm_r00, gl518sm_r80 };
19 /* Many GL518 constants specified below */
21 /* The GL518 registers */
22 #define GL518_REG_CHIP_ID 0x00
23 #define GL518_REG_REVISION 0x01
24 #define GL518_REG_VENDOR_ID 0x02
25 #define GL518_REG_CONF 0x03
26 #define GL518_REG_TEMP_IN 0x04
27 #define GL518_REG_TEMP_MAX 0x05
28 #define GL518_REG_TEMP_HYST 0x06
29 #define GL518_REG_FAN_COUNT 0x07
30 #define GL518_REG_FAN_LIMIT 0x08
31 #define GL518_REG_VIN1_LIMIT 0x09
32 #define GL518_REG_VIN2_LIMIT 0x0a
33 #define GL518_REG_VIN3_LIMIT 0x0b
34 #define GL518_REG_VDD_LIMIT 0x0c
35 #define GL518_REG_VIN3 0x0d
36 #define GL518_REG_MISC 0x0f
37 #define GL518_REG_ALARM 0x10
38 #define GL518_REG_MASK 0x11
39 #define GL518_REG_INT 0x12
40 #define GL518_REG_VIN2 0x13
41 #define GL518_REG_VIN1 0x14
42 #define GL518_REG_VDD 0x15
46 * Conversions. Rounding and limit checking is only done on the TO_REG
47 * variants. Note that you should be a bit careful with which arguments
48 * these macros are called: arguments may be evaluated more than once.
49 * Fixing this is just not worth it.
52 #define RAW_FROM_REG(val) val
54 #define BOOL_FROM_REG(val) ((val)?0:1)
55 #define BOOL_TO_REG(val) ((val)?0:1)
57 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \
58 (val)-500:(val)+500)/1000)+119),0,255))
59 #define TEMP_FROM_REG(val) (((val) - 119) * 1000)
61 static inline u8 FAN_TO_REG(long rpm, int div)
63 long rpmdiv;
64 if (rpm == 0)
65 return 0;
66 rpmdiv = SENSORS_LIMIT(rpm, 1, 960000) * div;
67 return SENSORS_LIMIT((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
69 #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val)*(div))))
71 #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
72 #define IN_FROM_REG(val) ((val)*19)
74 #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
75 #define VDD_FROM_REG(val) (((val)*95+2)/4)
77 #define DIV_FROM_REG(val) (1 << (val))
79 #define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
80 #define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
82 /* Each client has this additional data */
83 struct gl518_data {
84 struct device *hwmon_dev;
85 enum chips type;
87 struct mutex update_lock;
88 char valid; /* !=0 if following fields are valid */
89 unsigned long last_updated; /* In jiffies */
91 u8 voltage_in[4]; /* Register values; [0] = VDD */
92 u8 voltage_min[4]; /* Register values; [0] = VDD */
93 u8 voltage_max[4]; /* Register values; [0] = VDD */
94 u8 fan_in[2];
95 u8 fan_min[2];
96 u8 fan_div[2]; /* Register encoding, shifted right */
97 u8 fan_auto1; /* Boolean */
98 u8 temp_in; /* Register values */
99 u8 temp_max; /* Register values */
100 u8 temp_hyst; /* Register values */
101 u8 alarms; /* Register value */
102 u8 alarm_mask;
103 u8 beep_mask; /* Register value */
104 u8 beep_enable; /* Boolean */
107 static int gl518_probe(struct i2c_client *client,
108 const struct i2c_device_id *id);
109 static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info);
110 static void gl518_init_client(struct i2c_client *client);
111 static int gl518_remove(struct i2c_client *client);
112 static int gl518_read_value(struct i2c_client *client, u8 reg);
113 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
114 static struct gl518_data *gl518_update_device(struct device *dev);
116 static const struct i2c_device_id gl518_id[] = {
117 { "gl518sm", 0 },
120 MODULE_DEVICE_TABLE(i2c, gl518_id);
122 /* This is the driver that will be inserted */
123 static struct i2c_driver gl518_driver = {
124 .class = I2C_CLASS_HWMON,
125 .driver = {
126 .name = "gl518sm",
128 .probe = gl518_probe,
129 .remove = gl518_remove,
130 .id_table = gl518_id,
131 .detect = gl518_detect,
132 .address_list = normal_i2c,
136 * Sysfs stuff
139 #define show(type, suffix, value) \
140 static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
142 struct gl518_data *data = gl518_update_device(dev); \
143 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
146 show(TEMP, temp_input1, temp_in);
147 show(TEMP, temp_max1, temp_max);
148 show(TEMP, temp_hyst1, temp_hyst);
149 show(BOOL, fan_auto1, fan_auto1);
150 show(VDD, in_input0, voltage_in[0]);
151 show(IN, in_input1, voltage_in[1]);
152 show(IN, in_input2, voltage_in[2]);
153 show(IN, in_input3, voltage_in[3]);
154 show(VDD, in_min0, voltage_min[0]);
155 show(IN, in_min1, voltage_min[1]);
156 show(IN, in_min2, voltage_min[2]);
157 show(IN, in_min3, voltage_min[3]);
158 show(VDD, in_max0, voltage_max[0]);
159 show(IN, in_max1, voltage_max[1]);
160 show(IN, in_max2, voltage_max[2]);
161 show(IN, in_max3, voltage_max[3]);
162 show(RAW, alarms, alarms);
163 show(BOOL, beep_enable, beep_enable);
164 show(BEEP_MASK, beep_mask, beep_mask);
166 static ssize_t show_fan_input(struct device *dev,
167 struct device_attribute *attr, char *buf)
169 int nr = to_sensor_dev_attr(attr)->index;
170 struct gl518_data *data = gl518_update_device(dev);
171 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
172 DIV_FROM_REG(data->fan_div[nr])));
175 static ssize_t show_fan_min(struct device *dev,
176 struct device_attribute *attr, char *buf)
178 int nr = to_sensor_dev_attr(attr)->index;
179 struct gl518_data *data = gl518_update_device(dev);
180 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
181 DIV_FROM_REG(data->fan_div[nr])));
184 static ssize_t show_fan_div(struct device *dev,
185 struct device_attribute *attr, char *buf)
187 int nr = to_sensor_dev_attr(attr)->index;
188 struct gl518_data *data = gl518_update_device(dev);
189 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
192 #define set(type, suffix, value, reg) \
193 static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
194 size_t count) \
196 struct i2c_client *client = to_i2c_client(dev); \
197 struct gl518_data *data = i2c_get_clientdata(client); \
198 long val = simple_strtol(buf, NULL, 10); \
200 mutex_lock(&data->update_lock); \
201 data->value = type##_TO_REG(val); \
202 gl518_write_value(client, reg, data->value); \
203 mutex_unlock(&data->update_lock); \
204 return count; \
207 #define set_bits(type, suffix, value, reg, mask, shift) \
208 static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
209 size_t count) \
211 struct i2c_client *client = to_i2c_client(dev); \
212 struct gl518_data *data = i2c_get_clientdata(client); \
213 int regvalue; \
214 unsigned long val = simple_strtoul(buf, NULL, 10); \
216 mutex_lock(&data->update_lock); \
217 regvalue = gl518_read_value(client, reg); \
218 data->value = type##_TO_REG(val); \
219 regvalue = (regvalue & ~mask) | (data->value << shift); \
220 gl518_write_value(client, reg, regvalue); \
221 mutex_unlock(&data->update_lock); \
222 return count; \
225 #define set_low(type, suffix, value, reg) \
226 set_bits(type, suffix, value, reg, 0x00ff, 0)
227 #define set_high(type, suffix, value, reg) \
228 set_bits(type, suffix, value, reg, 0xff00, 8)
230 set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
231 set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
232 set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
233 set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
234 set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
235 set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
236 set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
237 set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
238 set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
239 set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
240 set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
241 set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
242 set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
244 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
245 const char *buf, size_t count)
247 struct i2c_client *client = to_i2c_client(dev);
248 struct gl518_data *data = i2c_get_clientdata(client);
249 int nr = to_sensor_dev_attr(attr)->index;
250 int regvalue;
251 unsigned long val = simple_strtoul(buf, NULL, 10);
253 mutex_lock(&data->update_lock);
254 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
255 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
256 regvalue = (regvalue & (0xff << (8 * nr)))
257 | (data->fan_min[nr] << (8 * (1 - nr)));
258 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
260 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
261 if (data->fan_min[nr] == 0)
262 data->alarm_mask &= ~(0x20 << nr);
263 else
264 data->alarm_mask |= (0x20 << nr);
265 data->beep_mask &= data->alarm_mask;
266 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
268 mutex_unlock(&data->update_lock);
269 return count;
272 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
273 const char *buf, size_t count)
275 struct i2c_client *client = to_i2c_client(dev);
276 struct gl518_data *data = i2c_get_clientdata(client);
277 int nr = to_sensor_dev_attr(attr)->index;
278 int regvalue;
279 unsigned long val = simple_strtoul(buf, NULL, 10);
281 switch (val) {
282 case 1: val = 0; break;
283 case 2: val = 1; break;
284 case 4: val = 2; break;
285 case 8: val = 3; break;
286 default:
287 dev_err(dev, "Invalid fan clock divider %lu, choose one "
288 "of 1, 2, 4 or 8\n", val);
289 return -EINVAL;
292 mutex_lock(&data->update_lock);
293 regvalue = gl518_read_value(client, GL518_REG_MISC);
294 data->fan_div[nr] = val;
295 regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
296 | (data->fan_div[nr] << (6 - 2 * nr));
297 gl518_write_value(client, GL518_REG_MISC, regvalue);
298 mutex_unlock(&data->update_lock);
299 return count;
302 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
303 static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
304 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
305 show_temp_hyst1, set_temp_hyst1);
306 static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
307 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
308 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
309 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO,
310 show_fan_min, set_fan_min, 0);
311 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO,
312 show_fan_min, set_fan_min, 1);
313 static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO,
314 show_fan_div, set_fan_div, 0);
315 static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO,
316 show_fan_div, set_fan_div, 1);
317 static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
318 static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
319 static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
320 static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
321 static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
322 static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
323 static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
324 static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
325 static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
326 static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
327 static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
328 static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
329 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
330 static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
331 show_beep_enable, set_beep_enable);
332 static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
333 show_beep_mask, set_beep_mask);
335 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
336 char *buf)
338 int bitnr = to_sensor_dev_attr(attr)->index;
339 struct gl518_data *data = gl518_update_device(dev);
340 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
343 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
344 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
345 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
346 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
347 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
348 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
349 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
351 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
352 char *buf)
354 int bitnr = to_sensor_dev_attr(attr)->index;
355 struct gl518_data *data = gl518_update_device(dev);
356 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
359 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
360 const char *buf, size_t count)
362 struct i2c_client *client = to_i2c_client(dev);
363 struct gl518_data *data = i2c_get_clientdata(client);
364 int bitnr = to_sensor_dev_attr(attr)->index;
365 unsigned long bit;
367 bit = simple_strtoul(buf, NULL, 10);
368 if (bit & ~1)
369 return -EINVAL;
371 mutex_lock(&data->update_lock);
372 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
373 if (bit)
374 data->beep_mask |= (1 << bitnr);
375 else
376 data->beep_mask &= ~(1 << bitnr);
377 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
378 mutex_unlock(&data->update_lock);
379 return count;
382 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
383 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
384 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
385 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
386 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
387 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
388 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
390 static struct attribute *gl518_attributes[] = {
391 &dev_attr_in3_input.attr,
392 &dev_attr_in0_min.attr,
393 &dev_attr_in1_min.attr,
394 &dev_attr_in2_min.attr,
395 &dev_attr_in3_min.attr,
396 &dev_attr_in0_max.attr,
397 &dev_attr_in1_max.attr,
398 &dev_attr_in2_max.attr,
399 &dev_attr_in3_max.attr,
400 &sensor_dev_attr_in0_alarm.dev_attr.attr,
401 &sensor_dev_attr_in1_alarm.dev_attr.attr,
402 &sensor_dev_attr_in2_alarm.dev_attr.attr,
403 &sensor_dev_attr_in3_alarm.dev_attr.attr,
404 &sensor_dev_attr_in0_beep.dev_attr.attr,
405 &sensor_dev_attr_in1_beep.dev_attr.attr,
406 &sensor_dev_attr_in2_beep.dev_attr.attr,
407 &sensor_dev_attr_in3_beep.dev_attr.attr,
409 &dev_attr_fan1_auto.attr,
410 &sensor_dev_attr_fan1_input.dev_attr.attr,
411 &sensor_dev_attr_fan2_input.dev_attr.attr,
412 &sensor_dev_attr_fan1_min.dev_attr.attr,
413 &sensor_dev_attr_fan2_min.dev_attr.attr,
414 &sensor_dev_attr_fan1_div.dev_attr.attr,
415 &sensor_dev_attr_fan2_div.dev_attr.attr,
416 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
417 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
418 &sensor_dev_attr_fan1_beep.dev_attr.attr,
419 &sensor_dev_attr_fan2_beep.dev_attr.attr,
421 &dev_attr_temp1_input.attr,
422 &dev_attr_temp1_max.attr,
423 &dev_attr_temp1_max_hyst.attr,
424 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
425 &sensor_dev_attr_temp1_beep.dev_attr.attr,
427 &dev_attr_alarms.attr,
428 &dev_attr_beep_enable.attr,
429 &dev_attr_beep_mask.attr,
430 NULL
433 static const struct attribute_group gl518_group = {
434 .attrs = gl518_attributes,
437 static struct attribute *gl518_attributes_r80[] = {
438 &dev_attr_in0_input.attr,
439 &dev_attr_in1_input.attr,
440 &dev_attr_in2_input.attr,
441 NULL
444 static const struct attribute_group gl518_group_r80 = {
445 .attrs = gl518_attributes_r80,
449 * Real code
452 /* Return 0 if detection is successful, -ENODEV otherwise */
453 static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
455 struct i2c_adapter *adapter = client->adapter;
456 int rev;
458 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
459 I2C_FUNC_SMBUS_WORD_DATA))
460 return -ENODEV;
462 /* Now, we do the remaining detection. */
463 if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
464 || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
465 return -ENODEV;
467 /* Determine the chip type. */
468 rev = gl518_read_value(client, GL518_REG_REVISION);
469 if (rev != 0x00 && rev != 0x80)
470 return -ENODEV;
472 strlcpy(info->type, "gl518sm", I2C_NAME_SIZE);
474 return 0;
477 static int gl518_probe(struct i2c_client *client,
478 const struct i2c_device_id *id)
480 struct gl518_data *data;
481 int err, revision;
483 data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL);
484 if (!data) {
485 err = -ENOMEM;
486 goto exit;
489 i2c_set_clientdata(client, data);
490 revision = gl518_read_value(client, GL518_REG_REVISION);
491 data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
492 mutex_init(&data->update_lock);
494 /* Initialize the GL518SM chip */
495 data->alarm_mask = 0xff;
496 gl518_init_client(client);
498 /* Register sysfs hooks */
499 if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group)))
500 goto exit_free;
501 if (data->type == gl518sm_r80)
502 if ((err = sysfs_create_group(&client->dev.kobj,
503 &gl518_group_r80)))
504 goto exit_remove_files;
506 data->hwmon_dev = hwmon_device_register(&client->dev);
507 if (IS_ERR(data->hwmon_dev)) {
508 err = PTR_ERR(data->hwmon_dev);
509 goto exit_remove_files;
512 return 0;
514 exit_remove_files:
515 sysfs_remove_group(&client->dev.kobj, &gl518_group);
516 if (data->type == gl518sm_r80)
517 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
518 exit_free:
519 kfree(data);
520 exit:
521 return err;
525 /* Called when we have found a new GL518SM.
526 Note that we preserve D4:NoFan2 and D2:beep_enable. */
527 static void gl518_init_client(struct i2c_client *client)
529 /* Make sure we leave D7:Reset untouched */
530 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
532 /* Comparator mode (D3=0), standby mode (D6=0) */
533 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
535 /* Never interrupts */
536 gl518_write_value(client, GL518_REG_MASK, 0x00);
538 /* Clear status register (D5=1), start (D6=1) */
539 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
540 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
543 static int gl518_remove(struct i2c_client *client)
545 struct gl518_data *data = i2c_get_clientdata(client);
547 hwmon_device_unregister(data->hwmon_dev);
548 sysfs_remove_group(&client->dev.kobj, &gl518_group);
549 if (data->type == gl518sm_r80)
550 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
552 kfree(data);
553 return 0;
556 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
557 GL518 uses a high-byte first convention, which is exactly opposite to
558 the SMBus standard. */
559 static int gl518_read_value(struct i2c_client *client, u8 reg)
561 if ((reg >= 0x07) && (reg <= 0x0c))
562 return swab16(i2c_smbus_read_word_data(client, reg));
563 else
564 return i2c_smbus_read_byte_data(client, reg);
567 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
569 if ((reg >= 0x07) && (reg <= 0x0c))
570 return i2c_smbus_write_word_data(client, reg, swab16(value));
571 else
572 return i2c_smbus_write_byte_data(client, reg, value);
575 static struct gl518_data *gl518_update_device(struct device *dev)
577 struct i2c_client *client = to_i2c_client(dev);
578 struct gl518_data *data = i2c_get_clientdata(client);
579 int val;
581 mutex_lock(&data->update_lock);
583 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
584 || !data->valid) {
585 dev_dbg(&client->dev, "Starting gl518 update\n");
587 data->alarms = gl518_read_value(client, GL518_REG_INT);
588 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
590 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
591 data->voltage_min[0] = val & 0xff;
592 data->voltage_max[0] = (val >> 8) & 0xff;
593 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
594 data->voltage_min[1] = val & 0xff;
595 data->voltage_max[1] = (val >> 8) & 0xff;
596 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
597 data->voltage_min[2] = val & 0xff;
598 data->voltage_max[2] = (val >> 8) & 0xff;
599 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
600 data->voltage_min[3] = val & 0xff;
601 data->voltage_max[3] = (val >> 8) & 0xff;
603 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
604 data->fan_in[0] = (val >> 8) & 0xff;
605 data->fan_in[1] = val & 0xff;
607 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
608 data->fan_min[0] = (val >> 8) & 0xff;
609 data->fan_min[1] = val & 0xff;
611 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
612 data->temp_max =
613 gl518_read_value(client, GL518_REG_TEMP_MAX);
614 data->temp_hyst =
615 gl518_read_value(client, GL518_REG_TEMP_HYST);
617 val = gl518_read_value(client, GL518_REG_MISC);
618 data->fan_div[0] = (val >> 6) & 0x03;
619 data->fan_div[1] = (val >> 4) & 0x03;
620 data->fan_auto1 = (val >> 3) & 0x01;
622 data->alarms &= data->alarm_mask;
624 val = gl518_read_value(client, GL518_REG_CONF);
625 data->beep_enable = (val >> 2) & 1;
627 if (data->type != gl518sm_r00) {
628 data->voltage_in[0] =
629 gl518_read_value(client, GL518_REG_VDD);
630 data->voltage_in[1] =
631 gl518_read_value(client, GL518_REG_VIN1);
632 data->voltage_in[2] =
633 gl518_read_value(client, GL518_REG_VIN2);
635 data->voltage_in[3] =
636 gl518_read_value(client, GL518_REG_VIN3);
638 data->last_updated = jiffies;
639 data->valid = 1;
642 mutex_unlock(&data->update_lock);
644 return data;
647 static int __init sensors_gl518sm_init(void)
649 return i2c_add_driver(&gl518_driver);
652 static void __exit sensors_gl518sm_exit(void)
654 i2c_del_driver(&gl518_driver);
657 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
658 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
659 "Hong-Gunn Chew <hglinux@gunnet.org>");
660 MODULE_DESCRIPTION("GL518SM driver");
661 MODULE_LICENSE("GPL");
663 module_init(sensors_gl518sm_init);
664 module_exit(sensors_gl518sm_exit);