moel@1: /*
moel@1:   
moel@1:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1: 
moel@1:   The contents of this file are subject to the Mozilla Public License Version
moel@1:   1.1 (the "License"); you may not use this file except in compliance with
moel@1:   the License. You may obtain a copy of the License at
moel@1:  
moel@1:   http://www.mozilla.org/MPL/
moel@1: 
moel@1:   Software distributed under the License is distributed on an "AS IS" basis,
moel@1:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1:   for the specific language governing rights and limitations under the License.
moel@1: 
moel@1:   The Original Code is the Open Hardware Monitor code.
moel@1: 
moel@1:   The Initial Developer of the Original Code is 
moel@1:   Michael Möller <m.moeller@gmx.ch>.
moel@1:   Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1:   the Initial Developer. All Rights Reserved.
moel@1: 
moel@1:   Contributor(s):
moel@1: 
moel@1:   Alternatively, the contents of this file may be used under the terms of
moel@1:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@1:   of those above. If you wish to allow use of your version of this file only
moel@1:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@1:   use your version of this file under the terms of the MPL, indicate your
moel@1:   decision by deleting the provisions above and replace them with the notice
moel@1:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@1:   the provisions above, a recipient may use your version of this file under
moel@1:   the terms of any one of the MPL, the GPL or the LGPL.
moel@1:  
moel@1: */
moel@1: 
moel@1: using System;
moel@1: using System.Collections.Generic;
moel@1: using System.Drawing;
moel@1: using System.Text;
moel@1: 
moel@1: namespace OpenHardwareMonitor.Hardware.LPC {
moel@1:   public class IT87 : IHardware {
moel@1: 
moel@1:     private string name;
moel@1:     private Image icon;
moel@1:         
moel@1:     private bool available = false;
moel@1:     private Chip chip;
moel@1:     private ushort address;
moel@1: 
moel@1:     private Sensor[] temperatures;
moel@1:     private Sensor[] fans;
moel@1:     private Sensor[] voltages;
moel@1:     private List<ISensor> active = new List<ISensor>();
moel@1:     private float[] voltageGains;
moel@1:    
moel@1:     // Consts
moel@1:     private const byte ITE_VENDOR_ID = 0x90;
moel@1:        
moel@1:     // Environment Controller
moel@1:     private const byte ADDRESS_REGISTER_OFFSET = 0x05;
moel@1:     private const byte DATA_REGISTER_OFFSET = 0x06;
moel@1: 
moel@1:     // Environment Controller Registers    
moel@1:     private const byte CONFIGURATION_REGISTER = 0x00;
moel@1:     private const byte TEMPERATURE_BASE_REG = 0x29;
moel@1:     private const byte VENDOR_ID_REGISTER = 0x58;
moel@1:     private const byte FAN_TACHOMETER_16_BIT_ENABLE_REGISTER = 0x0c;
moel@1:     private byte[] FAN_TACHOMETER_REG = 
moel@1:       new byte[] { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
moel@1:     private byte[] FAN_TACHOMETER_EXT_REG =
moel@1:       new byte[] { 0x18, 0x19, 0x1a, 0x81, 0x83 };
moel@1:     private const byte VOLTAGE_BASE_REG = 0x20;  
moel@1:     
moel@1:     private byte ReadByte(byte register) {
moel@1:       WinRing0.WriteIoPortByte(
moel@1:         (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
moel@1:       return WinRing0.ReadIoPortByte((ushort)(address + DATA_REGISTER_OFFSET));
moel@1:     }
moel@1: 
moel@1:     public IT87(Chip chip, ushort address) {
moel@1:       
moel@1:       this.chip = chip;
moel@1:       this.address = address;
moel@1: 
moel@1:       switch (chip) {
moel@1:         case Chip.IT8716F: name = "ITE IT8716F"; break;
moel@1:         case Chip.IT8718F: name = "ITE IT8718F"; break;
moel@1:         case Chip.IT8720F: name = "ITE IT8720F"; break;
moel@1:         case Chip.IT8726F: name = "ITE IT8726F"; break;
moel@1:         default: return;
moel@1:       }
moel@1:       
moel@1:       // Check vendor id
moel@1:       byte vendorId = ReadByte(VENDOR_ID_REGISTER);      
moel@1:       if (vendorId != ITE_VENDOR_ID)
moel@1:         return;
moel@1: 
moel@1:       // Bit 0x10 of the configuration register should always be 1
moel@1:       if ((ReadByte(CONFIGURATION_REGISTER) & 0x10) == 0)
moel@1:         return;
moel@1: 
moel@1:       temperatures = new Sensor[3];
moel@1:       for (int i = 0; i < temperatures.Length; i++) 
moel@1:         temperatures[i] = new Sensor("Temperature #" + (i + 1), i,
moel@1:           SensorType.Temperature, this);
moel@1: 
moel@6:       fans = new Sensor[5];
moel@1:       for (int i = 0; i < fans.Length; i++)
moel@1:         fans[i] = new Sensor("Fan #" + (i + 1), i, SensorType.Fan, this);
moel@1: 
moel@1:       voltageGains = new float[] { 
moel@1:         1, 1, 1, (6.8f / 10 + 1), 1, 1, 1, 1, 1 };
moel@1: 
moel@1:       voltages = new Sensor[2];
moel@1:       voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
moel@1:       voltages[1] = new Sensor("Battery", 8, SensorType.Voltage, this);           
moel@1: 
moel@1:       available = true;
moel@1:       icon = Utilities.EmbeddedResources.GetImage("chip.png");
moel@1:     }
moel@1: 
moel@1:     public bool IsAvailable {
moel@1:       get { return available; } 
moel@1:     }
moel@1: 
moel@1:     public string Name {
moel@1:       get { return name; }
moel@1:     }
moel@1: 
moel@1:     public string Identifier {
moel@1:       get { return "/lpc/it87"; }
moel@1:     }
moel@1: 
moel@1:     public Image Icon {
moel@1:       get { return icon; }
moel@1:     }
moel@1: 
moel@1:     public ISensor[] Sensors {
moel@1:       get { return active.ToArray(); }
moel@1:     }
moel@1: 
moel@1:     public string GetReport() {
moel@1:       StringBuilder r = new StringBuilder();
moel@1: 
moel@1:       r.AppendLine("LPC IT87");
moel@1:       r.AppendLine();
moel@1:       r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
moel@1:       r.Append("Chip Name: "); r.AppendLine(name);
moel@7:       r.Append("Base Address: 0x"); r.AppendLine(address.ToString("X4"));
moel@1:       r.AppendLine();
moel@1:       r.AppendLine("Environment Controller Registers");
moel@1:       r.AppendLine();
moel@1: 
moel@1:       r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@1:       r.AppendLine();
moel@1:       for (int i = 0; i <= 0xA; i++) {
moel@1:         r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
moel@1:         for (int j = 0; j <= 0xF; j++) {
moel@1:           r.Append(" ");
moel@1:           r.Append(ReadByte((byte)((i << 4) | j)).ToString("X2"));
moel@1:         }
moel@1:         r.AppendLine();
moel@1:       }
moel@1:       r.AppendLine();
moel@1: 
moel@1:       return r.ToString();
moel@1:     }
moel@1: 
moel@1:     public void Update() {
moel@1: 
moel@1:       foreach (Sensor sensor in voltages) {
moel@1:         int value = ReadByte((byte)(VOLTAGE_BASE_REG + sensor.Index));
moel@1:         sensor.Value = voltageGains[sensor.Index] * 0.001f * (value << 4);
moel@1:         if (sensor.Value > 0)
moel@1:           ActivateSensor(sensor);
moel@1:         else
moel@1:           DeactivateSensor(sensor);
moel@1:       }
moel@1: 
moel@1:       foreach (Sensor sensor in temperatures) {
moel@1:         int value = ReadByte((byte)(TEMPERATURE_BASE_REG + sensor.Index));
moel@1:         sensor.Value = value;
moel@1:         if (value < 254)
moel@1:           ActivateSensor(sensor);
moel@1:         else
moel@1:           DeactivateSensor(sensor);
moel@1:       }
moel@1: 
moel@1:       foreach (Sensor sensor in fans) {
moel@1:         int value = ReadByte(FAN_TACHOMETER_REG[sensor.Index]);
moel@1:         value |= ReadByte(FAN_TACHOMETER_EXT_REG[sensor.Index]) << 8;
moel@1: 
moel@1:         if (value > 0) {
moel@1:           sensor.Value = (value < 0xffff) ? 1.35e6f / ((value) * 2) : 0;
moel@1:           ActivateSensor(sensor);
moel@1:         } else {
moel@1:           DeactivateSensor(sensor);
moel@1:         }
moel@1:       }      
moel@1:     }
moel@1: 
moel@1:     private void ActivateSensor(Sensor sensor) {
moel@1:       if (!active.Contains(sensor)) {
moel@1:         active.Add(sensor);
moel@1:         SensorAdded(sensor);
moel@1:       }
moel@1:     }
moel@1: 
moel@1:     private void DeactivateSensor(Sensor sensor) {
moel@1:       if (active.Contains(sensor)) {
moel@1:         active.Remove(sensor);
moel@1:         SensorRemoved(sensor);
moel@1:       }
moel@1:     }
moel@1: 
moel@1:     public event SensorEventHandler SensorAdded;
moel@1:     public event SensorEventHandler SensorRemoved;
moel@1: 
moel@1:   }
moel@1: }