linux/drivers/hwmon/adt7462.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * A hwmon driver for the Analog Devices ADT7462
   4 * Copyright (C) 2008 IBM
   5 *
   6 * Author: Darrick J. Wong <darrick.wong@oracle.com>
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/jiffies.h>
  11#include <linux/i2c.h>
  12#include <linux/hwmon.h>
  13#include <linux/hwmon-sysfs.h>
  14#include <linux/err.h>
  15#include <linux/mutex.h>
  16#include <linux/log2.h>
  17#include <linux/slab.h>
  18
  19/* Addresses to scan */
  20static const unsigned short normal_i2c[] = { 0x58, 0x5C, I2C_CLIENT_END };
  21
  22/* ADT7462 registers */
  23#define ADT7462_REG_DEVICE                      0x3D
  24#define ADT7462_REG_VENDOR                      0x3E
  25#define ADT7462_REG_REVISION                    0x3F
  26
  27#define ADT7462_REG_MIN_TEMP_BASE_ADDR          0x44
  28#define ADT7462_REG_MIN_TEMP_MAX_ADDR           0x47
  29#define ADT7462_REG_MAX_TEMP_BASE_ADDR          0x48
  30#define ADT7462_REG_MAX_TEMP_MAX_ADDR           0x4B
  31#define ADT7462_REG_TEMP_BASE_ADDR              0x88
  32#define ADT7462_REG_TEMP_MAX_ADDR               0x8F
  33
  34#define ADT7462_REG_FAN_BASE_ADDR               0x98
  35#define ADT7462_REG_FAN_MAX_ADDR                0x9F
  36#define ADT7462_REG_FAN2_BASE_ADDR              0xA2
  37#define ADT7462_REG_FAN2_MAX_ADDR               0xA9
  38#define ADT7462_REG_FAN_ENABLE                  0x07
  39#define ADT7462_REG_FAN_MIN_BASE_ADDR           0x78
  40#define ADT7462_REG_FAN_MIN_MAX_ADDR            0x7F
  41
  42#define ADT7462_REG_CFG2                        0x02
  43#define         ADT7462_FSPD_MASK               0x20
  44
  45#define ADT7462_REG_PWM_BASE_ADDR               0xAA
  46#define ADT7462_REG_PWM_MAX_ADDR                0xAD
  47#define ADT7462_REG_PWM_MIN_BASE_ADDR           0x28
  48#define ADT7462_REG_PWM_MIN_MAX_ADDR            0x2B
  49#define ADT7462_REG_PWM_MAX                     0x2C
  50#define ADT7462_REG_PWM_TEMP_MIN_BASE_ADDR      0x5C
  51#define ADT7462_REG_PWM_TEMP_MIN_MAX_ADDR       0x5F
  52#define ADT7462_REG_PWM_TEMP_RANGE_BASE_ADDR    0x60
  53#define ADT7462_REG_PWM_TEMP_RANGE_MAX_ADDR     0x63
  54#define ADT7462_PWM_HYST_MASK                   0x0F
  55#define ADT7462_PWM_RANGE_MASK                  0xF0
  56#define         ADT7462_PWM_RANGE_SHIFT         4
  57#define ADT7462_REG_PWM_CFG_BASE_ADDR           0x21
  58#define ADT7462_REG_PWM_CFG_MAX_ADDR            0x24
  59#define         ADT7462_PWM_CHANNEL_MASK        0xE0
  60#define         ADT7462_PWM_CHANNEL_SHIFT       5
  61
  62#define ADT7462_REG_PIN_CFG_BASE_ADDR           0x10
  63#define ADT7462_REG_PIN_CFG_MAX_ADDR            0x13
  64#define         ADT7462_PIN7_INPUT              0x01    /* cfg0 */
  65#define         ADT7462_DIODE3_INPUT            0x20
  66#define         ADT7462_DIODE1_INPUT            0x40
  67#define         ADT7462_VID_INPUT               0x80
  68#define         ADT7462_PIN22_INPUT             0x04    /* cfg1 */
  69#define         ADT7462_PIN21_INPUT             0x08
  70#define         ADT7462_PIN19_INPUT             0x10
  71#define         ADT7462_PIN15_INPUT             0x20
  72#define         ADT7462_PIN13_INPUT             0x40
  73#define         ADT7462_PIN8_INPUT              0x80
  74#define         ADT7462_PIN23_MASK              0x03
  75#define         ADT7462_PIN23_SHIFT             0
  76#define         ADT7462_PIN26_MASK              0x0C    /* cfg2 */
  77#define         ADT7462_PIN26_SHIFT             2
  78#define         ADT7462_PIN25_MASK              0x30
  79#define         ADT7462_PIN25_SHIFT             4
  80#define         ADT7462_PIN24_MASK              0xC0
  81#define         ADT7462_PIN24_SHIFT             6
  82#define         ADT7462_PIN26_VOLT_INPUT        0x08
  83#define         ADT7462_PIN25_VOLT_INPUT        0x20
  84#define         ADT7462_PIN28_SHIFT             4       /* cfg3 */
  85#define         ADT7462_PIN28_VOLT              0x5
  86
  87#define ADT7462_REG_ALARM1                      0xB8
  88#define ADT7462_LT_ALARM                        0x02
  89#define         ADT7462_R1T_ALARM               0x04
  90#define         ADT7462_R2T_ALARM               0x08
  91#define         ADT7462_R3T_ALARM               0x10
  92#define ADT7462_REG_ALARM2                      0xBB
  93#define         ADT7462_V0_ALARM                0x01
  94#define         ADT7462_V1_ALARM                0x02
  95#define         ADT7462_V2_ALARM                0x04
  96#define         ADT7462_V3_ALARM                0x08
  97#define         ADT7462_V4_ALARM                0x10
  98#define         ADT7462_V5_ALARM                0x20
  99#define         ADT7462_V6_ALARM                0x40
 100#define         ADT7462_V7_ALARM                0x80
 101#define ADT7462_REG_ALARM3                      0xBC
 102#define         ADT7462_V8_ALARM                0x08
 103#define         ADT7462_V9_ALARM                0x10
 104#define         ADT7462_V10_ALARM               0x20
 105#define         ADT7462_V11_ALARM               0x40
 106#define         ADT7462_V12_ALARM               0x80
 107#define ADT7462_REG_ALARM4                      0xBD
 108#define         ADT7462_F0_ALARM                0x01
 109#define         ADT7462_F1_ALARM                0x02
 110#define         ADT7462_F2_ALARM                0x04
 111#define         ADT7462_F3_ALARM                0x08
 112#define         ADT7462_F4_ALARM                0x10
 113#define         ADT7462_F5_ALARM                0x20
 114#define         ADT7462_F6_ALARM                0x40
 115#define         ADT7462_F7_ALARM                0x80
 116#define ADT7462_ALARM1                          0x0000
 117#define ADT7462_ALARM2                          0x0100
 118#define ADT7462_ALARM3                          0x0200
 119#define ADT7462_ALARM4                          0x0300
 120#define ADT7462_ALARM_REG_SHIFT                 8
 121#define ADT7462_ALARM_FLAG_MASK                 0x0F
 122
 123#define ADT7462_TEMP_COUNT              4
 124#define ADT7462_TEMP_REG(x)             (ADT7462_REG_TEMP_BASE_ADDR + ((x) * 2))
 125#define ADT7462_TEMP_MIN_REG(x)         (ADT7462_REG_MIN_TEMP_BASE_ADDR + (x))
 126#define ADT7462_TEMP_MAX_REG(x)         (ADT7462_REG_MAX_TEMP_BASE_ADDR + (x))
 127#define TEMP_FRAC_OFFSET                6
 128
 129#define ADT7462_FAN_COUNT               8
 130#define ADT7462_REG_FAN_MIN(x)          (ADT7462_REG_FAN_MIN_BASE_ADDR + (x))
 131
 132#define ADT7462_PWM_COUNT               4
 133#define ADT7462_REG_PWM(x)              (ADT7462_REG_PWM_BASE_ADDR + (x))
 134#define ADT7462_REG_PWM_MIN(x)          (ADT7462_REG_PWM_MIN_BASE_ADDR + (x))
 135#define ADT7462_REG_PWM_TMIN(x)         \
 136        (ADT7462_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
 137#define ADT7462_REG_PWM_TRANGE(x)       \
 138        (ADT7462_REG_PWM_TEMP_RANGE_BASE_ADDR + (x))
 139
 140#define ADT7462_PIN_CFG_REG_COUNT       4
 141#define ADT7462_REG_PIN_CFG(x)          (ADT7462_REG_PIN_CFG_BASE_ADDR + (x))
 142#define ADT7462_REG_PWM_CFG(x)          (ADT7462_REG_PWM_CFG_BASE_ADDR + (x))
 143
 144#define ADT7462_ALARM_REG_COUNT         4
 145
 146/*
 147 * The chip can measure 13 different voltage sources:
 148 *
 149 * 1. +12V1 (pin 7)
 150 * 2. Vccp1/+2.5V/+1.8V/+1.5V (pin 23)
 151 * 3. +12V3 (pin 22)
 152 * 4. +5V (pin 21)
 153 * 5. +1.25V/+0.9V (pin 19)
 154 * 6. +2.5V/+1.8V (pin 15)
 155 * 7. +3.3v (pin 13)
 156 * 8. +12V2 (pin 8)
 157 * 9. Vbatt/FSB_Vtt (pin 26)
 158 * A. +3.3V/+1.2V1 (pin 25)
 159 * B. Vccp2/+2.5V/+1.8V/+1.5V (pin 24)
 160 * C. +1.5V ICH (only if BOTH pin 28/29 are set to +1.5V)
 161 * D. +1.5V 3GPIO (only if BOTH pin 28/29 are set to +1.5V)
 162 *
 163 * Each of these 13 has a factor to convert raw to voltage.  Even better,
 164 * the pins can be connected to other sensors (tach/gpio/hot/etc), which
 165 * makes the bookkeeping tricky.
 166 *
 167 * Some, but not all, of these voltages have low/high limits.
 168 */
 169#define ADT7462_VOLT_COUNT      13
 170
 171#define ADT7462_VENDOR          0x41
 172#define ADT7462_DEVICE          0x62
 173/* datasheet only mentions a revision 4 */
 174#define ADT7462_REVISION        0x04
 175
 176/* How often do we reread sensors values? (In jiffies) */
 177#define SENSOR_REFRESH_INTERVAL (2 * HZ)
 178
 179/* How often do we reread sensor limit values? (In jiffies) */
 180#define LIMIT_REFRESH_INTERVAL  (60 * HZ)
 181
 182/* datasheet says to divide this number by the fan reading to get fan rpm */
 183#define FAN_PERIOD_TO_RPM(x)    ((90000 * 60) / (x))
 184#define FAN_RPM_TO_PERIOD       FAN_PERIOD_TO_RPM
 185#define FAN_PERIOD_INVALID      65535
 186#define FAN_DATA_VALID(x)       ((x) && (x) != FAN_PERIOD_INVALID)
 187
 188#define MASK_AND_SHIFT(value, prefix)   \
 189        (((value) & prefix##_MASK) >> prefix##_SHIFT)
 190
 191struct adt7462_data {
 192        struct i2c_client       *client;
 193        struct mutex            lock;
 194        char                    sensors_valid;
 195        char                    limits_valid;
 196        unsigned long           sensors_last_updated;   /* In jiffies */
 197        unsigned long           limits_last_updated;    /* In jiffies */
 198
 199        u8                      temp[ADT7462_TEMP_COUNT];
 200                                /* bits 6-7 are quarter pieces of temp */
 201        u8                      temp_frac[ADT7462_TEMP_COUNT];
 202        u8                      temp_min[ADT7462_TEMP_COUNT];
 203        u8                      temp_max[ADT7462_TEMP_COUNT];
 204        u16                     fan[ADT7462_FAN_COUNT];
 205        u8                      fan_enabled;
 206        u8                      fan_min[ADT7462_FAN_COUNT];
 207        u8                      cfg2;
 208        u8                      pwm[ADT7462_PWM_COUNT];
 209        u8                      pin_cfg[ADT7462_PIN_CFG_REG_COUNT];
 210        u8                      voltages[ADT7462_VOLT_COUNT];
 211        u8                      volt_max[ADT7462_VOLT_COUNT];
 212        u8                      volt_min[ADT7462_VOLT_COUNT];
 213        u8                      pwm_min[ADT7462_PWM_COUNT];
 214        u8                      pwm_tmin[ADT7462_PWM_COUNT];
 215        u8                      pwm_trange[ADT7462_PWM_COUNT];
 216        u8                      pwm_max;        /* only one per chip */
 217        u8                      pwm_cfg[ADT7462_PWM_COUNT];
 218        u8                      alarms[ADT7462_ALARM_REG_COUNT];
 219};
 220
 221/*
 222 * 16-bit registers on the ADT7462 are low-byte first.  The data sheet says
 223 * that the low byte must be read before the high byte.
 224 */
 225static inline int adt7462_read_word_data(struct i2c_client *client, u8 reg)
 226{
 227        u16 foo;
 228        foo = i2c_smbus_read_byte_data(client, reg);
 229        foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
 230        return foo;
 231}
 232
 233/* For some reason these registers are not contiguous. */
 234static int ADT7462_REG_FAN(int fan)
 235{
 236        if (fan < 4)
 237                return ADT7462_REG_FAN_BASE_ADDR + (2 * fan);
 238        return ADT7462_REG_FAN2_BASE_ADDR + (2 * (fan - 4));
 239}
 240
 241/* Voltage registers are scattered everywhere */
 242static int ADT7462_REG_VOLT_MAX(struct adt7462_data *data, int which)
 243{
 244        switch (which) {
 245        case 0:
 246                if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
 247                        return 0x7C;
 248                break;
 249        case 1:
 250                return 0x69;
 251        case 2:
 252                if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
 253                        return 0x7F;
 254                break;
 255        case 3:
 256                if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
 257                        return 0x7E;
 258                break;
 259        case 4:
 260                if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
 261                        return 0x4B;
 262                break;
 263        case 5:
 264                if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
 265                        return 0x49;
 266                break;
 267        case 6:
 268                if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
 269                        return 0x68;
 270                break;
 271        case 7:
 272                if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
 273                        return 0x7D;
 274                break;
 275        case 8:
 276                if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
 277                        return 0x6C;
 278                break;
 279        case 9:
 280                if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
 281                        return 0x6B;
 282                break;
 283        case 10:
 284                return 0x6A;
 285        case 11:
 286                if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
 287                                        ADT7462_PIN28_VOLT &&
 288                    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
 289                        return 0x50;
 290                break;
 291        case 12:
 292                if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
 293                                        ADT7462_PIN28_VOLT &&
 294                    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
 295                        return 0x4C;
 296                break;
 297        }
 298        return 0;
 299}
 300
 301static int ADT7462_REG_VOLT_MIN(struct adt7462_data *data, int which)
 302{
 303        switch (which) {
 304        case 0:
 305                if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
 306                        return 0x6D;
 307                break;
 308        case 1:
 309                return 0x72;
 310        case 2:
 311                if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
 312                        return 0x6F;
 313                break;
 314        case 3:
 315                if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
 316                        return 0x71;
 317                break;
 318        case 4:
 319                if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
 320                        return 0x47;
 321                break;
 322        case 5:
 323                if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
 324                        return 0x45;
 325                break;
 326        case 6:
 327                if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
 328                        return 0x70;
 329                break;
 330        case 7:
 331                if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
 332                        return 0x6E;
 333                break;
 334        case 8:
 335                if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
 336                        return 0x75;
 337                break;
 338        case 9:
 339                if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
 340                        return 0x74;
 341                break;
 342        case 10:
 343                return 0x73;
 344        case 11:
 345                if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
 346                                        ADT7462_PIN28_VOLT &&
 347                    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
 348                        return 0x76;
 349                break;
 350        case 12:
 351                if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
 352                                        ADT7462_PIN28_VOLT &&
 353                    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
 354                        return 0x77;
 355                break;
 356        }
 357        return 0;
 358}
 359
 360static int ADT7462_REG_VOLT(struct adt7462_data *data, int which)
 361{
 362        switch (which) {
 363        case 0:
 364                if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
 365                        return 0xA3;
 366                break;
 367        case 1:
 368                return 0x90;
 369        case 2:
 370                if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
 371                        return 0xA9;
 372                break;
 373        case 3:
 374                if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
 375                        return 0xA7;
 376                break;
 377        case 4:
 378                if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
 379                        return 0x8F;
 380                break;
 381        case 5:
 382                if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
 383                        return 0x8B;
 384                break;
 385        case 6:
 386                if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
 387                        return 0x96;
 388                break;
 389        case 7:
 390                if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
 391                        return 0xA5;
 392                break;
 393        case 8:
 394                if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
 395                        return 0x93;
 396                break;
 397        case 9:
 398                if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
 399                        return 0x92;
 400                break;
 401        case 10:
 402                return 0x91;
 403        case 11:
 404                if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
 405                                        ADT7462_PIN28_VOLT &&
 406                    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
 407                        return 0x94;
 408                break;
 409        case 12:
 410                if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
 411                                        ADT7462_PIN28_VOLT &&
 412                    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
 413                        return 0x95;
 414                break;
 415        }
 416        return 0;
 417}
 418
 419/* Provide labels for sysfs */
 420static const char *voltage_label(struct adt7462_data *data, int which)
 421{
 422        switch (which) {
 423        case 0:
 424                if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
 425                        return "+12V1";
 426                break;
 427        case 1:
 428                switch (MASK_AND_SHIFT(data->pin_cfg[1], ADT7462_PIN23)) {
 429                case 0:
 430                        return "Vccp1";
 431                case 1:
 432                        return "+2.5V";
 433                case 2:
 434                        return "+1.8V";
 435                case 3:
 436                        return "+1.5V";
 437                }
 438                fallthrough;
 439        case 2:
 440                if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
 441                        return "+12V3";
 442                break;
 443        case 3:
 444                if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
 445                        return "+5V";
 446                break;
 447        case 4:
 448                if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT)) {
 449                        if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
 450                                return "+0.9V";
 451                        return "+1.25V";
 452                }
 453                break;
 454        case 5:
 455                if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT)) {
 456                        if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
 457                                return "+1.8V";
 458                        return "+2.5V";
 459                }
 460                break;
 461        case 6:
 462                if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
 463                        return "+3.3V";
 464                break;
 465        case 7:
 466                if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
 467                        return "+12V2";
 468                break;
 469        case 8:
 470                switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN26)) {
 471                case 0:
 472                        return "Vbatt";
 473                case 1:
 474                        return "FSB_Vtt";
 475                }
 476                break;
 477        case 9:
 478                switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN25)) {
 479                case 0:
 480                        return "+3.3V";
 481                case 1:
 482                        return "+1.2V1";
 483                }
 484                break;
 485        case 10:
 486                switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN24)) {
 487                case 0:
 488                        return "Vccp2";
 489                case 1:
 490                        return "+2.5V";
 491                case 2:
 492                        return "+1.8V";
 493                case 3:
 494                        return "+1.5";
 495                }
 496                fallthrough;
 497        case 11:
 498                if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
 499                                        ADT7462_PIN28_VOLT &&
 500                    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
 501                        return "+1.5V ICH";
 502                break;
 503        case 12:
 504                if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
 505                                        ADT7462_PIN28_VOLT &&
 506                    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
 507                        return "+1.5V 3GPIO";
 508                break;
 509        }
 510        return "N/A";
 511}
 512
 513/* Multipliers are actually in uV, not mV. */
 514static int voltage_multiplier(struct adt7462_data *data, int which)
 515{
 516        switch (which) {
 517        case 0:
 518                if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
 519                        return 62500;
 520                break;
 521        case 1:
 522                switch (MASK_AND_SHIFT(data->pin_cfg[1], ADT7462_PIN23)) {
 523                case 0:
 524                        if (data->pin_cfg[0] & ADT7462_VID_INPUT)
 525                                return 12500;
 526                        return 6250;
 527                case 1:
 528                        return 13000;
 529                case 2:
 530                        return 9400;
 531                case 3:
 532                        return 7800;
 533                }
 534                fallthrough;
 535        case 2:
 536                if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
 537                        return 62500;
 538                break;
 539        case 3:
 540                if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
 541                        return 26000;
 542                break;
 543        case 4:
 544                if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT)) {
 545                        if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
 546                                return 4690;
 547                        return 6500;
 548                }
 549                break;
 550        case 5:
 551                if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT)) {
 552                        if (data->pin_cfg[1] & ADT7462_PIN15_INPUT)
 553                                return 9400;
 554                        return 13000;
 555                }
 556                break;
 557        case 6:
 558                if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
 559                        return 17200;
 560                break;
 561        case 7:
 562                if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
 563                        return 62500;
 564                break;
 565        case 8:
 566                switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN26)) {
 567                case 0:
 568                        return 15600;
 569                case 1:
 570                        return 6250;
 571                }
 572                break;
 573        case 9:
 574                switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN25)) {
 575                case 0:
 576                        return 17200;
 577                case 1:
 578                        return 6250;
 579                }
 580                break;
 581        case 10:
 582                switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN24)) {
 583                case 0:
 584                        return 6250;
 585                case 1:
 586                        return 13000;
 587                case 2:
 588                        return 9400;
 589                case 3:
 590                        return 7800;
 591                }
 592                fallthrough;
 593        case 11:
 594        case 12:
 595                if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
 596                                        ADT7462_PIN28_VOLT &&
 597                    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
 598                        return 7800;
 599        }
 600        return 0;
 601}
 602
 603static int temp_enabled(struct adt7462_data *data, int which)
 604{
 605        switch (which) {
 606        case 0:
 607        case 2:
 608                return 1;
 609        case 1:
 610                if (data->pin_cfg[0] & ADT7462_DIODE1_INPUT)
 611                        return 1;
 612                break;
 613        case 3:
 614                if (data->pin_cfg[0] & ADT7462_DIODE3_INPUT)
 615                        return 1;
 616                break;
 617        }
 618        return 0;
 619}
 620
 621static const char *temp_label(struct adt7462_data *data, int which)
 622{
 623        switch (which) {
 624        case 0:
 625                return "local";
 626        case 1:
 627                if (data->pin_cfg[0] & ADT7462_DIODE1_INPUT)
 628                        return "remote1";
 629                break;
 630        case 2:
 631                return "remote2";
 632        case 3:
 633                if (data->pin_cfg[0] & ADT7462_DIODE3_INPUT)
 634                        return "remote3";
 635                break;
 636        }
 637        return "N/A";
 638}
 639
 640/* Map Trange register values to mC */
 641#define NUM_TRANGE_VALUES       16
 642static const int trange_values[NUM_TRANGE_VALUES] = {
 643        2000,
 644        2500,
 645        3300,
 646        4000,
 647        5000,
 648        6700,
 649        8000,
 650        10000,
 651        13300,
 652        16000,
 653        20000,
 654        26700,
 655        32000,
 656        40000,
 657        53300,
 658        80000
 659};
 660
 661static int find_trange_value(int trange)
 662{
 663        int i;
 664
 665        for (i = 0; i < NUM_TRANGE_VALUES; i++)
 666                if (trange_values[i] == trange)
 667                        return i;
 668
 669        return -EINVAL;
 670}
 671
 672static struct adt7462_data *adt7462_update_device(struct device *dev)
 673{
 674        struct adt7462_data *data = dev_get_drvdata(dev);
 675        struct i2c_client *client = data->client;
 676        unsigned long local_jiffies = jiffies;
 677        int i;
 678
 679        mutex_lock(&data->lock);
 680        if (time_before(local_jiffies, data->sensors_last_updated +
 681                SENSOR_REFRESH_INTERVAL)
 682                && data->sensors_valid)
 683                goto no_sensor_update;
 684
 685        for (i = 0; i < ADT7462_TEMP_COUNT; i++) {
 686                /*
 687                 * Reading the fractional register locks the integral
 688                 * register until both have been read.
 689                 */
 690                data->temp_frac[i] = i2c_smbus_read_byte_data(client,
 691                                                ADT7462_TEMP_REG(i));
 692                data->temp[i] = i2c_smbus_read_byte_data(client,
 693                                                ADT7462_TEMP_REG(i) + 1);
 694        }
 695
 696        for (i = 0; i < ADT7462_FAN_COUNT; i++)
 697                data->fan[i] = adt7462_read_word_data(client,
 698                                                ADT7462_REG_FAN(i));
 699
 700        data->fan_enabled = i2c_smbus_read_byte_data(client,
 701                                        ADT7462_REG_FAN_ENABLE);
 702
 703        for (i = 0; i < ADT7462_PWM_COUNT; i++)
 704                data->pwm[i] = i2c_smbus_read_byte_data(client,
 705                                                ADT7462_REG_PWM(i));
 706
 707        for (i = 0; i < ADT7462_PIN_CFG_REG_COUNT; i++)
 708                data->pin_cfg[i] = i2c_smbus_read_byte_data(client,
 709                                ADT7462_REG_PIN_CFG(i));
 710
 711        for (i = 0; i < ADT7462_VOLT_COUNT; i++) {
 712                int reg = ADT7462_REG_VOLT(data, i);
 713                if (!reg)
 714                        data->voltages[i] = 0;
 715                else
 716                        data->voltages[i] = i2c_smbus_read_byte_data(client,
 717                                                                     reg);
 718        }
 719
 720        data->alarms[0] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM1);
 721        data->alarms[1] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM2);
 722        data->alarms[2] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM3);
 723        data->alarms[3] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM4);
 724
 725        data->sensors_last_updated = local_jiffies;
 726        data->sensors_valid = 1;
 727
 728no_sensor_update:
 729        if (time_before(local_jiffies, data->limits_last_updated +
 730                LIMIT_REFRESH_INTERVAL)
 731                && data->limits_valid)
 732                goto out;
 733
 734        for (i = 0; i < ADT7462_TEMP_COUNT; i++) {
 735                data->temp_min[i] = i2c_smbus_read_byte_data(client,
 736                                                ADT7462_TEMP_MIN_REG(i));
 737                data->temp_max[i] = i2c_smbus_read_byte_data(client,
 738                                                ADT7462_TEMP_MAX_REG(i));
 739        }
 740
 741        for (i = 0; i < ADT7462_FAN_COUNT; i++)
 742                data->fan_min[i] = i2c_smbus_read_byte_data(client,
 743                                                ADT7462_REG_FAN_MIN(i));
 744
 745        for (i = 0; i < ADT7462_VOLT_COUNT; i++) {
 746                int reg = ADT7462_REG_VOLT_MAX(data, i);
 747                data->volt_max[i] =
 748                        (reg ? i2c_smbus_read_byte_data(client, reg) : 0);
 749
 750                reg = ADT7462_REG_VOLT_MIN(data, i);
 751                data->volt_min[i] =
 752                        (reg ? i2c_smbus_read_byte_data(client, reg) : 0);
 753        }
 754
 755        for (i = 0; i < ADT7462_PWM_COUNT; i++) {
 756                data->pwm_min[i] = i2c_smbus_read_byte_data(client,
 757                                                ADT7462_REG_PWM_MIN(i));
 758                data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
 759                                                ADT7462_REG_PWM_TMIN(i));
 760                data->pwm_trange[i] = i2c_smbus_read_byte_data(client,
 761                                                ADT7462_REG_PWM_TRANGE(i));
 762                data->pwm_cfg[i] = i2c_smbus_read_byte_data(client,
 763                                                ADT7462_REG_PWM_CFG(i));
 764        }
 765
 766        data->pwm_max = i2c_smbus_read_byte_data(client, ADT7462_REG_PWM_MAX);
 767
 768        data->cfg2 = i2c_smbus_read_byte_data(client, ADT7462_REG_CFG2);
 769
 770        data->limits_last_updated = local_jiffies;
 771        data->limits_valid = 1;
 772
 773out:
 774        mutex_unlock(&data->lock);
 775        return data;
 776}
 777
 778static ssize_t temp_min_show(struct device *dev,
 779                             struct device_attribute *devattr, char *buf)
 780{
 781        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 782        struct adt7462_data *data = adt7462_update_device(dev);
 783
 784        if (!temp_enabled(data, attr->index))
 785                return sprintf(buf, "0\n");
 786
 787        return sprintf(buf, "%d\n", 1000 * (data->temp_min[attr->index] - 64));
 788}
 789
 790static ssize_t temp_min_store(struct device *dev,
 791                              struct device_attribute *devattr,
 792                              const char *buf, size_t count)
 793{
 794        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 795        struct adt7462_data *data = dev_get_drvdata(dev);
 796        struct i2c_client *client = data->client;
 797        long temp;
 798
 799        if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
 800                return -EINVAL;
 801
 802        temp = clamp_val(temp, -64000, 191000);
 803        temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
 804
 805        mutex_lock(&data->lock);
 806        data->temp_min[attr->index] = temp;
 807        i2c_smbus_write_byte_data(client, ADT7462_TEMP_MIN_REG(attr->index),
 808                                  temp);
 809        mutex_unlock(&data->lock);
 810
 811        return count;
 812}
 813
 814static ssize_t temp_max_show(struct device *dev,
 815                             struct device_attribute *devattr, char *buf)
 816{
 817        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 818        struct adt7462_data *data = adt7462_update_device(dev);
 819
 820        if (!temp_enabled(data, attr->index))
 821                return sprintf(buf, "0\n");
 822
 823        return sprintf(buf, "%d\n", 1000 * (data->temp_max[attr->index] - 64));
 824}
 825
 826static ssize_t temp_max_store(struct device *dev,
 827                              struct device_attribute *devattr,
 828                              const char *buf, size_t count)
 829{
 830        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 831        struct adt7462_data *data = dev_get_drvdata(dev);
 832        struct i2c_client *client = data->client;
 833        long temp;
 834
 835        if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
 836                return -EINVAL;
 837
 838        temp = clamp_val(temp, -64000, 191000);
 839        temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
 840
 841        mutex_lock(&data->lock);
 842        data->temp_max[attr->index] = temp;
 843        i2c_smbus_write_byte_data(client, ADT7462_TEMP_MAX_REG(attr->index),
 844                                  temp);
 845        mutex_unlock(&data->lock);
 846
 847        return count;
 848}
 849
 850static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
 851                         char *buf)
 852{
 853        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 854        struct adt7462_data *data = adt7462_update_device(dev);
 855        u8 frac = data->temp_frac[attr->index] >> TEMP_FRAC_OFFSET;
 856
 857        if (!temp_enabled(data, attr->index))
 858                return sprintf(buf, "0\n");
 859
 860        return sprintf(buf, "%d\n", 1000 * (data->temp[attr->index] - 64) +
 861                                     250 * frac);
 862}
 863
 864static ssize_t temp_label_show(struct device *dev,
 865                               struct device_attribute *devattr, char *buf)
 866{
 867        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 868        struct adt7462_data *data = adt7462_update_device(dev);
 869
 870        return sprintf(buf, "%s\n", temp_label(data, attr->index));
 871}
 872
 873static ssize_t volt_max_show(struct device *dev,
 874                             struct device_attribute *devattr, char *buf)
 875{
 876        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 877        struct adt7462_data *data = adt7462_update_device(dev);
 878        int x = voltage_multiplier(data, attr->index);
 879
 880        x *= data->volt_max[attr->index];
 881        x /= 1000; /* convert from uV to mV */
 882
 883        return sprintf(buf, "%d\n", x);
 884}
 885
 886static ssize_t volt_max_store(struct device *dev,
 887                              struct device_attribute *devattr,
 888                              const char *buf, size_t count)
 889{
 890        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 891        struct adt7462_data *data = dev_get_drvdata(dev);
 892        struct i2c_client *client = data->client;
 893        int x = voltage_multiplier(data, attr->index);
 894        long temp;
 895
 896        if (kstrtol(buf, 10, &temp) || !x)
 897                return -EINVAL;
 898
 899        temp = clamp_val(temp, 0, 255 * x / 1000);
 900        temp *= 1000; /* convert mV to uV */
 901        temp = DIV_ROUND_CLOSEST(temp, x);
 902
 903        mutex_lock(&data->lock);
 904        data->volt_max[attr->index] = temp;
 905        i2c_smbus_write_byte_data(client,
 906                                  ADT7462_REG_VOLT_MAX(data, attr->index),
 907                                  temp);
 908        mutex_unlock(&data->lock);
 909
 910        return count;
 911}
 912
 913static ssize_t volt_min_show(struct device *dev,
 914                             struct device_attribute *devattr, char *buf)
 915{
 916        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 917        struct adt7462_data *data = adt7462_update_device(dev);
 918        int x = voltage_multiplier(data, attr->index);
 919
 920        x *= data->volt_min[attr->index];
 921        x /= 1000; /* convert from uV to mV */
 922
 923        return sprintf(buf, "%d\n", x);
 924}
 925
 926static ssize_t volt_min_store(struct device *dev,
 927                              struct device_attribute *devattr,
 928                              const char *buf, size_t count)
 929{
 930        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 931        struct adt7462_data *data = dev_get_drvdata(dev);
 932        struct i2c_client *client = data->client;
 933        int x = voltage_multiplier(data, attr->index);
 934        long temp;
 935
 936        if (kstrtol(buf, 10, &temp) || !x)
 937                return -EINVAL;
 938
 939        temp = clamp_val(temp, 0, 255 * x / 1000);
 940        temp *= 1000; /* convert mV to uV */
 941        temp = DIV_ROUND_CLOSEST(temp, x);
 942
 943        mutex_lock(&data->lock);
 944        data->volt_min[attr->index] = temp;
 945        i2c_smbus_write_byte_data(client,
 946                                  ADT7462_REG_VOLT_MIN(data, attr->index),
 947                                  temp);
 948        mutex_unlock(&data->lock);
 949
 950        return count;
 951}
 952
 953static ssize_t voltage_show(struct device *dev,
 954                            struct device_attribute *devattr, char *buf)
 955{
 956        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 957        struct adt7462_data *data = adt7462_update_device(dev);
 958        int x = voltage_multiplier(data, attr->index);
 959
 960        x *= data->voltages[attr->index];
 961        x /= 1000; /* convert from uV to mV */
 962
 963        return sprintf(buf, "%d\n", x);
 964}
 965
 966static ssize_t voltage_label_show(struct device *dev,
 967                                  struct device_attribute *devattr, char *buf)
 968{
 969        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 970        struct adt7462_data *data = adt7462_update_device(dev);
 971
 972        return sprintf(buf, "%s\n", voltage_label(data, attr->index));
 973}
 974
 975static ssize_t alarm_show(struct device *dev,
 976                          struct device_attribute *devattr, char *buf)
 977{
 978        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 979        struct adt7462_data *data = adt7462_update_device(dev);
 980        int reg = attr->index >> ADT7462_ALARM_REG_SHIFT;
 981        int mask = attr->index & ADT7462_ALARM_FLAG_MASK;
 982
 983        if (data->alarms[reg] & mask)
 984                return sprintf(buf, "1\n");
 985        else
 986                return sprintf(buf, "0\n");
 987}
 988
 989static int fan_enabled(struct adt7462_data *data, int fan)
 990{
 991        return data->fan_enabled & (1 << fan);
 992}
 993
 994static ssize_t fan_min_show(struct device *dev,
 995                            struct device_attribute *devattr, char *buf)
 996{
 997        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 998        struct adt7462_data *data = adt7462_update_device(dev);
 999        u16 temp;
1000
1001        /* Only the MSB of the min fan period is stored... */
1002        temp = data->fan_min[attr->index];
1003        temp <<= 8;
1004
1005        if (!fan_enabled(data, attr->index) ||
1006            !FAN_DATA_VALID(temp))
1007                return sprintf(buf, "0\n");
1008
1009        return sprintf(buf, "%d\n", FAN_PERIOD_TO_RPM(temp));
1010}
1011
1012static ssize_t fan_min_store(struct device *dev,
1013                             struct device_attribute *devattr,
1014                             const char *buf, size_t count)
1015{
1016        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1017        struct adt7462_data *data = dev_get_drvdata(dev);
1018        struct i2c_client *client = data->client;
1019        long temp;
1020
1021        if (kstrtol(buf, 10, &temp) || !temp ||
1022            !fan_enabled(data, attr->index))
1023                return -EINVAL;
1024
1025        temp = FAN_RPM_TO_PERIOD(temp);
1026        temp >>= 8;
1027        temp = clamp_val(temp, 1, 255);
1028
1029        mutex_lock(&data->lock);
1030        data->fan_min[attr->index] = temp;
1031        i2c_smbus_write_byte_data(client, ADT7462_REG_FAN_MIN(attr->index),
1032                                  temp);
1033        mutex_unlock(&data->lock);
1034
1035        return count;
1036}
1037
1038static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
1039                        char *buf)
1040{
1041        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1042        struct adt7462_data *data = adt7462_update_device(dev);
1043
1044        if (!fan_enabled(data, attr->index) ||
1045            !FAN_DATA_VALID(data->fan[attr->index]))
1046                return sprintf(buf, "0\n");
1047
1048        return sprintf(buf, "%d\n",
1049                       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
1050}
1051
1052static ssize_t force_pwm_max_show(struct device *dev,
1053                                  struct device_attribute *devattr, char *buf)
1054{
1055        struct adt7462_data *data = adt7462_update_device(dev);
1056        return sprintf(buf, "%d\n", (data->cfg2 & ADT7462_FSPD_MASK ? 1 : 0));
1057}
1058
1059static ssize_t force_pwm_max_store(struct device *dev,
1060                                   struct device_attribute *devattr,
1061                                   const char *buf, size_t count)
1062{
1063        struct adt7462_data *data = dev_get_drvdata(dev);
1064        struct i2c_client *client = data->client;
1065        long temp;
1066        u8 reg;
1067
1068        if (kstrtol(buf, 10, &temp))
1069                return -EINVAL;
1070
1071        mutex_lock(&data->lock);
1072        reg = i2c_smbus_read_byte_data(client, ADT7462_REG_CFG2);
1073        if (temp)
1074                reg |= ADT7462_FSPD_MASK;
1075        else
1076                reg &= ~ADT7462_FSPD_MASK;
1077        data->cfg2 = reg;
1078        i2c_smbus_write_byte_data(client, ADT7462_REG_CFG2, reg);
1079        mutex_unlock(&data->lock);
1080
1081        return count;
1082}
1083
1084static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
1085                        char *buf)
1086{
1087        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1088        struct adt7462_data *data = adt7462_update_device(dev);
1089        return sprintf(buf, "%d\n", data->pwm[attr->index]);
1090}
1091
1092static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
1093                         const char *buf, size_t count)
1094{
1095        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1096        struct adt7462_data *data = dev_get_drvdata(dev);
1097        struct i2c_client *client = data->client;
1098        long temp;
1099
1100        if (kstrtol(buf, 10, &temp))
1101                return -EINVAL;
1102
1103        temp = clamp_val(temp, 0, 255);
1104
1105        mutex_lock(&data->lock);
1106        data->pwm[attr->index] = temp;
1107        i2c_smbus_write_byte_data(client, ADT7462_REG_PWM(attr->index), temp);
1108        mutex_unlock(&data->lock);
1109
1110        return count;
1111}
1112
1113static ssize_t pwm_max_show(struct device *dev,
1114                            struct device_attribute *devattr, char *buf)
1115{
1116        struct adt7462_data *data = adt7462_update_device(dev);
1117        return sprintf(buf, "%d\n", data->pwm_max);
1118}
1119
1120static ssize_t pwm_max_store(struct device *dev,
1121                             struct device_attribute *devattr,
1122                             const char *buf, size_t count)
1123{
1124        struct adt7462_data *data = dev_get_drvdata(dev);
1125        struct i2c_client *client = data->client;
1126        long temp;
1127
1128        if (kstrtol(buf, 10, &temp))
1129                return -EINVAL;
1130
1131        temp = clamp_val(temp, 0, 255);
1132
1133        mutex_lock(&data->lock);
1134        data->pwm_max = temp;
1135        i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_MAX, temp);
1136        mutex_unlock(&data->lock);
1137
1138        return count;
1139}
1140
1141static ssize_t pwm_min_show(struct device *dev,
1142                            struct device_attribute *devattr, char *buf)
1143{
1144        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1145        struct adt7462_data *data = adt7462_update_device(dev);
1146        return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
1147}
1148
1149static ssize_t pwm_min_store(struct device *dev,
1150                             struct device_attribute *devattr,
1151                             const char *buf, size_t count)
1152{
1153        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1154        struct adt7462_data *data = dev_get_drvdata(dev);
1155        struct i2c_client *client = data->client;
1156        long temp;
1157
1158        if (kstrtol(buf, 10, &temp))
1159                return -EINVAL;
1160
1161        temp = clamp_val(temp, 0, 255);
1162
1163        mutex_lock(&data->lock);
1164        data->pwm_min[attr->index] = temp;
1165        i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_MIN(attr->index),
1166                                  temp);
1167        mutex_unlock(&data->lock);
1168
1169        return count;
1170}
1171
1172static ssize_t pwm_hyst_show(struct device *dev,
1173                             struct device_attribute *devattr, char *buf)
1174{
1175        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1176        struct adt7462_data *data = adt7462_update_device(dev);
1177        return sprintf(buf, "%d\n", 1000 *
1178                      (data->pwm_trange[attr->index] & ADT7462_PWM_HYST_MASK));
1179}
1180
1181static ssize_t pwm_hyst_store(struct device *dev,
1182                              struct device_attribute *devattr,
1183                              const char *buf, size_t count)
1184{
1185        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1186        struct adt7462_data *data = dev_get_drvdata(dev);
1187        struct i2c_client *client = data->client;
1188        long temp;
1189
1190        if (kstrtol(buf, 10, &temp))
1191                return -EINVAL;
1192
1193        temp = clamp_val(temp, 0, 15000);
1194        temp = DIV_ROUND_CLOSEST(temp, 1000);
1195
1196        /* package things up */
1197        temp &= ADT7462_PWM_HYST_MASK;
1198        temp |= data->pwm_trange[attr->index] & ADT7462_PWM_RANGE_MASK;
1199
1200        mutex_lock(&data->lock);
1201        data->pwm_trange[attr->index] = temp;
1202        i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TRANGE(attr->index),
1203                                  temp);
1204        mutex_unlock(&data->lock);
1205
1206        return count;
1207}
1208
1209static ssize_t pwm_tmax_show(struct device *dev,
1210                             struct device_attribute *devattr, char *buf)
1211{
1212        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1213        struct adt7462_data *data = adt7462_update_device(dev);
1214
1215        /* tmax = tmin + trange */
1216        int trange = trange_values[data->pwm_trange[attr->index] >>
1217                                   ADT7462_PWM_RANGE_SHIFT];
1218        int tmin = (data->pwm_tmin[attr->index] - 64) * 1000;
1219
1220        return sprintf(buf, "%d\n", tmin + trange);
1221}
1222
1223static ssize_t pwm_tmax_store(struct device *dev,
1224                              struct device_attribute *devattr,
1225                              const char *buf, size_t count)
1226{
1227        int temp;
1228        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1229        struct adt7462_data *data = dev_get_drvdata(dev);
1230        struct i2c_client *client = data->client;
1231        int tmin, trange_value;
1232        long trange;
1233
1234        if (kstrtol(buf, 10, &trange))
1235                return -EINVAL;
1236
1237        /* trange = tmax - tmin */
1238        tmin = (data->pwm_tmin[attr->index] - 64) * 1000;
1239        trange_value = find_trange_value(trange - tmin);
1240        if (trange_value < 0)
1241                return trange_value;
1242
1243        temp = trange_value << ADT7462_PWM_RANGE_SHIFT;
1244        temp |= data->pwm_trange[attr->index] & ADT7462_PWM_HYST_MASK;
1245
1246        mutex_lock(&data->lock);
1247        data->pwm_trange[attr->index] = temp;
1248        i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TRANGE(attr->index),
1249                                  temp);
1250        mutex_unlock(&data->lock);
1251
1252        return count;
1253}
1254
1255static ssize_t pwm_tmin_show(struct device *dev,
1256                             struct device_attribute *devattr, char *buf)
1257{
1258        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1259        struct adt7462_data *data = adt7462_update_device(dev);
1260        return sprintf(buf, "%d\n", 1000 * (data->pwm_tmin[attr->index] - 64));
1261}
1262
1263static ssize_t pwm_tmin_store(struct device *dev,
1264                              struct device_attribute *devattr,
1265                              const char *buf, size_t count)
1266{
1267        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1268        struct adt7462_data *data = dev_get_drvdata(dev);
1269        struct i2c_client *client = data->client;
1270        long temp;
1271
1272        if (kstrtol(buf, 10, &temp))
1273                return -EINVAL;
1274
1275        temp = clamp_val(temp, -64000, 191000);
1276        temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
1277
1278        mutex_lock(&data->lock);
1279        data->pwm_tmin[attr->index] = temp;
1280        i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TMIN(attr->index),
1281                                  temp);
1282        mutex_unlock(&data->lock);
1283
1284        return count;
1285}
1286
1287static ssize_t pwm_auto_show(struct device *dev,
1288                             struct device_attribute *devattr, char *buf)
1289{
1290        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1291        struct adt7462_data *data = adt7462_update_device(dev);
1292        int cfg = data->pwm_cfg[attr->index] >> ADT7462_PWM_CHANNEL_SHIFT;
1293
1294        switch (cfg) {
1295        case 4: /* off */
1296                return sprintf(buf, "0\n");
1297        case 7: /* manual */
1298                return sprintf(buf, "1\n");
1299        default: /* automatic */
1300                return sprintf(buf, "2\n");
1301        }
1302}
1303
1304static void set_pwm_channel(struct i2c_client *client,
1305                            struct adt7462_data *data,
1306                            int which,
1307                            int value)
1308{
1309        int temp = data->pwm_cfg[which] & ~ADT7462_PWM_CHANNEL_MASK;
1310        temp |= value << ADT7462_PWM_CHANNEL_SHIFT;
1311
1312        mutex_lock(&data->lock);
1313        data->pwm_cfg[which] = temp;
1314        i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_CFG(which), temp);
1315        mutex_unlock(&data->lock);
1316}
1317
1318static ssize_t pwm_auto_store(struct device *dev,
1319                              struct device_attribute *devattr,
1320                              const char *buf, size_t count)
1321{
1322        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1323        struct adt7462_data *data = dev_get_drvdata(dev);
1324        struct i2c_client *client = data->client;
1325        long temp;
1326
1327        if (kstrtol(buf, 10, &temp))
1328                return -EINVAL;
1329
1330        switch (temp) {
1331        case 0: /* off */
1332                set_pwm_channel(client, data, attr->index, 4);
1333                return count;
1334        case 1: /* manual */
1335                set_pwm_channel(client, data, attr->index, 7);
1336                return count;
1337        default:
1338                return -EINVAL;
1339        }
1340}
1341
1342static ssize_t pwm_auto_temp_show(struct device *dev,
1343                                  struct device_attribute *devattr, char *buf)
1344{
1345        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1346        struct adt7462_data *data = adt7462_update_device(dev);
1347        int channel = data->pwm_cfg[attr->index] >> ADT7462_PWM_CHANNEL_SHIFT;
1348
1349        switch (channel) {
1350        case 0: /* temp[1234] only */
1351        case 1:
1352        case 2:
1353        case 3:
1354                return sprintf(buf, "%d\n", (1 << channel));
1355        case 5: /* temp1 & temp4  */
1356                return sprintf(buf, "9\n");
1357        case 6:
1358                return sprintf(buf, "15\n");
1359        default:
1360                return sprintf(buf, "0\n");
1361        }
1362}
1363
1364static int cvt_auto_temp(int input)
1365{
1366        if (input == 0xF)
1367                return 6;
1368        if (input == 0x9)
1369                return 5;
1370        if (input < 1 || !is_power_of_2(input))
1371                return -EINVAL;
1372        return ilog2(input);
1373}
1374
1375static ssize_t pwm_auto_temp_store(struct device *dev,
1376                                   struct device_attribute *devattr,
1377                                   const char *buf, size_t count)
1378{
1379        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1380        struct adt7462_data *data = dev_get_drvdata(dev);
1381        struct i2c_client *client = data->client;
1382        long temp;
1383
1384        if (kstrtol(buf, 10, &temp))
1385                return -EINVAL;
1386
1387        temp = cvt_auto_temp(temp);
1388        if (temp < 0)
1389                return temp;
1390
1391        set_pwm_channel(client, data, attr->index, temp);
1392
1393        return count;
1394}
1395
1396static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
1397static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
1398static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
1399static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
1400
1401static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
1402static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
1403static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
1404static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
1405
1406static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1407static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1408static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1409static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
1410
1411static SENSOR_DEVICE_ATTR_RO(temp1_label, temp_label, 0);
1412static SENSOR_DEVICE_ATTR_RO(temp2_label, temp_label, 1);
1413static SENSOR_DEVICE_ATTR_RO(temp3_label, temp_label, 2);
1414static SENSOR_DEVICE_ATTR_RO(temp4_label, temp_label, 3);
1415
1416static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm,
1417                             ADT7462_ALARM1 | ADT7462_LT_ALARM);
1418static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm,
1419                             ADT7462_ALARM1 | ADT7462_R1T_ALARM);
1420static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm,
1421                             ADT7462_ALARM1 | ADT7462_R2T_ALARM);
1422static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm,
1423                             ADT7462_ALARM1 | ADT7462_R3T_ALARM);
1424
1425static SENSOR_DEVICE_ATTR_RW(in1_max, volt_max, 0);
1426static SENSOR_DEVICE_ATTR_RW(in2_max, volt_max, 1);
1427static SENSOR_DEVICE_ATTR_RW(in3_max, volt_max, 2);
1428static SENSOR_DEVICE_ATTR_RW(in4_max, volt_max, 3);
1429static SENSOR_DEVICE_ATTR_RW(in5_max, volt_max, 4);
1430static SENSOR_DEVICE_ATTR_RW(in6_max, volt_max, 5);
1431static SENSOR_DEVICE_ATTR_RW(in7_max, volt_max, 6);
1432static SENSOR_DEVICE_ATTR_RW(in8_max, volt_max, 7);
1433static SENSOR_DEVICE_ATTR_RW(in9_max, volt_max, 8);
1434static SENSOR_DEVICE_ATTR_RW(in10_max, volt_max, 9);
1435static SENSOR_DEVICE_ATTR_RW(in11_max, volt_max, 10);
1436static SENSOR_DEVICE_ATTR_RW(in12_max, volt_max, 11);
1437static SENSOR_DEVICE_ATTR_RW(in13_max, volt_max, 12);
1438
1439static SENSOR_DEVICE_ATTR_RW(in1_min, volt_min, 0);
1440static SENSOR_DEVICE_ATTR_RW(in2_min, volt_min, 1);
1441static SENSOR_DEVICE_ATTR_RW(in3_min, volt_min, 2);
1442static SENSOR_DEVICE_ATTR_RW(in4_min, volt_min, 3);
1443static SENSOR_DEVICE_ATTR_RW(in5_min, volt_min, 4);
1444static SENSOR_DEVICE_ATTR_RW(in6_min, volt_min, 5);
1445static SENSOR_DEVICE_ATTR_RW(in7_min, volt_min, 6);
1446static SENSOR_DEVICE_ATTR_RW(in8_min, volt_min, 7);
1447static SENSOR_DEVICE_ATTR_RW(in9_min, volt_min, 8);
1448static SENSOR_DEVICE_ATTR_RW(in10_min, volt_min, 9);
1449static SENSOR_DEVICE_ATTR_RW(in11_min, volt_min, 10);
1450static SENSOR_DEVICE_ATTR_RW(in12_min, volt_min, 11);
1451static SENSOR_DEVICE_ATTR_RW(in13_min, volt_min, 12);
1452
1453static SENSOR_DEVICE_ATTR_RO(in1_input, voltage, 0);
1454static SENSOR_DEVICE_ATTR_RO(in2_input, voltage, 1);
1455static SENSOR_DEVICE_ATTR_RO(in3_input, voltage, 2);
1456static SENSOR_DEVICE_ATTR_RO(in4_input, voltage, 3);
1457static SENSOR_DEVICE_ATTR_RO(in5_input, voltage, 4);
1458static SENSOR_DEVICE_ATTR_RO(in6_input, voltage, 5);
1459static SENSOR_DEVICE_ATTR_RO(in7_input, voltage, 6);
1460static SENSOR_DEVICE_ATTR_RO(in8_input, voltage, 7);
1461static SENSOR_DEVICE_ATTR_RO(in9_input, voltage, 8);
1462static SENSOR_DEVICE_ATTR_RO(in10_input, voltage, 9);
1463static SENSOR_DEVICE_ATTR_RO(in11_input, voltage, 10);
1464static SENSOR_DEVICE_ATTR_RO(in12_input, voltage, 11);
1465static SENSOR_DEVICE_ATTR_RO(in13_input, voltage, 12);
1466
1467static SENSOR_DEVICE_ATTR_RO(in1_label, voltage_label, 0);
1468static SENSOR_DEVICE_ATTR_RO(in2_label, voltage_label, 1);
1469static SENSOR_DEVICE_ATTR_RO(in3_label, voltage_label, 2);
1470static SENSOR_DEVICE_ATTR_RO(in4_label, voltage_label, 3);
1471static SENSOR_DEVICE_ATTR_RO(in5_label, voltage_label, 4);
1472static SENSOR_DEVICE_ATTR_RO(in6_label, voltage_label, 5);
1473static SENSOR_DEVICE_ATTR_RO(in7_label, voltage_label, 6);
1474static SENSOR_DEVICE_ATTR_RO(in8_label, voltage_label, 7);
1475static SENSOR_DEVICE_ATTR_RO(in9_label, voltage_label, 8);
1476static SENSOR_DEVICE_ATTR_RO(in10_label, voltage_label, 9);
1477static SENSOR_DEVICE_ATTR_RO(in11_label, voltage_label, 10);
1478static SENSOR_DEVICE_ATTR_RO(in12_label, voltage_label, 11);
1479static SENSOR_DEVICE_ATTR_RO(in13_label, voltage_label, 12);
1480
1481static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm,
1482                             ADT7462_ALARM2 | ADT7462_V0_ALARM);
1483static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm,
1484                             ADT7462_ALARM2 | ADT7462_V7_ALARM);
1485static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm,
1486                             ADT7462_ALARM2 | ADT7462_V2_ALARM);
1487static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm,
1488                             ADT7462_ALARM2 | ADT7462_V6_ALARM);
1489static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm,
1490                             ADT7462_ALARM2 | ADT7462_V5_ALARM);
1491static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm,
1492                             ADT7462_ALARM2 | ADT7462_V4_ALARM);
1493static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm,
1494                             ADT7462_ALARM2 | ADT7462_V3_ALARM);
1495static SENSOR_DEVICE_ATTR_RO(in8_alarm, alarm,
1496                             ADT7462_ALARM2 | ADT7462_V1_ALARM);
1497static SENSOR_DEVICE_ATTR_RO(in9_alarm, alarm,
1498                             ADT7462_ALARM3 | ADT7462_V10_ALARM);
1499static SENSOR_DEVICE_ATTR_RO(in10_alarm, alarm,
1500                             ADT7462_ALARM3 | ADT7462_V9_ALARM);
1501static SENSOR_DEVICE_ATTR_RO(in11_alarm, alarm,
1502                             ADT7462_ALARM3 | ADT7462_V8_ALARM);
1503static SENSOR_DEVICE_ATTR_RO(in12_alarm, alarm,
1504                             ADT7462_ALARM3 | ADT7462_V11_ALARM);
1505static SENSOR_DEVICE_ATTR_RO(in13_alarm, alarm,
1506                             ADT7462_ALARM3 | ADT7462_V12_ALARM);
1507
1508static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
1509static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
1510static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
1511static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
1512static SENSOR_DEVICE_ATTR_RW(fan5_min, fan_min, 4);
1513static SENSOR_DEVICE_ATTR_RW(fan6_min, fan_min, 5);
1514static SENSOR_DEVICE_ATTR_RW(fan7_min, fan_min, 6);
1515static SENSOR_DEVICE_ATTR_RW(fan8_min, fan_min, 7);
1516
1517static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
1518static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
1519static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
1520static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
1521static SENSOR_DEVICE_ATTR_RO(fan5_input, fan, 4);
1522static SENSOR_DEVICE_ATTR_RO(fan6_input, fan, 5);
1523static SENSOR_DEVICE_ATTR_RO(fan7_input, fan, 6);
1524static SENSOR_DEVICE_ATTR_RO(fan8_input, fan, 7);
1525
1526static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm,
1527                             ADT7462_ALARM4 | ADT7462_F0_ALARM);
1528static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm,
1529                             ADT7462_ALARM4 | ADT7462_F1_ALARM);
1530static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm,
1531                             ADT7462_ALARM4 | ADT7462_F2_ALARM);
1532static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm,
1533                             ADT7462_ALARM4 | ADT7462_F3_ALARM);
1534static SENSOR_DEVICE_ATTR_RO(fan5_alarm, alarm,
1535                             ADT7462_ALARM4 | ADT7462_F4_ALARM);
1536static SENSOR_DEVICE_ATTR_RO(fan6_alarm, alarm,
1537                             ADT7462_ALARM4 | ADT7462_F5_ALARM);
1538static SENSOR_DEVICE_ATTR_RO(fan7_alarm, alarm,
1539                             ADT7462_ALARM4 | ADT7462_F6_ALARM);
1540static SENSOR_DEVICE_ATTR_RO(fan8_alarm, alarm,
1541                             ADT7462_ALARM4 | ADT7462_F7_ALARM);
1542
1543static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0);
1544
1545static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1546static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1547static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
1548static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3);
1549
1550static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0);
1551static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1);
1552static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2);
1553static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3);
1554
1555static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0);
1556static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1);
1557static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2);
1558static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3);
1559
1560static SENSOR_DEVICE_ATTR_RW(temp1_auto_point1_hyst, pwm_hyst, 0);
1561static SENSOR_DEVICE_ATTR_RW(temp2_auto_point1_hyst, pwm_hyst, 1);
1562static SENSOR_DEVICE_ATTR_RW(temp3_auto_point1_hyst, pwm_hyst, 2);
1563static SENSOR_DEVICE_ATTR_RW(temp4_auto_point1_hyst, pwm_hyst, 3);
1564
1565static SENSOR_DEVICE_ATTR_RW(temp1_auto_point2_hyst, pwm_hyst, 0);
1566static SENSOR_DEVICE_ATTR_RW(temp2_auto_point2_hyst, pwm_hyst, 1);
1567static SENSOR_DEVICE_ATTR_RW(temp3_auto_point2_hyst, pwm_hyst, 2);
1568static SENSOR_DEVICE_ATTR_RW(temp4_auto_point2_hyst, pwm_hyst, 3);
1569
1570static SENSOR_DEVICE_ATTR_RW(temp1_auto_point1_temp, pwm_tmin, 0);
1571static SENSOR_DEVICE_ATTR_RW(temp2_auto_point1_temp, pwm_tmin, 1);
1572static SENSOR_DEVICE_ATTR_RW(temp3_auto_point1_temp, pwm_tmin, 2);
1573static SENSOR_DEVICE_ATTR_RW(temp4_auto_point1_temp, pwm_tmin, 3);
1574
1575static SENSOR_DEVICE_ATTR_RW(temp1_auto_point2_temp, pwm_tmax, 0);
1576static SENSOR_DEVICE_ATTR_RW(temp2_auto_point2_temp, pwm_tmax, 1);
1577static SENSOR_DEVICE_ATTR_RW(temp3_auto_point2_temp, pwm_tmax, 2);
1578static SENSOR_DEVICE_ATTR_RW(temp4_auto_point2_temp, pwm_tmax, 3);
1579
1580static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_auto, 0);
1581static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_auto, 1);
1582static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_auto, 2);
1583static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_auto, 3);
1584
1585static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0);
1586static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1);
1587static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2);
1588static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3);
1589
1590static struct attribute *adt7462_attrs[] = {
1591        &sensor_dev_attr_temp1_max.dev_attr.attr,
1592        &sensor_dev_attr_temp2_max.dev_attr.attr,
1593        &sensor_dev_attr_temp3_max.dev_attr.attr,
1594        &sensor_dev_attr_temp4_max.dev_attr.attr,
1595
1596        &sensor_dev_attr_temp1_min.dev_attr.attr,
1597        &sensor_dev_attr_temp2_min.dev_attr.attr,
1598        &sensor_dev_attr_temp3_min.dev_attr.attr,
1599        &sensor_dev_attr_temp4_min.dev_attr.attr,
1600
1601        &sensor_dev_attr_temp1_input.dev_attr.attr,
1602        &sensor_dev_attr_temp2_input.dev_attr.attr,
1603        &sensor_dev_attr_temp3_input.dev_attr.attr,
1604        &sensor_dev_attr_temp4_input.dev_attr.attr,
1605
1606        &sensor_dev_attr_temp1_label.dev_attr.attr,
1607        &sensor_dev_attr_temp2_label.dev_attr.attr,
1608        &sensor_dev_attr_temp3_label.dev_attr.attr,
1609        &sensor_dev_attr_temp4_label.dev_attr.attr,
1610
1611        &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1612        &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1613        &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1614        &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1615
1616        &sensor_dev_attr_in1_max.dev_attr.attr,
1617        &sensor_dev_attr_in2_max.dev_attr.attr,
1618        &sensor_dev_attr_in3_max.dev_attr.attr,
1619        &sensor_dev_attr_in4_max.dev_attr.attr,
1620        &sensor_dev_attr_in5_max.dev_attr.attr,
1621        &sensor_dev_attr_in6_max.dev_attr.attr,
1622        &sensor_dev_attr_in7_max.dev_attr.attr,
1623        &sensor_dev_attr_in8_max.dev_attr.attr,
1624        &sensor_dev_attr_in9_max.dev_attr.attr,
1625        &sensor_dev_attr_in10_max.dev_attr.attr,
1626        &sensor_dev_attr_in11_max.dev_attr.attr,
1627        &sensor_dev_attr_in12_max.dev_attr.attr,
1628        &sensor_dev_attr_in13_max.dev_attr.attr,
1629
1630        &sensor_dev_attr_in1_min.dev_attr.attr,
1631        &sensor_dev_attr_in2_min.dev_attr.attr,
1632        &sensor_dev_attr_in3_min.dev_attr.attr,
1633        &sensor_dev_attr_in4_min.dev_attr.attr,
1634        &sensor_dev_attr_in5_min.dev_attr.attr,
1635        &sensor_dev_attr_in6_min.dev_attr.attr,
1636        &sensor_dev_attr_in7_min.dev_attr.attr,
1637        &sensor_dev_attr_in8_min.dev_attr.attr,
1638        &sensor_dev_attr_in9_min.dev_attr.attr,
1639        &sensor_dev_attr_in10_min.dev_attr.attr,
1640        &sensor_dev_attr_in11_min.dev_attr.attr,
1641        &sensor_dev_attr_in12_min.dev_attr.attr,
1642        &sensor_dev_attr_in13_min.dev_attr.attr,
1643
1644        &sensor_dev_attr_in1_input.dev_attr.attr,
1645        &sensor_dev_attr_in2_input.dev_attr.attr,
1646        &sensor_dev_attr_in3_input.dev_attr.attr,
1647        &sensor_dev_attr_in4_input.dev_attr.attr,
1648        &sensor_dev_attr_in5_input.dev_attr.attr,
1649        &sensor_dev_attr_in6_input.dev_attr.attr,
1650        &sensor_dev_attr_in7_input.dev_attr.attr,
1651        &sensor_dev_attr_in8_input.dev_attr.attr,
1652        &sensor_dev_attr_in9_input.dev_attr.attr,
1653        &sensor_dev_attr_in10_input.dev_attr.attr,
1654        &sensor_dev_attr_in11_input.dev_attr.attr,
1655        &sensor_dev_attr_in12_input.dev_attr.attr,
1656        &sensor_dev_attr_in13_input.dev_attr.attr,
1657
1658        &sensor_dev_attr_in1_label.dev_attr.attr,
1659        &sensor_dev_attr_in2_label.dev_attr.attr,
1660        &sensor_dev_attr_in3_label.dev_attr.attr,
1661        &sensor_dev_attr_in4_label.dev_attr.attr,
1662        &sensor_dev_attr_in5_label.dev_attr.attr,
1663        &sensor_dev_attr_in6_label.dev_attr.attr,
1664        &sensor_dev_attr_in7_label.dev_attr.attr,
1665        &sensor_dev_attr_in8_label.dev_attr.attr,
1666        &sensor_dev_attr_in9_label.dev_attr.attr,
1667        &sensor_dev_attr_in10_label.dev_attr.attr,
1668        &sensor_dev_attr_in11_label.dev_attr.attr,
1669        &sensor_dev_attr_in12_label.dev_attr.attr,
1670        &sensor_dev_attr_in13_label.dev_attr.attr,
1671
1672        &sensor_dev_attr_in1_alarm.dev_attr.attr,
1673        &sensor_dev_attr_in2_alarm.dev_attr.attr,
1674        &sensor_dev_attr_in3_alarm.dev_attr.attr,
1675        &sensor_dev_attr_in4_alarm.dev_attr.attr,
1676        &sensor_dev_attr_in5_alarm.dev_attr.attr,
1677        &sensor_dev_attr_in6_alarm.dev_attr.attr,
1678        &sensor_dev_attr_in7_alarm.dev_attr.attr,
1679        &sensor_dev_attr_in8_alarm.dev_attr.attr,
1680        &sensor_dev_attr_in9_alarm.dev_attr.attr,
1681        &sensor_dev_attr_in10_alarm.dev_attr.attr,
1682        &sensor_dev_attr_in11_alarm.dev_attr.attr,
1683        &sensor_dev_attr_in12_alarm.dev_attr.attr,
1684        &sensor_dev_attr_in13_alarm.dev_attr.attr,
1685
1686        &sensor_dev_attr_fan1_min.dev_attr.attr,
1687        &sensor_dev_attr_fan2_min.dev_attr.attr,
1688        &sensor_dev_attr_fan3_min.dev_attr.attr,
1689        &sensor_dev_attr_fan4_min.dev_attr.attr,
1690        &sensor_dev_attr_fan5_min.dev_attr.attr,
1691        &sensor_dev_attr_fan6_min.dev_attr.attr,
1692        &sensor_dev_attr_fan7_min.dev_attr.attr,
1693        &sensor_dev_attr_fan8_min.dev_attr.attr,
1694
1695        &sensor_dev_attr_fan1_input.dev_attr.attr,
1696        &sensor_dev_attr_fan2_input.dev_attr.attr,
1697        &sensor_dev_attr_fan3_input.dev_attr.attr,
1698        &sensor_dev_attr_fan4_input.dev_attr.attr,
1699        &sensor_dev_attr_fan5_input.dev_attr.attr,
1700        &sensor_dev_attr_fan6_input.dev_attr.attr,
1701        &sensor_dev_attr_fan7_input.dev_attr.attr,
1702        &sensor_dev_attr_fan8_input.dev_attr.attr,
1703
1704        &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1705        &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1706        &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1707        &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1708        &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1709        &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1710        &sensor_dev_attr_fan7_alarm.dev_attr.attr,
1711        &sensor_dev_attr_fan8_alarm.dev_attr.attr,
1712
1713        &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1714        &sensor_dev_attr_pwm1.dev_attr.attr,
1715        &sensor_dev_attr_pwm2.dev_attr.attr,
1716        &sensor_dev_attr_pwm3.dev_attr.attr,
1717        &sensor_dev_attr_pwm4.dev_attr.attr,
1718
1719        &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1720        &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1721        &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1722        &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1723
1724        &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1725        &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1726        &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1727        &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1728
1729        &sensor_dev_attr_temp1_auto_point1_hyst.dev_attr.attr,
1730        &sensor_dev_attr_temp2_auto_point1_hyst.dev_attr.attr,
1731        &sensor_dev_attr_temp3_auto_point1_hyst.dev_attr.attr,
1732        &sensor_dev_attr_temp4_auto_point1_hyst.dev_attr.attr,
1733
1734        &sensor_dev_attr_temp1_auto_point2_hyst.dev_attr.attr,
1735        &sensor_dev_attr_temp2_auto_point2_hyst.dev_attr.attr,
1736        &sensor_dev_attr_temp3_auto_point2_hyst.dev_attr.attr,
1737        &sensor_dev_attr_temp4_auto_point2_hyst.dev_attr.attr,
1738
1739        &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1740        &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1741        &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1742        &sensor_dev_attr_temp4_auto_point1_temp.dev_attr.attr,
1743
1744        &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1745        &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1746        &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1747        &sensor_dev_attr_temp4_auto_point2_temp.dev_attr.attr,
1748
1749        &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1750        &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1751        &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1752        &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1753
1754        &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1755        &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1756        &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1757        &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1758        NULL
1759};
1760
1761ATTRIBUTE_GROUPS(adt7462);
1762
1763/* Return 0 if detection is successful, -ENODEV otherwise */
1764static int adt7462_detect(struct i2c_client *client,
1765                          struct i2c_board_info *info)
1766{
1767        struct i2c_adapter *adapter = client->adapter;
1768        int vendor, device, revision;
1769
1770        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1771                return -ENODEV;
1772
1773        vendor = i2c_smbus_read_byte_data(client, ADT7462_REG_VENDOR);
1774        if (vendor != ADT7462_VENDOR)
1775                return -ENODEV;
1776
1777        device = i2c_smbus_read_byte_data(client, ADT7462_REG_DEVICE);
1778        if (device != ADT7462_DEVICE)
1779                return -ENODEV;
1780
1781        revision = i2c_smbus_read_byte_data(client, ADT7462_REG_REVISION);
1782        if (revision != ADT7462_REVISION)
1783                return -ENODEV;
1784
1785        strlcpy(info->type, "adt7462", I2C_NAME_SIZE);
1786
1787        return 0;
1788}
1789
1790static int adt7462_probe(struct i2c_client *client)
1791{
1792        struct device *dev = &client->dev;
1793        struct adt7462_data *data;
1794        struct device *hwmon_dev;
1795
1796        data = devm_kzalloc(dev, sizeof(struct adt7462_data), GFP_KERNEL);
1797        if (!data)
1798                return -ENOMEM;
1799
1800        data->client = client;
1801        mutex_init(&data->lock);
1802
1803        dev_info(&client->dev, "%s chip found\n", client->name);
1804
1805        hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1806                                                           data,
1807                                                           adt7462_groups);
1808        return PTR_ERR_OR_ZERO(hwmon_dev);
1809}
1810
1811static const struct i2c_device_id adt7462_id[] = {
1812        { "adt7462", 0 },
1813        { }
1814};
1815MODULE_DEVICE_TABLE(i2c, adt7462_id);
1816
1817static struct i2c_driver adt7462_driver = {
1818        .class          = I2C_CLASS_HWMON,
1819        .driver = {
1820                .name   = "adt7462",
1821        },
1822        .probe_new      = adt7462_probe,
1823        .id_table       = adt7462_id,
1824        .detect         = adt7462_detect,
1825        .address_list   = normal_i2c,
1826};
1827
1828module_i2c_driver(adt7462_driver);
1829
1830MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
1831MODULE_DESCRIPTION("ADT7462 driver");
1832MODULE_LICENSE("GPL");
1833