Linux 2.6.16.55
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / f71805f.c
blobe029e0a94eccc0e447cd86766498189648dd7ba5
1 /*
2 * f71805f.c - driver for the Fintek F71805F/FG Super-I/O chip integrated
3 * hardware monitoring features
4 * Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
6 * The F71805F/FG is a LPC Super-I/O chip made by Fintek. It integrates
7 * complete hardware monitoring features: voltage, fan and temperature
8 * sensors, and manual and automatic fan speed control.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/platform_device.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <asm/io.h>
35 static struct platform_device *pdev;
37 #define DRVNAME "f71805f"
40 * Super-I/O constants and functions
43 #define F71805F_LD_HWM 0x04
45 #define SIO_REG_LDSEL 0x07 /* Logical device select */
46 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
47 #define SIO_REG_DEVREV 0x22 /* Device revision */
48 #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
49 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
50 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
52 #define SIO_FINTEK_ID 0x1934
53 #define SIO_F71805F_ID 0x0406
55 static inline int
56 superio_inb(int base, int reg)
58 outb(reg, base);
59 return inb(base + 1);
62 static int
63 superio_inw(int base, int reg)
65 int val;
66 outb(reg++, base);
67 val = inb(base + 1) << 8;
68 outb(reg, base);
69 val |= inb(base + 1);
70 return val;
73 static inline void
74 superio_select(int base, int ld)
76 outb(SIO_REG_LDSEL, base);
77 outb(ld, base + 1);
80 static inline void
81 superio_enter(int base)
83 outb(0x87, base);
84 outb(0x87, base);
87 static inline void
88 superio_exit(int base)
90 outb(0xaa, base);
94 * ISA constants
97 #define REGION_LENGTH 2
98 #define ADDR_REG_OFFSET 0
99 #define DATA_REG_OFFSET 1
101 static struct resource f71805f_resource __initdata = {
102 .flags = IORESOURCE_IO,
106 * Registers
109 /* in nr from 0 to 8 (8-bit values) */
110 #define F71805F_REG_IN(nr) (0x10 + (nr))
111 #define F71805F_REG_IN_HIGH(nr) (0x40 + 2 * (nr))
112 #define F71805F_REG_IN_LOW(nr) (0x41 + 2 * (nr))
113 /* fan nr from 0 to 2 (12-bit values, two registers) */
114 #define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
115 #define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
116 #define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
117 /* temp nr from 0 to 2 (8-bit values) */
118 #define F71805F_REG_TEMP(nr) (0x1B + (nr))
119 #define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
120 #define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
121 #define F71805F_REG_TEMP_MODE 0x01
123 #define F71805F_REG_START 0x00
124 /* status nr from 0 to 2 */
125 #define F71805F_REG_STATUS(nr) (0x36 + (nr))
128 * Data structures and manipulation thereof
131 struct f71805f_data {
132 unsigned short addr;
133 const char *name;
134 struct semaphore lock;
135 struct class_device *class_dev;
137 struct semaphore update_lock;
138 char valid; /* !=0 if following fields are valid */
139 unsigned long last_updated; /* In jiffies */
140 unsigned long last_limits; /* In jiffies */
142 /* Register values */
143 u8 in[9];
144 u8 in_high[9];
145 u8 in_low[9];
146 u16 fan[3];
147 u16 fan_low[3];
148 u8 fan_enabled; /* Read once at init time */
149 u8 temp[3];
150 u8 temp_high[3];
151 u8 temp_hyst[3];
152 u8 temp_mode;
153 u8 alarms[3];
156 static inline long in_from_reg(u8 reg)
158 return (reg * 8);
161 /* The 2 least significant bits are not used */
162 static inline u8 in_to_reg(long val)
164 if (val <= 0)
165 return 0;
166 if (val >= 2016)
167 return 0xfc;
168 return (((val + 16) / 32) << 2);
171 /* in0 is downscaled by a factor 2 internally */
172 static inline long in0_from_reg(u8 reg)
174 return (reg * 16);
177 static inline u8 in0_to_reg(long val)
179 if (val <= 0)
180 return 0;
181 if (val >= 4032)
182 return 0xfc;
183 return (((val + 32) / 64) << 2);
186 /* The 4 most significant bits are not used */
187 static inline long fan_from_reg(u16 reg)
189 reg &= 0xfff;
190 if (!reg || reg == 0xfff)
191 return 0;
192 return (1500000 / reg);
195 static inline u16 fan_to_reg(long rpm)
197 /* If the low limit is set below what the chip can measure,
198 store the largest possible 12-bit value in the registers,
199 so that no alarm will ever trigger. */
200 if (rpm < 367)
201 return 0xfff;
202 return (1500000 / rpm);
205 static inline long temp_from_reg(u8 reg)
207 return (reg * 1000);
210 static inline u8 temp_to_reg(long val)
212 if (val < 0)
213 val = 0;
214 else if (val > 1000 * 0xff)
215 val = 0xff;
216 return ((val + 500) / 1000);
220 * Device I/O access
223 static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
225 u8 val;
227 down(&data->lock);
228 outb(reg, data->addr + ADDR_REG_OFFSET);
229 val = inb(data->addr + DATA_REG_OFFSET);
230 up(&data->lock);
232 return val;
235 static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
237 down(&data->lock);
238 outb(reg, data->addr + ADDR_REG_OFFSET);
239 outb(val, data->addr + DATA_REG_OFFSET);
240 up(&data->lock);
243 /* It is important to read the MSB first, because doing so latches the
244 value of the LSB, so we are sure both bytes belong to the same value. */
245 static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
247 u16 val;
249 down(&data->lock);
250 outb(reg, data->addr + ADDR_REG_OFFSET);
251 val = inb(data->addr + DATA_REG_OFFSET) << 8;
252 outb(++reg, data->addr + ADDR_REG_OFFSET);
253 val |= inb(data->addr + DATA_REG_OFFSET);
254 up(&data->lock);
256 return val;
259 static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
261 down(&data->lock);
262 outb(reg, data->addr + ADDR_REG_OFFSET);
263 outb(val >> 8, data->addr + DATA_REG_OFFSET);
264 outb(++reg, data->addr + ADDR_REG_OFFSET);
265 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
266 up(&data->lock);
269 static struct f71805f_data *f71805f_update_device(struct device *dev)
271 struct f71805f_data *data = dev_get_drvdata(dev);
272 int nr;
274 down(&data->update_lock);
276 /* Limit registers cache is refreshed after 60 seconds */
277 if (time_after(jiffies, data->last_updated + 60 * HZ)
278 || !data->valid) {
279 for (nr = 0; nr < 9; nr++) {
280 data->in_high[nr] = f71805f_read8(data,
281 F71805F_REG_IN_HIGH(nr));
282 data->in_low[nr] = f71805f_read8(data,
283 F71805F_REG_IN_LOW(nr));
285 for (nr = 0; nr < 3; nr++) {
286 if (data->fan_enabled & (1 << nr))
287 data->fan_low[nr] = f71805f_read16(data,
288 F71805F_REG_FAN_LOW(nr));
290 for (nr = 0; nr < 3; nr++) {
291 data->temp_high[nr] = f71805f_read8(data,
292 F71805F_REG_TEMP_HIGH(nr));
293 data->temp_hyst[nr] = f71805f_read8(data,
294 F71805F_REG_TEMP_HYST(nr));
296 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
298 data->last_limits = jiffies;
301 /* Measurement registers cache is refreshed after 1 second */
302 if (time_after(jiffies, data->last_updated + HZ)
303 || !data->valid) {
304 for (nr = 0; nr < 9; nr++) {
305 data->in[nr] = f71805f_read8(data,
306 F71805F_REG_IN(nr));
308 for (nr = 0; nr < 3; nr++) {
309 if (data->fan_enabled & (1 << nr))
310 data->fan[nr] = f71805f_read16(data,
311 F71805F_REG_FAN(nr));
313 for (nr = 0; nr < 3; nr++) {
314 data->temp[nr] = f71805f_read8(data,
315 F71805F_REG_TEMP(nr));
317 for (nr = 0; nr < 3; nr++) {
318 data->alarms[nr] = f71805f_read8(data,
319 F71805F_REG_STATUS(nr));
322 data->last_updated = jiffies;
323 data->valid = 1;
326 up(&data->update_lock);
328 return data;
332 * Sysfs interface
335 static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
336 char *buf)
338 struct f71805f_data *data = f71805f_update_device(dev);
340 return sprintf(buf, "%ld\n", in0_from_reg(data->in[0]));
343 static ssize_t show_in0_max(struct device *dev, struct device_attribute
344 *devattr, char *buf)
346 struct f71805f_data *data = f71805f_update_device(dev);
348 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[0]));
351 static ssize_t show_in0_min(struct device *dev, struct device_attribute
352 *devattr, char *buf)
354 struct f71805f_data *data = f71805f_update_device(dev);
356 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[0]));
359 static ssize_t set_in0_max(struct device *dev, struct device_attribute
360 *devattr, const char *buf, size_t count)
362 struct f71805f_data *data = dev_get_drvdata(dev);
363 long val = simple_strtol(buf, NULL, 10);
365 down(&data->update_lock);
366 data->in_high[0] = in0_to_reg(val);
367 f71805f_write8(data, F71805F_REG_IN_HIGH(0), data->in_high[0]);
368 up(&data->update_lock);
370 return count;
373 static ssize_t set_in0_min(struct device *dev, struct device_attribute
374 *devattr, const char *buf, size_t count)
376 struct f71805f_data *data = dev_get_drvdata(dev);
377 long val = simple_strtol(buf, NULL, 10);
379 down(&data->update_lock);
380 data->in_low[0] = in0_to_reg(val);
381 f71805f_write8(data, F71805F_REG_IN_LOW(0), data->in_low[0]);
382 up(&data->update_lock);
384 return count;
387 static DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL);
388 static DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR, show_in0_max, set_in0_max);
389 static DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR, show_in0_min, set_in0_min);
391 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
392 char *buf)
394 struct f71805f_data *data = f71805f_update_device(dev);
395 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
396 int nr = attr->index;
398 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
401 static ssize_t show_in_max(struct device *dev, struct device_attribute
402 *devattr, char *buf)
404 struct f71805f_data *data = f71805f_update_device(dev);
405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
406 int nr = attr->index;
408 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
411 static ssize_t show_in_min(struct device *dev, struct device_attribute
412 *devattr, char *buf)
414 struct f71805f_data *data = f71805f_update_device(dev);
415 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
416 int nr = attr->index;
418 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
421 static ssize_t set_in_max(struct device *dev, struct device_attribute
422 *devattr, const char *buf, size_t count)
424 struct f71805f_data *data = dev_get_drvdata(dev);
425 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
426 int nr = attr->index;
427 long val = simple_strtol(buf, NULL, 10);
429 down(&data->update_lock);
430 data->in_high[nr] = in_to_reg(val);
431 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
432 up(&data->update_lock);
434 return count;
437 static ssize_t set_in_min(struct device *dev, struct device_attribute
438 *devattr, const char *buf, size_t count)
440 struct f71805f_data *data = dev_get_drvdata(dev);
441 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
442 int nr = attr->index;
443 long val = simple_strtol(buf, NULL, 10);
445 down(&data->update_lock);
446 data->in_low[nr] = in_to_reg(val);
447 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
448 up(&data->update_lock);
450 return count;
453 #define sysfs_in(offset) \
454 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
455 show_in, NULL, offset); \
456 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
457 show_in_max, set_in_max, offset); \
458 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
459 show_in_min, set_in_min, offset)
461 sysfs_in(1);
462 sysfs_in(2);
463 sysfs_in(3);
464 sysfs_in(4);
465 sysfs_in(5);
466 sysfs_in(6);
467 sysfs_in(7);
468 sysfs_in(8);
470 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
471 char *buf)
473 struct f71805f_data *data = f71805f_update_device(dev);
474 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
475 int nr = attr->index;
477 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
480 static ssize_t show_fan_min(struct device *dev, struct device_attribute
481 *devattr, char *buf)
483 struct f71805f_data *data = f71805f_update_device(dev);
484 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
485 int nr = attr->index;
487 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
490 static ssize_t set_fan_min(struct device *dev, struct device_attribute
491 *devattr, const char *buf, size_t count)
493 struct f71805f_data *data = dev_get_drvdata(dev);
494 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
495 int nr = attr->index;
496 long val = simple_strtol(buf, NULL, 10);
498 down(&data->update_lock);
499 data->fan_low[nr] = fan_to_reg(val);
500 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
501 up(&data->update_lock);
503 return count;
506 #define sysfs_fan(offset) \
507 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
508 show_fan, NULL, offset - 1); \
509 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
510 show_fan_min, set_fan_min, offset - 1)
512 sysfs_fan(1);
513 sysfs_fan(2);
514 sysfs_fan(3);
516 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
517 char *buf)
519 struct f71805f_data *data = f71805f_update_device(dev);
520 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
521 int nr = attr->index;
523 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
526 static ssize_t show_temp_max(struct device *dev, struct device_attribute
527 *devattr, char *buf)
529 struct f71805f_data *data = f71805f_update_device(dev);
530 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
531 int nr = attr->index;
533 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
536 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
537 *devattr, char *buf)
539 struct f71805f_data *data = f71805f_update_device(dev);
540 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
541 int nr = attr->index;
543 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
546 static ssize_t show_temp_type(struct device *dev, struct device_attribute
547 *devattr, char *buf)
549 struct f71805f_data *data = f71805f_update_device(dev);
550 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
551 int nr = attr->index;
553 /* 3 is diode, 4 is thermistor */
554 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
557 static ssize_t set_temp_max(struct device *dev, struct device_attribute
558 *devattr, const char *buf, size_t count)
560 struct f71805f_data *data = dev_get_drvdata(dev);
561 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
562 int nr = attr->index;
563 long val = simple_strtol(buf, NULL, 10);
565 down(&data->update_lock);
566 data->temp_high[nr] = temp_to_reg(val);
567 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
568 up(&data->update_lock);
570 return count;
573 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
574 *devattr, const char *buf, size_t count)
576 struct f71805f_data *data = dev_get_drvdata(dev);
577 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
578 int nr = attr->index;
579 long val = simple_strtol(buf, NULL, 10);
581 down(&data->update_lock);
582 data->temp_hyst[nr] = temp_to_reg(val);
583 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
584 up(&data->update_lock);
586 return count;
589 #define sysfs_temp(offset) \
590 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
591 show_temp, NULL, offset - 1); \
592 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
593 show_temp_max, set_temp_max, offset - 1); \
594 static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
595 show_temp_hyst, set_temp_hyst, offset - 1); \
596 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO, \
597 show_temp_type, NULL, offset - 1)
599 sysfs_temp(1);
600 sysfs_temp(2);
601 sysfs_temp(3);
603 static ssize_t show_alarms_in(struct device *dev, struct device_attribute
604 *devattr, char *buf)
606 struct f71805f_data *data = f71805f_update_device(dev);
608 return sprintf(buf, "%d\n", data->alarms[0] |
609 ((data->alarms[1] & 0x01) << 8));
612 static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
613 *devattr, char *buf)
615 struct f71805f_data *data = f71805f_update_device(dev);
617 return sprintf(buf, "%d\n", data->alarms[2] & 0x07);
620 static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
621 *devattr, char *buf)
623 struct f71805f_data *data = f71805f_update_device(dev);
625 return sprintf(buf, "%d\n", (data->alarms[1] >> 3) & 0x07);
628 static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
629 static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
630 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
632 static ssize_t show_name(struct device *dev, struct device_attribute
633 *devattr, char *buf)
635 struct f71805f_data *data = dev_get_drvdata(dev);
637 return sprintf(buf, "%s\n", data->name);
640 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
643 * Device registration and initialization
646 static void __devinit f71805f_init_device(struct f71805f_data *data)
648 u8 reg;
649 int i;
651 reg = f71805f_read8(data, F71805F_REG_START);
652 if ((reg & 0x41) != 0x01) {
653 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
654 "operations\n");
655 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
658 /* Fan monitoring can be disabled. If it is, we won't be polling
659 the register values, and won't create the related sysfs files. */
660 for (i = 0; i < 3; i++) {
661 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(i));
662 if (!(reg & 0x80))
663 data->fan_enabled |= (1 << i);
667 static int __devinit f71805f_probe(struct platform_device *pdev)
669 struct f71805f_data *data;
670 struct resource *res;
671 int err;
673 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
674 err = -ENOMEM;
675 printk(KERN_ERR DRVNAME ": Out of memory\n");
676 goto exit;
679 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
680 data->addr = res->start;
681 init_MUTEX(&data->lock);
682 data->name = "f71805f";
683 init_MUTEX(&data->update_lock);
685 platform_set_drvdata(pdev, data);
687 data->class_dev = hwmon_device_register(&pdev->dev);
688 if (IS_ERR(data->class_dev)) {
689 err = PTR_ERR(data->class_dev);
690 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
691 goto exit_free;
694 /* Initialize the F71805F chip */
695 f71805f_init_device(data);
697 /* Register sysfs interface files */
698 device_create_file(&pdev->dev, &dev_attr_in0_input);
699 device_create_file(&pdev->dev, &dev_attr_in0_max);
700 device_create_file(&pdev->dev, &dev_attr_in0_min);
701 device_create_file(&pdev->dev, &sensor_dev_attr_in1_input.dev_attr);
702 device_create_file(&pdev->dev, &sensor_dev_attr_in2_input.dev_attr);
703 device_create_file(&pdev->dev, &sensor_dev_attr_in3_input.dev_attr);
704 device_create_file(&pdev->dev, &sensor_dev_attr_in4_input.dev_attr);
705 device_create_file(&pdev->dev, &sensor_dev_attr_in5_input.dev_attr);
706 device_create_file(&pdev->dev, &sensor_dev_attr_in6_input.dev_attr);
707 device_create_file(&pdev->dev, &sensor_dev_attr_in7_input.dev_attr);
708 device_create_file(&pdev->dev, &sensor_dev_attr_in8_input.dev_attr);
709 device_create_file(&pdev->dev, &sensor_dev_attr_in1_max.dev_attr);
710 device_create_file(&pdev->dev, &sensor_dev_attr_in2_max.dev_attr);
711 device_create_file(&pdev->dev, &sensor_dev_attr_in3_max.dev_attr);
712 device_create_file(&pdev->dev, &sensor_dev_attr_in4_max.dev_attr);
713 device_create_file(&pdev->dev, &sensor_dev_attr_in5_max.dev_attr);
714 device_create_file(&pdev->dev, &sensor_dev_attr_in6_max.dev_attr);
715 device_create_file(&pdev->dev, &sensor_dev_attr_in7_max.dev_attr);
716 device_create_file(&pdev->dev, &sensor_dev_attr_in8_max.dev_attr);
717 device_create_file(&pdev->dev, &sensor_dev_attr_in1_min.dev_attr);
718 device_create_file(&pdev->dev, &sensor_dev_attr_in2_min.dev_attr);
719 device_create_file(&pdev->dev, &sensor_dev_attr_in3_min.dev_attr);
720 device_create_file(&pdev->dev, &sensor_dev_attr_in4_min.dev_attr);
721 device_create_file(&pdev->dev, &sensor_dev_attr_in5_min.dev_attr);
722 device_create_file(&pdev->dev, &sensor_dev_attr_in6_min.dev_attr);
723 device_create_file(&pdev->dev, &sensor_dev_attr_in7_min.dev_attr);
724 device_create_file(&pdev->dev, &sensor_dev_attr_in8_min.dev_attr);
725 if (data->fan_enabled & (1 << 0)) {
726 device_create_file(&pdev->dev,
727 &sensor_dev_attr_fan1_input.dev_attr);
728 device_create_file(&pdev->dev,
729 &sensor_dev_attr_fan1_min.dev_attr);
731 if (data->fan_enabled & (1 << 1)) {
732 device_create_file(&pdev->dev,
733 &sensor_dev_attr_fan2_input.dev_attr);
734 device_create_file(&pdev->dev,
735 &sensor_dev_attr_fan2_min.dev_attr);
737 if (data->fan_enabled & (1 << 2)) {
738 device_create_file(&pdev->dev,
739 &sensor_dev_attr_fan3_input.dev_attr);
740 device_create_file(&pdev->dev,
741 &sensor_dev_attr_fan3_min.dev_attr);
743 device_create_file(&pdev->dev,
744 &sensor_dev_attr_temp1_input.dev_attr);
745 device_create_file(&pdev->dev,
746 &sensor_dev_attr_temp2_input.dev_attr);
747 device_create_file(&pdev->dev,
748 &sensor_dev_attr_temp3_input.dev_attr);
749 device_create_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
750 device_create_file(&pdev->dev, &sensor_dev_attr_temp2_max.dev_attr);
751 device_create_file(&pdev->dev, &sensor_dev_attr_temp3_max.dev_attr);
752 device_create_file(&pdev->dev,
753 &sensor_dev_attr_temp1_max_hyst.dev_attr);
754 device_create_file(&pdev->dev,
755 &sensor_dev_attr_temp2_max_hyst.dev_attr);
756 device_create_file(&pdev->dev,
757 &sensor_dev_attr_temp3_max_hyst.dev_attr);
758 device_create_file(&pdev->dev, &sensor_dev_attr_temp1_type.dev_attr);
759 device_create_file(&pdev->dev, &sensor_dev_attr_temp2_type.dev_attr);
760 device_create_file(&pdev->dev, &sensor_dev_attr_temp3_type.dev_attr);
761 device_create_file(&pdev->dev, &dev_attr_alarms_in);
762 device_create_file(&pdev->dev, &dev_attr_alarms_fan);
763 device_create_file(&pdev->dev, &dev_attr_alarms_temp);
764 device_create_file(&pdev->dev, &dev_attr_name);
766 return 0;
768 exit_free:
769 kfree(data);
770 exit:
771 return err;
774 static int __devexit f71805f_remove(struct platform_device *pdev)
776 struct f71805f_data *data = platform_get_drvdata(pdev);
778 platform_set_drvdata(pdev, NULL);
779 hwmon_device_unregister(data->class_dev);
780 kfree(data);
782 return 0;
785 static struct platform_driver f71805f_driver = {
786 .driver = {
787 .owner = THIS_MODULE,
788 .name = DRVNAME,
790 .probe = f71805f_probe,
791 .remove = __devexit_p(f71805f_remove),
794 static int __init f71805f_device_add(unsigned short address)
796 int err;
798 pdev = platform_device_alloc(DRVNAME, address);
799 if (!pdev) {
800 err = -ENOMEM;
801 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
802 goto exit;
805 f71805f_resource.start = address;
806 f71805f_resource.end = address + REGION_LENGTH - 1;
807 f71805f_resource.name = pdev->name;
808 err = platform_device_add_resources(pdev, &f71805f_resource, 1);
809 if (err) {
810 printk(KERN_ERR DRVNAME ": Device resource addition failed "
811 "(%d)\n", err);
812 goto exit_device_put;
815 err = platform_device_add(pdev);
816 if (err) {
817 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
818 err);
819 goto exit_device_put;
822 return 0;
824 exit_device_put:
825 platform_device_put(pdev);
826 exit:
827 return err;
830 static int __init f71805f_find(int sioaddr, unsigned short *address)
832 int err = -ENODEV;
833 u16 devid;
835 superio_enter(sioaddr);
837 devid = superio_inw(sioaddr, SIO_REG_MANID);
838 if (devid != SIO_FINTEK_ID)
839 goto exit;
841 devid = superio_inw(sioaddr, SIO_REG_DEVID);
842 if (devid != SIO_F71805F_ID) {
843 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
844 "skipping\n");
845 goto exit;
848 superio_select(sioaddr, F71805F_LD_HWM);
849 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
850 printk(KERN_WARNING DRVNAME ": Device not activated, "
851 "skipping\n");
852 goto exit;
855 *address = superio_inw(sioaddr, SIO_REG_ADDR);
856 if (*address == 0) {
857 printk(KERN_WARNING DRVNAME ": Base address not set, "
858 "skipping\n");
859 goto exit;
862 err = 0;
863 printk(KERN_INFO DRVNAME ": Found F71805F chip at %#x, revision %u\n",
864 *address, superio_inb(sioaddr, SIO_REG_DEVREV));
866 exit:
867 superio_exit(sioaddr);
868 return err;
871 static int __init f71805f_init(void)
873 int err;
874 unsigned short address;
876 if (f71805f_find(0x2e, &address)
877 && f71805f_find(0x4e, &address))
878 return -ENODEV;
880 err = platform_driver_register(&f71805f_driver);
881 if (err)
882 goto exit;
884 /* Sets global pdev as a side effect */
885 err = f71805f_device_add(address);
886 if (err)
887 goto exit_driver;
889 return 0;
891 exit_driver:
892 platform_driver_unregister(&f71805f_driver);
893 exit:
894 return err;
897 static void __exit f71805f_exit(void)
899 platform_device_unregister(pdev);
900 platform_driver_unregister(&f71805f_driver);
903 MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
904 MODULE_LICENSE("GPL");
905 MODULE_DESCRIPTION("F71805F hardware monitoring driver");
907 module_init(f71805f_init);
908 module_exit(f71805f_exit);