]> git.sur5r.net Git - groeck-it87/blobdiff - it87.c
Disable SMBus access while accessing Enviromnental Controller registers
[groeck-it87] / it87.c
diff --git a/it87.c b/it87.c
index 43578fd5213c032d1ff00dccfbc82d8c5f90ceef..533d243a80d363b884062c29c15e74855bd011c5 100644 (file)
--- a/it87.c
+++ b/it87.c
@@ -74,6 +74,7 @@
 #include <linux/acpi.h>
 #include <linux/io.h>
 #include "compat.h"
+#include "version.h"
 
 #define DRVNAME "it87"
 
@@ -86,6 +87,10 @@ static unsigned short force_id;
 module_param(force_id, ushort, 0);
 MODULE_PARM_DESC(force_id, "Override the detected device ID");
 
+static bool ignore_resource_conflict;
+module_param(ignore_resource_conflict, bool, 0);
+MODULE_PARM_DESC(ignore_resource_conflict, "Ignore ACPI resource conflict");
+
 static struct platform_device *it87_pdev[2];
 
 #define        REG_2E  0x2e    /* The register to read/write */
@@ -141,13 +146,10 @@ static inline int superio_enter(int ioreg)
         * Try to reserve ioreg and ioreg + 1 for exclusive access.
         */
        if (!request_muxed_region(ioreg, 2, DRVNAME))
-               goto error;
+               return -EBUSY;
 
        __superio_enter(ioreg);
        return 0;
-
-error:
-       return -EBUSY;
 }
 
 static inline void superio_exit(int ioreg, bool doexit)
@@ -188,10 +190,13 @@ static inline void superio_exit(int ioreg, bool doexit)
 #define IT8655E_DEVID 0x8655
 #define IT8665E_DEVID 0x8665
 #define IT8686E_DEVID 0x8686
-#define IT87_ACT_REG  0x30
-#define IT87_BASE_REG 0x60
 
-/* Logical device 7 registers (IT8712F and later) */
+/* Logical device 4 (Environmental Monitor) registers */
+#define IT87_ACT_REG           0x30
+#define IT87_BASE_REG          0x60
+#define IT87_SPECIAL_CFG_REG   0xf3    /* special configuration register */
+
+/* Logical device 7 (GPIO) registers (IT8712F and later) */
 #define IT87_SIO_GPIO1_REG     0x25
 #define IT87_SIO_GPIO2_REG     0x26
 #define IT87_SIO_GPIO3_REG     0x27
@@ -327,8 +332,11 @@ struct it87_devices {
        u32 features;
        u8 num_temp_limit;
        u8 num_temp_offset;
+       u8 num_temp_map;        /* Number of temperature sources for pwm */
        u8 peci_mask;
        u8 old_peci_mask;
+       u8 smbus_bitmap;        /* SMBus enable bits in extra config register */
+       u8 ec_special_config;
 };
 
 #define FEAT_12MV_ADC          BIT(0)
@@ -365,6 +373,7 @@ static const struct it87_devices it87_devices[] = {
                                                /* may need to overwrite */
                .num_temp_limit = 3,
                .num_temp_offset = 0,
+               .num_temp_map = 3,
        },
        [it8712] = {
                .name = "it8712",
@@ -373,6 +382,7 @@ static const struct it87_devices it87_devices[] = {
                                                /* may need to overwrite */
                .num_temp_limit = 3,
                .num_temp_offset = 0,
+               .num_temp_map = 3,
        },
        [it8716] = {
                .name = "it8716",
@@ -382,6 +392,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
        },
        [it8718] = {
                .name = "it8718",
@@ -391,6 +402,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .old_peci_mask = 0x4,
        },
        [it8720] = {
@@ -401,6 +413,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .old_peci_mask = 0x4,
        },
        [it8721] = {
@@ -412,6 +425,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x05,
                .old_peci_mask = 0x02,  /* Actually reports PCH */
        },
@@ -424,6 +438,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 6,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x07,
        },
        [it8732] = {
@@ -435,6 +450,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_FOUR_PWM | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x07,
                .old_peci_mask = 0x02,  /* Actually reports PCH */
        },
