hwmon/f71882fg: Separate temp beep sysfs attr from the other temp sysfs attr
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / f71882fg.c
blob62352fd397df5354c52e988de0f877d9e5752867
1 /***************************************************************************
2 * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
3 * Copyright (C) 2007-2011 Hans de Goede <hdegoede@redhat.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/jiffies.h>
27 #include <linux/platform_device.h>
28 #include <linux/hwmon.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <linux/err.h>
31 #include <linux/mutex.h>
32 #include <linux/io.h>
33 #include <linux/acpi.h>
35 #define DRVNAME "f71882fg"
37 #define SIO_F71858FG_LD_HWM 0x02 /* Hardware monitor logical device */
38 #define SIO_F71882FG_LD_HWM 0x04 /* Hardware monitor logical device */
39 #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
40 #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
42 #define SIO_REG_LDSEL 0x07 /* Logical device select */
43 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
44 #define SIO_REG_DEVREV 0x22 /* Device revision */
45 #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
46 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
47 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
49 #define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
50 #define SIO_F71858_ID 0x0507 /* Chipset ID */
51 #define SIO_F71862_ID 0x0601 /* Chipset ID */
52 #define SIO_F71882_ID 0x0541 /* Chipset ID */
53 #define SIO_F71889_ID 0x0723 /* Chipset ID */
54 #define SIO_F8000_ID 0x0581 /* Chipset ID */
56 #define REGION_LENGTH 8
57 #define ADDR_REG_OFFSET 5
58 #define DATA_REG_OFFSET 6
60 #define F71882FG_REG_IN_STATUS 0x12 /* f71882fg only */
61 #define F71882FG_REG_IN_BEEP 0x13 /* f71882fg only */
62 #define F71882FG_REG_IN(nr) (0x20 + (nr))
63 #define F71882FG_REG_IN1_HIGH 0x32 /* f71882fg only */
65 #define F71882FG_REG_FAN(nr) (0xA0 + (16 * (nr)))
66 #define F71882FG_REG_FAN_TARGET(nr) (0xA2 + (16 * (nr)))
67 #define F71882FG_REG_FAN_FULL_SPEED(nr) (0xA4 + (16 * (nr)))
68 #define F71882FG_REG_FAN_STATUS 0x92
69 #define F71882FG_REG_FAN_BEEP 0x93
71 #define F71882FG_REG_TEMP(nr) (0x70 + 2 * (nr))
72 #define F71882FG_REG_TEMP_OVT(nr) (0x80 + 2 * (nr))
73 #define F71882FG_REG_TEMP_HIGH(nr) (0x81 + 2 * (nr))
74 #define F71882FG_REG_TEMP_STATUS 0x62
75 #define F71882FG_REG_TEMP_BEEP 0x63
76 #define F71882FG_REG_TEMP_CONFIG 0x69
77 #define F71882FG_REG_TEMP_HYST(nr) (0x6C + (nr))
78 #define F71882FG_REG_TEMP_TYPE 0x6B
79 #define F71882FG_REG_TEMP_DIODE_OPEN 0x6F
81 #define F71882FG_REG_PWM(nr) (0xA3 + (16 * (nr)))
82 #define F71882FG_REG_PWM_TYPE 0x94
83 #define F71882FG_REG_PWM_ENABLE 0x96
85 #define F71882FG_REG_FAN_HYST(nr) (0x98 + (nr))
87 #define F71882FG_REG_FAN_FAULT_T 0x9F
88 #define F71882FG_FAN_NEG_TEMP_EN 0x20
90 #define F71882FG_REG_POINT_PWM(pwm, point) (0xAA + (point) + (16 * (pwm)))
91 #define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm)))
92 #define F71882FG_REG_POINT_MAPPING(nr) (0xAF + 16 * (nr))
94 #define F71882FG_REG_START 0x01
96 #define F71882FG_MAX_INS 9
98 #define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */
100 static unsigned short force_id;
101 module_param(force_id, ushort, 0);
102 MODULE_PARM_DESC(force_id, "Override the detected device ID");
104 enum chips { f71858fg, f71862fg, f71882fg, f71889fg, f8000 };
106 static const char *f71882fg_names[] = {
107 "f71858fg",
108 "f71862fg",
109 "f71882fg",
110 "f71889fg",
111 "f8000",
114 static const char f71882fg_has_in[5][F71882FG_MAX_INS] = {
115 { 1, 1, 1, 0, 0, 0, 0, 0, 0 }, /* f71858fg */
116 { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* f71862fg */
117 { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* f71882fg */
118 { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* f71889fg */
119 { 1, 1, 1, 0, 0, 0, 0, 0, 0 }, /* f8000 */
122 static const char f71882fg_has_in1_alarm[5] = {
123 0, /* f71858fg */
124 0, /* f71862fg */
125 1, /* f71882fg */
126 1, /* f71889fg */
127 0, /* f8000 */
130 static const char f71882fg_has_beep[5] = {
131 0, /* f71858fg */
132 1, /* f71862fg */
133 1, /* f71882fg */
134 1, /* f71889fg */
135 0, /* f8000 */
138 static struct platform_device *f71882fg_pdev;
140 /* Super-I/O Function prototypes */
141 static inline int superio_inb(int base, int reg);
142 static inline int superio_inw(int base, int reg);
143 static inline int superio_enter(int base);
144 static inline void superio_select(int base, int ld);
145 static inline void superio_exit(int base);
147 struct f71882fg_sio_data {
148 enum chips type;
151 struct f71882fg_data {
152 unsigned short addr;
153 enum chips type;
154 struct device *hwmon_dev;
156 struct mutex update_lock;
157 int temp_start; /* temp numbering start (0 or 1) */
158 char valid; /* !=0 if following fields are valid */
159 char auto_point_temp_signed;
160 unsigned long last_updated; /* In jiffies */
161 unsigned long last_limits; /* In jiffies */
163 /* Register Values */
164 u8 in[F71882FG_MAX_INS];
165 u8 in1_max;
166 u8 in_status;
167 u8 in_beep;
168 u16 fan[4];
169 u16 fan_target[4];
170 u16 fan_full_speed[4];
171 u8 fan_status;
172 u8 fan_beep;
173 /* Note: all models have only 3 temperature channels, but on some
174 they are addressed as 0-2 and on others as 1-3, so for coding
175 convenience we reserve space for 4 channels */
176 u16 temp[4];
177 u8 temp_ovt[4];
178 u8 temp_high[4];
179 u8 temp_hyst[2]; /* 2 hysts stored per reg */
180 u8 temp_type[4];
181 u8 temp_status;
182 u8 temp_beep;
183 u8 temp_diode_open;
184 u8 temp_config;
185 u8 pwm[4];
186 u8 pwm_enable;
187 u8 pwm_auto_point_hyst[2];
188 u8 pwm_auto_point_mapping[4];
189 u8 pwm_auto_point_pwm[4][5];
190 s8 pwm_auto_point_temp[4][4];
193 /* Sysfs in */
194 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
195 char *buf);
196 static ssize_t show_in_max(struct device *dev, struct device_attribute
197 *devattr, char *buf);
198 static ssize_t store_in_max(struct device *dev, struct device_attribute
199 *devattr, const char *buf, size_t count);
200 static ssize_t show_in_beep(struct device *dev, struct device_attribute
201 *devattr, char *buf);
202 static ssize_t store_in_beep(struct device *dev, struct device_attribute
203 *devattr, const char *buf, size_t count);
204 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
205 *devattr, char *buf);
206 /* Sysfs Fan */
207 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
208 char *buf);
209 static ssize_t show_fan_full_speed(struct device *dev,
210 struct device_attribute *devattr, char *buf);
211 static ssize_t store_fan_full_speed(struct device *dev,
212 struct device_attribute *devattr, const char *buf, size_t count);
213 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
214 *devattr, char *buf);
215 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
216 *devattr, const char *buf, size_t count);
217 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
218 *devattr, char *buf);
219 /* Sysfs Temp */
220 static ssize_t show_temp(struct device *dev, struct device_attribute
221 *devattr, char *buf);
222 static ssize_t show_temp_max(struct device *dev, struct device_attribute
223 *devattr, char *buf);
224 static ssize_t store_temp_max(struct device *dev, struct device_attribute
225 *devattr, const char *buf, size_t count);
226 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
227 *devattr, char *buf);
228 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
229 *devattr, const char *buf, size_t count);
230 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
231 *devattr, char *buf);
232 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
233 *devattr, const char *buf, size_t count);
234 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
235 *devattr, char *buf);
236 static ssize_t show_temp_type(struct device *dev, struct device_attribute
237 *devattr, char *buf);
238 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
239 *devattr, char *buf);
240 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
241 *devattr, const char *buf, size_t count);
242 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
243 *devattr, char *buf);
244 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
245 *devattr, char *buf);
246 /* PWM and Auto point control */
247 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
248 char *buf);
249 static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
250 const char *buf, size_t count);
251 static ssize_t show_pwm_enable(struct device *dev,
252 struct device_attribute *devattr, char *buf);
253 static ssize_t store_pwm_enable(struct device *dev,
254 struct device_attribute *devattr, const char *buf, size_t count);
255 static ssize_t show_pwm_interpolate(struct device *dev,
256 struct device_attribute *devattr, char *buf);
257 static ssize_t store_pwm_interpolate(struct device *dev,
258 struct device_attribute *devattr, const char *buf, size_t count);
259 static ssize_t show_pwm_auto_point_channel(struct device *dev,
260 struct device_attribute *devattr, char *buf);
261 static ssize_t store_pwm_auto_point_channel(struct device *dev,
262 struct device_attribute *devattr, const char *buf, size_t count);
263 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
264 struct device_attribute *devattr, char *buf);
265 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
266 struct device_attribute *devattr, const char *buf, size_t count);
267 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
268 struct device_attribute *devattr, char *buf);
269 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
270 struct device_attribute *devattr, const char *buf, size_t count);
271 static ssize_t show_pwm_auto_point_temp(struct device *dev,
272 struct device_attribute *devattr, char *buf);
273 static ssize_t store_pwm_auto_point_temp(struct device *dev,
274 struct device_attribute *devattr, const char *buf, size_t count);
275 /* Sysfs misc */
276 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
277 char *buf);
279 static int __devinit f71882fg_probe(struct platform_device * pdev);
280 static int f71882fg_remove(struct platform_device *pdev);
282 static struct platform_driver f71882fg_driver = {
283 .driver = {
284 .owner = THIS_MODULE,
285 .name = DRVNAME,
287 .probe = f71882fg_probe,
288 .remove = f71882fg_remove,
291 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
293 /* Temp attr for the f71858fg, the f71858fg is special as it has its
294 temperature indexes start at 0 (the others start at 1) */
295 static struct sensor_device_attribute_2 f71858fg_temp_attr[] = {
296 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
297 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
298 store_temp_max, 0, 0),
299 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
300 store_temp_max_hyst, 0, 0),
301 SENSOR_ATTR_2(temp1_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 0),
302 SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
303 store_temp_crit, 0, 0),
304 SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
305 0, 0),
306 SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 4),
307 SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
308 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
309 SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
310 store_temp_max, 0, 1),
311 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
312 store_temp_max_hyst, 0, 1),
313 SENSOR_ATTR_2(temp2_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
314 SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
315 store_temp_crit, 0, 1),
316 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
317 0, 1),
318 SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
319 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
320 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
321 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
322 store_temp_max, 0, 2),
323 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
324 store_temp_max_hyst, 0, 2),
325 SENSOR_ATTR_2(temp3_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
326 SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
327 store_temp_crit, 0, 2),
328 SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
329 0, 2),
330 SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
331 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
334 /* Temp attr for the standard models */
335 static struct sensor_device_attribute_2 fxxxx_temp_attr[3][9] = { {
336 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1),
337 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
338 store_temp_max, 0, 1),
339 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
340 store_temp_max_hyst, 0, 1),
341 /* Should really be temp1_max_alarm, but older versions did not handle
342 the max and crit alarms separately and lm_sensors v2 depends on the
343 presence of temp#_alarm files. The same goes for temp2/3 _alarm. */
344 SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
345 SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
346 store_temp_crit, 0, 1),
347 SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
348 0, 1),
349 SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
350 SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1),
351 SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
352 }, {
353 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2),
354 SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
355 store_temp_max, 0, 2),
356 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
357 store_temp_max_hyst, 0, 2),
358 /* Should be temp2_max_alarm, see temp1_alarm note */
359 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
360 SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
361 store_temp_crit, 0, 2),
362 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
363 0, 2),
364 SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
365 SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2),
366 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
367 }, {
368 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3),
369 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
370 store_temp_max, 0, 3),
371 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
372 store_temp_max_hyst, 0, 3),
373 /* Should be temp3_max_alarm, see temp1_alarm note */
374 SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3),
375 SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
376 store_temp_crit, 0, 3),
377 SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
378 0, 3),
379 SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 7),
380 SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3),
381 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3),
382 } };
384 /* Temp attr for models which can beep on temp alarm */
385 static struct sensor_device_attribute_2 fxxxx_temp_beep_attr[3][2] = { {
386 SENSOR_ATTR_2(temp1_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
387 store_temp_beep, 0, 1),
388 SENSOR_ATTR_2(temp1_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
389 store_temp_beep, 0, 5),
390 }, {
391 SENSOR_ATTR_2(temp2_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
392 store_temp_beep, 0, 2),
393 SENSOR_ATTR_2(temp2_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
394 store_temp_beep, 0, 6),
395 }, {
396 SENSOR_ATTR_2(temp3_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
397 store_temp_beep, 0, 3),
398 SENSOR_ATTR_2(temp3_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
399 store_temp_beep, 0, 7),
400 } };
402 /* Temp attr for the f8000
403 Note on the f8000 temp_ovt (crit) is used as max, and temp_high (max)
404 is used as hysteresis value to clear alarms
405 Also like the f71858fg its temperature indexes start at 0
407 static struct sensor_device_attribute_2 f8000_temp_attr[] = {
408 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
409 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_crit,
410 store_temp_crit, 0, 0),
411 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
412 store_temp_max, 0, 0),
413 SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 4),
414 SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
415 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
416 SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_crit,
417 store_temp_crit, 0, 1),
418 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
419 store_temp_max, 0, 1),
420 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
421 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
422 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
423 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_crit,
424 store_temp_crit, 0, 2),
425 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
426 store_temp_max, 0, 2),
427 SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
428 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
431 /* in attr for all models */
432 static struct sensor_device_attribute_2 fxxxx_in_attr[] = {
433 SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
434 SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
435 SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
436 SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
437 SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
438 SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
439 SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
440 SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
441 SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
444 /* For models with in1 alarm capability */
445 static struct sensor_device_attribute_2 fxxxx_in1_alarm_attr[] = {
446 SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
447 0, 1),
448 SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
449 0, 1),
450 SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
453 /* Fan / PWM attr common to all models */
454 static struct sensor_device_attribute_2 fxxxx_fan_attr[4][6] = { {
455 SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
456 SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
457 show_fan_full_speed,
458 store_fan_full_speed, 0, 0),
459 SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
460 SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
461 SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
462 store_pwm_enable, 0, 0),
463 SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
464 show_pwm_interpolate, store_pwm_interpolate, 0, 0),
465 }, {
466 SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
467 SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
468 show_fan_full_speed,
469 store_fan_full_speed, 0, 1),
470 SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
471 SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
472 SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
473 store_pwm_enable, 0, 1),
474 SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
475 show_pwm_interpolate, store_pwm_interpolate, 0, 1),
476 }, {
477 SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
478 SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
479 show_fan_full_speed,
480 store_fan_full_speed, 0, 2),
481 SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
482 SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
483 SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
484 store_pwm_enable, 0, 2),
485 SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
486 show_pwm_interpolate, store_pwm_interpolate, 0, 2),
487 }, {
488 SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
489 SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
490 show_fan_full_speed,
491 store_fan_full_speed, 0, 3),
492 SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
493 SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
494 SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
495 store_pwm_enable, 0, 3),
496 SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
497 show_pwm_interpolate, store_pwm_interpolate, 0, 3),
498 } };
500 /* Attr for models which can beep on Fan alarm */
501 static struct sensor_device_attribute_2 fxxxx_fan_beep_attr[] = {
502 SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
503 store_fan_beep, 0, 0),
504 SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
505 store_fan_beep, 0, 1),
506 SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
507 store_fan_beep, 0, 2),
508 SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
509 store_fan_beep, 0, 3),
512 /* PWM attr for the f71862fg, fewer pwms and fewer zones per pwm than the
513 f71858fg / f71882fg / f71889fg */
514 static struct sensor_device_attribute_2 f71862fg_auto_pwm_attr[] = {
515 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
516 show_pwm_auto_point_channel,
517 store_pwm_auto_point_channel, 0, 0),
518 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
519 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
520 1, 0),
521 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
522 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
523 4, 0),
524 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
525 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
526 0, 0),
527 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
528 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
529 3, 0),
530 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
531 show_pwm_auto_point_temp_hyst,
532 store_pwm_auto_point_temp_hyst,
533 0, 0),
534 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
535 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
537 SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
538 show_pwm_auto_point_channel,
539 store_pwm_auto_point_channel, 0, 1),
540 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
541 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
542 1, 1),
543 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
544 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
545 4, 1),
546 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
547 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
548 0, 1),
549 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
550 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
551 3, 1),
552 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
553 show_pwm_auto_point_temp_hyst,
554 store_pwm_auto_point_temp_hyst,
555 0, 1),
556 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
557 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
559 SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
560 show_pwm_auto_point_channel,
561 store_pwm_auto_point_channel, 0, 2),
562 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
563 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
564 1, 2),
565 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
566 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
567 4, 2),
568 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
569 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
570 0, 2),
571 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
572 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
573 3, 2),
574 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
575 show_pwm_auto_point_temp_hyst,
576 store_pwm_auto_point_temp_hyst,
577 0, 2),
578 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
579 show_pwm_auto_point_temp_hyst, NULL, 3, 2),
582 /* PWM attr common to the f71858fg, f71882fg and f71889fg */
583 static struct sensor_device_attribute_2 fxxxx_auto_pwm_attr[4][14] = { {
584 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
585 show_pwm_auto_point_channel,
586 store_pwm_auto_point_channel, 0, 0),
587 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
588 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
589 0, 0),
590 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
591 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
592 1, 0),
593 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
594 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
595 2, 0),
596 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
597 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
598 3, 0),
599 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
600 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
601 4, 0),
602 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
603 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
604 0, 0),
605 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
606 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
607 1, 0),
608 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
609 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
610 2, 0),
611 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
612 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
613 3, 0),
614 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
615 show_pwm_auto_point_temp_hyst,
616 store_pwm_auto_point_temp_hyst,
617 0, 0),
618 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
619 show_pwm_auto_point_temp_hyst, NULL, 1, 0),
620 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
621 show_pwm_auto_point_temp_hyst, NULL, 2, 0),
622 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
623 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
624 }, {
625 SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
626 show_pwm_auto_point_channel,
627 store_pwm_auto_point_channel, 0, 1),
628 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
629 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
630 0, 1),
631 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
632 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
633 1, 1),
634 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
635 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
636 2, 1),
637 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
638 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
639 3, 1),
640 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
641 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
642 4, 1),
643 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
644 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
645 0, 1),
646 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
647 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
648 1, 1),
649 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
650 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
651 2, 1),
652 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
653 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
654 3, 1),
655 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
656 show_pwm_auto_point_temp_hyst,
657 store_pwm_auto_point_temp_hyst,
658 0, 1),
659 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
660 show_pwm_auto_point_temp_hyst, NULL, 1, 1),
661 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
662 show_pwm_auto_point_temp_hyst, NULL, 2, 1),
663 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
664 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
665 }, {
666 SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
667 show_pwm_auto_point_channel,
668 store_pwm_auto_point_channel, 0, 2),
669 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
670 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
671 0, 2),
672 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
673 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
674 1, 2),
675 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
676 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
677 2, 2),
678 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
679 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
680 3, 2),
681 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
682 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
683 4, 2),
684 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
685 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
686 0, 2),
687 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
688 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
689 1, 2),
690 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
691 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
692 2, 2),
693 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
694 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
695 3, 2),
696 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
697 show_pwm_auto_point_temp_hyst,
698 store_pwm_auto_point_temp_hyst,
699 0, 2),
700 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
701 show_pwm_auto_point_temp_hyst, NULL, 1, 2),
702 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
703 show_pwm_auto_point_temp_hyst, NULL, 2, 2),
704 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
705 show_pwm_auto_point_temp_hyst, NULL, 3, 2),
706 }, {
707 SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
708 show_pwm_auto_point_channel,
709 store_pwm_auto_point_channel, 0, 3),
710 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
711 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
712 0, 3),
713 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
714 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
715 1, 3),
716 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
717 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
718 2, 3),
719 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
720 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
721 3, 3),
722 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
723 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
724 4, 3),
725 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
726 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
727 0, 3),
728 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
729 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
730 1, 3),
731 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
732 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
733 2, 3),
734 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
735 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
736 3, 3),
737 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
738 show_pwm_auto_point_temp_hyst,
739 store_pwm_auto_point_temp_hyst,
740 0, 3),
741 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
742 show_pwm_auto_point_temp_hyst, NULL, 1, 3),
743 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
744 show_pwm_auto_point_temp_hyst, NULL, 2, 3),
745 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
746 show_pwm_auto_point_temp_hyst, NULL, 3, 3),
747 } };
749 /* Fan attr specific to the f8000 (4th fan input can only measure speed) */
750 static struct sensor_device_attribute_2 f8000_fan_attr[] = {
751 SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
754 /* PWM attr for the f8000, zones mapped to temp instead of to pwm!
755 Also the register block at offset A0 maps to TEMP1 (so our temp2, as the
756 F8000 starts counting temps at 0), B0 maps the TEMP2 and C0 maps to TEMP0 */
757 static struct sensor_device_attribute_2 f8000_auto_pwm_attr[] = {
758 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
759 show_pwm_auto_point_channel,
760 store_pwm_auto_point_channel, 0, 0),
761 SENSOR_ATTR_2(temp1_auto_point1_pwm, S_IRUGO|S_IWUSR,
762 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
763 0, 2),
764 SENSOR_ATTR_2(temp1_auto_point2_pwm, S_IRUGO|S_IWUSR,
765 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
766 1, 2),
767 SENSOR_ATTR_2(temp1_auto_point3_pwm, S_IRUGO|S_IWUSR,
768 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
769 2, 2),
770 SENSOR_ATTR_2(temp1_auto_point4_pwm, S_IRUGO|S_IWUSR,
771 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
772 3, 2),
773 SENSOR_ATTR_2(temp1_auto_point5_pwm, S_IRUGO|S_IWUSR,
774 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
775 4, 2),
776 SENSOR_ATTR_2(temp1_auto_point1_temp, S_IRUGO|S_IWUSR,
777 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
778 0, 2),
779 SENSOR_ATTR_2(temp1_auto_point2_temp, S_IRUGO|S_IWUSR,
780 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
781 1, 2),
782 SENSOR_ATTR_2(temp1_auto_point3_temp, S_IRUGO|S_IWUSR,
783 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
784 2, 2),
785 SENSOR_ATTR_2(temp1_auto_point4_temp, S_IRUGO|S_IWUSR,
786 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
787 3, 2),
788 SENSOR_ATTR_2(temp1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
789 show_pwm_auto_point_temp_hyst,
790 store_pwm_auto_point_temp_hyst,
791 0, 2),
792 SENSOR_ATTR_2(temp1_auto_point2_temp_hyst, S_IRUGO,
793 show_pwm_auto_point_temp_hyst, NULL, 1, 2),
794 SENSOR_ATTR_2(temp1_auto_point3_temp_hyst, S_IRUGO,
795 show_pwm_auto_point_temp_hyst, NULL, 2, 2),
796 SENSOR_ATTR_2(temp1_auto_point4_temp_hyst, S_IRUGO,
797 show_pwm_auto_point_temp_hyst, NULL, 3, 2),
799 SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
800 show_pwm_auto_point_channel,
801 store_pwm_auto_point_channel, 0, 1),
802 SENSOR_ATTR_2(temp2_auto_point1_pwm, S_IRUGO|S_IWUSR,
803 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
804 0, 0),
805 SENSOR_ATTR_2(temp2_auto_point2_pwm, S_IRUGO|S_IWUSR,
806 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
807 1, 0),
808 SENSOR_ATTR_2(temp2_auto_point3_pwm, S_IRUGO|S_IWUSR,
809 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
810 2, 0),
811 SENSOR_ATTR_2(temp2_auto_point4_pwm, S_IRUGO|S_IWUSR,
812 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
813 3, 0),
814 SENSOR_ATTR_2(temp2_auto_point5_pwm, S_IRUGO|S_IWUSR,
815 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
816 4, 0),
817 SENSOR_ATTR_2(temp2_auto_point1_temp, S_IRUGO|S_IWUSR,
818 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
819 0, 0),
820 SENSOR_ATTR_2(temp2_auto_point2_temp, S_IRUGO|S_IWUSR,
821 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
822 1, 0),
823 SENSOR_ATTR_2(temp2_auto_point3_temp, S_IRUGO|S_IWUSR,
824 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
825 2, 0),
826 SENSOR_ATTR_2(temp2_auto_point4_temp, S_IRUGO|S_IWUSR,
827 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
828 3, 0),
829 SENSOR_ATTR_2(temp2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
830 show_pwm_auto_point_temp_hyst,
831 store_pwm_auto_point_temp_hyst,
832 0, 0),
833 SENSOR_ATTR_2(temp2_auto_point2_temp_hyst, S_IRUGO,
834 show_pwm_auto_point_temp_hyst, NULL, 1, 0),
835 SENSOR_ATTR_2(temp2_auto_point3_temp_hyst, S_IRUGO,
836 show_pwm_auto_point_temp_hyst, NULL, 2, 0),
837 SENSOR_ATTR_2(temp2_auto_point4_temp_hyst, S_IRUGO,
838 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
840 SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
841 show_pwm_auto_point_channel,
842 store_pwm_auto_point_channel, 0, 2),
843 SENSOR_ATTR_2(temp3_auto_point1_pwm, S_IRUGO|S_IWUSR,
844 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
845 0, 1),
846 SENSOR_ATTR_2(temp3_auto_point2_pwm, S_IRUGO|S_IWUSR,
847 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
848 1, 1),
849 SENSOR_ATTR_2(temp3_auto_point3_pwm, S_IRUGO|S_IWUSR,
850 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
851 2, 1),
852 SENSOR_ATTR_2(temp3_auto_point4_pwm, S_IRUGO|S_IWUSR,
853 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
854 3, 1),
855 SENSOR_ATTR_2(temp3_auto_point5_pwm, S_IRUGO|S_IWUSR,
856 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
857 4, 1),
858 SENSOR_ATTR_2(temp3_auto_point1_temp, S_IRUGO|S_IWUSR,
859 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
860 0, 1),
861 SENSOR_ATTR_2(temp3_auto_point2_temp, S_IRUGO|S_IWUSR,
862 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
863 1, 1),
864 SENSOR_ATTR_2(temp3_auto_point3_temp, S_IRUGO|S_IWUSR,
865 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
866 2, 1),
867 SENSOR_ATTR_2(temp3_auto_point4_temp, S_IRUGO|S_IWUSR,
868 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
869 3, 1),
870 SENSOR_ATTR_2(temp3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
871 show_pwm_auto_point_temp_hyst,
872 store_pwm_auto_point_temp_hyst,
873 0, 1),
874 SENSOR_ATTR_2(temp3_auto_point2_temp_hyst, S_IRUGO,
875 show_pwm_auto_point_temp_hyst, NULL, 1, 1),
876 SENSOR_ATTR_2(temp3_auto_point3_temp_hyst, S_IRUGO,
877 show_pwm_auto_point_temp_hyst, NULL, 2, 1),
878 SENSOR_ATTR_2(temp3_auto_point4_temp_hyst, S_IRUGO,
879 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
882 /* Super I/O functions */
883 static inline int superio_inb(int base, int reg)
885 outb(reg, base);
886 return inb(base + 1);
889 static int superio_inw(int base, int reg)
891 int val;
892 val = superio_inb(base, reg) << 8;
893 val |= superio_inb(base, reg + 1);
894 return val;
897 static inline int superio_enter(int base)
899 /* Don't step on other drivers' I/O space by accident */
900 if (!request_muxed_region(base, 2, DRVNAME)) {
901 pr_err("I/O address 0x%04x already in use\n", base);
902 return -EBUSY;
905 /* according to the datasheet the key must be send twice! */
906 outb(SIO_UNLOCK_KEY, base);
907 outb(SIO_UNLOCK_KEY, base);
909 return 0;
912 static inline void superio_select(int base, int ld)
914 outb(SIO_REG_LDSEL, base);
915 outb(ld, base + 1);
918 static inline void superio_exit(int base)
920 outb(SIO_LOCK_KEY, base);
921 release_region(base, 2);
924 static inline int fan_from_reg(u16 reg)
926 return reg ? (1500000 / reg) : 0;
929 static inline u16 fan_to_reg(int fan)
931 return fan ? (1500000 / fan) : 0;
934 static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
936 u8 val;
938 outb(reg, data->addr + ADDR_REG_OFFSET);
939 val = inb(data->addr + DATA_REG_OFFSET);
941 return val;
944 static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
946 u16 val;
948 val = f71882fg_read8(data, reg) << 8;
949 val |= f71882fg_read8(data, reg + 1);
951 return val;
954 static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
956 outb(reg, data->addr + ADDR_REG_OFFSET);
957 outb(val, data->addr + DATA_REG_OFFSET);
960 static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
962 f71882fg_write8(data, reg, val >> 8);
963 f71882fg_write8(data, reg + 1, val & 0xff);
966 static u16 f71882fg_read_temp(struct f71882fg_data *data, int nr)
968 if (data->type == f71858fg)
969 return f71882fg_read16(data, F71882FG_REG_TEMP(nr));
970 else
971 return f71882fg_read8(data, F71882FG_REG_TEMP(nr));
974 static struct f71882fg_data *f71882fg_update_device(struct device *dev)
976 struct f71882fg_data *data = dev_get_drvdata(dev);
977 int nr, reg;
978 int nr_fans = (data->type == f71882fg) ? 4 : 3;
980 mutex_lock(&data->update_lock);
982 /* Update once every 60 seconds */
983 if (time_after(jiffies, data->last_limits + 60 * HZ) ||
984 !data->valid) {
985 if (f71882fg_has_in1_alarm[data->type]) {
986 data->in1_max =
987 f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
988 data->in_beep =
989 f71882fg_read8(data, F71882FG_REG_IN_BEEP);
992 /* Get High & boundary temps*/
993 for (nr = data->temp_start; nr < 3 + data->temp_start; nr++) {
994 data->temp_ovt[nr] = f71882fg_read8(data,
995 F71882FG_REG_TEMP_OVT(nr));
996 data->temp_high[nr] = f71882fg_read8(data,
997 F71882FG_REG_TEMP_HIGH(nr));
1000 if (data->type != f8000) {
1001 data->temp_hyst[0] = f71882fg_read8(data,
1002 F71882FG_REG_TEMP_HYST(0));
1003 data->temp_hyst[1] = f71882fg_read8(data,
1004 F71882FG_REG_TEMP_HYST(1));
1006 /* All but the f71858fg / f8000 have this register */
1007 if ((data->type != f71858fg) && (data->type != f8000)) {
1008 reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
1009 data->temp_type[1] = (reg & 0x02) ? 2 : 4;
1010 data->temp_type[2] = (reg & 0x04) ? 2 : 4;
1011 data->temp_type[3] = (reg & 0x08) ? 2 : 4;
1014 if (f71882fg_has_beep[data->type]) {
1015 data->fan_beep = f71882fg_read8(data,
1016 F71882FG_REG_FAN_BEEP);
1017 data->temp_beep = f71882fg_read8(data,
1018 F71882FG_REG_TEMP_BEEP);
1021 data->pwm_enable = f71882fg_read8(data,
1022 F71882FG_REG_PWM_ENABLE);
1023 data->pwm_auto_point_hyst[0] =
1024 f71882fg_read8(data, F71882FG_REG_FAN_HYST(0));
1025 data->pwm_auto_point_hyst[1] =
1026 f71882fg_read8(data, F71882FG_REG_FAN_HYST(1));
1028 for (nr = 0; nr < nr_fans; nr++) {
1029 data->pwm_auto_point_mapping[nr] =
1030 f71882fg_read8(data,
1031 F71882FG_REG_POINT_MAPPING(nr));
1033 if (data->type != f71862fg) {
1034 int point;
1035 for (point = 0; point < 5; point++) {
1036 data->pwm_auto_point_pwm[nr][point] =
1037 f71882fg_read8(data,
1038 F71882FG_REG_POINT_PWM
1039 (nr, point));
1041 for (point = 0; point < 4; point++) {
1042 data->pwm_auto_point_temp[nr][point] =
1043 f71882fg_read8(data,
1044 F71882FG_REG_POINT_TEMP
1045 (nr, point));
1047 } else {
1048 data->pwm_auto_point_pwm[nr][1] =
1049 f71882fg_read8(data,
1050 F71882FG_REG_POINT_PWM
1051 (nr, 1));
1052 data->pwm_auto_point_pwm[nr][4] =
1053 f71882fg_read8(data,
1054 F71882FG_REG_POINT_PWM
1055 (nr, 4));
1056 data->pwm_auto_point_temp[nr][0] =
1057 f71882fg_read8(data,
1058 F71882FG_REG_POINT_TEMP
1059 (nr, 0));
1060 data->pwm_auto_point_temp[nr][3] =
1061 f71882fg_read8(data,
1062 F71882FG_REG_POINT_TEMP
1063 (nr, 3));
1066 data->last_limits = jiffies;
1069 /* Update every second */
1070 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
1071 data->temp_status = f71882fg_read8(data,
1072 F71882FG_REG_TEMP_STATUS);
1073 data->temp_diode_open = f71882fg_read8(data,
1074 F71882FG_REG_TEMP_DIODE_OPEN);
1075 for (nr = data->temp_start; nr < 3 + data->temp_start; nr++)
1076 data->temp[nr] = f71882fg_read_temp(data, nr);
1078 data->fan_status = f71882fg_read8(data,
1079 F71882FG_REG_FAN_STATUS);
1080 for (nr = 0; nr < nr_fans; nr++) {
1081 data->fan[nr] = f71882fg_read16(data,
1082 F71882FG_REG_FAN(nr));
1083 data->fan_target[nr] =
1084 f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
1085 data->fan_full_speed[nr] =
1086 f71882fg_read16(data,
1087 F71882FG_REG_FAN_FULL_SPEED(nr));
1088 data->pwm[nr] =
1089 f71882fg_read8(data, F71882FG_REG_PWM(nr));
1091 /* The f8000 can monitor 1 more fan, but has no pwm for it */
1092 if (data->type == f8000)
1093 data->fan[3] = f71882fg_read16(data,
1094 F71882FG_REG_FAN(3));
1096 if (f71882fg_has_in1_alarm[data->type])
1097 data->in_status = f71882fg_read8(data,
1098 F71882FG_REG_IN_STATUS);
1099 for (nr = 0; nr < F71882FG_MAX_INS; nr++)
1100 if (f71882fg_has_in[data->type][nr])
1101 data->in[nr] = f71882fg_read8(data,
1102 F71882FG_REG_IN(nr));
1104 data->last_updated = jiffies;
1105 data->valid = 1;
1108 mutex_unlock(&data->update_lock);
1110 return data;
1113 /* Sysfs Interface */
1114 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
1115 char *buf)
1117 struct f71882fg_data *data = f71882fg_update_device(dev);
1118 int nr = to_sensor_dev_attr_2(devattr)->index;
1119 int speed = fan_from_reg(data->fan[nr]);
1121 if (speed == FAN_MIN_DETECT)
1122 speed = 0;
1124 return sprintf(buf, "%d\n", speed);
1127 static ssize_t show_fan_full_speed(struct device *dev,
1128 struct device_attribute *devattr, char *buf)
1130 struct f71882fg_data *data = f71882fg_update_device(dev);
1131 int nr = to_sensor_dev_attr_2(devattr)->index;
1132 int speed = fan_from_reg(data->fan_full_speed[nr]);
1133 return sprintf(buf, "%d\n", speed);
1136 static ssize_t store_fan_full_speed(struct device *dev,
1137 struct device_attribute *devattr,
1138 const char *buf, size_t count)
1140 struct f71882fg_data *data = dev_get_drvdata(dev);
1141 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1142 long val;
1144 err = strict_strtol(buf, 10, &val);
1145 if (err)
1146 return err;
1148 val = SENSORS_LIMIT(val, 23, 1500000);
1149 val = fan_to_reg(val);
1151 mutex_lock(&data->update_lock);
1152 f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
1153 data->fan_full_speed[nr] = val;
1154 mutex_unlock(&data->update_lock);
1156 return count;
1159 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
1160 *devattr, char *buf)
1162 struct f71882fg_data *data = f71882fg_update_device(dev);
1163 int nr = to_sensor_dev_attr_2(devattr)->index;
1165 if (data->fan_beep & (1 << nr))
1166 return sprintf(buf, "1\n");
1167 else
1168 return sprintf(buf, "0\n");
1171 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
1172 *devattr, const char *buf, size_t count)
1174 struct f71882fg_data *data = dev_get_drvdata(dev);
1175 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1176 unsigned long val;
1178 err = strict_strtoul(buf, 10, &val);
1179 if (err)
1180 return err;
1182 mutex_lock(&data->update_lock);
1183 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
1184 if (val)
1185 data->fan_beep |= 1 << nr;
1186 else
1187 data->fan_beep &= ~(1 << nr);
1189 f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
1190 mutex_unlock(&data->update_lock);
1192 return count;
1195 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
1196 *devattr, char *buf)
1198 struct f71882fg_data *data = f71882fg_update_device(dev);
1199 int nr = to_sensor_dev_attr_2(devattr)->index;
1201 if (data->fan_status & (1 << nr))
1202 return sprintf(buf, "1\n");
1203 else
1204 return sprintf(buf, "0\n");
1207 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
1208 char *buf)
1210 struct f71882fg_data *data = f71882fg_update_device(dev);
1211 int nr = to_sensor_dev_attr_2(devattr)->index;
1213 return sprintf(buf, "%d\n", data->in[nr] * 8);
1216 static ssize_t show_in_max(struct device *dev, struct device_attribute
1217 *devattr, char *buf)
1219 struct f71882fg_data *data = f71882fg_update_device(dev);
1221 return sprintf(buf, "%d\n", data->in1_max * 8);
1224 static ssize_t store_in_max(struct device *dev, struct device_attribute
1225 *devattr, const char *buf, size_t count)
1227 struct f71882fg_data *data = dev_get_drvdata(dev);
1228 int err;
1229 long val;
1231 err = strict_strtol(buf, 10, &val);
1232 if (err)
1233 return err;
1235 val /= 8;
1236 val = SENSORS_LIMIT(val, 0, 255);
1238 mutex_lock(&data->update_lock);
1239 f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
1240 data->in1_max = val;
1241 mutex_unlock(&data->update_lock);
1243 return count;
1246 static ssize_t show_in_beep(struct device *dev, struct device_attribute
1247 *devattr, char *buf)
1249 struct f71882fg_data *data = f71882fg_update_device(dev);
1250 int nr = to_sensor_dev_attr_2(devattr)->index;
1252 if (data->in_beep & (1 << nr))
1253 return sprintf(buf, "1\n");
1254 else
1255 return sprintf(buf, "0\n");
1258 static ssize_t store_in_beep(struct device *dev, struct device_attribute
1259 *devattr, const char *buf, size_t count)
1261 struct f71882fg_data *data = dev_get_drvdata(dev);
1262 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1263 unsigned long val;
1265 err = strict_strtoul(buf, 10, &val);
1266 if (err)
1267 return err;
1269 mutex_lock(&data->update_lock);
1270 data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
1271 if (val)
1272 data->in_beep |= 1 << nr;
1273 else
1274 data->in_beep &= ~(1 << nr);
1276 f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
1277 mutex_unlock(&data->update_lock);
1279 return count;
1282 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
1283 *devattr, char *buf)
1285 struct f71882fg_data *data = f71882fg_update_device(dev);
1286 int nr = to_sensor_dev_attr_2(devattr)->index;
1288 if (data->in_status & (1 << nr))
1289 return sprintf(buf, "1\n");
1290 else
1291 return sprintf(buf, "0\n");
1294 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
1295 char *buf)
1297 struct f71882fg_data *data = f71882fg_update_device(dev);
1298 int nr = to_sensor_dev_attr_2(devattr)->index;
1299 int sign, temp;
1301 if (data->type == f71858fg) {
1302 /* TEMP_TABLE_SEL 1 or 3 ? */
1303 if (data->temp_config & 1) {
1304 sign = data->temp[nr] & 0x0001;
1305 temp = (data->temp[nr] >> 5) & 0x7ff;
1306 } else {
1307 sign = data->temp[nr] & 0x8000;
1308 temp = (data->temp[nr] >> 5) & 0x3ff;
1310 temp *= 125;
1311 if (sign)
1312 temp -= 128000;
1313 } else
1314 temp = data->temp[nr] * 1000;
1316 return sprintf(buf, "%d\n", temp);
1319 static ssize_t show_temp_max(struct device *dev, struct device_attribute
1320 *devattr, char *buf)
1322 struct f71882fg_data *data = f71882fg_update_device(dev);
1323 int nr = to_sensor_dev_attr_2(devattr)->index;
1325 return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
1328 static ssize_t store_temp_max(struct device *dev, struct device_attribute
1329 *devattr, const char *buf, size_t count)
1331 struct f71882fg_data *data = dev_get_drvdata(dev);
1332 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1333 long val;
1335 err = strict_strtol(buf, 10, &val);
1336 if (err)
1337 return err;
1339 val /= 1000;
1340 val = SENSORS_LIMIT(val, 0, 255);
1342 mutex_lock(&data->update_lock);
1343 f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
1344 data->temp_high[nr] = val;
1345 mutex_unlock(&data->update_lock);
1347 return count;
1350 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
1351 *devattr, char *buf)
1353 struct f71882fg_data *data = f71882fg_update_device(dev);
1354 int nr = to_sensor_dev_attr_2(devattr)->index;
1355 int temp_max_hyst;
1357 mutex_lock(&data->update_lock);
1358 if (nr & 1)
1359 temp_max_hyst = data->temp_hyst[nr / 2] >> 4;
1360 else
1361 temp_max_hyst = data->temp_hyst[nr / 2] & 0x0f;
1362 temp_max_hyst = (data->temp_high[nr] - temp_max_hyst) * 1000;
1363 mutex_unlock(&data->update_lock);
1365 return sprintf(buf, "%d\n", temp_max_hyst);
1368 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
1369 *devattr, const char *buf, size_t count)
1371 struct f71882fg_data *data = dev_get_drvdata(dev);
1372 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1373 ssize_t ret = count;
1374 u8 reg;
1375 long val;
1377 err = strict_strtol(buf, 10, &val);
1378 if (err)
1379 return err;
1381 val /= 1000;
1383 mutex_lock(&data->update_lock);
1385 /* convert abs to relative and check */
1386 data->temp_high[nr] = f71882fg_read8(data, F71882FG_REG_TEMP_HIGH(nr));
1387 val = SENSORS_LIMIT(val, data->temp_high[nr] - 15,
1388 data->temp_high[nr]);
1389 val = data->temp_high[nr] - val;
1391 /* convert value to register contents */
1392 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST(nr / 2));
1393 if (nr & 1)
1394 reg = (reg & 0x0f) | (val << 4);
1395 else
1396 reg = (reg & 0xf0) | val;
1397 f71882fg_write8(data, F71882FG_REG_TEMP_HYST(nr / 2), reg);
1398 data->temp_hyst[nr / 2] = reg;
1400 mutex_unlock(&data->update_lock);
1401 return ret;
1404 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
1405 *devattr, char *buf)
1407 struct f71882fg_data *data = f71882fg_update_device(dev);
1408 int nr = to_sensor_dev_attr_2(devattr)->index;
1410 return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
1413 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
1414 *devattr, const char *buf, size_t count)
1416 struct f71882fg_data *data = dev_get_drvdata(dev);
1417 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1418 long val;
1420 err = strict_strtol(buf, 10, &val);
1421 if (err)
1422 return err;
1424 val /= 1000;
1425 val = SENSORS_LIMIT(val, 0, 255);
1427 mutex_lock(&data->update_lock);
1428 f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
1429 data->temp_ovt[nr] = val;
1430 mutex_unlock(&data->update_lock);
1432 return count;
1435 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
1436 *devattr, char *buf)
1438 struct f71882fg_data *data = f71882fg_update_device(dev);
1439 int nr = to_sensor_dev_attr_2(devattr)->index;
1440 int temp_crit_hyst;
1442 mutex_lock(&data->update_lock);
1443 if (nr & 1)
1444 temp_crit_hyst = data->temp_hyst[nr / 2] >> 4;
1445 else
1446 temp_crit_hyst = data->temp_hyst[nr / 2] & 0x0f;
1447 temp_crit_hyst = (data->temp_ovt[nr] - temp_crit_hyst) * 1000;
1448 mutex_unlock(&data->update_lock);
1450 return sprintf(buf, "%d\n", temp_crit_hyst);
1453 static ssize_t show_temp_type(struct device *dev, struct device_attribute
1454 *devattr, char *buf)
1456 struct f71882fg_data *data = f71882fg_update_device(dev);
1457 int nr = to_sensor_dev_attr_2(devattr)->index;
1459 return sprintf(buf, "%d\n", data->temp_type[nr]);
1462 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
1463 *devattr, char *buf)
1465 struct f71882fg_data *data = f71882fg_update_device(dev);
1466 int nr = to_sensor_dev_attr_2(devattr)->index;
1468 if (data->temp_beep & (1 << nr))
1469 return sprintf(buf, "1\n");
1470 else
1471 return sprintf(buf, "0\n");
1474 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
1475 *devattr, const char *buf, size_t count)
1477 struct f71882fg_data *data = dev_get_drvdata(dev);
1478 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1479 unsigned long val;
1481 err = strict_strtoul(buf, 10, &val);
1482 if (err)
1483 return err;
1485 mutex_lock(&data->update_lock);
1486 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
1487 if (val)
1488 data->temp_beep |= 1 << nr;
1489 else
1490 data->temp_beep &= ~(1 << nr);
1492 f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
1493 mutex_unlock(&data->update_lock);
1495 return count;
1498 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
1499 *devattr, char *buf)
1501 struct f71882fg_data *data = f71882fg_update_device(dev);
1502 int nr = to_sensor_dev_attr_2(devattr)->index;
1504 if (data->temp_status & (1 << nr))
1505 return sprintf(buf, "1\n");
1506 else
1507 return sprintf(buf, "0\n");
1510 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
1511 *devattr, char *buf)
1513 struct f71882fg_data *data = f71882fg_update_device(dev);
1514 int nr = to_sensor_dev_attr_2(devattr)->index;
1516 if (data->temp_diode_open & (1 << nr))
1517 return sprintf(buf, "1\n");
1518 else
1519 return sprintf(buf, "0\n");
1522 static ssize_t show_pwm(struct device *dev,
1523 struct device_attribute *devattr, char *buf)
1525 struct f71882fg_data *data = f71882fg_update_device(dev);
1526 int val, nr = to_sensor_dev_attr_2(devattr)->index;
1527 mutex_lock(&data->update_lock);
1528 if (data->pwm_enable & (1 << (2 * nr)))
1529 /* PWM mode */
1530 val = data->pwm[nr];
1531 else {
1532 /* RPM mode */
1533 val = 255 * fan_from_reg(data->fan_target[nr])
1534 / fan_from_reg(data->fan_full_speed[nr]);
1536 mutex_unlock(&data->update_lock);
1537 return sprintf(buf, "%d\n", val);
1540 static ssize_t store_pwm(struct device *dev,
1541 struct device_attribute *devattr, const char *buf,
1542 size_t count)
1544 struct f71882fg_data *data = dev_get_drvdata(dev);
1545 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1546 long val;
1548 err = strict_strtol(buf, 10, &val);
1549 if (err)
1550 return err;
1552 val = SENSORS_LIMIT(val, 0, 255);
1554 mutex_lock(&data->update_lock);
1555 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1556 if ((data->type == f8000 && ((data->pwm_enable >> 2 * nr) & 3) != 2) ||
1557 (data->type != f8000 && !((data->pwm_enable >> 2 * nr) & 2))) {
1558 count = -EROFS;
1559 goto leave;
1561 if (data->pwm_enable & (1 << (2 * nr))) {
1562 /* PWM mode */
1563 f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1564 data->pwm[nr] = val;
1565 } else {
1566 /* RPM mode */
1567 int target, full_speed;
1568 full_speed = f71882fg_read16(data,
1569 F71882FG_REG_FAN_FULL_SPEED(nr));
1570 target = fan_to_reg(val * fan_from_reg(full_speed) / 255);
1571 f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr), target);
1572 data->fan_target[nr] = target;
1573 data->fan_full_speed[nr] = full_speed;
1575 leave:
1576 mutex_unlock(&data->update_lock);
1578 return count;
1581 static ssize_t show_pwm_enable(struct device *dev,
1582 struct device_attribute *devattr, char *buf)
1584 int result = 0;
1585 struct f71882fg_data *data = f71882fg_update_device(dev);
1586 int nr = to_sensor_dev_attr_2(devattr)->index;
1588 switch ((data->pwm_enable >> 2 * nr) & 3) {
1589 case 0:
1590 case 1:
1591 result = 2; /* Normal auto mode */
1592 break;
1593 case 2:
1594 result = 1; /* Manual mode */
1595 break;
1596 case 3:
1597 if (data->type == f8000)
1598 result = 3; /* Thermostat mode */
1599 else
1600 result = 1; /* Manual mode */
1601 break;
1604 return sprintf(buf, "%d\n", result);
1607 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1608 *devattr, const char *buf, size_t count)
1610 struct f71882fg_data *data = dev_get_drvdata(dev);
1611 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1612 long val;
1614 err = strict_strtol(buf, 10, &val);
1615 if (err)
1616 return err;
1618 /* Special case for F8000 pwm channel 3 which only does auto mode */
1619 if (data->type == f8000 && nr == 2 && val != 2)
1620 return -EINVAL;
1622 mutex_lock(&data->update_lock);
1623 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1624 /* Special case for F8000 auto PWM mode / Thermostat mode */
1625 if (data->type == f8000 && ((data->pwm_enable >> 2 * nr) & 1)) {
1626 switch (val) {
1627 case 2:
1628 data->pwm_enable &= ~(2 << (2 * nr));
1629 break; /* Normal auto mode */
1630 case 3:
1631 data->pwm_enable |= 2 << (2 * nr);
1632 break; /* Thermostat mode */
1633 default:
1634 count = -EINVAL;
1635 goto leave;
1637 } else {
1638 switch (val) {
1639 case 1:
1640 /* The f71858fg does not support manual RPM mode */
1641 if (data->type == f71858fg &&
1642 ((data->pwm_enable >> (2 * nr)) & 1)) {
1643 count = -EINVAL;
1644 goto leave;
1646 data->pwm_enable |= 2 << (2 * nr);
1647 break; /* Manual */
1648 case 2:
1649 data->pwm_enable &= ~(2 << (2 * nr));
1650 break; /* Normal auto mode */
1651 default:
1652 count = -EINVAL;
1653 goto leave;
1656 f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
1657 leave:
1658 mutex_unlock(&data->update_lock);
1660 return count;
1663 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
1664 struct device_attribute *devattr,
1665 char *buf)
1667 int result;
1668 struct f71882fg_data *data = f71882fg_update_device(dev);
1669 int pwm = to_sensor_dev_attr_2(devattr)->index;
1670 int point = to_sensor_dev_attr_2(devattr)->nr;
1672 mutex_lock(&data->update_lock);
1673 if (data->pwm_enable & (1 << (2 * pwm))) {
1674 /* PWM mode */
1675 result = data->pwm_auto_point_pwm[pwm][point];
1676 } else {
1677 /* RPM mode */
1678 result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
1680 mutex_unlock(&data->update_lock);
1682 return sprintf(buf, "%d\n", result);
1685 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
1686 struct device_attribute *devattr,
1687 const char *buf, size_t count)
1689 struct f71882fg_data *data = dev_get_drvdata(dev);
1690 int err, pwm = to_sensor_dev_attr_2(devattr)->index;
1691 int point = to_sensor_dev_attr_2(devattr)->nr;
1692 long val;
1694 err = strict_strtol(buf, 10, &val);
1695 if (err)
1696 return err;
1698 val = SENSORS_LIMIT(val, 0, 255);
1700 mutex_lock(&data->update_lock);
1701 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1702 if (data->pwm_enable & (1 << (2 * pwm))) {
1703 /* PWM mode */
1704 } else {
1705 /* RPM mode */
1706 if (val < 29) /* Prevent negative numbers */
1707 val = 255;
1708 else
1709 val = (255 - val) * 32 / val;
1711 f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
1712 data->pwm_auto_point_pwm[pwm][point] = val;
1713 mutex_unlock(&data->update_lock);
1715 return count;
1718 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
1719 struct device_attribute *devattr,
1720 char *buf)
1722 int result = 0;
1723 struct f71882fg_data *data = f71882fg_update_device(dev);
1724 int nr = to_sensor_dev_attr_2(devattr)->index;
1725 int point = to_sensor_dev_attr_2(devattr)->nr;
1727 mutex_lock(&data->update_lock);
1728 if (nr & 1)
1729 result = data->pwm_auto_point_hyst[nr / 2] >> 4;
1730 else
1731 result = data->pwm_auto_point_hyst[nr / 2] & 0x0f;
1732 result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
1733 mutex_unlock(&data->update_lock);
1735 return sprintf(buf, "%d\n", result);
1738 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
1739 struct device_attribute *devattr,
1740 const char *buf, size_t count)
1742 struct f71882fg_data *data = dev_get_drvdata(dev);
1743 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1744 int point = to_sensor_dev_attr_2(devattr)->nr;
1745 u8 reg;
1746 long val;
1748 err = strict_strtol(buf, 10, &val);
1749 if (err)
1750 return err;
1752 val /= 1000;
1754 mutex_lock(&data->update_lock);
1755 data->pwm_auto_point_temp[nr][point] =
1756 f71882fg_read8(data, F71882FG_REG_POINT_TEMP(nr, point));
1757 val = SENSORS_LIMIT(val, data->pwm_auto_point_temp[nr][point] - 15,
1758 data->pwm_auto_point_temp[nr][point]);
1759 val = data->pwm_auto_point_temp[nr][point] - val;
1761 reg = f71882fg_read8(data, F71882FG_REG_FAN_HYST(nr / 2));
1762 if (nr & 1)
1763 reg = (reg & 0x0f) | (val << 4);
1764 else
1765 reg = (reg & 0xf0) | val;
1767 f71882fg_write8(data, F71882FG_REG_FAN_HYST(nr / 2), reg);
1768 data->pwm_auto_point_hyst[nr / 2] = reg;
1769 mutex_unlock(&data->update_lock);
1771 return count;
1774 static ssize_t show_pwm_interpolate(struct device *dev,
1775 struct device_attribute *devattr, char *buf)
1777 int result;
1778 struct f71882fg_data *data = f71882fg_update_device(dev);
1779 int nr = to_sensor_dev_attr_2(devattr)->index;
1781 result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
1783 return sprintf(buf, "%d\n", result);
1786 static ssize_t store_pwm_interpolate(struct device *dev,
1787 struct device_attribute *devattr,
1788 const char *buf, size_t count)
1790 struct f71882fg_data *data = dev_get_drvdata(dev);
1791 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1792 unsigned long val;
1794 err = strict_strtoul(buf, 10, &val);
1795 if (err)
1796 return err;
1798 mutex_lock(&data->update_lock);
1799 data->pwm_auto_point_mapping[nr] =
1800 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
1801 if (val)
1802 val = data->pwm_auto_point_mapping[nr] | (1 << 4);
1803 else
1804 val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
1805 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1806 data->pwm_auto_point_mapping[nr] = val;
1807 mutex_unlock(&data->update_lock);
1809 return count;
1812 static ssize_t show_pwm_auto_point_channel(struct device *dev,
1813 struct device_attribute *devattr,
1814 char *buf)
1816 int result;
1817 struct f71882fg_data *data = f71882fg_update_device(dev);
1818 int nr = to_sensor_dev_attr_2(devattr)->index;
1820 result = 1 << ((data->pwm_auto_point_mapping[nr] & 3) -
1821 data->temp_start);
1823 return sprintf(buf, "%d\n", result);
1826 static ssize_t store_pwm_auto_point_channel(struct device *dev,
1827 struct device_attribute *devattr,
1828 const char *buf, size_t count)
1830 struct f71882fg_data *data = dev_get_drvdata(dev);
1831 int err, nr = to_sensor_dev_attr_2(devattr)->index;
1832 long val;
1834 err = strict_strtol(buf, 10, &val);
1835 if (err)
1836 return err;
1838 switch (val) {
1839 case 1:
1840 val = 0;
1841 break;
1842 case 2:
1843 val = 1;
1844 break;
1845 case 4:
1846 val = 2;
1847 break;
1848 default:
1849 return -EINVAL;
1851 val += data->temp_start;
1852 mutex_lock(&data->update_lock);
1853 data->pwm_auto_point_mapping[nr] =
1854 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
1855 val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
1856 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1857 data->pwm_auto_point_mapping[nr] = val;
1858 mutex_unlock(&data->update_lock);
1860 return count;
1863 static ssize_t show_pwm_auto_point_temp(struct device *dev,
1864 struct device_attribute *devattr,
1865 char *buf)
1867 int result;
1868 struct f71882fg_data *data = f71882fg_update_device(dev);
1869 int pwm = to_sensor_dev_attr_2(devattr)->index;
1870 int point = to_sensor_dev_attr_2(devattr)->nr;
1872 result = data->pwm_auto_point_temp[pwm][point];
1873 return sprintf(buf, "%d\n", 1000 * result);
1876 static ssize_t store_pwm_auto_point_temp(struct device *dev,
1877 struct device_attribute *devattr,
1878 const char *buf, size_t count)
1880 struct f71882fg_data *data = dev_get_drvdata(dev);
1881 int err, pwm = to_sensor_dev_attr_2(devattr)->index;
1882 int point = to_sensor_dev_attr_2(devattr)->nr;
1883 long val;
1885 err = strict_strtol(buf, 10, &val);
1886 if (err)
1887 return err;
1889 val /= 1000;
1891 if (data->auto_point_temp_signed)
1892 val = SENSORS_LIMIT(val, -128, 127);
1893 else
1894 val = SENSORS_LIMIT(val, 0, 127);
1896 mutex_lock(&data->update_lock);
1897 f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
1898 data->pwm_auto_point_temp[pwm][point] = val;
1899 mutex_unlock(&data->update_lock);
1901 return count;
1904 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
1905 char *buf)
1907 struct f71882fg_data *data = dev_get_drvdata(dev);
1908 return sprintf(buf, "%s\n", f71882fg_names[data->type]);
1911 static int __devinit f71882fg_create_sysfs_files(struct platform_device *pdev,
1912 struct sensor_device_attribute_2 *attr, int count)
1914 int err, i;
1916 for (i = 0; i < count; i++) {
1917 err = device_create_file(&pdev->dev, &attr[i].dev_attr);
1918 if (err)
1919 return err;
1921 return 0;
1924 static void f71882fg_remove_sysfs_files(struct platform_device *pdev,
1925 struct sensor_device_attribute_2 *attr, int count)
1927 int i;
1929 for (i = 0; i < count; i++)
1930 device_remove_file(&pdev->dev, &attr[i].dev_attr);
1933 static int __devinit f71882fg_probe(struct platform_device *pdev)
1935 struct f71882fg_data *data;
1936 struct f71882fg_sio_data *sio_data = pdev->dev.platform_data;
1937 int err, i, nr_fans = (sio_data->type == f71882fg) ? 4 : 3;
1938 int nr_temps = 3;
1939 u8 start_reg, reg;
1941 data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL);
1942 if (!data)
1943 return -ENOMEM;
1945 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1946 data->type = sio_data->type;
1947 data->temp_start =
1948 (data->type == f71858fg || data->type == f8000) ? 0 : 1;
1949 mutex_init(&data->update_lock);
1950 platform_set_drvdata(pdev, data);
1952 start_reg = f71882fg_read8(data, F71882FG_REG_START);
1953 if (start_reg & 0x04) {
1954 dev_warn(&pdev->dev, "Hardware monitor is powered down\n");
1955 err = -ENODEV;
1956 goto exit_free;
1958 if (!(start_reg & 0x03)) {
1959 dev_warn(&pdev->dev, "Hardware monitoring not activated\n");
1960 err = -ENODEV;
1961 goto exit_free;
1964 /* Register sysfs interface files */
1965 err = device_create_file(&pdev->dev, &dev_attr_name);
1966 if (err)
1967 goto exit_unregister_sysfs;
1969 if (start_reg & 0x01) {
1970 switch (data->type) {
1971 case f71858fg:
1972 data->temp_config =
1973 f71882fg_read8(data, F71882FG_REG_TEMP_CONFIG);
1974 if (data->temp_config & 0x10)
1975 /* The f71858fg temperature alarms behave as
1976 the f8000 alarms in this mode */
1977 err = f71882fg_create_sysfs_files(pdev,
1978 f8000_temp_attr,
1979 ARRAY_SIZE(f8000_temp_attr));
1980 else
1981 err = f71882fg_create_sysfs_files(pdev,
1982 f71858fg_temp_attr,
1983 ARRAY_SIZE(f71858fg_temp_attr));
1984 break;
1985 case f8000:
1986 err = f71882fg_create_sysfs_files(pdev,
1987 f8000_temp_attr,
1988 ARRAY_SIZE(f8000_temp_attr));
1989 break;
1990 default:
1991 err = f71882fg_create_sysfs_files(pdev,
1992 &fxxxx_temp_attr[0][0],
1993 ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
1995 if (err)
1996 goto exit_unregister_sysfs;
1998 if (f71882fg_has_beep[data->type]) {
1999 err = f71882fg_create_sysfs_files(pdev,
2000 &fxxxx_temp_beep_attr[0][0],
2001 ARRAY_SIZE(fxxxx_temp_beep_attr[0])
2002 * nr_temps);
2003 if (err)
2004 goto exit_unregister_sysfs;
2007 for (i = 0; i < F71882FG_MAX_INS; i++) {
2008 if (f71882fg_has_in[data->type][i]) {
2009 err = device_create_file(&pdev->dev,
2010 &fxxxx_in_attr[i].dev_attr);
2011 if (err)
2012 goto exit_unregister_sysfs;
2015 if (f71882fg_has_in1_alarm[data->type]) {
2016 err = f71882fg_create_sysfs_files(pdev,
2017 fxxxx_in1_alarm_attr,
2018 ARRAY_SIZE(fxxxx_in1_alarm_attr));
2019 if (err)
2020 goto exit_unregister_sysfs;
2024 if (start_reg & 0x02) {
2025 switch (data->type) {
2026 case f71889fg:
2027 reg = f71882fg_read8(data, F71882FG_REG_FAN_FAULT_T);
2028 if (reg & F71882FG_FAN_NEG_TEMP_EN)
2029 data->auto_point_temp_signed = 1;
2030 break;
2031 default:
2032 break;
2035 data->pwm_enable =
2036 f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
2038 /* Sanity check the pwm settings */
2039 switch (data->type) {
2040 case f71858fg:
2041 err = 0;
2042 for (i = 0; i < nr_fans; i++)
2043 if (((data->pwm_enable >> (i * 2)) & 3) == 3)
2044 err = 1;
2045 break;
2046 case f71862fg:
2047 err = (data->pwm_enable & 0x15) != 0x15;
2048 break;
2049 case f71882fg:
2050 case f71889fg:
2051 err = 0;
2052 break;
2053 case f8000:
2054 err = data->pwm_enable & 0x20;
2055 break;
2057 if (err) {
2058 dev_err(&pdev->dev,
2059 "Invalid (reserved) pwm settings: 0x%02x\n",
2060 (unsigned int)data->pwm_enable);
2061 err = -ENODEV;
2062 goto exit_unregister_sysfs;
2065 err = f71882fg_create_sysfs_files(pdev, &fxxxx_fan_attr[0][0],
2066 ARRAY_SIZE(fxxxx_fan_attr[0]) * nr_fans);
2067 if (err)
2068 goto exit_unregister_sysfs;
2070 if (f71882fg_has_beep[data->type]) {
2071 err = f71882fg_create_sysfs_files(pdev,
2072 fxxxx_fan_beep_attr, nr_fans);
2073 if (err)
2074 goto exit_unregister_sysfs;
2077 switch (data->type) {
2078 case f71862fg:
2079 err = f71882fg_create_sysfs_files(pdev,
2080 f71862fg_auto_pwm_attr,
2081 ARRAY_SIZE(f71862fg_auto_pwm_attr));
2082 break;
2083 case f8000:
2084 err = f71882fg_create_sysfs_files(pdev,
2085 f8000_fan_attr,
2086 ARRAY_SIZE(f8000_fan_attr));
2087 if (err)
2088 goto exit_unregister_sysfs;
2089 err = f71882fg_create_sysfs_files(pdev,
2090 f8000_auto_pwm_attr,
2091 ARRAY_SIZE(f8000_auto_pwm_attr));
2092 break;
2093 case f71889fg:
2094 for (i = 0; i < nr_fans; i++) {
2095 data->pwm_auto_point_mapping[i] =
2096 f71882fg_read8(data,
2097 F71882FG_REG_POINT_MAPPING(i));
2098 if (data->pwm_auto_point_mapping[i] & 0x80)
2099 break;
2101 if (i != nr_fans) {
2102 dev_warn(&pdev->dev,
2103 "Auto pwm controlled by raw digital "
2104 "data, disabling pwm auto_point "
2105 "sysfs attributes\n");
2106 break;
2108 /* fall through */
2109 default: /* f71858fg / f71882fg */
2110 err = f71882fg_create_sysfs_files(pdev,
2111 &fxxxx_auto_pwm_attr[0][0],
2112 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
2114 if (err)
2115 goto exit_unregister_sysfs;
2117 for (i = 0; i < nr_fans; i++)
2118 dev_info(&pdev->dev, "Fan: %d is in %s mode\n", i + 1,
2119 (data->pwm_enable & (1 << 2 * i)) ?
2120 "duty-cycle" : "RPM");
2123 data->hwmon_dev = hwmon_device_register(&pdev->dev);
2124 if (IS_ERR(data->hwmon_dev)) {
2125 err = PTR_ERR(data->hwmon_dev);
2126 data->hwmon_dev = NULL;
2127 goto exit_unregister_sysfs;
2130 return 0;
2132 exit_unregister_sysfs:
2133 f71882fg_remove(pdev); /* Will unregister the sysfs files for us */
2134 return err; /* f71882fg_remove() also frees our data */
2135 exit_free:
2136 kfree(data);
2137 return err;
2140 static int f71882fg_remove(struct platform_device *pdev)
2142 struct f71882fg_data *data = platform_get_drvdata(pdev);
2143 int i, nr_fans = (data->type == f71882fg) ? 4 : 3;
2144 int nr_temps = 3;
2145 u8 start_reg = f71882fg_read8(data, F71882FG_REG_START);
2147 if (data->hwmon_dev)
2148 hwmon_device_unregister(data->hwmon_dev);
2150 device_remove_file(&pdev->dev, &dev_attr_name);
2152 if (start_reg & 0x01) {
2153 switch (data->type) {
2154 case f71858fg:
2155 if (data->temp_config & 0x10)
2156 f71882fg_remove_sysfs_files(pdev,
2157 f8000_temp_attr,
2158 ARRAY_SIZE(f8000_temp_attr));
2159 else
2160 f71882fg_remove_sysfs_files(pdev,
2161 f71858fg_temp_attr,
2162 ARRAY_SIZE(f71858fg_temp_attr));
2163 break;
2164 case f8000:
2165 f71882fg_remove_sysfs_files(pdev,
2166 f8000_temp_attr,
2167 ARRAY_SIZE(f8000_temp_attr));
2168 break;
2169 default:
2170 f71882fg_remove_sysfs_files(pdev,
2171 &fxxxx_temp_attr[0][0],
2172 ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
2174 if (f71882fg_has_beep[data->type]) {
2175 f71882fg_remove_sysfs_files(pdev,
2176 &fxxxx_temp_beep_attr[0][0],
2177 ARRAY_SIZE(fxxxx_temp_beep_attr[0]) * nr_temps);
2180 for (i = 0; i < F71882FG_MAX_INS; i++) {
2181 if (f71882fg_has_in[data->type][i]) {
2182 device_remove_file(&pdev->dev,
2183 &fxxxx_in_attr[i].dev_attr);
2186 if (f71882fg_has_in1_alarm[data->type]) {
2187 f71882fg_remove_sysfs_files(pdev,
2188 fxxxx_in1_alarm_attr,
2189 ARRAY_SIZE(fxxxx_in1_alarm_attr));
2193 if (start_reg & 0x02) {
2194 f71882fg_remove_sysfs_files(pdev, &fxxxx_fan_attr[0][0],
2195 ARRAY_SIZE(fxxxx_fan_attr[0]) * nr_fans);
2197 if (f71882fg_has_beep[data->type]) {
2198 f71882fg_remove_sysfs_files(pdev,
2199 fxxxx_fan_beep_attr, nr_fans);
2202 switch (data->type) {
2203 case f71862fg:
2204 f71882fg_remove_sysfs_files(pdev,
2205 f71862fg_auto_pwm_attr,
2206 ARRAY_SIZE(f71862fg_auto_pwm_attr));
2207 break;
2208 case f8000:
2209 f71882fg_remove_sysfs_files(pdev,
2210 f8000_fan_attr,
2211 ARRAY_SIZE(f8000_fan_attr));
2212 f71882fg_remove_sysfs_files(pdev,
2213 f8000_auto_pwm_attr,
2214 ARRAY_SIZE(f8000_auto_pwm_attr));
2215 break;
2216 default: /* f71858fg / f71882fg / f71889fg */
2217 f71882fg_remove_sysfs_files(pdev,
2218 &fxxxx_auto_pwm_attr[0][0],
2219 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
2223 platform_set_drvdata(pdev, NULL);
2224 kfree(data);
2226 return 0;
2229 static int __init f71882fg_find(int sioaddr, unsigned short *address,
2230 struct f71882fg_sio_data *sio_data)
2232 u16 devid;
2233 int err = superio_enter(sioaddr);
2234 if (err)
2235 return err;
2237 devid = superio_inw(sioaddr, SIO_REG_MANID);
2238 if (devid != SIO_FINTEK_ID) {
2239 pr_debug("Not a Fintek device\n");
2240 err = -ENODEV;
2241 goto exit;
2244 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
2245 switch (devid) {
2246 case SIO_F71858_ID:
2247 sio_data->type = f71858fg;
2248 break;
2249 case SIO_F71862_ID:
2250 sio_data->type = f71862fg;
2251 break;
2252 case SIO_F71882_ID:
2253 sio_data->type = f71882fg;
2254 break;
2255 case SIO_F71889_ID:
2256 sio_data->type = f71889fg;
2257 break;
2258 case SIO_F8000_ID:
2259 sio_data->type = f8000;
2260 break;
2261 default:
2262 pr_info("Unsupported Fintek device: %04x\n",
2263 (unsigned int)devid);
2264 err = -ENODEV;
2265 goto exit;
2268 if (sio_data->type == f71858fg)
2269 superio_select(sioaddr, SIO_F71858FG_LD_HWM);
2270 else
2271 superio_select(sioaddr, SIO_F71882FG_LD_HWM);
2273 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
2274 pr_warn("Device not activated\n");
2275 err = -ENODEV;
2276 goto exit;
2279 *address = superio_inw(sioaddr, SIO_REG_ADDR);
2280 if (*address == 0) {
2281 pr_warn("Base address not set\n");
2282 err = -ENODEV;
2283 goto exit;
2285 *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
2287 err = 0;
2288 pr_info("Found %s chip at %#x, revision %d\n",
2289 f71882fg_names[sio_data->type], (unsigned int)*address,
2290 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
2291 exit:
2292 superio_exit(sioaddr);
2293 return err;
2296 static int __init f71882fg_device_add(unsigned short address,
2297 const struct f71882fg_sio_data *sio_data)
2299 struct resource res = {
2300 .start = address,
2301 .end = address + REGION_LENGTH - 1,
2302 .flags = IORESOURCE_IO,
2304 int err;
2306 f71882fg_pdev = platform_device_alloc(DRVNAME, address);
2307 if (!f71882fg_pdev)
2308 return -ENOMEM;
2310 res.name = f71882fg_pdev->name;
2311 err = acpi_check_resource_conflict(&res);
2312 if (err)
2313 goto exit_device_put;
2315 err = platform_device_add_resources(f71882fg_pdev, &res, 1);
2316 if (err) {
2317 pr_err("Device resource addition failed\n");
2318 goto exit_device_put;
2321 err = platform_device_add_data(f71882fg_pdev, sio_data,
2322 sizeof(struct f71882fg_sio_data));
2323 if (err) {
2324 pr_err("Platform data allocation failed\n");
2325 goto exit_device_put;
2328 err = platform_device_add(f71882fg_pdev);
2329 if (err) {
2330 pr_err("Device addition failed\n");
2331 goto exit_device_put;
2334 return 0;
2336 exit_device_put:
2337 platform_device_put(f71882fg_pdev);
2339 return err;
2342 static int __init f71882fg_init(void)
2344 int err = -ENODEV;
2345 unsigned short address;
2346 struct f71882fg_sio_data sio_data;
2348 memset(&sio_data, 0, sizeof(sio_data));
2350 if (f71882fg_find(0x2e, &address, &sio_data) &&
2351 f71882fg_find(0x4e, &address, &sio_data))
2352 goto exit;
2354 err = platform_driver_register(&f71882fg_driver);
2355 if (err)
2356 goto exit;
2358 err = f71882fg_device_add(address, &sio_data);
2359 if (err)
2360 goto exit_driver;
2362 return 0;
2364 exit_driver:
2365 platform_driver_unregister(&f71882fg_driver);
2366 exit:
2367 return err;
2370 static void __exit f71882fg_exit(void)
2372 platform_device_unregister(f71882fg_pdev);
2373 platform_driver_unregister(&f71882fg_driver);
2376 MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
2377 MODULE_AUTHOR("Hans Edgington, Hans de Goede (hdegoede@redhat.com)");
2378 MODULE_LICENSE("GPL");
2380 module_init(f71882fg_init);
2381 module_exit(f71882fg_exit);