1 /***************************************************************************
2 * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
3 * Copyright (C) 2007,2008 by Hans de Goede <hdegoede@redhat.com> *
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_F71862_ID 0x0601 /* Chipset ID */
47 #define SIO_F71882_ID 0x0541 /* Chipset ID */
49 #define REGION_LENGTH 8
50 #define ADDR_REG_OFFSET 5
51 #define DATA_REG_OFFSET 6
53 #define F71882FG_REG_PECI 0x0A
55 #define F71882FG_REG_IN_STATUS 0x12 /* f71882fg only */
56 #define F71882FG_REG_IN_BEEP 0x13 /* f71882fg only */
57 #define F71882FG_REG_IN(nr) (0x20 + (nr))
58 #define F71882FG_REG_IN1_HIGH 0x32 /* f71882fg only */
60 #define F71882FG_REG_FAN(nr) (0xA0 + (16 * (nr)))
61 #define F71882FG_REG_FAN_TARGET(nr) (0xA2 + (16 * (nr)))
62 #define F71882FG_REG_FAN_FULL_SPEED(nr) (0xA4 + (16 * (nr)))
63 #define F71882FG_REG_FAN_STATUS 0x92
64 #define F71882FG_REG_FAN_BEEP 0x93
66 #define F71882FG_REG_TEMP(nr) (0x70 + 2 * (nr))
67 #define F71882FG_REG_TEMP_OVT(nr) (0x80 + 2 * (nr))
68 #define F71882FG_REG_TEMP_HIGH(nr) (0x81 + 2 * (nr))
69 #define F71882FG_REG_TEMP_STATUS 0x62
70 #define F71882FG_REG_TEMP_BEEP 0x63
71 #define F71882FG_REG_TEMP_HYST(nr) (0x6C + (nr))
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_HYST(nr) (0x98 + (nr))
81 #define F71882FG_REG_POINT_PWM(pwm, point) (0xAA + (point) + (16 * (pwm)))
82 #define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm)))
83 #define F71882FG_REG_POINT_MAPPING(nr) (0xAF + 16 * (nr))
85 #define F71882FG_REG_START 0x01
87 #define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */
89 static unsigned short force_id;
90 module_param(force_id, ushort, 0);
91 MODULE_PARM_DESC(force_id, "Override the detected device ID");
93 static int fan_mode[4] = { 0, 0, 0, 0 };
94 module_param_array(fan_mode, int, NULL, 0644);
95 MODULE_PARM_DESC(fan_mode, "List of fan control modes (f71882fg only) "
96 "(0=don't change, 1=pwm, 2=rpm)\n"
97 "Note: this needs a write to pwm#_enable to take effect");
99 enum chips { f71862fg, f71882fg };
101 static const char *f71882fg_names[] = {
106 static struct platform_device *f71882fg_pdev;
108 /* Super-I/O Function prototypes */
109 static inline int superio_inb(int base, int reg);
110 static inline int superio_inw(int base, int reg);
111 static inline void superio_enter(int base);
112 static inline void superio_select(int base, int ld);
113 static inline void superio_exit(int base);
115 struct f71882fg_sio_data {
119 struct f71882fg_data {
122 struct device *hwmon_dev;
124 struct mutex update_lock;
125 char valid; /* !=0 if following fields are valid */
126 unsigned long last_updated; /* In jiffies */
127 unsigned long last_limits; /* In jiffies */
129 /* Register Values */
136 u16 fan_full_speed[4];
139 /* Note: all models have only 3 temperature channels, but on some
140 they are addressed as 0-2 and on others as 1-3, so for coding
141 convenience we reserve space for 4 channels */
145 u8 temp_hyst[2]; /* 2 hysts stored per reg */
152 u8 pwm_auto_point_hyst[2];
153 u8 pwm_auto_point_mapping[4];
154 u8 pwm_auto_point_pwm[4][5];
155 u8 pwm_auto_point_temp[4][4];
159 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
161 static ssize_t show_in_max(struct device *dev, struct device_attribute
162 *devattr, char *buf);
163 static ssize_t store_in_max(struct device *dev, struct device_attribute
164 *devattr, const char *buf, size_t count);
165 static ssize_t show_in_beep(struct device *dev, struct device_attribute
166 *devattr, char *buf);
167 static ssize_t store_in_beep(struct device *dev, struct device_attribute
168 *devattr, const char *buf, size_t count);
169 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
170 *devattr, char *buf);
172 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
174 static ssize_t show_fan_full_speed(struct device *dev,
175 struct device_attribute *devattr, char *buf);
176 static ssize_t store_fan_full_speed(struct device *dev,
177 struct device_attribute *devattr, const char *buf, size_t count);
178 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
179 *devattr, char *buf);
180 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
181 *devattr, const char *buf, size_t count);
182 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
183 *devattr, char *buf);
185 static ssize_t show_temp(struct device *dev, struct device_attribute
186 *devattr, char *buf);
187 static ssize_t show_temp_max(struct device *dev, struct device_attribute
188 *devattr, char *buf);
189 static ssize_t store_temp_max(struct device *dev, struct device_attribute
190 *devattr, const char *buf, size_t count);
191 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
192 *devattr, char *buf);
193 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
194 *devattr, const char *buf, size_t count);
195 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
196 *devattr, char *buf);
197 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
198 *devattr, const char *buf, size_t count);
199 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
200 *devattr, char *buf);
201 static ssize_t show_temp_type(struct device *dev, struct device_attribute
202 *devattr, char *buf);
203 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
204 *devattr, char *buf);
205 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
206 *devattr, const char *buf, size_t count);
207 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
208 *devattr, char *buf);
209 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
210 *devattr, char *buf);
211 /* PWM and Auto point control */
212 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
214 static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
215 const char *buf, size_t count);
216 static ssize_t show_pwm_enable(struct device *dev,
217 struct device_attribute *devattr, char *buf);
218 static ssize_t store_pwm_enable(struct device *dev,
219 struct device_attribute *devattr, const char *buf, size_t count);
220 static ssize_t show_pwm_interpolate(struct device *dev,
221 struct device_attribute *devattr, char *buf);
222 static ssize_t store_pwm_interpolate(struct device *dev,
223 struct device_attribute *devattr, const char *buf, size_t count);
224 static ssize_t show_pwm_auto_point_channel(struct device *dev,
225 struct device_attribute *devattr, char *buf);
226 static ssize_t store_pwm_auto_point_channel(struct device *dev,
227 struct device_attribute *devattr, const char *buf, size_t count);
228 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
229 struct device_attribute *devattr, char *buf);
230 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
231 struct device_attribute *devattr, const char *buf, size_t count);
232 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
233 struct device_attribute *devattr, char *buf);
234 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
235 struct device_attribute *devattr, const char *buf, size_t count);
236 static ssize_t show_pwm_auto_point_temp(struct device *dev,
237 struct device_attribute *devattr, char *buf);
238 static ssize_t store_pwm_auto_point_temp(struct device *dev,
239 struct device_attribute *devattr, const char *buf, size_t count);
241 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
244 static int __devinit f71882fg_probe(struct platform_device * pdev);
245 static int f71882fg_remove(struct platform_device *pdev);
247 static struct platform_driver f71882fg_driver = {
249 .owner = THIS_MODULE,
252 .probe = f71882fg_probe,
253 .remove = __devexit_p(f71882fg_remove),
256 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
258 static struct sensor_device_attribute_2 f718x2fg_in_temp_attr[] = {
259 SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
260 SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
261 SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
262 SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
263 SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
264 SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
265 SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
266 SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
267 SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
268 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1),
269 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
270 store_temp_max, 0, 1),
271 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
272 store_temp_max_hyst, 0, 1),
273 SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
274 store_temp_crit, 0, 1),
275 SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
277 SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1),
278 SENSOR_ATTR_2(temp1_beep, S_IRUGO|S_IWUSR, show_temp_beep,
279 store_temp_beep, 0, 1),
280 SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
281 SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
282 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2),
283 SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
284 store_temp_max, 0, 2),
285 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
286 store_temp_max_hyst, 0, 2),
287 SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
288 store_temp_crit, 0, 2),
289 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
291 SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2),
292 SENSOR_ATTR_2(temp2_beep, S_IRUGO|S_IWUSR, show_temp_beep,
293 store_temp_beep, 0, 2),
294 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
295 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
296 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3),
297 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
298 store_temp_max, 0, 3),
299 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
300 store_temp_max_hyst, 0, 3),
301 SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
302 store_temp_crit, 0, 3),
303 SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
305 SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3),
306 SENSOR_ATTR_2(temp3_beep, S_IRUGO|S_IWUSR, show_temp_beep,
307 store_temp_beep, 0, 3),
308 SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3),
309 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3),
312 static struct sensor_device_attribute_2 f71882fg_in_temp_attr[] = {
313 SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
315 SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
317 SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
320 static struct sensor_device_attribute_2 f718x2fg_fan_attr[] = {
321 SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
322 SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
324 store_fan_full_speed, 0, 0),
325 SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
326 store_fan_beep, 0, 0),
327 SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
328 SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
329 SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
331 store_fan_full_speed, 0, 1),
332 SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
333 store_fan_beep, 0, 1),
334 SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
335 SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
336 SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
338 store_fan_full_speed, 0, 2),
339 SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
340 store_fan_beep, 0, 2),
341 SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
343 SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
344 SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
345 store_pwm_enable, 0, 0),
346 SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
347 show_pwm_interpolate, store_pwm_interpolate, 0, 0),
348 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
349 show_pwm_auto_point_channel,
350 store_pwm_auto_point_channel, 0, 0),
352 SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
353 SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
354 store_pwm_enable, 0, 1),
355 SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
356 show_pwm_interpolate, store_pwm_interpolate, 0, 1),
357 SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
358 show_pwm_auto_point_channel,
359 store_pwm_auto_point_channel, 0, 1),
361 SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
362 SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
363 store_pwm_enable, 0, 2),
364 SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
365 show_pwm_interpolate, store_pwm_interpolate, 0, 2),
366 SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
367 show_pwm_auto_point_channel,
368 store_pwm_auto_point_channel, 0, 2),
371 static struct sensor_device_attribute_2 f71862fg_fan_attr[] = {
372 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
373 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
375 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
376 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
378 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
379 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
381 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
382 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
384 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
385 show_pwm_auto_point_temp_hyst,
386 store_pwm_auto_point_temp_hyst,
388 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
389 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
391 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
392 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
394 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
395 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
397 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
398 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
400 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
401 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
403 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
404 show_pwm_auto_point_temp_hyst,
405 store_pwm_auto_point_temp_hyst,
407 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
408 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
411 static struct sensor_device_attribute_2 f71882fg_fan_attr[] = {
412 SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
413 SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
415 store_fan_full_speed, 0, 3),
416 SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
417 store_fan_beep, 0, 3),
418 SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
420 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
421 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
423 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
424 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
426 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
427 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
429 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
430 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
432 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
433 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
435 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
436 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
438 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
439 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
441 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
442 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
444 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
445 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
447 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
448 show_pwm_auto_point_temp_hyst,
449 store_pwm_auto_point_temp_hyst,
451 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
452 show_pwm_auto_point_temp_hyst, NULL, 1, 0),
453 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
454 show_pwm_auto_point_temp_hyst, NULL, 2, 0),
455 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
456 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
458 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
459 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
461 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
462 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
464 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
465 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
467 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
468 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
470 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
471 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
473 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
474 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
476 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
477 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
479 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
480 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
482 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
483 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
485 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
486 show_pwm_auto_point_temp_hyst,
487 store_pwm_auto_point_temp_hyst,
489 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
490 show_pwm_auto_point_temp_hyst, NULL, 1, 1),
491 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
492 show_pwm_auto_point_temp_hyst, NULL, 2, 1),
493 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
494 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
496 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
497 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
499 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
500 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
502 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
503 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
505 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
506 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
508 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
509 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
511 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
512 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
514 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
515 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
517 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
518 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
520 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
521 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
523 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
524 show_pwm_auto_point_temp_hyst,
525 store_pwm_auto_point_temp_hyst,
527 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
528 show_pwm_auto_point_temp_hyst, NULL, 1, 2),
529 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
530 show_pwm_auto_point_temp_hyst, NULL, 2, 2),
531 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
532 show_pwm_auto_point_temp_hyst, NULL, 3, 2),
534 SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
535 SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
536 store_pwm_enable, 0, 3),
537 SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
538 show_pwm_interpolate, store_pwm_interpolate, 0, 3),
539 SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
540 show_pwm_auto_point_channel,
541 store_pwm_auto_point_channel, 0, 3),
542 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
543 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
545 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
546 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
548 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
549 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
551 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
552 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
554 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
555 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
557 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
558 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
560 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
561 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
563 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
564 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
566 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
567 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
569 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
570 show_pwm_auto_point_temp_hyst,
571 store_pwm_auto_point_temp_hyst,
573 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
574 show_pwm_auto_point_temp_hyst, NULL, 1, 3),
575 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
576 show_pwm_auto_point_temp_hyst, NULL, 2, 3),
577 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
578 show_pwm_auto_point_temp_hyst, NULL, 3, 3),
582 /* Super I/O functions */
583 static inline int superio_inb(int base, int reg)
586 return inb(base + 1);
589 static int superio_inw(int base, int reg)
593 val = inb(base + 1) << 8;
595 val |= inb(base + 1);
599 static inline void superio_enter(int base)
601 /* according to the datasheet the key must be send twice! */
602 outb( SIO_UNLOCK_KEY, base);
603 outb( SIO_UNLOCK_KEY, base);
606 static inline void superio_select( int base, int ld)
608 outb(SIO_REG_LDSEL, base);
612 static inline void superio_exit(int base)
614 outb(SIO_LOCK_KEY, base);
617 static inline u16 fan_from_reg(u16 reg)
619 return reg ? (1500000 / reg) : 0;
622 static inline u16 fan_to_reg(u16 fan)
624 return fan ? (1500000 / fan) : 0;
627 static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
631 outb(reg, data->addr + ADDR_REG_OFFSET);
632 val = inb(data->addr + DATA_REG_OFFSET);
637 static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
641 outb(reg++, data->addr + ADDR_REG_OFFSET);
642 val = inb(data->addr + DATA_REG_OFFSET) << 8;
643 outb(reg, data->addr + ADDR_REG_OFFSET);
644 val |= inb(data->addr + DATA_REG_OFFSET);
649 static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
651 outb(reg, data->addr + ADDR_REG_OFFSET);
652 outb(val, data->addr + DATA_REG_OFFSET);
655 static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
657 outb(reg++, data->addr + ADDR_REG_OFFSET);
658 outb(val >> 8, data->addr + DATA_REG_OFFSET);
659 outb(reg, data->addr + ADDR_REG_OFFSET);
660 outb(val & 255, data->addr + DATA_REG_OFFSET);
663 static struct f71882fg_data *f71882fg_update_device(struct device *dev)
665 struct f71882fg_data *data = dev_get_drvdata(dev);
667 int nr_fans = (data->type == f71862fg) ? 3 : 4;
669 mutex_lock(&data->update_lock);
671 /* Update once every 60 seconds */
672 if ( time_after(jiffies, data->last_limits + 60 * HZ ) ||
674 if (data->type == f71882fg) {
676 f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
678 f71882fg_read8(data, F71882FG_REG_IN_BEEP);
681 /* Get High & boundary temps*/
682 for (nr = 1; nr < 4; nr++) {
683 data->temp_ovt[nr] = f71882fg_read8(data,
684 F71882FG_REG_TEMP_OVT(nr));
685 data->temp_high[nr] = f71882fg_read8(data,
686 F71882FG_REG_TEMP_HIGH(nr));
691 f71882fg_read8(data, F71882FG_REG_TEMP_HYST(0));
693 f71882fg_read8(data, F71882FG_REG_TEMP_HYST(1));
695 /* Have to hardcode type, because temp1 is special */
696 reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
697 reg2 = f71882fg_read8(data, F71882FG_REG_PECI);
698 if ((reg2 & 0x03) == 0x01)
699 data->temp_type[1] = 6 /* PECI */;
700 else if ((reg2 & 0x03) == 0x02)
701 data->temp_type[1] = 5 /* AMDSI */;
703 data->temp_type[1] = (reg & 0x02) ? 2 : 4;
705 data->temp_type[2] = (reg & 0x04) ? 2 : 4;
706 data->temp_type[3] = (reg & 0x08) ? 2 : 4;
708 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
710 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
712 data->pwm_enable = f71882fg_read8(data,
713 F71882FG_REG_PWM_ENABLE);
714 data->pwm_auto_point_hyst[0] =
715 f71882fg_read8(data, F71882FG_REG_FAN_HYST(0));
716 data->pwm_auto_point_hyst[1] =
717 f71882fg_read8(data, F71882FG_REG_FAN_HYST(1));
719 for (nr = 0; nr < nr_fans; nr++) {
720 data->pwm_auto_point_mapping[nr] =
722 F71882FG_REG_POINT_MAPPING(nr));
724 if (data->type == f71882fg) {
726 for (point = 0; point < 5; point++) {
727 data->pwm_auto_point_pwm[nr][point] =
729 F71882FG_REG_POINT_PWM
732 for (point = 0; point < 4; point++) {
733 data->pwm_auto_point_temp[nr][point] =
735 F71882FG_REG_POINT_TEMP
739 data->pwm_auto_point_pwm[nr][1] =
741 F71882FG_REG_POINT_PWM
743 data->pwm_auto_point_pwm[nr][4] =
745 F71882FG_REG_POINT_PWM
747 data->pwm_auto_point_temp[nr][0] =
749 F71882FG_REG_POINT_TEMP
751 data->pwm_auto_point_temp[nr][3] =
753 F71882FG_REG_POINT_TEMP
757 data->last_limits = jiffies;
760 /* Update every second */
761 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
762 data->temp_status = f71882fg_read8(data,
763 F71882FG_REG_TEMP_STATUS);
764 data->temp_diode_open = f71882fg_read8(data,
765 F71882FG_REG_TEMP_DIODE_OPEN);
766 for (nr = 1; nr < 4; nr++)
767 data->temp[nr] = f71882fg_read8(data,
768 F71882FG_REG_TEMP(nr));
770 data->fan_status = f71882fg_read8(data,
771 F71882FG_REG_FAN_STATUS);
772 for (nr = 0; nr < nr_fans; nr++) {
773 data->fan[nr] = f71882fg_read16(data,
774 F71882FG_REG_FAN(nr));
775 data->fan_target[nr] =
776 f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
777 data->fan_full_speed[nr] =
778 f71882fg_read16(data,
779 F71882FG_REG_FAN_FULL_SPEED(nr));
781 f71882fg_read8(data, F71882FG_REG_PWM(nr));
784 if (data->type == f71882fg)
785 data->in_status = f71882fg_read8(data,
786 F71882FG_REG_IN_STATUS);
787 for (nr = 0; nr < 9; nr++)
788 data->in[nr] = f71882fg_read8(data,
789 F71882FG_REG_IN(nr));
791 data->last_updated = jiffies;
795 mutex_unlock(&data->update_lock);
800 /* Sysfs Interface */
801 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
804 struct f71882fg_data *data = f71882fg_update_device(dev);
805 int nr = to_sensor_dev_attr_2(devattr)->index;
806 int speed = fan_from_reg(data->fan[nr]);
808 if (speed == FAN_MIN_DETECT)
811 return sprintf(buf, "%d\n", speed);
814 static ssize_t show_fan_full_speed(struct device *dev,
815 struct device_attribute *devattr, char *buf)
817 struct f71882fg_data *data = f71882fg_update_device(dev);
818 int nr = to_sensor_dev_attr_2(devattr)->index;
819 int speed = fan_from_reg(data->fan_full_speed[nr]);
820 return sprintf(buf, "%d\n", speed);
823 static ssize_t store_fan_full_speed(struct device *dev,
824 struct device_attribute *devattr,
825 const char *buf, size_t count)
827 struct f71882fg_data *data = dev_get_drvdata(dev);
828 int nr = to_sensor_dev_attr_2(devattr)->index;
829 long val = simple_strtol(buf, NULL, 10);
831 val = SENSORS_LIMIT(val, 23, 1500000);
832 val = fan_to_reg(val);
834 mutex_lock(&data->update_lock);
835 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
836 if (data->pwm_enable & (1 << (2 * nr)))
841 f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
842 data->fan_full_speed[nr] = val;
844 mutex_unlock(&data->update_lock);
849 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
852 struct f71882fg_data *data = f71882fg_update_device(dev);
853 int nr = to_sensor_dev_attr_2(devattr)->index;
855 if (data->fan_beep & (1 << nr))
856 return sprintf(buf, "1\n");
858 return sprintf(buf, "0\n");
861 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
862 *devattr, const char *buf, size_t count)
864 struct f71882fg_data *data = dev_get_drvdata(dev);
865 int nr = to_sensor_dev_attr_2(devattr)->index;
866 unsigned long val = simple_strtoul(buf, NULL, 10);
868 mutex_lock(&data->update_lock);
869 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
871 data->fan_beep |= 1 << nr;
873 data->fan_beep &= ~(1 << nr);
875 f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
876 mutex_unlock(&data->update_lock);
881 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
884 struct f71882fg_data *data = f71882fg_update_device(dev);
885 int nr = to_sensor_dev_attr_2(devattr)->index;
887 if (data->fan_status & (1 << nr))
888 return sprintf(buf, "1\n");
890 return sprintf(buf, "0\n");
893 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
896 struct f71882fg_data *data = f71882fg_update_device(dev);
897 int nr = to_sensor_dev_attr_2(devattr)->index;
899 return sprintf(buf, "%d\n", data->in[nr] * 8);
902 static ssize_t show_in_max(struct device *dev, struct device_attribute
905 struct f71882fg_data *data = f71882fg_update_device(dev);
907 return sprintf(buf, "%d\n", data->in1_max * 8);
910 static ssize_t store_in_max(struct device *dev, struct device_attribute
911 *devattr, const char *buf, size_t count)
913 struct f71882fg_data *data = dev_get_drvdata(dev);
914 long val = simple_strtol(buf, NULL, 10) / 8;
915 val = SENSORS_LIMIT(val, 0, 255);
917 mutex_lock(&data->update_lock);
918 f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
920 mutex_unlock(&data->update_lock);
925 static ssize_t show_in_beep(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 if (data->in_beep & (1 << nr))
932 return sprintf(buf, "1\n");
934 return sprintf(buf, "0\n");
937 static ssize_t store_in_beep(struct device *dev, struct device_attribute
938 *devattr, const char *buf, size_t count)
940 struct f71882fg_data *data = dev_get_drvdata(dev);
941 int nr = to_sensor_dev_attr_2(devattr)->index;
942 unsigned long val = simple_strtoul(buf, NULL, 10);
944 mutex_lock(&data->update_lock);
945 data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
947 data->in_beep |= 1 << nr;
949 data->in_beep &= ~(1 << nr);
951 f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
952 mutex_unlock(&data->update_lock);
957 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
960 struct f71882fg_data *data = f71882fg_update_device(dev);
961 int nr = to_sensor_dev_attr_2(devattr)->index;
963 if (data->in_status & (1 << nr))
964 return sprintf(buf, "1\n");
966 return sprintf(buf, "0\n");
969 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
972 struct f71882fg_data *data = f71882fg_update_device(dev);
973 int nr = to_sensor_dev_attr_2(devattr)->index;
975 return sprintf(buf, "%d\n", data->temp[nr] * 1000);
978 static ssize_t show_temp_max(struct device *dev, struct device_attribute
981 struct f71882fg_data *data = f71882fg_update_device(dev);
982 int nr = to_sensor_dev_attr_2(devattr)->index;
984 return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
987 static ssize_t store_temp_max(struct device *dev, struct device_attribute
988 *devattr, const char *buf, size_t count)
990 struct f71882fg_data *data = dev_get_drvdata(dev);
991 int nr = to_sensor_dev_attr_2(devattr)->index;
992 long val = simple_strtol(buf, NULL, 10) / 1000;
993 val = SENSORS_LIMIT(val, 0, 255);
995 mutex_lock(&data->update_lock);
996 f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
997 data->temp_high[nr] = val;
998 mutex_unlock(&data->update_lock);
1003 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
1004 *devattr, char *buf)
1006 struct f71882fg_data *data = f71882fg_update_device(dev);
1007 int nr = to_sensor_dev_attr_2(devattr)->index;
1010 mutex_lock(&data->update_lock);
1012 temp_max_hyst = data->temp_hyst[nr / 2] >> 4;
1014 temp_max_hyst = data->temp_hyst[nr / 2] & 0x0f;
1015 temp_max_hyst = (data->temp_high[nr] - temp_max_hyst) * 1000;
1016 mutex_unlock(&data->update_lock);
1018 return sprintf(buf, "%d\n", temp_max_hyst);
1021 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
1022 *devattr, const char *buf, size_t count)
1024 struct f71882fg_data *data = dev_get_drvdata(dev);
1025 int nr = to_sensor_dev_attr_2(devattr)->index;
1026 long val = simple_strtol(buf, NULL, 10) / 1000;
1027 ssize_t ret = count;
1030 mutex_lock(&data->update_lock);
1032 /* convert abs to relative and check */
1033 data->temp_high[nr] = f71882fg_read8(data, F71882FG_REG_TEMP_HIGH(nr));
1034 val = SENSORS_LIMIT(val, data->temp_high[nr] - 15,
1035 data->temp_high[nr]);
1036 val = data->temp_high[nr] - val;
1038 /* convert value to register contents */
1039 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST(nr / 2));
1041 reg = (reg & 0x0f) | (val << 4);
1043 reg = (reg & 0xf0) | val;
1044 f71882fg_write8(data, F71882FG_REG_TEMP_HYST(nr / 2), reg);
1045 data->temp_hyst[nr / 2] = reg;
1047 mutex_unlock(&data->update_lock);
1051 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
1052 *devattr, char *buf)
1054 struct f71882fg_data *data = f71882fg_update_device(dev);
1055 int nr = to_sensor_dev_attr_2(devattr)->index;
1057 return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
1060 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
1061 *devattr, const char *buf, size_t count)
1063 struct f71882fg_data *data = dev_get_drvdata(dev);
1064 int nr = to_sensor_dev_attr_2(devattr)->index;
1065 long val = simple_strtol(buf, NULL, 10) / 1000;
1066 val = SENSORS_LIMIT(val, 0, 255);
1068 mutex_lock(&data->update_lock);
1069 f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
1070 data->temp_ovt[nr] = val;
1071 mutex_unlock(&data->update_lock);
1076 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
1077 *devattr, char *buf)
1079 struct f71882fg_data *data = f71882fg_update_device(dev);
1080 int nr = to_sensor_dev_attr_2(devattr)->index;
1083 mutex_lock(&data->update_lock);
1085 temp_crit_hyst = data->temp_hyst[nr / 2] >> 4;
1087 temp_crit_hyst = data->temp_hyst[nr / 2] & 0x0f;
1088 temp_crit_hyst = (data->temp_ovt[nr] - temp_crit_hyst) * 1000;
1089 mutex_unlock(&data->update_lock);
1091 return sprintf(buf, "%d\n", temp_crit_hyst);
1094 static ssize_t show_temp_type(struct device *dev, struct device_attribute
1095 *devattr, char *buf)
1097 struct f71882fg_data *data = f71882fg_update_device(dev);
1098 int nr = to_sensor_dev_attr_2(devattr)->index;
1100 return sprintf(buf, "%d\n", data->temp_type[nr]);
1103 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
1104 *devattr, char *buf)
1106 struct f71882fg_data *data = f71882fg_update_device(dev);
1107 int nr = to_sensor_dev_attr_2(devattr)->index;
1109 if (data->temp_beep & (1 << nr))
1110 return sprintf(buf, "1\n");
1112 return sprintf(buf, "0\n");
1115 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
1116 *devattr, const char *buf, size_t count)
1118 struct f71882fg_data *data = dev_get_drvdata(dev);
1119 int nr = to_sensor_dev_attr_2(devattr)->index;
1120 unsigned long val = simple_strtoul(buf, NULL, 10);
1122 mutex_lock(&data->update_lock);
1123 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
1125 data->temp_beep |= 1 << nr;
1127 data->temp_beep &= ~(1 << nr);
1129 f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
1130 mutex_unlock(&data->update_lock);
1135 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
1136 *devattr, char *buf)
1138 struct f71882fg_data *data = f71882fg_update_device(dev);
1139 int nr = to_sensor_dev_attr_2(devattr)->index;
1141 if (data->temp_status & (1 << nr))
1142 return sprintf(buf, "1\n");
1144 return sprintf(buf, "0\n");
1147 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
1148 *devattr, char *buf)
1150 struct f71882fg_data *data = f71882fg_update_device(dev);
1151 int nr = to_sensor_dev_attr_2(devattr)->index;
1153 if (data->temp_diode_open & (1 << nr))
1154 return sprintf(buf, "1\n");
1156 return sprintf(buf, "0\n");
1159 static ssize_t show_pwm(struct device *dev,
1160 struct device_attribute *devattr, char *buf)
1162 struct f71882fg_data *data = f71882fg_update_device(dev);
1163 int val, nr = to_sensor_dev_attr_2(devattr)->index;
1164 mutex_lock(&data->update_lock);
1165 if (data->pwm_enable & (1 << (2 * nr)))
1167 val = data->pwm[nr];
1170 val = 255 * fan_from_reg(data->fan_target[nr])
1171 / fan_from_reg(data->fan_full_speed[nr]);
1173 mutex_unlock(&data->update_lock);
1174 return sprintf(buf, "%d\n", val);
1177 static ssize_t store_pwm(struct device *dev,
1178 struct device_attribute *devattr, const char *buf,
1181 struct f71882fg_data *data = dev_get_drvdata(dev);
1182 int nr = to_sensor_dev_attr_2(devattr)->index;
1183 long val = simple_strtol(buf, NULL, 10);
1184 val = SENSORS_LIMIT(val, 0, 255);
1186 mutex_lock(&data->update_lock);
1187 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1188 if (data->pwm_enable & (1 << (2 * nr))) {
1190 f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1191 data->pwm[nr] = val;
1194 int target, full_speed;
1195 full_speed = f71882fg_read16(data,
1196 F71882FG_REG_FAN_FULL_SPEED(nr));
1197 target = fan_to_reg(val * fan_from_reg(full_speed) / 255);
1198 f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr), target);
1199 data->fan_target[nr] = target;
1200 data->fan_full_speed[nr] = full_speed;
1202 mutex_unlock(&data->update_lock);
1207 static ssize_t show_pwm_enable(struct device *dev,
1208 struct device_attribute *devattr, char *buf)
1211 struct f71882fg_data *data = f71882fg_update_device(dev);
1212 int nr = to_sensor_dev_attr_2(devattr)->index;
1214 if (data->pwm_enable & (2 << (2 * nr)))
1219 return sprintf(buf, "%d\n", result);
1222 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1223 *devattr, const char *buf, size_t count)
1225 struct f71882fg_data *data = dev_get_drvdata(dev);
1226 int nr = to_sensor_dev_attr_2(devattr)->index;
1227 long val = simple_strtol(buf, NULL, 10);
1228 if (val < 1 || val > 2)
1231 mutex_lock(&data->update_lock);
1232 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1235 data->pwm_enable |= 2 << (2 * nr);
1238 data->pwm_enable &= ~(2 << (2 * nr));
1239 break; /* Temperature ctrl */
1241 if (data->type == f71882fg) {
1242 switch (fan_mode[nr]) {
1244 data->pwm_enable |= 1 << (2 * nr);
1245 break; /* Duty cycle mode */
1247 data->pwm_enable &= ~(1 << (2 * nr));
1248 break; /* RPM mode */
1251 f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
1252 mutex_unlock(&data->update_lock);
1257 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
1258 struct device_attribute *devattr,
1262 struct f71882fg_data *data = f71882fg_update_device(dev);
1263 int pwm = to_sensor_dev_attr_2(devattr)->index;
1264 int point = to_sensor_dev_attr_2(devattr)->nr;
1266 mutex_lock(&data->update_lock);
1267 if (data->pwm_enable & (1 << (2 * pwm))) {
1269 result = data->pwm_auto_point_pwm[pwm][point];
1272 result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
1274 mutex_unlock(&data->update_lock);
1276 return sprintf(buf, "%d\n", result);
1279 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
1280 struct device_attribute *devattr,
1281 const char *buf, size_t count)
1283 struct f71882fg_data *data = dev_get_drvdata(dev);
1284 int pwm = to_sensor_dev_attr_2(devattr)->index;
1285 int point = to_sensor_dev_attr_2(devattr)->nr;
1286 long val = simple_strtol(buf, NULL, 10);
1287 val = SENSORS_LIMIT(val, 0, 255);
1289 mutex_lock(&data->update_lock);
1290 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1291 if (data->pwm_enable & (1 << (2 * pwm))) {
1295 if (val < 29) /* Prevent negative numbers */
1298 val = (255 - val) * 32 / val;
1300 f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
1301 data->pwm_auto_point_pwm[pwm][point] = val;
1302 mutex_unlock(&data->update_lock);
1307 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
1308 struct device_attribute *devattr,
1312 struct f71882fg_data *data = f71882fg_update_device(dev);
1313 int nr = to_sensor_dev_attr_2(devattr)->index;
1314 int point = to_sensor_dev_attr_2(devattr)->nr;
1316 mutex_lock(&data->update_lock);
1318 result = data->pwm_auto_point_hyst[nr / 2] >> 4;
1320 result = data->pwm_auto_point_hyst[nr / 2] & 0x0f;
1321 result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
1322 mutex_unlock(&data->update_lock);
1324 return sprintf(buf, "%d\n", result);
1327 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
1328 struct device_attribute *devattr,
1329 const char *buf, size_t count)
1331 struct f71882fg_data *data = dev_get_drvdata(dev);
1332 int nr = to_sensor_dev_attr_2(devattr)->index;
1333 int point = to_sensor_dev_attr_2(devattr)->nr;
1334 long val = simple_strtol(buf, NULL, 10) / 1000;
1337 mutex_lock(&data->update_lock);
1338 data->pwm_auto_point_temp[nr][point] =
1339 f71882fg_read8(data, F71882FG_REG_POINT_TEMP(nr, point));
1340 val = SENSORS_LIMIT(val, data->pwm_auto_point_temp[nr][point] - 15,
1341 data->pwm_auto_point_temp[nr][point]);
1342 val = data->pwm_auto_point_temp[nr][point] - val;
1344 reg = f71882fg_read8(data, F71882FG_REG_FAN_HYST(nr / 2));
1346 reg = (reg & 0x0f) | (val << 4);
1348 reg = (reg & 0xf0) | val;
1350 f71882fg_write8(data, F71882FG_REG_FAN_HYST(nr / 2), reg);
1351 data->pwm_auto_point_hyst[nr / 2] = reg;
1352 mutex_unlock(&data->update_lock);
1357 static ssize_t show_pwm_interpolate(struct device *dev,
1358 struct device_attribute *devattr, char *buf)
1361 struct f71882fg_data *data = f71882fg_update_device(dev);
1362 int nr = to_sensor_dev_attr_2(devattr)->index;
1364 result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
1366 return sprintf(buf, "%d\n", result);
1369 static ssize_t store_pwm_interpolate(struct device *dev,
1370 struct device_attribute *devattr,
1371 const char *buf, size_t count)
1373 struct f71882fg_data *data = dev_get_drvdata(dev);
1374 int nr = to_sensor_dev_attr_2(devattr)->index;
1375 unsigned long val = simple_strtoul(buf, NULL, 10);
1377 mutex_lock(&data->update_lock);
1378 data->pwm_auto_point_mapping[nr] =
1379 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
1381 val = data->pwm_auto_point_mapping[nr] | (1 << 4);
1383 val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
1384 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1385 data->pwm_auto_point_mapping[nr] = val;
1386 mutex_unlock(&data->update_lock);
1391 static ssize_t show_pwm_auto_point_channel(struct device *dev,
1392 struct device_attribute *devattr,
1396 struct f71882fg_data *data = f71882fg_update_device(dev);
1397 int nr = to_sensor_dev_attr_2(devattr)->index;
1399 result = 1 << ((data->pwm_auto_point_mapping[nr] & 3) - 1);
1401 return sprintf(buf, "%d\n", result);
1404 static ssize_t store_pwm_auto_point_channel(struct device *dev,
1405 struct device_attribute *devattr,
1406 const char *buf, size_t count)
1408 struct f71882fg_data *data = dev_get_drvdata(dev);
1409 int nr = to_sensor_dev_attr_2(devattr)->index;
1410 long val = simple_strtol(buf, NULL, 10);
1424 mutex_lock(&data->update_lock);
1425 data->pwm_auto_point_mapping[nr] =
1426 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
1427 val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
1428 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1429 data->pwm_auto_point_mapping[nr] = val;
1430 mutex_unlock(&data->update_lock);
1435 static ssize_t show_pwm_auto_point_temp(struct device *dev,
1436 struct device_attribute *devattr,
1440 struct f71882fg_data *data = f71882fg_update_device(dev);
1441 int pwm = to_sensor_dev_attr_2(devattr)->index;
1442 int point = to_sensor_dev_attr_2(devattr)->nr;
1444 result = data->pwm_auto_point_temp[pwm][point];
1445 return sprintf(buf, "%d\n", 1000 * result);
1448 static ssize_t store_pwm_auto_point_temp(struct device *dev,
1449 struct device_attribute *devattr,
1450 const char *buf, size_t count)
1452 struct f71882fg_data *data = dev_get_drvdata(dev);
1453 int pwm = to_sensor_dev_attr_2(devattr)->index;
1454 int point = to_sensor_dev_attr_2(devattr)->nr;
1455 long val = simple_strtol(buf, NULL, 10) / 1000;
1456 val = SENSORS_LIMIT(val, 0, 255);
1458 mutex_lock(&data->update_lock);
1459 f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
1460 data->pwm_auto_point_temp[pwm][point] = val;
1461 mutex_unlock(&data->update_lock);
1466 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
1469 struct f71882fg_data *data = dev_get_drvdata(dev);
1470 return sprintf(buf, "%s\n", f71882fg_names[data->type]);
1473 static int __devinit f71882fg_create_sysfs_files(struct platform_device *pdev,
1474 struct sensor_device_attribute_2 *attr, int count)
1478 for (i = 0; i < count; i++) {
1479 err = device_create_file(&pdev->dev, &attr[i].dev_attr);
1486 static int __devinit f71882fg_probe(struct platform_device *pdev)
1488 struct f71882fg_data *data;
1489 struct f71882fg_sio_data *sio_data = pdev->dev.platform_data;
1493 data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL);
1497 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1498 data->type = sio_data->type;
1499 mutex_init(&data->update_lock);
1500 platform_set_drvdata(pdev, data);
1502 start_reg = f71882fg_read8(data, F71882FG_REG_START);
1503 if (start_reg & 0x04) {
1504 dev_warn(&pdev->dev, "Hardware monitor is powered down\n");
1508 if (!(start_reg & 0x03)) {
1509 dev_warn(&pdev->dev, "Hardware monitoring not activated\n");
1514 /* If it is a 71862 and the fan / pwm part is enabled sanity check
1516 if (data->type == f71862fg && (start_reg & 0x02)) {
1517 u8 reg = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1518 if ((reg & 0x15) != 0x15) {
1520 "Invalid (reserved) pwm settings: 0x%02x\n",
1527 /* Register sysfs interface files */
1528 err = device_create_file(&pdev->dev, &dev_attr_name);
1530 goto exit_unregister_sysfs;
1532 if (start_reg & 0x01) {
1533 err = f71882fg_create_sysfs_files(pdev, f718x2fg_in_temp_attr,
1534 ARRAY_SIZE(f718x2fg_in_temp_attr));
1536 goto exit_unregister_sysfs;
1538 if (data->type == f71882fg) {
1539 err = f71882fg_create_sysfs_files(pdev,
1540 f71882fg_in_temp_attr,
1541 ARRAY_SIZE(f71882fg_in_temp_attr));
1543 goto exit_unregister_sysfs;
1547 if (start_reg & 0x02) {
1548 err = f71882fg_create_sysfs_files(pdev, f718x2fg_fan_attr,
1549 ARRAY_SIZE(f718x2fg_fan_attr));
1551 goto exit_unregister_sysfs;
1553 if (data->type == f71862fg) {
1554 err = f71882fg_create_sysfs_files(pdev,
1556 ARRAY_SIZE(f71862fg_fan_attr));
1558 err = f71882fg_create_sysfs_files(pdev,
1560 ARRAY_SIZE(f71882fg_fan_attr));
1563 goto exit_unregister_sysfs;
1566 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1567 if (IS_ERR(data->hwmon_dev)) {
1568 err = PTR_ERR(data->hwmon_dev);
1569 data->hwmon_dev = NULL;
1570 goto exit_unregister_sysfs;
1575 exit_unregister_sysfs:
1576 f71882fg_remove(pdev); /* Will unregister the sysfs files for us */
1577 return err; /* f71882fg_remove() also frees our data */
1583 static int f71882fg_remove(struct platform_device *pdev)
1586 struct f71882fg_data *data = platform_get_drvdata(pdev);
1588 platform_set_drvdata(pdev, NULL);
1589 if (data->hwmon_dev)
1590 hwmon_device_unregister(data->hwmon_dev);
1592 device_remove_file(&pdev->dev, &dev_attr_name);
1594 for (i = 0; i < ARRAY_SIZE(f718x2fg_in_temp_attr); i++)
1595 device_remove_file(&pdev->dev,
1596 &f718x2fg_in_temp_attr[i].dev_attr);
1598 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
1599 device_remove_file(&pdev->dev,
1600 &f71882fg_in_temp_attr[i].dev_attr);
1602 for (i = 0; i < ARRAY_SIZE(f718x2fg_fan_attr); i++)
1603 device_remove_file(&pdev->dev, &f718x2fg_fan_attr[i].dev_attr);
1605 for (i = 0; i < ARRAY_SIZE(f71862fg_fan_attr); i++)
1606 device_remove_file(&pdev->dev, &f71862fg_fan_attr[i].dev_attr);
1608 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
1609 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
1616 static int __init f71882fg_find(int sioaddr, unsigned short *address,
1617 struct f71882fg_sio_data *sio_data)
1622 superio_enter(sioaddr);
1624 devid = superio_inw(sioaddr, SIO_REG_MANID);
1625 if (devid != SIO_FINTEK_ID) {
1626 printk(KERN_INFO DRVNAME ": Not a Fintek device\n");
1630 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
1633 sio_data->type = f71862fg;
1636 sio_data->type = f71882fg;
1639 printk(KERN_INFO DRVNAME ": Unsupported Fintek device\n");
1643 superio_select(sioaddr, SIO_F71882FG_LD_HWM);
1644 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
1645 printk(KERN_WARNING DRVNAME ": Device not activated\n");
1649 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1652 printk(KERN_WARNING DRVNAME ": Base address not set\n");
1655 *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
1658 printk(KERN_INFO DRVNAME ": Found %s chip at %#x, revision %d\n",
1659 f71882fg_names[sio_data->type], (unsigned int)*address,
1660 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
1662 superio_exit(sioaddr);
1666 static int __init f71882fg_device_add(unsigned short address,
1667 const struct f71882fg_sio_data *sio_data)
1669 struct resource res = {
1671 .end = address + REGION_LENGTH - 1,
1672 .flags = IORESOURCE_IO,
1676 f71882fg_pdev = platform_device_alloc(DRVNAME, address);
1680 res.name = f71882fg_pdev->name;
1681 err = platform_device_add_resources(f71882fg_pdev, &res, 1);
1683 printk(KERN_ERR DRVNAME ": Device resource addition failed\n");
1684 goto exit_device_put;
1687 err = platform_device_add_data(f71882fg_pdev, sio_data,
1688 sizeof(struct f71882fg_sio_data));
1690 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1691 goto exit_device_put;
1694 err = platform_device_add(f71882fg_pdev);
1696 printk(KERN_ERR DRVNAME ": Device addition failed\n");
1697 goto exit_device_put;
1703 platform_device_put(f71882fg_pdev);
1708 static int __init f71882fg_init(void)
1711 unsigned short address;
1712 struct f71882fg_sio_data sio_data;
1714 memset(&sio_data, 0, sizeof(sio_data));
1716 if (f71882fg_find(0x2e, &address, &sio_data) &&
1717 f71882fg_find(0x4e, &address, &sio_data))
1720 err = platform_driver_register(&f71882fg_driver);
1724 err = f71882fg_device_add(address, &sio_data);
1731 platform_driver_unregister(&f71882fg_driver);
1736 static void __exit f71882fg_exit(void)
1738 platform_device_unregister(f71882fg_pdev);
1739 platform_driver_unregister(&f71882fg_driver);
1742 MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
1743 MODULE_AUTHOR("Hans Edgington, Hans de Goede (hdegoede@redhat.com)");
1744 MODULE_LICENSE("GPL");
1746 module_init(f71882fg_init);
1747 module_exit(f71882fg_exit);