linux/drivers/hwmon/adm1026.c
<<
>>
Prefs
   1/*
   2    adm1026.c - Part of lm_sensors, Linux kernel modules for hardware
   3             monitoring
   4    Copyright (C) 2002, 2003  Philip Pokorny <ppokorny@penguincomputing.com>
   5    Copyright (C) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
   6
   7    Chip details at:
   8
   9    <http://www.analog.com/UploadedFiles/Data_Sheets/779263102ADM1026_a.pdf>
  10
  11    This program is free software; you can redistribute it and/or modify
  12    it under the terms of the GNU General Public License as published by
  13    the Free Software Foundation; either version 2 of the License, or
  14    (at your option) any later version.
  15
  16    This program is distributed in the hope that it will be useful,
  17    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19    GNU General Public License for more details.
  20
  21    You should have received a copy of the GNU General Public License
  22    along with this program; if not, write to the Free Software
  23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24*/
  25
  26#include <linux/module.h>
  27#include <linux/init.h>
  28#include <linux/slab.h>
  29#include <linux/jiffies.h>
  30#include <linux/i2c.h>
  31#include <linux/hwmon.h>
  32#include <linux/hwmon-sysfs.h>
  33#include <linux/hwmon-vid.h>
  34#include <linux/err.h>
  35#include <linux/mutex.h>
  36
  37/* Addresses to scan */
  38static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  39
  40/* Insmod parameters */
  41I2C_CLIENT_INSMOD_1(adm1026);
  42
  43static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
  44                                -1, -1, -1, -1, -1, -1, -1, -1 };
  45static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
  46                                -1, -1, -1, -1, -1, -1, -1, -1 };
  47static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
  48                                -1, -1, -1, -1, -1, -1, -1, -1 };
  49static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
  50                                -1, -1, -1, -1, -1, -1, -1, -1 };
  51static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
  52module_param_array(gpio_input, int, NULL, 0);
  53MODULE_PARM_DESC(gpio_input, "List of GPIO pins (0-16) to program as inputs");
  54module_param_array(gpio_output, int, NULL, 0);
  55MODULE_PARM_DESC(gpio_output, "List of GPIO pins (0-16) to program as "
  56        "outputs");
  57module_param_array(gpio_inverted, int, NULL, 0);
  58MODULE_PARM_DESC(gpio_inverted, "List of GPIO pins (0-16) to program as "
  59        "inverted");
  60module_param_array(gpio_normal, int, NULL, 0);
  61MODULE_PARM_DESC(gpio_normal, "List of GPIO pins (0-16) to program as "
  62        "normal/non-inverted");
  63module_param_array(gpio_fan, int, NULL, 0);
  64MODULE_PARM_DESC(gpio_fan, "List of GPIO pins (0-7) to program as fan tachs");
  65
  66/* Many ADM1026 constants specified below */
  67
  68/* The ADM1026 registers */
  69#define ADM1026_REG_CONFIG1     0x00
  70#define CFG1_MONITOR            0x01
  71#define CFG1_INT_ENABLE         0x02
  72#define CFG1_INT_CLEAR          0x04
  73#define CFG1_AIN8_9             0x08
  74#define CFG1_THERM_HOT          0x10
  75#define CFG1_DAC_AFC            0x20
  76#define CFG1_PWM_AFC            0x40
  77#define CFG1_RESET              0x80
  78
  79#define ADM1026_REG_CONFIG2     0x01
  80/* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
  81
  82#define ADM1026_REG_CONFIG3     0x07
  83#define CFG3_GPIO16_ENABLE      0x01
  84#define CFG3_CI_CLEAR           0x02
  85#define CFG3_VREF_250           0x04
  86#define CFG3_GPIO16_DIR         0x40
  87#define CFG3_GPIO16_POL         0x80
  88
  89#define ADM1026_REG_E2CONFIG    0x13
  90#define E2CFG_READ              0x01
  91#define E2CFG_WRITE             0x02
  92#define E2CFG_ERASE             0x04
  93#define E2CFG_ROM               0x08
  94#define E2CFG_CLK_EXT           0x80
  95
  96/* There are 10 general analog inputs and 7 dedicated inputs
  97 * They are:
  98 *    0 - 9  =  AIN0 - AIN9
  99 *       10  =  Vbat
 100 *       11  =  3.3V Standby
 101 *       12  =  3.3V Main
 102 *       13  =  +5V
 103 *       14  =  Vccp (CPU core voltage)
 104 *       15  =  +12V
 105 *       16  =  -12V
 106 */
 107static u16 ADM1026_REG_IN[] = {
 108                0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
 109                0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
 110                0x2b, 0x2c, 0x2d, 0x2e, 0x2f
 111        };
 112static u16 ADM1026_REG_IN_MIN[] = {
 113                0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
 114                0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
 115                0x4b, 0x4c, 0x4d, 0x4e, 0x4f
 116        };
 117static u16 ADM1026_REG_IN_MAX[] = {
 118                0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
 119                0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
 120                0x43, 0x44, 0x45, 0x46, 0x47
 121        };
 122
 123/* Temperatures are:
 124 *    0 - Internal
 125 *    1 - External 1
 126 *    2 - External 2
 127 */
 128static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
 129static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
 130static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
 131static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
 132static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
 133static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
 134
 135#define ADM1026_REG_FAN(nr)             (0x38 + (nr))
 136#define ADM1026_REG_FAN_MIN(nr)         (0x60 + (nr))
 137#define ADM1026_REG_FAN_DIV_0_3         0x02
 138#define ADM1026_REG_FAN_DIV_4_7         0x03
 139
 140#define ADM1026_REG_DAC                 0x04
 141#define ADM1026_REG_PWM                 0x05
 142
 143#define ADM1026_REG_GPIO_CFG_0_3        0x08
 144#define ADM1026_REG_GPIO_CFG_4_7        0x09
 145#define ADM1026_REG_GPIO_CFG_8_11       0x0a
 146#define ADM1026_REG_GPIO_CFG_12_15      0x0b
 147/* CFG_16 in REG_CFG3 */
 148#define ADM1026_REG_GPIO_STATUS_0_7     0x24
 149#define ADM1026_REG_GPIO_STATUS_8_15    0x25
 150/* STATUS_16 in REG_STATUS4 */
 151#define ADM1026_REG_GPIO_MASK_0_7       0x1c
 152#define ADM1026_REG_GPIO_MASK_8_15      0x1d
 153/* MASK_16 in REG_MASK4 */
 154
 155#define ADM1026_REG_COMPANY             0x16
 156#define ADM1026_REG_VERSTEP             0x17
 157/* These are the recognized values for the above regs */
 158#define ADM1026_COMPANY_ANALOG_DEV      0x41
 159#define ADM1026_VERSTEP_GENERIC         0x40
 160#define ADM1026_VERSTEP_ADM1026         0x44
 161
 162#define ADM1026_REG_MASK1               0x18
 163#define ADM1026_REG_MASK2               0x19
 164#define ADM1026_REG_MASK3               0x1a
 165#define ADM1026_REG_MASK4               0x1b
 166
 167#define ADM1026_REG_STATUS1             0x20
 168#define ADM1026_REG_STATUS2             0x21
 169#define ADM1026_REG_STATUS3             0x22
 170#define ADM1026_REG_STATUS4             0x23
 171
 172#define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
 173#define ADM1026_FAN_CONTROL_TEMP_RANGE  20
 174#define ADM1026_PWM_MAX                 255
 175
 176/* Conversions. Rounding and limit checking is only done on the TO_REG
 177 * variants. Note that you should be a bit careful with which arguments
 178 * these macros are called: arguments may be evaluated more than once.
 179 */
 180
 181/* IN are scaled acording to built-in resistors.  These are the
 182 *   voltages corresponding to 3/4 of full scale (192 or 0xc0)
 183 *   NOTE: The -12V input needs an additional factor to account
 184 *      for the Vref pullup resistor.
 185 *      NEG12_OFFSET = SCALE * Vref / V-192 - Vref
 186 *                   = 13875 * 2.50 / 1.875 - 2500
 187 *                   = 16000
 188 *
 189 * The values in this table are based on Table II, page 15 of the
 190 *    datasheet.
 191 */
 192static int adm1026_scaling[] = { /* .001 Volts */
 193                2250, 2250, 2250, 2250, 2250, 2250,
 194                1875, 1875, 1875, 1875, 3000, 3330,
 195                3330, 4995, 2250, 12000, 13875
 196        };
 197#define NEG12_OFFSET  16000
 198#define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
 199#define INS_TO_REG(n, val)  (SENSORS_LIMIT(SCALE(val, adm1026_scaling[n], 192),\
 200        0, 255))
 201#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
 202
 203/* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
 204 *   and we assume a 2 pulse-per-rev fan tach signal
 205 *      22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
 206 */
 207#define FAN_TO_REG(val, div)  ((val) <= 0 ? 0xff : \
 208                                SENSORS_LIMIT(1350000/((val)*(div)), 1, 254))
 209#define FAN_FROM_REG(val, div) ((val) == 0 ? -1:(val) == 0xff ? 0 : \
 210                                1350000/((val)*(div)))
 211#define DIV_FROM_REG(val) (1<<(val))
 212#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
 213
 214/* Temperature is reported in 1 degC increments */
 215#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
 216        -127, 127))
 217#define TEMP_FROM_REG(val) ((val) * 1000)
 218#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
 219        -127, 127))
 220#define OFFSET_FROM_REG(val) ((val) * 1000)
 221
 222#define PWM_TO_REG(val) (SENSORS_LIMIT(val, 0, 255))
 223#define PWM_FROM_REG(val) (val)
 224
 225#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
 226#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
 227
 228/* Analog output is a voltage, and scaled to millivolts.  The datasheet
 229 *   indicates that the DAC could be used to drive the fans, but in our
 230 *   example board (Arima HDAMA) it isn't connected to the fans at all.
 231 */
 232#define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500), 0, 255))
 233#define DAC_FROM_REG(val) (((val)*2500)/255)
 234
 235/* Chip sampling rates
 236 *
 237 * Some sensors are not updated more frequently than once per second
 238 *    so it doesn't make sense to read them more often than that.
 239 *    We cache the results and return the saved data if the driver
 240 *    is called again before a second has elapsed.
 241 *
 242 * Also, there is significant configuration data for this chip
 243 *    So, we keep the config data up to date in the cache
 244 *    when it is written and only sample it once every 5 *minutes*
 245 */
 246#define ADM1026_DATA_INTERVAL           (1 * HZ)
 247#define ADM1026_CONFIG_INTERVAL         (5 * 60 * HZ)
 248
 249/* We allow for multiple chips in a single system.
 250 *
 251 * For each registered ADM1026, we need to keep state information
 252 * at client->data. The adm1026_data structure is dynamically
 253 * allocated, when a new client structure is allocated. */
 254
 255struct pwm_data {
 256        u8 pwm;
 257        u8 enable;
 258        u8 auto_pwm_min;
 259};
 260
 261struct adm1026_data {
 262        struct device *hwmon_dev;
 263
 264        struct mutex update_lock;
 265        int valid;              /* !=0 if following fields are valid */
 266        unsigned long last_reading;     /* In jiffies */
 267        unsigned long last_config;      /* In jiffies */
 268
 269        u8 in[17];              /* Register value */
 270        u8 in_max[17];          /* Register value */
 271        u8 in_min[17];          /* Register value */
 272        s8 temp[3];             /* Register value */
 273        s8 temp_min[3];         /* Register value */
 274        s8 temp_max[3];         /* Register value */
 275        s8 temp_tmin[3];        /* Register value */
 276        s8 temp_crit[3];        /* Register value */
 277        s8 temp_offset[3];      /* Register value */
 278        u8 fan[8];              /* Register value */
 279        u8 fan_min[8];          /* Register value */
 280        u8 fan_div[8];          /* Decoded value */
 281        struct pwm_data pwm1;   /* Pwm control values */
 282        u8 vrm;                 /* VRM version */
 283        u8 analog_out;          /* Register value (DAC) */
 284        long alarms;            /* Register encoding, combined */
 285        long alarm_mask;        /* Register encoding, combined */
 286        long gpio;              /* Register encoding, combined */
 287        long gpio_mask;         /* Register encoding, combined */
 288        u8 gpio_config[17];     /* Decoded value */
 289        u8 config1;             /* Register value */
 290        u8 config2;             /* Register value */
 291        u8 config3;             /* Register value */
 292};
 293
 294static int adm1026_probe(struct i2c_client *client,
 295                         const struct i2c_device_id *id);
 296static int adm1026_detect(struct i2c_client *client, int kind,
 297                          struct i2c_board_info *info);
 298static int adm1026_remove(struct i2c_client *client);
 299static int adm1026_read_value(struct i2c_client *client, u8 reg);
 300static int adm1026_write_value(struct i2c_client *client, u8 reg, int value);
 301static void adm1026_print_gpio(struct i2c_client *client);
 302static void adm1026_fixup_gpio(struct i2c_client *client);
 303static struct adm1026_data *adm1026_update_device(struct device *dev);
 304static void adm1026_init_client(struct i2c_client *client);
 305
 306
 307static const struct i2c_device_id adm1026_id[] = {
 308        { "adm1026", adm1026 },
 309        { }
 310};
 311MODULE_DEVICE_TABLE(i2c, adm1026_id);
 312
 313static struct i2c_driver adm1026_driver = {
 314        .class          = I2C_CLASS_HWMON,
 315        .driver = {
 316                .name   = "adm1026",
 317        },
 318        .probe          = adm1026_probe,
 319        .remove         = adm1026_remove,
 320        .id_table       = adm1026_id,
 321        .detect         = adm1026_detect,
 322        .address_data   = &addr_data,
 323};
 324
 325static int adm1026_read_value(struct i2c_client *client, u8 reg)
 326{
 327        int res;
 328
 329        if (reg < 0x80) {
 330                /* "RAM" locations */
 331                res = i2c_smbus_read_byte_data(client, reg) & 0xff;
 332        } else {
 333                /* EEPROM, do nothing */
 334                res = 0;
 335        }
 336        return res;
 337}
 338
 339static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
 340{
 341        int res;
 342
 343        if (reg < 0x80) {
 344                /* "RAM" locations */
 345                res = i2c_smbus_write_byte_data(client, reg, value);
 346        } else {
 347                /* EEPROM, do nothing */
 348                res = 0;
 349        }
 350        return res;
 351}
 352
 353static void adm1026_init_client(struct i2c_client *client)
 354{
 355        int value, i;
 356        struct adm1026_data *data = i2c_get_clientdata(client);
 357
 358        dev_dbg(&client->dev, "Initializing device\n");
 359        /* Read chip config */
 360        data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
 361        data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
 362        data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
 363
 364        /* Inform user of chip config */
 365        dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
 366                data->config1);
 367        if ((data->config1 & CFG1_MONITOR) == 0) {
 368                dev_dbg(&client->dev, "Monitoring not currently "
 369                        "enabled.\n");
 370        }
 371        if (data->config1 & CFG1_INT_ENABLE) {
 372                dev_dbg(&client->dev, "SMBALERT interrupts are "
 373                        "enabled.\n");
 374        }
 375        if (data->config1 & CFG1_AIN8_9) {
 376                dev_dbg(&client->dev, "in8 and in9 enabled. "
 377                        "temp3 disabled.\n");
 378        } else {
 379                dev_dbg(&client->dev, "temp3 enabled.  in8 and "
 380                        "in9 disabled.\n");
 381        }
 382        if (data->config1 & CFG1_THERM_HOT) {
 383                dev_dbg(&client->dev, "Automatic THERM, PWM, "
 384                        "and temp limits enabled.\n");
 385        }
 386
 387        if (data->config3 & CFG3_GPIO16_ENABLE) {
 388                dev_dbg(&client->dev, "GPIO16 enabled.  THERM "
 389                        "pin disabled.\n");
 390        } else {
 391                dev_dbg(&client->dev, "THERM pin enabled.  "
 392                        "GPIO16 disabled.\n");
 393        }
 394        if (data->config3 & CFG3_VREF_250) {
 395                dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
 396        } else {
 397                dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
 398        }
 399        /* Read and pick apart the existing GPIO configuration */
 400        value = 0;
 401        for (i = 0;i <= 15;++i) {
 402                if ((i & 0x03) == 0) {
 403                        value = adm1026_read_value(client,
 404                                        ADM1026_REG_GPIO_CFG_0_3 + i/4);
 405                }
 406                data->gpio_config[i] = value & 0x03;
 407                value >>= 2;
 408        }
 409        data->gpio_config[16] = (data->config3 >> 6) & 0x03;
 410
 411        /* ... and then print it */
 412        adm1026_print_gpio(client);
 413
 414        /* If the user asks us to reprogram the GPIO config, then
 415         * do it now.
 416         */
 417        if (gpio_input[0] != -1 || gpio_output[0] != -1
 418                || gpio_inverted[0] != -1 || gpio_normal[0] != -1
 419                || gpio_fan[0] != -1) {
 420                adm1026_fixup_gpio(client);
 421        }
 422
 423        /* WE INTENTIONALLY make no changes to the limits,
 424         *   offsets, pwms, fans and zones.  If they were
 425         *   configured, we don't want to mess with them.
 426         *   If they weren't, the default is 100% PWM, no
 427         *   control and will suffice until 'sensors -s'
 428         *   can be run by the user.  We DO set the default
 429         *   value for pwm1.auto_pwm_min to its maximum
 430         *   so that enabling automatic pwm fan control
 431         *   without first setting a value for pwm1.auto_pwm_min
 432         *   will not result in potentially dangerous fan speed decrease.
 433         */
 434        data->pwm1.auto_pwm_min=255;
 435        /* Start monitoring */
 436        value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
 437        /* Set MONITOR, clear interrupt acknowledge and s/w reset */
 438        value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
 439        dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
 440        data->config1 = value;
 441        adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
 442
 443        /* initialize fan_div[] to hardware defaults */
 444        value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
 445                (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
 446        for (i = 0;i <= 7;++i) {
 447                data->fan_div[i] = DIV_FROM_REG(value & 0x03);
 448                value >>= 2;
 449        }
 450}
 451
 452static void adm1026_print_gpio(struct i2c_client *client)
 453{
 454        struct adm1026_data *data = i2c_get_clientdata(client);
 455        int i;
 456
 457        dev_dbg(&client->dev, "GPIO config is:\n");
 458        for (i = 0;i <= 7;++i) {
 459                if (data->config2 & (1 << i)) {
 460                        dev_dbg(&client->dev, "\t%sGP%s%d\n",
 461                                data->gpio_config[i] & 0x02 ? "" : "!",
 462                                data->gpio_config[i] & 0x01 ? "OUT" : "IN",
 463                                i);
 464                } else {
 465                        dev_dbg(&client->dev, "\tFAN%d\n", i);
 466                }
 467        }
 468        for (i = 8;i <= 15;++i) {
 469                dev_dbg(&client->dev, "\t%sGP%s%d\n",
 470                        data->gpio_config[i] & 0x02 ? "" : "!",
 471                        data->gpio_config[i] & 0x01 ? "OUT" : "IN",
 472                        i);
 473        }
 474        if (data->config3 & CFG3_GPIO16_ENABLE) {
 475                dev_dbg(&client->dev, "\t%sGP%s16\n",
 476                        data->gpio_config[16] & 0x02 ? "" : "!",
 477                        data->gpio_config[16] & 0x01 ? "OUT" : "IN");
 478        } else {
 479                /* GPIO16 is THERM */
 480                dev_dbg(&client->dev, "\tTHERM\n");
 481        }
 482}
 483
 484static void adm1026_fixup_gpio(struct i2c_client *client)
 485{
 486        struct adm1026_data *data = i2c_get_clientdata(client);
 487        int i;
 488        int value;
 489
 490        /* Make the changes requested. */
 491        /* We may need to unlock/stop monitoring or soft-reset the
 492         *    chip before we can make changes.  This hasn't been
 493         *    tested much.  FIXME
 494         */
 495
 496        /* Make outputs */
 497        for (i = 0;i <= 16;++i) {
 498                if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
 499                        data->gpio_config[gpio_output[i]] |= 0x01;
 500                }
 501                /* if GPIO0-7 is output, it isn't a FAN tach */
 502                if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
 503                        data->config2 |= 1 << gpio_output[i];
 504                }
 505        }
 506
 507        /* Input overrides output */
 508        for (i = 0;i <= 16;++i) {
 509                if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
 510                        data->gpio_config[gpio_input[i]] &= ~ 0x01;
 511                }
 512                /* if GPIO0-7 is input, it isn't a FAN tach */
 513                if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
 514                        data->config2 |= 1 << gpio_input[i];
 515                }
 516        }
 517
 518        /* Inverted */
 519        for (i = 0;i <= 16;++i) {
 520                if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
 521                        data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
 522                }
 523        }
 524
 525        /* Normal overrides inverted */
 526        for (i = 0;i <= 16;++i) {
 527                if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
 528                        data->gpio_config[gpio_normal[i]] |= 0x02;
 529                }
 530        }
 531
 532        /* Fan overrides input and output */
 533        for (i = 0;i <= 7;++i) {
 534                if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
 535                        data->config2 &= ~(1 << gpio_fan[i]);
 536                }
 537        }
 538
 539        /* Write new configs to registers */
 540        adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
 541        data->config3 = (data->config3 & 0x3f)
 542                        | ((data->gpio_config[16] & 0x03) << 6);
 543        adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
 544        for (i = 15, value = 0;i >= 0;--i) {
 545                value <<= 2;
 546                value |= data->gpio_config[i] & 0x03;
 547                if ((i & 0x03) == 0) {
 548                        adm1026_write_value(client,
 549                                        ADM1026_REG_GPIO_CFG_0_3 + i/4,
 550                                        value);
 551                        value = 0;
 552                }
 553        }
 554
 555        /* Print the new config */
 556        adm1026_print_gpio(client);
 557}
 558
 559
 560static struct adm1026_data *adm1026_update_device(struct device *dev)
 561{
 562        struct i2c_client *client = to_i2c_client(dev);
 563        struct adm1026_data *data = i2c_get_clientdata(client);
 564        int i;
 565        long value, alarms, gpio;
 566
 567        mutex_lock(&data->update_lock);
 568        if (!data->valid
 569            || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
 570                /* Things that change quickly */
 571                dev_dbg(&client->dev, "Reading sensor values\n");
 572                for (i = 0;i <= 16;++i) {
 573                        data->in[i] =
 574                            adm1026_read_value(client, ADM1026_REG_IN[i]);
 575                }
 576
 577                for (i = 0;i <= 7;++i) {
 578                        data->fan[i] =
 579                            adm1026_read_value(client, ADM1026_REG_FAN(i));
 580                }
 581
 582                for (i = 0;i <= 2;++i) {
 583                        /* NOTE: temp[] is s8 and we assume 2's complement
 584                         *   "conversion" in the assignment */
 585                        data->temp[i] =
 586                            adm1026_read_value(client, ADM1026_REG_TEMP[i]);
 587                }
 588
 589                data->pwm1.pwm = adm1026_read_value(client,
 590                        ADM1026_REG_PWM);
 591                data->analog_out = adm1026_read_value(client,
 592                        ADM1026_REG_DAC);
 593                /* GPIO16 is MSbit of alarms, move it to gpio */
 594                alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
 595                gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
 596                alarms &= 0x7f;
 597                alarms <<= 8;
 598                alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
 599                alarms <<= 8;
 600                alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
 601                alarms <<= 8;
 602                alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
 603                data->alarms = alarms;
 604
 605                /* Read the GPIO values */
 606                gpio |= adm1026_read_value(client,
 607                        ADM1026_REG_GPIO_STATUS_8_15);
 608                gpio <<= 8;
 609                gpio |= adm1026_read_value(client,
 610                        ADM1026_REG_GPIO_STATUS_0_7);
 611                data->gpio = gpio;
 612
 613                data->last_reading = jiffies;
 614        }; /* last_reading */
 615
 616        if (!data->valid ||
 617            time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
 618                /* Things that don't change often */
 619                dev_dbg(&client->dev, "Reading config values\n");
 620                for (i = 0;i <= 16;++i) {
 621                        data->in_min[i] = adm1026_read_value(client,
 622                                ADM1026_REG_IN_MIN[i]);
 623                        data->in_max[i] = adm1026_read_value(client,
 624                                ADM1026_REG_IN_MAX[i]);
 625                }
 626
 627                value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
 628                        | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
 629                        << 8);
 630                for (i = 0;i <= 7;++i) {
 631                        data->fan_min[i] = adm1026_read_value(client,
 632                                ADM1026_REG_FAN_MIN(i));
 633                        data->fan_div[i] = DIV_FROM_REG(value & 0x03);
 634                        value >>= 2;
 635                }
 636
 637                for (i = 0; i <= 2; ++i) {
 638                        /* NOTE: temp_xxx[] are s8 and we assume 2's
 639                         *    complement "conversion" in the assignment
 640                         */
 641                        data->temp_min[i] = adm1026_read_value(client,
 642                                ADM1026_REG_TEMP_MIN[i]);
 643                        data->temp_max[i] = adm1026_read_value(client,
 644                                ADM1026_REG_TEMP_MAX[i]);
 645                        data->temp_tmin[i] = adm1026_read_value(client,
 646                                ADM1026_REG_TEMP_TMIN[i]);
 647                        data->temp_crit[i] = adm1026_read_value(client,
 648                                ADM1026_REG_TEMP_THERM[i]);
 649                        data->temp_offset[i] = adm1026_read_value(client,
 650                                ADM1026_REG_TEMP_OFFSET[i]);
 651                }
 652
 653                /* Read the STATUS/alarm masks */
 654                alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
 655                gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
 656                alarms = (alarms & 0x7f) << 8;
 657                alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
 658                alarms <<= 8;
 659                alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
 660                alarms <<= 8;
 661                alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
 662                data->alarm_mask = alarms;
 663
 664                /* Read the GPIO values */
 665                gpio |= adm1026_read_value(client,
 666                        ADM1026_REG_GPIO_MASK_8_15);
 667                gpio <<= 8;
 668                gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
 669                data->gpio_mask = gpio;
 670
 671                /* Read various values from CONFIG1 */
 672                data->config1 = adm1026_read_value(client,
 673                        ADM1026_REG_CONFIG1);
 674                if (data->config1 & CFG1_PWM_AFC) {
 675                        data->pwm1.enable = 2;
 676                        data->pwm1.auto_pwm_min =
 677                                PWM_MIN_FROM_REG(data->pwm1.pwm);
 678                }
 679                /* Read the GPIO config */
 680                data->config2 = adm1026_read_value(client,
 681                        ADM1026_REG_CONFIG2);
 682                data->config3 = adm1026_read_value(client,
 683                        ADM1026_REG_CONFIG3);
 684                data->gpio_config[16] = (data->config3 >> 6) & 0x03;
 685
 686                value = 0;
 687                for (i = 0;i <= 15;++i) {
 688                        if ((i & 0x03) == 0) {
 689                                value = adm1026_read_value(client,
 690                                            ADM1026_REG_GPIO_CFG_0_3 + i/4);
 691                        }
 692                        data->gpio_config[i] = value & 0x03;
 693                        value >>= 2;
 694                }
 695
 696                data->last_config = jiffies;
 697        }; /* last_config */
 698
 699        data->valid = 1;
 700        mutex_unlock(&data->update_lock);
 701        return data;
 702}
 703
 704static ssize_t show_in(struct device *dev, struct device_attribute *attr,
 705                char *buf)
 706{
 707        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 708        int nr = sensor_attr->index;
 709        struct adm1026_data *data = adm1026_update_device(dev);
 710        return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
 711}
 712static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
 713                char *buf)
 714{
 715        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 716        int nr = sensor_attr->index;
 717        struct adm1026_data *data = adm1026_update_device(dev);
 718        return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
 719}
 720static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
 721                const char *buf, size_t count)
 722{
 723        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 724        int nr = sensor_attr->index;
 725        struct i2c_client *client = to_i2c_client(dev);
 726        struct adm1026_data *data = i2c_get_clientdata(client);
 727        int val = simple_strtol(buf, NULL, 10);
 728
 729        mutex_lock(&data->update_lock);
 730        data->in_min[nr] = INS_TO_REG(nr, val);
 731        adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
 732        mutex_unlock(&data->update_lock);
 733        return count;
 734}
 735static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
 736                char *buf)
 737{
 738        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 739        int nr = sensor_attr->index;
 740        struct adm1026_data *data = adm1026_update_device(dev);
 741        return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
 742}
 743static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
 744                const char *buf, size_t count)
 745{
 746        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 747        int nr = sensor_attr->index;
 748        struct i2c_client *client = to_i2c_client(dev);
 749        struct adm1026_data *data = i2c_get_clientdata(client);
 750        int val = simple_strtol(buf, NULL, 10);
 751
 752        mutex_lock(&data->update_lock);
 753        data->in_max[nr] = INS_TO_REG(nr, val);
 754        adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
 755        mutex_unlock(&data->update_lock);
 756        return count;
 757}
 758
 759#define in_reg(offset)                                          \
 760static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
 761                NULL, offset);                                  \
 762static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,  \
 763                show_in_min, set_in_min, offset);               \
 764static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,  \
 765                show_in_max, set_in_max, offset);
 766
 767
 768in_reg(0);
 769in_reg(1);
 770in_reg(2);
 771in_reg(3);
 772in_reg(4);
 773in_reg(5);
 774in_reg(6);
 775in_reg(7);
 776in_reg(8);
 777in_reg(9);
 778in_reg(10);
 779in_reg(11);
 780in_reg(12);
 781in_reg(13);
 782in_reg(14);
 783in_reg(15);
 784
 785static ssize_t show_in16(struct device *dev, struct device_attribute *attr, char *buf)
 786{
 787        struct adm1026_data *data = adm1026_update_device(dev);
 788        return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
 789                NEG12_OFFSET);
 790}
 791static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, char *buf)
 792{
 793        struct adm1026_data *data = adm1026_update_device(dev);
 794        return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
 795                - NEG12_OFFSET);
 796}
 797static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
 798{
 799        struct i2c_client *client = to_i2c_client(dev);
 800        struct adm1026_data *data = i2c_get_clientdata(client);
 801        int val = simple_strtol(buf, NULL, 10);
 802
 803        mutex_lock(&data->update_lock);
 804        data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
 805        adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
 806        mutex_unlock(&data->update_lock);
 807        return count;
 808}
 809static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, char *buf)
 810{
 811        struct adm1026_data *data = adm1026_update_device(dev);
 812        return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
 813                        - NEG12_OFFSET);
 814}
 815static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
 816{
 817        struct i2c_client *client = to_i2c_client(dev);
 818        struct adm1026_data *data = i2c_get_clientdata(client);
 819        int val = simple_strtol(buf, NULL, 10);
 820
 821        mutex_lock(&data->update_lock);
 822        data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
 823        adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
 824        mutex_unlock(&data->update_lock);
 825        return count;
 826}
 827
 828static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
 829static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min, 16);
 830static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max, 16);
 831
 832
 833
 834
 835/* Now add fan read/write functions */
 836
 837static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 838                char *buf)
 839{
 840        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 841        int nr = sensor_attr->index;
 842        struct adm1026_data *data = adm1026_update_device(dev);
 843        return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
 844                data->fan_div[nr]));
 845}
 846static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
 847                char *buf)
 848{
 849        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 850        int nr = sensor_attr->index;
 851        struct adm1026_data *data = adm1026_update_device(dev);
 852        return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
 853                data->fan_div[nr]));
 854}
 855static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
 856                const char *buf, size_t count)
 857{
 858        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 859        int nr = sensor_attr->index;
 860        struct i2c_client *client = to_i2c_client(dev);
 861        struct adm1026_data *data = i2c_get_clientdata(client);
 862        int val = simple_strtol(buf, NULL, 10);
 863
 864        mutex_lock(&data->update_lock);
 865        data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
 866        adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
 867                data->fan_min[nr]);
 868        mutex_unlock(&data->update_lock);
 869        return count;
 870}
 871
 872#define fan_offset(offset)                                              \
 873static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
 874                offset - 1);                                            \
 875static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,         \
 876                show_fan_min, set_fan_min, offset - 1);
 877
 878fan_offset(1);
 879fan_offset(2);
 880fan_offset(3);
 881fan_offset(4);
 882fan_offset(5);
 883fan_offset(6);
 884fan_offset(7);
 885fan_offset(8);
 886
 887/* Adjust fan_min to account for new fan divisor */
 888static void fixup_fan_min(struct device *dev, int fan, int old_div)
 889{
 890        struct i2c_client *client = to_i2c_client(dev);
 891        struct adm1026_data *data = i2c_get_clientdata(client);
 892        int new_min;
 893        int new_div = data->fan_div[fan];
 894
 895        /* 0 and 0xff are special.  Don't adjust them */
 896        if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
 897                return;
 898        }
 899
 900        new_min = data->fan_min[fan] * old_div / new_div;
 901        new_min = SENSORS_LIMIT(new_min, 1, 254);
 902        data->fan_min[fan] = new_min;
 903        adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
 904}
 905
 906/* Now add fan_div read/write functions */
 907static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
 908                char *buf)
 909{
 910        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 911        int nr = sensor_attr->index;
 912        struct adm1026_data *data = adm1026_update_device(dev);
 913        return sprintf(buf, "%d\n", data->fan_div[nr]);
 914}
 915static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
 916                const char *buf, size_t count)
 917{
 918        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 919        int nr = sensor_attr->index;
 920        struct i2c_client *client = to_i2c_client(dev);
 921        struct adm1026_data *data = i2c_get_clientdata(client);
 922        int val, orig_div, new_div, shift;
 923
 924        val = simple_strtol(buf, NULL, 10);
 925        new_div = DIV_TO_REG(val);
 926        if (new_div == 0) {
 927                return -EINVAL;
 928        }
 929        mutex_lock(&data->update_lock);
 930        orig_div = data->fan_div[nr];
 931        data->fan_div[nr] = DIV_FROM_REG(new_div);
 932
 933        if (nr < 4) { /* 0 <= nr < 4 */
 934                shift = 2 * nr;
 935                adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
 936                        ((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
 937                        (new_div << shift)));
 938        } else { /* 3 < nr < 8 */
 939                shift = 2 * (nr - 4);
 940                adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
 941                        ((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
 942                        (new_div << shift)));
 943        }
 944
 945        if (data->fan_div[nr] != orig_div) {
 946                fixup_fan_min(dev, nr, orig_div);
 947        }
 948        mutex_unlock(&data->update_lock);
 949        return count;
 950}
 951
 952#define fan_offset_div(offset)                                          \
 953static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,         \
 954                show_fan_div, set_fan_div, offset - 1);
 955
 956fan_offset_div(1);
 957fan_offset_div(2);
 958fan_offset_div(3);
 959fan_offset_div(4);
 960fan_offset_div(5);
 961fan_offset_div(6);
 962fan_offset_div(7);
 963fan_offset_div(8);
 964
 965/* Temps */
 966static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
 967                char *buf)
 968{
 969        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 970        int nr = sensor_attr->index;
 971        struct adm1026_data *data = adm1026_update_device(dev);
 972        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
 973}
 974static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
 975                char *buf)
 976{
 977        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 978        int nr = sensor_attr->index;
 979        struct adm1026_data *data = adm1026_update_device(dev);
 980        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
 981}
 982static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
 983                const char *buf, size_t count)
 984{
 985        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
 986        int nr = sensor_attr->index;
 987        struct i2c_client *client = to_i2c_client(dev);
 988        struct adm1026_data *data = i2c_get_clientdata(client);
 989        int val = simple_strtol(buf, NULL, 10);
 990
 991        mutex_lock(&data->update_lock);
 992        data->temp_min[nr] = TEMP_TO_REG(val);
 993        adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
 994                data->temp_min[nr]);
 995        mutex_unlock(&data->update_lock);
 996        return count;
 997}
 998static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
 999                char *buf)