@@ -450,6 +466,7 @@ static const struct it87_devices it87_devices[] = {
                                /* three fans, always 16 bit (guesswork) */
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x07,
        },
        [it8772] = {
@@ -464,6 +481,7 @@ static const struct it87_devices it87_devices[] = {
                                /* three fans, always 16 bit (datasheet) */
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x07,
        },
        [it8781] = {
@@ -474,6 +492,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .old_peci_mask = 0x4,
        },
        [it8782] = {
@@ -484,6 +503,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .old_peci_mask = 0x4,
        },
        [it8783] = {
@@ -494,6 +514,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .old_peci_mask = 0x4,
        },
        [it8786] = {
@@ -504,6 +525,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x07,
        },
        [it8790] = {
@@ -514,6 +536,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x07,
        },
        [it8792] = {
@@ -524,6 +547,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x07,
        },
        [it8603] = {
@@ -534,17 +558,19 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 4,
                .peci_mask = 0x07,
        },
        [it8607] = {
                .name = "it8607",
                .suffix = "E",
                .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
-                 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
+                 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL | FEAT_NEW_TEMPMAP
                  | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING
                  | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 6,
                .peci_mask = 0x07,
        },
        [it8613] = {
@@ -556,6 +582,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_AVCC3 | FEAT_SCALING | FEAT_NEW_TEMPMAP,
                .num_temp_limit = 6,
                .num_temp_offset = 6,
+               .num_temp_map = 6,
                .peci_mask = 0x07,
        },
        [it8620] = {
@@ -568,6 +595,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x07,
        },
        [it8622] = {
@@ -579,6 +607,7 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_AVCC3 | FEAT_VIN3_5V | FEAT_SCALING,
                .num_temp_limit = 3,
                .num_temp_offset = 3,
+               .num_temp_map = 4,
                .peci_mask = 0x07,
        },
        [it8625] = {
@@ -590,6 +619,8 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_SIX_PWM | FEAT_BANK_SEL | FEAT_SCALING,
                .num_temp_limit = 6,
                .num_temp_offset = 6,
+               .num_temp_map = 6,
+               .smbus_bitmap = BIT(1) | BIT(2),
        },
        [it8628] = {
                .name = "it8628",
@@ -601,26 +632,31 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_FANCTL_ONOFF,
                .num_temp_limit = 6,
                .num_temp_offset = 3,
+               .num_temp_map = 3,
                .peci_mask = 0x07,
        },
        [it8655] = {
                .name = "it8655",
                .suffix = "E",
                .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
-                 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP
+                 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
                  | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_BANK_SEL,
                .num_temp_limit = 6,
                .num_temp_offset = 6,
+               .num_temp_map = 6,
+               .smbus_bitmap = BIT(2),
        },
        [it8665] = {
                .name = "it8665",
                .suffix = "E",
                .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
-                 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP
+                 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
                  | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
                  | FEAT_SIX_PWM | FEAT_BANK_SEL,
                .num_temp_limit = 6,
                .num_temp_offset = 6,
+               .num_temp_map = 6,
+               .smbus_bitmap = BIT(2),
        },
        [it8686] = {
                .name = "it8686",
@@ -631,6 +667,8 @@ static const struct it87_devices it87_devices[] = {
                  | FEAT_SIX_TEMP | FEAT_BANK_SEL | FEAT_SCALING | FEAT_AVCC3,
                .num_temp_limit = 6,
                .num_temp_offset = 6,
+               .num_temp_map = 7,
+               .smbus_bitmap = BIT(1) | BIT(2),
        },
 };
 
@@ -671,6 +709,8 @@ static const struct it87_devices it87_devices[] = {
 
 struct it87_sio_data {
        enum chips type;
+       u8 sioaddr;
+       u8 doexit;
        /* Values read from Super-I/O config space */
        u8 revision;
        u8 vid_value;
@@ -682,6 +722,8 @@ struct it87_sio_data {
        u8 skip_fan;
        u8 skip_pwm;
        u8 skip_temp;
+       u8 smbus_bitmap;
+       u8 ec_special_config;
 };
 
 /*
@@ -692,10 +734,14 @@ struct it87_data {
        const struct attribute_group *groups[7];
        enum chips type;
        u32 features;
-       u8 bank;
        u8 peci_mask;
        u8 old_peci_mask;
 
+       u8 smbus_bitmap;        /* !=0 if SMBus needs to be disabled */
+       u8 ec_special_config;   /* EC special config register restore value */
+       u8 sioaddr;             /* SIO port address */
+       bool doexit;            /* true if exit from sio config is ok */
+
        const u8 *REG_FAN;
        const u8 *REG_FANX;
        const u8 *REG_FAN_MIN;
@@ -723,6 +769,7 @@ struct it87_data {
        s8 temp[NUM_TEMP][4];   /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
        u8 num_temp_limit;      /* Number of temperature limit registers */
        u8 num_temp_offset;     /* Number of temperature offset registers */
+       u8 temp_src[4];         /* Up to 4 temperature source registers */
        u8 sensor;              /* Register value (IT87_REG_TEMP_ENABLE) */
        u8 extra;               /* Register value (IT87_REG_TEMP_EXTRA) */
        u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
@@ -749,6 +796,9 @@ struct it87_data {
        u8 pwm_ctrl[NUM_PWM];   /* Register value */
        u8 pwm_duty[NUM_PWM];   /* Manual PWM value set by user */
        u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
+       u8 pwm_temp_map_mask;   /* 0x03 for old, 0x07 for new temp map */
+       u8 pwm_temp_map_shift;  /* 0 for old, 3 for new temp map */
+       u8 pwm_num_temp_map;    /* from config data, 3..7 depending on chip */
 
        /* Automatic fan speed control registers */
        u8 auto_pwm[NUM_AUTO_PWM][4];   /* [nr][3] is hard-coded */
@@ -835,6 +885,25 @@ static int DIV_TO_REG(int val)
 
 #define DIV_FROM_REG(val) BIT(val)
 
+static u8 temp_map_from_reg(const struct it87_data *data, u8 reg)
+{
+       u8 map;
+
+       map  = (reg >> data->pwm_temp_map_shift) & data->pwm_temp_map_mask;
+       if (map >= data->pwm_num_temp_map)      /* map is 0-based */
+               map = 0;
+
+       return map;
+}
+
+static u8 temp_map_to_reg(const struct it87_data *data, int nr, u8 map)
+{
+       u8 ctrl = data->pwm_ctrl[nr];
+
+       return (ctrl & ~(data->pwm_temp_map_mask << data->pwm_temp_map_shift)) |
+              (map << data->pwm_temp_map_shift);
+}
+
 /*
  * PWM base frequencies. The frequency has to be divided by either 128 or 256,
  * depending on the chip type, to calculate the actual PWM frequency.
@@ -856,6 +925,39 @@ static const unsigned int pwm_freq[8] = {
        750000,
 };
 
+static int smbus_disable(struct it87_data *data)
+{
+       int err;
+
+       if (data->smbus_bitmap) {
+               err = superio_enter(data->sioaddr);
+               if (err)
+                       return err;
+               superio_select(data->sioaddr, PME);
+               superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
+                            data->ec_special_config & ~data->smbus_bitmap);
+               superio_exit(data->sioaddr, data->doexit);
+       }
+       return 0;
+}
+
+static int smbus_enable(struct it87_data *data)
+{
+       int err;
+
+       if (data->smbus_bitmap) {
+               err = superio_enter(data->sioaddr);
+               if (err)
+                       return err;
+
+               superio_select(data->sioaddr, PME);
+               superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
+                            data->ec_special_config);
+               superio_exit(data->sioaddr, data->doexit);
+       }
+       return 0;
+}
+
 static int _it87_read_value(struct it87_data *data, u8 reg)
 {
        outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
@@ -868,55 +970,71 @@ static void _it87_write_value(struct it87_data *data, u8 reg, u8 value)
        outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
 }
 
-static void it87_set_bank(struct it87_data *data, u8 bank)
+static u8 it87_set_bank(struct it87_data *data, u8 bank)
 {
-       if (has_bank_sel(data) && bank != data->bank) {
+       u8 _bank = bank;
+
+       if (has_bank_sel(data)) {
                u8 breg = _it87_read_value(data, IT87_REG_BANK);
 
-               breg &= 0x1f;
-               breg |= (bank << 5);
-               data->bank = bank;
-               _it87_write_value(data, IT87_REG_BANK, breg);
+               _bank = breg >> 5;
+               if (bank != _bank) {
+                       breg &= 0x1f;
+                       breg |= (bank << 5);
+                       _it87_write_value(data, IT87_REG_BANK, breg);
+               }
        }
+       return _bank;
 }
 
 /*
  * Must be called with data->update_lock held, except during initialization.
+ * Must be called with SMBus accesses disabled.
  * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
  * would slow down the IT87 access and should not be necessary.
  */
 static int it87_read_value(struct it87_data *data, u16 reg)
 {
-       it87_set_bank(data, reg >> 8);
-       return _it87_read_value(data, reg & 0xff);
+       u8 bank;
+       int val;
+
+       bank = it87_set_bank(data, reg >> 8);
+       val = _it87_read_value(data, reg & 0xff);
+       it87_set_bank(data, bank);
+
+       return val;
 }
 
 /*
  * Must be called with data->update_lock held, except during initialization.
+ * Must be called with SMBus accesses disabled
  * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
  * would slow down the IT87 access and should not be necessary.
  */
 static void it87_write_value(struct it87_data *data, u16 reg, u8 value)
 {
-       it87_set_bank(data, reg >> 8);
+       u8 bank;
+
+       bank = it87_set_bank(data, reg >> 8);
        _it87_write_value(data, reg & 0xff, value);
+       it87_set_bank(data, bank);
 }
 
 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
 {
-       data->pwm_ctrl[nr] = it87_read_value(data, data->REG_PWM[nr]);
+       u8 ctrl;
+
+       ctrl = it87_read_value(data, data->REG_PWM[nr]);
+       data->pwm_ctrl[nr] = ctrl;
        if (has_newer_autopwm(data)) {
-               if (has_new_tempmap(data))
-                       data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x38;
-               else
-                       data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
+               data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
                data->pwm_duty[nr] = it87_read_value(data,
                                                     IT87_REG_PWM_DUTY[nr]);
        } else {
-               if (data->pwm_ctrl[nr] & 0x80)  /* Automatic mode */
-                       data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
+               if (ctrl & 0x80)        /* Automatic mode */
+                       data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
                else                            /* Manual mode */
-                       data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
+                       data->pwm_duty[nr] = ctrl & 0x7f;
        }
 
        if (has_old_autopwm(data)) {
@@ -955,12 +1073,32 @@ static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
        }
 }
 
+static int it87_lock(struct it87_data *data)
+{
+       int err;
+
+       mutex_lock(&data->update_lock);
+       err = smbus_disable(data);
+       if (err)
+               mutex_unlock(&data->update_lock);
+       return err;
+}
+
+static void it87_unlock(struct it87_data *data)
+{
+       smbus_enable(data);
+       mutex_unlock(&data->update_lock);
+}
+
 static struct it87_data *it87_update_device(struct device *dev)
 {
        struct it87_data *data = dev_get_drvdata(dev);
+       int err;
        int i;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return ERR_PTR(err);
 
        if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
            !data->valid) {
@@ -1067,9 +1205,7 @@ static struct it87_data *it87_update_device(struct device *dev)
                data->last_updated = jiffies;
                data->valid = 1;
        }
-
-       mutex_unlock(&data->update_lock);
-
+       it87_unlock(data);
        return data;
 }
 
@@ -1081,6 +1217,9 @@ static ssize_t show_in(struct device *dev, struct device_attribute *attr,
        int index = sattr->index;
        int nr = sattr->nr;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
 }
 
@@ -1092,17 +1231,21 @@ static ssize_t set_in(struct device *dev, struct device_attribute *attr,
        int index = sattr->index;
        int nr = sattr->nr;
        unsigned long val;
+       int err;
 
        if (kstrtoul(buf, 10, &val) < 0)
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
+
        data->in[nr][index] = in_to_reg(data, nr, val);
        it87_write_value(data,
                         index == 1 ? IT87_REG_VIN_MIN(nr)
                                    : IT87_REG_VIN_MAX(nr),
                         data->in[nr][index]);
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -1169,6 +1312,9 @@ static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
        int index = sattr->index;
        struct it87_data *data = it87_update_device(dev);
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
 }
 
@@ -1181,11 +1327,14 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = dev_get_drvdata(dev);
        long val;
        u8 reg, regval;
+       int err;
 
        if (kstrtol(buf, 10, &val) < 0)
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
 
        switch (index) {
        default:
@@ -1208,7 +1357,7 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
 
        data->temp[nr][index] = TEMP_TO_REG(val);
        it87_write_value(data, reg, data->temp[nr][index]);
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -1255,37 +1404,29 @@ static SENSOR_DEVICE_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
 static SENSOR_DEVICE_ATTR_2(temp6_offset, S_IRUGO | S_IWUSR, show_temp,
                            set_temp, 5, 3);
 
+static const u8 temp_types_8686[NUM_TEMP][9] = {
+       { 0, 8, 8, 8, 8, 8, 8, 8, 7 },
+       { 0, 6, 8, 8, 6, 0, 0, 0, 7 },
+       { 0, 6, 5, 8, 6, 0, 0, 0, 7 },
+       { 4, 8, 8, 8, 8, 8, 8, 8, 7 },
+       { 4, 6, 8, 8, 6, 0, 0, 0, 7 },
+       { 4, 6, 5, 8, 6, 0, 0, 0, 7 },
+};
+
 static int get_temp_type(struct it87_data *data, int index)
 {
        u8 reg, extra;
        int type = 0;
 
        if (has_bank_sel(data)) {
-               int s1reg = IT87_REG_TEMP_SRC1[index/2] >> ((index % 2) * 4);
                u8 src1, src2;
 
-               src1 = (it87_read_value(data, s1reg) >> ((index % 2) * 4)) & 0x0f;
-               src2 = it87_read_value(data, IT87_REG_TEMP_SRC2);
+               src1 = (data->temp_src[index / 2] >> ((index % 2) * 4)) & 0x0f;
 
                switch (data->type) {
                case it8686:
-                       switch (src1) {
-                       case 0:
-                               if (index >= 3)
-                                       return 4;
-                               break;
-                       case 1:
-                               if (index == 1 || index == 2 ||
-                                         index == 4 || index == 5)
-                                       return 6;
-                               break;
-                       case 2:
-                               if (index == 2 || index == 6)
-                                       return 5;
-                               break;
-                       default:
-                               break;
-                       }
+                       if (src1 < 9)
+                               type = temp_types_8686[index][src1];
                        break;
                case it8625:
                        if (index < 3)
@@ -1296,6 +1437,7 @@ static int get_temp_type(struct it87_data *data, int index)
                                index = src1;
                                break;
                        }
+                       src2 = data->temp_src[3];
                        switch(src1) {
                        case 3:
                                type = (src2 & BIT(index)) ? 6 : 5;
@@ -1314,8 +1456,8 @@ static int get_temp_type(struct it87_data *data, int index)
                        return 0;
                }
        }
-       if (index >= 3)
-               return 0;
+       if (type || index >= 3)
+               return type;
 
        reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
        extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
@@ -1336,8 +1478,12 @@ static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
 {
        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
        struct it87_data *data = it87_update_device(dev);
-       int type = get_temp_type(data, sensor_attr->index);
+       int type;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
+       type = get_temp_type(data, sensor_attr->index);
        return sprintf(buf, "%d\n", type);
 }
 
@@ -1350,10 +1496,15 @@ static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = dev_get_drvdata(dev);
        long val;
        u8 reg, extra;
+       int err;
 
        if (kstrtol(buf, 10, &val) < 0)
                return -EINVAL;
 
+       err = it87_lock(data);
+       if (err)
+               return err;
+
        reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
        reg &= ~(1 << nr);
        reg &= ~(8 << nr);
@@ -1376,17 +1527,19 @@ static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
                reg |= (nr + 1) << 6;
        else if (has_temp_old_peci(data, nr) && val == 6)
                extra |= 0x80;
-       else if (val != 0)
-               return -EINVAL;
+       else if (val != 0) {
+               count = -EINVAL;
+               goto unlock;
+       }
 
-       mutex_lock(&data->update_lock);
        data->sensor = reg;
        data->extra = extra;
        it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
        if (has_temp_old_peci(data, nr))
                it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
        data->valid = 0;        /* Force cache refresh */
-       mutex_unlock(&data->update_lock);
+unlock:
+       it87_unlock(data);
        return count;
 }
 
@@ -1428,6 +1581,9 @@ static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
        int speed;
        struct it87_data *data = it87_update_device(dev);
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        speed = has_16bit_fans(data) ?
                FAN16_FROM_REG(data->fan[nr][index]) :
                FAN_FROM_REG(data->fan[nr][index],
@@ -1442,6 +1598,9 @@ static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = it87_update_device(dev);
        int nr = sensor_attr->index;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
 }
 
@@ -1452,6 +1611,9 @@ static ssize_t show_pwm_enable(struct device *dev,
        struct it87_data *data = it87_update_device(dev);
        int nr = sensor_attr->index;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%d\n", pwm_mode(data, nr));
 }
 
@@ -1462,6 +1624,9 @@ static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = it87_update_device(dev);
        int nr = sensor_attr->index;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%d\n",
                       pwm_from_reg(data, data->pwm_duty[nr]));
 }
@@ -1475,6 +1640,9 @@ static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
        unsigned int freq;
        int index;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        if (has_pwm_freq2(data) && nr == 1)
                index = (data->extra >> 4) & 0x07;
        else
@@ -1494,12 +1662,15 @@ static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
 
        struct it87_data *data = dev_get_drvdata(dev);
        long val;
+       int err;
        u8 reg;
 
        if (kstrtol(buf, 10, &val) < 0)
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
 
        if (has_16bit_fans(data)) {
                data->fan[nr][index] = FAN16_TO_REG(val);
@@ -1525,8 +1696,7 @@ static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
                it87_write_value(data, data->REG_FAN_MIN[nr],
                                 data->fan[nr][index]);
        }
-
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -1537,13 +1707,16 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = dev_get_drvdata(dev);
        int nr = sensor_attr->index;
        unsigned long val;
-       int min;
+       int min, err;
        u8 old;
 
        if (kstrtoul(buf, 10, &val) < 0)
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
+
        old = it87_read_value(data, IT87_REG_FAN_DIV);
 
        /* Save fan min limit */
@@ -1570,8 +1743,7 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
        /* Restore fan min limit */
        data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
        it87_write_value(data, data->REG_FAN_MIN[nr], data->fan[nr][1]);
-
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -1612,6 +1784,7 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = dev_get_drvdata(dev);
        int nr = sensor_attr->index;
        long val;
+       int err;
 
        if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
                return -EINVAL;
@@ -1622,7 +1795,11 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
                        return -EINVAL;
        }
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;;
+
+       it87_update_pwm_ctrl(data, nr);
 
        if (val == 0) {
                if (nr < 3 && has_fanctl_onoff(data)) {
@@ -1643,8 +1820,9 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
                                         data->pwm_duty[nr]);
                        /* and set manual mode */
                        if (has_newer_autopwm(data)) {
-                               ctrl = (data->pwm_ctrl[nr] & 0x7c) |
-                                       data->pwm_temp_map[nr];
+                               ctrl = temp_map_to_reg(data, nr,
+                                                      data->pwm_temp_map[nr]);
+                               ctrl &= 0x7f;
                        } else {
                                ctrl = data->pwm_duty[nr];
                        }
@@ -1655,9 +1833,11 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
                u8 ctrl;
 
                if (has_newer_autopwm(data)) {
-                       ctrl = (data->pwm_ctrl[nr] & 0x7c) |
-                               data->pwm_temp_map[nr];
-                       if (val != 1)
+                       ctrl = temp_map_to_reg(data, nr,
+                                              data->pwm_temp_map[nr]);
+                       if (val == 1)
+                               ctrl &= 0x7f;
+                       else
                                ctrl |= 0x80;
                } else {
                        ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
@@ -1672,8 +1852,7 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
                                         data->fan_main_ctrl);
                }
        }
-
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -1684,11 +1863,15 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = dev_get_drvdata(dev);
        int nr = sensor_attr->index;
        long val;
+       int err;
 
        if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
+
        it87_update_pwm_ctrl(data, nr);
        if (has_newer_autopwm(data)) {
                /*
@@ -1696,8 +1879,8 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
                 * is read-only so we can't write the value.
                 */
                if (data->pwm_ctrl[nr] & 0x80) {
-                       mutex_unlock(&data->update_lock);
-                       return -EBUSY;
+                       count = -EBUSY;
+                       goto unlock;
                }
                data->pwm_duty[nr] = pwm_to_reg(data, val);
                it87_write_value(data, IT87_REG_PWM_DUTY[nr],
@@ -1714,7 +1897,8 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
                                         data->pwm_ctrl[nr]);
                }
        }
