]>
Commit | Line | Data |
---|---|---|
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 MH |
40 | #include <linux/hwmon.h> |
41 | #include <linux/err.h> | |
9a61bf63 | 42 | #include <linux/mutex.h> |
87808be4 | 43 | #include <linux/sysfs.h> |
1da177e4 LT |
44 | |
45 | /* Addresses to scan */ | |
46 | static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; | |
1da177e4 LT |
47 | |
48 | /* Insmod parameters */ | |
f4b50261 | 49 | I2C_CLIENT_INSMOD_2(gl518sm_r00, gl518sm_r80); |
1da177e4 LT |
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_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \ | |
90 | (val)-500:(val)+500)/1000)+119),0,255)) | |
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 = SENSORS_LIMIT(rpm, 1, 1920000) * div; | |
99 | return SENSORS_LIMIT((960000 + rpmdiv / 2) / rpmdiv, 1, 255); | |
100 | } | |
101 | #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (960000/((val)*(div)))) | |
102 | ||
103 | #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255)) | |
104 | #define IN_FROM_REG(val) ((val)*19) | |
105 | ||
106 | #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255)) | |
107 | #define VDD_FROM_REG(val) (((val)*95+2)/4) | |
108 | ||
109 | #define DIV_TO_REG(val) ((val)==4?2:(val)==2?1:(val)==1?0:3) | |
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 */ | |
116 | struct gl518_data { | |
117 | struct i2c_client client; | |
1beeffe4 | 118 | struct device *hwmon_dev; |
1da177e4 LT |
119 | enum chips type; |
120 | ||
9a61bf63 | 121 | struct mutex update_lock; |
1da177e4 LT |
122 | char valid; /* !=0 if following fields are valid */ |
123 | unsigned long last_updated; /* In jiffies */ | |
124 | ||
125 | u8 voltage_in[4]; /* Register values; [0] = VDD */ | |
126 | u8 voltage_min[4]; /* Register values; [0] = VDD */ | |
127 | u8 voltage_max[4]; /* Register values; [0] = VDD */ | |
1da177e4 LT |
128 | u8 fan_in[2]; |
129 | u8 fan_min[2]; | |
130 | u8 fan_div[2]; /* Register encoding, shifted right */ | |
131 | u8 fan_auto1; /* Boolean */ | |
132 | u8 temp_in; /* Register values */ | |
133 | u8 temp_max; /* Register values */ | |
134 | u8 temp_hyst; /* Register values */ | |
135 | u8 alarms; /* Register value */ | |
a5955ed2 | 136 | u8 alarm_mask; |
1da177e4 LT |
137 | u8 beep_mask; /* Register value */ |
138 | u8 beep_enable; /* Boolean */ | |
139 | }; | |
140 | ||
141 | static int gl518_attach_adapter(struct i2c_adapter *adapter); | |
142 | static int gl518_detect(struct i2c_adapter *adapter, int address, int kind); | |
143 | static void gl518_init_client(struct i2c_client *client); | |
144 | static int gl518_detach_client(struct i2c_client *client); | |
145 | static int gl518_read_value(struct i2c_client *client, u8 reg); | |
146 | static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value); | |
147 | static struct gl518_data *gl518_update_device(struct device *dev); | |
148 | ||
149 | /* This is the driver that will be inserted */ | |
150 | static struct i2c_driver gl518_driver = { | |
cdaf7934 | 151 | .driver = { |
cdaf7934 LR |
152 | .name = "gl518sm", |
153 | }, | |
1da177e4 LT |
154 | .attach_adapter = gl518_attach_adapter, |
155 | .detach_client = gl518_detach_client, | |
156 | }; | |
157 | ||
158 | /* | |
159 | * Sysfs stuff | |
160 | */ | |
161 | ||
162 | #define show(type, suffix, value) \ | |
30f74292 | 163 | static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
164 | { \ |
165 | struct gl518_data *data = gl518_update_device(dev); \ | |
166 | return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \ | |
167 | } | |
168 | ||
169 | #define show_fan(suffix, value, index) \ | |
30f74292 | 170 | static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
171 | { \ |
172 | struct gl518_data *data = gl518_update_device(dev); \ | |
173 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[index], \ | |
174 | DIV_FROM_REG(data->fan_div[index]))); \ | |
175 | } | |
176 | ||
177 | show(TEMP, temp_input1, temp_in); | |
178 | show(TEMP, temp_max1, temp_max); | |
179 | show(TEMP, temp_hyst1, temp_hyst); | |
180 | show(BOOL, fan_auto1, fan_auto1); | |
181 | show_fan(fan_input1, fan_in, 0); | |
182 | show_fan(fan_input2, fan_in, 1); | |
183 | show_fan(fan_min1, fan_min, 0); | |
184 | show_fan(fan_min2, fan_min, 1); | |
185 | show(DIV, fan_div1, fan_div[0]); | |
186 | show(DIV, fan_div2, fan_div[1]); | |
187 | show(VDD, in_input0, voltage_in[0]); | |
188 | show(IN, in_input1, voltage_in[1]); | |
189 | show(IN, in_input2, voltage_in[2]); | |
190 | show(IN, in_input3, voltage_in[3]); | |
191 | show(VDD, in_min0, voltage_min[0]); | |
192 | show(IN, in_min1, voltage_min[1]); | |
193 | show(IN, in_min2, voltage_min[2]); | |
194 | show(IN, in_min3, voltage_min[3]); | |
195 | show(VDD, in_max0, voltage_max[0]); | |
196 | show(IN, in_max1, voltage_max[1]); | |
197 | show(IN, in_max2, voltage_max[2]); | |
198 | show(IN, in_max3, voltage_max[3]); | |
199 | show(RAW, alarms, alarms); | |
200 | show(BOOL, beep_enable, beep_enable); | |
201 | show(BEEP_MASK, beep_mask, beep_mask); | |
202 | ||
203 | #define set(type, suffix, value, reg) \ | |
30f74292 | 204 | static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \ |
1da177e4 LT |
205 | size_t count) \ |
206 | { \ | |
207 | struct i2c_client *client = to_i2c_client(dev); \ | |
208 | struct gl518_data *data = i2c_get_clientdata(client); \ | |
209 | long val = simple_strtol(buf, NULL, 10); \ | |
210 | \ | |
9a61bf63 | 211 | mutex_lock(&data->update_lock); \ |
1da177e4 LT |
212 | data->value = type##_TO_REG(val); \ |
213 | gl518_write_value(client, reg, data->value); \ | |
9a61bf63 | 214 | mutex_unlock(&data->update_lock); \ |
1da177e4 LT |
215 | return count; \ |
216 | } | |
217 | ||
218 | #define set_bits(type, suffix, value, reg, mask, shift) \ | |
30f74292 | 219 | static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \ |
1da177e4 LT |
220 | size_t count) \ |
221 | { \ | |
222 | struct i2c_client *client = to_i2c_client(dev); \ | |
223 | struct gl518_data *data = i2c_get_clientdata(client); \ | |
224 | int regvalue; \ | |
225 | unsigned long val = simple_strtoul(buf, NULL, 10); \ | |
226 | \ | |
9a61bf63 | 227 | mutex_lock(&data->update_lock); \ |
1da177e4 LT |
228 | regvalue = gl518_read_value(client, reg); \ |
229 | data->value = type##_TO_REG(val); \ | |
230 | regvalue = (regvalue & ~mask) | (data->value << shift); \ | |
231 | gl518_write_value(client, reg, regvalue); \ | |
9a61bf63 | 232 | mutex_unlock(&data->update_lock); \ |
1da177e4 LT |
233 | return count; \ |
234 | } | |
235 | ||
236 | #define set_low(type, suffix, value, reg) \ | |
237 | set_bits(type, suffix, value, reg, 0x00ff, 0) | |
238 | #define set_high(type, suffix, value, reg) \ | |
239 | set_bits(type, suffix, value, reg, 0xff00, 8) | |
240 | ||
241 | set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX); | |
242 | set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST); | |
243 | set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3); | |
244 | set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0xc0, 6); | |
245 | set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0x30, 4); | |
246 | set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT); | |
247 | set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT); | |
248 | set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT); | |
249 | set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT); | |
250 | set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT); | |
251 | set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT); | |
252 | set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT); | |
253 | set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT); | |
254 | set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2); | |
255 | set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM); | |
256 | ||
30f74292 | 257 | static ssize_t set_fan_min1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
258 | { |
259 | struct i2c_client *client = to_i2c_client(dev); | |
260 | struct gl518_data *data = i2c_get_clientdata(client); | |
261 | int regvalue; | |
262 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
263 | ||
9a61bf63 | 264 | mutex_lock(&data->update_lock); |
1da177e4 LT |
265 | regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); |
266 | data->fan_min[0] = FAN_TO_REG(val, | |
267 | DIV_FROM_REG(data->fan_div[0])); | |
268 | regvalue = (regvalue & 0x00ff) | (data->fan_min[0] << 8); | |
269 | gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); | |
270 | ||
271 | data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); | |
272 | if (data->fan_min[0] == 0) | |
273 | data->alarm_mask &= ~0x20; | |
274 | else | |
275 | data->alarm_mask |= 0x20; | |
276 | data->beep_mask &= data->alarm_mask; | |
277 | gl518_write_value(client, GL518_REG_ALARM, data->beep_mask); | |
278 | ||
9a61bf63 | 279 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
280 | return count; |
281 | } | |
282 | ||
30f74292 | 283 | static ssize_t set_fan_min2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
284 | { |
285 | struct i2c_client *client = to_i2c_client(dev); | |
286 | struct gl518_data *data = i2c_get_clientdata(client); | |
287 | int regvalue; | |
288 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
289 | ||
9a61bf63 | 290 | mutex_lock(&data->update_lock); |
1da177e4 LT |
291 | regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); |
292 | data->fan_min[1] = FAN_TO_REG(val, | |
293 | DIV_FROM_REG(data->fan_div[1])); | |
294 | regvalue = (regvalue & 0xff00) | data->fan_min[1]; | |
295 | gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); | |
296 | ||
297 | data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); | |
298 | if (data->fan_min[1] == 0) | |
299 | data->alarm_mask &= ~0x40; | |
300 | else | |
301 | data->alarm_mask |= 0x40; | |
302 | data->beep_mask &= data->alarm_mask; | |
303 | gl518_write_value(client, GL518_REG_ALARM, data->beep_mask); | |
304 | ||
9a61bf63 | 305 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
306 | return count; |
307 | } | |
308 | ||
309 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL); | |
310 | static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1); | |
311 | static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO, | |
312 | show_temp_hyst1, set_temp_hyst1); | |
313 | static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1); | |
314 | static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL); | |
315 | static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL); | |
316 | static DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1); | |
317 | static DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2); | |
318 | static DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1); | |
319 | static DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2); | |
320 | static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL); | |
321 | static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL); | |
322 | static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL); | |
323 | static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL); | |
324 | static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0); | |
325 | static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1); | |
326 | static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2); | |
327 | static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3); | |
328 | static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0); | |
329 | static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1); | |
330 | static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2); | |
331 | static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3); | |
332 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | |
333 | static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO, | |
334 | show_beep_enable, set_beep_enable); | |
335 | static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO, | |
336 | show_beep_mask, set_beep_mask); | |
337 | ||
87808be4 JD |
338 | static struct attribute *gl518_attributes[] = { |
339 | &dev_attr_in0_input.attr, | |
340 | &dev_attr_in1_input.attr, | |
341 | &dev_attr_in2_input.attr, | |
342 | &dev_attr_in3_input.attr, | |
343 | &dev_attr_in0_min.attr, | |
344 | &dev_attr_in1_min.attr, | |
345 | &dev_attr_in2_min.attr, | |
346 | &dev_attr_in3_min.attr, | |
347 | &dev_attr_in0_max.attr, | |
348 | &dev_attr_in1_max.attr, | |
349 | &dev_attr_in2_max.attr, | |
350 | &dev_attr_in3_max.attr, | |
351 | ||
352 | &dev_attr_fan1_auto.attr, | |
353 | &dev_attr_fan1_input.attr, | |
354 | &dev_attr_fan2_input.attr, | |
355 | &dev_attr_fan1_min.attr, | |
356 | &dev_attr_fan2_min.attr, | |
357 | &dev_attr_fan1_div.attr, | |
358 | &dev_attr_fan2_div.attr, | |
359 | ||
360 | &dev_attr_temp1_input.attr, | |
361 | &dev_attr_temp1_max.attr, | |
362 | &dev_attr_temp1_max_hyst.attr, | |
363 | ||
364 | &dev_attr_alarms.attr, | |
365 | &dev_attr_beep_enable.attr, | |
366 | &dev_attr_beep_mask.attr, | |
367 | NULL | |
368 | }; | |
369 | ||
370 | static const struct attribute_group gl518_group = { | |
371 | .attrs = gl518_attributes, | |
372 | }; | |
373 | ||
1da177e4 LT |
374 | /* |
375 | * Real code | |
376 | */ | |
377 | ||
378 | static int gl518_attach_adapter(struct i2c_adapter *adapter) | |
379 | { | |
380 | if (!(adapter->class & I2C_CLASS_HWMON)) | |
381 | return 0; | |
2ed2dc3c | 382 | return i2c_probe(adapter, &addr_data, gl518_detect); |
1da177e4 LT |
383 | } |
384 | ||
385 | static int gl518_detect(struct i2c_adapter *adapter, int address, int kind) | |
386 | { | |
387 | int i; | |
a5955ed2 | 388 | struct i2c_client *client; |
1da177e4 LT |
389 | struct gl518_data *data; |
390 | int err = 0; | |
391 | ||
392 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | | |
393 | I2C_FUNC_SMBUS_WORD_DATA)) | |
394 | goto exit; | |
395 | ||
396 | /* OK. For now, we presume we have a valid client. We now create the | |
397 | client structure, even though we cannot fill it completely yet. | |
398 | But it allows us to access gl518_{read,write}_value. */ | |
399 | ||
ba9c2e8d | 400 | if (!(data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL))) { |
1da177e4 LT |
401 | err = -ENOMEM; |
402 | goto exit; | |
403 | } | |
1da177e4 | 404 | |
a5955ed2 JD |
405 | client = &data->client; |
406 | i2c_set_clientdata(client, data); | |
1da177e4 | 407 | |
a5955ed2 JD |
408 | client->addr = address; |
409 | client->adapter = adapter; | |
410 | client->driver = &gl518_driver; | |
1da177e4 LT |
411 | |
412 | /* Now, we do the remaining detection. */ | |
413 | ||
414 | if (kind < 0) { | |
a5955ed2 JD |
415 | if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80) |
416 | || (gl518_read_value(client, GL518_REG_CONF) & 0x80)) | |
1da177e4 LT |
417 | goto exit_free; |
418 | } | |
419 | ||
420 | /* Determine the chip type. */ | |
421 | if (kind <= 0) { | |
a5955ed2 | 422 | i = gl518_read_value(client, GL518_REG_REVISION); |
1da177e4 LT |
423 | if (i == 0x00) { |
424 | kind = gl518sm_r00; | |
425 | } else if (i == 0x80) { | |
426 | kind = gl518sm_r80; | |
427 | } else { | |
428 | if (kind <= 0) | |
429 | dev_info(&adapter->dev, | |
430 | "Ignoring 'force' parameter for unknown " | |
431 | "chip at adapter %d, address 0x%02x\n", | |
432 | i2c_adapter_id(adapter), address); | |
433 | goto exit_free; | |
434 | } | |
435 | } | |
436 | ||
437 | /* Fill in the remaining client fields */ | |
a5955ed2 | 438 | strlcpy(client->name, "gl518sm", I2C_NAME_SIZE); |
1da177e4 | 439 | data->type = kind; |
9a61bf63 | 440 | mutex_init(&data->update_lock); |
1da177e4 LT |
441 | |
442 | /* Tell the I2C layer a new client has arrived */ | |
a5955ed2 | 443 | if ((err = i2c_attach_client(client))) |
1da177e4 LT |
444 | goto exit_free; |
445 | ||
446 | /* Initialize the GL518SM chip */ | |
447 | data->alarm_mask = 0xff; | |
448 | data->voltage_in[0]=data->voltage_in[1]=data->voltage_in[2]=0; | |
a5955ed2 | 449 | gl518_init_client(client); |
1da177e4 LT |
450 | |
451 | /* Register sysfs hooks */ | |
a5955ed2 | 452 | if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group))) |
87808be4 JD |
453 | goto exit_detach; |
454 | ||
a5955ed2 | 455 | data->hwmon_dev = hwmon_device_register(&client->dev); |
1beeffe4 TJ |
456 | if (IS_ERR(data->hwmon_dev)) { |
457 | err = PTR_ERR(data->hwmon_dev); | |
87808be4 | 458 | goto exit_remove_files; |
943b0830 MH |
459 | } |
460 | ||
1da177e4 LT |
461 | return 0; |
462 | ||
87808be4 | 463 | exit_remove_files: |
a5955ed2 | 464 | sysfs_remove_group(&client->dev.kobj, &gl518_group); |
943b0830 | 465 | exit_detach: |
a5955ed2 | 466 | i2c_detach_client(client); |
1da177e4 LT |
467 | exit_free: |
468 | kfree(data); | |
469 | exit: | |
470 | return err; | |
471 | } | |
472 | ||
473 | ||
474 | /* Called when we have found a new GL518SM. | |
475 | Note that we preserve D4:NoFan2 and D2:beep_enable. */ | |
476 | static void gl518_init_client(struct i2c_client *client) | |
477 | { | |
478 | /* Make sure we leave D7:Reset untouched */ | |
479 | u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f; | |
480 | ||
481 | /* Comparator mode (D3=0), standby mode (D6=0) */ | |
482 | gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37)); | |
483 | ||
484 | /* Never interrupts */ | |
485 | gl518_write_value(client, GL518_REG_MASK, 0x00); | |
486 | ||
487 | /* Clear status register (D5=1), start (D6=1) */ | |
488 | gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue); | |
489 | gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue); | |
490 | } | |
491 | ||
492 | static int gl518_detach_client(struct i2c_client *client) | |
493 | { | |
943b0830 | 494 | struct gl518_data *data = i2c_get_clientdata(client); |
1da177e4 LT |
495 | int err; |
496 | ||
1beeffe4 | 497 | hwmon_device_unregister(data->hwmon_dev); |
87808be4 | 498 | sysfs_remove_group(&client->dev.kobj, &gl518_group); |
943b0830 | 499 | |
7bef5594 | 500 | if ((err = i2c_detach_client(client))) |
1da177e4 | 501 | return err; |
1da177e4 | 502 | |
943b0830 | 503 | kfree(data); |
1da177e4 LT |
504 | return 0; |
505 | } | |
506 | ||
a5955ed2 | 507 | /* Registers 0x07 to 0x0c are word-sized, others are byte-sized |
1da177e4 | 508 | GL518 uses a high-byte first convention, which is exactly opposite to |
a5955ed2 | 509 | the SMBus standard. */ |
1da177e4 LT |
510 | static int gl518_read_value(struct i2c_client *client, u8 reg) |
511 | { | |
512 | if ((reg >= 0x07) && (reg <= 0x0c)) | |
513 | return swab16(i2c_smbus_read_word_data(client, reg)); | |
514 | else | |
515 | return i2c_smbus_read_byte_data(client, reg); | |
516 | } | |
517 | ||
1da177e4 LT |
518 | static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value) |
519 | { | |
520 | if ((reg >= 0x07) && (reg <= 0x0c)) | |
521 | return i2c_smbus_write_word_data(client, reg, swab16(value)); | |
522 | else | |
523 | return i2c_smbus_write_byte_data(client, reg, value); | |
524 | } | |
525 | ||
526 | static struct gl518_data *gl518_update_device(struct device *dev) | |
527 | { | |
528 | struct i2c_client *client = to_i2c_client(dev); | |
529 | struct gl518_data *data = i2c_get_clientdata(client); | |
530 | int val; | |
531 | ||
9a61bf63 | 532 | mutex_lock(&data->update_lock); |
1da177e4 LT |
533 | |
534 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
535 | || !data->valid) { | |
536 | dev_dbg(&client->dev, "Starting gl518 update\n"); | |
537 | ||
538 | data->alarms = gl518_read_value(client, GL518_REG_INT); | |
539 | data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); | |
540 | ||
541 | val = gl518_read_value(client, GL518_REG_VDD_LIMIT); | |
542 | data->voltage_min[0] = val & 0xff; | |
543 | data->voltage_max[0] = (val >> 8) & 0xff; | |
544 | val = gl518_read_value(client, GL518_REG_VIN1_LIMIT); | |
545 | data->voltage_min[1] = val & 0xff; | |
546 | data->voltage_max[1] = (val >> 8) & 0xff; | |
547 | val = gl518_read_value(client, GL518_REG_VIN2_LIMIT); | |
548 | data->voltage_min[2] = val & 0xff; | |
549 | data->voltage_max[2] = (val >> 8) & 0xff; | |
550 | val = gl518_read_value(client, GL518_REG_VIN3_LIMIT); | |
551 | data->voltage_min[3] = val & 0xff; | |
552 | data->voltage_max[3] = (val >> 8) & 0xff; | |
553 | ||
554 | val = gl518_read_value(client, GL518_REG_FAN_COUNT); | |
555 | data->fan_in[0] = (val >> 8) & 0xff; | |
556 | data->fan_in[1] = val & 0xff; | |
557 | ||
558 | val = gl518_read_value(client, GL518_REG_FAN_LIMIT); | |
559 | data->fan_min[0] = (val >> 8) & 0xff; | |
560 | data->fan_min[1] = val & 0xff; | |
561 | ||
562 | data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN); | |
563 | data->temp_max = | |
564 | gl518_read_value(client, GL518_REG_TEMP_MAX); | |
565 | data->temp_hyst = | |
566 | gl518_read_value(client, GL518_REG_TEMP_HYST); | |
567 | ||
568 | val = gl518_read_value(client, GL518_REG_MISC); | |
569 | data->fan_div[0] = (val >> 6) & 0x03; | |
570 | data->fan_div[1] = (val >> 4) & 0x03; | |
571 | data->fan_auto1 = (val >> 3) & 0x01; | |
572 | ||
573 | data->alarms &= data->alarm_mask; | |
574 | ||
575 | val = gl518_read_value(client, GL518_REG_CONF); | |
576 | data->beep_enable = (val >> 2) & 1; | |
577 | ||
578 | if (data->type != gl518sm_r00) { | |
579 | data->voltage_in[0] = | |
580 | gl518_read_value(client, GL518_REG_VDD); | |
581 | data->voltage_in[1] = | |
582 | gl518_read_value(client, GL518_REG_VIN1); | |
583 | data->voltage_in[2] = | |
584 | gl518_read_value(client, GL518_REG_VIN2); | |
585 | } | |
586 | data->voltage_in[3] = | |
587 | gl518_read_value(client, GL518_REG_VIN3); | |
588 | ||
589 | data->last_updated = jiffies; | |
590 | data->valid = 1; | |
591 | } | |
592 | ||
9a61bf63 | 593 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
594 | |
595 | return data; | |
596 | } | |
597 | ||
598 | static int __init sensors_gl518sm_init(void) | |
599 | { | |
600 | return i2c_add_driver(&gl518_driver); | |
601 | } | |
602 | ||
603 | static void __exit sensors_gl518sm_exit(void) | |
604 | { | |
605 | i2c_del_driver(&gl518_driver); | |
606 | } | |
607 | ||
608 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " | |
609 | "Kyosti Malkki <kmalkki@cc.hut.fi> and " | |
610 | "Hong-Gunn Chew <hglinux@gunnet.org>"); | |
611 | MODULE_DESCRIPTION("GL518SM driver"); | |
612 | MODULE_LICENSE("GPL"); | |
613 | ||
614 | module_init(sensors_gl518sm_init); | |
615 | module_exit(sensors_gl518sm_exit); |