Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * w83l786ng.c - Linux kernel driver for hardware monitoring
0004  * Copyright (c) 2007 Kevin Lo <kevlo@kevlo.org>
0005  */
0006 
0007 /*
0008  * Supports following chips:
0009  *
0010  * Chip     #vin    #fanin  #pwm    #temp   wchipid vendid  i2c ISA
0011  * w83l786ng    3   2   2   2   0x7b    0x5ca3  yes no
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/slab.h>
0017 #include <linux/i2c.h>
0018 #include <linux/hwmon.h>
0019 #include <linux/hwmon-vid.h>
0020 #include <linux/hwmon-sysfs.h>
0021 #include <linux/err.h>
0022 #include <linux/mutex.h>
0023 #include <linux/jiffies.h>
0024 
0025 /* Addresses to scan */
0026 static const unsigned short normal_i2c[] = { 0x2e, 0x2f, I2C_CLIENT_END };
0027 
0028 /* Insmod parameters */
0029 
0030 static bool reset;
0031 module_param(reset, bool, 0);
0032 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
0033 
0034 #define W83L786NG_REG_IN_MIN(nr)    (0x2C + (nr) * 2)
0035 #define W83L786NG_REG_IN_MAX(nr)    (0x2B + (nr) * 2)
0036 #define W83L786NG_REG_IN(nr)        ((nr) + 0x20)
0037 
0038 #define W83L786NG_REG_FAN(nr)       ((nr) + 0x28)
0039 #define W83L786NG_REG_FAN_MIN(nr)   ((nr) + 0x3B)
0040 
0041 #define W83L786NG_REG_CONFIG        0x40
0042 #define W83L786NG_REG_ALARM1        0x41
0043 #define W83L786NG_REG_ALARM2        0x42
0044 #define W83L786NG_REG_GPIO_EN       0x47
0045 #define W83L786NG_REG_MAN_ID2       0x4C
0046 #define W83L786NG_REG_MAN_ID1       0x4D
0047 #define W83L786NG_REG_CHIP_ID       0x4E
0048 
0049 #define W83L786NG_REG_DIODE     0x53
0050 #define W83L786NG_REG_FAN_DIV       0x54
0051 #define W83L786NG_REG_FAN_CFG       0x80
0052 
0053 #define W83L786NG_REG_TOLERANCE     0x8D
0054 
0055 static const u8 W83L786NG_REG_TEMP[2][3] = {
0056     { 0x25,     /* TEMP 0 in DataSheet */
0057       0x35,     /* TEMP 0 Over in DataSheet */
0058       0x36 },   /* TEMP 0 Hyst in DataSheet */
0059     { 0x26,     /* TEMP 1 in DataSheet */
0060       0x37,     /* TEMP 1 Over in DataSheet */
0061       0x38 }    /* TEMP 1 Hyst in DataSheet */
0062 };
0063 
0064 static const u8 W83L786NG_PWM_MODE_SHIFT[] = {6, 7};
0065 static const u8 W83L786NG_PWM_ENABLE_SHIFT[] = {2, 4};
0066 
0067 /* FAN Duty Cycle, be used to control */
0068 static const u8 W83L786NG_REG_PWM[] = {0x81, 0x87};
0069 
0070 
0071 static inline u8
0072 FAN_TO_REG(long rpm, int div)
0073 {
0074     if (rpm == 0)
0075         return 255;
0076     rpm = clamp_val(rpm, 1, 1000000);
0077     return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
0078 }
0079 
0080 #define FAN_FROM_REG(val, div)  ((val) == 0   ? -1 : \
0081                 ((val) == 255 ? 0 : \
0082                 1350000 / ((val) * (div))))
0083 
0084 /* for temp */
0085 #define TEMP_TO_REG(val)    (clamp_val(((val) < 0 ? (val) + 0x100 * 1000 \
0086                               : (val)) / 1000, 0, 0xff))
0087 #define TEMP_FROM_REG(val)  (((val) & 0x80 ? \
0088                   (val) - 0x100 : (val)) * 1000)
0089 
0090 /*
0091  * The analog voltage inputs have 8mV LSB. Since the sysfs output is
0092  * in mV as would be measured on the chip input pin, need to just
0093  * multiply/divide by 8 to translate from/to register values.
0094  */
0095 #define IN_TO_REG(val)      (clamp_val((((val) + 4) / 8), 0, 255))
0096 #define IN_FROM_REG(val)    ((val) * 8)
0097 
0098 #define DIV_FROM_REG(val)   (1 << (val))
0099 
0100 static inline u8
0101 DIV_TO_REG(long val)
0102 {
0103     int i;
0104     val = clamp_val(val, 1, 128) >> 1;
0105     for (i = 0; i < 7; i++) {
0106         if (val == 0)
0107             break;
0108         val >>= 1;
0109     }
0110     return (u8)i;
0111 }
0112 
0113 struct w83l786ng_data {
0114     struct i2c_client *client;
0115     struct mutex update_lock;
0116     bool valid;         /* true if following fields are valid */
0117     unsigned long last_updated; /* In jiffies */
0118     unsigned long last_nonvolatile; /* In jiffies, last time we update the
0119                      * nonvolatile registers */
0120 
0121     u8 in[3];
0122     u8 in_max[3];
0123     u8 in_min[3];
0124     u8 fan[2];
0125     u8 fan_div[2];
0126     u8 fan_min[2];
0127     u8 temp_type[2];
0128     u8 temp[2][3];
0129     u8 pwm[2];
0130     u8 pwm_mode[2]; /* 0->DC variable voltage
0131              * 1->PWM variable duty cycle */
0132 
0133     u8 pwm_enable[2]; /* 1->manual
0134                * 2->thermal cruise (also called SmartFan I) */
0135     u8 tolerance[2];
0136 };
0137 
0138 static u8
0139 w83l786ng_read_value(struct i2c_client *client, u8 reg)
0140 {
0141     return i2c_smbus_read_byte_data(client, reg);
0142 }
0143 
0144 static int
0145 w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value)
0146 {
0147     return i2c_smbus_write_byte_data(client, reg, value);
0148 }
0149 
0150 static struct w83l786ng_data *w83l786ng_update_device(struct device *dev)
0151 {
0152     struct w83l786ng_data *data = dev_get_drvdata(dev);
0153     struct i2c_client *client = data->client;
0154     int i, j;
0155     u8 reg_tmp, pwmcfg;
0156 
0157     mutex_lock(&data->update_lock);
0158     if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
0159         || !data->valid) {
0160         dev_dbg(&client->dev, "Updating w83l786ng data.\n");
0161 
0162         /* Update the voltages measured value and limits */
0163         for (i = 0; i < 3; i++) {
0164             data->in[i] = w83l786ng_read_value(client,
0165                 W83L786NG_REG_IN(i));
0166             data->in_min[i] = w83l786ng_read_value(client,
0167                 W83L786NG_REG_IN_MIN(i));
0168             data->in_max[i] = w83l786ng_read_value(client,
0169                 W83L786NG_REG_IN_MAX(i));
0170         }
0171 
0172         /* Update the fan counts and limits */
0173         for (i = 0; i < 2; i++) {
0174             data->fan[i] = w83l786ng_read_value(client,
0175                 W83L786NG_REG_FAN(i));
0176             data->fan_min[i] = w83l786ng_read_value(client,
0177                 W83L786NG_REG_FAN_MIN(i));
0178         }
0179 
0180         /* Update the fan divisor */
0181         reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
0182         data->fan_div[0] = reg_tmp & 0x07;
0183         data->fan_div[1] = (reg_tmp >> 4) & 0x07;
0184 
0185         pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
0186         for (i = 0; i < 2; i++) {
0187             data->pwm_mode[i] =
0188                 ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
0189                 ? 0 : 1;
0190             data->pwm_enable[i] =
0191                 ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1;
0192             data->pwm[i] =
0193                 (w83l786ng_read_value(client, W83L786NG_REG_PWM[i])
0194                  & 0x0f) * 0x11;
0195         }
0196 
0197 
0198         /* Update the temperature sensors */
0199         for (i = 0; i < 2; i++) {
0200             for (j = 0; j < 3; j++) {
0201                 data->temp[i][j] = w83l786ng_read_value(client,
0202                     W83L786NG_REG_TEMP[i][j]);
0203             }
0204         }
0205 
0206         /* Update Smart Fan I/II tolerance */
0207         reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE);
0208         data->tolerance[0] = reg_tmp & 0x0f;
0209         data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
0210 
0211         data->last_updated = jiffies;
0212         data->valid = true;
0213 
0214     }
0215 
0216     mutex_unlock(&data->update_lock);
0217 
0218     return data;
0219 }
0220 
0221 /* following are the sysfs callback functions */
0222 #define show_in_reg(reg) \
0223 static ssize_t \
0224 show_##reg(struct device *dev, struct device_attribute *attr, \
0225        char *buf) \
0226 { \
0227     int nr = to_sensor_dev_attr(attr)->index; \
0228     struct w83l786ng_data *data = w83l786ng_update_device(dev); \
0229     return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
0230 }
0231 
0232 show_in_reg(in)
0233 show_in_reg(in_min)
0234 show_in_reg(in_max)
0235 
0236 #define store_in_reg(REG, reg) \
0237 static ssize_t \
0238 store_in_##reg(struct device *dev, struct device_attribute *attr, \
0239            const char *buf, size_t count) \
0240 { \
0241     int nr = to_sensor_dev_attr(attr)->index; \
0242     struct w83l786ng_data *data = dev_get_drvdata(dev); \
0243     struct i2c_client *client = data->client; \
0244     unsigned long val; \
0245     int err = kstrtoul(buf, 10, &val); \
0246     if (err) \
0247         return err; \
0248     mutex_lock(&data->update_lock); \
0249     data->in_##reg[nr] = IN_TO_REG(val); \
0250     w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \
0251                   data->in_##reg[nr]); \
0252     mutex_unlock(&data->update_lock); \
0253     return count; \
0254 }
0255 
0256 store_in_reg(MIN, min)
0257 store_in_reg(MAX, max)
0258 
0259 static struct sensor_device_attribute sda_in_input[] = {
0260     SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
0261     SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
0262     SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
0263 };
0264 
0265 static struct sensor_device_attribute sda_in_min[] = {
0266     SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
0267     SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
0268     SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
0269 };
0270 
0271 static struct sensor_device_attribute sda_in_max[] = {
0272     SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
0273     SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
0274     SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
0275 };
0276 
0277 #define show_fan_reg(reg) \
0278 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
0279               char *buf) \
0280 { \
0281     int nr = to_sensor_dev_attr(attr)->index; \
0282     struct w83l786ng_data *data = w83l786ng_update_device(dev); \
0283     return sprintf(buf, "%d\n", \
0284         FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
0285 }
0286 
0287 show_fan_reg(fan);
0288 show_fan_reg(fan_min);
0289 
0290 static ssize_t
0291 store_fan_min(struct device *dev, struct device_attribute *attr,
0292           const char *buf, size_t count)
0293 {
0294     int nr = to_sensor_dev_attr(attr)->index;
0295     struct w83l786ng_data *data = dev_get_drvdata(dev);
0296     struct i2c_client *client = data->client;
0297     unsigned long val;
0298     int err;
0299 
0300     err = kstrtoul(buf, 10, &val);
0301     if (err)
0302         return err;
0303 
0304     mutex_lock(&data->update_lock);
0305     data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
0306     w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
0307                   data->fan_min[nr]);
0308     mutex_unlock(&data->update_lock);
0309 
0310     return count;
0311 }
0312 
0313 static ssize_t
0314 show_fan_div(struct device *dev, struct device_attribute *attr,
0315          char *buf)
0316 {
0317     int nr = to_sensor_dev_attr(attr)->index;
0318     struct w83l786ng_data *data = w83l786ng_update_device(dev);
0319     return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr]));
0320 }
0321 
0322 /*
0323  * Note: we save and restore the fan minimum here, because its value is
0324  * determined in part by the fan divisor.  This follows the principle of
0325  * least surprise; the user doesn't expect the fan minimum to change just
0326  * because the divisor changed.
0327  */
0328 static ssize_t
0329 store_fan_div(struct device *dev, struct device_attribute *attr,
0330           const char *buf, size_t count)
0331 {
0332     int nr = to_sensor_dev_attr(attr)->index;
0333     struct w83l786ng_data *data = dev_get_drvdata(dev);
0334     struct i2c_client *client = data->client;
0335 
0336     unsigned long min;
0337     u8 tmp_fan_div;
0338     u8 fan_div_reg;
0339     u8 keep_mask = 0;
0340     u8 new_shift = 0;
0341 
0342     unsigned long val;
0343     int err;
0344 
0345     err = kstrtoul(buf, 10, &val);
0346     if (err)
0347         return err;
0348 
0349     /* Save fan_min */
0350     mutex_lock(&data->update_lock);
0351     min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
0352 
0353     data->fan_div[nr] = DIV_TO_REG(val);
0354 
0355     switch (nr) {
0356     case 0:
0357         keep_mask = 0xf8;
0358         new_shift = 0;
0359         break;
0360     case 1:
0361         keep_mask = 0x8f;
0362         new_shift = 4;
0363         break;
0364     }
0365 
0366     fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV)
0367                        & keep_mask;
0368 
0369     tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask;
0370 
0371     w83l786ng_write_value(client, W83L786NG_REG_FAN_DIV,
0372                   fan_div_reg | tmp_fan_div);
0373 
0374     /* Restore fan_min */
0375     data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
0376     w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
0377                   data->fan_min[nr]);
0378     mutex_unlock(&data->update_lock);
0379 
0380     return count;
0381 }
0382 
0383 static struct sensor_device_attribute sda_fan_input[] = {
0384     SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
0385     SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
0386 };
0387 
0388 static struct sensor_device_attribute sda_fan_min[] = {
0389     SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
0390             store_fan_min, 0),
0391     SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
0392             store_fan_min, 1),
0393 };
0394 
0395 static struct sensor_device_attribute sda_fan_div[] = {
0396     SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div,
0397             store_fan_div, 0),
0398     SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div,
0399             store_fan_div, 1),
0400 };
0401 
0402 
0403 /* read/write the temperature, includes measured value and limits */
0404 
0405 static ssize_t
0406 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
0407 {
0408     struct sensor_device_attribute_2 *sensor_attr =
0409         to_sensor_dev_attr_2(attr);
0410     int nr = sensor_attr->nr;
0411     int index = sensor_attr->index;
0412     struct w83l786ng_data *data = w83l786ng_update_device(dev);
0413     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
0414 }
0415 
0416 static ssize_t
0417 store_temp(struct device *dev, struct device_attribute *attr,
0418        const char *buf, size_t count)
0419 {
0420     struct sensor_device_attribute_2 *sensor_attr =
0421         to_sensor_dev_attr_2(attr);
0422     int nr = sensor_attr->nr;
0423     int index = sensor_attr->index;
0424     struct w83l786ng_data *data = dev_get_drvdata(dev);
0425     struct i2c_client *client = data->client;
0426     long val;
0427     int err;
0428 
0429     err = kstrtol(buf, 10, &val);
0430     if (err)
0431         return err;
0432 
0433     mutex_lock(&data->update_lock);
0434     data->temp[nr][index] = TEMP_TO_REG(val);
0435     w83l786ng_write_value(client, W83L786NG_REG_TEMP[nr][index],
0436                   data->temp[nr][index]);
0437     mutex_unlock(&data->update_lock);
0438 
0439     return count;
0440 }
0441 
0442 static struct sensor_device_attribute_2 sda_temp_input[] = {
0443     SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
0444     SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
0445 };
0446 
0447 static struct sensor_device_attribute_2 sda_temp_max[] = {
0448     SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
0449               show_temp, store_temp, 0, 1),
0450     SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR,
0451               show_temp, store_temp, 1, 1),
0452 };
0453 
0454 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
0455     SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
0456               show_temp, store_temp, 0, 2),
0457     SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
0458               show_temp, store_temp, 1, 2),
0459 };
0460 
0461 #define show_pwm_reg(reg) \
0462 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
0463               char *buf) \
0464 { \
0465     struct w83l786ng_data *data = w83l786ng_update_device(dev); \
0466     int nr = to_sensor_dev_attr(attr)->index; \
0467     return sprintf(buf, "%d\n", data->reg[nr]); \
0468 }
0469 
0470 show_pwm_reg(pwm_mode)
0471 show_pwm_reg(pwm_enable)
0472 show_pwm_reg(pwm)
0473 
0474 static ssize_t
0475 store_pwm_mode(struct device *dev, struct device_attribute *attr,
0476            const char *buf, size_t count)
0477 {
0478     int nr = to_sensor_dev_attr(attr)->index;
0479     struct w83l786ng_data *data = dev_get_drvdata(dev);
0480     struct i2c_client *client = data->client;
0481     u8 reg;
0482     unsigned long val;
0483     int err;
0484 
0485     err = kstrtoul(buf, 10, &val);
0486     if (err)
0487         return err;
0488 
0489     if (val > 1)
0490         return -EINVAL;
0491     mutex_lock(&data->update_lock);
0492     data->pwm_mode[nr] = val;
0493     reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
0494     reg &= ~(1 << W83L786NG_PWM_MODE_SHIFT[nr]);
0495     if (!val)
0496         reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr];
0497     w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
0498     mutex_unlock(&data->update_lock);
0499     return count;
0500 }
0501 
0502 static ssize_t
0503 store_pwm(struct device *dev, struct device_attribute *attr,
0504       const char *buf, size_t count)
0505 {
0506     int nr = to_sensor_dev_attr(attr)->index;
0507     struct w83l786ng_data *data = dev_get_drvdata(dev);
0508     struct i2c_client *client = data->client;
0509     unsigned long val;
0510     int err;
0511 
0512     err = kstrtoul(buf, 10, &val);
0513     if (err)
0514         return err;
0515     val = clamp_val(val, 0, 255);
0516     val = DIV_ROUND_CLOSEST(val, 0x11);
0517 
0518     mutex_lock(&data->update_lock);
0519     data->pwm[nr] = val * 0x11;
0520     val |= w83l786ng_read_value(client, W83L786NG_REG_PWM[nr]) & 0xf0;
0521     w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val);
0522     mutex_unlock(&data->update_lock);
0523     return count;
0524 }
0525 
0526 static ssize_t
0527 store_pwm_enable(struct device *dev, struct device_attribute *attr,
0528          const char *buf, size_t count)
0529 {
0530     int nr = to_sensor_dev_attr(attr)->index;
0531     struct w83l786ng_data *data = dev_get_drvdata(dev);
0532     struct i2c_client *client = data->client;
0533     u8 reg;
0534     unsigned long val;
0535     int err;
0536 
0537     err = kstrtoul(buf, 10, &val);
0538     if (err)
0539         return err;
0540 
0541     if (!val || val > 2)  /* only modes 1 and 2 are supported */
0542         return -EINVAL;
0543 
0544     mutex_lock(&data->update_lock);
0545     reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
0546     data->pwm_enable[nr] = val;
0547     reg &= ~(0x03 << W83L786NG_PWM_ENABLE_SHIFT[nr]);
0548     reg |= (val - 1) << W83L786NG_PWM_ENABLE_SHIFT[nr];
0549     w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
0550     mutex_unlock(&data->update_lock);
0551     return count;
0552 }
0553 
0554 static struct sensor_device_attribute sda_pwm[] = {
0555     SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
0556     SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
0557 };
0558 
0559 static struct sensor_device_attribute sda_pwm_mode[] = {
0560     SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
0561             store_pwm_mode, 0),
0562     SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
0563             store_pwm_mode, 1),
0564 };
0565 
0566 static struct sensor_device_attribute sda_pwm_enable[] = {
0567     SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
0568             store_pwm_enable, 0),
0569     SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
0570             store_pwm_enable, 1),
0571 };
0572 
0573 /* For Smart Fan I/Thermal Cruise and Smart Fan II */
0574 static ssize_t
0575 show_tolerance(struct device *dev, struct device_attribute *attr, char *buf)
0576 {
0577     int nr = to_sensor_dev_attr(attr)->index;
0578     struct w83l786ng_data *data = w83l786ng_update_device(dev);
0579     return sprintf(buf, "%ld\n", (long)data->tolerance[nr]);
0580 }
0581 
0582 static ssize_t
0583 store_tolerance(struct device *dev, struct device_attribute *attr,
0584         const char *buf, size_t count)
0585 {
0586     int nr = to_sensor_dev_attr(attr)->index;
0587     struct w83l786ng_data *data = dev_get_drvdata(dev);
0588     struct i2c_client *client = data->client;
0589     u8 tol_tmp, tol_mask;
0590     unsigned long val;
0591     int err;
0592 
0593     err = kstrtoul(buf, 10, &val);
0594     if (err)
0595         return err;
0596 
0597     mutex_lock(&data->update_lock);
0598     tol_mask = w83l786ng_read_value(client,
0599         W83L786NG_REG_TOLERANCE) & ((nr == 1) ? 0x0f : 0xf0);
0600     tol_tmp = clamp_val(val, 0, 15);
0601     tol_tmp &= 0x0f;
0602     data->tolerance[nr] = tol_tmp;
0603     if (nr == 1)
0604         tol_tmp <<= 4;
0605 
0606     w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE,
0607                   tol_mask | tol_tmp);
0608     mutex_unlock(&data->update_lock);
0609     return count;
0610 }
0611 
0612 static struct sensor_device_attribute sda_tolerance[] = {
0613     SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO,
0614             show_tolerance, store_tolerance, 0),
0615     SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO,
0616             show_tolerance, store_tolerance, 1),
0617 };
0618 
0619 
0620 #define IN_UNIT_ATTRS(X)    \
0621     &sda_in_input[X].dev_attr.attr,     \
0622     &sda_in_min[X].dev_attr.attr,       \
0623     &sda_in_max[X].dev_attr.attr
0624 
0625 #define FAN_UNIT_ATTRS(X)   \
0626     &sda_fan_input[X].dev_attr.attr,    \
0627     &sda_fan_min[X].dev_attr.attr,      \
0628     &sda_fan_div[X].dev_attr.attr
0629 
0630 #define TEMP_UNIT_ATTRS(X)  \
0631     &sda_temp_input[X].dev_attr.attr,   \
0632     &sda_temp_max[X].dev_attr.attr,     \
0633     &sda_temp_max_hyst[X].dev_attr.attr
0634 
0635 #define PWM_UNIT_ATTRS(X)   \
0636     &sda_pwm[X].dev_attr.attr,      \
0637     &sda_pwm_mode[X].dev_attr.attr,     \
0638     &sda_pwm_enable[X].dev_attr.attr
0639 
0640 #define TOLERANCE_UNIT_ATTRS(X) \
0641     &sda_tolerance[X].dev_attr.attr
0642 
0643 static struct attribute *w83l786ng_attrs[] = {
0644     IN_UNIT_ATTRS(0),
0645     IN_UNIT_ATTRS(1),
0646     IN_UNIT_ATTRS(2),
0647     FAN_UNIT_ATTRS(0),
0648     FAN_UNIT_ATTRS(1),
0649     TEMP_UNIT_ATTRS(0),
0650     TEMP_UNIT_ATTRS(1),
0651     PWM_UNIT_ATTRS(0),
0652     PWM_UNIT_ATTRS(1),
0653     TOLERANCE_UNIT_ATTRS(0),
0654     TOLERANCE_UNIT_ATTRS(1),
0655     NULL
0656 };
0657 
0658 ATTRIBUTE_GROUPS(w83l786ng);
0659 
0660 static int
0661 w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info)
0662 {
0663     struct i2c_adapter *adapter = client->adapter;
0664     u16 man_id;
0665     u8 chip_id;
0666 
0667     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0668         return -ENODEV;
0669 
0670     /* Detection */
0671     if ((w83l786ng_read_value(client, W83L786NG_REG_CONFIG) & 0x80)) {
0672         dev_dbg(&adapter->dev, "W83L786NG detection failed at 0x%02x\n",
0673             client->addr);
0674         return -ENODEV;
0675     }
0676 
0677     /* Identification */
0678     man_id = (w83l786ng_read_value(client, W83L786NG_REG_MAN_ID1) << 8) +
0679          w83l786ng_read_value(client, W83L786NG_REG_MAN_ID2);
0680     chip_id = w83l786ng_read_value(client, W83L786NG_REG_CHIP_ID);
0681 
0682     if (man_id != 0x5CA3 ||     /* Winbond */
0683         chip_id != 0x80) {      /* W83L786NG */
0684         dev_dbg(&adapter->dev,
0685             "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
0686             man_id, chip_id);
0687         return -ENODEV;
0688     }
0689 
0690     strlcpy(info->type, "w83l786ng", I2C_NAME_SIZE);
0691 
0692     return 0;
0693 }
0694 
0695 static void w83l786ng_init_client(struct i2c_client *client)
0696 {
0697     u8 tmp;
0698 
0699     if (reset)
0700         w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
0701 
0702     /* Start monitoring */
0703     tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
0704     if (!(tmp & 0x01))
0705         w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01);
0706 }
0707 
0708 static int
0709 w83l786ng_probe(struct i2c_client *client)
0710 {
0711     struct device *dev = &client->dev;
0712     struct w83l786ng_data *data;
0713     struct device *hwmon_dev;
0714     int i;
0715     u8 reg_tmp;
0716 
0717     data = devm_kzalloc(dev, sizeof(struct w83l786ng_data), GFP_KERNEL);
0718     if (!data)
0719         return -ENOMEM;
0720 
0721     data->client = client;
0722     mutex_init(&data->update_lock);
0723 
0724     /* Initialize the chip */
0725     w83l786ng_init_client(client);
0726 
0727     /* A few vars need to be filled upon startup */
0728     for (i = 0; i < 2; i++) {
0729         data->fan_min[i] = w83l786ng_read_value(client,
0730             W83L786NG_REG_FAN_MIN(i));
0731     }
0732 
0733     /* Update the fan divisor */
0734     reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
0735     data->fan_div[0] = reg_tmp & 0x07;
0736     data->fan_div[1] = (reg_tmp >> 4) & 0x07;
0737 
0738     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0739                                data,
0740                                w83l786ng_groups);
0741     return PTR_ERR_OR_ZERO(hwmon_dev);
0742 }
0743 
0744 static const struct i2c_device_id w83l786ng_id[] = {
0745     { "w83l786ng", 0 },
0746     { }
0747 };
0748 MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
0749 
0750 static struct i2c_driver w83l786ng_driver = {
0751     .class      = I2C_CLASS_HWMON,
0752     .driver = {
0753            .name = "w83l786ng",
0754     },
0755     .probe_new  = w83l786ng_probe,
0756     .id_table   = w83l786ng_id,
0757     .detect     = w83l786ng_detect,
0758     .address_list   = normal_i2c,
0759 };
0760 
0761 module_i2c_driver(w83l786ng_driver);
0762 
0763 MODULE_AUTHOR("Kevin Lo");
0764 MODULE_DESCRIPTION("w83l786ng driver");
0765 MODULE_LICENSE("GPL");