blob: 80ae8d30c2afc4d556f054a0f6e7fbcf1a0d627e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
7
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.
12
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.
17
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.
21
22*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
Jean Delvare0cacdf22005-07-29 12:15:12 -070027#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/i2c.h>
29#include <linux/i2c-sensor.h>
30#include <linux/i2c-vid.h>
31
32/* Type of the extra sensor */
33static unsigned short extra_sensor_type;
34module_param(extra_sensor_type, ushort, 0);
35MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
36
37/* Addresses to scan */
38static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
39static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
40
41/* Insmod parameters */
42SENSORS_INSMOD_1(gl520sm);
43
44/* Many GL520 constants specified below
45One of the inputs can be configured as either temp or voltage.
46That's why _TEMP2 and _IN4 access the same register
47*/
48
49/* The GL520 registers */
50#define GL520_REG_CHIP_ID 0x00
51#define GL520_REG_REVISION 0x01
52#define GL520_REG_CONF 0x03
53#define GL520_REG_MASK 0x11
54
55#define GL520_REG_VID_INPUT 0x02
56
57#define GL520_REG_IN0_INPUT 0x15
58#define GL520_REG_IN0_LIMIT 0x0c
59#define GL520_REG_IN0_MIN GL520_REG_IN0_LIMIT
60#define GL520_REG_IN0_MAX GL520_REG_IN0_LIMIT
61
62#define GL520_REG_IN1_INPUT 0x14
63#define GL520_REG_IN1_LIMIT 0x09
64#define GL520_REG_IN1_MIN GL520_REG_IN1_LIMIT
65#define GL520_REG_IN1_MAX GL520_REG_IN1_LIMIT
66
67#define GL520_REG_IN2_INPUT 0x13
68#define GL520_REG_IN2_LIMIT 0x0a
69#define GL520_REG_IN2_MIN GL520_REG_IN2_LIMIT
70#define GL520_REG_IN2_MAX GL520_REG_IN2_LIMIT
71
72#define GL520_REG_IN3_INPUT 0x0d
73#define GL520_REG_IN3_LIMIT 0x0b
74#define GL520_REG_IN3_MIN GL520_REG_IN3_LIMIT
75#define GL520_REG_IN3_MAX GL520_REG_IN3_LIMIT
76
77#define GL520_REG_IN4_INPUT 0x0e
78#define GL520_REG_IN4_MAX 0x17
79#define GL520_REG_IN4_MIN 0x18
80
81#define GL520_REG_TEMP1_INPUT 0x04
82#define GL520_REG_TEMP1_MAX 0x05
83#define GL520_REG_TEMP1_MAX_HYST 0x06
84
85#define GL520_REG_TEMP2_INPUT 0x0e
86#define GL520_REG_TEMP2_MAX 0x17
87#define GL520_REG_TEMP2_MAX_HYST 0x18
88
89#define GL520_REG_FAN_INPUT 0x07
90#define GL520_REG_FAN_MIN 0x08
91#define GL520_REG_FAN_DIV 0x0f
92#define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
93
94#define GL520_REG_ALARMS 0x12
95#define GL520_REG_BEEP_MASK 0x10
96#define GL520_REG_BEEP_ENABLE GL520_REG_CONF
97
98/*
99 * Function declarations
100 */
101
102static int gl520_attach_adapter(struct i2c_adapter *adapter);
103static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
104static void gl520_init_client(struct i2c_client *client);
105static int gl520_detach_client(struct i2c_client *client);
106static int gl520_read_value(struct i2c_client *client, u8 reg);
107static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
108static struct gl520_data *gl520_update_device(struct device *dev);
109
110/* Driver data */
111static struct i2c_driver gl520_driver = {
112 .owner = THIS_MODULE,
113 .name = "gl520sm",
114 .id = I2C_DRIVERID_GL520,
115 .flags = I2C_DF_NOTIFY,
116 .attach_adapter = gl520_attach_adapter,
117 .detach_client = gl520_detach_client,
118};
119
120/* Client data */
121struct gl520_data {
122 struct i2c_client client;
123 struct semaphore update_lock;
124 char valid; /* zero until the following fields are valid */
125 unsigned long last_updated; /* in jiffies */
126
127 u8 vid;
128 u8 vrm;
129 u8 in_input[5]; /* [0] = VVD */
130 u8 in_min[5]; /* [0] = VDD */
131 u8 in_max[5]; /* [0] = VDD */
132 u8 fan_input[2];
133 u8 fan_min[2];
134 u8 fan_div[2];
135 u8 fan_off;
136 u8 temp_input[2];
137 u8 temp_max[2];
138 u8 temp_max_hyst[2];
139 u8 alarms;
140 u8 beep_enable;
141 u8 beep_mask;
142 u8 alarm_mask;
143 u8 two_temps;
144};
145
146/*
147 * Sysfs stuff
148 */
149
150#define sysfs_r(type, n, item, reg) \
151static ssize_t get_##type##item (struct gl520_data *, char *, int); \
Yani Ioannou30f74292005-05-17 06:41:35 -0400152static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \
153static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{ \
155 struct gl520_data *data = gl520_update_device(dev); \
156 return get_##type##item(data, buf, (n)); \
157}
158
159#define sysfs_w(type, n, item, reg) \
160static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \
Yani Ioannou30f74292005-05-17 06:41:35 -0400161static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \
162static ssize_t set_##type##n##item (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{ \
164 struct i2c_client *client = to_i2c_client(dev); \
165 struct gl520_data *data = i2c_get_clientdata(client); \
166 return set_##type##item(client, data, buf, count, (n), reg); \
167}
168
169#define sysfs_rw_n(type, n, item, reg) \
170sysfs_r(type, n, item, reg) \
171sysfs_w(type, n, item, reg) \
172static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item);
173
174#define sysfs_ro_n(type, n, item, reg) \
175sysfs_r(type, n, item, reg) \
176static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL);
177
178#define sysfs_rw(type, item, reg) \
179sysfs_r(type, 0, item, reg) \
180sysfs_w(type, 0, item, reg) \
181static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item);
182
183#define sysfs_ro(type, item, reg) \
184sysfs_r(type, 0, item, reg) \
185static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL);
186
187
188#define sysfs_vid(n) \
189sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT)
190
191#define device_create_file_vid(client, n) \
192device_create_file(&client->dev, &dev_attr_cpu##n##_vid)
193
194#define sysfs_in(n) \
195sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \
196sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \
197sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX) \
198
199#define device_create_file_in(client, n) \
200({device_create_file(&client->dev, &dev_attr_in##n##_input); \
201device_create_file(&client->dev, &dev_attr_in##n##_min); \
202device_create_file(&client->dev, &dev_attr_in##n##_max);})
203
204#define sysfs_fan(n) \
205sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \
206sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \
207sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV)
208
209#define device_create_file_fan(client, n) \
210({device_create_file(&client->dev, &dev_attr_fan##n##_input); \
211device_create_file(&client->dev, &dev_attr_fan##n##_min); \
212device_create_file(&client->dev, &dev_attr_fan##n##_div);})
213
214#define sysfs_fan_off(n) \
215sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) \
216
217#define device_create_file_fan_off(client, n) \
218device_create_file(&client->dev, &dev_attr_fan##n##_off)
219
220#define sysfs_temp(n) \
221sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \
222sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \
223sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST)
224
225#define device_create_file_temp(client, n) \
226({device_create_file(&client->dev, &dev_attr_temp##n##_input); \
227device_create_file(&client->dev, &dev_attr_temp##n##_max); \
228device_create_file(&client->dev, &dev_attr_temp##n##_max_hyst);})
229
230#define sysfs_alarms() \
231sysfs_ro(alarms, , GL520_REG_ALARMS) \
232sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \
233sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK)
234
235#define device_create_file_alarms(client) \
236({device_create_file(&client->dev, &dev_attr_alarms); \
237device_create_file(&client->dev, &dev_attr_beep_enable); \
238device_create_file(&client->dev, &dev_attr_beep_mask);})
239
240
241sysfs_vid(0)
242
243sysfs_in(0)
244sysfs_in(1)
245sysfs_in(2)
246sysfs_in(3)
247sysfs_in(4)
248
249sysfs_fan(1)
250sysfs_fan(2)
251sysfs_fan_off(1)
252
253sysfs_temp(1)
254sysfs_temp(2)
255
256sysfs_alarms()
257
258
259static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n)
260{
261 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
262}
263
264#define VDD_FROM_REG(val) (((val)*95+2)/4)
265#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
266
267#define IN_FROM_REG(val) ((val)*19)
268#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
269
270static ssize_t get_in_input(struct gl520_data *data, char *buf, int n)
271{
272 u8 r = data->in_input[n];
273
274 if (n == 0)
275 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
276 else
277 return sprintf(buf, "%d\n", IN_FROM_REG(r));
278}
279
280static ssize_t get_in_min(struct gl520_data *data, char *buf, int n)
281{
282 u8 r = data->in_min[n];
283
284 if (n == 0)
285 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
286 else
287 return sprintf(buf, "%d\n", IN_FROM_REG(r));
288}
289
290static ssize_t get_in_max(struct gl520_data *data, char *buf, int n)
291{
292 u8 r = data->in_max[n];
293
294 if (n == 0)
295 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
296 else
297 return sprintf(buf, "%d\n", IN_FROM_REG(r));
298}
299
300static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
301{
302 long v = simple_strtol(buf, NULL, 10);
303 u8 r;
304
305 down(&data->update_lock);
306
307 if (n == 0)
308 r = VDD_TO_REG(v);
309 else
310 r = IN_TO_REG(v);
311
312 data->in_min[n] = r;
313
314 if (n < 4)
315 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
316 else
317 gl520_write_value(client, reg, r);
318
319 up(&data->update_lock);
320 return count;
321}
322
323static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
324{
325 long v = simple_strtol(buf, NULL, 10);
326 u8 r;
327
328 if (n == 0)
329 r = VDD_TO_REG(v);
330 else
331 r = IN_TO_REG(v);
332
333 down(&data->update_lock);
334
335 data->in_max[n] = r;
336
337 if (n < 4)
338 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
339 else
340 gl520_write_value(client, reg, r);
341
342 up(&data->update_lock);
343 return count;
344}
345
346#define DIV_FROM_REG(val) (1 << (val))
347#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
348#define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
349
350static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n)
351{
352 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1]));
353}
354
355static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n)
356{
357 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1]));
358}
359
360static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n)
361{
362 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1]));
363}
364
365static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n)
366{
367 return sprintf(buf, "%d\n", data->fan_off);
368}
369
370static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
371{
372 unsigned long v = simple_strtoul(buf, NULL, 10);
373 u8 r;
374
375 down(&data->update_lock);
376 r = FAN_TO_REG(v, data->fan_div[n - 1]);
377 data->fan_min[n - 1] = r;
378
379 if (n == 1)
380 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
381 else
382 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
383
384 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
385 if (data->fan_min[n - 1] == 0)
386 data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40;
387 else
388 data->alarm_mask |= (n == 1) ? 0x20 : 0x40;
389 data->beep_mask &= data->alarm_mask;
390 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
391
392 up(&data->update_lock);
393 return count;
394}
395
396static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
397{
398 unsigned long v = simple_strtoul(buf, NULL, 10);
399 u8 r;
400
401 switch (v) {
402 case 1: r = 0; break;
403 case 2: r = 1; break;
404 case 4: r = 2; break;
405 case 8: r = 3; break;
406 default:
407 dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
408 return -EINVAL;
409 }
410
411 down(&data->update_lock);
412 data->fan_div[n - 1] = r;
413
414 if (n == 1)
415 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
416 else
417 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
418
419 up(&data->update_lock);
420 return count;
421}
422
423static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
424{
425 u8 r = simple_strtoul(buf, NULL, 10)?1:0;
426
427 down(&data->update_lock);
428 data->fan_off = r;
429 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
430 up(&data->update_lock);
431 return count;
432}
433
434#define TEMP_FROM_REG(val) (((val) - 130) * 1000)
435#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
436
437static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n)
438{
439 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1]));
440}
441
442static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n)
443{
444 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1]));
445}
446
447static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n)
448{
449 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1]));
450}
451
452static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
453{
454 long v = simple_strtol(buf, NULL, 10);
455
456 down(&data->update_lock);
457 data->temp_max[n - 1] = TEMP_TO_REG(v);;
458 gl520_write_value(client, reg, data->temp_max[n - 1]);
459 up(&data->update_lock);
460 return count;
461}
462
463static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
464{
465 long v = simple_strtol(buf, NULL, 10);
466
467 down(&data->update_lock);
468 data->temp_max_hyst[n - 1] = TEMP_TO_REG(v);
469 gl520_write_value(client, reg, data->temp_max_hyst[n - 1]);
470 up(&data->update_lock);
471 return count;
472}
473
474static ssize_t get_alarms(struct gl520_data *data, char *buf, int n)
475{
476 return sprintf(buf, "%d\n", data->alarms);
477}
478
479static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n)
480{
481 return sprintf(buf, "%d\n", data->beep_enable);
482}
483
484static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n)
485{
486 return sprintf(buf, "%d\n", data->beep_mask);
487}
488
489static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
490{
491 u8 r = simple_strtoul(buf, NULL, 10)?0:1;
492
493 down(&data->update_lock);
494 data->beep_enable = !r;
495 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
496 up(&data->update_lock);
497 return count;
498}
499
500static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
501{
502 u8 r = simple_strtoul(buf, NULL, 10);
503
504 down(&data->update_lock);
505 r &= data->alarm_mask;
506 data->beep_mask = r;
507 gl520_write_value(client, reg, r);
508 up(&data->update_lock);
509 return count;
510}
511
512
513/*
514 * Real code
515 */
516
517static int gl520_attach_adapter(struct i2c_adapter *adapter)
518{
519 if (!(adapter->class & I2C_CLASS_HWMON))
520 return 0;
521 return i2c_detect(adapter, &addr_data, gl520_detect);
522}
523
524static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
525{
526 struct i2c_client *new_client;
527 struct gl520_data *data;
528 int err = 0;
529
530 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
531 I2C_FUNC_SMBUS_WORD_DATA))
532 goto exit;
533
534 /* OK. For now, we presume we have a valid client. We now create the
535 client structure, even though we cannot fill it completely yet.
536 But it allows us to access gl520_{read,write}_value. */
537
538 if (!(data = kmalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
539 err = -ENOMEM;
540 goto exit;
541 }
542 memset(data, 0, sizeof(struct gl520_data));
543
544 new_client = &data->client;
545 i2c_set_clientdata(new_client, data);
546 new_client->addr = address;
547 new_client->adapter = adapter;
548 new_client->driver = &gl520_driver;
549 new_client->flags = 0;
550
551 /* Determine the chip type. */
552 if (kind < 0) {
553 if ((gl520_read_value(new_client, GL520_REG_CHIP_ID) != 0x20) ||
554 ((gl520_read_value(new_client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
555 ((gl520_read_value(new_client, GL520_REG_CONF) & 0x80) != 0x00)) {
556 dev_dbg(&new_client->dev, "Unknown chip type, skipping\n");
557 goto exit_free;
558 }
559 }
560
561 /* Fill in the remaining client fields */
562 strlcpy(new_client->name, "gl520sm", I2C_NAME_SIZE);
563 data->valid = 0;
564 init_MUTEX(&data->update_lock);
565
566 /* Tell the I2C layer a new client has arrived */
567 if ((err = i2c_attach_client(new_client)))
568 goto exit_free;
569
570 /* Initialize the GL520SM chip */
571 gl520_init_client(new_client);
572
573 /* Register sysfs hooks */
574 device_create_file_vid(new_client, 0);
575
576 device_create_file_in(new_client, 0);
577 device_create_file_in(new_client, 1);
578 device_create_file_in(new_client, 2);
579 device_create_file_in(new_client, 3);
580 if (!data->two_temps)
581 device_create_file_in(new_client, 4);
582
583 device_create_file_fan(new_client, 1);
584 device_create_file_fan(new_client, 2);
585 device_create_file_fan_off(new_client, 1);
586
587 device_create_file_temp(new_client, 1);
588 if (data->two_temps)
589 device_create_file_temp(new_client, 2);
590
591 device_create_file_alarms(new_client);
592
593 return 0;
594
595exit_free:
596 kfree(data);
597exit:
598 return err;
599}
600
601
602/* Called when we have found a new GL520SM. */
603static void gl520_init_client(struct i2c_client *client)
604{
605 struct gl520_data *data = i2c_get_clientdata(client);
606 u8 oldconf, conf;
607
608 conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
609
610 data->alarm_mask = 0xff;
611 data->vrm = i2c_which_vrm();
612
613 if (extra_sensor_type == 1)
614 conf &= ~0x10;
615 else if (extra_sensor_type == 2)
616 conf |= 0x10;
617 data->two_temps = !(conf & 0x10);
618
619 /* If IRQ# is disabled, we can safely force comparator mode */
620 if (!(conf & 0x20))
621 conf &= 0xf7;
622
623 /* Enable monitoring if needed */
624 conf |= 0x40;
625
626 if (conf != oldconf)
627 gl520_write_value(client, GL520_REG_CONF, conf);
628
629 gl520_update_device(&(client->dev));
630
631 if (data->fan_min[0] == 0)
632 data->alarm_mask &= ~0x20;
633 if (data->fan_min[1] == 0)
634 data->alarm_mask &= ~0x40;
635
636 data->beep_mask &= data->alarm_mask;
637 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
638}
639
640static int gl520_detach_client(struct i2c_client *client)
641{
642 int err;
643
644 if ((err = i2c_detach_client(client))) {
645 dev_err(&client->dev, "Client deregistration failed, "
646 "client not detached.\n");
647 return err;
648 }
649
650 kfree(i2c_get_clientdata(client));
651 return 0;
652}
653
654
655/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
656 GL520 uses a high-byte first convention */
657static int gl520_read_value(struct i2c_client *client, u8 reg)
658{
659 if ((reg >= 0x07) && (reg <= 0x0c))
660 return swab16(i2c_smbus_read_word_data(client, reg));
661 else
662 return i2c_smbus_read_byte_data(client, reg);
663}
664
665static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
666{
667 if ((reg >= 0x07) && (reg <= 0x0c))
668 return i2c_smbus_write_word_data(client, reg, swab16(value));
669 else
670 return i2c_smbus_write_byte_data(client, reg, value);
671}
672
673
674static struct gl520_data *gl520_update_device(struct device *dev)
675{
676 struct i2c_client *client = to_i2c_client(dev);
677 struct gl520_data *data = i2c_get_clientdata(client);
678 int val;
679
680 down(&data->update_lock);
681
Jean Delvare0cacdf22005-07-29 12:15:12 -0700682 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 dev_dbg(&client->dev, "Starting gl520sm update\n");
685
686 data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
687 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
688 data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
689
690 val = gl520_read_value(client, GL520_REG_IN0_LIMIT);
691 data->in_min[0] = val & 0xff;
692 data->in_max[0] = (val >> 8) & 0xff;
693 val = gl520_read_value(client, GL520_REG_IN1_LIMIT);
694 data->in_min[1] = val & 0xff;
695 data->in_max[1] = (val >> 8) & 0xff;
696 val = gl520_read_value(client, GL520_REG_IN2_LIMIT);
697 data->in_min[2] = val & 0xff;
698 data->in_max[2] = (val >> 8) & 0xff;
699 val = gl520_read_value(client, GL520_REG_IN3_LIMIT);
700 data->in_min[3] = val & 0xff;
701 data->in_max[3] = (val >> 8) & 0xff;
702
703 val = gl520_read_value(client, GL520_REG_FAN_INPUT);
704 data->fan_input[0] = (val >> 8) & 0xff;
705 data->fan_input[1] = val & 0xff;
706
707 val = gl520_read_value(client, GL520_REG_FAN_MIN);
708 data->fan_min[0] = (val >> 8) & 0xff;
709 data->fan_min[1] = val & 0xff;
710
711 data->temp_input[0] = gl520_read_value(client, GL520_REG_TEMP1_INPUT);
712 data->temp_max[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX);
713 data->temp_max_hyst[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX_HYST);
714
715 val = gl520_read_value(client, GL520_REG_FAN_DIV);
716 data->fan_div[0] = (val >> 6) & 0x03;
717 data->fan_div[1] = (val >> 4) & 0x03;
718 data->fan_off = (val >> 2) & 0x01;
719
720 data->alarms &= data->alarm_mask;
721
722 val = gl520_read_value(client, GL520_REG_CONF);
723 data->beep_enable = !((val >> 2) & 1);
724
725 data->in_input[0] = gl520_read_value(client, GL520_REG_IN0_INPUT);
726 data->in_input[1] = gl520_read_value(client, GL520_REG_IN1_INPUT);
727 data->in_input[2] = gl520_read_value(client, GL520_REG_IN2_INPUT);
728 data->in_input[3] = gl520_read_value(client, GL520_REG_IN3_INPUT);
729
730 /* Temp1 and Vin4 are the same input */
731 if (data->two_temps) {
732 data->temp_input[1] = gl520_read_value(client, GL520_REG_TEMP2_INPUT);
733 data->temp_max[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX);
734 data->temp_max_hyst[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX_HYST);
735 } else {
736 data->in_input[4] = gl520_read_value(client, GL520_REG_IN4_INPUT);
737 data->in_min[4] = gl520_read_value(client, GL520_REG_IN4_MIN);
738 data->in_max[4] = gl520_read_value(client, GL520_REG_IN4_MAX);
739 }
740
741 data->last_updated = jiffies;
742 data->valid = 1;
743 }
744
745 up(&data->update_lock);
746
747 return data;
748}
749
750
751static int __init sensors_gl520sm_init(void)
752{
753 return i2c_add_driver(&gl520_driver);
754}
755
756static void __exit sensors_gl520sm_exit(void)
757{
758 i2c_del_driver(&gl520_driver);
759}
760
761
762MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
763 "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
764 "Maarten Deprez <maartendeprez@users.sourceforge.net>");
765MODULE_DESCRIPTION("GL520SM driver");
766MODULE_LICENSE("GPL");
767
768module_init(sensors_gl520sm_init);
769module_exit(sensors_gl520sm_exit);