linux/drivers/hwmon/vt1211.c
<<
>>
Prefs
   1/*
   2 * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware
   3 *            monitoring features
   4 * Copyright (C) 2006 Juerg Haefliger <juergh@gmail.com>
   5 *
   6 * This driver is based on the driver for kernel 2.4 by Mark D. Studebaker
   7 * and its port to kernel 2.6 by Lars Ekman.
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU General Public License
  20 * along with this program; if not, write to the Free Software
  21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22 */
  23
  24#include <linux/module.h>
  25#include <linux/init.h>
  26#include <linux/slab.h>
  27#include <linux/jiffies.h>
  28#include <linux/platform_device.h>
  29#include <linux/hwmon.h>
  30#include <linux/hwmon-sysfs.h>
  31#include <linux/hwmon-vid.h>
  32#include <linux/err.h>
  33#include <linux/mutex.h>
  34#include <linux/ioport.h>
  35#include <linux/acpi.h>
  36#include <linux/io.h>
  37
  38static int uch_config = -1;
  39module_param(uch_config, int, 0);
  40MODULE_PARM_DESC(uch_config, "Initialize the universal channel configuration");
  41
  42static int int_mode = -1;
  43module_param(int_mode, int, 0);
  44MODULE_PARM_DESC(int_mode, "Force the temperature interrupt mode");
  45
  46static unsigned short force_id;
  47module_param(force_id, ushort, 0);
  48MODULE_PARM_DESC(force_id, "Override the detected device ID");
  49
  50static struct platform_device *pdev;
  51
  52#define DRVNAME "vt1211"
  53
  54/* ---------------------------------------------------------------------
  55 * Registers
  56 *
  57 * The sensors are defined as follows.
  58 *
  59 * Sensor          Voltage Mode   Temp Mode   Notes (from the datasheet)
  60 * --------        ------------   ---------   --------------------------
  61 * Reading 1                      temp1       Intel thermal diode
  62 * Reading 3                      temp2       Internal thermal diode
  63 * UCH1/Reading2   in0            temp3       NTC type thermistor
  64 * UCH2            in1            temp4       +2.5V
  65 * UCH3            in2            temp5       VccP
  66 * UCH4            in3            temp6       +5V
  67 * UCH5            in4            temp7       +12V
  68 * 3.3V            in5                        Internal VDD (+3.3V)
  69 *
  70 * --------------------------------------------------------------------- */
  71
  72/* Voltages (in) numbered 0-5 (ix) */
  73#define VT1211_REG_IN(ix)               (0x21 + (ix))
  74#define VT1211_REG_IN_MIN(ix)           ((ix) == 0 ? 0x3e : 0x2a + 2 * (ix))
  75#define VT1211_REG_IN_MAX(ix)           ((ix) == 0 ? 0x3d : 0x29 + 2 * (ix))
  76
  77/* Temperatures (temp) numbered 0-6 (ix) */
  78static u8 regtemp[]     = {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
  79static u8 regtempmax[]  = {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31};
  80static u8 regtemphyst[] = {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32};
  81
  82/* Fans numbered 0-1 (ix) */
  83#define VT1211_REG_FAN(ix)              (0x29 + (ix))
  84#define VT1211_REG_FAN_MIN(ix)          (0x3b + (ix))
  85#define VT1211_REG_FAN_DIV               0x47
  86
  87/* PWMs numbered 0-1 (ix) */
  88/* Auto points numbered 0-3 (ap) */
  89#define VT1211_REG_PWM(ix)              (0x60 + (ix))
  90#define VT1211_REG_PWM_CLK               0x50
  91#define VT1211_REG_PWM_CTL               0x51
  92#define VT1211_REG_PWM_AUTO_TEMP(ap)    (0x55 - (ap))
  93#define VT1211_REG_PWM_AUTO_PWM(ix, ap) (0x58 + 2 * (ix) - (ap))
  94
  95/* Miscellaneous registers */
  96#define VT1211_REG_CONFIG               0x40
  97#define VT1211_REG_ALARM1               0x41
  98#define VT1211_REG_ALARM2               0x42
  99#define VT1211_REG_VID                  0x45
 100#define VT1211_REG_UCH_CONFIG           0x4a
 101#define VT1211_REG_TEMP1_CONFIG         0x4b
 102#define VT1211_REG_TEMP2_CONFIG         0x4c
 103
 104/* In, temp & fan alarm bits */
 105static const u8 bitalarmin[]    = {11, 0, 1, 3, 8, 2, 9};
 106static const u8 bitalarmtemp[]  = {4, 15, 11, 0, 1, 3, 8};
 107static const u8 bitalarmfan[]   = {6, 7};
 108
 109/* ---------------------------------------------------------------------
 110 * Data structures and manipulation thereof
 111 * --------------------------------------------------------------------- */
 112
 113struct vt1211_data {
 114        unsigned short addr;
 115        const char *name;
 116        struct device *hwmon_dev;
 117
 118        struct mutex update_lock;
 119        char valid;                     /* !=0 if following fields are valid */
 120        unsigned long last_updated;     /* In jiffies */
 121
 122        /* Register values */
 123        u8  in[6];
 124        u8  in_max[6];
 125        u8  in_min[6];
 126        u8  temp[7];
 127        u8  temp_max[7];
 128        u8  temp_hyst[7];
 129        u8  fan[2];
 130        u8  fan_min[2];
 131        u8  fan_div[2];
 132        u8  fan_ctl;
 133        u8  pwm[2];
 134        u8  pwm_ctl[2];
 135        u8  pwm_clk;
 136        u8  pwm_auto_temp[4];
 137        u8  pwm_auto_pwm[2][4];
 138        u8  vid;                /* Read once at init time */
 139        u8  vrm;
 140        u8  uch_config;         /* Read once at init time */
 141        u16 alarms;
 142};
 143
 144/* ix = [0-5] */
 145#define ISVOLT(ix, uch_config)  ((ix) > 4 ? 1 : \
 146                                 !(((uch_config) >> ((ix) + 2)) & 1))
 147
 148/* ix = [0-6] */
 149#define ISTEMP(ix, uch_config)  ((ix) < 2 ? 1 : \
 150                                 ((uch_config) >> (ix)) & 1)
 151
 152/* in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
 153   driver according to the VT1211 BIOS porting guide */
 154#define IN_FROM_REG(ix, reg)    ((reg) < 3 ? 0 : (ix) == 5 ? \
 155                                 (((reg) - 3) * 15882 + 479) / 958 : \
 156                                 (((reg) - 3) * 10000 + 479) / 958)
 157#define IN_TO_REG(ix, val)      (SENSORS_LIMIT((ix) == 5 ? \
 158                                 ((val) * 958 + 7941) / 15882 + 3 : \
 159                                 ((val) * 958 + 5000) / 10000 + 3, 0, 255))
 160
 161/* temp1 (ix = 0) is an intel thermal diode which is scaled in user space.
 162   temp2 (ix = 1) is the internal temp diode so it's scaled in the driver
 163   according to some measurements that I took on an EPIA M10000.
 164   temp3-7 are thermistor based so the driver returns the voltage measured at
 165   the pin (range 0V - 2.2V). */
 166#define TEMP_FROM_REG(ix, reg)  ((ix) == 0 ? (reg) * 1000 : \
 167                                 (ix) == 1 ? (reg) < 51 ? 0 : \
 168                                 ((reg) - 51) * 1000 : \
 169                                 ((253 - (reg)) * 2200 + 105) / 210)
 170#define TEMP_TO_REG(ix, val)    SENSORS_LIMIT( \
 171                                 ((ix) == 0 ? ((val) + 500) / 1000 : \
 172                                  (ix) == 1 ? ((val) + 500) / 1000 + 51 : \
 173                                  253 - ((val) * 210 + 1100) / 2200), 0, 255)
 174
 175#define DIV_FROM_REG(reg)       (1 << (reg))
 176
 177#define RPM_FROM_REG(reg, div)  (((reg) == 0) || ((reg) == 255) ? 0 : \
 178                                 1310720 / (reg) / DIV_FROM_REG(div))
 179#define RPM_TO_REG(val, div)    ((val) == 0 ? 255 : \
 180                                 SENSORS_LIMIT((1310720 / (val) / \
 181                                 DIV_FROM_REG(div)), 1, 254))
 182
 183/* ---------------------------------------------------------------------
 184 * Super-I/O constants and functions
 185 * --------------------------------------------------------------------- */
 186
 187/* Configuration index port registers
 188 * The vt1211 can live at 2 different addresses so we need to probe both */
 189#define SIO_REG_CIP1            0x2e
 190#define SIO_REG_CIP2            0x4e
 191
 192/* Configuration registers */
 193#define SIO_VT1211_LDN          0x07    /* logical device number */
 194#define SIO_VT1211_DEVID        0x20    /* device ID */
 195#define SIO_VT1211_DEVREV       0x21    /* device revision */
 196#define SIO_VT1211_ACTIVE       0x30    /* HW monitor active */
 197#define SIO_VT1211_BADDR        0x60    /* base I/O address */
 198#define SIO_VT1211_ID           0x3c    /* VT1211 device ID */
 199
 200/* VT1211 logical device numbers */
 201#define SIO_VT1211_LDN_HWMON    0x0b    /* HW monitor */
 202
 203static inline void superio_outb(int sio_cip, int reg, int val)
 204{
 205        outb(reg, sio_cip);
 206        outb(val, sio_cip + 1);
 207}
 208
 209static inline int superio_inb(int sio_cip, int reg)
 210{
 211        outb(reg, sio_cip);
 212        return inb(sio_cip + 1);
 213}
 214
 215static inline void superio_select(int sio_cip, int ldn)
 216{
 217        outb(SIO_VT1211_LDN, sio_cip);
 218        outb(ldn, sio_cip + 1);
 219}
 220
 221static inline void superio_enter(int sio_cip)
 222{
 223        outb(0x87, sio_cip);
 224        outb(0x87, sio_cip);
 225}
 226
 227static inline void superio_exit(int sio_cip)
 228{
 229        outb(0xaa, sio_cip);
 230}
 231
 232/* ---------------------------------------------------------------------
 233 * Device I/O access
 234 * --------------------------------------------------------------------- */
 235
 236static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg)
 237{
 238        return inb(data->addr + reg);
 239}
 240
 241static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val)
 242{
 243        outb(val, data->addr + reg);
 244}
 245
 246static struct vt1211_data *vt1211_update_device(struct device *dev)
 247{
 248        struct vt1211_data *data = dev_get_drvdata(dev);
 249        int ix, val;
 250
 251        mutex_lock(&data->update_lock);
 252
 253        /* registers cache is refreshed after 1 second */
 254        if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
 255                /* read VID */
 256                data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f;
 257
 258                /* voltage (in) registers */
 259                for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
 260                        if (ISVOLT(ix, data->uch_config)) {
 261                                data->in[ix] = vt1211_read8(data,
 262                                                VT1211_REG_IN(ix));
 263                                data->in_min[ix] = vt1211_read8(data,
 264                                                VT1211_REG_IN_MIN(ix));
 265                                data->in_max[ix] = vt1211_read8(data,
 266                                                VT1211_REG_IN_MAX(ix));
 267                        }
 268                }
 269
 270                /* temp registers */
 271                for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
 272                        if (ISTEMP(ix, data->uch_config)) {
 273                                data->temp[ix] = vt1211_read8(data,
 274                                                regtemp[ix]);
 275                                data->temp_max[ix] = vt1211_read8(data,
 276                                                regtempmax[ix]);
 277                                data->temp_hyst[ix] = vt1211_read8(data,
 278                                                regtemphyst[ix]);
 279                        }
 280                }
 281
 282                /* fan & pwm registers */
 283                for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
 284                        data->fan[ix] = vt1211_read8(data,
 285                                                VT1211_REG_FAN(ix));
 286                        data->fan_min[ix] = vt1211_read8(data,
 287                                                VT1211_REG_FAN_MIN(ix));
 288                        data->pwm[ix] = vt1211_read8(data,
 289                                                VT1211_REG_PWM(ix));
 290                }
 291                val = vt1211_read8(data, VT1211_REG_FAN_DIV);
 292                data->fan_div[0] = (val >> 4) & 3;
 293                data->fan_div[1] = (val >> 6) & 3;
 294                data->fan_ctl = val & 0xf;
 295
 296                val = vt1211_read8(data, VT1211_REG_PWM_CTL);
 297                data->pwm_ctl[0] = val & 0xf;
 298                data->pwm_ctl[1] = (val >> 4) & 0xf;
 299
 300                data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK);
 301
 302                /* pwm & temp auto point registers */
 303                data->pwm_auto_pwm[0][1] = vt1211_read8(data,
 304                                                VT1211_REG_PWM_AUTO_PWM(0, 1));
 305                data->pwm_auto_pwm[0][2] = vt1211_read8(data,
 306                                                VT1211_REG_PWM_AUTO_PWM(0, 2));
 307                data->pwm_auto_pwm[1][1] = vt1211_read8(data,
 308                                                VT1211_REG_PWM_AUTO_PWM(1, 1));
 309                data->pwm_auto_pwm[1][2] = vt1211_read8(data,
 310                                                VT1211_REG_PWM_AUTO_PWM(1, 2));
 311                for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) {
 312                        data->pwm_auto_temp[ix] = vt1211_read8(data,
 313                                                VT1211_REG_PWM_AUTO_TEMP(ix));
 314                }
 315
 316                /* alarm registers */
 317                data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) |
 318                                vt1211_read8(data, VT1211_REG_ALARM1);
 319
 320                data->last_updated = jiffies;
 321                data->valid = 1;
 322        }
 323
 324        mutex_unlock(&data->update_lock);
 325
 326        return data;
 327}
 328
 329/* ---------------------------------------------------------------------
 330 * Voltage sysfs interfaces
 331 * ix = [0-5]
 332 * --------------------------------------------------------------------- */
 333
 334#define SHOW_IN_INPUT   0
 335#define SHOW_SET_IN_MIN 1
 336#define SHOW_SET_IN_MAX 2
 337#define SHOW_IN_ALARM   3
 338
 339static ssize_t show_in(struct device *dev, struct device_attribute *attr,
 340                       char *buf)
 341{
 342        struct vt1211_data *data = vt1211_update_device(dev);
 343        struct sensor_device_attribute_2 *sensor_attr_2 =
 344                                                to_sensor_dev_attr_2(attr);
 345        int ix = sensor_attr_2->index;
 346        int fn = sensor_attr_2->nr;
 347        int res;
 348
 349        switch (fn) {
 350        case SHOW_IN_INPUT:
 351                res = IN_FROM_REG(ix, data->in[ix]);
 352                break;
 353        case SHOW_SET_IN_MIN:
 354                res = IN_FROM_REG(ix, data->in_min[ix]);
 355                break;
 356        case SHOW_SET_IN_MAX:
 357                res = IN_FROM_REG(ix, data->in_max[ix]);
 358                break;
 359        case SHOW_IN_ALARM:
 360                res = (data->alarms >> bitalarmin[ix]) & 1;
 361                break;
 362        default:
 363                res = 0;
 364                dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
 365        }
 366
 367        return sprintf(buf, "%d\n", res);
 368}
 369
 370static ssize_t set_in(struct device *dev, struct device_attribute *attr,
 371                      const char *buf, size_t count)
 372{
 373        struct vt1211_data *data = dev_get_drvdata(dev);
 374        struct sensor_device_attribute_2 *sensor_attr_2 =
 375                                                to_sensor_dev_attr_2(attr);
 376        int ix = sensor_attr_2->index;
 377        int fn = sensor_attr_2->nr;
 378        long val = simple_strtol(buf, NULL, 10);
 379
 380        mutex_lock(&data->update_lock);
 381        switch (fn) {
 382        case SHOW_SET_IN_MIN:
 383                data->in_min[ix] = IN_TO_REG(ix, val);
 384                vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]);
 385                break;
 386        case SHOW_SET_IN_MAX:
 387                data->in_max[ix] = IN_TO_REG(ix, val);
 388                vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]);
 389                break;
 390        default:
 391                dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
 392        }
 393        mutex_unlock(&data->update_lock);
 394
 395        return count;
 396}
 397
 398/* ---------------------------------------------------------------------
 399 * Temperature sysfs interfaces
 400 * ix = [0-6]
 401 * --------------------------------------------------------------------- */
 402
 403#define SHOW_TEMP_INPUT         0
 404#define SHOW_SET_TEMP_MAX       1
 405#define SHOW_SET_TEMP_MAX_HYST  2
 406#define SHOW_TEMP_ALARM         3
 407
 408static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
 409                         char *buf)
 410{
 411        struct vt1211_data *data = vt1211_update_device(dev);
 412        struct sensor_device_attribute_2 *sensor_attr_2 =
 413                                                to_sensor_dev_attr_2(attr);
 414        int ix = sensor_attr_2->index;
 415        int fn = sensor_attr_2->nr;
 416        int res;
 417
 418        switch (fn) {
 419        case SHOW_TEMP_INPUT:
 420                res = TEMP_FROM_REG(ix, data->temp[ix]);
 421                break;
 422        case SHOW_SET_TEMP_MAX:
 423                res = TEMP_FROM_REG(ix, data->temp_max[ix]);
 424                break;
 425        case SHOW_SET_TEMP_MAX_HYST:
 426                res = TEMP_FROM_REG(ix, data->temp_hyst[ix]);
 427                break;
 428        case SHOW_TEMP_ALARM:
 429                res = (data->alarms >> bitalarmtemp[ix]) & 1;
 430                break;
 431        default:
 432                res = 0;
 433                dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
 434        }
 435
 436        return sprintf(buf, "%d\n", res);
 437}
 438
 439static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
 440                        const char *buf, size_t count)
 441{
 442        struct vt1211_data *data = dev_get_drvdata(dev);
 443        struct sensor_device_attribute_2 *sensor_attr_2 =
 444                                                to_sensor_dev_attr_2(attr);
 445        int ix = sensor_attr_2->index;
 446        int fn = sensor_attr_2->nr;
 447        long val = simple_strtol(buf, NULL, 10);
 448
 449        mutex_lock(&data->update_lock);
 450        switch (fn) {
 451        case SHOW_SET_TEMP_MAX:
 452                data->temp_max[ix] = TEMP_TO_REG(ix, val);
 453                vt1211_write8(data, regtempmax[ix],
 454                              data->temp_max[ix]);
 455                break;
 456        case SHOW_SET_TEMP_MAX_HYST:
 457                data->temp_hyst[ix] = TEMP_TO_REG(ix, val);
 458                vt1211_write8(data, regtemphyst[ix],
 459                              data->temp_hyst[ix]);
 460                break;
 461        default:
 462                dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
 463        }
 464        mutex_unlock(&data->update_lock);
 465
 466        return count;
 467}
 468
 469/* ---------------------------------------------------------------------
 470 * Fan sysfs interfaces
 471 * ix = [0-1]
 472 * --------------------------------------------------------------------- */
 473
 474#define SHOW_FAN_INPUT          0
 475#define SHOW_SET_FAN_MIN        1
 476#define SHOW_SET_FAN_DIV        2
 477#define SHOW_FAN_ALARM          3
 478
 479static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 480                        char *buf)
 481{
 482        struct vt1211_data *data = vt1211_update_device(dev);
 483        struct sensor_device_attribute_2 *sensor_attr_2 =
 484                                                to_sensor_dev_attr_2(attr);
 485        int ix = sensor_attr_2->index;
 486        int fn = sensor_attr_2->nr;
 487        int res;
 488
 489        switch (fn) {
 490        case SHOW_FAN_INPUT:
 491                res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]);
 492                break;
 493        case SHOW_SET_FAN_MIN:
 494                res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]);
 495                break;
 496        case SHOW_SET_FAN_DIV:
 497                res = DIV_FROM_REG(data->fan_div[ix]);
 498                break;
 499        case SHOW_FAN_ALARM:
 500                res = (data->alarms >> bitalarmfan[ix]) & 1;
 501                break;
 502        default:
 503                res = 0;
 504                dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
 505        }
 506
 507        return sprintf(buf, "%d\n", res);
 508}
 509
 510static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
 511                       const char *buf, size_t count)
 512{
 513        struct vt1211_data *data = dev_get_drvdata(dev);
 514        struct sensor_device_attribute_2 *sensor_attr_2 =
 515                                                to_sensor_dev_attr_2(attr);
 516        int ix = sensor_attr_2->index;
 517        int fn = sensor_attr_2->nr;
 518        long val = simple_strtol(buf, NULL, 10);
 519        int reg;
 520
 521        mutex_lock(&data->update_lock);
 522
 523        /* sync the data cache */
 524        reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
 525        data->fan_div[0] = (reg >> 4) & 3;
 526        data->fan_div[1] = (reg >> 6) & 3;
 527        data->fan_ctl = reg & 0xf;
 528
 529        switch (fn) {
 530        case SHOW_SET_FAN_MIN:
 531                data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]);
 532                vt1211_write8(data, VT1211_REG_FAN_MIN(ix),
 533                              data->fan_min[ix]);
 534                break;
 535        case SHOW_SET_FAN_DIV:
 536                switch (val) {
 537                        case 1: data->fan_div[ix] = 0; break;
 538                        case 2: data->fan_div[ix] = 1; break;
 539                        case 4: data->fan_div[ix] = 2; break;
 540                        case 8: data->fan_div[ix] = 3; break;
 541                        default:
 542                                count = -EINVAL;
 543                                dev_warn(dev, "fan div value %ld not "
 544                                         "supported. Choose one of 1, 2, "
 545                                         "4, or 8.\n", val);
 546                                goto EXIT;
 547                }
 548                vt1211_write8(data, VT1211_REG_FAN_DIV,
 549                              ((data->fan_div[1] << 6) |
 550                               (data->fan_div[0] << 4) |
 551                                data->fan_ctl));
 552                break;
 553        default:
 554                dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
 555        }
 556
 557EXIT:
 558        mutex_unlock(&data->update_lock);
 559        return count;
 560}
 561
 562/* ---------------------------------------------------------------------
 563 * PWM sysfs interfaces
 564 * ix = [0-1]
 565 * --------------------------------------------------------------------- */
 566
 567#define SHOW_PWM                        0
 568#define SHOW_SET_PWM_ENABLE             1
 569#define SHOW_SET_PWM_FREQ               2
 570#define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
 571
 572static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
 573                        char *buf)
 574{
 575        struct vt1211_data *data = vt1211_update_device(dev);
 576        struct sensor_device_attribute_2 *sensor_attr_2 =
 577                                                to_sensor_dev_attr_2(attr);
 578        int ix = sensor_attr_2->index;
 579        int fn = sensor_attr_2->nr;
 580        int res;
 581
 582        switch (fn) {
 583        case SHOW_PWM:
 584                res = data->pwm[ix];
 585                break;
 586        case SHOW_SET_PWM_ENABLE:
 587                res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
 588                break;
 589        case SHOW_SET_PWM_FREQ:
 590                res = 90000 >> (data->pwm_clk & 7);
 591                break;
 592        case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
 593                res = (data->pwm_ctl[ix] & 7) + 1;
 594                break;
 595        default:
 596                res = 0;
 597                dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
 598        }
 599
 600        return sprintf(buf, "%d\n", res);
 601}
 602
 603static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
 604                       const char *buf, size_t count)
 605{
 606        struct vt1211_data *data = dev_get_drvdata(dev);
 607        struct sensor_device_attribute_2 *sensor_attr_2 =
 608                                                to_sensor_dev_attr_2(attr);
 609        int ix = sensor_attr_2->index;
 610        int fn = sensor_attr_2->nr;
 611        long val = simple_strtol(buf, NULL, 10);
 612        int tmp, reg;
 613
 614        mutex_lock(&data->update_lock);
 615
 616        switch (fn) {
 617        case SHOW_SET_PWM_ENABLE:
 618                /* sync the data cache */
 619                reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
 620                data->fan_div[0] = (reg >> 4) & 3;
 621                data->fan_div[1] = (reg >> 6) & 3;
 622                data->fan_ctl = reg & 0xf;
 623                reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
 624                data->pwm_ctl[0] = reg & 0xf;
 625                data->pwm_ctl[1] = (reg >> 4) & 0xf;
 626                switch (val) {
 627                case 0:
 628                        data->pwm_ctl[ix] &= 7;
 629                        /* disable SmartGuardian if both PWM outputs are
 630                         * disabled */
 631                        if ((data->pwm_ctl[ix ^ 1] & 1) == 0) {
 632                                data->fan_ctl &= 0xe;
 633                        }
 634                        break;
 635                case 2:
 636                        data->pwm_ctl[ix] |= 8;
 637                        data->fan_ctl |= 1;
 638                        break;
 639                default:
 640                        count = -EINVAL;
 641                        dev_warn(dev, "pwm mode %ld not supported. "
 642                                 "Choose one of 0 or 2.\n", val);
 643                        goto EXIT;
 644                }
 645                vt1211_write8(data, VT1211_REG_PWM_CTL,
 646                              ((data->pwm_ctl[1] << 4) |
 647                                data->pwm_ctl[0]));
 648                vt1211_write8(data, VT1211_REG_FAN_DIV,
 649                              ((data->fan_div[1] << 6) |
 650                               (data->fan_div[0] << 4) |
 651                                data->fan_ctl));
 652                break;
 653        case SHOW_SET_PWM_FREQ:
 654                val = 135000 / SENSORS_LIMIT(val, 135000 >> 7, 135000);
 655                /* calculate tmp = log2(val) */
 656                tmp = 0;
 657                for (val >>= 1; val > 0; val >>= 1) {
 658                        tmp++;
 659                }
 660                /* sync the data cache */
 661                reg = vt1211_read8(data, VT1211_REG_PWM_CLK);
 662                data->pwm_clk = (reg & 0xf8) | tmp;
 663                vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk);
 664                break;
 665        case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
 666                if ((val < 1) || (val > 7)) {
 667                        count = -EINVAL;
 668                        dev_warn(dev, "temp channel %ld not supported. "
 669                                 "Choose a value between 1 and 7.\n", val);
 670                        goto EXIT;
 671                }
 672                if (!ISTEMP(val - 1, data->uch_config)) {
 673                        count = -EINVAL;
 674                        dev_warn(dev, "temp channel %ld is not available.\n",
 675                                 val);
 676                        goto EXIT;
 677                }
 678                /* sync the data cache */
 679                reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
 680                data->pwm_ctl[0] = reg & 0xf;
 681                data->pwm_ctl[1] = (reg >> 4) & 0xf;
 682                data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1);
 683                vt1211_write8(data, VT1211_REG_PWM_CTL,
 684                              ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0]));
 685                break;
 686        default:
 687                dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
 688        }
 689
 690EXIT:
 691        mutex_unlock(&data->update_lock);
 692        return count;
 693}
 694
 695/* ---------------------------------------------------------------------
 696 * PWM auto point definitions
 697 * ix = [0-1]
 698 * ap = [0-3]
 699 * --------------------------------------------------------------------- */
 700
 701/*
 702 * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
 703 * Note that there is only a single set of temp auto points that controls both
 704 * PWM controllers. We still create 2 sets of sysfs files to make it look
 705 * more consistent even though they map to the same registers.
 706 *
 707 * ix ap : description
 708 * -------------------
 709 * 0  0  : pwm1/2 off temperature        (pwm_auto_temp[0])
 710 * 0  1  : pwm1/2 low speed temperature  (pwm_auto_temp[1])
 711 * 0  2  : pwm1/2 high speed temperature (pwm_auto_temp[2])
 712 * 0  3  : pwm1/2 full speed temperature (pwm_auto_temp[3])
 713 * 1  0  : pwm1/2 off temperature        (pwm_auto_temp[0])
 714 * 1  1  : pwm1/2 low speed temperature  (pwm_auto_temp[1])
 715 * 1  2  : pwm1/2 high speed temperature (pwm_auto_temp[2])
 716 * 1  3  : pwm1/2 full speed temperature (pwm_auto_temp[3])
 717 */
 718
 719static ssize_t show_pwm_auto_point_temp(struct device *dev,
 720                                        struct device_attribute *attr,
 721                                        char *buf)
 722{
 723        struct vt1211_data *data = vt1211_update_device(dev);
 724        struct sensor_device_attribute_2 *sensor_attr_2 =
 725                                                to_sensor_dev_attr_2(attr);
 726        int ix = sensor_attr_2->index;
 727        int ap = sensor_attr_2->nr;
 728
 729        return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7,
 730                       data->pwm_auto_temp[ap]));
 731}
 732
 733static ssize_t set_pwm_auto_point_temp(struct device *dev,
 734                                       struct device_attribute *attr,
 735                                       const char *buf, size_t count)
 736{
 737        struct vt1211_data *data = dev_get_drvdata(dev);
 738        struct sensor_device_attribute_2 *sensor_attr_2 =
 739                                                to_sensor_dev_attr_2(attr);
 740        int ix = sensor_attr_2->index;
 741        int ap = sensor_attr_2->nr;
 742        long val = simple_strtol(buf, NULL, 10);
 743        int reg;
 744
 745        mutex_lock(&data->update_lock);
 746
 747        /* sync the data cache */
 748        reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
 749        data->pwm_ctl[0] = reg & 0xf;
 750        data->pwm_ctl[1] = (reg >> 4) & 0xf;
 751
 752        data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val);
 753        vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap),
 754                      data->pwm_auto_temp[ap]);
 755        mutex_unlock(&data->update_lock);
 756
 757        return count;
 758}
 759
 760/*
 761 * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
 762 * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
 763 * be changed.
 764 *
 765 * ix ap : description
 766 * -------------------
 767 * 0  0  : pwm1 off                   (pwm_auto_pwm[0][0], hard-wired to 0)
 768 * 0  1  : pwm1 low speed duty cycle  (pwm_auto_pwm[0][1])
 769 * 0  2  : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
 770 * 0  3  : pwm1 full speed            (pwm_auto_pwm[0][3], hard-wired to 255)
 771 * 1  0  : pwm2 off                   (pwm_auto_pwm[1][0], hard-wired to 0)
 772 * 1  1  : pwm2 low speed duty cycle  (pwm_auto_pwm[1][1])
 773 * 1  2  : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
 774 * 1  3  : pwm2 full speed            (pwm_auto_pwm[1][3], hard-wired to 255)
 775*/
 776
 777static ssize_t show_pwm_auto_point_pwm(struct device *dev,
 778                                       struct device_attribute *attr,
 779                                       char *buf)
 780{
 781        struct vt1211_data *data = vt1211_update_device(dev);
 782        struct sensor_device_attribute_2 *sensor_attr_2 =
 783                                                to_sensor_dev_attr_2(attr);
 784        int ix = sensor_attr_2->index;
 785        int ap = sensor_attr_2->nr;
 786
 787        return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]);
 788}
 789
 790static ssize_t set_pwm_auto_point_pwm(struct device *dev,
 791                                      struct device_attribute *attr,
 792                                      const char *buf, size_t count)
 793{
 794        struct vt1211_data *data = dev_get_drvdata(dev);
 795        struct sensor_device_attribute_2 *sensor_attr_2 =
 796                                                to_sensor_dev_attr_2(attr);
 797        int ix = sensor_attr_2->index;
 798        int ap = sensor_attr_2->nr;
 799        long val = simple_strtol(buf, NULL, 10);
 800
 801        if ((val < 0) || (val > 255)) {
 802                dev_err(dev, "pwm value %ld is out of range. "
 803                        "Choose a value between 0 and 255.\n" , val);
 804                return -EINVAL;
 805        }
 806
 807        mutex_lock(&data->update_lock);
 808        data->pwm_auto_pwm[ix][ap] = val;
 809        vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap),
 810                      data->pwm_auto_pwm[ix][ap]);
 811        mutex_unlock(&data->update_lock);
 812
 813        return count;
 814}
 815
 816/* ---------------------------------------------------------------------
 817 * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
 818 * --------------------------------------------------------------------- */
 819
 820static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
 821                        char *buf)
 822{
 823        struct vt1211_data *data = dev_get_drvdata(dev);
 824
 825        return sprintf(buf, "%d\n", data->vrm);
 826}
 827
 828static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
 829                       const char *buf, size_t count)
 830{
 831        struct vt1211_data *data = dev_get_drvdata(dev);
 832        long val = simple_strtol(buf, NULL, 10);
 833
 834        data->vrm = val;
 835
 836        return count;
 837}
 838
 839static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
 840                        char *buf)
 841{
 842        struct vt1211_data *data = dev_get_drvdata(dev);
 843
 844        return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
 845}
 846
 847static ssize_t show_name(struct device *dev,
 848                         struct device_attribute *attr, char *buf)
 849{
 850        struct vt1211_data *data = dev_get_drvdata(dev);
 851
 852        return sprintf(buf, "%s\n", data->name);
 853}
 854
 855static ssize_t show_alarms(struct device *dev,
 856                           struct device_attribute *attr, char *buf)
 857{
 858        struct vt1211_data *data = vt1211_update_device(dev);
 859
 860        return sprintf(buf, "%d\n", data->alarms);
 861}
 862
 863/* ---------------------------------------------------------------------
 864 * Device attribute structs
 865 * --------------------------------------------------------------------- */
 866
 867#define SENSOR_ATTR_IN_INPUT(ix) \
 868        SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
 869                show_in, NULL, SHOW_IN_INPUT, ix)
 870
 871static struct sensor_device_attribute_2 vt1211_sysfs_in_input[] = {
 872        SENSOR_ATTR_IN_INPUT(0),
 873        SENSOR_ATTR_IN_INPUT(1),
 874        SENSOR_ATTR_IN_INPUT(2),
 875        SENSOR_ATTR_IN_INPUT(3),
 876        SENSOR_ATTR_IN_INPUT(4),
 877        SENSOR_ATTR_IN_INPUT(5),
 878};
 879
 880#define SENSOR_ATTR_IN_MIN(ix) \
 881        SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
 882                show_in, set_in, SHOW_SET_IN_MIN, ix)
 883
 884static struct sensor_device_attribute_2 vt1211_sysfs_in_min[] = {
 885        SENSOR_ATTR_IN_MIN(0),
 886        SENSOR_ATTR_IN_MIN(1),
 887        SENSOR_ATTR_IN_MIN(2),
 888        SENSOR_ATTR_IN_MIN(3),
 889        SENSOR_ATTR_IN_MIN(4),
 890        SENSOR_ATTR_IN_MIN(5),
 891};
 892
 893#define SENSOR_ATTR_IN_MAX(ix) \
 894        SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
 895                show_in, set_in, SHOW_SET_IN_MAX, ix)
 896
 897static struct sensor_device_attribute_2 vt1211_sysfs_in_max[] = {
 898        SENSOR_ATTR_IN_MAX(0),
 899        SENSOR_ATTR_IN_MAX(1),
 900        SENSOR_ATTR_IN_MAX(2),
 901        SENSOR_ATTR_IN_MAX(3),
 902        SENSOR_ATTR_IN_MAX(4),
 903        SENSOR_ATTR_IN_MAX(5),
 904};
 905
 906#define SENSOR_ATTR_IN_ALARM(ix) \
 907        SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
 908                show_in, NULL, SHOW_IN_ALARM, ix)
 909
 910static struct sensor_device_attribute_2 vt1211_sysfs_in_alarm[] = {
 911        SENSOR_ATTR_IN_ALARM(0),
 912        SENSOR_ATTR_IN_ALARM(1),
 913        SENSOR_ATTR_IN_ALARM(2),
 914        SENSOR_ATTR_IN_ALARM(3),
 915        SENSOR_ATTR_IN_ALARM(4),
 916        SENSOR_ATTR_IN_ALARM(5),
 917};
 918
 919#define SENSOR_ATTR_TEMP_INPUT(ix) \
 920        SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
 921                show_temp, NULL, SHOW_TEMP_INPUT, ix-1)
 922
 923static struct sensor_device_attribute_2 vt1211_sysfs_temp_input[] = {
 924        SENSOR_ATTR_TEMP_INPUT(1),
 925        SENSOR_ATTR_TEMP_INPUT(2),
 926        SENSOR_ATTR_TEMP_INPUT(3),
 927        SENSOR_ATTR_TEMP_INPUT(4),
 928        SENSOR_ATTR_TEMP_INPUT(5),
 929        SENSOR_ATTR_TEMP_INPUT(6),
 930        SENSOR_ATTR_TEMP_INPUT(7),
 931};
 932
 933#define SENSOR_ATTR_TEMP_MAX(ix) \
 934        SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
 935                show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1)
 936
 937static struct sensor_device_attribute_2 vt1211_sysfs_temp_max[] = {
 938        SENSOR_ATTR_TEMP_MAX(1),
 939        SENSOR_ATTR_TEMP_MAX(2),
 940        SENSOR_ATTR_TEMP_MAX(3),
 941        SENSOR_ATTR_TEMP_MAX(4),
 942        SENSOR_ATTR_TEMP_MAX(5),
 943        SENSOR_ATTR_TEMP_MAX(6),
 944        SENSOR_ATTR_TEMP_MAX(7),
 945};
 946
 947#define SENSOR_ATTR_TEMP_MAX_HYST(ix) \
 948        SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
 949                show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1)
 950
 951static struct sensor_device_attribute_2 vt1211_sysfs_temp_max_hyst[] = {
 952        SENSOR_ATTR_TEMP_MAX_HYST(1),
 953        SENSOR_ATTR_TEMP_MAX_HYST(2),
 954        SENSOR_ATTR_TEMP_MAX_HYST(3),
 955        SENSOR_ATTR_TEMP_MAX_HYST(4),
 956        SENSOR_ATTR_TEMP_MAX_HYST(5),
 957        SENSOR_ATTR_TEMP_MAX_HYST(6),
 958        SENSOR_ATTR_TEMP_MAX_HYST(7),
 959};
 960
 961#define SENSOR_ATTR_TEMP_ALARM(ix) \
 962        SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
 963                show_temp, NULL, SHOW_TEMP_ALARM, ix-1)
 964
 965static struct sensor_device_attribute_2 vt1211_sysfs_temp_alarm[] = {
 966        SENSOR_ATTR_TEMP_ALARM(1),
 967        SENSOR_ATTR_TEMP_ALARM(2),
 968        SENSOR_ATTR_TEMP_ALARM(3),
 969        SENSOR_ATTR_TEMP_ALARM(4),
 970        SENSOR_ATTR_TEMP_ALARM(5),
 971        SENSOR_ATTR_TEMP_ALARM(6),
 972        SENSOR_ATTR_TEMP_ALARM(7),
 973};
 974
 975#define SENSOR_ATTR_FAN(ix) \
 976        SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
 977                show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
 978        SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
 979                show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
 980        SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
 981                show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
 982        SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
 983                show_fan, NULL, SHOW_FAN_ALARM, ix-1)
 984
 985#define SENSOR_ATTR_PWM(ix) \
 986        SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
 987                show_pwm, NULL, SHOW_PWM, ix-1), \
 988        SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
 989                show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
 990        SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
 991                show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
 992
 993#define SENSOR_ATTR_PWM_FREQ(ix) \
 994        SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
 995                show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
 996
 997#define SENSOR_ATTR_PWM_FREQ_RO(ix) \
 998        SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
 999                show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
