2 * f71805f.c - driver for the Fintek F71805F/FG Super-I/O chip integrated
3 * hardware monitoring features
4 * Copyright (C) 2005-2006 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 <linux/mutex.h>
34 #include <linux/sysfs.h>
37 static struct platform_device *pdev;
39 #define DRVNAME "f71805f"
42 * Super-I/O constants and functions
45 #define F71805F_LD_HWM 0x04
47 #define SIO_REG_LDSEL 0x07 /* Logical device select */
48 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
49 #define SIO_REG_DEVREV 0x22 /* Device revision */
50 #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
51 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
52 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
54 #define SIO_FINTEK_ID 0x1934
55 #define SIO_F71805F_ID 0x0406
58 superio_inb(int base, int reg)
65 superio_inw(int base, int reg)
69 val = inb(base + 1) << 8;
76 superio_select(int base, int ld)
78 outb(SIO_REG_LDSEL, base);
83 superio_enter(int base)
90 superio_exit(int base)
99 #define REGION_LENGTH 2
100 #define ADDR_REG_OFFSET 0
101 #define DATA_REG_OFFSET 1
107 /* in nr from 0 to 8 (8-bit values) */
108 #define F71805F_REG_IN(nr) (0x10 + (nr))
109 #define F71805F_REG_IN_HIGH(nr) (0x40 + 2 * (nr))
110 #define F71805F_REG_IN_LOW(nr) (0x41 + 2 * (nr))
111 /* fan nr from 0 to 2 (12-bit values, two registers) */
112 #define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
113 #define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
114 #define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
115 #define F71805F_REG_PWM_DUTY(nr) (0x6B + 16 * (nr))
116 /* temp nr from 0 to 2 (8-bit values) */
117 #define F71805F_REG_TEMP(nr) (0x1B + (nr))
118 #define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
119 #define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
120 #define F71805F_REG_TEMP_MODE 0x01
122 #define F71805F_REG_START 0x00
123 /* status nr from 0 to 2 */
124 #define F71805F_REG_STATUS(nr) (0x36 + (nr))
126 /* individual register bits */
127 #define FAN_CTRL_SKIP 0x80
128 #define FAN_CTRL_MODE_MASK 0x03
129 #define FAN_CTRL_MODE_SPEED 0x00
130 #define FAN_CTRL_MODE_TEMPERATURE 0x01
131 #define FAN_CTRL_MODE_MANUAL 0x02
134 * Data structures and manipulation thereof
137 struct f71805f_data {
141 struct class_device *class_dev;
143 struct mutex update_lock;
144 char valid; /* !=0 if following fields are valid */
145 unsigned long last_updated; /* In jiffies */
146 unsigned long last_limits; /* In jiffies */
148 /* Register values */
160 unsigned long alarms;
163 static inline long in_from_reg(u8 reg)
168 /* The 2 least significant bits are not used */
169 static inline u8 in_to_reg(long val)
175 return (((val + 16) / 32) << 2);
178 /* in0 is downscaled by a factor 2 internally */
179 static inline long in0_from_reg(u8 reg)
184 static inline u8 in0_to_reg(long val)
190 return (((val + 32) / 64) << 2);
193 /* The 4 most significant bits are not used */
194 static inline long fan_from_reg(u16 reg)
197 if (!reg || reg == 0xfff)
199 return (1500000 / reg);
202 static inline u16 fan_to_reg(long rpm)
204 /* If the low limit is set below what the chip can measure,
205 store the largest possible 12-bit value in the registers,
206 so that no alarm will ever trigger. */
209 return (1500000 / rpm);
212 static inline long temp_from_reg(u8 reg)
217 static inline u8 temp_to_reg(long val)
221 else if (val > 1000 * 0xff)
223 return ((val + 500) / 1000);
230 static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
234 mutex_lock(&data->lock);
235 outb(reg, data->addr + ADDR_REG_OFFSET);
236 val = inb(data->addr + DATA_REG_OFFSET);
237 mutex_unlock(&data->lock);
242 static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
244 mutex_lock(&data->lock);
245 outb(reg, data->addr + ADDR_REG_OFFSET);
246 outb(val, data->addr + DATA_REG_OFFSET);
247 mutex_unlock(&data->lock);
250 /* It is important to read the MSB first, because doing so latches the
251 value of the LSB, so we are sure both bytes belong to the same value. */
252 static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
256 mutex_lock(&data->lock);
257 outb(reg, data->addr + ADDR_REG_OFFSET);
258 val = inb(data->addr + DATA_REG_OFFSET) << 8;
259 outb(++reg, data->addr + ADDR_REG_OFFSET);
260 val |= inb(data->addr + DATA_REG_OFFSET);
261 mutex_unlock(&data->lock);
266 static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
268 mutex_lock(&data->lock);
269 outb(reg, data->addr + ADDR_REG_OFFSET);
270 outb(val >> 8, data->addr + DATA_REG_OFFSET);
271 outb(++reg, data->addr + ADDR_REG_OFFSET);
272 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
273 mutex_unlock(&data->lock);
276 static struct f71805f_data *f71805f_update_device(struct device *dev)
278 struct f71805f_data *data = dev_get_drvdata(dev);
281 mutex_lock(&data->update_lock);
283 /* Limit registers cache is refreshed after 60 seconds */
284 if (time_after(jiffies, data->last_updated + 60 * HZ)
286 for (nr = 0; nr < 9; nr++) {
287 data->in_high[nr] = f71805f_read8(data,
288 F71805F_REG_IN_HIGH(nr));
289 data->in_low[nr] = f71805f_read8(data,
290 F71805F_REG_IN_LOW(nr));
292 for (nr = 0; nr < 3; nr++) {
293 if (data->fan_ctrl[nr] & FAN_CTRL_SKIP)
295 data->fan_low[nr] = f71805f_read16(data,
296 F71805F_REG_FAN_LOW(nr));
298 for (nr = 0; nr < 3; nr++) {
299 data->temp_high[nr] = f71805f_read8(data,
300 F71805F_REG_TEMP_HIGH(nr));
301 data->temp_hyst[nr] = f71805f_read8(data,
302 F71805F_REG_TEMP_HYST(nr));
304 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
306 data->last_limits = jiffies;
309 /* Measurement registers cache is refreshed after 1 second */
310 if (time_after(jiffies, data->last_updated + HZ)
312 for (nr = 0; nr < 9; nr++) {
313 data->in[nr] = f71805f_read8(data,
316 for (nr = 0; nr < 3; nr++) {
317 if (data->fan_ctrl[nr] & FAN_CTRL_SKIP)
319 data->fan[nr] = f71805f_read16(data,
320 F71805F_REG_FAN(nr));
321 data->fan_ctrl[nr] = f71805f_read8(data,
322 F71805F_REG_FAN_CTRL(nr));
323 data->pwm[nr] = f71805f_read8(data,
324 F71805F_REG_PWM_DUTY(nr));
326 for (nr = 0; nr < 3; nr++) {
327 data->temp[nr] = f71805f_read8(data,
328 F71805F_REG_TEMP(nr));
330 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
331 + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
332 + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
334 data->last_updated = jiffies;
338 mutex_unlock(&data->update_lock);
347 static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
350 struct f71805f_data *data = f71805f_update_device(dev);
352 return sprintf(buf, "%ld\n", in0_from_reg(data->in[0]));
355 static ssize_t show_in0_max(struct device *dev, struct device_attribute
358 struct f71805f_data *data = f71805f_update_device(dev);
360 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[0]));
363 static ssize_t show_in0_min(struct device *dev, struct device_attribute
366 struct f71805f_data *data = f71805f_update_device(dev);
368 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[0]));
371 static ssize_t set_in0_max(struct device *dev, struct device_attribute
372 *devattr, const char *buf, size_t count)
374 struct f71805f_data *data = dev_get_drvdata(dev);
375 long val = simple_strtol(buf, NULL, 10);
377 mutex_lock(&data->update_lock);
378 data->in_high[0] = in0_to_reg(val);
379 f71805f_write8(data, F71805F_REG_IN_HIGH(0), data->in_high[0]);
380 mutex_unlock(&data->update_lock);
385 static ssize_t set_in0_min(struct device *dev, struct device_attribute
386 *devattr, const char *buf, size_t count)
388 struct f71805f_data *data = dev_get_drvdata(dev);
389 long val = simple_strtol(buf, NULL, 10);
391 mutex_lock(&data->update_lock);
392 data->in_low[0] = in0_to_reg(val);
393 f71805f_write8(data, F71805F_REG_IN_LOW(0), data->in_low[0]);
394 mutex_unlock(&data->update_lock);
399 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
402 struct f71805f_data *data = f71805f_update_device(dev);
403 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
404 int nr = attr->index;
406 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
409 static ssize_t show_in_max(struct device *dev, struct device_attribute
412 struct f71805f_data *data = f71805f_update_device(dev);
413 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
414 int nr = attr->index;
416 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
419 static ssize_t show_in_min(struct device *dev, struct device_attribute
422 struct f71805f_data *data = f71805f_update_device(dev);
423 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
424 int nr = attr->index;
426 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
429 static ssize_t set_in_max(struct device *dev, struct device_attribute
430 *devattr, const char *buf, size_t count)
432 struct f71805f_data *data = dev_get_drvdata(dev);
433 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
434 int nr = attr->index;
435 long val = simple_strtol(buf, NULL, 10);
437 mutex_lock(&data->update_lock);
438 data->in_high[nr] = in_to_reg(val);
439 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
440 mutex_unlock(&data->update_lock);
445 static ssize_t set_in_min(struct device *dev, struct device_attribute
446 *devattr, const char *buf, size_t count)
448 struct f71805f_data *data = dev_get_drvdata(dev);
449 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
450 int nr = attr->index;
451 long val = simple_strtol(buf, NULL, 10);
453 mutex_lock(&data->update_lock);
454 data->in_low[nr] = in_to_reg(val);
455 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
456 mutex_unlock(&data->update_lock);
461 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
464 struct f71805f_data *data = f71805f_update_device(dev);
465 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
466 int nr = attr->index;
468 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
471 static ssize_t show_fan_min(struct device *dev, struct device_attribute
474 struct f71805f_data *data = f71805f_update_device(dev);
475 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
476 int nr = attr->index;
478 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
481 static ssize_t set_fan_min(struct device *dev, struct device_attribute
482 *devattr, const char *buf, size_t count)
484 struct f71805f_data *data = dev_get_drvdata(dev);
485 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
486 int nr = attr->index;
487 long val = simple_strtol(buf, NULL, 10);
489 mutex_lock(&data->update_lock);
490 data->fan_low[nr] = fan_to_reg(val);
491 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
492 mutex_unlock(&data->update_lock);
497 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
500 struct f71805f_data *data = f71805f_update_device(dev);
501 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
502 int nr = attr->index;
504 return sprintf(buf, "%d\n", (int)data->pwm[nr]);
507 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
510 struct f71805f_data *data = f71805f_update_device(dev);
511 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
512 int nr = attr->index;
515 switch (data->fan_ctrl[nr] & FAN_CTRL_MODE_MASK) {
516 case FAN_CTRL_MODE_SPEED:
519 case FAN_CTRL_MODE_TEMPERATURE:
522 default: /* MANUAL */
526 return sprintf(buf, "%d\n", mode);
529 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
530 const char *buf, size_t count)
532 struct f71805f_data *data = dev_get_drvdata(dev);
533 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
534 int nr = attr->index;
535 unsigned long val = simple_strtoul(buf, NULL, 10);
540 mutex_lock(&data->update_lock);
542 f71805f_write8(data, F71805F_REG_PWM_DUTY(nr), data->pwm[nr]);
543 mutex_unlock(&data->update_lock);
548 static struct attribute *f71805f_attr_pwm[];
550 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
551 *devattr, const char *buf, size_t count)
553 struct f71805f_data *data = dev_get_drvdata(dev);
554 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
555 int nr = attr->index;
556 unsigned long val = simple_strtoul(buf, NULL, 10);
559 if (val < 1 || val > 3)
562 if (val > 1) { /* Automatic mode, user can't set PWM value */
563 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
565 dev_dbg(dev, "chmod -w pwm%d failed\n", nr + 1);
568 mutex_lock(&data->update_lock);
569 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(nr))
570 & ~FAN_CTRL_MODE_MASK;
573 reg |= FAN_CTRL_MODE_MANUAL;
576 reg |= FAN_CTRL_MODE_TEMPERATURE;
579 reg |= FAN_CTRL_MODE_SPEED;
582 data->fan_ctrl[nr] = reg;
583 f71805f_write8(data, F71805F_REG_FAN_CTRL(nr), reg);
584 mutex_unlock(&data->update_lock);
586 if (val == 1) { /* Manual mode, user can set PWM value */
587 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
589 dev_dbg(dev, "chmod +w pwm%d failed\n", nr + 1);
595 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
598 struct f71805f_data *data = f71805f_update_device(dev);
599 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
600 int nr = attr->index;
602 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
605 static ssize_t show_temp_max(struct device *dev, struct device_attribute
608 struct f71805f_data *data = f71805f_update_device(dev);
609 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
610 int nr = attr->index;
612 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
615 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
618 struct f71805f_data *data = f71805f_update_device(dev);
619 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
620 int nr = attr->index;
622 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
625 static ssize_t show_temp_type(struct device *dev, struct device_attribute
628 struct f71805f_data *data = f71805f_update_device(dev);
629 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
630 int nr = attr->index;
632 /* 3 is diode, 4 is thermistor */
633 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
636 static ssize_t set_temp_max(struct device *dev, struct device_attribute
637 *devattr, const char *buf, size_t count)
639 struct f71805f_data *data = dev_get_drvdata(dev);
640 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
641 int nr = attr->index;
642 long val = simple_strtol(buf, NULL, 10);
644 mutex_lock(&data->update_lock);
645 data->temp_high[nr] = temp_to_reg(val);
646 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
647 mutex_unlock(&data->update_lock);
652 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
653 *devattr, const char *buf, size_t count)
655 struct f71805f_data *data = dev_get_drvdata(dev);
656 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
657 int nr = attr->index;
658 long val = simple_strtol(buf, NULL, 10);
660 mutex_lock(&data->update_lock);
661 data->temp_hyst[nr] = temp_to_reg(val);
662 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
663 mutex_unlock(&data->update_lock);
668 static ssize_t show_alarms_in(struct device *dev, struct device_attribute
671 struct f71805f_data *data = f71805f_update_device(dev);
673 return sprintf(buf, "%lu\n", data->alarms & 0x1ff);
676 static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
679 struct f71805f_data *data = f71805f_update_device(dev);
681 return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
684 static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
687 struct f71805f_data *data = f71805f_update_device(dev);
689 return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
692 static ssize_t show_alarm(struct device *dev, struct device_attribute
695 struct f71805f_data *data = f71805f_update_device(dev);
696 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
697 int bitnr = attr->index;
699 return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
702 static ssize_t show_name(struct device *dev, struct device_attribute
705 struct f71805f_data *data = dev_get_drvdata(dev);
707 return sprintf(buf, "%s\n", data->name);
710 static DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL);
711 static DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR, show_in0_max, set_in0_max);
712 static DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR, show_in0_min, set_in0_min);
713 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
714 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
715 show_in_max, set_in_max, 1);
716 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
717 show_in_min, set_in_min, 1);
718 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
719 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
720 show_in_max, set_in_max, 2);
721 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
722 show_in_min, set_in_min, 2);
723 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
724 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
725 show_in_max, set_in_max, 3);
726 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
727 show_in_min, set_in_min, 3);
728 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
729 static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
730 show_in_max, set_in_max, 4);
731 static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
732 show_in_min, set_in_min, 4);
733 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
734 static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
735 show_in_max, set_in_max, 5);
736 static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
737 show_in_min, set_in_min, 5);
738 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
739 static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
740 show_in_max, set_in_max, 6);
741 static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
742 show_in_min, set_in_min, 6);
743 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
744 static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
745 show_in_max, set_in_max, 7);
746 static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
747 show_in_min, set_in_min, 7);
748 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
749 static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
750 show_in_max, set_in_max, 8);
751 static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
752 show_in_min, set_in_min, 8);
754 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
755 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
756 show_fan_min, set_fan_min, 0);
757 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
758 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
759 show_fan_min, set_fan_min, 1);
760 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
761 static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
762 show_fan_min, set_fan_min, 2);
764 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
765 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
766 show_temp_max, set_temp_max, 0);
767 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
768 show_temp_hyst, set_temp_hyst, 0);
769 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
770 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
771 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
772 show_temp_max, set_temp_max, 1);
773 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
774 show_temp_hyst, set_temp_hyst, 1);
775 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
776 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
777 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
778 show_temp_max, set_temp_max, 2);
779 static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
780 show_temp_hyst, set_temp_hyst, 2);
781 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
783 /* pwm (value) files are created read-only, write permission is
784 then added or removed dynamically as needed */
785 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, show_pwm, set_pwm, 0);
786 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
787 show_pwm_enable, set_pwm_enable, 0);
788 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO, show_pwm, set_pwm, 1);
789 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
790 show_pwm_enable, set_pwm_enable, 1);
791 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO, show_pwm, set_pwm, 2);
792 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
793 show_pwm_enable, set_pwm_enable, 2);
795 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
796 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
797 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
798 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
799 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
800 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
801 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
802 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
803 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
804 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
805 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
806 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
807 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
808 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
809 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
810 static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
811 static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
812 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
814 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
816 static struct attribute *f71805f_attributes[] = {
817 &dev_attr_in0_input.attr,
818 &dev_attr_in0_max.attr,
819 &dev_attr_in0_min.attr,
820 &sensor_dev_attr_in1_input.dev_attr.attr,
821 &sensor_dev_attr_in1_max.dev_attr.attr,
822 &sensor_dev_attr_in1_min.dev_attr.attr,
823 &sensor_dev_attr_in2_input.dev_attr.attr,
824 &sensor_dev_attr_in2_max.dev_attr.attr,
825 &sensor_dev_attr_in2_min.dev_attr.attr,
826 &sensor_dev_attr_in3_input.dev_attr.attr,
827 &sensor_dev_attr_in3_max.dev_attr.attr,
828 &sensor_dev_attr_in3_min.dev_attr.attr,
829 &sensor_dev_attr_in4_input.dev_attr.attr,
830 &sensor_dev_attr_in4_max.dev_attr.attr,
831 &sensor_dev_attr_in4_min.dev_attr.attr,
832 &sensor_dev_attr_in5_input.dev_attr.attr,
833 &sensor_dev_attr_in5_max.dev_attr.attr,
834 &sensor_dev_attr_in5_min.dev_attr.attr,
835 &sensor_dev_attr_in6_input.dev_attr.attr,
836 &sensor_dev_attr_in6_max.dev_attr.attr,
837 &sensor_dev_attr_in6_min.dev_attr.attr,
838 &sensor_dev_attr_in7_input.dev_attr.attr,
839 &sensor_dev_attr_in7_max.dev_attr.attr,
840 &sensor_dev_attr_in7_min.dev_attr.attr,
841 &sensor_dev_attr_in8_input.dev_attr.attr,
842 &sensor_dev_attr_in8_max.dev_attr.attr,
843 &sensor_dev_attr_in8_min.dev_attr.attr,
845 &sensor_dev_attr_temp1_input.dev_attr.attr,
846 &sensor_dev_attr_temp1_max.dev_attr.attr,
847 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
848 &sensor_dev_attr_temp1_type.dev_attr.attr,
849 &sensor_dev_attr_temp2_input.dev_attr.attr,
850 &sensor_dev_attr_temp2_max.dev_attr.attr,
851 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
852 &sensor_dev_attr_temp2_type.dev_attr.attr,
853 &sensor_dev_attr_temp3_input.dev_attr.attr,
854 &sensor_dev_attr_temp3_max.dev_attr.attr,
855 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
856 &sensor_dev_attr_temp3_type.dev_attr.attr,
858 &sensor_dev_attr_in0_alarm.dev_attr.attr,
859 &sensor_dev_attr_in1_alarm.dev_attr.attr,
860 &sensor_dev_attr_in2_alarm.dev_attr.attr,
861 &sensor_dev_attr_in3_alarm.dev_attr.attr,
862 &sensor_dev_attr_in4_alarm.dev_attr.attr,
863 &sensor_dev_attr_in5_alarm.dev_attr.attr,
864 &sensor_dev_attr_in6_alarm.dev_attr.attr,
865 &sensor_dev_attr_in7_alarm.dev_attr.attr,
866 &sensor_dev_attr_in8_alarm.dev_attr.attr,
867 &dev_attr_alarms_in.attr,
868 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
869 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
870 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
871 &dev_attr_alarms_temp.attr,
872 &dev_attr_alarms_fan.attr,
878 static const struct attribute_group f71805f_group = {
879 .attrs = f71805f_attributes,
882 static struct attribute *f71805f_attributes_fan[3][6] = {
884 &sensor_dev_attr_fan1_input.dev_attr.attr,
885 &sensor_dev_attr_fan1_min.dev_attr.attr,
886 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
887 &sensor_dev_attr_pwm1.dev_attr.attr,
888 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
891 &sensor_dev_attr_fan2_input.dev_attr.attr,
892 &sensor_dev_attr_fan2_min.dev_attr.attr,
893 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
894 &sensor_dev_attr_pwm2.dev_attr.attr,
895 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
898 &sensor_dev_attr_fan3_input.dev_attr.attr,
899 &sensor_dev_attr_fan3_min.dev_attr.attr,
900 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
901 &sensor_dev_attr_pwm3.dev_attr.attr,
902 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
907 static const struct attribute_group f71805f_group_fan[3] = {
908 { .attrs = f71805f_attributes_fan[0] },
909 { .attrs = f71805f_attributes_fan[1] },
910 { .attrs = f71805f_attributes_fan[2] },
913 /* We also need an indexed access to pwmN files to toggle writability */
914 static struct attribute *f71805f_attr_pwm[] = {
915 &sensor_dev_attr_pwm1.dev_attr.attr,
916 &sensor_dev_attr_pwm2.dev_attr.attr,
917 &sensor_dev_attr_pwm3.dev_attr.attr,
921 * Device registration and initialization
924 static void __devinit f71805f_init_device(struct f71805f_data *data)
929 reg = f71805f_read8(data, F71805F_REG_START);
930 if ((reg & 0x41) != 0x01) {
931 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
933 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
936 /* Fan monitoring can be disabled. If it is, we won't be polling
937 the register values, and won't create the related sysfs files. */
938 for (i = 0; i < 3; i++) {
939 data->fan_ctrl[i] = f71805f_read8(data,
940 F71805F_REG_FAN_CTRL(i));
944 static int __devinit f71805f_probe(struct platform_device *pdev)
946 struct f71805f_data *data;
947 struct resource *res;
950 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
952 printk(KERN_ERR DRVNAME ": Out of memory\n");
956 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
957 data->addr = res->start;
958 mutex_init(&data->lock);
959 data->name = "f71805f";
960 mutex_init(&data->update_lock);
962 platform_set_drvdata(pdev, data);
964 /* Initialize the F71805F chip */
965 f71805f_init_device(data);
967 /* Register sysfs interface files */
968 if ((err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group)))
970 for (i = 0; i < 3; i++) {
971 if (data->fan_ctrl[i] & FAN_CTRL_SKIP)
973 if ((err = sysfs_create_group(&pdev->dev.kobj,
974 &f71805f_group_fan[i])))
975 goto exit_remove_files;
976 /* If PWM is in manual mode, add write permission */
977 if (data->fan_ctrl[i] & FAN_CTRL_MODE_MANUAL) {
978 if ((err = sysfs_chmod_file(&pdev->dev.kobj,
980 S_IRUGO | S_IWUSR))) {
981 dev_err(&pdev->dev, "chmod +w pwm%d failed\n",
983 goto exit_remove_files;
988 data->class_dev = hwmon_device_register(&pdev->dev);
989 if (IS_ERR(data->class_dev)) {
990 err = PTR_ERR(data->class_dev);
991 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
992 goto exit_remove_files;
998 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
999 for (i = 0; i < 3; i++)
1000 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
1002 platform_set_drvdata(pdev, NULL);
1008 static int __devexit f71805f_remove(struct platform_device *pdev)
1010 struct f71805f_data *data = platform_get_drvdata(pdev);
1013 platform_set_drvdata(pdev, NULL);
1014 hwmon_device_unregister(data->class_dev);
1015 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
1016 for (i = 0; i < 3; i++)
1017 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
1023 static struct platform_driver f71805f_driver = {
1025 .owner = THIS_MODULE,
1028 .probe = f71805f_probe,
1029 .remove = __devexit_p(f71805f_remove),
1032 static int __init f71805f_device_add(unsigned short address)
1034 struct resource res = {
1036 .end = address + REGION_LENGTH - 1,
1037 .flags = IORESOURCE_IO,
1041 pdev = platform_device_alloc(DRVNAME, address);
1044 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1048 res.name = pdev->name;
1049 err = platform_device_add_resources(pdev, &res, 1);
1051 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1053 goto exit_device_put;
1056 err = platform_device_add(pdev);
1058 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1060 goto exit_device_put;
1066 platform_device_put(pdev);
1071 static int __init f71805f_find(int sioaddr, unsigned short *address)
1076 superio_enter(sioaddr);
1078 devid = superio_inw(sioaddr, SIO_REG_MANID);
1079 if (devid != SIO_FINTEK_ID)
1082 devid = superio_inw(sioaddr, SIO_REG_DEVID);
1083 if (devid != SIO_F71805F_ID) {
1084 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
1089 superio_select(sioaddr, F71805F_LD_HWM);
1090 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
1091 printk(KERN_WARNING DRVNAME ": Device not activated, "
1096 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1097 if (*address == 0) {
1098 printk(KERN_WARNING DRVNAME ": Base address not set, "
1104 printk(KERN_INFO DRVNAME ": Found F71805F chip at %#x, revision %u\n",
1105 *address, superio_inb(sioaddr, SIO_REG_DEVREV));
1108 superio_exit(sioaddr);
1112 static int __init f71805f_init(void)
1115 unsigned short address;
1117 if (f71805f_find(0x2e, &address)
1118 && f71805f_find(0x4e, &address))
1121 err = platform_driver_register(&f71805f_driver);
1125 /* Sets global pdev as a side effect */
1126 err = f71805f_device_add(address);
1133 platform_driver_unregister(&f71805f_driver);
1138 static void __exit f71805f_exit(void)
1140 platform_device_unregister(pdev);
1141 platform_driver_unregister(&f71805f_driver);
1144 MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
1145 MODULE_LICENSE("GPL");
1146 MODULE_DESCRIPTION("F71805F hardware monitoring driver");
1148 module_init(f71805f_init);
1149 module_exit(f71805f_exit);