-       mutex_unlock(&data->update_lock);
+unlock:
+       it87_unlock(data);
        return count;
 }
 
@@ -1725,6 +1909,7 @@ static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = dev_get_drvdata(dev);
        int nr = sensor_attr->index;
        unsigned long val;
+       int err;
        int i;
 
        if (kstrtoul(buf, 10, &val) < 0)
@@ -1739,7 +1924,10 @@ static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
                        break;
        }
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
+
        if (nr == 0) {
                data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
                data->fan_ctl |= i << 4;
@@ -1749,8 +1937,7 @@ static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
                data->extra |= i << 4;
                it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
        }
-       mutex_unlock(&data->update_lock);
-
+       it87_unlock(data);
        return count;
 }
 
@@ -1760,21 +1947,11 @@ static ssize_t show_pwm_temp_map(struct device *dev,
        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
        struct it87_data *data = it87_update_device(dev);
        int nr = sensor_attr->index;
-       int map;
 
-       map = data->pwm_temp_map[nr];
-       if (has_new_tempmap(data)) {
-               map >>= 3;
-               if (map >= 6)
-                       map = 0;        /* Should never happen */
-       } else {
-               if (map >= 3)
-                       map = 0;        /* Should never happen */
-               if (nr >= 3)            /* pwm channels 3..6 map to temp4..6 */
-                       map += 3;
-       }
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
-       return sprintf(buf, "%d\n", (int)BIT(map));
+       return sprintf(buf, "%d\n", data->pwm_temp_map[nr] + 1);
 }
 
 static ssize_t set_pwm_temp_map(struct device *dev,
@@ -1784,61 +1961,33 @@ static ssize_t set_pwm_temp_map(struct device *dev,
        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
        struct it87_data *data = dev_get_drvdata(dev);
        int nr = sensor_attr->index;
-       long val;
-       u8 reg;
+       unsigned long val;
+       int err;
+       u8 map;
 
-       if (kstrtol(buf, 10, &val) < 0)
+       if (kstrtoul(buf, 10, &val) < 0)
                return -EINVAL;
 
-       if (nr >= 3 && !has_new_tempmap(data))
-               val -= 3;
-
-       switch (val) {
-       case BIT(0):
-               reg = 0x00;
-               break;
-       case BIT(1):
-               reg = 0x01;
-               break;
-       case BIT(2):
-               reg = 0x02;
-               break;
-       case BIT(3):
-               reg = 0x03;
-               break;
-       case BIT(4):
-               reg = 0x04;
-               break;
-       case BIT(5):
-               reg = 0x05;
-               break;
-       case BIT(6):
-               reg = 0x06;
-               break;
-       default:
+       if (!val || val > data->pwm_num_temp_map)
                return -EINVAL;
-       }
 
-       if (has_new_tempmap(data))
-               reg <<= 3;
-       else if (reg > 0x02)
-               return -EINVAL;
+       map = val - 1;
+
+       err = it87_lock(data);
+       if (err)
+               return err;
 
-       mutex_lock(&data->update_lock);
        it87_update_pwm_ctrl(data, nr);
-       data->pwm_temp_map[nr] = reg;
+       data->pwm_temp_map[nr] = map;
        /*
         * If we are in automatic mode, write the temp mapping immediately;
         * otherwise, just store it for later use.
         */
        if (data->pwm_ctrl[nr] & 0x80) {
-               u8 mask = has_new_tempmap(data) ? 0xc7 : 0xfc;
-
-               data->pwm_ctrl[nr] = (data->pwm_ctrl[nr] & mask) |
-                                               data->pwm_temp_map[nr];
+               data->pwm_ctrl[nr] = temp_map_to_reg(data, nr, map);
                it87_write_value(data, data->REG_PWM[nr], data->pwm_ctrl[nr]);
        }
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -1851,6 +2000,9 @@ static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
        int nr = sensor_attr->nr;
        int point = sensor_attr->index;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%d\n",
                       pwm_from_reg(data, data->auto_pwm[nr][point]));
 }
