Merge branch 'release' of git://lm-sensors.org/kernel/mhoffman/hwmon-2.6
[pandora-kernel.git] / drivers / hwmon / fschmd.c
index 63a4df0..9671703 100644 (file)
 #include <linux/err.h>
 #include <linux/mutex.h>
 #include <linux/sysfs.h>
+#include <linux/dmi.h>
 
 /* Addresses to scan */
-static unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
+static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
 
 /* Insmod parameters */
 I2C_CLIENT_INSMOD_5(fscpos, fscher, fscscy, fschrc, fschmd);
@@ -133,7 +134,7 @@ static const u8 FSCHMD_REG_TEMP_STATE[5][5] = {
        { 0x71, 0x81, 0x91 },                           /* her */
        { 0x71, 0xd1, 0x81, 0x91 },                     /* scy */
        { 0x71, 0x81, 0x91 },                           /* hrc */
-       { 0x71, 0x81, 0x91, 0xd1, 0xe1 },               /* hmd */
+       { 0x71, 0x81, 0x91, 0xd1, 0xe1 },               /* hmd */
 };
 
 /* temperature high limit registers, FSC does not document these. Proven to be
@@ -146,7 +147,7 @@ static const u8 FSCHMD_REG_TEMP_LIMIT[5][5] = {
        { 0x76, 0x86, 0x96 },                           /* her */
        { 0x76, 0xd6, 0x86, 0x96 },                     /* scy */
        { 0x76, 0x86, 0x96 },                           /* hrc */
-       { 0x76, 0x86, 0x96, 0xd6, 0xe6 },               /* hmd */
+       { 0x76, 0x86, 0x96, 0xd6, 0xe6 },               /* hmd */
 };
 
 /* These were found through experimenting with an fscher, currently they are
@@ -170,20 +171,37 @@ static const int FSCHMD_NO_TEMP_SENSORS[5] = { 3, 3, 4, 3, 5 };
  * Functions declarations
  */
 
-static int fschmd_attach_adapter(struct i2c_adapter *adapter);
-static int fschmd_detach_client(struct i2c_client *client);
+static int fschmd_probe(struct i2c_client *client,
+                       const struct i2c_device_id *id);
+static int fschmd_detect(struct i2c_client *client, int kind,
+                        struct i2c_board_info *info);
+static int fschmd_remove(struct i2c_client *client);
 static struct fschmd_data *fschmd_update_device(struct device *dev);
 
 /*
  * Driver data (common to all clients)
  */
 
+static const struct i2c_device_id fschmd_id[] = {
+       { "fscpos", fscpos },
+       { "fscher", fscher },
+       { "fscscy", fscscy },
+       { "fschrc", fschrc },
+       { "fschmd", fschmd },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, fschmd_id);
+
 static struct i2c_driver fschmd_driver = {
+       .class          = I2C_CLASS_HWMON,
        .driver = {
                .name   = FSCHMD_NAME,
        },
-       .attach_adapter = fschmd_attach_adapter,
-       .detach_client  = fschmd_detach_client,
+       .probe          = fschmd_probe,
+       .remove         = fschmd_remove,
+       .id_table       = fschmd_id,
+       .detect         = fschmd_detect,
+       .address_data   = &addr_data,
 };
 
 /*
@@ -191,7 +209,6 @@ static struct i2c_driver fschmd_driver = {
  */
 
 struct fschmd_data {
-       struct i2c_client client;
        struct device *hwmon_dev;
        struct mutex update_lock;
        int kind;
@@ -210,6 +227,13 @@ struct fschmd_data {
        u8 fan_ripple[6];       /* divider for rps */
 };
 
+/* Global variables to hold information read from special DMI tables, which are
+   available on FSC machines with an fscher or later chip. */
+static int dmi_mult[3] = { 490, 200, 100 };
+static int dmi_offset[3] = { 0, 0, 0 };
+static int dmi_vref = -1;
+
+
 /*
  * Sysfs attr show / store functions
  */
@@ -221,8 +245,13 @@ static ssize_t show_in_value(struct device *dev,
        int index = to_sensor_dev_attr(devattr)->index;
        struct fschmd_data *data = fschmd_update_device(dev);
 
-       return sprintf(buf, "%d\n", (data->volt[index] *
-               max_reading[index] + 128) / 255);
+       /* fscher / fschrc - 1 as data->kind is an array index, not a chips */
+       if (data->kind == (fscher - 1) || data->kind >= (fschrc - 1))
+               return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref *
+                       dmi_mult[index]) / 255 + dmi_offset[index]);
+       else
+               return sprintf(buf, "%d\n", (data->volt[index] *
+                       max_reading[index] + 128) / 255);
 }
 
 
