Jump to content
18 posts in this topic

Recommended Posts

Spoiler

Hmonitor Version 4.5.4.2 Pro 02ACFA04

(Trial)

H/W diagnostics info:

Windows 8 Professional x64 Version 6.2.9200 

Manufacturer: LENOVO

Board model: ThinkCentre M57

Version: LENOVO

BIOS vendor: LENOVO

BIOS version: 2RKT64AUS, Release date: 11/24/2011

Chipset: 29B08086

Vendor ID: 303817AA

SouthBridge: 29148086

SMB port used: 1C20

CPU:Intel® Core2 Quad CPU  Q9550 @ 2.83GHz

CPUId:0-6-7-A

Cores:1

Internal Freq=2826.0 MHz

External Freq=403.71 MHz

Multiplier=7.0

SystemBus Freq=21.801 GT/s 

Thermo control configuration:

Full Throttle:100.0%

AMD Low Power mode:Disabled

Device:None Throttle:25.0% Enable at:70.0°C Disable at:65.0°C

Main Sensor: SCH5617

Poll intervals: Sensors=5 sec, HDDs=5 min, Warnings=5 sec

EventLog: Disabled, EventLog.txt

DataLog: Disabled, DataLog.txt

Loginterval=1 min, LogRotation=Never

 

Monitored data values:

Mainboard=xx.x°C (0,Auto,0.0°C)

CPU1=xx.x°C (1,Auto,0.0°C)

CPU1 Core=41.0°C (1,CoreTemp,0.0°C)

CPU0 Core=44.0°C (0,CoreTemp,0.0°C)

Chassis=1449 rpm

CPU1=xxxx rpm

CPU2=337 rpm

Power=xxxx rpm

Core=+x.xxV

Aux=+x.xxV

+3.3V=+x.xxV

+5V=+x.xxV

+12V=+xx.xxV

-12V=-xx.xxV

-5V=-x.xxV

HDD1 temp=27.0°C ST9500420AS 0003LVM1 SATA

HDD2 temp=40.0°C ST380815AS 3.CCA SATA

VGA device: NVIDIA GeForce GTX 745

VGA Device ID: 138210DE,106510DE.A2

VGA Acceleration Level: 0

VGA sensor: ForceWare

VGA GPU=50.0°C

VGA Env=xx.x°C

VGA Fan=xxxx rpm (xx%)

VGA Core=+x.xxV

VGA Bus=+x.xxV

..hey devs, looking for an assist getting sensors read... chipset SCH5617...not currently supported by LPCSensors plugin....I've found some info, and an app in windows that does read from the chip (Hmonitor Version 4.5.4.2) but I'm not knowledgeable enough to tear into the app and see how it works.....I'll include what info I could find...please find attached 2 pdf files, one relates specifically to the SCH5617, the other is data sheet for SCH5627P but refers to SCH5617 differences (hoping it supplies some useful tidbits)

 
 
 
 
 
 
 
...hoping for a reply...thx.

 

Edited by RobertX
cleaning up another tooooo long post
  • Like 1
Link to comment
https://www.insanelymac.com/forum/topic/333787-main-sensor-sch5617reprise/
Share on other sites

 

some more  information maybe can be help full

 

Supported chips:
  * SMSC DME1737 and compatibles (like Asus A8000)
    Prefix: 'dme1737'
    Addresses scanned: I2C 0x2c, 0x2d, 0x2e
    Datasheet: Provided by SMSC upon request and under NDA
  * SMSC SCH3112, SCH3114, SCH3116
    Prefix: 'sch311x'
    Addresses scanned: none, address read from Super-I/O config space
    Datasheet: Available on the Internet
  * SMSC SCH5027
    Prefix: 'sch5027'
    Addresses scanned: I2C 0x2c, 0x2d, 0x2e
    Datasheet: Provided by SMSC upon request and under NDA
  * SMSC SCH5127
    Prefix: 'sch5127'
    Addresses scanned: none, address read from Super-I/O config space
    Datasheet: Provided by SMSC upon request and under NDA

Authors:
    Juerg Haefliger <juergh@gmail.com>


Module Parameters
-----------------

* force_start: bool    Enables the monitoring of voltage, fan and temp inputs
            and PWM output control functions. Using this parameter
            shouldn't be required since the BIOS usually takes care
            of this.
* probe_all_addr: bool    Include non-standard LPC addresses 0x162e and 0x164e
            when probing for ISA devices. This is required for the
            following boards:
            - VIA EPIA SN18000


Description
-----------

This driver implements support for the hardware monitoring capabilities of the
SMSC DME1737 and Asus A8000 (which are the same), SMSC SCH5027, SCH311x,
and SCH5127 Super-I/O chips. These chips feature monitoring of 3 temp sensors
temp[1-3] (2 remote diodes and 1 internal), 8 voltages in[0-7] (7 external and
1 internal) and up to 6 fan speeds fan[1-6]. Additionally, the chips implement
up to 5 PWM outputs pwm[1-3,5-6] for controlling fan speeds both manually and
automatically.

For the DME1737, A8000 and SCH5027, fan[1-2] and pwm[1-2] are always present.
Fan[3-6] and pwm[3,5-6] are optional features and their availability depends on
the configuration of the chip. The driver will detect which features are
present during initialization and create the sysfs attributes accordingly.

For the SCH311x and SCH5127, fan[1-3] and pwm[1-3] are always present and
fan[4-6] and pwm[5-6] don't exist.

The hardware monitoring features of the DME1737, A8000, and SCH5027 are only
accessible via SMBus, while the SCH311x and SCH5127 only provide access via
the ISA bus. The driver will therefore register itself as an I2C client driver
if it detects a DME1737, A8000, or SCH5027 and as a platform driver if it
detects a SCH311x or SCH5127 chip.


Voltage Monitoring
------------------

The voltage inputs are sampled with 12-bit resolution and have internal
scaling resistors. The values returned by the driver therefore reflect true
millivolts and don't need scaling. The voltage inputs are mapped as follows
(the last column indicates the input ranges):

DME1737, A8000:
    in0: +5VTR    (+5V standby)        0V - 6.64V
    in1: Vccp    (processor core)    0V - 3V
    in2: VCC    (internal +3.3V)    0V - 4.38V
    in3: +5V                0V - 6.64V
    in4: +12V                0V - 16V
    in5: VTR    (+3.3V standby)        0V - 4.38V
    in6: Vbat    (+3.0V)            0V - 4.38V

SCH311x:
    in0: +2.5V                0V - 3.32V
    in1: Vccp    (processor core)    0V - 2V
    in2: VCC    (internal +3.3V)    0V - 4.38V
    in3: +5V                0V - 6.64V
    in4: +12V                0V - 16V
    in5: VTR    (+3.3V standby)        0V - 4.38V
    in6: Vbat    (+3.0V)            0V - 4.38V

SCH5027:
    in0: +5VTR    (+5V standby)        0V - 6.64V
    in1: Vccp    (processor core)    0V - 3V
    in2: VCC    (internal +3.3V)    0V - 4.38V
    in3: V2_IN                0V - 1.5V
    in4: V1_IN                0V - 1.5V
    in5: VTR    (+3.3V standby)        0V - 4.38V
    in6: Vbat    (+3.0V)            0V - 4.38V

SCH5127:
    in0: +2.5                0V - 3.32V
    in1: Vccp    (processor core)    0V - 3V
    in2: VCC    (internal +3.3V)    0V - 4.38V
    in3: V2_IN                0V - 1.5V
    in4: V1_IN                0V - 1.5V
    in5: VTR    (+3.3V standby)        0V - 4.38V
    in6: Vbat    (+3.0V)            0V - 4.38V
    in7: Vtrip    (+1.5V)            0V - 1.99V

Each voltage input has associated min and max limits which trigger an alarm
when crossed.


Temperature Monitoring
----------------------

Temperatures are measured with 12-bit resolution and reported in millidegree
Celsius. The chip also features offsets for all 3 temperature inputs which -
when programmed - get added to the input readings. The chip does all the
scaling by itself and the driver therefore reports true temperatures that don't
need any user-space adjustments. The temperature inputs are mapped as follows
(the last column indicates the input ranges):

    temp1: Remote diode 1 (3904 type) temperature    -127C - +127C
    temp2: DME1737 internal temperature        -127C - +127C
    temp3: Remote diode 2 (3904 type) temperature    -127C - +127C

Each temperature input has associated min and max limits which trigger an alarm
when crossed. Additionally, each temperature input has a fault attribute that
returns 1 when a faulty diode or an unconnected input is detected and 0
otherwise.


Fan Monitoring
--------------

Fan RPMs are measured with 16-bit resolution. The chip provides inputs for 6
fan tachometers. All 6 inputs have an associated min limit which triggers an
alarm when crossed. Fan inputs 1-4 provide type attributes that need to be set
to the number of pulses per fan revolution that the connected tachometer
generates. Supported values are 1, 2, and 4. Fan inputs 5-6 only support fans
that generate 2 pulses per revolution. Fan inputs 5-6 also provide a max
attribute that needs to be set to the maximum attainable RPM (fan at 100% duty-
cycle) of the input. The chip adjusts the sampling rate based on this value.


PWM Output Control
------------------

This chip features 5 PWM outputs. PWM outputs 1-3 are associated with fan
inputs 1-3 and PWM outputs 5-6 are associated with fan inputs 5-6. PWM outputs
1-3 can be configured to operate either in manual or automatic mode by setting
the appropriate enable attribute accordingly. PWM outputs 5-6 can only operate
in manual mode, their enable attributes are therefore read-only. When set to
manual mode, the fan speed is set by writing the duty-cycle value to the
appropriate PWM attribute. In automatic mode, the PWM attribute returns the
current duty-cycle as set by the fan controller in the chip. All PWM outputs
support the setting of the output frequency via the freq attribute.

In automatic mode, the chip supports the setting of the PWM ramp rate which
defines how fast the PWM output is adjusting to changes of the associated
temperature input. Associating PWM outputs to temperature inputs is done via
temperature zones. The chip features 3 zones whose assignments to temperature
inputs is static and determined during initialization. These assignments can
be retrieved via the zone[1-3]_auto_channels_temp attributes. Each PWM output
is assigned to one (or hottest of multiple) temperature zone(s) through the
pwm[1-3]_auto_channels_zone attributes. Each PWM output has 3 distinct output
duty-cycles: full, low, and min. Full is internally hard-wired to 255 (100%)
and low and min can be programmed via pwm[1-3]_auto_point1_pwm and
pwm[1-3]_auto_pwm_min, respectively. The thermal thresholds of the zones are
programmed via zone[1-3]_auto_point[1-3]_temp and
zone[1-3]_auto_point1_temp_hyst:

    pwm[1-3]_auto_point2_pwm    full-speed duty-cycle (255, i.e., 100%)
    pwm[1-3]_auto_point1_pwm    low-speed duty-cycle
    pwm[1-3]_auto_pwm_min        min-speed duty-cycle

    zone[1-3]_auto_point3_temp    full-speed temp (all outputs)
    zone[1-3]_auto_point2_temp    full-speed temp
    zone[1-3]_auto_point1_temp    low-speed temp
    zone[1-3]_auto_point1_temp_hyst    min-speed temp

The chip adjusts the output duty-cycle linearly in the range of auto_point1_pwm
to auto_point2_pwm if the temperature of the associated zone is between
auto_point1_temp and auto_point2_temp. If the temperature drops below the
auto_point1_temp_hyst value, the output duty-cycle is set to the auto_pwm_min
value which only supports two values: 0 or auto_point1_pwm. That means that the
fan either turns completely off or keeps spinning with the low-speed
duty-cycle. If any of the temperatures rise above the auto_point3_temp value,
all PWM outputs are set to 100% duty-cycle.

Following is another representation of how the chip sets the output duty-cycle
based on the temperature of the associated thermal zone:

            Duty-Cycle    Duty-Cycle
    Temperature    Rising Temp    Falling Temp
    -----------    -----------    ------------
    full-speed    full-speed    full-speed

            < linearly adjusted duty-cycle >

    low-speed    low-speed    low-speed
            min-speed    low-speed
    min-speed    min-speed    min-speed
            min-speed    min-speed


Sysfs Attributes
----------------

Following is a list of all sysfs attributes that the driver provides, their
permissions and a short description:

Name                Perm    Description
----                ----    -----------
cpu0_vid            RO    CPU core reference voltage in
                    millivolts.
vrm                RW    Voltage regulator module version
                    number.

in[0-7]_input            RO    Measured voltage in millivolts.
in[0-7]_min            RW    Low limit for voltage input.
in[0-7]_max            RW    High limit for voltage input.
in[0-7]_alarm            RO    Voltage input alarm. Returns 1 if
                    voltage input is or went outside the
                    associated min-max range, 0 otherwise.

temp[1-3]_input            RO    Measured temperature in millidegree
                    Celsius.
temp[1-3]_min            RW    Low limit for temp input.
temp[1-3]_max            RW    High limit for temp input.
temp[1-3]_offset        RW    Offset for temp input. This value will
                    be added by the chip to the measured
                    temperature.
temp[1-3]_alarm            RO    Alarm for temp input. Returns 1 if temp
                    input is or went outside the associated
                    min-max range, 0 otherwise.
temp[1-3]_fault            RO    Temp input fault. Returns 1 if the chip
                    detects a faulty thermal diode or an
                    unconnected temp input, 0 otherwise.

zone[1-3]_auto_channels_temp    RO    Temperature zone to temperature input
                    mapping. This attribute is a bitfield
                    and supports the following values:
                        1: temp1
                        2: temp2
                        4: temp3
zone[1-3]_auto_point1_temp_hyst    RW    Auto PWM temp point1 hysteresis. The
                    output of the corresponding PWM is set
                    to the pwm_auto_min value if the temp
                    falls below the auto_point1_temp_hyst
                    value.
zone[1-3]_auto_point[1-3]_temp    RW    Auto PWM temp points. Auto_point1 is
                    the low-speed temp, auto_point2 is the
                    full-speed temp, and auto_point3 is the
                    temp at which all PWM outputs are set
                    to full-speed (100% duty-cycle).

fan[1-6]_input            RO    Measured fan speed in RPM.
fan[1-6]_min            RW    Low limit for fan input.
fan[1-6]_alarm            RO    Alarm for fan input. Returns 1 if fan
                    input is or went below the associated
                    min value, 0 otherwise.
fan[1-4]_type            RW    Type of attached fan. Expressed in
                    number of pulses per revolution that
                    the fan generates. Supported values are
                    1, 2, and 4.
fan[5-6]_max            RW    Max attainable RPM at 100% duty-cycle.
                    Required for chip to adjust the
                    sampling rate accordingly.

pmw[1-3,5-6]            RO/RW    Duty-cycle of PWM output. Supported
                    values are 0-255 (0%-100%). Only
                    writeable if the associated PWM is in
                    manual mode.
pwm[1-3]_enable            RW    Enable of PWM outputs 1-3. Supported
                    values are:
                         0: turned off (output @ 100%)
                         1: manual mode
                         2: automatic mode
pwm[5-6]_enable            RO    Enable of PWM outputs 5-6. Always
                    returns 1 since these 2 outputs are
                    hard-wired to manual mode.
pmw[1-3,5-6]_freq        RW    Frequency of PWM output. Supported
                    values are in the range 11Hz-30000Hz
                    (default is 25000Hz).
pmw[1-3]_ramp_rate        RW    Ramp rate of PWM output. Determines how
                    fast the PWM duty-cycle will change
                    when the PWM is in automatic mode.
                    Expressed in ms per PWM step. Supported
                    values are in the range 0ms-206ms
                    (default is 0, which means the duty-
                    cycle changes instantly).
pwm[1-3]_auto_channels_zone    RW    PWM output to temperature zone mapping.
                    This attribute is a bitfield and
                    supports the following values:
                        1: zone1
                        2: zone2
                        4: zone3
                        6: highest of zone[2-3]
                        7: highest of zone[1-3]
pwm[1-3]_auto_pwm_min        RW    Auto PWM min pwm. Minimum PWM duty-
                    cycle. Supported values are 0 or
                    auto_point1_pwm.
pwm[1-3]_auto_point1_pwm    RW    Auto PWM pwm point. Auto_point1 is the
                    low-speed duty-cycle.
pwm[1-3]_auto_point2_pwm    RO    Auto PWM pwm point. Auto_point2 is the
                    full-speed duty-cycle which is hard-
                    wired to 255 (100% duty-cycle).

