blob: 14e810f3c2c09db43ab6687efbfaa8076c65fc5c [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>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020030#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040031#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* Type of the extra sensor */
35static unsigned short extra_sensor_type;
36module_param(extra_sensor_type, ushort, 0);
37MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
38
39/* Addresses to scan */
40static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020043I2C_CLIENT_INSMOD_1(gl520sm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* Many GL520 constants specified below
46One of the inputs can be configured as either temp or voltage.
47That's why _TEMP2 and _IN4 access the same register
48*/
49
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
55
56#define GL520_REG_VID_INPUT 0x02
57
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
62
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
67
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
72
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
77
78#define GL520_REG_IN4_INPUT 0x0e
79#define GL520_REG_IN4_MAX 0x17
80#define GL520_REG_IN4_MIN 0x18
81
82#define GL520_REG_TEMP1_INPUT 0x04
83#define GL520_REG_TEMP1_MAX 0x05
84#define GL520_REG_TEMP1_MAX_HYST 0x06
85
86#define GL520_REG_TEMP2_INPUT 0x0e
87#define GL520_REG_TEMP2_MAX 0x17
88#define GL520_REG_TEMP2_MAX_HYST 0x18
89
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
94
95#define GL520_REG_ALARMS 0x12
96#define GL520_REG_BEEP_MASK 0x10
97#define GL520_REG_BEEP_ENABLE GL520_REG_CONF
98
99/*
100 * Function declarations
101 */
102
103static int gl520_attach_adapter(struct i2c_adapter *adapter);
104static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
105static void gl520_init_client(struct i2c_client *client);
106static int gl520_detach_client(struct i2c_client *client);
107static int gl520_read_value(struct i2c_client *client, u8 reg);
108static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
109static struct gl520_data *gl520_update_device(struct device *dev);
110
111/* Driver data */
112static struct i2c_driver gl520_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100113 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100114 .name = "gl520sm",
115 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .id = I2C_DRIVERID_GL520,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .attach_adapter = gl520_attach_adapter,
118 .detach_client = gl520_detach_client,
119};
120
121/* Client data */
122struct gl520_data {
123 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400124 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100125 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 char valid; /* zero until the following fields are valid */
127 unsigned long last_updated; /* in jiffies */
128
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;
146};
147
148/*
149 * Sysfs stuff
150 */
151
152#define sysfs_r(type, n, item, reg) \
153static ssize_t get_##type##item (struct gl520_data *, char *, int); \
Yani Ioannou30f74292005-05-17 06:41:35 -0400154static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \
155static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{ \
157 struct gl520_data *data = gl520_update_device(dev); \
158 return get_##type##item(data, buf, (n)); \
159}
160
161#define sysfs_w(type, n, item, reg) \
162static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \
Yani Ioannou30f74292005-05-17 06:41:35 -0400163static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \
164static 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 -0700165{ \
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); \
169}
170
171#define sysfs_rw_n(type, n, item, reg) \
172sysfs_r(type, n, item, reg) \
173sysfs_w(type, n, item, reg) \
174static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item);
175
176#define sysfs_ro_n(type, n, item, reg) \
177sysfs_r(type, n, item, reg) \
178static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL);
179
180#define sysfs_rw(type, item, reg) \
181sysfs_r(type, 0, item, reg) \
182sysfs_w(type, 0, item, reg) \
183static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item);
184
185#define sysfs_ro(type, item, reg) \
186sysfs_r(type, 0, item, reg) \
187static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL);
188
189
190#define sysfs_vid(n) \
191sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT)
192
193#define device_create_file_vid(client, n) \
194device_create_file(&client->dev, &dev_attr_cpu##n##_vid)
195
196#define sysfs_in(n) \
197sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \
198sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \
199sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX) \
200
201#define device_create_file_in(client, n) \
202({device_create_file(&client->dev, &dev_attr_in##n##_input); \
203device_create_file(&client->dev, &dev_attr_in##n##_min); \
204device_create_file(&client->dev, &dev_attr_in##n##_max);})
205
206#define sysfs_fan(n) \
207sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \
208sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \
209sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV)
210
211#define device_create_file_fan(client, n) \
212({device_create_file(&client->dev, &dev_attr_fan##n##_input); \
213device_create_file(&client->dev, &dev_attr_fan##n##_min); \
214device_create_file(&client->dev, &dev_attr_fan##n##_div);})
215
216#define sysfs_fan_off(n) \
217sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) \
218
219#define device_create_file_fan_off(client, n) \
220device_create_file(&client->dev, &dev_attr_fan##n##_off)
221
222#define sysfs_temp(n) \
223sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \
224sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \
225sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST)
226
227#define device_create_file_temp(client, n) \
228({device_create_file(&client->dev, &dev_attr_temp##n##_input); \
229device_create_file(&client->dev, &dev_attr_temp##n##_max); \
230device_create_file(&client->dev, &dev_attr_temp##n##_max_hyst);})
231
232#define sysfs_alarms() \
233sysfs_ro(alarms, , GL520_REG_ALARMS) \
234sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \
235sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK)
236
237#define device_create_file_alarms(client) \
238({device_create_file(&client->dev, &dev_attr_alarms); \
239device_create_file(&client->dev, &dev_attr_beep_enable); \
240device_create_file(&client->dev, &dev_attr_beep_mask);})
241
242
243sysfs_vid(0)
244
245sysfs_in(0)
246sysfs_in(1)
247sysfs_in(2)
248sysfs_in(3)
249sysfs_in(4)
250
251sysfs_fan(1)
252sysfs_fan(2)
253sysfs_fan_off(1)
254
255sysfs_temp(1)
256sysfs_temp(2)
257
258sysfs_alarms()
259
260
261static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n)
262{
263 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
264}
265
266#define VDD_FROM_REG(val) (((val)*95+2)/4)
267#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
268
269#define IN_FROM_REG(val) ((val)*19)
270#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
271
272static ssize_t get_in_input(struct gl520_data *data, char *buf, int n)
273{
274 u8 r = data->in_input[n];
275
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));
280}
281
282static ssize_t get_in_min(struct gl520_data *data, char *buf, int n)
283{
284 u8 r = data->in_min[n];
285
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));
290}
291
292static ssize_t get_in_max(struct gl520_data *data, char *buf, int n)
293{
294 u8 r = data->in_max[n];
295
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));
300}
301
302static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
303{
304 long v = simple_strtol(buf, NULL, 10);
305 u8 r;
306
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100307 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (n == 0)
310 r = VDD_TO_REG(v);
311 else
312 r = IN_TO_REG(v);
313
314 data->in_min[n] = r;
315
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);
320
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100321 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return count;
323}
324
325static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
326{
327 long v = simple_strtol(buf, NULL, 10);
328 u8 r;
329
330 if (n == 0)
331 r = VDD_TO_REG(v);
332 else
333 r = IN_TO_REG(v);
334
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100335 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 data->in_max[n] = r;
338
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);
343
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100344 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return count;
346}
347
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));
351
352static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n)
353{
354 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1]));
355}
356
357static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n)
358{
359 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1]));
360}
361
362static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n)
363{
364 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1]));
365}
366
367static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n)
368{
369 return sprintf(buf, "%d\n", data->fan_off);
370}
371
372static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
373{
374 unsigned long v = simple_strtoul(buf, NULL, 10);
375 u8 r;
376
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100377 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 r = FAN_TO_REG(v, data->fan_div[n - 1]);
379 data->fan_min[n - 1] = r;
380
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);
385
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);
393
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100394 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return count;
396}
397
398static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
399{
400 unsigned long v = simple_strtoul(buf, NULL, 10);
401 u8 r;
402
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;
411 }
412
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100413 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 data->fan_div[n - 1] = r;
415
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));
420
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100421 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return count;
423}
424
425static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
426{
427 u8 r = simple_strtoul(buf, NULL, 10)?1:0;
428
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100429 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 data->fan_off = r;
431 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100432 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return count;
434}
435
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))
438
439static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n)
440{
441 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1]));
442}
443
444static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n)
445{
446 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1]));
447}
448
449static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n)
450{
451 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1]));
452}
453
454static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
455{
456 long v = simple_strtol(buf, NULL, 10);
457
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100458 mutex_lock(&data->update_lock);
Alexey Dobriyan53b35312006-03-24 03:16:13 -0800459 data->temp_max[n - 1] = TEMP_TO_REG(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 gl520_write_value(client, reg, data->temp_max[n - 1]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100461 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return count;
463}
464
465static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
466{
467 long v = simple_strtol(buf, NULL, 10);
468
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100469 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 data->temp_max_hyst[n - 1] = TEMP_TO_REG(v);
471 gl520_write_value(client, reg, data->temp_max_hyst[n - 1]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100472 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return count;
474}
475
476static ssize_t get_alarms(struct gl520_data *data, char *buf, int n)
477{
478 return sprintf(buf, "%d\n", data->alarms);
479}
480
481static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n)
482{
483 return sprintf(buf, "%d\n", data->beep_enable);
484}
485
486static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n)
487{
488 return sprintf(buf, "%d\n", data->beep_mask);
489}
490
491static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
492{
493 u8 r = simple_strtoul(buf, NULL, 10)?0:1;
494
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100495 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 data->beep_enable = !r;
497 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100498 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return count;
500}
501
502static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
503{
504 u8 r = simple_strtoul(buf, NULL, 10);
505
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100506 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 r &= data->alarm_mask;
508 data->beep_mask = r;
509 gl520_write_value(client, reg, r);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return count;
512}
513
514
515/*
516 * Real code
517 */
518
519static int gl520_attach_adapter(struct i2c_adapter *adapter)
520{
521 if (!(adapter->class & I2C_CLASS_HWMON))
522 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200523 return i2c_probe(adapter, &addr_data, gl520_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
527{
528 struct i2c_client *new_client;
529 struct gl520_data *data;
530 int err = 0;
531
532 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
533 I2C_FUNC_SMBUS_WORD_DATA))
534 goto exit;
535
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. */
539
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200540 if (!(data = kzalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 err = -ENOMEM;
542 goto exit;
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
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;
551
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;
559 }
560 }
561
562 /* Fill in the remaining client fields */
563 strlcpy(new_client->name, "gl520sm", I2C_NAME_SIZE);
564 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100565 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* Tell the I2C layer a new client has arrived */
568 if ((err = i2c_attach_client(new_client)))
569 goto exit_free;
570
571 /* Initialize the GL520SM chip */
572 gl520_init_client(new_client);
573
574 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400575 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;
579 }
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 device_create_file_vid(new_client, 0);
582
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);
589
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);
593
594 device_create_file_temp(new_client, 1);
595 if (data->two_temps)
596 device_create_file_temp(new_client, 2);
597
598 device_create_file_alarms(new_client);
599
600 return 0;
601
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400602exit_detach:
603 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604exit_free:
605 kfree(data);
606exit:
607 return err;
608}
609
610
611/* Called when we have found a new GL520SM. */
612static void gl520_init_client(struct i2c_client *client)
613{
614 struct gl520_data *data = i2c_get_clientdata(client);
615 u8 oldconf, conf;
616
617 conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
618
619 data->alarm_mask = 0xff;
Jean Delvare303760b2005-07-31 21:52:01 +0200620 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
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);
627
628 /* If IRQ# is disabled, we can safely force comparator mode */
629 if (!(conf & 0x20))
630 conf &= 0xf7;
631
632 /* Enable monitoring if needed */
633 conf |= 0x40;
634
635 if (conf != oldconf)
636 gl520_write_value(client, GL520_REG_CONF, conf);
637
638 gl520_update_device(&(client->dev));
639
640 if (data->fan_min[0] == 0)
641 data->alarm_mask &= ~0x20;
642 if (data->fan_min[1] == 0)
643 data->alarm_mask &= ~0x40;
644
645 data->beep_mask &= data->alarm_mask;
646 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
647}
648
649static int gl520_detach_client(struct i2c_client *client)
650{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400651 struct gl520_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int err;
653
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400654 hwmon_device_unregister(data->class_dev);
655
Jean Delvare7bef5592005-07-27 22:14:49 +0200656 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400659 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
661}
662
663
664/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
665 GL520 uses a high-byte first convention */
666static int gl520_read_value(struct i2c_client *client, u8 reg)
667{
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);
672}
673
674static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
675{
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);
680}
681
682
683static struct gl520_data *gl520_update_device(struct device *dev)
684{
685 struct i2c_client *client = to_i2c_client(dev);
686 struct gl520_data *data = i2c_get_clientdata(client);
687 int val;
688
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100689 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jean Delvare0cacdf22005-07-29 12:15:12 -0700691 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 dev_dbg(&client->dev, "Starting gl520sm update\n");
694
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;
698
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;
711
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;
715
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;
719
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);
723
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;
728
729 data->alarms &= data->alarm_mask;
730
731 val = gl520_read_value(client, GL520_REG_CONF);
732 data->beep_enable = !((val >> 2) & 1);
733
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);
738
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);
748 }
749
750 data->last_updated = jiffies;
751 data->valid = 1;
752 }
753
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100754 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 return data;
757}
758
759
760static int __init sensors_gl520sm_init(void)
761{
762 return i2c_add_driver(&gl520_driver);
763}
764
765static void __exit sensors_gl520sm_exit(void)
766{
767 i2c_del_driver(&gl520_driver);
768}
769
770
771MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
772 "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
773 "Maarten Deprez <maartendeprez@users.sourceforge.net>");
774MODULE_DESCRIPTION("GL520SM driver");
775MODULE_LICENSE("GPL");
776
777module_init(sensors_gl520sm_init);
778module_exit(sensors_gl520sm_exit);