@@ -1865,18 +2017,22 @@ static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
        int point = sensor_attr->index;
        int regaddr;
        long val;
+       int err;
 
        if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
+
        data->auto_pwm[nr][point] = pwm_to_reg(data, val);
        if (has_newer_autopwm(data))
                regaddr = IT87_REG_AUTO_TEMP(nr, 3);
        else
                regaddr = IT87_REG_AUTO_PWM(nr, point);
        it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -1887,6 +2043,9 @@ static ssize_t show_auto_pwm_slope(struct device *dev,
        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
        int nr = sensor_attr->index;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
 }
 
@@ -1898,15 +2057,19 @@ static ssize_t set_auto_pwm_slope(struct device *dev,
        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
        int nr = sensor_attr->index;
        unsigned long val;
+       int err;
 
        if (kstrtoul(buf, 10, &val) < 0 || val > 127)
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
+
        data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
        it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
                         data->auto_pwm[nr][1]);
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -1920,6 +2083,9 @@ static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
        int point = sensor_attr->index;
        int reg;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        if (has_old_autopwm(data) || point)
                reg = data->auto_temp[nr][point];
        else
@@ -1938,11 +2104,15 @@ static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
        int point = sensor_attr->index;
        long val;
        int reg;
+       int err;
 
        if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