1000{
1001        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1002        int nr = sensor_attr->index;
1003        struct adm1026_data *data = adm1026_update_device(dev);
1004        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
1005}
1006static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
1007                const char *buf, size_t count)
1008{
1009        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1010        int nr = sensor_attr->index;
1011        struct i2c_client *client = to_i2c_client(dev);
1012        struct adm1026_data *data = i2c_get_clientdata(client);
1013        int val = simple_strtol(buf, NULL, 10);
1014
1015        mutex_lock(&data->update_lock);
1016        data->temp_max[nr] = TEMP_TO_REG(val);
1017        adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1018                data->temp_max[nr]);
1019        mutex_unlock(&data->update_lock);
1020        return count;
1021}
1022
1023#define temp_reg(offset)                                                \
1024static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp,     \
1025                NULL, offset - 1);                                      \
1026static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,        \
1027                show_temp_min, set_temp_min, offset - 1);               \
1028static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,        \
1029                show_temp_max, set_temp_max, offset - 1);
1030
1031
1032temp_reg(1);
1033temp_reg(2);
1034temp_reg(3);
1035
1036static ssize_t show_temp_offset(struct device *dev,
1037                struct device_attribute *attr, char *buf)
1038{
1039        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1040        int nr = sensor_attr->index;
1041        struct adm1026_data *data = adm1026_update_device(dev);
1042        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
1043}
1044static ssize_t set_temp_offset(struct device *dev,
1045                struct device_attribute *attr, const char *buf,
1046                size_t count)
1047{
1048        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1049        int nr = sensor_attr->index;
1050        struct i2c_client *client = to_i2c_client(dev);
1051        struct adm1026_data *data = i2c_get_clientdata(client);
1052        int val = simple_strtol(buf, NULL, 10);
1053
1054        mutex_lock(&data->update_lock);
1055        data->temp_offset[nr] = TEMP_TO_REG(val);
1056        adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1057                data->temp_offset[nr]);
1058        mutex_unlock(&data->update_lock);
1059        return count;
1060}
1061
1062#define temp_offset_reg(offset)                                                 \
1063static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR,             \
1064                show_temp_offset, set_temp_offset, offset - 1);
1065
1066temp_offset_reg(1);
1067temp_offset_reg(2);
1068temp_offset_reg(3);
1069
1070static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
1071                struct device_attribute *attr, char *buf)
1072{
1073        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1074        int nr = sensor_attr->index;
1075        struct adm1026_data *data = adm1026_update_device(dev);
1076        return sprintf(buf, "%d\n", TEMP_FROM_REG(
1077                ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1078}
1079static ssize_t show_temp_auto_point2_temp(struct device *dev,
1080                struct device_attribute *attr, char *buf)
1081{
1082        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1083        int nr = sensor_attr->index;
1084        struct adm1026_data *data = adm1026_update_device(dev);
1085        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
1086                ADM1026_FAN_CONTROL_TEMP_RANGE));
1087}
1088static ssize_t show_temp_auto_point1_temp(struct device *dev,
1089                struct device_attribute *attr, char *buf)
1090{
1091        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1092        int nr = sensor_attr->index;
1093        struct adm1026_data *data = adm1026_update_device(dev);
1094        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
1095}
1096static ssize_t set_temp_auto_point1_temp(struct device *dev,
1097                struct device_attribute *attr, const char *buf, size_t count)
1098{
1099        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1100        int nr = sensor_attr->index;
1101        struct i2c_client *client = to_i2c_client(dev);
1102        struct adm1026_data *data = i2c_get_clientdata(client);
1103        int val = simple_strtol(buf, NULL, 10);
1104
1105        mutex_lock(&data->update_lock);
1106        data->temp_tmin[nr] = TEMP_TO_REG(val);
1107        adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1108                data->temp_tmin[nr]);
1109        mutex_unlock(&data->update_lock);
1110        return count;
1111}
1112
1113#define temp_auto_point(offset)                                         \
1114static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp,              \
1115                S_IRUGO | S_IWUSR, show_temp_auto_point1_temp,          \
1116                set_temp_auto_point1_temp, offset - 1);                 \
1117static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
1118                show_temp_auto_point1_temp_hyst, NULL, offset - 1);     \
1119static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO,     \
1120                show_temp_auto_point2_temp, NULL, offset - 1);
1121
1122temp_auto_point(1);
1123temp_auto_point(2);
1124temp_auto_point(3);
1125
1126static ssize_t show_temp_crit_enable(struct device *dev,
1127                struct device_attribute *attr, char *buf)
1128{
1129        struct adm1026_data *data = adm1026_update_device(dev);
1130        return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
1131}
1132static ssize_t set_temp_crit_enable(struct device *dev,
1133                struct device_attribute *attr, const char *buf, size_t count)
1134{
1135        struct i2c_client *client = to_i2c_client(dev);
1136        struct adm1026_data *data = i2c_get_clientdata(client);
1137        int val = simple_strtol(buf, NULL, 10);
1138
1139        if ((val == 1) || (val==0)) {
1140                mutex_lock(&data->update_lock);
1141                data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
1142                adm1026_write_value(client, ADM1026_REG_CONFIG1,
1143                        data->config1);
1144                mutex_unlock(&data->update_lock);
1145        }
1146        return count;
1147}
1148
1149#define temp_crit_enable(offset)                                \
1150static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
1151        show_temp_crit_enable, set_temp_crit_enable);
1152
1153temp_crit_enable(1);
1154temp_crit_enable(2);
1155temp_crit_enable(3);
1156
1157static ssize_t show_temp_crit(struct device *dev,
1158                struct device_attribute *attr, char *buf)
1159{
1160        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1161        int nr = sensor_attr->index;
1162        struct adm1026_data *data = adm1026_update_device(dev);
1163        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
1164}
1165static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
1166                const char *buf, size_t count)
1167{
1168        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1169        int nr = sensor_attr->index;
1170        struct i2c_client *client = to_i2c_client(dev);
1171        struct adm1026_data *data = i2c_get_clientdata(client);
1172        int val = simple_strtol(buf, NULL, 10);
1173
1174        mutex_lock(&data->update_lock);
1175        data->temp_crit[nr] = TEMP_TO_REG(val);
1176        adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1177                data->temp_crit[nr]);
1178        mutex_unlock(&data->update_lock);
1179        return count;
1180}
1181
1182#define temp_crit_reg(offset)                                           \
1183static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,       \
1184                show_temp_crit, set_temp_crit, offset - 1);
1185
1186temp_crit_reg(1);
1187temp_crit_reg(2);
1188temp_crit_reg(3);
1189
1190static ssize_t show_analog_out_reg(struct device *dev, struct device_attribute *attr, char *buf)
1191{
1192        struct adm1026_data *data = adm1026_update_device(dev);
1193        return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
1194}
1195static ssize_t set_analog_out_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1196                size_t count)
1197{
1198        struct i2c_client *client = to_i2c_client(dev);
1199        struct adm1026_data *data = i2c_get_clientdata(client);
1200        int val = simple_strtol(buf, NULL, 10);
1201
1202        mutex_lock(&data->update_lock);
1203        data->analog_out = DAC_TO_REG(val);
1204        adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
1205        mutex_unlock(&data->update_lock);
1206        return count;
1207}
1208
1209static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
1210        set_analog_out_reg);
1211
1212static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
1213{
1214        struct adm1026_data *data = adm1026_update_device(dev);
1215        int vid = (data->gpio >> 11) & 0x1f;
1216
1217        dev_dbg(dev, "Setting VID from GPIO11-15.\n");
1218        return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm));
1219}
1220static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1221
1222static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1223{
1224        struct adm1026_data *data = dev_get_drvdata(dev);
1225        return sprintf(buf, "%d\n", data->vrm);
1226}
1227static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1228                size_t count)
1229{
1230        struct adm1026_data *data = dev_get_drvdata(dev);
1231
1232        data->vrm = simple_strtol(buf, NULL, 10);
1233        return count;
1234}
1235
1236static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1237
1238static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
1239{
1240        struct adm1026_data *data = adm1026_update_device(dev);
1241        return sprintf(buf, "%ld\n", data->alarms);
1242}
1243
1244static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1245
1246static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1247                          char *buf)
1248{
1249        struct adm1026_data *data = adm1026_update_device(dev);
1250        int bitnr = to_sensor_dev_attr(attr)->index;
1251        return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
1252}
1253
1254static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
1255static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
1256static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
1257static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
1258static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
1259static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
1260static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
1261static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
1262static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
1263static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1264static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1265static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1266static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1267static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1268static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1269static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1270static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1271static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1272static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1273static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1274static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
1275static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
1276static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
1277static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
1278static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
1279static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
1280static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1281static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
1282
1283static ssize_t show_alarm_mask(struct device *dev, struct device_attribute *attr, char *buf)
1284{
1285        struct adm1026_data *data = adm1026_update_device(dev);
1286        return sprintf(buf, "%ld\n", data->alarm_mask);
1287}
1288static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, const char *buf,
1289                size_t count)
1290{
1291        struct i2c_client *client = to_i2c_client(dev);
1292        struct adm1026_data *data = i2c_get_clientdata(client);
1293        int val = simple_strtol(buf, NULL, 10);
1294        unsigned long mask;
1295
1296        mutex_lock(&data->update_lock);
1297        data->alarm_mask = val & 0x7fffffff;
1298        mask = data->alarm_mask
1299                | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1300        adm1026_write_value(client, ADM1026_REG_MASK1,
1301                mask & 0xff);
1302        mask >>= 8;
1303        adm1026_write_value(client, ADM1026_REG_MASK2,
1304                mask & 0xff);
1305        mask >>= 8;
1306        adm1026_write_value(client, ADM1026_REG_MASK3,
1307                mask & 0xff);
1308        mask >>= 8;
1309        adm1026_write_value(client, ADM1026_REG_MASK4,
1310                mask & 0xff);
1311        mutex_unlock(&data->update_lock);
1312        return count;
1313}
1314
1315static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1316        set_alarm_mask);
1317
1318
1319static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, char *buf)
1320{
1321        struct adm1026_data *data = adm1026_update_device(dev);
1322        return sprintf(buf, "%ld\n", data->gpio);
1323}
1324static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, const char *buf,
1325                size_t count)
1326{
1327        struct i2c_client *client = to_i2c_client(dev);
1328        struct adm1026_data *data = i2c_get_clientdata(client);
1329        int val = simple_strtol(buf, NULL, 10);
1330        long gpio;
1331
1332        mutex_lock(&data->update_lock);
1333        data->gpio = val & 0x1ffff;
1334        gpio = data->gpio;
1335        adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
1336        gpio >>= 8;
1337        adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
1338        gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
1339        adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
1340        mutex_unlock(&data->update_lock);
1341        return count;
1342}
1343
1344static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1345
1346
1347static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, char *buf)
1348{
1349        struct adm1026_data *data = adm1026_update_device(dev);
1350        return sprintf(buf, "%ld\n", data->gpio_mask);
1351}
1352static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, const char *buf,
1353                size_t count)
1354{
1355        struct i2c_client *client = to_i2c_client(dev);
1356        struct adm1026_data *data = i2c_get_clientdata(client);
1357        int val = simple_strtol(buf, NULL, 10);
1358        long mask;
1359
1360        mutex_lock(&data->update_lock);
1361        data->gpio_mask = val & 0x1ffff;
1362        mask = data->gpio_mask;
1363        adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
1364        mask >>= 8;
1365        adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
1366        mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
1367        adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
1368        mutex_unlock(&data->update_lock);
1369        return count;
1370}
1371
1372static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1373
1374static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1375{
1376        struct adm1026_data *data = adm1026_update_device(dev);
1377        return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
1378}
1379static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1380                size_t count)
1381{
1382        struct i2c_client *client = to_i2c_client(dev);
1383        struct adm1026_data *data = i2c_get_clientdata(client);
1384
1385        if (data->pwm1.enable == 1) {
1386                int val = simple_strtol(buf, NULL, 10);
1387
1388                mutex_lock(&data->update_lock);
1389                data->pwm1.pwm = PWM_TO_REG(val);
1390                adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1391                mutex_unlock(&data->update_lock);
1392        }
1393        return count;
1394}
1395static ssize_t show_auto_pwm_min(struct device *dev, struct device_attribute *attr, char *buf)
1396{
1397        struct adm1026_data *data = adm1026_update_device(dev);
1398        return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
1399}
1400static ssize_t set_auto_pwm_min(struct device *dev, struct device_attribute *attr, const char *buf,
1401                size_t count)
1402{
1403        struct i2c_client *client = to_i2c_client(dev);
1404        struct adm1026_data *data = i2c_get_clientdata(client);
1405        int val = simple_strtol(buf, NULL, 10);
1406
1407        mutex_lock(&data->update_lock);
1408        data->pwm1.auto_pwm_min = SENSORS_LIMIT(val, 0, 255);
1409        if (data->pwm1.enable == 2) { /* apply immediately */
1410                data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1411                        PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1412                adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1413        }
1414        mutex_unlock(&data->update_lock);
1415        return count;
1416}
1417static ssize_t show_auto_pwm_max(struct device *dev, struct device_attribute *attr, char *buf)
1418{
1419        return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
1420}
1421static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1422{
1423        struct adm1026_data *data = adm1026_update_device(dev);
1424        return sprintf(buf, "%d\n", data->pwm1.enable);
1425}
1426static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf,
1427                size_t count)
1428{
1429        struct i2c_client *client = to_i2c_client(dev);
1430        struct adm1026_data *data = i2c_get_clientdata(client);
1431        int val = simple_strtol(buf, NULL, 10);
1432        int old_enable;
1433
1434        if ((val >= 0) && (val < 3)) {
1435                mutex_lock(&data->update_lock);
1436                old_enable = data->pwm1.enable;
1437                data->pwm1.enable = val;
1438                data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1439                                | ((val == 2) ? CFG1_PWM_AFC : 0);
1440                adm1026_write_value(client, ADM1026_REG_CONFIG1,
1441                        data->config1);
1442                if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
1443                        data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1444                                PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1445                        adm1026_write_value(client, ADM1026_REG_PWM,
1446                                data->pwm1.pwm);
1447                } else if (!((old_enable == 1) && (val == 1))) {
1448                        /* set pwm to safe value */
1449                        data->pwm1.pwm = 255;
1450                        adm1026_write_value(client, ADM1026_REG_PWM,
1451                                data->pwm1.pwm);
1452                }
1453                mutex_unlock(&data->update_lock);
1454        }
1455        return count;
1456}
1457
1458/* enable PWM fan control */
1459static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1460static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1461static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1462static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1463        set_pwm_enable);
1464static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1465        set_pwm_enable);
1466static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1467        set_pwm_enable);
1468static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1469        show_auto_pwm_min, set_auto_pwm_min);
1470static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1471        show_auto_pwm_min, set_auto_pwm_min);
1472static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1473        show_auto_pwm_min, set_auto_pwm_min);
1474
1475static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1476static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1477static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1478
1479static struct attribute *adm1026_attributes[] = {
1480        &sensor_dev_attr_in0_input.dev_attr.attr,
1481        &sensor_dev_attr_in0_max.dev_attr.attr,
1482        &sensor_dev_attr_in0_min.dev_attr.attr,
1483        &sensor_dev_attr_in0_alarm.dev_attr.attr,
1484        &sensor_dev_attr_in1_input.dev_attr.attr,
1485        &sensor_dev_attr_in1_max.dev_attr.attr,
1486        &sensor_dev_attr_in1_min.dev_attr.attr,
1487        &sensor_dev_attr_in1_alarm.dev_attr.attr,
1488        &sensor_dev_attr_in2_input.dev_attr.attr,
1489        &sensor_dev_attr_in2_max.dev_attr.attr,
1490        &sensor_dev_attr_in2_min.dev_attr.attr,
1491        &sensor_dev_attr_in2_alarm.dev_attr.attr,
1492        &sensor_dev_attr_in3_input.dev_attr.attr,
1493        &sensor_dev_attr_in3_max.dev_attr.attr,
1494        &sensor_dev_attr_in3_min.dev_attr.attr,
1495        &sensor_dev_attr_in3_alarm.dev_attr.attr,
1496        &sensor_dev_attr_in4_input.dev_attr.attr,
1497        &sensor_dev_attr_in4_max.dev_attr.attr,
1498        &sensor_dev_attr_in4_min.dev_attr.attr,
1499        &sensor_dev_attr_in4_alarm.dev_attr.attr,
1500        &sensor_dev_attr_in5_input.dev_attr.attr,
1501        &sensor_dev_attr_in5_max.dev_attr.attr,
1502        &sensor_dev_attr_in5_min.dev_attr.attr,
1503        &sensor_dev_attr_in5_alarm.dev_attr.attr,
1504        &sensor_dev_attr_in6_input.dev_attr.attr,
1505        &sensor_dev_attr_in6_max.dev_attr.attr,
1506        &sensor_dev_attr_in6_min.dev_attr.attr,
1507        &sensor_dev_attr_in6_alarm.dev_attr.attr,
1508        &sensor_dev_attr_in7_input.dev_attr.attr,
1509        &sensor_dev_attr_in7_max.dev_attr.attr,
1510        &sensor_dev_attr_in7_min.dev_attr.attr,
1511        &sensor_dev_attr_in7_alarm.dev_attr.attr,
1512        &sensor_dev_attr_in10_input.dev_attr.attr,
1513        &sensor_dev_attr_in10_max.dev_attr.attr,
1514        &sensor_dev_attr_in10_min.dev_attr.attr,
1515        &sensor_dev_attr_in10_alarm.dev_attr.attr,
1516        &sensor_dev_attr_in11_input.dev_attr.attr,
1517        &sensor_dev_attr_in11_max.dev_attr.attr,
1518        &sensor_dev_attr_in11_min.dev_attr.attr,
1519        &sensor_dev_attr_in11_alarm.dev_attr.attr,
1520        &sensor_dev_attr_in12_input.dev_attr.attr,
1521        &sensor_dev_attr_in12_max.dev_attr.attr,
1522        &sensor_dev_attr_in12_min.dev_attr.attr,
1523        &sensor_dev_attr_in12_alarm.dev_attr.attr,
1524        &sensor_dev_attr_in13_input.dev_attr.attr,
1525        &sensor_dev_attr_in13_max.dev_attr.attr,
1526        &sensor_dev_attr_in13_min.dev_attr.attr,
1527        &sensor_dev_attr_in13_alarm.dev_attr.attr,
1528        &sensor_dev_attr_in14_input.dev_attr.attr,
1529        &sensor_dev_attr_in14_max.dev_attr.attr,
1530        &sensor_dev_attr_in14_min.dev_attr.attr,
1531        &sensor_dev_attr_in14_alarm.dev_attr.attr,
1532        &sensor_dev_attr_in15_input.dev_attr.attr,
1533        &sensor_dev_attr_in15_max.dev_attr.attr,
1534        &sensor_dev_attr_in15_min.dev_attr.attr,
1535        &sensor_dev_attr_in15_alarm.dev_attr.attr,
1536        &sensor_dev_attr_in16_input.dev_attr.attr,
1537        &sensor_dev_attr_in16_max.dev_attr.attr,
1538        &sensor_dev_attr_in16_min.dev_attr.attr,
1539        &sensor_dev_attr_in16_alarm.dev_attr.attr,
1540        &sensor_dev_attr_fan1_input.dev_attr.attr,
1541        &sensor_dev_attr_fan1_div.dev_attr.attr,
1542        &sensor_dev_attr_fan1_min.dev_attr.attr,
1543        &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1544        &sensor_dev_attr_fan2_input.dev_attr.attr,
1545        &sensor_dev_attr_fan2_div.dev_attr.attr,
1546        &sensor_dev_attr_fan2_min.dev_attr.attr,
1547        &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1548        &sensor_dev_attr_fan3_input.dev_attr.attr,
1549        &sensor_dev_attr_fan3_div.dev_attr.attr,
1550        &sensor_dev_attr_fan3_min.dev_attr.attr,
1551        &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1552        &sensor_dev_attr_fan4_input.dev_attr.attr,
1553        &sensor_dev_attr_fan4_div.dev_attr.attr,
1554        &sensor_dev_attr_fan4_min.dev_attr.attr,
1555        &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1556        &sensor_dev_attr_fan5_input.dev_attr.attr,
1557        &sensor_dev_attr_fan5_div.dev_attr.attr,
1558        &sensor_dev_attr_fan5_min.dev_attr.attr,
1559        &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1560        &sensor_dev_attr_fan6_input.dev_attr.attr,
1561        &sensor_dev_attr_fan6_div.dev_attr.attr,
1562        &sensor_dev_attr_fan6_min.dev_attr.attr,
1563        &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1564        &sensor_dev_attr_fan7_input.dev_attr.attr,
1565        &sensor_dev_attr_fan7_div.dev_attr.attr,
1566        &sensor_dev_attr_fan7_min.dev_attr.attr,
1567        &sensor_dev_attr_fan7_alarm.dev_attr.attr,
1568        &sensor_dev_attr_fan8_input.dev_attr.attr,
1569        &sensor_dev_attr_fan8_div.dev_attr.attr,
1570        &sensor_dev_attr_fan8_min.dev_attr.attr,
1571        &sensor_dev_attr_fan8_alarm.dev_attr.attr,
1572        &sensor_dev_attr_temp1_input.dev_attr.attr,
1573        &sensor_dev_attr_temp1_max.dev_attr.attr,
1574        &sensor_dev_attr_temp1_min.dev_attr.attr,
1575        &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1576        &sensor_dev_attr_temp2_input.dev_attr.attr,
1577        &sensor_dev_attr_temp2_max.dev_attr.attr,
1578        &sensor_dev_attr_temp2_min.dev_attr.attr,
1579        &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1580        &sensor_dev_attr_temp1_offset.dev_attr.attr,
1581        &sensor_dev_attr_temp2_offset.dev_attr.attr,
1582        &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1583        &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1584        &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
1585        &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
1586        &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1587        &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1588        &sensor_dev_attr_temp1_crit.dev_attr.attr,
1589        &sensor_dev_attr_temp2_crit.dev_attr.attr,
1590        &dev_attr_temp1_crit_enable.attr,
1591        &dev_attr_temp2_crit_enable.attr,
1592        &dev_attr_cpu0_vid.attr,
1593        &dev_attr_vrm.attr,
1594        &dev_attr_alarms.attr,
1595        &dev_attr_alarm_mask.attr,
1596        &dev_attr_gpio.attr,
1597        &dev_attr_gpio_mask.attr,
1598        &dev_attr_pwm1.attr,
1599        &dev_attr_pwm2.attr,
1600        &dev_attr_pwm3.attr,
1601        &dev_attr_pwm1_enable.attr,
1602        &dev_attr_pwm2_enable.attr,
1603        &dev_attr_pwm3_enable.attr,
1604        &dev_attr_temp1_auto_point1_pwm.attr,
1605        &dev_attr_temp2_auto_point1_pwm.attr,
1606        &dev_attr_temp1_auto_point2_pwm.attr,
1607        &dev_attr_temp2_auto_point2_pwm.attr,
1608        &dev_attr_analog_out.attr,
1609        NULL
1610};
1611
1612static const struct attribute_group adm1026_group = {
1613        .attrs = adm1026_attributes,
1614};
1615
1616static struct attribute *adm1026_attributes_temp3[] = {
1617        &sensor_dev_attr_temp3_input.dev_attr.attr,
1618        &sensor_dev_attr_temp3_max.dev_attr.attr,
1619        &sensor_dev_attr_temp3_min.dev_attr.attr,
1620        &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1621        &sensor_dev_attr_temp3_offset.dev_attr.attr,
1622        &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1623        &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
1624        &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1625        &sensor_dev_attr_temp3_crit.dev_attr.attr,
1626        &dev_attr_temp3_crit_enable.attr,
1627        &dev_attr_temp3_auto_point1_pwm.attr,
1628        &dev_attr_temp3_auto_point2_pwm.attr,
1629        NULL
1630};
1631
1632static const struct attribute_group adm1026_group_temp3 = {
1633        .attrs = adm1026_attributes_temp3,
1634};
1635
1636static struct attribute *adm1026_attributes_in8_9[] = {
1637        &sensor_dev_attr_in8_input.dev_attr.attr,
1638        &sensor_dev_attr_in8_max.dev_attr.attr,
1639        &sensor_dev_attr_in8_min.dev_attr.attr,
1640        &sensor_dev_attr_in8_alarm.dev_attr.attr,
1641        &sensor_dev_attr_in9_input.dev_attr.attr,
1642        &sensor_dev_attr_in9_max.dev_attr.attr,
1643        &sensor_dev_attr_in9_min.dev_attr.attr,
1644        &sensor_dev_attr_in9_alarm.dev_attr.attr,
1645        NULL
1646};
1647
1648static const struct attribute_group adm1026_group_in8_9 = {
1649        .attrs = adm1026_attributes_in8_9,
1650};
1651
1652/* Return 0 if detection is successful, -ENODEV otherwise */
1653static int adm1026_detect(struct i2c_client *client, int kind,
1654                          struct i2c_board_info *info)
1655{
1656        struct i2c_adapter *adapter = client->adapter;
1657        int address = client->addr;
1658        int company, verstep;
1659
1660        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1661                /* We need to be able to do byte I/O */
1662                return -ENODEV;
1663        };
1664
1665        /* Now, we do the remaining detection. */
1666
1667        company = adm1026_read_value(client, ADM1026_REG_COMPANY);
1668        verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
1669
1670        dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1671                " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1672                i2c_adapter_id(client->adapter), client->addr,
1673                company, verstep);
1674
1675        /* If auto-detecting, Determine the chip type. */
1676        if (kind <= 0) {
1677                dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x "
1678                        "...\n", i2c_adapter_id(adapter), address);
1679                if (company == ADM1026_COMPANY_ANALOG_DEV
1680                    && verstep == ADM1026_VERSTEP_ADM1026) {
1681                        kind = adm1026;
1682                } else if (company == ADM1026_COMPANY_ANALOG_DEV
1683                        && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1684                        dev_err(&adapter->dev, "Unrecognized stepping "
1685                                "0x%02x. Defaulting to ADM1026.\n", verstep);
1686                        kind = adm1026;
1687                } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1688                        dev_err(&adapter->dev, "Found version/stepping "
1689                                "0x%02x. Assuming generic ADM1026.\n",
1690                                verstep);
1691                        kind = any_chip;
1692                } else {
1693                        dev_dbg(&adapter->dev, "Autodetection failed\n");
1694                        /* Not an ADM1026 ... */
1695                        if (kind == 0) { /* User used force=x,y */
1696                                dev_err(&adapter->dev, "Generic ADM1026 not "
1697                                        "found at %d,0x%02x.  Try "
1698                                        "force_adm1026.\n",
1699                                        i2c_adapter_id(adapter), address);
1700                        }
1701                        return -ENODEV;
1702                }
1703        }
1704        strlcpy(info->type, "adm1026", I2C_NAME_SIZE);
1705
1706        return 0;
1707}
1708
1709static int adm1026_probe(struct i2c_client *client,
1710                         const struct i2c_device_id *id)
1711{
1712        struct adm1026_data *data;
1713        int err;
1714
1715        data = kzalloc(sizeof(struct adm1026_data), GFP_KERNEL);
1716        if (!data) {
1717                err = -ENOMEM;
1718                goto exit;
1719        }
1720
1721        i2c_set_clientdata(client, data);
1722        mutex_init(&data->update_lock);
1723
1724        /* Set the VRM version */
1725        data->vrm = vid_which_vrm();
1726
1727        /* Initialize the ADM1026 chip */
1728        adm1026_init_client(client);
1729
1730        /* Register sysfs hooks */
1731        if ((err = sysfs_create_group(&client->dev.kobj, &adm1026_group)))
1732                goto exitfree;
1733        if (data->config1 & CFG1_AIN8_9)
1734                err = sysfs_create_group(&client->dev.kobj,
1735                                         &adm1026_group_in8_9);
1736        else
1737                err = sysfs_create_group(&client->dev.kobj,
1738                                         &adm1026_group_temp3);
1739        if (err)
1740                goto exitremove;
1741
1742        data->hwmon_dev = hwmon_device_register(&client->dev);
1743        if (IS_ERR(data->hwmon_dev)) {
1744                err = PTR_ERR(data->hwmon_dev);
1745                goto exitremove;
1746        }
1747
1748        return 0;
1749
1750        /* Error out and cleanup code */
1751exitremove:
1752        sysfs_remove_group(&client->dev.kobj, &adm1026_group);
1753        if (data->config1 & CFG1_AIN8_9)
1754                sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1755        else
1756                sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
1757exitfree:
1758        kfree(data);
1759exit:
1760        return err;
1761}
1762
1763static int adm1026_remove(struct i2c_client *client)
1764{
1765        struct adm1026_data *data = i2c_get_clientdata(client);
1766        hwmon_device_unregister(data->hwmon_dev);
1767        sysfs_remove_group(&client->dev.kobj, &adm1026_group);
1768        if (data->config1 & CFG1_AIN8_9)
1769                sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1770        else
1771                sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
1772        kfree(data);
1773        return 0;
1774}
1775
1776static int __init sm_adm1026_init(void)
1777{
1778        return i2c_add_driver(&adm1026_driver);
1779}
1780
1781static void __exit sm_adm1026_exit(void)
1782{
1783        i2c_del_driver(&adm1026_driver);
1784}
1785
1786MODULE_LICENSE("GPL");
1787MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1788              "Justin Thiessen <jthiessen@penguincomputing.com>");
1789MODULE_DESCRIPTION("ADM1026 driver");
1790
1791module_init(sm_adm1026_init);
1792module_exit(sm_adm1026_exit);
1793
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.