Chip Differences
----------------

Feature            dme1737    sch311x    sch5027    sch5127
-------------------------------------------------------
temp[1-3]_offset    yes    yes
vid            yes
zone3            yes    yes    yes
zone[1-3]_hyst        yes    yes
pwm min/off        yes    yes
fan3            opt    yes    opt    yes
pwm3            opt    yes    opt    yes
fan4            opt        opt
fan5            opt        opt
pwm5            opt        opt
fan6            opt        opt
pwm6            opt        opt
in7                        yes
 

  • Like 1

I’ve long known that my HPz400 workstation - running High Sierra and Sierra - contains an SMSC SCH5327 rev B Super-IO chip.

Poking about under Ubuntu I discovered that lm-sensors seems to support this chipset and several others from SMSC, notably the

SMSC LPC47B397-NC
SMSC SCH5307-NS
SMSC SCH5317

via its smsc47b397 kernel module. I’m guessing that my SCH5327 is register compatible with the supported SCH5317 as loading the appropriate module gets me sensible temperature and fan speed readings under Ubuntu, at least if minus values / missing sensors are discounted.

Trying family `SMSC'... Yes

Found `SMSC SCH5317 Super IO'   Success!

(address 0x480, driver `smsc47b397')

Driver `smsc47b397':

  • ISA bus, address 0x480
    Chip `SMSC SCH5317 Super IO' (confidence: 9)

hpz400@ubuntu:~$ sensors

smsc47b397-isa-0480
Adapter: ISA adapter
fan1:        1288 RPM
fan2:           0 RPM
fan3:           0 RPM
fan4:        1624 RPM
temp1:        -50.0°C  
temp2:        +21.0°C  
temp3:        +64.0°C  
temp4:       -128.0°C  

Perhaps more importantly there is a data sheet contributed by Mark Hoffman and Craig Kelly describing how to read the various registers. This mentions the Compaq dc7100 so it seems possible that this SMSC family is used across a number of HP / Compaq models.

https://www.kernel.org/doc/Documentation/hwmon/smsc47b397

I know that adding support for a new family of Super IO controllers involves a great deal of work - certainly this is far beyond my very rudimentary skills in this area, but there's an active community of users here running OSX on the HPz400/z600/z800 workstation family and I’m certain there would be a lot of interest if it might be possible to extend the capabilities of LPCSensors to support the SuperIO chip they all include.

Finally I’d like to offer my services as tester should anyone feel motivated to take this on.

Many thanks for reading thus far.

Edited by pharillion
Remove brackets which created an unwanted smiley.
  • Like 1

SCH311x datasheet which covers SCH3112, SCH3114 and SCH3116 LPC chips.

00001872A.pdf

SCH5147 datasheet

I hope these might be useful.  Both seem to be freely available and not under NDA.

 

 

00001788A.pdf

Edited by pharillion
add second data sheet
3 hours ago, pharillion said:

SCH311x datasheet which covers SCH3112, SCH3114 and SCH3116 LPC chips.

00001872A.pdf

SCH5147 datasheet

I hope these might be useful.  Both seem to be freely available and not under NDA.

 

 

00001788A.pdf

It's a pity these documents not provided programmer interface for these chip. There are only packages information. I need datasheet for developers.

 
i hope this could help you further  hwmon  Linux Source code




hwmon: (dme1737) Add support for the SMSC SCH5027

Add support for the SCH5027. The differences to the DME1737 are:
- No support for programmable temp offsets
- In auto mode, PWM outputs stay on min value if temp goes below low threshold
  and can't be programmed to fully turn off
- Different voltage scaling
- No VID input

Signed-off-by: Juerg Haefliger <juergh@gmail.com>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
 Documentation/hwmon/dme1737 |  53 ++++++++----
 drivers/hwmon/Kconfig       |   4 +-
 drivers/hwmon/dme1737.c     | 193 +++++++++++++++++++++++++++++---------------
 3 files changed, 169 insertions(+), 81 deletions(-)

diff --git a/Documentation/hwmon/dme1737 b/Documentation/hwmon/dme1737
index b1fe009..001d2e7 100644
--- a/Documentation/hwmon/dme1737
+++ b/Documentation/hwmon/dme1737
@@ -10,6 +10,10 @@ Supported chips:
     Prefix: 'sch311x'
     Addresses scanned: none, address read from Super-I/O config space
     Datasheet: http://www.nuhorizons.com/FeaturedProducts/Volume1/SMSC/311x.pdf
+  * SMSC SCH5027
+    Prefix: 'sch5027'
+    Addresses scanned: I2C 0x2c, 0x2d, 0x2e
+    Datasheet: Provided by SMSC upon request and under NDA

 Authors:
     Juerg Haefliger <juergh@gmail.com>
@@ -27,33 +31,31 @@ Module Parameters
            following boards:
            - VIA EPIA SN18000

-Note that there is no need to use this parameter if the driver loads without
-complaining. The driver will say so if it is necessary.
-

 Description
 -----------

 This driver implements support for the hardware monitoring capabilities of the
-SMSC DME1737 and Asus A8000 (which are the same) and SMSC SCH311x Super-I/O
-chips. These chips feature monitoring of 3 temp sensors temp[1-3] (2 remote
-diodes and 1 internal), 7 voltages in[0-6] (6 external and 1 internal) and up
-to 6 fan speeds fan[1-6]. Additionally, the chips implement up to 5 PWM
-outputs pwm[1-3,5-6] for controlling fan speeds both manually and
+SMSC DME1737 and Asus A8000 (which are the same), SMSC SCH5027, and SMSC
+SCH311x Super-I/O chips. These chips feature monitoring of 3 temp sensors
+temp[1-3] (2 remote diodes and 1 internal), 7 voltages in[0-6] (6 external and
+1 internal) and up to 6 fan speeds fan[1-6]. Additionally, the chips implement
+up to 5 PWM outputs pwm[1-3,5-6] for controlling fan speeds both manually and
 automatically.

-For the DME1737 and A8000, fan[1-2] and pwm[1-2] are always present. Fan[3-6]
-and pwm[3,5-6] are optional features and their availability depends on the
-configuration of the chip. The driver will detect which features are present
-during initialization and create the sysfs attributes accordingly.
+For the DME1737, A8000 and SCH5027, fan[1-2] and pwm[1-2] are always present.
+Fan[3-6] and pwm[3,5-6] are optional features and their availability depends on
+the configuration of the chip. The driver will detect which features are
+present during initialization and create the sysfs attributes accordingly.

 For the SCH311x, fan[1-3] and pwm[1-3] are always present and fan[4-6] and
 pwm[5-6] don't exist.

-The hardware monitoring features of the DME1737 and A8000 are only accessible
-via SMBus, while the SCH311x only provides access via the ISA bus. The driver
-will therefore register itself as an I2C client driver if it detects a DME1737
-or A8000 and as a platform driver if it detects a SCH311x chip.
+The hardware monitoring features of the DME1737, A8000, and SCH5027 are only
+accessible via SMBus, while the SCH311x only provides access via the ISA bus.
+The driver will therefore register itself as an I2C client driver if it detects
+a DME1737, A8000, or SCH5027 and as a platform driver if it detects a SCH311x
+chip.

 Voltage Monitoring
@@ -64,6 +66,7 @@ scaling resistors. The values returned by the driver therefore reflect true
 millivolts and don't need scaling. The voltage inputs are mapped as follows
 (the last column indicates the input ranges):

+DME1737, A8000:
    in0: +5VTR  (+5V standby)       0V - 6.64V
    in1: Vccp   (processor core)    0V - 3V
    in2: VCC    (internal +3.3V)    0V - 4.38V
@@ -72,6 +75,24 @@ millivolts and don't need scaling. The voltage inputs are mapped as follows
    in5: VTR    (+3.3V standby)     0V - 4.38V
    in6: Vbat   (+3.0V)         0V - 4.38V

+SCH311x:
+   in0: +2.5V              0V - 6.64V
+   in1: Vccp   (processor core)    0V - 2V
+   in2: VCC    (internal +3.3V)    0V - 4.38V
+   in3: +5V                0V - 6.64V
+   in4: +12V               0V - 16V
+   in5: VTR    (+3.3V standby)     0V - 4.38V
+   in6: Vbat   (+3.0V)         0V - 4.38V
+
+SCH5027:
+   in0: +5VTR  (+5V standby)       0V - 6.64V
+   in1: Vccp   (processor core)    0V - 3V
+   in2: VCC    (internal +3.3V)    0V - 4.38V
+   in3: V2_IN              0V - 1.5V
+   in4: V1_IN              0V - 1.5V
+   in5: VTR    (+3.3V standby)     0V - 4.38V
+   in6: Vbat   (+3.0V)         0V - 4.38V
+
 Each voltage input has associated min and max limits which trigger an alarm
 when crossed.

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index c882fd0..1de240a 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -575,8 +575,8 @@ config SENSORS_DME1737
    select HWMON_VID
    help
      If you say yes here you get support for the hardware monitoring
-     and fan control features of the SMSC DME1737 (and compatibles
-     like the Asus A8000) and SCH311x Super-I/O chips.
+     and fan control features of the SMSC DME1737, SCH311x, SCH5027, and
+     Asus A8000 Super-I/O chips.

      This driver can also be built as a module.  If so, the module
      will be called dme1737.
diff --git a/drivers/hwmon/dme1737.c b/drivers/hwmon/dme1737.c
index b362900..cdb8311 100644
--- a/drivers/hwmon/dme1737.c
+++ b/drivers/hwmon/dme1737.c
@@ -1,11 +1,11 @@
 /*
- * dme1737.c - Driver for the SMSC DME1737, Asus A8000, and SMSC SCH311x
- *             Super-I/O chips integrated hardware monitoring features.
- * Copyright (c) 2007 Juerg Haefliger <juergh@gmail.com>
+ * dme1737.c - Driver for the SMSC DME1737, Asus A8000, SMSC SCH311x and
+ *             SCH5027 Super-I/O chips integrated hardware monitoring features.
+ * Copyright (c) 2007, 2008 Juerg Haefliger <juergh@gmail.com>
  *
  * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access
- * the chip registers if a DME1737 (or A8000) is found and the ISA bus if a
- * SCH311x chip is found. Both types of chips have very similar hardware
+ * the chip registers if a DME1737, A8000, or SCH5027 is found and the ISA bus
+ * if a SCH311x chip is found. Both types of chips have very similar hardware
  * monitoring capabilities but differ in the way they can be accessed.
  *
  * This program is free software; you can redistribute it and/or modify
@@ -57,7 +57,10 @@
 static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};

 /* Insmod parameters */
-I2C_CLIENT_INSMOD_1(dme1737);
+I2C_CLIENT_INSMOD_2(dme1737, sch5027);
+
+/* ISA chip types */
+enum isa_chips { sch311x = sch5027 + 1 };

 /* ---------------------------------------------------------------------
  * Registers
@@ -163,6 +166,7 @@
 #define DME1737_VERSTEP        0x88
 #define DME1737_VERSTEP_MASK   0xf8
 #define SCH311X_DEVICE     0x8c
+#define SCH5027_VERSTEP        0x69

 /* Length of ISA address segment */
 #define DME1737_EXTENT 2
@@ -182,6 +186,7 @@ struct dme1737_data {
    unsigned long last_update;  /* in jiffies */
    unsigned long last_vbat;    /* in jiffies */
    enum chips type;
+   const int *in_nominal;      /* pointer to IN_NOMINAL array */

    u8 vid;
    u8 pwm_rr_en;
@@ -220,23 +225,23 @@ struct dme1737_data {
                     3300};
 static const int IN_NOMINAL_SCH311x[] = {2500, 1500, 3300, 5000, 12000, 3300,
                     3300};
-#define IN_NOMINAL(ix, type)   (((type) == dme1737) ? 
-               IN_NOMINAL_DME1737[(ix)] : 
-               IN_NOMINAL_SCH311x[(ix)])
+static const int IN_NOMINAL_SCH5027[] = {5000, 2250, 3300, 1125, 1125, 3300,
+                    3300};
+#define IN_NOMINAL(type)   ((type) == sch311x ? IN_NOMINAL_SCH311x : 
+                (type) == sch5027 ? IN_NOMINAL_SCH5027 : 
+                IN_NOMINAL_DME1737)

 /* Voltage input
  * Voltage inputs have 16 bits resolution, limit values have 8 bits
  * resolution. */
-static inline int IN_FROM_REG(int reg, int ix, int res, int type)
+static inline int IN_FROM_REG(int reg, int nominal, int res)
 {
-   return (reg * IN_NOMINAL(ix, type) + (3 << (res - 3))) /
-       (3 << (res - 2));
+   return (reg * nominal + (3 << (res - 3))) / (3 << (res - 2));
 }

-static inline int IN_TO_REG(int val, int ix, int type)
+static inline int IN_TO_REG(int val, int nominal)
 {
-   return SENSORS_LIMIT((val * 192 + IN_NOMINAL(ix, type) / 2) /
-                IN_NOMINAL(ix, type), 0, 255);
+   return SENSORS_LIMIT((val * 192 + nominal / 2) / nominal, 0, 255);
 }

 /* Temperature input
@@ -565,7 +570,10 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)

    /* Sample register contents every 1 sec */
    if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
-       data->vid = dme1737_read(client, DME1737_REG_VID) & 0x3f;
+       if (data->type != sch5027) {
+           data->vid = dme1737_read(client, DME1737_REG_VID) &
+               0x3f;
+       }

        /* In (voltage) registers */
        for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
@@ -593,8 +601,10 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)
                    DME1737_REG_TEMP_MIN(ix));
            data->temp_max[ix] = dme1737_read(client,
                    DME1737_REG_TEMP_MAX(ix));
-           data->temp_offset[ix] = dme1737_read(client,
-                   DME1737_REG_TEMP_OFFSET(ix));
+           if (data->type != sch5027) {
+               data->temp_offset[ix] = dme1737_read(client,
+                       DME1737_REG_TEMP_OFFSET(ix));
+           }
        }

        /* In and temp LSB registers
@@ -669,9 +679,11 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)
            data->zone_abs[ix] = dme1737_read(client,
                    DME1737_REG_ZONE_ABS(ix));
        }
-       for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
-           data->zone_hyst[ix] = dme1737_read(client,
+       if (data->type != sch5027) {
+           for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
+               data->zone_hyst[ix] = dme1737_read(client,
                        DME1737_REG_ZONE_HYST(ix));
+           }
        }

        /* Alarm registers */
@@ -735,13 +747,13 @@ static ssize_t show_in(struct device *dev, struct device_attribute *attr,

    switch (fn) {
    case SYS_IN_INPUT:
-       res = IN_FROM_REG(data->in[ix], ix, 16, data->type);
+       res = IN_FROM_REG(data->in[ix], data->in_nominal[ix], 16);
        break;
    case SYS_IN_MIN:
-       res = IN_FROM_REG(data->in_min[ix], ix, 8, data->type);
+       res = IN_FROM_REG(data->in_min[ix], data->in_nominal[ix], 8);
        break;
    case SYS_IN_MAX:
-       res = IN_FROM_REG(data->in_max[ix], ix, 8, data->type);
+       res = IN_FROM_REG(data->in_max[ix], data->in_nominal[ix], 8);
        break;
    case SYS_IN_ALARM:
        res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
@@ -768,12 +780,12 @@ static ssize_t set_in(struct device *dev, struct device_attribute *attr,
    mutex_lock(&data->update_lock);
    switch (fn) {
    case SYS_IN_MIN:
-       data->in_min[ix] = IN_TO_REG(val, ix, data->type);
+       data->in_min[ix] = IN_TO_REG(val, data->in_nominal[ix]);
        dme1737_write(client, DME1737_REG_IN_MIN(ix),
                  data->in_min[ix]);
        break;
    case SYS_IN_MAX:
-       data->in_max[ix] = IN_TO_REG(val, ix, data->type);
+       data->in_max[ix] = IN_TO_REG(val, data->in_nominal[ix]);
        dme1737_write(client, DME1737_REG_IN_MAX(ix),
                  data->in_max[ix]);
        break;
@@ -1570,43 +1582,56 @@ static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, 
    &sensor_dev_attr_temp1_max.dev_attr.attr,
    &sensor_dev_attr_temp1_alarm.dev_attr.attr,
    &sensor_dev_attr_temp1_fault.dev_attr.attr,
-   &sensor_dev_attr_temp1_offset.dev_attr.attr,
    &sensor_dev_attr_temp2_input.dev_attr.attr,
    &sensor_dev_attr_temp2_min.dev_attr.attr,
    &sensor_dev_attr_temp2_max.dev_attr.attr,
    &sensor_dev_attr_temp2_alarm.dev_attr.attr,
    &sensor_dev_attr_temp2_fault.dev_attr.attr,
-   &sensor_dev_attr_temp2_offset.dev_attr.attr,
    &sensor_dev_attr_temp3_input.dev_attr.attr,
    &sensor_dev_attr_temp3_min.dev_attr.attr,
    &sensor_dev_attr_temp3_max.dev_attr.attr,
    &sensor_dev_attr_temp3_alarm.dev_attr.attr,
    &sensor_dev_attr_temp3_fault.dev_attr.attr,
-   &sensor_dev_attr_temp3_offset.dev_attr.attr,
    /* Zones */
-   &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
-   &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
-   &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
+   NULL
+};
+
+static const struct attribute_group dme1737_group = {
+   .attrs = dme1737_attr,
+};
+
+/* The following struct holds misc attributes, which are not available in all
+ * chips. Their creation depends on the chip type which is determined during
+ * module load. */
+static struct attribute *dme1737_misc_attr[] = {
+   /* Temperatures */
+   &sensor_dev_attr_temp1_offset.dev_attr.attr,
+   &sensor_dev_attr_temp2_offset.dev_attr.attr,
+   &sensor_dev_attr_temp3_offset.dev_attr.attr,
+   /* Zones */
+   &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
+   &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
+   &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
    /* Misc */
    &dev_attr_vrm.attr,
    &dev_attr_cpu0_vid.attr,
    NULL
 };

-static const struct attribute_group dme1737_group = {
-   .attrs = dme1737_attr,
+static const struct attribute_group dme1737_misc_group = {
+   .attrs = dme1737_misc_attr,
 };

 /* The following structs hold the PWM attributes, some of which are optional.
@@ -1618,7 +1643,6 @@ static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, 
    &sensor_dev_attr_pwm1_enable.dev_attr.attr,
    &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
-   &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
    NULL
@@ -1629,7 +1653,6 @@ static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, 
    &sensor_dev_attr_pwm2_enable.dev_attr.attr,
    &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
-   &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
    NULL
@@ -1640,7 +1663,6 @@ static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, 
    &sensor_dev_attr_pwm3_enable.dev_attr.attr,
    &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
-   &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
    NULL
@@ -1667,6 +1689,15 @@ static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, 
    { .attrs = dme1737_pwm6_attr },
 };

+/* The following struct holds misc PWM attributes, which are not available in
+ * all chips. Their creation depends on the chip type which is determined
+ * during module load. */
+static struct attribute *dme1737_pwm_misc_attr[] = {
+   &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
+   &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
+   &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
+};
+
 /* The following structs hold the fan attributes, some of which are optional.
  * Their creation depends on the chip configuration which is determined during
  * module load. */
@@ -1722,31 +1753,23 @@ static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, 
    { .attrs = dme1737_fan6_attr },
 };

-/* The permissions of all of the following attributes are changed to read-
+/* The permissions of the following zone attributes are changed to read-
  * writeable if the chip is *not* locked. Otherwise they stay read-only. */
-static struct attribute *dme1737_misc_chmod_attr[] = {
-   /* Temperatures */
-   &sensor_dev_attr_temp1_offset.dev_attr.attr,
-   &sensor_dev_attr_temp2_offset.dev_attr.attr,
-   &sensor_dev_attr_temp3_offset.dev_attr.attr,
-   /* Zones */
-   &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
+static struct attribute *dme1737_zone_chmod_attr[] = {
    &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
-   &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
-   &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
    NULL
 };

-static const struct attribute_group dme1737_misc_chmod_group = {
-   .attrs = dme1737_misc_chmod_attr,
+static const struct attribute_group dme1737_zone_chmod_group = {
+   .attrs = dme1737_zone_chmod_attr,
 };

 /* The permissions of the following PWM attributes are changed to read-
@@ -1757,7 +1780,6 @@ static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, 
    &sensor_dev_attr_pwm1_enable.dev_attr.attr,
    &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
-   &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
    NULL
 };
@@ -1766,7 +1788,6 @@ static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, 
    &sensor_dev_attr_pwm2_enable.dev_attr.attr,
    &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
-   &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
    NULL
 };
@@ -1775,7 +1796,6 @@ static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, 
    &sensor_dev_attr_pwm3_enable.dev_attr.attr,
    &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
-   &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
    NULL
 };
@@ -1875,9 +1895,17 @@ static void dme1737_remove_files(struct device *dev)
        if (data->has_pwm & (1 << ix)) {
            sysfs_remove_group(&dev->kobj,
                       &dme1737_pwm_group[ix]);
+           if (data->type != sch5027 && ix < 3) {
+               sysfs_remove_file(&dev->kobj,
+                         dme1737_pwm_misc_attr[ix]);
+           }
        }
    }

+   if (data->type != sch5027) {
+       sysfs_remove_group(&dev->kobj, &dme1737_misc_group);
+   }
+
    sysfs_remove_group(&dev->kobj, &dme1737_group);

    if (!data->client.driver) {
@@ -1901,6 +1929,13 @@ static int dme1737_create_files(struct device *dev)
        goto exit_remove;
    }

+   /* Create misc sysfs attributes */
+   if ((data->type != sch5027) &&
+       (err = sysfs_create_group(&dev->kobj,
+                     &dme1737_misc_group))) {
+       goto exit_remove;
+   }
+
    /* Create fan sysfs attributes */
    for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
        if (data->has_fan & (1 << ix)) {
@@ -1918,6 +1953,11 @@ static int dme1737_create_files(struct device *dev)
                        &dme1737_pwm_group[ix]))) {
                goto exit_remove;
            }
+           if (data->type != sch5027 && ix < 3 &&
+               (err = sysfs_create_file(&dev->kobj,
+                       dme1737_pwm_misc_attr[ix]))) {
+               goto exit_remove;
+           }
        }
    }

@@ -1927,16 +1967,27 @@ static int dme1737_create_files(struct device *dev)
        dev_info(dev, "Device is locked. Some attributes "
             "will be read-only.n");
    } else {
-       /* Change permissions of standard sysfs attributes */
-       dme1737_chmod_group(dev, &dme1737_misc_chmod_group,
+       /* Change permissions of zone sysfs attributes */
+       dme1737_chmod_group(dev, &dme1737_zone_chmod_group,
                    S_IRUGO | S_IWUSR);

+       /* Change permissions of misc sysfs attributes */
+       if (data->type != sch5027) {
+           dme1737_chmod_group(dev, &dme1737_misc_group,
+                       S_IRUGO | S_IWUSR);
+       }
+
        /* Change permissions of PWM sysfs attributes */
        for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_chmod_group); ix++) {
            if (data->has_pwm & (1 << ix)) {
                dme1737_chmod_group(dev,
                        &dme1737_pwm_chmod_group[ix],
                        S_IRUGO | S_IWUSR);
+               if (data->type != sch5027 && ix < 3) {
+                   dme1737_chmod_file(dev,
+                       dme1737_pwm_misc_attr[ix],
+                       S_IRUGO | S_IWUSR);
+               }
            }
        }

@@ -1966,6 +2017,9 @@ static int dme1737_init_device(struct device *dev)
    int ix;
    u8 reg;

+   /* Point to the right nominal voltages array */
+   data->in_nominal = IN_NOMINAL(data->type);
+
    data->config = dme1737_read(client, DME1737_REG_CONFIG);
    /* Inform if part is not monitoring/started */
    if (!(data->config & 0x01)) {
@@ -2076,7 +2130,9 @@ static int dme1737_init_device(struct device *dev)
    data->pwm_acz[2] = 4;   /* pwm3 -> zone3 */

    /* Set VRM */
-   data->vrm = vid_which_vrm();
+   if (data->type != sch5027) {
+       data->vrm = vid_which_vrm();
+   }

    return 0;
 }
@@ -2095,9 +2151,10 @@ static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
    dme1737_sio_enter(sio_cip);

    /* Check device ID
-    * The DME1737 can return either 0x78 or 0x77 as its device ID. */
+    * The DME1737 can return either 0x78 or 0x77 as its device ID.
+    * The SCH5027 returns 0x89 as its device ID. */
    reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
-   if (!(reg == 0x77 || reg == 0x78)) {
+   if (!(reg == 0x77 || reg == 0x78 || reg == 0x89)) {
        err = -ENODEV;
        goto exit;
    }
@@ -2166,15 +2223,24 @@ static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
        company = dme1737_read(client, DME1737_REG_COMPANY);
        verstep = dme1737_read(client, DME1737_REG_VERSTEP);

-       if (!((company == DME1737_COMPANY_SMSC) &&
-             ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) {
+       if (company == DME1737_COMPANY_SMSC &&
+           (verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP) {
+           kind = dme1737;
+       } else if (company == DME1737_COMPANY_SMSC &&
+              verstep == SCH5027_VERSTEP) {
+           kind = sch5027;
+       } else {
            err = -ENODEV;
            goto exit_kfree;
        }
    }

-   kind = dme1737;
-   name = "dme1737";
+   if (kind == sch5027) {
+       name = "sch5027";
+   } else {
+       kind = dme1737;
+       name = "dme1737";
+   }
    data->type = kind;

    /* Fill in the remaining client fields and put it into the global
@@ -2187,8 +2253,9 @@ static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
        goto exit_kfree;
    }

-   dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).n",
-        client->addr, verstep);
+   dev_info(dev, "Found a %s chip at 0x%02x (rev 0x%02x).n",
+        kind == sch5027 ? "SCH5027" : "DME1737", client->addr,
+        verstep);

    /* Initialize the DME1737 chip */
    if ((err = dme1737_init_device(dev))) {
@@ -2371,7 +2438,7 @@ static int __devinit dme1737_isa_probe(struct platform_device *pdev)
            goto exit_kfree;
        }
    }
-   data->type = -1;
+   data->type = sch311x;

    /* Fill in the remaining client fields and initialize the mutex */
    strlcpy(client->name, "sch311x", I2C_NAME_SIZE);
 
/*
 * dme1737.c - Driver for the SMSC DME1737, Asus A8000, SMSC SCH311x, SCH5027,
 *             and SCH5127 Super-I/O chips integrated hardware monitoring
 *             features.
 * Copyright (c) 2007, 2008, 2009, 2010 Juerg Haefliger <juergh@gmail.com>
 *
 * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access
 * the chip registers if a DME1737, A8000, or SCH5027 is found and the ISA bus
 * if a SCH311x or SCH5127 chip is found. Both types of chips have very
 * similar hardware monitoring capabilities but differ in the way they can be
 * accessed.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/hwmon-vid.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/acpi.h>
#include <linux/io.h>

/* ISA device, if found */
static struct platform_device *pdev;

/* Module load parameters */
static int force_start;
module_param(force_start, bool, 0);
MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");

static unsigned short force_id;
module_param(force_id, ushort, 0);
MODULE_PARM_DESC(force_id, "Override the detected device ID");

static int probe_all_addr;
module_param(probe_all_addr, bool, 0);
MODULE_PARM_DESC(probe_all_addr, "Include probing of non-standard LPC "
         "addresses");

/* Addresses to scan */
static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};

enum chips { dme1737, sch5027, sch311x, sch5127 };

/* ---------------------------------------------------------------------
 * Registers
 *
 * The sensors are defined as follows:
 *
 * Voltages                          Temperatures
 * --------                          ------------
 * in0   +5VTR (+5V stdby)           temp1   Remote diode 1
 * in1   Vccp  (proc core)           temp2   Internal temp
 * in2   VCC   (internal +3.3V)      temp3   Remote diode 2
 * in3   +5V
 * in4   +12V
 * in5   VTR   (+3.3V stby)
 * in6   Vbat
 *
 * --------------------------------------------------------------------- */

/* Voltages (in) numbered 0-6 (ix) */
#define    DME1737_REG_IN(ix)        ((ix) < 5 ? 0x20 + (ix) \
                          : 0x94 + (ix))
#define    DME1737_REG_IN_MIN(ix)        ((ix) < 5 ? 0x44 + (ix) * 2 \
                          : 0x91 + (ix) * 2)
#define    DME1737_REG_IN_MAX(ix)        ((ix) < 5 ? 0x45 + (ix) * 2 \
                          : 0x92 + (ix) * 2)

/* Temperatures (temp) numbered 0-2 (ix) */
#define DME1737_REG_TEMP(ix)        (0x25 + (ix))
#define DME1737_REG_TEMP_MIN(ix)    (0x4e + (ix) * 2)
#define DME1737_REG_TEMP_MAX(ix)    (0x4f + (ix) * 2)
#define DME1737_REG_TEMP_OFFSET(ix)    ((ix) == 0 ? 0x1f \
                           : 0x1c + (ix))

/* Voltage and temperature LSBs
 * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
 *    IN_TEMP_LSB(0) = [in5, in6]
 *    IN_TEMP_LSB(1) = [temp3, temp1]
 *    IN_TEMP_LSB(2) = [in4, temp2]
 *    IN_TEMP_LSB(3) = [in3, in0]
 *    IN_TEMP_LSB(4) = [in2, in1] */
#define DME1737_REG_IN_TEMP_LSB(ix)    (0x84 + (ix))
static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0};
static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4};
static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};

/* Fans numbered 0-5 (ix) */
#define DME1737_REG_FAN(ix)        ((ix) < 4 ? 0x28 + (ix) * 2 \
                          : 0xa1 + (ix) * 2)
#define DME1737_REG_FAN_MIN(ix)        ((ix) < 4 ? 0x54 + (ix) * 2 \
                          : 0xa5 + (ix) * 2)
#define DME1737_REG_FAN_OPT(ix)        ((ix) < 4 ? 0x90 + (ix) \
                          : 0xb2 + (ix))
#define DME1737_REG_FAN_MAX(ix)        (0xb4 + (ix)) /* only for fan[4-5] */

/* PWMs numbered 0-2, 4-5 (ix) */
#define DME1737_REG_PWM(ix)        ((ix) < 3 ? 0x30 + (ix) \
                          : 0xa1 + (ix))
#define DME1737_REG_PWM_CONFIG(ix)    (0x5c + (ix)) /* only for pwm[0-2] */
#define DME1737_REG_PWM_MIN(ix)        (0x64 + (ix)) /* only for pwm[0-2] */
#define DME1737_REG_PWM_FREQ(ix)    ((ix) < 3 ? 0x5f + (ix) \
                          : 0xa3 + (ix))
/* The layout of the ramp rate registers is different from the other pwm
 * registers. The bits for the 3 PWMs are stored in 2 registers:
 *    PWM_RR(0) = [OFF3, OFF2,  OFF1,  RES,   RR1E, RR1-2, RR1-1, RR1-0]
 *    PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
#define DME1737_REG_PWM_RR(ix)        (0x62 + (ix)) /* only for pwm[0-2] */

/* Thermal zones 0-2 */
#define DME1737_REG_ZONE_LOW(ix)    (0x67 + (ix))
#define DME1737_REG_ZONE_ABS(ix)    (0x6a + (ix))
/* The layout of the hysteresis registers is different from the other zone
 * registers. The bits for the 3 zones are stored in 2 registers:
 *    ZONE_HYST(0) = [H1-3,  H1-2,  H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
 *    ZONE_HYST(1) = [H3-3,  H3-2,  H3-1, H3-0, RES,  RES,  RES,  RES] */
#define DME1737_REG_ZONE_HYST(ix)    (0x6d + (ix))

/* Alarm registers and bit mapping
 * The 3 8-bit alarm registers will be concatenated to a single 32-bit
 * alarm value [0, ALARM3, ALARM2, ALARM1]. */
#define DME1737_REG_ALARM1        0x41
#define DME1737_REG_ALARM2        0x42
#define DME1737_REG_ALARM3        0x83
static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17};
static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};

/* Miscellaneous registers */
#define DME1737_REG_DEVICE        0x3d
#define DME1737_REG_COMPANY        0x3e
#define DME1737_REG_VERSTEP        0x3f
#define DME1737_REG_CONFIG        0x40
#define DME1737_REG_CONFIG2        0x7f
#define DME1737_REG_VID            0x43
#define DME1737_REG_TACH_PWM        0x81

/* ---------------------------------------------------------------------
 * Misc defines
 * --------------------------------------------------------------------- */

/* Chip identification */
#define DME1737_COMPANY_SMSC    0x5c
#define DME1737_VERSTEP        0x88
#define DME1737_VERSTEP_MASK    0xf8
#define SCH311X_DEVICE        0x8c
#define SCH5027_VERSTEP        0x69
#define SCH5127_DEVICE        0x8e

/* Device ID values (global configuration register index 0x20) */
#define DME1737_ID_1    0x77
#define DME1737_ID_2    0x78
#define SCH3112_ID    0x7c
#define SCH3114_ID    0x7d
#define SCH3116_ID    0x7f
#define SCH5027_ID    0x89
#define SCH5127_ID    0x86

/* Length of ISA address segment */
#define DME1737_EXTENT    2

/* chip-dependent features */
#define HAS_TEMP_OFFSET        (1 << 0)        /* bit 0 */
#define HAS_VID            (1 << 1)        /* bit 1 */
#define HAS_ZONE3        (1 << 2)        /* bit 2 */
#define HAS_ZONE_HYST        (1 << 3)        /* bit 3 */
#define HAS_PWM_MIN        (1 << 4)        /* bit 4 */
#define HAS_FAN(ix)        (1 << ((ix) + 5))    /* bits 5-10 */
#define HAS_PWM(ix)        (1 << ((ix) + 11))    /* bits 11-16 */

/* ---------------------------------------------------------------------
 * Data structures and manipulation thereof
 * --------------------------------------------------------------------- */

struct dme1737_data {
    struct i2c_client *client;    /* for I2C devices only */
    struct device *hwmon_dev;
    const char *name;
    unsigned int addr;        /* for ISA devices only */

    struct mutex update_lock;
    int valid;            /* !=0 if following fields are valid */
    unsigned long last_update;    /* in jiffies */
    unsigned long last_vbat;    /* in jiffies */
    enum chips type;
    const int *in_nominal;        /* pointer to IN_NOMINAL array */

    u8 vid;
    u8 pwm_rr_en;
    u32 has_features;

    /* Register values */
    u16 in[7];
    u8  in_min[7];
    u8  in_max[7];
    s16 temp[3];
    s8  temp_min[3];
    s8  temp_max[3];
    s8  temp_offset[3];
    u8  config;
    u8  config2;
    u8  vrm;
    u16 fan[6];
    u16 fan_min[6];
    u8  fan_max[2];
    u8  fan_opt[6];
    u8  pwm[6];
    u8  pwm_min[3];
    u8  pwm_config[3];
    u8  pwm_acz[3];
    u8  pwm_freq[6];
    u8  pwm_rr[2];
    u8  zone_low[3];
    u8  zone_abs[3];
    u8  zone_hyst[2];
    u32 alarms;
};

/* Nominal voltage values */
static const int IN_NOMINAL_DME1737[] = {5000, 2250, 3300, 5000, 12000, 3300,
                     3300};
static const int IN_NOMINAL_SCH311x[] = {2500, 1500, 3300, 5000, 12000, 3300,
                     3300};
static const int IN_NOMINAL_SCH5027[] = {5000, 2250, 3300, 1125, 1125, 3300,
                     3300};
static const int IN_NOMINAL_SCH5127[] = {2500, 2250, 3300, 1125, 1125, 3300,
                     3300};
#define IN_NOMINAL(type)    ((type) == sch311x ? IN_NOMINAL_SCH311x : \
                 (type) == sch5027 ? IN_NOMINAL_SCH5027 : \
                 (type) == sch5127 ? IN_NOMINAL_SCH5127 : \
                 IN_NOMINAL_DME1737)

/* Voltage input
 * Voltage inputs have 16 bits resolution, limit values have 8 bits
 * resolution. */
static inline int IN_FROM_REG(int reg, int nominal, int res)
{
    return (reg * nominal + (3 << (res - 3))) / (3 << (res - 2));
}

static inline int IN_TO_REG(int val, int nominal)
{
    return SENSORS_LIMIT((val * 192 + nominal / 2) / nominal, 0, 255);
}

/* Temperature input
 * The register values represent temperatures in 2's complement notation from
 * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
 * values have 8 bits resolution. */
static inline int TEMP_FROM_REG(int reg, int res)
{
    return (reg * 1000) >> (res - 8);
}

static inline int TEMP_TO_REG(int val)
{
    return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
                 -128, 127);
}

/* Temperature range */
static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
                 10000, 13333, 16000, 20000, 26666, 32000,
                 40000, 53333, 80000};

static inline int TEMP_RANGE_FROM_REG(int reg)
{
    return TEMP_RANGE[(reg >> 4) & 0x0f];
}

static int TEMP_RANGE_TO_REG(int val, int reg)
{
    int i;

    for (i = 15; i > 0; i--) {
        if (val > (TEMP_RANGE + TEMP_RANGE[i - 1] + 1) / 2) {
            break;
        }
    }

    return (reg & 0x0f) | (i << 4);
}

/* Temperature hysteresis
 * Register layout:
 *    reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
 *    reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
static inline int TEMP_HYST_FROM_REG(int reg, int ix)
{
    return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
}

static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
{
    int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);

    return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
}

/* Fan input RPM */
static inline int FAN_FROM_REG(int reg, int tpc)
{
    if (tpc) {
        return tpc * reg;
    } else {
        return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
    }
}

static inline int FAN_TO_REG(int val, int tpc)
{
    if (tpc) {
        return SENSORS_LIMIT(val / tpc, 0, 0xffff);
    } else {
        return (val <= 0) ? 0xffff :
            SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
    }
}

/* Fan TPC (tach pulse count)
 * Converts a register value to a TPC multiplier or returns 0 if the tachometer
 * is configured in legacy (non-tpc) mode */
static inline int FAN_TPC_FROM_REG(int reg)
{
    return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
}

/* Fan type
 * The type of a fan is expressed in number of pulses-per-revolution that it
 * emits */
static inline int FAN_TYPE_FROM_REG(int reg)
{
    int edge = (reg >> 1) & 0x03;

    return (edge > 0) ? 1 << (edge - 1) : 0;
}

static inline int FAN_TYPE_TO_REG(int val, int reg)
{
    int edge = (val == 4) ? 3 : val;

    return (reg & 0xf9) | (edge << 1);
}

/* Fan max RPM */
static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
                  0x11, 0x0f, 0x0e};

static int FAN_MAX_FROM_REG(int reg)
{
    int i;

    for (i = 10; i > 0; i--) {
        if (reg == FAN_MAX) {
            break;
        }
    }

    return 1000 + i * 500;
}

static int FAN_MAX_TO_REG(int val)
{
    int i;

    for (i = 10; i > 0; i--) {
        if (val > (1000 + (i - 1) * 500)) {
            break;
        }
    }

    return FAN_MAX;
}

/* PWM enable
 * Register to enable mapping:
 * 000:  2  fan on zone 1 auto
 * 001:  2  fan on zone 2 auto
 * 010:  2  fan on zone 3 auto
 * 011:  0  fan full on
 * 100: -1  fan disabled
 * 101:  2  fan on hottest of zones 2,3 auto
 * 110:  2  fan on hottest of zones 1,2,3 auto
 * 111:  1  fan in manual mode */
static inline int PWM_EN_FROM_REG(int reg)
{
    static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};

    return en[(reg >> 5) & 0x07];
}

static inline int PWM_EN_TO_REG(int val, int reg)
{
    int en = (val == 1) ? 7 : 3;

    return (reg & 0x1f) | ((en & 0x07) << 5);
}

/* PWM auto channels zone
 * Register to auto channels zone mapping (ACZ is a bitfield with bit x
 * corresponding to zone x+1):
 * 000: 001  fan on zone 1 auto
 * 001: 010  fan on zone 2 auto
 * 010: 100  fan on zone 3 auto
 * 011: 000  fan full on
 * 100: 000  fan disabled
 * 101: 110  fan on hottest of zones 2,3 auto
 * 110: 111  fan on hottest of zones 1,2,3 auto
 * 111: 000  fan in manual mode */
static inline int PWM_ACZ_FROM_REG(int reg)
{
    static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};

    return acz[(reg >> 5) & 0x07];
}

static inline int PWM_ACZ_TO_REG(int val, int reg)
{
    int acz = (val == 4) ? 2 : val - 1;

    return (reg & 0x1f) | ((acz & 0x07) << 5);
}

/* PWM frequency */
static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
                   15000, 20000, 30000, 25000, 0, 0, 0, 0};

static inline int PWM_FREQ_FROM_REG(int reg)
{
    return PWM_FREQ[reg & 0x0f];
}

static int PWM_FREQ_TO_REG(int val, int reg)
{
    int i;

    /* the first two cases are special - stupid chip design! */
    if (val > 27500) {
        i = 10;
    } else if (val > 22500) {
        i = 11;
    } else {
        for (i = 9; i > 0; i--) {
            if (val > (PWM_FREQ + PWM_FREQ[i - 1] + 1) / 2) {
                break;
            }
        }
    }

    return (reg & 0xf0) | i;
}

/* PWM ramp rate
 * Register layout:
 *    reg[0] = [OFF3,  OFF2,  OFF1,  RES,   RR1-E, RR1-2, RR1-1, RR1-0]
 *    reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};

static inline int PWM_RR_FROM_REG(int reg, int ix)
{
    int rr = (ix == 1) ? reg >> 4 : reg;

    return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
}

static int PWM_RR_TO_REG(int val, int ix, int reg)
{
    int i;

    for (i = 0; i < 7; i++) {
        if (val > (PWM_RR + PWM_RR[i + 1] + 1) / 2) {
            break;
        }
    }

    return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
}

/* PWM ramp rate enable */
static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
{
    return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
}

static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
{
    int en = (ix == 1) ? 0x80 : 0x08;

    return val ? reg | en : reg & ~en;
}

/* PWM min/off
 * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
 * the register layout). */
static inline int PWM_OFF_FROM_REG(int reg, int ix)
{
    return (reg >> (ix + 5)) & 0x01;
}

static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
{
    return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
}

/* ---------------------------------------------------------------------
 * Device I/O access
 *
 * ISA access is performed through an index/data register pair and needs to
 * be protected by a mutex during runtime (not required for initialization).
 * We use data->update_lock for this and need to ensure that we acquire it
 * before calling dme1737_read or dme1737_write.
 * --------------------------------------------------------------------- */

static u8 dme1737_read(const struct dme1737_data *data, u8 reg)
{
    struct i2c_client *client = data->client;
    s32 val;

    if (client) { /* I2C device */
        val = i2c_smbus_read_byte_data(client, reg);

        if (val < 0) {
            dev_warn(&client->dev, "Read from register "
                 "0x%02x failed! Please report to the driver "
                 "maintainer.\n", reg);
        }
    } else { /* ISA device */
        outb(reg, data->addr);
        val = inb(data->addr + 1);
    }

    return val;
}

static s32 dme1737_write(const struct dme1737_data *data, u8 reg, u8 val)
{
    struct i2c_client *client = data->client;
    s32 res = 0;

    if (client) { /* I2C device */
        res = i2c_smbus_write_byte_data(client, reg, val);

        if (res < 0) {
            dev_warn(&client->dev, "Write to register "
                 "0x%02x failed! Please report to the driver "
                 "maintainer.\n", reg);
        }
    } else { /* ISA device */
        outb(reg, data->addr);
        outb(val, data->addr + 1);
    }

    return res;
}

static struct dme1737_data *dme1737_update_device(struct device *dev)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    int ix;
    u8 lsb[5];

    mutex_lock(&data->update_lock);

    /* Enable a Vbat monitoring cycle every 10 mins */
    if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
        dme1737_write(data, DME1737_REG_CONFIG, dme1737_read(data,
                        DME1737_REG_CONFIG) | 0x10);
        data->last_vbat = jiffies;
    }

    /* Sample register contents every 1 sec */
    if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
        if (data->has_features & HAS_VID) {
            data->vid = dme1737_read(data, DME1737_REG_VID) &
                0x3f;
        }

        /* In (voltage) registers */
        for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
            /* Voltage inputs are stored as 16 bit values even
             * though they have only 12 bits resolution. This is
             * to make it consistent with the temp inputs. */
            data->in[ix] = dme1737_read(data,
                    DME1737_REG_IN(ix)) << 8;
            data->in_min[ix] = dme1737_read(data,
                    DME1737_REG_IN_MIN(ix));
            data->in_max[ix] = dme1737_read(data,
                    DME1737_REG_IN_MAX(ix));
        }

        /* Temp registers */
        for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
            /* Temp inputs are stored as 16 bit values even
             * though they have only 12 bits resolution. This is
             * to take advantage of implicit conversions between
             * register values (2's complement) and temp values
             * (signed decimal). */
            data->temp[ix] = dme1737_read(data,
                    DME1737_REG_TEMP(ix)) << 8;
            data->temp_min[ix] = dme1737_read(data,
                    DME1737_REG_TEMP_MIN(ix));
            data->temp_max[ix] = dme1737_read(data,
                    DME1737_REG_TEMP_MAX(ix));
            if (data->has_features & HAS_TEMP_OFFSET) {
                data->temp_offset[ix] = dme1737_read(data,
                        DME1737_REG_TEMP_OFFSET(ix));
            }
        }

        /* In and temp LSB registers
         * The LSBs are latched when the MSBs are read, so the order in
         * which the registers are read (MSB first, then LSB) is
         * important! */
        for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
            lsb[ix] = dme1737_read(data,
                    DME1737_REG_IN_TEMP_LSB(ix));
        }
        for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
            data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
                    DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
        }
        for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
            data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
                    DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
        }

        /* Fan registers */
        for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
            /* Skip reading registers if optional fans are not
             * present */
            if (!(data->has_features & HAS_FAN(ix))) {
                continue;
            }
            data->fan[ix] = dme1737_read(data,
                    DME1737_REG_FAN(ix));
            data->fan[ix] |= dme1737_read(data,
                    DME1737_REG_FAN(ix) + 1) << 8;
            data->fan_min[ix] = dme1737_read(data,
                    DME1737_REG_FAN_MIN(ix));
            data->fan_min[ix] |= dme1737_read(data,
                    DME1737_REG_FAN_MIN(ix) + 1) << 8;
            data->fan_opt[ix] = dme1737_read(data,
                    DME1737_REG_FAN_OPT(ix));
            /* fan_max exists only for fan[5-6] */
            if (ix > 3) {
                data->fan_max[ix - 4] = dme1737_read(data,
                    DME1737_REG_FAN_MAX(ix));
            }
        }

        /* PWM registers */
        for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
            /* Skip reading registers if optional PWMs are not
             * present */
            if (!(data->has_features & HAS_PWM(ix))) {
                continue;
            }
            data->pwm[ix] = dme1737_read(data,
                    DME1737_REG_PWM(ix));
            data->pwm_freq[ix] = dme1737_read(data,
                    DME1737_REG_PWM_FREQ(ix));
            /* pwm_config and pwm_min exist only for pwm[1-3] */
            if (ix < 3) {
                data->pwm_config[ix] = dme1737_read(data,
                        DME1737_REG_PWM_CONFIG(ix));
                data->pwm_min[ix] = dme1737_read(data,
                        DME1737_REG_PWM_MIN(ix));
            }
        }
        for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
            data->pwm_rr[ix] = dme1737_read(data,
                        DME1737_REG_PWM_RR(ix));
        }

        /* Thermal zone registers */
        for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
            /* Skip reading registers if zone3 is not present */
            if ((ix == 2) && !(data->has_features & HAS_ZONE3)) {
                continue;
            }
            /* sch5127 zone2 registers are special */
            if ((ix == 1) && (data->type == sch5127)) {
                data->zone_low[1] = dme1737_read(data,
                        DME1737_REG_ZONE_LOW(2));
                data->zone_abs[1] = dme1737_read(data,
                        DME1737_REG_ZONE_ABS(2));
            } else {
                data->zone_low[ix] = dme1737_read(data,
                        DME1737_REG_ZONE_LOW(ix));
                data->zone_abs[ix] = dme1737_read(data,
                        DME1737_REG_ZONE_ABS(ix));
            }
        }
        if (data->has_features & HAS_ZONE_HYST) {
            for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
                data->zone_hyst[ix] = dme1737_read(data,
                        DME1737_REG_ZONE_HYST(ix));
            }
        }

        /* Alarm registers */
        data->alarms = dme1737_read(data,
                        DME1737_REG_ALARM1);
        /* Bit 7 tells us if the other alarm registers are non-zero and
         * therefore also need to be read */
        if (data->alarms & 0x80) {
            data->alarms |= dme1737_read(data,
                        DME1737_REG_ALARM2) << 8;
            data->alarms |= dme1737_read(data,
                        DME1737_REG_ALARM3) << 16;
        }

        /* The ISA chips require explicit clearing of alarm bits.
         * Don't worry, an alarm will come back if the condition
         * that causes it still exists */
        if (!data->client) {
            if (data->alarms & 0xff0000) {
                dme1737_write(data, DME1737_REG_ALARM3,
                          0xff);
            }
            if (data->alarms & 0xff00) {
                dme1737_write(data, DME1737_REG_ALARM2,
                          0xff);
            }
            if (data->alarms & 0xff) {
                dme1737_write(data, DME1737_REG_ALARM1,
                          0xff);
            }
        }

        data->last_update = jiffies;
        data->valid = 1;
    }

    mutex_unlock(&data->update_lock);

    return data;
}

/* ---------------------------------------------------------------------
 * Voltage sysfs attributes
 * ix = [0-5]
 * --------------------------------------------------------------------- */

#define SYS_IN_INPUT    0
#define SYS_IN_MIN    1
#define SYS_IN_MAX    2
#define SYS_IN_ALARM    3

static ssize_t show_in(struct device *dev, struct device_attribute *attr,
               char *buf)
{
    struct dme1737_data *data = dme1737_update_device(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    int res;

    switch (fn) {
    case SYS_IN_INPUT:
        res = IN_FROM_REG(data->in[ix], data->in_nominal[ix], 16);
        break;
    case SYS_IN_MIN:
        res = IN_FROM_REG(data->in_min[ix], data->in_nominal[ix], 8);
        break;
    case SYS_IN_MAX:
        res = IN_FROM_REG(data->in_max[ix], data->in_nominal[ix], 8);
        break;
    case SYS_IN_ALARM:
        res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
        break;
    default:
        res = 0;
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }

    return sprintf(buf, "%d\n", res);
}

static ssize_t set_in(struct device *dev, struct device_attribute *attr,
              const char *buf, size_t count)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    long val = simple_strtol(buf, NULL, 10);

    mutex_lock(&data->update_lock);
    switch (fn) {
    case SYS_IN_MIN:
        data->in_min[ix] = IN_TO_REG(val, data->in_nominal[ix]);
        dme1737_write(data, DME1737_REG_IN_MIN(ix),
                  data->in_min[ix]);
        break;
    case SYS_IN_MAX:
        data->in_max[ix] = IN_TO_REG(val, data->in_nominal[ix]);
        dme1737_write(data, DME1737_REG_IN_MAX(ix),
                  data->in_max[ix]);
        break;
    default:
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }
    mutex_unlock(&data->update_lock);

    return count;
}

/* ---------------------------------------------------------------------
 * Temperature sysfs attributes
 * ix = [0-2]
 * --------------------------------------------------------------------- */

#define SYS_TEMP_INPUT            0
#define SYS_TEMP_MIN            1
#define SYS_TEMP_MAX            2
#define SYS_TEMP_OFFSET            3
#define SYS_TEMP_ALARM            4
#define SYS_TEMP_FAULT            5

static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
             char *buf)
{
    struct dme1737_data *data = dme1737_update_device(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    int res;

    switch (fn) {
    case SYS_TEMP_INPUT:
        res = TEMP_FROM_REG(data->temp[ix], 16);
        break;
    case SYS_TEMP_MIN:
        res = TEMP_FROM_REG(data->temp_min[ix], 8);
        break;
    case SYS_TEMP_MAX:
        res = TEMP_FROM_REG(data->temp_max[ix], 8);
        break;
    case SYS_TEMP_OFFSET:
        res = TEMP_FROM_REG(data->temp_offset[ix], 8);
        break;
    case SYS_TEMP_ALARM:
        res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
        break;
    case SYS_TEMP_FAULT:
        res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
        break;
    default:
        res = 0;
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }

    return sprintf(buf, "%d\n", res);
}

static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
            const char *buf, size_t count)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    long val = simple_strtol(buf, NULL, 10);

    mutex_lock(&data->update_lock);
    switch (fn) {
    case SYS_TEMP_MIN:
        data->temp_min[ix] = TEMP_TO_REG(val);
        dme1737_write(data, DME1737_REG_TEMP_MIN(ix),
                  data->temp_min[ix]);
        break;
    case SYS_TEMP_MAX:
        data->temp_max[ix] = TEMP_TO_REG(val);
        dme1737_write(data, DME1737_REG_TEMP_MAX(ix),
                  data->temp_max[ix]);
        break;
    case SYS_TEMP_OFFSET:
        data->temp_offset[ix] = TEMP_TO_REG(val);
        dme1737_write(data, DME1737_REG_TEMP_OFFSET(ix),
                  data->temp_offset[ix]);
        break;
    default:
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }
    mutex_unlock(&data->update_lock);

    return count;
}

/* ---------------------------------------------------------------------
 * Zone sysfs attributes
 * ix = [0-2]
 * --------------------------------------------------------------------- */

#define SYS_ZONE_AUTO_CHANNELS_TEMP    0
#define SYS_ZONE_AUTO_POINT1_TEMP_HYST    1
#define SYS_ZONE_AUTO_POINT1_TEMP    2
#define SYS_ZONE_AUTO_POINT2_TEMP    3
#define SYS_ZONE_AUTO_POINT3_TEMP    4

static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
             char *buf)
{
    struct dme1737_data *data = dme1737_update_device(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    int res;

    switch (fn) {
    case SYS_ZONE_AUTO_CHANNELS_TEMP:
        /* check config2 for non-standard temp-to-zone mapping */
        if ((ix == 1) && (data->config2 & 0x02)) {
            res = 4;
        } else {
            res = 1 << ix;
        }
        break;
    case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
        res = TEMP_FROM_REG(data->zone_low[ix], 8) -
              TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
        break;
    case SYS_ZONE_AUTO_POINT1_TEMP:
        res = TEMP_FROM_REG(data->zone_low[ix], 8);
        break;
    case SYS_ZONE_AUTO_POINT2_TEMP:
        /* pwm_freq holds the temp range bits in the upper nibble */
        res = TEMP_FROM_REG(data->zone_low[ix], 8) +
              TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
        break;
    case SYS_ZONE_AUTO_POINT3_TEMP:
        res = TEMP_FROM_REG(data->zone_abs[ix], 8);
        break;
    default:
        res = 0;
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }

    return sprintf(buf, "%d\n", res);
}

static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
            const char *buf, size_t count)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    long val = simple_strtol(buf, NULL, 10);

    mutex_lock(&data->update_lock);
    switch (fn) {
    case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
        /* Refresh the cache */
        data->zone_low[ix] = dme1737_read(data,
                          DME1737_REG_ZONE_LOW(ix));
        /* Modify the temp hyst value */
        data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
                    TEMP_FROM_REG(data->zone_low[ix], 8) -
                    val, ix, dme1737_read(data,
                    DME1737_REG_ZONE_HYST(ix == 2)));
        dme1737_write(data, DME1737_REG_ZONE_HYST(ix == 2),
                  data->zone_hyst[ix == 2]);
        break;
    case SYS_ZONE_AUTO_POINT1_TEMP:
        data->zone_low[ix] = TEMP_TO_REG(val);
        dme1737_write(data, DME1737_REG_ZONE_LOW(ix),
                  data->zone_low[ix]);
        break;
    case SYS_ZONE_AUTO_POINT2_TEMP:
        /* Refresh the cache */
        data->zone_low[ix] = dme1737_read(data,
                          DME1737_REG_ZONE_LOW(ix));
        /* Modify the temp range value (which is stored in the upper
         * nibble of the pwm_freq register) */
        data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
                    TEMP_FROM_REG(data->zone_low[ix], 8),
                    dme1737_read(data,
                    DME1737_REG_PWM_FREQ(ix)));
        dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
                  data->pwm_freq[ix]);
        break;
    case SYS_ZONE_AUTO_POINT3_TEMP:
        data->zone_abs[ix] = TEMP_TO_REG(val);
        dme1737_write(data, DME1737_REG_ZONE_ABS(ix),
                  data->zone_abs[ix]);
        break;
    default:
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }
    mutex_unlock(&data->update_lock);

    return count;
}

/* ---------------------------------------------------------------------
 * Fan sysfs attributes
 * ix = [0-5]
 * --------------------------------------------------------------------- */

#define SYS_FAN_INPUT    0
#define SYS_FAN_MIN    1
#define SYS_FAN_MAX    2
#define SYS_FAN_ALARM    3
#define SYS_FAN_TYPE    4

static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
            char *buf)
{
    struct dme1737_data *data = dme1737_update_device(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    int res;

    switch (fn) {
    case SYS_FAN_INPUT:
        res = FAN_FROM_REG(data->fan[ix],
                   ix < 4 ? 0 :
                   FAN_TPC_FROM_REG(data->fan_opt[ix]));
        break;
    case SYS_FAN_MIN:
        res = FAN_FROM_REG(data->fan_min[ix],
                   ix < 4 ? 0 :
                   FAN_TPC_FROM_REG(data->fan_opt[ix]));
        break;
    case SYS_FAN_MAX:
        /* only valid for fan[5-6] */
        res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
        break;
    case SYS_FAN_ALARM:
        res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
        break;
    case SYS_FAN_TYPE:
        /* only valid for fan[1-4] */
        res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
        break;
    default:
        res = 0;
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }

    return sprintf(buf, "%d\n", res);
}

static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
               const char *buf, size_t count)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    long val = simple_strtol(buf, NULL, 10);

    mutex_lock(&data->update_lock);
    switch (fn) {
    case SYS_FAN_MIN:
        if (ix < 4) {
            data->fan_min[ix] = FAN_TO_REG(val, 0);
        } else {
            /* Refresh the cache */
            data->fan_opt[ix] = dme1737_read(data,
                        DME1737_REG_FAN_OPT(ix));
            /* Modify the fan min value */
            data->fan_min[ix] = FAN_TO_REG(val,
                    FAN_TPC_FROM_REG(data->fan_opt[ix]));
        }
        dme1737_write(data, DME1737_REG_FAN_MIN(ix),
                  data->fan_min[ix] & 0xff);
        dme1737_write(data, DME1737_REG_FAN_MIN(ix) + 1,
                  data->fan_min[ix] >> 8);
        break;
    case SYS_FAN_MAX:
        /* Only valid for fan[5-6] */
        data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
        dme1737_write(data, DME1737_REG_FAN_MAX(ix),
                  data->fan_max[ix - 4]);
        break;
    case SYS_FAN_TYPE:
        /* Only valid for fan[1-4] */
        if (!(val == 1 || val == 2 || val == 4)) {
            count = -EINVAL;
            dev_warn(dev, "Fan type value %ld not "
                 "supported. Choose one of 1, 2, or 4.\n",
                 val);
            goto exit;
        }
        data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(data,
                    DME1737_REG_FAN_OPT(ix)));
        dme1737_write(data, DME1737_REG_FAN_OPT(ix),
                  data->fan_opt[ix]);
        break;
    default:
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }
exit:
    mutex_unlock(&data->update_lock);

    return count;
}

/* ---------------------------------------------------------------------
 * PWM sysfs attributes
 * ix = [0-4]
 * --------------------------------------------------------------------- */

#define SYS_PWM                0
#define SYS_PWM_FREQ            1
#define SYS_PWM_ENABLE            2
#define SYS_PWM_RAMP_RATE        3
#define SYS_PWM_AUTO_CHANNELS_ZONE    4
#define SYS_PWM_AUTO_PWM_MIN        5
#define SYS_PWM_AUTO_POINT1_PWM        6
#define SYS_PWM_AUTO_POINT2_PWM        7

static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
            char *buf)
{
    struct dme1737_data *data = dme1737_update_device(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    int res;

    switch (fn) {
    case SYS_PWM:
        if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
            res = 255;
        } else {
            res = data->pwm[ix];
        }
        break;
    case SYS_PWM_FREQ:
        res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
        break;
    case SYS_PWM_ENABLE:
        if (ix >= 3) {
            res = 1; /* pwm[5-6] hard-wired to manual mode */
        } else {
            res = PWM_EN_FROM_REG(data->pwm_config[ix]);
        }
        break;
    case SYS_PWM_RAMP_RATE:
        /* Only valid for pwm[1-3] */
        res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
        break;
    case SYS_PWM_AUTO_CHANNELS_ZONE:
        /* Only valid for pwm[1-3] */
        if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
            res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
        } else {
            res = data->pwm_acz[ix];
        }
        break;
    case SYS_PWM_AUTO_PWM_MIN:
        /* Only valid for pwm[1-3] */
        if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
            res = data->pwm_min[ix];
        } else {
            res = 0;
        }
        break;
    case SYS_PWM_AUTO_POINT1_PWM:
        /* Only valid for pwm[1-3] */
        res = data->pwm_min[ix];
        break;
    case SYS_PWM_AUTO_POINT2_PWM:
        /* Only valid for pwm[1-3] */
        res = 255; /* hard-wired */
        break;
    default:
        res = 0;
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }

    return sprintf(buf, "%d\n", res);
}

static struct attribute *dme1737_pwm_chmod_attr[];
static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);

static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
               const char *buf, size_t count)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    struct sensor_device_attribute_2
        *sensor_attr_2 = to_sensor_dev_attr_2(attr);
    int ix = sensor_attr_2->index;
    int fn = sensor_attr_2->nr;
    long val = simple_strtol(buf, NULL, 10);

    mutex_lock(&data->update_lock);
    switch (fn) {
    case SYS_PWM:
        data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
        dme1737_write(data, DME1737_REG_PWM(ix), data->pwm[ix]);
        break;
    case SYS_PWM_FREQ:
        data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(data,
                        DME1737_REG_PWM_FREQ(ix)));
        dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
                  data->pwm_freq[ix]);
        break;
    case SYS_PWM_ENABLE:
        /* Only valid for pwm[1-3] */
        if (val < 0 || val > 2) {
            count = -EINVAL;
            dev_warn(dev, "PWM enable %ld not "
                 "supported. Choose one of 0, 1, or 2.\n",
                 val);
            goto exit;
        }
        /* Refresh the cache */
        data->pwm_config[ix] = dme1737_read(data,
                        DME1737_REG_PWM_CONFIG(ix));
        if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
            /* Bail out if no change */
            goto exit;
        }
        /* Do some housekeeping if we are currently in auto mode */
        if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
            /* Save the current zone channel assignment */
            data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
                            data->pwm_config[ix]);
            /* Save the current ramp rate state and disable it */
            data->pwm_rr[ix > 0] = dme1737_read(data,
                        DME1737_REG_PWM_RR(ix > 0));
            data->pwm_rr_en &= ~(1 << ix);
            if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
                data->pwm_rr_en |= (1 << ix);
                data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
                            data->pwm_rr[ix > 0]);
                dme1737_write(data,
                          DME1737_REG_PWM_RR(ix > 0),
                          data->pwm_rr[ix > 0]);
            }
        }
        /* Set the new PWM mode */
        switch (val) {
        case 0:
            /* Change permissions of pwm[ix] to read-only */
            dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
                       S_IRUGO);
            /* Turn fan fully on */
            data->pwm_config[ix] = PWM_EN_TO_REG(0,
                            data->pwm_config[ix]);
            dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
                      data->pwm_config[ix]);
            break;
        case 1:
            /* Turn on manual mode */
            data->pwm_config[ix] = PWM_EN_TO_REG(1,
                            data->pwm_config[ix]);
            dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
                      data->pwm_config[ix]);
            /* Change permissions of pwm[ix] to read-writeable */
            dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
                       S_IRUGO | S_IWUSR);
            break;
        case 2:
            /* Change permissions of pwm[ix] to read-only */
            dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
                       S_IRUGO);
            /* Turn on auto mode using the saved zone channel
             * assignment */
            data->pwm_config[ix] = PWM_ACZ_TO_REG(
                            data->pwm_acz[ix],
                            data->pwm_config[ix]);
            dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
                      data->pwm_config[ix]);
            /* Enable PWM ramp rate if previously enabled */
            if (data->pwm_rr_en & (1 << ix)) {
                data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
                        dme1737_read(data,
                        DME1737_REG_PWM_RR(ix > 0)));
                dme1737_write(data,
                          DME1737_REG_PWM_RR(ix > 0),
                          data->pwm_rr[ix > 0]);
            }
            break;
        }
        break;
    case SYS_PWM_RAMP_RATE:
        /* Only valid for pwm[1-3] */
        /* Refresh the cache */
        data->pwm_config[ix] = dme1737_read(data,
                        DME1737_REG_PWM_CONFIG(ix));
        data->pwm_rr[ix > 0] = dme1737_read(data,
                        DME1737_REG_PWM_RR(ix > 0));
        /* Set the ramp rate value */
        if (val > 0) {
            data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
                            data->pwm_rr[ix > 0]);
        }
        /* Enable/disable the feature only if the associated PWM
         * output is in automatic mode. */
        if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
            data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
                            data->pwm_rr[ix > 0]);
        }
        dme1737_write(data, DME1737_REG_PWM_RR(ix > 0),
                  data->pwm_rr[ix > 0]);
        break;
    case SYS_PWM_AUTO_CHANNELS_ZONE:
        /* Only valid for pwm[1-3] */
        if (!(val == 1 || val == 2 || val == 4 ||
              val == 6 || val == 7)) {
            count = -EINVAL;
            dev_warn(dev, "PWM auto channels zone %ld "
                 "not supported. Choose one of 1, 2, 4, 6, "
                 "or 7.\n", val);
            goto exit;
        }
        /* Refresh the cache */
        data->pwm_config[ix] = dme1737_read(data,
                        DME1737_REG_PWM_CONFIG(ix));
        if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
            /* PWM is already in auto mode so update the temp
             * channel assignment */
            data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
                        data->pwm_config[ix]);
            dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
                      data->pwm_config[ix]);
        } else {
            /* PWM is not in auto mode so we save the temp
             * channel assignment for later use */
            data->pwm_acz[ix] = val;
        }
        break;
    case SYS_PWM_AUTO_PWM_MIN:
        /* Only valid for pwm[1-3] */
        /* Refresh the cache */
        data->pwm_min[ix] = dme1737_read(data,
                        DME1737_REG_PWM_MIN(ix));
        /* There are only 2 values supported for the auto_pwm_min
         * value: 0 or auto_point1_pwm. So if the temperature drops
         * below the auto_point1_temp_hyst value, the fan either turns
         * off or runs at auto_point1_pwm duty-cycle. */
        if (val > ((data->pwm_min[ix] + 1) / 2)) {
            data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
                        dme1737_read(data,
                        DME1737_REG_PWM_RR(0)));
        } else {
            data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
                        dme1737_read(data,
                        DME1737_REG_PWM_RR(0)));
        }
        dme1737_write(data, DME1737_REG_PWM_RR(0),
                  data->pwm_rr[0]);
        break;
    case SYS_PWM_AUTO_POINT1_PWM:
        /* Only valid for pwm[1-3] */
        data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
        dme1737_write(data, DME1737_REG_PWM_MIN(ix),
                  data->pwm_min[ix]);
        break;
    default:
        dev_dbg(dev, "Unknown function %d.\n", fn);
    }
exit:
    mutex_unlock(&data->update_lock);

    return count;
}

/* ---------------------------------------------------------------------
 * Miscellaneous sysfs attributes
 * --------------------------------------------------------------------- */

static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
            char *buf)
{
    struct i2c_client *client = to_i2c_client(dev);
    struct dme1737_data *data = i2c_get_clientdata(client);

    return sprintf(buf, "%d\n", data->vrm);
}

static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
               const char *buf, size_t count)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    long val = simple_strtol(buf, NULL, 10);

    data->vrm = val;
    return count;
}

static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
            char *buf)
{
    struct dme1737_data *data = dme1737_update_device(dev);

    return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
}

static ssize_t show_name(struct device *dev, struct device_attribute *attr,
             char *buf)
{
    struct dme1737_data *data = dev_get_drvdata(dev);

    return sprintf(buf, "%s\n", data->name);
}

/* ---------------------------------------------------------------------
 * Sysfs device attribute defines and structs
 * --------------------------------------------------------------------- */

/* Voltages 0-6 */

#define SENSOR_DEVICE_ATTR_IN(ix) \
static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
    show_in, NULL, SYS_IN_INPUT, ix); \
static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
    show_in, set_in, SYS_IN_MIN, ix); \
static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
    show_in, set_in, SYS_IN_MAX, ix); \
static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
    show_in, NULL, SYS_IN_ALARM, ix)

SENSOR_DEVICE_ATTR_IN(0);
SENSOR_DEVICE_ATTR_IN(1);
SENSOR_DEVICE_ATTR_IN(2);
SENSOR_DEVICE_ATTR_IN(3);
SENSOR_DEVICE_ATTR_IN(4);
SENSOR_DEVICE_ATTR_IN(5);
SENSOR_DEVICE_ATTR_IN(6);

/* Temperatures 1-3 */

#define SENSOR_DEVICE_ATTR_TEMP(ix) \
static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
    show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
    show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
    show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
    show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
    show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
    show_temp, NULL, SYS_TEMP_FAULT, ix-1)

SENSOR_DEVICE_ATTR_TEMP(1);
SENSOR_DEVICE_ATTR_TEMP(2);
SENSOR_DEVICE_ATTR_TEMP(3);

/* Zones 1-3 */

#define SENSOR_DEVICE_ATTR_ZONE(ix) \
static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
    show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
    show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
    show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
    show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
    show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)

SENSOR_DEVICE_ATTR_ZONE(1);
SENSOR_DEVICE_ATTR_ZONE(2);
SENSOR_DEVICE_ATTR_ZONE(3);

/* Fans 1-4 */

#define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
    show_fan, NULL, SYS_FAN_INPUT, ix-1); \
static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
    show_fan, set_fan, SYS_FAN_MIN, ix-1); \
static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
    show_fan, NULL, SYS_FAN_ALARM, ix-1); \
static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
    show_fan, set_fan, SYS_FAN_TYPE, ix-1)

SENSOR_DEVICE_ATTR_FAN_1TO4(1);
SENSOR_DEVICE_ATTR_FAN_1TO4(2);
SENSOR_DEVICE_ATTR_FAN_1TO4(3);
SENSOR_DEVICE_ATTR_FAN_1TO4(4);

/* Fans 5-6 */

#define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
    show_fan, NULL, SYS_FAN_INPUT, ix-1); \
static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
    show_fan, set_fan, SYS_FAN_MIN, ix-1); \
static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
    show_fan, NULL, SYS_FAN_ALARM, ix-1); \
static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
    show_fan, set_fan, SYS_FAN_MAX, ix-1)

SENSOR_DEVICE_ATTR_FAN_5TO6(5);
SENSOR_DEVICE_ATTR_FAN_5TO6(6);

/* PWMs 1-3 */

#define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
    show_pwm, set_pwm, SYS_PWM, ix-1); \
static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
    show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
    show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
    show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
    show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
    show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
    show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
    show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)

SENSOR_DEVICE_ATTR_PWM_1TO3(1);
SENSOR_DEVICE_ATTR_PWM_1TO3(2);
SENSOR_DEVICE_ATTR_PWM_1TO3(3);

/* PWMs 5-6 */

#define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
    show_pwm, set_pwm, SYS_PWM, ix-1); \
static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
    show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
    show_pwm, NULL, SYS_PWM_ENABLE, ix-1)

SENSOR_DEVICE_ATTR_PWM_5TO6(5);
SENSOR_DEVICE_ATTR_PWM_5TO6(6);

/* Misc */

static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);   /* for ISA devices */

/* This struct holds all the attributes that are always present and need to be
 * created unconditionally. The attributes that need modification of their
 * permissions are created read-only and write permissions are added or removed
 * on the fly when required */
static struct attribute *dme1737_attr[] ={
    /* Voltages */
    &sensor_dev_attr_in0_input.dev_attr.attr,
    &sensor_dev_attr_in0_min.dev_attr.attr,
    &sensor_dev_attr_in0_max.dev_attr.attr,
    &sensor_dev_attr_in0_alarm.dev_attr.attr,
    &sensor_dev_attr_in1_input.dev_attr.attr,
    &sensor_dev_attr_in1_min.dev_attr.attr,
    &sensor_dev_attr_in1_max.dev_attr.attr,
    &sensor_dev_attr_in1_alarm.dev_attr.attr,
    &sensor_dev_attr_in2_input.dev_attr.attr,
    &sensor_dev_attr_in2_min.dev_attr.attr,
    &sensor_dev_attr_in2_max.dev_attr.attr,
    &sensor_dev_attr_in2_alarm.dev_attr.attr,
    &sensor_dev_attr_in3_input.dev_attr.attr,
    &sensor_dev_attr_in3_min.dev_attr.attr,
    &sensor_dev_attr_in3_max.dev_attr.attr,
    &sensor_dev_attr_in3_alarm.dev_attr.attr,
    &sensor_dev_attr_in4_input.dev_attr.attr,
    &sensor_dev_attr_in4_min.dev_attr.attr,
    &sensor_dev_attr_in4_max.dev_attr.attr,
    &sensor_dev_attr_in4_alarm.dev_attr.attr,
    &sensor_dev_attr_in5_input.dev_attr.attr,
    &sensor_dev_attr_in5_min.dev_attr.attr,
    &sensor_dev_attr_in5_max.dev_attr.attr,
    &sensor_dev_attr_in5_alarm.dev_attr.attr,
    &sensor_dev_attr_in6_input.dev_attr.attr,
    &sensor_dev_attr_in6_min.dev_attr.attr,
    &sensor_dev_attr_in6_max.dev_attr.attr,
    &sensor_dev_attr_in6_alarm.dev_attr.attr,
    /* Temperatures */
    &sensor_dev_attr_temp1_input.dev_attr.attr,
    &sensor_dev_attr_temp1_min.dev_attr.attr,
    &sensor_dev_attr_temp1_max.dev_attr.attr,
    &sensor_dev_attr_temp1_alarm.dev_attr.attr,
    &sensor_dev_attr_temp1_fault.dev_attr.attr,
    &sensor_dev_attr_temp2_input.dev_attr.attr,
    &sensor_dev_attr_temp2_min.dev_attr.attr,
    &sensor_dev_attr_temp2_max.dev_attr.attr,
    &sensor_dev_attr_temp2_alarm.dev_attr.attr,
    &sensor_dev_attr_temp2_fault.dev_attr.attr,
    &sensor_dev_attr_temp3_input.dev_attr.attr,
    &sensor_dev_attr_temp3_min.dev_attr.attr,
    &sensor_dev_attr_temp3_max.dev_attr.attr,
    &sensor_dev_attr_temp3_alarm.dev_attr.attr,
    &sensor_dev_attr_temp3_fault.dev_attr.attr,
    /* Zones */
    &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
    NULL
};

static const struct attribute_group dme1737_group = {
    .attrs = dme1737_attr,
};

/* The following struct holds temp offset attributes, which are not available
 * in all chips. The following chips support them:
 * DME1737, SCH311x */
static struct attribute *dme1737_temp_offset_attr[] = {
    &sensor_dev_attr_temp1_offset.dev_attr.attr,
    &sensor_dev_attr_temp2_offset.dev_attr.attr,
    &sensor_dev_attr_temp3_offset.dev_attr.attr,
    NULL
};

static const struct attribute_group dme1737_temp_offset_group = {
    .attrs = dme1737_temp_offset_attr,
};

/* The following struct holds VID related attributes, which are not available
 * in all chips. The following chips support them:
 * DME1737 */
static struct attribute *dme1737_vid_attr[] = {
    &dev_attr_vrm.attr,
    &dev_attr_cpu0_vid.attr,
    NULL
};

static const struct attribute_group dme1737_vid_group = {
    .attrs = dme1737_vid_attr,
};

/* The following struct holds temp zone 3 related attributes, which are not
 * available in all chips. The following chips support them:
 * DME1737, SCH311x, SCH5027 */
static struct attribute *dme1737_zone3_attr[] = {
    &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
    NULL
};

static const struct attribute_group dme1737_zone3_group = {
    .attrs = dme1737_zone3_attr,
};


/* The following struct holds temp zone hysteresis  related attributes, which
 * are not available in all chips. The following chips support them:
 * DME1737, SCH311x */
static struct attribute *dme1737_zone_hyst_attr[] = {
    &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
    NULL
};

static const struct attribute_group dme1737_zone_hyst_group = {
    .attrs = dme1737_zone_hyst_attr,
};

/* The following structs hold the PWM attributes, some of which are optional.
 * Their creation depends on the chip configuration which is determined during
 * module load. */
static struct attribute *dme1737_pwm1_attr[] = {
    &sensor_dev_attr_pwm1.dev_attr.attr,
    &sensor_dev_attr_pwm1_freq.dev_attr.attr,
    &sensor_dev_attr_pwm1_enable.dev_attr.attr,
    &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_pwm2_attr[] = {
    &sensor_dev_attr_pwm2.dev_attr.attr,
    &sensor_dev_attr_pwm2_freq.dev_attr.attr,
    &sensor_dev_attr_pwm2_enable.dev_attr.attr,
    &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_pwm3_attr[] = {
    &sensor_dev_attr_pwm3.dev_attr.attr,
    &sensor_dev_attr_pwm3_freq.dev_attr.attr,
    &sensor_dev_attr_pwm3_enable.dev_attr.attr,
    &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_pwm5_attr[] = {
    &sensor_dev_attr_pwm5.dev_attr.attr,
    &sensor_dev_attr_pwm5_freq.dev_attr.attr,
    &sensor_dev_attr_pwm5_enable.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_pwm6_attr[] = {
    &sensor_dev_attr_pwm6.dev_attr.attr,
    &sensor_dev_attr_pwm6_freq.dev_attr.attr,
    &sensor_dev_attr_pwm6_enable.dev_attr.attr,
    NULL
};

static const struct attribute_group dme1737_pwm_group[] = {
    { .attrs = dme1737_pwm1_attr },
    { .attrs = dme1737_pwm2_attr },
    { .attrs = dme1737_pwm3_attr },
    { .attrs = NULL },
    { .attrs = dme1737_pwm5_attr },
    { .attrs = dme1737_pwm6_attr },
};

/* The following struct holds auto PWM min attributes, which are not available
 * in all chips. Their creation depends on the chip type which is determined
 * during module load. */
static struct attribute *dme1737_auto_pwm_min_attr[] = {
    &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
};

/* The following structs hold the fan attributes, some of which are optional.
 * Their creation depends on the chip configuration which is determined during
 * module load. */
static struct attribute *dme1737_fan1_attr[] = {
    &sensor_dev_attr_fan1_input.dev_attr.attr,
    &sensor_dev_attr_fan1_min.dev_attr.attr,
    &sensor_dev_attr_fan1_alarm.dev_attr.attr,
    &sensor_dev_attr_fan1_type.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_fan2_attr[] = {
    &sensor_dev_attr_fan2_input.dev_attr.attr,
    &sensor_dev_attr_fan2_min.dev_attr.attr,
    &sensor_dev_attr_fan2_alarm.dev_attr.attr,
    &sensor_dev_attr_fan2_type.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_fan3_attr[] = {
    &sensor_dev_attr_fan3_input.dev_attr.attr,
    &sensor_dev_attr_fan3_min.dev_attr.attr,
    &sensor_dev_attr_fan3_alarm.dev_attr.attr,
    &sensor_dev_attr_fan3_type.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_fan4_attr[] = {
    &sensor_dev_attr_fan4_input.dev_attr.attr,
    &sensor_dev_attr_fan4_min.dev_attr.attr,
    &sensor_dev_attr_fan4_alarm.dev_attr.attr,
    &sensor_dev_attr_fan4_type.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_fan5_attr[] = {
    &sensor_dev_attr_fan5_input.dev_attr.attr,
    &sensor_dev_attr_fan5_min.dev_attr.attr,
    &sensor_dev_attr_fan5_alarm.dev_attr.attr,
    &sensor_dev_attr_fan5_max.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_fan6_attr[] = {
    &sensor_dev_attr_fan6_input.dev_attr.attr,
    &sensor_dev_attr_fan6_min.dev_attr.attr,
    &sensor_dev_attr_fan6_alarm.dev_attr.attr,
    &sensor_dev_attr_fan6_max.dev_attr.attr,
    NULL
};

static const struct attribute_group dme1737_fan_group[] = {
    { .attrs = dme1737_fan1_attr },
    { .attrs = dme1737_fan2_attr },
    { .attrs = dme1737_fan3_attr },
    { .attrs = dme1737_fan4_attr },
    { .attrs = dme1737_fan5_attr },
    { .attrs = dme1737_fan6_attr },
};

/* The permissions of the following zone attributes are changed to read-
 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
static struct attribute *dme1737_zone_chmod_attr[] = {
    &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
    NULL
};

static const struct attribute_group dme1737_zone_chmod_group = {
    .attrs = dme1737_zone_chmod_attr,
};


/* The permissions of the following zone 3 attributes are changed to read-
 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
static struct attribute *dme1737_zone3_chmod_attr[] = {
    &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
    &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
    NULL
};

static const struct attribute_group dme1737_zone3_chmod_group = {
    .attrs = dme1737_zone3_chmod_attr,
};

/* The permissions of the following PWM attributes are changed to read-
 * writeable if the chip is *not* locked and the respective PWM is available.
 * Otherwise they stay read-only. */
static struct attribute *dme1737_pwm1_chmod_attr[] = {
    &sensor_dev_attr_pwm1_freq.dev_attr.attr,
    &sensor_dev_attr_pwm1_enable.dev_attr.attr,
    &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
    &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_pwm2_chmod_attr[] = {
    &sensor_dev_attr_pwm2_freq.dev_attr.attr,
    &sensor_dev_attr_pwm2_enable.dev_attr.attr,
    &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
    &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_pwm3_chmod_attr[] = {
    &sensor_dev_attr_pwm3_freq.dev_attr.attr,
    &sensor_dev_attr_pwm3_enable.dev_attr.attr,
    &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
    &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_pwm5_chmod_attr[] = {
    &sensor_dev_attr_pwm5.dev_attr.attr,
    &sensor_dev_attr_pwm5_freq.dev_attr.attr,
    NULL
};
static struct attribute *dme1737_pwm6_chmod_attr[] = {
    &sensor_dev_attr_pwm6.dev_attr.attr,
    &sensor_dev_attr_pwm6_freq.dev_attr.attr,
    NULL
};

static const struct attribute_group dme1737_pwm_chmod_group[] = {
    { .attrs = dme1737_pwm1_chmod_attr },
    { .attrs = dme1737_pwm2_chmod_attr },
    { .attrs = dme1737_pwm3_chmod_attr },
    { .attrs = NULL },
    { .attrs = dme1737_pwm5_chmod_attr },
    { .attrs = dme1737_pwm6_chmod_attr },
};

/* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
 * chip is not locked. Otherwise they are read-only. */
static struct attribute *dme1737_pwm_chmod_attr[] = {
    &sensor_dev_attr_pwm1.dev_attr.attr,
    &sensor_dev_attr_pwm2.dev_attr.attr,
    &sensor_dev_attr_pwm3.dev_attr.attr,
};

/* ---------------------------------------------------------------------
 * Super-IO functions
 * --------------------------------------------------------------------- */

static inline void dme1737_sio_enter(int sio_cip)
{
    outb(0x55, sio_cip);
}

static inline void dme1737_sio_exit(int sio_cip)
{
    outb(0xaa, sio_cip);
}

static inline int dme1737_sio_inb(int sio_cip, int reg)
{
    outb(reg, sio_cip);
    return inb(sio_cip + 1);
}

static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
{
    outb(reg, sio_cip);
    outb(val, sio_cip + 1);
}

/* ---------------------------------------------------------------------
 * Device initialization
 * --------------------------------------------------------------------- */

static int dme1737_i2c_get_features(int, struct dme1737_data*);

static void dme1737_chmod_file(struct device *dev,
                   struct attribute *attr, mode_t mode)
{
    if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
        dev_warn(dev, "Failed to change permissions of %s.\n",
             attr->name);
    }
}

static void dme1737_chmod_group(struct device *dev,
                const struct attribute_group *group,
                mode_t mode)
{
    struct attribute **attr;

    for (attr = group->attrs; *attr; attr++) {
        dme1737_chmod_file(dev, *attr, mode);
    }
}

static void dme1737_remove_files(struct device *dev)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    int ix;

    for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
        if (data->has_features & HAS_FAN(ix)) {
            sysfs_remove_group(&dev->kobj,
                       &dme1737_fan_group[ix]);
        }
    }

    for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
        if (data->has_features & HAS_PWM(ix)) {
            sysfs_remove_group(&dev->kobj,
                       &dme1737_pwm_group[ix]);
            if ((data->has_features & HAS_PWM_MIN) && ix < 3) {
                sysfs_remove_file(&dev->kobj,
                        dme1737_auto_pwm_min_attr[ix]);
            }
        }
    }

    if (data->has_features & HAS_TEMP_OFFSET) {
        sysfs_remove_group(&dev->kobj, &dme1737_temp_offset_group);
    }
    if (data->has_features & HAS_VID) {
        sysfs_remove_group(&dev->kobj, &dme1737_vid_group);
    }
    if (data->has_features & HAS_ZONE3) {
        sysfs_remove_group(&dev->kobj, &dme1737_zone3_group);
    }
    if (data->has_features & HAS_ZONE_HYST) {
        sysfs_remove_group(&dev->kobj, &dme1737_zone_hyst_group);
    }
    sysfs_remove_group(&dev->kobj, &dme1737_group);

    if (!data->client) {
        sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
    }
}

static int dme1737_create_files(struct device *dev)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    int err, ix;

    /* Create a name attribute for ISA devices */
    if (!data->client &&
        (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) {
        goto exit;
    }

    /* Create standard sysfs attributes */
    if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
        goto exit_remove;
    }

    /* Create chip-dependent sysfs attributes */
    if ((data->has_features & HAS_TEMP_OFFSET) &&
        (err = sysfs_create_group(&dev->kobj,
                      &dme1737_temp_offset_group))) {
        goto exit_remove;
    }
    if ((data->has_features & HAS_VID) &&
        (err = sysfs_create_group(&dev->kobj,
                      &dme1737_vid_group))) {
        goto exit_remove;
    }
    if ((data->has_features & HAS_ZONE3) &&
        (err = sysfs_create_group(&dev->kobj,
                      &dme1737_zone3_group))) {
        goto exit_remove;
    }
    if ((data->has_features & HAS_ZONE_HYST) &&
        (err = sysfs_create_group(&dev->kobj,
                      &dme1737_zone_hyst_group))) {
        goto exit_remove;
    }

    /* Create fan sysfs attributes */
    for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
        if (data->has_features & HAS_FAN(ix)) {
            if ((err = sysfs_create_group(&dev->kobj,
                        &dme1737_fan_group[ix]))) {
                goto exit_remove;
            }
        }
    }

    /* Create PWM sysfs attributes */
    for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
        if (data->has_features & HAS_PWM(ix)) {
            if ((err = sysfs_create_group(&dev->kobj,
                        &dme1737_pwm_group[ix]))) {
                goto exit_remove;
            }
            if ((data->has_features & HAS_PWM_MIN) && ix < 3 &&
                (err = sysfs_create_file(&dev->kobj,
                    dme1737_auto_pwm_min_attr[ix]))) {
                goto exit_remove;
            }
        }
    }

    /* Inform if the device is locked. Otherwise change the permissions of
     * selected attributes from read-only to read-writeable. */
    if (data->config & 0x02) {
        dev_info(dev, "Device is locked. Some attributes "
             "will be read-only.\n");
    } else {
        /* Change permissions of zone sysfs attributes */
        dme1737_chmod_group(dev, &dme1737_zone_chmod_group,
                    S_IRUGO | S_IWUSR);

        /* Change permissions of chip-dependent sysfs attributes */
        if (data->has_features & HAS_TEMP_OFFSET) {
            dme1737_chmod_group(dev, &dme1737_temp_offset_group,
                        S_IRUGO | S_IWUSR);
        }
        if (data->has_features & HAS_ZONE3) {
            dme1737_chmod_group(dev, &dme1737_zone3_chmod_group,
                        S_IRUGO | S_IWUSR);
        }
        if (data->has_features & HAS_ZONE_HYST) {
            dme1737_chmod_group(dev, &dme1737_zone_hyst_group,
                        S_IRUGO | S_IWUSR);
        }

        /* Change permissions of PWM sysfs attributes */
        for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_chmod_group); ix++) {
            if (data->has_features & HAS_PWM(ix)) {
                dme1737_chmod_group(dev,
                        &dme1737_pwm_chmod_group[ix],
                        S_IRUGO | S_IWUSR);
                if ((data->has_features & HAS_PWM_MIN) &&
                    ix < 3) {
                    dme1737_chmod_file(dev,
                        dme1737_auto_pwm_min_attr[ix],
                        S_IRUGO | S_IWUSR);
                }
            }
        }

        /* Change permissions of pwm[1-3] if in manual mode */
        for (ix = 0; ix < 3; ix++) {
            if ((data->has_features & HAS_PWM(ix)) &&
                (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
                dme1737_chmod_file(dev,
                        dme1737_pwm_chmod_attr[ix],
                        S_IRUGO | S_IWUSR);
            }
        }
    }

    return 0;

exit_remove:
    dme1737_remove_files(dev);
exit:
    return err;
}

static int dme1737_init_device(struct device *dev)
{
    struct dme1737_data *data = dev_get_drvdata(dev);
    struct i2c_client *client = data->client;
    int ix;
    u8 reg;

    /* Point to the right nominal voltages array */
    data->in_nominal = IN_NOMINAL(data->type);

    data->config = dme1737_read(data, DME1737_REG_CONFIG);
    /* Inform if part is not monitoring/started */
    if (!(data->config & 0x01)) {
        if (!force_start) {
            dev_err(dev, "Device is not monitoring. "
                "Use the force_start load parameter to "
                "override.\n");
            return -EFAULT;
        }

        /* Force monitoring */
        data->config |= 0x01;
        dme1737_write(data, DME1737_REG_CONFIG, data->config);
    }
    /* Inform if part is not ready */
    if (!(data->config & 0x04)) {
        dev_err(dev, "Device is not ready.\n");
        return -EFAULT;
    }

    /* Determine which optional fan and pwm features are enabled (only
     * valid for I2C devices) */
    if (client) {   /* I2C chip */
        data->config2 = dme1737_read(data, DME1737_REG_CONFIG2);
        /* Check if optional fan3 input is enabled */
        if (data->config2 & 0x04) {
            data->has_features |= HAS_FAN(2);
        }

        /* Fan4 and pwm3 are only available if the client's I2C address
         * is the default 0x2e. Otherwise the I/Os associated with
         * these functions are used for addr enable/select. */
        if (client->addr == 0x2e) {
            data->has_features |= HAS_FAN(3) | HAS_PWM(2);
        }

        /* Determine which of the optional fan[5-6] and pwm[5-6]
         * features are enabled. For this, we need to query the runtime
         * registers through the Super-IO LPC interface. Try both
         * config ports 0x2e and 0x4e. */
        if (dme1737_i2c_get_features(0x2e, data) &&
            dme1737_i2c_get_features(0x4e, data)) {
            dev_warn(dev, "Failed to query Super-IO for optional "
                 "features.\n");
        }
    }

    /* Fan[1-2] and pwm[1-2] are present in all chips */
    data->has_features |= HAS_FAN(0) | HAS_FAN(1) | HAS_PWM(0) | HAS_PWM(1);

    /* Chip-dependent features */
    switch (data->type) {
    case dme1737:
        data->has_features |= HAS_TEMP_OFFSET | HAS_VID | HAS_ZONE3 |
            HAS_ZONE_HYST | HAS_PWM_MIN;
        break;
    case sch311x:
        data->has_features |= HAS_TEMP_OFFSET | HAS_ZONE3 |
            HAS_ZONE_HYST | HAS_PWM_MIN | HAS_FAN(2) | HAS_PWM(2);
        break;
    case sch5027:
        data->has_features |= HAS_ZONE3;
        break;
    case sch5127:
        data->has_features |= HAS_FAN(2) | HAS_PWM(2);
        break;
    default:
        break;
    }

    dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
         "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
         (data->has_features & HAS_PWM(2)) ? "yes" : "no",
         (data->has_features & HAS_PWM(4)) ? "yes" : "no",
         (data->has_features & HAS_PWM(5)) ? "yes" : "no",
         (data->has_features & HAS_FAN(2)) ? "yes" : "no",
         (data->has_features & HAS_FAN(3)) ? "yes" : "no",
         (data->has_features & HAS_FAN(4)) ? "yes" : "no",
         (data->has_features & HAS_FAN(5)) ? "yes" : "no");

    reg = dme1737_read(data, DME1737_REG_TACH_PWM);
    /* Inform if fan-to-pwm mapping differs from the default */
    if (client && reg != 0xa4) {   /* I2C chip */
        dev_warn(dev, "Non-standard fan to pwm mapping: "
             "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
             "fan4->pwm%d. Please report to the driver "
             "maintainer.\n",
             (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
             ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
    } else if (!client && reg != 0x24) {   /* ISA chip */
        dev_warn(dev, "Non-standard fan to pwm mapping: "
             "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
             "Please report to the driver maintainer.\n",
             (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
             ((reg >> 4) & 0x03) + 1);
    }

    /* Switch pwm[1-3] to manual mode if they are currently disabled and
     * set the duty-cycles to 0% (which is identical to the PWMs being
     * disabled). */
    if (!(data->config & 0x02)) {
        for (ix = 0; ix < 3; ix++) {
            data->pwm_config[ix] = dme1737_read(data,
                        DME1737_REG_PWM_CONFIG(ix));
            if ((data->has_features & HAS_PWM(ix)) &&
                (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
                dev_info(dev, "Switching pwm%d to "
                     "manual mode.\n", ix + 1);
                data->pwm_config[ix] = PWM_EN_TO_REG(1,
                            data->pwm_config[ix]);
                dme1737_write(data, DME1737_REG_PWM(ix), 0);
                dme1737_write(data,
                          DME1737_REG_PWM_CONFIG(ix),
                          data->pwm_config[ix]);
            }
        }
    }

    /* Initialize the default PWM auto channels zone (acz) assignments */
    data->pwm_acz[0] = 1;    /* pwm1 -> zone1 */
    data->pwm_acz[1] = 2;    /* pwm2 -> zone2 */
    data->pwm_acz[2] = 4;    /* pwm3 -> zone3 */

    /* Set VRM */
    if (data->has_features & HAS_VID) {
        data->vrm = vid_which_vrm();
    }

    return 0;
}

/* ---------------------------------------------------------------------
 * I2C device detection and registration
 * --------------------------------------------------------------------- */

static struct i2c_driver dme1737_i2c_driver;

static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
{
    int err = 0, reg;
    u16 addr;

    dme1737_sio_enter(sio_cip);

    /* Check device ID
     * We currently know about two kinds of DME1737 and SCH5027. */
    reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
    if (!(reg == DME1737_ID_1 || reg == DME1737_ID_2 ||
          reg == SCH5027_ID)) {
        err = -ENODEV;
        goto exit;
    }

    /* Select logical device A (runtime registers) */
    dme1737_sio_outb(sio_cip, 0x07, 0x0a);

    /* Get the base address of the runtime registers */
    if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
              dme1737_sio_inb(sio_cip, 0x61))) {
        err = -ENODEV;
        goto exit;
    }

    /* Read the runtime registers to determine which optional features
     * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
     * to '10' if the respective feature is enabled. */
    if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
        data->has_features |= HAS_FAN(5);
    }
    if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
        data->has_features |= HAS_PWM(5);
    }
    if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
        data->has_features |= HAS_FAN(4);
    }
    if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
        data->has_features |= HAS_PWM(4);
    }

exit:
    dme1737_sio_exit(sio_cip);

    return err;
}

/* Return 0 if detection is successful, -ENODEV otherwise */
static int dme1737_i2c_detect(struct i2c_client *client,
                  struct i2c_board_info *info)
{
    struct i2c_adapter *adapter = client->adapter;
    struct device *dev = &adapter->dev;
    u8 company, verstep = 0;
    const char *name;

    if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
        return -ENODEV;
    }

    company = i2c_smbus_read_byte_data(client, DME1737_REG_COMPANY);
    verstep = i2c_smbus_read_byte_data(client, DME1737_REG_VERSTEP);

    if (company == DME1737_COMPANY_SMSC &&
        verstep == SCH5027_VERSTEP) {
        name = "sch5027";
    } else if (company == DME1737_COMPANY_SMSC &&
           (verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP) {
        name = "dme1737";
    } else {
        return -ENODEV;
    }

    dev_info(dev, "Found a %s chip at 0x%02x (rev 0x%02x).\n",
         verstep == SCH5027_VERSTEP ? "SCH5027" : "DME1737",
         client->addr, verstep);
    strlcpy(info->type, name, I2C_NAME_SIZE);

    return 0;
}

static int dme1737_i2c_probe(struct i2c_client *client,
                 const struct i2c_device_id *id)
{
    struct dme1737_data *data;
    struct device *dev = &client->dev;
    int err;

    data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL);
    if (!data) {
        err = -ENOMEM;
        goto exit;
    }

    i2c_set_clientdata(client, data);
    data->type = id->driver_data;
    data->client = client;
    data->name = client->name;
    mutex_init(&data->update_lock);

    /* Initialize the DME1737 chip */
    if ((err = dme1737_init_device(dev))) {
        dev_err(dev, "Failed to initialize device.\n");
        goto exit_kfree;
    }

    /* Create sysfs files */
    if ((err = dme1737_create_files(dev))) {
        dev_err(dev, "Failed to create sysfs files.\n");
        goto exit_kfree;
    }

    /* Register device */
    data->hwmon_dev = hwmon_device_register(dev);
    if (IS_ERR(data->hwmon_dev)) {
        dev_err(dev, "Failed to register device.\n");
        err = PTR_ERR(data->hwmon_dev);
        goto exit_remove;
    }

    return 0;

exit_remove:
    dme1737_remove_files(dev);
exit_kfree:
    kfree(data);
exit:
    return err;
}

static int dme1737_i2c_remove(struct i2c_client *client)
{
    struct dme1737_data *data = i2c_get_clientdata(client);

    hwmon_device_unregister(data->hwmon_dev);
    dme1737_remove_files(&client->dev);

    kfree(data);
    return 0;
}

static const struct i2c_device_id dme1737_id[] = {
    { "dme1737", dme1737 },
    { "sch5027", sch5027 },
    { }
};
MODULE_DEVICE_TABLE(i2c, dme1737_id);

static struct i2c_driver dme1737_i2c_driver = {
    .class = I2C_CLASS_HWMON,
    .driver = {
        .name = "dme1737",
    },
    .probe = dme1737_i2c_probe,
    .remove = dme1737_i2c_remove,
    .id_table = dme1737_id,
    .detect = dme1737_i2c_detect,
    .address_list = normal_i2c,
};

/* ---------------------------------------------------------------------
 * ISA device detection and registration
 * --------------------------------------------------------------------- */

static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
{
    int err = 0, reg;
    unsigned short base_addr;

    dme1737_sio_enter(sio_cip);

    /* Check device ID
     * We currently know about SCH3112, SCH3114, SCH3116, and SCH5127 */
    reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
    if (!(reg == SCH3112_ID || reg == SCH3114_ID || reg == SCH3116_ID ||
          reg == SCH5127_ID)) {
        err = -ENODEV;
        goto exit;
    }

    /* Select logical device A (runtime registers) */
    dme1737_sio_outb(sio_cip, 0x07, 0x0a);

    /* Get the base address of the runtime registers */
    if (!(base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
               dme1737_sio_inb(sio_cip, 0x61))) {
        printk(KERN_ERR "dme1737: Base address not set.\n");
        err = -ENODEV;
        goto exit;
    }

    /* Access to the hwmon registers is through an index/data register
     * pair located at offset 0x70/0x71. */
    *addr = base_addr + 0x70;

exit:
    dme1737_sio_exit(sio_cip);
    return err;
}

static int __init dme1737_isa_device_add(unsigned short addr)
{
    struct resource res = {
        .start    = addr,
        .end    = addr + DME1737_EXTENT - 1,
        .name    = "dme1737",
        .flags    = IORESOURCE_IO,
    };
    int err;

    err = acpi_check_resource_conflict(&res);
    if (err)
        goto exit;

    if (!(pdev = platform_device_alloc("dme1737", addr))) {
        printk(KERN_ERR "dme1737: Failed to allocate device.\n");
        err = -ENOMEM;
        goto exit;
    }

    if ((err = platform_device_add_resources(pdev, &res, 1))) {
        printk(KERN_ERR "dme1737: Failed to add device resource "
               "(err = %d).\n", err);
        goto exit_device_put;
    }

    if ((err = platform_device_add(pdev))) {
        printk(KERN_ERR "dme1737: Failed to add device (err = %d).\n",
               err);
        goto exit_device_put;
    }

    return 0;

exit_device_put:
    platform_device_put(pdev);
    pdev = NULL;
exit:
    return err;
}

static int __devinit dme1737_isa_probe(struct platform_device *pdev)
{
    u8 company, device;
    struct resource *res;
    struct dme1737_data *data;
    struct device *dev = &pdev->dev;
    int err;

    res = platform_get_resource(pdev, IORESOURCE_IO, 0);
    if (!request_region(res->start, DME1737_EXTENT, "dme1737")) {
        dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
            (unsigned short)res->start,
            (unsigned short)res->start + DME1737_EXTENT - 1);
                err = -EBUSY;
                goto exit;
        }

    if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
        err = -ENOMEM;
        goto exit_release_region;
    }

    data->addr = res->start;
    platform_set_drvdata(pdev, data);

    /* Skip chip detection if module is loaded with force_id parameter */
    switch (force_id) {
    case SCH3112_ID:
    case SCH3114_ID:
    case SCH3116_ID:
        data->type = sch311x;
        break;
    case SCH5127_ID:
        data->type = sch5127;
        break;
    default:
        company = dme1737_read(data, DME1737_REG_COMPANY);
        device = dme1737_read(data, DME1737_REG_DEVICE);

        if ((company == DME1737_COMPANY_SMSC) &&
            (device == SCH311X_DEVICE)) {
            data->type = sch311x;
        } else if ((company == DME1737_COMPANY_SMSC) &&
               (device == SCH5127_DEVICE)) {
            data->type = sch5127;
        } else {
            err = -ENODEV;
            goto exit_kfree;
        }
    }

    if (data->type == sch5127) {
        data->name = "sch5127";
    } else {
        data->name = "sch311x";
    }

    /* Initialize the mutex */
    mutex_init(&data->update_lock);

    dev_info(dev, "Found a %s chip at 0x%04x\n",
         data->type == sch5127 ? "SCH5127" : "SCH311x", data->addr);

    /* Initialize the chip */
    if ((err = dme1737_init_device(dev))) {
        dev_err(dev, "Failed to initialize device.\n");
        goto exit_kfree;
    }

    /* Create sysfs files */
    if ((err = dme1737_create_files(dev))) {
        dev_err(dev, "Failed to create sysfs files.\n");
        goto exit_kfree;
    }

    /* Register device */
    data->hwmon_dev = hwmon_device_register(dev);
    if (IS_ERR(data->hwmon_dev)) {
        dev_err(dev, "Failed to register device.\n");
        err = PTR_ERR(data->hwmon_dev);
        goto exit_remove_files;
    }

    return 0;

exit_remove_files:
    dme1737_remove_files(dev);
exit_kfree:
    platform_set_drvdata(pdev, NULL);
    kfree(data);
exit_release_region:
    release_region(res->start, DME1737_EXTENT);
exit:
    return err;
}

static int __devexit dme1737_isa_remove(struct platform_device *pdev)
{
    struct dme1737_data *data = platform_get_drvdata(pdev);

    hwmon_device_unregister(data->hwmon_dev);
    dme1737_remove_files(&pdev->dev);
    release_region(data->addr, DME1737_EXTENT);
    platform_set_drvdata(pdev, NULL);
    kfree(data);

    return 0;
}

static struct platform_driver dme1737_isa_driver = {
    .driver = {
        .owner = THIS_MODULE,
        .name = "dme1737",
    },
    .probe = dme1737_isa_probe,
    .remove = __devexit_p(dme1737_isa_remove),
};

/* ---------------------------------------------------------------------
 * Module initialization and cleanup
 * --------------------------------------------------------------------- */

static int __init dme1737_init(void)
{
    int err;
    unsigned short addr;

    if ((err = i2c_add_driver(&dme1737_i2c_driver))) {
        goto exit;
    }

    if (dme1737_isa_detect(0x2e, &addr) &&
        dme1737_isa_detect(0x4e, &addr) &&
        (!probe_all_addr ||
         (dme1737_isa_detect(0x162e, &addr) &&
          dme1737_isa_detect(0x164e, &addr)))) {
        /* Return 0 if we didn't find an ISA device */
        return 0;
    }

    if ((err = platform_driver_register(&dme1737_isa_driver))) {
        goto exit_del_i2c_driver;
    }

    /* Sets global pdev as a side effect */
    if ((err = dme1737_isa_device_add(addr))) {
        goto exit_del_isa_driver;
    }

    return 0;

exit_del_isa_driver:
    platform_driver_unregister(&dme1737_isa_driver);
exit_del_i2c_driver:
    i2c_del_driver(&dme1737_i2c_driver);
exit:
    return err;
}

static void __exit dme1737_exit(void)
{
    if (pdev) {
        platform_device_unregister(pdev);
        platform_driver_unregister(&dme1737_isa_driver);
    }

    i2c_del_driver(&dme1737_i2c_driver);
}

MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
MODULE_DESCRIPTION("DME1737 sensors");
MODULE_LICENSE("GPL");

module_init(dme1737_init);
module_exit(dme1737_exit);
 

 

  • Like 1

ubuntu@ubuntu:~$ sudo -s
root@ubuntu:~#  superiotool -d
superiotool r6637
Found SMSC FDC37N972 (id=0x0b, rev=0x00) at 0x2e
Register dump:
idx 02 20 21 22 23 24 25 26  27 28 29 2a 2b 2c 2d 2e  2f
val 00 0b 00 00 00 00 00 2e  00 00 00 00 00 00 00 00  00
def 00 0b 00 00 00 04 04 NA  NA 00 00 00 00 00 00 00  00
LDN 0x00 (Floppy)
idx 30 60 61 70 74 f0 f1 f2  f3 f4 f5
val 00 00 00 00 00 00 d1 15  0b 00 10
def 00 03 f0 06 02 0e 00 ff  RR 00 00
LDN 0x01 (Power management (PM1))
idx 30 60 61
val 00 00 00
def 00 00 00
LDN 0x03 (Parallel port)
idx 30 60 61 70 74 f0 f1
val 00 00 00 00 00 00 d1
def 00 00 00 00 04 3c 00
LDN 0x04 (COM1)
idx 30 60 61 70 f0
val 00 00 00 00 00
def 00 00 00 00 00
LDN 0x05 (COM2)
idx 30 60 61 62 63 70 74 f0  f1 f2 f7 f8
val 00 00 00 00 00 00 00 00  d1 15 00 00
def 00 00 00 00 00 00 04 00  02 03 00 00
LDN 0x06 (Real-time clock (RTC))
idx 30 60 61 62 63 70 f0 f1
val 00 00 00 00 00 00 00 d1
def 00 00 70 00 74 00 00 NA
LDN 0x07 (Keyboard)
idx 30 60 61 70 72 f0
val 00 00 00 00 00 00
def 00 00 00 00 00 00
LDN 0x08 (Embedded controller (EC))
idx 30 60 61
val 00 00 00
def 00 00 62
LDN 0x09 (Mailbox)
idx 30 60 61
val 00 00 00
def 00 00 00
Found SMSC SCH5027 (id=0x89, rev=0x03) at 0x4e
No dump available for this Super I/O

 

i was some kind of lucky to dump some information but i am still not able  to dump the sensor chip

Ubuntu Dumps.zip

I think the SMSC chip is analogous to NSC chip supported by plugin  https://sourceforge.net/p/hwsensors/hwsensors3/code3/HEAD/tree/trunk/plugins/SuperIOSensors/NSCPC8739x/

No direct access to monitoring but we can find registers shadow to show them.

Anyway, continue the research!

  • Like 1

Hi

Slice thank you for the comments on this topic and  helping us i am really interested in

so things which i can do by my  self I am doing this and trying to provide more information

But this registers shadow thing is something new for me  i would greatly  appreciate if you could help me on this matter how to do

 

i am allso inviting all user they are interested to in this project please come across and help together

Edited by Rockey12

Mainboard Model        D2618-A1 (0x000001B4 - 0x00980C24)

LPCIO
-------------------------------------------------------------------------

LPCIO Vendor        SMSC
LPCIO Model        SCH5027
LPCIO Vendor ID        0x55
LPCIO Chip ID        0x89
Config Mode I/O address    0x4E
Config Mode LDN        0x8
Config Mode registers    
            00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
    00    00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 00
    10    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    20    89 03 00 00 44 00 4E 00 00 00 00 00 00 00 00 00
    30    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    40    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    50    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    60    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    70    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

 

 

Hi Slice sorry if i disappointing you i was just able to get these information

Because the RWEverything on this specific function MMIO doesn't show any kind of data

 


#    Sensors configuration file used by 'libsensors' for Tyan S5197
#
#        To support Intel ICH7 SMBus controller, lm-sensors 2.9.0 is at least
#        If using Linux Kernel 2.6,  version 2.6.11 is needed
#
#        Otherwise, user need edit the driver code and recompile it according to follow instruction
#
#        1. If Linux Kernel equal or above 2.6.11, go to step 4.
#
#        2. Add ICH7 Device ID into driver code.
#           Edit file "driver/i2c/busses/i2c-i801.c",
#
#             Look for structure "i801_ids", and add new member with
#                "{ PCI_VENDOR_ID_INTEL, 0x27DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },"
#                or "{
#                            .vendor    =    PCI_VENDOR_ID_INTEL,
#                            .device    = 0x27DA,
#                            .subvendor    =    PCI_ANY_ID,
#                            .subdevice    =    PCI_ANY_ID,
#                            .driver_data    =    0,
#                        }", depend on Linux Kernel.
#
#        3. recompile the i2c-i801 module(make modules and make modules_install)
#
#        4. To your /etc/modules.conf file, add the lines:
#           alias char-major-89 i2c-dev
#            (most new Linux distribution already do this step)
#
#        5. To your /etc/rc.xxx files, add the lines:
#            (in some Linux distribution, please use file /etc/modprobe.conf.local)
#               modprobe i2c-i801
#             modprobe lm85 force_adm1027=0,0x2e
#                 sensors -s
#
#  Then copy this file to /etc/sensors.conf
#
# As LM-Sensors not support the SMSC SCH5027 Chip, I use other similar chip
# to instead of it.
#
#######

chip "adm1027-i2c-*-2e"

    ignore pwm1
    ignore pwm2
    ignore pwm3
    ignore temp2
    ignore vid

    label in0 "System 5V"
    label in1 "CPU Core Volt"
    label in2 "System 3.3V"
    label in3 "Dimm Volt 1.8V"
    label in4 "System 12V"

    label fan1 "Chassis Fan 1"
    label fan2 "Chassis Fan 3"
    label fan3 "Chassis Fan 5"
    label fan4 "Chassis Fan 4"
    
    label temp1 "CPU Temp"
    label temp3 "System Temp"
    
    compute in0 2.15*@ , @/2.15
    
    set in0_min  5 * 0.95
    set in0_max  5 * 1.05
    set in1_min  0.8
    set in1_max  1.6
    set in2_min  3.3 * 0.95
    set in2_max  3.3 * 1.05
    set in3_min  1.8 * 0.95
    set in3_max  1.8 * 1.05
    set in4_min  12 * 0.95
    set in4_max  12 * 1.05

 

Edited by Rockey12
  • 11 months later...

i am again in the hope that you Dev guy can do something more then i can

i got a UEFI Rom which is easily readable  and hold all the information what we need 

Specifically i am talking about sensor chip or Super IO what ever you want to call 

These are some of the Chips they are working just the same way

SMSC DME1737 - SMSC SCH5027D-NW -SMSC SCH5027E-NW - SMSC SCH5017-NW - Asus A8000  

  • my request is if we can use the data to add this chip SMSC SCH5027E-NW Screenshot 2019-04-17 at 05.10.38.png

Screenshot 2019-04-17 at 05.11.29.png

Screenshot 2019-04-17 at 05.11.57.png

Screenshot 2019-04-17 at 05.12.10.png

Screenshot 2019-04-17 at 05.12.23.png

Screenshot 2019-04-17 at 05.12.30.png

Screenshot 2019-04-17 at 05.14.09.png

Edited by Rockey12
×
×
  • Create New...