blob: 03a4f84b4e5cd4df420f9b2f99db045413c6cc3d [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
Hans de Goedebc274902009-01-07 16:37:29 +010071#define F71882FG_REG_TEMP_HYST(nr) (0x6C + (nr))
Hans de Goede45fb3662007-07-13 14:34:19 +020072#define F71882FG_REG_TEMP_TYPE 0x6B
73#define F71882FG_REG_TEMP_DIODE_OPEN 0x6F
74
Mark van Doesburg9ab796e2009-01-07 16:37:27 +010075#define F71882FG_REG_PWM(nr) (0xA3 + (16 * (nr)))
76#define F71882FG_REG_PWM_TYPE 0x94
77#define F71882FG_REG_PWM_ENABLE 0x96
78
Hans de Goedebc274902009-01-07 16:37:29 +010079#define F71882FG_REG_FAN_HYST(nr) (0x98 + (nr))
Mark van Doesburg9ab796e2009-01-07 16:37:27 +010080
81#define F71882FG_REG_POINT_PWM(pwm, point) (0xAA + (point) + (16 * (pwm)))
82#define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm)))
83#define F71882FG_REG_POINT_MAPPING(nr) (0xAF + 16 * (nr))
84
Hans de Goede45fb3662007-07-13 14:34:19 +020085#define F71882FG_REG_START 0x01
86
87#define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */
88
Jean Delvare67b671b2007-12-06 23:13:42 +010089static unsigned short force_id;
90module_param(force_id, ushort, 0);
91MODULE_PARM_DESC(force_id, "Override the detected device ID");
92
Mark van Doesburg9ab796e2009-01-07 16:37:27 +010093static int fan_mode[4] = { 0, 0, 0, 0 };
94module_param_array(fan_mode, int, NULL, 0644);
95MODULE_PARM_DESC(fan_mode, "List of fan control modes (f71882fg only) "
96 "(0=don't change, 1=pwm, 2=rpm)\n"
97 "Note: this needs a write to pwm#_enable to take effect");
98
Hans de Goede498be962009-01-07 16:37:28 +010099enum chips { f71862fg, f71882fg };
100
101static const char *f71882fg_names[] = {
102 "f71862fg",
103 "f71882fg",
104};
105
Mark van Doesburg77a4a3e2009-01-07 16:37:27 +0100106static struct platform_device *f71882fg_pdev;
Hans de Goede45fb3662007-07-13 14:34:19 +0200107
108/* Super-I/O Function prototypes */
109static inline int superio_inb(int base, int reg);
110static inline int superio_inw(int base, int reg);
111static inline void superio_enter(int base);
112static inline void superio_select(int base, int ld);
113static inline void superio_exit(int base);
114
Hans de Goede498be962009-01-07 16:37:28 +0100115struct f71882fg_sio_data {
116 enum chips type;
117};
118
Hans de Goede45fb3662007-07-13 14:34:19 +0200119struct f71882fg_data {
120 unsigned short addr;
Hans de Goede498be962009-01-07 16:37:28 +0100121 enum chips type;
Tony Jones1beeffe2007-08-20 13:46:20 -0700122 struct device *hwmon_dev;
Hans de Goede45fb3662007-07-13 14:34:19 +0200123
124 struct mutex update_lock;
125 char valid; /* !=0 if following fields are valid */
126 unsigned long last_updated; /* In jiffies */
127 unsigned long last_limits; /* In jiffies */
128
129 /* Register Values */
130 u8 in[9];
131 u8 in1_max;
132 u8 in_status;
133 u8 in_beep;
134 u16 fan[4];
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100135 u16 fan_target[4];
136 u16 fan_full_speed[4];
Hans de Goede45fb3662007-07-13 14:34:19 +0200137 u8 fan_status;
138 u8 fan_beep;
Hans de Goede7567a042009-01-07 16:37:28 +0100139 /* Note: all models have only 3 temperature channels, but on some
140 they are addressed as 0-2 and on others as 1-3, so for coding
141 convenience we reserve space for 4 channels */
142 u8 temp[4];
143 u8 temp_ovt[4];
144 u8 temp_high[4];
Hans de Goedebc274902009-01-07 16:37:29 +0100145 u8 temp_hyst[2]; /* 2 hysts stored per reg */
Hans de Goede7567a042009-01-07 16:37:28 +0100146 u8 temp_type[4];
Hans de Goede45fb3662007-07-13 14:34:19 +0200147 u8 temp_status;
148 u8 temp_beep;
149 u8 temp_diode_open;
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100150 u8 pwm[4];
151 u8 pwm_enable;
152 u8 pwm_auto_point_hyst[2];
153 u8 pwm_auto_point_mapping[4];
154 u8 pwm_auto_point_pwm[4][5];
155 u8 pwm_auto_point_temp[4][4];
Hans de Goede45fb3662007-07-13 14:34:19 +0200156};
157
Mark van Doesburg77a4a3e2009-01-07 16:37:27 +0100158/* Sysfs in */
Hans de Goede45fb3662007-07-13 14:34:19 +0200159static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
160 char *buf);
161static ssize_t show_in_max(struct device *dev, struct device_attribute
162 *devattr, char *buf);
163static ssize_t store_in_max(struct device *dev, struct device_attribute
164 *devattr, const char *buf, size_t count);
165static ssize_t show_in_beep(struct device *dev, struct device_attribute
166 *devattr, char *buf);
167static ssize_t store_in_beep(struct device *dev, struct device_attribute
168 *devattr, const char *buf, size_t count);
169static ssize_t show_in_alarm(struct device *dev, struct device_attribute
170 *devattr, char *buf);
171/* Sysfs Fan */
172static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
173 char *buf);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100174static ssize_t show_fan_full_speed(struct device *dev,
175 struct device_attribute *devattr, char *buf);
176static ssize_t store_fan_full_speed(struct device *dev,
177 struct device_attribute *devattr, const char *buf, size_t count);
Hans de Goede45fb3662007-07-13 14:34:19 +0200178static ssize_t show_fan_beep(struct device *dev, struct device_attribute
179 *devattr, char *buf);
180static ssize_t store_fan_beep(struct device *dev, struct device_attribute
181 *devattr, const char *buf, size_t count);
182static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
183 *devattr, char *buf);
184/* Sysfs Temp */
185static ssize_t show_temp(struct device *dev, struct device_attribute
186 *devattr, char *buf);
187static ssize_t show_temp_max(struct device *dev, struct device_attribute
188 *devattr, char *buf);
189static ssize_t store_temp_max(struct device *dev, struct device_attribute
190 *devattr, const char *buf, size_t count);
191static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
192 *devattr, char *buf);
193static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
194 *devattr, const char *buf, size_t count);
195static ssize_t show_temp_crit(struct device *dev, struct device_attribute
196 *devattr, char *buf);
197static ssize_t store_temp_crit(struct device *dev, struct device_attribute
198 *devattr, const char *buf, size_t count);
199static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
200 *devattr, char *buf);
201static ssize_t show_temp_type(struct device *dev, struct device_attribute
202 *devattr, char *buf);
203static ssize_t show_temp_beep(struct device *dev, struct device_attribute
204 *devattr, char *buf);
205static ssize_t store_temp_beep(struct device *dev, struct device_attribute
206 *devattr, const char *buf, size_t count);
207static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
208 *devattr, char *buf);
209static ssize_t show_temp_fault(struct device *dev, struct device_attribute
210 *devattr, char *buf);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100211/* PWM and Auto point control */
212static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
213 char *buf);
214static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
215 const char *buf, size_t count);
216static ssize_t show_pwm_enable(struct device *dev,
217 struct device_attribute *devattr, char *buf);
218static ssize_t store_pwm_enable(struct device *dev,
219 struct device_attribute *devattr, const char *buf, size_t count);
220static ssize_t show_pwm_interpolate(struct device *dev,
221 struct device_attribute *devattr, char *buf);
222static ssize_t store_pwm_interpolate(struct device *dev,
223 struct device_attribute *devattr, const char *buf, size_t count);
224static ssize_t show_pwm_auto_point_channel(struct device *dev,
225 struct device_attribute *devattr, char *buf);
226static ssize_t store_pwm_auto_point_channel(struct device *dev,
227 struct device_attribute *devattr, const char *buf, size_t count);
228static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
229 struct device_attribute *devattr, char *buf);
230static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
231 struct device_attribute *devattr, const char *buf, size_t count);
232static ssize_t show_pwm_auto_point_pwm(struct device *dev,
233 struct device_attribute *devattr, char *buf);
234static ssize_t store_pwm_auto_point_pwm(struct device *dev,
235 struct device_attribute *devattr, const char *buf, size_t count);
236static ssize_t show_pwm_auto_point_temp(struct device *dev,
237 struct device_attribute *devattr, char *buf);
238static ssize_t store_pwm_auto_point_temp(struct device *dev,
239 struct device_attribute *devattr, const char *buf, size_t count);
Hans de Goede45fb3662007-07-13 14:34:19 +0200240/* Sysfs misc */
241static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
242 char *buf);
243
244static int __devinit f71882fg_probe(struct platform_device * pdev);
Hans de Goedec13548c2009-01-07 16:37:27 +0100245static int f71882fg_remove(struct platform_device *pdev);
Hans de Goede45fb3662007-07-13 14:34:19 +0200246
247static struct platform_driver f71882fg_driver = {
248 .driver = {
249 .owner = THIS_MODULE,
250 .name = DRVNAME,
251 },
252 .probe = f71882fg_probe,
253 .remove = __devexit_p(f71882fg_remove),
254};
255
Hans de Goedec13548c2009-01-07 16:37:27 +0100256static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Hans de Goede45fb3662007-07-13 14:34:19 +0200257
Hans de Goede498be962009-01-07 16:37:28 +0100258static struct sensor_device_attribute_2 f718x2fg_in_temp_attr[] = {
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100259 SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
260 SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100261 SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
262 SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
263 SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
264 SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
265 SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
266 SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
267 SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
Hans de Goede7567a042009-01-07 16:37:28 +0100268 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100269 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100270 store_temp_max, 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100271 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100272 store_temp_max_hyst, 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100273 SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100274 store_temp_crit, 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100275 SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100276 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100277 SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1),
278 SENSOR_ATTR_2(temp1_beep, S_IRUGO|S_IWUSR, show_temp_beep,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100279 store_temp_beep, 0, 1),
Hans de Goede7567a042009-01-07 16:37:28 +0100280 SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
281 SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
282 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2),
283 SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100284 store_temp_max, 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100285 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100286 store_temp_max_hyst, 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100287 SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100288 store_temp_crit, 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100289 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100290 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100291 SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2),
292 SENSOR_ATTR_2(temp2_beep, S_IRUGO|S_IWUSR, show_temp_beep,
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100293 store_temp_beep, 0, 2),
Hans de Goede7567a042009-01-07 16:37:28 +0100294 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
295 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
296 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3),
297 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
298 store_temp_max, 0, 3),
299 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
300 store_temp_max_hyst, 0, 3),
301 SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
302 store_temp_crit, 0, 3),
303 SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
304 0, 3),
305 SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3),
306 SENSOR_ATTR_2(temp3_beep, S_IRUGO|S_IWUSR, show_temp_beep,
307 store_temp_beep, 0, 3),
308 SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3),
309 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3),
Hans de Goede45fb3662007-07-13 14:34:19 +0200310};
311
Hans de Goede498be962009-01-07 16:37:28 +0100312static struct sensor_device_attribute_2 f71882fg_in_temp_attr[] = {
313 SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
314 0, 1),
315 SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
316 0, 1),
317 SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
318};
319
320static struct sensor_device_attribute_2 f718x2fg_fan_attr[] = {
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100321 SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100322 SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
323 show_fan_full_speed,
324 store_fan_full_speed, 0, 0),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100325 SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
326 store_fan_beep, 0, 0),
327 SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
328 SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100329 SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
330 show_fan_full_speed,
331 store_fan_full_speed, 0, 1),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100332 SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
333 store_fan_beep, 0, 1),
334 SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
335 SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100336 SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
337 show_fan_full_speed,
338 store_fan_full_speed, 0, 2),
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100339 SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
340 store_fan_beep, 0, 2),
341 SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100342
343 SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
344 SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
345 store_pwm_enable, 0, 0),
346 SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
347 show_pwm_interpolate, store_pwm_interpolate, 0, 0),
348 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
349 show_pwm_auto_point_channel,
350 store_pwm_auto_point_channel, 0, 0),
Hans de Goede498be962009-01-07 16:37:28 +0100351
352 SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
353 SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
354 store_pwm_enable, 0, 1),
355 SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
356 show_pwm_interpolate, store_pwm_interpolate, 0, 1),
357 SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
358 show_pwm_auto_point_channel,
359 store_pwm_auto_point_channel, 0, 1),
360
361 SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
362 SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
363 store_pwm_enable, 0, 2),
364 SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
365 show_pwm_interpolate, store_pwm_interpolate, 0, 2),
366 SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
367 show_pwm_auto_point_channel,
368 store_pwm_auto_point_channel, 0, 2),
369};
370
371static struct sensor_device_attribute_2 f71862fg_fan_attr[] = {
372 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
373 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
374 1, 0),
375 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
376 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
377 4, 0),
378 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
379 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
380 0, 0),
381 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
382 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
383 3, 0),
384 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
385 show_pwm_auto_point_temp_hyst,
386 store_pwm_auto_point_temp_hyst,
387 0, 0),
388 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
389 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
390
391 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
392 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
393 1, 1),
394 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
395 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
396 4, 1),
397 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
398 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
399 0, 1),
400 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
401 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
402 3, 1),
403 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
404 show_pwm_auto_point_temp_hyst,
405 store_pwm_auto_point_temp_hyst,
406 0, 1),
407 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
408 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
409};
410
411static struct sensor_device_attribute_2 f71882fg_fan_attr[] = {
412 SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
413 SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
414 show_fan_full_speed,
415 store_fan_full_speed, 0, 3),
416 SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
417 store_fan_beep, 0, 3),
418 SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
419
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100420 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
421 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
422 0, 0),
423 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
424 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
425 1, 0),
426 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
427 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
428 2, 0),
429 SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
430 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
431 3, 0),
432 SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
433 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
434 4, 0),
435 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
436 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
437 0, 0),
438 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
439 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
440 1, 0),
441 SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
442 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
443 2, 0),
444 SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
445 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
446 3, 0),
447 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
448 show_pwm_auto_point_temp_hyst,
449 store_pwm_auto_point_temp_hyst,
450 0, 0),
451 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
452 show_pwm_auto_point_temp_hyst, NULL, 1, 0),
453 SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
454 show_pwm_auto_point_temp_hyst, NULL, 2, 0),
455 SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
456 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
457
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100458 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
459 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
460 0, 1),
461 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
462 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
463 1, 1),
464 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
465 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
466 2, 1),
467 SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
468 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
469 3, 1),
470 SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
471 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
472 4, 1),
473 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
474 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
475 0, 1),
476 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
477 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
478 1, 1),
479 SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
480 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
481 2, 1),
482 SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
483 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
484 3, 1),
485 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
486 show_pwm_auto_point_temp_hyst,
487 store_pwm_auto_point_temp_hyst,
488 0, 1),
489 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
490 show_pwm_auto_point_temp_hyst, NULL, 1, 1),
491 SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
492 show_pwm_auto_point_temp_hyst, NULL, 2, 1),
493 SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
494 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
495
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100496 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
497 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
498 0, 2),
499 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
500 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
501 1, 2),
502 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
503 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
504 2, 2),
505 SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
506 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
507 3, 2),
508 SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
509 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
510 4, 2),
511 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
512 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
513 0, 2),
514 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
515 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
516 1, 2),
517 SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
518 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
519 2, 2),
520 SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
521 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
522 3, 2),
523 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
524 show_pwm_auto_point_temp_hyst,
525 store_pwm_auto_point_temp_hyst,
526 0, 2),
527 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
528 show_pwm_auto_point_temp_hyst, NULL, 1, 2),
529 SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
530 show_pwm_auto_point_temp_hyst, NULL, 2, 2),
531 SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
532 show_pwm_auto_point_temp_hyst, NULL, 3, 2),
533
534 SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
535 SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
536 store_pwm_enable, 0, 3),
537 SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
538 show_pwm_interpolate, store_pwm_interpolate, 0, 3),
539 SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
540 show_pwm_auto_point_channel,
541 store_pwm_auto_point_channel, 0, 3),
542 SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
543 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
544 0, 3),
545 SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
546 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
547 1, 3),
548 SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
549 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
550 2, 3),
551 SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
552 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
553 3, 3),
554 SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
555 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
556 4, 3),
557 SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
558 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
559 0, 3),
560 SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
561 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
562 1, 3),
563 SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
564 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
565 2, 3),
566 SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
567 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
568 3, 3),
569 SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
570 show_pwm_auto_point_temp_hyst,
571 store_pwm_auto_point_temp_hyst,
572 0, 3),
573 SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
574 show_pwm_auto_point_temp_hyst, NULL, 1, 3),
575 SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
576 show_pwm_auto_point_temp_hyst, NULL, 2, 3),
577 SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
578 show_pwm_auto_point_temp_hyst, NULL, 3, 3),
Hans de Goede45fb3662007-07-13 14:34:19 +0200579};
580
581
582/* Super I/O functions */
583static inline int superio_inb(int base, int reg)
584{
585 outb(reg, base);
586 return inb(base + 1);
587}
588
589static int superio_inw(int base, int reg)
590{
591 int val;
592 outb(reg++, base);
593 val = inb(base + 1) << 8;
594 outb(reg, base);
595 val |= inb(base + 1);
596 return val;
597}
598
599static inline void superio_enter(int base)
600{
601 /* according to the datasheet the key must be send twice! */
602 outb( SIO_UNLOCK_KEY, base);
603 outb( SIO_UNLOCK_KEY, base);
604}
605
606static inline void superio_select( int base, int ld)
607{
608 outb(SIO_REG_LDSEL, base);
609 outb(ld, base + 1);
610}
611
612static inline void superio_exit(int base)
613{
614 outb(SIO_LOCK_KEY, base);
615}
616
617static inline u16 fan_from_reg(u16 reg)
618{
619 return reg ? (1500000 / reg) : 0;
620}
621
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100622static inline u16 fan_to_reg(u16 fan)
623{
624 return fan ? (1500000 / fan) : 0;
625}
626
Hans de Goede45fb3662007-07-13 14:34:19 +0200627static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
628{
629 u8 val;
630
631 outb(reg, data->addr + ADDR_REG_OFFSET);
632 val = inb(data->addr + DATA_REG_OFFSET);
633
634 return val;
635}
636
637static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
638{
639 u16 val;
640
641 outb(reg++, data->addr + ADDR_REG_OFFSET);
642 val = inb(data->addr + DATA_REG_OFFSET) << 8;
643 outb(reg, data->addr + ADDR_REG_OFFSET);
644 val |= inb(data->addr + DATA_REG_OFFSET);
645
646 return val;
647}
648
649static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
650{
651 outb(reg, data->addr + ADDR_REG_OFFSET);
652 outb(val, data->addr + DATA_REG_OFFSET);
653}
654
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100655static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
656{
657 outb(reg++, data->addr + ADDR_REG_OFFSET);
658 outb(val >> 8, data->addr + DATA_REG_OFFSET);
659 outb(reg, data->addr + ADDR_REG_OFFSET);
660 outb(val & 255, data->addr + DATA_REG_OFFSET);
661}
662
Mark van Doesburg77a4a3e2009-01-07 16:37:27 +0100663static struct f71882fg_data *f71882fg_update_device(struct device *dev)
Hans de Goede45fb3662007-07-13 14:34:19 +0200664{
665 struct f71882fg_data *data = dev_get_drvdata(dev);
666 int nr, reg, reg2;
Hans de Goede498be962009-01-07 16:37:28 +0100667 int nr_fans = (data->type == f71862fg) ? 3 : 4;
Hans de Goede45fb3662007-07-13 14:34:19 +0200668
669 mutex_lock(&data->update_lock);
670
671 /* Update once every 60 seconds */
672 if ( time_after(jiffies, data->last_limits + 60 * HZ ) ||
673 !data->valid) {
Hans de Goede498be962009-01-07 16:37:28 +0100674 if (data->type == f71882fg) {
675 data->in1_max =
676 f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
677 data->in_beep =
678 f71882fg_read8(data, F71882FG_REG_IN_BEEP);
679 }
Hans de Goede45fb3662007-07-13 14:34:19 +0200680
681 /* Get High & boundary temps*/
Hans de Goede7567a042009-01-07 16:37:28 +0100682 for (nr = 1; nr < 4; nr++) {
Hans de Goede45fb3662007-07-13 14:34:19 +0200683 data->temp_ovt[nr] = f71882fg_read8(data,
684 F71882FG_REG_TEMP_OVT(nr));
685 data->temp_high[nr] = f71882fg_read8(data,
686 F71882FG_REG_TEMP_HIGH(nr));
687 }
688
Hans de Goedebc274902009-01-07 16:37:29 +0100689 /* hyst */
690 data->temp_hyst[0] =
691 f71882fg_read8(data, F71882FG_REG_TEMP_HYST(0));
692 data->temp_hyst[1] =
693 f71882fg_read8(data, F71882FG_REG_TEMP_HYST(1));
Hans de Goede45fb3662007-07-13 14:34:19 +0200694
695 /* Have to hardcode type, because temp1 is special */
696 reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
697 reg2 = f71882fg_read8(data, F71882FG_REG_PECI);
698 if ((reg2 & 0x03) == 0x01)
Hans de Goede7567a042009-01-07 16:37:28 +0100699 data->temp_type[1] = 6 /* PECI */;
Hans de Goede45fb3662007-07-13 14:34:19 +0200700 else if ((reg2 & 0x03) == 0x02)
Hans de Goede7567a042009-01-07 16:37:28 +0100701 data->temp_type[1] = 5 /* AMDSI */;
Hans de Goede45fb3662007-07-13 14:34:19 +0200702 else
Hans de Goede7567a042009-01-07 16:37:28 +0100703 data->temp_type[1] = (reg & 0x02) ? 2 : 4;
Hans de Goede45fb3662007-07-13 14:34:19 +0200704
Hans de Goede7567a042009-01-07 16:37:28 +0100705 data->temp_type[2] = (reg & 0x04) ? 2 : 4;
706 data->temp_type[3] = (reg & 0x08) ? 2 : 4;
Hans de Goede45fb3662007-07-13 14:34:19 +0200707
708 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
709
710 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
711
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100712 data->pwm_enable = f71882fg_read8(data,
713 F71882FG_REG_PWM_ENABLE);
Hans de Goedebc274902009-01-07 16:37:29 +0100714 data->pwm_auto_point_hyst[0] =
715 f71882fg_read8(data, F71882FG_REG_FAN_HYST(0));
716 data->pwm_auto_point_hyst[1] =
717 f71882fg_read8(data, F71882FG_REG_FAN_HYST(1));
718
Hans de Goede498be962009-01-07 16:37:28 +0100719 for (nr = 0; nr < nr_fans; nr++) {
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100720 data->pwm_auto_point_mapping[nr] =
721 f71882fg_read8(data,
722 F71882FG_REG_POINT_MAPPING(nr));
723
Hans de Goede498be962009-01-07 16:37:28 +0100724 if (data->type == f71882fg) {
725 int point;
726 for (point = 0; point < 5; point++) {
727 data->pwm_auto_point_pwm[nr][point] =
728 f71882fg_read8(data,
729 F71882FG_REG_POINT_PWM
730 (nr, point));
731 }
732 for (point = 0; point < 4; point++) {
733 data->pwm_auto_point_temp[nr][point] =
734 f71882fg_read8(data,
735 F71882FG_REG_POINT_TEMP
736 (nr, point));
737 }
738 } else {
739 data->pwm_auto_point_pwm[nr][1] =
740 f71882fg_read8(data,
741 F71882FG_REG_POINT_PWM
742 (nr, 1));
743 data->pwm_auto_point_pwm[nr][4] =
744 f71882fg_read8(data,
745 F71882FG_REG_POINT_PWM
746 (nr, 4));
747 data->pwm_auto_point_temp[nr][0] =
748 f71882fg_read8(data,
749 F71882FG_REG_POINT_TEMP
750 (nr, 0));
751 data->pwm_auto_point_temp[nr][3] =
752 f71882fg_read8(data,
753 F71882FG_REG_POINT_TEMP
754 (nr, 3));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100755 }
756 }
Hans de Goede45fb3662007-07-13 14:34:19 +0200757 data->last_limits = jiffies;
758 }
759
760 /* Update every second */
Mark M. Hoffman8afb1042007-08-21 23:10:46 -0400761 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Hans de Goede45fb3662007-07-13 14:34:19 +0200762 data->temp_status = f71882fg_read8(data,
763 F71882FG_REG_TEMP_STATUS);
764 data->temp_diode_open = f71882fg_read8(data,
765 F71882FG_REG_TEMP_DIODE_OPEN);
Hans de Goede7567a042009-01-07 16:37:28 +0100766 for (nr = 1; nr < 4; nr++)
Hans de Goede45fb3662007-07-13 14:34:19 +0200767 data->temp[nr] = f71882fg_read8(data,
768 F71882FG_REG_TEMP(nr));
769
770 data->fan_status = f71882fg_read8(data,
771 F71882FG_REG_FAN_STATUS);
Hans de Goede498be962009-01-07 16:37:28 +0100772 for (nr = 0; nr < nr_fans; nr++) {
Hans de Goede45fb3662007-07-13 14:34:19 +0200773 data->fan[nr] = f71882fg_read16(data,
774 F71882FG_REG_FAN(nr));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100775 data->fan_target[nr] =
776 f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
777 data->fan_full_speed[nr] =
778 f71882fg_read16(data,
779 F71882FG_REG_FAN_FULL_SPEED(nr));
780 data->pwm[nr] =
781 f71882fg_read8(data, F71882FG_REG_PWM(nr));
782 }
Hans de Goede45fb3662007-07-13 14:34:19 +0200783
Hans de Goede498be962009-01-07 16:37:28 +0100784 if (data->type == f71882fg)
785 data->in_status = f71882fg_read8(data,
Hans de Goede45fb3662007-07-13 14:34:19 +0200786 F71882FG_REG_IN_STATUS);
787 for (nr = 0; nr < 9; nr++)
788 data->in[nr] = f71882fg_read8(data,
789 F71882FG_REG_IN(nr));
790
791 data->last_updated = jiffies;
792 data->valid = 1;
793 }
794
795 mutex_unlock(&data->update_lock);
796
797 return data;
798}
799
800/* Sysfs Interface */
801static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
802 char *buf)
803{
804 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100805 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200806 int speed = fan_from_reg(data->fan[nr]);
807
808 if (speed == FAN_MIN_DETECT)
809 speed = 0;
810
811 return sprintf(buf, "%d\n", speed);
812}
813
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100814static ssize_t show_fan_full_speed(struct device *dev,
815 struct device_attribute *devattr, char *buf)
816{
817 struct f71882fg_data *data = f71882fg_update_device(dev);
818 int nr = to_sensor_dev_attr_2(devattr)->index;
819 int speed = fan_from_reg(data->fan_full_speed[nr]);
820 return sprintf(buf, "%d\n", speed);
821}
822
823static ssize_t store_fan_full_speed(struct device *dev,
824 struct device_attribute *devattr,
825 const char *buf, size_t count)
826{
827 struct f71882fg_data *data = dev_get_drvdata(dev);
828 int nr = to_sensor_dev_attr_2(devattr)->index;
829 long val = simple_strtol(buf, NULL, 10);
830
831 val = SENSORS_LIMIT(val, 23, 1500000);
832 val = fan_to_reg(val);
833
834 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +0100835 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +0100836 if (data->pwm_enable & (1 << (2 * nr)))
837 /* PWM mode */
838 count = -EINVAL;
839 else {
840 /* RPM mode */
841 f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
842 data->fan_full_speed[nr] = val;
843 }
844 mutex_unlock(&data->update_lock);
845
846 return count;
847}
848
Hans de Goede45fb3662007-07-13 14:34:19 +0200849static ssize_t show_fan_beep(struct device *dev, struct device_attribute
850 *devattr, char *buf)
851{
852 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100853 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200854
855 if (data->fan_beep & (1 << nr))
856 return sprintf(buf, "1\n");
857 else
858 return sprintf(buf, "0\n");
859}
860
861static ssize_t store_fan_beep(struct device *dev, struct device_attribute
862 *devattr, const char *buf, size_t count)
863{
864 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100865 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +0100866 unsigned long val = simple_strtoul(buf, NULL, 10);
Hans de Goede45fb3662007-07-13 14:34:19 +0200867
868 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +0100869 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
Hans de Goede45fb3662007-07-13 14:34:19 +0200870 if (val)
871 data->fan_beep |= 1 << nr;
872 else
873 data->fan_beep &= ~(1 << nr);
874
875 f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
876 mutex_unlock(&data->update_lock);
877
878 return count;
879}
880
881static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
882 *devattr, char *buf)
883{
884 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100885 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200886
887 if (data->fan_status & (1 << nr))
888 return sprintf(buf, "1\n");
889 else
890 return sprintf(buf, "0\n");
891}
892
893static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
894 char *buf)
895{
896 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100897 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200898
899 return sprintf(buf, "%d\n", data->in[nr] * 8);
900}
901
902static ssize_t show_in_max(struct device *dev, struct device_attribute
903 *devattr, char *buf)
904{
905 struct f71882fg_data *data = f71882fg_update_device(dev);
906
907 return sprintf(buf, "%d\n", data->in1_max * 8);
908}
909
910static ssize_t store_in_max(struct device *dev, struct device_attribute
911 *devattr, const char *buf, size_t count)
912{
913 struct f71882fg_data *data = dev_get_drvdata(dev);
Hans de Goedece0bfa52009-01-07 16:37:28 +0100914 long val = simple_strtol(buf, NULL, 10) / 8;
915 val = SENSORS_LIMIT(val, 0, 255);
Hans de Goede45fb3662007-07-13 14:34:19 +0200916
917 mutex_lock(&data->update_lock);
918 f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
919 data->in1_max = val;
920 mutex_unlock(&data->update_lock);
921
922 return count;
923}
924
925static ssize_t show_in_beep(struct device *dev, struct device_attribute
926 *devattr, char *buf)
927{
928 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100929 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200930
931 if (data->in_beep & (1 << nr))
932 return sprintf(buf, "1\n");
933 else
934 return sprintf(buf, "0\n");
935}
936
937static ssize_t store_in_beep(struct device *dev, struct device_attribute
938 *devattr, const char *buf, size_t count)
939{
940 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100941 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +0100942 unsigned long val = simple_strtoul(buf, NULL, 10);
Hans de Goede45fb3662007-07-13 14:34:19 +0200943
944 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +0100945 data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
Hans de Goede45fb3662007-07-13 14:34:19 +0200946 if (val)
947 data->in_beep |= 1 << nr;
948 else
949 data->in_beep &= ~(1 << nr);
950
951 f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
952 mutex_unlock(&data->update_lock);
953
954 return count;
955}
956
957static ssize_t show_in_alarm(struct device *dev, struct device_attribute
958 *devattr, char *buf)
959{
960 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100961 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200962
963 if (data->in_status & (1 << nr))
964 return sprintf(buf, "1\n");
965 else
966 return sprintf(buf, "0\n");
967}
968
969static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
970 char *buf)
971{
972 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100973 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200974
975 return sprintf(buf, "%d\n", data->temp[nr] * 1000);
976}
977
978static ssize_t show_temp_max(struct device *dev, struct device_attribute
979 *devattr, char *buf)
980{
981 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100982 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +0200983
984 return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
985}
986
987static ssize_t store_temp_max(struct device *dev, struct device_attribute
988 *devattr, const char *buf, size_t count)
989{
990 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +0100991 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +0100992 long val = simple_strtol(buf, NULL, 10) / 1000;
993 val = SENSORS_LIMIT(val, 0, 255);
Hans de Goede45fb3662007-07-13 14:34:19 +0200994
995 mutex_lock(&data->update_lock);
996 f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
997 data->temp_high[nr] = val;
998 mutex_unlock(&data->update_lock);
999
1000 return count;
1001}
1002
1003static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
1004 *devattr, char *buf)
1005{
1006 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001007 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001008 int temp_max_hyst;
Hans de Goede45fb3662007-07-13 14:34:19 +02001009
Hans de Goedece0bfa52009-01-07 16:37:28 +01001010 mutex_lock(&data->update_lock);
Hans de Goedebc274902009-01-07 16:37:29 +01001011 if (nr & 1)
1012 temp_max_hyst = data->temp_hyst[nr / 2] >> 4;
1013 else
1014 temp_max_hyst = data->temp_hyst[nr / 2] & 0x0f;
1015 temp_max_hyst = (data->temp_high[nr] - temp_max_hyst) * 1000;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001016 mutex_unlock(&data->update_lock);
1017
1018 return sprintf(buf, "%d\n", temp_max_hyst);
Hans de Goede45fb3662007-07-13 14:34:19 +02001019}
1020
1021static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
1022 *devattr, const char *buf, size_t count)
1023{
1024 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001025 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001026 long val = simple_strtol(buf, NULL, 10) / 1000;
Hans de Goede45fb3662007-07-13 14:34:19 +02001027 ssize_t ret = count;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001028 u8 reg;
Hans de Goede45fb3662007-07-13 14:34:19 +02001029
1030 mutex_lock(&data->update_lock);
1031
1032 /* convert abs to relative and check */
Hans de Goedece0bfa52009-01-07 16:37:28 +01001033 data->temp_high[nr] = f71882fg_read8(data, F71882FG_REG_TEMP_HIGH(nr));
1034 val = SENSORS_LIMIT(val, data->temp_high[nr] - 15,
1035 data->temp_high[nr]);
Hans de Goede45fb3662007-07-13 14:34:19 +02001036 val = data->temp_high[nr] - val;
Hans de Goede45fb3662007-07-13 14:34:19 +02001037
1038 /* convert value to register contents */
Hans de Goedebc274902009-01-07 16:37:29 +01001039 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST(nr / 2));
1040 if (nr & 1)
1041 reg = (reg & 0x0f) | (val << 4);
1042 else
1043 reg = (reg & 0xf0) | val;
1044 f71882fg_write8(data, F71882FG_REG_TEMP_HYST(nr / 2), reg);
1045 data->temp_hyst[nr / 2] = reg;
Hans de Goede45fb3662007-07-13 14:34:19 +02001046
Hans de Goede45fb3662007-07-13 14:34:19 +02001047 mutex_unlock(&data->update_lock);
1048 return ret;
1049}
1050
1051static ssize_t show_temp_crit(struct device *dev, struct device_attribute
1052 *devattr, char *buf)
1053{
1054 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001055 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001056
1057 return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
1058}
1059
1060static ssize_t store_temp_crit(struct device *dev, struct device_attribute
1061 *devattr, const char *buf, size_t count)
1062{
1063 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001064 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001065 long val = simple_strtol(buf, NULL, 10) / 1000;
1066 val = SENSORS_LIMIT(val, 0, 255);
Hans de Goede45fb3662007-07-13 14:34:19 +02001067
1068 mutex_lock(&data->update_lock);
1069 f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
1070 data->temp_ovt[nr] = val;
1071 mutex_unlock(&data->update_lock);
1072
1073 return count;
1074}
1075
1076static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
1077 *devattr, char *buf)
1078{
1079 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001080 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001081 int temp_crit_hyst;
Hans de Goede45fb3662007-07-13 14:34:19 +02001082
Hans de Goedece0bfa52009-01-07 16:37:28 +01001083 mutex_lock(&data->update_lock);
Hans de Goedebc274902009-01-07 16:37:29 +01001084 if (nr & 1)
1085 temp_crit_hyst = data->temp_hyst[nr / 2] >> 4;
1086 else
1087 temp_crit_hyst = data->temp_hyst[nr / 2] & 0x0f;
1088 temp_crit_hyst = (data->temp_ovt[nr] - temp_crit_hyst) * 1000;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001089 mutex_unlock(&data->update_lock);
1090
1091 return sprintf(buf, "%d\n", temp_crit_hyst);
Hans de Goede45fb3662007-07-13 14:34:19 +02001092}
1093
1094static ssize_t show_temp_type(struct device *dev, struct device_attribute
1095 *devattr, char *buf)
1096{
1097 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001098 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001099
1100 return sprintf(buf, "%d\n", data->temp_type[nr]);
1101}
1102
1103static ssize_t show_temp_beep(struct device *dev, struct device_attribute
1104 *devattr, char *buf)
1105{
1106 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001107 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001108
Hans de Goede7567a042009-01-07 16:37:28 +01001109 if (data->temp_beep & (1 << nr))
Hans de Goede45fb3662007-07-13 14:34:19 +02001110 return sprintf(buf, "1\n");
1111 else
1112 return sprintf(buf, "0\n");
1113}
1114
1115static ssize_t store_temp_beep(struct device *dev, struct device_attribute
1116 *devattr, const char *buf, size_t count)
1117{
1118 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001119 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001120 unsigned long val = simple_strtoul(buf, NULL, 10);
Hans de Goede45fb3662007-07-13 14:34:19 +02001121
1122 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001123 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
Hans de Goede45fb3662007-07-13 14:34:19 +02001124 if (val)
Hans de Goede7567a042009-01-07 16:37:28 +01001125 data->temp_beep |= 1 << nr;
Hans de Goede45fb3662007-07-13 14:34:19 +02001126 else
Hans de Goede7567a042009-01-07 16:37:28 +01001127 data->temp_beep &= ~(1 << nr);
Hans de Goede45fb3662007-07-13 14:34:19 +02001128
1129 f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
1130 mutex_unlock(&data->update_lock);
1131
1132 return count;
1133}
1134
1135static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
1136 *devattr, char *buf)
1137{
1138 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001139 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001140
Hans de Goede7567a042009-01-07 16:37:28 +01001141 if (data->temp_status & (1 << nr))
Hans de Goede45fb3662007-07-13 14:34:19 +02001142 return sprintf(buf, "1\n");
1143 else
1144 return sprintf(buf, "0\n");
1145}
1146
1147static ssize_t show_temp_fault(struct device *dev, struct device_attribute
1148 *devattr, char *buf)
1149{
1150 struct f71882fg_data *data = f71882fg_update_device(dev);
Mark van Doesburgbc37ae72009-01-07 16:37:27 +01001151 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goede45fb3662007-07-13 14:34:19 +02001152
Hans de Goede7567a042009-01-07 16:37:28 +01001153 if (data->temp_diode_open & (1 << nr))
Hans de Goede45fb3662007-07-13 14:34:19 +02001154 return sprintf(buf, "1\n");
1155 else
1156 return sprintf(buf, "0\n");
1157}
1158
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001159static ssize_t show_pwm(struct device *dev,
1160 struct device_attribute *devattr, char *buf)
1161{
1162 struct f71882fg_data *data = f71882fg_update_device(dev);
1163 int val, nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001164 mutex_lock(&data->update_lock);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001165 if (data->pwm_enable & (1 << (2 * nr)))
1166 /* PWM mode */
1167 val = data->pwm[nr];
1168 else {
1169 /* RPM mode */
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001170 val = 255 * fan_from_reg(data->fan_target[nr])
1171 / fan_from_reg(data->fan_full_speed[nr]);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001172 }
Hans de Goedece0bfa52009-01-07 16:37:28 +01001173 mutex_unlock(&data->update_lock);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001174 return sprintf(buf, "%d\n", val);
1175}
1176
1177static ssize_t store_pwm(struct device *dev,
1178 struct device_attribute *devattr, const char *buf,
1179 size_t count)
1180{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001181 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001182 int nr = to_sensor_dev_attr_2(devattr)->index;
1183 long val = simple_strtol(buf, NULL, 10);
1184 val = SENSORS_LIMIT(val, 0, 255);
1185
1186 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001187 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001188 if (data->pwm_enable & (1 << (2 * nr))) {
1189 /* PWM mode */
1190 f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1191 data->pwm[nr] = val;
1192 } else {
1193 /* RPM mode */
Hans de Goedece0bfa52009-01-07 16:37:28 +01001194 int target, full_speed;
1195 full_speed = f71882fg_read16(data,
1196 F71882FG_REG_FAN_FULL_SPEED(nr));
1197 target = fan_to_reg(val * fan_from_reg(full_speed) / 255);
1198 f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr), target);
1199 data->fan_target[nr] = target;
1200 data->fan_full_speed[nr] = full_speed;
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001201 }
1202 mutex_unlock(&data->update_lock);
1203
1204 return count;
1205}
1206
1207static ssize_t show_pwm_enable(struct device *dev,
1208 struct device_attribute *devattr, char *buf)
1209{
1210 int result;
1211 struct f71882fg_data *data = f71882fg_update_device(dev);
1212 int nr = to_sensor_dev_attr_2(devattr)->index;
1213
1214 if (data->pwm_enable & (2 << (2 * nr)))
1215 result = 1;
1216 else
1217 result = 2;
1218
1219 return sprintf(buf, "%d\n", result);
1220}
1221
1222static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1223 *devattr, const char *buf, size_t count)
1224{
1225 struct f71882fg_data *data = dev_get_drvdata(dev);
1226 int nr = to_sensor_dev_attr_2(devattr)->index;
1227 long val = simple_strtol(buf, NULL, 10);
1228 if (val < 1 || val > 2)
1229 return -EINVAL;
1230
1231 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001232 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001233 switch (val) {
1234 case 1:
1235 data->pwm_enable |= 2 << (2 * nr);
1236 break; /* Manual */
1237 case 2:
1238 data->pwm_enable &= ~(2 << (2 * nr));
1239 break; /* Temperature ctrl */
1240 }
Hans de Goede498be962009-01-07 16:37:28 +01001241 if (data->type == f71882fg) {
1242 switch (fan_mode[nr]) {
1243 case 1:
1244 data->pwm_enable |= 1 << (2 * nr);
1245 break; /* Duty cycle mode */
1246 case 2:
1247 data->pwm_enable &= ~(1 << (2 * nr));
1248 break; /* RPM mode */
1249 }
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001250 }
1251 f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
1252 mutex_unlock(&data->update_lock);
1253
1254 return count;
1255}
1256
1257static ssize_t show_pwm_auto_point_pwm(struct device *dev,
1258 struct device_attribute *devattr,
1259 char *buf)
1260{
1261 int result;
1262 struct f71882fg_data *data = f71882fg_update_device(dev);
1263 int pwm = to_sensor_dev_attr_2(devattr)->index;
1264 int point = to_sensor_dev_attr_2(devattr)->nr;
1265
Hans de Goedece0bfa52009-01-07 16:37:28 +01001266 mutex_lock(&data->update_lock);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001267 if (data->pwm_enable & (1 << (2 * pwm))) {
1268 /* PWM mode */
1269 result = data->pwm_auto_point_pwm[pwm][point];
1270 } else {
1271 /* RPM mode */
1272 result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
1273 }
Hans de Goedece0bfa52009-01-07 16:37:28 +01001274 mutex_unlock(&data->update_lock);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001275
1276 return sprintf(buf, "%d\n", result);
1277}
1278
1279static ssize_t store_pwm_auto_point_pwm(struct device *dev,
1280 struct device_attribute *devattr,
1281 const char *buf, size_t count)
1282{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001283 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001284 int pwm = to_sensor_dev_attr_2(devattr)->index;
1285 int point = to_sensor_dev_attr_2(devattr)->nr;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001286 long val = simple_strtol(buf, NULL, 10);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001287 val = SENSORS_LIMIT(val, 0, 255);
1288
1289 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001290 data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001291 if (data->pwm_enable & (1 << (2 * pwm))) {
1292 /* PWM mode */
1293 } else {
1294 /* RPM mode */
1295 if (val < 29) /* Prevent negative numbers */
1296 val = 255;
1297 else
1298 val = (255 - val) * 32 / val;
1299 }
1300 f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
1301 data->pwm_auto_point_pwm[pwm][point] = val;
1302 mutex_unlock(&data->update_lock);
1303
1304 return count;
1305}
1306
1307static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
1308 struct device_attribute *devattr,
1309 char *buf)
1310{
1311 int result = 0;
1312 struct f71882fg_data *data = f71882fg_update_device(dev);
1313 int nr = to_sensor_dev_attr_2(devattr)->index;
1314 int point = to_sensor_dev_attr_2(devattr)->nr;
1315
1316 mutex_lock(&data->update_lock);
Hans de Goedebc274902009-01-07 16:37:29 +01001317 if (nr & 1)
1318 result = data->pwm_auto_point_hyst[nr / 2] >> 4;
1319 else
1320 result = data->pwm_auto_point_hyst[nr / 2] & 0x0f;
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001321 result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
1322 mutex_unlock(&data->update_lock);
1323
1324 return sprintf(buf, "%d\n", result);
1325}
1326
1327static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
1328 struct device_attribute *devattr,
1329 const char *buf, size_t count)
1330{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001331 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001332 int nr = to_sensor_dev_attr_2(devattr)->index;
1333 int point = to_sensor_dev_attr_2(devattr)->nr;
1334 long val = simple_strtol(buf, NULL, 10) / 1000;
Hans de Goedebc274902009-01-07 16:37:29 +01001335 u8 reg;
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001336
1337 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001338 data->pwm_auto_point_temp[nr][point] =
1339 f71882fg_read8(data, F71882FG_REG_POINT_TEMP(nr, point));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001340 val = SENSORS_LIMIT(val, data->pwm_auto_point_temp[nr][point] - 15,
1341 data->pwm_auto_point_temp[nr][point]);
1342 val = data->pwm_auto_point_temp[nr][point] - val;
1343
Hans de Goedebc274902009-01-07 16:37:29 +01001344 reg = f71882fg_read8(data, F71882FG_REG_FAN_HYST(nr / 2));
1345 if (nr & 1)
1346 reg = (reg & 0x0f) | (val << 4);
1347 else
1348 reg = (reg & 0xf0) | val;
1349
1350 f71882fg_write8(data, F71882FG_REG_FAN_HYST(nr / 2), reg);
1351 data->pwm_auto_point_hyst[nr / 2] = reg;
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001352 mutex_unlock(&data->update_lock);
1353
1354 return count;
1355}
1356
1357static ssize_t show_pwm_interpolate(struct device *dev,
1358 struct device_attribute *devattr, char *buf)
1359{
1360 int result;
1361 struct f71882fg_data *data = f71882fg_update_device(dev);
1362 int nr = to_sensor_dev_attr_2(devattr)->index;
1363
1364 result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
1365
1366 return sprintf(buf, "%d\n", result);
1367}
1368
1369static ssize_t store_pwm_interpolate(struct device *dev,
1370 struct device_attribute *devattr,
1371 const char *buf, size_t count)
1372{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001373 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001374 int nr = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedece0bfa52009-01-07 16:37:28 +01001375 unsigned long val = simple_strtoul(buf, NULL, 10);
1376
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001377 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001378 data->pwm_auto_point_mapping[nr] =
1379 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001380 if (val)
1381 val = data->pwm_auto_point_mapping[nr] | (1 << 4);
1382 else
1383 val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
1384 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1385 data->pwm_auto_point_mapping[nr] = val;
1386 mutex_unlock(&data->update_lock);
1387
1388 return count;
1389}
1390
1391static ssize_t show_pwm_auto_point_channel(struct device *dev,
1392 struct device_attribute *devattr,
1393 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 = 1 << ((data->pwm_auto_point_mapping[nr] & 3) - 1);
1400
1401 return sprintf(buf, "%d\n", result);
1402}
1403
1404static ssize_t store_pwm_auto_point_channel(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;
1410 long val = simple_strtol(buf, NULL, 10);
1411 switch (val) {
1412 case 1:
1413 val = 1;
1414 break;
1415 case 2:
1416 val = 2;
1417 break;
1418 case 4:
1419 val = 3;
1420 break;
1421 default:
1422 return -EINVAL;
1423 }
1424 mutex_lock(&data->update_lock);
Hans de Goedece0bfa52009-01-07 16:37:28 +01001425 data->pwm_auto_point_mapping[nr] =
1426 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001427 val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
1428 f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1429 data->pwm_auto_point_mapping[nr] = val;
1430 mutex_unlock(&data->update_lock);
1431
1432 return count;
1433}
1434
1435static ssize_t show_pwm_auto_point_temp(struct device *dev,
1436 struct device_attribute *devattr,
1437 char *buf)
1438{
1439 int result;
1440 struct f71882fg_data *data = f71882fg_update_device(dev);
1441 int pwm = to_sensor_dev_attr_2(devattr)->index;
1442 int point = to_sensor_dev_attr_2(devattr)->nr;
1443
1444 result = data->pwm_auto_point_temp[pwm][point];
1445 return sprintf(buf, "%d\n", 1000 * result);
1446}
1447
1448static ssize_t store_pwm_auto_point_temp(struct device *dev,
1449 struct device_attribute *devattr,
1450 const char *buf, size_t count)
1451{
Hans de Goedece0bfa52009-01-07 16:37:28 +01001452 struct f71882fg_data *data = dev_get_drvdata(dev);
Mark van Doesburg9ab796e2009-01-07 16:37:27 +01001453 int pwm = to_sensor_dev_attr_2(devattr)->index;
1454 int point = to_sensor_dev_attr_2(devattr)->nr;
1455 long val = simple_strtol(buf, NULL, 10) / 1000;
1456 val = SENSORS_LIMIT(val, 0, 255);
1457
1458 mutex_lock(&data->update_lock);
1459 f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
1460 data->pwm_auto_point_temp[pwm][point] = val;
1461 mutex_unlock(&data->update_lock);
1462
1463 return count;
1464}
1465
Hans de Goede45fb3662007-07-13 14:34:19 +02001466static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
1467 char *buf)
1468{
Hans de Goede498be962009-01-07 16:37:28 +01001469 struct f71882fg_data *data = dev_get_drvdata(dev);
1470 return sprintf(buf, "%s\n", f71882fg_names[data->type]);
Hans de Goede45fb3662007-07-13 14:34:19 +02001471}
1472
Hans de Goedec13548c2009-01-07 16:37:27 +01001473static int __devinit f71882fg_create_sysfs_files(struct platform_device *pdev,
1474 struct sensor_device_attribute_2 *attr, int count)
1475{
1476 int err, i;
Hans de Goede45fb3662007-07-13 14:34:19 +02001477
Hans de Goedec13548c2009-01-07 16:37:27 +01001478 for (i = 0; i < count; i++) {
1479 err = device_create_file(&pdev->dev, &attr[i].dev_attr);
1480 if (err)
1481 return err;
1482 }
1483 return 0;
1484}
1485
1486static int __devinit f71882fg_probe(struct platform_device *pdev)
Hans de Goede45fb3662007-07-13 14:34:19 +02001487{
1488 struct f71882fg_data *data;
Hans de Goede498be962009-01-07 16:37:28 +01001489 struct f71882fg_sio_data *sio_data = pdev->dev.platform_data;
Hans de Goedec13548c2009-01-07 16:37:27 +01001490 int err;
Hans de Goede45fb3662007-07-13 14:34:19 +02001491 u8 start_reg;
1492
Hans de Goedec13548c2009-01-07 16:37:27 +01001493 data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL);
1494 if (!data)
Hans de Goede45fb3662007-07-13 14:34:19 +02001495 return -ENOMEM;
1496
1497 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
Hans de Goede498be962009-01-07 16:37:28 +01001498 data->type = sio_data->type;
Hans de Goede45fb3662007-07-13 14:34:19 +02001499 mutex_init(&data->update_lock);
1500 platform_set_drvdata(pdev, data);
1501
Hans de Goede3cc74752009-01-07 16:37:28 +01001502 start_reg = f71882fg_read8(data, F71882FG_REG_START);
Hans de Goede12d66e82009-01-07 16:37:29 +01001503 if (start_reg & 0x04) {
1504 dev_warn(&pdev->dev, "Hardware monitor is powered down\n");
1505 err = -ENODEV;
1506 goto exit_free;
1507 }
Hans de Goede3cc74752009-01-07 16:37:28 +01001508 if (!(start_reg & 0x03)) {
1509 dev_warn(&pdev->dev, "Hardware monitoring not activated\n");
1510 err = -ENODEV;
1511 goto exit_free;
1512 }
1513
1514 /* If it is a 71862 and the fan / pwm part is enabled sanity check
1515 the pwm settings */
1516 if (data->type == f71862fg && (start_reg & 0x02)) {
1517 u8 reg = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1518 if ((reg & 0x15) != 0x15) {
1519 dev_err(&pdev->dev,
1520 "Invalid (reserved) pwm settings: 0x%02x\n",
1521 (unsigned int)reg);
1522 err = -ENODEV;
1523 goto exit_free;
1524 }
1525 }
1526
Hans de Goede45fb3662007-07-13 14:34:19 +02001527 /* Register sysfs interface files */
Hans de Goedec13548c2009-01-07 16:37:27 +01001528 err = device_create_file(&pdev->dev, &dev_attr_name);
1529 if (err)
1530 goto exit_unregister_sysfs;
1531
Hans de Goedec13548c2009-01-07 16:37:27 +01001532 if (start_reg & 0x01) {
Hans de Goede498be962009-01-07 16:37:28 +01001533 err = f71882fg_create_sysfs_files(pdev, f718x2fg_in_temp_attr,
1534 ARRAY_SIZE(f718x2fg_in_temp_attr));
Hans de Goede45fb3662007-07-13 14:34:19 +02001535 if (err)
1536 goto exit_unregister_sysfs;
Hans de Goede498be962009-01-07 16:37:28 +01001537
1538 if (data->type == f71882fg) {
1539 err = f71882fg_create_sysfs_files(pdev,
1540 f71882fg_in_temp_attr,
1541 ARRAY_SIZE(f71882fg_in_temp_attr));
1542 if (err)
1543 goto exit_unregister_sysfs;
1544 }
Hans de Goede45fb3662007-07-13 14:34:19 +02001545 }
1546
Hans de Goede45fb3662007-07-13 14:34:19 +02001547 if (start_reg & 0x02) {
Hans de Goede498be962009-01-07 16:37:28 +01001548 err = f71882fg_create_sysfs_files(pdev, f718x2fg_fan_attr,
1549 ARRAY_SIZE(f718x2fg_fan_attr));
1550 if (err)
1551 goto exit_unregister_sysfs;
1552
1553 if (data->type == f71862fg) {
1554 err = f71882fg_create_sysfs_files(pdev,
1555 f71862fg_fan_attr,
1556 ARRAY_SIZE(f71862fg_fan_attr));
1557 } else {
1558 err = f71882fg_create_sysfs_files(pdev,
1559 f71882fg_fan_attr,
Hans de Goedec13548c2009-01-07 16:37:27 +01001560 ARRAY_SIZE(f71882fg_fan_attr));
Hans de Goede498be962009-01-07 16:37:28 +01001561 }
Hans de Goedec13548c2009-01-07 16:37:27 +01001562 if (err)
1563 goto exit_unregister_sysfs;
Hans de Goede45fb3662007-07-13 14:34:19 +02001564 }
1565
Tony Jones1beeffe2007-08-20 13:46:20 -07001566 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1567 if (IS_ERR(data->hwmon_dev)) {
1568 err = PTR_ERR(data->hwmon_dev);
Hans de Goedec13548c2009-01-07 16:37:27 +01001569 data->hwmon_dev = NULL;
Hans de Goede45fb3662007-07-13 14:34:19 +02001570 goto exit_unregister_sysfs;
1571 }
1572
1573 return 0;
1574
1575exit_unregister_sysfs:
Hans de Goedec13548c2009-01-07 16:37:27 +01001576 f71882fg_remove(pdev); /* Will unregister the sysfs files for us */
Hans de Goede3cc74752009-01-07 16:37:28 +01001577 return err; /* f71882fg_remove() also frees our data */
1578exit_free:
1579 kfree(data);
Hans de Goede45fb3662007-07-13 14:34:19 +02001580 return err;
1581}
1582
Hans de Goedec13548c2009-01-07 16:37:27 +01001583static int f71882fg_remove(struct platform_device *pdev)
Hans de Goede45fb3662007-07-13 14:34:19 +02001584{
1585 int i;
1586 struct f71882fg_data *data = platform_get_drvdata(pdev);
1587
1588 platform_set_drvdata(pdev, NULL);
Hans de Goedec13548c2009-01-07 16:37:27 +01001589 if (data->hwmon_dev)
1590 hwmon_device_unregister(data->hwmon_dev);
Hans de Goede45fb3662007-07-13 14:34:19 +02001591
Hans de Goedec13548c2009-01-07 16:37:27 +01001592 device_remove_file(&pdev->dev, &dev_attr_name);
Hans de Goede45fb3662007-07-13 14:34:19 +02001593
Hans de Goede498be962009-01-07 16:37:28 +01001594 for (i = 0; i < ARRAY_SIZE(f718x2fg_in_temp_attr); i++)
1595 device_remove_file(&pdev->dev,
1596 &f718x2fg_in_temp_attr[i].dev_attr);
1597
Hans de Goede45fb3662007-07-13 14:34:19 +02001598 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
1599 device_remove_file(&pdev->dev,
1600 &f71882fg_in_temp_attr[i].dev_attr);
1601
Hans de Goede498be962009-01-07 16:37:28 +01001602 for (i = 0; i < ARRAY_SIZE(f718x2fg_fan_attr); i++)
1603 device_remove_file(&pdev->dev, &f718x2fg_fan_attr[i].dev_attr);
1604
1605 for (i = 0; i < ARRAY_SIZE(f71862fg_fan_attr); i++)
1606 device_remove_file(&pdev->dev, &f71862fg_fan_attr[i].dev_attr);
1607
Hans de Goede45fb3662007-07-13 14:34:19 +02001608 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
1609 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
1610
1611 kfree(data);
1612
1613 return 0;
1614}
1615
Hans de Goede498be962009-01-07 16:37:28 +01001616static int __init f71882fg_find(int sioaddr, unsigned short *address,
1617 struct f71882fg_sio_data *sio_data)
Hans de Goede45fb3662007-07-13 14:34:19 +02001618{
1619 int err = -ENODEV;
1620 u16 devid;
Hans de Goede45fb3662007-07-13 14:34:19 +02001621
1622 superio_enter(sioaddr);
1623
1624 devid = superio_inw(sioaddr, SIO_REG_MANID);
1625 if (devid != SIO_FINTEK_ID) {
1626 printk(KERN_INFO DRVNAME ": Not a Fintek device\n");
1627 goto exit;
1628 }
1629
Jean Delvare67b671b2007-12-06 23:13:42 +01001630 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
Hans de Goede498be962009-01-07 16:37:28 +01001631 switch (devid) {
1632 case SIO_F71862_ID:
1633 sio_data->type = f71862fg;
1634 break;
1635 case SIO_F71882_ID:
1636 sio_data->type = f71882fg;
1637 break;
1638 default:
Hans de Goede45fb3662007-07-13 14:34:19 +02001639 printk(KERN_INFO DRVNAME ": Unsupported Fintek device\n");
1640 goto exit;
1641 }
1642
1643 superio_select(sioaddr, SIO_F71882FG_LD_HWM);
Mark M. Hoffman8afb1042007-08-21 23:10:46 -04001644 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
Hans de Goede45fb3662007-07-13 14:34:19 +02001645 printk(KERN_WARNING DRVNAME ": Device not activated\n");
1646 goto exit;
1647 }
1648
1649 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1650 if (*address == 0)
1651 {
1652 printk(KERN_WARNING DRVNAME ": Base address not set\n");
1653 goto exit;
1654 }
1655 *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
1656
Hans de Goede45fb3662007-07-13 14:34:19 +02001657 err = 0;
Hans de Goede498be962009-01-07 16:37:28 +01001658 printk(KERN_INFO DRVNAME ": Found %s chip at %#x, revision %d\n",
1659 f71882fg_names[sio_data->type], (unsigned int)*address,
Hans de Goede45fb3662007-07-13 14:34:19 +02001660 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
1661exit:
1662 superio_exit(sioaddr);
1663 return err;
1664}
1665
Hans de Goede498be962009-01-07 16:37:28 +01001666static int __init f71882fg_device_add(unsigned short address,
1667 const struct f71882fg_sio_data *sio_data)
Hans de Goede45fb3662007-07-13 14:34:19 +02001668{
1669 struct resource res = {
1670 .start = address,
1671 .end = address + REGION_LENGTH - 1,
1672 .flags = IORESOURCE_IO,
1673 };
1674 int err;
1675
1676 f71882fg_pdev = platform_device_alloc(DRVNAME, address);
Mark M. Hoffman8afb1042007-08-21 23:10:46 -04001677 if (!f71882fg_pdev)
Hans de Goede45fb3662007-07-13 14:34:19 +02001678 return -ENOMEM;
1679
1680 res.name = f71882fg_pdev->name;
1681 err = platform_device_add_resources(f71882fg_pdev, &res, 1);
Mark M. Hoffman8afb1042007-08-21 23:10:46 -04001682 if (err) {
Hans de Goede45fb3662007-07-13 14:34:19 +02001683 printk(KERN_ERR DRVNAME ": Device resource addition failed\n");
1684 goto exit_device_put;
1685 }
1686
Hans de Goede498be962009-01-07 16:37:28 +01001687 err = platform_device_add_data(f71882fg_pdev, sio_data,
1688 sizeof(struct f71882fg_sio_data));
1689 if (err) {
1690 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1691 goto exit_device_put;
1692 }
1693
Hans de Goede45fb3662007-07-13 14:34:19 +02001694 err = platform_device_add(f71882fg_pdev);
Mark M. Hoffman8afb1042007-08-21 23:10:46 -04001695 if (err) {
Hans de Goede45fb3662007-07-13 14:34:19 +02001696 printk(KERN_ERR DRVNAME ": Device addition failed\n");
1697 goto exit_device_put;
1698 }
1699
1700 return 0;
1701
1702exit_device_put:
1703 platform_device_put(f71882fg_pdev);
1704
1705 return err;
1706}
1707
1708static int __init f71882fg_init(void)
1709{
1710 int err = -ENODEV;
1711 unsigned short address;
Hans de Goede498be962009-01-07 16:37:28 +01001712 struct f71882fg_sio_data sio_data;
Hans de Goede45fb3662007-07-13 14:34:19 +02001713
Hans de Goede498be962009-01-07 16:37:28 +01001714 memset(&sio_data, 0, sizeof(sio_data));
1715
1716 if (f71882fg_find(0x2e, &address, &sio_data) &&
1717 f71882fg_find(0x4e, &address, &sio_data))
Hans de Goede45fb3662007-07-13 14:34:19 +02001718 goto exit;
1719
Hans de Goedec13548c2009-01-07 16:37:27 +01001720 err = platform_driver_register(&f71882fg_driver);
1721 if (err)
Hans de Goede45fb3662007-07-13 14:34:19 +02001722 goto exit;
1723
Hans de Goede498be962009-01-07 16:37:28 +01001724 err = f71882fg_device_add(address, &sio_data);
Hans de Goedec13548c2009-01-07 16:37:27 +01001725 if (err)
Hans de Goede45fb3662007-07-13 14:34:19 +02001726 goto exit_driver;
1727
1728 return 0;
1729
1730exit_driver:
1731 platform_driver_unregister(&f71882fg_driver);
1732exit:
1733 return err;
1734}
1735
1736static void __exit f71882fg_exit(void)
1737{
1738 platform_device_unregister(f71882fg_pdev);
1739 platform_driver_unregister(&f71882fg_driver);
1740}
1741
1742MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
Hans de Goedec13548c2009-01-07 16:37:27 +01001743MODULE_AUTHOR("Hans Edgington, Hans de Goede (hdegoede@redhat.com)");
Hans de Goede45fb3662007-07-13 14:34:19 +02001744MODULE_LICENSE("GPL");
1745
1746module_init(f71882fg_init);
1747module_exit(f71882fg_exit);