linux/drivers/hwmon/adt7470.c
<<
>>
Prefs
   1/*
   2 * A hwmon driver for the Analog Devices ADT7470
   3 * Copyright (C) 2007 IBM
   4 *
   5 * Author: Darrick J. Wong <djwong@us.ibm.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 2 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20 */
  21
  22#include <linux/module.h>
  23#include <linux/jiffies.h>
  24#include <linux/i2c.h>
  25#include <linux/hwmon.h>
  26#include <linux/hwmon-sysfs.h>
  27#include <linux/err.h>
  28#include <linux/mutex.h>
  29#include <linux/delay.h>
  30#include <linux/log2.h>
  31
  32/* Addresses to scan */
  33static unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
  34
  35/* Insmod parameters */
  36I2C_CLIENT_INSMOD_1(adt7470);
  37
  38/* ADT7470 registers */
  39#define ADT7470_REG_BASE_ADDR                   0x20
  40#define ADT7470_REG_TEMP_BASE_ADDR              0x20
  41#define ADT7470_REG_TEMP_MAX_ADDR               0x29
  42#define ADT7470_REG_FAN_BASE_ADDR               0x2A
  43#define ADT7470_REG_FAN_MAX_ADDR                0x31
  44#define ADT7470_REG_PWM_BASE_ADDR               0x32
  45#define ADT7470_REG_PWM_MAX_ADDR                0x35
  46#define ADT7470_REG_PWM_MAX_BASE_ADDR           0x38
  47#define ADT7470_REG_PWM_MAX_MAX_ADDR            0x3B
  48#define ADT7470_REG_CFG                         0x40
  49#define         ADT7470_FSPD_MASK               0x04
  50#define ADT7470_REG_ALARM1                      0x41
  51#define ADT7470_REG_ALARM2                      0x42
  52#define ADT7470_REG_TEMP_LIMITS_BASE_ADDR       0x44
  53#define ADT7470_REG_TEMP_LIMITS_MAX_ADDR        0x57
  54#define ADT7470_REG_FAN_MIN_BASE_ADDR           0x58
  55#define ADT7470_REG_FAN_MIN_MAX_ADDR            0x5F
  56#define ADT7470_REG_FAN_MAX_BASE_ADDR           0x60
  57#define ADT7470_REG_FAN_MAX_MAX_ADDR            0x67
  58#define ADT7470_REG_PWM_CFG_BASE_ADDR           0x68
  59#define ADT7470_REG_PWM12_CFG                   0x68
  60#define         ADT7470_PWM2_AUTO_MASK          0x40
  61#define         ADT7470_PWM1_AUTO_MASK          0x80
  62#define ADT7470_REG_PWM34_CFG                   0x69
  63#define         ADT7470_PWM3_AUTO_MASK          0x40
  64#define         ADT7470_PWM4_AUTO_MASK          0x80
  65#define ADT7470_REG_PWM_MIN_BASE_ADDR           0x6A
  66#define ADT7470_REG_PWM_MIN_MAX_ADDR            0x6D
  67#define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR      0x6E
  68#define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR       0x71
  69#define ADT7470_REG_ACOUSTICS12                 0x75
  70#define ADT7470_REG_ACOUSTICS34                 0x76
  71#define ADT7470_REG_DEVICE                      0x3D
  72#define ADT7470_REG_VENDOR                      0x3E
  73#define ADT7470_REG_REVISION                    0x3F
  74#define ADT7470_REG_ALARM1_MASK                 0x72
  75#define ADT7470_REG_ALARM2_MASK                 0x73
  76#define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR     0x7C
  77#define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR      0x7D
  78#define ADT7470_REG_MAX_ADDR                    0x81
  79
  80#define ADT7470_TEMP_COUNT      10
  81#define ADT7470_TEMP_REG(x)     (ADT7470_REG_TEMP_BASE_ADDR + (x))
  82#define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
  83#define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
  84                                ((x) * 2) + 1)
  85
  86#define ADT7470_FAN_COUNT       4
  87#define ADT7470_REG_FAN(x)      (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
  88#define ADT7470_REG_FAN_MIN(x)  (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
  89#define ADT7470_REG_FAN_MAX(x)  (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
  90
  91#define ADT7470_PWM_COUNT       4
  92#define ADT7470_REG_PWM(x)      (ADT7470_REG_PWM_BASE_ADDR + (x))
  93#define ADT7470_REG_PWM_MAX(x)  (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
  94#define ADT7470_REG_PWM_MIN(x)  (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
  95#define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
  96#define ADT7470_REG_PWM_CFG(x)  (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
  97#define ADT7470_REG_PWM_AUTO_TEMP(x)    (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
  98                                        ((x) / 2))
  99
 100#define ADT7470_VENDOR          0x41
 101#define ADT7470_DEVICE          0x70
 102/* datasheet only mentions a revision 2 */
 103#define ADT7470_REVISION        0x02
 104
 105/* "all temps" according to hwmon sysfs interface spec */
 106#define ADT7470_PWM_ALL_TEMPS   0x3FF
 107
 108/* How often do we reread sensors values? (In jiffies) */
 109#define SENSOR_REFRESH_INTERVAL (5 * HZ)
 110
 111/* How often do we reread sensor limit values? (In jiffies) */
 112#define LIMIT_REFRESH_INTERVAL  (60 * HZ)
 113
 114/* sleep 1s while gathering temperature data */
 115#define TEMP_COLLECTION_TIME    1000
 116
 117#define power_of_2(x)   (((x) & ((x) - 1)) == 0)
 118
 119/* datasheet says to divide this number by the fan reading to get fan rpm */
 120#define FAN_PERIOD_TO_RPM(x)    ((90000 * 60) / (x))
 121#define FAN_RPM_TO_PERIOD       FAN_PERIOD_TO_RPM
 122#define FAN_PERIOD_INVALID      65535
 123#define FAN_DATA_VALID(x)       ((x) && (x) != FAN_PERIOD_INVALID)
 124
 125struct adt7470_data {
 126        struct i2c_client       client;
 127        struct device           *hwmon_dev;
 128        struct attribute_group  attrs;
 129        struct mutex            lock;
 130        char                    sensors_valid;
 131        char                    limits_valid;
 132        unsigned long           sensors_last_updated;   /* In jiffies */
 133        unsigned long           limits_last_updated;    /* In jiffies */
 134
 135        s8                      temp[ADT7470_TEMP_COUNT];
 136        s8                      temp_min[ADT7470_TEMP_COUNT];
 137        s8                      temp_max[ADT7470_TEMP_COUNT];
 138        u16                     fan[ADT7470_FAN_COUNT];
 139        u16                     fan_min[ADT7470_FAN_COUNT];
 140        u16                     fan_max[ADT7470_FAN_COUNT];
 141        u16                     alarms, alarms_mask;
 142        u8                      force_pwm_max;
 143        u8                      pwm[ADT7470_PWM_COUNT];
 144        u8                      pwm_max[ADT7470_PWM_COUNT];
 145        u8                      pwm_automatic[ADT7470_PWM_COUNT];
 146        u8                      pwm_min[ADT7470_PWM_COUNT];
 147        s8                      pwm_tmin[ADT7470_PWM_COUNT];
 148        u8                      pwm_auto_temp[ADT7470_PWM_COUNT];
 149};
 150
 151static int adt7470_attach_adapter(struct i2c_adapter *adapter);
 152static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind);
 153static int adt7470_detach_client(struct i2c_client *client);
 154
 155static struct i2c_driver adt7470_driver = {
 156        .driver = {
 157                .name   = "adt7470",
 158        },
 159        .attach_adapter = adt7470_attach_adapter,
 160        .detach_client  = adt7470_detach_client,
 161};
 162
 163/*
 164 * 16-bit registers on the ADT7470 are low-byte first.  The data sheet says
 165 * that the low byte must be read before the high byte.
 166 */
 167static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
 168{
 169        u16 foo;
 170        foo = i2c_smbus_read_byte_data(client, reg);
 171        foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
 172        return foo;
 173}
 174
 175static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
 176                                          u16 value)
 177{
 178        return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
 179               && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
 180}
 181
 182static void adt7470_init_client(struct i2c_client *client)
 183{
 184        int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
 185
 186        if (reg < 0) {
 187                dev_err(&client->dev, "cannot read configuration register\n");
 188        } else {
 189                /* start monitoring (and do a self-test) */
 190                i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
 191        }
 192}
 193
 194static struct adt7470_data *adt7470_update_device(struct device *dev)
 195{
 196        struct i2c_client *client = to_i2c_client(dev);
 197        struct adt7470_data *data = i2c_get_clientdata(client);
 198        unsigned long local_jiffies = jiffies;
 199        u8 cfg;
 200        int i;
 201
 202        mutex_lock(&data->lock);
 203        if (time_before(local_jiffies, data->sensors_last_updated +
 204                SENSOR_REFRESH_INTERVAL)
 205                && data->sensors_valid)
 206                goto no_sensor_update;
 207
 208        /* start reading temperature sensors */
 209        cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
 210        cfg |= 0x80;
 211        i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
 212
 213        /*
 214         * Delay is 200ms * number of tmp05 sensors.  Too bad
 215         * there's no way to figure out how many are connected.
 216         * For now, assume 1s will work.
 217         */
 218        msleep(TEMP_COLLECTION_TIME);
 219
 220        /* done reading temperature sensors */
 221        cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
 222        cfg &= ~0x80;
 223        i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
 224
 225        for (i = 0; i < ADT7470_TEMP_COUNT; i++)
 226                data->temp[i] = i2c_smbus_read_byte_data(client,
 227                                                ADT7470_TEMP_REG(i));
 228
 229        for (i = 0; i < ADT7470_FAN_COUNT; i++)
 230                data->fan[i] = adt7470_read_word_data(client,
 231                                                ADT7470_REG_FAN(i));
 232
 233        for (i = 0; i < ADT7470_PWM_COUNT; i++) {
 234                int reg;
 235                int reg_mask;
 236
 237                data->pwm[i] = i2c_smbus_read_byte_data(client,
 238                                                ADT7470_REG_PWM(i));
 239
 240                if (i % 2)
 241                        reg_mask = ADT7470_PWM2_AUTO_MASK;
 242                else
 243                        reg_mask = ADT7470_PWM1_AUTO_MASK;
 244
 245                reg = ADT7470_REG_PWM_CFG(i);
 246                if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
 247                        data->pwm_automatic[i] = 1;
 248                else
 249                        data->pwm_automatic[i] = 0;
 250
 251                reg = ADT7470_REG_PWM_AUTO_TEMP(i);
 252                cfg = i2c_smbus_read_byte_data(client, reg);
 253                if (!(i % 2))
 254                        data->pwm_auto_temp[i] = cfg >> 4;
 255                else
 256                        data->pwm_auto_temp[i] = cfg & 0xF;
 257        }
 258
 259        if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
 260            ADT7470_FSPD_MASK)
 261                data->force_pwm_max = 1;
 262        else
 263                data->force_pwm_max = 0;
 264
 265        data->alarms = adt7470_read_word_data(client, ADT7470_REG_ALARM1);
 266        data->alarms_mask = adt7470_read_word_data(client,
 267                                                   ADT7470_REG_ALARM1_MASK);
 268
 269        data->sensors_last_updated = local_jiffies;
 270        data->sensors_valid = 1;
 271
 272no_sensor_update:
 273        if (time_before(local_jiffies, data->limits_last_updated +
 274                LIMIT_REFRESH_INTERVAL)
 275                && data->limits_valid)
 276                goto out;
 277
 278        for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
 279                data->temp_min[i] = i2c_smbus_read_byte_data(client,
 280                                                ADT7470_TEMP_MIN_REG(i));
 281                data->temp_max[i] = i2c_smbus_read_byte_data(client,
 282                                                ADT7470_TEMP_MAX_REG(i));
 283        }
 284
 285        for (i = 0; i < ADT7470_FAN_COUNT; i++) {
 286                data->fan_min[i] = adt7470_read_word_data(client,
 287                                                ADT7470_REG_FAN_MIN(i));
 288                data->fan_max[i] = adt7470_read_word_data(client,
 289                                                ADT7470_REG_FAN_MAX(i));
 290        }
 291
 292        for (i = 0; i < ADT7470_PWM_COUNT; i++) {
 293                data->pwm_max[i] = i2c_smbus_read_byte_data(client,
 294                                                ADT7470_REG_PWM_MAX(i));
 295                data->pwm_min[i] = i2c_smbus_read_byte_data(client,
 296                                                ADT7470_REG_PWM_MIN(i));
 297                data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
 298                                                ADT7470_REG_PWM_TMIN(i));
 299        }
 300
 301        data->limits_last_updated = local_jiffies;
 302        data->limits_valid = 1;
 303
 304out:
 305        mutex_unlock(&data->lock);
 306        return data;
 307}
 308
 309static ssize_t show_temp_min(struct device *dev,
 310                             struct device_attribute *devattr,
 311                             char *buf)
 312{
 313        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 314        struct adt7470_data *data = adt7470_update_device(dev);
 315        return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
 316}
 317
 318static ssize_t set_temp_min(struct device *dev,
 319                            struct device_attribute *devattr,
 320                            const char *buf,
 321                            size_t count)
 322{
 323        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 324        struct i2c_client *client = to_i2c_client(dev);
 325        struct adt7470_data *data = i2c_get_clientdata(client);
 326        int temp = simple_strtol(buf, NULL, 10) / 1000;
 327
 328        mutex_lock(&data->lock);
 329        data->temp_min[attr->index] = temp;
 330        i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
 331                                  temp);
 332        mutex_unlock(&data->lock);
 333
 334        return count;
 335}
 336
 337static ssize_t show_temp_max(struct device *dev,
 338                             struct device_attribute *devattr,
 339                             char *buf)
 340{
 341        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 342        struct adt7470_data *data = adt7470_update_device(dev);
 343        return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
 344}
 345
 346static ssize_t set_temp_max(struct device *dev,
 347                            struct device_attribute *devattr,
 348                            const char *buf,
 349                            size_t count)
 350{
 351        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 352        struct i2c_client *client = to_i2c_client(dev);
 353        struct adt7470_data *data = i2c_get_clientdata(client);
 354        int temp = simple_strtol(buf, NULL, 10) / 1000;
 355
 356        mutex_lock(&data->lock);
 357        data->temp_max[attr->index] = temp;
 358        i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
 359                                  temp);
 360        mutex_unlock(&data->lock);
 361
 362        return count;
 363}
 364
 365static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
 366                         char *buf)
 367{
 368        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 369        struct adt7470_data *data = adt7470_update_device(dev);
 370        return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
 371}
 372
 373static ssize_t show_alarms(struct device *dev,
 374                           struct device_attribute *devattr,
 375                           char *buf)
 376{
 377        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 378        struct adt7470_data *data = adt7470_update_device(dev);
 379
 380        if (attr->index)
 381                return sprintf(buf, "%x\n", data->alarms);
 382        else
 383                return sprintf(buf, "%x\n", data->alarms_mask);
 384}
 385
 386static ssize_t show_fan_max(struct device *dev,
 387                            struct device_attribute *devattr,
 388                            char *buf)
 389{
 390        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 391        struct adt7470_data *data = adt7470_update_device(dev);
 392
 393        if (FAN_DATA_VALID(data->fan_max[attr->index]))
 394                return sprintf(buf, "%d\n",
 395                               FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
 396        else
 397                return sprintf(buf, "0\n");
 398}
 399
 400static ssize_t set_fan_max(struct device *dev,
 401                           struct device_attribute *devattr,
 402                           const char *buf, size_t count)
 403{
 404        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 405        struct i2c_client *client = to_i2c_client(dev);
 406        struct adt7470_data *data = i2c_get_clientdata(client);
 407        int temp = simple_strtol(buf, NULL, 10);
 408
 409        if (!temp)
 410                return -EINVAL;
 411        temp = FAN_RPM_TO_PERIOD(temp);
 412
 413        mutex_lock(&data->lock);
 414        data->fan_max[attr->index] = temp;
 415        adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
 416        mutex_unlock(&data->lock);
 417
 418        return count;
 419}
 420
 421static ssize_t show_fan_min(struct device *dev,
 422                            struct device_attribute *devattr,
 423                            char *buf)
 424{
 425        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 426        struct adt7470_data *data = adt7470_update_device(dev);
 427
 428        if (FAN_DATA_VALID(data->fan_min[attr->index]))
 429                return sprintf(buf, "%d\n",
 430                               FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
 431        else
 432                return sprintf(buf, "0\n");
 433}
 434
 435static ssize_t set_fan_min(struct device *dev,
 436                           struct device_attribute *devattr,
 437                           const char *buf, size_t count)
 438{
 439        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 440        struct i2c_client *client = to_i2c_client(dev);
 441        struct adt7470_data *data = i2c_get_clientdata(client);
 442        int temp = simple_strtol(buf, NULL, 10);
 443
 444        if (!temp)
 445                return -EINVAL;
 446        temp = FAN_RPM_TO_PERIOD(temp);
 447
 448        mutex_lock(&data->lock);
 449        data->fan_min[attr->index] = temp;
 450        adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
 451        mutex_unlock(&data->lock);
 452
 453        return count;
 454}
 455
 456static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
 457                        char *buf)
 458{
 459        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 460        struct adt7470_data *data = adt7470_update_device(dev);
 461
 462        if (FAN_DATA_VALID(data->fan[attr->index]))
 463                return sprintf(buf, "%d\n",
 464                               FAN_PERIOD_TO_RPM(data->fan[attr->index]));
 465        else
 466                return sprintf(buf, "0\n");
 467}
 468
 469static ssize_t show_force_pwm_max(struct device *dev,
 470                                  struct device_attribute *devattr,
 471                                  char *buf)
 472{
 473        struct adt7470_data *data = adt7470_update_device(dev);
 474        return sprintf(buf, "%d\n", data->force_pwm_max);
 475}
 476
 477static ssize_t set_force_pwm_max(struct device *dev,
 478                                 struct device_attribute *devattr,
 479                                 const char *buf,
 480                                 size_t count)
 481{
 482        struct i2c_client *client = to_i2c_client(dev);
 483        struct adt7470_data *data = i2c_get_clientdata(client);
 484        int temp = simple_strtol(buf, NULL, 10);
 485        u8 reg;
 486
 487        mutex_lock(&data->lock);
 488        data->force_pwm_max = temp;
 489        reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
 490        if (temp)
 491                reg |= ADT7470_FSPD_MASK;
 492        else
 493                reg &= ~ADT7470_FSPD_MASK;
 494        i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
 495        mutex_unlock(&data->lock);
 496
 497        return count;
 498}
 499
 500static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
 501                        char *buf)
 502{
 503        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 504        struct adt7470_data *data = adt7470_update_device(dev);
 505        return sprintf(buf, "%d\n", data->pwm[attr->index]);
 506}
 507
 508static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
 509                        const char *buf, size_t count)
 510{
 511        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 512        struct i2c_client *client = to_i2c_client(dev);
 513        struct adt7470_data *data = i2c_get_clientdata(client);
 514        int temp = simple_strtol(buf, NULL, 10);
 515
 516        mutex_lock(&data->lock);
 517        data->pwm[attr->index] = temp;
 518        i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
 519        mutex_unlock(&data->lock);
 520
 521        return count;
 522}
 523
 524static ssize_t show_pwm_max(struct device *dev,
 525                            struct device_attribute *devattr,
 526                            char *buf)
 527{
 528        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 529        struct adt7470_data *data = adt7470_update_device(dev);
 530        return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
 531}
 532
 533static ssize_t set_pwm_max(struct device *dev,
 534                           struct device_attribute *devattr,
 535                           const char *buf,
 536                           size_t count)
 537{
 538        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 539        struct i2c_client *client = to_i2c_client(dev);
 540        struct adt7470_data *data = i2c_get_clientdata(client);
 541        int temp = simple_strtol(buf, NULL, 10);
 542
 543        mutex_lock(&data->lock);
 544        data->pwm_max[attr->index] = temp;
 545        i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
 546                                  temp);
 547        mutex_unlock(&data->lock);
 548
 549        return count;
 550}
 551
 552static ssize_t show_pwm_min(struct device *dev,
 553                            struct device_attribute *devattr,
 554                            char *buf)
 555{
 556        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 557        struct adt7470_data *data = adt7470_update_device(dev);
 558        return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
 559}
 560
 561static ssize_t set_pwm_min(struct device *dev,
 562                           struct device_attribute *devattr,
 563                           const char *buf,
 564                           size_t count)
 565{
 566        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 567        struct i2c_client *client = to_i2c_client(dev);
 568        struct adt7470_data *data = i2c_get_clientdata(client);
 569        int temp = simple_strtol(buf, NULL, 10);
 570
 571        mutex_lock(&data->lock);
 572        data->pwm_min[attr->index] = temp;
 573        i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
 574                                  temp);
 575        mutex_unlock(&data->lock);
 576
 577        return count;
 578}
 579
 580static ssize_t show_pwm_tmax(struct device *dev,
 581                             struct device_attribute *devattr,
 582                             char *buf)
 583{
 584        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 585        struct adt7470_data *data = adt7470_update_device(dev);
 586        /* the datasheet says that tmax = tmin + 20C */
 587        return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
 588}
 589
 590static ssize_t show_pwm_tmin(struct device *dev,
 591                             struct device_attribute *devattr,
 592                             char *buf)
 593{
 594        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 595        struct adt7470_data *data = adt7470_update_device(dev);
 596        return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
 597}
 598
 599static ssize_t set_pwm_tmin(struct device *dev,
 600                            struct device_attribute *devattr,
 601                            const char *buf,
 602                            size_t count)
 603{
 604        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 605        struct i2c_client *client = to_i2c_client(dev);
 606        struct adt7470_data *data = i2c_get_clientdata(client);
 607        int temp = simple_strtol(buf, NULL, 10) / 1000;
 608
 609        mutex_lock(&data->lock);
 610        data->pwm_tmin[attr->index] = temp;
 611        i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
 612                                  temp);
 613        mutex_unlock(&data->lock);
 614
 615        return count;
 616}
 617
 618static ssize_t show_pwm_auto(struct device *dev,
 619                             struct device_attribute *devattr,
 620                             char *buf)
 621{
 622        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 623        struct adt7470_data *data = adt7470_update_device(dev);
 624        return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
 625}
 626
 627static ssize_t set_pwm_auto(struct device *dev,
 628                            struct device_attribute *devattr,
 629                            const char *buf,
 630                            size_t count)
 631{
 632        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 633        struct i2c_client *client = to_i2c_client(dev);
 634        struct adt7470_data *data = i2c_get_clientdata(client);
 635        int temp = simple_strtol(buf, NULL, 10);
 636        int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
 637        int pwm_auto_reg_mask;
 638        u8 reg;
 639
 640        if (attr->index % 2)
 641                pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
 642        else
 643                pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
 644
 645        if (temp != 2 && temp != 1)
 646                return -EINVAL;
 647        temp--;
 648
 649        mutex_lock(&data->lock);
 650        data->pwm_automatic[attr->index] = temp;
 651        reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
 652        if (temp)
 653                reg |= pwm_auto_reg_mask;
 654        else
 655                reg &= ~pwm_auto_reg_mask;
 656        i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
 657        mutex_unlock(&data->lock);
 658
 659        return count;
 660}
 661
 662static ssize_t show_pwm_auto_temp(struct device *dev,
 663                                  struct device_attribute *devattr,
 664                                  char *buf)
 665{
 666        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 667        struct adt7470_data *data = adt7470_update_device(dev);
 668        u8 ctrl = data->pwm_auto_temp[attr->index];
 669
 670        if (ctrl)
 671                return sprintf(buf, "%d\n", 1 << (ctrl - 1));
 672        else
 673                return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
 674}
 675
 676static int cvt_auto_temp(int input)
 677{
 678        if (input == ADT7470_PWM_ALL_TEMPS)
 679                return 0;
 680        if (input < 1 || !power_of_2(input))
 681                return -EINVAL;
 682        return ilog2(input) + 1;
 683}
 684
 685static ssize_t set_pwm_auto_temp(struct device *dev,
 686                                 struct device_attribute *devattr,
 687                                 const char *buf,
 688                                 size_t count)
 689{
 690        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 691        struct i2c_client *client = to_i2c_client(dev);
 692        struct adt7470_data *data = i2c_get_clientdata(client);
 693        int temp = cvt_auto_temp(simple_strtol(buf, NULL, 10));
 694        int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
 695        u8 reg;
 696
 697        if (temp < 0)
 698                return temp;
 699
 700        mutex_lock(&data->lock);
 701        data->pwm_automatic[attr->index] = temp;
 702        reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
 703
 704        if (!(attr->index % 2)) {
 705                reg &= 0xF;
 706                reg |= (temp << 4) & 0xF0;
 707        } else {
 708                reg &= 0xF0;
 709                reg |= temp & 0xF;
 710        }
 711
 712        i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
 713        mutex_unlock(&data->lock);
 714
 715        return count;
 716}
 717
 718static SENSOR_DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL, 0);
 719static SENSOR_DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarms, NULL, 1);
 720
 721static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
 722                    set_temp_max, 0);
 723static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
 724                    set_temp_max, 1);
 725static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
 726                    set_temp_max, 2);
 727static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
 728                    set_temp_max, 3);
 729static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
 730                    set_temp_max, 4);
 731static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
 732                    set_temp_max, 5);
 733static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
 734                    set_temp_max, 6);
 735static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
 736                    set_temp_max, 7);
 737static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
 738                    set_temp_max, 8);
 739static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
 740                    set_temp_max, 9);
 741
 742static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
 743                    set_temp_min, 0);
 744static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
 745                    set_temp_min, 1);
 746static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
 747                    set_temp_min, 2);
 748static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
 749                    set_temp_min, 3);
 750static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
 751                    set_temp_min, 4);
 752static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
 753                    set_temp_min, 5);
 754static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
 755                    set_temp_min, 6);
 756static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
 757                    set_temp_min, 7);
 758static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
 759                    set_temp_min, 8);
 760static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
 761                    set_temp_min, 9);
 762
 763static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
 764static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
 765static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
 766static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
 767static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
 768static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
 769static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
 770static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
 771static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
 772static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
 773
 774static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
 775                    set_fan_max, 0);
 776static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
 777                    set_fan_max, 1);
 778static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
 779                    set_fan_max, 2);
 780static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
 781                    set_fan_max, 3);
 782
 783static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
 784                    set_fan_min, 0);
 785static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
 786                    set_fan_min, 1);
 787static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
 788                    set_fan_min, 2);
 789static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
 790                    set_fan_min, 3);
 791
 792static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
 793static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
 794static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
 795static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
 796
 797static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
 798                    show_force_pwm_max, set_force_pwm_max, 0);
 799
 800static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
 801static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
 802static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
 803static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
 804
 805static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
 806                    show_pwm_min, set_pwm_min, 0);
 807static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
 808                    show_pwm_min, set_pwm_min, 1);
 809static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
 810                    show_pwm_min, set_pwm_min, 2);
 811static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
 812                    show_pwm_min, set_pwm_min, 3);
 813
 814static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
 815                    show_pwm_max, set_pwm_max, 0);
 816static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
 817                    show_pwm_max, set_pwm_max, 1);
 818static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
 819                    show_pwm_max, set_pwm_max, 2);
 820static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
 821                    show_pwm_max, set_pwm_max, 3);
 822
 823static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
 824                    show_pwm_tmin, set_pwm_tmin, 0);
 825static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
 826                    show_pwm_tmin, set_pwm_tmin, 1);
 827static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
 828                    show_pwm_tmin, set_pwm_tmin, 2);
 829static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
 830                    show_pwm_tmin, set_pwm_tmin, 3);
 831
 832static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
 833                    NULL, 0);
 834static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
 835                    NULL, 1);
 836static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
 837                    NULL, 2);
 838static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
 839                    NULL, 3);
 840
 841static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
 842                    set_pwm_auto, 0);
 843static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
 844                    set_pwm_auto, 1);
 845static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
 846                    set_pwm_auto, 2);
 847static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
 848                    set_pwm_auto, 3);
 849
 850static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
 851                    show_pwm_auto_temp, set_pwm_auto_temp, 0);
 852static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
 853                    show_pwm_auto_temp, set_pwm_auto_temp, 1);
 854static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
 855                    show_pwm_auto_temp, set_pwm_auto_temp, 2);
 856static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
 857                    show_pwm_auto_temp, set_pwm_auto_temp, 3);
 858
 859static struct attribute *adt7470_attr[] =
 860{
 861        &sensor_dev_attr_alarms.dev_attr.attr,
 862        &sensor_dev_attr_alarm_mask.dev_attr.attr,
 863        &sensor_dev_attr_temp1_max.dev_attr.attr,
 864        &sensor_dev_attr_temp2_max.dev_attr.attr,
 865        &sensor_dev_attr_temp3_max.dev_attr.attr,
 866        &sensor_dev_attr_temp4_max.dev_attr.attr,
 867        &sensor_dev_attr_temp5_max.dev_attr.attr,
 868        &sensor_dev_attr_temp6_max.dev_attr.attr,
 869        &sensor_dev_attr_temp7_max.dev_attr.attr,
 870        &sensor_dev_attr_temp8_max.dev_attr.attr,
 871        &sensor_dev_attr_temp9_max.dev_attr.attr,
 872        &sensor_dev_attr_temp10_max.dev_attr.attr,
 873        &sensor_dev_attr_temp1_min.dev_attr.attr,
 874        &sensor_dev_attr_temp2_min.dev_attr.attr,
 875        &sensor_dev_attr_temp3_min.dev_attr.attr,
 876        &sensor_dev_attr_temp4_min.dev_attr.attr,
 877        &sensor_dev_attr_temp5_min.dev_attr.attr,
 878        &sensor_dev_attr_temp6_min.dev_attr.attr,
 879        &sensor_dev_attr_temp7_min.dev_attr.attr,
 880        &sensor_dev_attr_temp8_min.dev_attr.attr,
 881        &sensor_dev_attr_temp9_min.dev_attr.attr,
 882        &sensor_dev_attr_temp10_min.dev_attr.attr,
 883        &sensor_dev_attr_temp1_input.dev_attr.attr,
 884        &sensor_dev_attr_temp2_input.dev_attr.attr,
 885        &sensor_dev_attr_temp3_input.dev_attr.attr,
 886        &sensor_dev_attr_temp4_input.dev_attr.attr,
 887        &sensor_dev_attr_temp5_input.dev_attr.attr,
 888        &sensor_dev_attr_temp6_input.dev_attr.attr,
 889        &sensor_dev_attr_temp7_input.dev_attr.attr,
 890        &sensor_dev_attr_temp8_input.dev_attr.attr,
 891        &sensor_dev_attr_temp9_input.dev_attr.attr,
 892        &sensor_dev_attr_temp10_input.dev_attr.attr,
 893        &sensor_dev_attr_fan1_max.dev_attr.attr,
 894        &sensor_dev_attr_fan2_max.dev_attr.attr,
 895        &sensor_dev_attr_fan3_max.dev_attr.attr,
 896        &sensor_dev_attr_fan4_max.dev_attr.attr,
 897        &sensor_dev_attr_fan1_min.dev_attr.attr,
 898        &sensor_dev_attr_fan2_min.dev_attr.attr,
 899        &sensor_dev_attr_fan3_min.dev_attr.attr,
 900        &sensor_dev_attr_fan4_min.dev_attr.attr,
 901        &sensor_dev_attr_fan1_input.dev_attr.attr,
 902        &sensor_dev_attr_fan2_input.dev_attr.attr,
 903        &sensor_dev_attr_fan3_input.dev_attr.attr,
 904        &sensor_dev_attr_fan4_input.dev_attr.attr,
 905        &sensor_dev_attr_force_pwm_max.dev_attr.attr,
 906        &sensor_dev_attr_pwm1.dev_attr.attr,
 907        &sensor_dev_attr_pwm2.dev_attr.attr,
 908        &sensor_dev_attr_pwm3.dev_attr.attr,
 909        &sensor_dev_attr_pwm4.dev_attr.attr,
 910        &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
 911        &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
 912        &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
 913        &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
 914        &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
 915        &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
 916        &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
 917        &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
 918        &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
 919        &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
 920        &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
 921        &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
 922        &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
 923        &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
 924        &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
 925        &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
 926        &sensor_dev_attr_pwm1_enable.dev_attr.attr,
 927        &sensor_dev_attr_pwm2_enable.dev_attr.attr,
 928        &sensor_dev_attr_pwm3_enable.dev_attr.attr,
 929        &sensor_dev_attr_pwm4_enable.dev_attr.attr,
 930        &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
 931        &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
 932        &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
 933        &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
 934        NULL
 935};
 936
 937static int adt7470_attach_adapter(struct i2c_adapter *adapter)
 938{
 939        if (!(adapter->class & I2C_CLASS_HWMON))
 940                return 0;
 941        return i2c_probe(adapter, &addr_data, adt7470_detect);
 942}
 943
 944static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind)
 945{
 946        struct i2c_client *client;
 947        struct adt7470_data *data;
 948        int err = 0;
 949
 950        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 951                goto exit;
 952
 953        if (!(data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL))) {
 954                err = -ENOMEM;
 955                goto exit;
 956        }
 957
 958        client = &data->client;
 959        client->addr = address;
 960        client->adapter = adapter;
 961        client->driver = &adt7470_driver;
 962
 963        i2c_set_clientdata(client, data);
 964
 965        mutex_init(&data->lock);
 966
 967        if (kind <= 0) {
 968                int vendor, device, revision;
 969
 970                vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
 971                if (vendor != ADT7470_VENDOR) {
 972                        err = -ENODEV;
 973                        goto exit_free;
 974                }
 975
 976                device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
 977                if (device != ADT7470_DEVICE) {
 978                        err = -ENODEV;
 979                        goto exit_free;
 980                }
 981
 982                revision = i2c_smbus_read_byte_data(client,
 983                                                    ADT7470_REG_REVISION);
 984                if (revision != ADT7470_REVISION) {
 985                        err = -ENODEV;
 986                        goto exit_free;
 987                }
 988        } else
 989                dev_dbg(&adapter->dev, "detection forced\n");
 990
 991        strlcpy(client->name, "adt7470", I2C_NAME_SIZE);
 992
 993        if ((err = i2c_attach_client(client)))
 994                goto exit_free;
 995
 996        dev_info(&client->dev, "%s chip found\n", client->name);
 997
 998        /* Initialize the ADT7470 chip */
 999        adt7470_init_client(client);
