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