linux/drivers/hwmon/w83l786ng.c
<<
>>
Prefs
   1/*
   2 * w83l786ng.c - Linux kernel driver for hardware monitoring
   3 * Copyright (c) 2007 Kevin Lo <kevlo@kevlo.org>
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation - version 2.
   8 *
   9 * This program is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write to the Free Software
  16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17 * 02110-1301 USA.
  18 */
  19
  20/*
  21 * Supports following chips:
  22 *
  23 * Chip         #vin    #fanin  #pwm    #temp   wchipid vendid  i2c     ISA
  24 * w83l786ng    3       2       2       2       0x7b    0x5ca3  yes     no
  25 */
  26
  27#include <linux/module.h>
  28#include <linux/init.h>
  29#include <linux/slab.h>
  30#include <linux/i2c.h>
  31#include <linux/hwmon.h>
  32#include <linux/hwmon-vid.h>
  33#include <linux/hwmon-sysfs.h>
  34#include <linux/err.h>
  35#include <linux/mutex.h>
  36#include <linux/jiffies.h>
  37
  38/* Addresses to scan */
  39static const unsigned short normal_i2c[] = { 0x2e, 0x2f, I2C_CLIENT_END };
  40
  41/* Insmod parameters */
  42
  43static bool reset;
  44module_param(reset, bool, 0);
  45MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
  46
  47#define W83L786NG_REG_IN_MIN(nr)        (0x2C + (nr) * 2)
  48#define W83L786NG_REG_IN_MAX(nr)        (0x2B + (nr) * 2)
  49#define W83L786NG_REG_IN(nr)            ((nr) + 0x20)
  50
  51#define W83L786NG_REG_FAN(nr)           ((nr) + 0x28)
  52#define W83L786NG_REG_FAN_MIN(nr)       ((nr) + 0x3B)
  53
  54#define W83L786NG_REG_CONFIG            0x40
  55#define W83L786NG_REG_ALARM1            0x41
  56#define W83L786NG_REG_ALARM2            0x42
  57#define W83L786NG_REG_GPIO_EN           0x47
  58#define W83L786NG_REG_MAN_ID2           0x4C
  59#define W83L786NG_REG_MAN_ID1           0x4D
  60#define W83L786NG_REG_CHIP_ID           0x4E
  61
  62#define W83L786NG_REG_DIODE             0x53
  63#define W83L786NG_REG_FAN_DIV           0x54
  64#define W83L786NG_REG_FAN_CFG           0x80
  65
  66#define W83L786NG_REG_TOLERANCE         0x8D
  67
  68static const u8 W83L786NG_REG_TEMP[2][3] = {
  69        { 0x25,         /* TEMP 0 in DataSheet */
  70          0x35,         /* TEMP 0 Over in DataSheet */
  71          0x36 },       /* TEMP 0 Hyst in DataSheet */
  72        { 0x26,         /* TEMP 1 in DataSheet */
  73          0x37,         /* TEMP 1 Over in DataSheet */
  74          0x38 }        /* TEMP 1 Hyst in DataSheet */
  75};
  76
  77static const u8 W83L786NG_PWM_MODE_SHIFT[] = {6, 7};
  78static const u8 W83L786NG_PWM_ENABLE_SHIFT[] = {2, 4};
  79
  80/* FAN Duty Cycle, be used to control */
  81static const u8 W83L786NG_REG_PWM[] = {0x81, 0x87};
  82
  83
  84static inline u8
  85FAN_TO_REG(long rpm, int div)
  86{
  87        if (rpm == 0)
  88                return 255;
  89        rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  90        return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  91}
  92
  93#define FAN_FROM_REG(val, div)  ((val) == 0   ? -1 : \
  94                                ((val) == 255 ? 0 : \
  95                                1350000 / ((val) * (div))))
  96
  97/* for temp */
  98#define TEMP_TO_REG(val)        (SENSORS_LIMIT(((val) < 0 ? \
  99                                                (val) + 0x100 * 1000 \
 100                                                : (val)) / 1000, 0, 0xff))
 101#define TEMP_FROM_REG(val)      (((val) & 0x80 ? \
 102                                  (val) - 0x100 : (val)) * 1000)
 103
 104/*
 105 * The analog voltage inputs have 8mV LSB. Since the sysfs output is
 106 * in mV as would be measured on the chip input pin, need to just
 107 * multiply/divide by 8 to translate from/to register values.
 108 */
 109#define IN_TO_REG(val)          (SENSORS_LIMIT((((val) + 4) / 8), 0, 255))
 110#define IN_FROM_REG(val)        ((val) * 8)
 111
 112#define DIV_FROM_REG(val)       (1 << (val))
 113
 114static inline u8
 115DIV_TO_REG(long val)
 116{
 117        int i;
 118        val = SENSORS_LIMIT(val, 1, 128) >> 1;
 119        for (i = 0; i < 7; i++) {
 120                if (val == 0)
 121                        break;
 122                val >>= 1;
 123        }
 124        return (u8)i;
 125}
 126
 127struct w83l786ng_data {
 128        struct device *hwmon_dev;
 129        struct mutex update_lock;
 130        char valid;                     /* !=0 if following fields are valid */
 131        unsigned long last_updated;     /* In jiffies */
 132        unsigned long last_nonvolatile; /* In jiffies, last time we update the
 133                                         * nonvolatile registers */
 134
 135        u8 in[3];
 136        u8 in_max[3];
 137        u8 in_min[3];
 138        u8 fan[2];
 139        u8 fan_div[2];
 140        u8 fan_min[2];
 141        u8 temp_type[2];
 142        u8 temp[2][3];
 143        u8 pwm[2];
 144        u8 pwm_mode[2]; /* 0->DC variable voltage
 145                         * 1->PWM variable duty cycle */
 146
 147        u8 pwm_enable[2]; /* 1->manual
 148                           * 2->thermal cruise (also called SmartFan I) */
 149        u8 tolerance[2];
 150};
 151
 152static int w83l786ng_probe(struct i2c_client *client,
 153                           const struct i2c_device_id *id);
 154static int w83l786ng_detect(struct i2c_client *client,
 155                            struct i2c_board_info *info);
 156static int w83l786ng_remove(struct i2c_client *client);
 157static void w83l786ng_init_client(struct i2c_client *client);
 158static struct w83l786ng_data *w83l786ng_update_device(struct device *dev);
 159
 160static const struct i2c_device_id w83l786ng_id[] = {
 161        { "w83l786ng", 0 },
 162        { }
 163};
 164MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
 165
 166static struct i2c_driver w83l786ng_driver = {
 167        .class          = I2C_CLASS_HWMON,
 168        .driver = {
 169                   .name = "w83l786ng",
 170        },
 171        .probe          = w83l786ng_probe,
 172        .remove         = w83l786ng_remove,
 173        .id_table       = w83l786ng_id,
 174        .detect         = w83l786ng_detect,
 175        .address_list   = normal_i2c,
 176};
 177
 178static u8
 179w83l786ng_read_value(struct i2c_client *client, u8 reg)
 180{
 181        return i2c_smbus_read_byte_data(client, reg);
 182}
 183
 184static int
 185w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value)
 186{
 187        return i2c_smbus_write_byte_data(client, reg, value);
 188}
 189
 190/* following are the sysfs callback functions */
 191#define show_in_reg(reg) \
 192static ssize_t \
 193show_##reg(struct device *dev, struct device_attribute *attr, \
 194           char *buf) \
 195{ \
 196        int nr = to_sensor_dev_attr(attr)->index; \
 197        struct w83l786ng_data *data = w83l786ng_update_device(dev); \
 198        return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
 199}
 200
 201show_in_reg(in)
 202show_in_reg(in_min)
 203show_in_reg(in_max)
 204
 205#define store_in_reg(REG, reg) \
 206static ssize_t \
 207store_in_##reg(struct device *dev, struct device_attribute *attr, \
 208               const char *buf, size_t count) \
 209{ \
 210        int nr = to_sensor_dev_attr(attr)->index; \
 211        struct i2c_client *client = to_i2c_client(dev); \
 212        struct w83l786ng_data *data = i2c_get_clientdata(client); \
 213        unsigned long val; \
 214        int err = kstrtoul(buf, 10, &val); \
 215        if (err) \
 216                return err; \
 217        mutex_lock(&data->update_lock); \
 218        data->in_##reg[nr] = IN_TO_REG(val); \
 219        w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \
 220                              data->in_##reg[nr]); \
 221        mutex_unlock(&data->update_lock); \
 222        return count; \
 223}
 224
 225store_in_reg(MIN, min)
 226store_in_reg(MAX, max)
 227
 228static struct sensor_device_attribute sda_in_input[] = {
 229        SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
 230        SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
 231        SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
 232};
 233
 234static struct sensor_device_attribute sda_in_min[] = {
 235        SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
 236        SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
 237        SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
 238};
 239
 240static struct sensor_device_attribute sda_in_max[] = {
 241        SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
 242        SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
 243        SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
 244};
 245
 246#define show_fan_reg(reg) \
 247static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
 248                          char *buf) \
 249{ \
 250        int nr = to_sensor_dev_attr(attr)->index; \
 251        struct w83l786ng_data *data = w83l786ng_update_device(dev); \
 252        return sprintf(buf, "%d\n", \
 253                FAN_FROM_REG(data->fan[nr], DIV_FROM_REG(data->fan_div[nr]))); \
 254}
 255
 256show_fan_reg(fan);
 257show_fan_reg(fan_min);
 258
 259static ssize_t
 260store_fan_min(struct device *dev, struct device_attribute *attr,
 261              const char *buf, size_t count)
 262{
 263        int nr = to_sensor_dev_attr(attr)->index;
 264        struct i2c_client *client = to_i2c_client(dev);
 265        struct w83l786ng_data *data = i2c_get_clientdata(client);
 266        unsigned long val;
 267        int err;
 268
 269        err = kstrtoul(buf, 10, &val);
 270        if (err)
 271                return err;
 272
 273        mutex_lock(&data->update_lock);
 274        data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
 275        w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
 276                              data->fan_min[nr]);
 277        mutex_unlock(&data->update_lock);
 278
 279        return count;
 280}
 281
 282static ssize_t
 283show_fan_div(struct device *dev, struct device_attribute *attr,
 284             char *buf)
 285{
 286        int nr = to_sensor_dev_attr(attr)->index;
 287        struct w83l786ng_data *data = w83l786ng_update_device(dev);
 288        return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr]));
 289}
 290
 291/*
 292 * Note: we save and restore the fan minimum here, because its value is
 293 * determined in part by the fan divisor.  This follows the principle of
 294 * least surprise; the user doesn't expect the fan minimum to change just
 295 * because the divisor changed.
 296 */
 297static ssize_t
 298store_fan_div(struct device *dev, struct device_attribute *attr,
 299              const char *buf, size_t count)
 300{
 301        int nr = to_sensor_dev_attr(attr)->index;
 302        struct i2c_client *client = to_i2c_client(dev);
 303        struct w83l786ng_data *data = i2c_get_clientdata(client);
 304
 305        unsigned long min;
 306        u8 tmp_fan_div;
 307        u8 fan_div_reg;
 308        u8 keep_mask = 0;
 309        u8 new_shift = 0;
 310
 311        unsigned long val;
 312        int err;
 313
 314        err = kstrtoul(buf, 10, &val);
 315        if (err)
 316                return err;
 317
 318        /* Save fan_min */
 319        mutex_lock(&data->update_lock);
 320        min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
 321
 322        data->fan_div[nr] = DIV_TO_REG(val);
 323
 324        switch (nr) {
 325        case 0:
 326                keep_mask = 0xf8;
 327                new_shift = 0;
 328                break;
 329        case 1:
 330                keep_mask = 0x8f;
 331                new_shift = 4;
 332                break;
 333        }
 334
 335        fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV)
 336                                           & keep_mask;
 337
 338        tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask;
 339
 340        w83l786ng_write_value(client, W83L786NG_REG_FAN_DIV,
 341                              fan_div_reg | tmp_fan_div);
 342
 343        /* Restore fan_min */
 344        data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
 345        w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
 346                              data->fan_min[nr]);
 347        mutex_unlock(&data->update_lock);
 348
 349        return count;
 350}
 351
 352static struct sensor_device_attribute sda_fan_input[] = {
 353        SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
 354        SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
 355};
 356
 357static struct sensor_device_attribute sda_fan_min[] = {
 358        SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
 359                    store_fan_min, 0),
 360        SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
 361                    store_fan_min, 1),
 362};
 363
 364static struct sensor_device_attribute sda_fan_div[] = {
 365        SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div,
 366                    store_fan_div, 0),
 367        SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div,
 368                    store_fan_div, 1),
 369};
 370
 371
 372/* read/write the temperature, includes measured value and limits */
 373
 374static ssize_t
 375show_temp(struct device *dev, struct device_attribute *attr, char *buf)
 376{
 377        struct sensor_device_attribute_2 *sensor_attr =
 378            to_sensor_dev_attr_2(attr);
 379        int nr = sensor_attr->nr;
 380        int index = sensor_attr->index;
 381        struct w83l786ng_data *data = w83l786ng_update_device(dev);
 382        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
 383}
 384
 385static ssize_t
 386store_temp(struct device *dev, struct device_attribute *attr,
 387           const char *buf, size_t count)
 388{
 389        struct sensor_device_attribute_2 *sensor_attr =
 390            to_sensor_dev_attr_2(attr);
 391        int nr = sensor_attr->nr;
 392        int index = sensor_attr->index;
 393        struct i2c_client *client = to_i2c_client(dev);
 394        struct w83l786ng_data *data = i2c_get_clientdata(client);
 395        long val;
 396        int err;
 397
 398        err = kstrtol(buf, 10, &val);
 399        if (err)
 400                return err;
 401
 402        mutex_lock(&data->update_lock);
 403        data->temp[nr][index] = TEMP_TO_REG(val);
 404        w83l786ng_write_value(client, W83L786NG_REG_TEMP[nr][index],
 405                              data->temp[nr][index]);
 406        mutex_unlock(&data->update_lock);
 407
 408        return count;
 409}
 410
 411static struct sensor_device_attribute_2 sda_temp_input[] = {
 412        SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
 413        SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
 414};
 415
 416static struct sensor_device_attribute_2 sda_temp_max[] = {
 417        SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
 418                      show_temp, store_temp, 0, 1),
 419        SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR,
 420                      show_temp, store_temp, 1, 1),
 421};
 422
 423static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
 424        SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
 425                      show_temp, store_temp, 0, 2),
 426        SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
 427                      show_temp, store_temp, 1, 2),
 428};
 429
 430#define show_pwm_reg(reg) \
 431static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
 432                          char *buf) \
 433{ \
 434        struct w83l786ng_data *data = w83l786ng_update_device(dev); \
 435        int nr = to_sensor_dev_attr(attr)->index; \
 436        return sprintf(buf, "%d\n", data->reg[nr]); \
 437}
 438
 439show_pwm_reg(pwm_mode)
 440show_pwm_reg(pwm_enable)
 441show_pwm_reg(pwm)
 442
 443static ssize_t
 444store_pwm_mode(struct device *dev, struct device_attribute *attr,
 445               const char *buf, size_t count)
 446{
 447        int nr = to_sensor_dev_attr(attr)->index;
 448        struct i2c_client *client = to_i2c_client(dev);
 449        struct w83l786ng_data *data = i2c_get_clientdata(client);
 450        u8 reg;
 451        unsigned long val;
 452        int err;
 453
 454        err = kstrtoul(buf, 10, &val);
 455        if (err)
 456                return err;
 457
 458        if (val > 1)
 459                return -EINVAL;
 460        mutex_lock(&data->update_lock);
 461        data->pwm_mode[nr] = val;
 462        reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
 463        reg &= ~(1 << W83L786NG_PWM_MODE_SHIFT[nr]);
 464        if (!val)
 465                reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr];
 466        w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
 467        mutex_unlock(&data->update_lock);
 468        return count;
 469}
 470
 471static ssize_t
 472store_pwm(struct device *dev, struct device_attribute *attr,
 473          const char *buf, size_t count)
 474{
 475        int nr = to_sensor_dev_attr(attr)->index;
 476        struct i2c_client *client = to_i2c_client(dev);
 477        struct w83l786ng_data *data = i2c_get_clientdata(client);
 478        unsigned long val;
 479        int err;
 480
 481        err = kstrtoul(buf, 10, &val);
 482        if (err)
 483                return err;
 484        val = SENSORS_LIMIT(val, 0, 255);
 485
 486        mutex_lock(&data->update_lock);
 487        data->pwm[nr] = val;
 488        w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val);
 489        mutex_unlock(&data->update_lock);
 490        return count;
 491}
 492
 493static ssize_t
 494store_pwm_enable(struct device *dev, struct device_attribute *attr,
 495                 const char *buf, size_t count)
 496{
 497        int nr = to_sensor_dev_attr(attr)->index;
 498        struct i2c_client *client = to_i2c_client(dev);
 499        struct w83l786ng_data *data = i2c_get_clientdata(client);
 500        u8 reg;
 501        unsigned long val;
 502        int err;
 503
 504        err = kstrtoul(buf, 10, &val);
 505        if (err)
 506                return err;
 507
 508        if (!val || val > 2)  /* only modes 1 and 2 are supported */
 509                return -EINVAL;
 510
 511        mutex_lock(&data->update_lock);
 512        reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
 513        data->pwm_enable[nr] = val;
 514        reg &= ~(0x02 << W83L786NG_PWM_ENABLE_SHIFT[nr]);
 515        reg |= (val - 1) << W83L786NG_PWM_ENABLE_SHIFT[nr];
 516        w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
 517        mutex_unlock(&data->update_lock);
 518        return count;
 519}
 520
 521static struct sensor_device_attribute sda_pwm[] = {
 522        SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
 523        SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
 524};
 525
 526static struct sensor_device_attribute sda_pwm_mode[] = {
 527        SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
 528                    store_pwm_mode, 0),
 529        SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
 530                    store_pwm_mode, 1),
 531};
 532
 533static struct sensor_device_attribute sda_pwm_enable[] = {
 534        SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
 535                    store_pwm_enable, 0),
 536        SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
 537                    store_pwm_enable, 1),
 538};
 539
 540/* For Smart Fan I/Thermal Cruise and Smart Fan II */
 541static ssize_t
 542show_tolerance(struct device *dev, struct device_attribute *attr, char *buf)
 543{
 544        int nr = to_sensor_dev_attr(attr)->index;
 545        struct w83l786ng_data *data = w83l786ng_update_device(dev);
 546        return sprintf(buf, "%ld\n", (long)data->tolerance[nr]);
 547}
 548
 549static ssize_t
 550store_tolerance(struct device *dev, struct device_attribute *attr,
 551                const char *buf, size_t count)
 552{
 553        int nr = to_sensor_dev_attr(attr)->index;
 554        struct i2c_client *client = to_i2c_client(dev);
 555        struct w83l786ng_data *data = i2c_get_clientdata(client);
 556        u8 tol_tmp, tol_mask;
 557        unsigned long val;
 558        int err;
 559
 560        err = kstrtoul(buf, 10, &val);
 561        if (err)
 562                return err;
 563
 564        mutex_lock(&data->update_lock);
 565        tol_mask = w83l786ng_read_value(client,
 566            W83L786NG_REG_TOLERANCE) & ((nr == 1) ? 0x0f : 0xf0);
 567        tol_tmp = SENSORS_LIMIT(val, 0, 15);
 568        tol_tmp &= 0x0f;
 569        data->tolerance[nr] = tol_tmp;
 570        if (nr == 1)
 571                tol_tmp <<= 4;
 572
 573        w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE,
 574                              tol_mask | tol_tmp);
 575        mutex_unlock(&data->update_lock);
 576        return count;
 577}
 578
 579static struct sensor_device_attribute sda_tolerance[] = {
 580        SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO,
 581                    show_tolerance, store_tolerance, 0),
 582        SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO,
 583                    show_tolerance, store_tolerance, 1),
 584};
 585
 586
 587#define IN_UNIT_ATTRS(X)        \
 588        &sda_in_input[X].dev_attr.attr,         \
 589        &sda_in_min[X].dev_attr.attr,           \
 590        &sda_in_max[X].dev_attr.attr
 591
 592#define FAN_UNIT_ATTRS(X)       \
 593        &sda_fan_input[X].dev_attr.attr,        \
 594        &sda_fan_min[X].dev_attr.attr,          \
 595        &sda_fan_div[X].dev_attr.attr
 596
 597#define TEMP_UNIT_ATTRS(X)      \
 598        &sda_temp_input[X].dev_attr.attr,       \
 599        &sda_temp_max[X].dev_attr.attr,         \
 600        &sda_temp_max_hyst[X].dev_attr.attr
 601
 602#define PWM_UNIT_ATTRS(X)       \
 603        &sda_pwm[X].dev_attr.attr,              \
 604        &sda_pwm_mode[X].dev_attr.attr,         \
 605        &sda_pwm_enable[X].dev_attr.attr
 606
 607#define TOLERANCE_UNIT_ATTRS(X) \
 608        &sda_tolerance[X].dev_attr.attr
 609
 610static struct attribute *w83l786ng_attributes[] = {
 611        IN_UNIT_ATTRS(0),
 612        IN_UNIT_ATTRS(1),
 613        IN_UNIT_ATTRS(2),
 614        FAN_UNIT_ATTRS(0),
 615        FAN_UNIT_ATTRS(1),
 616        TEMP_UNIT_ATTRS(0),
 617        TEMP_UNIT_ATTRS(1),
 618        PWM_UNIT_ATTRS(0),
 619        PWM_UNIT_ATTRS(1),
 620        TOLERANCE_UNIT_ATTRS(0),
 621        TOLERANCE_UNIT_ATTRS(1),
 622        NULL
 623};
 624
 625static const struct attribute_group w83l786ng_group = {
 626        .attrs = w83l786ng_attributes,
 627};
 628
 629static int
 630w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info)
 631{
 632        struct i2c_adapter *adapter = client->adapter;
 633        u16 man_id;
 634        u8 chip_id;
 635
 636        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 637                return -ENODEV;
 638
 639        /* Detection */
 640        if ((w83l786ng_read_value(client, W83L786NG_REG_CONFIG) & 0x80)) {
 641                dev_dbg(&adapter->dev, "W83L786NG detection failed at 0x%02x\n",
 642                        client->addr);
 643                return -ENODEV;
 644        }
 645
 646        /* Identification */
 647        man_id = (w83l786ng_read_value(client, W83L786NG_REG_MAN_ID1) << 8) +
 648                 w83l786ng_read_value(client, W83L786NG_REG_MAN_ID2);
 649        chip_id = w83l786ng_read_value(client, W83L786NG_REG_CHIP_ID);
 650
 651        if (man_id != 0x5CA3 ||         /* Winbond */
 652            chip_id != 0x80) {          /* W83L786NG */
 653                dev_dbg(&adapter->dev,
 654                        "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
 655                        man_id, chip_id);
 656                return -ENODEV;
 657        }
 658
 659        strlcpy(info->type, "w83l786ng", I2C_NAME_SIZE);
 660
 661        return 0;
 662}
 663
 664static int
 665w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id)
 666{
 667        struct device *dev = &client->dev;
 668        struct w83l786ng_data *data;
 669        int i, err = 0;
 670        u8 reg_tmp;
 671
 672        data = devm_kzalloc(&client->dev, sizeof(struct w83l786ng_data),
 673                            GFP_KERNEL);
 674        if (!data)
 675                return -ENOMEM;
 676
 677        i2c_set_clientdata(client, data);
 678        mutex_init(&data->update_lock);
 679
 680        /* Initialize the chip */
 681        w83l786ng_init_client(client);
 682
 683        /* A few vars need to be filled upon startup */
 684        for (i = 0; i < 2; i++) {
 685                data->fan_min[i] = w83l786ng_read_value(client,
 686                    W83L786NG_REG_FAN_MIN(i));
 687        }
 688
 689        /* Update the fan divisor */
 690        reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
 691        data->fan_div[0] = reg_tmp & 0x07;
 692        data->fan_div[1] = (reg_tmp >> 4) & 0x07;
 693
 694        /* Register sysfs hooks */
 695        err = sysfs_create_group(&client->dev.kobj, &w83l786ng_group);
 696        if (err)
 697                goto exit_remove;
 698
 699        data->hwmon_dev = hwmon_device_register(dev);
 700        if (IS_ERR(data->hwmon_dev)) {
 701                err = PTR_ERR(data->hwmon_dev);
 702                goto exit_remove;
 703        }
 704
 705        return 0;
 706
 707        /* Unregister sysfs hooks */
 708
 709exit_remove:
 710        sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
 711        return err;
 712}
 713
 714static int
 715w83l786ng_remove(struct i2c_client *client)
 716{
 717        struct w83l786ng_data *data = i2c_get_clientdata(client);
 718
 719        hwmon_device_unregister(data->hwmon_dev);
 720        sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
 721
 722        return 0;
 723}
 724
 725static void
 726w83l786ng_init_client(struct i2c_client *client)
 727{
 728        u8 tmp;
 729
 730        if (reset)
 731                w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
 732
 733        /* Start monitoring */
 734        tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
 735        if (!(tmp & 0x01))
 736                w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01);
 737}
 738
 739static struct w83l786ng_data *w83l786ng_update_device(struct device *dev)
 740{
 741        struct i2c_client *client = to_i2c_client(dev);
 742        struct w83l786ng_data *data = i2c_get_clientdata(client);
 743        int i, j;
 744        u8 reg_tmp, pwmcfg;
 745
 746        mutex_lock(&data->update_lock);
 747        if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
 748            || !data->valid) {
 749                dev_dbg(&client->dev, "Updating w83l786ng data.\n");
 750
 751                /* Update the voltages measured value and limits */
 752                for (i = 0; i < 3; i++) {
 753                        data->in[i] = w83l786ng_read_value(client,
 754                            W83L786NG_REG_IN(i));
 755                        data->in_min[i] = w83l786ng_read_value(client,
 756                            W83L786NG_REG_IN_MIN(i));
 757                        data->in_max[i] = w83l786ng_read_value(client,
 758                            W83L786NG_REG_IN_MAX(i));
 759                }
 760
 761                /* Update the fan counts and limits */
 762                for (i = 0; i < 2; i++) {
 763                        data->fan[i] = w83l786ng_read_value(client,
 764                            W83L786NG_REG_FAN(i));
 765                        data->fan_min[i] = w83l786ng_read_value(client,
 766                            W83L786NG_REG_FAN_MIN(i));
 767                }
 768
 769                /* Update the fan divisor */
 770                reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
 771                data->fan_div[0] = reg_tmp & 0x07;
 772                data->fan_div[1] = (reg_tmp >> 4) & 0x07;
 773
 774                pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
 775                for (i = 0; i < 2; i++) {
 776                        data->pwm_mode[i] =
 777                            ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
 778                            ? 0 : 1;
 779                        data->pwm_enable[i] =
 780                            ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 2) + 1;
 781                        data->pwm[i] = w83l786ng_read_value(client,
 782                            W83L786NG_REG_PWM[i]);
 783                }
 784
 785
 786                /* Update the temperature sensors */
 787                for (i = 0; i < 2; i++) {
 788                        for (j = 0; j < 3; j++) {
 789                                data->temp[i][j] = w83l786ng_read_value(client,
 790                                    W83L786NG_REG_TEMP[i][j]);
 791                        }
 792                }
 793
 794                /* Update Smart Fan I/II tolerance */
 795                reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE);
 796                data->tolerance[0] = reg_tmp & 0x0f;
 797                data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
 798
 799                data->last_updated = jiffies;
 800                data->valid = 1;
 801
 802        }
 803
 804        mutex_unlock(&data->update_lock);
 805
 806        return data;
 807}
 808
 809module_i2c_driver(w83l786ng_driver);
 810
 811MODULE_AUTHOR("Kevin Lo");
 812MODULE_DESCRIPTION("w83l786ng driver");
 813MODULE_LICENSE("GPL");
 814
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.