[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/drivers/hwmon/w83627hf.c

Version: -  FREEBSD  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  cheribsd  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD8-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
    3  *              monitoring
    4  * Copyright (c) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
    5  *                            Philip Edelbrock <phil@netroedge.com>,
    6  *                            and Mark Studebaker <mdsxyz123@yahoo.com>
    7  * Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
    8  * Copyright (c) 2007 - 1012  Jean Delvare <khali@linux-fr.org>
    9  *
   10  * This program is free software; you can redistribute it and/or modify
   11  * it under the terms of the GNU General Public License as published by
   12  * the Free Software Foundation; either version 2 of the License, or
   13  * (at your option) any later version.
   14  *
   15  * This program is distributed in the hope that it will be useful,
   16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18  * GNU General Public License for more details.
   19  *
   20  * You should have received a copy of the GNU General Public License
   21  * along with this program; if not, write to the Free Software
   22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   23  */
   24 
   25 /*
   26  * Supports following chips:
   27  *
   28  * Chip         #vin    #fanin  #pwm    #temp   wchipid vendid  i2c     ISA
   29  * w83627hf     9       3       2       3       0x20    0x5ca3  no      yes(LPC)
   30  * w83627thf    7       3       3       3       0x90    0x5ca3  no      yes(LPC)
   31  * w83637hf     7       3       3       3       0x80    0x5ca3  no      yes(LPC)
   32  * w83687thf    7       3       3       3       0x90    0x5ca3  no      yes(LPC)
   33  * w83697hf     8       2       2       2       0x60    0x5ca3  no      yes(LPC)
   34  *
   35  * For other winbond chips, and for i2c support in the above chips,
   36  * use w83781d.c.
   37  *
   38  * Note: automatic ("cruise") fan control for 697, 637 & 627thf not
   39  * supported yet.
   40  */
   41 
   42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   43 
   44 #include <linux/module.h>
   45 #include <linux/init.h>
   46 #include <linux/slab.h>
   47 #include <linux/jiffies.h>
   48 #include <linux/platform_device.h>
   49 #include <linux/hwmon.h>
   50 #include <linux/hwmon-sysfs.h>
   51 #include <linux/hwmon-vid.h>
   52 #include <linux/err.h>
   53 #include <linux/mutex.h>
   54 #include <linux/ioport.h>
   55 #include <linux/acpi.h>
   56 #include <linux/io.h>
   57 #include "lm75.h"
   58 
   59 static struct platform_device *pdev;
   60 
   61 #define DRVNAME "w83627hf"
   62 enum chips { w83627hf, w83627thf, w83697hf, w83637hf, w83687thf };
   63 
   64 struct w83627hf_sio_data {
   65         enum chips type;
   66         int sioaddr;
   67 };
   68 
   69 static u8 force_i2c = 0x1f;
   70 module_param(force_i2c, byte, 0);
   71 MODULE_PARM_DESC(force_i2c,
   72                  "Initialize the i2c address of the sensors");
   73 
   74 static bool init = 1;
   75 module_param(init, bool, 0);
   76 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
   77 
   78 static unsigned short force_id;
   79 module_param(force_id, ushort, 0);
   80 MODULE_PARM_DESC(force_id, "Override the detected device ID");
   81 
   82 /* modified from kernel/include/traps.c */
   83 #define DEV                     0x07 /* Register: Logical device select */
   84 
   85 /* logical device numbers for superio_select (below) */
   86 #define W83627HF_LD_FDC         0x00
   87 #define W83627HF_LD_PRT         0x01
   88 #define W83627HF_LD_UART1       0x02
   89 #define W83627HF_LD_UART2       0x03
   90 #define W83627HF_LD_KBC         0x05
   91 #define W83627HF_LD_CIR         0x06 /* w83627hf only */
   92 #define W83627HF_LD_GAME        0x07
   93 #define W83627HF_LD_MIDI        0x07
   94 #define W83627HF_LD_GPIO1       0x07
   95 #define W83627HF_LD_GPIO5       0x07 /* w83627thf only */
   96 #define W83627HF_LD_GPIO2       0x08
   97 #define W83627HF_LD_GPIO3       0x09
   98 #define W83627HF_LD_GPIO4       0x09 /* w83627thf only */
   99 #define W83627HF_LD_ACPI        0x0a
  100 #define W83627HF_LD_HWM         0x0b
  101 
  102 #define DEVID                   0x20 /* Register: Device ID */
  103 
  104 #define W83627THF_GPIO5_EN      0x30 /* w83627thf only */
  105 #define W83627THF_GPIO5_IOSR    0xf3 /* w83627thf only */
  106 #define W83627THF_GPIO5_DR      0xf4 /* w83627thf only */
  107 
  108 #define W83687THF_VID_EN        0x29 /* w83687thf only */
  109 #define W83687THF_VID_CFG       0xF0 /* w83687thf only */
  110 #define W83687THF_VID_DATA      0xF1 /* w83687thf only */
  111 
  112 static inline void
  113 superio_outb(struct w83627hf_sio_data *sio, int reg, int val)
  114 {
  115         outb(reg, sio->sioaddr);
  116         outb(val, sio->sioaddr + 1);
  117 }
  118 
  119 static inline int
  120 superio_inb(struct w83627hf_sio_data *sio, int reg)
  121 {
  122         outb(reg, sio->sioaddr);
  123         return inb(sio->sioaddr + 1);
  124 }
  125 
  126 static inline void
  127 superio_select(struct w83627hf_sio_data *sio, int ld)
  128 {
  129         outb(DEV, sio->sioaddr);
  130         outb(ld,  sio->sioaddr + 1);
  131 }
  132 
  133 static inline void
  134 superio_enter(struct w83627hf_sio_data *sio)
  135 {
  136         outb(0x87, sio->sioaddr);
  137         outb(0x87, sio->sioaddr);
  138 }
  139 
  140 static inline void
  141 superio_exit(struct w83627hf_sio_data *sio)
  142 {
  143         outb(0xAA, sio->sioaddr);
  144 }
  145 
  146 #define W627_DEVID 0x52
  147 #define W627THF_DEVID 0x82
  148 #define W697_DEVID 0x60
  149 #define W637_DEVID 0x70
  150 #define W687THF_DEVID 0x85
  151 #define WINB_ACT_REG 0x30
  152 #define WINB_BASE_REG 0x60
  153 /* Constants specified below */
  154 
  155 /* Alignment of the base address */
  156 #define WINB_ALIGNMENT          ~7
  157 
  158 /* Offset & size of I/O region we are interested in */
  159 #define WINB_REGION_OFFSET      5
  160 #define WINB_REGION_SIZE        2
  161 
  162 /* Where are the sensors address/data registers relative to the region offset */
  163 #define W83781D_ADDR_REG_OFFSET 0
  164 #define W83781D_DATA_REG_OFFSET 1
  165 
  166 /* The W83781D registers */
  167 /* The W83782D registers for nr=7,8 are in bank 5 */
  168 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
  169                                            (0x554 + (((nr) - 7) * 2)))
  170 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
  171                                            (0x555 + (((nr) - 7) * 2)))
  172 #define W83781D_REG_IN(nr)     ((nr < 7) ? (0x20 + (nr)) : \
  173                                            (0x550 + (nr) - 7))
  174 
  175 /* nr:0-2 for fans:1-3 */
  176 #define W83627HF_REG_FAN_MIN(nr)        (0x3b + (nr))
  177 #define W83627HF_REG_FAN(nr)            (0x28 + (nr))
  178 
  179 #define W83627HF_REG_TEMP2_CONFIG 0x152
  180 #define W83627HF_REG_TEMP3_CONFIG 0x252
  181 /* these are zero-based, unlike config constants above */
  182 static const u16 w83627hf_reg_temp[]            = { 0x27, 0x150, 0x250 };
  183 static const u16 w83627hf_reg_temp_hyst[]       = { 0x3A, 0x153, 0x253 };
  184 static const u16 w83627hf_reg_temp_over[]       = { 0x39, 0x155, 0x255 };
  185 
  186 #define W83781D_REG_BANK 0x4E
  187 
  188 #define W83781D_REG_CONFIG 0x40
  189 #define W83781D_REG_ALARM1 0x459
  190 #define W83781D_REG_ALARM2 0x45A
  191 #define W83781D_REG_ALARM3 0x45B
  192 
  193 #define W83781D_REG_BEEP_CONFIG 0x4D
  194 #define W83781D_REG_BEEP_INTS1 0x56
  195 #define W83781D_REG_BEEP_INTS2 0x57
  196 #define W83781D_REG_BEEP_INTS3 0x453
  197 
  198 #define W83781D_REG_VID_FANDIV 0x47
  199 
  200 #define W83781D_REG_CHIPID 0x49
  201 #define W83781D_REG_WCHIPID 0x58
  202 #define W83781D_REG_CHIPMAN 0x4F
  203 #define W83781D_REG_PIN 0x4B
  204 
  205 #define W83781D_REG_VBAT 0x5D
  206 
  207 #define W83627HF_REG_PWM1 0x5A
  208 #define W83627HF_REG_PWM2 0x5B
  209 
  210 static const u8 W83627THF_REG_PWM_ENABLE[] = {
  211         0x04,           /* FAN 1 mode */
  212         0x04,           /* FAN 2 mode */
  213         0x12,           /* FAN AUX mode */
  214 };
  215 static const u8 W83627THF_PWM_ENABLE_SHIFT[] = { 2, 4, 1 };
  216 
  217 #define W83627THF_REG_PWM1              0x01    /* 697HF/637HF/687THF too */
  218 #define W83627THF_REG_PWM2              0x03    /* 697HF/637HF/687THF too */
  219 #define W83627THF_REG_PWM3              0x11    /* 637HF/687THF too */
  220 
  221 #define W83627THF_REG_VRM_OVT_CFG       0x18    /* 637HF/687THF too */
  222 
  223 static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
  224 static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
  225                              W83627THF_REG_PWM3 };
  226 #define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
  227                                     regpwm_627hf[nr] : regpwm[nr])
  228 
  229 #define W83627HF_REG_PWM_FREQ           0x5C    /* Only for the 627HF */
  230 
  231 #define W83637HF_REG_PWM_FREQ1          0x00    /* 697HF/687THF too */
  232 #define W83637HF_REG_PWM_FREQ2          0x02    /* 697HF/687THF too */
  233 #define W83637HF_REG_PWM_FREQ3          0x10    /* 687THF too */
  234 
  235 static const u8 W83637HF_REG_PWM_FREQ[] = { W83637HF_REG_PWM_FREQ1,
  236                                         W83637HF_REG_PWM_FREQ2,
  237                                         W83637HF_REG_PWM_FREQ3 };
  238 
  239 #define W83627HF_BASE_PWM_FREQ  46870
  240 
  241 #define W83781D_REG_I2C_ADDR 0x48
  242 #define W83781D_REG_I2C_SUBADDR 0x4A
  243 
  244 /* Sensor selection */
  245 #define W83781D_REG_SCFG1 0x5D
  246 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
  247 #define W83781D_REG_SCFG2 0x59
  248 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
  249 #define W83781D_DEFAULT_BETA 3435
  250 
  251 /*
  252  * Conversions. Limit checking is only done on the TO_REG
  253  * variants. Note that you should be a bit careful with which arguments
  254  * these macros are called: arguments may be evaluated more than once.
  255  * Fixing this is just not worth it.
  256  */
  257 #define IN_TO_REG(val)  (SENSORS_LIMIT((((val) + 8)/16),0,255))
  258 #define IN_FROM_REG(val) ((val) * 16)
  259 
  260 static inline u8 FAN_TO_REG(long rpm, int div)
  261 {
  262         if (rpm == 0)
  263                 return 255;
  264         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  265         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
  266                              254);
  267 }
  268 
  269 #define TEMP_MIN (-128000)
  270 #define TEMP_MAX ( 127000)
  271 
  272 /*
  273  * TEMP: 0.001C/bit (-128C to +127C)
  274  * REG: 1C/bit, two's complement
  275  */
  276 static u8 TEMP_TO_REG(long temp)
  277 {
  278         int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
  279         ntemp += (ntemp<0 ? -500 : 500);
  280         return (u8)(ntemp / 1000);
  281 }
  282 
  283 static int TEMP_FROM_REG(u8 reg)
  284 {
  285         return (s8)reg * 1000;
  286 }
  287 
  288 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
  289 
  290 #define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
  291 
  292 static inline unsigned long pwm_freq_from_reg_627hf(u8 reg)
  293 {
  294         unsigned long freq;
  295         freq = W83627HF_BASE_PWM_FREQ >> reg;
  296         return freq;
  297 }
  298 static inline u8 pwm_freq_to_reg_627hf(unsigned long val)
  299 {
  300         u8 i;
  301         /*
  302          * Only 5 dividers (1 2 4 8 16)
  303          * Search for the nearest available frequency
  304          */
  305         for (i = 0; i < 4; i++) {
  306                 if (val > (((W83627HF_BASE_PWM_FREQ >> i) +
  307                             (W83627HF_BASE_PWM_FREQ >> (i+1))) / 2))
  308                         break;
  309         }
  310         return i;
  311 }
  312 
  313 static inline unsigned long pwm_freq_from_reg(u8 reg)
  314 {
  315         /* Clock bit 8 -> 180 kHz or 24 MHz */
  316         unsigned long clock = (reg & 0x80) ? 180000UL : 24000000UL;
  317 
  318         reg &= 0x7f;
  319         /* This should not happen but anyway... */
  320         if (reg == 0)
  321                 reg++;
  322         return clock / (reg << 8);
  323 }
  324 static inline u8 pwm_freq_to_reg(unsigned long val)
  325 {
  326         /* Minimum divider value is 0x01 and maximum is 0x7F */
  327         if (val >= 93750)       /* The highest we can do */
  328                 return 0x01;
  329         if (val >= 720) /* Use 24 MHz clock */
  330                 return 24000000UL / (val << 8);
  331         if (val < 6)            /* The lowest we can do */
  332                 return 0xFF;
  333         else                    /* Use 180 kHz clock */
  334                 return 0x80 | (180000UL / (val << 8));
  335 }
  336 
  337 #define BEEP_MASK_FROM_REG(val)         ((val) & 0xff7fff)
  338 #define BEEP_MASK_TO_REG(val)           ((val) & 0xff7fff)
  339 
  340 #define DIV_FROM_REG(val) (1 << (val))
  341 
  342 static inline u8 DIV_TO_REG(long val)
  343 {
  344         int i;
  345         val = SENSORS_LIMIT(val, 1, 128) >> 1;
  346         for (i = 0; i < 7; i++) {
  347                 if (val == 0)
  348                         break;
  349                 val >>= 1;
  350         }
  351         return (u8)i;
  352 }
  353 
  354 /*
  355  * For each registered chip, we need to keep some data in memory.
  356  * The structure is dynamically allocated.
  357  */
  358 struct w83627hf_data {
  359         unsigned short addr;
  360         const char *name;
  361         struct device *hwmon_dev;
  362         struct mutex lock;
  363         enum chips type;
  364 
  365         struct mutex update_lock;
  366         char valid;             /* !=0 if following fields are valid */
  367         unsigned long last_updated;     /* In jiffies */
  368 
  369         u8 in[9];               /* Register value */
  370         u8 in_max[9];           /* Register value */
  371         u8 in_min[9];           /* Register value */
  372         u8 fan[3];              /* Register value */
  373         u8 fan_min[3];          /* Register value */
  374         u16 temp[3];            /* Register value */
  375         u16 temp_max[3];        /* Register value */
  376         u16 temp_max_hyst[3];   /* Register value */
  377         u8 fan_div[3];          /* Register encoding, shifted right */
  378         u8 vid;                 /* Register encoding, combined */
  379         u32 alarms;             /* Register encoding, combined */
  380         u32 beep_mask;          /* Register encoding, combined */
  381         u8 pwm[3];              /* Register value */
  382         u8 pwm_enable[3];       /* 1 = manual
  383                                  * 2 = thermal cruise (also called SmartFan I)
  384                                  * 3 = fan speed cruise
  385                                  */
  386         u8 pwm_freq[3];         /* Register value */
  387         u16 sens[3];            /* 1 = pentium diode; 2 = 3904 diode;
  388                                  * 4 = thermistor
  389                                  */
  390         u8 vrm;
  391         u8 vrm_ovt;             /* Register value, 627THF/637HF/687THF only */
  392 
  393 #ifdef CONFIG_PM
  394         /* Remember extra register values over suspend/resume */
  395         u8 scfg1;
  396         u8 scfg2;
  397 #endif
  398 };
  399 
  400 
  401 static int w83627hf_probe(struct platform_device *pdev);
  402 static int w83627hf_remove(struct platform_device *pdev);
  403 
  404 static int w83627hf_read_value(struct w83627hf_data *data, u16 reg);
  405 static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value);
  406 static void w83627hf_update_fan_div(struct w83627hf_data *data);
  407 static struct w83627hf_data *w83627hf_update_device(struct device *dev);
  408 static void w83627hf_init_device(struct platform_device *pdev);
  409 
  410 #ifdef CONFIG_PM
  411 static int w83627hf_suspend(struct device *dev)
  412 {
  413         struct w83627hf_data *data = w83627hf_update_device(dev);
  414 
  415         mutex_lock(&data->update_lock);
  416         data->scfg1 = w83627hf_read_value(data, W83781D_REG_SCFG1);
  417         data->scfg2 = w83627hf_read_value(data, W83781D_REG_SCFG2);
  418         mutex_unlock(&data->update_lock);
  419 
  420         return 0;
  421 }
  422 
  423 static int w83627hf_resume(struct device *dev)
  424 {
  425         struct w83627hf_data *data = dev_get_drvdata(dev);
  426         int i, num_temps = (data->type == w83697hf) ? 2 : 3;
  427 
  428         /* Restore limits */
  429         mutex_lock(&data->update_lock);
  430         for (i = 0; i <= 8; i++) {
  431                 /* skip missing sensors */
  432                 if (((data->type == w83697hf) && (i == 1)) ||
  433                     ((data->type != w83627hf && data->type != w83697hf)
  434                     && (i == 5 || i == 6)))
  435                         continue;
  436                 w83627hf_write_value(data, W83781D_REG_IN_MAX(i),
  437                                      data->in_max[i]);
  438                 w83627hf_write_value(data, W83781D_REG_IN_MIN(i),
  439                                      data->in_min[i]);
  440         }
  441         for (i = 0; i <= 2; i++)
  442                 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(i),
  443                                      data->fan_min[i]);
  444         for (i = 0; i < num_temps; i++) {
  445                 w83627hf_write_value(data, w83627hf_reg_temp_over[i],
  446                                      data->temp_max[i]);
  447                 w83627hf_write_value(data, w83627hf_reg_temp_hyst[i],
  448                                      data->temp_max_hyst[i]);
  449         }
  450 
  451         /* Fixup BIOS bugs */
  452         if (data->type == w83627thf || data->type == w83637hf ||
  453             data->type == w83687thf)
  454                 w83627hf_write_value(data, W83627THF_REG_VRM_OVT_CFG,
  455                                      data->vrm_ovt);
  456         w83627hf_write_value(data, W83781D_REG_SCFG1, data->scfg1);
  457         w83627hf_write_value(data, W83781D_REG_SCFG2, data->scfg2);
  458 
  459         /* Force re-reading all values */
  460         data->valid = 0;
  461         mutex_unlock(&data->update_lock);
  462 
  463         return 0;
  464 }
  465 
  466 static const struct dev_pm_ops w83627hf_dev_pm_ops = {
  467         .suspend = w83627hf_suspend,
  468         .resume = w83627hf_resume,
  469 };
  470 
  471 #define W83627HF_DEV_PM_OPS     (&w83627hf_dev_pm_ops)
  472 #else
  473 #define W83627HF_DEV_PM_OPS     NULL
  474 #endif /* CONFIG_PM */
  475 
  476 static struct platform_driver w83627hf_driver = {
  477         .driver = {
  478                 .owner  = THIS_MODULE,
  479                 .name   = DRVNAME,
  480                 .pm     = W83627HF_DEV_PM_OPS,
  481         },
  482         .probe          = w83627hf_probe,
  483         .remove         = w83627hf_remove,
  484 };
  485 
  486 static ssize_t
  487 show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
  488 {
  489         int nr = to_sensor_dev_attr(devattr)->index;
  490         struct w83627hf_data *data = w83627hf_update_device(dev);
  491         return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in[nr]));
  492 }
  493 static ssize_t
  494 show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
  495 {
  496         int nr = to_sensor_dev_attr(devattr)->index;
  497         struct w83627hf_data *data = w83627hf_update_device(dev);
  498         return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_min[nr]));
  499 }
  500 static ssize_t
  501 show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
  502 {
  503         int nr = to_sensor_dev_attr(devattr)->index;
  504         struct w83627hf_data *data = w83627hf_update_device(dev);
  505         return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
  506 }
  507 static ssize_t
  508 store_in_min(struct device *dev, struct device_attribute *devattr,
  509              const char *buf, size_t count)
  510 {
  511         int nr = to_sensor_dev_attr(devattr)->index;
  512         struct w83627hf_data *data = dev_get_drvdata(dev);
  513         long val;
  514         int err;
  515 
  516         err = kstrtol(buf, 10, &val);
  517         if (err)
  518                 return err;
  519 
  520         mutex_lock(&data->update_lock);
  521         data->in_min[nr] = IN_TO_REG(val);
  522         w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
  523         mutex_unlock(&data->update_lock);
  524         return count;
  525 }
  526 static ssize_t
  527 store_in_max(struct device *dev, struct device_attribute *devattr,
  528              const char *buf, size_t count)
  529 {
  530         int nr = to_sensor_dev_attr(devattr)->index;
  531         struct w83627hf_data *data = dev_get_drvdata(dev);
  532         long val;
  533         int err;
  534 
  535         err = kstrtol(buf, 10, &val);
  536         if (err)
  537                 return err;
  538 
  539         mutex_lock(&data->update_lock);
  540         data->in_max[nr] = IN_TO_REG(val);
  541         w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
  542         mutex_unlock(&data->update_lock);
  543         return count;
  544 }
  545 #define sysfs_vin_decl(offset) \
  546 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO,          \
  547                           show_in_input, NULL, offset);         \
  548 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO|S_IWUSR,    \
  549                           show_in_min, store_in_min, offset);   \
  550 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO|S_IWUSR,    \
  551                           show_in_max, store_in_max, offset);
  552 
  553 sysfs_vin_decl(1);
  554 sysfs_vin_decl(2);
  555 sysfs_vin_decl(3);
  556 sysfs_vin_decl(4);
  557 sysfs_vin_decl(5);
  558 sysfs_vin_decl(6);
  559 sysfs_vin_decl(7);
  560 sysfs_vin_decl(8);
  561 
  562 /* use a different set of functions for in0 */
  563 static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
  564 {
  565         long in0;
  566 
  567         if ((data->vrm_ovt & 0x01) &&
  568                 (w83627thf == data->type || w83637hf == data->type
  569                  || w83687thf == data->type))
  570 
  571                 /* use VRM9 calculation */
  572                 in0 = (long)((reg * 488 + 70000 + 50) / 100);
  573         else
  574                 /* use VRM8 (standard) calculation */
  575                 in0 = (long)IN_FROM_REG(reg);
  576 
  577         return sprintf(buf,"%ld\n", in0);
  578 }
  579 
  580 static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
  581 {
  582         struct w83627hf_data *data = w83627hf_update_device(dev);
  583         return show_in_0(data, buf, data->in[0]);
  584 }
  585 
  586 static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
  587 {
  588         struct w83627hf_data *data = w83627hf_update_device(dev);
  589         return show_in_0(data, buf, data->in_min[0]);
  590 }
  591 
  592 static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
  593 {
  594         struct w83627hf_data *data = w83627hf_update_device(dev);
  595         return show_in_0(data, buf, data->in_max[0]);
  596 }
  597 
  598 static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
  599         const char *buf, size_t count)
  600 {
  601         struct w83627hf_data *data = dev_get_drvdata(dev);
  602         unsigned long val;
  603         int err;
  604 
  605         err = kstrtoul(buf, 10, &val);
  606         if (err)
  607                 return err;
  608 
  609         mutex_lock(&data->update_lock);
  610         
  611         if ((data->vrm_ovt & 0x01) &&
  612                 (w83627thf == data->type || w83637hf == data->type
  613                  || w83687thf == data->type))
  614 
  615                 /* use VRM9 calculation */
  616                 data->in_min[0] =
  617                         SENSORS_LIMIT(((val * 100) - 70000 + 244) / 488, 0,
  618                                         255);
  619         else
  620                 /* use VRM8 (standard) calculation */
  621                 data->in_min[0] = IN_TO_REG(val);
  622 
  623         w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
  624         mutex_unlock(&data->update_lock);
  625         return count;
  626 }
  627 
  628 static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
  629         const char *buf, size_t count)
  630 {
  631         struct w83627hf_data *data = dev_get_drvdata(dev);
  632         unsigned long val;
  633         int err;
  634 
  635         err = kstrtoul(buf, 10, &val);
  636         if (err)
  637                 return err;
  638 
  639         mutex_lock(&data->update_lock);
  640 
  641         if ((data->vrm_ovt & 0x01) &&
  642                 (w83627thf == data->type || w83637hf == data->type
  643                  || w83687thf == data->type))
  644                 
  645                 /* use VRM9 calculation */
  646                 data->in_max[0] =
  647                         SENSORS_LIMIT(((val * 100) - 70000 + 244) / 488, 0,
  648                                         255);
  649         else
  650                 /* use VRM8 (standard) calculation */
  651                 data->in_max[0] = IN_TO_REG(val);
  652 
  653         w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
  654         mutex_unlock(&data->update_lock);
  655         return count;
  656 }
  657 
  658 static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
  659 static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
  660         show_regs_in_min0, store_regs_in_min0);
  661 static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
  662         show_regs_in_max0, store_regs_in_max0);
  663 
  664 static ssize_t
  665 show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
  666 {
  667         int nr = to_sensor_dev_attr(devattr)->index;
  668         struct w83627hf_data *data = w83627hf_update_device(dev);
  669         return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
  670                                 (long)DIV_FROM_REG(data->fan_div[nr])));
  671 }
  672 static ssize_t
  673 show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
  674 {
  675         int nr = to_sensor_dev_attr(devattr)->index;
  676         struct w83627hf_data *data = w83627hf_update_device(dev);
  677         return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
  678                                 (long)DIV_FROM_REG(data->fan_div[nr])));
  679 }
  680 static ssize_t
  681 store_fan_min(struct device *dev, struct device_attribute *devattr,
  682               const char *buf, size_t count)
  683 {
  684         int nr = to_sensor_dev_attr(devattr)->index;
  685         struct w83627hf_data *data = dev_get_drvdata(dev);
  686         unsigned long val;
  687         int err;
  688 
  689         err = kstrtoul(buf, 10, &val);
  690         if (err)
  691                 return err;
  692 
  693         mutex_lock(&data->update_lock);
  694         data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  695         w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
  696                              data->fan_min[nr]);
  697 
  698         mutex_unlock(&data->update_lock);
  699         return count;
  700 }
  701 #define sysfs_fan_decl(offset)  \
  702 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO,                 \
  703                           show_fan_input, NULL, offset - 1);            \
  704 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,         \
  705                           show_fan_min, store_fan_min, offset - 1);
  706 
  707 sysfs_fan_decl(1);
  708 sysfs_fan_decl(2);
  709 sysfs_fan_decl(3);
  710 
  711 static ssize_t
  712 show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
  713 {
  714         int nr = to_sensor_dev_attr(devattr)->index;
  715         struct w83627hf_data *data = w83627hf_update_device(dev);
  716 
  717         u16 tmp = data->temp[nr];
  718         return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
  719                                           : (long) TEMP_FROM_REG(tmp));
  720 }
  721 
  722 static ssize_t
  723 show_temp_max(struct device *dev, struct device_attribute *devattr,
  724               char *buf)
  725 {
  726         int nr = to_sensor_dev_attr(devattr)->index;
  727         struct w83627hf_data *data = w83627hf_update_device(dev);
  728 
  729         u16 tmp = data->temp_max[nr];
  730         return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
  731                                           : (long) TEMP_FROM_REG(tmp));
  732 }
  733 
  734 static ssize_t
  735 show_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
  736                    char *buf)
  737 {
  738         int nr = to_sensor_dev_attr(devattr)->index;
  739         struct w83627hf_data *data = w83627hf_update_device(dev);
  740 
  741         u16 tmp = data->temp_max_hyst[nr];
  742         return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
  743                                           : (long) TEMP_FROM_REG(tmp));
  744 }
  745 
  746 static ssize_t
  747 store_temp_max(struct device *dev, struct device_attribute *devattr,
  748                const char *buf, size_t count)
  749 {
  750         int nr = to_sensor_dev_attr(devattr)->index;
  751         struct w83627hf_data *data = dev_get_drvdata(dev);
  752         u16 tmp;
  753         long val;
  754         int err;
  755 
  756         err = kstrtol(buf, 10, &val);
  757         if (err)
  758                 return err;
  759 
  760         tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
  761         mutex_lock(&data->update_lock);
  762         data->temp_max[nr] = tmp;
  763         w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
  764         mutex_unlock(&data->update_lock);
  765         return count;
  766 }
  767 
  768 static ssize_t
  769 store_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
  770                     const char *buf, size_t count)
  771 {
  772         int nr = to_sensor_dev_attr(devattr)->index;
  773         struct w83627hf_data *data = dev_get_drvdata(dev);
  774         u16 tmp;
  775         long val;
  776         int err;
  777 
  778         err = kstrtol(buf, 10, &val);
  779         if (err)
  780                 return err;
  781 
  782         tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
  783         mutex_lock(&data->update_lock);
  784         data->temp_max_hyst[nr] = tmp;
  785         w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
  786         mutex_unlock(&data->update_lock);
  787         return count;
  788 }
  789 
  790 #define sysfs_temp_decl(offset) \
  791 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO,                \
  792                           show_temp, NULL, offset - 1);                 \
  793 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO|S_IWUSR,          \
  794                           show_temp_max, store_temp_max, offset - 1);   \
  795 static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO|S_IWUSR,     \
  796                           show_temp_max_hyst, store_temp_max_hyst, offset - 1);
  797 
  798 sysfs_temp_decl(1);
  799 sysfs_temp_decl(2);
  800 sysfs_temp_decl(3);
  801 
  802 static ssize_t
  803 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
  804 {
  805         struct w83627hf_data *data = w83627hf_update_device(dev);
  806         return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
  807 }
  808 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  809 
  810 static ssize_t
  811 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
  812 {
  813         struct w83627hf_data *data = dev_get_drvdata(dev);
  814         return sprintf(buf, "%ld\n", (long) data->vrm);
  815 }
  816 static ssize_t
  817 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  818 {
  819         struct w83627hf_data *data = dev_get_drvdata(dev);
  820         unsigned long val;
  821         int err;
  822 
  823         err = kstrtoul(buf, 10, &val);
  824         if (err)
  825                 return err;
  826         data->vrm = val;
  827 
  828         return count;
  829 }
  830 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  831 
  832 static ssize_t
  833 show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
  834 {
  835         struct w83627hf_data *data = w83627hf_update_device(dev);
  836         return sprintf(buf, "%ld\n", (long) data->alarms);
  837 }
  838 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
  839 
  840 static ssize_t
  841 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
  842 {
  843         struct w83627hf_data *data = w83627hf_update_device(dev);
  844         int bitnr = to_sensor_dev_attr(attr)->index;
  845         return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  846 }
  847 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  848 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  849 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  850 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  851 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  852 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
  853 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
  854 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
  855 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
  856 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  857 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  858 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
  859 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  860 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
  861 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
  862 
  863 static ssize_t
  864 show_beep_mask(struct device *dev, struct device_attribute *attr, char *buf)
  865 {
  866         struct w83627hf_data *data = w83627hf_update_device(dev);
  867         return sprintf(buf, "%ld\n",
  868                       (long)BEEP_MASK_FROM_REG(data->beep_mask));
  869 }
  870 
  871 static ssize_t
  872 store_beep_mask(struct device *dev, struct device_attribute *attr,
  873                 const char *buf, size_t count)
  874 {
  875         struct w83627hf_data *data = dev_get_drvdata(dev);
  876         unsigned long val;
  877         int err;
  878 
  879         err = kstrtoul(buf, 10, &val);
  880         if (err)
  881                 return err;
  882 
  883         mutex_lock(&data->update_lock);
  884 
  885         /* preserve beep enable */
  886         data->beep_mask = (data->beep_mask & 0x8000)
  887                         | BEEP_MASK_TO_REG(val);
  888         w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
  889                             data->beep_mask & 0xff);
  890         w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
  891                             ((data->beep_mask) >> 16) & 0xff);
  892         w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
  893                             (data->beep_mask >> 8) & 0xff);
  894 
  895         mutex_unlock(&data->update_lock);
  896         return count;
  897 }
  898 
  899 static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
  900                    show_beep_mask, store_beep_mask);
  901 
  902 static ssize_t
  903 show_beep(struct device *dev, struct device_attribute *attr, char *buf)
  904 {
  905         struct w83627hf_data *data = w83627hf_update_device(dev);
  906         int bitnr = to_sensor_dev_attr(attr)->index;
  907         return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
  908 }
  909 
  910 static ssize_t
  911 store_beep(struct device *dev, struct device_attribute *attr,
  912                 const char *buf, size_t count)
  913 {
  914         struct w83627hf_data *data = dev_get_drvdata(dev);
  915         int bitnr = to_sensor_dev_attr(attr)->index;
  916         u8 reg;
  917         unsigned long bit;
  918         int err;
  919 
  920         err = kstrtoul(buf, 10, &bit);
  921         if (err)
  922                 return err;
  923 
  924         if (bit & ~1)
  925                 return -EINVAL;
  926 
  927         mutex_lock(&data->update_lock);
  928         if (bit)
  929                 data->beep_mask |= (1 << bitnr);
  930         else
  931                 data->beep_mask &= ~(1 << bitnr);
  932 
  933         if (bitnr < 8) {
  934                 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
  935                 if (bit)
  936                         reg |= (1 << bitnr);
  937                 else
  938                         reg &= ~(1 << bitnr);
  939                 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
  940         } else if (bitnr < 16) {
  941                 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
  942                 if (bit)
  943                         reg |= (1 << (bitnr - 8));
  944                 else
  945                         reg &= ~(1 << (bitnr - 8));
  946                 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
  947         } else {
  948                 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
  949                 if (bit)
  950                         reg |= (1 << (bitnr - 16));
  951                 else
  952                         reg &= ~(1 << (bitnr - 16));
  953                 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
  954         }
  955         mutex_unlock(&data->update_lock);
  956 
  957         return count;
  958 }
  959 
  960 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
  961                         show_beep, store_beep, 0);
  962 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
  963                         show_beep, store_beep, 1);
  964 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
  965                         show_beep, store_beep, 2);
  966 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
  967                         show_beep, store_beep, 3);
  968 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
  969                         show_beep, store_beep, 8);
  970 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
  971                         show_beep, store_beep, 9);
  972 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
  973                         show_beep, store_beep, 10);
  974 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
  975                         show_beep, store_beep, 16);
  976 static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
  977                         show_beep, store_beep, 17);
  978 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
  979                         show_beep, store_beep, 6);
  980 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
  981                         show_beep, store_beep, 7);
  982 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
  983                         show_beep, store_beep, 11);
  984 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
  985                         show_beep, store_beep, 4);
  986 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
  987                         show_beep, store_beep, 5);
  988 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO | S_IWUSR,
  989                         show_beep, store_beep, 13);
  990 static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
  991                         show_beep, store_beep, 15);
  992 
  993 static ssize_t
  994 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
  995 {
  996         int nr = to_sensor_dev_attr(devattr)->index;
  997         struct w83627hf_data *data = w83627hf_update_device(dev);
  998         return sprintf(buf, "%ld\n",
  999                        (long) DIV_FROM_REG(data->fan_div[nr]));
 1000 }
 1001 /*
 1002  * Note: we save and restore the fan minimum here, because its value is
 1003  * determined in part by the fan divisor.  This follows the principle of
 1004  * least surprise; the user doesn't expect the fan minimum to change just
 1005  * because the divisor changed.
 1006  */
 1007 static ssize_t
 1008 store_fan_div(struct device *dev, struct device_attribute *devattr,
 1009               const char *buf, size_t count)
 1010 {
 1011         int nr = to_sensor_dev_attr(devattr)->index;
 1012         struct w83627hf_data *data = dev_get_drvdata(dev);
 1013         unsigned long min;
 1014         u8 reg;
 1015         unsigned long val;
 1016         int err;
 1017 
 1018         err = kstrtoul(buf, 10, &val);
 1019         if (err)
 1020                 return err;
 1021 
 1022         mutex_lock(&data->update_lock);
 1023 
 1024         /* Save fan_min */
 1025         min = FAN_FROM_REG(data->fan_min[nr],
 1026                            DIV_FROM_REG(data->fan_div[nr]));
 1027 
 1028         data->fan_div[nr] = DIV_TO_REG(val);
 1029 
 1030         reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
 1031                & (nr==0 ? 0xcf : 0x3f))
 1032             | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
 1033         w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
 1034 
 1035         reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
 1036                & ~(1 << (5 + nr)))
 1037             | ((data->fan_div[nr] & 0x04) << (3 + nr));
 1038         w83627hf_write_value(data, W83781D_REG_VBAT, reg);
 1039 
 1040         /* Restore fan_min */
 1041         data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
 1042         w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
 1043 
 1044         mutex_unlock(&data->update_lock);
 1045         return count;
 1046 }
 1047 
 1048 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO|S_IWUSR,
 1049                           show_fan_div, store_fan_div, 0);
 1050 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO|S_IWUSR,
 1051                           show_fan_div, store_fan_div, 1);
 1052 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO|S_IWUSR,
 1053                           show_fan_div, store_fan_div, 2);
 1054 
 1055 static ssize_t
 1056 show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
 1057 {
 1058         int nr = to_sensor_dev_attr(devattr)->index;
 1059         struct w83627hf_data *data = w83627hf_update_device(dev);
 1060         return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
 1061 }
 1062 
 1063 static ssize_t
 1064 store_pwm(struct device *dev, struct device_attribute *devattr,
 1065           const char *buf, size_t count)
 1066 {
 1067         int nr = to_sensor_dev_attr(devattr)->index;
 1068         struct w83627hf_data *data = dev_get_drvdata(dev);
 1069         unsigned long val;
 1070         int err;
 1071 
 1072         err = kstrtoul(buf, 10, &val);
 1073         if (err)
 1074                 return err;
 1075 
 1076         mutex_lock(&data->update_lock);
 1077 
 1078         if (data->type == w83627thf) {
 1079                 /* bits 0-3 are reserved  in 627THF */
 1080                 data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
 1081                 w83627hf_write_value(data,
 1082                                      W836X7HF_REG_PWM(data->type, nr),
 1083                                      data->pwm[nr] |
 1084                                      (w83627hf_read_value(data,
 1085                                      W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
 1086         } else {
 1087                 data->pwm[nr] = PWM_TO_REG(val);
 1088                 w83627hf_write_value(data,
 1089                                      W836X7HF_REG_PWM(data->type, nr),
 1090                                      data->pwm[nr]);
 1091         }
 1092 
 1093         mutex_unlock(&data->update_lock);
 1094         return count;
 1095 }
 1096 
 1097 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0);
 1098 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 1);
 1099 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 2);
 1100 
 1101 static ssize_t
 1102 show_pwm_enable(struct device *dev, struct device_attribute *devattr, char *buf)
 1103 {
 1104         int nr = to_sensor_dev_attr(devattr)->index;
 1105         struct w83627hf_data *data = w83627hf_update_device(dev);
 1106         return sprintf(buf, "%d\n", data->pwm_enable[nr]);
 1107 }
 1108 
 1109 static ssize_t
 1110 store_pwm_enable(struct device *dev, struct device_attribute *devattr,
 1111           const char *buf, size_t count)
 1112 {
 1113         int nr = to_sensor_dev_attr(devattr)->index;
 1114         struct w83627hf_data *data = dev_get_drvdata(dev);
 1115         u8 reg;
 1116         unsigned long val;
 1117         int err;
 1118 
 1119         err = kstrtoul(buf, 10, &val);
 1120         if (err)
 1121                 return err;
 1122 
 1123         if (!val || val > 3)    /* modes 1, 2 and 3 are supported */
 1124                 return -EINVAL;
 1125         mutex_lock(&data->update_lock);
 1126         data->pwm_enable[nr] = val;
 1127         reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
 1128         reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
 1129         reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
 1130         w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
 1131         mutex_unlock(&data->update_lock);
 1132         return count;
 1133 }
 1134 
 1135 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
 1136                                                   store_pwm_enable, 0);
 1137 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
 1138                                                   store_pwm_enable, 1);
 1139 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
 1140                                                   store_pwm_enable, 2);
 1141 
 1142 static ssize_t
 1143 show_pwm_freq(struct device *dev, struct device_attribute *devattr, char *buf)
 1144 {
 1145         int nr = to_sensor_dev_attr(devattr)->index;
 1146         struct w83627hf_data *data = w83627hf_update_device(dev);
 1147         if (data->type == w83627hf)
 1148                 return sprintf(buf, "%ld\n",
 1149                         pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
 1150         else
 1151                 return sprintf(buf, "%ld\n",
 1152                         pwm_freq_from_reg(data->pwm_freq[nr]));
 1153 }
 1154 
 1155 static ssize_t
 1156 store_pwm_freq(struct device *dev, struct device_attribute *devattr,
 1157                const char *buf, size_t count)
 1158 {
 1159         int nr = to_sensor_dev_attr(devattr)->index;
 1160         struct w83627hf_data *data = dev_get_drvdata(dev);
 1161         static const u8 mask[]={0xF8, 0x8F};
 1162         unsigned long val;
 1163         int err;
 1164 
 1165         err = kstrtoul(buf, 10, &val);
 1166         if (err)
 1167                 return err;
 1168 
 1169         mutex_lock(&data->update_lock);
 1170 
 1171         if (data->type == w83627hf) {
 1172                 data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
 1173                 w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
 1174                                 (data->pwm_freq[nr] << (nr*4)) |
 1175                                 (w83627hf_read_value(data,
 1176                                 W83627HF_REG_PWM_FREQ) & mask[nr]));
 1177         } else {
 1178                 data->pwm_freq[nr] = pwm_freq_to_reg(val);
 1179                 w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
 1180                                 data->pwm_freq[nr]);
 1181         }
 1182 
 1183         mutex_unlock(&data->update_lock);
 1184         return count;
 1185 }
 1186 
 1187 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO|S_IWUSR,
 1188                           show_pwm_freq, store_pwm_freq, 0);
 1189 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO|S_IWUSR,
 1190                           show_pwm_freq, store_pwm_freq, 1);
 1191 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO|S_IWUSR,
 1192                           show_pwm_freq, store_pwm_freq, 2);
 1193 
 1194 static ssize_t
 1195 show_temp_type(struct device *dev, struct device_attribute *devattr,
 1196                char *buf)
 1197 {
 1198         int nr = to_sensor_dev_attr(devattr)->index;
 1199         struct w83627hf_data *data = w83627hf_update_device(dev);
 1200         return sprintf(buf, "%ld\n", (long) data->sens[nr]);
 1201 }
 1202 
 1203 static ssize_t
 1204 store_temp_type(struct device *dev, struct device_attribute *devattr,
 1205                 const char *buf, size_t count)
 1206 {
 1207         int nr = to_sensor_dev_attr(devattr)->index;
 1208         struct w83627hf_data *data = dev_get_drvdata(dev);
 1209         unsigned long val;
 1210         u32 tmp;
 1211         int err;
 1212 
 1213         err = kstrtoul(buf, 10, &val);
 1214         if (err)
 1215                 return err;
 1216 
 1217         mutex_lock(&data->update_lock);
 1218 
 1219         switch (val) {
 1220         case 1:         /* PII/Celeron diode */
 1221                 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
 1222                 w83627hf_write_value(data, W83781D_REG_SCFG1,
 1223                                     tmp | BIT_SCFG1[nr]);
 1224                 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
 1225                 w83627hf_write_value(data, W83781D_REG_SCFG2,
 1226                                     tmp | BIT_SCFG2[nr]);
 1227                 data->sens[nr] = val;
 1228                 break;
 1229         case 2:         /* 3904 */
 1230                 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
 1231                 w83627hf_write_value(data, W83781D_REG_SCFG1,
 1232                                     tmp | BIT_SCFG1[nr]);
 1233                 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
 1234                 w83627hf_write_value(data, W83781D_REG_SCFG2,
 1235                                     tmp & ~BIT_SCFG2[nr]);
 1236                 data->sens[nr] = val;
 1237                 break;
 1238         case W83781D_DEFAULT_BETA:
 1239                 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
 1240                          "instead\n", W83781D_DEFAULT_BETA);
 1241                 /* fall through */
 1242         case 4:         /* thermistor */
 1243                 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
 1244                 w83627hf_write_value(data, W83781D_REG_SCFG1,
 1245                                     tmp & ~BIT_SCFG1[nr]);
 1246                 data->sens[nr] = val;
 1247                 break;
 1248         default:
 1249                 dev_err(dev,
 1250                        "Invalid sensor type %ld; must be 1, 2, or 4\n",
 1251                        (long) val);
 1252                 break;
 1253         }
 1254 
 1255         mutex_unlock(&data->update_lock);
 1256         return count;
 1257 }
 1258 
 1259 #define sysfs_temp_type(offset) \
 1260 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
 1261                           show_temp_type, store_temp_type, offset - 1);
 1262 
 1263 sysfs_temp_type(1);
 1264 sysfs_temp_type(2);
 1265 sysfs_temp_type(3);
 1266 
 1267 static ssize_t
 1268 show_name(struct device *dev, struct device_attribute *devattr, char *buf)
 1269 {
 1270         struct w83627hf_data *data = dev_get_drvdata(dev);
 1271 
 1272         return sprintf(buf, "%s\n", data->name);
 1273 }
 1274 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
 1275 
 1276 static int __init w83627hf_find(int sioaddr, unsigned short *addr,
 1277                                 struct w83627hf_sio_data *sio_data)
 1278 {
 1279         int err = -ENODEV;
 1280         u16 val;
 1281 
 1282         static __initconst char *const names[] = {
 1283                 "W83627HF",
 1284                 "W83627THF",
 1285                 "W83697HF",
 1286                 "W83637HF",
 1287                 "W83687THF",
 1288         };
 1289 
 1290         sio_data->sioaddr = sioaddr;
 1291         superio_enter(sio_data);
 1292         val = force_id ? force_id : superio_inb(sio_data, DEVID);
 1293         switch (val) {
 1294         case W627_DEVID:
 1295                 sio_data->type = w83627hf;
 1296                 break;
 1297         case W627THF_DEVID:
 1298                 sio_data->type = w83627thf;
 1299                 break;
 1300         case W697_DEVID:
 1301                 sio_data->type = w83697hf;
 1302                 break;
 1303         case W637_DEVID:
 1304                 sio_data->type = w83637hf;
 1305                 break;
 1306         case W687THF_DEVID:
 1307                 sio_data->type = w83687thf;
 1308                 break;
 1309         case 0xff:      /* No device at all */
 1310                 goto exit;
 1311         default:
 1312                 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
 1313                 goto exit;
 1314         }
 1315 
 1316         superio_select(sio_data, W83627HF_LD_HWM);
 1317         val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
 1318                superio_inb(sio_data, WINB_BASE_REG + 1);
 1319         *addr = val & WINB_ALIGNMENT;
 1320         if (*addr == 0) {
 1321                 pr_warn("Base address not set, skipping\n");
 1322                 goto exit;
 1323         }
 1324 
 1325         val = superio_inb(sio_data, WINB_ACT_REG);
 1326         if (!(val & 0x01)) {
 1327                 pr_warn("Enabling HWM logical device\n");
 1328                 superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
 1329         }
 1330 
 1331         err = 0;
 1332         pr_info(DRVNAME ": Found %s chip at %#x\n",
 1333                 names[sio_data->type], *addr);
 1334 
 1335  exit:
 1336         superio_exit(sio_data);
 1337         return err;
 1338 }
 1339 
 1340 #define VIN_UNIT_ATTRS(_X_)     \
 1341         &sensor_dev_attr_in##_X_##_input.dev_attr.attr,         \
 1342         &sensor_dev_attr_in##_X_##_min.dev_attr.attr,           \
 1343         &sensor_dev_attr_in##_X_##_max.dev_attr.attr,           \
 1344         &sensor_dev_attr_in##_X_##_alarm.dev_attr.attr,         \
 1345         &sensor_dev_attr_in##_X_##_beep.dev_attr.attr
 1346 
 1347 #define FAN_UNIT_ATTRS(_X_)     \
 1348         &sensor_dev_attr_fan##_X_##_input.dev_attr.attr,        \
 1349         &sensor_dev_attr_fan##_X_##_min.dev_attr.attr,          \
 1350         &sensor_dev_attr_fan##_X_##_div.dev_attr.attr,          \
 1351         &sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr,        \
 1352         &sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
 1353 
 1354 #define TEMP_UNIT_ATTRS(_X_)    \
 1355         &sensor_dev_attr_temp##_X_##_input.dev_attr.attr,       \
 1356         &sensor_dev_attr_temp##_X_##_max.dev_attr.attr,         \
 1357         &sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr,    \
 1358         &sensor_dev_attr_temp##_X_##_type.dev_attr.attr,        \
 1359         &sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr,       \
 1360         &sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
 1361 
 1362 static struct attribute *w83627hf_attributes[] = {
 1363         &dev_attr_in0_input.attr,
 1364         &dev_attr_in0_min.attr,
 1365         &dev_attr_in0_max.attr,
 1366         &sensor_dev_attr_in0_alarm.dev_attr.attr,
 1367         &sensor_dev_attr_in0_beep.dev_attr.attr,
 1368         VIN_UNIT_ATTRS(2),
 1369         VIN_UNIT_ATTRS(3),
 1370         VIN_UNIT_ATTRS(4),
 1371         VIN_UNIT_ATTRS(7),
 1372         VIN_UNIT_ATTRS(8),
 1373 
 1374         FAN_UNIT_ATTRS(1),
 1375         FAN_UNIT_ATTRS(2),
 1376 
 1377         TEMP_UNIT_ATTRS(1),
 1378         TEMP_UNIT_ATTRS(2),
 1379 
 1380         &dev_attr_alarms.attr,
 1381         &sensor_dev_attr_beep_enable.dev_attr.attr,
 1382         &dev_attr_beep_mask.attr,
 1383 
 1384         &sensor_dev_attr_pwm1.dev_attr.attr,
 1385         &sensor_dev_attr_pwm2.dev_attr.attr,
 1386         &dev_attr_name.attr,
 1387         NULL
 1388 };
 1389 
 1390 static const struct attribute_group w83627hf_group = {
 1391         .attrs = w83627hf_attributes,
 1392 };
 1393 
 1394 static struct attribute *w83627hf_attributes_opt[] = {
 1395         VIN_UNIT_ATTRS(1),
 1396         VIN_UNIT_ATTRS(5),
 1397         VIN_UNIT_ATTRS(6),
 1398 
 1399         FAN_UNIT_ATTRS(3),
 1400         TEMP_UNIT_ATTRS(3),
 1401         &sensor_dev_attr_pwm3.dev_attr.attr,
 1402 
 1403         &sensor_dev_attr_pwm1_freq.dev_attr.attr,
 1404         &sensor_dev_attr_pwm2_freq.dev_attr.attr,
 1405         &sensor_dev_attr_pwm3_freq.dev_attr.attr,
 1406 
 1407         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
 1408         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
 1409         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
 1410 
 1411         NULL
 1412 };
 1413 
 1414 static const struct attribute_group w83627hf_group_opt = {
 1415         .attrs = w83627hf_attributes_opt,
 1416 };
 1417 
 1418 static int w83627hf_probe(struct platform_device *pdev)
 1419 {
 1420         struct device *dev = &pdev->dev;
 1421         struct w83627hf_sio_data *sio_data = dev->platform_data;
 1422         struct w83627hf_data *data;
 1423         struct resource *res;
 1424         int err, i;
 1425 
 1426         static const char *names[] = {
 1427                 "w83627hf",
 1428                 "w83627thf",
 1429                 "w83697hf",
 1430                 "w83637hf",
 1431                 "w83687thf",
 1432         };
 1433 
 1434         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
 1435         if (!devm_request_region(dev, res->start, WINB_REGION_SIZE, DRVNAME)) {
 1436                 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
 1437                         (unsigned long)res->start,
 1438                         (unsigned long)(res->start + WINB_REGION_SIZE - 1));
 1439                 return -EBUSY;
 1440         }
 1441 
 1442         data = devm_kzalloc(dev, sizeof(struct w83627hf_data), GFP_KERNEL);
 1443         if (!data)
 1444                 return -ENOMEM;
 1445 
 1446         data->addr = res->start;
 1447         data->type = sio_data->type;
 1448         data->name = names[sio_data->type];
 1449         mutex_init(&data->lock);
 1450         mutex_init(&data->update_lock);
 1451         platform_set_drvdata(pdev, data);
 1452 
 1453         /* Initialize the chip */
 1454         w83627hf_init_device(pdev);
 1455 
 1456         /* A few vars need to be filled upon startup */
 1457         for (i = 0; i <= 2; i++)
 1458                 data->fan_min[i] = w83627hf_read_value(
 1459                                         data, W83627HF_REG_FAN_MIN(i));
 1460         w83627hf_update_fan_div(data);
 1461 
 1462         /* Register common device attributes */
 1463         err = sysfs_create_group(&dev->kobj, &w83627hf_group);
 1464         if (err)
 1465                 return err;
 1466 
 1467         /* Register chip-specific device attributes */
 1468         if (data->type == w83627hf || data->type == w83697hf)
 1469                 if ((err = device_create_file(dev,
 1470                                 &sensor_dev_attr_in5_input.dev_attr))
 1471                  || (err = device_create_file(dev,
 1472                                 &sensor_dev_attr_in5_min.dev_attr))
 1473                  || (err = device_create_file(dev,
 1474                                 &sensor_dev_attr_in5_max.dev_attr))
 1475                  || (err = device_create_file(dev,
 1476                                 &sensor_dev_attr_in5_alarm.dev_attr))
 1477                  || (err = device_create_file(dev,
 1478                                 &sensor_dev_attr_in5_beep.dev_attr))
 1479                  || (err = device_create_file(dev,
 1480                                 &sensor_dev_attr_in6_input.dev_attr))
 1481                  || (err = device_create_file(dev,
 1482                                 &sensor_dev_attr_in6_min.dev_attr))
 1483                  || (err = device_create_file(dev,
 1484                                 &sensor_dev_attr_in6_max.dev_attr))
 1485                  || (err = device_create_file(dev,
 1486                                 &sensor_dev_attr_in6_alarm.dev_attr))
 1487                  || (err = device_create_file(dev,
 1488                                 &sensor_dev_attr_in6_beep.dev_attr))
 1489                  || (err = device_create_file(dev,
 1490                                 &sensor_dev_attr_pwm1_freq.dev_attr))
 1491                  || (err = device_create_file(dev,
 1492                                 &sensor_dev_attr_pwm2_freq.dev_attr)))
 1493                         goto error;
 1494 
 1495         if (data->type != w83697hf)
 1496                 if ((err = device_create_file(dev,
 1497                                 &sensor_dev_attr_in1_input.dev_attr))
 1498                  || (err = device_create_file(dev,
 1499                                 &sensor_dev_attr_in1_min.dev_attr))
 1500                  || (err = device_create_file(dev,
 1501                                 &sensor_dev_attr_in1_max.dev_attr))
 1502                  || (err = device_create_file(dev,
 1503                                 &sensor_dev_attr_in1_alarm.dev_attr))
 1504                  || (err = device_create_file(dev,
 1505                                 &sensor_dev_attr_in1_beep.dev_attr))
 1506                  || (err = device_create_file(dev,
 1507                                 &sensor_dev_attr_fan3_input.dev_attr))
 1508                  || (err = device_create_file(dev,
 1509                                 &sensor_dev_attr_fan3_min.dev_attr))
 1510                  || (err = device_create_file(dev,
 1511                                 &sensor_dev_attr_fan3_div.dev_attr))
 1512                  || (err = device_create_file(dev,
 1513                                 &sensor_dev_attr_fan3_alarm.dev_attr))
 1514                  || (err = device_create_file(dev,
 1515                                 &sensor_dev_attr_fan3_beep.dev_attr))
 1516                  || (err = device_create_file(dev,
 1517                                 &sensor_dev_attr_temp3_input.dev_attr))
 1518                  || (err = device_create_file(dev,
 1519                                 &sensor_dev_attr_temp3_max.dev_attr))
 1520                  || (err = device_create_file(dev,
 1521                                 &sensor_dev_attr_temp3_max_hyst.dev_attr))
 1522                  || (err = device_create_file(dev,
 1523                                 &sensor_dev_attr_temp3_alarm.dev_attr))
 1524                  || (err = device_create_file(dev,
 1525                                 &sensor_dev_attr_temp3_beep.dev_attr))
 1526                  || (err = device_create_file(dev,
 1527                                 &sensor_dev_attr_temp3_type.dev_attr)))
 1528                         goto error;
 1529 
 1530         if (data->type != w83697hf && data->vid != 0xff) {
 1531                 /* Convert VID to voltage based on VRM */
 1532                 data->vrm = vid_which_vrm();
 1533 
 1534                 if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
 1535                  || (err = device_create_file(dev, &dev_attr_vrm)))
 1536                         goto error;
 1537         }
 1538 
 1539         if (data->type == w83627thf || data->type == w83637hf
 1540             || data->type == w83687thf) {
 1541                 err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
 1542                 if (err)
 1543                         goto error;
 1544         }
 1545 
 1546         if (data->type == w83637hf || data->type == w83687thf)
 1547                 if ((err = device_create_file(dev,
 1548                                 &sensor_dev_attr_pwm1_freq.dev_attr))
 1549                  || (err = device_create_file(dev,
 1550                                 &sensor_dev_attr_pwm2_freq.dev_attr))
 1551                  || (err = device_create_file(dev,
 1552                                 &sensor_dev_attr_pwm3_freq.dev_attr)))
 1553                         goto error;
 1554 
 1555         if (data->type != w83627hf)
 1556                 if ((err = device_create_file(dev,
 1557                                 &sensor_dev_attr_pwm1_enable.dev_attr))
 1558                  || (err = device_create_file(dev,
 1559                                 &sensor_dev_attr_pwm2_enable.dev_attr)))
 1560                         goto error;
 1561 
 1562         if (data->type == w83627thf || data->type == w83637hf
 1563             || data->type == w83687thf) {
 1564                 err = device_create_file(dev,
 1565                                          &sensor_dev_attr_pwm3_enable.dev_attr);
 1566                 if (err)
 1567                         goto error;
 1568         }
 1569 
 1570         data->hwmon_dev = hwmon_device_register(dev);
 1571         if (IS_ERR(data->hwmon_dev)) {
 1572                 err = PTR_ERR(data->hwmon_dev);
 1573                 goto error;
 1574         }
 1575 
 1576         return 0;
 1577 
 1578  error:
 1579         sysfs_remove_group(&dev->kobj, &w83627hf_group);
 1580         sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
 1581         return err;
 1582 }
 1583 
 1584 static int w83627hf_remove(struct platform_device *pdev)
 1585 {
 1586         struct w83627hf_data *data = platform_get_drvdata(pdev);
 1587 
 1588         hwmon_device_unregister(data->hwmon_dev);
 1589 
 1590         sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
 1591         sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
 1592 
 1593         return 0;
 1594 }
 1595 
 1596 
 1597 /* Registers 0x50-0x5f are banked */
 1598 static inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
 1599 {
 1600         if ((reg & 0x00f0) == 0x50) {
 1601                 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
 1602                 outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
 1603         }
 1604 }
 1605 
 1606 /* Not strictly necessary, but play it safe for now */
 1607 static inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
 1608 {
 1609         if (reg & 0xff00) {
 1610                 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
 1611                 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
 1612         }
 1613 }
 1614 
 1615 static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
 1616 {
 1617         int res, word_sized;
 1618 
 1619         mutex_lock(&data->lock);
 1620         word_sized = (((reg & 0xff00) == 0x100)
 1621                    || ((reg & 0xff00) == 0x200))
 1622                   && (((reg & 0x00ff) == 0x50)
 1623                    || ((reg & 0x00ff) == 0x53)
 1624                    || ((reg & 0x00ff) == 0x55));
 1625         w83627hf_set_bank(data, reg);
 1626         outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
 1627         res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
 1628         if (word_sized) {
 1629                 outb_p((reg & 0xff) + 1,
 1630                        data->addr + W83781D_ADDR_REG_OFFSET);
 1631                 res =
 1632                     (res << 8) + inb_p(data->addr +
 1633                                        W83781D_DATA_REG_OFFSET);
 1634         }
 1635         w83627hf_reset_bank(data, reg);
 1636         mutex_unlock(&data->lock);
 1637         return res;
 1638 }
 1639 
 1640 static int w83627thf_read_gpio5(struct platform_device *pdev)
 1641 {
 1642         struct w83627hf_sio_data *sio_data = pdev->dev.platform_data;
 1643         int res = 0xff, sel;
 1644 
 1645         superio_enter(sio_data);
 1646         superio_select(sio_data, W83627HF_LD_GPIO5);
 1647 
 1648         /* Make sure these GPIO pins are enabled */
 1649         if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
 1650                 dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
 1651                 goto exit;
 1652         }
 1653 
 1654         /*
 1655          * Make sure the pins are configured for input
 1656          * There must be at least five (VRM 9), and possibly 6 (VRM 10)
 1657          */
 1658         sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
 1659         if ((sel & 0x1f) != 0x1f) {
 1660                 dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
 1661                         "function\n");
 1662                 goto exit;
 1663         }
 1664 
 1665         dev_info(&pdev->dev, "Reading VID from GPIO5\n");
 1666         res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
 1667 
 1668 exit:
 1669         superio_exit(sio_data);
 1670         return res;
 1671 }
 1672 
 1673 static int w83687thf_read_vid(struct platform_device *pdev)
 1674 {
 1675         struct w83627hf_sio_data *sio_data = pdev->dev.platform_data;
 1676         int res = 0xff;
 1677 
 1678         superio_enter(sio_data);
 1679         superio_select(sio_data, W83627HF_LD_HWM);
 1680 
 1681         /* Make sure these GPIO pins are enabled */
 1682         if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
 1683                 dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
 1684                 goto exit;
 1685         }
 1686 
 1687         /* Make sure the pins are configured for input */
 1688         if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
 1689                 dev_dbg(&pdev->dev, "VID configured as output, "
 1690                         "no VID function\n");
 1691                 goto exit;
 1692         }
 1693 
 1694         res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
 1695 
 1696 exit:
 1697         superio_exit(sio_data);
 1698         return res;
 1699 }
 1700 
 1701 static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
 1702 {
 1703         int word_sized;
 1704 
 1705         mutex_lock(&data->lock);
 1706         word_sized = (((reg & 0xff00) == 0x100)
 1707                    || ((reg & 0xff00) == 0x200))
 1708                   && (((reg & 0x00ff) == 0x53)
 1709                    || ((reg & 0x00ff) == 0x55));
 1710         w83627hf_set_bank(data, reg);
 1711         outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
 1712         if (word_sized) {
 1713                 outb_p(value >> 8,
 1714                        data->addr + W83781D_DATA_REG_OFFSET);
 1715                 outb_p((reg & 0xff) + 1,
 1716                        data->addr + W83781D_ADDR_REG_OFFSET);
 1717         }
 1718         outb_p(value & 0xff,
 1719                data->addr + W83781D_DATA_REG_OFFSET);
 1720         w83627hf_reset_bank(data, reg);
 1721         mutex_unlock(&data->lock);
 1722         return 0;
 1723 }
 1724 
 1725 static void w83627hf_init_device(struct platform_device *pdev)
 1726 {
 1727         struct w83627hf_data *data = platform_get_drvdata(pdev);
 1728         int i;
 1729         enum chips type = data->type;
 1730         u8 tmp;
 1731 
 1732         /* Minimize conflicts with other winbond i2c-only clients...  */
 1733         /* disable i2c subclients... how to disable main i2c client?? */
 1734         /* force i2c address to relatively uncommon address */
 1735         if (type == w83627hf) {
 1736                 w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
 1737                 w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
 1738         }
 1739 
 1740         /* Read VID only once */
 1741         if (type == w83627hf || type == w83637hf) {
 1742                 int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
 1743                 int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
 1744                 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
 1745         } else if (type == w83627thf) {
 1746                 data->vid = w83627thf_read_gpio5(pdev);
 1747         } else if (type == w83687thf) {
 1748                 data->vid = w83687thf_read_vid(pdev);
 1749         }
 1750 
 1751         /* Read VRM & OVT Config only once */
 1752         if (type == w83627thf || type == w83637hf || type == w83687thf) {
 1753                 data->vrm_ovt = 
 1754                         w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
 1755         }
 1756 
 1757         tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
 1758         for (i = 1; i <= 3; i++) {
 1759                 if (!(tmp & BIT_SCFG1[i - 1])) {
 1760                         data->sens[i - 1] = 4;
 1761                 } else {
 1762                         if (w83627hf_read_value
 1763                             (data,
 1764                              W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
 1765                                 data->sens[i - 1] = 1;
 1766                         else
 1767                                 data->sens[i - 1] = 2;
 1768                 }
 1769                 if ((type == w83697hf) && (i == 2))
 1770                         break;
 1771         }
 1772 
 1773         if(init) {
 1774                 /* Enable temp2 */
 1775                 tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
 1776                 if (tmp & 0x01) {
 1777                         dev_warn(&pdev->dev, "Enabling temp2, readings "
 1778                                  "might not make sense\n");
 1779                         w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
 1780                                 tmp & 0xfe);
 1781                 }
 1782 
 1783                 /* Enable temp3 */
 1784                 if (type != w83697hf) {
 1785                         tmp = w83627hf_read_value(data,
 1786                                 W83627HF_REG_TEMP3_CONFIG);
 1787                         if (tmp & 0x01) {
 1788                                 dev_warn(&pdev->dev, "Enabling temp3, "
 1789                                          "readings might not make sense\n");
 1790                                 w83627hf_write_value(data,
 1791                                         W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
 1792                         }
 1793                 }
 1794         }
 1795 
 1796         /* Start monitoring */
 1797         w83627hf_write_value(data, W83781D_REG_CONFIG,
 1798                             (w83627hf_read_value(data,
 1799                                                 W83781D_REG_CONFIG) & 0xf7)
 1800                             | 0x01);
 1801 
 1802         /* Enable VBAT monitoring if needed */
 1803         tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
 1804         if (!(tmp & 0x01))
 1805                 w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
 1806 }
 1807 
 1808 static void w83627hf_update_fan_div(struct w83627hf_data *data)
 1809 {
 1810         int reg;
 1811 
 1812         reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
 1813         data->fan_div[0] = (reg >> 4) & 0x03;
 1814         data->fan_div[1] = (reg >> 6) & 0x03;
 1815         if (data->type != w83697hf) {
 1816                 data->fan_div[2] = (w83627hf_read_value(data,
 1817                                        W83781D_REG_PIN) >> 6) & 0x03;
 1818         }
 1819         reg = w83627hf_read_value(data, W83781D_REG_VBAT);
 1820         data->fan_div[0] |= (reg >> 3) & 0x04;
 1821         data->fan_div[1] |= (reg >> 4) & 0x04;
 1822         if (data->type != w83697hf)
 1823                 data->fan_div[2] |= (reg >> 5) & 0x04;
 1824 }
 1825 
 1826 static struct w83627hf_data *w83627hf_update_device(struct device *dev)
 1827 {
 1828         struct w83627hf_data *data = dev_get_drvdata(dev);
 1829         int i, num_temps = (data->type == w83697hf) ? 2 : 3;
 1830         int num_pwms = (data->type == w83697hf) ? 2 : 3;
 1831 
 1832         mutex_lock(&data->update_lock);
 1833 
 1834         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
 1835             || !data->valid) {
 1836                 for (i = 0; i <= 8; i++) {
 1837                         /* skip missing sensors */
 1838                         if (((data->type == w83697hf) && (i == 1)) ||
 1839                             ((data->type != w83627hf && data->type != w83697hf)
 1840                             && (i == 5 || i == 6)))
 1841                                 continue;
 1842                         data->in[i] =
 1843                             w83627hf_read_value(data, W83781D_REG_IN(i));
 1844                         data->in_min[i] =
 1845                             w83627hf_read_value(data,
 1846                                                W83781D_REG_IN_MIN(i));
 1847                         data->in_max[i] =
 1848                             w83627hf_read_value(data,
 1849                                                W83781D_REG_IN_MAX(i));
 1850                 }
 1851                 for (i = 0; i <= 2; i++) {
 1852                         data->fan[i] =
 1853                             w83627hf_read_value(data, W83627HF_REG_FAN(i));
 1854                         data->fan_min[i] =
 1855                             w83627hf_read_value(data,
 1856                                                W83627HF_REG_FAN_MIN(i));
 1857                 }
 1858                 for (i = 0; i <= 2; i++) {
 1859                         u8 tmp = w83627hf_read_value(data,
 1860                                 W836X7HF_REG_PWM(data->type, i));
 1861                         /* bits 0-3 are reserved  in 627THF */
 1862                         if (data->type == w83627thf)
 1863                                 tmp &= 0xf0;
 1864                         data->pwm[i] = tmp;
 1865                         if (i == 1 &&
 1866                             (data->type == w83627hf || data->type == w83697hf))
 1867                                 break;
 1868                 }
 1869                 if (data->type == w83627hf) {
 1870                                 u8 tmp = w83627hf_read_value(data,
 1871                                                 W83627HF_REG_PWM_FREQ);
 1872                                 data->pwm_freq[0] = tmp & 0x07;
 1873                                 data->pwm_freq[1] = (tmp >> 4) & 0x07;
 1874                 } else if (data->type != w83627thf) {
 1875                         for (i = 1; i <= 3; i++) {
 1876                                 data->pwm_freq[i - 1] =
 1877                                         w83627hf_read_value(data,
 1878                                                 W83637HF_REG_PWM_FREQ[i - 1]);
 1879                                 if (i == 2 && (data->type == w83697hf))
 1880                                         break;
 1881                         }
 1882                 }
 1883                 if (data->type != w83627hf) {
 1884                         for (i = 0; i < num_pwms; i++) {
 1885                                 u8 tmp = w83627hf_read_value(data,
 1886                                         W83627THF_REG_PWM_ENABLE[i]);
 1887                                 data->pwm_enable[i] =
 1888                                         ((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
 1889                                         & 0x03) + 1;
 1890                         }
 1891                 }
 1892                 for (i = 0; i < num_temps; i++) {
 1893                         data->temp[i] = w83627hf_read_value(
 1894                                                 data, w83627hf_reg_temp[i]);
 1895                         data->temp_max[i] = w83627hf_read_value(
 1896                                                 data, w83627hf_reg_temp_over[i]);
 1897                         data->temp_max_hyst[i] = w83627hf_read_value(
 1898                                                 data, w83627hf_reg_temp_hyst[i]);
 1899                 }
 1900 
 1901                 w83627hf_update_fan_div(data);
 1902 
 1903                 data->alarms =
 1904                     w83627hf_read_value(data, W83781D_REG_ALARM1) |
 1905                     (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
 1906                     (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
 1907                 i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
 1908                 data->beep_mask = (i << 8) |
 1909                     w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
 1910                     w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
 1911                 data->last_updated = jiffies;
 1912                 data->valid = 1;
 1913         }
 1914 
 1915         mutex_unlock(&data->update_lock);
 1916 
 1917         return data;
 1918 }
 1919 
 1920 static int __init w83627hf_device_add(unsigned short address,
 1921                                       const struct w83627hf_sio_data *sio_data)
 1922 {
 1923         struct resource res = {
 1924                 .start  = address + WINB_REGION_OFFSET,
 1925                 .end    = address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
 1926                 .name   = DRVNAME,
 1927                 .flags  = IORESOURCE_IO,
 1928         };
 1929         int err;
 1930 
 1931         err = acpi_check_resource_conflict(&res);
 1932         if (err)
 1933                 goto exit;
 1934 
 1935         pdev = platform_device_alloc(DRVNAME, address);
 1936         if (!pdev) {
 1937                 err = -ENOMEM;
 1938                 pr_err("Device allocation failed\n");
 1939                 goto exit;
 1940         }
 1941 
 1942         err = platform_device_add_resources(pdev, &res, 1);
 1943         if (err) {
 1944                 pr_err("Device resource addition failed (%d)\n", err);
 1945                 goto exit_device_put;
 1946         }
 1947 
 1948         err = platform_device_add_data(pdev, sio_data,
 1949                                        sizeof(struct w83627hf_sio_data));
 1950         if (err) {
 1951                 pr_err("Platform data allocation failed\n");
 1952                 goto exit_device_put;
 1953         }
 1954 
 1955         err = platform_device_add(pdev);
 1956         if (err) {
 1957                 pr_err("Device addition failed (%d)\n", err);
 1958                 goto exit_device_put;
 1959         }
 1960 
 1961         return 0;
 1962 
 1963 exit_device_put:
 1964         platform_device_put(pdev);
 1965 exit:
 1966         return err;
 1967 }
 1968 
 1969 static int __init sensors_w83627hf_init(void)
 1970 {
 1971         int err;
 1972         unsigned short address;
 1973         struct w83627hf_sio_data sio_data;
 1974 
 1975         if (w83627hf_find(0x2e, &address, &sio_data)
 1976          && w83627hf_find(0x4e, &address, &sio_data))
 1977                 return -ENODEV;
 1978 
 1979         err = platform_driver_register(&w83627hf_driver);
 1980         if (err)
 1981                 goto exit;
 1982 
 1983         /* Sets global pdev as a side effect */
 1984         err = w83627hf_device_add(address, &sio_data);
 1985         if (err)
 1986                 goto exit_driver;
 1987 
 1988         return 0;
 1989 
 1990 exit_driver:
 1991         platform_driver_unregister(&w83627hf_driver);
 1992 exit:
 1993         return err;
 1994 }
 1995 
 1996 static void __exit sensors_w83627hf_exit(void)
 1997 {
 1998         platform_device_unregister(pdev);
 1999         platform_driver_unregister(&w83627hf_driver);
 2000 }
 2001 
 2002 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
 2003               "Philip Edelbrock <phil@netroedge.com>, "
 2004               "and Mark Studebaker <mdsxyz123@yahoo.com>");
 2005 MODULE_DESCRIPTION("W83627HF driver");
 2006 MODULE_LICENSE("GPL");
 2007 
 2008 module_init(sensors_w83627hf_init);
 2009 module_exit(sensors_w83627hf_exit);

Cache object: f186fff54e331deb07cf135324dc5760


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.