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