• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/hwmon/
1
2
3#include <linux/module.h>
4#include <linux/init.h>
5#include <linux/slab.h>
6#include <linux/jiffies.h>
7#include <linux/i2c.h>
8#include <linux/hwmon.h>
9#include <linux/hwmon-sysfs.h>
10#include <linux/err.h>
11#include <linux/mutex.h>
12#include <linux/sysfs.h>
13
14/* Addresses to scan */
15static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
16
17enum chips { gl518sm_r00, gl518sm_r80 };
18
19/* Many GL518 constants specified below */
20
21/* The GL518 registers */
22#define GL518_REG_CHIP_ID	0x00
23#define GL518_REG_REVISION	0x01
24#define GL518_REG_VENDOR_ID	0x02
25#define GL518_REG_CONF		0x03
26#define GL518_REG_TEMP_IN	0x04
27#define GL518_REG_TEMP_MAX	0x05
28#define GL518_REG_TEMP_HYST	0x06
29#define GL518_REG_FAN_COUNT	0x07
30#define GL518_REG_FAN_LIMIT	0x08
31#define GL518_REG_VIN1_LIMIT	0x09
32#define GL518_REG_VIN2_LIMIT	0x0a
33#define GL518_REG_VIN3_LIMIT	0x0b
34#define GL518_REG_VDD_LIMIT	0x0c
35#define GL518_REG_VIN3		0x0d
36#define GL518_REG_MISC		0x0f
37#define GL518_REG_ALARM		0x10
38#define GL518_REG_MASK		0x11
39#define GL518_REG_INT		0x12
40#define GL518_REG_VIN2		0x13
41#define GL518_REG_VIN1		0x14
42#define GL518_REG_VDD		0x15
43
44
45/*
46 * Conversions. Rounding and limit checking is only done on the TO_REG
47 * variants. Note that you should be a bit careful with which arguments
48 * these macros are called: arguments may be evaluated more than once.
49 * Fixing this is just not worth it.
50 */
51
52#define RAW_FROM_REG(val)	val
53
54#define BOOL_FROM_REG(val)	((val)?0:1)
55#define BOOL_TO_REG(val)	((val)?0:1)
56
57#define TEMP_TO_REG(val)	(SENSORS_LIMIT(((((val)<0? \
58				(val)-500:(val)+500)/1000)+119),0,255))
59#define TEMP_FROM_REG(val)	(((val) - 119) * 1000)
60
61static inline u8 FAN_TO_REG(long rpm, int div)
62{
63	long rpmdiv;
64	if (rpm == 0)
65		return 0;
66	rpmdiv = SENSORS_LIMIT(rpm, 1, 960000) * div;
67	return SENSORS_LIMIT((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
68}
69#define FAN_FROM_REG(val,div)	((val)==0 ? 0 : (480000/((val)*(div))))
70
71#define IN_TO_REG(val)		(SENSORS_LIMIT((((val)+9)/19),0,255))
72#define IN_FROM_REG(val)	((val)*19)
73
74#define VDD_TO_REG(val)		(SENSORS_LIMIT((((val)*4+47)/95),0,255))
75#define VDD_FROM_REG(val)	(((val)*95+2)/4)
76
77#define DIV_FROM_REG(val)	(1 << (val))
78
79#define BEEP_MASK_TO_REG(val)	((val) & 0x7f & data->alarm_mask)
80#define BEEP_MASK_FROM_REG(val)	((val) & 0x7f)
81
82/* Each client has this additional data */
83struct gl518_data {
84	struct device *hwmon_dev;
85	enum chips type;
86
87	struct mutex update_lock;
88	char valid;		/* !=0 if following fields are valid */
89	unsigned long last_updated;	/* In jiffies */
90
91	u8 voltage_in[4];	/* Register values; [0] = VDD */
92	u8 voltage_min[4];	/* Register values; [0] = VDD */
93	u8 voltage_max[4];	/* Register values; [0] = VDD */
94	u8 fan_in[2];
95	u8 fan_min[2];
96	u8 fan_div[2];		/* Register encoding, shifted right */
97	u8 fan_auto1;		/* Boolean */
98	u8 temp_in;		/* Register values */
99	u8 temp_max;		/* Register values */
100	u8 temp_hyst;		/* Register values */
101	u8 alarms;		/* Register value */
102	u8 alarm_mask;
103	u8 beep_mask;		/* Register value */
104	u8 beep_enable;		/* Boolean */
105};
106
107static int gl518_probe(struct i2c_client *client,
108		       const struct i2c_device_id *id);
109static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info);
110static void gl518_init_client(struct i2c_client *client);
111static int gl518_remove(struct i2c_client *client);
112static int gl518_read_value(struct i2c_client *client, u8 reg);
113static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
114static struct gl518_data *gl518_update_device(struct device *dev);
115
116static const struct i2c_device_id gl518_id[] = {
117	{ "gl518sm", 0 },
118	{ }
119};
120MODULE_DEVICE_TABLE(i2c, gl518_id);
121
122/* This is the driver that will be inserted */
123static struct i2c_driver gl518_driver = {
124	.class		= I2C_CLASS_HWMON,
125	.driver = {
126		.name	= "gl518sm",
127	},
128	.probe		= gl518_probe,
129	.remove		= gl518_remove,
130	.id_table	= gl518_id,
131	.detect		= gl518_detect,
132	.address_list	= normal_i2c,
133};
134
135/*
136 * Sysfs stuff
137 */
138
139#define show(type, suffix, value)					\
140static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf)		\
141{									\
142	struct gl518_data *data = gl518_update_device(dev);		\
143	return sprintf(buf, "%d\n", type##_FROM_REG(data->value));	\
144}
145
146show(TEMP, temp_input1, temp_in);
147show(TEMP, temp_max1, temp_max);
148show(TEMP, temp_hyst1, temp_hyst);
149show(BOOL, fan_auto1, fan_auto1);
150show(VDD, in_input0, voltage_in[0]);
151show(IN, in_input1, voltage_in[1]);
152show(IN, in_input2, voltage_in[2]);
153show(IN, in_input3, voltage_in[3]);
154show(VDD, in_min0, voltage_min[0]);
155show(IN, in_min1, voltage_min[1]);
156show(IN, in_min2, voltage_min[2]);
157show(IN, in_min3, voltage_min[3]);
158show(VDD, in_max0, voltage_max[0]);
159show(IN, in_max1, voltage_max[1]);
160show(IN, in_max2, voltage_max[2]);
161show(IN, in_max3, voltage_max[3]);
162show(RAW, alarms, alarms);
163show(BOOL, beep_enable, beep_enable);
164show(BEEP_MASK, beep_mask, beep_mask);
165
166static ssize_t show_fan_input(struct device *dev,
167			      struct device_attribute *attr, char *buf)
168{
169	int nr = to_sensor_dev_attr(attr)->index;
170	struct gl518_data *data = gl518_update_device(dev);
171	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
172					DIV_FROM_REG(data->fan_div[nr])));
173}
174
175static ssize_t show_fan_min(struct device *dev,
176			    struct device_attribute *attr, char *buf)
177{
178	int nr = to_sensor_dev_attr(attr)->index;
179	struct gl518_data *data = gl518_update_device(dev);
180	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
181					DIV_FROM_REG(data->fan_div[nr])));
182}
183
184static ssize_t show_fan_div(struct device *dev,
185			    struct device_attribute *attr, char *buf)
186{
187	int nr = to_sensor_dev_attr(attr)->index;
188	struct gl518_data *data = gl518_update_device(dev);
189	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
190}
191
192#define set(type, suffix, value, reg)					\
193static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf,	\
194	size_t count)							\
195{									\
196	struct i2c_client *client = to_i2c_client(dev);			\
197	struct gl518_data *data = i2c_get_clientdata(client);		\
198	long val = simple_strtol(buf, NULL, 10);			\
199									\
200	mutex_lock(&data->update_lock);					\
201	data->value = type##_TO_REG(val);				\
202	gl518_write_value(client, reg, data->value);			\
203	mutex_unlock(&data->update_lock);				\
204	return count;							\
205}
206
207#define set_bits(type, suffix, value, reg, mask, shift)			\
208static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf,	\
209	size_t count)							\
210{									\
211	struct i2c_client *client = to_i2c_client(dev);			\
212	struct gl518_data *data = i2c_get_clientdata(client);		\
213	int regvalue;							\
214	unsigned long val = simple_strtoul(buf, NULL, 10);		\
215									\
216	mutex_lock(&data->update_lock);					\
217	regvalue = gl518_read_value(client, reg);			\
218	data->value = type##_TO_REG(val);				\
219	regvalue = (regvalue & ~mask) | (data->value << shift);		\
220	gl518_write_value(client, reg, regvalue);			\
221	mutex_unlock(&data->update_lock);				\
222	return count;							\
223}
224
225#define set_low(type, suffix, value, reg)				\
226	set_bits(type, suffix, value, reg, 0x00ff, 0)
227#define set_high(type, suffix, value, reg)				\
228	set_bits(type, suffix, value, reg, 0xff00, 8)
229
230set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
231set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
232set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
233set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
234set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
235set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
236set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
237set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
238set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
239set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
240set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
241set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
242set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
243
244static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
245			   const char *buf, size_t count)
246{
247	struct i2c_client *client = to_i2c_client(dev);
248	struct gl518_data *data = i2c_get_clientdata(client);
249	int nr = to_sensor_dev_attr(attr)->index;
250	int regvalue;
251	unsigned long val = simple_strtoul(buf, NULL, 10);
252
253	mutex_lock(&data->update_lock);
254	regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
255	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
256	regvalue = (regvalue & (0xff << (8 * nr)))
257		 | (data->fan_min[nr] << (8 * (1 - nr)));
258	gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
259
260	data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
261	if (data->fan_min[nr] == 0)
262		data->alarm_mask &= ~(0x20 << nr);
263	else
264		data->alarm_mask |= (0x20 << nr);
265	data->beep_mask &= data->alarm_mask;
266	gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
267
268	mutex_unlock(&data->update_lock);
269	return count;
270}
271
272static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
273			   const char *buf, size_t count)
274{
275	struct i2c_client *client = to_i2c_client(dev);
276	struct gl518_data *data = i2c_get_clientdata(client);
277	int nr = to_sensor_dev_attr(attr)->index;
278	int regvalue;
279	unsigned long val = simple_strtoul(buf, NULL, 10);
280
281	switch (val) {
282	case 1: val = 0; break;
283	case 2: val = 1; break;
284	case 4: val = 2; break;
285	case 8: val = 3; break;
286	default:
287		dev_err(dev, "Invalid fan clock divider %lu, choose one "
288			"of 1, 2, 4 or 8\n", val);
289		return -EINVAL;
290	}
291
292	mutex_lock(&data->update_lock);
293	regvalue = gl518_read_value(client, GL518_REG_MISC);
294	data->fan_div[nr] = val;
295	regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
296		 | (data->fan_div[nr] << (6 - 2 * nr));
297	gl518_write_value(client, GL518_REG_MISC, regvalue);
298	mutex_unlock(&data->update_lock);
299	return count;
300}
301
302static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
303static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
304static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
305	show_temp_hyst1, set_temp_hyst1);
306static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
307static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
308static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
309static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO,
310	show_fan_min, set_fan_min, 0);
311static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO,
312	show_fan_min, set_fan_min, 1);
313static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO,
314	show_fan_div, set_fan_div, 0);
315static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO,
316	show_fan_div, set_fan_div, 1);
317static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
318static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
319static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
320static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
321static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
322static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
323static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
324static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
325static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
326static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
327static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
328static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
329static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
330static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
331	show_beep_enable, set_beep_enable);
332static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
333	show_beep_mask, set_beep_mask);
334
335static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
336			  char *buf)
337{
338	int bitnr = to_sensor_dev_attr(attr)->index;
339	struct gl518_data *data = gl518_update_device(dev);
340	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
341}
342
343static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
344static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
345static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
346static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
347static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
348static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
349static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
350
351static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
352			  char *buf)
353{
354	int bitnr = to_sensor_dev_attr(attr)->index;
355	struct gl518_data *data = gl518_update_device(dev);
356	return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
357}
358
359static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
360			const char *buf, size_t count)
361{
362	struct i2c_client *client = to_i2c_client(dev);
363	struct gl518_data *data = i2c_get_clientdata(client);
364	int bitnr = to_sensor_dev_attr(attr)->index;
365	unsigned long bit;
366
367	bit = simple_strtoul(buf, NULL, 10);
368	if (bit & ~1)
369		return -EINVAL;
370
371	mutex_lock(&data->update_lock);
372	data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
373	if (bit)
374		data->beep_mask |= (1 << bitnr);
375	else
376		data->beep_mask &= ~(1 << bitnr);
377	gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
378	mutex_unlock(&data->update_lock);
379	return count;
380}
381
382static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
383static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
384static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
385static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
386static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
387static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
388static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
389
390static struct attribute *gl518_attributes[] = {
391	&dev_attr_in3_input.attr,
392	&dev_attr_in0_min.attr,
393	&dev_attr_in1_min.attr,
394	&dev_attr_in2_min.attr,
395	&dev_attr_in3_min.attr,
396	&dev_attr_in0_max.attr,
397	&dev_attr_in1_max.attr,
398	&dev_attr_in2_max.attr,
399	&dev_attr_in3_max.attr,
400	&sensor_dev_attr_in0_alarm.dev_attr.attr,
401	&sensor_dev_attr_in1_alarm.dev_attr.attr,
402	&sensor_dev_attr_in2_alarm.dev_attr.attr,
403	&sensor_dev_attr_in3_alarm.dev_attr.attr,
404	&sensor_dev_attr_in0_beep.dev_attr.attr,
405	&sensor_dev_attr_in1_beep.dev_attr.attr,
406	&sensor_dev_attr_in2_beep.dev_attr.attr,
407	&sensor_dev_attr_in3_beep.dev_attr.attr,
408
409	&dev_attr_fan1_auto.attr,
410	&sensor_dev_attr_fan1_input.dev_attr.attr,
411	&sensor_dev_attr_fan2_input.dev_attr.attr,
412	&sensor_dev_attr_fan1_min.dev_attr.attr,
413	&sensor_dev_attr_fan2_min.dev_attr.attr,
414	&sensor_dev_attr_fan1_div.dev_attr.attr,
415	&sensor_dev_attr_fan2_div.dev_attr.attr,
416	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
417	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
418	&sensor_dev_attr_fan1_beep.dev_attr.attr,
419	&sensor_dev_attr_fan2_beep.dev_attr.attr,
420
421	&dev_attr_temp1_input.attr,
422	&dev_attr_temp1_max.attr,
423	&dev_attr_temp1_max_hyst.attr,
424	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
425	&sensor_dev_attr_temp1_beep.dev_attr.attr,
426
427	&dev_attr_alarms.attr,
428	&dev_attr_beep_enable.attr,
429	&dev_attr_beep_mask.attr,
430	NULL
431};
432
433static const struct attribute_group gl518_group = {
434	.attrs = gl518_attributes,
435};
436
437static struct attribute *gl518_attributes_r80[] = {
438	&dev_attr_in0_input.attr,
439	&dev_attr_in1_input.attr,
440	&dev_attr_in2_input.attr,
441	NULL
442};
443
444static const struct attribute_group gl518_group_r80 = {
445	.attrs = gl518_attributes_r80,
446};
447
448/*
449 * Real code
450 */
451
452/* Return 0 if detection is successful, -ENODEV otherwise */
453static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
454{
455	struct i2c_adapter *adapter = client->adapter;
456	int rev;
457
458	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
459				     I2C_FUNC_SMBUS_WORD_DATA))
460		return -ENODEV;
461
462	/* Now, we do the remaining detection. */
463	if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
464	 || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
465		return -ENODEV;
466
467	/* Determine the chip type. */
468	rev = gl518_read_value(client, GL518_REG_REVISION);
469	if (rev != 0x00 && rev != 0x80)
470		return -ENODEV;
471
472	strlcpy(info->type, "gl518sm", I2C_NAME_SIZE);
473
474	return 0;
475}
476
477static int gl518_probe(struct i2c_client *client,
478		       const struct i2c_device_id *id)
479{
480	struct gl518_data *data;
481	int err, revision;
482
483	data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL);
484	if (!data) {
485		err = -ENOMEM;
486		goto exit;
487	}
488
489	i2c_set_clientdata(client, data);
490	revision = gl518_read_value(client, GL518_REG_REVISION);
491	data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
492	mutex_init(&data->update_lock);
493
494	/* Initialize the GL518SM chip */
495	data->alarm_mask = 0xff;
496	gl518_init_client(client);
497
498	/* Register sysfs hooks */
499	if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group)))
500		goto exit_free;
501	if (data->type == gl518sm_r80)
502		if ((err = sysfs_create_group(&client->dev.kobj,
503					      &gl518_group_r80)))
504			goto exit_remove_files;
505
506	data->hwmon_dev = hwmon_device_register(&client->dev);
507	if (IS_ERR(data->hwmon_dev)) {
508		err = PTR_ERR(data->hwmon_dev);
509		goto exit_remove_files;
510	}
511
512	return 0;
513
514exit_remove_files:
515	sysfs_remove_group(&client->dev.kobj, &gl518_group);
516	if (data->type == gl518sm_r80)
517		sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
518exit_free:
519	kfree(data);
520exit:
521	return err;
522}
523
524
525/* Called when we have found a new GL518SM.
526   Note that we preserve D4:NoFan2 and D2:beep_enable. */
527static void gl518_init_client(struct i2c_client *client)
528{
529	/* Make sure we leave D7:Reset untouched */
530	u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
531
532	/* Comparator mode (D3=0), standby mode (D6=0) */
533	gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
534
535	/* Never interrupts */
536	gl518_write_value(client, GL518_REG_MASK, 0x00);
537
538	/* Clear status register (D5=1), start (D6=1) */
539	gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
540	gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
541}
542
543static int gl518_remove(struct i2c_client *client)
544{
545	struct gl518_data *data = i2c_get_clientdata(client);
546
547	hwmon_device_unregister(data->hwmon_dev);
548	sysfs_remove_group(&client->dev.kobj, &gl518_group);
549	if (data->type == gl518sm_r80)
550		sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
551
552	kfree(data);
553	return 0;
554}
555
556/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
557   GL518 uses a high-byte first convention, which is exactly opposite to
558   the SMBus standard. */
559static int gl518_read_value(struct i2c_client *client, u8 reg)
560{
561	if ((reg >= 0x07) && (reg <= 0x0c))
562		return swab16(i2c_smbus_read_word_data(client, reg));
563	else
564		return i2c_smbus_read_byte_data(client, reg);
565}
566
567static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
568{
569	if ((reg >= 0x07) && (reg <= 0x0c))
570		return i2c_smbus_write_word_data(client, reg, swab16(value));
571	else
572		return i2c_smbus_write_byte_data(client, reg, value);
573}
574
575static struct gl518_data *gl518_update_device(struct device *dev)
576{
577	struct i2c_client *client = to_i2c_client(dev);
578	struct gl518_data *data = i2c_get_clientdata(client);
579	int val;
580
581	mutex_lock(&data->update_lock);
582
583	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
584	    || !data->valid) {
585		dev_dbg(&client->dev, "Starting gl518 update\n");
586
587		data->alarms = gl518_read_value(client, GL518_REG_INT);
588		data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
589
590		val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
591		data->voltage_min[0] = val & 0xff;
592		data->voltage_max[0] = (val >> 8) & 0xff;
593		val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
594		data->voltage_min[1] = val & 0xff;
595		data->voltage_max[1] = (val >> 8) & 0xff;
596		val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
597		data->voltage_min[2] = val & 0xff;
598		data->voltage_max[2] = (val >> 8) & 0xff;
599		val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
600		data->voltage_min[3] = val & 0xff;
601		data->voltage_max[3] = (val >> 8) & 0xff;
602
603		val = gl518_read_value(client, GL518_REG_FAN_COUNT);
604		data->fan_in[0] = (val >> 8) & 0xff;
605		data->fan_in[1] = val & 0xff;
606
607		val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
608		data->fan_min[0] = (val >> 8) & 0xff;
609		data->fan_min[1] = val & 0xff;
610
611		data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
612		data->temp_max =
613		    gl518_read_value(client, GL518_REG_TEMP_MAX);
614		data->temp_hyst =
615		    gl518_read_value(client, GL518_REG_TEMP_HYST);
616
617		val = gl518_read_value(client, GL518_REG_MISC);
618		data->fan_div[0] = (val >> 6) & 0x03;
619		data->fan_div[1] = (val >> 4) & 0x03;
620		data->fan_auto1  = (val >> 3) & 0x01;
621
622		data->alarms &= data->alarm_mask;
623
624		val = gl518_read_value(client, GL518_REG_CONF);
625		data->beep_enable = (val >> 2) & 1;
626
627		if (data->type != gl518sm_r00) {
628			data->voltage_in[0] =
629			    gl518_read_value(client, GL518_REG_VDD);
630			data->voltage_in[1] =
631			    gl518_read_value(client, GL518_REG_VIN1);
632			data->voltage_in[2] =
633			    gl518_read_value(client, GL518_REG_VIN2);
634		}
635		data->voltage_in[3] =
636		    gl518_read_value(client, GL518_REG_VIN3);
637
638		data->last_updated = jiffies;
639		data->valid = 1;
640	}
641
642	mutex_unlock(&data->update_lock);
643
644	return data;
645}
646
647static int __init sensors_gl518sm_init(void)
648{
649	return i2c_add_driver(&gl518_driver);
650}
651
652static void __exit sensors_gl518sm_exit(void)
653{
654	i2c_del_driver(&gl518_driver);
655}
656
657MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
658	"Kyosti Malkki <kmalkki@cc.hut.fi> and "
659	"Hong-Gunn Chew <hglinux@gunnet.org>");
660MODULE_DESCRIPTION("GL518SM driver");
661MODULE_LICENSE("GPL");
662
663module_init(sensors_gl518sm_init);
664module_exit(sensors_gl518sm_exit);
665