1000
1001        /* Register sysfs hooks */
1002        data->attrs.attrs = adt7470_attr;
1003        if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
1004                goto exit_detach;
1005
1006        data->hwmon_dev = hwmon_device_register(&client->dev);
1007        if (IS_ERR(data->hwmon_dev)) {
1008                err = PTR_ERR(data->hwmon_dev);
1009                goto exit_remove;
1010        }
1011
1012        return 0;
1013
1014exit_remove:
1015        sysfs_remove_group(&client->dev.kobj, &data->attrs);
1016exit_detach:
1017        i2c_detach_client(client);
1018exit_free:
1019        kfree(data);
1020exit:
1021        return err;
1022}
1023
1024static int adt7470_detach_client(struct i2c_client *client)
1025{
1026        struct adt7470_data *data = i2c_get_clientdata(client);
1027
1028        hwmon_device_unregister(data->hwmon_dev);
1029        sysfs_remove_group(&client->dev.kobj, &data->attrs);
1030        i2c_detach_client(client);
1031        kfree(data);
1032        return 0;
1033}
1034
1035static int __init adt7470_init(void)
1036{
1037        return i2c_add_driver(&adt7470_driver);
1038}
1039
1040static void __exit adt7470_exit(void)
1041{
1042        i2c_del_driver(&adt7470_driver);
1043}
1044
1045MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1046MODULE_DESCRIPTION("ADT7470 driver");
1047MODULE_LICENSE("GPL");
1048
1049module_init(adt7470_init);
1050module_exit(adt7470_exit);
1051