Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / f71882fg.c
blobcbeb4984b5c7177e2c8fc90c8f22afe85cf30e64
1 /***************************************************************************
2 * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
3 * Copyright (C) 2007 by Hans de Goede <j.w.r.degoede@hhs.nl> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #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>
30 #include <asm/io.h>
32 #define DRVNAME "f71882fg"
34 #define SIO_F71882FG_LD_HWM 0x04 /* Hardware monitor logical device*/
35 #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
36 #define SIO_LOCK_KEY 0xAA /* Key to diasble Super-I/O */
38 #define SIO_REG_LDSEL 0x07 /* Logical device select */
39 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
40 #define SIO_REG_DEVREV 0x22 /* Device revision */
41 #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
42 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
43 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
45 #define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
46 #define SIO_F71882_ID 0x0541 /* Chipset ID */
48 #define REGION_LENGTH 8
49 #define ADDR_REG_OFFSET 5
50 #define DATA_REG_OFFSET 6
52 #define F71882FG_REG_PECI 0x0A
54 #define F71882FG_REG_IN_STATUS 0x12
55 #define F71882FG_REG_IN_BEEP 0x13
56 #define F71882FG_REG_IN(nr) (0x20 + (nr))
57 #define F71882FG_REG_IN1_HIGH 0x32
59 #define F71882FG_REG_FAN(nr) (0xA0 + (16 * (nr)))
60 #define F71882FG_REG_FAN_STATUS 0x92
61 #define F71882FG_REG_FAN_BEEP 0x93
63 #define F71882FG_REG_TEMP(nr) (0x72 + 2 * (nr))
64 #define F71882FG_REG_TEMP_OVT(nr) (0x82 + 2 * (nr))
65 #define F71882FG_REG_TEMP_HIGH(nr) (0x83 + 2 * (nr))
66 #define F71882FG_REG_TEMP_STATUS 0x62
67 #define F71882FG_REG_TEMP_BEEP 0x63
68 #define F71882FG_REG_TEMP_HYST1 0x6C
69 #define F71882FG_REG_TEMP_HYST23 0x6D
70 #define F71882FG_REG_TEMP_TYPE 0x6B
71 #define F71882FG_REG_TEMP_DIODE_OPEN 0x6F
73 #define F71882FG_REG_START 0x01
75 #define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */
77 static unsigned short force_id;
78 module_param(force_id, ushort, 0);
79 MODULE_PARM_DESC(force_id, "Override the detected device ID");
81 static struct platform_device *f71882fg_pdev = NULL;
83 /* Super-I/O Function prototypes */
84 static inline int superio_inb(int base, int reg);
85 static inline int superio_inw(int base, int reg);
86 static inline void superio_enter(int base);
87 static inline void superio_select(int base, int ld);
88 static inline void superio_exit(int base);
90 static inline u16 fan_from_reg ( u16 reg );
92 struct f71882fg_data {
93 unsigned short addr;
94 struct device *hwmon_dev;
96 struct mutex update_lock;
97 char valid; /* !=0 if following fields are valid */
98 unsigned long last_updated; /* In jiffies */
99 unsigned long last_limits; /* In jiffies */
101 /* Register Values */
102 u8 in[9];
103 u8 in1_max;
104 u8 in_status;
105 u8 in_beep;
106 u16 fan[4];
107 u8 fan_status;
108 u8 fan_beep;
109 u8 temp[3];
110 u8 temp_ovt[3];
111 u8 temp_high[3];
112 u8 temp_hyst[3];
113 u8 temp_type[3];
114 u8 temp_status;
115 u8 temp_beep;
116 u8 temp_diode_open;
119 static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg);
120 static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg);
121 static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val);
123 /* Sysfs in*/
124 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
125 char *buf);
126 static ssize_t show_in_max(struct device *dev, struct device_attribute
127 *devattr, char *buf);
128 static ssize_t store_in_max(struct device *dev, struct device_attribute
129 *devattr, const char *buf, size_t count);
130 static ssize_t show_in_beep(struct device *dev, struct device_attribute
131 *devattr, char *buf);
132 static ssize_t store_in_beep(struct device *dev, struct device_attribute
133 *devattr, const char *buf, size_t count);
134 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
135 *devattr, char *buf);
136 /* Sysfs Fan */
137 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
138 char *buf);
139 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
140 *devattr, char *buf);
141 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
142 *devattr, const char *buf, size_t count);
143 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
144 *devattr, char *buf);
145 /* Sysfs Temp */
146 static ssize_t show_temp(struct device *dev, struct device_attribute
147 *devattr, char *buf);
148 static ssize_t show_temp_max(struct device *dev, struct device_attribute
149 *devattr, char *buf);
150 static ssize_t store_temp_max(struct device *dev, struct device_attribute
151 *devattr, const char *buf, size_t count);
152 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
153 *devattr, char *buf);
154 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
155 *devattr, const char *buf, size_t count);
156 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
157 *devattr, char *buf);
158 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
159 *devattr, const char *buf, size_t count);
160 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
161 *devattr, char *buf);
162 static ssize_t show_temp_type(struct device *dev, struct device_attribute
163 *devattr, char *buf);
164 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
165 *devattr, char *buf);
166 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
167 *devattr, const char *buf, size_t count);
168 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
169 *devattr, char *buf);
170 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
171 *devattr, char *buf);
172 /* Sysfs misc */
173 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
174 char *buf);
176 static int __devinit f71882fg_probe(struct platform_device * pdev);
177 static int __devexit f71882fg_remove(struct platform_device *pdev);
178 static int __init f71882fg_init(void);
179 static int __init f71882fg_find(int sioaddr, unsigned short *address);
180 static int __init f71882fg_device_add(unsigned short address);
181 static void __exit f71882fg_exit(void);
183 static struct platform_driver f71882fg_driver = {
184 .driver = {
185 .owner = THIS_MODULE,
186 .name = DRVNAME,
188 .probe = f71882fg_probe,
189 .remove = __devexit_p(f71882fg_remove),
192 static struct device_attribute f71882fg_dev_attr[] =
194 __ATTR( name, S_IRUGO, show_name, NULL ),
197 static struct sensor_device_attribute f71882fg_in_temp_attr[] =
199 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
200 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
201 SENSOR_ATTR(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max, 1),
202 SENSOR_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep, 1),
203 SENSOR_ATTR(in1_alarm, S_IRUGO, show_in_alarm, NULL, 1),
204 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
205 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
206 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
207 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
208 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
209 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
210 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
211 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
212 SENSOR_ATTR(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
213 store_temp_max, 0),
214 SENSOR_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
215 store_temp_max_hyst, 0),
216 SENSOR_ATTR(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
217 store_temp_crit, 0),
218 SENSOR_ATTR(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 0),
219 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
220 SENSOR_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_temp_beep,
221 store_temp_beep, 0),
222 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0),
223 SENSOR_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0),
224 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
225 SENSOR_ATTR(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
226 store_temp_max, 1),
227 SENSOR_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
228 store_temp_max_hyst, 1),
229 SENSOR_ATTR(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
230 store_temp_crit, 1),
231 SENSOR_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 1),
232 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
233 SENSOR_ATTR(temp2_beep, S_IRUGO|S_IWUSR, show_temp_beep,
234 store_temp_beep, 1),
235 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 1),
236 SENSOR_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1),
237 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
238 SENSOR_ATTR(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
239 store_temp_max, 2),
240 SENSOR_ATTR(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
241 store_temp_max_hyst, 2),
242 SENSOR_ATTR(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
243 store_temp_crit, 2),
244 SENSOR_ATTR(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 2),
245 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
246 SENSOR_ATTR(temp3_beep, S_IRUGO|S_IWUSR, show_temp_beep,
247 store_temp_beep, 2),
248 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 2),
249 SENSOR_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2)
252 static struct sensor_device_attribute f71882fg_fan_attr[] =
254 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
255 SENSOR_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
256 store_fan_beep, 0),
257 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0),
258 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
259 SENSOR_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
260 store_fan_beep, 1),
261 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 1),
262 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
263 SENSOR_ATTR(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
264 store_fan_beep, 2),
265 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 2),
266 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
267 SENSOR_ATTR(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
268 store_fan_beep, 3),
269 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 3)
273 /* Super I/O functions */
274 static inline int superio_inb(int base, int reg)
276 outb(reg, base);
277 return inb(base + 1);
280 static int superio_inw(int base, int reg)
282 int val;
283 outb(reg++, base);
284 val = inb(base + 1) << 8;
285 outb(reg, base);
286 val |= inb(base + 1);
287 return val;
290 static inline void superio_enter(int base)
292 /* according to the datasheet the key must be send twice! */
293 outb( SIO_UNLOCK_KEY, base);
294 outb( SIO_UNLOCK_KEY, base);
297 static inline void superio_select( int base, int ld)
299 outb(SIO_REG_LDSEL, base);
300 outb(ld, base + 1);
303 static inline void superio_exit(int base)
305 outb(SIO_LOCK_KEY, base);
308 static inline u16 fan_from_reg(u16 reg)
310 return reg ? (1500000 / reg) : 0;
313 static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
315 u8 val;
317 outb(reg, data->addr + ADDR_REG_OFFSET);
318 val = inb(data->addr + DATA_REG_OFFSET);
320 return val;
323 static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
325 u16 val;
327 outb(reg++, data->addr + ADDR_REG_OFFSET);
328 val = inb(data->addr + DATA_REG_OFFSET) << 8;
329 outb(reg, data->addr + ADDR_REG_OFFSET);
330 val |= inb(data->addr + DATA_REG_OFFSET);
332 return val;
335 static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
337 outb(reg, data->addr + ADDR_REG_OFFSET);
338 outb(val, data->addr + DATA_REG_OFFSET);
341 static struct f71882fg_data *f71882fg_update_device(struct device * dev)
343 struct f71882fg_data *data = dev_get_drvdata(dev);
344 int nr, reg, reg2;
346 mutex_lock(&data->update_lock);
348 /* Update once every 60 seconds */
349 if ( time_after(jiffies, data->last_limits + 60 * HZ ) ||
350 !data->valid) {
351 data->in1_max = f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
352 data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
354 /* Get High & boundary temps*/
355 for (nr = 0; nr < 3; nr++) {
356 data->temp_ovt[nr] = f71882fg_read8(data,
357 F71882FG_REG_TEMP_OVT(nr));
358 data->temp_high[nr] = f71882fg_read8(data,
359 F71882FG_REG_TEMP_HIGH(nr));
362 /* Have to hardcode hyst*/
363 data->temp_hyst[0] = f71882fg_read8(data,
364 F71882FG_REG_TEMP_HYST1) >> 4;
365 /* Hyst temps 2 & 3 stored in same register */
366 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST23);
367 data->temp_hyst[1] = reg & 0x0F;
368 data->temp_hyst[2] = reg >> 4;
370 /* Have to hardcode type, because temp1 is special */
371 reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
372 reg2 = f71882fg_read8(data, F71882FG_REG_PECI);
373 if ((reg2 & 0x03) == 0x01)
374 data->temp_type[0] = 6 /* PECI */;
375 else if ((reg2 & 0x03) == 0x02)
376 data->temp_type[0] = 5 /* AMDSI */;
377 else
378 data->temp_type[0] = (reg & 0x02) ? 2 : 4;
380 data->temp_type[1] = (reg & 0x04) ? 2 : 4;
381 data->temp_type[2] = (reg & 0x08) ? 2 : 4;
383 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
385 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
387 data->last_limits = jiffies;
390 /* Update every second */
391 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
392 data->temp_status = f71882fg_read8(data,
393 F71882FG_REG_TEMP_STATUS);
394 data->temp_diode_open = f71882fg_read8(data,
395 F71882FG_REG_TEMP_DIODE_OPEN);
396 for (nr = 0; nr < 3; nr++)
397 data->temp[nr] = f71882fg_read8(data,
398 F71882FG_REG_TEMP(nr));
400 data->fan_status = f71882fg_read8(data,
401 F71882FG_REG_FAN_STATUS);
402 for (nr = 0; nr < 4; nr++)
403 data->fan[nr] = f71882fg_read16(data,
404 F71882FG_REG_FAN(nr));
406 data->in_status = f71882fg_read8(data,
407 F71882FG_REG_IN_STATUS);
408 for (nr = 0; nr < 9; nr++)
409 data->in[nr] = f71882fg_read8(data,
410 F71882FG_REG_IN(nr));
412 data->last_updated = jiffies;
413 data->valid = 1;
416 mutex_unlock(&data->update_lock);
418 return data;
421 /* Sysfs Interface */
422 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
423 char *buf)
425 struct f71882fg_data *data = f71882fg_update_device(dev);
426 int nr = to_sensor_dev_attr(devattr)->index;
427 int speed = fan_from_reg(data->fan[nr]);
429 if (speed == FAN_MIN_DETECT)
430 speed = 0;
432 return sprintf(buf, "%d\n", speed);
435 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
436 *devattr, char *buf)
438 struct f71882fg_data *data = f71882fg_update_device(dev);
439 int nr = to_sensor_dev_attr(devattr)->index;
441 if (data->fan_beep & (1 << nr))
442 return sprintf(buf, "1\n");
443 else
444 return sprintf(buf, "0\n");
447 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
448 *devattr, const char *buf, size_t count)
450 struct f71882fg_data *data = dev_get_drvdata(dev);
451 int nr = to_sensor_dev_attr(devattr)->index;
452 int val = simple_strtoul(buf, NULL, 10);
454 mutex_lock(&data->update_lock);
455 if (val)
456 data->fan_beep |= 1 << nr;
457 else
458 data->fan_beep &= ~(1 << nr);
460 f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
461 mutex_unlock(&data->update_lock);
463 return count;
466 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
467 *devattr, char *buf)
469 struct f71882fg_data *data = f71882fg_update_device(dev);
470 int nr = to_sensor_dev_attr(devattr)->index;
472 if (data->fan_status & (1 << nr))
473 return sprintf(buf, "1\n");
474 else
475 return sprintf(buf, "0\n");
478 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
479 char *buf)
481 struct f71882fg_data *data = f71882fg_update_device(dev);
482 int nr = to_sensor_dev_attr(devattr)->index;
484 return sprintf(buf, "%d\n", data->in[nr] * 8);
487 static ssize_t show_in_max(struct device *dev, struct device_attribute
488 *devattr, char *buf)
490 struct f71882fg_data *data = f71882fg_update_device(dev);
492 return sprintf(buf, "%d\n", data->in1_max * 8);
495 static ssize_t store_in_max(struct device *dev, struct device_attribute
496 *devattr, const char *buf, size_t count)
498 struct f71882fg_data *data = dev_get_drvdata(dev);
499 int val = simple_strtoul(buf, NULL, 10) / 8;
501 if (val > 255)
502 val = 255;
504 mutex_lock(&data->update_lock);
505 f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
506 data->in1_max = val;
507 mutex_unlock(&data->update_lock);
509 return count;
512 static ssize_t show_in_beep(struct device *dev, struct device_attribute
513 *devattr, char *buf)
515 struct f71882fg_data *data = f71882fg_update_device(dev);
516 int nr = to_sensor_dev_attr(devattr)->index;
518 if (data->in_beep & (1 << nr))
519 return sprintf(buf, "1\n");
520 else
521 return sprintf(buf, "0\n");
524 static ssize_t store_in_beep(struct device *dev, struct device_attribute
525 *devattr, const char *buf, size_t count)
527 struct f71882fg_data *data = dev_get_drvdata(dev);
528 int nr = to_sensor_dev_attr(devattr)->index;
529 int val = simple_strtoul(buf, NULL, 10);
531 mutex_lock(&data->update_lock);
532 if (val)
533 data->in_beep |= 1 << nr;
534 else
535 data->in_beep &= ~(1 << nr);
537 f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
538 mutex_unlock(&data->update_lock);
540 return count;
543 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
544 *devattr, char *buf)
546 struct f71882fg_data *data = f71882fg_update_device(dev);
547 int nr = to_sensor_dev_attr(devattr)->index;
549 if (data->in_status & (1 << nr))
550 return sprintf(buf, "1\n");
551 else
552 return sprintf(buf, "0\n");
555 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
556 char *buf)
558 struct f71882fg_data *data = f71882fg_update_device(dev);
559 int nr = to_sensor_dev_attr(devattr)->index;
561 return sprintf(buf, "%d\n", data->temp[nr] * 1000);
564 static ssize_t show_temp_max(struct device *dev, struct device_attribute
565 *devattr, char *buf)
567 struct f71882fg_data *data = f71882fg_update_device(dev);
568 int nr = to_sensor_dev_attr(devattr)->index;
570 return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
573 static ssize_t store_temp_max(struct device *dev, struct device_attribute
574 *devattr, const char *buf, size_t count)
576 struct f71882fg_data *data = dev_get_drvdata(dev);
577 int nr = to_sensor_dev_attr(devattr)->index;
578 int val = simple_strtoul(buf, NULL, 10) / 1000;
580 if (val > 255)
581 val = 255;
583 mutex_lock(&data->update_lock);
584 f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
585 data->temp_high[nr] = val;
586 mutex_unlock(&data->update_lock);
588 return count;
591 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
592 *devattr, char *buf)
594 struct f71882fg_data *data = f71882fg_update_device(dev);
595 int nr = to_sensor_dev_attr(devattr)->index;
597 return sprintf(buf, "%d\n",
598 (data->temp_high[nr] - data->temp_hyst[nr]) * 1000);
601 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
602 *devattr, const char *buf, size_t count)
604 struct f71882fg_data *data = dev_get_drvdata(dev);
605 int nr = to_sensor_dev_attr(devattr)->index;
606 int val = simple_strtoul(buf, NULL, 10) / 1000;
607 ssize_t ret = count;
609 mutex_lock(&data->update_lock);
611 /* convert abs to relative and check */
612 val = data->temp_high[nr] - val;
613 if (val < 0 || val > 15) {
614 ret = -EINVAL;
615 goto store_temp_max_hyst_exit;
618 data->temp_hyst[nr] = val;
620 /* convert value to register contents */
621 switch (nr) {
622 case 0:
623 val = val << 4;
624 break;
625 case 1:
626 val = val | (data->temp_hyst[2] << 4);
627 break;
628 case 2:
629 val = data->temp_hyst[1] | (val << 4);
630 break;
633 f71882fg_write8(data, nr ? F71882FG_REG_TEMP_HYST23 :
634 F71882FG_REG_TEMP_HYST1, val);
636 store_temp_max_hyst_exit:
637 mutex_unlock(&data->update_lock);
638 return ret;
641 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
642 *devattr, char *buf)
644 struct f71882fg_data *data = f71882fg_update_device(dev);
645 int nr = to_sensor_dev_attr(devattr)->index;
647 return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
650 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
651 *devattr, const char *buf, size_t count)
653 struct f71882fg_data *data = dev_get_drvdata(dev);
654 int nr = to_sensor_dev_attr(devattr)->index;
655 int val = simple_strtoul(buf, NULL, 10) / 1000;
657 if (val > 255)
658 val = 255;
660 mutex_lock(&data->update_lock);
661 f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
662 data->temp_ovt[nr] = val;
663 mutex_unlock(&data->update_lock);
665 return count;
668 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
669 *devattr, char *buf)
671 struct f71882fg_data *data = f71882fg_update_device(dev);
672 int nr = to_sensor_dev_attr(devattr)->index;
674 return sprintf(buf, "%d\n",
675 (data->temp_ovt[nr] - data->temp_hyst[nr]) * 1000);
678 static ssize_t show_temp_type(struct device *dev, struct device_attribute
679 *devattr, char *buf)
681 struct f71882fg_data *data = f71882fg_update_device(dev);
682 int nr = to_sensor_dev_attr(devattr)->index;
684 return sprintf(buf, "%d\n", data->temp_type[nr]);
687 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
688 *devattr, char *buf)
690 struct f71882fg_data *data = f71882fg_update_device(dev);
691 int nr = to_sensor_dev_attr(devattr)->index;
693 if (data->temp_beep & (1 << (nr + 1)))
694 return sprintf(buf, "1\n");
695 else
696 return sprintf(buf, "0\n");
699 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
700 *devattr, const char *buf, size_t count)
702 struct f71882fg_data *data = dev_get_drvdata(dev);
703 int nr = to_sensor_dev_attr(devattr)->index;
704 int val = simple_strtoul(buf, NULL, 10);
706 mutex_lock(&data->update_lock);
707 if (val)
708 data->temp_beep |= 1 << (nr + 1);
709 else
710 data->temp_beep &= ~(1 << (nr + 1));
712 f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
713 mutex_unlock(&data->update_lock);
715 return count;
718 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
719 *devattr, char *buf)
721 struct f71882fg_data *data = f71882fg_update_device(dev);
722 int nr = to_sensor_dev_attr(devattr)->index;
724 if (data->temp_status & (1 << (nr + 1)))
725 return sprintf(buf, "1\n");
726 else
727 return sprintf(buf, "0\n");
730 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
731 *devattr, char *buf)
733 struct f71882fg_data *data = f71882fg_update_device(dev);
734 int nr = to_sensor_dev_attr(devattr)->index;
736 if (data->temp_diode_open & (1 << (nr + 1)))
737 return sprintf(buf, "1\n");
738 else
739 return sprintf(buf, "0\n");
742 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
743 char *buf)
745 return sprintf(buf, DRVNAME "\n");
749 static int __devinit f71882fg_probe(struct platform_device * pdev)
751 struct f71882fg_data *data;
752 int err, i;
753 u8 start_reg;
755 if (!(data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL)))
756 return -ENOMEM;
758 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
759 mutex_init(&data->update_lock);
760 platform_set_drvdata(pdev, data);
762 /* Register sysfs interface files */
763 for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++) {
764 err = device_create_file(&pdev->dev, &f71882fg_dev_attr[i]);
765 if (err)
766 goto exit_unregister_sysfs;
769 start_reg = f71882fg_read8(data, F71882FG_REG_START);
770 if (start_reg & 0x01) {
771 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++) {
772 err = device_create_file(&pdev->dev,
773 &f71882fg_in_temp_attr[i].dev_attr);
774 if (err)
775 goto exit_unregister_sysfs;
779 if (start_reg & 0x02) {
780 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++) {
781 err = device_create_file(&pdev->dev,
782 &f71882fg_fan_attr[i].dev_attr);
783 if (err)
784 goto exit_unregister_sysfs;
788 data->hwmon_dev = hwmon_device_register(&pdev->dev);
789 if (IS_ERR(data->hwmon_dev)) {
790 err = PTR_ERR(data->hwmon_dev);
791 goto exit_unregister_sysfs;
794 return 0;
796 exit_unregister_sysfs:
797 for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++)
798 device_remove_file(&pdev->dev, &f71882fg_dev_attr[i]);
800 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
801 device_remove_file(&pdev->dev,
802 &f71882fg_in_temp_attr[i].dev_attr);
804 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
805 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
807 kfree(data);
809 return err;
812 static int __devexit f71882fg_remove(struct platform_device *pdev)
814 int i;
815 struct f71882fg_data *data = platform_get_drvdata(pdev);
817 platform_set_drvdata(pdev, NULL);
818 hwmon_device_unregister(data->hwmon_dev);
820 for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++)
821 device_remove_file(&pdev->dev, &f71882fg_dev_attr[i]);
823 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
824 device_remove_file(&pdev->dev,
825 &f71882fg_in_temp_attr[i].dev_attr);
827 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
828 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
830 kfree(data);
832 return 0;
835 static int __init f71882fg_find(int sioaddr, unsigned short *address)
837 int err = -ENODEV;
838 u16 devid;
839 u8 start_reg;
840 struct f71882fg_data data;
842 superio_enter(sioaddr);
844 devid = superio_inw(sioaddr, SIO_REG_MANID);
845 if (devid != SIO_FINTEK_ID) {
846 printk(KERN_INFO DRVNAME ": Not a Fintek device\n");
847 goto exit;
850 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
851 if (devid != SIO_F71882_ID) {
852 printk(KERN_INFO DRVNAME ": Unsupported Fintek device\n");
853 goto exit;
856 superio_select(sioaddr, SIO_F71882FG_LD_HWM);
857 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
858 printk(KERN_WARNING DRVNAME ": Device not activated\n");
859 goto exit;
862 *address = superio_inw(sioaddr, SIO_REG_ADDR);
863 if (*address == 0)
865 printk(KERN_WARNING DRVNAME ": Base address not set\n");
866 goto exit;
868 *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
870 data.addr = *address;
871 start_reg = f71882fg_read8(&data, F71882FG_REG_START);
872 if (!(start_reg & 0x03)) {
873 printk(KERN_WARNING DRVNAME
874 ": Hardware monitoring not activated\n");
875 goto exit;
878 err = 0;
879 printk(KERN_INFO DRVNAME ": Found F71882FG chip at %#x, revision %d\n",
880 (unsigned int)*address,
881 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
882 exit:
883 superio_exit(sioaddr);
884 return err;
887 static int __init f71882fg_device_add(unsigned short address)
889 struct resource res = {
890 .start = address,
891 .end = address + REGION_LENGTH - 1,
892 .flags = IORESOURCE_IO,
894 int err;
896 f71882fg_pdev = platform_device_alloc(DRVNAME, address);
897 if (!f71882fg_pdev)
898 return -ENOMEM;
900 res.name = f71882fg_pdev->name;
901 err = platform_device_add_resources(f71882fg_pdev, &res, 1);
902 if (err) {
903 printk(KERN_ERR DRVNAME ": Device resource addition failed\n");
904 goto exit_device_put;
907 err = platform_device_add(f71882fg_pdev);
908 if (err) {
909 printk(KERN_ERR DRVNAME ": Device addition failed\n");
910 goto exit_device_put;
913 return 0;
915 exit_device_put:
916 platform_device_put(f71882fg_pdev);
918 return err;
921 static int __init f71882fg_init(void)
923 int err = -ENODEV;
924 unsigned short address;
926 if (f71882fg_find(0x2e, &address) && f71882fg_find(0x4e, &address))
927 goto exit;
929 if ((err = platform_driver_register(&f71882fg_driver)))
930 goto exit;
932 if ((err = f71882fg_device_add(address)))
933 goto exit_driver;
935 return 0;
937 exit_driver:
938 platform_driver_unregister(&f71882fg_driver);
939 exit:
940 return err;
943 static void __exit f71882fg_exit(void)
945 platform_device_unregister(f71882fg_pdev);
946 platform_driver_unregister(&f71882fg_driver);
949 MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
950 MODULE_AUTHOR("Hans Edgington (hans@edgington.nl)");
951 MODULE_LICENSE("GPL");
953 module_init(f71882fg_init);
954 module_exit(f71882fg_exit);