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