blob: 345f465aa28887101f6849f00520c1a0b984b1eb [file] [log] [blame]
Hans de Goede45fb3662007-07-13 14:34:19 +02001/***************************************************************************
2 * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
Hans de Goedec13548c2009-01-07 16:37:27 +01003 * Copyright (C) 2007,2008 by Hans de Goede <hdegoede@redhat.com> *
Hans de Goede45fb3662007-07-13 14:34:19 +02004 * *
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 ***************************************************************************/
20
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>
Mark van Doesburg77a4a3e2009-01-07 16:37:27 +010030#include <linux/io.h>
Hans de Goede45fb3662007-07-13 14:34:19 +020031
32#define DRVNAME "f71882fg"
33
Mark van Doesburg77a4a3e2009-01-07 16:37:27 +010034#define SIO_F71882FG_LD_HWM 0x04 /* Hardware monitor logical device */
Hans de Goede45fb3662007-07-13 14:34:19 +020035#define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
36#define SIO_LOCK_KEY 0xAA /* Key to diasble Super-I/O */
37
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) */
44
45#define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
Hans de Goede498be962009-01-07 16:37:28 +010046#define SIO_F71862_ID 0x0601 /* Chipset ID */
Hans de Goede45fb3662007-07-13 14:34:19 +020047#define SIO_F71882_ID 0x0541 /* Chipset ID */
48
49#define REGION_LENGTH 8
50#define ADDR_REG_OFFSET 5
51#define DATA_REG_OFFSET 6
52
53#define F71882FG_REG_PECI 0x0A
54
Hans de Goede498be962009-01-07 16:37:28 +010055#define F71882FG_REG_IN_STATUS 0x12 /* f71882fg only */
56#define F71882FG_REG_IN_BEEP 0x13 /* f71882fg only */
Hans de Goede45fb3662007-07-13 14:34:19 +020057#define F71882FG_REG_IN(nr) (0x20 + (nr))
Hans de Goede498be962009-01-07 16:37:28 +010058#define F71882FG_REG_IN1_HIGH 0x32 /* f71882fg only */
Hans de Goede45fb3662007-07-13 14:34:19 +020059
60#define F71882FG_REG_FAN(nr) (0xA0 + (16 * (nr)))
Mark van Doesburg9ab796e2009-01-07 16:37:27 +010061#define F71882FG_REG_FAN_TARGET(nr) (0xA2 + (16 * (nr)))
62#define F71882FG_REG_FAN_FULL_SPEED(nr) (0xA4 + (16 * (nr)))
Hans de Goede45fb3662007-07-13 14:34:19 +020063#define F71882FG_REG_FAN_STATUS 0x92
64#define F71882FG_REG_FAN_BEEP 0x93
65
Hans de Goede7567a042009-01-07 16:37:28 +010066#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))
Hans de Goede45fb3662007-07-13 14:34:19 +020069#define F71882FG_REG_TEMP_STATUS 0x62
70#define F71882FG_REG_TEMP_BEEP 0x63
71#define F71882FG_REG_TEMP_HYST1 0x6C
72#define F71882FG_REG_TEMP_HYST23 0x6D
73#define F71882FG_REG_TEMP_TYPE 0x6B
74#define F71882FG_REG_TEMP_DIODE_OPEN 0x6F
75
Mark van Doesburg9ab796e2009-01-07 16:37:27 +010076#define F71882FG_REG_PWM(nr) (0xA3 + (16 * (nr)))
77#define F71882FG_REG_PWM_TYPE 0x94
78#define F71882FG_REG_PWM_ENABLE 0x96
79
80#define F71882FG_REG_FAN_HYST0 0x98
81#define F71882FG_REG_FAN_HYST1 0x99
82
83#define F71882FG_REG_POINT_PWM(pwm, point) (0xAA + (point) + (16 * (pwm)))
84#define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm)))
85#define F71882FG_REG_POINT_MAPPING(nr) (0xAF + 16 * (nr))
86
Hans de Goede45fb3662007-07-13 14:34:19 +020087#define F71882FG_REG_START 0x01
88
89#define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */
90
Jean Delvare67b671b2007-12-06 23:13:42 +010091static unsigned short force_id;
92module_param(force_id, ushort, 0);
93MODULE_PARM_DESC(force_id, "Override the detected device ID");
94
Mark van Doesburg9ab796e2009-01-07 16:37:27 +010095static int fan_mode[4] = { 0, 0, 0, 0 };
96module_param_array(fan_mode, int, NULL, 0644);
97MODULE_PARM_DESC(fan_mode, "List of fan control modes (f71882fg only) "
98 "(0=don't change, 1=pwm, 2=rpm)\n"
99 "Note: this needs a write to pwm#_enable to take effect");
100
Hans de Goede498be962009-01-07 16:37:28 +0100101enum chips { f71862fg, f71882fg };
102
103static const char *f71882fg_names[] = {
104 "f71862fg",
105 "f71882fg",
106};
107
Mark van Doesburg77a4a3e2009-01-07 16:37:27 +0100108static struct platform_device *f71882fg_pdev;
Hans de Goede45fb3662007-07-13 14:34:19 +0200109
110/* Super-I/O Function prototypes */
111static inline int superio_inb(int base, int reg);
112static inline int superio_inw(int base, int reg);
113static inline void superio_enter(int base);
114static inline void superio_select(int base, int ld);
115static inline void superio_exit(int base);
116
Hans de Goede498be962009-01-07 16:37:28 +0100117struct f71882fg_sio_data {
118 enum chips type;
119};
120
Hans de Goede45fb3662007-07-13 14:34:19 +0200121struct f71882fg_data {
122 unsigned short addr;
Hans de Goede498be962009-01-07 16:37:28 +0100123 enum chips type;
Tony Jones1beeffe2007-08-20 13:46:20 -0700124 struct device *hwmon_dev;
Hans de Goede45fb3662007-07-13 14:34:19 +0200125
126 struct mutex update_lock;
127 char valid; /* !=0 if following fields are valid */
128 unsigned long last_updated; /* In jiffies */
129 unsigned long last_limits; /* In jiffies */
130
131 /* Register Values */
132 u8 in[9];
133 u8 in1_max;
134 u8 in_status;
135 u8 in_beep;
136 u16 fan[4];
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100137 u16 fan_target[4];
138 u16 fan_full_speed[4];
Hans de Goede45fb3662007-07-13 14:34:19 +0200139 u8 fan_status;
140 u8 fan_beep;
Hans de Goede7567a042009-01-07 16:37:28 +0100141 /* Note: all models have only 3 temperature channels, but on some
142 they are addressed as 0-2 and on others as 1-3, so for coding
143 convenience we reserve space for 4 channels */
144 u8 temp[4];
145 u8 temp_ovt[4];
146 u8 temp_high[4];
147 u8 temp_hyst[4];
148 u8 temp_type[4];
Hans de Goede45fb3662007-07-13 14:34:19 +0200149 u8 temp_status;
150 u8 temp_beep;
151 u8 temp_diode_open;
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100152 u8 pwm[4];
153 u8 pwm_enable;
154 u8 pwm_auto_point_hyst[2];
155 u8 pwm_auto_point_mapping[4];
156 u8 pwm_auto_point_pwm[4][5];
157 u8 pwm_auto_point_temp[4][4];
Hans de Goede45fb3662007-07-13 14:34:19 +0200158};
159
Mark van Doesburg77a4a3e2009-01-07 16:37:27 +0100160/* Sysfs in */
Hans de Goede45fb3662007-07-13 14:34:19 +0200161static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
162 char *buf);
163static ssize_t show_in_max(struct device *dev, struct device_attribute
164 *devattr, char *buf);
165static ssize_t store_in_max(struct device *dev, struct device_attribute
166 *devattr, const char *buf, size_t count);
167static ssize_t show_in_beep(struct device *dev, struct device_attribute
168 *devattr, char *buf);
169static ssize_t store_in_beep(struct device *dev, struct device_attribute
170 *devattr, const char *buf, size_t count);
171static ssize_t show_in_alarm(struct device *dev, struct device_attribute
172 *devattr, char *buf);
173/* Sysfs Fan */
174static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
175 char *buf);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100176static ssize_t show_fan_full_speed(struct device *dev,
177 struct device_attribute *devattr, char *buf);
178static ssize_t store_fan_full_speed(struct device *dev,
179 struct device_attribute *devattr, const char *buf, size_t count);
Hans de Goede45fb3662007-07-13 14:34:19 +0200180static ssize_t show_fan_beep(struct device *dev, struct device_attribute
181 *devattr, char *buf);
182static ssize_t store_fan_beep(struct device *dev, struct device_attribute
183 *devattr, const char *buf, size_t count);
184static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
185 *devattr, char *buf);
186/* Sysfs Temp */
187static ssize_t show_temp(struct device *dev, struct device_attribute
188 *devattr, char *buf);
189static ssize_t show_temp_max(struct device *dev, struct device_attribute
190 *devattr, char *buf);
191static ssize_t store_temp_max(struct device *dev, struct device_attribute
192 *devattr, const char *buf, size_t count);
193static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
194 *devattr, char *buf);
195static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
196 *devattr, const char *buf, size_t count);
197static ssize_t show_temp_crit(struct device *dev, struct device_attribute
198 *devattr, char *buf);
199static ssize_t store_temp_crit(struct device *dev, struct device_attribute
200 *devattr, const char *buf, size_t count);
201static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
202 *devattr, char *buf);
203static ssize_t show_temp_type(struct device *dev, struct device_attribute
204 *devattr, char *buf);
205static ssize_t show_temp_beep(struct device *dev, struct device_attribute
206 *devattr, char *buf);
207static ssize_t store_temp_beep(struct device *dev, struct device_attribute
208 *devattr, const char *buf, size_t count);
209static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
210 *devattr, char *buf);
211static ssize_t show_temp_fault(struct device *dev, struct device_attribute
212 *devattr, char *buf);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100213/* PWM and Auto point control */
214static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
215 char *buf);
216static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
217 const char *buf, size_t count);
218static ssize_t show_pwm_enable(struct device *dev,
219 struct device_attribute *devattr, char *buf);
220static ssize_t store_pwm_enable(struct device *dev,
221 struct device_attribute *devattr, const char *buf, size_t count);
222static ssize_t show_pwm_interpolate(struct device *dev,
223 struct device_attribute *devattr, char *buf);
224static ssize_t store_pwm_interpolate(struct device *dev,
225 struct device_attribute *devattr, const char *buf, size_t count);
226static ssize_t show_pwm_auto_point_channel(struct device *dev,
227 struct device_attribute *devattr, char *buf);
228static ssize_t store_pwm_auto_point_channel(struct device *dev,
229 struct device_attribute *devattr, const char *buf, size_t count);
230static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
231 struct device_attribute *devattr, char *buf);
232static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
233 struct device_attribute *devattr, const char *buf, size_t count);
234static ssize_t show_pwm_auto_point_pwm(struct device *dev,
235 struct device_attribute *devattr, char *buf);
236static ssize_t store_pwm_auto_point_pwm(struct device *dev,
237 struct device_attribute *devattr, const char *buf, size_t count);
238static ssize_t show_pwm_auto_point_temp(struct device *dev,
239 struct device_attribute *devattr, char *buf);
240static ssize_t store_pwm_auto_point_temp(struct device *dev,
241 struct device_attribute *devattr, const char *buf, size_t count);
Hans de Goede45fb3662007-07-13 14:34:19 +0200242/* Sysfs misc */
243static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
244 char *buf);
245
246static int __devinit f71882fg_probe(struct platform_device * pdev);
Hans de Goedec13548c2009-01-07 16:37:27 +0100247static int f71882fg_remove(struct platform_device *pdev);
Hans de Goede45fb3662007-07-13 14:34:19 +0200248
249static struct platform_driver f71882fg_driver = {
250 .driver = {
251 .owner = THIS_MODULE,
252 .name = DRVNAME,
253 },
254 .probe = f71882fg_probe,
255 .remove = __devexit_p(f71882fg_remove),
256};
257
Hans de Goedec13548c2009-01-07 16:37:27 +0100258static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Hans de Goede45fb3662007-07-13 14:34:19 +0200259
Hans de Goede498be962009-01-07 16:37:28 +0100260static struct sensor_device_attribute_2 f718x2fg_in_temp_attr[] = {
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100261 SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
262 SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100263 SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
264 SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
265 SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
266 SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
267 SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
268 SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
269 SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
Hans de Goede7567a042009-01-07 16:37:28 +0100270 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100271 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100272 store_temp_max, 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100273 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100274 store_temp_max_hyst, 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100275 SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100276 store_temp_crit, 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100277 SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100278 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100279 SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1),
280 SENSOR_ATTR_2(temp1_beep, S_IRUGO|S_IWUSR, show_temp_beep,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100281 store_temp_beep, 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100282 SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
283 SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
284 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2),
285 SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100286 store_temp_max, 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100287 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100288 store_temp_max_hyst, 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100289 SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100290 store_temp_crit, 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100291 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100292 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100293 SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2),
294 SENSOR_ATTR_2(temp2_beep, S_IRUGO|S_IWUSR, show_temp_beep,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100295 store_temp_beep, 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100296 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
297 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
298 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3),
299 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
300 store_temp_max, 0, 3),
301 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
302 store_temp_max_hyst, 0, 3),
303 SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
304 store_temp_crit, 0, 3),
305 SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
306 0, 3),
307 SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3),
308 SENSOR_ATTR_2(temp3_beep, S_IRUGO|S_IWUSR, show_temp_beep,
309 store_temp_beep, 0, 3),
310 SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3),
311 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3),
Hans de Goede45fb3662007-07-13 14:34:19 +0200312};
313
Hans de Goede498be962009-01-07 16:37:28 +0100314static struct sensor_device_attribute_2 f71882fg_in_temp_attr[] = {
315 SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
316 0, 1),
317 SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
318 0, 1),
319 SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
320};
321
322static struct sensor_device_attribute_2 f718x2fg_fan_attr[] = {
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100323 SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100324 SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
325 show_fan_full_speed,
326 store_fan_full_speed, 0, 0),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100327 SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
328 store_fan_beep, 0, 0),
329 SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
330 SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100331 SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
332 show_fan_full_speed,
333 store_fan_full_speed, 0, 1),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100334 SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
335 store_fan_beep, 0, 1),
336 SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
337 SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100338 SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
339 show_fan_full_speed,
340 store_fan_full_speed, 0, 2),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100341 SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
342 store_fan_beep, 0, 2),
343 SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100344
345 SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
346 SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
347 store_pwm_enable, 0, 0),
348 SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
349 show_pwm_interpolate, store_pwm_interpolate, 0, 0),
350 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
351 show_pwm_auto_point_channel,
352 store_pwm_auto_point_channel, 0, 0),
Hans de Goede498be962009-01-07 16:37:28 +0100353
354 SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
355 SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
356 store_pwm_enable, 0, 1),
357 SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
358 show_pwm_interpolate, store_pwm_interpolate, 0, 1),
359 SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
360 show_pwm_auto_point_channel,
361 store_pwm_auto_point_channel, 0, 1),
362
363 SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
364 SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
365 store_pwm_enable, 0, 2),
366 SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
367 show_pwm_interpolate, store_pwm_interpolate, 0, 2),
368 SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
369 show_pwm_auto_point_channel,
370 store_pwm_auto_point_channel, 0, 2),
371};
372
373static struct sensor_device_attribute_2 f71862fg_fan_attr[] = {
374 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
375 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
376 1, 0),
377 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
378 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
379 4, 0),
380 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
381 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
382 0, 0),
383 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
384 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
385 3, 0),
386 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
387 show_pwm_auto_point_temp_hyst,
388 store_pwm_auto_point_temp_hyst,
389 0, 0),
390 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
391 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
392
393 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
394 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
395 1, 1),
396 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
397 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
398 4, 1),
399 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
400 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
401 0, 1),
402 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
403 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
404 3, 1),
405 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
406 show_pwm_auto_point_temp_hyst,
407 store_pwm_auto_point_temp_hyst,
408 0, 1),
409 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
410 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
411};
412
413static struct sensor_device_attribute_2 f71882fg_fan_attr[] = {
414 SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
415 SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
416 show_fan_full_speed,
417 store_fan_full_speed, 0, 3),
418 SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
419 store_fan_beep, 0, 3),
420 SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
421
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100422 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
423 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
424 0, 0),
425 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
426 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
427 1, 0),
428 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
429 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
430 2, 0),
431 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
432 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
433 3, 0),
434 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
435 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
436 4, 0),
437 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
438 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
439 0, 0),
440 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
441 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
442 1, 0),
443 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
444 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
445 2, 0),
446 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
447 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
448 3, 0),
449 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
450 show_pwm_auto_point_temp_hyst,
451 store_pwm_auto_point_temp_hyst,
452 0, 0),
453 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
454 show_pwm_auto_point_temp_hyst, NULL, 1, 0),
455 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
456 show_pwm_auto_point_temp_hyst, NULL, 2, 0),
457 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
458 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
459
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100460 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
461 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
462 0, 1),
463 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
464 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
465 1, 1),
466 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
467 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
468 2, 1),
469 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
470 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
471 3, 1),
472 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
473 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
474 4, 1),
475 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
476 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
477 0, 1),
478 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
479 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
480 1, 1),
481 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
482 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
483 2, 1),
484 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
485 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
486 3, 1),
487 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
488 show_pwm_auto_point_temp_hyst,
489 store_pwm_auto_point_temp_hyst,
490 0, 1),
491 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
492 show_pwm_auto_point_temp_hyst, NULL, 1, 1),
493 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
494 show_pwm_auto_point_temp_hyst, NULL, 2, 1),
495 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
496 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
497
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100498 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
499 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
500 0, 2),
501 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
502 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
503 1, 2),
504 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
505 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
506 2, 2),
507 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
508 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
509 3, 2),
510 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
511 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
512 4, 2),
513 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
514 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
515 0, 2),
516 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
517 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
518 1, 2),
519 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
520 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
521 2, 2),
522 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
523 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
524 3, 2),
525 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
526 show_pwm_auto_point_temp_hyst,
527 store_pwm_auto_point_temp_hyst,
528 0, 2),
529 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
530 show_pwm_auto_point_temp_hyst, NULL, 1, 2),
531 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
532 show_pwm_auto_point_temp_hyst, NULL, 2, 2),
533 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
534 show_pwm_auto_point_temp_hyst, NULL, 3, 2),
535
536 SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
537 SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
538 store_pwm_enable, 0, 3),
539 SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
540 show_pwm_interpolate, store_pwm_interpolate, 0, 3),
541 SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
542 show_pwm_auto_point_channel,
543 store_pwm_auto_point_channel, 0, 3),
544 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
545 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
546 0, 3),
547 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
548 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
549 1, 3),
550 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
551 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
552 2, 3),
553 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
554 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
555 3, 3),
556 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
557 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
558 4, 3),
559 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
560 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
561 0, 3),
562 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
563 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
564 1, 3),
565 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
566 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
567 2, 3),
568 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
569 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
570 3, 3),
571 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
572 show_pwm_auto_point_temp_hyst,
573 store_pwm_auto_point_temp_hyst,
574 0, 3),
575 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
576 show_pwm_auto_point_temp_hyst, NULL, 1, 3),
577 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
578 show_pwm_auto_point_temp_hyst, NULL, 2, 3),
579 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
580 show_pwm_auto_point_temp_hyst, NULL, 3, 3),
Hans de Goede45fb3662007-07-13 14:34:19 +0200581};
582
583
584/* Super I/O functions */
585static inline int superio_inb(int base, int reg)
586{
587 outb(reg, base);
588 return inb(base + 1);
589}
590
591static int superio_inw(int base, int reg)
592{
593 int val;
594 outb(reg++, base);
595 val = inb(base + 1) << 8;
596 outb(reg, base);
597 val |= inb(base + 1);
598 return val;
599}
600
601static inline void superio_enter(int base)
602{
603 /* according to the datasheet the key must be send twice! */
604 outb( SIO_UNLOCK_KEY, base);
605 outb( SIO_UNLOCK_KEY, base);
606}
607
608static inline void superio_select( int base, int ld)
609{
610 outb(SIO_REG_LDSEL, base);
611 outb(ld, base + 1);
612}
613
614static inline void superio_exit(int base)
615{
616 outb(SIO_LOCK_KEY, base);
617}
618
619static inline u16 fan_from_reg(u16 reg)
620{
621 return reg ? (1500000 / reg) : 0;
622}
623
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100624static inline u16 fan_to_reg(u16 fan)
625{
626 return fan ? (1500000 / fan) : 0;
627}
628
Hans de Goede45fb3662007-07-13 14:34:19 +0200629static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
630{
631 u8 val;
632
633 outb(reg, data->addr + ADDR_REG_OFFSET);
634 val = inb(data->addr + DATA_REG_OFFSET);
635
636 return val;
637}
638
639static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
640{
641 u16 val;
642
643 outb(reg++, data->addr + ADDR_REG_OFFSET);
644 val = inb(data->addr + DATA_REG_OFFSET) << 8;
645 outb(reg, data->addr + ADDR_REG_OFFSET);
646 val |= inb(data->addr + DATA_REG_OFFSET);
647
648 return val;
649}
650
651static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
652{
653 outb(reg, data->addr + ADDR_REG_OFFSET);
654 outb(val, data->addr + DATA_REG_OFFSET);
655}
656
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100657static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
658{
659 outb(reg++, data->addr + ADDR_REG_OFFSET);
660 outb(val >> 8, data->addr + DATA_REG_OFFSET);
661 outb(reg, data->addr + ADDR_REG_OFFSET);
662 outb(val & 255, data->addr + DATA_REG_OFFSET);
663}
664
Mark van Doesburg77a4a3e2009-01-07 16:37:27 +0100665static struct f71882fg_data *f71882fg_update_device(struct device *dev)
Hans de Goede45fb3662007-07-13 14:34:19 +0200666{
667 struct f71882fg_data *data = dev_get_drvdata(dev);
668 int nr, reg, reg2;
Hans de Goede498be962009-01-07 16:37:28 +0100669 int nr_fans = (data->type == f71862fg) ? 3 : 4;
Hans de Goede45fb3662007-07-13 14:34:19 +0200670
671 mutex_lock(&data->update_lock);
672
673 /* Update once every 60 seconds */
674 if ( time_after(jiffies, data->last_limits + 60 * HZ ) ||
675 !data->valid) {
Hans de Goede498be962009-01-07 16:37:28 +0100676 if (data->type == f71882fg) {
677 data->in1_max =
678 f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
679 data->in_beep =
680 f71882fg_read8(data, F71882FG_REG_IN_BEEP);
681 }
Hans de Goede45fb3662007-07-13 14:34:19 +0200682
683 /* Get High & boundary temps*/
Hans de Goede7567a042009-01-07 16:37:28 +0100684 for (nr = 1; nr < 4; nr++) {
Hans de Goede45fb3662007-07-13 14:34:19 +0200685 data->temp_ovt[nr] = f71882fg_read8(data,
686 F71882FG_REG_TEMP_OVT(nr));
687 data->temp_high[nr] = f71882fg_read8(data,
688 F71882FG_REG_TEMP_HIGH(nr));
689 }
690
691 /* Have to hardcode hyst*/
Hans de Goede7567a042009-01-07 16:37:28 +0100692 data->temp_hyst[1] = f71882fg_read8(data,
Hans de Goede45fb3662007-07-13 14:34:19 +0200693 F71882FG_REG_TEMP_HYST1) >> 4;
694 /* Hyst temps 2 & 3 stored in same register */
695 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST23);
Hans de Goede7567a042009-01-07 16:37:28 +0100696 data->temp_hyst[2] = reg & 0x0F;
697 data->temp_hyst[3] = reg >> 4;
Hans de Goede45fb3662007-07-13 14:34:19 +0200698
699 /* Have to hardcode type, because temp1 is special */
700 reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
701 reg2 = f71882fg_read8(data, F71882FG_REG_PECI);
702 if ((reg2 & 0x03) == 0x01)
Hans de Goede7567a042009-01-07 16:37:28 +0100703 data->temp_type[1] = 6 /* PECI */;
Hans de Goede45fb3662007-07-13 14:34:19 +0200704 else if ((reg2 & 0x03) == 0x02)
Hans de Goede7567a042009-01-07 16:37:28 +0100705 data->temp_type[1] = 5 /* AMDSI */;
Hans de Goede45fb3662007-07-13 14:34:19 +0200706 else
Hans de Goede7567a042009-01-07 16:37:28 +0100707 data->temp_type[1] = (reg & 0x02) ? 2 : 4;
Hans de Goede45fb3662007-07-13 14:34:19 +0200708
Hans de Goede7567a042009-01-07 16:37:28 +0100709 data->temp_type[2] = (reg & 0x04) ? 2 : 4;
710 data->temp_type[3] = (reg & 0x08) ? 2 : 4;
Hans de Goede45fb3662007-07-13 14:34:19 +0200711
712 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
713
714 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
715
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100716 data->pwm_enable = f71882fg_read8(data,
717 F71882FG_REG_PWM_ENABLE);
718 data->pwm_auto_point_hyst[0] = f71882fg_read8(data,
719 F71882FG_REG_FAN_HYST0);
720 data->pwm_auto_point_hyst[1] = f71882fg_read8(data,
721 F71882FG_REG_FAN_HYST1);
Hans de Goede498be962009-01-07 16:37:28 +0100722 for (nr = 0; nr < nr_fans; nr++) {
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100723 data->pwm_auto_point_mapping[nr] =
724 f71882fg_read8(data,
725 F71882FG_REG_POINT_MAPPING(nr));
726
Hans de Goede498be962009-01-07 16:37:28 +0100727 if (data->type == f71882fg) {
728 int point;
729 for (point = 0; point < 5; point++) {
730 data->pwm_auto_point_pwm[nr][point] =
731 f71882fg_read8(data,
732 F71882FG_REG_POINT_PWM
733 (nr, point));
734 }
735 for (point = 0; point < 4; point++) {
736 data->pwm_auto_point_temp[nr][point] =
737 f71882fg_read8(data,
738 F71882FG_REG_POINT_TEMP
739 (nr, point));
740 }
741 } else {
742 data->pwm_auto_point_pwm[nr][1] =
743 f71882fg_read8(data,
744 F71882FG_REG_POINT_PWM
745 (nr, 1));
746 data->pwm_auto_point_pwm[nr][4] =
747 f71882fg_read8(data,
748 F71882FG_REG_POINT_PWM
749 (nr, 4));
750 data->pwm_auto_point_temp[nr][0] =
751 f71882fg_read8(data,
752 F71882FG_REG_POINT_TEMP
753 (nr, 0));
754 data->pwm_auto_point_temp[nr][3] =
755 f71882fg_read8(data,
756 F71882FG_REG_POINT_TEMP
757 (nr, 3));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100758 }
759 }
Hans de Goede45fb3662007-07-13 14:34:19 +0200760 data->last_limits = jiffies;
761 }
762
763 /* Update every second */
Mark M. Hoffman8afb1042007-08-21 23:10:46 -0400764 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Hans de Goede45fb3662007-07-13 14:34:19 +0200765 data->temp_status = f71882fg_read8(data,
766 F71882FG_REG_TEMP_STATUS);
767 data->temp_diode_open = f71882fg_read8(data,
768 F71882FG_REG_TEMP_DIODE_OPEN);
Hans de Goede7567a042009-01-07 16:37:28 +0100769 for (nr = 1; nr < 4; nr++)
Hans de Goede45fb3662007-07-13 14:34:19 +0200770 data->temp[nr] = f71882fg_read8(data,
771 F71882FG_REG_TEMP(nr));
772
773 data->fan_status = f71882fg_read8(data,
774 F71882FG_REG_FAN_STATUS);
Hans de Goede498be962009-01-07 16:37:28 +0100775 for (nr = 0; nr < nr_fans; nr++) {
Hans de Goede45fb3662007-07-13 14:34:19 +0200776 data->fan[nr] = f71882fg_read16(data,
777 F71882FG_REG_FAN(nr));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100778 data->fan_target[nr] =
779 f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
780 data->fan_full_speed[nr] =
781 f71882fg_read16(data,
782 F71882FG_REG_FAN_FULL_SPEED(nr));
783 data->pwm[nr] =
784 f71882fg_read8(data, F71882FG_REG_PWM(nr));
785 }
Hans de Goede45fb3662007-07-13 14:34:19 +0200786
Hans de Goede498be962009-01-07 16:37:28 +0100787 if (data->type == f71882fg)
788 data->in_status = f71882fg_read8(data,
Hans de Goede45fb3662007-07-13 14:34:19 +0200789 F71882FG_REG_IN_STATUS);
790 for (nr = 0; nr < 9; nr++)
791 data->in[nr] = f71882fg_read8(data,
792 F71882FG_REG_IN(nr));
793
794 data->last_updated = jiffies;
795 data->valid = 1;
796 }
797
798 mutex_unlock(&data->update_lock);
799
800 return data;
801}
802
803/* Sysfs Interface */
804static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
805 char *buf)
806{
807 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100808 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200809 int speed = fan_from_reg(data->fan[nr]);
810
811 if (speed == FAN_MIN_DETECT)
812 speed = 0;
813
814 return sprintf(buf, "%d\n", speed);
815}
816
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100817static ssize_t show_fan_full_speed(struct device *dev,
818 struct device_attribute *devattr, char *buf)
819{
820 struct f71882fg_data *data = f71882fg_update_device(dev);
821 int nr = to_sensor_dev_attr_2(devattr)->index;
822 int speed = fan_from_reg(data->fan_full_speed[nr]);
823 return sprintf(buf, "%d\n", speed);
824}
825
826static ssize_t store_fan_full_speed(struct device *dev,
827 struct device_attribute *devattr,
828 const char *buf, size_t count)
829{
830 struct f71882fg_data *data = dev_get_drvdata(dev);
831 int nr = to_sensor_dev_attr_2(devattr)->index;
832 long val = simple_strtol(buf, NULL, 10);
833
834 val = SENSORS_LIMIT(val, 23, 1500000);
835 val = fan_to_reg(val);
836
837 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +0100838 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100839 if (data->pwm_enable & (1 << (2 * nr)))
840 /* PWM mode */
841 count = -EINVAL;
842 else {
843 /* RPM mode */
844 f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
845 data->fan_full_speed[nr] = val;
846 }
847 mutex_unlock(&data->update_lock);
848
849 return count;
850}
851
Hans de Goede45fb3662007-07-13 14:34:19 +0200852static ssize_t show_fan_beep(struct device *dev, struct device_attribute
853 *devattr, char *buf)
854{
855 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100856 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200857
858 if (data->fan_beep & (1 << nr))
859 return sprintf(buf, "1\n");
860 else
861 return sprintf(buf, "0\n");
862}
863
864static ssize_t store_fan_beep(struct device *dev, struct device_attribute
865 *devattr, const char *buf, size_t count)
866{
867 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100868 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +0100869 unsigned long val = simple_strtoul(buf, NULL, 10);
Hans de Goede45fb3662007-07-13 14:34:19 +0200870
871 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +0100872 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
Hans de Goede45fb3662007-07-13 14:34:19 +0200873 if (val)
874 data->fan_beep |= 1 << nr;
875 else
876 data->fan_beep &= ~(1 << nr);
877
878 f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
879 mutex_unlock(&data->update_lock);
880
881 return count;
882}
883
884static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
885 *devattr, char *buf)
886{
887 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100888 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200889
890 if (data->fan_status & (1 << nr))
891 return sprintf(buf, "1\n");
892 else
893 return sprintf(buf, "0\n");
894}
895
896static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
897 char *buf)
898{
899 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100900 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200901
902 return sprintf(buf, "%d\n", data->in[nr] * 8);
903}
904
905static ssize_t show_in_max(struct device *dev, struct device_attribute
906 *devattr, char *buf)
907{
908 struct f71882fg_data *data = f71882fg_update_device(dev);
909
910 return sprintf(buf, "%d\n", data->in1_max * 8);
911}
912
913static ssize_t store_in_max(struct device *dev, struct device_attribute
914 *devattr, const char *buf, size_t count)
915{
916 struct f71882fg_data *data = dev_get_drvdata(dev);
Hans de Goedece0bfa52009-01-07 16:37:28 +0100917 long val = simple_strtol(buf, NULL, 10) / 8;
918 val = SENSORS_LIMIT(val, 0, 255);
Hans de Goede45fb3662007-07-13 14:34:19 +0200919
920 mutex_lock(&data->update_lock);
921 f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
922 data->in1_max = val;
923 mutex_unlock(&data->update_lock);
924
925 return count;
926}
927
928static ssize_t show_in_beep(struct device *dev, struct device_attribute
929 *devattr, char *buf)
930{
931 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100932 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200933
934 if (data->in_beep & (1 << nr))
935 return sprintf(buf, "1\n");
936 else
937 return sprintf(buf, "0\n");
938}
939
940static ssize_t store_in_beep(struct device *dev, struct device_attribute
941 *devattr, const char *buf, size_t count)
942{
943 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100944 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +0100945 unsigned long val = simple_strtoul(buf, NULL, 10);
Hans de Goede45fb3662007-07-13 14:34:19 +0200946
947 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +0100948 data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
Hans de Goede45fb3662007-07-13 14:34:19 +0200949 if (val)
950 data->in_beep |= 1 << nr;
951 else
952 data->in_beep &= ~(1 << nr);
953
954 f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
955 mutex_unlock(&data->update_lock);
956
957 return count;
958}
959
960static ssize_t show_in_alarm(struct device *dev, struct device_attribute
961 *devattr, char *buf)
962{
963 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100964 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200965
966 if (data->in_status & (1 << nr))
967 return sprintf(buf, "1\n");
968 else
969 return sprintf(buf, "0\n");
970}
971
972static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
973 char *buf)
974{
975 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100976 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200977
978 return sprintf(buf, "%d\n", data->temp[nr] * 1000);
979}
980
981static ssize_t show_temp_max(struct device *dev, struct device_attribute
982 *devattr, char *buf)
983{
984 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100985 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200986
987 return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
988}
989
990static ssize_t store_temp_max(struct device *dev, struct device_attribute
991 *devattr, const char *buf, size_t count)
992{
993 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100994 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +0100995 long val = simple_strtol(buf, NULL, 10) / 1000;
996 val = SENSORS_LIMIT(val, 0, 255);
Hans de Goede45fb3662007-07-13 14:34:19 +0200997
998 mutex_lock(&data->update_lock);
999 f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
1000 data->temp_high[nr] = val;
1001 mutex_unlock(&data->update_lock);
1002
1003 return count;
1004}
1005
1006static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
1007 *devattr, char *buf)
1008{
1009 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001010 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001011 int temp_max_hyst;
Hans de Goede45fb3662007-07-13 14:34:19 +02001012
Hans de Goedece0bfa52009-01-07 16:37:28 +01001013 mutex_lock(&data->update_lock);
1014 temp_max_hyst = (data->temp_high[nr] - data->temp_hyst[nr]) * 1000;
1015 mutex_unlock(&data->update_lock);
1016
1017 return sprintf(buf, "%d\n", temp_max_hyst);
Hans de Goede45fb3662007-07-13 14:34:19 +02001018}
1019
1020static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
1021 *devattr, const char *buf, size_t count)
1022{
1023 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001024 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001025 long val = simple_strtol(buf, NULL, 10) / 1000;
Hans de Goede45fb3662007-07-13 14:34:19 +02001026 ssize_t ret = count;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001027 u8 reg;
Hans de Goede45fb3662007-07-13 14:34:19 +02001028
1029 mutex_lock(&data->update_lock);
1030
1031 /* convert abs to relative and check */
Hans de Goedece0bfa52009-01-07 16:37:28 +01001032 data->temp_high[nr] = f71882fg_read8(data, F71882FG_REG_TEMP_HIGH(nr));
1033 val = SENSORS_LIMIT(val, data->temp_high[nr] - 15,
1034 data->temp_high[nr]);
Hans de Goede45fb3662007-07-13 14:34:19 +02001035 val = data->temp_high[nr] - val;
Hans de Goede45fb3662007-07-13 14:34:19 +02001036 data->temp_hyst[nr] = val;
1037
1038 /* convert value to register contents */
Mark M. Hoffman8afb1042007-08-21 23:10:46 -04001039 switch (nr) {
Hans de Goede7567a042009-01-07 16:37:28 +01001040 case 1:
Hans de Goedece0bfa52009-01-07 16:37:28 +01001041 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST1);
1042 reg = (reg & 0x0f) | (val << 4);
Hans de Goede45fb3662007-07-13 14:34:19 +02001043 break;
Hans de Goede45fb3662007-07-13 14:34:19 +02001044 case 2:
Hans de Goedece0bfa52009-01-07 16:37:28 +01001045 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST23);
1046 reg = (reg & 0xf0) | val;
Hans de Goede7567a042009-01-07 16:37:28 +01001047 break;
1048 case 3:
Hans de Goedece0bfa52009-01-07 16:37:28 +01001049 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST23);
1050 reg = (reg & 0x0f) | (val << 4);
Hans de Goede45fb3662007-07-13 14:34:19 +02001051 break;
1052 }
1053
Hans de Goede7567a042009-01-07 16:37:28 +01001054 f71882fg_write8(data, (nr <= 1) ? F71882FG_REG_TEMP_HYST1 :
Hans de Goedece0bfa52009-01-07 16:37:28 +01001055 F71882FG_REG_TEMP_HYST23, reg);
Hans de Goede45fb3662007-07-13 14:34:19 +02001056
Hans de Goede45fb3662007-07-13 14:34:19 +02001057 mutex_unlock(&data->update_lock);
1058 return ret;
1059}
1060
1061static ssize_t show_temp_crit(struct device *dev, struct device_attribute
1062 *devattr, char *buf)
1063{
1064 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001065 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001066
1067 return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
1068}
1069
1070static ssize_t store_temp_crit(struct device *dev, struct device_attribute
1071 *devattr, const char *buf, size_t count)
1072{
1073 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001074 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001075 long val = simple_strtol(buf, NULL, 10) / 1000;
1076 val = SENSORS_LIMIT(val, 0, 255);
Hans de Goede45fb3662007-07-13 14:34:19 +02001077
1078 mutex_lock(&data->update_lock);
1079 f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
1080 data->temp_ovt[nr] = val;
1081 mutex_unlock(&data->update_lock);
1082
1083 return count;
1084}
1085
1086static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
1087 *devattr, char *buf)
1088{
1089 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001090 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001091 int temp_crit_hyst;
Hans de Goede45fb3662007-07-13 14:34:19 +02001092
Hans de Goedece0bfa52009-01-07 16:37:28 +01001093 mutex_lock(&data->update_lock);
1094 temp_crit_hyst = (data->temp_ovt[nr] - data->temp_hyst[nr]) * 1000;
1095 mutex_unlock(&data->update_lock);
1096
1097 return sprintf(buf, "%d\n", temp_crit_hyst);
Hans de Goede45fb3662007-07-13 14:34:19 +02001098}
1099
1100static ssize_t show_temp_type(struct device *dev, struct device_attribute
1101 *devattr, char *buf)
1102{
1103 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001104 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001105
1106 return sprintf(buf, "%d\n", data->temp_type[nr]);
1107}
1108
1109static ssize_t show_temp_beep(struct device *dev, struct device_attribute
1110 *devattr, char *buf)
1111{
1112 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001113 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001114
Hans de Goede7567a042009-01-07 16:37:28 +01001115 if (data->temp_beep & (1 << nr))
Hans de Goede45fb3662007-07-13 14:34:19 +02001116 return sprintf(buf, "1\n");
1117 else
1118 return sprintf(buf, "0\n");
1119}
1120
1121static ssize_t store_temp_beep(struct device *dev, struct device_attribute
1122 *devattr, const char *buf, size_t count)
1123{
1124 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001125 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001126 unsigned long val = simple_strtoul(buf, NULL, 10);
Hans de Goede45fb3662007-07-13 14:34:19 +02001127
1128 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001129 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
Hans de Goede45fb3662007-07-13 14:34:19 +02001130 if (val)
Hans de Goede7567a042009-01-07 16:37:28 +01001131 data->temp_beep |= 1 << nr;
Hans de Goede45fb3662007-07-13 14:34:19 +02001132 else
Hans de Goede7567a042009-01-07 16:37:28 +01001133 data->temp_beep &= ~(1 << nr);
Hans de Goede45fb3662007-07-13 14:34:19 +02001134
1135 f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
1136 mutex_unlock(&data->update_lock);
1137
1138 return count;
1139}
1140
1141static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
1142 *devattr, char *buf)
1143{
1144 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001145 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001146
Hans de Goede7567a042009-01-07 16:37:28 +01001147 if (data->temp_status & (1 << nr))
Hans de Goede45fb3662007-07-13 14:34:19 +02001148 return sprintf(buf, "1\n");
1149 else
1150 return sprintf(buf, "0\n");
1151}
1152
1153static ssize_t show_temp_fault(struct device *dev, struct device_attribute
1154 *devattr, char *buf)
1155{
1156 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001157 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001158
Hans de Goede7567a042009-01-07 16:37:28 +01001159 if (data->temp_diode_open & (1 << nr))
Hans de Goede45fb3662007-07-13 14:34:19 +02001160 return sprintf(buf, "1\n");
1161 else
1162 return sprintf(buf, "0\n");
1163}
1164
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001165static ssize_t show_pwm(struct device *dev,
1166 struct device_attribute *devattr, char *buf)
1167{
1168 struct f71882fg_data *data = f71882fg_update_device(dev);
1169 int val, nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001170 mutex_lock(&data->update_lock);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001171 if (data->pwm_enable & (1 << (2 * nr)))
1172 /* PWM mode */
1173 val = data->pwm[nr];
1174 else {
1175 /* RPM mode */
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001176 val = 255 * fan_from_reg(data->fan_target[nr])
1177 / fan_from_reg(data->fan_full_speed[nr]);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001178 }
Hans de Goedece0bfa52009-01-07 16:37:28 +01001179 mutex_unlock(&data->update_lock);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001180 return sprintf(buf, "%d\n", val);
1181}
1182
1183static ssize_t store_pwm(struct device *dev,
1184 struct device_attribute *devattr, const char *buf,
1185 size_t count)
1186{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001187 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001188 int nr = to_sensor_dev_attr_2(devattr)->index;
1189 long val = simple_strtol(buf, NULL, 10);
1190 val = SENSORS_LIMIT(val, 0, 255);
1191
1192 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001193 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001194 if (data->pwm_enable & (1 << (2 * nr))) {
1195 /* PWM mode */
1196 f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1197 data->pwm[nr] = val;
1198 } else {
1199 /* RPM mode */
Hans de Goedece0bfa52009-01-07 16:37:28 +01001200 int target, full_speed;
1201 full_speed = f71882fg_read16(data,
1202 F71882FG_REG_FAN_FULL_SPEED(nr));
1203 target = fan_to_reg(val * fan_from_reg(full_speed) / 255);
1204 f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr), target);
1205 data->fan_target[nr] = target;
1206 data->fan_full_speed[nr] = full_speed;
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001207 }
1208 mutex_unlock(&data->update_lock);
1209
1210 return count;
1211}
1212
1213static ssize_t show_pwm_enable(struct device *dev,
1214 struct device_attribute *devattr, char *buf)
1215{
1216 int result;
1217 struct f71882fg_data *data = f71882fg_update_device(dev);
1218 int nr = to_sensor_dev_attr_2(devattr)->index;
1219
1220 if (data->pwm_enable & (2 << (2 * nr)))
1221 result = 1;
1222 else
1223 result = 2;
1224
1225 return sprintf(buf, "%d\n", result);
1226}
1227
1228static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1229 *devattr, const char *buf, size_t count)
1230{
1231 struct f71882fg_data *data = dev_get_drvdata(dev);
1232 int nr = to_sensor_dev_attr_2(devattr)->index;
1233 long val = simple_strtol(buf, NULL, 10);
1234 if (val < 1 || val > 2)
1235 return -EINVAL;
1236
1237 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001238 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001239 switch (val) {
1240 case 1:
1241 data->pwm_enable |= 2 << (2 * nr);
1242 break; /* Manual */
1243 case 2:
1244 data->pwm_enable &= ~(2 << (2 * nr));
1245 break; /* Temperature ctrl */
1246 }
Hans de Goede498be962009-01-07 16:37:28 +01001247 if (data->type == f71882fg) {
1248 switch (fan_mode[nr]) {
1249 case 1:
1250 data->pwm_enable |= 1 << (2 * nr);
1251 break; /* Duty cycle mode */
1252 case 2:
1253 data->pwm_enable &= ~(1 << (2 * nr));
1254 break; /* RPM mode */
1255 }
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001256 }
1257 f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
1258 mutex_unlock(&data->update_lock);
1259
1260 return count;
1261}
1262
1263static ssize_t show_pwm_auto_point_pwm(struct device *dev,
1264 struct device_attribute *devattr,
1265 char *buf)
1266{
1267 int result;
1268 struct f71882fg_data *data = f71882fg_update_device(dev);
1269 int pwm = to_sensor_dev_attr_2(devattr)->index;
1270 int point = to_sensor_dev_attr_2(devattr)->nr;
1271
Hans de Goedece0bfa52009-01-07 16:37:28 +01001272 mutex_lock(&data->update_lock);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001273 if (data->pwm_enable & (1 << (2 * pwm))) {
1274 /* PWM mode */
1275 result = data->pwm_auto_point_pwm[pwm][point];
1276 } else {
1277 /* RPM mode */
1278 result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
1279 }
Hans de Goedece0bfa52009-01-07 16:37:28 +01001280 mutex_unlock(&data->update_lock);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001281
1282 return sprintf(buf, "%d\n", result);
1283}
1284
1285static ssize_t store_pwm_auto_point_pwm(struct device *dev,
1286 struct device_attribute *devattr,
1287 const char *buf, size_t count)
1288{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001289 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001290 int pwm = to_sensor_dev_attr_2(devattr)->index;
1291 int point = to_sensor_dev_attr_2(devattr)->nr;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001292 long val = simple_strtol(buf, NULL, 10);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001293 val = SENSORS_LIMIT(val, 0, 255);
1294
1295 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001296 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001297 if (data->pwm_enable & (1 << (2 * pwm))) {
1298 /* PWM mode */
1299 } else {
1300 /* RPM mode */
1301 if (val < 29) /* Prevent negative numbers */
1302 val = 255;
1303 else
1304 val = (255 - val) * 32 / val;
1305 }
1306 f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
1307 data->pwm_auto_point_pwm[pwm][point] = val;
1308 mutex_unlock(&data->update_lock);
1309
1310 return count;
1311}
1312
1313static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
1314 struct device_attribute *devattr,
1315 char *buf)
1316{
1317 int result = 0;
1318 struct f71882fg_data *data = f71882fg_update_device(dev);
1319 int nr = to_sensor_dev_attr_2(devattr)->index;
1320 int point = to_sensor_dev_attr_2(devattr)->nr;
1321
1322 mutex_lock(&data->update_lock);
1323 switch (nr) {
1324 case 0:
1325 result = data->pwm_auto_point_hyst[0] & 0x0f;
1326 break;
1327 case 1:
1328 result = data->pwm_auto_point_hyst[0] >> 4;
1329 break;
1330 case 2:
1331 result = data->pwm_auto_point_hyst[1] & 0x0f;
1332 break;
1333 case 3:
1334 result = data->pwm_auto_point_hyst[1] >> 4;
1335 break;
1336 }
1337 result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
1338 mutex_unlock(&data->update_lock);
1339
1340 return sprintf(buf, "%d\n", result);
1341}
1342
1343static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
1344 struct device_attribute *devattr,
1345 const char *buf, size_t count)
1346{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001347 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001348 int nr = to_sensor_dev_attr_2(devattr)->index;
1349 int point = to_sensor_dev_attr_2(devattr)->nr;
1350 long val = simple_strtol(buf, NULL, 10) / 1000;
1351
1352 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001353 data->pwm_auto_point_temp[nr][point] =
1354 f71882fg_read8(data, F71882FG_REG_POINT_TEMP(nr, point));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001355 val = SENSORS_LIMIT(val, data->pwm_auto_point_temp[nr][point] - 15,
1356 data->pwm_auto_point_temp[nr][point]);
1357 val = data->pwm_auto_point_temp[nr][point] - val;
1358
Hans de Goedece0bfa52009-01-07 16:37:28 +01001359 if (nr == 0 || nr == 1) {
1360 data->pwm_auto_point_hyst[0] =
1361 f71882fg_read8(data, F71882FG_REG_FAN_HYST0);
1362 } else {
1363 data->pwm_auto_point_hyst[1] =
1364 f71882fg_read8(data, F71882FG_REG_FAN_HYST1);
1365 }
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001366 switch (nr) {
1367 case 0:
1368 val = (data->pwm_auto_point_hyst[0] & 0xf0) | val;
1369 break;
1370 case 1:
1371 val = (data->pwm_auto_point_hyst[0] & 0x0f) | (val << 4);
1372 break;
1373 case 2:
1374 val = (data->pwm_auto_point_hyst[1] & 0xf0) | val;
1375 break;
1376 case 3:
1377 val = (data->pwm_auto_point_hyst[1] & 0x0f) | (val << 4);
1378 break;
1379 }
1380 if (nr == 0 || nr == 1) {
1381 f71882fg_write8(data, F71882FG_REG_FAN_HYST0, val);
1382 data->pwm_auto_point_hyst[0] = val;
1383 } else {
1384 f71882fg_write8(data, F71882FG_REG_FAN_HYST1, val);
1385 data->pwm_auto_point_hyst[1] = val;
1386 }
1387 mutex_unlock(&data->update_lock);
1388
1389 return count;
1390}
1391
1392static ssize_t show_pwm_interpolate(struct device *dev,
1393 struct device_attribute *devattr, char *buf)
1394{
1395 int result;
1396 struct f71882fg_data *data = f71882fg_update_device(dev);
1397 int nr = to_sensor_dev_attr_2(devattr)->index;
1398
1399 result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
1400
1401 return sprintf(buf, "%d\n", result);
1402}
1403
1404static ssize_t store_pwm_interpolate(struct device *dev,
1405 struct device_attribute *devattr,
1406 const char *buf, size_t count)
1407{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001408 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001409 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001410 unsigned long val = simple_strtoul(buf, NULL, 10);
1411
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001412 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001413 data->pwm_auto_point_mapping[nr] =
1414 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001415 if (val)
1416 val = data->pwm_auto_point_mapping[nr] | (1 << 4);
1417 else
1418 val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
1419 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1420 data->pwm_auto_point_mapping[nr] = val;
1421 mutex_unlock(&data->update_lock);
1422
1423 return count;
1424}
1425
1426static ssize_t show_pwm_auto_point_channel(struct device *dev,
1427 struct device_attribute *devattr,
1428 char *buf)
1429{
1430 int result;
1431 struct f71882fg_data *data = f71882fg_update_device(dev);
1432 int nr = to_sensor_dev_attr_2(devattr)->index;
1433
1434 result = 1 << ((data->pwm_auto_point_mapping[nr] & 3) - 1);
1435
1436 return sprintf(buf, "%d\n", result);
1437}
1438
1439static ssize_t store_pwm_auto_point_channel(struct device *dev,
1440 struct device_attribute *devattr,
1441 const char *buf, size_t count)
1442{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001443 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001444 int nr = to_sensor_dev_attr_2(devattr)->index;
1445 long val = simple_strtol(buf, NULL, 10);
1446 switch (val) {
1447 case 1:
1448 val = 1;
1449 break;
1450 case 2:
1451 val = 2;
1452 break;
1453 case 4:
1454 val = 3;
1455 break;
1456 default:
1457 return -EINVAL;
1458 }
1459 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001460 data->pwm_auto_point_mapping[nr] =
1461 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001462 val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
1463 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1464 data->pwm_auto_point_mapping[nr] = val;
1465 mutex_unlock(&data->update_lock);
1466
1467 return count;
1468}
1469
1470static ssize_t show_pwm_auto_point_temp(struct device *dev,
1471 struct device_attribute *devattr,
1472 char *buf)
1473{
1474 int result;
1475 struct f71882fg_data *data = f71882fg_update_device(dev);
1476 int pwm = to_sensor_dev_attr_2(devattr)->index;
1477 int point = to_sensor_dev_attr_2(devattr)->nr;
1478
1479 result = data->pwm_auto_point_temp[pwm][point];
1480 return sprintf(buf, "%d\n", 1000 * result);
1481}
1482
1483static ssize_t store_pwm_auto_point_temp(struct device *dev,
1484 struct device_attribute *devattr,
1485 const char *buf, size_t count)
1486{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001487 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001488 int pwm = to_sensor_dev_attr_2(devattr)->index;
1489 int point = to_sensor_dev_attr_2(devattr)->nr;
1490 long val = simple_strtol(buf, NULL, 10) / 1000;
1491 val = SENSORS_LIMIT(val, 0, 255);
1492
1493 mutex_lock(&data->update_lock);
1494 f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
1495 data->pwm_auto_point_temp[pwm][point] = val;
1496 mutex_unlock(&data->update_lock);
1497
1498 return count;
1499}
1500
Hans de Goede45fb3662007-07-13 14:34:19 +02001501static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
1502 char *buf)
1503{
Hans de Goede498be962009-01-07 16:37:28 +01001504 struct f71882fg_data *data = dev_get_drvdata(dev);
1505 return sprintf(buf, "%s\n", f71882fg_names[data->type]);
Hans de Goede45fb3662007-07-13 14:34:19 +02001506}
1507
Hans de Goedec13548c2009-01-07 16:37:27 +01001508static int __devinit f71882fg_create_sysfs_files(struct platform_device *pdev,
1509 struct sensor_device_attribute_2 *attr, int count)
1510{
1511 int err, i;
Hans de Goede45fb3662007-07-13 14:34:19 +02001512
Hans de Goedec13548c2009-01-07 16:37:27 +01001513 for (i = 0; i < count; i++) {
1514 err = device_create_file(&pdev->dev, &attr[i].dev_attr);
1515 if (err)
1516 return err;
1517 }
1518 return 0;
1519}
1520
1521static int __devinit f71882fg_probe(struct platform_device *pdev)
Hans de Goede45fb3662007-07-13 14:34:19 +02001522{
1523 struct f71882fg_data *data;
Hans de Goede498be962009-01-07 16:37:28 +01001524 struct f71882fg_sio_data *sio_data = pdev->dev.platform_data;
Hans de Goedec13548c2009-01-07 16:37:27 +01001525 int err;
Hans de Goede45fb3662007-07-13 14:34:19 +02001526 u8 start_reg;
1527
Hans de Goedec13548c2009-01-07 16:37:27 +01001528 data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL);
1529 if (!data)
Hans de Goede45fb3662007-07-13 14:34:19 +02001530 return -ENOMEM;
1531
1532 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
Hans de Goede498be962009-01-07 16:37:28 +01001533 data->type = sio_data->type;
Hans de Goede45fb3662007-07-13 14:34:19 +02001534 mutex_init(&data->update_lock);
1535 platform_set_drvdata(pdev, data);
1536
Hans de Goede3cc74752009-01-07 16:37:28 +01001537 start_reg = f71882fg_read8(data, F71882FG_REG_START);
1538 if (!(start_reg & 0x03)) {
1539 dev_warn(&pdev->dev, "Hardware monitoring not activated\n");
1540 err = -ENODEV;
1541 goto exit_free;
1542 }
1543
1544 /* If it is a 71862 and the fan / pwm part is enabled sanity check
1545 the pwm settings */
1546 if (data->type == f71862fg && (start_reg & 0x02)) {
1547 u8 reg = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1548 if ((reg & 0x15) != 0x15) {
1549 dev_err(&pdev->dev,
1550 "Invalid (reserved) pwm settings: 0x%02x\n",
1551 (unsigned int)reg);
1552 err = -ENODEV;
1553 goto exit_free;
1554 }
1555 }
1556
Hans de Goede45fb3662007-07-13 14:34:19 +02001557 /* Register sysfs interface files */
Hans de Goedec13548c2009-01-07 16:37:27 +01001558 err = device_create_file(&pdev->dev, &dev_attr_name);
1559 if (err)
1560 goto exit_unregister_sysfs;
1561
Hans de Goedec13548c2009-01-07 16:37:27 +01001562 if (start_reg & 0x01) {
Hans de Goede498be962009-01-07 16:37:28 +01001563 err = f71882fg_create_sysfs_files(pdev, f718x2fg_in_temp_attr,
1564 ARRAY_SIZE(f718x2fg_in_temp_attr));
Hans de Goede45fb3662007-07-13 14:34:19 +02001565 if (err)
1566 goto exit_unregister_sysfs;
Hans de Goede498be962009-01-07 16:37:28 +01001567
1568 if (data->type == f71882fg) {
1569 err = f71882fg_create_sysfs_files(pdev,
1570 f71882fg_in_temp_attr,
1571 ARRAY_SIZE(f71882fg_in_temp_attr));
1572 if (err)
1573 goto exit_unregister_sysfs;
1574 }
Hans de Goede45fb3662007-07-13 14:34:19 +02001575 }
1576
Hans de Goede45fb3662007-07-13 14:34:19 +02001577 if (start_reg & 0x02) {
Hans de Goede498be962009-01-07 16:37:28 +01001578 err = f71882fg_create_sysfs_files(pdev, f718x2fg_fan_attr,
1579 ARRAY_SIZE(f718x2fg_fan_attr));
1580 if (err)
1581 goto exit_unregister_sysfs;
1582
1583 if (data->type == f71862fg) {
1584 err = f71882fg_create_sysfs_files(pdev,
1585 f71862fg_fan_attr,
1586 ARRAY_SIZE(f71862fg_fan_attr));
1587 } else {
1588 err = f71882fg_create_sysfs_files(pdev,
1589 f71882fg_fan_attr,
Hans de Goedec13548c2009-01-07 16:37:27 +01001590 ARRAY_SIZE(f71882fg_fan_attr));
Hans de Goede498be962009-01-07 16:37:28 +01001591 }
Hans de Goedec13548c2009-01-07 16:37:27 +01001592 if (err)
1593 goto exit_unregister_sysfs;
Hans de Goede45fb3662007-07-13 14:34:19 +02001594 }
1595
Tony Jones1beeffe2007-08-20 13:46:20 -07001596 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1597 if (IS_ERR(data->hwmon_dev)) {
1598 err = PTR_ERR(data->hwmon_dev);
Hans de Goedec13548c2009-01-07 16:37:27 +01001599 data->hwmon_dev = NULL;
Hans de Goede45fb3662007-07-13 14:34:19 +02001600 goto exit_unregister_sysfs;
1601 }
1602
1603 return 0;
1604
1605exit_unregister_sysfs:
Hans de Goedec13548c2009-01-07 16:37:27 +01001606 f71882fg_remove(pdev); /* Will unregister the sysfs files for us */
Hans de Goede3cc74752009-01-07 16:37:28 +01001607 return err; /* f71882fg_remove() also frees our data */
1608exit_free:
1609 kfree(data);
Hans de Goede45fb3662007-07-13 14:34:19 +02001610 return err;
1611}
1612
Hans de Goedec13548c2009-01-07 16:37:27 +01001613static int f71882fg_remove(struct platform_device *pdev)
Hans de Goede45fb3662007-07-13 14:34:19 +02001614{
1615 int i;
1616 struct f71882fg_data *data = platform_get_drvdata(pdev);
1617
1618 platform_set_drvdata(pdev, NULL);
Hans de Goedec13548c2009-01-07 16:37:27 +01001619 if (data->hwmon_dev)
1620 hwmon_device_unregister(data->hwmon_dev);
Hans de Goede45fb3662007-07-13 14:34:19 +02001621
Hans de Goedec13548c2009-01-07 16:37:27 +01001622 device_remove_file(&pdev->dev, &dev_attr_name);
Hans de Goede45fb3662007-07-13 14:34:19 +02001623
Hans de Goede498be962009-01-07 16:37:28 +01001624 for (i = 0; i < ARRAY_SIZE(f718x2fg_in_temp_attr); i++)
1625 device_remove_file(&pdev->dev,
1626 &f718x2fg_in_temp_attr[i].dev_attr);
1627
Hans de Goede45fb3662007-07-13 14:34:19 +02001628 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
1629 device_remove_file(&pdev->dev,
1630 &f71882fg_in_temp_attr[i].dev_attr);
1631
Hans de Goede498be962009-01-07 16:37:28 +01001632 for (i = 0; i < ARRAY_SIZE(f718x2fg_fan_attr); i++)
1633 device_remove_file(&pdev->dev, &f718x2fg_fan_attr[i].dev_attr);
1634
1635 for (i = 0; i < ARRAY_SIZE(f71862fg_fan_attr); i++)
1636 device_remove_file(&pdev->dev, &f71862fg_fan_attr[i].dev_attr);
1637
Hans de Goede45fb3662007-07-13 14:34:19 +02001638 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
1639 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
1640
1641 kfree(data);
1642
1643 return 0;
1644}
1645
Hans de Goede498be962009-01-07 16:37:28 +01001646static int __init f71882fg_find(int sioaddr, unsigned short *address,
1647 struct f71882fg_sio_data *sio_data)
Hans de Goede45fb3662007-07-13 14:34:19 +02001648{
1649 int err = -ENODEV;
1650 u16 devid;
Hans de Goede45fb3662007-07-13 14:34:19 +02001651
1652 superio_enter(sioaddr);
1653
1654 devid = superio_inw(sioaddr, SIO_REG_MANID);
1655 if (devid != SIO_FINTEK_ID) {
1656 printk(KERN_INFO DRVNAME ": Not a Fintek device\n");
1657 goto exit;
1658 }
1659
Jean Delvare67b671b2007-12-06 23:13:42 +01001660 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
Hans de Goede498be962009-01-07 16:37:28 +01001661 switch (devid) {
1662 case SIO_F71862_ID:
1663 sio_data->type = f71862fg;
1664 break;
1665 case SIO_F71882_ID:
1666 sio_data->type = f71882fg;
1667 break;
1668 default:
Hans de Goede45fb3662007-07-13 14:34:19 +02001669 printk(KERN_INFO DRVNAME ": Unsupported Fintek device\n");
1670 goto exit;
1671 }
1672
1673 superio_select(sioaddr, SIO_F71882FG_LD_HWM);
Mark M. Hoffman8afb1042007-08-21 23:10:46 -04001674 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
Hans de Goede45fb3662007-07-13 14:34:19 +02001675 printk(KERN_WARNING DRVNAME ": Device not activated\n");
1676 goto exit;
1677 }
1678
1679 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1680 if (*address == 0)
1681 {
1682 printk(KERN_WARNING DRVNAME ": Base address not set\n");
1683 goto exit;
1684 }
1685 *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
1686
Hans de Goede45fb3662007-07-13 14:34:19 +02001687 err = 0;
Hans de Goede498be962009-01-07 16:37:28 +01001688 printk(KERN_INFO DRVNAME ": Found %s chip at %#x, revision %d\n",
1689 f71882fg_names[sio_data->type], (unsigned int)*address,
Hans de Goede45fb3662007-07-13 14:34:19 +02001690 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
1691exit:
1692 superio_exit(sioaddr);
1693 return err;
1694}
1695
Hans de Goede498be962009-01-07 16:37:28 +01001696static int __init f71882fg_device_add(unsigned short address,
1697 const struct f71882fg_sio_data *sio_data)
Hans de Goede45fb3662007-07-13 14:34:19 +02001698{
1699 struct resource res = {
1700 .start = address,
1701 .end = address + REGION_LENGTH - 1,
1702 .flags = IORESOURCE_IO,
1703 };
1704 int err;
1705
1706 f71882fg_pdev = platform_device_alloc(DRVNAME, address);
Mark M. Hoffman8afb1042007-08-21 23:10:46 -04001707 if (!f71882fg_pdev)
Hans de Goede45fb3662007-07-13 14:34:19 +02001708 return -ENOMEM;
1709
1710 res.name = f71882fg_pdev->name;
1711 err = platform_device_add_resources(f71882fg_pdev, &res, 1);
Mark M. Hoffman8afb1042007-08-21 23:10:46 -04001712 if (err) {
Hans de Goede45fb3662007-07-13 14:34:19 +02001713 printk(KERN_ERR DRVNAME ": Device resource addition failed\n");
1714 goto exit_device_put;
1715 }
1716
Hans de Goede498be962009-01-07 16:37:28 +01001717 err = platform_device_add_data(f71882fg_pdev, sio_data,
1718 sizeof(struct f71882fg_sio_data));
1719 if (err) {
1720 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1721 goto exit_device_put;
1722 }
1723
Hans de Goede45fb3662007-07-13 14:34:19 +02001724 err = platform_device_add(f71882fg_pdev);
Mark M. Hoffman8afb1042007-08-21 23:10:46 -04001725 if (err) {
Hans de Goede45fb3662007-07-13 14:34:19 +02001726 printk(KERN_ERR DRVNAME ": Device addition failed\n");
1727 goto exit_device_put;
1728 }
1729
1730 return 0;
1731
1732exit_device_put:
1733 platform_device_put(f71882fg_pdev);
1734
1735 return err;
1736}
1737
1738static int __init f71882fg_init(void)
1739{
1740 int err = -ENODEV;
1741 unsigned short address;
Hans de Goede498be962009-01-07 16:37:28 +01001742 struct f71882fg_sio_data sio_data;
Hans de Goede45fb3662007-07-13 14:34:19 +02001743
Hans de Goede498be962009-01-07 16:37:28 +01001744 memset(&sio_data, 0, sizeof(sio_data));
1745
1746 if (f71882fg_find(0x2e, &address, &sio_data) &&
1747 f71882fg_find(0x4e, &address, &sio_data))
Hans de Goede45fb3662007-07-13 14:34:19 +02001748 goto exit;
1749
Hans de Goedec13548c2009-01-07 16:37:27 +01001750 err = platform_driver_register(&f71882fg_driver);
1751 if (err)
Hans de Goede45fb3662007-07-13 14:34:19 +02001752 goto exit;
1753
Hans de Goede498be962009-01-07 16:37:28 +01001754 err = f71882fg_device_add(address, &sio_data);
Hans de Goedec13548c2009-01-07 16:37:27 +01001755 if (err)
Hans de Goede45fb3662007-07-13 14:34:19 +02001756 goto exit_driver;
1757
1758 return 0;
1759
1760exit_driver:
1761 platform_driver_unregister(&f71882fg_driver);
1762exit:
1763 return err;
1764}
1765
1766static void __exit f71882fg_exit(void)
1767{
1768 platform_device_unregister(f71882fg_pdev);
1769 platform_driver_unregister(&f71882fg_driver);
1770}
1771
1772MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
Hans de Goedec13548c2009-01-07 16:37:27 +01001773MODULE_AUTHOR("Hans Edgington, Hans de Goede (hdegoede@redhat.com)");
Hans de Goede45fb3662007-07-13 14:34:19 +02001774MODULE_LICENSE("GPL");
1775
1776module_init(f71882fg_init);
1777module_exit(f71882fg_exit);