Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / gl520sm.c
blob19b54d1edef1505a5242c3cf019c9595e2008961
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-sysfs.h>
31 #include <linux/hwmon-vid.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 #include <linux/sysfs.h>
36 /* Type of the extra sensor */
37 static unsigned short extra_sensor_type;
38 module_param(extra_sensor_type, ushort, 0);
39 MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
41 /* Addresses to scan */
42 <<<<<<< HEAD:drivers/hwmon/gl520sm.c
43 static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
44 =======
45 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
46 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/gl520sm.c
48 /* Insmod parameters */
49 I2C_CLIENT_INSMOD_1(gl520sm);
51 /* Many GL520 constants specified below
52 One of the inputs can be configured as either temp or voltage.
53 That's why _TEMP2 and _IN4 access the same register
56 /* The GL520 registers */
57 #define GL520_REG_CHIP_ID 0x00
58 #define GL520_REG_REVISION 0x01
59 #define GL520_REG_CONF 0x03
60 #define GL520_REG_MASK 0x11
62 #define GL520_REG_VID_INPUT 0x02
64 static const u8 GL520_REG_IN_INPUT[] = { 0x15, 0x14, 0x13, 0x0d, 0x0e };
65 static const u8 GL520_REG_IN_LIMIT[] = { 0x0c, 0x09, 0x0a, 0x0b };
66 static const u8 GL520_REG_IN_MIN[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x18 };
67 static const u8 GL520_REG_IN_MAX[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x17 };
69 static const u8 GL520_REG_TEMP_INPUT[] = { 0x04, 0x0e };
70 static const u8 GL520_REG_TEMP_MAX[] = { 0x05, 0x17 };
71 static const u8 GL520_REG_TEMP_MAX_HYST[] = { 0x06, 0x18 };
73 #define GL520_REG_FAN_INPUT 0x07
74 #define GL520_REG_FAN_MIN 0x08
75 #define GL520_REG_FAN_DIV 0x0f
76 #define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
78 #define GL520_REG_ALARMS 0x12
79 #define GL520_REG_BEEP_MASK 0x10
80 #define GL520_REG_BEEP_ENABLE GL520_REG_CONF
83 * Function declarations
86 static int gl520_attach_adapter(struct i2c_adapter *adapter);
87 static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
88 static void gl520_init_client(struct i2c_client *client);
89 static int gl520_detach_client(struct i2c_client *client);
90 static int gl520_read_value(struct i2c_client *client, u8 reg);
91 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
92 static struct gl520_data *gl520_update_device(struct device *dev);
94 /* Driver data */
95 static struct i2c_driver gl520_driver = {
96 .driver = {
97 .name = "gl520sm",
99 .attach_adapter = gl520_attach_adapter,
100 .detach_client = gl520_detach_client,
103 /* Client data */
104 struct gl520_data {
105 struct i2c_client client;
106 struct device *hwmon_dev;
107 struct mutex update_lock;
108 char valid; /* zero until the following fields are valid */
109 unsigned long last_updated; /* in jiffies */
111 u8 vid;
112 u8 vrm;
113 u8 in_input[5]; /* [0] = VVD */
114 u8 in_min[5]; /* [0] = VDD */
115 u8 in_max[5]; /* [0] = VDD */
116 u8 fan_input[2];
117 u8 fan_min[2];
118 u8 fan_div[2];
119 u8 fan_off;
120 u8 temp_input[2];
121 u8 temp_max[2];
122 u8 temp_max_hyst[2];
123 u8 alarms;
124 u8 beep_enable;
125 u8 beep_mask;
126 u8 alarm_mask;
127 u8 two_temps;
131 * Sysfs stuff
134 static ssize_t get_cpu_vid(struct device *dev, struct device_attribute *attr,
135 char *buf)
137 struct gl520_data *data = gl520_update_device(dev);
138 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
140 static DEVICE_ATTR(cpu0_vid, S_IRUGO, get_cpu_vid, NULL);
142 #define VDD_FROM_REG(val) (((val)*95+2)/4)
143 #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
145 #define IN_FROM_REG(val) ((val)*19)
146 #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
148 static ssize_t get_in_input(struct device *dev, struct device_attribute *attr,
149 char *buf)
151 int n = to_sensor_dev_attr(attr)->index;
152 struct gl520_data *data = gl520_update_device(dev);
153 u8 r = data->in_input[n];
155 if (n == 0)
156 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
157 else
158 return sprintf(buf, "%d\n", IN_FROM_REG(r));
161 static ssize_t get_in_min(struct device *dev, struct device_attribute *attr,
162 char *buf)
164 int n = to_sensor_dev_attr(attr)->index;
165 struct gl520_data *data = gl520_update_device(dev);
166 u8 r = data->in_min[n];
168 if (n == 0)
169 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
170 else
171 return sprintf(buf, "%d\n", IN_FROM_REG(r));
174 static ssize_t get_in_max(struct device *dev, struct device_attribute *attr,
175 char *buf)
177 int n = to_sensor_dev_attr(attr)->index;
178 struct gl520_data *data = gl520_update_device(dev);
179 u8 r = data->in_max[n];
181 if (n == 0)
182 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
183 else
184 return sprintf(buf, "%d\n", IN_FROM_REG(r));
187 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
188 const char *buf, size_t count)
190 struct i2c_client *client = to_i2c_client(dev);
191 struct gl520_data *data = i2c_get_clientdata(client);
192 int n = to_sensor_dev_attr(attr)->index;
193 long v = simple_strtol(buf, NULL, 10);
194 u8 r;
196 mutex_lock(&data->update_lock);
198 if (n == 0)
199 r = VDD_TO_REG(v);
200 else
201 r = IN_TO_REG(v);
203 data->in_min[n] = r;
205 if (n < 4)
206 gl520_write_value(client, GL520_REG_IN_MIN[n],
207 (gl520_read_value(client, GL520_REG_IN_MIN[n])
208 & ~0xff) | r);
209 else
210 gl520_write_value(client, GL520_REG_IN_MIN[n], r);
212 mutex_unlock(&data->update_lock);
213 return count;
216 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
217 const char *buf, size_t count)
219 struct i2c_client *client = to_i2c_client(dev);
220 struct gl520_data *data = i2c_get_clientdata(client);
221 int n = to_sensor_dev_attr(attr)->index;
222 long v = simple_strtol(buf, NULL, 10);
223 u8 r;
225 if (n == 0)
226 r = VDD_TO_REG(v);
227 else
228 r = IN_TO_REG(v);
230 mutex_lock(&data->update_lock);
232 data->in_max[n] = r;
234 if (n < 4)
235 gl520_write_value(client, GL520_REG_IN_MAX[n],
236 (gl520_read_value(client, GL520_REG_IN_MAX[n])
237 & ~0xff00) | (r << 8));
238 else
239 gl520_write_value(client, GL520_REG_IN_MAX[n], r);
241 mutex_unlock(&data->update_lock);
242 return count;
245 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, get_in_input, NULL, 0);
246 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, get_in_input, NULL, 1);
247 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, get_in_input, NULL, 2);
248 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, get_in_input, NULL, 3);
249 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, get_in_input, NULL, 4);
250 static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
251 get_in_min, set_in_min, 0);
252 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
253 get_in_min, set_in_min, 1);
254 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
255 get_in_min, set_in_min, 2);
256 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
257 get_in_min, set_in_min, 3);
258 static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
259 get_in_min, set_in_min, 4);
260 static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
261 get_in_max, set_in_max, 0);
262 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
263 get_in_max, set_in_max, 1);
264 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
265 get_in_max, set_in_max, 2);
266 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
267 get_in_max, set_in_max, 3);
268 static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
269 get_in_max, set_in_max, 4);
271 #define DIV_FROM_REG(val) (1 << (val))
272 #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
273 #define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
275 static ssize_t get_fan_input(struct device *dev, struct device_attribute *attr,
276 char *buf)
278 int n = to_sensor_dev_attr(attr)->index;
279 struct gl520_data *data = gl520_update_device(dev);
281 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n],
282 data->fan_div[n]));
285 static ssize_t get_fan_min(struct device *dev, struct device_attribute *attr,
286 char *buf)
288 int n = to_sensor_dev_attr(attr)->index;
289 struct gl520_data *data = gl520_update_device(dev);
291 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n],
292 data->fan_div[n]));
295 static ssize_t get_fan_div(struct device *dev, struct device_attribute *attr,
296 char *buf)
298 int n = to_sensor_dev_attr(attr)->index;
299 struct gl520_data *data = gl520_update_device(dev);
301 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n]));
304 static ssize_t get_fan_off(struct device *dev, struct device_attribute *attr,
305 char *buf)
307 struct gl520_data *data = gl520_update_device(dev);
308 return sprintf(buf, "%d\n", data->fan_off);
311 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
312 const char *buf, size_t count)
314 struct i2c_client *client = to_i2c_client(dev);
315 struct gl520_data *data = i2c_get_clientdata(client);
316 int n = to_sensor_dev_attr(attr)->index;
317 unsigned long v = simple_strtoul(buf, NULL, 10);
318 u8 r;
320 mutex_lock(&data->update_lock);
321 r = FAN_TO_REG(v, data->fan_div[n]);
322 data->fan_min[n] = r;
324 if (n == 0)
325 gl520_write_value(client, GL520_REG_FAN_MIN,
326 (gl520_read_value(client, GL520_REG_FAN_MIN)
327 & ~0xff00) | (r << 8));
328 else
329 gl520_write_value(client, GL520_REG_FAN_MIN,
330 (gl520_read_value(client, GL520_REG_FAN_MIN)
331 & ~0xff) | r);
333 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
334 if (data->fan_min[n] == 0)
335 data->alarm_mask &= (n == 0) ? ~0x20 : ~0x40;
336 else
337 data->alarm_mask |= (n == 0) ? 0x20 : 0x40;
338 data->beep_mask &= data->alarm_mask;
339 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
341 mutex_unlock(&data->update_lock);
342 return count;
345 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
346 const char *buf, size_t count)
348 struct i2c_client *client = to_i2c_client(dev);
349 struct gl520_data *data = i2c_get_clientdata(client);
350 int n = to_sensor_dev_attr(attr)->index;
351 unsigned long v = simple_strtoul(buf, NULL, 10);
352 u8 r;
354 switch (v) {
355 case 1: r = 0; break;
356 case 2: r = 1; break;
357 case 4: r = 2; break;
358 case 8: r = 3; break;
359 default:
360 dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
361 return -EINVAL;
364 mutex_lock(&data->update_lock);
365 data->fan_div[n] = r;
367 if (n == 0)
368 gl520_write_value(client, GL520_REG_FAN_DIV,
369 (gl520_read_value(client, GL520_REG_FAN_DIV)
370 & ~0xc0) | (r << 6));
371 else
372 gl520_write_value(client, GL520_REG_FAN_DIV,
373 (gl520_read_value(client, GL520_REG_FAN_DIV)
374 & ~0x30) | (r << 4));
376 mutex_unlock(&data->update_lock);
377 return count;
380 static ssize_t set_fan_off(struct device *dev, struct device_attribute *attr,
381 const char *buf, size_t count)
383 struct i2c_client *client = to_i2c_client(dev);
384 struct gl520_data *data = i2c_get_clientdata(client);
385 u8 r = simple_strtoul(buf, NULL, 10)?1:0;
387 mutex_lock(&data->update_lock);
388 data->fan_off = r;
389 gl520_write_value(client, GL520_REG_FAN_OFF,
390 (gl520_read_value(client, GL520_REG_FAN_OFF)
391 & ~0x0c) | (r << 2));
392 mutex_unlock(&data->update_lock);
393 return count;
396 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_input, NULL, 0);
397 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan_input, NULL, 1);
398 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
399 get_fan_min, set_fan_min, 0);
400 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
401 get_fan_min, set_fan_min, 1);
402 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
403 get_fan_div, set_fan_div, 0);
404 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
405 get_fan_div, set_fan_div, 1);
406 static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR,
407 get_fan_off, set_fan_off);
409 #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
410 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
412 static ssize_t get_temp_input(struct device *dev, struct device_attribute *attr,
413 char *buf)
415 int n = to_sensor_dev_attr(attr)->index;
416 struct gl520_data *data = gl520_update_device(dev);
418 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n]));
421 static ssize_t get_temp_max(struct device *dev, struct device_attribute *attr,
422 char *buf)
424 int n = to_sensor_dev_attr(attr)->index;
425 struct gl520_data *data = gl520_update_device(dev);
427 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n]));
430 static ssize_t get_temp_max_hyst(struct device *dev, struct device_attribute
431 *attr, char *buf)
433 int n = to_sensor_dev_attr(attr)->index;
434 struct gl520_data *data = gl520_update_device(dev);
436 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n]));
439 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
440 const char *buf, size_t count)
442 struct i2c_client *client = to_i2c_client(dev);
443 struct gl520_data *data = i2c_get_clientdata(client);
444 int n = to_sensor_dev_attr(attr)->index;
445 long v = simple_strtol(buf, NULL, 10);
447 mutex_lock(&data->update_lock);
448 data->temp_max[n] = TEMP_TO_REG(v);
449 gl520_write_value(client, GL520_REG_TEMP_MAX[n], data->temp_max[n]);
450 mutex_unlock(&data->update_lock);
451 return count;
454 static ssize_t set_temp_max_hyst(struct device *dev, struct device_attribute
455 *attr, const char *buf, size_t count)
457 struct i2c_client *client = to_i2c_client(dev);
458 struct gl520_data *data = i2c_get_clientdata(client);
459 int n = to_sensor_dev_attr(attr)->index;
460 long v = simple_strtol(buf, NULL, 10);
462 mutex_lock(&data->update_lock);
463 data->temp_max_hyst[n] = TEMP_TO_REG(v);
464 gl520_write_value(client, GL520_REG_TEMP_MAX_HYST[n],
465 data->temp_max_hyst[n]);
466 mutex_unlock(&data->update_lock);
467 return count;
470 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, get_temp_input, NULL, 0);
471 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, get_temp_input, NULL, 1);
472 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
473 get_temp_max, set_temp_max, 0);
474 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
475 get_temp_max, set_temp_max, 1);
476 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
477 get_temp_max_hyst, set_temp_max_hyst, 0);
478 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
479 get_temp_max_hyst, set_temp_max_hyst, 1);
481 static ssize_t get_alarms(struct device *dev, struct device_attribute *attr,
482 char *buf)
484 struct gl520_data *data = gl520_update_device(dev);
485 return sprintf(buf, "%d\n", data->alarms);
488 static ssize_t get_beep_enable(struct device *dev, struct device_attribute
489 *attr, char *buf)
491 struct gl520_data *data = gl520_update_device(dev);
492 return sprintf(buf, "%d\n", data->beep_enable);
495 static ssize_t get_beep_mask(struct device *dev, struct device_attribute *attr,
496 char *buf)
498 struct gl520_data *data = gl520_update_device(dev);
499 return sprintf(buf, "%d\n", data->beep_mask);
502 static ssize_t set_beep_enable(struct device *dev, struct device_attribute
503 *attr, const char *buf, size_t count)
505 struct i2c_client *client = to_i2c_client(dev);
506 struct gl520_data *data = i2c_get_clientdata(client);
507 u8 r = simple_strtoul(buf, NULL, 10)?0:1;
509 mutex_lock(&data->update_lock);
510 data->beep_enable = !r;
511 gl520_write_value(client, GL520_REG_BEEP_ENABLE,
512 (gl520_read_value(client, GL520_REG_BEEP_ENABLE)
513 & ~0x04) | (r << 2));
514 mutex_unlock(&data->update_lock);
515 return count;
518 static ssize_t set_beep_mask(struct device *dev, struct device_attribute *attr,
519 const char *buf, size_t count)
521 struct i2c_client *client = to_i2c_client(dev);
522 struct gl520_data *data = i2c_get_clientdata(client);
523 u8 r = simple_strtoul(buf, NULL, 10);
525 mutex_lock(&data->update_lock);
526 r &= data->alarm_mask;
527 data->beep_mask = r;
528 gl520_write_value(client, GL520_REG_BEEP_MASK, r);
529 mutex_unlock(&data->update_lock);
530 return count;
533 static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
534 static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
535 get_beep_enable, set_beep_enable);
536 static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
537 get_beep_mask, set_beep_mask);
539 static ssize_t get_alarm(struct device *dev, struct device_attribute *attr,
540 char *buf)
542 int bit_nr = to_sensor_dev_attr(attr)->index;
543 struct gl520_data *data = gl520_update_device(dev);
545 return sprintf(buf, "%d\n", (data->alarms >> bit_nr) & 1);
548 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, get_alarm, NULL, 0);
549 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, get_alarm, NULL, 1);
550 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, get_alarm, NULL, 2);
551 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, get_alarm, NULL, 3);
552 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, get_alarm, NULL, 4);
553 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, get_alarm, NULL, 5);
554 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, get_alarm, NULL, 6);
555 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, get_alarm, NULL, 7);
556 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, get_alarm, NULL, 7);
558 static ssize_t get_beep(struct device *dev, struct device_attribute *attr,
559 char *buf)
561 int bitnr = to_sensor_dev_attr(attr)->index;
562 struct gl520_data *data = gl520_update_device(dev);
564 return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1);
567 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
568 const char *buf, size_t count)
570 struct i2c_client *client = to_i2c_client(dev);
571 struct gl520_data *data = i2c_get_clientdata(client);
572 int bitnr = to_sensor_dev_attr(attr)->index;
573 unsigned long bit;
575 bit = simple_strtoul(buf, NULL, 10);
576 if (bit & ~1)
577 return -EINVAL;
579 mutex_lock(&data->update_lock);
580 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
581 if (bit)
582 data->beep_mask |= (1 << bitnr);
583 else
584 data->beep_mask &= ~(1 << bitnr);
585 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
586 mutex_unlock(&data->update_lock);
587 return count;
590 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 0);
591 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 1);
592 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 2);
593 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 3);
594 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 4);
595 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 5);
596 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 6);
597 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
598 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
600 static struct attribute *gl520_attributes[] = {
601 &dev_attr_cpu0_vid.attr,
603 &sensor_dev_attr_in0_input.dev_attr.attr,
604 &sensor_dev_attr_in0_min.dev_attr.attr,
605 &sensor_dev_attr_in0_max.dev_attr.attr,
606 &sensor_dev_attr_in0_alarm.dev_attr.attr,
607 &sensor_dev_attr_in0_beep.dev_attr.attr,
608 &sensor_dev_attr_in1_input.dev_attr.attr,
609 &sensor_dev_attr_in1_min.dev_attr.attr,
610 &sensor_dev_attr_in1_max.dev_attr.attr,
611 &sensor_dev_attr_in1_alarm.dev_attr.attr,
612 &sensor_dev_attr_in1_beep.dev_attr.attr,
613 &sensor_dev_attr_in2_input.dev_attr.attr,
614 &sensor_dev_attr_in2_min.dev_attr.attr,
615 &sensor_dev_attr_in2_max.dev_attr.attr,
616 &sensor_dev_attr_in2_alarm.dev_attr.attr,
617 &sensor_dev_attr_in2_beep.dev_attr.attr,
618 &sensor_dev_attr_in3_input.dev_attr.attr,
619 &sensor_dev_attr_in3_min.dev_attr.attr,
620 &sensor_dev_attr_in3_max.dev_attr.attr,
621 &sensor_dev_attr_in3_alarm.dev_attr.attr,
622 &sensor_dev_attr_in3_beep.dev_attr.attr,
624 &sensor_dev_attr_fan1_input.dev_attr.attr,
625 &sensor_dev_attr_fan1_min.dev_attr.attr,
626 &sensor_dev_attr_fan1_div.dev_attr.attr,
627 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
628 &sensor_dev_attr_fan1_beep.dev_attr.attr,
629 &dev_attr_fan1_off.attr,
630 &sensor_dev_attr_fan2_input.dev_attr.attr,
631 &sensor_dev_attr_fan2_min.dev_attr.attr,
632 &sensor_dev_attr_fan2_div.dev_attr.attr,
633 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
634 &sensor_dev_attr_fan2_beep.dev_attr.attr,
636 &sensor_dev_attr_temp1_input.dev_attr.attr,
637 &sensor_dev_attr_temp1_max.dev_attr.attr,
638 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
639 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
640 &sensor_dev_attr_temp1_beep.dev_attr.attr,
642 &dev_attr_alarms.attr,
643 &dev_attr_beep_enable.attr,
644 &dev_attr_beep_mask.attr,
645 NULL
648 static const struct attribute_group gl520_group = {
649 .attrs = gl520_attributes,
652 static struct attribute *gl520_attributes_opt[] = {
653 &sensor_dev_attr_in4_input.dev_attr.attr,
654 &sensor_dev_attr_in4_min.dev_attr.attr,
655 &sensor_dev_attr_in4_max.dev_attr.attr,
656 &sensor_dev_attr_in4_alarm.dev_attr.attr,
657 &sensor_dev_attr_in4_beep.dev_attr.attr,
659 &sensor_dev_attr_temp2_input.dev_attr.attr,
660 &sensor_dev_attr_temp2_max.dev_attr.attr,
661 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
662 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
663 &sensor_dev_attr_temp2_beep.dev_attr.attr,
664 NULL
667 static const struct attribute_group gl520_group_opt = {
668 .attrs = gl520_attributes_opt,
673 * Real code
676 static int gl520_attach_adapter(struct i2c_adapter *adapter)
678 if (!(adapter->class & I2C_CLASS_HWMON))
679 return 0;
680 return i2c_probe(adapter, &addr_data, gl520_detect);
683 static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
685 struct i2c_client *client;
686 struct gl520_data *data;
687 int err = 0;
689 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
690 I2C_FUNC_SMBUS_WORD_DATA))
691 goto exit;
693 /* OK. For now, we presume we have a valid client. We now create the
694 client structure, even though we cannot fill it completely yet.
695 But it allows us to access gl520_{read,write}_value. */
697 if (!(data = kzalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
698 err = -ENOMEM;
699 goto exit;
702 client = &data->client;
703 i2c_set_clientdata(client, data);
704 client->addr = address;
705 client->adapter = adapter;
706 client->driver = &gl520_driver;
708 /* Determine the chip type. */
709 if (kind < 0) {
710 if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) ||
711 ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
712 ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) {
713 dev_dbg(&client->dev, "Unknown chip type, skipping\n");
714 goto exit_free;
718 /* Fill in the remaining client fields */
719 strlcpy(client->name, "gl520sm", I2C_NAME_SIZE);
720 mutex_init(&data->update_lock);
722 /* Tell the I2C layer a new client has arrived */
723 if ((err = i2c_attach_client(client)))
724 goto exit_free;
726 /* Initialize the GL520SM chip */
727 gl520_init_client(client);
729 /* Register sysfs hooks */
730 if ((err = sysfs_create_group(&client->dev.kobj, &gl520_group)))
731 goto exit_detach;
733 if (data->two_temps) {
734 if ((err = device_create_file(&client->dev,
735 &sensor_dev_attr_temp2_input.dev_attr))
736 || (err = device_create_file(&client->dev,
737 &sensor_dev_attr_temp2_max.dev_attr))
738 || (err = device_create_file(&client->dev,
739 &sensor_dev_attr_temp2_max_hyst.dev_attr))
740 || (err = device_create_file(&client->dev,
741 &sensor_dev_attr_temp2_alarm.dev_attr))
742 || (err = device_create_file(&client->dev,
743 &sensor_dev_attr_temp2_beep.dev_attr)))
744 goto exit_remove_files;
745 } else {
746 if ((err = device_create_file(&client->dev,
747 &sensor_dev_attr_in4_input.dev_attr))
748 || (err = device_create_file(&client->dev,
749 &sensor_dev_attr_in4_min.dev_attr))
750 || (err = device_create_file(&client->dev,
751 &sensor_dev_attr_in4_max.dev_attr))
752 || (err = device_create_file(&client->dev,
753 &sensor_dev_attr_in4_alarm.dev_attr))
754 || (err = device_create_file(&client->dev,
755 &sensor_dev_attr_in4_beep.dev_attr)))
756 goto exit_remove_files;
760 data->hwmon_dev = hwmon_device_register(&client->dev);
761 if (IS_ERR(data->hwmon_dev)) {
762 err = PTR_ERR(data->hwmon_dev);
763 goto exit_remove_files;
766 return 0;
768 exit_remove_files:
769 sysfs_remove_group(&client->dev.kobj, &gl520_group);
770 sysfs_remove_group(&client->dev.kobj, &gl520_group_opt);
771 exit_detach:
772 i2c_detach_client(client);
773 exit_free:
774 kfree(data);
775 exit:
776 return err;
780 /* Called when we have found a new GL520SM. */
781 static void gl520_init_client(struct i2c_client *client)
783 struct gl520_data *data = i2c_get_clientdata(client);
784 u8 oldconf, conf;
786 conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
788 data->alarm_mask = 0xff;
789 data->vrm = vid_which_vrm();
791 if (extra_sensor_type == 1)
792 conf &= ~0x10;
793 else if (extra_sensor_type == 2)
794 conf |= 0x10;
795 data->two_temps = !(conf & 0x10);
797 /* If IRQ# is disabled, we can safely force comparator mode */
798 if (!(conf & 0x20))
799 conf &= 0xf7;
801 /* Enable monitoring if needed */
802 conf |= 0x40;
804 if (conf != oldconf)
805 gl520_write_value(client, GL520_REG_CONF, conf);
807 gl520_update_device(&(client->dev));
809 if (data->fan_min[0] == 0)
810 data->alarm_mask &= ~0x20;
811 if (data->fan_min[1] == 0)
812 data->alarm_mask &= ~0x40;
814 data->beep_mask &= data->alarm_mask;
815 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
818 static int gl520_detach_client(struct i2c_client *client)
820 struct gl520_data *data = i2c_get_clientdata(client);
821 int err;
823 hwmon_device_unregister(data->hwmon_dev);
824 sysfs_remove_group(&client->dev.kobj, &gl520_group);
825 sysfs_remove_group(&client->dev.kobj, &gl520_group_opt);
827 if ((err = i2c_detach_client(client)))
828 return err;
830 kfree(data);
831 return 0;
835 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
836 GL520 uses a high-byte first convention */
837 static int gl520_read_value(struct i2c_client *client, u8 reg)
839 if ((reg >= 0x07) && (reg <= 0x0c))
840 return swab16(i2c_smbus_read_word_data(client, reg));
841 else
842 return i2c_smbus_read_byte_data(client, reg);
845 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
847 if ((reg >= 0x07) && (reg <= 0x0c))
848 return i2c_smbus_write_word_data(client, reg, swab16(value));
849 else
850 return i2c_smbus_write_byte_data(client, reg, value);
854 static struct gl520_data *gl520_update_device(struct device *dev)
856 struct i2c_client *client = to_i2c_client(dev);
857 struct gl520_data *data = i2c_get_clientdata(client);
858 int val, i;
860 mutex_lock(&data->update_lock);
862 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
864 dev_dbg(&client->dev, "Starting gl520sm update\n");
866 data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
867 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
868 data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
870 for (i = 0; i < 4; i++) {
871 data->in_input[i] = gl520_read_value(client,
872 GL520_REG_IN_INPUT[i]);
873 val = gl520_read_value(client, GL520_REG_IN_LIMIT[i]);
874 data->in_min[i] = val & 0xff;
875 data->in_max[i] = (val >> 8) & 0xff;
878 val = gl520_read_value(client, GL520_REG_FAN_INPUT);
879 data->fan_input[0] = (val >> 8) & 0xff;
880 data->fan_input[1] = val & 0xff;
882 val = gl520_read_value(client, GL520_REG_FAN_MIN);
883 data->fan_min[0] = (val >> 8) & 0xff;
884 data->fan_min[1] = val & 0xff;
886 data->temp_input[0] = gl520_read_value(client,
887 GL520_REG_TEMP_INPUT[0]);
888 data->temp_max[0] = gl520_read_value(client,
889 GL520_REG_TEMP_MAX[0]);
890 data->temp_max_hyst[0] = gl520_read_value(client,
891 GL520_REG_TEMP_MAX_HYST[0]);
893 val = gl520_read_value(client, GL520_REG_FAN_DIV);
894 data->fan_div[0] = (val >> 6) & 0x03;
895 data->fan_div[1] = (val >> 4) & 0x03;
896 data->fan_off = (val >> 2) & 0x01;
898 data->alarms &= data->alarm_mask;
900 val = gl520_read_value(client, GL520_REG_CONF);
901 data->beep_enable = !((val >> 2) & 1);
903 /* Temp1 and Vin4 are the same input */
904 if (data->two_temps) {
905 data->temp_input[1] = gl520_read_value(client,
906 GL520_REG_TEMP_INPUT[1]);
907 data->temp_max[1] = gl520_read_value(client,
908 GL520_REG_TEMP_MAX[1]);
909 data->temp_max_hyst[1] = gl520_read_value(client,
910 GL520_REG_TEMP_MAX_HYST[1]);
911 } else {
912 data->in_input[4] = gl520_read_value(client,
913 GL520_REG_IN_INPUT[4]);
914 data->in_min[4] = gl520_read_value(client,
915 GL520_REG_IN_MIN[4]);
916 data->in_max[4] = gl520_read_value(client,
917 GL520_REG_IN_MAX[4]);
920 data->last_updated = jiffies;
921 data->valid = 1;
924 mutex_unlock(&data->update_lock);
926 return data;
930 static int __init sensors_gl520sm_init(void)
932 return i2c_add_driver(&gl520_driver);
935 static void __exit sensors_gl520sm_exit(void)
937 i2c_del_driver(&gl520_driver);
941 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
942 "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
943 "Maarten Deprez <maartendeprez@users.sourceforge.net>");
944 MODULE_DESCRIPTION("GL520SM driver");
945 MODULE_LICENSE("GPL");
947 module_init(sensors_gl520sm_init);
948 module_exit(sensors_gl520sm_exit);