linux/drivers/hwmon/lm93.c
<<
>>
Prefs
   1/*
   2 * lm93.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
   3 *
   4 * Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
   5 *      Copyright (c) 2004 Utilitek Systems, Inc.
   6 *
   7 * derived in part from lm78.c:
   8 *      Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
   9 *
  10 * derived in part from lm85.c:
  11 *      Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
  12 *      Copyright (c) 2003       Margit Schubert-While <margitsw@t-online.de>
  13 *
  14 * derived in part from w83l785ts.c:
  15 *      Copyright (c) 2003-2004 Jean Delvare <khali@linux-fr.org>
  16 *
  17 * Ported to Linux 2.6 by Eric J. Bowersox <ericb@aspsys.com>
  18 *      Copyright (c) 2005 Aspen Systems, Inc.
  19 *
  20 * Adapted to 2.6.20 by Carsten Emde <cbe@osadl.org>
  21 *      Copyright (c) 2006 Carsten Emde, Open Source Automation Development Lab
  22 *
  23 * Modified for mainline integration by Hans J. Koch <hjk@hansjkoch.de>
  24 *      Copyright (c) 2007 Hans J. Koch, Linutronix GmbH
  25 *
  26 * This program is free software; you can redistribute it and/or modify
  27 * it under the terms of the GNU General Public License as published by
  28 * the Free Software Foundation; either version 2 of the License, or
  29 * (at your option) any later version.
  30 *
  31 * This program is distributed in the hope that it will be useful,
  32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34 * GNU General Public License for more details.
  35 *
  36 * You should have received a copy of the GNU General Public License
  37 * along with this program; if not, write to the Free Software
  38 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  39 */
  40
  41#include <linux/module.h>
  42#include <linux/init.h>
  43#include <linux/slab.h>
  44#include <linux/i2c.h>
  45#include <linux/hwmon.h>
  46#include <linux/hwmon-sysfs.h>
  47#include <linux/hwmon-vid.h>
  48#include <linux/err.h>
  49#include <linux/delay.h>
  50
  51/* LM93 REGISTER ADDRESSES */
  52
  53/* miscellaneous */
  54#define LM93_REG_MFR_ID                 0x3e
  55#define LM93_REG_VER                    0x3f
  56#define LM93_REG_STATUS_CONTROL         0xe2
  57#define LM93_REG_CONFIG                 0xe3
  58#define LM93_REG_SLEEP_CONTROL          0xe4
  59
  60/* alarm values start here */
  61#define LM93_REG_HOST_ERROR_1           0x48
  62
  63/* voltage inputs: in1-in16 (nr => 0-15) */
  64#define LM93_REG_IN(nr)                 (0x56 + (nr))
  65#define LM93_REG_IN_MIN(nr)             (0x90 + (nr) * 2)
  66#define LM93_REG_IN_MAX(nr)             (0x91 + (nr) * 2)
  67
  68/* temperature inputs: temp1-temp4 (nr => 0-3) */
  69#define LM93_REG_TEMP(nr)               (0x50 + (nr))
  70#define LM93_REG_TEMP_MIN(nr)           (0x78 + (nr) * 2)
  71#define LM93_REG_TEMP_MAX(nr)           (0x79 + (nr) * 2)
  72
  73/* temp[1-4]_auto_boost (nr => 0-3) */
  74#define LM93_REG_BOOST(nr)              (0x80 + (nr))
  75
  76/* #PROCHOT inputs: prochot1-prochot2 (nr => 0-1) */
  77#define LM93_REG_PROCHOT_CUR(nr)        (0x67 + (nr) * 2)
  78#define LM93_REG_PROCHOT_AVG(nr)        (0x68 + (nr) * 2)
  79#define LM93_REG_PROCHOT_MAX(nr)        (0xb0 + (nr))
  80
  81/* fan tach inputs: fan1-fan4 (nr => 0-3) */
  82#define LM93_REG_FAN(nr)                (0x6e + (nr) * 2)
  83#define LM93_REG_FAN_MIN(nr)            (0xb4 + (nr) * 2)
  84
  85/* pwm outputs: pwm1-pwm2 (nr => 0-1, reg => 0-3) */
  86#define LM93_REG_PWM_CTL(nr, reg)       (0xc8 + (reg) + (nr) * 4)
  87#define LM93_PWM_CTL1   0x0
  88#define LM93_PWM_CTL2   0x1
  89#define LM93_PWM_CTL3   0x2
  90#define LM93_PWM_CTL4   0x3
  91
  92/* GPIO input state */
  93#define LM93_REG_GPI                    0x6b
  94
  95/* vid inputs: vid1-vid2 (nr => 0-1) */
  96#define LM93_REG_VID(nr)                (0x6c + (nr))
  97
  98/* vccp1 & vccp2: VID relative inputs (nr => 0-1) */
  99#define LM93_REG_VCCP_LIMIT_OFF(nr)     (0xb2 + (nr))
 100
 101/* temp[1-4]_auto_boost_hyst */
 102#define LM93_REG_BOOST_HYST_12          0xc0
 103#define LM93_REG_BOOST_HYST_34          0xc1
 104#define LM93_REG_BOOST_HYST(nr)         (0xc0 + (nr)/2)
 105
 106/* temp[1-4]_auto_pwm_[min|hyst] */
 107#define LM93_REG_PWM_MIN_HYST_12        0xc3
 108#define LM93_REG_PWM_MIN_HYST_34        0xc4
 109#define LM93_REG_PWM_MIN_HYST(nr)       (0xc3 + (nr)/2)
 110
 111/* prochot_override & prochot_interval */
 112#define LM93_REG_PROCHOT_OVERRIDE       0xc6
 113#define LM93_REG_PROCHOT_INTERVAL       0xc7
 114
 115/* temp[1-4]_auto_base (nr => 0-3) */
 116#define LM93_REG_TEMP_BASE(nr)          (0xd0 + (nr))
 117
 118/* temp[1-4]_auto_offsets (step => 0-11) */
 119#define LM93_REG_TEMP_OFFSET(step)      (0xd4 + (step))
 120
 121/* #PROCHOT & #VRDHOT PWM ramp control */
 122#define LM93_REG_PWM_RAMP_CTL           0xbf
 123
 124/* miscellaneous */
 125#define LM93_REG_SFC1           0xbc
 126#define LM93_REG_SFC2           0xbd
 127#define LM93_REG_GPI_VID_CTL    0xbe
 128#define LM93_REG_SF_TACH_TO_PWM 0xe0
 129
 130/* error masks */
 131#define LM93_REG_GPI_ERR_MASK   0xec
 132#define LM93_REG_MISC_ERR_MASK  0xed
 133
 134/* LM93 REGISTER VALUES */
 135#define LM93_MFR_ID             0x73
 136#define LM93_MFR_ID_PROTOTYPE   0x72
 137
 138/* LM94 REGISTER VALUES */
 139#define LM94_MFR_ID_2           0x7a
 140#define LM94_MFR_ID             0x79
 141#define LM94_MFR_ID_PROTOTYPE   0x78
 142
 143/* SMBus capabilities */
 144#define LM93_SMBUS_FUNC_FULL (I2C_FUNC_SMBUS_BYTE_DATA | \
 145                I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA)
 146#define LM93_SMBUS_FUNC_MIN  (I2C_FUNC_SMBUS_BYTE_DATA | \
 147                I2C_FUNC_SMBUS_WORD_DATA)
 148
 149/* Addresses to scan */
 150static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
 151
 152/* Insmod parameters */
 153
 154static bool disable_block;
 155module_param(disable_block, bool, 0);
 156MODULE_PARM_DESC(disable_block,
 157        "Set to non-zero to disable SMBus block data transactions.");
 158
 159static bool init;
 160module_param(init, bool, 0);
 161MODULE_PARM_DESC(init, "Set to non-zero to force chip initialization.");
 162
 163static int vccp_limit_type[2] = {0, 0};
 164module_param_array(vccp_limit_type, int, NULL, 0);
 165MODULE_PARM_DESC(vccp_limit_type, "Configures in7 and in8 limit modes.");
 166
 167static int vid_agtl;
 168module_param(vid_agtl, int, 0);
 169MODULE_PARM_DESC(vid_agtl, "Configures VID pin input thresholds.");
 170
 171/* Driver data */
 172static struct i2c_driver lm93_driver;
 173
 174/* LM93 BLOCK READ COMMANDS */
 175static const struct { u8 cmd; u8 len; } lm93_block_read_cmds[12] = {
 176        { 0xf2,  8 },
 177        { 0xf3,  8 },
 178        { 0xf4,  6 },
 179        { 0xf5, 16 },
 180        { 0xf6,  4 },
 181        { 0xf7,  8 },
 182        { 0xf8, 12 },
 183        { 0xf9, 32 },
 184        { 0xfa,  8 },
 185        { 0xfb,  8 },
 186        { 0xfc, 16 },
 187        { 0xfd,  9 },
 188};
 189
 190/*
 191 * ALARMS: SYSCTL format described further below
 192 * REG: 64 bits in 8 registers, as immediately below
 193 */
 194struct block1_t {
 195        u8 host_status_1;
 196        u8 host_status_2;
 197        u8 host_status_3;
 198        u8 host_status_4;
 199        u8 p1_prochot_status;
 200        u8 p2_prochot_status;
 201        u8 gpi_status;
 202        u8 fan_status;
 203};
 204
 205/*
 206 * Client-specific data
 207 */
 208struct lm93_data {
 209        struct device *hwmon_dev;
 210
 211        struct mutex update_lock;
 212        unsigned long last_updated;     /* In jiffies */
 213
 214        /* client update function */
 215        void (*update)(struct lm93_data *, struct i2c_client *);
 216
 217        char valid; /* !=0 if following fields are valid */
 218
 219        /* register values, arranged by block read groups */
 220        struct block1_t block1;
 221
 222        /*
 223         * temp1 - temp4: unfiltered readings
 224         * temp1 - temp2: filtered readings
 225         */
 226        u8 block2[6];
 227
 228        /* vin1 - vin16: readings */
 229        u8 block3[16];
 230
 231        /* prochot1 - prochot2: readings */
 232        struct {
 233                u8 cur;
 234                u8 avg;
 235        } block4[2];
 236
 237        /* fan counts 1-4 => 14-bits, LE, *left* justified */
 238        u16 block5[4];
 239
 240        /* block6 has a lot of data we don't need */
 241        struct {
 242                u8 min;
 243                u8 max;
 244        } temp_lim[4];
 245
 246        /* vin1 - vin16: low and high limits */
 247        struct {
 248                u8 min;
 249                u8 max;
 250        } block7[16];
 251
 252        /* fan count limits 1-4 => same format as block5 */
 253        u16 block8[4];
 254
 255        /* pwm control registers (2 pwms, 4 regs) */
 256        u8 block9[2][4];
 257
 258        /* auto/pwm base temp and offset temp registers */
 259        struct {
 260                u8 base[4];
 261                u8 offset[12];
 262        } block10;
 263
 264        /* master config register */
 265        u8 config;
 266
 267        /* VID1 & VID2 => register format, 6-bits, right justified */
 268        u8 vid[2];
 269
 270        /* prochot1 - prochot2: limits */
 271        u8 prochot_max[2];
 272
 273        /* vccp1 & vccp2 (in7 & in8): VID relative limits (register format) */
 274        u8 vccp_limits[2];
 275
 276        /* GPIO input state (register format, i.e. inverted) */
 277        u8 gpi;
 278
 279        /* #PROCHOT override (register format) */
 280        u8 prochot_override;
 281
 282        /* #PROCHOT intervals (register format) */
 283        u8 prochot_interval;
 284
 285        /* Fan Boost Temperatures (register format) */
 286        u8 boost[4];
 287
 288        /* Fan Boost Hysteresis (register format) */
 289        u8 boost_hyst[2];
 290
 291        /* Temperature Zone Min. PWM & Hysteresis (register format) */
 292        u8 auto_pwm_min_hyst[2];
 293
 294        /* #PROCHOT & #VRDHOT PWM Ramp Control */
 295        u8 pwm_ramp_ctl;
 296
 297        /* miscellaneous setup regs */
 298        u8 sfc1;
 299        u8 sfc2;
 300        u8 sf_tach_to_pwm;
 301
 302        /*
 303         * The two PWM CTL2  registers can read something other than what was
 304         * last written for the OVR_DC field (duty cycle override).  So, we
 305         * save the user-commanded value here.
 306         */
 307        u8 pwm_override[2];
 308};
 309
 310/*
 311 * VID: mV
 312 * REG: 6-bits, right justified, *always* using Intel VRM/VRD 10
 313 */
 314static int LM93_VID_FROM_REG(u8 reg)
 315{
 316        return vid_from_reg((reg & 0x3f), 100);
 317}
 318
 319/* min, max, and nominal register values, per channel (u8) */
 320static const u8 lm93_vin_reg_min[16] = {
 321        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 322        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae,
 323};
 324static const u8 lm93_vin_reg_max[16] = {
 325        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 326        0xff, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd1,
 327};
 328/*
 329 * Values from the datasheet. They're here for documentation only.
 330 * static const u8 lm93_vin_reg_nom[16] = {
 331 * 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
 332 * 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, 0xc0,
 333 * };
 334 */
 335
 336/* min, max, and nominal voltage readings, per channel (mV)*/
 337static const unsigned long lm93_vin_val_min[16] = {
 338        0, 0, 0, 0, 0, 0, 0, 0,
 339        0, 0, 0, 0, 0, 0, 0, 3000,
 340};
 341
 342static const unsigned long lm93_vin_val_max[16] = {
 343        1236, 1236, 1236, 1600, 2000, 2000, 1600, 1600,
 344        4400, 6500, 3333, 2625, 1312, 1312, 1236, 3600,
 345};
 346/*
 347 * Values from the datasheet. They're here for documentation only.
 348 * static const unsigned long lm93_vin_val_nom[16] = {
 349 * 927,  927,  927, 1200, 1500, 1500, 1200, 1200,
 350 * 3300, 5000, 2500, 1969,  984,  984,  309, 3300,
 351 * };
 352 */
 353
 354static unsigned LM93_IN_FROM_REG(int nr, u8 reg)
 355{
 356        const long uV_max = lm93_vin_val_max[nr] * 1000;
 357        const long uV_min = lm93_vin_val_min[nr] * 1000;
 358
 359        const long slope = (uV_max - uV_min) /
 360                (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
 361        const long intercept = uV_min - slope * lm93_vin_reg_min[nr];
 362
 363        return (slope * reg + intercept + 500) / 1000;
 364}
 365
 366/*
 367 * IN: mV, limits determined by channel nr
 368 * REG: scaling determined by channel nr
 369 */
 370static u8 LM93_IN_TO_REG(int nr, unsigned val)
 371{
 372        /* range limit */
 373        const long mV = SENSORS_LIMIT(val,
 374                lm93_vin_val_min[nr], lm93_vin_val_max[nr]);
 375
 376        /* try not to lose too much precision here */
 377        const long uV = mV * 1000;
 378        const long uV_max = lm93_vin_val_max[nr] * 1000;
 379        const long uV_min = lm93_vin_val_min[nr] * 1000;
 380
 381        /* convert */
 382        const long slope = (uV_max - uV_min) /
 383                (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
 384        const long intercept = uV_min - slope * lm93_vin_reg_min[nr];
 385
 386        u8 result = ((uV - intercept + (slope/2)) / slope);
 387        result = SENSORS_LIMIT(result,
 388                        lm93_vin_reg_min[nr], lm93_vin_reg_max[nr]);
 389        return result;
 390}
 391
 392/* vid in mV, upper == 0 indicates low limit, otherwise upper limit */
 393static unsigned LM93_IN_REL_FROM_REG(u8 reg, int upper, int vid)
 394{
 395        const long uV_offset = upper ? (((reg >> 4 & 0x0f) + 1) * 12500) :
 396                                (((reg >> 0 & 0x0f) + 1) * -25000);
 397        const long uV_vid = vid * 1000;
 398        return (uV_vid + uV_offset + 5000) / 10000;
 399}
 400
 401#define LM93_IN_MIN_FROM_REG(reg, vid)  LM93_IN_REL_FROM_REG((reg), 0, (vid))
 402#define LM93_IN_MAX_FROM_REG(reg, vid)  LM93_IN_REL_FROM_REG((reg), 1, (vid))
 403
 404/*
 405 * vid in mV , upper == 0 indicates low limit, otherwise upper limit
 406 * upper also determines which nibble of the register is returned
 407 * (the other nibble will be 0x0)
 408 */
 409static u8 LM93_IN_REL_TO_REG(unsigned val, int upper, int vid)
 410{
 411        long uV_offset = vid * 1000 - val * 10000;
 412        if (upper) {
 413                uV_offset = SENSORS_LIMIT(uV_offset, 12500, 200000);
 414                return (u8)((uV_offset /  12500 - 1) << 4);
 415        } else {
 416                uV_offset = SENSORS_LIMIT(uV_offset, -400000, -25000);
 417                return (u8)((uV_offset / -25000 - 1) << 0);
 418        }
 419}
 420
 421/*
 422 * TEMP: 1/1000 degrees C (-128C to +127C)
 423 * REG: 1C/bit, two's complement
 424 */
 425static int LM93_TEMP_FROM_REG(u8 reg)
 426{
 427        return (s8)reg * 1000;
 428}
 429
 430#define LM93_TEMP_MIN (-128000)
 431#define LM93_TEMP_MAX (127000)
 432
 433/*
 434 * TEMP: 1/1000 degrees C (-128C to +127C)
 435 * REG: 1C/bit, two's complement
 436 */
 437static u8 LM93_TEMP_TO_REG(long temp)
 438{
 439        int ntemp = SENSORS_LIMIT(temp, LM93_TEMP_MIN, LM93_TEMP_MAX);
 440        ntemp += (ntemp < 0 ? -500 : 500);
 441        return (u8)(ntemp / 1000);
 442}
 443
 444/* Determine 4-bit temperature offset resolution */
 445static int LM93_TEMP_OFFSET_MODE_FROM_REG(u8 sfc2, int nr)
 446{
 447        /* mode: 0 => 1C/bit, nonzero => 0.5C/bit */
 448        return sfc2 & (nr < 2 ? 0x10 : 0x20);
 449}
 450
 451/*
 452 * This function is common to all 4-bit temperature offsets
 453 * reg is 4 bits right justified
 454 * mode 0 => 1C/bit, mode !0 => 0.5C/bit
 455 */
 456static int LM93_TEMP_OFFSET_FROM_REG(u8 reg, int mode)
 457{
 458        return (reg & 0x0f) * (mode ? 5 : 10);
 459}
 460
 461#define LM93_TEMP_OFFSET_MIN  (0)
 462#define LM93_TEMP_OFFSET_MAX0 (150)
 463#define LM93_TEMP_OFFSET_MAX1 (75)
 464
 465/*
 466 * This function is common to all 4-bit temperature offsets
 467 * returns 4 bits right justified
 468 * mode 0 => 1C/bit, mode !0 => 0.5C/bit
 469 */
 470static u8 LM93_TEMP_OFFSET_TO_REG(int off, int mode)
 471{
 472        int factor = mode ? 5 : 10;
 473
 474        off = SENSORS_LIMIT(off, LM93_TEMP_OFFSET_MIN,
 475                mode ? LM93_TEMP_OFFSET_MAX1 : LM93_TEMP_OFFSET_MAX0);
 476        return (u8)((off + factor/2) / factor);
 477}
 478
 479/* 0 <= nr <= 3 */
 480static int LM93_TEMP_AUTO_OFFSET_FROM_REG(u8 reg, int nr, int mode)
 481{
 482        /* temp1-temp2 (nr=0,1) use lower nibble */
 483        if (nr < 2)
 484                return LM93_TEMP_OFFSET_FROM_REG(reg & 0x0f, mode);
 485
 486        /* temp3-temp4 (nr=2,3) use upper nibble */
 487        else
 488                return LM93_TEMP_OFFSET_FROM_REG(reg >> 4 & 0x0f, mode);
 489}
 490
 491/*
 492 * TEMP: 1/10 degrees C (0C to +15C (mode 0) or +7.5C (mode non-zero))
 493 * REG: 1.0C/bit (mode 0) or 0.5C/bit (mode non-zero)
 494 * 0 <= nr <= 3
 495 */
 496static u8 LM93_TEMP_AUTO_OFFSET_TO_REG(u8 old, int off, int nr, int mode)
 497{
 498        u8 new = LM93_TEMP_OFFSET_TO_REG(off, mode);
 499
 500        /* temp1-temp2 (nr=0,1) use lower nibble */
 501        if (nr < 2)
 502                return (old & 0xf0) | (new & 0x0f);
 503
 504        /* temp3-temp4 (nr=2,3) use upper nibble */
 505        else
 506                return (new << 4 & 0xf0) | (old & 0x0f);
 507}
 508
 509static int LM93_AUTO_BOOST_HYST_FROM_REGS(struct lm93_data *data, int nr,
 510                int mode)
 511{
 512        u8 reg;
 513
 514        switch (nr) {
 515        case 0:
 516                reg = data->boost_hyst[0] & 0x0f;
 517                break;
 518        case 1:
 519                reg = data->boost_hyst[0] >> 4 & 0x0f;
 520                break;
 521        case 2:
 522                reg = data->boost_hyst[1] & 0x0f;
 523                break;
 524        case 3:
 525        default:
 526                reg = data->boost_hyst[1] >> 4 & 0x0f;
 527                break;
 528        }
 529
 530        return LM93_TEMP_FROM_REG(data->boost[nr]) -
 531                        LM93_TEMP_OFFSET_FROM_REG(reg, mode);
 532}
 533
 534static u8 LM93_AUTO_BOOST_HYST_TO_REG(struct lm93_data *data, long hyst,
 535                int nr, int mode)
 536{
 537        u8 reg = LM93_TEMP_OFFSET_TO_REG(
 538                        (LM93_TEMP_FROM_REG(data->boost[nr]) - hyst), mode);
 539
 540        switch (nr) {
 541        case 0:
 542                reg = (data->boost_hyst[0] & 0xf0) | (reg & 0x0f);
 543                break;
 544        case 1:
 545                reg = (reg << 4 & 0xf0) | (data->boost_hyst[0] & 0x0f);
 546                break;
 547        case 2:
 548                reg = (data->boost_hyst[1] & 0xf0) | (reg & 0x0f);
 549                break;
 550        case 3:
 551        default:
 552                reg = (reg << 4 & 0xf0) | (data->boost_hyst[1] & 0x0f);
 553                break;
 554        }
 555
 556        return reg;
 557}
 558
 559/*
 560 * PWM: 0-255 per sensors documentation
 561 * REG: 0-13 as mapped below... right justified
 562 */
 563enum pwm_freq { LM93_PWM_MAP_HI_FREQ, LM93_PWM_MAP_LO_FREQ };
 564
 565static int lm93_pwm_map[2][16] = {
 566        {
 567                0x00, /*   0.00% */ 0x40, /*  25.00% */
 568                0x50, /*  31.25% */ 0x60, /*  37.50% */
 569                0x70, /*  43.75% */ 0x80, /*  50.00% */
 570                0x90, /*  56.25% */ 0xa0, /*  62.50% */
 571                0xb0, /*  68.75% */ 0xc0, /*  75.00% */
 572                0xd0, /*  81.25% */ 0xe0, /*  87.50% */
 573                0xf0, /*  93.75% */ 0xff, /* 100.00% */
 574                0xff, 0xff, /* 14, 15 are reserved and should never occur */
 575        },
 576        {
 577                0x00, /*   0.00% */ 0x40, /*  25.00% */
 578                0x49, /*  28.57% */ 0x52, /*  32.14% */
 579                0x5b, /*  35.71% */ 0x64, /*  39.29% */
 580                0x6d, /*  42.86% */ 0x76, /*  46.43% */
 581                0x80, /*  50.00% */ 0x89, /*  53.57% */
 582                0x92, /*  57.14% */ 0xb6, /*  71.43% */
 583                0xdb, /*  85.71% */ 0xff, /* 100.00% */
 584                0xff, 0xff, /* 14, 15 are reserved and should never occur */
 585        },
 586};
 587
 588static int LM93_PWM_FROM_REG(u8 reg, enum pwm_freq freq)
 589{
 590        return lm93_pwm_map[freq][reg & 0x0f];
 591}
 592
 593/* round up to nearest match */
 594static u8 LM93_PWM_TO_REG(int pwm, enum pwm_freq freq)
 595{
 596        int i;
 597        for (i = 0; i < 13; i++)
 598                if (pwm <= lm93_pwm_map[freq][i])
 599                        break;
 600
 601        /* can fall through with i==13 */
 602        return (u8)i;
 603}
 604
 605static int LM93_FAN_FROM_REG(u16 regs)
 606{
 607        const u16 count = le16_to_cpu(regs) >> 2;
 608        return count == 0 ? -1 : count == 0x3fff ? 0 : 1350000 / count;
 609}
 610
 611/*
 612 * RPM: (82.5 to 1350000)
 613 * REG: 14-bits, LE, *left* justified
 614 */
 615static u16 LM93_FAN_TO_REG(long rpm)
 616{
 617        u16 count, regs;
 618
 619        if (rpm == 0) {
 620                count = 0x3fff;
 621        } else {
 622                rpm = SENSORS_LIMIT(rpm, 1, 1000000);
 623                count = SENSORS_LIMIT((1350000 + rpm) / rpm, 1, 0x3ffe);
 624        }
 625
 626        regs = count << 2;
 627        return cpu_to_le16(regs);
 628}
 629
 630/*
 631 * PWM FREQ: HZ
 632 * REG: 0-7 as mapped below
 633 */
 634static int lm93_pwm_freq_map[8] = {
 635        22500, 96, 84, 72, 60, 48, 36, 12
 636};
 637
 638static int LM93_PWM_FREQ_FROM_REG(u8 reg)
 639{
 640        return lm93_pwm_freq_map[reg & 0x07];
 641}
 642
 643/* round up to nearest match */
 644static u8 LM93_PWM_FREQ_TO_REG(int freq)
 645{
 646        int i;
 647        for (i = 7; i > 0; i--)
 648                if (freq <= lm93_pwm_freq_map[i])
 649                        break;
 650
 651        /* can fall through with i==0 */
 652        return (u8)i;
 653}
 654
 655/*
 656 * TIME: 1/100 seconds
 657 * REG: 0-7 as mapped below
 658 */
 659static int lm93_spinup_time_map[8] = {
 660        0, 10, 25, 40, 70, 100, 200, 400,
 661};
 662
 663static int LM93_SPINUP_TIME_FROM_REG(u8 reg)
 664{
 665        return lm93_spinup_time_map[reg >> 5 & 0x07];
 666}
 667
 668/* round up to nearest match */
 669static u8 LM93_SPINUP_TIME_TO_REG(int time)
 670{
 671        int i;
 672        for (i = 0; i < 7; i++)
 673                if (time <= lm93_spinup_time_map[i])
 674                        break;
 675
 676        /* can fall through with i==8 */
 677        return (u8)i;
 678}
 679
 680#define LM93_RAMP_MIN 0
 681#define LM93_RAMP_MAX 75
 682
 683static int LM93_RAMP_FROM_REG(u8 reg)
 684{
 685        return (reg & 0x0f) * 5;
 686}
 687
 688/*
 689 * RAMP: 1/100 seconds
 690 * REG: 50mS/bit 4-bits right justified
 691 */
 692static u8 LM93_RAMP_TO_REG(int ramp)
 693{
 694        ramp = SENSORS_LIMIT(ramp, LM93_RAMP_MIN, LM93_RAMP_MAX);
 695        return (u8)((ramp + 2) / 5);
 696}
 697
 698/*
 699 * PROCHOT: 0-255, 0 => 0%, 255 => > 96.6%
 700 * REG: (same)
 701 */
 702static u8 LM93_PROCHOT_TO_REG(long prochot)
 703{
 704        prochot = SENSORS_LIMIT(prochot, 0, 255);
 705        return (u8)prochot;
 706}
 707
 708/*
 709 * PROCHOT-INTERVAL: 73 - 37200 (1/100 seconds)
 710 * REG: 0-9 as mapped below
 711 */
 712static int lm93_interval_map[10] = {
 713        73, 146, 290, 580, 1170, 2330, 4660, 9320, 18600, 37200,
 714};
 715
 716static int LM93_INTERVAL_FROM_REG(u8 reg)
 717{
 718        return lm93_interval_map[reg & 0x0f];
 719}
 720
 721/* round up to nearest match */
 722static u8 LM93_INTERVAL_TO_REG(long interval)
 723{
 724        int i;
 725        for (i = 0; i < 9; i++)
 726                if (interval <= lm93_interval_map[i])
 727                        break;
 728
 729        /* can fall through with i==9 */
 730        return (u8)i;
 731}
 732
 733/*
 734 * GPIO: 0-255, GPIO0 is LSB
 735 * REG: inverted
 736 */
 737static unsigned LM93_GPI_FROM_REG(u8 reg)
 738{
 739        return ~reg & 0xff;
 740}
 741
 742/*
 743 * alarm bitmask definitions
 744 * The LM93 has nearly 64 bits of error status... I've pared that down to
 745 * what I think is a useful subset in order to fit it into 32 bits.
 746 *
 747 * Especially note that the #VRD_HOT alarms are missing because we provide
 748 * that information as values in another sysfs file.
 749 *
 750 * If libsensors is extended to support 64 bit values, this could be revisited.
 751 */
 752#define LM93_ALARM_IN1          0x00000001
 753#define LM93_ALARM_IN2          0x00000002
 754#define LM93_ALARM_IN3          0x00000004
 755#define LM93_ALARM_IN4          0x00000008
 756#define LM93_ALARM_IN5          0x00000010
 757#define LM93_ALARM_IN6          0x00000020
 758#define LM93_ALARM_IN7          0x00000040
 759#define LM93_ALARM_IN8          0x00000080
 760#define LM93_ALARM_IN9          0x00000100
 761#define LM93_ALARM_IN10         0x00000200
 762#define LM93_ALARM_IN11         0x00000400
 763#define LM93_ALARM_IN12         0x00000800
 764#define LM93_ALARM_IN13         0x00001000
 765#define LM93_ALARM_IN14         0x00002000
 766#define LM93_ALARM_IN15         0x00004000
 767#define LM93_ALARM_IN16         0x00008000
 768#define LM93_ALARM_FAN1         0x00010000
 769#define LM93_ALARM_FAN2         0x00020000
 770#define LM93_ALARM_FAN3         0x00040000
 771#define LM93_ALARM_FAN4         0x00080000
 772#define LM93_ALARM_PH1_ERR      0x00100000
 773#define LM93_ALARM_PH2_ERR      0x00200000
 774#define LM93_ALARM_SCSI1_ERR    0x00400000
 775#define LM93_ALARM_SCSI2_ERR    0x00800000
 776#define LM93_ALARM_DVDDP1_ERR   0x01000000
 777#define LM93_ALARM_DVDDP2_ERR   0x02000000
 778#define LM93_ALARM_D1_ERR       0x04000000
 779#define LM93_ALARM_D2_ERR       0x08000000
 780#define LM93_ALARM_TEMP1        0x10000000
 781#define LM93_ALARM_TEMP2        0x20000000
 782#define LM93_ALARM_TEMP3        0x40000000
 783
 784static unsigned LM93_ALARMS_FROM_REG(struct block1_t b1)
 785{
 786        unsigned result;
 787        result  = b1.host_status_2 & 0x3f;
 788
 789        if (vccp_limit_type[0])
 790                result |= (b1.host_status_4 & 0x10) << 2;
 791        else
 792                result |= b1.host_status_2 & 0x40;
 793
 794        if (vccp_limit_type[1])
 795                result |= (b1.host_status_4 & 0x20) << 2;
 796        else
 797                result |= b1.host_status_2 & 0x80;
 798
 799        result |= b1.host_status_3 << 8;
 800        result |= (b1.fan_status & 0x0f) << 16;
 801        result |= (b1.p1_prochot_status & 0x80) << 13;
 802        result |= (b1.p2_prochot_status & 0x80) << 14;
 803        result |= (b1.host_status_4 & 0xfc) << 20;
 804        result |= (b1.host_status_1 & 0x07) << 28;
 805        return result;
 806}
 807
 808#define MAX_RETRIES 5
 809
 810static u8 lm93_read_byte(struct i2c_client *client, u8 reg)
 811{
 812        int value, i;
 813
 814        /* retry in case of read errors */
 815        for (i = 1; i <= MAX_RETRIES; i++) {
 816                value = i2c_smbus_read_byte_data(client, reg);
 817                if (value >= 0) {
 818                        return value;
 819                } else {
 820                        dev_warn(&client->dev, "lm93: read byte data failed, "
 821                                "address 0x%02x.\n", reg);
 822                        mdelay(i + 3);
 823                }
 824
 825        }
 826
 827        /* <TODO> what to return in case of error? */
 828        dev_err(&client->dev, "lm93: All read byte retries failed!!\n");
 829        return 0;
 830}
 831
 832static int lm93_write_byte(struct i2c_client *client, u8 reg, u8 value)
 833{
 834        int result;
 835
 836        /* <TODO> how to handle write errors? */
 837        result = i2c_smbus_write_byte_data(client, reg, value);
 838
 839        if (result < 0)
 840                dev_warn(&client->dev, "lm93: write byte data failed, "
 841                         "0x%02x at address 0x%02x.\n", value, reg);
 842
 843        return result;
 844}
 845
 846static u16 lm93_read_word(struct i2c_client *client, u8 reg)
 847{
 848        int value, i;
 849
 850        /* retry in case of read errors */
 851        for (i = 1; i <= MAX_RETRIES; i++) {
 852                value = i2c_smbus_read_word_data(client, reg);
 853                if (value >= 0) {
 854                        return value;
 855                } else {
 856                        dev_warn(&client->dev, "lm93: read word data failed, "
 857                                 "address 0x%02x.\n", reg);
 858                        mdelay(i + 3);
 859                }
 860
 861        }
 862
 863        /* <TODO> what to return in case of error? */
 864        dev_err(&client->dev, "lm93: All read word retries failed!!\n");
 865        return 0;
 866}
 867
 868static int lm93_write_word(struct i2c_client *client, u8 reg, u16 value)
 869{
 870        int result;
 871
 872        /* <TODO> how to handle write errors? */
 873        result = i2c_smbus_write_word_data(client, reg, value);
 874
 875        if (result < 0)
 876                dev_warn(&client->dev, "lm93: write word data failed, "
 877                         "0x%04x at address 0x%02x.\n", value, reg);
 878
 879        return result;
 880}
 881
 882static u8 lm93_block_buffer[I2C_SMBUS_BLOCK_MAX];
 883
 884/*
 885 * read block data into values, retry if not expected length
 886 * fbn => index to lm93_block_read_cmds table
 887 * (Fixed Block Number - section 14.5.2 of LM93 datasheet)
 888 */
 889static void lm93_read_block(struct i2c_client *client, u8 fbn, u8 *values)
 890{
 891        int i, result = 0;
 892
 893        for (i = 1; i <= MAX_RETRIES; i++) {
 894                result = i2c_smbus_read_block_data(client,
 895                        lm93_block_read_cmds[fbn].cmd, lm93_block_buffer);
 896
 897                if (result == lm93_block_read_cmds[fbn].len) {
 898                        break;
 899                } else {
 900                        dev_warn(&client->dev, "lm93: block read data failed, "
 901                                 "command 0x%02x.\n",
 902                                 lm93_block_read_cmds[fbn].cmd);
 903                        mdelay(i + 3);
 904                }
 905        }
 906
 907        if (result == lm93_block_read_cmds[fbn].len) {
 908                memcpy(values, lm93_block_buffer,
 909                       lm93_block_read_cmds[fbn].len);
 910        } else {
 911                /* <TODO> what to do in case of error? */
 912        }
 913}
 914
 915static struct lm93_data *lm93_update_device(struct device *dev)
 916{
 917        struct i2c_client *client = to_i2c_client(dev);
 918        struct lm93_data *data = i2c_get_clientdata(client);
 919        const unsigned long interval = HZ + (HZ / 2);
 920
 921        mutex_lock(&data->update_lock);
 922
 923        if (time_after(jiffies, data->last_updated + interval) ||
 924                !data->valid) {
 925
 926                data->update(data, client);
 927                data->last_updated = jiffies;
 928                data->valid = 1;
 929        }
 930
 931        mutex_unlock(&data->update_lock);
 932        return data;
 933}
 934
 935/* update routine for data that has no corresponding SMBus block command */
 936static void lm93_update_client_common(struct lm93_data *data,
 937                                      struct i2c_client *client)
 938{
 939        int i;
 940        u8 *ptr;
 941
 942        /* temp1 - temp4: limits */
 943        for (i = 0; i < 4; i++) {
 944                data->temp_lim[i].min =
 945                        lm93_read_byte(client, LM93_REG_TEMP_MIN(i));
 946                data->temp_lim[i].max =
 947                        lm93_read_byte(client, LM93_REG_TEMP_MAX(i));
 948        }
 949
 950        /* config register */
 951        data->config = lm93_read_byte(client, LM93_REG_CONFIG);
 952
 953        /* vid1 - vid2: values */
 954        for (i = 0; i < 2; i++)
 955                data->vid[i] = lm93_read_byte(client, LM93_REG_VID(i));
 956
 957        /* prochot1 - prochot2: limits */
 958        for (i = 0; i < 2; i++)
 959                data->prochot_max[i] = lm93_read_byte(client,
 960                                LM93_REG_PROCHOT_MAX(i));
 961
 962        /* vccp1 - vccp2: VID relative limits */
 963        for (i = 0; i < 2; i++)
 964                data->vccp_limits[i] = lm93_read_byte(client,
 965                                LM93_REG_VCCP_LIMIT_OFF(i));
 966
 967        /* GPIO input state */
 968        data->gpi = lm93_read_byte(client, LM93_REG_GPI);
 969
 970        /* #PROCHOT override state */
 971        data->prochot_override = lm93_read_byte(client,
 972                        LM93_REG_PROCHOT_OVERRIDE);
 973
 974        /* #PROCHOT intervals */
 975        data->prochot_interval = lm93_read_byte(client,
 976                        LM93_REG_PROCHOT_INTERVAL);
 977
 978        /* Fan Boost Temperature registers */
 979        for (i = 0; i < 4; i++)
 980                data->boost[i] = lm93_read_byte(client, LM93_REG_BOOST(i));
 981
 982        /* Fan Boost Temperature Hyst. registers */
 983        data->boost_hyst[0] = lm93_read_byte(client, LM93_REG_BOOST_HYST_12);
 984        data->boost_hyst[1] = lm93_read_byte(client, LM93_REG_BOOST_HYST_34);
 985
 986        /* Temperature Zone Min. PWM & Hysteresis registers */
 987        data->auto_pwm_min_hyst[0] =
 988                        lm93_read_byte(client, LM93_REG_PWM_MIN_HYST_12);
 989        data->auto_pwm_min_hyst[1] =
 990                        lm93_read_byte(client, LM93_REG_PWM_MIN_HYST_34);
 991
 992        /* #PROCHOT & #VRDHOT PWM Ramp Control register */
 993        data->pwm_ramp_ctl = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
 994
 995        /* misc setup registers */
 996        data->sfc1 = lm93_read_byte(client, LM93_REG_SFC1);
 997        data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
 998        data->sf_tach_to_pwm = lm93_read_byte(client,
 999                        LM93_REG_SF_TACH_TO_PWM);
1000
1001        /* write back alarm values to clear */
1002        for (i = 0, ptr = (u8 *)(&data->block1); i < 8; i++)
1003                lm93_write_byte(client, LM93_REG_HOST_ERROR_1 + i, *(ptr + i));
1004}
1005
1006/* update routine which uses SMBus block data commands */
1007static void lm93_update_client_full(struct lm93_data *data,
1008                                    struct i2c_client *client)
1009{
1010        dev_dbg(&client->dev, "starting device update (block data enabled)\n");
1011
1012        /* in1 - in16: values & limits */
1013        lm93_read_block(client, 3, (u8 *)(data->block3));
1014        lm93_read_block(client, 7, (u8 *)(data->block7));
1015
1016        /* temp1 - temp4: values */
1017        lm93_read_block(client, 2, (u8 *)(data->block2));
1018
1019        /* prochot1 - prochot2: values */
1020        lm93_read_block(client, 4, (u8 *)(data->block4));
1021
1022        /* fan1 - fan4: values & limits */
1023        lm93_read_block(client, 5, (u8 *)(data->block5));
1024        lm93_read_block(client, 8, (u8 *)(data->block8));
1025
1026        /* pmw control registers */
1027        lm93_read_block(client, 9, (u8 *)(data->block9));
1028
1029        /* alarm values */
1030        lm93_read_block(client, 1, (u8 *)(&data->block1));
1031
1032        /* auto/pwm registers */
1033        lm93_read_block(client, 10, (u8 *)(&data->block10));
1034
1035        lm93_update_client_common(data, client);
1036}
1037
1038/* update routine which uses SMBus byte/word data commands only */
1039static void lm93_update_client_min(struct lm93_data *data,
1040                                   struct i2c_client *client)
1041{
1042        int i, j;
1043        u8 *ptr;
1044
1045        dev_dbg(&client->dev, "starting device update (block data disabled)\n");
1046
1047        /* in1 - in16: values & limits */
1048        for (i = 0; i < 16; i++) {
1049                data->block3[i] =
1050                        lm93_read_byte(client, LM93_REG_IN(i));
1051                data->block7[i].min =
1052                        lm93_read_byte(client, LM93_REG_IN_MIN(i));
1053                data->block7[i].max =
1054                        lm93_read_byte(client, LM93_REG_IN_MAX(i));
1055        }
1056
1057        /* temp1 - temp4: values */
1058        for (i = 0; i < 4; i++) {
1059                data->block2[i] =
1060                        lm93_read_byte(client, LM93_REG_TEMP(i));
1061        }
1062
1063        /* prochot1 - prochot2: values */
1064        for (i = 0; i < 2; i++) {
1065                data->block4[i].cur =
1066                        lm93_read_byte(client, LM93_REG_PROCHOT_CUR(i));
1067                data->block4[i].avg =
1068                        lm93_read_byte(client, LM93_REG_PROCHOT_AVG(i));
1069        }
1070
1071        /* fan1 - fan4: values & limits */
1072        for (i = 0; i < 4; i++) {
1073                data->block5[i] =
1074                        lm93_read_word(client, LM93_REG_FAN(i));
1075                data->block8[i] =
1076                        lm93_read_word(client, LM93_REG_FAN_MIN(i));
1077        }
1078
1079        /* pwm control registers */
1080        for (i = 0; i < 2; i++) {
1081                for (j = 0; j < 4; j++) {
1082                        data->block9[i][j] =
1083                                lm93_read_byte(client, LM93_REG_PWM_CTL(i, j));
1084                }
1085        }
1086
1087        /* alarm values */
1088        for (i = 0, ptr = (u8 *)(&data->block1); i < 8; i++) {
1089                *(ptr + i) =
1090                        lm93_read_byte(client, LM93_REG_HOST_ERROR_1 + i);
1091        }
1092
1093        /* auto/pwm (base temp) registers */
1094        for (i = 0; i < 4; i++) {
1095                data->block10.base[i] =
1096                        lm93_read_byte(client, LM93_REG_TEMP_BASE(i));
1097        }
1098
1099        /* auto/pwm (offset temp) registers */
1100        for (i = 0; i < 12; i++) {
1101                data->block10.offset[i] =
1102                        lm93_read_byte(client, LM93_REG_TEMP_OFFSET(i));
1103        }
1104
1105        lm93_update_client_common(data, client);
1106}
1107
1108/* following are the sysfs callback functions */
1109static ssize_t show_in(struct device *dev, struct device_attribute *attr,
1110                        char *buf)
1111{
1112        int nr = (to_sensor_dev_attr(attr))->index;
1113
1114        struct lm93_data *data = lm93_update_device(dev);
1115        return sprintf(buf, "%d\n", LM93_IN_FROM_REG(nr, data->block3[nr]));
1116}
1117
1118static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 0);
1119static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 1);
1120static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 2);
1121static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 3);
1122static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 4);
1123static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 5);
1124static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 6);
1125static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 7);
1126static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in, NULL, 8);
1127static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in, NULL, 9);
1128static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_in, NULL, 10);
1129static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_in, NULL, 11);
1130static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_in, NULL, 12);
1131static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, show_in, NULL, 13);
1132static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, show_in, NULL, 14);
1133static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in, NULL, 15);
1134
1135static ssize_t show_in_min(struct device *dev,
1136                        struct device_attribute *attr, char *buf)
1137{
1138        int nr = (to_sensor_dev_attr(attr))->index;
1139        struct lm93_data *data = lm93_update_device(dev);
1140        int vccp = nr - 6;
1141        long rc, vid;
1142
1143        if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
1144                vid = LM93_VID_FROM_REG(data->vid[vccp]);
1145                rc = LM93_IN_MIN_FROM_REG(data->vccp_limits[vccp], vid);
1146        } else {
1147                rc = LM93_IN_FROM_REG(nr, data->block7[nr].min);
1148        }
1149        return sprintf(buf, "%ld\n", rc);
1150}
1151
1152static ssize_t store_in_min(struct device *dev, struct device_attribute *attr,
1153                            const char *buf, size_t count)
1154{
1155        int nr = (to_sensor_dev_attr(attr))->index;
1156        struct i2c_client *client = to_i2c_client(dev);
1157        struct lm93_data *data = i2c_get_clientdata(client);
1158        int vccp = nr - 6;
1159        long vid;
1160        unsigned long val;
1161        int err;
1162
1163        err = kstrtoul(buf, 10, &val);
1164        if (err)
1165                return err;
1166
1167        mutex_lock(&data->update_lock);
1168        if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
1169                vid = LM93_VID_FROM_REG(data->vid[vccp]);
1170                data->vccp_limits[vccp] = (data->vccp_limits[vccp] & 0xf0) |
1171                                LM93_IN_REL_TO_REG(val, 0, vid);
1172                lm93_write_byte(client, LM93_REG_VCCP_LIMIT_OFF(vccp),
1173                                data->vccp_limits[vccp]);
1174        } else {
1175                data->block7[nr].min = LM93_IN_TO_REG(nr, val);
1176                lm93_write_byte(client, LM93_REG_IN_MIN(nr),
1177                                data->block7[nr].min);
1178        }
1179        mutex_unlock(&data->update_lock);
1180        return count;
1181}
1182
1183static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
1184                          show_in_min, store_in_min, 0);
1185static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
1186                          show_in_min, store_in_min, 1);
1187static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
1188                          show_in_min, store_in_min, 2);
1189static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
1190                          show_in_min, store_in_min, 3);
1191static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
1192                          show_in_min, store_in_min, 4);
1193static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
1194                          show_in_min, store_in_min, 5);
1195static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO,
1196                          show_in_min, store_in_min, 6);
1197static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO,
1198                          show_in_min, store_in_min, 7);
1199static SENSOR_DEVICE_ATTR(in9_min, S_IWUSR | S_IRUGO,
1200                          show_in_min, store_in_min, 8);
1201static SENSOR_DEVICE_ATTR(in10_min, S_IWUSR | S_IRUGO,
1202                          show_in_min, store_in_min, 9);
1203static SENSOR_DEVICE_ATTR(in11_min, S_IWUSR | S_IRUGO,
1204                          show_in_min, store_in_min, 10);
1205static SENSOR_DEVICE_ATTR(in12_min, S_IWUSR | S_IRUGO,
1206                          show_in_min, store_in_min, 11);
1207static SENSOR_DEVICE_ATTR(in13_min, S_IWUSR | S_IRUGO,
1208                          show_in_min, store_in_min, 12);
1209static SENSOR_DEVICE_ATTR(in14_min, S_IWUSR | S_IRUGO,
1210                          show_in_min, store_in_min, 13);
1211static SENSOR_DEVICE_ATTR(in15_min, S_IWUSR | S_IRUGO,
1212                          show_in_min, store_in_min, 14);
1213static SENSOR_DEVICE_ATTR(in16_min, S_IWUSR | S_IRUGO,
1214                          show_in_min, store_in_min, 15);
1215
1216static ssize_t show_in_max(struct device *dev,
1217                           struct device_attribute *attr, char *buf)
1218{
1219        int nr = (to_sensor_dev_attr(attr))->index;
1220        struct lm93_data *data = lm93_update_device(dev);
1221        int vccp = nr - 6;
1222        long rc, vid;
1223
1224        if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
1225                vid = LM93_VID_FROM_REG(data->vid[vccp]);
1226                rc = LM93_IN_MAX_FROM_REG(data->vccp_limits[vccp], vid);
1227        } else {
1228                rc = LM93_IN_FROM_REG(nr, data->block7[nr].max);
1229        }
1230        return sprintf(buf, "%ld\n", rc);
1231}
1232
1233static ssize_t store_in_max(struct device *dev, struct device_attribute *attr,
1234                            const char *buf, size_t count)
1235{
1236        int nr = (to_sensor_dev_attr(attr))->index;
1237        struct i2c_client *client = to_i2c_client(dev);
1238        struct lm93_data *data = i2c_get_clientdata(client);
1239        int vccp = nr - 6;
1240        long vid;
1241        unsigned long val;
1242        int err;
1243
1244        err = kstrtoul(buf, 10, &val);
1245        if (err)
1246                return err;
1247
1248        mutex_lock(&data->update_lock);
1249        if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
1250                vid = LM93_VID_FROM_REG(data->vid[vccp]);
1251                data->vccp_limits[vccp] = (data->vccp_limits[vccp] & 0x0f) |
1252                                LM93_IN_REL_TO_REG(val, 1, vid);
1253                lm93_write_byte(client, LM93_REG_VCCP_LIMIT_OFF(vccp),
1254                                data->vccp_limits[vccp]);
1255        } else {
1256                data->block7[nr].max = LM93_IN_TO_REG(nr, val);
1257                lm93_write_byte(client, LM93_REG_IN_MAX(nr),
1258                                data->block7[nr].max);
1259        }
1260        mutex_unlock(&data->update_lock);
1261        return count;
1262}
1263
1264static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
1265                          show_in_max, store_in_max, 0);
1266static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
1267                          show_in_max, store_in_max, 1);
1268static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
1269                          show_in_max, store_in_max, 2);
1270static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
1271                          show_in_max, store_in_max, 3);
1272static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
1273                          show_in_max, store_in_max, 4);
1274static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
1275                          show_in_max, store_in_max, 5);
1276static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO,
1277                          show_in_max, store_in_max, 6);
1278static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO,
1279                          show_in_max, store_in_max, 7);
1280static SENSOR_DEVICE_ATTR(in9_max, S_IWUSR | S_IRUGO,
1281                          show_in_max, store_in_max, 8);
1282static SENSOR_DEVICE_ATTR(in10_max, S_IWUSR | S_IRUGO,
1283                          show_in_max, store_in_max, 9);
1284static SENSOR_DEVICE_ATTR(in11_max, S_IWUSR | S_IRUGO,
1285                          show_in_max, store_in_max, 10);
1286static SENSOR_DEVICE_ATTR(in12_max, S_IWUSR | S_IRUGO,
1287                          show_in_max, store_in_max, 11);
1288static SENSOR_DEVICE_ATTR(in13_max, S_IWUSR | S_IRUGO,
1289                          show_in_max, store_in_max, 12);
1290static SENSOR_DEVICE_ATTR(in14_max, S_IWUSR | S_IRUGO,
1291                          show_in_max, store_in_max, 13);
1292static SENSOR_DEVICE_ATTR(in15_max, S_IWUSR | S_IRUGO,
1293                          show_in_max, store_in_max, 14);
1294static SENSOR_DEVICE_ATTR(in16_max, S_IWUSR | S_IRUGO,
1295                          show_in_max, store_in_max, 15);
1296
1297static ssize_t show_temp(struct device *dev,
1298                         struct device_attribute *attr, char *buf)
1299{
1300        int nr = (to_sensor_dev_attr(attr))->index;
1301        struct lm93_data *data = lm93_update_device(dev);
1302        return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->block2[nr]));
1303}
1304
1305static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
1306static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
1307static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1308
1309static ssize_t show_temp_min(struct device *dev,
1310                                struct device_attribute *attr, char *buf)
1311{
1312        int nr = (to_sensor_dev_attr(attr))->index;
1313        struct lm93_data *data = lm93_update_device(dev);
1314        return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->temp_lim[nr].min));
1315}
1316
1317static ssize_t store_temp_min(struct device *dev, struct device_attribute *attr,
1318                              const char *buf, size_t count)
1319{
1320        int nr = (to_sensor_dev_attr(attr))->index;
1321        struct i2c_client *client = to_i2c_client(dev);
1322        struct lm93_data *data = i2c_get_clientdata(client);
1323        long val;
1324        int err;
1325
1326        err = kstrtol(buf, 10, &val);
1327        if (err)
1328                return err;
1329
1330        mutex_lock(&data->update_lock);
1331        data->temp_lim[nr].min = LM93_TEMP_TO_REG(val);
1332        lm93_write_byte(client, LM93_REG_TEMP_MIN(nr), data->temp_lim[nr].min);
1333        mutex_unlock(&data->update_lock);
1334        return count;
1335}
1336
1337static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
1338                          show_temp_min, store_temp_min, 0);
1339static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO,
1340                          show_temp_min, store_temp_min, 1);
1341static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO,
1342                          show_temp_min, store_temp_min, 2);
1343
1344static ssize_t show_temp_max(struct device *dev,
1345                             struct device_attribute *attr, char *buf)
1346{
1347        int nr = (to_sensor_dev_attr(attr))->index;
1348        struct lm93_data *data = lm93_update_device(dev);
1349        return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->temp_lim[nr].max));
1350}
1351
1352static ssize_t store_temp_max(struct device *dev, struct device_attribute *attr,
1353                              const char *buf, size_t count)
1354{
1355        int nr = (to_sensor_dev_attr(attr))->index;
1356        struct i2c_client *client = to_i2c_client(dev);
1357        struct lm93_data *data = i2c_get_clientdata(client);
1358        long val;
1359        int err;
1360
1361        err = kstrtol(buf, 10, &val);
1362        if (err)
1363                return err;
1364
1365        mutex_lock(&data->update_lock);
1366        data->temp_lim[nr].max = LM93_TEMP_TO_REG(val);
1367        lm93_write_byte(client, LM93_REG_TEMP_MAX(nr), data->temp_lim[nr].max);
1368        mutex_unlock(&data->update_lock);
1369        return count;
1370}
1371
1372static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
1373                          show_temp_max, store_temp_max, 0);
1374static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO,
1375                          show_temp_max, store_temp_max, 1);
1376static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO,
1377                          show_temp_max, store_temp_max, 2);
1378
1379static ssize_t show_temp_auto_base(struct device *dev,
1380                                struct device_attribute *attr, char *buf)
1381{
1382        int nr = (to_sensor_dev_attr(attr))->index;
1383        struct lm93_data *data = lm93_update_device(dev);
1384        return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->block10.base[nr]));
1385}
1386
1387static ssize_t store_temp_auto_base(struct device *dev,
1388                                        struct device_attribute *attr,
1389                                        const char *buf, size_t count)
1390{
1391        int nr = (to_sensor_dev_attr(attr))->index;
1392        struct i2c_client *client = to_i2c_client(dev);
1393        struct lm93_data *data = i2c_get_clientdata(client);
1394        long val;
1395        int err;
1396
1397        err = kstrtol(buf, 10, &val);
1398        if (err)
1399                return err;
1400
1401        mutex_lock(&data->update_lock);
1402        data->block10.base[nr] = LM93_TEMP_TO_REG(val);
1403        lm93_write_byte(client, LM93_REG_TEMP_BASE(nr), data->block10.base[nr]);
1404        mutex_unlock(&data->update_lock);
1405        return count;
1406}
1407
1408static SENSOR_DEVICE_ATTR(temp1_auto_base, S_IWUSR | S_IRUGO,
1409                          show_temp_auto_base, store_temp_auto_base, 0);
1410static SENSOR_DEVICE_ATTR(temp2_auto_base, S_IWUSR | S_IRUGO,
1411                          show_temp_auto_base, store_temp_auto_base, 1);
1412static SENSOR_DEVICE_ATTR(temp3_auto_base, S_IWUSR | S_IRUGO,
1413                          show_temp_auto_base, store_temp_auto_base, 2);
1414
1415static ssize_t show_temp_auto_boost(struct device *dev,
1416                                    struct device_attribute *attr, char *buf)
1417{
1418        int nr = (to_sensor_dev_attr(attr))->index;
1419        struct lm93_data *data = lm93_update_device(dev);
1420        return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->boost[nr]));
1421}
1422
1423static ssize_t store_temp_auto_boost(struct device *dev,
1424                                     struct device_attribute *attr,
1425                                     const char *buf, size_t count)
1426{
1427        int nr = (to_sensor_dev_attr(attr))->index;
1428        struct i2c_client *client = to_i2c_client(dev);
1429        struct lm93_data *data = i2c_get_clientdata(client);
1430        long val;
1431        int err;
1432
1433        err = kstrtol(buf, 10, &val);
1434        if (err)
1435                return err;
1436
1437        mutex_lock(&data->update_lock);
1438        data->boost[nr] = LM93_TEMP_TO_REG(val);
1439        lm93_write_byte(client, LM93_REG_BOOST(nr), data->boost[nr]);
1440        mutex_unlock(&data->update_lock);
1441        return count;
1442}
1443
1444static SENSOR_DEVICE_ATTR(temp1_auto_boost, S_IWUSR | S_IRUGO,
1445                          show_temp_auto_boost, store_temp_auto_boost, 0);
1446static SENSOR_DEVICE_ATTR(temp2_auto_boost, S_IWUSR | S_IRUGO,
1447                          show_temp_auto_boost, store_temp_auto_boost, 1);
1448static SENSOR_DEVICE_ATTR(temp3_auto_boost, S_IWUSR | S_IRUGO,
1449                          show_temp_auto_boost, store_temp_auto_boost, 2);
1450
1451static ssize_t show_temp_auto_boost_hyst(struct device *dev,
1452                                         struct device_attribute *attr,
1453                                         char *buf)
1454{
1455        int nr = (to_sensor_dev_attr(attr))->index;
1456        struct lm93_data *data = lm93_update_device(dev);
1457        int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
1458        return sprintf(buf, "%d\n",
1459                       LM93_AUTO_BOOST_HYST_FROM_REGS(data, nr, mode));
1460}
1461
1462static ssize_t store_temp_auto_boost_hyst(struct device *dev,
1463                                          struct device_attribute *attr,
1464                                          const char *buf, size_t count)
1465{
1466        int nr = (to_sensor_dev_attr(attr))->index;
1467        struct i2c_client *client = to_i2c_client(dev);
1468        struct lm93_data *data = i2c_get_clientdata(client);
1469        unsigned long val;
1470        int err;
1471
1472        err = kstrtoul(buf, 10, &val);
1473        if (err)
1474                return err;
1475
1476        mutex_lock(&data->update_lock);
1477        /* force 0.5C/bit mode */
1478        data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1479        data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1480        lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1481        data->boost_hyst[nr/2] = LM93_AUTO_BOOST_HYST_TO_REG(data, val, nr, 1);
1482        lm93_write_byte(client, LM93_REG_BOOST_HYST(nr),
1483                        data->boost_hyst[nr/2]);
1484        mutex_unlock(&data->update_lock);
1485        return count;
1486}
1487
1488static SENSOR_DEVICE_ATTR(temp1_auto_boost_hyst, S_IWUSR | S_IRUGO,
1489                          show_temp_auto_boost_hyst,
1490                          store_temp_auto_boost_hyst, 0);
1491static SENSOR_DEVICE_ATTR(temp2_auto_boost_hyst, S_IWUSR | S_IRUGO,
1492                          show_temp_auto_boost_hyst,
1493                          store_temp_auto_boost_hyst, 1);
1494static SENSOR_DEVICE_ATTR(temp3_auto_boost_hyst, S_IWUSR | S_IRUGO,
1495                          show_temp_auto_boost_hyst,
1496                          store_temp_auto_boost_hyst, 2);
1497
1498static ssize_t show_temp_auto_offset(struct device *dev,
1499                                struct device_attribute *attr, char *buf)
1500{
1501        struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
1502        int nr = s_attr->index;
1503        int ofs = s_attr->nr;
1504        struct lm93_data *data = lm93_update_device(dev);
1505        int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
1506        return sprintf(buf, "%d\n",
1507               LM93_TEMP_AUTO_OFFSET_FROM_REG(data->block10.offset[ofs],
1508                                              nr, mode));
1509}
1510
1511static ssize_t store_temp_auto_offset(struct device *dev,
1512                                        struct device_attribute *attr,
1513                                        const char *buf, size_t count)
1514{
1515        struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
1516        int nr = s_attr->index;
1517        int ofs = s_attr->nr;
1518        struct i2c_client *client = to_i2c_client(dev);
1519        struct lm93_data *data = i2c_get_clientdata(client);
1520        unsigned long val;
1521        int err;
1522
1523        err = kstrtoul(buf, 10, &val);
1524        if (err)
1525                return err;
1526
1527        mutex_lock(&data->update_lock);
1528        /* force 0.5C/bit mode */
1529        data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1530        data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1531        lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1532        data->block10.offset[ofs] = LM93_TEMP_AUTO_OFFSET_TO_REG(
1533                        data->block10.offset[ofs], val, nr, 1);
1534        lm93_write_byte(client, LM93_REG_TEMP_OFFSET(ofs),
1535                        data->block10.offset[ofs]);
1536        mutex_unlock(&data->update_lock);
1537        return count;
1538}
1539
1540static SENSOR_DEVICE_ATTR_2(temp1_auto_offset1, S_IWUSR | S_IRUGO,
1541                          show_temp_auto_offset, store_temp_auto_offset, 0, 0);
1542static SENSOR_DEVICE_ATTR_2(temp1_auto_offset2, S_IWUSR | S_IRUGO,
1543                          show_temp_auto_offset, store_temp_auto_offset, 1, 0);
1544static SENSOR_DEVICE_ATTR_2(temp1_auto_offset3, S_IWUSR | S_IRUGO,
1545                          show_temp_auto_offset, store_temp_auto_offset, 2, 0);
1546static SENSOR_DEVICE_ATTR_2(temp1_auto_offset4, S_IWUSR | S_IRUGO,
1547                          show_temp_auto_offset, store_temp_auto_offset, 3, 0);
1548static SENSOR_DEVICE_ATTR_2(temp1_auto_offset5, S_IWUSR | S_IRUGO,
1549                          show_temp_auto_offset, store_temp_auto_offset, 4, 0);
1550static SENSOR_DEVICE_ATTR_2(temp1_auto_offset6, S_IWUSR | S_IRUGO,
1551                          show_temp_auto_offset, store_temp_auto_offset, 5, 0);
1552static SENSOR_DEVICE_ATTR_2(temp1_auto_offset7, S_IWUSR | S_IRUGO,
1553                          show_temp_auto_offset, store_temp_auto_offset, 6, 0);
1554static SENSOR_DEVICE_ATTR_2(temp1_auto_offset8, S_IWUSR | S_IRUGO,
1555                          show_temp_auto_offset, store_temp_auto_offset, 7, 0);
1556static SENSOR_DEVICE_ATTR_2(temp1_auto_offset9, S_IWUSR | S_IRUGO,
1557                          show_temp_auto_offset, store_temp_auto_offset, 8, 0);
1558static SENSOR_DEVICE_ATTR_2(temp1_auto_offset10, S_IWUSR | S_IRUGO,
1559                          show_temp_auto_offset, store_temp_auto_offset, 9, 0);
1560static SENSOR_DEVICE_ATTR_2(temp1_auto_offset11, S_IWUSR | S_IRUGO,
1561                          show_temp_auto_offset, store_temp_auto_offset, 10, 0);
1562static SENSOR_DEVICE_ATTR_2(temp1_auto_offset12, S_IWUSR | S_IRUGO,
1563                          show_temp_auto_offset, store_temp_auto_offset, 11, 0);
1564static SENSOR_DEVICE_ATTR_2(temp2_auto_offset1, S_IWUSR | S_IRUGO,
1565                          show_temp_auto_offset, store_temp_auto_offset, 0, 1);
1566static SENSOR_DEVICE_ATTR_2(temp2_auto_offset2, S_IWUSR | S_IRUGO,
1567                          show_temp_auto_offset, store_temp_auto_offset, 1, 1);
1568static SENSOR_DEVICE_ATTR_2(temp2_auto_offset3, S_IWUSR | S_IRUGO,
1569                          show_temp_auto_offset, store_temp_auto_offset, 2, 1);
1570static SENSOR_DEVICE_ATTR_2(temp2_auto_offset4, S_IWUSR | S_IRUGO,
1571                          show_temp_auto_offset, store_temp_auto_offset, 3, 1);
1572static SENSOR_DEVICE_ATTR_2(temp2_auto_offset5, S_IWUSR | S_IRUGO,
1573                          show_temp_auto_offset, store_temp_auto_offset, 4, 1);
1574static SENSOR_DEVICE_ATTR_2(temp2_auto_offset6, S_IWUSR | S_IRUGO,
1575                          show_temp_auto_offset, store_temp_auto_offset, 5, 1);
1576static SENSOR_DEVICE_ATTR_2(temp2_auto_offset7, S_IWUSR | S_IRUGO,
1577                          show_temp_auto_offset, store_temp_auto_offset, 6, 1);
1578static SENSOR_DEVICE_ATTR_2(temp2_auto_offset8, S_IWUSR | S_IRUGO,
1579                          show_temp_auto_offset, store_temp_auto_offset, 7, 1);
1580static SENSOR_DEVICE_ATTR_2(temp2_auto_offset9, S_IWUSR | S_IRUGO,
1581                          show_temp_auto_offset, store_temp_auto_offset, 8, 1);
1582static SENSOR_DEVICE_ATTR_2(temp2_auto_offset10, S_IWUSR | S_IRUGO,
1583                          show_temp_auto_offset, store_temp_auto_offset, 9, 1);
1584static SENSOR_DEVICE_ATTR_2(temp2_auto_offset11, S_IWUSR | S_IRUGO,
1585                          show_temp_auto_offset, store_temp_auto_offset, 10, 1);
1586static SENSOR_DEVICE_ATTR_2(temp2_auto_offset12, S_IWUSR | S_IRUGO,
1587                          show_temp_auto_offset, store_temp_auto_offset, 11, 1);
1588static SENSOR_DEVICE_ATTR_2(temp3_auto_offset1, S_IWUSR | S_IRUGO,
1589                          show_temp_auto_offset, store_temp_auto_offset, 0, 2);
1590static SENSOR_DEVICE_ATTR_2(temp3_auto_offset2, S_IWUSR | S_IRUGO,
1591                          show_temp_auto_offset, store_temp_auto_offset, 1, 2);
1592static SENSOR_DEVICE_ATTR_2(temp3_auto_offset3, S_IWUSR | S_IRUGO,
1593                          show_temp_auto_offset, store_temp_auto_offset, 2, 2);
1594static SENSOR_DEVICE_ATTR_2(temp3_auto_offset4, S_IWUSR | S_IRUGO,
1595                          show_temp_auto_offset, store_temp_auto_offset, 3, 2);
1596static SENSOR_DEVICE_ATTR_2(temp3_auto_offset5, S_IWUSR | S_IRUGO,
1597                          show_temp_auto_offset, store_temp_auto_offset, 4, 2);
1598static SENSOR_DEVICE_ATTR_2(temp3_auto_offset6, S_IWUSR | S_IRUGO,
1599                          show_temp_auto_offset, store_temp_auto_offset, 5, 2);
1600static SENSOR_DEVICE_ATTR_2(temp3_auto_offset7, S_IWUSR | S_IRUGO,
1601                          show_temp_auto_offset, store_temp_auto_offset, 6, 2);
1602static SENSOR_DEVICE_ATTR_2(temp3_auto_offset8, S_IWUSR | S_IRUGO,
1603                          show_temp_auto_offset, store_temp_auto_offset, 7, 2);
1604static SENSOR_DEVICE_ATTR_2(temp3_auto_offset9, S_IWUSR | S_IRUGO,
1605                          show_temp_auto_offset, store_temp_auto_offset, 8, 2);
1606static SENSOR_DEVICE_ATTR_2(temp3_auto_offset10, S_IWUSR | S_IRUGO,
1607                          show_temp_auto_offset, store_temp_auto_offset, 9, 2);
1608static SENSOR_DEVICE_ATTR_2(temp3_auto_offset11, S_IWUSR | S_IRUGO,
1609                          show_temp_auto_offset, store_temp_auto_offset, 10, 2);
1610static SENSOR_DEVICE_ATTR_2(temp3_auto_offset12, S_IWUSR | S_IRUGO,
1611                          show_temp_auto_offset, store_temp_auto_offset, 11, 2);
1612
1613static ssize_t show_temp_auto_pwm_min(struct device *dev,
1614                                struct device_attribute *attr, char *buf)
1615{
1616        int nr = (to_sensor_dev_attr(attr))->index;
1617        u8 reg, ctl4;
1618        struct lm93_data *data = lm93_update_device(dev);
1619        reg = data->auto_pwm_min_hyst[nr/2] >> 4 & 0x0f;
1620        ctl4 = data->block9[nr][LM93_PWM_CTL4];
1621        return sprintf(buf, "%d\n", LM93_PWM_FROM_REG(reg, (ctl4 & 0x07) ?
1622                                LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
1623}
1624
1625static ssize_t store_temp_auto_pwm_min(struct device *dev,
1626                                        struct device_attribute *attr,
1627                                        const char *buf, size_t count)
1628{
1629        int nr = (to_sensor_dev_attr(attr))->index;
1630        struct i2c_client *client = to_i2c_client(dev);
1631        struct lm93_data *data = i2c_get_clientdata(client);
1632        u8 reg, ctl4;
1633        unsigned long val;
1634        int err;
1635
1636        err = kstrtoul(buf, 10, &val);
1637        if (err)
1638                return err;
1639
1640        mutex_lock(&data->update_lock);
1641        reg = lm93_read_byte(client, LM93_REG_PWM_MIN_HYST(nr));
1642        ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
1643        reg = (reg & 0x0f) |
1644                LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
1645                                LM93_PWM_MAP_LO_FREQ :
1646                                LM93_PWM_MAP_HI_FREQ) << 4;
1647        data->auto_pwm_min_hyst[nr/2] = reg;
1648        lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
1649        mutex_unlock(&data->update_lock);
1650        return count;
1651}
1652
1653static SENSOR_DEVICE_ATTR(temp1_auto_pwm_min, S_IWUSR | S_IRUGO,
1654                          show_temp_auto_pwm_min,
1655                          store_temp_auto_pwm_min, 0);
1656static SENSOR_DEVICE_ATTR(temp2_auto_pwm_min, S_IWUSR | S_IRUGO,
1657                          show_temp_auto_pwm_min,
1658                          store_temp_auto_pwm_min, 1);
1659static SENSOR_DEVICE_ATTR(temp3_auto_pwm_min, S_IWUSR | S_IRUGO,
1660                          show_temp_auto_pwm_min,
1661                          store_temp_auto_pwm_min, 2);
1662
1663static ssize_t show_temp_auto_offset_hyst(struct device *dev,
1664                                struct device_attribute *attr, char *buf)
1665{
1666        int nr = (to_sensor_dev_attr(attr))->index;
1667        struct lm93_data *data = lm93_update_device(dev);
1668        int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
1669        return sprintf(buf, "%d\n", LM93_TEMP_OFFSET_FROM_REG(
1670                                        data->auto_pwm_min_hyst[nr / 2], mode));
1671}
1672
1673static ssize_t store_temp_auto_offset_hyst(struct device *dev,
1674                                                struct device_attribute *attr,
1675                                                const char *buf, size_t count)
1676{
1677        int nr = (to_sensor_dev_attr(attr))->index;
1678        struct i2c_client *client = to_i2c_client(dev);
1679        struct lm93_data *data = i2c_get_clientdata(client);
1680        u8 reg;
1681        unsigned long val;
1682        int err;
1683
1684        err = kstrtoul(buf, 10, &val);
1685        if (err)
1686                return err;
1687
1688        mutex_lock(&data->update_lock);
1689        /* force 0.5C/bit mode */
1690        data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1691        data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1692        lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1693        reg = data->auto_pwm_min_hyst[nr/2];
1694        reg = (reg & 0xf0) | (LM93_TEMP_OFFSET_TO_REG(val, 1) & 0x0f);
1695        data->auto_pwm_min_hyst[nr/2] = reg;
1696        lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
1697        mutex_unlock(&data->update_lock);
1698        return count;
1699}
1700
1701static SENSOR_DEVICE_ATTR(temp1_auto_offset_hyst, S_IWUSR | S_IRUGO,
1702                          show_temp_auto_offset_hyst,
1703                          store_temp_auto_offset_hyst, 0);
1704static SENSOR_DEVICE_ATTR(temp2_auto_offset_hyst, S_IWUSR | S_IRUGO,
1705                          show_temp_auto_offset_hyst,
1706                          store_temp_auto_offset_hyst, 1);
1707static SENSOR_DEVICE_ATTR(temp3_auto_offset_hyst, S_IWUSR | S_IRUGO,
1708                          show_temp_auto_offset_hyst,
1709                          store_temp_auto_offset_hyst, 2);
1710
1711static ssize_t show_fan_input(struct device *dev,
1712                struct device_attribute *attr, char *buf)
1713{
1714        struct sensor_device_attribute *s_attr = to_sensor_dev_attr(attr);
1715        int nr = s_attr->index;
1716        struct lm93_data *data = lm93_update_device(dev);
1717
1718        return sprintf(buf, "%d\n", LM93_FAN_FROM_REG(data->block5[nr]));
1719}
1720
1721static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
1722static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
1723static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2);
1724static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_input, NULL, 3);
1725
1726static ssize_t show_fan_min(struct device *dev,
1727                              struct device_attribute *attr, char *buf)
1728{
1729        int nr = (to_sensor_dev_attr(attr))->index;
1730        struct lm93_data *data = lm93_update_device(dev);
1731
1732        return sprintf(buf, "%d\n", LM93_FAN_FROM_REG(data->block8[nr]));
1733}
1734
1735static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr,
1736                                const char *buf, size_t count)
1737{
1738        int nr = (to_sensor_dev_attr(attr))->index;
1739        struct i2c_client *client = to_i2c_client(dev);
1740        struct lm93_data *data = i2c_get_clientdata(client);
1741        unsigned long val;
1742        int err;
1743
1744        err = kstrtoul(buf, 10, &val);
1745        if (err)
1746                return err;
1747
1748        mutex_lock(&data->update_lock);
1749        data->block8[nr] = LM93_FAN_TO_REG(val);
1750        lm93_write_word(client, LM93_REG_FAN_MIN(nr), data->block8[nr]);
1751        mutex_unlock(&data->update_lock);
1752        return count;
1753}
1754
1755static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
1756                          show_fan_min, store_fan_min, 0);
1757static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
1758                          show_fan_min, store_fan_min, 1);
1759static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO,
1760                          show_fan_min, store_fan_min, 2);
1761static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO,
1762                          show_fan_min, store_fan_min, 3);
1763
1764/*
1765 * some tedious bit-twiddling here to deal with the register format:
1766 *
1767 *      data->sf_tach_to_pwm: (tach to pwm mapping bits)
1768 *
1769 *              bit |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0
1770 *                   T4:P2 T4:P1 T3:P2 T3:P1 T2:P2 T2:P1 T1:P2 T1:P1
1771 *
1772 *      data->sfc2: (enable bits)
1773 *
1774 *              bit |  3  |  2  |  1  |  0
1775 *                     T4    T3    T2    T1
1776 */
1777
1778static ssize_t show_fan_smart_tach(struct device *dev,
1779                                struct device_attribute *attr, char *buf)
1780{
1781        int nr = (to_sensor_dev_attr(attr))->index;
1782        struct lm93_data *data = lm93_update_device(dev);
1783        long rc = 0;
1784        int mapping;
1785
1786        /* extract the relevant mapping */
1787        mapping = (data->sf_tach_to_pwm >> (nr * 2)) & 0x03;
1788
1789        /* if there's a mapping and it's enabled */
1790        if (mapping && ((data->sfc2 >> nr) & 0x01))
1791                rc = mapping;
1792        return sprintf(buf, "%ld\n", rc);
1793}
1794
1795/*
1796 * helper function - must grab data->update_lock before calling
1797 * fan is 0-3, indicating fan1-fan4
1798 */
1799static void lm93_write_fan_smart_tach(struct i2c_client *client,
1800        struct lm93_data *data, int fan, long value)
1801{
1802        /* insert the new mapping and write it out */
1803        data->sf_tach_to_pwm = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
1804        data->sf_tach_to_pwm &= ~(0x3 << fan * 2);
1805        data->sf_tach_to_pwm |= value << fan * 2;
1806        lm93_write_byte(client, LM93_REG_SF_TACH_TO_PWM, data->sf_tach_to_pwm);
1807
1808        /* insert the enable bit and write it out */
1809        data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1810        if (value)
1811                data->sfc2 |= 1 << fan;
1812        else
1813                data->sfc2 &= ~(1 << fan);
1814        lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1815}
1816
1817static ssize_t store_fan_smart_tach(struct device *dev,
1818                                        struct device_attribute *attr,
1819                                        const char *buf, size_t count)
1820{
1821        int nr = (to_sensor_dev_attr(attr))->index;
1822        struct i2c_client *client = to_i2c_client(dev);
1823        struct lm93_data *data = i2c_get_clientdata(client);
1824        unsigned long val;
1825        int err;
1826
1827        err = kstrtoul(buf, 10, &val);
1828        if (err)
1829                return err;
1830
1831        mutex_lock(&data->update_lock);
1832        /* sanity test, ignore the write otherwise */
1833        if (0 <= val && val <= 2) {
1834                /* can't enable if pwm freq is 22.5KHz */
1835                if (val) {
1836                        u8 ctl4 = lm93_read_byte(client,
1837                                LM93_REG_PWM_CTL(val - 1, LM93_PWM_CTL4));
1838                        if ((ctl4 & 0x07) == 0)
1839                                val = 0;
1840                }
1841                lm93_write_fan_smart_tach(client, data, nr, val);
1842        }
1843        mutex_unlock(&data->update_lock);
1844        return count;
1845}
1846
1847static SENSOR_DEVICE_ATTR(fan1_smart_tach, S_IWUSR | S_IRUGO,
1848                          show_fan_smart_tach, store_fan_smart_tach, 0);
1849static SENSOR_DEVICE_ATTR(fan2_smart_tach, S_IWUSR | S_IRUGO,
1850                          show_fan_smart_tach, store_fan_smart_tach, 1);
1851static SENSOR_DEVICE_ATTR(fan3_smart_tach, S_IWUSR | S_IRUGO,
1852                          show_fan_smart_tach, store_fan_smart_tach, 2);
1853static SENSOR_DEVICE_ATTR(fan4_smart_tach, S_IWUSR | S_IRUGO,
1854                          show_fan_smart_tach, store_fan_smart_tach, 3);
1855
1856static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1857                        char *buf)
1858{
1859        int nr = (to_sensor_dev_attr(attr))->index;
1860        struct lm93_data *data = lm93_update_device(dev);
1861        u8 ctl2, ctl4;
1862        long rc;
1863
1864        ctl2 = data->block9[nr][LM93_PWM_CTL2];
1865        ctl4 = data->block9[nr][LM93_PWM_CTL4];
1866        if (ctl2 & 0x01) /* show user commanded value if enabled */
1867                rc = data->pwm_override[nr];
1868        else /* show present h/w value if manual pwm disabled */
1869                rc = LM93_PWM_FROM_REG(ctl2 >> 4, (ctl4 & 0x07) ?
1870                        LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ);
1871        return sprintf(buf, "%ld\n", rc);
1872}
1873
1874static ssize_t store_pwm(struct device *dev, struct device_attribute *attr,
1875                                const char *buf, size_t count)
1876{
1877        int nr = (to_sensor_dev_attr(attr))->index;
1878        struct i2c_client *client = to_i2c_client(dev);
1879        struct lm93_data *data = i2c_get_clientdata(client);
1880        u8 ctl2, ctl4;
1881        unsigned long val;
1882        int err;
1883
1884        err = kstrtoul(buf, 10, &val);
1885        if (err)
1886                return err;
1887
1888        mutex_lock(&data->update_lock);
1889        ctl2 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2));
1890        ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
1891        ctl2 = (ctl2 & 0x0f) | LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
1892                        LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ) << 4;
1893        /* save user commanded value */
1894        data->pwm_override[nr] = LM93_PWM_FROM_REG(ctl2 >> 4,
1895                        (ctl4 & 0x07) ?  LM93_PWM_MAP_LO_FREQ :
1896                        LM93_PWM_MAP_HI_FREQ);
1897        lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2), ctl2);
1898        mutex_unlock(&data->update_lock);
1899        return count;
1900}
1901
1902static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0);
1903static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1);
1904
1905static ssize_t show_pwm_enable(struct device *dev,
1906                                struct device_attribute *attr, char *buf)
1907{
1908        int nr = (to_sensor_dev_attr(attr))->index;
1909        struct lm93_data *data = lm93_update_device(dev);
1910        u8 ctl2;
1911        long rc;
1912
1913        ctl2 = data->block9[nr][LM93_PWM_CTL2];
1914        if (ctl2 & 0x01) /* manual override enabled ? */
1915                rc = ((ctl2 & 0xF0) == 0xF0) ? 0 : 1;
1916        else
1917                rc = 2;
1918        return sprintf(buf, "%ld\n", rc);
1919}
1920
1921static ssize_t store_pwm_enable(struct device *dev,
1922                                struct device_attribute *attr,
1923                                const char *buf, size_t count)
1924{
1925        int nr = (to_sensor_dev_attr(attr))->index;
1926        struct i2c_client *client = to_i2c_client(dev);
1927        struct lm93_data *data = i2c_get_clientdata(client);
1928        u8 ctl2;
1929        unsigned long val;
1930        int err;
1931
1932        err = kstrtoul(buf, 10, &val);
1933        if (err)
1934                return err;
1935
1936        mutex_lock(&data->update_lock);
1937        ctl2 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2));
1938
1939        switch (val) {
1940        case 0:
1941                ctl2 |= 0xF1; /* enable manual override, set PWM to max */
1942                break;
1943        case 1:
1944                ctl2 |= 0x01; /* enable manual override */
1945                break;
1946        case 2:
1947                ctl2 &= ~0x01; /* disable manual override */
1948                break;
1949        default:
1950                mutex_unlock(&data->update_lock);
1951                return -EINVAL;
1952        }
1953
1954        lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2), ctl2);
1955        mutex_unlock(&data->update_lock);
1956        return count;
1957}
1958
1959static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
1960                                show_pwm_enable, store_pwm_enable, 0);
1961static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
1962                                show_pwm_enable, store_pwm_enable, 1);
1963
1964static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1965                                char *buf)
1966{
1967        int nr = (to_sensor_dev_attr(attr))->index;
1968        struct lm93_data *data = lm93_update_device(dev);
1969        u8 ctl4;
1970
1971        ctl4 = data->block9[nr][LM93_PWM_CTL4];
1972        return sprintf(buf, "%d\n", LM93_PWM_FREQ_FROM_REG(ctl4));
1973}
1974
1975/*
1976 * helper function - must grab data->update_lock before calling
1977 * pwm is 0-1, indicating pwm1-pwm2
1978 * this disables smart tach for all tach channels bound to the given pwm
1979 */
1980static void lm93_disable_fan_smart_tach(struct i2c_client *client,
1981        struct lm93_data *data, int pwm)
1982{
1983        int mapping = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
1984        int mask;
1985
1986        /* collapse the mapping into a mask of enable bits */
1987        mapping = (mapping >> pwm) & 0x55;
1988        mask = mapping & 0x01;
1989        mask |= (mapping & 0x04) >> 1;
1990        mask |= (mapping & 0x10) >> 2;
1991        mask |= (mapping & 0x40) >> 3;
1992
1993        /* disable smart tach according to the mask */
1994        data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1995        data->sfc2 &= ~mask;
1996        lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1997}
1998
1999static ssize_t store_pwm_freq(struct device *dev,
2000                                struct device_attribute *attr,
2001                                const char *buf, size_t count)
2002{
2003        int nr = (to_sensor_dev_attr(attr))->index;
2004        struct i2c_client *client = to_i2c_client(dev);
2005        struct lm93_data *data = i2c_get_clientdata(client);
2006        u8 ctl4;
2007        unsigned long val;
2008        int err;
2009
2010        err = kstrtoul(buf, 10, &val);
2011        if (err)
2012                return err;
2013
2014        mutex_lock(&data->update_lock);
2015        ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
2016        ctl4 = (ctl4 & 0xf8) | LM93_PWM_FREQ_TO_REG(val);
2017        data->block9[nr][LM93_PWM_CTL4] = ctl4;
2018        /* ctl4 == 0 -> 22.5KHz -> disable smart tach */
2019        if (!ctl4)
2020                lm93_disable_fan_smart_tach(client, data, nr);
2021        lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4), ctl4);
2022        mutex_unlock(&data->update_lock);
2023        return count;
2024}
2025
2026static SENSOR_DEVICE_ATTR(pwm1_freq, S_IWUSR | S_IRUGO,
2027                          show_pwm_freq, store_pwm_freq, 0);
2028static SENSOR_DEVICE_ATTR(pwm2_freq, S_IWUSR | S_IRUGO,
2029                          show_pwm_freq, store_pwm_freq, 1);
2030
2031static ssize_t show_pwm_auto_channels(struct device *dev,
2032                                struct device_attribute *attr, char *buf)
2033{
2034        int nr = (to_sensor_dev_attr(attr))->index;
2035        struct lm93_data *data = lm93_update_device(dev);
2036        return sprintf(buf, "%d\n", data->block9[nr][LM93_PWM_CTL1]);
2037}
2038
2039static ssize_t store_pwm_auto_channels(struct device *dev,
2040                                        struct device_attribute *attr,
2041                                        const char *buf, size_t count)
2042{
2043        int nr = (to_sensor_dev_attr(attr))->index;
2044        struct i2c_client *client = to_i2c_client(dev);
2045        struct lm93_data *data = i2c_get_clientdata(client);
2046        unsigned long val;
2047        int err;
2048
2049        err = kstrtoul(buf, 10, &val);
2050        if (err)
2051                return err;
2052
2053        mutex_lock(&data->update_lock);
2054        data->block9[nr][LM93_PWM_CTL1] = SENSORS_LIMIT(val, 0, 255);
2055        lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL1),
2056                                data->block9[nr][LM93_PWM_CTL1]);
2057        mutex_unlock(&data->update_lock);
2058        return count;
2059}
2060
2061static SENSOR_DEVICE_ATTR(pwm1_auto_channels, S_IWUSR | S_IRUGO,
2062                          show_pwm_auto_channels, store_pwm_auto_channels, 0);
2063static SENSOR_DEVICE_ATTR(pwm2_auto_channels, S_IWUSR | S_IRUGO,
2064                          show_pwm_auto_channels, store_pwm_auto_channels, 1);
2065
2066static ssize_t show_pwm_auto_spinup_min(struct device *dev,
2067                                struct device_attribute *attr, char *buf)
2068{
2069        int nr = (to_sensor_dev_attr(attr))->index;
2070        struct lm93_data *data = lm93_update_device(dev);
2071        u8 ctl3, ctl4;
2072
2073        ctl3 = data->block9[nr][LM93_PWM_CTL3];
2074        ctl4 = data->block9[nr][LM93_PWM_CTL4];
2075        return sprintf(buf, "%d\n",
2076                       LM93_PWM_FROM_REG(ctl3 & 0x0f, (ctl4 & 0x07) ?
2077                        LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
2078}
2079
2080static ssize_t store_pwm_auto_spinup_min(struct device *dev,
2081                                                struct device_attribute *attr,
2082                                                const char *buf, size_t count)
2083{
2084        int nr = (to_sensor_dev_attr(attr))->index;
2085        struct i2c_client *client = to_i2c_client(dev);
2086        struct lm93_data *data = i2c_get_clientdata(client);
2087        u8 ctl3, ctl4;
2088        unsigned long val;
2089        int err;
2090
2091        err = kstrtoul(buf, 10, &val);
2092        if (err)
2093                return err;
2094
2095        mutex_lock(&data->update_lock);
2096        ctl3 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
2097        ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
2098        ctl3 = (ctl3 & 0xf0) | LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
2099                        LM93_PWM_MAP_LO_FREQ :
2100                        LM93_PWM_MAP_HI_FREQ);
2101        data->block9[nr][LM93_PWM_CTL3] = ctl3;
2102        lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
2103        mutex_unlock(&data->update_lock);
2104        return count;
2105}
2106
2107static SENSOR_DEVICE_ATTR(pwm1_auto_spinup_min, S_IWUSR | S_IRUGO,
2108                          show_pwm_auto_spinup_min,
2109                          store_pwm_auto_spinup_min, 0);
2110static SENSOR_DEVICE_ATTR(pwm2_auto_spinup_min, S_IWUSR | S_IRUGO,
2111                          show_pwm_auto_spinup_min,
2112                          store_pwm_auto_spinup_min, 1);
2113
2114static ssize_t show_pwm_auto_spinup_time(struct device *dev,
2115                                struct device_attribute *attr, char *buf)
2116{
2117        int nr = (to_sensor_dev_attr(attr))->index;
2118        struct lm93_data *data = lm93_update_device(dev);
2119        return sprintf(buf, "%d\n", LM93_SPINUP_TIME_FROM_REG(
2120                                data->block9[nr][LM93_PWM_CTL3]));
2121}
2122
2123static ssize_t store_pwm_auto_spinup_time(struct device *dev,
2124                                                struct device_attribute *attr,
2125                                                const char *buf, size_t count)
2126{
2127        int nr = (to_sensor_dev_attr(attr))->index;
2128        struct i2c_client *client = to_i2c_client(dev);
2129        struct lm93_data *data = i2c_get_clientdata(client);
2130        u8 ctl3;
2131        unsigned long val;
2132        int err;
2133
2134        err = kstrtoul(buf, 10, &val);
2135        if (err)
2136                return err;
2137
2138        mutex_lock(&data->update_lock);
2139        ctl3 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
2140        ctl3 = (ctl3 & 0x1f) | (LM93_SPINUP_TIME_TO_REG(val) << 5 & 0xe0);
2141        data->block9[nr][LM93_PWM_CTL3] = ctl3;
2142        lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
2143        mutex_unlock(&data->update_lock);
2144        return count;
2145}
2146
2147static SENSOR_DEVICE_ATTR(pwm1_auto_spinup_time, S_IWUSR | S_IRUGO,
2148                          show_pwm_auto_spinup_time,
2149                          store_pwm_auto_spinup_time, 0);
2150static SENSOR_DEVICE_ATTR(pwm2_auto_spinup_time, S_IWUSR | S_IRUGO,
2151                          show_pwm_auto_spinup_time,
2152                          store_pwm_auto_spinup_time, 1);
2153
2154static ssize_t show_pwm_auto_prochot_ramp(struct device *dev,
2155                                struct device_attribute *attr, char *buf)
2156{
2157        struct lm93_data *data = lm93_update_device(dev);
2158        return sprintf(buf, "%d\n",
2159                       LM93_RAMP_FROM_REG(data->pwm_ramp_ctl >> 4 & 0x0f));
2160}
2161
2162static ssize_t store_pwm_auto_prochot_ramp(struct device *dev,
2163                                                struct device_attribute *attr,
2164                                                const char *buf, size_t count)
2165{
2166        struct i2c_client *client = to_i2c_client(dev);
2167        struct lm93_data *data = i2c_get_clientdata(client);
2168        u8 ramp;
2169        unsigned long val;
2170        int err;
2171
2172        err = kstrtoul(buf, 10, &val);
2173        if (err)
2174                return err;
2175
2176        mutex_lock(&data->update_lock);
2177        ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
2178        ramp = (ramp & 0x0f) | (LM93_RAMP_TO_REG(val) << 4 & 0xf0);
2179        lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
2180        mutex_unlock(&data->update_lock);
2181        return count;
2182}
2183
2184static DEVICE_ATTR(pwm_auto_prochot_ramp, S_IRUGO | S_IWUSR,
2185                        show_pwm_auto_prochot_ramp,
2186                        store_pwm_auto_prochot_ramp);
2187
2188static ssize_t show_pwm_auto_vrdhot_ramp(struct device *dev,
2189                                struct device_attribute *attr, char *buf)
2190{
2191        struct lm93_data *data = lm93_update_device(dev);
2192        return sprintf(buf, "%d\n",
2193                       LM93_RAMP_FROM_REG(data->pwm_ramp_ctl & 0x0f));
2194}
2195
2196static ssize_t store_pwm_auto_vrdhot_ramp(struct device *dev,
2197                                                struct device_attribute *attr,
2198                                                const char *buf, size_t count)
2199{
2200        struct i2c_client *client = to_i2c_client(dev);
2201        struct lm93_data *data = i2c_get_clientdata(client);
2202        u8 ramp;
2203        unsigned long val;
2204        int err;
2205
2206        err = kstrtoul(buf, 10, &val);
2207        if (err)
2208                return err;
2209
2210        mutex_lock(&data->update_lock);
2211        ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
2212        ramp = (ramp & 0xf0) | (LM93_RAMP_TO_REG(val) & 0x0f);
2213        lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
2214        mutex_unlock(&data->update_lock);
2215        return 0;
2216}
2217
2218static DEVICE_ATTR(pwm_auto_vrdhot_ramp, S_IRUGO | S_IWUSR,
2219                        show_pwm_auto_vrdhot_ramp,
2220                        store_pwm_auto_vrdhot_ramp);
2221
2222static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
2223                        char *buf)
2224{
2225        int nr = (to_sensor_dev_attr(attr))->index;
2226        struct lm93_data *data = lm93_update_device(dev);
2227        return sprintf(buf, "%d\n", LM93_VID_FROM_REG(data->vid[nr]));
2228}
2229
2230static SENSOR_DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL, 0);
2231static SENSOR_DEVICE_ATTR(cpu1_vid, S_IRUGO, show_vid, NULL, 1);
2232
2233static ssize_t show_prochot(struct device *dev, struct device_attribute *attr,
2234                                char *buf)
2235{
2236        int nr = (to_sensor_dev_attr(attr))->index;
2237        struct lm93_data *data = lm93_update_device(dev);
2238        return sprintf(buf, "%d\n", data->block4[nr].cur);
2239}
2240
2241static SENSOR_DEVICE_ATTR(prochot1, S_IRUGO, show_prochot, NULL, 0);
2242static SENSOR_DEVICE_ATTR(prochot2, S_IRUGO, show_prochot, NULL, 1);
2243
2244static ssize_t show_prochot_avg(struct device *dev,
2245                                struct device_attribute *attr, char *buf)
2246{
2247        int nr = (to_sensor_dev_attr(attr))->index;
2248        struct lm93_data *data = lm93_update_device(dev);
2249        return sprintf(buf, "%d\n", data->block4[nr].avg);
2250}
2251
2252static SENSOR_DEVICE_ATTR(prochot1_avg, S_IRUGO, show_prochot_avg, NULL, 0);
2253static SENSOR_DEVICE_ATTR(prochot2_avg, S_IRUGO, show_prochot_avg, NULL, 1);
2254
2255static ssize_t show_prochot_max(struct device *dev,
2256                                struct device_attribute *attr, char *buf)
2257{
2258        int nr = (to_sensor_dev_attr(attr))->index;
2259        struct lm93_data *data = lm93_update_device(dev);
2260        return sprintf(buf, "%d\n", data->prochot_max[nr]);
2261}
2262
2263static ssize_t store_prochot_max(struct device *dev,
2264                                        struct device_attribute *attr,
2265                                        const char *buf, size_t count)
2266{
2267        int nr = (to_sensor_dev_attr(attr))->index;
2268        struct i2c_client *client = to_i2c_client(dev);
2269        struct lm93_data *data = i2c_get_clientdata(client);
2270        unsigned long val;
2271        int err;
2272
2273        err = kstrtoul(buf, 10, &val);
2274        if (err)
2275                return err;
2276
2277        mutex_lock(&data->update_lock);
2278        data->prochot_max[nr] = LM93_PROCHOT_TO_REG(val);
2279        lm93_write_byte(client, LM93_REG_PROCHOT_MAX(nr),
2280                        data->prochot_max[nr]);
2281        mutex_unlock(&data->update_lock);
2282        return count;
2283}
2284
2285static SENSOR_DEVICE_ATTR(prochot1_max, S_IWUSR | S_IRUGO,
2286                          show_prochot_max, store_prochot_max, 0);
2287static SENSOR_DEVICE_ATTR(prochot2_max, S_IWUSR | S_IRUGO,
2288                          show_prochot_max, store_prochot_max, 1);
2289
2290static const u8 prochot_override_mask[] = { 0x80, 0x40 };
2291
2292static ssize_t show_prochot_override(struct device *dev,
2293                                struct device_attribute *attr, char *buf)
2294{
2295        int nr = (to_sensor_dev_attr(attr))->index;
2296        struct lm93_data *data = lm93_update_device(dev);
2297        return sprintf(buf, "%d\n",
2298                (data->prochot_override & prochot_override_mask[nr]) ? 1 : 0);
2299}
2300
2301static ssize_t store_prochot_override(struct device *dev,
2302                                        struct device_attribute *attr,
2303                                        const char *buf, size_t count)
2304{
2305        int nr = (to_sensor_dev_attr(attr))->index;
2306        struct i2c_client *client = to_i2c_client(dev);
2307        struct lm93_data *data = i2c_get_clientdata(client);
2308        unsigned long val;
2309        int err;
2310
2311        err = kstrtoul(buf, 10, &val);
2312        if (err)
2313                return err;
2314
2315        mutex_lock(&data->update_lock);
2316        if (val)
2317                data->prochot_override |= prochot_override_mask[nr];
2318        else
2319                data->prochot_override &= (~prochot_override_mask[nr]);
2320        lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
2321                        data->prochot_override);
2322        mutex_unlock(&data->update_lock);
2323        return count;
2324}
2325
2326static SENSOR_DEVICE_ATTR(prochot1_override, S_IWUSR | S_IRUGO,
2327                          show_prochot_override, store_prochot_override, 0);
2328static SENSOR_DEVICE_ATTR(prochot2_override, S_IWUSR | S_IRUGO,
2329                          show_prochot_override, store_prochot_override, 1);
2330
2331static ssize_t show_prochot_interval(struct device *dev,
2332                                struct device_attribute *attr, char *buf)
2333{
2334        int nr = (to_sensor_dev_attr(attr))->index;
2335        struct lm93_data *data = lm93_update_device(dev);
2336        u8 tmp;
2337        if (nr == 1)
2338                tmp = (data->prochot_interval & 0xf0) >> 4;
2339        else
2340                tmp = data->prochot_interval & 0x0f;
2341        return sprintf(buf, "%d\n", LM93_INTERVAL_FROM_REG(tmp));
2342}
2343
2344static ssize_t store_prochot_interval(struct device *dev,
2345                                        struct device_attribute *attr,
2346                                        const char *buf, size_t count)
2347{
2348        int nr = (to_sensor_dev_attr(attr))->index;
2349        struct i2c_client *client = to_i2c_client(dev);
2350        struct lm93_data *data = i2c_get_clientdata(client);
2351        u8 tmp;
2352        unsigned long val;
2353        int err;
2354
2355        err = kstrtoul(buf, 10, &val);
2356        if (err)
2357                return err;
2358
2359        mutex_lock(&data->update_lock);
2360        tmp = lm93_read_byte(client, LM93_REG_PROCHOT_INTERVAL);
2361        if (nr == 1)
2362                tmp = (tmp & 0x0f) | (LM93_INTERVAL_TO_REG(val) << 4);
2363        else
2364                tmp = (tmp & 0xf0) | LM93_INTERVAL_TO_REG(val);
2365        data->prochot_interval = tmp;
2366        lm93_write_byte(client, LM93_REG_PROCHOT_INTERVAL, tmp);
2367        mutex_unlock(&data->update_lock);
2368        return count;
2369}
2370
2371static SENSOR_DEVICE_ATTR(prochot1_interval, S_IWUSR | S_IRUGO,
2372                          show_prochot_interval, store_prochot_interval, 0);
2373static SENSOR_DEVICE_ATTR(prochot2_interval, S_IWUSR | S_IRUGO,
2374                          show_prochot_interval, store_prochot_interval, 1);
2375
2376static ssize_t show_prochot_override_duty_cycle(struct device *dev,
2377                                                struct device_attribute *attr,
2378                                                char *buf)
2379{
2380        struct lm93_data *data = lm93_update_device(dev);
2381        return sprintf(buf, "%d\n", data->prochot_override & 0x0f);
2382}
2383
2384static ssize_t store_prochot_override_duty_cycle(struct device *dev,
2385                                                struct device_attribute *attr,
2386                                                const char *buf, size_t count)
2387{
2388        struct i2c_client *client = to_i2c_client(dev);
2389        struct lm93_data *data = i2c_get_clientdata(client);
2390        unsigned long val;
2391        int err;
2392
2393        err = kstrtoul(buf, 10, &val);
2394        if (err)
2395                return err;
2396
2397        mutex_lock(&data->update_lock);
2398        data->prochot_override = (data->prochot_override & 0xf0) |
2399                                        SENSORS_LIMIT(val, 0, 15);
2400        lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
2401                        data->prochot_override);
2402        mutex_unlock(&data->update_lock);
2403        return count;
2404}
2405
2406static DEVICE_ATTR(prochot_override_duty_cycle, S_IRUGO | S_IWUSR,
2407                        show_prochot_override_duty_cycle,
2408                        store_prochot_override_duty_cycle);
2409
2410static ssize_t show_prochot_short(struct device *dev,
2411                                struct device_attribute *attr, char *buf)
2412{
2413        struct lm93_data *data = lm93_update_device(dev);
2414        return sprintf(buf, "%d\n", (data->config & 0x10) ? 1 : 0);
2415}
2416
2417static ssize_t store_prochot_short(struct device *dev,
2418                                        struct device_attribute *attr,
2419                                        const char *buf, size_t count)
2420{
2421        struct i2c_client *client = to_i2c_client(dev);
2422        struct lm93_data *data = i2c_get_clientdata(client);
2423        unsigned long val;
2424        int err;
2425
2426        err = kstrtoul(buf, 10, &val);
2427        if (err)
2428                return err;
2429
2430        mutex_lock(&data->update_lock);
2431        if (val)
2432                data->config |= 0x10;
2433        else
2434                data->config &= ~0x10;
2435        lm93_write_byte(client, LM93_REG_CONFIG, data->config);
2436        mutex_unlock(&data->update_lock);
2437        return count;
2438}
2439
2440static DEVICE_ATTR(prochot_short, S_IRUGO | S_IWUSR,
2441                   show_prochot_short, store_prochot_short);
2442
2443static ssize_t show_vrdhot(struct device *dev, struct device_attribute *attr,
2444                                char *buf)
2445{
2446        int nr = (to_sensor_dev_attr(attr))->index;
2447        struct lm93_data *data = lm93_update_device(dev);
2448        return sprintf(buf, "%d\n",
2449                       data->block1.host_status_1 & (1 << (nr + 4)) ? 1 : 0);
2450}
2451
2452static SENSOR_DEVICE_ATTR(vrdhot1, S_IRUGO, show_vrdhot, NULL, 0);
2453static SENSOR_DEVICE_ATTR(vrdhot2, S_IRUGO, show_vrdhot, NULL, 1);
2454
2455static ssize_t show_gpio(struct device *dev, struct device_attribute *attr,
2456                                char *buf)
2457{
2458        struct lm93_data *data = lm93_update_device(dev);
2459        return sprintf(buf, "%d\n", LM93_GPI_FROM_REG(data->gpi));
2460}
2461
2462static DEVICE_ATTR(gpio, S_IRUGO, show_gpio, NULL);
2463
2464static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
2465                                char *buf)
2466{
2467        struct lm93_data *data = lm93_update_device(dev);
2468        return sprintf(buf, "%d\n", LM93_ALARMS_FROM_REG(data->block1));
2469}
2470
2471static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
2472
2473static struct attribute *lm93_attrs[] = {
2474        &sensor_dev_attr_in1_input.dev_attr.attr,
2475        &sensor_dev_attr_in2_input.dev_attr.attr,
2476        &sensor_dev_attr_in3_input.dev_attr.attr,
2477        &sensor_dev_attr_in4_input.dev_attr.attr,
2478        &sensor_dev_attr_in5_input.dev_attr.attr,
2479        &sensor_dev_attr_in6_input.dev_attr.attr,
2480        &sensor_dev_attr_in7_input.dev_attr.attr,
2481        &sensor_dev_attr_in8_input.dev_attr.attr,
2482        &sensor_dev_attr_in9_input.dev_attr.attr,
2483        &sensor_dev_attr_in10_input.dev_attr.attr,
2484        &sensor_dev_attr_in11_input.dev_attr.attr,
2485        &sensor_dev_attr_in12_input.dev_attr.attr,
2486        &sensor_dev_attr_in13_input.dev_attr.attr,
2487        &sensor_dev_attr_in14_input.dev_attr.attr,
2488        &sensor_dev_attr_in15_input.dev_attr.attr,
2489        &sensor_dev_attr_in16_input.dev_attr.attr,
2490        &sensor_dev_attr_in1_min.dev_attr.attr,
2491        &sensor_dev_attr_in2_min.dev_attr.attr,
2492        &sensor_dev_attr_in3_min.dev_attr.attr,
2493        &sensor_dev_attr_in4_min.dev_attr.attr,
2494        &sensor_dev_attr_in5_min.dev_attr.attr,
2495        &sensor_dev_attr_in6_min.dev_attr.attr,
2496        &sensor_dev_attr_in7_min.dev_attr.attr,
2497        &sensor_dev_attr_in8_min.dev_attr.attr,
2498        &sensor_dev_attr_in9_min.dev_attr.attr,
2499        &sensor_dev_attr_in10_min.dev_attr.attr,
2500        &sensor_dev_attr_in11_min.dev_attr.attr,
2501        &sensor_dev_attr_in12_min.dev_attr.attr,
2502        &sensor_dev_attr_in13_min.dev_attr.attr,
2503        &sensor_dev_attr_in14_min.dev_attr.attr,
2504        &sensor_dev_attr_in15_min.dev_attr.attr,
2505        &sensor_dev_attr_in16_min.dev_attr.attr,
2506        &sensor_dev_attr_in1_max.dev_attr.attr,
2507        &sensor_dev_attr_in2_max.dev_attr.attr,
2508        &sensor_dev_attr_in3_max.dev_attr.attr,
2509        &sensor_dev_attr_in4_max.dev_attr.attr,
2510        &sensor_dev_attr_in5_max.dev_attr.attr,
2511        &sensor_dev_attr_in6_max.dev_attr.attr,
2512        &sensor_dev_attr_in7_max.dev_attr.attr,
2513        &sensor_dev_attr_in8_max.dev_attr.attr,
2514        &sensor_dev_attr_in9_max.dev_attr.attr,
2515        &sensor_dev_attr_in10_max.dev_attr.attr,
2516        &sensor_dev_attr_in11_max.dev_attr.attr,
2517        &sensor_dev_attr_in12_max.dev_attr.attr,
2518        &sensor_dev_attr_in13_max.dev_attr.attr,
2519        &sensor_dev_attr_in14_max.dev_attr.attr,
2520        &sensor_dev_attr_in15_max.dev_attr.attr,
2521        &sensor_dev_attr_in16_max.dev_attr.attr,
2522        &sensor_dev_attr_temp1_input.dev_attr.attr,
2523        &sensor_dev_attr_temp2_input.dev_attr.attr,
2524        &sensor_dev_attr_temp3_input.dev_attr.attr,
2525        &sensor_dev_attr_temp1_min.dev_attr.attr,
2526        &sensor_dev_attr_temp2_min.dev_attr.attr,
2527        &sensor_dev_attr_temp3_min.dev_attr.attr,
2528        &sensor_dev_attr_temp1_max.dev_attr.attr,
2529        &sensor_dev_attr_temp2_max.dev_attr.attr,
2530        &sensor_dev_attr_temp3_max.dev_attr.attr,
2531        &sensor_dev_attr_temp1_auto_base.dev_attr.attr,
2532        &sensor_dev_attr_temp2_auto_base.dev_attr.attr,
2533        &sensor_dev_attr_temp3_auto_base.dev_attr.attr,
2534        &sensor_dev_attr_temp1_auto_boost.dev_attr.attr,
2535        &sensor_dev_attr_temp2_auto_boost.dev_attr.attr,
2536        &sensor_dev_attr_temp3_auto_boost.dev_attr.attr,
2537        &sensor_dev_attr_temp1_auto_boost_hyst.dev_attr.attr,
2538        &sensor_dev_attr_temp2_auto_boost_hyst.dev_attr.attr,
2539        &sensor_dev_attr_temp3_auto_boost_hyst.dev_attr.attr,
2540        &sensor_dev_attr_temp1_auto_offset1.dev_attr.attr,
2541        &sensor_dev_attr_temp1_auto_offset2.dev_attr.attr,
2542        &sensor_dev_attr_temp1_auto_offset3.dev_attr.attr,
2543        &sensor_dev_attr_temp1_auto_offset4.dev_attr.attr,
2544        &sensor_dev_attr_temp1_auto_offset5.dev_attr.attr,
2545        &sensor_dev_attr_temp1_auto_offset6.dev_attr.attr,
2546        &sensor_dev_attr_temp1_auto_offset7.dev_attr.attr,
2547        &sensor_dev_attr_temp1_auto_offset8.dev_attr.attr,
2548        &sensor_dev_attr_temp1_auto_offset9.dev_attr.attr,
2549        &sensor_dev_attr_temp1_auto_offset10.dev_attr.attr,
2550        &sensor_dev_attr_temp1_auto_offset11.dev_attr.attr,
2551        &sensor_dev_attr_temp1_auto_offset12.dev_attr.attr,
2552        &sensor_dev_attr_temp2_auto_offset1.dev_attr.attr,
2553        &sensor_dev_attr_temp2_auto_offset2.dev_attr.attr,
2554        &sensor_dev_attr_temp2_auto_offset3.dev_attr.attr,
2555        &sensor_dev_attr_temp2_auto_offset4.dev_attr.attr,
2556        &sensor_dev_attr_temp2_auto_offset5.dev_attr.attr,
2557        &sensor_dev_attr_temp2_auto_offset6.dev_attr.attr,
2558        &sensor_dev_attr_temp2_auto_offset7.dev_attr.attr,
2559        &sensor_dev_attr_temp2_auto_offset8.dev_attr.attr,
2560        &sensor_dev_attr_temp2_auto_offset9.dev_attr.attr,
2561        &sensor_dev_attr_temp2_auto_offset10.dev_attr.attr,
2562        &sensor_dev_attr_temp2_auto_offset11.dev_attr.attr,
2563        &sensor_dev_attr_temp2_auto_offset12.dev_attr.attr,
2564        &sensor_dev_attr_temp3_auto_offset1.dev_attr.attr,
2565        &sensor_dev_attr_temp3_auto_offset2.dev_attr.attr,
2566        &sensor_dev_attr_temp3_auto_offset3.dev_attr.attr,
2567        &sensor_dev_attr_temp3_auto_offset4.dev_attr.attr,
2568        &sensor_dev_attr_temp3_auto_offset5.dev_attr.attr,
2569        &sensor_dev_attr_temp3_auto_offset6.dev_attr.attr,
2570        &sensor_dev_attr_temp3_auto_offset7.dev_attr.attr,
2571        &sensor_dev_attr_temp3_auto_offset8.dev_attr.attr,
2572        &sensor_dev_attr_temp3_auto_offset9.dev_attr.attr,
2573        &sensor_dev_attr_temp3_auto_offset10.dev_attr.attr,
2574        &sensor_dev_attr_temp3_auto_offset11.dev_attr.attr,
2575        &sensor_dev_attr_temp3_auto_offset12.dev_attr.attr,
2576        &sensor_dev_attr_temp1_auto_pwm_min.dev_attr.attr,
2577        &sensor_dev_attr_temp2_auto_pwm_min.dev_attr.attr,
2578        &sensor_dev_attr_temp3_auto_pwm_min.dev_attr.attr,
2579        &sensor_dev_attr_temp1_auto_offset_hyst.dev_attr.attr,
2580        &sensor_dev_attr_temp2_auto_offset_hyst.dev_attr.attr,
2581        &sensor_dev_attr_temp3_auto_offset_hyst.dev_attr.attr,
2582        &sensor_dev_attr_fan1_input.dev_attr.attr,
2583        &sensor_dev_attr_fan2_input.dev_attr.attr,
2584        &sensor_dev_attr_fan3_input.dev_attr.attr,
2585        &sensor_dev_attr_fan4_input.dev_attr.attr,
2586        &sensor_dev_attr_fan1_min.dev_attr.attr,
2587        &sensor_dev_attr_fan2_min.dev_attr.attr,
2588        &sensor_dev_attr_fan3_min.dev_attr.attr,
2589        &sensor_dev_attr_fan4_min.dev_attr.attr,
2590        &sensor_dev_attr_fan1_smart_tach.dev_attr.attr,
2591        &sensor_dev_attr_fan2_smart_tach.dev_attr.attr,
2592        &sensor_dev_attr_fan3_smart_tach.dev_attr.attr,
2593        &sensor_dev_attr_fan4_smart_tach.dev_attr.attr,
2594        &sensor_dev_attr_pwm1.dev_attr.attr,
2595        &sensor_dev_attr_pwm2.dev_attr.attr,
2596        &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2597        &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2598        &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2599        &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2600        &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
2601        &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
2602        &sensor_dev_attr_pwm1_auto_spinup_min.dev_attr.attr,
2603        &sensor_dev_attr_pwm2_auto_spinup_min.dev_attr.attr,
2604        &sensor_dev_attr_pwm1_auto_spinup_time.dev_attr.attr,
2605        &sensor_dev_attr_pwm2_auto_spinup_time.dev_attr.attr,
2606        &dev_attr_pwm_auto_prochot_ramp.attr,
2607        &dev_attr_pwm_auto_vrdhot_ramp.attr,
2608        &sensor_dev_attr_cpu0_vid.dev_attr.attr,
2609        &sensor_dev_attr_cpu1_vid.dev_attr.attr,
2610        &sensor_dev_attr_prochot1.dev_attr.attr,
2611        &sensor_dev_attr_prochot2.dev_attr.attr,
2612        &sensor_dev_attr_prochot1_avg.dev_attr.attr,
2613        &sensor_dev_attr_prochot2_avg.dev_attr.attr,
2614        &sensor_dev_attr_prochot1_max.dev_attr.attr,
2615        &sensor_dev_attr_prochot2_max.dev_attr.attr,
2616        &sensor_dev_attr_prochot1_override.dev_attr.attr,
2617        &sensor_dev_attr_prochot2_override.dev_attr.attr,
2618        &sensor_dev_attr_prochot1_interval.dev_attr.attr,
2619        &sensor_dev_attr_prochot2_interval.dev_attr.attr,
2620        &dev_attr_prochot_override_duty_cycle.attr,
2621        &dev_attr_prochot_short.attr,
2622        &sensor_dev_attr_vrdhot1.dev_attr.attr,
2623        &sensor_dev_attr_vrdhot2.dev_attr.attr,
2624        &dev_attr_gpio.attr,
2625        &dev_attr_alarms.attr,
2626        NULL
2627};
2628
2629static struct attribute_group lm93_attr_grp = {
2630        .attrs = lm93_attrs,
2631};
2632
2633static void lm93_init_client(struct i2c_client *client)
2634{
2635        int i;
2636        u8 reg;
2637
2638        /* configure VID pin input thresholds */
2639        reg = lm93_read_byte(client, LM93_REG_GPI_VID_CTL);
2640        lm93_write_byte(client, LM93_REG_GPI_VID_CTL,
2641                        reg | (vid_agtl ? 0x03 : 0x00));
2642
2643        if (init) {
2644                /* enable #ALERT pin */
2645                reg = lm93_read_byte(client, LM93_REG_CONFIG);
2646                lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x08);
2647
2648                /* enable ASF mode for BMC status registers */
2649                reg = lm93_read_byte(client, LM93_REG_STATUS_CONTROL);
2650                lm93_write_byte(client, LM93_REG_STATUS_CONTROL, reg | 0x02);
2651
2652                /* set sleep state to S0 */
2653                lm93_write_byte(client, LM93_REG_SLEEP_CONTROL, 0);
2654
2655                /* unmask #VRDHOT and dynamic VCCP (if nec) error events */
2656                reg = lm93_read_byte(client, LM93_REG_MISC_ERR_MASK);
2657                reg &= ~0x03;
2658                reg &= ~(vccp_limit_type[0] ? 0x10 : 0);
2659                reg &= ~(vccp_limit_type[1] ? 0x20 : 0);
2660                lm93_write_byte(client, LM93_REG_MISC_ERR_MASK, reg);
2661        }
2662
2663        /* start monitoring */
2664        reg = lm93_read_byte(client, LM93_REG_CONFIG);
2665        lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x01);
2666
2667        /* spin until ready */
2668        for (i = 0; i < 20; i++) {
2669                msleep(10);
2670                if ((lm93_read_byte(client, LM93_REG_CONFIG) & 0x80) == 0x80)
2671                        return;
2672        }
2673
2674        dev_warn(&client->dev, "timed out waiting for sensor "
2675                 "chip to signal ready!\n");
2676}
2677
2678/* Return 0 if detection is successful, -ENODEV otherwise */
2679static int lm93_detect(struct i2c_client *client, struct i2c_board_info *info)
2680{
2681        struct i2c_adapter *adapter = client->adapter;
2682        int mfr, ver;
2683        const char *name;
2684
2685        if (!i2c_check_functionality(adapter, LM93_SMBUS_FUNC_MIN))
2686                return -ENODEV;
2687
2688        /* detection */
2689        mfr = lm93_read_byte(client, LM93_REG_MFR_ID);
2690        if (mfr != 0x01) {
2691                dev_dbg(&adapter->dev,
2692                        "detect failed, bad manufacturer id 0x%02x!\n", mfr);
2693                return -ENODEV;
2694        }
2695
2696        ver = lm93_read_byte(client, LM93_REG_VER);
2697        switch (ver) {
2698        case LM93_MFR_ID:
2699        case LM93_MFR_ID_PROTOTYPE:
2700                name = "lm93";
2701                break;
2702        case LM94_MFR_ID_2:
2703        case LM94_MFR_ID:
2704        case LM94_MFR_ID_PROTOTYPE:
2705                name = "lm94";
2706                break;
2707        default:
2708                dev_dbg(&adapter->dev,
2709                        "detect failed, bad version id 0x%02x!\n", ver);
2710                return -ENODEV;
2711        }
2712
2713        strlcpy(info->type, name, I2C_NAME_SIZE);
2714        dev_dbg(&adapter->dev, "loading %s at %d, 0x%02x\n",
2715                client->name, i2c_adapter_id(client->adapter),
2716                client->addr);
2717
2718        return 0;
2719}
2720
2721static int lm93_probe(struct i2c_client *client,
2722                      const struct i2c_device_id *id)
2723{
2724        struct lm93_data *data;
2725        int err, func;
2726        void (*update)(struct lm93_data *, struct i2c_client *);
2727
2728        /* choose update routine based on bus capabilities */
2729        func = i2c_get_functionality(client->adapter);
2730        if (((LM93_SMBUS_FUNC_FULL & func) == LM93_SMBUS_FUNC_FULL) &&
2731                        (!disable_block)) {
2732                dev_dbg(&client->dev, "using SMBus block data transactions\n");
2733                update = lm93_update_client_full;
2734        } else if ((LM93_SMBUS_FUNC_MIN & func) == LM93_SMBUS_FUNC_MIN) {
2735                dev_dbg(&client->dev, "disabled SMBus block data "
2736                        "transactions\n");
2737                update = lm93_update_client_min;
2738        } else {
2739                dev_dbg(&client->dev, "detect failed, "
2740                        "smbus byte and/or word data not supported!\n");
2741                return -ENODEV;
2742        }
2743
2744        data = devm_kzalloc(&client->dev, sizeof(struct lm93_data), GFP_KERNEL);
2745        if (!data) {
2746                dev_dbg(&client->dev, "out of memory!\n");
2747                return -ENOMEM;
2748        }
2749        i2c_set_clientdata(client, data);
2750
2751        /* housekeeping */
2752        data->valid = 0;
2753        data->update = update;
2754        mutex_init(&data->update_lock);
2755
2756        /* initialize the chip */
2757        lm93_init_client(client);
2758
2759        err = sysfs_create_group(&client->dev.kobj, &lm93_attr_grp);
2760        if (err)
2761                return err;
2762
2763        /* Register hwmon driver class */
2764        data->hwmon_dev = hwmon_device_register(&client->dev);
2765        if (!IS_ERR(data->hwmon_dev))
2766                return 0;
2767
2768        err = PTR_ERR(data->hwmon_dev);
2769        dev_err(&client->dev, "error registering hwmon device.\n");
2770        sysfs_remove_group(&client->dev.kobj, &lm93_attr_grp);
2771        return err;
2772}
2773
2774static int lm93_remove(struct i2c_client *client)
2775{
2776        struct lm93_data *data = i2c_get_clientdata(client);
2777
2778        hwmon_device_unregister(data->hwmon_dev);
2779        sysfs_remove_group(&client->dev.kobj, &lm93_attr_grp);
2780
2781        return 0;
2782}
2783
2784static const struct i2c_device_id lm93_id[] = {
2785        { "lm93", 0 },
2786        { "lm94", 0 },
2787        { }
2788};
2789MODULE_DEVICE_TABLE(i2c, lm93_id);
2790
2791static struct i2c_driver lm93_driver = {
2792        .class          = I2C_CLASS_HWMON,
2793        .driver = {
2794                .name   = "lm93",
2795        },
2796        .probe          = lm93_probe,
2797        .remove         = lm93_remove,
2798        .id_table       = lm93_id,
2799        .detect         = lm93_detect,
2800        .address_list   = normal_i2c,
2801};
2802
2803module_i2c_driver(lm93_driver);
2804
2805MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>, "
2806                "Hans J. Koch <hjk@hansjkoch.de>");
2807MODULE_DESCRIPTION("LM93 driver");
2808MODULE_LICENSE("GPL");
2809
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.