[CPUFREQ] CodingStyle nits in cpufreq_stats.c
[linux/fpc-iii.git] / drivers / hwmon / gl520sm.c
blob14e810f3c2c09db43ab6687efbfaa8076c65fc5c
1 /*
2 gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>,
5 Kyösti Mälkki <kmalkki@cc.hut.fi>
6 Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-vid.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
34 /* Type of the extra sensor */
35 static unsigned short extra_sensor_type;
36 module_param(extra_sensor_type, ushort, 0);
37 MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
39 /* Addresses to scan */
40 static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
42 /* Insmod parameters */
43 I2C_CLIENT_INSMOD_1(gl520sm);
45 /* Many GL520 constants specified below
46 One of the inputs can be configured as either temp or voltage.
47 That's why _TEMP2 and _IN4 access the same register
50 /* The GL520 registers */
51 #define GL520_REG_CHIP_ID 0x00
52 #define GL520_REG_REVISION 0x01
53 #define GL520_REG_CONF 0x03
54 #define GL520_REG_MASK 0x11
56 #define GL520_REG_VID_INPUT 0x02
58 #define GL520_REG_IN0_INPUT 0x15
59 #define GL520_REG_IN0_LIMIT 0x0c
60 #define GL520_REG_IN0_MIN GL520_REG_IN0_LIMIT
61 #define GL520_REG_IN0_MAX GL520_REG_IN0_LIMIT
63 #define GL520_REG_IN1_INPUT 0x14
64 #define GL520_REG_IN1_LIMIT 0x09
65 #define GL520_REG_IN1_MIN GL520_REG_IN1_LIMIT
66 #define GL520_REG_IN1_MAX GL520_REG_IN1_LIMIT
68 #define GL520_REG_IN2_INPUT 0x13
69 #define GL520_REG_IN2_LIMIT 0x0a
70 #define GL520_REG_IN2_MIN GL520_REG_IN2_LIMIT
71 #define GL520_REG_IN2_MAX GL520_REG_IN2_LIMIT
73 #define GL520_REG_IN3_INPUT 0x0d
74 #define GL520_REG_IN3_LIMIT 0x0b
75 #define GL520_REG_IN3_MIN GL520_REG_IN3_LIMIT
76 #define GL520_REG_IN3_MAX GL520_REG_IN3_LIMIT
78 #define GL520_REG_IN4_INPUT 0x0e
79 #define GL520_REG_IN4_MAX 0x17
80 #define GL520_REG_IN4_MIN 0x18
82 #define GL520_REG_TEMP1_INPUT 0x04
83 #define GL520_REG_TEMP1_MAX 0x05
84 #define GL520_REG_TEMP1_MAX_HYST 0x06
86 #define GL520_REG_TEMP2_INPUT 0x0e
87 #define GL520_REG_TEMP2_MAX 0x17
88 #define GL520_REG_TEMP2_MAX_HYST 0x18
90 #define GL520_REG_FAN_INPUT 0x07
91 #define GL520_REG_FAN_MIN 0x08
92 #define GL520_REG_FAN_DIV 0x0f
93 #define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
95 #define GL520_REG_ALARMS 0x12
96 #define GL520_REG_BEEP_MASK 0x10
97 #define GL520_REG_BEEP_ENABLE GL520_REG_CONF
100 * Function declarations
103 static int gl520_attach_adapter(struct i2c_adapter *adapter);
104 static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
105 static void gl520_init_client(struct i2c_client *client);
106 static int gl520_detach_client(struct i2c_client *client);
107 static int gl520_read_value(struct i2c_client *client, u8 reg);
108 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
109 static struct gl520_data *gl520_update_device(struct device *dev);
111 /* Driver data */
112 static struct i2c_driver gl520_driver = {
113 .driver = {
114 .name = "gl520sm",
116 .id = I2C_DRIVERID_GL520,
117 .attach_adapter = gl520_attach_adapter,
118 .detach_client = gl520_detach_client,
121 /* Client data */
122 struct gl520_data {
123 struct i2c_client client;
124 struct class_device *class_dev;
125 struct mutex update_lock;
126 char valid; /* zero until the following fields are valid */
127 unsigned long last_updated; /* in jiffies */
129 u8 vid;
130 u8 vrm;
131 u8 in_input[5]; /* [0] = VVD */
132 u8 in_min[5]; /* [0] = VDD */
133 u8 in_max[5]; /* [0] = VDD */
134 u8 fan_input[2];
135 u8 fan_min[2];
136 u8 fan_div[2];
137 u8 fan_off;
138 u8 temp_input[2];
139 u8 temp_max[2];
140 u8 temp_max_hyst[2];
141 u8 alarms;
142 u8 beep_enable;
143 u8 beep_mask;
144 u8 alarm_mask;
145 u8 two_temps;
149 * Sysfs stuff
152 #define sysfs_r(type, n, item, reg) \
153 static ssize_t get_##type##item (struct gl520_data *, char *, int); \
154 static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \
155 static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \
157 struct gl520_data *data = gl520_update_device(dev); \
158 return get_##type##item(data, buf, (n)); \
161 #define sysfs_w(type, n, item, reg) \
162 static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \
163 static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \
164 static ssize_t set_##type##n##item (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
166 struct i2c_client *client = to_i2c_client(dev); \
167 struct gl520_data *data = i2c_get_clientdata(client); \
168 return set_##type##item(client, data, buf, count, (n), reg); \
171 #define sysfs_rw_n(type, n, item, reg) \
172 sysfs_r(type, n, item, reg) \
173 sysfs_w(type, n, item, reg) \
174 static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item);
176 #define sysfs_ro_n(type, n, item, reg) \
177 sysfs_r(type, n, item, reg) \
178 static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL);
180 #define sysfs_rw(type, item, reg) \
181 sysfs_r(type, 0, item, reg) \
182 sysfs_w(type, 0, item, reg) \
183 static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item);
185 #define sysfs_ro(type, item, reg) \
186 sysfs_r(type, 0, item, reg) \
187 static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL);
190 #define sysfs_vid(n) \
191 sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT)
193 #define device_create_file_vid(client, n) \
194 device_create_file(&client->dev, &dev_attr_cpu##n##_vid)
196 #define sysfs_in(n) \
197 sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \
198 sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \
199 sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX) \
201 #define device_create_file_in(client, n) \
202 ({device_create_file(&client->dev, &dev_attr_in##n##_input); \
203 device_create_file(&client->dev, &dev_attr_in##n##_min); \
204 device_create_file(&client->dev, &dev_attr_in##n##_max);})
206 #define sysfs_fan(n) \
207 sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \
208 sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \
209 sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV)
211 #define device_create_file_fan(client, n) \
212 ({device_create_file(&client->dev, &dev_attr_fan##n##_input); \
213 device_create_file(&client->dev, &dev_attr_fan##n##_min); \
214 device_create_file(&client->dev, &dev_attr_fan##n##_div);})
216 #define sysfs_fan_off(n) \
217 sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) \
219 #define device_create_file_fan_off(client, n) \
220 device_create_file(&client->dev, &dev_attr_fan##n##_off)
222 #define sysfs_temp(n) \
223 sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \
224 sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \
225 sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST)
227 #define device_create_file_temp(client, n) \
228 ({device_create_file(&client->dev, &dev_attr_temp##n##_input); \
229 device_create_file(&client->dev, &dev_attr_temp##n##_max); \
230 device_create_file(&client->dev, &dev_attr_temp##n##_max_hyst);})
232 #define sysfs_alarms() \
233 sysfs_ro(alarms, , GL520_REG_ALARMS) \
234 sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \
235 sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK)
237 #define device_create_file_alarms(client) \
238 ({device_create_file(&client->dev, &dev_attr_alarms); \
239 device_create_file(&client->dev, &dev_attr_beep_enable); \
240 device_create_file(&client->dev, &dev_attr_beep_mask);})
243 sysfs_vid(0)
245 sysfs_in(0)
246 sysfs_in(1)
247 sysfs_in(2)
248 sysfs_in(3)
249 sysfs_in(4)
251 sysfs_fan(1)
252 sysfs_fan(2)
253 sysfs_fan_off(1)
255 sysfs_temp(1)
256 sysfs_temp(2)
258 sysfs_alarms()
261 static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n)
263 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
266 #define VDD_FROM_REG(val) (((val)*95+2)/4)
267 #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
269 #define IN_FROM_REG(val) ((val)*19)
270 #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
272 static ssize_t get_in_input(struct gl520_data *data, char *buf, int n)
274 u8 r = data->in_input[n];
276 if (n == 0)
277 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
278 else
279 return sprintf(buf, "%d\n", IN_FROM_REG(r));
282 static ssize_t get_in_min(struct gl520_data *data, char *buf, int n)
284 u8 r = data->in_min[n];
286 if (n == 0)
287 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
288 else
289 return sprintf(buf, "%d\n", IN_FROM_REG(r));
292 static ssize_t get_in_max(struct gl520_data *data, char *buf, int n)
294 u8 r = data->in_max[n];
296 if (n == 0)
297 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
298 else
299 return sprintf(buf, "%d\n", IN_FROM_REG(r));
302 static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
304 long v = simple_strtol(buf, NULL, 10);
305 u8 r;
307 mutex_lock(&data->update_lock);
309 if (n == 0)
310 r = VDD_TO_REG(v);
311 else
312 r = IN_TO_REG(v);
314 data->in_min[n] = r;
316 if (n < 4)
317 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
318 else
319 gl520_write_value(client, reg, r);
321 mutex_unlock(&data->update_lock);
322 return count;
325 static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
327 long v = simple_strtol(buf, NULL, 10);
328 u8 r;
330 if (n == 0)
331 r = VDD_TO_REG(v);
332 else
333 r = IN_TO_REG(v);
335 mutex_lock(&data->update_lock);
337 data->in_max[n] = r;
339 if (n < 4)
340 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
341 else
342 gl520_write_value(client, reg, r);
344 mutex_unlock(&data->update_lock);
345 return count;
348 #define DIV_FROM_REG(val) (1 << (val))
349 #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
350 #define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
352 static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n)
354 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1]));
357 static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n)
359 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1]));
362 static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n)
364 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1]));
367 static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n)
369 return sprintf(buf, "%d\n", data->fan_off);
372 static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
374 unsigned long v = simple_strtoul(buf, NULL, 10);
375 u8 r;
377 mutex_lock(&data->update_lock);
378 r = FAN_TO_REG(v, data->fan_div[n - 1]);
379 data->fan_min[n - 1] = r;
381 if (n == 1)
382 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
383 else
384 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
386 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
387 if (data->fan_min[n - 1] == 0)
388 data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40;
389 else
390 data->alarm_mask |= (n == 1) ? 0x20 : 0x40;
391 data->beep_mask &= data->alarm_mask;
392 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
394 mutex_unlock(&data->update_lock);
395 return count;
398 static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
400 unsigned long v = simple_strtoul(buf, NULL, 10);
401 u8 r;
403 switch (v) {
404 case 1: r = 0; break;
405 case 2: r = 1; break;
406 case 4: r = 2; break;
407 case 8: r = 3; break;
408 default:
409 dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
410 return -EINVAL;
413 mutex_lock(&data->update_lock);
414 data->fan_div[n - 1] = r;
416 if (n == 1)
417 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
418 else
419 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
421 mutex_unlock(&data->update_lock);
422 return count;
425 static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
427 u8 r = simple_strtoul(buf, NULL, 10)?1:0;
429 mutex_lock(&data->update_lock);
430 data->fan_off = r;
431 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
432 mutex_unlock(&data->update_lock);
433 return count;
436 #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
437 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
439 static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n)
441 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1]));
444 static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n)
446 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1]));
449 static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n)
451 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1]));
454 static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
456 long v = simple_strtol(buf, NULL, 10);
458 mutex_lock(&data->update_lock);
459 data->temp_max[n - 1] = TEMP_TO_REG(v);
460 gl520_write_value(client, reg, data->temp_max[n - 1]);
461 mutex_unlock(&data->update_lock);
462 return count;
465 static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
467 long v = simple_strtol(buf, NULL, 10);
469 mutex_lock(&data->update_lock);
470 data->temp_max_hyst[n - 1] = TEMP_TO_REG(v);
471 gl520_write_value(client, reg, data->temp_max_hyst[n - 1]);
472 mutex_unlock(&data->update_lock);
473 return count;
476 static ssize_t get_alarms(struct gl520_data *data, char *buf, int n)
478 return sprintf(buf, "%d\n", data->alarms);
481 static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n)
483 return sprintf(buf, "%d\n", data->beep_enable);
486 static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n)
488 return sprintf(buf, "%d\n", data->beep_mask);
491 static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
493 u8 r = simple_strtoul(buf, NULL, 10)?0:1;
495 mutex_lock(&data->update_lock);
496 data->beep_enable = !r;
497 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
498 mutex_unlock(&data->update_lock);
499 return count;
502 static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
504 u8 r = simple_strtoul(buf, NULL, 10);
506 mutex_lock(&data->update_lock);
507 r &= data->alarm_mask;
508 data->beep_mask = r;
509 gl520_write_value(client, reg, r);
510 mutex_unlock(&data->update_lock);
511 return count;
516 * Real code
519 static int gl520_attach_adapter(struct i2c_adapter *adapter)
521 if (!(adapter->class & I2C_CLASS_HWMON))
522 return 0;
523 return i2c_probe(adapter, &addr_data, gl520_detect);
526 static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
528 struct i2c_client *new_client;
529 struct gl520_data *data;
530 int err = 0;
532 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
533 I2C_FUNC_SMBUS_WORD_DATA))
534 goto exit;
536 /* OK. For now, we presume we have a valid client. We now create the
537 client structure, even though we cannot fill it completely yet.
538 But it allows us to access gl520_{read,write}_value. */
540 if (!(data = kzalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
541 err = -ENOMEM;
542 goto exit;
545 new_client = &data->client;
546 i2c_set_clientdata(new_client, data);
547 new_client->addr = address;
548 new_client->adapter = adapter;
549 new_client->driver = &gl520_driver;
550 new_client->flags = 0;
552 /* Determine the chip type. */
553 if (kind < 0) {
554 if ((gl520_read_value(new_client, GL520_REG_CHIP_ID) != 0x20) ||
555 ((gl520_read_value(new_client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
556 ((gl520_read_value(new_client, GL520_REG_CONF) & 0x80) != 0x00)) {
557 dev_dbg(&new_client->dev, "Unknown chip type, skipping\n");
558 goto exit_free;
562 /* Fill in the remaining client fields */
563 strlcpy(new_client->name, "gl520sm", I2C_NAME_SIZE);
564 data->valid = 0;
565 mutex_init(&data->update_lock);
567 /* Tell the I2C layer a new client has arrived */
568 if ((err = i2c_attach_client(new_client)))
569 goto exit_free;
571 /* Initialize the GL520SM chip */
572 gl520_init_client(new_client);
574 /* Register sysfs hooks */
575 data->class_dev = hwmon_device_register(&new_client->dev);
576 if (IS_ERR(data->class_dev)) {
577 err = PTR_ERR(data->class_dev);
578 goto exit_detach;
581 device_create_file_vid(new_client, 0);
583 device_create_file_in(new_client, 0);
584 device_create_file_in(new_client, 1);
585 device_create_file_in(new_client, 2);
586 device_create_file_in(new_client, 3);
587 if (!data->two_temps)
588 device_create_file_in(new_client, 4);
590 device_create_file_fan(new_client, 1);
591 device_create_file_fan(new_client, 2);
592 device_create_file_fan_off(new_client, 1);
594 device_create_file_temp(new_client, 1);
595 if (data->two_temps)
596 device_create_file_temp(new_client, 2);
598 device_create_file_alarms(new_client);
600 return 0;
602 exit_detach:
603 i2c_detach_client(new_client);
604 exit_free:
605 kfree(data);
606 exit:
607 return err;
611 /* Called when we have found a new GL520SM. */
612 static void gl520_init_client(struct i2c_client *client)
614 struct gl520_data *data = i2c_get_clientdata(client);
615 u8 oldconf, conf;
617 conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
619 data->alarm_mask = 0xff;
620 data->vrm = vid_which_vrm();
622 if (extra_sensor_type == 1)
623 conf &= ~0x10;
624 else if (extra_sensor_type == 2)
625 conf |= 0x10;
626 data->two_temps = !(conf & 0x10);
628 /* If IRQ# is disabled, we can safely force comparator mode */
629 if (!(conf & 0x20))
630 conf &= 0xf7;
632 /* Enable monitoring if needed */
633 conf |= 0x40;
635 if (conf != oldconf)
636 gl520_write_value(client, GL520_REG_CONF, conf);
638 gl520_update_device(&(client->dev));
640 if (data->fan_min[0] == 0)
641 data->alarm_mask &= ~0x20;
642 if (data->fan_min[1] == 0)
643 data->alarm_mask &= ~0x40;
645 data->beep_mask &= data->alarm_mask;
646 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
649 static int gl520_detach_client(struct i2c_client *client)
651 struct gl520_data *data = i2c_get_clientdata(client);
652 int err;
654 hwmon_device_unregister(data->class_dev);
656 if ((err = i2c_detach_client(client)))
657 return err;
659 kfree(data);
660 return 0;
664 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
665 GL520 uses a high-byte first convention */
666 static int gl520_read_value(struct i2c_client *client, u8 reg)
668 if ((reg >= 0x07) && (reg <= 0x0c))
669 return swab16(i2c_smbus_read_word_data(client, reg));
670 else
671 return i2c_smbus_read_byte_data(client, reg);
674 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
676 if ((reg >= 0x07) && (reg <= 0x0c))
677 return i2c_smbus_write_word_data(client, reg, swab16(value));
678 else
679 return i2c_smbus_write_byte_data(client, reg, value);
683 static struct gl520_data *gl520_update_device(struct device *dev)
685 struct i2c_client *client = to_i2c_client(dev);
686 struct gl520_data *data = i2c_get_clientdata(client);
687 int val;
689 mutex_lock(&data->update_lock);
691 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
693 dev_dbg(&client->dev, "Starting gl520sm update\n");
695 data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
696 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
697 data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
699 val = gl520_read_value(client, GL520_REG_IN0_LIMIT);
700 data->in_min[0] = val & 0xff;
701 data->in_max[0] = (val >> 8) & 0xff;
702 val = gl520_read_value(client, GL520_REG_IN1_LIMIT);
703 data->in_min[1] = val & 0xff;
704 data->in_max[1] = (val >> 8) & 0xff;
705 val = gl520_read_value(client, GL520_REG_IN2_LIMIT);
706 data->in_min[2] = val & 0xff;
707 data->in_max[2] = (val >> 8) & 0xff;
708 val = gl520_read_value(client, GL520_REG_IN3_LIMIT);
709 data->in_min[3] = val & 0xff;
710 data->in_max[3] = (val >> 8) & 0xff;
712 val = gl520_read_value(client, GL520_REG_FAN_INPUT);
713 data->fan_input[0] = (val >> 8) & 0xff;
714 data->fan_input[1] = val & 0xff;
716 val = gl520_read_value(client, GL520_REG_FAN_MIN);
717 data->fan_min[0] = (val >> 8) & 0xff;
718 data->fan_min[1] = val & 0xff;
720 data->temp_input[0] = gl520_read_value(client, GL520_REG_TEMP1_INPUT);
721 data->temp_max[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX);
722 data->temp_max_hyst[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX_HYST);
724 val = gl520_read_value(client, GL520_REG_FAN_DIV);
725 data->fan_div[0] = (val >> 6) & 0x03;
726 data->fan_div[1] = (val >> 4) & 0x03;
727 data->fan_off = (val >> 2) & 0x01;
729 data->alarms &= data->alarm_mask;
731 val = gl520_read_value(client, GL520_REG_CONF);
732 data->beep_enable = !((val >> 2) & 1);
734 data->in_input[0] = gl520_read_value(client, GL520_REG_IN0_INPUT);
735 data->in_input[1] = gl520_read_value(client, GL520_REG_IN1_INPUT);
736 data->in_input[2] = gl520_read_value(client, GL520_REG_IN2_INPUT);
737 data->in_input[3] = gl520_read_value(client, GL520_REG_IN3_INPUT);
739 /* Temp1 and Vin4 are the same input */
740 if (data->two_temps) {
741 data->temp_input[1] = gl520_read_value(client, GL520_REG_TEMP2_INPUT);
742 data->temp_max[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX);
743 data->temp_max_hyst[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX_HYST);
744 } else {
745 data->in_input[4] = gl520_read_value(client, GL520_REG_IN4_INPUT);
746 data->in_min[4] = gl520_read_value(client, GL520_REG_IN4_MIN);
747 data->in_max[4] = gl520_read_value(client, GL520_REG_IN4_MAX);
750 data->last_updated = jiffies;
751 data->valid = 1;
754 mutex_unlock(&data->update_lock);
756 return data;
760 static int __init sensors_gl520sm_init(void)
762 return i2c_add_driver(&gl520_driver);
765 static void __exit sensors_gl520sm_exit(void)
767 i2c_del_driver(&gl520_driver);
771 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
772 "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
773 "Maarten Deprez <maartendeprez@users.sourceforge.net>");
774 MODULE_DESCRIPTION("GL520SM driver");
775 MODULE_LICENSE("GPL");
777 module_init(sensors_gl520sm_init);
778 module_exit(sensors_gl520sm_exit);