1 /***************************************************************************
2 * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
3 * Copyright (C) 2007 by Hans de Goede <j.w.r.degoede@hhs.nl> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/platform_device.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
32 #define DRVNAME "f71882fg"
34 #define SIO_F71882FG_LD_HWM 0x04 /* Hardware monitor logical device */
35 #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
36 #define SIO_LOCK_KEY 0xAA /* Key to diasble Super-I/O */
38 #define SIO_REG_LDSEL 0x07 /* Logical device select */
39 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
40 #define SIO_REG_DEVREV 0x22 /* Device revision */
41 #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
42 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
43 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
45 #define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
46 #define SIO_F71882_ID 0x0541 /* Chipset ID */
48 #define REGION_LENGTH 8
49 #define ADDR_REG_OFFSET 5
50 #define DATA_REG_OFFSET 6
52 #define F71882FG_REG_PECI 0x0A
54 #define F71882FG_REG_IN_STATUS 0x12
55 #define F71882FG_REG_IN_BEEP 0x13
56 #define F71882FG_REG_IN(nr) (0x20 + (nr))
57 #define F71882FG_REG_IN1_HIGH 0x32
59 #define F71882FG_REG_FAN(nr) (0xA0 + (16 * (nr)))
60 #define F71882FG_REG_FAN_TARGET(nr) (0xA2 + (16 * (nr)))
61 #define F71882FG_REG_FAN_FULL_SPEED(nr) (0xA4 + (16 * (nr)))
62 #define F71882FG_REG_FAN_STATUS 0x92
63 #define F71882FG_REG_FAN_BEEP 0x93
65 #define F71882FG_REG_TEMP(nr) (0x72 + 2 * (nr))
66 #define F71882FG_REG_TEMP_OVT(nr) (0x82 + 2 * (nr))
67 #define F71882FG_REG_TEMP_HIGH(nr) (0x83 + 2 * (nr))
68 #define F71882FG_REG_TEMP_STATUS 0x62
69 #define F71882FG_REG_TEMP_BEEP 0x63
70 #define F71882FG_REG_TEMP_HYST1 0x6C
71 #define F71882FG_REG_TEMP_HYST23 0x6D
72 #define F71882FG_REG_TEMP_TYPE 0x6B
73 #define F71882FG_REG_TEMP_DIODE_OPEN 0x6F
75 #define F71882FG_REG_PWM(nr) (0xA3 + (16 * (nr)))
76 #define F71882FG_REG_PWM_TYPE 0x94
77 #define F71882FG_REG_PWM_ENABLE 0x96
79 #define F71882FG_REG_FAN_HYST0 0x98
80 #define F71882FG_REG_FAN_HYST1 0x99
82 #define F71882FG_REG_POINT_PWM(pwm, point) (0xAA + (point) + (16 * (pwm)))
83 #define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm)))
84 #define F71882FG_REG_POINT_MAPPING(nr) (0xAF + 16 * (nr))
86 #define F71882FG_REG_START 0x01
88 #define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */
90 static unsigned short force_id;
91 module_param(force_id, ushort, 0);
92 MODULE_PARM_DESC(force_id, "Override the detected device ID");
94 static int fan_mode[4] = { 0, 0, 0, 0 };
95 module_param_array(fan_mode, int, NULL, 0644);
96 MODULE_PARM_DESC(fan_mode, "List of fan control modes (f71882fg only) "
97 "(0=don't change, 1=pwm, 2=rpm)\n"
98 "Note: this needs a write to pwm#_enable to take effect");
100 static struct platform_device *f71882fg_pdev;
102 /* Super-I/O Function prototypes */
103 static inline int superio_inb(int base, int reg);
104 static inline int superio_inw(int base, int reg);
105 static inline void superio_enter(int base);
106 static inline void superio_select(int base, int ld);
107 static inline void superio_exit(int base);
109 struct f71882fg_data {
111 struct device *hwmon_dev;
113 struct mutex update_lock;
114 char valid; /* !=0 if following fields are valid */
115 unsigned long last_updated; /* In jiffies */
116 unsigned long last_limits; /* In jiffies */
118 /* Register Values */
125 u16 fan_full_speed[4];
138 u8 pwm_auto_point_hyst[2];
139 u8 pwm_auto_point_mapping[4];
140 u8 pwm_auto_point_pwm[4][5];
141 u8 pwm_auto_point_temp[4][4];
145 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
147 static ssize_t show_in_max(struct device *dev, struct device_attribute
148 *devattr, char *buf);
149 static ssize_t store_in_max(struct device *dev, struct device_attribute
150 *devattr, const char *buf, size_t count);
151 static ssize_t show_in_beep(struct device *dev, struct device_attribute
152 *devattr, char *buf);
153 static ssize_t store_in_beep(struct device *dev, struct device_attribute
154 *devattr, const char *buf, size_t count);
155 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
156 *devattr, char *buf);
158 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
160 static ssize_t show_fan_full_speed(struct device *dev,
161 struct device_attribute *devattr, char *buf);
162 static ssize_t store_fan_full_speed(struct device *dev,
163 struct device_attribute *devattr, const char *buf, size_t count);
164 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
165 *devattr, char *buf);
166 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
167 *devattr, const char *buf, size_t count);
168 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
169 *devattr, char *buf);
171 static ssize_t show_temp(struct device *dev, struct device_attribute
172 *devattr, char *buf);
173 static ssize_t show_temp_max(struct device *dev, struct device_attribute
174 *devattr, char *buf);
175 static ssize_t store_temp_max(struct device *dev, struct device_attribute
176 *devattr, const char *buf, size_t count);
177 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
178 *devattr, char *buf);
179 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
180 *devattr, const char *buf, size_t count);
181 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
182 *devattr, char *buf);
183 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
184 *devattr, const char *buf, size_t count);
185 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
186 *devattr, char *buf);
187 static ssize_t show_temp_type(struct device *dev, struct device_attribute
188 *devattr, char *buf);
189 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
190 *devattr, char *buf);
191 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
192 *devattr, const char *buf, size_t count);
193 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
194 *devattr, char *buf);
195 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
196 *devattr, char *buf);
197 /* PWM and Auto point control */
198 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
200 static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
201 const char *buf, size_t count);
202 static ssize_t show_pwm_enable(struct device *dev,
203 struct device_attribute *devattr, char *buf);
204 static ssize_t store_pwm_enable(struct device *dev,
205 struct device_attribute *devattr, const char *buf, size_t count);
206 static ssize_t show_pwm_interpolate(struct device *dev,
207 struct device_attribute *devattr, char *buf);
208 static ssize_t store_pwm_interpolate(struct device *dev,
209 struct device_attribute *devattr, const char *buf, size_t count);
210 static ssize_t show_pwm_auto_point_channel(struct device *dev,
211 struct device_attribute *devattr, char *buf);
212 static ssize_t store_pwm_auto_point_channel(struct device *dev,
213 struct device_attribute *devattr, const char *buf, size_t count);
214 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
215 struct device_attribute *devattr, char *buf);
216 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
217 struct device_attribute *devattr, const char *buf, size_t count);
218 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
219 struct device_attribute *devattr, char *buf);
220 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
221 struct device_attribute *devattr, const char *buf, size_t count);
222 static ssize_t show_pwm_auto_point_temp(struct device *dev,
223 struct device_attribute *devattr, char *buf);
224 static ssize_t store_pwm_auto_point_temp(struct device *dev,
225 struct device_attribute *devattr, const char *buf, size_t count);
227 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
230 static int __devinit f71882fg_probe(struct platform_device * pdev);
231 static int __devexit f71882fg_remove(struct platform_device *pdev);
232 static int __init f71882fg_init(void);
233 static int __init f71882fg_find(int sioaddr, unsigned short *address);
234 static int __init f71882fg_device_add(unsigned short address);
235 static void __exit f71882fg_exit(void);
237 static struct platform_driver f71882fg_driver = {
239 .owner = THIS_MODULE,
242 .probe = f71882fg_probe,
243 .remove = __devexit_p(f71882fg_remove),
246 static struct device_attribute f71882fg_dev_attr[] =
248 __ATTR( name, S_IRUGO, show_name, NULL ),
251 static struct sensor_device_attribute_2 f71882fg_in_temp_attr[] = {
252 SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
253 SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
254 SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
256 SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
258 SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
259 SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
260 SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
261 SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
262 SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
263 SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
264 SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
265 SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
266 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
267 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
268 store_temp_max, 0, 0),
269 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
270 store_temp_max_hyst, 0, 0),
271 SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
272 store_temp_crit, 0, 0),
273 SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
275 SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 0),
276 SENSOR_ATTR_2(temp1_beep, S_IRUGO|S_IWUSR, show_temp_beep,
277 store_temp_beep, 0, 0),
278 SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 0),
279 SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
280 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
281 SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
282 store_temp_max, 0, 1),
283 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
284 store_temp_max_hyst, 0, 1),
285 SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
286 store_temp_crit, 0, 1),
287 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
289 SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 1),
290 SENSOR_ATTR_2(temp2_beep, S_IRUGO|S_IWUSR, show_temp_beep,
291 store_temp_beep, 0, 1),
292 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
293 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
294 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
295 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
296 store_temp_max, 0, 2),
297 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
298 store_temp_max_hyst, 0, 2),
299 SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
300 store_temp_crit, 0, 2),
301 SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
303 SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 2),
304 SENSOR_ATTR_2(temp3_beep, S_IRUGO|S_IWUSR, show_temp_beep,
305 store_temp_beep, 0, 2),
306 SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
307 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
310 static struct sensor_device_attribute_2 f71882fg_fan_attr[] = {
311 SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
312 SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
314 store_fan_full_speed, 0, 0),
315 SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
316 store_fan_beep, 0, 0),
317 SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
318 SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
319 SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
321 store_fan_full_speed, 0, 1),
322 SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
323 store_fan_beep, 0, 1),
324 SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
325 SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
326 SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
328 store_fan_full_speed, 0, 2),
329 SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
330 store_fan_beep, 0, 2),
331 SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
332 SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
333 SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
335 store_fan_full_speed, 0, 3),
336 SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
337 store_fan_beep, 0, 3),
338 SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
340 SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
341 SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
342 store_pwm_enable, 0, 0),
343 SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
344 show_pwm_interpolate, store_pwm_interpolate, 0, 0),
345 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
346 show_pwm_auto_point_channel,
347 store_pwm_auto_point_channel, 0, 0),
348 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
349 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
351 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
352 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
354 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
355 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
357 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
358 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
360 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
361 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
363 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
364 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
366 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
367 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
369 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
370 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
372 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
373 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
375 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
376 show_pwm_auto_point_temp_hyst,
377 store_pwm_auto_point_temp_hyst,
379 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
380 show_pwm_auto_point_temp_hyst, NULL, 1, 0),
381 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
382 show_pwm_auto_point_temp_hyst, NULL, 2, 0),
383 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
384 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
386 SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
387 SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
388 store_pwm_enable, 0, 1),
389 SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
390 show_pwm_interpolate, store_pwm_interpolate, 0, 1),
391 SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
392 show_pwm_auto_point_channel,
393 store_pwm_auto_point_channel, 0, 1),
394 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
395 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
397 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
398 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
400 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
401 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
403 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
404 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
406 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
407 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
409 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
410 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
412 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
413 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
415 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
416 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
418 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
419 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
421 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
422 show_pwm_auto_point_temp_hyst,
423 store_pwm_auto_point_temp_hyst,
425 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
426 show_pwm_auto_point_temp_hyst, NULL, 1, 1),
427 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
428 show_pwm_auto_point_temp_hyst, NULL, 2, 1),
429 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
430 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
432 SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
433 SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
434 store_pwm_enable, 0, 2),
435 SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
436 show_pwm_interpolate, store_pwm_interpolate, 0, 2),
437 SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
438 show_pwm_auto_point_channel,
439 store_pwm_auto_point_channel, 0, 2),
440 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
441 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
443 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
444 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
446 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
447 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
449 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
450 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
452 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
453 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
455 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
456 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
458 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
459 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
461 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
462 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
464 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
465 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
467 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
468 show_pwm_auto_point_temp_hyst,
469 store_pwm_auto_point_temp_hyst,
471 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
472 show_pwm_auto_point_temp_hyst, NULL, 1, 2),
473 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
474 show_pwm_auto_point_temp_hyst, NULL, 2, 2),
475 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
476 show_pwm_auto_point_temp_hyst, NULL, 3, 2),
478 SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
479 SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
480 store_pwm_enable, 0, 3),
481 SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
482 show_pwm_interpolate, store_pwm_interpolate, 0, 3),
483 SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
484 show_pwm_auto_point_channel,
485 store_pwm_auto_point_channel, 0, 3),
486 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
487 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
489 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
490 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
492 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
493 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
495 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
496 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
498 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
499 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
501 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
502 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
504 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
505 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
507 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
508 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
510 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
511 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
513 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
514 show_pwm_auto_point_temp_hyst,
515 store_pwm_auto_point_temp_hyst,
517 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
518 show_pwm_auto_point_temp_hyst, NULL, 1, 3),
519 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
520 show_pwm_auto_point_temp_hyst, NULL, 2, 3),
521 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
522 show_pwm_auto_point_temp_hyst, NULL, 3, 3),
526 /* Super I/O functions */
527 static inline int superio_inb(int base, int reg)
530 return inb(base + 1);
533 static int superio_inw(int base, int reg)
537 val = inb(base + 1) << 8;
539 val |= inb(base + 1);
543 static inline void superio_enter(int base)
545 /* according to the datasheet the key must be send twice! */
546 outb( SIO_UNLOCK_KEY, base);
547 outb( SIO_UNLOCK_KEY, base);
550 static inline void superio_select( int base, int ld)
552 outb(SIO_REG_LDSEL, base);
556 static inline void superio_exit(int base)
558 outb(SIO_LOCK_KEY, base);
561 static inline u16 fan_from_reg(u16 reg)
563 return reg ? (1500000 / reg) : 0;
566 static inline u16 fan_to_reg(u16 fan)
568 return fan ? (1500000 / fan) : 0;
571 static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
575 outb(reg, data->addr + ADDR_REG_OFFSET);
576 val = inb(data->addr + DATA_REG_OFFSET);
581 static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
585 outb(reg++, data->addr + ADDR_REG_OFFSET);
586 val = inb(data->addr + DATA_REG_OFFSET) << 8;
587 outb(reg, data->addr + ADDR_REG_OFFSET);
588 val |= inb(data->addr + DATA_REG_OFFSET);
593 static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
595 outb(reg, data->addr + ADDR_REG_OFFSET);
596 outb(val, data->addr + DATA_REG_OFFSET);
599 static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
601 outb(reg++, data->addr + ADDR_REG_OFFSET);
602 outb(val >> 8, data->addr + DATA_REG_OFFSET);
603 outb(reg, data->addr + ADDR_REG_OFFSET);
604 outb(val & 255, data->addr + DATA_REG_OFFSET);
607 static struct f71882fg_data *f71882fg_update_device(struct device *dev)
609 struct f71882fg_data *data = dev_get_drvdata(dev);
612 mutex_lock(&data->update_lock);
614 /* Update once every 60 seconds */
615 if ( time_after(jiffies, data->last_limits + 60 * HZ ) ||
617 data->in1_max = f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
618 data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
620 /* Get High & boundary temps*/
621 for (nr = 0; nr < 3; nr++) {
622 data->temp_ovt[nr] = f71882fg_read8(data,
623 F71882FG_REG_TEMP_OVT(nr));
624 data->temp_high[nr] = f71882fg_read8(data,
625 F71882FG_REG_TEMP_HIGH(nr));
628 /* Have to hardcode hyst*/
629 data->temp_hyst[0] = f71882fg_read8(data,
630 F71882FG_REG_TEMP_HYST1) >> 4;
631 /* Hyst temps 2 & 3 stored in same register */
632 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST23);
633 data->temp_hyst[1] = reg & 0x0F;
634 data->temp_hyst[2] = reg >> 4;
636 /* Have to hardcode type, because temp1 is special */
637 reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
638 reg2 = f71882fg_read8(data, F71882FG_REG_PECI);
639 if ((reg2 & 0x03) == 0x01)
640 data->temp_type[0] = 6 /* PECI */;
641 else if ((reg2 & 0x03) == 0x02)
642 data->temp_type[0] = 5 /* AMDSI */;
644 data->temp_type[0] = (reg & 0x02) ? 2 : 4;
646 data->temp_type[1] = (reg & 0x04) ? 2 : 4;
647 data->temp_type[2] = (reg & 0x08) ? 2 : 4;
649 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
651 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
653 data->pwm_enable = f71882fg_read8(data,
654 F71882FG_REG_PWM_ENABLE);
655 data->pwm_auto_point_hyst[0] = f71882fg_read8(data,
656 F71882FG_REG_FAN_HYST0);
657 data->pwm_auto_point_hyst[1] = f71882fg_read8(data,
658 F71882FG_REG_FAN_HYST1);
659 for (nr = 0; nr < 4; nr++) {
662 data->pwm_auto_point_mapping[nr] =
664 F71882FG_REG_POINT_MAPPING(nr));
666 for (point = 0; point < 5; point++) {
667 data->pwm_auto_point_pwm[nr][point] =
669 F71882FG_REG_POINT_PWM
672 for (point = 0; point < 4; point++) {
673 data->pwm_auto_point_temp[nr][point] =
675 F71882FG_REG_POINT_TEMP
679 data->last_limits = jiffies;
682 /* Update every second */
683 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
684 data->temp_status = f71882fg_read8(data,
685 F71882FG_REG_TEMP_STATUS);
686 data->temp_diode_open = f71882fg_read8(data,
687 F71882FG_REG_TEMP_DIODE_OPEN);
688 for (nr = 0; nr < 3; nr++)
689 data->temp[nr] = f71882fg_read8(data,
690 F71882FG_REG_TEMP(nr));
692 data->fan_status = f71882fg_read8(data,
693 F71882FG_REG_FAN_STATUS);
694 for (nr = 0; nr < 4; nr++) {
695 data->fan[nr] = f71882fg_read16(data,
696 F71882FG_REG_FAN(nr));
697 data->fan_target[nr] =
698 f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
699 data->fan_full_speed[nr] =
700 f71882fg_read16(data,
701 F71882FG_REG_FAN_FULL_SPEED(nr));
703 f71882fg_read8(data, F71882FG_REG_PWM(nr));
706 data->in_status = f71882fg_read8(data,
707 F71882FG_REG_IN_STATUS);
708 for (nr = 0; nr < 9; nr++)
709 data->in[nr] = f71882fg_read8(data,
710 F71882FG_REG_IN(nr));
712 data->last_updated = jiffies;
716 mutex_unlock(&data->update_lock);
721 /* Sysfs Interface */
722 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
725 struct f71882fg_data *data = f71882fg_update_device(dev);
726 int nr = to_sensor_dev_attr_2(devattr)->index;
727 int speed = fan_from_reg(data->fan[nr]);
729 if (speed == FAN_MIN_DETECT)
732 return sprintf(buf, "%d\n", speed);
735 static ssize_t show_fan_full_speed(struct device *dev,
736 struct device_attribute *devattr, char *buf)
738 struct f71882fg_data *data = f71882fg_update_device(dev);
739 int nr = to_sensor_dev_attr_2(devattr)->index;
740 int speed = fan_from_reg(data->fan_full_speed[nr]);
741 return sprintf(buf, "%d\n", speed);
744 static ssize_t store_fan_full_speed(struct device *dev,
745 struct device_attribute *devattr,
746 const char *buf, size_t count)
748 struct f71882fg_data *data = dev_get_drvdata(dev);
749 int nr = to_sensor_dev_attr_2(devattr)->index;
750 long val = simple_strtol(buf, NULL, 10);
752 val = SENSORS_LIMIT(val, 23, 1500000);
753 val = fan_to_reg(val);
755 mutex_lock(&data->update_lock);
756 if (data->pwm_enable & (1 << (2 * nr)))
761 f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
762 data->fan_full_speed[nr] = val;
764 mutex_unlock(&data->update_lock);
769 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
772 struct f71882fg_data *data = f71882fg_update_device(dev);
773 int nr = to_sensor_dev_attr_2(devattr)->index;
775 if (data->fan_beep & (1 << nr))
776 return sprintf(buf, "1\n");
778 return sprintf(buf, "0\n");
781 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
782 *devattr, const char *buf, size_t count)
784 struct f71882fg_data *data = dev_get_drvdata(dev);
785 int nr = to_sensor_dev_attr_2(devattr)->index;
786 int val = simple_strtoul(buf, NULL, 10);
788 mutex_lock(&data->update_lock);
790 data->fan_beep |= 1 << nr;
792 data->fan_beep &= ~(1 << nr);
794 f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
795 mutex_unlock(&data->update_lock);
800 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
803 struct f71882fg_data *data = f71882fg_update_device(dev);
804 int nr = to_sensor_dev_attr_2(devattr)->index;
806 if (data->fan_status & (1 << nr))
807 return sprintf(buf, "1\n");
809 return sprintf(buf, "0\n");
812 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
815 struct f71882fg_data *data = f71882fg_update_device(dev);
816 int nr = to_sensor_dev_attr_2(devattr)->index;
818 return sprintf(buf, "%d\n", data->in[nr] * 8);
821 static ssize_t show_in_max(struct device *dev, struct device_attribute
824 struct f71882fg_data *data = f71882fg_update_device(dev);
826 return sprintf(buf, "%d\n", data->in1_max * 8);
829 static ssize_t store_in_max(struct device *dev, struct device_attribute
830 *devattr, const char *buf, size_t count)
832 struct f71882fg_data *data = dev_get_drvdata(dev);
833 int val = simple_strtoul(buf, NULL, 10) / 8;
838 mutex_lock(&data->update_lock);
839 f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
841 mutex_unlock(&data->update_lock);
846 static ssize_t show_in_beep(struct device *dev, struct device_attribute
849 struct f71882fg_data *data = f71882fg_update_device(dev);
850 int nr = to_sensor_dev_attr_2(devattr)->index;
852 if (data->in_beep & (1 << nr))
853 return sprintf(buf, "1\n");
855 return sprintf(buf, "0\n");
858 static ssize_t store_in_beep(struct device *dev, struct device_attribute
859 *devattr, const char *buf, size_t count)
861 struct f71882fg_data *data = dev_get_drvdata(dev);
862 int nr = to_sensor_dev_attr_2(devattr)->index;
863 int val = simple_strtoul(buf, NULL, 10);
865 mutex_lock(&data->update_lock);
867 data->in_beep |= 1 << nr;
869 data->in_beep &= ~(1 << nr);
871 f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
872 mutex_unlock(&data->update_lock);
877 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
880 struct f71882fg_data *data = f71882fg_update_device(dev);
881 int nr = to_sensor_dev_attr_2(devattr)->index;
883 if (data->in_status & (1 << nr))
884 return sprintf(buf, "1\n");
886 return sprintf(buf, "0\n");
889 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
892 struct f71882fg_data *data = f71882fg_update_device(dev);
893 int nr = to_sensor_dev_attr_2(devattr)->index;
895 return sprintf(buf, "%d\n", data->temp[nr] * 1000);
898 static ssize_t show_temp_max(struct device *dev, struct device_attribute
901 struct f71882fg_data *data = f71882fg_update_device(dev);
902 int nr = to_sensor_dev_attr_2(devattr)->index;
904 return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
907 static ssize_t store_temp_max(struct device *dev, struct device_attribute
908 *devattr, const char *buf, size_t count)
910 struct f71882fg_data *data = dev_get_drvdata(dev);
911 int nr = to_sensor_dev_attr_2(devattr)->index;
912 int val = simple_strtoul(buf, NULL, 10) / 1000;
917 mutex_lock(&data->update_lock);
918 f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
919 data->temp_high[nr] = val;
920 mutex_unlock(&data->update_lock);
925 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
928 struct f71882fg_data *data = f71882fg_update_device(dev);
929 int nr = to_sensor_dev_attr_2(devattr)->index;
931 return sprintf(buf, "%d\n",
932 (data->temp_high[nr] - data->temp_hyst[nr]) * 1000);
935 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
936 *devattr, const char *buf, size_t count)
938 struct f71882fg_data *data = dev_get_drvdata(dev);
939 int nr = to_sensor_dev_attr_2(devattr)->index;
940 int val = simple_strtoul(buf, NULL, 10) / 1000;
943 mutex_lock(&data->update_lock);
945 /* convert abs to relative and check */
946 val = data->temp_high[nr] - val;
947 if (val < 0 || val > 15) {
949 goto store_temp_max_hyst_exit;
952 data->temp_hyst[nr] = val;
954 /* convert value to register contents */
960 val = val | (data->temp_hyst[2] << 4);
963 val = data->temp_hyst[1] | (val << 4);
967 f71882fg_write8(data, nr ? F71882FG_REG_TEMP_HYST23 :
968 F71882FG_REG_TEMP_HYST1, val);
970 store_temp_max_hyst_exit:
971 mutex_unlock(&data->update_lock);
975 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
978 struct f71882fg_data *data = f71882fg_update_device(dev);
979 int nr = to_sensor_dev_attr_2(devattr)->index;
981 return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
984 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
985 *devattr, const char *buf, size_t count)
987 struct f71882fg_data *data = dev_get_drvdata(dev);
988 int nr = to_sensor_dev_attr_2(devattr)->index;
989 int val = simple_strtoul(buf, NULL, 10) / 1000;
994 mutex_lock(&data->update_lock);
995 f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
996 data->temp_ovt[nr] = val;
997 mutex_unlock(&data->update_lock);
1002 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
1003 *devattr, char *buf)
1005 struct f71882fg_data *data = f71882fg_update_device(dev);
1006 int nr = to_sensor_dev_attr_2(devattr)->index;
1008 return sprintf(buf, "%d\n",
1009 (data->temp_ovt[nr] - data->temp_hyst[nr]) * 1000);
1012 static ssize_t show_temp_type(struct device *dev, struct device_attribute
1013 *devattr, char *buf)
1015 struct f71882fg_data *data = f71882fg_update_device(dev);
1016 int nr = to_sensor_dev_attr_2(devattr)->index;
1018 return sprintf(buf, "%d\n", data->temp_type[nr]);
1021 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
1022 *devattr, char *buf)
1024 struct f71882fg_data *data = f71882fg_update_device(dev);
1025 int nr = to_sensor_dev_attr_2(devattr)->index;
1027 if (data->temp_beep & (1 << (nr + 1)))
1028 return sprintf(buf, "1\n");
1030 return sprintf(buf, "0\n");
1033 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
1034 *devattr, const char *buf, size_t count)
1036 struct f71882fg_data *data = dev_get_drvdata(dev);
1037 int nr = to_sensor_dev_attr_2(devattr)->index;
1038 int val = simple_strtoul(buf, NULL, 10);
1040 mutex_lock(&data->update_lock);
1042 data->temp_beep |= 1 << (nr + 1);
1044 data->temp_beep &= ~(1 << (nr + 1));
1046 f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
1047 mutex_unlock(&data->update_lock);
1052 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
1053 *devattr, char *buf)
1055 struct f71882fg_data *data = f71882fg_update_device(dev);
1056 int nr = to_sensor_dev_attr_2(devattr)->index;
1058 if (data->temp_status & (1 << (nr + 1)))
1059 return sprintf(buf, "1\n");
1061 return sprintf(buf, "0\n");
1064 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
1065 *devattr, char *buf)
1067 struct f71882fg_data *data = f71882fg_update_device(dev);
1068 int nr = to_sensor_dev_attr_2(devattr)->index;
1070 if (data->temp_diode_open & (1 << (nr + 1)))
1071 return sprintf(buf, "1\n");
1073 return sprintf(buf, "0\n");
1076 static ssize_t show_pwm(struct device *dev,
1077 struct device_attribute *devattr, char *buf)
1079 struct f71882fg_data *data = f71882fg_update_device(dev);
1080 int val, nr = to_sensor_dev_attr_2(devattr)->index;
1081 if (data->pwm_enable & (1 << (2 * nr)))
1083 val = data->pwm[nr];
1086 mutex_lock(&data->update_lock);
1087 val = 255 * fan_from_reg(data->fan_target[nr])
1088 / fan_from_reg(data->fan_full_speed[nr]);
1089 mutex_unlock(&data->update_lock);
1091 return sprintf(buf, "%d\n", val);
1094 static ssize_t store_pwm(struct device *dev,
1095 struct device_attribute *devattr, const char *buf,
1098 /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1099 struct f71882fg_data *data = f71882fg_update_device(dev);
1100 int nr = to_sensor_dev_attr_2(devattr)->index;
1101 long val = simple_strtol(buf, NULL, 10);
1102 val = SENSORS_LIMIT(val, 0, 255);
1104 mutex_lock(&data->update_lock);
1105 if (data->pwm_enable & (1 << (2 * nr))) {
1107 f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1108 data->pwm[nr] = val;
1111 int target = val * fan_from_reg(data->fan_full_speed[nr]) / 255;
1112 f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr),
1113 fan_to_reg(target));
1114 data->fan_target[nr] = fan_to_reg(target);
1116 mutex_unlock(&data->update_lock);
1121 static ssize_t show_pwm_enable(struct device *dev,
1122 struct device_attribute *devattr, char *buf)
1125 struct f71882fg_data *data = f71882fg_update_device(dev);
1126 int nr = to_sensor_dev_attr_2(devattr)->index;
1128 if (data->pwm_enable & (2 << (2 * nr)))
1133 return sprintf(buf, "%d\n", result);
1136 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1137 *devattr, const char *buf, size_t count)
1139 struct f71882fg_data *data = dev_get_drvdata(dev);
1140 int nr = to_sensor_dev_attr_2(devattr)->index;
1141 long val = simple_strtol(buf, NULL, 10);
1142 if (val < 1 || val > 2)
1145 mutex_lock(&data->update_lock);
1148 data->pwm_enable |= 2 << (2 * nr);
1151 data->pwm_enable &= ~(2 << (2 * nr));
1152 break; /* Temperature ctrl */
1154 switch (fan_mode[nr]) {
1156 data->pwm_enable |= 1 << (2 * nr);
1157 break; /* Duty cycle mode */
1159 data->pwm_enable &= ~(1 << (2 * nr));
1160 break; /* RPM mode */
1162 f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
1163 mutex_unlock(&data->update_lock);
1168 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
1169 struct device_attribute *devattr,
1173 struct f71882fg_data *data = f71882fg_update_device(dev);
1174 int pwm = to_sensor_dev_attr_2(devattr)->index;
1175 int point = to_sensor_dev_attr_2(devattr)->nr;
1177 if (data->pwm_enable & (1 << (2 * pwm))) {
1179 result = data->pwm_auto_point_pwm[pwm][point];
1182 result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
1185 return sprintf(buf, "%d\n", result);
1188 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
1189 struct device_attribute *devattr,
1190 const char *buf, size_t count)
1192 /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1193 struct f71882fg_data *data = f71882fg_update_device(dev);
1194 int pwm = to_sensor_dev_attr_2(devattr)->index;
1195 int point = to_sensor_dev_attr_2(devattr)->nr;
1196 int val = simple_strtoul(buf, NULL, 10);
1197 val = SENSORS_LIMIT(val, 0, 255);
1199 mutex_lock(&data->update_lock);
1200 if (data->pwm_enable & (1 << (2 * pwm))) {
1204 if (val < 29) /* Prevent negative numbers */
1207 val = (255 - val) * 32 / val;
1209 f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
1210 data->pwm_auto_point_pwm[pwm][point] = val;
1211 mutex_unlock(&data->update_lock);
1216 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
1217 struct device_attribute *devattr,
1221 struct f71882fg_data *data = f71882fg_update_device(dev);
1222 int nr = to_sensor_dev_attr_2(devattr)->index;
1223 int point = to_sensor_dev_attr_2(devattr)->nr;
1225 mutex_lock(&data->update_lock);
1228 result = data->pwm_auto_point_hyst[0] & 0x0f;
1231 result = data->pwm_auto_point_hyst[0] >> 4;
1234 result = data->pwm_auto_point_hyst[1] & 0x0f;
1237 result = data->pwm_auto_point_hyst[1] >> 4;
1240 result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
1241 mutex_unlock(&data->update_lock);
1243 return sprintf(buf, "%d\n", result);
1246 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
1247 struct device_attribute *devattr,
1248 const char *buf, size_t count)
1250 struct f71882fg_data *data = f71882fg_update_device(dev);
1251 int nr = to_sensor_dev_attr_2(devattr)->index;
1252 int point = to_sensor_dev_attr_2(devattr)->nr;
1253 long val = simple_strtol(buf, NULL, 10) / 1000;
1255 mutex_lock(&data->update_lock);
1256 val = SENSORS_LIMIT(val, data->pwm_auto_point_temp[nr][point] - 15,
1257 data->pwm_auto_point_temp[nr][point]);
1258 val = data->pwm_auto_point_temp[nr][point] - val;
1262 val = (data->pwm_auto_point_hyst[0] & 0xf0) | val;
1265 val = (data->pwm_auto_point_hyst[0] & 0x0f) | (val << 4);
1268 val = (data->pwm_auto_point_hyst[1] & 0xf0) | val;
1271 val = (data->pwm_auto_point_hyst[1] & 0x0f) | (val << 4);
1274 if (nr == 0 || nr == 1) {
1275 f71882fg_write8(data, F71882FG_REG_FAN_HYST0, val);
1276 data->pwm_auto_point_hyst[0] = val;
1278 f71882fg_write8(data, F71882FG_REG_FAN_HYST1, val);
1279 data->pwm_auto_point_hyst[1] = val;
1281 mutex_unlock(&data->update_lock);
1286 static ssize_t show_pwm_interpolate(struct device *dev,
1287 struct device_attribute *devattr, char *buf)
1290 struct f71882fg_data *data = f71882fg_update_device(dev);
1291 int nr = to_sensor_dev_attr_2(devattr)->index;
1293 result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
1295 return sprintf(buf, "%d\n", result);
1298 static ssize_t store_pwm_interpolate(struct device *dev,
1299 struct device_attribute *devattr,
1300 const char *buf, size_t count)
1302 /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1303 struct f71882fg_data *data = f71882fg_update_device(dev);
1304 int nr = to_sensor_dev_attr_2(devattr)->index;
1305 int val = simple_strtoul(buf, NULL, 10);
1306 mutex_lock(&data->update_lock);
1308 val = data->pwm_auto_point_mapping[nr] | (1 << 4);
1310 val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
1311 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1312 data->pwm_auto_point_mapping[nr] = val;
1313 mutex_unlock(&data->update_lock);
1318 static ssize_t show_pwm_auto_point_channel(struct device *dev,
1319 struct device_attribute *devattr,
1323 struct f71882fg_data *data = f71882fg_update_device(dev);
1324 int nr = to_sensor_dev_attr_2(devattr)->index;
1326 result = 1 << ((data->pwm_auto_point_mapping[nr] & 3) - 1);
1328 return sprintf(buf, "%d\n", result);
1331 static ssize_t store_pwm_auto_point_channel(struct device *dev,
1332 struct device_attribute *devattr,
1333 const char *buf, size_t count)
1335 /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1336 struct f71882fg_data *data = f71882fg_update_device(dev);
1337 int nr = to_sensor_dev_attr_2(devattr)->index;
1338 long val = simple_strtol(buf, NULL, 10);
1352 mutex_lock(&data->update_lock);
1353 val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
1354 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1355 data->pwm_auto_point_mapping[nr] = val;
1356 mutex_unlock(&data->update_lock);
1361 static ssize_t show_pwm_auto_point_temp(struct device *dev,
1362 struct device_attribute *devattr,
1366 struct f71882fg_data *data = f71882fg_update_device(dev);
1367 int pwm = to_sensor_dev_attr_2(devattr)->index;
1368 int point = to_sensor_dev_attr_2(devattr)->nr;
1370 result = data->pwm_auto_point_temp[pwm][point];
1371 return sprintf(buf, "%d\n", 1000 * result);
1374 static ssize_t store_pwm_auto_point_temp(struct device *dev,
1375 struct device_attribute *devattr,
1376 const char *buf, size_t count)
1378 /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1379 struct f71882fg_data *data = f71882fg_update_device(dev);
1380 int pwm = to_sensor_dev_attr_2(devattr)->index;
1381 int point = to_sensor_dev_attr_2(devattr)->nr;
1382 long val = simple_strtol(buf, NULL, 10) / 1000;
1383 val = SENSORS_LIMIT(val, 0, 255);
1385 mutex_lock(&data->update_lock);
1386 f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
1387 data->pwm_auto_point_temp[pwm][point] = val;
1388 mutex_unlock(&data->update_lock);
1393 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
1396 return sprintf(buf, DRVNAME "\n");
1400 static int __devinit f71882fg_probe(struct platform_device * pdev)
1402 struct f71882fg_data *data;
1406 if (!(data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL)))
1409 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1410 mutex_init(&data->update_lock);
1411 platform_set_drvdata(pdev, data);
1413 /* Register sysfs interface files */
1414 for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++) {
1415 err = device_create_file(&pdev->dev, &f71882fg_dev_attr[i]);
1417 goto exit_unregister_sysfs;
1420 start_reg = f71882fg_read8(data, F71882FG_REG_START);
1421 if (start_reg & 0x01) {
1422 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++) {
1423 err = device_create_file(&pdev->dev,
1424 &f71882fg_in_temp_attr[i].dev_attr);
1426 goto exit_unregister_sysfs;
1430 if (start_reg & 0x02) {
1431 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++) {
1432 err = device_create_file(&pdev->dev,
1433 &f71882fg_fan_attr[i].dev_attr);
1435 goto exit_unregister_sysfs;
1439 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1440 if (IS_ERR(data->hwmon_dev)) {
1441 err = PTR_ERR(data->hwmon_dev);
1442 goto exit_unregister_sysfs;
1447 exit_unregister_sysfs:
1448 for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++)
1449 device_remove_file(&pdev->dev, &f71882fg_dev_attr[i]);
1451 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
1452 device_remove_file(&pdev->dev,
1453 &f71882fg_in_temp_attr[i].dev_attr);
1455 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
1456 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
1463 static int __devexit f71882fg_remove(struct platform_device *pdev)
1466 struct f71882fg_data *data = platform_get_drvdata(pdev);
1468 platform_set_drvdata(pdev, NULL);
1469 hwmon_device_unregister(data->hwmon_dev);
1471 for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++)
1472 device_remove_file(&pdev->dev, &f71882fg_dev_attr[i]);
1474 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
1475 device_remove_file(&pdev->dev,
1476 &f71882fg_in_temp_attr[i].dev_attr);
1478 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
1479 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
1486 static int __init f71882fg_find(int sioaddr, unsigned short *address)
1491 struct f71882fg_data data;
1493 superio_enter(sioaddr);
1495 devid = superio_inw(sioaddr, SIO_REG_MANID);
1496 if (devid != SIO_FINTEK_ID) {
1497 printk(KERN_INFO DRVNAME ": Not a Fintek device\n");
1501 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
1502 if (devid != SIO_F71882_ID) {
1503 printk(KERN_INFO DRVNAME ": Unsupported Fintek device\n");
1507 superio_select(sioaddr, SIO_F71882FG_LD_HWM);
1508 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
1509 printk(KERN_WARNING DRVNAME ": Device not activated\n");
1513 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1516 printk(KERN_WARNING DRVNAME ": Base address not set\n");
1519 *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
1521 data.addr = *address;
1522 start_reg = f71882fg_read8(&data, F71882FG_REG_START);
1523 if (!(start_reg & 0x03)) {
1524 printk(KERN_WARNING DRVNAME
1525 ": Hardware monitoring not activated\n");
1530 printk(KERN_INFO DRVNAME ": Found F71882FG chip at %#x, revision %d\n",
1531 (unsigned int)*address,
1532 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
1534 superio_exit(sioaddr);
1538 static int __init f71882fg_device_add(unsigned short address)
1540 struct resource res = {
1542 .end = address + REGION_LENGTH - 1,
1543 .flags = IORESOURCE_IO,
1547 f71882fg_pdev = platform_device_alloc(DRVNAME, address);
1551 res.name = f71882fg_pdev->name;
1552 err = platform_device_add_resources(f71882fg_pdev, &res, 1);
1554 printk(KERN_ERR DRVNAME ": Device resource addition failed\n");
1555 goto exit_device_put;
1558 err = platform_device_add(f71882fg_pdev);
1560 printk(KERN_ERR DRVNAME ": Device addition failed\n");
1561 goto exit_device_put;
1567 platform_device_put(f71882fg_pdev);
1572 static int __init f71882fg_init(void)
1575 unsigned short address;
1577 if (f71882fg_find(0x2e, &address) && f71882fg_find(0x4e, &address))
1580 if ((err = platform_driver_register(&f71882fg_driver)))
1583 if ((err = f71882fg_device_add(address)))
1589 platform_driver_unregister(&f71882fg_driver);
1594 static void __exit f71882fg_exit(void)
1596 platform_device_unregister(f71882fg_pdev);
1597 platform_driver_unregister(&f71882fg_driver);
1600 MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
1601 MODULE_AUTHOR("Hans Edgington (hans@edgington.nl)");
1602 MODULE_LICENSE("GPL");
1604 module_init(f71882fg_init);
1605 module_exit(f71882fg_exit);