Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /***************************************************************************
0003  *   Copyright (C) 2006 by Hans Edgington <hans@edgington.nl>              *
0004  *   Copyright (C) 2007-2011 Hans de Goede <hdegoede@redhat.com>           *
0005  *                                                                         *
0006  ***************************************************************************/
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/slab.h>
0013 #include <linux/jiffies.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/hwmon.h>
0016 #include <linux/hwmon-sysfs.h>
0017 #include <linux/err.h>
0018 #include <linux/mutex.h>
0019 #include <linux/io.h>
0020 #include <linux/acpi.h>
0021 
0022 #define DRVNAME "f71882fg"
0023 
0024 #define SIO_F71858FG_LD_HWM 0x02    /* Hardware monitor logical device */
0025 #define SIO_F71882FG_LD_HWM 0x04    /* Hardware monitor logical device */
0026 #define SIO_UNLOCK_KEY      0x87    /* Key to enable Super-I/O */
0027 #define SIO_LOCK_KEY        0xAA    /* Key to disable Super-I/O */
0028 
0029 #define SIO_REG_LDSEL       0x07    /* Logical device select */
0030 #define SIO_REG_DEVID       0x20    /* Device ID (2 bytes) */
0031 #define SIO_REG_DEVREV      0x22    /* Device revision */
0032 #define SIO_REG_MANID       0x23    /* Fintek ID (2 bytes) */
0033 #define SIO_REG_ENABLE      0x30    /* Logical device enable */
0034 #define SIO_REG_ADDR        0x60    /* Logical device address (2 bytes) */
0035 
0036 #define SIO_FINTEK_ID       0x1934  /* Manufacturers ID */
0037 #define SIO_F71808E_ID      0x0901  /* Chipset ID */
0038 #define SIO_F71808A_ID      0x1001  /* Chipset ID */
0039 #define SIO_F71858_ID       0x0507  /* Chipset ID */
0040 #define SIO_F71862_ID       0x0601  /* Chipset ID */
0041 #define SIO_F71868_ID       0x1106  /* Chipset ID */
0042 #define SIO_F71869_ID       0x0814  /* Chipset ID */
0043 #define SIO_F71869A_ID      0x1007  /* Chipset ID */
0044 #define SIO_F71882_ID       0x0541  /* Chipset ID */
0045 #define SIO_F71889_ID       0x0723  /* Chipset ID */
0046 #define SIO_F71889E_ID      0x0909  /* Chipset ID */
0047 #define SIO_F71889A_ID      0x1005  /* Chipset ID */
0048 #define SIO_F8000_ID        0x0581  /* Chipset ID */
0049 #define SIO_F81768D_ID      0x1210  /* Chipset ID */
0050 #define SIO_F81865_ID       0x0704  /* Chipset ID */
0051 #define SIO_F81866_ID       0x1010  /* Chipset ID */
0052 #define SIO_F71858AD_ID     0x0903  /* Chipset ID */
0053 #define SIO_F81966_ID       0x1502  /* Chipset ID */
0054 
0055 #define REGION_LENGTH       8
0056 #define ADDR_REG_OFFSET     5
0057 #define DATA_REG_OFFSET     6
0058 
0059 #define F71882FG_REG_IN_STATUS      0x12 /* f7188x only */
0060 #define F71882FG_REG_IN_BEEP        0x13 /* f7188x only */
0061 #define F71882FG_REG_IN(nr)     (0x20  + (nr))
0062 #define F71882FG_REG_IN1_HIGH       0x32 /* f7188x only */
0063 
0064 #define F81866_REG_IN_STATUS        0x16 /* F81866 only */
0065 #define F81866_REG_IN_BEEP          0x17 /* F81866 only */
0066 #define F81866_REG_IN1_HIGH     0x3a /* F81866 only */
0067 
0068 #define F71882FG_REG_FAN(nr)        (0xA0 + (16 * (nr)))
0069 #define F71882FG_REG_FAN_TARGET(nr) (0xA2 + (16 * (nr)))
0070 #define F71882FG_REG_FAN_FULL_SPEED(nr) (0xA4 + (16 * (nr)))
0071 #define F71882FG_REG_FAN_STATUS     0x92
0072 #define F71882FG_REG_FAN_BEEP       0x93
0073 
0074 #define F71882FG_REG_TEMP(nr)       (0x70 + 2 * (nr))
0075 #define F71882FG_REG_TEMP_OVT(nr)   (0x80 + 2 * (nr))
0076 #define F71882FG_REG_TEMP_HIGH(nr)  (0x81 + 2 * (nr))
0077 #define F71882FG_REG_TEMP_STATUS    0x62
0078 #define F71882FG_REG_TEMP_BEEP      0x63
0079 #define F71882FG_REG_TEMP_CONFIG    0x69
0080 #define F71882FG_REG_TEMP_HYST(nr)  (0x6C + (nr))
0081 #define F71882FG_REG_TEMP_TYPE      0x6B
0082 #define F71882FG_REG_TEMP_DIODE_OPEN    0x6F
0083 
0084 #define F71882FG_REG_PWM(nr)        (0xA3 + (16 * (nr)))
0085 #define F71882FG_REG_PWM_TYPE       0x94
0086 #define F71882FG_REG_PWM_ENABLE     0x96
0087 
0088 #define F71882FG_REG_FAN_HYST(nr)   (0x98 + (nr))
0089 
0090 #define F71882FG_REG_FAN_FAULT_T    0x9F
0091 #define F71882FG_FAN_NEG_TEMP_EN    0x20
0092 #define F71882FG_FAN_PROG_SEL       0x80
0093 
0094 #define F71882FG_REG_POINT_PWM(pwm, point)  (0xAA + (point) + (16 * (pwm)))
0095 #define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm)))
0096 #define F71882FG_REG_POINT_MAPPING(nr)      (0xAF + 16 * (nr))
0097 
0098 #define F71882FG_REG_START      0x01
0099 
0100 #define F71882FG_MAX_INS        11
0101 
0102 #define FAN_MIN_DETECT          366 /* Lowest detectable fanspeed */
0103 
0104 static unsigned short force_id;
0105 module_param(force_id, ushort, 0);
0106 MODULE_PARM_DESC(force_id, "Override the detected device ID");
0107 
0108 enum chips { f71808e, f71808a, f71858fg, f71862fg, f71868a, f71869, f71869a,
0109     f71882fg, f71889fg, f71889ed, f71889a, f8000, f81768d, f81865f,
0110     f81866a};
0111 
0112 static const char *const f71882fg_names[] = {
0113     "f71808e",
0114     "f71808a",
0115     "f71858fg",
0116     "f71862fg",
0117     "f71868a",
0118     "f71869", /* Both f71869f and f71869e, reg. compatible and same id */
0119     "f71869a",
0120     "f71882fg",
0121     "f71889fg", /* f81801u too, same id */
0122     "f71889ed",
0123     "f71889a",
0124     "f8000",
0125     "f81768d",
0126     "f81865f",
0127     "f81866a",
0128 };
0129 
0130 static const char f71882fg_has_in[][F71882FG_MAX_INS] = {
0131     [f71808e]   = { 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0 },
0132     [f71808a]   = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0 },
0133     [f71858fg]  = { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
0134     [f71862fg]  = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
0135     [f71868a]   = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
0136     [f71869]    = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
0137     [f71869a]   = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
0138     [f71882fg]  = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
0139     [f71889fg]  = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
0140     [f71889ed]  = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
0141     [f71889a]   = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
0142     [f8000]     = { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
0143     [f81768d]   = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
0144     [f81865f]   = { 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
0145     [f81866a]   = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 },
0146 };
0147 
0148 static const char f71882fg_has_in1_alarm[] = {
0149     [f71808e]   = 0,
0150     [f71808a]   = 0,
0151     [f71858fg]  = 0,
0152     [f71862fg]  = 0,
0153     [f71868a]   = 0,
0154     [f71869]    = 0,
0155     [f71869a]   = 0,
0156     [f71882fg]  = 1,
0157     [f71889fg]  = 1,
0158     [f71889ed]  = 1,
0159     [f71889a]   = 1,
0160     [f8000]     = 0,
0161     [f81768d]   = 1,
0162     [f81865f]   = 1,
0163     [f81866a]   = 1,
0164 };
0165 
0166 static const char f71882fg_fan_has_beep[] = {
0167     [f71808e]   = 0,
0168     [f71808a]   = 0,
0169     [f71858fg]  = 0,
0170     [f71862fg]  = 1,
0171     [f71868a]   = 1,
0172     [f71869]    = 1,
0173     [f71869a]   = 1,
0174     [f71882fg]  = 1,
0175     [f71889fg]  = 1,
0176     [f71889ed]  = 1,
0177     [f71889a]   = 1,
0178     [f8000]     = 0,
0179     [f81768d]   = 1,
0180     [f81865f]   = 1,
0181     [f81866a]   = 1,
0182 };
0183 
0184 static const char f71882fg_nr_fans[] = {
0185     [f71808e]   = 3,
0186     [f71808a]   = 2, /* +1 fan which is monitor + simple pwm only */
0187     [f71858fg]  = 3,
0188     [f71862fg]  = 3,
0189     [f71868a]   = 3,
0190     [f71869]    = 3,
0191     [f71869a]   = 3,
0192     [f71882fg]  = 4,
0193     [f71889fg]  = 3,
0194     [f71889ed]  = 3,
0195     [f71889a]   = 3,
0196     [f8000]     = 3, /* +1 fan which is monitor only */
0197     [f81768d]   = 3,
0198     [f81865f]   = 2,
0199     [f81866a]   = 3,
0200 };
0201 
0202 static const char f71882fg_temp_has_beep[] = {
0203     [f71808e]   = 0,
0204     [f71808a]   = 1,
0205     [f71858fg]  = 0,
0206     [f71862fg]  = 1,
0207     [f71868a]   = 1,
0208     [f71869]    = 1,
0209     [f71869a]   = 1,
0210     [f71882fg]  = 1,
0211     [f71889fg]  = 1,
0212     [f71889ed]  = 1,
0213     [f71889a]   = 1,
0214     [f8000]     = 0,
0215     [f81768d]   = 1,
0216     [f81865f]   = 1,
0217     [f81866a]   = 1,
0218 };
0219 
0220 static const char f71882fg_nr_temps[] = {
0221     [f71808e]   = 2,
0222     [f71808a]   = 2,
0223     [f71858fg]  = 3,
0224     [f71862fg]  = 3,
0225     [f71868a]   = 3,
0226     [f71869]    = 3,
0227     [f71869a]   = 3,
0228     [f71882fg]  = 3,
0229     [f71889fg]  = 3,
0230     [f71889ed]  = 3,
0231     [f71889a]   = 3,
0232     [f8000]     = 3,
0233     [f81768d]   = 3,
0234     [f81865f]   = 2,
0235     [f81866a]   = 3,
0236 };
0237 
0238 static struct platform_device *f71882fg_pdev;
0239 
0240 /* Super-I/O Function prototypes */
0241 static inline int superio_inb(int base, int reg);
0242 static inline int superio_inw(int base, int reg);
0243 static inline int superio_enter(int base);
0244 static inline void superio_select(int base, int ld);
0245 static inline void superio_exit(int base);
0246 
0247 struct f71882fg_sio_data {
0248     enum chips type;
0249 };
0250 
0251 struct f71882fg_data {
0252     unsigned short addr;
0253     enum chips type;
0254     struct device *hwmon_dev;
0255 
0256     struct mutex update_lock;
0257     int temp_start;         /* temp numbering start (0 or 1) */
0258     bool valid;         /* true if following fields are valid */
0259     char auto_point_temp_signed;
0260     unsigned long last_updated; /* In jiffies */
0261     unsigned long last_limits;  /* In jiffies */
0262 
0263     /* Register Values */
0264     u8  in[F71882FG_MAX_INS];
0265     u8  in1_max;
0266     u8  in_status;
0267     u8  in_beep;
0268     u16 fan[4];
0269     u16 fan_target[4];
0270     u16 fan_full_speed[4];
0271     u8  fan_status;
0272     u8  fan_beep;
0273     /*
0274      * Note: all models have max 3 temperature channels, but on some
0275      * they are addressed as 0-2 and on others as 1-3, so for coding
0276      * convenience we reserve space for 4 channels
0277      */
0278     u16 temp[4];
0279     u8  temp_ovt[4];
0280     u8  temp_high[4];
0281     u8  temp_hyst[2]; /* 2 hysts stored per reg */
0282     u8  temp_type[4];
0283     u8  temp_status;
0284     u8  temp_beep;
0285     u8  temp_diode_open;
0286     u8  temp_config;
0287     u8  pwm[4];
0288     u8  pwm_enable;
0289     u8  pwm_auto_point_hyst[2];
0290     u8  pwm_auto_point_mapping[4];
0291     u8  pwm_auto_point_pwm[4][5];
0292     s8  pwm_auto_point_temp[4][4];
0293 };
0294 
0295 /* Sysfs in */
0296 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
0297     char *buf);
0298 static ssize_t show_in_max(struct device *dev, struct device_attribute
0299     *devattr, char *buf);
0300 static ssize_t store_in_max(struct device *dev, struct device_attribute
0301     *devattr, const char *buf, size_t count);
0302 static ssize_t show_in_beep(struct device *dev, struct device_attribute
0303     *devattr, char *buf);
0304 static ssize_t store_in_beep(struct device *dev, struct device_attribute
0305     *devattr, const char *buf, size_t count);
0306 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
0307     *devattr, char *buf);
0308 /* Sysfs Fan */
0309 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
0310     char *buf);
0311 static ssize_t show_fan_full_speed(struct device *dev,
0312     struct device_attribute *devattr, char *buf);
0313 static ssize_t store_fan_full_speed(struct device *dev,
0314     struct device_attribute *devattr, const char *buf, size_t count);
0315 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
0316     *devattr, char *buf);
0317 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
0318     *devattr, const char *buf, size_t count);
0319 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
0320     *devattr, char *buf);
0321 /* Sysfs Temp */
0322 static ssize_t show_temp(struct device *dev, struct device_attribute
0323     *devattr, char *buf);
0324 static ssize_t show_temp_max(struct device *dev, struct device_attribute
0325     *devattr, char *buf);
0326 static ssize_t store_temp_max(struct device *dev, struct device_attribute
0327     *devattr, const char *buf, size_t count);
0328 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
0329     *devattr, char *buf);
0330 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
0331     *devattr, const char *buf, size_t count);
0332 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
0333     *devattr, char *buf);
0334 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
0335     *devattr, const char *buf, size_t count);
0336 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
0337     *devattr, char *buf);
0338 static ssize_t show_temp_type(struct device *dev, struct device_attribute
0339     *devattr, char *buf);
0340 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
0341     *devattr, char *buf);
0342 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
0343     *devattr, const char *buf, size_t count);
0344 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
0345     *devattr, char *buf);
0346 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
0347     *devattr, char *buf);
0348 /* PWM and Auto point control */
0349 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
0350     char *buf);
0351 static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
0352     const char *buf, size_t count);
0353 static ssize_t show_simple_pwm(struct device *dev,
0354     struct device_attribute *devattr, char *buf);
0355 static ssize_t store_simple_pwm(struct device *dev,
0356     struct device_attribute *devattr, const char *buf, size_t count);
0357 static ssize_t show_pwm_enable(struct device *dev,
0358     struct device_attribute *devattr, char *buf);
0359 static ssize_t store_pwm_enable(struct device *dev,
0360     struct device_attribute *devattr, const char *buf, size_t count);
0361 static ssize_t show_pwm_interpolate(struct device *dev,
0362     struct device_attribute *devattr, char *buf);
0363 static ssize_t store_pwm_interpolate(struct device *dev,
0364     struct device_attribute *devattr, const char *buf, size_t count);
0365 static ssize_t show_pwm_auto_point_channel(struct device *dev,
0366     struct device_attribute *devattr, char *buf);
0367 static ssize_t store_pwm_auto_point_channel(struct device *dev,
0368     struct device_attribute *devattr, const char *buf, size_t count);
0369 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
0370     struct device_attribute *devattr, char *buf);
0371 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
0372     struct device_attribute *devattr, const char *buf, size_t count);
0373 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
0374     struct device_attribute *devattr, char *buf);
0375 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
0376     struct device_attribute *devattr, const char *buf, size_t count);
0377 static ssize_t show_pwm_auto_point_temp(struct device *dev,
0378     struct device_attribute *devattr, char *buf);
0379 static ssize_t store_pwm_auto_point_temp(struct device *dev,
0380     struct device_attribute *devattr, const char *buf, size_t count);
0381 /* Sysfs misc */
0382 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
0383     char *buf);
0384 
0385 static int f71882fg_probe(struct platform_device *pdev);
0386 static int f71882fg_remove(struct platform_device *pdev);
0387 
0388 static struct platform_driver f71882fg_driver = {
0389     .driver = {
0390         .name   = DRVNAME,
0391     },
0392     .probe      = f71882fg_probe,
0393     .remove     = f71882fg_remove,
0394 };
0395 
0396 static DEVICE_ATTR_RO(name);
0397 
0398 /*
0399  * Temp attr for the f71858fg, the f71858fg is special as it has its
0400  * temperature indexes start at 0 (the others start at 1)
0401  */
0402 static struct sensor_device_attribute_2 f71858fg_temp_attr[] = {
0403     SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
0404     SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
0405         store_temp_max, 0, 0),
0406     SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
0407         store_temp_max_hyst, 0, 0),
0408     SENSOR_ATTR_2(temp1_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 0),
0409     SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
0410         store_temp_crit, 0, 0),
0411     SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
0412         0, 0),
0413     SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 4),
0414     SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
0415     SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
0416     SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
0417         store_temp_max, 0, 1),
0418     SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
0419         store_temp_max_hyst, 0, 1),
0420     SENSOR_ATTR_2(temp2_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
0421     SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
0422         store_temp_crit, 0, 1),
0423     SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
0424         0, 1),
0425     SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
0426     SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
0427     SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
0428     SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
0429         store_temp_max, 0, 2),
0430     SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
0431         store_temp_max_hyst, 0, 2),
0432     SENSOR_ATTR_2(temp3_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
0433     SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
0434         store_temp_crit, 0, 2),
0435     SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
0436         0, 2),
0437     SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
0438     SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
0439 };
0440 
0441 /* Temp attr for the standard models */
0442 static struct sensor_device_attribute_2 fxxxx_temp_attr[3][9] = { {
0443     SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1),
0444     SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
0445         store_temp_max, 0, 1),
0446     SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
0447         store_temp_max_hyst, 0, 1),
0448     /*
0449      * Should really be temp1_max_alarm, but older versions did not handle
0450      * the max and crit alarms separately and lm_sensors v2 depends on the
0451      * presence of temp#_alarm files. The same goes for temp2/3 _alarm.
0452      */
0453     SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
0454     SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
0455         store_temp_crit, 0, 1),
0456     SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
0457         0, 1),
0458     SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
0459     SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1),
0460     SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
0461 }, {
0462     SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2),
0463     SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
0464         store_temp_max, 0, 2),
0465     SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
0466         store_temp_max_hyst, 0, 2),
0467     /* Should be temp2_max_alarm, see temp1_alarm note */
0468     SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
0469     SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
0470         store_temp_crit, 0, 2),
0471     SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
0472         0, 2),
0473     SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
0474     SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2),
0475     SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
0476 }, {
0477     SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3),
0478     SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
0479         store_temp_max, 0, 3),
0480     SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
0481         store_temp_max_hyst, 0, 3),
0482     /* Should be temp3_max_alarm, see temp1_alarm note */
0483     SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3),
0484     SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
0485         store_temp_crit, 0, 3),
0486     SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
0487         0, 3),
0488     SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 7),
0489     SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3),
0490     SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3),
0491 } };
0492 
0493 /* Temp attr for models which can beep on temp alarm */
0494 static struct sensor_device_attribute_2 fxxxx_temp_beep_attr[3][2] = { {
0495     SENSOR_ATTR_2(temp1_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0496         store_temp_beep, 0, 1),
0497     SENSOR_ATTR_2(temp1_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0498         store_temp_beep, 0, 5),
0499 }, {
0500     SENSOR_ATTR_2(temp2_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0501         store_temp_beep, 0, 2),
0502     SENSOR_ATTR_2(temp2_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0503         store_temp_beep, 0, 6),
0504 }, {
0505     SENSOR_ATTR_2(temp3_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0506         store_temp_beep, 0, 3),
0507     SENSOR_ATTR_2(temp3_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0508         store_temp_beep, 0, 7),
0509 } };
0510 
0511 static struct sensor_device_attribute_2 f81866_temp_beep_attr[3][2] = { {
0512     SENSOR_ATTR_2(temp1_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0513         store_temp_beep, 0, 0),
0514     SENSOR_ATTR_2(temp1_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0515         store_temp_beep, 0, 4),
0516 }, {
0517     SENSOR_ATTR_2(temp2_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0518         store_temp_beep, 0, 1),
0519     SENSOR_ATTR_2(temp2_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0520         store_temp_beep, 0, 5),
0521 }, {
0522     SENSOR_ATTR_2(temp3_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0523         store_temp_beep, 0, 2),
0524     SENSOR_ATTR_2(temp3_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
0525         store_temp_beep, 0, 6),
0526 } };
0527 
0528 /*
0529  * Temp attr for the f8000
0530  * Note on the f8000 temp_ovt (crit) is used as max, and temp_high (max)
0531  * is used as hysteresis value to clear alarms
0532  * Also like the f71858fg its temperature indexes start at 0
0533  */
0534 static struct sensor_device_attribute_2 f8000_temp_attr[] = {
0535     SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
0536     SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_crit,
0537         store_temp_crit, 0, 0),
0538     SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
0539         store_temp_max, 0, 0),
0540     SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 4),
0541     SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
0542     SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
0543     SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_crit,
0544         store_temp_crit, 0, 1),
0545     SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
0546         store_temp_max, 0, 1),
0547     SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
0548     SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
0549     SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
0550     SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_crit,
0551         store_temp_crit, 0, 2),
0552     SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
0553         store_temp_max, 0, 2),
0554     SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
0555     SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
0556 };
0557 
0558 /* in attr for all models */
0559 static struct sensor_device_attribute_2 fxxxx_in_attr[] = {
0560     SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
0561     SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
0562     SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
0563     SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
0564     SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
0565     SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
0566     SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
0567     SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
0568     SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
0569     SENSOR_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 0, 9),
0570     SENSOR_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 0, 10),
0571 };
0572 
0573 /* For models with in1 alarm capability */
0574 static struct sensor_device_attribute_2 fxxxx_in1_alarm_attr[] = {
0575     SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
0576         0, 1),
0577     SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
0578         0, 1),
0579     SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
0580 };
0581 
0582 /* Fan / PWM attr common to all models */
0583 static struct sensor_device_attribute_2 fxxxx_fan_attr[4][6] = { {
0584     SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
0585     SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
0586               show_fan_full_speed,
0587               store_fan_full_speed, 0, 0),
0588     SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
0589     SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
0590     SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
0591               store_pwm_enable, 0, 0),
0592     SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
0593               show_pwm_interpolate, store_pwm_interpolate, 0, 0),
0594 }, {
0595     SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
0596     SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
0597               show_fan_full_speed,
0598               store_fan_full_speed, 0, 1),
0599     SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
0600     SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
0601     SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
0602               store_pwm_enable, 0, 1),
0603     SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
0604               show_pwm_interpolate, store_pwm_interpolate, 0, 1),
0605 }, {
0606     SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
0607     SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
0608               show_fan_full_speed,
0609               store_fan_full_speed, 0, 2),
0610     SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
0611     SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
0612     SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
0613               store_pwm_enable, 0, 2),
0614     SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
0615               show_pwm_interpolate, store_pwm_interpolate, 0, 2),
0616 }, {
0617     SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
0618     SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
0619               show_fan_full_speed,
0620               store_fan_full_speed, 0, 3),
0621     SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
0622     SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
0623     SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
0624               store_pwm_enable, 0, 3),
0625     SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
0626               show_pwm_interpolate, store_pwm_interpolate, 0, 3),
0627 } };
0628 
0629 /* Attr for the third fan of the f71808a, which only has manual pwm */
0630 static struct sensor_device_attribute_2 f71808a_fan3_attr[] = {
0631     SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
0632     SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
0633     SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR,
0634               show_simple_pwm, store_simple_pwm, 0, 2),
0635 };
0636 
0637 /* Attr for models which can beep on Fan alarm */
0638 static struct sensor_device_attribute_2 fxxxx_fan_beep_attr[] = {
0639     SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
0640         store_fan_beep, 0, 0),
0641     SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
0642         store_fan_beep, 0, 1),
0643     SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
0644         store_fan_beep, 0, 2),
0645     SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
0646         store_fan_beep, 0, 3),
0647 };
0648 
0649 /*
0650  * PWM attr for the f71862fg, fewer pwms and fewer zones per pwm than the
0651  * standard models
0652  */
0653 static struct sensor_device_attribute_2 f71862fg_auto_pwm_attr[3][7] = { {
0654     SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
0655               show_pwm_auto_point_channel,
0656               store_pwm_auto_point_channel, 0, 0),
0657     SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
0658               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0659               1, 0),
0660     SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
0661               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0662               4, 0),
0663     SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
0664               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0665               0, 0),
0666     SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
0667               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0668               3, 0),
0669     SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0670               show_pwm_auto_point_temp_hyst,
0671               store_pwm_auto_point_temp_hyst,
0672               0, 0),
0673     SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
0674               show_pwm_auto_point_temp_hyst, NULL, 3, 0),
0675 }, {
0676     SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
0677               show_pwm_auto_point_channel,
0678               store_pwm_auto_point_channel, 0, 1),
0679     SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
0680               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0681               1, 1),
0682     SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
0683               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0684               4, 1),
0685     SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
0686               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0687               0, 1),
0688     SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
0689               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0690               3, 1),
0691     SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0692               show_pwm_auto_point_temp_hyst,
0693               store_pwm_auto_point_temp_hyst,
0694               0, 1),
0695     SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
0696               show_pwm_auto_point_temp_hyst, NULL, 3, 1),
0697 }, {
0698     SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
0699               show_pwm_auto_point_channel,
0700               store_pwm_auto_point_channel, 0, 2),
0701     SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
0702               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0703               1, 2),
0704     SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
0705               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0706               4, 2),
0707     SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
0708               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0709               0, 2),
0710     SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
0711               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0712               3, 2),
0713     SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0714               show_pwm_auto_point_temp_hyst,
0715               store_pwm_auto_point_temp_hyst,
0716               0, 2),
0717     SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
0718               show_pwm_auto_point_temp_hyst, NULL, 3, 2),
0719 } };
0720 
0721 /*
0722  * PWM attr for the f71808e/f71869, almost identical to the f71862fg, but the
0723  * pwm setting when the temperature is above the pwmX_auto_point1_temp can be
0724  * programmed instead of being hardcoded to 0xff
0725  */
0726 static struct sensor_device_attribute_2 f71869_auto_pwm_attr[3][8] = { {
0727     SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
0728               show_pwm_auto_point_channel,
0729               store_pwm_auto_point_channel, 0, 0),
0730     SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
0731               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0732               0, 0),
0733     SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
0734               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0735               1, 0),
0736     SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
0737               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0738               4, 0),
0739     SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
0740               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0741               0, 0),
0742     SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
0743               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0744               3, 0),
0745     SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0746               show_pwm_auto_point_temp_hyst,
0747               store_pwm_auto_point_temp_hyst,
0748               0, 0),
0749     SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
0750               show_pwm_auto_point_temp_hyst, NULL, 3, 0),
0751 }, {
0752     SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
0753               show_pwm_auto_point_channel,
0754               store_pwm_auto_point_channel, 0, 1),
0755     SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
0756               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0757               0, 1),
0758     SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
0759               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0760               1, 1),
0761     SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
0762               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0763               4, 1),
0764     SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
0765               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0766               0, 1),
0767     SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
0768               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0769               3, 1),
0770     SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0771               show_pwm_auto_point_temp_hyst,
0772               store_pwm_auto_point_temp_hyst,
0773               0, 1),
0774     SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
0775               show_pwm_auto_point_temp_hyst, NULL, 3, 1),
0776 }, {
0777     SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
0778               show_pwm_auto_point_channel,
0779               store_pwm_auto_point_channel, 0, 2),
0780     SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
0781               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0782               0, 2),
0783     SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
0784               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0785               1, 2),
0786     SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
0787               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0788               4, 2),
0789     SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
0790               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0791               0, 2),
0792     SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
0793               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0794               3, 2),
0795     SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0796               show_pwm_auto_point_temp_hyst,
0797               store_pwm_auto_point_temp_hyst,
0798               0, 2),
0799     SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
0800               show_pwm_auto_point_temp_hyst, NULL, 3, 2),
0801 } };
0802 
0803 /* PWM attr for the standard models */
0804 static struct sensor_device_attribute_2 fxxxx_auto_pwm_attr[4][14] = { {
0805     SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
0806               show_pwm_auto_point_channel,
0807               store_pwm_auto_point_channel, 0, 0),
0808     SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
0809               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0810               0, 0),
0811     SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
0812               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0813               1, 0),
0814     SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
0815               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0816               2, 0),
0817     SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
0818               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0819               3, 0),
0820     SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
0821               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0822               4, 0),
0823     SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
0824               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0825               0, 0),
0826     SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
0827               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0828               1, 0),
0829     SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
0830               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0831               2, 0),
0832     SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
0833               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0834               3, 0),
0835     SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0836               show_pwm_auto_point_temp_hyst,
0837               store_pwm_auto_point_temp_hyst,
0838               0, 0),
0839     SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
0840               show_pwm_auto_point_temp_hyst, NULL, 1, 0),
0841     SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
0842               show_pwm_auto_point_temp_hyst, NULL, 2, 0),
0843     SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
0844               show_pwm_auto_point_temp_hyst, NULL, 3, 0),
0845 }, {
0846     SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
0847               show_pwm_auto_point_channel,
0848               store_pwm_auto_point_channel, 0, 1),
0849     SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
0850               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0851               0, 1),
0852     SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
0853               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0854               1, 1),
0855     SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
0856               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0857               2, 1),
0858     SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
0859               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0860               3, 1),
0861     SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
0862               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0863               4, 1),
0864     SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
0865               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0866               0, 1),
0867     SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
0868               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0869               1, 1),
0870     SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
0871               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0872               2, 1),
0873     SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
0874               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0875               3, 1),
0876     SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0877               show_pwm_auto_point_temp_hyst,
0878               store_pwm_auto_point_temp_hyst,
0879               0, 1),
0880     SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
0881               show_pwm_auto_point_temp_hyst, NULL, 1, 1),
0882     SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
0883               show_pwm_auto_point_temp_hyst, NULL, 2, 1),
0884     SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
0885               show_pwm_auto_point_temp_hyst, NULL, 3, 1),
0886 }, {
0887     SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
0888               show_pwm_auto_point_channel,
0889               store_pwm_auto_point_channel, 0, 2),
0890     SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
0891               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0892               0, 2),
0893     SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
0894               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0895               1, 2),
0896     SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
0897               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0898               2, 2),
0899     SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
0900               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0901               3, 2),
0902     SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
0903               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0904               4, 2),
0905     SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
0906               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0907               0, 2),
0908     SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
0909               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0910               1, 2),
0911     SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
0912               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0913               2, 2),
0914     SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
0915               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0916               3, 2),
0917     SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0918               show_pwm_auto_point_temp_hyst,
0919               store_pwm_auto_point_temp_hyst,
0920               0, 2),
0921     SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
0922               show_pwm_auto_point_temp_hyst, NULL, 1, 2),
0923     SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
0924               show_pwm_auto_point_temp_hyst, NULL, 2, 2),
0925     SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
0926               show_pwm_auto_point_temp_hyst, NULL, 3, 2),
0927 }, {
0928     SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
0929               show_pwm_auto_point_channel,
0930               store_pwm_auto_point_channel, 0, 3),
0931     SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
0932               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0933               0, 3),
0934     SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
0935               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0936               1, 3),
0937     SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
0938               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0939               2, 3),
0940     SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
0941               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0942               3, 3),
0943     SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
0944               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0945               4, 3),
0946     SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
0947               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0948               0, 3),
0949     SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
0950               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0951               1, 3),
0952     SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
0953               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0954               2, 3),
0955     SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
0956               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
0957               3, 3),
0958     SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
0959               show_pwm_auto_point_temp_hyst,
0960               store_pwm_auto_point_temp_hyst,
0961               0, 3),
0962     SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
0963               show_pwm_auto_point_temp_hyst, NULL, 1, 3),
0964     SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
0965               show_pwm_auto_point_temp_hyst, NULL, 2, 3),
0966     SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
0967               show_pwm_auto_point_temp_hyst, NULL, 3, 3),
0968 } };
0969 
0970 /* Fan attr specific to the f8000 (4th fan input can only measure speed) */
0971 static struct sensor_device_attribute_2 f8000_fan_attr[] = {
0972     SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
0973 };
0974 
0975 /*
0976  * PWM attr for the f8000, zones mapped to temp instead of to pwm!
0977  * Also the register block at offset A0 maps to TEMP1 (so our temp2, as the
0978  * F8000 starts counting temps at 0), B0 maps the TEMP2 and C0 maps to TEMP0
0979  */
0980 static struct sensor_device_attribute_2 f8000_auto_pwm_attr[3][14] = { {
0981     SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
0982               show_pwm_auto_point_channel,
0983               store_pwm_auto_point_channel, 0, 0),
0984     SENSOR_ATTR_2(temp1_auto_point1_pwm, S_IRUGO|S_IWUSR,
0985               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0986               0, 2),
0987     SENSOR_ATTR_2(temp1_auto_point2_pwm, S_IRUGO|S_IWUSR,
0988               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0989               1, 2),
0990     SENSOR_ATTR_2(temp1_auto_point3_pwm, S_IRUGO|S_IWUSR,
0991               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0992               2, 2),
0993     SENSOR_ATTR_2(temp1_auto_point4_pwm, S_IRUGO|S_IWUSR,
0994               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0995               3, 2),
0996     SENSOR_ATTR_2(temp1_auto_point5_pwm, S_IRUGO|S_IWUSR,
0997               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
0998               4, 2),
0999     SENSOR_ATTR_2(temp1_auto_point1_temp, S_IRUGO|S_IWUSR,
1000               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1001               0, 2),
1002     SENSOR_ATTR_2(temp1_auto_point2_temp, S_IRUGO|S_IWUSR,
1003               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1004               1, 2),
1005     SENSOR_ATTR_2(temp1_auto_point3_temp, S_IRUGO|S_IWUSR,
1006               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1007               2, 2),
1008     SENSOR_ATTR_2(temp1_auto_point4_temp, S_IRUGO|S_IWUSR,
1009               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1010               3, 2),
1011     SENSOR_ATTR_2(temp1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
1012               show_pwm_auto_point_temp_hyst,
1013               store_pwm_auto_point_temp_hyst,
1014               0, 2),
1015     SENSOR_ATTR_2(temp1_auto_point2_temp_hyst, S_IRUGO,
1016               show_pwm_auto_point_temp_hyst, NULL, 1, 2),
1017     SENSOR_ATTR_2(temp1_auto_point3_temp_hyst, S_IRUGO,
1018               show_pwm_auto_point_temp_hyst, NULL, 2, 2),
1019     SENSOR_ATTR_2(temp1_auto_point4_temp_hyst, S_IRUGO,
1020               show_pwm_auto_point_temp_hyst, NULL, 3, 2),
1021 }, {
1022     SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
1023               show_pwm_auto_point_channel,
1024               store_pwm_auto_point_channel, 0, 1),
1025     SENSOR_ATTR_2(temp2_auto_point1_pwm, S_IRUGO|S_IWUSR,
1026               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1027               0, 0),
1028     SENSOR_ATTR_2(temp2_auto_point2_pwm, S_IRUGO|S_IWUSR,
1029               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1030               1, 0),
1031     SENSOR_ATTR_2(temp2_auto_point3_pwm, S_IRUGO|S_IWUSR,
1032               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1033               2, 0),
1034     SENSOR_ATTR_2(temp2_auto_point4_pwm, S_IRUGO|S_IWUSR,
1035               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1036               3, 0),
1037     SENSOR_ATTR_2(temp2_auto_point5_pwm, S_IRUGO|S_IWUSR,
1038               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1039               4, 0),
1040     SENSOR_ATTR_2(temp2_auto_point1_temp, S_IRUGO|S_IWUSR,
1041               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1042               0, 0),
1043     SENSOR_ATTR_2(temp2_auto_point2_temp, S_IRUGO|S_IWUSR,
1044               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1045               1, 0),
1046     SENSOR_ATTR_2(temp2_auto_point3_temp, S_IRUGO|S_IWUSR,
1047               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1048               2, 0),
1049     SENSOR_ATTR_2(temp2_auto_point4_temp, S_IRUGO|S_IWUSR,
1050               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1051               3, 0),
1052     SENSOR_ATTR_2(temp2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
1053               show_pwm_auto_point_temp_hyst,
1054               store_pwm_auto_point_temp_hyst,
1055               0, 0),
1056     SENSOR_ATTR_2(temp2_auto_point2_temp_hyst, S_IRUGO,
1057               show_pwm_auto_point_temp_hyst, NULL, 1, 0),
1058     SENSOR_ATTR_2(temp2_auto_point3_temp_hyst, S_IRUGO,
1059               show_pwm_auto_point_temp_hyst, NULL, 2, 0),
1060     SENSOR_ATTR_2(temp2_auto_point4_temp_hyst, S_IRUGO,
1061               show_pwm_auto_point_temp_hyst, NULL, 3, 0),
1062 }, {
1063     SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
1064               show_pwm_auto_point_channel,
1065               store_pwm_auto_point_channel, 0, 2),
1066     SENSOR_ATTR_2(temp3_auto_point1_pwm, S_IRUGO|S_IWUSR,
1067               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1068               0, 1),
1069     SENSOR_ATTR_2(temp3_auto_point2_pwm, S_IRUGO|S_IWUSR,
1070               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1071               1, 1),
1072     SENSOR_ATTR_2(temp3_auto_point3_pwm, S_IRUGO|S_IWUSR,
1073               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1074               2, 1),
1075     SENSOR_ATTR_2(temp3_auto_point4_pwm, S_IRUGO|S_IWUSR,
1076               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1077               3, 1),
1078     SENSOR_ATTR_2(temp3_auto_point5_pwm, S_IRUGO|S_IWUSR,
1079               show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1080               4, 1),
1081     SENSOR_ATTR_2(temp3_auto_point1_temp, S_IRUGO|S_IWUSR,
1082               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1083               0, 1),
1084     SENSOR_ATTR_2(temp3_auto_point2_temp, S_IRUGO|S_IWUSR,
1085               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1086               1, 1),
1087     SENSOR_ATTR_2(temp3_auto_point3_temp, S_IRUGO|S_IWUSR,
1088               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1089               2, 1),
1090     SENSOR_ATTR_2(temp3_auto_point4_temp, S_IRUGO|S_IWUSR,
1091               show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1092               3, 1),
1093     SENSOR_ATTR_2(temp3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
1094               show_pwm_auto_point_temp_hyst,
1095               store_pwm_auto_point_temp_hyst,
1096               0, 1),
1097     SENSOR_ATTR_2(temp3_auto_point2_temp_hyst, S_IRUGO,
1098               show_pwm_auto_point_temp_hyst, NULL, 1, 1),
1099     SENSOR_ATTR_2(temp3_auto_point3_temp_hyst, S_IRUGO,
1100               show_pwm_auto_point_temp_hyst, NULL, 2, 1),
1101     SENSOR_ATTR_2(temp3_auto_point4_temp_hyst, S_IRUGO,
1102               show_pwm_auto_point_temp_hyst, NULL, 3, 1),
1103 } };
1104 
1105 /* Super I/O functions */
1106 static inline int superio_inb(int base, int reg)
1107 {
1108     outb(reg, base);
1109     return inb(base + 1);
1110 }
1111 
1112 static int superio_inw(int base, int reg)
1113 {
1114     int val;
1115     val  = superio_inb(base, reg) << 8;
1116     val |= superio_inb(base, reg + 1);
1117     return val;
1118 }
1119 
1120 static inline int superio_enter(int base)
1121 {
1122     /* Don't step on other drivers' I/O space by accident */
1123     if (!request_muxed_region(base, 2, DRVNAME)) {
1124         pr_err("I/O address 0x%04x already in use\n", base);
1125         return -EBUSY;
1126     }
1127 
1128     /* according to the datasheet the key must be send twice! */
1129     outb(SIO_UNLOCK_KEY, base);
1130     outb(SIO_UNLOCK_KEY, base);
1131 
1132     return 0;
1133 }
1134 
1135 static inline void superio_select(int base, int ld)
1136 {
1137     outb(SIO_REG_LDSEL, base);
1138     outb(ld, base + 1);
1139 }
1140 
1141 static inline void superio_exit(int base)
1142 {
1143     outb(SIO_LOCK_KEY, base);
1144     release_region(base, 2);
1145 }
1146 
1147 static inline int fan_from_reg(u16 reg)
1148 {
1149     return reg ? (1500000 / reg) : 0;
1150 }
1151 
1152 static inline u16 fan_to_reg(int fan)
1153 {
1154     return fan ? (1500000 / fan) : 0;
1155 }
1156 
1157 static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
1158 {
1159     u8 val;
1160 
1161     outb(reg, data->addr + ADDR_REG_OFFSET);
1162     val = inb(data->addr + DATA_REG_OFFSET);
1163 
1164     return val;
1165 }
1166 
1167 static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
1168 {
1169     u16 val;
1170 
1171     val  = f71882fg_read8(data, reg) << 8;
1172     val |= f71882fg_read8(data, reg + 1);
1173 
1174     return val;
1175 }
1176 
1177 static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
1178 {
1179     outb(reg, data->addr + ADDR_REG_OFFSET);
1180     outb(val, data->addr + DATA_REG_OFFSET);
1181 }
1182 
1183 static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
1184 {
1185     f71882fg_write8(data, reg,     val >> 8);
1186     f71882fg_write8(data, reg + 1, val & 0xff);
1187 }
1188 
1189 static u16 f71882fg_read_temp(struct f71882fg_data *data, int nr)
1190 {
1191     if (data->type == f71858fg)
1192         return f71882fg_read16(data, F71882FG_REG_TEMP(nr));
1193     else
1194         return f71882fg_read8(data, F71882FG_REG_TEMP(nr));
1195 }
1196 
1197 static struct f71882fg_data *f71882fg_update_device(struct device *dev)
1198 {
1199     struct f71882fg_data *data = dev_get_drvdata(dev);
1200     int nr_fans = f71882fg_nr_fans[data->type];
1201     int nr_temps = f71882fg_nr_temps[data->type];
1202     int nr, reg, point;
1203 
1204     mutex_lock(&data->update_lock);
1205 
1206     /* Update once every 60 seconds */
1207     if (time_after(jiffies, data->last_limits + 60 * HZ) ||
1208             !data->valid) {
1209         if (f71882fg_has_in1_alarm[data->type]) {
1210             if (data->type == f81866a) {
1211                 data->in1_max =
1212                     f71882fg_read8(data,
1213                                F81866_REG_IN1_HIGH);
1214                 data->in_beep =
1215                     f71882fg_read8(data,
1216                                F81866_REG_IN_BEEP);
1217             } else {
1218                 data->in1_max =
1219                     f71882fg_read8(data,
1220                                F71882FG_REG_IN1_HIGH);
1221                 data->in_beep =
1222                     f71882fg_read8(data,
1223                                F71882FG_REG_IN_BEEP);
1224             }
1225         }
1226 
1227         /* Get High & boundary temps*/
1228         for (nr = data->temp_start; nr < nr_temps + data->temp_start;
1229                                     nr++) {
1230             data->temp_ovt[nr] = f71882fg_read8(data,
1231                         F71882FG_REG_TEMP_OVT(nr));
1232             data->temp_high[nr] = f71882fg_read8(data,
1233                         F71882FG_REG_TEMP_HIGH(nr));
1234         }
1235 
1236         if (data->type != f8000) {
1237             data->temp_hyst[0] = f71882fg_read8(data,
1238                         F71882FG_REG_TEMP_HYST(0));
1239             data->temp_hyst[1] = f71882fg_read8(data,
1240                         F71882FG_REG_TEMP_HYST(1));
1241         }
1242         /* All but the f71858fg / f8000 have this register */
1243         if ((data->type != f71858fg) && (data->type != f8000)) {
1244             reg  = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
1245             data->temp_type[1] = (reg & 0x02) ? 2 : 4;
1246             data->temp_type[2] = (reg & 0x04) ? 2 : 4;
1247             data->temp_type[3] = (reg & 0x08) ? 2 : 4;
1248         }
1249 
1250         if (f71882fg_fan_has_beep[data->type])
1251             data->fan_beep = f71882fg_read8(data,
1252                         F71882FG_REG_FAN_BEEP);
1253 
1254         if (f71882fg_temp_has_beep[data->type])
1255             data->temp_beep = f71882fg_read8(data,
1256                         F71882FG_REG_TEMP_BEEP);
1257 
1258         data->pwm_enable = f71882fg_read8(data,
1259                           F71882FG_REG_PWM_ENABLE);
1260         data->pwm_auto_point_hyst[0] =
1261             f71882fg_read8(data, F71882FG_REG_FAN_HYST(0));
1262         data->pwm_auto_point_hyst[1] =
1263             f71882fg_read8(data, F71882FG_REG_FAN_HYST(1));
1264 
1265         for (nr = 0; nr < nr_fans; nr++) {
1266             data->pwm_auto_point_mapping[nr] =
1267                 f71882fg_read8(data,
1268                        F71882FG_REG_POINT_MAPPING(nr));
1269 
1270             switch (data->type) {
1271             default:
1272                 for (point = 0; point < 5; point++) {
1273                     data->pwm_auto_point_pwm[nr][point] =
1274                         f71882fg_read8(data,
1275                             F71882FG_REG_POINT_PWM
1276                             (nr, point));
1277                 }
1278                 for (point = 0; point < 4; point++) {
1279                     data->pwm_auto_point_temp[nr][point] =
1280                         f71882fg_read8(data,
1281                             F71882FG_REG_POINT_TEMP
1282                             (nr, point));
1283                 }
1284                 break;
1285             case f71808e:
1286             case f71869:
1287                 data->pwm_auto_point_pwm[nr][0] =
1288                     f71882fg_read8(data,
1289                         F71882FG_REG_POINT_PWM(nr, 0));
1290                 fallthrough;
1291             case f71862fg:
1292                 data->pwm_auto_point_pwm[nr][1] =
1293                     f71882fg_read8(data,
1294                         F71882FG_REG_POINT_PWM
1295                         (nr, 1));
1296                 data->pwm_auto_point_pwm[nr][4] =
1297                     f71882fg_read8(data,
1298                         F71882FG_REG_POINT_PWM
1299                         (nr, 4));
1300                 data->pwm_auto_point_temp[nr][0] =
1301                     f71882fg_read8(data,
1302                         F71882FG_REG_POINT_TEMP
1303                         (nr, 0));
1304                 data->pwm_auto_point_temp[nr][3] =
1305                     f71882fg_read8(data,
1306                         F71882FG_REG_POINT_TEMP
1307                         (nr, 3));
1308                 break;
1309             }
1310         }
1311         data->last_limits = jiffies;
1312     }
1313 
1314     /* Update every second */
1315     if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
1316         data->temp_status = f71882fg_read8(data,
1317                         F71882FG_REG_TEMP_STATUS);
1318         data->temp_diode_open = f71882fg_read8(data,
1319                         F71882FG_REG_TEMP_DIODE_OPEN);
1320         for (nr = data->temp_start; nr < nr_temps + data->temp_start;
1321                                     nr++)
1322             data->temp[nr] = f71882fg_read_temp(data, nr);
1323 
1324         data->fan_status = f71882fg_read8(data,
1325                         F71882FG_REG_FAN_STATUS);
1326         for (nr = 0; nr < nr_fans; nr++) {
1327             data->fan[nr] = f71882fg_read16(data,
1328                         F71882FG_REG_FAN(nr));
1329             data->fan_target[nr] =
1330                 f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
1331             data->fan_full_speed[nr] =
1332                 f71882fg_read16(data,
1333                         F71882FG_REG_FAN_FULL_SPEED(nr));
1334             data->pwm[nr] =
1335                 f71882fg_read8(data, F71882FG_REG_PWM(nr));
1336         }
1337         /* Some models have 1 more fan with limited capabilities */
1338         if (data->type == f71808a) {
1339             data->fan[2] = f71882fg_read16(data,
1340                         F71882FG_REG_FAN(2));
1341             data->pwm[2] = f71882fg_read8(data,
1342                             F71882FG_REG_PWM(2));
1343         }
1344         if (data->type == f8000)
1345             data->fan[3] = f71882fg_read16(data,
1346                         F71882FG_REG_FAN(3));
1347 
1348         if (f71882fg_has_in1_alarm[data->type]) {
1349             if (data->type == f81866a)
1350                 data->in_status = f71882fg_read8(data,
1351                         F81866_REG_IN_STATUS);
1352 
1353             else
1354                 data->in_status = f71882fg_read8(data,
1355                         F71882FG_REG_IN_STATUS);
1356         }
1357 
1358         for (nr = 0; nr < F71882FG_MAX_INS; nr++)
1359             if (f71882fg_has_in[data->type][nr])
1360                 data->in[nr] = f71882fg_read8(data,
1361                             F71882FG_REG_IN(nr));
1362 
1363         data->last_updated = jiffies;
1364         data->valid = true;
1365     }
1366 
1367     mutex_unlock(&data->update_lock);
1368 
1369     return data;
1370 }
1371 
1372 /* Sysfs Interface */
1373 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
1374     char *buf)
1375 {
1376     struct f71882fg_data *data = f71882fg_update_device(dev);
1377     int nr = to_sensor_dev_attr_2(devattr)->index;
1378     int speed = fan_from_reg(data->fan[nr]);
1379 
1380     if (speed == FAN_MIN_DETECT)
1381         speed = 0;
1382 
1383     return sprintf(buf, "%d\n", speed);
1384 }
1385 
1386 static ssize_t show_fan_full_speed(struct device *dev,
1387                    struct device_attribute *devattr, char *buf)
1388 {
1389     struct f71882fg_data *data = f71882fg_update_device(dev);
1390     int nr = to_sensor_dev_attr_2(devattr)->index;
1391     int speed = fan_from_reg(data->fan_full_speed[nr]);
1392     return sprintf(buf, "%d\n", speed);
1393 }
1394 
1395 static ssize_t store_fan_full_speed(struct device *dev,
1396                     struct device_attribute *devattr,
1397                     const char *buf, size_t count)
1398 {
1399     struct f71882fg_data *data = dev_get_drvdata(dev);
1400     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1401     long val;
1402 
1403     err = kstrtol(buf, 10, &val);
1404     if (err)
1405         return err;
1406 
1407     val = clamp_val(val, 23, 1500000);
1408     val = fan_to_reg(val);
1409 
1410     mutex_lock(&data->update_lock);
1411     f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
1412     data->fan_full_speed[nr] = val;
1413     mutex_unlock(&data->update_lock);
1414 
1415     return count;
1416 }
1417 
1418 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
1419     *devattr, char *buf)
1420 {
1421     struct f71882fg_data *data = f71882fg_update_device(dev);
1422     int nr = to_sensor_dev_attr_2(devattr)->index;
1423 
1424     if (data->fan_beep & (1 << nr))
1425         return sprintf(buf, "1\n");
1426     else
1427         return sprintf(buf, "0\n");
1428 }
1429 
1430 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
1431     *devattr, const char *buf, size_t count)
1432 {
1433     struct f71882fg_data *data = dev_get_drvdata(dev);
1434     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1435     unsigned long val;
1436 
1437     err = kstrtoul(buf, 10, &val);
1438     if (err)
1439         return err;
1440 
1441     mutex_lock(&data->update_lock);
1442     data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
1443     if (val)
1444         data->fan_beep |= 1 << nr;
1445     else
1446         data->fan_beep &= ~(1 << nr);
1447 
1448     f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
1449     mutex_unlock(&data->update_lock);
1450 
1451     return count;
1452 }
1453 
1454 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
1455     *devattr, char *buf)
1456 {
1457     struct f71882fg_data *data = f71882fg_update_device(dev);
1458     int nr = to_sensor_dev_attr_2(devattr)->index;
1459 
1460     if (data->fan_status & (1 << nr))
1461         return sprintf(buf, "1\n");
1462     else
1463         return sprintf(buf, "0\n");
1464 }
1465 
1466 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
1467     char *buf)
1468 {
1469     struct f71882fg_data *data = f71882fg_update_device(dev);
1470     int nr = to_sensor_dev_attr_2(devattr)->index;
1471 
1472     return sprintf(buf, "%d\n", data->in[nr] * 8);
1473 }
1474 
1475 static ssize_t show_in_max(struct device *dev, struct device_attribute
1476     *devattr, char *buf)
1477 {
1478     struct f71882fg_data *data = f71882fg_update_device(dev);
1479 
1480     return sprintf(buf, "%d\n", data->in1_max * 8);
1481 }
1482 
1483 static ssize_t store_in_max(struct device *dev, struct device_attribute
1484     *devattr, const char *buf, size_t count)
1485 {
1486     struct f71882fg_data *data = dev_get_drvdata(dev);
1487     int err;
1488     long val;
1489 
1490     err = kstrtol(buf, 10, &val);
1491     if (err)
1492         return err;
1493 
1494     val /= 8;
1495     val = clamp_val(val, 0, 255);
1496 
1497     mutex_lock(&data->update_lock);
1498     if (data->type == f81866a)
1499         f71882fg_write8(data, F81866_REG_IN1_HIGH, val);
1500     else
1501         f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
1502     data->in1_max = val;
1503     mutex_unlock(&data->update_lock);
1504 
1505     return count;
1506 }
1507 
1508 static ssize_t show_in_beep(struct device *dev, struct device_attribute
1509     *devattr, char *buf)
1510 {
1511     struct f71882fg_data *data = f71882fg_update_device(dev);
1512     int nr = to_sensor_dev_attr_2(devattr)->index;
1513 
1514     if (data->in_beep & (1 << nr))
1515         return sprintf(buf, "1\n");
1516     else
1517         return sprintf(buf, "0\n");
1518 }
1519 
1520 static ssize_t store_in_beep(struct device *dev, struct device_attribute
1521     *devattr, const char *buf, size_t count)
1522 {
1523     struct f71882fg_data *data = dev_get_drvdata(dev);
1524     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1525     unsigned long val;
1526 
1527     err = kstrtoul(buf, 10, &val);
1528     if (err)
1529         return err;
1530 
1531     mutex_lock(&data->update_lock);
1532     if (data->type == f81866a)
1533         data->in_beep = f71882fg_read8(data, F81866_REG_IN_BEEP);
1534     else
1535         data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
1536 
1537     if (val)
1538         data->in_beep |= 1 << nr;
1539     else
1540         data->in_beep &= ~(1 << nr);
1541 
1542     if (data->type == f81866a)
1543         f71882fg_write8(data, F81866_REG_IN_BEEP, data->in_beep);
1544     else
1545         f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
1546     mutex_unlock(&data->update_lock);
1547 
1548     return count;
1549 }
1550 
1551 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
1552     *devattr, char *buf)
1553 {
1554     struct f71882fg_data *data = f71882fg_update_device(dev);
1555     int nr = to_sensor_dev_attr_2(devattr)->index;
1556 
1557     if (data->in_status & (1 << nr))
1558         return sprintf(buf, "1\n");
1559     else
1560         return sprintf(buf, "0\n");
1561 }
1562 
1563 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
1564     char *buf)
1565 {
1566     struct f71882fg_data *data = f71882fg_update_device(dev);
1567     int nr = to_sensor_dev_attr_2(devattr)->index;
1568     int sign, temp;
1569 
1570     if (data->type == f71858fg) {
1571         /* TEMP_TABLE_SEL 1 or 3 ? */
1572         if (data->temp_config & 1) {
1573             sign = data->temp[nr] & 0x0001;
1574             temp = (data->temp[nr] >> 5) & 0x7ff;
1575         } else {
1576             sign = data->temp[nr] & 0x8000;
1577             temp = (data->temp[nr] >> 5) & 0x3ff;
1578         }
1579         temp *= 125;
1580         if (sign)
1581             temp -= 128000;
1582     } else {
1583         temp = ((s8)data->temp[nr]) * 1000;
1584     }
1585 
1586     return sprintf(buf, "%d\n", temp);
1587 }
1588 
1589 static ssize_t show_temp_max(struct device *dev, struct device_attribute
1590     *devattr, char *buf)
1591 {
1592     struct f71882fg_data *data = f71882fg_update_device(dev);
1593     int nr = to_sensor_dev_attr_2(devattr)->index;
1594 
1595     return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
1596 }
1597 
1598 static ssize_t store_temp_max(struct device *dev, struct device_attribute
1599     *devattr, const char *buf, size_t count)
1600 {
1601     struct f71882fg_data *data = dev_get_drvdata(dev);
1602     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1603     long val;
1604 
1605     err = kstrtol(buf, 10, &val);
1606     if (err)
1607         return err;
1608 
1609     val /= 1000;
1610     val = clamp_val(val, 0, 255);
1611 
1612     mutex_lock(&data->update_lock);
1613     f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
1614     data->temp_high[nr] = val;
1615     mutex_unlock(&data->update_lock);
1616 
1617     return count;
1618 }
1619 
1620 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
1621     *devattr, char *buf)
1622 {
1623     struct f71882fg_data *data = f71882fg_update_device(dev);
1624     int nr = to_sensor_dev_attr_2(devattr)->index;
1625     int temp_max_hyst;
1626 
1627     mutex_lock(&data->update_lock);
1628     if (nr & 1)
1629         temp_max_hyst = data->temp_hyst[nr / 2] >> 4;
1630     else
1631         temp_max_hyst = data->temp_hyst[nr / 2] & 0x0f;
1632     temp_max_hyst = (data->temp_high[nr] - temp_max_hyst) * 1000;
1633     mutex_unlock(&data->update_lock);
1634 
1635     return sprintf(buf, "%d\n", temp_max_hyst);
1636 }
1637 
1638 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
1639     *devattr, const char *buf, size_t count)
1640 {
1641     struct f71882fg_data *data = dev_get_drvdata(dev);
1642     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1643     ssize_t ret = count;
1644     u8 reg;
1645     long val;
1646 
1647     err = kstrtol(buf, 10, &val);
1648     if (err)
1649         return err;
1650 
1651     val /= 1000;
1652 
1653     mutex_lock(&data->update_lock);
1654 
1655     /* convert abs to relative and check */
1656     data->temp_high[nr] = f71882fg_read8(data, F71882FG_REG_TEMP_HIGH(nr));
1657     val = clamp_val(val, data->temp_high[nr] - 15, data->temp_high[nr]);
1658     val = data->temp_high[nr] - val;
1659 
1660     /* convert value to register contents */
1661     reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST(nr / 2));
1662     if (nr & 1)
1663         reg = (reg & 0x0f) | (val << 4);
1664     else
1665         reg = (reg & 0xf0) | val;
1666     f71882fg_write8(data, F71882FG_REG_TEMP_HYST(nr / 2), reg);
1667     data->temp_hyst[nr / 2] = reg;
1668 
1669     mutex_unlock(&data->update_lock);
1670     return ret;
1671 }
1672 
1673 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
1674     *devattr, char *buf)
1675 {
1676     struct f71882fg_data *data = f71882fg_update_device(dev);
1677     int nr = to_sensor_dev_attr_2(devattr)->index;
1678 
1679     return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
1680 }
1681 
1682 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
1683     *devattr, const char *buf, size_t count)
1684 {
1685     struct f71882fg_data *data = dev_get_drvdata(dev);
1686     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1687     long val;
1688 
1689     err = kstrtol(buf, 10, &val);
1690     if (err)
1691         return err;
1692 
1693     val /= 1000;
1694     val = clamp_val(val, 0, 255);
1695 
1696     mutex_lock(&data->update_lock);
1697     f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
1698     data->temp_ovt[nr] = val;
1699     mutex_unlock(&data->update_lock);
1700 
1701     return count;
1702 }
1703 
1704 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
1705     *devattr, char *buf)
1706 {
1707     struct f71882fg_data *data = f71882fg_update_device(dev);
1708     int nr = to_sensor_dev_attr_2(devattr)->index;
1709     int temp_crit_hyst;
1710 
1711     mutex_lock(&data->update_lock);
1712     if (nr & 1)
1713         temp_crit_hyst = data->temp_hyst[nr / 2] >> 4;
1714     else
1715         temp_crit_hyst = data->temp_hyst[nr / 2] & 0x0f;
1716     temp_crit_hyst = (data->temp_ovt[nr] - temp_crit_hyst) * 1000;
1717     mutex_unlock(&data->update_lock);
1718 
1719     return sprintf(buf, "%d\n", temp_crit_hyst);
1720 }
1721 
1722 static ssize_t show_temp_type(struct device *dev, struct device_attribute
1723     *devattr, char *buf)
1724 {
1725     struct f71882fg_data *data = f71882fg_update_device(dev);
1726     int nr = to_sensor_dev_attr_2(devattr)->index;
1727 
1728     return sprintf(buf, "%d\n", data->temp_type[nr]);
1729 }
1730 
1731 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
1732     *devattr, char *buf)
1733 {
1734     struct f71882fg_data *data = f71882fg_update_device(dev);
1735     int nr = to_sensor_dev_attr_2(devattr)->index;
1736 
1737     if (data->temp_beep & (1 << nr))
1738         return sprintf(buf, "1\n");
1739     else
1740         return sprintf(buf, "0\n");
1741 }
1742 
1743 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
1744     *devattr, const char *buf, size_t count)
1745 {
1746     struct f71882fg_data *data = dev_get_drvdata(dev);
1747     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1748     unsigned long val;
1749 
1750     err = kstrtoul(buf, 10, &val);
1751     if (err)
1752         return err;
1753 
1754     mutex_lock(&data->update_lock);
1755     data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
1756     if (val)
1757         data->temp_beep |= 1 << nr;
1758     else
1759         data->temp_beep &= ~(1 << nr);
1760 
1761     f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
1762     mutex_unlock(&data->update_lock);
1763 
1764     return count;
1765 }
1766 
1767 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
1768     *devattr, char *buf)
1769 {
1770     struct f71882fg_data *data = f71882fg_update_device(dev);
1771     int nr = to_sensor_dev_attr_2(devattr)->index;
1772 
1773     if (data->temp_status & (1 << nr))
1774         return sprintf(buf, "1\n");
1775     else
1776         return sprintf(buf, "0\n");
1777 }
1778 
1779 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
1780     *devattr, char *buf)
1781 {
1782     struct f71882fg_data *data = f71882fg_update_device(dev);
1783     int nr = to_sensor_dev_attr_2(devattr)->index;
1784 
1785     if (data->temp_diode_open & (1 << nr))
1786         return sprintf(buf, "1\n");
1787     else
1788         return sprintf(buf, "0\n");
1789 }
1790 
1791 static ssize_t show_pwm(struct device *dev,
1792             struct device_attribute *devattr, char *buf)
1793 {
1794     struct f71882fg_data *data = f71882fg_update_device(dev);
1795     int val, nr = to_sensor_dev_attr_2(devattr)->index;
1796     mutex_lock(&data->update_lock);
1797     if (data->pwm_enable & (1 << (2 * nr)))
1798         /* PWM mode */
1799         val = data->pwm[nr];
1800     else {
1801         /* RPM mode */
1802         val = 255 * fan_from_reg(data->fan_target[nr])
1803             / fan_from_reg(data->fan_full_speed[nr]);
1804     }
1805     mutex_unlock(&data->update_lock);
1806     return sprintf(buf, "%d\n", val);
1807 }
1808 
1809 static ssize_t store_pwm(struct device *dev,
1810              struct device_attribute *devattr, const char *buf,
1811              size_t count)
1812 {
1813     struct f71882fg_data *data = dev_get_drvdata(dev);
1814     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1815     long val;
1816 
1817     err = kstrtol(buf, 10, &val);
1818     if (err)
1819         return err;
1820 
1821     val = clamp_val(val, 0, 255);
1822 
1823     mutex_lock(&data->update_lock);
1824     data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1825     if ((data->type == f8000 && ((data->pwm_enable >> 2 * nr) & 3) != 2) ||
1826         (data->type != f8000 && !((data->pwm_enable >> 2 * nr) & 2))) {
1827         count = -EROFS;
1828         goto leave;
1829     }
1830     if (data->pwm_enable & (1 << (2 * nr))) {
1831         /* PWM mode */
1832         f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1833         data->pwm[nr] = val;
1834     } else {
1835         /* RPM mode */
1836         int target, full_speed;
1837         full_speed = f71882fg_read16(data,
1838                          F71882FG_REG_FAN_FULL_SPEED(nr));
1839         target = fan_to_reg(val * fan_from_reg(full_speed) / 255);
1840         f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr), target);
1841         data->fan_target[nr] = target;
1842         data->fan_full_speed[nr] = full_speed;
1843     }
1844 leave:
1845     mutex_unlock(&data->update_lock);
1846 
1847     return count;
1848 }
1849 
1850 static ssize_t show_simple_pwm(struct device *dev,
1851                    struct device_attribute *devattr, char *buf)
1852 {
1853     struct f71882fg_data *data = f71882fg_update_device(dev);
1854     int val, nr = to_sensor_dev_attr_2(devattr)->index;
1855 
1856     val = data->pwm[nr];
1857     return sprintf(buf, "%d\n", val);
1858 }
1859 
1860 static ssize_t store_simple_pwm(struct device *dev,
1861                 struct device_attribute *devattr,
1862                 const char *buf, size_t count)
1863 {
1864     struct f71882fg_data *data = dev_get_drvdata(dev);
1865     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1866     long val;
1867 
1868     err = kstrtol(buf, 10, &val);
1869     if (err)
1870         return err;
1871 
1872     val = clamp_val(val, 0, 255);
1873 
1874     mutex_lock(&data->update_lock);
1875     f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1876     data->pwm[nr] = val;
1877     mutex_unlock(&data->update_lock);
1878 
1879     return count;
1880 }
1881 
1882 static ssize_t show_pwm_enable(struct device *dev,
1883                    struct device_attribute *devattr, char *buf)
1884 {
1885     int result = 0;
1886     struct f71882fg_data *data = f71882fg_update_device(dev);
1887     int nr = to_sensor_dev_attr_2(devattr)->index;
1888 
1889     switch ((data->pwm_enable >> 2 * nr) & 3) {
1890     case 0:
1891     case 1:
1892         result = 2; /* Normal auto mode */
1893         break;
1894     case 2:
1895         result = 1; /* Manual mode */
1896         break;
1897     case 3:
1898         if (data->type == f8000)
1899             result = 3; /* Thermostat mode */
1900         else
1901             result = 1; /* Manual mode */
1902         break;
1903     }
1904 
1905     return sprintf(buf, "%d\n", result);
1906 }
1907 
1908 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1909                 *devattr, const char *buf, size_t count)
1910 {
1911     struct f71882fg_data *data = dev_get_drvdata(dev);
1912     int err, nr = to_sensor_dev_attr_2(devattr)->index;
1913     long val;
1914 
1915     err = kstrtol(buf, 10, &val);
1916     if (err)
1917         return err;
1918 
1919     /* Special case for F8000 pwm channel 3 which only does auto mode */
1920     if (data->type == f8000 && nr == 2 && val != 2)
1921         return -EINVAL;
1922 
1923     mutex_lock(&data->update_lock);
1924     data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1925     /* Special case for F8000 auto PWM mode / Thermostat mode */
1926     if (data->type == f8000 && ((data->pwm_enable >> 2 * nr) & 1)) {
1927         switch (val) {
1928         case 2:
1929             data->pwm_enable &= ~(2 << (2 * nr));
1930             break;      /* Normal auto mode */
1931         case 3:
1932             data->pwm_enable |= 2 << (2 * nr);
1933             break;      /* Thermostat mode */
1934         default:
1935             count = -EINVAL;
1936             goto leave;
1937         }
1938     } else {
1939         switch (val) {
1940         case 1:
1941             /* The f71858fg does not support manual RPM mode */
1942             if (data->type == f71858fg &&
1943                 ((data->pwm_enable >> (2 * nr)) & 1)) {
1944                 count = -EINVAL;
1945                 goto leave;
1946             }
1947             data->pwm_enable |= 2 << (2 * nr);
1948             break;      /* Manual */
1949         case 2:
1950             data->pwm_enable &= ~(2 << (2 * nr));
1951             break;      /* Normal auto mode */
1952         default:
1953             count = -EINVAL;
1954             goto leave;
1955         }
1956     }
1957     f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
1958 leave:
1959     mutex_unlock(&data->update_lock);
1960 
1961     return count;
1962 }
1963 
1964 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
1965                        struct device_attribute *devattr,
1966                        char *buf)
1967 {
1968     int result;
1969     struct f71882fg_data *data = f71882fg_update_device(dev);
1970     int pwm = to_sensor_dev_attr_2(devattr)->index;
1971     int point = to_sensor_dev_attr_2(devattr)->nr;
1972 
1973     mutex_lock(&data->update_lock);
1974     if (data->pwm_enable & (1 << (2 * pwm))) {
1975         /* PWM mode */
1976         result = data->pwm_auto_point_pwm[pwm][point];
1977     } else {
1978         /* RPM mode */
1979         result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
1980     }
1981     mutex_unlock(&data->update_lock);
1982 
1983     return sprintf(buf, "%d\n", result);
1984 }
1985 
1986 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
1987                     struct device_attribute *devattr,
1988                     const char *buf, size_t count)
1989 {
1990     struct f71882fg_data *data = dev_get_drvdata(dev);
1991     int err, pwm = to_sensor_dev_attr_2(devattr)->index;
1992     int point = to_sensor_dev_attr_2(devattr)->nr;
1993     long val;
1994 
1995     err = kstrtol(buf, 10, &val);
1996     if (err)
1997         return err;
1998 
1999     val = clamp_val(val, 0, 255);
2000 
2001     mutex_lock(&data->update_lock);
2002     data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
2003     if (data->pwm_enable & (1 << (2 * pwm))) {
2004         /* PWM mode */
2005     } else {
2006         /* RPM mode */
2007         if (val < 29)   /* Prevent negative numbers */
2008             val = 255;
2009         else
2010             val = (255 - val) * 32 / val;
2011     }
2012     f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
2013     data->pwm_auto_point_pwm[pwm][point] = val;
2014     mutex_unlock(&data->update_lock);
2015 
2016     return count;
2017 }
2018 
2019 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
2020                          struct device_attribute *devattr,
2021                          char *buf)
2022 {
2023     int result = 0;
2024     struct f71882fg_data *data = f71882fg_update_device(dev);
2025     int nr = to_sensor_dev_attr_2(devattr)->index;
2026     int point = to_sensor_dev_attr_2(devattr)->nr;
2027 
2028     mutex_lock(&data->update_lock);
2029     if (nr & 1)
2030         result = data->pwm_auto_point_hyst[nr / 2] >> 4;
2031     else
2032         result = data->pwm_auto_point_hyst[nr / 2] & 0x0f;
2033     result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
2034     mutex_unlock(&data->update_lock);
2035 
2036     return sprintf(buf, "%d\n", result);
2037 }
2038 
2039 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
2040                           struct device_attribute *devattr,
2041                           const char *buf, size_t count)
2042 {
2043     struct f71882fg_data *data = dev_get_drvdata(dev);
2044     int err, nr = to_sensor_dev_attr_2(devattr)->index;
2045     int point = to_sensor_dev_attr_2(devattr)->nr;
2046     u8 reg;
2047     long val;
2048 
2049     err = kstrtol(buf, 10, &val);
2050     if (err)
2051         return err;
2052 
2053     val /= 1000;
2054 
2055     mutex_lock(&data->update_lock);
2056     data->pwm_auto_point_temp[nr][point] =
2057         f71882fg_read8(data, F71882FG_REG_POINT_TEMP(nr, point));
2058     val = clamp_val(val, data->pwm_auto_point_temp[nr][point] - 15,
2059             data->pwm_auto_point_temp[nr][point]);
2060     val = data->pwm_auto_point_temp[nr][point] - val;
2061 
2062     reg = f71882fg_read8(data, F71882FG_REG_FAN_HYST(nr / 2));
2063     if (nr & 1)
2064         reg = (reg & 0x0f) | (val << 4);
2065     else
2066         reg = (reg & 0xf0) | val;
2067 
2068     f71882fg_write8(data, F71882FG_REG_FAN_HYST(nr / 2), reg);
2069     data->pwm_auto_point_hyst[nr / 2] = reg;
2070     mutex_unlock(&data->update_lock);
2071 
2072     return count;
2073 }
2074 
2075 static ssize_t show_pwm_interpolate(struct device *dev,
2076                     struct device_attribute *devattr, char *buf)
2077 {
2078     int result;
2079     struct f71882fg_data *data = f71882fg_update_device(dev);
2080     int nr = to_sensor_dev_attr_2(devattr)->index;
2081 
2082     result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
2083 
2084     return sprintf(buf, "%d\n", result);
2085 }
2086 
2087 static ssize_t store_pwm_interpolate(struct device *dev,
2088                      struct device_attribute *devattr,
2089                      const char *buf, size_t count)
2090 {
2091     struct f71882fg_data *data = dev_get_drvdata(dev);
2092     int err, nr = to_sensor_dev_attr_2(devattr)->index;
2093     unsigned long val;
2094 
2095     err = kstrtoul(buf, 10, &val);
2096     if (err)
2097         return err;
2098 
2099     mutex_lock(&data->update_lock);
2100     data->pwm_auto_point_mapping[nr] =
2101         f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
2102     if (val)
2103         val = data->pwm_auto_point_mapping[nr] | (1 << 4);
2104     else
2105         val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
2106     f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
2107     data->pwm_auto_point_mapping[nr] = val;
2108     mutex_unlock(&data->update_lock);
2109 
2110     return count;
2111 }
2112 
2113 static ssize_t show_pwm_auto_point_channel(struct device *dev,
2114                        struct device_attribute *devattr,
2115                        char *buf)
2116 {
2117     int result;
2118     struct f71882fg_data *data = f71882fg_update_device(dev);
2119     int nr = to_sensor_dev_attr_2(devattr)->index;
2120 
2121     result = 1 << ((data->pwm_auto_point_mapping[nr] & 3) -
2122                data->temp_start);
2123 
2124     return sprintf(buf, "%d\n", result);
2125 }
2126 
2127 static ssize_t store_pwm_auto_point_channel(struct device *dev,
2128                         struct device_attribute *devattr,
2129                         const char *buf, size_t count)
2130 {
2131     struct f71882fg_data *data = dev_get_drvdata(dev);
2132     int err, nr = to_sensor_dev_attr_2(devattr)->index;
2133     long val;
2134 
2135     err = kstrtol(buf, 10, &val);
2136     if (err)
2137         return err;
2138 
2139     switch (val) {
2140     case 1:
2141         val = 0;
2142         break;
2143     case 2:
2144         val = 1;
2145         break;
2146     case 4:
2147         val = 2;
2148         break;
2149     default:
2150         return -EINVAL;
2151     }
2152     val += data->temp_start;
2153     mutex_lock(&data->update_lock);
2154     data->pwm_auto_point_mapping[nr] =
2155         f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
2156     val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
2157     f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
2158     data->pwm_auto_point_mapping[nr] = val;
2159     mutex_unlock(&data->update_lock);
2160 
2161     return count;
2162 }
2163 
2164 static ssize_t show_pwm_auto_point_temp(struct device *dev,
2165                     struct device_attribute *devattr,
2166                     char *buf)
2167 {
2168     int result;
2169     struct f71882fg_data *data = f71882fg_update_device(dev);
2170     int pwm = to_sensor_dev_attr_2(devattr)->index;
2171     int point = to_sensor_dev_attr_2(devattr)->nr;
2172 
2173     result = data->pwm_auto_point_temp[pwm][point];
2174     return sprintf(buf, "%d\n", 1000 * result);
2175 }
2176 
2177 static ssize_t store_pwm_auto_point_temp(struct device *dev,
2178                      struct device_attribute *devattr,
2179                      const char *buf, size_t count)
2180 {
2181     struct f71882fg_data *data = dev_get_drvdata(dev);
2182     int err, pwm = to_sensor_dev_attr_2(devattr)->index;
2183     int point = to_sensor_dev_attr_2(devattr)->nr;
2184     long val;
2185 
2186     err = kstrtol(buf, 10, &val);
2187     if (err)
2188         return err;
2189 
2190     val /= 1000;
2191 
2192     if (data->auto_point_temp_signed)
2193         val = clamp_val(val, -128, 127);
2194     else
2195         val = clamp_val(val, 0, 127);
2196 
2197     mutex_lock(&data->update_lock);
2198     f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
2199     data->pwm_auto_point_temp[pwm][point] = val;
2200     mutex_unlock(&data->update_lock);
2201 
2202     return count;
2203 }
2204 
2205 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
2206     char *buf)
2207 {
2208     struct f71882fg_data *data = dev_get_drvdata(dev);
2209     return sprintf(buf, "%s\n", f71882fg_names[data->type]);
2210 }
2211 
2212 static int f71882fg_create_sysfs_files(struct platform_device *pdev,
2213     struct sensor_device_attribute_2 *attr, int count)
2214 {
2215     int err, i;
2216 
2217     for (i = 0; i < count; i++) {
2218         err = device_create_file(&pdev->dev, &attr[i].dev_attr);
2219         if (err)
2220             return err;
2221     }
2222     return 0;
2223 }
2224 
2225 static void f71882fg_remove_sysfs_files(struct platform_device *pdev,
2226     struct sensor_device_attribute_2 *attr, int count)
2227 {
2228     int i;
2229 
2230     for (i = 0; i < count; i++)
2231         device_remove_file(&pdev->dev, &attr[i].dev_attr);
2232 }
2233 
2234 static int f71882fg_create_fan_sysfs_files(
2235     struct platform_device *pdev, int idx)
2236 {
2237     struct f71882fg_data *data = platform_get_drvdata(pdev);
2238     int err;
2239 
2240     /* Sanity check the pwm setting */
2241     err = 0;
2242     switch (data->type) {
2243     case f71858fg:
2244         if (((data->pwm_enable >> (idx * 2)) & 3) == 3)
2245             err = 1;
2246         break;
2247     case f71862fg:
2248         if (((data->pwm_enable >> (idx * 2)) & 1) != 1)
2249             err = 1;
2250         break;
2251     case f8000:
2252         if (idx == 2)
2253             err = data->pwm_enable & 0x20;
2254         break;
2255     default:
2256         break;
2257     }
2258     if (err) {
2259         dev_err(&pdev->dev,
2260             "Invalid (reserved) pwm settings: 0x%02x, "
2261             "skipping fan %d\n",
2262             (data->pwm_enable >> (idx * 2)) & 3, idx + 1);
2263         return 0; /* This is a non fatal condition */
2264     }
2265 
2266     err = f71882fg_create_sysfs_files(pdev, &fxxxx_fan_attr[idx][0],
2267                       ARRAY_SIZE(fxxxx_fan_attr[0]));
2268     if (err)
2269         return err;
2270 
2271     if (f71882fg_fan_has_beep[data->type]) {
2272         err = f71882fg_create_sysfs_files(pdev,
2273                           &fxxxx_fan_beep_attr[idx],
2274                           1);
2275         if (err)
2276             return err;
2277     }
2278 
2279     dev_info(&pdev->dev, "Fan: %d is in %s mode\n", idx + 1,
2280          (data->pwm_enable & (1 << (2 * idx))) ? "duty-cycle" : "RPM");
2281 
2282     /* Check for unsupported auto pwm settings */
2283     switch (data->type) {
2284     case f71808e:
2285     case f71808a:
2286     case f71869:
2287     case f71869a:
2288     case f71889fg:
2289     case f71889ed:
2290     case f71889a:
2291         data->pwm_auto_point_mapping[idx] =
2292             f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(idx));
2293         if ((data->pwm_auto_point_mapping[idx] & 0x80) ||
2294             (data->pwm_auto_point_mapping[idx] & 3) == 0) {
2295             dev_warn(&pdev->dev,
2296                  "Auto pwm controlled by raw digital "
2297                  "data, disabling pwm auto_point "
2298                  "sysfs attributes for fan %d\n", idx + 1);
2299             return 0; /* This is a non fatal condition */
2300         }
2301         break;
2302     default:
2303         break;
2304     }
2305 
2306     switch (data->type) {
2307     case f71862fg:
2308         err = f71882fg_create_sysfs_files(pdev,
2309                     &f71862fg_auto_pwm_attr[idx][0],
2310                     ARRAY_SIZE(f71862fg_auto_pwm_attr[0]));
2311         break;
2312     case f71808e:
2313     case f71869:
2314         err = f71882fg_create_sysfs_files(pdev,
2315                     &f71869_auto_pwm_attr[idx][0],
2316                     ARRAY_SIZE(f71869_auto_pwm_attr[0]));
2317         break;
2318     case f8000:
2319         err = f71882fg_create_sysfs_files(pdev,
2320                     &f8000_auto_pwm_attr[idx][0],
2321                     ARRAY_SIZE(f8000_auto_pwm_attr[0]));
2322         break;
2323     default:
2324         err = f71882fg_create_sysfs_files(pdev,
2325                     &fxxxx_auto_pwm_attr[idx][0],
2326                     ARRAY_SIZE(fxxxx_auto_pwm_attr[0]));
2327     }
2328 
2329     return err;
2330 }
2331 
2332 static int f71882fg_probe(struct platform_device *pdev)
2333 {
2334     struct f71882fg_data *data;
2335     struct f71882fg_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2336     int nr_fans = f71882fg_nr_fans[sio_data->type];
2337     int nr_temps = f71882fg_nr_temps[sio_data->type];
2338     int err, i;
2339     int size;
2340     u8 start_reg, reg;
2341 
2342     data = devm_kzalloc(&pdev->dev, sizeof(struct f71882fg_data),
2343                 GFP_KERNEL);
2344     if (!data)
2345         return -ENOMEM;
2346 
2347     data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
2348     data->type = sio_data->type;
2349     data->temp_start =
2350         (data->type == f71858fg || data->type == f8000 ||
2351         data->type == f81866a) ? 0 : 1;
2352     mutex_init(&data->update_lock);
2353     platform_set_drvdata(pdev, data);
2354 
2355     start_reg = f71882fg_read8(data, F71882FG_REG_START);
2356     if (start_reg & 0x04) {
2357         dev_warn(&pdev->dev, "Hardware monitor is powered down\n");
2358         return -ENODEV;
2359     }
2360     if (!(start_reg & 0x03)) {
2361         dev_warn(&pdev->dev, "Hardware monitoring not activated\n");
2362         return -ENODEV;
2363     }
2364 
2365     /* Register sysfs interface files */
2366     err = device_create_file(&pdev->dev, &dev_attr_name);
2367     if (err)
2368         goto exit_unregister_sysfs;
2369 
2370     if (start_reg & 0x01) {
2371         switch (data->type) {
2372         case f71858fg:
2373             data->temp_config =
2374                 f71882fg_read8(data, F71882FG_REG_TEMP_CONFIG);
2375             if (data->temp_config & 0x10)
2376                 /*
2377                  * The f71858fg temperature alarms behave as
2378                  * the f8000 alarms in this mode
2379                  */
2380                 err = f71882fg_create_sysfs_files(pdev,
2381                     f8000_temp_attr,
2382                     ARRAY_SIZE(f8000_temp_attr));
2383             else
2384                 err = f71882fg_create_sysfs_files(pdev,
2385                     f71858fg_temp_attr,
2386                     ARRAY_SIZE(f71858fg_temp_attr));
2387             break;
2388         case f8000:
2389             err = f71882fg_create_sysfs_files(pdev,
2390                     f8000_temp_attr,
2391                     ARRAY_SIZE(f8000_temp_attr));
2392             break;
2393         case f81866a:
2394             err = f71882fg_create_sysfs_files(pdev,
2395                     f71858fg_temp_attr,
2396                     ARRAY_SIZE(f71858fg_temp_attr));
2397             break;
2398         default:
2399             err = f71882fg_create_sysfs_files(pdev,
2400                 &fxxxx_temp_attr[0][0],
2401                 ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
2402         }
2403         if (err)
2404             goto exit_unregister_sysfs;
2405 
2406         if (f71882fg_temp_has_beep[data->type]) {
2407             if (data->type == f81866a) {
2408                 size = ARRAY_SIZE(f81866_temp_beep_attr[0]);
2409                 err = f71882fg_create_sysfs_files(pdev,
2410                         &f81866_temp_beep_attr[0][0],
2411                         size * nr_temps);
2412 
2413             } else {
2414                 size = ARRAY_SIZE(fxxxx_temp_beep_attr[0]);
2415                 err = f71882fg_create_sysfs_files(pdev,
2416                         &fxxxx_temp_beep_attr[0][0],
2417                         size * nr_temps);
2418             }
2419             if (err)
2420                 goto exit_unregister_sysfs;
2421         }
2422 
2423         for (i = 0; i < F71882FG_MAX_INS; i++) {
2424             if (f71882fg_has_in[data->type][i]) {
2425                 err = device_create_file(&pdev->dev,
2426                         &fxxxx_in_attr[i].dev_attr);
2427                 if (err)
2428                     goto exit_unregister_sysfs;
2429             }
2430         }
2431         if (f71882fg_has_in1_alarm[data->type]) {
2432             err = f71882fg_create_sysfs_files(pdev,
2433                     fxxxx_in1_alarm_attr,
2434                     ARRAY_SIZE(fxxxx_in1_alarm_attr));
2435             if (err)
2436                 goto exit_unregister_sysfs;
2437         }
2438     }
2439 
2440     if (start_reg & 0x02) {
2441         switch (data->type) {
2442         case f71808e:
2443         case f71808a:
2444         case f71869:
2445         case f71869a:
2446             /* These always have signed auto point temps */
2447             data->auto_point_temp_signed = 1;
2448             fallthrough;    /* to select correct fan/pwm reg bank! */
2449         case f71889fg:
2450         case f71889ed:
2451         case f71889a:
2452             reg = f71882fg_read8(data, F71882FG_REG_FAN_FAULT_T);
2453             if (reg & F71882FG_FAN_NEG_TEMP_EN)
2454                 data->auto_point_temp_signed = 1;
2455             /* Ensure banked pwm registers point to right bank */
2456             reg &= ~F71882FG_FAN_PROG_SEL;
2457             f71882fg_write8(data, F71882FG_REG_FAN_FAULT_T, reg);
2458             break;
2459         default:
2460             break;
2461         }
2462 
2463         data->pwm_enable =
2464             f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
2465 
2466         for (i = 0; i < nr_fans; i++) {
2467             err = f71882fg_create_fan_sysfs_files(pdev, i);
2468             if (err)
2469                 goto exit_unregister_sysfs;
2470         }
2471 
2472         /* Some types have 1 extra fan with limited functionality */
2473         switch (data->type) {
2474         case f71808a:
2475             err = f71882fg_create_sysfs_files(pdev,
2476                     f71808a_fan3_attr,
2477                     ARRAY_SIZE(f71808a_fan3_attr));
2478             break;
2479         case f8000:
2480             err = f71882fg_create_sysfs_files(pdev,
2481                     f8000_fan_attr,
2482                     ARRAY_SIZE(f8000_fan_attr));
2483             break;
2484         default:
2485             break;
2486         }
2487         if (err)
2488             goto exit_unregister_sysfs;
2489     }
2490 
2491     data->hwmon_dev = hwmon_device_register(&pdev->dev);
2492     if (IS_ERR(data->hwmon_dev)) {
2493         err = PTR_ERR(data->hwmon_dev);
2494         data->hwmon_dev = NULL;
2495         goto exit_unregister_sysfs;
2496     }
2497 
2498     return 0;
2499 
2500 exit_unregister_sysfs:
2501     f71882fg_remove(pdev); /* Will unregister the sysfs files for us */
2502     return err; /* f71882fg_remove() also frees our data */
2503 }
2504 
2505 static int f71882fg_remove(struct platform_device *pdev)
2506 {
2507     struct f71882fg_data *data = platform_get_drvdata(pdev);
2508     int nr_fans = f71882fg_nr_fans[data->type];
2509     int nr_temps = f71882fg_nr_temps[data->type];
2510     int i;
2511     u8 start_reg = f71882fg_read8(data, F71882FG_REG_START);
2512 
2513     if (data->hwmon_dev)
2514         hwmon_device_unregister(data->hwmon_dev);
2515 
2516     device_remove_file(&pdev->dev, &dev_attr_name);
2517 
2518     if (start_reg & 0x01) {
2519         switch (data->type) {
2520         case f71858fg:
2521             if (data->temp_config & 0x10)
2522                 f71882fg_remove_sysfs_files(pdev,
2523                     f8000_temp_attr,
2524                     ARRAY_SIZE(f8000_temp_attr));
2525             else
2526                 f71882fg_remove_sysfs_files(pdev,
2527                     f71858fg_temp_attr,
2528                     ARRAY_SIZE(f71858fg_temp_attr));
2529             break;
2530         case f8000:
2531             f71882fg_remove_sysfs_files(pdev,
2532                     f8000_temp_attr,
2533                     ARRAY_SIZE(f8000_temp_attr));
2534             break;
2535         case f81866a:
2536             f71882fg_remove_sysfs_files(pdev,
2537                     f71858fg_temp_attr,
2538                     ARRAY_SIZE(f71858fg_temp_attr));
2539             break;
2540         default:
2541             f71882fg_remove_sysfs_files(pdev,
2542                 &fxxxx_temp_attr[0][0],
2543                 ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
2544         }
2545         if (f71882fg_temp_has_beep[data->type]) {
2546             if (data->type == f81866a)
2547                 f71882fg_remove_sysfs_files(pdev,
2548                     &f81866_temp_beep_attr[0][0],
2549                     ARRAY_SIZE(f81866_temp_beep_attr[0])
2550                         * nr_temps);
2551             else
2552                 f71882fg_remove_sysfs_files(pdev,
2553                     &fxxxx_temp_beep_attr[0][0],
2554                     ARRAY_SIZE(fxxxx_temp_beep_attr[0])
2555                         * nr_temps);
2556         }
2557 
2558         for (i = 0; i < F71882FG_MAX_INS; i++) {
2559             if (f71882fg_has_in[data->type][i]) {
2560                 device_remove_file(&pdev->dev,
2561                         &fxxxx_in_attr[i].dev_attr);
2562             }
2563         }
2564         if (f71882fg_has_in1_alarm[data->type]) {
2565             f71882fg_remove_sysfs_files(pdev,
2566                     fxxxx_in1_alarm_attr,
2567                     ARRAY_SIZE(fxxxx_in1_alarm_attr));
2568         }
2569     }
2570 
2571     if (start_reg & 0x02) {
2572         f71882fg_remove_sysfs_files(pdev, &fxxxx_fan_attr[0][0],
2573                 ARRAY_SIZE(fxxxx_fan_attr[0]) * nr_fans);
2574 
2575         if (f71882fg_fan_has_beep[data->type]) {
2576             f71882fg_remove_sysfs_files(pdev,
2577                     fxxxx_fan_beep_attr, nr_fans);
2578         }
2579 
2580         switch (data->type) {
2581         case f71808a:
2582             f71882fg_remove_sysfs_files(pdev,
2583                 &fxxxx_auto_pwm_attr[0][0],
2584                 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
2585             f71882fg_remove_sysfs_files(pdev,
2586                     f71808a_fan3_attr,
2587                     ARRAY_SIZE(f71808a_fan3_attr));
2588             break;
2589         case f71862fg:
2590             f71882fg_remove_sysfs_files(pdev,
2591                 &f71862fg_auto_pwm_attr[0][0],
2592                 ARRAY_SIZE(f71862fg_auto_pwm_attr[0]) *
2593                     nr_fans);
2594             break;
2595         case f71808e:
2596         case f71869:
2597             f71882fg_remove_sysfs_files(pdev,
2598                 &f71869_auto_pwm_attr[0][0],
2599                 ARRAY_SIZE(f71869_auto_pwm_attr[0]) * nr_fans);
2600             break;
2601         case f8000:
2602             f71882fg_remove_sysfs_files(pdev,
2603                     f8000_fan_attr,
2604                     ARRAY_SIZE(f8000_fan_attr));
2605             f71882fg_remove_sysfs_files(pdev,
2606                 &f8000_auto_pwm_attr[0][0],
2607                 ARRAY_SIZE(f8000_auto_pwm_attr[0]) * nr_fans);
2608             break;
2609         default:
2610             f71882fg_remove_sysfs_files(pdev,
2611                 &fxxxx_auto_pwm_attr[0][0],
2612                 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
2613         }
2614     }
2615     return 0;
2616 }
2617 
2618 static int __init f71882fg_find(int sioaddr, struct f71882fg_sio_data *sio_data)
2619 {
2620     u16 devid;
2621     unsigned short address;
2622     int err = superio_enter(sioaddr);
2623     if (err)
2624         return err;
2625 
2626     devid = superio_inw(sioaddr, SIO_REG_MANID);
2627     if (devid != SIO_FINTEK_ID) {
2628         pr_debug("Not a Fintek device\n");
2629         err = -ENODEV;
2630         goto exit;
2631     }
2632 
2633     devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
2634     switch (devid) {
2635     case SIO_F71808E_ID:
2636         sio_data->type = f71808e;
2637         break;
2638     case SIO_F71808A_ID:
2639         sio_data->type = f71808a;
2640         break;
2641     case SIO_F71858_ID:
2642     case SIO_F71858AD_ID:
2643         sio_data->type = f71858fg;
2644         break;
2645     case SIO_F71862_ID:
2646         sio_data->type = f71862fg;
2647         break;
2648     case SIO_F71868_ID:
2649         sio_data->type = f71868a;
2650         break;
2651     case SIO_F71869_ID:
2652         sio_data->type = f71869;
2653         break;
2654     case SIO_F71869A_ID:
2655         sio_data->type = f71869a;
2656         break;
2657     case SIO_F71882_ID:
2658         sio_data->type = f71882fg;
2659         break;
2660     case SIO_F71889_ID:
2661         sio_data->type = f71889fg;
2662         break;
2663     case SIO_F71889E_ID:
2664         sio_data->type = f71889ed;
2665         break;
2666     case SIO_F71889A_ID:
2667         sio_data->type = f71889a;
2668         break;
2669     case SIO_F8000_ID:
2670         sio_data->type = f8000;
2671         break;
2672     case SIO_F81768D_ID:
2673         sio_data->type = f81768d;
2674         break;
2675     case SIO_F81865_ID:
2676         sio_data->type = f81865f;
2677         break;
2678     case SIO_F81866_ID:
2679     case SIO_F81966_ID:
2680         sio_data->type = f81866a;
2681         break;
2682     default:
2683         pr_info("Unsupported Fintek device: %04x\n",
2684             (unsigned int)devid);
2685         err = -ENODEV;
2686         goto exit;
2687     }
2688 
2689     if (sio_data->type == f71858fg)
2690         superio_select(sioaddr, SIO_F71858FG_LD_HWM);
2691     else
2692         superio_select(sioaddr, SIO_F71882FG_LD_HWM);
2693 
2694     if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
2695         pr_warn("Device not activated\n");
2696         err = -ENODEV;
2697         goto exit;
2698     }
2699 
2700     address = superio_inw(sioaddr, SIO_REG_ADDR);
2701     if (address == 0) {
2702         pr_warn("Base address not set\n");
2703         err = -ENODEV;
2704         goto exit;
2705     }
2706     address &= ~(REGION_LENGTH - 1);    /* Ignore 3 LSB */
2707 
2708     err = address;
2709     pr_info("Found %s chip at %#x, revision %d\n",
2710         f71882fg_names[sio_data->type], (unsigned int)address,
2711         (int)superio_inb(sioaddr, SIO_REG_DEVREV));
2712 exit:
2713     superio_exit(sioaddr);
2714     return err;
2715 }
2716 
2717 static int __init f71882fg_device_add(int address,
2718                       const struct f71882fg_sio_data *sio_data)
2719 {
2720     struct resource res = {
2721         .start  = address,
2722         .end    = address + REGION_LENGTH - 1,
2723         .flags  = IORESOURCE_IO,
2724     };
2725     int err;
2726 
2727     f71882fg_pdev = platform_device_alloc(DRVNAME, address);
2728     if (!f71882fg_pdev)
2729         return -ENOMEM;
2730 
2731     res.name = f71882fg_pdev->name;
2732     err = acpi_check_resource_conflict(&res);
2733     if (err)
2734         goto exit_device_put;
2735 
2736     err = platform_device_add_resources(f71882fg_pdev, &res, 1);
2737     if (err) {
2738         pr_err("Device resource addition failed\n");
2739         goto exit_device_put;
2740     }
2741 
2742     err = platform_device_add_data(f71882fg_pdev, sio_data,
2743                        sizeof(struct f71882fg_sio_data));
2744     if (err) {
2745         pr_err("Platform data allocation failed\n");
2746         goto exit_device_put;
2747     }
2748 
2749     err = platform_device_add(f71882fg_pdev);
2750     if (err) {
2751         pr_err("Device addition failed\n");
2752         goto exit_device_put;
2753     }
2754 
2755     return 0;
2756 
2757 exit_device_put:
2758     platform_device_put(f71882fg_pdev);
2759 
2760     return err;
2761 }
2762 
2763 static int __init f71882fg_init(void)
2764 {
2765     int err;
2766     int address;
2767     struct f71882fg_sio_data sio_data;
2768 
2769     memset(&sio_data, 0, sizeof(sio_data));
2770 
2771     address = f71882fg_find(0x2e, &sio_data);
2772     if (address < 0)
2773         address = f71882fg_find(0x4e, &sio_data);
2774     if (address < 0)
2775         return address;
2776 
2777     err = platform_driver_register(&f71882fg_driver);
2778     if (err)
2779         return err;
2780 
2781     err = f71882fg_device_add(address, &sio_data);
2782     if (err)
2783         goto exit_driver;
2784 
2785     return 0;
2786 
2787 exit_driver:
2788     platform_driver_unregister(&f71882fg_driver);
2789     return err;
2790 }
2791 
2792 static void __exit f71882fg_exit(void)
2793 {
2794     platform_device_unregister(f71882fg_pdev);
2795     platform_driver_unregister(&f71882fg_driver);
2796 }
2797 
2798 MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
2799 MODULE_AUTHOR("Hans Edgington, Hans de Goede <hdegoede@redhat.com>");
2800 MODULE_LICENSE("GPL");
2801 
2802 module_init(f71882fg_init);
2803 module_exit(f71882fg_exit);