1000
1001#define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
1002        SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
1003                show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
1004                ap-1, ix-1)
1005
1006#define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
1007        SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
1008                show_pwm_auto_point_temp, NULL, \
1009                ap-1, ix-1)
1010
1011#define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
1012        SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
1013                show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
1014                ap-1, ix-1)
1015
1016#define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
1017        SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
1018                show_pwm_auto_point_pwm, NULL, \
1019                ap-1, ix-1)
1020
1021static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = {
1022        SENSOR_ATTR_FAN(1),
1023        SENSOR_ATTR_FAN(2),
1024        SENSOR_ATTR_PWM(1),
1025        SENSOR_ATTR_PWM(2),
1026        SENSOR_ATTR_PWM_FREQ(1),
1027        SENSOR_ATTR_PWM_FREQ_RO(2),
1028        SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
1029        SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
1030        SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
1031        SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
1032        SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
1033        SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
1034        SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
1035        SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
1036        SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
1037        SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
1038        SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
1039        SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
1040        SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
1041        SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
1042        SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
1043        SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
1044};
1045
1046static struct device_attribute vt1211_sysfs_misc[] = {
1047        __ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm),
1048        __ATTR(cpu0_vid, S_IRUGO, show_vid, NULL),
1049        __ATTR(name, S_IRUGO, show_name, NULL),
1050        __ATTR(alarms, S_IRUGO, show_alarms, NULL),
1051};
1052
1053/* ---------------------------------------------------------------------
1054 * Device registration and initialization
1055 * --------------------------------------------------------------------- */
1056
1057static void __devinit vt1211_init_device(struct vt1211_data *data)
1058{
1059        /* set VRM */
1060        data->vrm = vid_which_vrm();
1061
1062        /* Read (and initialize) UCH config */
1063        data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG);
1064        if (uch_config > -1) {
1065                data->uch_config = (data->uch_config & 0x83) |
1066                                   (uch_config << 2);
1067                vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config);
1068        }
1069
1070        /* Initialize the interrupt mode (if request at module load time).
1071         * The VT1211 implements 3 different modes for clearing interrupts:
1072         * 0: Clear INT when status register is read. Regenerate INT as long
1073         *    as temp stays above hysteresis limit.
1074         * 1: Clear INT when status register is read. DON'T regenerate INT
1075         *    until temp falls below hysteresis limit and exceeds hot limit
1076         *    again.
1077         * 2: Clear INT when temp falls below max limit.
1078         *
1079         * The driver only allows to force mode 0 since that's the only one
1080         * that makes sense for 'sensors' */
1081        if (int_mode == 0) {
1082                vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0);
1083                vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0);
1084        }
1085
1086        /* Fill in some hard wired values into our data struct */
1087        data->pwm_auto_pwm[0][3] = 255;
1088        data->pwm_auto_pwm[1][3] = 255;
1089}
1090
1091static void vt1211_remove_sysfs(struct platform_device *pdev)
1092{
1093        struct device *dev = &pdev->dev;
1094        int i;
1095
1096        for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
1097                device_remove_file(dev,
1098                        &vt1211_sysfs_in_input[i].dev_attr);
1099                device_remove_file(dev,
1100                        &vt1211_sysfs_in_min[i].dev_attr);
1101                device_remove_file(dev,
1102                        &vt1211_sysfs_in_max[i].dev_attr);
1103                device_remove_file(dev,
1104                        &vt1211_sysfs_in_alarm[i].dev_attr);
1105        }
1106        for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
1107                device_remove_file(dev,
1108                        &vt1211_sysfs_temp_input[i].dev_attr);
1109                device_remove_file(dev,
1110                        &vt1211_sysfs_temp_max[i].dev_attr);
1111                device_remove_file(dev,
1112                        &vt1211_sysfs_temp_max_hyst[i].dev_attr);
1113                device_remove_file(dev,
1114                        &vt1211_sysfs_temp_alarm[i].dev_attr);
1115        }
1116        for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1117                device_remove_file(dev,
1118                        &vt1211_sysfs_fan_pwm[i].dev_attr);
1119        }
1120        for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1121                device_remove_file(dev, &vt1211_sysfs_misc[i]);
1122        }
1123}
1124
1125static int __devinit vt1211_probe(struct platform_device *pdev)
1126{
1127        struct device *dev = &pdev->dev;
1128        struct vt1211_data *data;
1129        struct resource *res;
1130        int i, err;
1131
1132        if (!(data = kzalloc(sizeof(struct vt1211_data), GFP_KERNEL))) {
1133                err = -ENOMEM;
1134                dev_err(dev, "Out of memory\n");
1135                goto EXIT;
1136        }
1137
1138        res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1139        if (!request_region(res->start, resource_size(res), DRVNAME)) {
1140                err = -EBUSY;
1141                dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1142                        (unsigned long)res->start, (unsigned long)res->end);
1143                goto EXIT_KFREE;
1144        }
1145        data->addr = res->start;
1146        data->name = DRVNAME;
1147        mutex_init(&data->update_lock);
1148
1149        platform_set_drvdata(pdev, data);
1150
1151        /* Initialize the VT1211 chip */
1152        vt1211_init_device(data);
1153
1154        /* Create sysfs interface files */
1155        for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
1156                if (ISVOLT(i, data->uch_config)) {
1157                        if ((err = device_create_file(dev,
1158                                &vt1211_sysfs_in_input[i].dev_attr)) ||
1159                            (err = device_create_file(dev,
1160                                &vt1211_sysfs_in_min[i].dev_attr)) ||
1161                            (err = device_create_file(dev,
1162                                &vt1211_sysfs_in_max[i].dev_attr)) ||
1163                            (err = device_create_file(dev,
1164                                &vt1211_sysfs_in_alarm[i].dev_attr))) {
1165                                goto EXIT_DEV_REMOVE;
1166                        }
1167                }
1168        }
1169        for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
1170                if (ISTEMP(i, data->uch_config)) {
1171                        if ((err = device_create_file(dev,
1172                                &vt1211_sysfs_temp_input[i].dev_attr)) ||
1173                            (err = device_create_file(dev,
1174                                &vt1211_sysfs_temp_max[i].dev_attr)) ||
1175                            (err = device_create_file(dev,
1176                                &vt1211_sysfs_temp_max_hyst[i].dev_attr)) ||
1177                            (err = device_create_file(dev,
1178                                &vt1211_sysfs_temp_alarm[i].dev_attr))) {
1179                                goto EXIT_DEV_REMOVE;
1180                        }
1181                }
1182        }
1183        for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1184                err = device_create_file(dev,
1185                        &vt1211_sysfs_fan_pwm[i].dev_attr);
1186                if (err) {
1187                        goto EXIT_DEV_REMOVE;
1188                }
1189        }
1190        for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1191                err = device_create_file(dev,
1192                       &vt1211_sysfs_misc[i]);
1193                if (err) {
1194                        goto EXIT_DEV_REMOVE;
1195                }
1196        }
1197
1198        /* Register device */
1199        data->hwmon_dev = hwmon_device_register(dev);
1200        if (IS_ERR(data->hwmon_dev)) {
1201                err = PTR_ERR(data->hwmon_dev);
1202                dev_err(dev, "Class registration failed (%d)\n", err);
1203                goto EXIT_DEV_REMOVE_SILENT;
1204        }
1205
1206        return 0;
1207
1208EXIT_DEV_REMOVE:
1209        dev_err(dev, "Sysfs interface creation failed (%d)\n", err);
1210EXIT_DEV_REMOVE_SILENT:
1211        vt1211_remove_sysfs(pdev);
1212        release_region(res->start, resource_size(res));
1213EXIT_KFREE:
1214        platform_set_drvdata(pdev, NULL);
1215        kfree(data);
1216EXIT:
1217        return err;
1218}
1219
1220static int __devexit vt1211_remove(struct platform_device *pdev)
1221{
1222        struct vt1211_data *data = platform_get_drvdata(pdev);
1223        struct resource *res;
1224
1225        hwmon_device_unregister(data->hwmon_dev);
1226        vt1211_remove_sysfs(pdev);
1227        platform_set_drvdata(pdev, NULL);
1228        kfree(data);
1229
1230        res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1231        release_region(res->start, resource_size(res));
1232
1233        return 0;
1234}
1235
1236static struct platform_driver vt1211_driver = {
1237        .driver = {
1238                .owner = THIS_MODULE,
1239                .name  = DRVNAME,
1240        },
1241        .probe  = vt1211_probe,
1242        .remove = __devexit_p(vt1211_remove),
1243};
1244
1245static int __init vt1211_device_add(unsigned short address)
1246{
1247        struct resource res = {
1248                .start  = address,
1249                .end    = address + 0x7f,
1250                .flags  = IORESOURCE_IO,
1251        };
1252        int err;
1253
1254        pdev = platform_device_alloc(DRVNAME, address);
1255        if (!pdev) {
1256                err = -ENOMEM;
1257                printk(KERN_ERR DRVNAME ": Device allocation failed (%d)\n",
1258                       err);
1259                goto EXIT;
1260        }
1261
1262        res.name = pdev->name;
1263        err = acpi_check_resource_conflict(&res);
1264        if (err)
1265                goto EXIT_DEV_PUT;
1266
1267        err = platform_device_add_resources(pdev, &res, 1);
1268        if (err) {
1269                printk(KERN_ERR DRVNAME ": Device resource addition failed "
1270                       "(%d)\n", err);
1271                goto EXIT_DEV_PUT;
1272        }
1273
1274        err = platform_device_add(pdev);
1275        if (err) {
1276                printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1277                       err);
1278                goto EXIT_DEV_PUT;
1279        }
1280
1281        return 0;
1282
1283EXIT_DEV_PUT:
1284        platform_device_put(pdev);
1285EXIT:
1286        return err;
1287}
1288
1289static int __init vt1211_find(int sio_cip, unsigned short *address)
1290{
1291        int err = -ENODEV;
1292        int devid;
1293
1294        superio_enter(sio_cip);
1295
1296        devid = force_id ? force_id : superio_inb(sio_cip, SIO_VT1211_DEVID);
1297        if (devid != SIO_VT1211_ID) {
1298                goto EXIT;
1299        }
1300
1301        superio_select(sio_cip, SIO_VT1211_LDN_HWMON);
1302
1303        if ((superio_inb(sio_cip, SIO_VT1211_ACTIVE) & 1) == 0) {
1304                printk(KERN_WARNING DRVNAME ": HW monitor is disabled, "
1305                       "skipping\n");
1306                goto EXIT;
1307        }
1308
1309        *address = ((superio_inb(sio_cip, SIO_VT1211_BADDR) << 8) |
1310                    (superio_inb(sio_cip, SIO_VT1211_BADDR + 1))) & 0xff00;
1311        if (*address == 0) {
1312                printk(KERN_WARNING DRVNAME ": Base address is not set, "
1313                       "skipping\n");
1314                goto EXIT;
1315        }
1316
1317        err = 0;
1318        printk(KERN_INFO DRVNAME ": Found VT1211 chip at 0x%04x, "
1319               "revision %u\n", *address,
1320               superio_inb(sio_cip, SIO_VT1211_DEVREV));
1321
1322EXIT:
1323        superio_exit(sio_cip);
1324        return err;
1325}
1326
1327static int __init vt1211_init(void)
1328{
1329        int err;
1330        unsigned short address = 0;
1331
1332        if ((err = vt1211_find(SIO_REG_CIP1, &address)) &&
1333            (err = vt1211_find(SIO_REG_CIP2, &address))) {
1334                goto EXIT;
1335        }
1336
1337        if ((uch_config < -1) || (uch_config > 31)) {
1338                err = -EINVAL;
1339                printk(KERN_WARNING DRVNAME ": Invalid UCH configuration %d. "
1340                       "Choose a value between 0 and 31.\n", uch_config);
1341          goto EXIT;
1342        }
1343
1344        if ((int_mode < -1) || (int_mode > 0)) {
1345                err = -EINVAL;
1346                printk(KERN_WARNING DRVNAME ": Invalid interrupt mode %d. "
1347                       "Only mode 0 is supported.\n", int_mode);
1348          goto EXIT;
1349        }
1350
1351        err = platform_driver_register(&vt1211_driver);
1352        if (err) {
1353                goto EXIT;
1354        }
1355
1356        /* Sets global pdev as a side effect */
1357        err = vt1211_device_add(address);
1358        if (err) {
1359                goto EXIT_DRV_UNREGISTER;
1360        }
1361
1362        return 0;
1363
1364EXIT_DRV_UNREGISTER:
1365        platform_driver_unregister(&vt1211_driver);
1366EXIT:
1367        return err;
1368}
1369
1370static void __exit vt1211_exit(void)
1371{
1372        platform_device_unregister(pdev);
1373        platform_driver_unregister(&vt1211_driver);
1374}
1375
1376MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
1377MODULE_DESCRIPTION("VT1211 sensors");
1378MODULE_LICENSE("GPL");
1379
1380module_init(vt1211_init);
1381module_exit(vt1211_exit);
1382