+
        if (has_newer_autopwm(data) && !point) {
                reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
                reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
@@ -1955,7 +2125,7 @@ static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
                        point--;
                it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
        }
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -2140,6 +2310,9 @@ static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
 {
        struct it87_data *data = it87_update_device(dev);
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%u\n", data->alarms);
 }
 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
@@ -2150,6 +2323,9 @@ static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = it87_update_device(dev);
        int bitnr = to_sensor_dev_attr(attr)->index;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
 }
 
@@ -2158,24 +2334,22 @@ static ssize_t clear_intrusion(struct device *dev,
                               size_t count)
 {
        struct it87_data *data = dev_get_drvdata(dev);
-       int config;
+       int err, config;
        long val;
 
        if (kstrtol(buf, 10, &val) < 0 || val != 0)
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
-       config = it87_read_value(data, IT87_REG_CONFIG);
-       if (config < 0) {
-               count = config;
-       } else {
-               config |= BIT(5);
-               it87_write_value(data, IT87_REG_CONFIG, config);
-               /* Invalidate cache to force re-read */
-               data->valid = 0;
-       }
-       mutex_unlock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
 
+       config = it87_read_value(data, IT87_REG_CONFIG);
+       config |= BIT(5);
+       it87_write_value(data, IT87_REG_CONFIG, config);
+       /* Invalidate cache to force re-read */
+       data->valid = 0;
+       it87_unlock(data);
        return count;
 }
 