@@ -256,7 +285,7 @@ static ssize_t store_temp_max(struct device *dev, struct device_attribute
        v = SENSORS_LIMIT(v, -128, 127) + 128;
 
        mutex_lock(&data->update_lock);
-       i2c_smbus_write_byte_data(&data->client,
+       i2c_smbus_write_byte_data(to_i2c_client(dev),
                FSCHMD_REG_TEMP_LIMIT[data->kind][index], v);
        data->temp_max[index] = v;
        mutex_unlock(&data->update_lock);
@@ -333,14 +362,14 @@ static ssize_t store_fan_div(struct device *dev, struct device_attribute
 
        mutex_lock(&data->update_lock);
 
-       reg = i2c_smbus_read_byte_data(&data->client,
+       reg = i2c_smbus_read_byte_data(to_i2c_client(dev),
                FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
 
        /* bits 2..7 reserved => mask with 0x03 */
        reg &= ~0x03;
        reg |= v;
 
-       i2c_smbus_write_byte_data(&data->client,
+       i2c_smbus_write_byte_data(to_i2c_client(dev),
                FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
 
        data->fan_ripple[index] = reg;
@@ -403,7 +432,7 @@ static ssize_t store_pwm_auto_point1_pwm(struct device *dev,
 
        mutex_lock(&data->update_lock);
 
-       i2c_smbus_write_byte_data(&data->client,
+       i2c_smbus_write_byte_data(to_i2c_client(dev),
                FSCHMD_REG_FAN_MIN[data->kind][index], v);
        data->fan_min[index] = v;
 
@@ -435,14 +464,14 @@ static ssize_t store_alert_led(struct device *dev,
 
        mutex_lock(&data->update_lock);
 
-       reg = i2c_smbus_read_byte_data(&data->client, FSCHMD_REG_CONTROL);
+       reg = i2c_smbus_read_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL);
 
        if (v)
                reg |= FSCHMD_CONTROL_ALERT_LED_MASK;
        else
                reg &= ~FSCHMD_CONTROL_ALERT_LED_MASK;
 
-       i2c_smbus_write_byte_data(&data->client, FSCHMD_REG_CONTROL, reg);
+       i2c_smbus_write_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL, reg);
 
        data->global_control = reg;
 
@@ -525,32 +554,77 @@ static struct sensor_device_attribute fschmd_fan_attr[] = {
  * Real code
  */
 
-static int fschmd_detect(struct i2c_adapter *adapter, int address, int kind)
+/* DMI decode routine to read voltage scaling factors from special DMI tables,
+   which are available on FSC machines with an fscher or later chip. */
+static void fschmd_dmi_decode(const struct dmi_header *header)
 {
-       struct i2c_client *client;
-       struct fschmd_data *data;
-       u8 revision;
-       const char * const names[5] = { "Poseidon", "Hermes", "Scylla",
-                                       "Heracles", "Heimdall" };
+       int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
+
+       /* dmi code ugliness, we get passed the address of the contents of
+          a complete DMI record, but in the form of a dmi_header pointer, in
+          reality this address holds header->length bytes of which the header
+          are the first 4 bytes */
+       u8 *dmi_data = (u8 *)header;
+
+       /* We are looking for OEM-specific type 185 */
+       if (header->type != 185)
+               return;
+
+       /* we are looking for what Siemens calls "subtype" 19, the subtype
+          is stored in byte 5 of the dmi block */
+       if (header->length < 5 || dmi_data[4] != 19)
+               return;
+
+       /* After the subtype comes 1 unknown byte and then blocks of 5 bytes,
+          consisting of what Siemens calls an "Entity" number, followed by
+          2 16-bit words in LSB first order */
+       for (i = 6; (i + 4) < header->length; i += 5) {
+               /* entity 1 - 3: voltage multiplier and offset */
+               if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
+                       /* Our in sensors order and the DMI order differ */
+                       const int shuffle[3] = { 1, 0, 2 };
+                       int in = shuffle[dmi_data[i] - 1];
+
+                       /* Check for twice the same entity */
+                       if (found & (1 << in))
+                               return;
+
+                       mult[in] = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
+                       offset[in] = dmi_data[i + 3] | (dmi_data[i + 4] << 8);
+
+                       found |= 1 << in;
+               }
+
+               /* entity 7: reference voltage */
+               if (dmi_data[i] == 7) {
+                       /* Check for twice the same entity */
+                       if (found & 0x08)
+                               return;
+
+                       vref = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
+
+                       found |= 0x08;
+               }
+       }
+
+       if (found == 0x0F) {
+               for (i = 0; i < 3; i++) {
+                       dmi_mult[i] = mult[i] * 10;
+                       dmi_offset[i] = offset[i] * 10;
+               }
+               dmi_vref = vref;
+       }
+}
+
+static int fschmd_detect(struct i2c_client *client, int kind,
+                        struct i2c_board_info *info)
+{
+       struct i2c_adapter *adapter = client->adapter;
        const char * const client_names[5] = { "fscpos", "fscher", "fscscy",
                                                "fschrc", "fschmd" };
-       int i, err = 0;
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
-               return 0;
-
-       /* OK. For now, we presume we have a valid client. We now create the
-        * client structure, even though we cannot fill it completely yet.
-        * But it allows us to access i2c_smbus_read_byte_data. */
-       if (!(data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL)))
-               return -ENOMEM;
-
-       client = &data->client;
-       i2c_set_clientdata(client, data);
-       client->addr = address;
-       client->adapter = adapter;
-       client->driver = &fschmd_driver;
-       mutex_init(&data->update_lock);
+               return -ENODEV;
 
        /* Detect & Identify the chip */
        if (kind <= 0) {
@@ -575,9 +649,31 @@ static int fschmd_detect(struct i2c_adapter *adapter, int address, int kind)
                else if (!strcmp(id, "HMD"))
                        kind = fschmd;
                else
-                       goto exit_free;
+                       return -ENODEV;
        }
 
+       strlcpy(info->type, client_names[kind - 1], I2C_NAME_SIZE);
+
+       return 0;
+}
+
+static int fschmd_probe(struct i2c_client *client,
+                       const struct i2c_device_id *id)
+{
+       struct fschmd_data *data;
+       u8 revision;
+       const char * const names[5] = { "Poseidon", "Hermes", "Scylla",
+                                       "Heracles", "Heimdall" };
+       int i, err;
+       enum chips kind = id->driver_data;
+
+       data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL);
+       if (!data)
+               return -ENOMEM;
+
+       i2c_set_clientdata(client, data);
+       mutex_init(&data->update_lock);
+
        if (kind == fscpos) {
                /* The Poseidon has hardwired temp limits, fill these
                   in for the alarm resetting code */
@@ -586,13 +682,19 @@ static int fschmd_detect(struct i2c_adapter *adapter, int address, int kind)
                data->temp_max[2] = 50 + 128;
        }
 
+       /* Read the special DMI table for fscher and newer chips */
+       if (kind == fscher || kind >= fschrc) {
+               dmi_walk(fschmd_dmi_decode);
+               if (dmi_vref == -1) {
+                       printk(KERN_WARNING FSCHMD_NAME
+                               ": Couldn't get voltage scaling factors from "
+                               "BIOS DMI table, using builtin defaults\n");
+                       dmi_vref = 33;
+               }
+       }
+
        /* i2c kind goes from 1-5, we want from 0-4 to address arrays */
        data->kind = kind - 1;
-       strlcpy(client->name, client_names[data->kind], I2C_NAME_SIZE);
-
-       /* Tell the I2C layer a new client has arrived */
-       if ((err = i2c_attach_client(client)))
-               goto exit_free;
 
        for (i = 0; i < ARRAY_SIZE(fschmd_attr); i++) {
                err = device_create_file(&client->dev,
@@ -640,25 +742,14 @@ static int fschmd_detect(struct i2c_adapter *adapter, int address, int kind)
        return 0;
 
 exit_detach:
-       fschmd_detach_client(client); /* will also free data for us */
-       return err;
-
-exit_free:
-       kfree(data);
+       fschmd_remove(client); /* will also free data for us */
        return err;
 }
 
-static int fschmd_attach_adapter(struct i2c_adapter *adapter)
-{
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_probe(adapter, &addr_data, fschmd_detect);
-}
-
-static int fschmd_detach_client(struct i2c_client *client)
+static int fschmd_remove(struct i2c_client *client)
 {
        struct fschmd_data *data = i2c_get_clientdata(client);
-       int i, err;
+       int i;
 
        /* Check if registered in case we're called from fschmd_detect
           to cleanup after an error */
@@ -674,9 +765,6 @@ static int fschmd_detach_client(struct i2c_client *client)
                device_remove_file(&client->dev,
                                        &fschmd_fan_attr[i].dev_attr);
 
-       if ((err = i2c_detach_client(client)))
-               return err;
-
        kfree(data);
        return 0;
 }