const: struct nla_policy
[safe/jmp/linux-2.6] / drivers / hwmon / gl518sm.c
index ed901f9..e7ae574 100644 (file)
 #include <linux/sysfs.h>
 
 /* Addresses to scan */
-static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
+static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
 
-/* Insmod parameters */
-I2C_CLIENT_INSMOD_2(gl518sm_r00, gl518sm_r80);
+enum chips { gl518sm_r00, gl518sm_r80 };
 
 /* Many GL518 constants specified below */
 
@@ -96,10 +95,10 @@ static inline u8 FAN_TO_REG(long rpm, int div)
        long rpmdiv;
        if (rpm == 0)
                return 0;
-       rpmdiv = SENSORS_LIMIT(rpm, 1, 1920000) * div;
-       return SENSORS_LIMIT((960000 + rpmdiv / 2) / rpmdiv, 1, 255);
+       rpmdiv = SENSORS_LIMIT(rpm, 1, 960000) * div;
+       return SENSORS_LIMIT((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
 }
-#define FAN_FROM_REG(val,div)  ((val)==0 ? 0 : (960000/((val)*(div))))
+#define FAN_FROM_REG(val,div)  ((val)==0 ? 0 : (480000/((val)*(div))))
 
 #define IN_TO_REG(val)         (SENSORS_LIMIT((((val)+9)/19),0,255))
 #define IN_FROM_REG(val)       ((val)*19)
@@ -107,7 +106,6 @@ static inline u8 FAN_TO_REG(long rpm, int div)
 #define VDD_TO_REG(val)                (SENSORS_LIMIT((((val)*4+47)/95),0,255))
 #define VDD_FROM_REG(val)      (((val)*95+2)/4)
 
-#define DIV_TO_REG(val)                ((val)==4?2:(val)==2?1:(val)==1?0:3)
 #define DIV_FROM_REG(val)      (1 << (val))
 
 #define BEEP_MASK_TO_REG(val)  ((val) & 0x7f & data->alarm_mask)
@@ -115,7 +113,6 @@ static inline u8 FAN_TO_REG(long rpm, int div)
 
 /* Each client has this additional data */
 struct gl518_data {
-       struct i2c_client client;
        struct device *hwmon_dev;
        enum chips type;
 
@@ -139,21 +136,32 @@ struct gl518_data {
        u8 beep_enable;         /* Boolean */
 };
 
-static int gl518_attach_adapter(struct i2c_adapter *adapter);
-static int gl518_detect(struct i2c_adapter *adapter, int address, int kind);
+static int gl518_probe(struct i2c_client *client,
+                      const struct i2c_device_id *id);
+static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info);
 static void gl518_init_client(struct i2c_client *client);
-static int gl518_detach_client(struct i2c_client *client);
+static int gl518_remove(struct i2c_client *client);
 static int gl518_read_value(struct i2c_client *client, u8 reg);
 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
 static struct gl518_data *gl518_update_device(struct device *dev);
 
+static const struct i2c_device_id gl518_id[] = {
+       { "gl518sm", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, gl518_id);
+
 /* This is the driver that will be inserted */
 static struct i2c_driver gl518_driver = {
+       .class          = I2C_CLASS_HWMON,
        .driver = {
                .name   = "gl518sm",
        },
-       .attach_adapter = gl518_attach_adapter,
-       .detach_client  = gl518_detach_client,
+       .probe          = gl518_probe,
+       .remove         = gl518_remove,
+       .id_table       = gl518_id,
+       .detect         = gl518_detect,
+       .address_list   = normal_i2c,
 };
 
 /*
@@ -302,9 +310,20 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
        int regvalue;
        unsigned long val = simple_strtoul(buf, NULL, 10);
 
+       switch (val) {
+       case 1: val = 0; break;
+       case 2: val = 1; break;
+       case 4: val = 2; break;
+       case 8: val = 3; break;
+       default:
+               dev_err(dev, "Invalid fan clock divider %lu, choose one "
+                       "of 1, 2, 4 or 8\n", val);
+               return -EINVAL;
+       }
+
        mutex_lock(&data->update_lock);
        regvalue = gl518_read_value(client, GL518_REG_MISC);
-       data->fan_div[nr] = DIV_TO_REG(val);
+       data->fan_div[nr] = val;
        regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
                 | (data->fan_div[nr] << (6 - 2 * nr));
        gl518_write_value(client, GL518_REG_MISC, regvalue);
@@ -345,6 +364,61 @@ static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
 static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
        show_beep_mask, set_beep_mask);
 
+static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
+                         char *buf)
+{
+       int bitnr = to_sensor_dev_attr(attr)->index;
+       struct gl518_data *data = gl518_update_device(dev);
+       return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
+}
+
+static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
+static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
+static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
+static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
+static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
+static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
+
+static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
+                         char *buf)
+{
+       int bitnr = to_sensor_dev_attr(attr)->index;
+       struct gl518_data *data = gl518_update_device(dev);
+       return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
+}
+
+static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
+                       const char *buf, size_t count)
+{
+       struct i2c_client *client = to_i2c_client(dev);
+       struct gl518_data *data = i2c_get_clientdata(client);
+       int bitnr = to_sensor_dev_attr(attr)->index;
+       unsigned long bit;
+
+       bit = simple_strtoul(buf, NULL, 10);
+       if (bit & ~1)
+               return -EINVAL;
+
+       mutex_lock(&data->update_lock);
+       data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
+       if (bit)
+               data->beep_mask |= (1 << bitnr);
+       else
+               data->beep_mask &= ~(1 << bitnr);
+       gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
+       mutex_unlock(&data->update_lock);
+       return count;
+}
+
+static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
+static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
+static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
+static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
+static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
+static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
+static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
+
 static struct attribute *gl518_attributes[] = {
        &dev_attr_in3_input.attr,
        &dev_attr_in0_min.attr,
@@ -355,6 +429,14 @@ static struct attribute *gl518_attributes[] = {
        &dev_attr_in1_max.attr,
        &dev_attr_in2_max.attr,
        &dev_attr_in3_max.attr,
+       &sensor_dev_attr_in0_alarm.dev_attr.attr,
+       &sensor_dev_attr_in1_alarm.dev_attr.attr,
+       &sensor_dev_attr_in2_alarm.dev_attr.attr,
+       &sensor_dev_attr_in3_alarm.dev_attr.attr,
+       &sensor_dev_attr_in0_beep.dev_attr.attr,
+       &sensor_dev_attr_in1_beep.dev_attr.attr,
+       &sensor_dev_attr_in2_beep.dev_attr.attr,
+       &sensor_dev_attr_in3_beep.dev_attr.attr,
 
        &dev_attr_fan1_auto.attr,
        &sensor_dev_attr_fan1_input.dev_attr.attr,
@@ -363,10 +445,16 @@ static struct attribute *gl518_attributes[] = {
        &sensor_dev_attr_fan2_min.dev_attr.attr,
        &sensor_dev_attr_fan1_div.dev_attr.attr,
        &sensor_dev_attr_fan2_div.dev_attr.attr,
+       &sensor_dev_attr_fan1_alarm.dev_attr.attr,
+       &sensor_dev_attr_fan2_alarm.dev_attr.attr,
+       &sensor_dev_attr_fan1_beep.dev_attr.attr,
+       &sensor_dev_attr_fan2_beep.dev_attr.attr,
 
        &dev_attr_temp1_input.attr,
        &dev_attr_temp1_max.attr,
        &dev_attr_temp1_max_hyst.attr,
+       &sensor_dev_attr_temp1_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp1_beep.dev_attr.attr,
 
        &dev_attr_alarms.attr,
        &dev_attr_beep_enable.attr,
@@ -393,81 +481,55 @@ static const struct attribute_group gl518_group_r80 = {
  * Real code
  */
 
-static int gl518_attach_adapter(struct i2c_adapter *adapter)
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
 {
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_probe(adapter, &addr_data, gl518_detect);
-}
-
-static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
-{
-       int i;
-       struct i2c_client *client;
-       struct gl518_data *data;
-       int err = 0;
+       struct i2c_adapter *adapter = client->adapter;
+       int rev;
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
                                     I2C_FUNC_SMBUS_WORD_DATA))
-               goto exit;
-
-       /* 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 gl518_{read,write}_value. */
+               return -ENODEV;
 
-       if (!(data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
-               err = -ENOMEM;
-               goto exit;
-       }
+       /* Now, we do the remaining detection. */
+       if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
+        || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
+               return -ENODEV;
 
-       client = &data->client;
-       i2c_set_clientdata(client, data);
+       /* Determine the chip type. */
+       rev = gl518_read_value(client, GL518_REG_REVISION);
+       if (rev != 0x00 && rev != 0x80)
+               return -ENODEV;
 
-       client->addr = address;
-       client->adapter = adapter;
-       client->driver = &gl518_driver;
+       strlcpy(info->type, "gl518sm", I2C_NAME_SIZE);
 
-       /* Now, we do the remaining detection. */
+       return 0;
+}
 
-       if (kind < 0) {
-               if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
-                || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
-                       goto exit_free;
-       }
+static int gl518_probe(struct i2c_client *client,
+                      const struct i2c_device_id *id)
+{
+       struct gl518_data *data;
+       int err, revision;
 
-       /* Determine the chip type. */
-       if (kind <= 0) {
-               i = gl518_read_value(client, GL518_REG_REVISION);
-               if (i == 0x00) {
-                       kind = gl518sm_r00;
-               } else if (i == 0x80) {
-                       kind = gl518sm_r80;
-               } else {
-                       if (kind <= 0)
-                               dev_info(&adapter->dev,
-                                   "Ignoring 'force' parameter for unknown "
-                                   "chip at adapter %d, address 0x%02x\n",
-                                   i2c_adapter_id(adapter), address);
-                       goto exit_free;
-               }
+       data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL);
+       if (!data) {
+               err = -ENOMEM;
+               goto exit;
        }
 
-       /* Fill in the remaining client fields */
-       strlcpy(client->name, "gl518sm", I2C_NAME_SIZE);
-       data->type = kind;
+       i2c_set_clientdata(client, data);
+       revision = gl518_read_value(client, GL518_REG_REVISION);
+       data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
        mutex_init(&data->update_lock);
 
-       /* Tell the I2C layer a new client has arrived */
-       if ((err = i2c_attach_client(client)))
-               goto exit_free;
-
        /* Initialize the GL518SM chip */
        data->alarm_mask = 0xff;
        gl518_init_client(client);
 
        /* Register sysfs hooks */
        if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group)))
-               goto exit_detach;
+               goto exit_free;
        if (data->type == gl518sm_r80)
                if ((err = sysfs_create_group(&client->dev.kobj,
                                              &gl518_group_r80)))
@@ -485,8 +547,6 @@ exit_remove_files:
        sysfs_remove_group(&client->dev.kobj, &gl518_group);
        if (data->type == gl518sm_r80)
                sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
-exit_detach:
-       i2c_detach_client(client);
 exit_free:
        kfree(data);
 exit:
@@ -512,19 +572,15 @@ static void gl518_init_client(struct i2c_client *client)
        gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
 }
 
-static int gl518_detach_client(struct i2c_client *client)
+static int gl518_remove(struct i2c_client *client)
 {
        struct gl518_data *data = i2c_get_clientdata(client);
-       int err;
 
        hwmon_device_unregister(data->hwmon_dev);
        sysfs_remove_group(&client->dev.kobj, &gl518_group);
        if (data->type == gl518sm_r80)
                sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
 
-       if ((err = i2c_detach_client(client)))
-               return err;
-
        kfree(data);
        return 0;
 }