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