Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / gl518sm.c
blob6eec5dd51ff279f0b6e13e5a4813c59941645381
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 <khali@linux-fr.org>
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.
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.
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.
23 * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
24 * and advice of Greg Kroah-Hartman.
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.
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>
46 /* Addresses to scan */
47 <<<<<<< HEAD:drivers/hwmon/gl518sm.c
48 static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
49 =======
50 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
51 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/gl518sm.c
53 /* Insmod parameters */
54 I2C_CLIENT_INSMOD_2(gl518sm_r00, gl518sm_r80);
56 /* Many GL518 constants specified below */
58 /* The GL518 registers */
59 #define GL518_REG_CHIP_ID 0x00
60 #define GL518_REG_REVISION 0x01
61 #define GL518_REG_VENDOR_ID 0x02
62 #define GL518_REG_CONF 0x03
63 #define GL518_REG_TEMP_IN 0x04
64 #define GL518_REG_TEMP_MAX 0x05
65 #define GL518_REG_TEMP_HYST 0x06
66 #define GL518_REG_FAN_COUNT 0x07
67 #define GL518_REG_FAN_LIMIT 0x08
68 #define GL518_REG_VIN1_LIMIT 0x09
69 #define GL518_REG_VIN2_LIMIT 0x0a
70 #define GL518_REG_VIN3_LIMIT 0x0b
71 #define GL518_REG_VDD_LIMIT 0x0c
72 #define GL518_REG_VIN3 0x0d
73 #define GL518_REG_MISC 0x0f
74 #define GL518_REG_ALARM 0x10
75 #define GL518_REG_MASK 0x11
76 #define GL518_REG_INT 0x12
77 #define GL518_REG_VIN2 0x13
78 #define GL518_REG_VIN1 0x14
79 #define GL518_REG_VDD 0x15
83 * Conversions. Rounding and limit checking is only done on the TO_REG
84 * variants. Note that you should be a bit careful with which arguments
85 * these macros are called: arguments may be evaluated more than once.
86 * Fixing this is just not worth it.
89 #define RAW_FROM_REG(val) val
91 #define BOOL_FROM_REG(val) ((val)?0:1)
92 #define BOOL_TO_REG(val) ((val)?0:1)
94 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \
95 (val)-500:(val)+500)/1000)+119),0,255))
96 #define TEMP_FROM_REG(val) (((val) - 119) * 1000)
98 static inline u8 FAN_TO_REG(long rpm, int div)
100 long rpmdiv;
101 if (rpm == 0)
102 return 0;
103 rpmdiv = SENSORS_LIMIT(rpm, 1, 960000) * div;
104 return SENSORS_LIMIT((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
106 #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val)*(div))))
108 #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
109 #define IN_FROM_REG(val) ((val)*19)
111 #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
112 #define VDD_FROM_REG(val) (((val)*95+2)/4)
114 #define DIV_FROM_REG(val) (1 << (val))
116 #define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
117 #define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
119 /* Each client has this additional data */
120 struct gl518_data {
121 struct i2c_client client;
122 struct device *hwmon_dev;
123 enum chips type;
125 struct mutex update_lock;
126 char valid; /* !=0 if following fields are valid */
127 unsigned long last_updated; /* In jiffies */
129 u8 voltage_in[4]; /* Register values; [0] = VDD */
130 u8 voltage_min[4]; /* Register values; [0] = VDD */
131 u8 voltage_max[4]; /* Register values; [0] = VDD */
132 u8 fan_in[2];
133 u8 fan_min[2];
134 u8 fan_div[2]; /* Register encoding, shifted right */
135 u8 fan_auto1; /* Boolean */
136 u8 temp_in; /* Register values */
137 u8 temp_max; /* Register values */
138 u8 temp_hyst; /* Register values */
139 u8 alarms; /* Register value */
140 u8 alarm_mask;
141 u8 beep_mask; /* Register value */
142 u8 beep_enable; /* Boolean */
145 static int gl518_attach_adapter(struct i2c_adapter *adapter);
146 static int gl518_detect(struct i2c_adapter *adapter, int address, int kind);
147 static void gl518_init_client(struct i2c_client *client);
148 static int gl518_detach_client(struct i2c_client *client);
149 static int gl518_read_value(struct i2c_client *client, u8 reg);
150 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
151 static struct gl518_data *gl518_update_device(struct device *dev);
153 /* This is the driver that will be inserted */
154 static struct i2c_driver gl518_driver = {
155 .driver = {
156 .name = "gl518sm",
158 .attach_adapter = gl518_attach_adapter,
159 .detach_client = gl518_detach_client,
163 * Sysfs stuff
166 #define show(type, suffix, value) \
167 static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
169 struct gl518_data *data = gl518_update_device(dev); \
170 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
173 show(TEMP, temp_input1, temp_in);
174 show(TEMP, temp_max1, temp_max);
175 show(TEMP, temp_hyst1, temp_hyst);
176 show(BOOL, fan_auto1, fan_auto1);
177 show(VDD, in_input0, voltage_in[0]);
178 show(IN, in_input1, voltage_in[1]);
179 show(IN, in_input2, voltage_in[2]);
180 show(IN, in_input3, voltage_in[3]);
181 show(VDD, in_min0, voltage_min[0]);
182 show(IN, in_min1, voltage_min[1]);
183 show(IN, in_min2, voltage_min[2]);
184 show(IN, in_min3, voltage_min[3]);
185 show(VDD, in_max0, voltage_max[0]);
186 show(IN, in_max1, voltage_max[1]);
187 show(IN, in_max2, voltage_max[2]);
188 show(IN, in_max3, voltage_max[3]);
189 show(RAW, alarms, alarms);
190 show(BOOL, beep_enable, beep_enable);
191 show(BEEP_MASK, beep_mask, beep_mask);
193 static ssize_t show_fan_input(struct device *dev,
194 struct device_attribute *attr, char *buf)
196 int nr = to_sensor_dev_attr(attr)->index;
197 struct gl518_data *data = gl518_update_device(dev);
198 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
199 DIV_FROM_REG(data->fan_div[nr])));
202 static ssize_t show_fan_min(struct device *dev,
203 struct device_attribute *attr, char *buf)
205 int nr = to_sensor_dev_attr(attr)->index;
206 struct gl518_data *data = gl518_update_device(dev);
207 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
208 DIV_FROM_REG(data->fan_div[nr])));
211 static ssize_t show_fan_div(struct device *dev,
212 struct device_attribute *attr, char *buf)
214 int nr = to_sensor_dev_attr(attr)->index;
215 struct gl518_data *data = gl518_update_device(dev);
216 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
219 #define set(type, suffix, value, reg) \
220 static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
221 size_t count) \
223 struct i2c_client *client = to_i2c_client(dev); \
224 struct gl518_data *data = i2c_get_clientdata(client); \
225 long val = simple_strtol(buf, NULL, 10); \
227 mutex_lock(&data->update_lock); \
228 data->value = type##_TO_REG(val); \
229 gl518_write_value(client, reg, data->value); \
230 mutex_unlock(&data->update_lock); \
231 return count; \
234 #define set_bits(type, suffix, value, reg, mask, shift) \
235 static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
236 size_t count) \
238 struct i2c_client *client = to_i2c_client(dev); \
239 struct gl518_data *data = i2c_get_clientdata(client); \
240 int regvalue; \
241 unsigned long val = simple_strtoul(buf, NULL, 10); \
243 mutex_lock(&data->update_lock); \
244 regvalue = gl518_read_value(client, reg); \
245 data->value = type##_TO_REG(val); \
246 regvalue = (regvalue & ~mask) | (data->value << shift); \
247 gl518_write_value(client, reg, regvalue); \
248 mutex_unlock(&data->update_lock); \
249 return count; \
252 #define set_low(type, suffix, value, reg) \
253 set_bits(type, suffix, value, reg, 0x00ff, 0)
254 #define set_high(type, suffix, value, reg) \
255 set_bits(type, suffix, value, reg, 0xff00, 8)
257 set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
258 set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
259 set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
260 set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
261 set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
262 set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
263 set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
264 set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
265 set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
266 set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
267 set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
268 set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
269 set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
271 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
272 const char *buf, size_t count)
274 struct i2c_client *client = to_i2c_client(dev);
275 struct gl518_data *data = i2c_get_clientdata(client);
276 int nr = to_sensor_dev_attr(attr)->index;
277 int regvalue;
278 unsigned long val = simple_strtoul(buf, NULL, 10);
280 mutex_lock(&data->update_lock);
281 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
282 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
283 regvalue = (regvalue & (0xff << (8 * nr)))
284 | (data->fan_min[nr] << (8 * (1 - nr)));
285 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
287 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
288 if (data->fan_min[nr] == 0)
289 data->alarm_mask &= ~(0x20 << nr);
290 else
291 data->alarm_mask |= (0x20 << nr);
292 data->beep_mask &= data->alarm_mask;
293 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
295 mutex_unlock(&data->update_lock);
296 return count;
299 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
300 const char *buf, size_t count)
302 struct i2c_client *client = to_i2c_client(dev);
303 struct gl518_data *data = i2c_get_clientdata(client);
304 int nr = to_sensor_dev_attr(attr)->index;
305 int regvalue;
306 unsigned long val = simple_strtoul(buf, NULL, 10);
308 switch (val) {
309 case 1: val = 0; break;
310 case 2: val = 1; break;
311 case 4: val = 2; break;
312 case 8: val = 3; break;
313 default:
314 dev_err(dev, "Invalid fan clock divider %lu, choose one "
315 "of 1, 2, 4 or 8\n", val);
316 return -EINVAL;
319 mutex_lock(&data->update_lock);
320 regvalue = gl518_read_value(client, GL518_REG_MISC);
321 data->fan_div[nr] = val;
322 regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
323 | (data->fan_div[nr] << (6 - 2 * nr));
324 gl518_write_value(client, GL518_REG_MISC, regvalue);
325 mutex_unlock(&data->update_lock);
326 return count;
329 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
330 static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
331 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
332 show_temp_hyst1, set_temp_hyst1);
333 static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
334 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
335 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
336 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO,
337 show_fan_min, set_fan_min, 0);
338 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO,
339 show_fan_min, set_fan_min, 1);
340 static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO,
341 show_fan_div, set_fan_div, 0);
342 static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO,
343 show_fan_div, set_fan_div, 1);
344 static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
345 static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
346 static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
347 static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
348 static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
349 static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
350 static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
351 static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
352 static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
353 static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
354 static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
355 static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
356 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
357 static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
358 show_beep_enable, set_beep_enable);
359 static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
360 show_beep_mask, set_beep_mask);
362 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
363 char *buf)
365 int bitnr = to_sensor_dev_attr(attr)->index;
366 struct gl518_data *data = gl518_update_device(dev);
367 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
370 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
371 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
372 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
373 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
374 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
375 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
376 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
378 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
379 char *buf)
381 int bitnr = to_sensor_dev_attr(attr)->index;
382 struct gl518_data *data = gl518_update_device(dev);
383 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
386 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
387 const char *buf, size_t count)
389 struct i2c_client *client = to_i2c_client(dev);
390 struct gl518_data *data = i2c_get_clientdata(client);
391 int bitnr = to_sensor_dev_attr(attr)->index;
392 unsigned long bit;
394 bit = simple_strtoul(buf, NULL, 10);
395 if (bit & ~1)
396 return -EINVAL;
398 mutex_lock(&data->update_lock);
399 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
400 if (bit)
401 data->beep_mask |= (1 << bitnr);
402 else
403 data->beep_mask &= ~(1 << bitnr);
404 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
405 mutex_unlock(&data->update_lock);
406 return count;
409 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
410 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
411 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
412 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
413 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
414 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
415 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
417 static struct attribute *gl518_attributes[] = {
418 &dev_attr_in3_input.attr,
419 &dev_attr_in0_min.attr,
420 &dev_attr_in1_min.attr,
421 &dev_attr_in2_min.attr,
422 &dev_attr_in3_min.attr,
423 &dev_attr_in0_max.attr,
424 &dev_attr_in1_max.attr,
425 &dev_attr_in2_max.attr,
426 &dev_attr_in3_max.attr,
427 &sensor_dev_attr_in0_alarm.dev_attr.attr,
428 &sensor_dev_attr_in1_alarm.dev_attr.attr,
429 &sensor_dev_attr_in2_alarm.dev_attr.attr,
430 &sensor_dev_attr_in3_alarm.dev_attr.attr,
431 &sensor_dev_attr_in0_beep.dev_attr.attr,
432 &sensor_dev_attr_in1_beep.dev_attr.attr,
433 &sensor_dev_attr_in2_beep.dev_attr.attr,
434 &sensor_dev_attr_in3_beep.dev_attr.attr,
436 &dev_attr_fan1_auto.attr,
437 &sensor_dev_attr_fan1_input.dev_attr.attr,
438 &sensor_dev_attr_fan2_input.dev_attr.attr,
439 &sensor_dev_attr_fan1_min.dev_attr.attr,
440 &sensor_dev_attr_fan2_min.dev_attr.attr,
441 &sensor_dev_attr_fan1_div.dev_attr.attr,
442 &sensor_dev_attr_fan2_div.dev_attr.attr,
443 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
444 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
445 &sensor_dev_attr_fan1_beep.dev_attr.attr,
446 &sensor_dev_attr_fan2_beep.dev_attr.attr,
448 &dev_attr_temp1_input.attr,
449 &dev_attr_temp1_max.attr,
450 &dev_attr_temp1_max_hyst.attr,
451 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
452 &sensor_dev_attr_temp1_beep.dev_attr.attr,
454 &dev_attr_alarms.attr,
455 &dev_attr_beep_enable.attr,
456 &dev_attr_beep_mask.attr,
457 NULL
460 static const struct attribute_group gl518_group = {
461 .attrs = gl518_attributes,
464 static struct attribute *gl518_attributes_r80[] = {
465 &dev_attr_in0_input.attr,
466 &dev_attr_in1_input.attr,
467 &dev_attr_in2_input.attr,
468 NULL
471 static const struct attribute_group gl518_group_r80 = {
472 .attrs = gl518_attributes_r80,
476 * Real code
479 static int gl518_attach_adapter(struct i2c_adapter *adapter)
481 if (!(adapter->class & I2C_CLASS_HWMON))
482 return 0;
483 return i2c_probe(adapter, &addr_data, gl518_detect);
486 static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
488 int i;
489 struct i2c_client *client;
490 struct gl518_data *data;
491 int err = 0;
493 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
494 I2C_FUNC_SMBUS_WORD_DATA))
495 goto exit;
497 /* OK. For now, we presume we have a valid client. We now create the
498 client structure, even though we cannot fill it completely yet.
499 But it allows us to access gl518_{read,write}_value. */
501 if (!(data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
502 err = -ENOMEM;
503 goto exit;
506 client = &data->client;
507 i2c_set_clientdata(client, data);
509 client->addr = address;
510 client->adapter = adapter;
511 client->driver = &gl518_driver;
513 /* Now, we do the remaining detection. */
515 if (kind < 0) {
516 if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
517 || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
518 goto exit_free;
521 /* Determine the chip type. */
522 if (kind <= 0) {
523 i = gl518_read_value(client, GL518_REG_REVISION);
524 if (i == 0x00) {
525 kind = gl518sm_r00;
526 } else if (i == 0x80) {
527 kind = gl518sm_r80;
528 } else {
529 if (kind <= 0)
530 dev_info(&adapter->dev,
531 "Ignoring 'force' parameter for unknown "
532 "chip at adapter %d, address 0x%02x\n",
533 i2c_adapter_id(adapter), address);
534 goto exit_free;
538 /* Fill in the remaining client fields */
539 strlcpy(client->name, "gl518sm", I2C_NAME_SIZE);
540 data->type = kind;
541 mutex_init(&data->update_lock);
543 /* Tell the I2C layer a new client has arrived */
544 if ((err = i2c_attach_client(client)))
545 goto exit_free;
547 /* Initialize the GL518SM chip */
548 data->alarm_mask = 0xff;
549 gl518_init_client(client);
551 /* Register sysfs hooks */
552 if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group)))
553 goto exit_detach;
554 if (data->type == gl518sm_r80)
555 if ((err = sysfs_create_group(&client->dev.kobj,
556 &gl518_group_r80)))
557 goto exit_remove_files;
559 data->hwmon_dev = hwmon_device_register(&client->dev);
560 if (IS_ERR(data->hwmon_dev)) {
561 err = PTR_ERR(data->hwmon_dev);
562 goto exit_remove_files;
565 return 0;
567 exit_remove_files:
568 sysfs_remove_group(&client->dev.kobj, &gl518_group);
569 if (data->type == gl518sm_r80)
570 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
571 exit_detach:
572 i2c_detach_client(client);
573 exit_free:
574 kfree(data);
575 exit:
576 return err;
580 /* Called when we have found a new GL518SM.
581 Note that we preserve D4:NoFan2 and D2:beep_enable. */
582 static void gl518_init_client(struct i2c_client *client)
584 /* Make sure we leave D7:Reset untouched */
585 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
587 /* Comparator mode (D3=0), standby mode (D6=0) */
588 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
590 /* Never interrupts */
591 gl518_write_value(client, GL518_REG_MASK, 0x00);
593 /* Clear status register (D5=1), start (D6=1) */
594 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
595 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
598 static int gl518_detach_client(struct i2c_client *client)
600 struct gl518_data *data = i2c_get_clientdata(client);
601 int err;
603 hwmon_device_unregister(data->hwmon_dev);
604 sysfs_remove_group(&client->dev.kobj, &gl518_group);
605 if (data->type == gl518sm_r80)
606 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
608 if ((err = i2c_detach_client(client)))
609 return err;
611 kfree(data);
612 return 0;
615 /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
616 GL518 uses a high-byte first convention, which is exactly opposite to
617 the SMBus standard. */
618 static int gl518_read_value(struct i2c_client *client, u8 reg)
620 if ((reg >= 0x07) && (reg <= 0x0c))
621 return swab16(i2c_smbus_read_word_data(client, reg));
622 else
623 return i2c_smbus_read_byte_data(client, reg);
626 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
628 if ((reg >= 0x07) && (reg <= 0x0c))
629 return i2c_smbus_write_word_data(client, reg, swab16(value));
630 else
631 return i2c_smbus_write_byte_data(client, reg, value);
634 static struct gl518_data *gl518_update_device(struct device *dev)
636 struct i2c_client *client = to_i2c_client(dev);
637 struct gl518_data *data = i2c_get_clientdata(client);
638 int val;
640 mutex_lock(&data->update_lock);
642 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
643 || !data->valid) {
644 dev_dbg(&client->dev, "Starting gl518 update\n");
646 data->alarms = gl518_read_value(client, GL518_REG_INT);
647 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
649 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
650 data->voltage_min[0] = val & 0xff;
651 data->voltage_max[0] = (val >> 8) & 0xff;
652 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
653 data->voltage_min[1] = val & 0xff;
654 data->voltage_max[1] = (val >> 8) & 0xff;
655 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
656 data->voltage_min[2] = val & 0xff;
657 data->voltage_max[2] = (val >> 8) & 0xff;
658 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
659 data->voltage_min[3] = val & 0xff;
660 data->voltage_max[3] = (val >> 8) & 0xff;
662 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
663 data->fan_in[0] = (val >> 8) & 0xff;
664 data->fan_in[1] = val & 0xff;
666 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
667 data->fan_min[0] = (val >> 8) & 0xff;
668 data->fan_min[1] = val & 0xff;
670 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
671 data->temp_max =
672 gl518_read_value(client, GL518_REG_TEMP_MAX);
673 data->temp_hyst =
674 gl518_read_value(client, GL518_REG_TEMP_HYST);
676 val = gl518_read_value(client, GL518_REG_MISC);
677 data->fan_div[0] = (val >> 6) & 0x03;
678 data->fan_div[1] = (val >> 4) & 0x03;
679 data->fan_auto1 = (val >> 3) & 0x01;
681 data->alarms &= data->alarm_mask;
683 val = gl518_read_value(client, GL518_REG_CONF);
684 data->beep_enable = (val >> 2) & 1;
686 if (data->type != gl518sm_r00) {
687 data->voltage_in[0] =
688 gl518_read_value(client, GL518_REG_VDD);
689 data->voltage_in[1] =
690 gl518_read_value(client, GL518_REG_VIN1);
691 data->voltage_in[2] =
692 gl518_read_value(client, GL518_REG_VIN2);
694 data->voltage_in[3] =
695 gl518_read_value(client, GL518_REG_VIN3);
697 data->last_updated = jiffies;
698 data->valid = 1;
701 mutex_unlock(&data->update_lock);
703 return data;
706 static int __init sensors_gl518sm_init(void)
708 return i2c_add_driver(&gl518_driver);
711 static void __exit sensors_gl518sm_exit(void)
713 i2c_del_driver(&gl518_driver);
716 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
717 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
718 "Hong-Gunn Chew <hglinux@gunnet.org>");
719 MODULE_DESCRIPTION("GL518SM driver");
720 MODULE_LICENSE("GPL");
722 module_init(sensors_gl518sm_init);
723 module_exit(sensors_gl518sm_exit);