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