@@ -2208,6 +2382,9 @@ static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
        struct it87_data *data = it87_update_device(dev);
        int bitnr = to_sensor_dev_attr(attr)->index;
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
 }
 
@@ -2217,18 +2394,22 @@ static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
        int bitnr = to_sensor_dev_attr(attr)->index;
        struct it87_data *data = dev_get_drvdata(dev);
        long val;
+       int err;
 
        if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
                return -EINVAL;
 
-       mutex_lock(&data->update_lock);
+       err = it87_lock(data);
+       if (err)
+               return err;
+
        data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
        if (val)
                data->beeps |= BIT(bitnr);
        else
                data->beeps &= ~BIT(bitnr);
        it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
-       mutex_unlock(&data->update_lock);
+       it87_unlock(data);
        return count;
 }
 
@@ -2284,6 +2465,9 @@ static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
 {
        struct it87_data *data = it87_update_device(dev);
 
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
        return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
 }
 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
@@ -2769,6 +2953,8 @@ static int __init it87_find(int sioaddr, unsigned short *address,
        if (err)
                return err;
 
+       sio_data->sioaddr = sioaddr;
+
        err = -ENODEV;
        chip_type = superio_inw(sioaddr, DEVID);
        if (chip_type == 0xffff)
@@ -2833,6 +3019,7 @@ static int __init it87_find(int sioaddr, unsigned short *address,
                break;
        case IT8790E_DEVID:
                sio_data->type = it8790;
+               doexit = false;         /* See IT8792E comment above */
                break;
        case IT8603E_DEVID:
        case IT8623E_DEVID:
@@ -2884,6 +3071,8 @@ static int __init it87_find(int sioaddr, unsigned short *address,
                goto exit;
        }
 
+       sio_data->doexit = doexit;
+
        err = 0;
        sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
        pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
@@ -3216,6 +3405,10 @@ static int __init it87_find(int sioaddr, unsigned short *address,
                /* Check for pwm2, fan2 */
                if (reg29 & BIT(1))
                        sio_data->skip_pwm |= BIT(1);
+               /*
+                * Note: Table 6-1 in datasheet claims that FAN_TAC2
+                * would be enabled with 29h[2]=0.
+                */
                if (reg2d & BIT(4))
                        sio_data->skip_fan |= BIT(1);
 
@@ -3246,7 +3439,7 @@ static int __init it87_find(int sioaddr, unsigned short *address,
                                sio_data->skip_fan |= BIT(3);
                        if (reg26 & BIT(5))
                                sio_data->skip_pwm |= BIT(4);
-                       if (!(reg26 & BIT(4)))
+                       if (reg26 & BIT(4))
                                sio_data->skip_fan |= BIT(4);
                }
 
@@ -3362,6 +3555,15 @@ static int __init it87_find(int sioaddr, unsigned short *address,
        if (sio_data->beep_pin)
                pr_info("Beeping is supported\n");
 
+       if (config->smbus_bitmap) {
+               u8 reg;
+
+               superio_select(sioaddr, PME);
+               reg = superio_inb(sioaddr, IT87_SPECIAL_CFG_REG);
+               sio_data->ec_special_config = reg;
+               sio_data->smbus_bitmap = reg & config->smbus_bitmap;
+       }
+
 exit:
        superio_exit(sioaddr, doexit);
        return err;
@@ -3437,13 +3639,21 @@ static void it87_init_device(struct platform_device *pdev)
        int tmp, i;
        u8 mask;
 
+       if (has_new_tempmap(data)) {
+               data->pwm_temp_map_shift = 3;
+               data->pwm_temp_map_mask = 0x07;
+       } else {
+               data->pwm_temp_map_shift = 0;
+               data->pwm_temp_map_mask = 0x03;
+       }
+
        /*
         * For each PWM channel:
         * - If it is in automatic mode, setting to manual mode should set
         *   the fan to full speed by default.
         * - If it is in manual mode, we need a mapping to temperature
         *   channels to use when later setting to automatic mode later.
-        *   Use a 1:1 mapping by default (we are clueless.)
+        *   Map to the first sensor by default (we are clueless.)
         * In both cases, the value can (and should) be changed by the user
         * prior to switching to a different mode.
         * Note that this is no longer needed for the IT8721F and later, as
@@ -3451,7 +3661,7 @@ static void it87_init_device(struct platform_device *pdev)
         * manual duty cycle.
         */
        for (i = 0; i < NUM_AUTO_PWM; i++) {
-               data->pwm_temp_map[i] = i;
+               data->pwm_temp_map[i] = 0;
                data->pwm_duty[i] = 0x7f;       /* Full speed */
                data->auto_pwm[i][3] = 0x7f;    /* Full speed, hard-coded */
        }
@@ -3552,6 +3762,13 @@ static void it87_init_device(struct platform_device *pdev)
                }
        }
 
+       if (has_bank_sel(data)) {
+               for (i = 0; i < 3; i++)
+                       data->temp_src[i] =
+                               it87_read_value(data, IT87_REG_TEMP_SRC1[i]);
+               data->temp_src[3] = it87_read_value(data, IT87_REG_TEMP_SRC2);
+       }
+
        /* Start monitoring */
        it87_write_value(data, IT87_REG_CONFIG,
                         (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
@@ -3624,6 +3841,7 @@ static int it87_probe(struct platform_device *pdev)
        struct it87_sio_data *sio_data = dev_get_platdata(dev);
        int enable_pwm_interface;
        struct device *hwmon_dev;
+       int err;
 
        res = platform_get_resource(pdev, IORESOURCE_IO, 0);
        if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
@@ -3640,12 +3858,16 @@ static int it87_probe(struct platform_device *pdev)
 
        data->addr = res->start;
        data->type = sio_data->type;
+       data->sioaddr = sio_data->sioaddr;
+       data->smbus_bitmap = sio_data->smbus_bitmap;
+       data->ec_special_config = sio_data->ec_special_config;
+       data->doexit = sio_data->doexit;
        data->features = it87_devices[sio_data->type].features;
        data->num_temp_limit = it87_devices[sio_data->type].num_temp_limit;
        data->num_temp_offset = it87_devices[sio_data->type].num_temp_offset;
+       data->pwm_num_temp_map = it87_devices[sio_data->type].num_temp_map;
        data->peci_mask = it87_devices[sio_data->type].peci_mask;
        data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
-       data->bank = 0xff;
 
        /*
         * IT8705F Datasheet 0.4.1, 3h == Version G.
@@ -3670,11 +3892,6 @@ static int it87_probe(struct platform_device *pdev)
                break;
        }
 
-       /* Now, we do the remaining detection. */
-       if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
-           it87_read_value(data, IT87_REG_CHIPID) != 0x90)
-               return -ENODEV;
-
        platform_set_drvdata(pdev, data);
 
        mutex_init(&data->update_lock);
@@ -3682,6 +3899,17 @@ static int it87_probe(struct platform_device *pdev)
        /* Initialize register pointers */
        it87_init_regs(pdev);
 
+       err = smbus_disable(data);
+       if (err)
+               return err;
+
+       /* Now, we do the remaining detection. */
+       if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
+           it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
+               smbus_enable(data);
+               return -ENODEV;
+       }
+
        /* Check PWM configuration */
        enable_pwm_interface = it87_check_pwm(dev);
 
@@ -3738,6 +3966,8 @@ static int it87_probe(struct platform_device *pdev)
        /* Initialize the IT87 chip */
        it87_init_device(pdev);
 
+       smbus_enable(data);
+
        if (!sio_data->skip_vid) {
                data->has_vid = true;
                data->vrm = vid_which_vrm();
@@ -3786,8 +4016,10 @@ static int __init it87_device_add(int index, unsigned short address,
        int err;
 
        err = acpi_check_resource_conflict(&res);
-       if (err)
-               return err;
+       if (err) {
+               if (!ignore_resource_conflict)
+                       return err;
+       }
 
        pdev = platform_device_alloc(DRVNAME, address);
        if (!pdev)
@@ -3821,9 +4053,28 @@ exit_device_put:
 }
 
 struct it87_dmi_data {
+       bool sio2_force_config; /* force sio2 into configuration mode   */
        u8 skip_pwm;            /* pwm channels to skip for this board  */
 };
 
+/*
+ * On various Gigabyte AM4 boards (AB350, AX370), the second Super-IO chip
+ * (IT8792E) needs to be in configuration mode before accessing the first
+ * due to a bug in IT8792E which otherwise results in LPC bus access errors.
+ * This needs to be done before accessing the first Super-IO chip since
+ * the second chip may have been accessed prior to loading this driver.
+ *
+ * The problem is also reported to affect IT8795E, which is used on X299 boards
+ * and has the same chip ID as IT8792E (0x8733). It also appears to affect
+ * systems with IT8790E, which is used on some Z97X-Gaming boards as well as
+ * Z87X-OC.
+ * DMI entries for those systems will be added as they become available and
+ * as the problem is confirmed to affect those boards.
+ */
+static struct it87_dmi_data gigabyte_sio2_force = {
+       .sio2_force_config = true,
+};
+
 /*
  * On the Shuttle SN68PT, FAN_CTL2 is apparently not
  * connected to a fan, but to something else. One user
@@ -3837,6 +4088,27 @@ static struct it87_dmi_data nvidia_fn68pt = {
 };
 
 static const struct dmi_system_id it87_dmi_table[] __initconst = {
+       {
+               .matches = {
+                       DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
+                       DMI_MATCH(DMI_BOARD_NAME, "AB350"),
+               },
+               .driver_data = &gigabyte_sio2_force,
+       },
+       {
+               .matches = {
+                       DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
+                       DMI_MATCH(DMI_BOARD_NAME, "AX370"),
+               },
+               .driver_data = &gigabyte_sio2_force,
+       },
+       {
+               .matches = {
+                       DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
+                       DMI_MATCH(DMI_BOARD_NAME, "Z97X-Gaming G1"),
+               },
+               .driver_data = &gigabyte_sio2_force,
+       },
        {
                .matches = {
                        DMI_MATCH(DMI_BOARD_VENDOR, "nVIDIA"),
@@ -3857,6 +4129,8 @@ static int __init sm_it87_init(void)
        bool found = false;
        int i, err;
 
+       pr_info("it87 driver version %s\n", IT87_DRIVER_VERSION);
+
        if (dmi)
                dmi_data = dmi->driver_data;
 
@@ -3864,6 +4138,9 @@ static int __init sm_it87_init(void)
        if (err)
                return err;
 
+       if (dmi_data && dmi_data->sio2_force_config)
+               __superio_enter(REG_4E);
+
        for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
                memset(&sio_data, 0, sizeof(struct it87_sio_data));
                isa_address = 0;