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