hwmon: (lm95241) Convert to new-style i2c driver
authorJean Delvare <khali@linux-fr.org>
Tue, 7 Apr 2009 13:32:59 +0000 (15:32 +0200)
committerJean Delvare <khali@linux-fr.org>
Tue, 7 Apr 2009 13:32:59 +0000 (15:32 +0200)
The legacy i2c binding model is going away really soon now, so convert
the lm95241 driver to the new binding model or it will break.

Signed-off-by: Jean Delvare <khali@linux-fr.org>
Cc: Davide Rizzo <elpa.rizzo@gmail.com>
drivers/hwmon/lm95241.c

index 091d95f..e34f9e4 100644 (file)
@@ -87,25 +87,11 @@ I2C_CLIENT_INSMOD_1(lm95241);
     (val_h)) * 1000 + (val_l) * 1000 / 256)
 
 /* Functions declaration */
-static int lm95241_attach_adapter(struct i2c_adapter *adapter);
-static int lm95241_detect(struct i2c_adapter *adapter, int address,
-                         int kind);
 static void lm95241_init_client(struct i2c_client *client);
-static int lm95241_detach_client(struct i2c_client *client);
 static struct lm95241_data *lm95241_update_device(struct device *dev);
 
-/* Driver data (common to all clients) */
-static struct i2c_driver lm95241_driver = {
-       .driver = {
-               .name   = "lm95241",
-       },
-       .attach_adapter = lm95241_attach_adapter,
-       .detach_client  = lm95241_detach_client,
-};
-
 /* Client data (each client gets its own) */
 struct lm95241_data {
-       struct i2c_client client;
        struct device *hwmon_dev;
        struct mutex update_lock;
        unsigned long last_updated, rate; /* in jiffies */
@@ -323,42 +309,16 @@ static const struct attribute_group lm95241_group = {
        .attrs = lm95241_attributes,
 };
 
-/* Init/exit code */
-static int lm95241_attach_adapter(struct i2c_adapter *adapter)
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int lm95241_detect(struct i2c_client *new_client, int kind,
+                         struct i2c_board_info *info)
 {
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_probe(adapter, &addr_data, lm95241_detect);
-}
-
-/*
- * The following function does more than just detection. If detection
- * succeeds, it also registers the new chip.
- */
-static int lm95241_detect(struct i2c_adapter *adapter, int address, int kind)
-{
-       struct i2c_client *new_client;
-       struct lm95241_data *data;
-       int err = 0;
+       struct i2c_adapter *adapter = new_client->adapter;
+       int address = new_client->addr;
        const char *name = "";
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
-               goto exit;
-
-       data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
-       if (!data) {
-               err = -ENOMEM;
-               goto exit;
-       }
-
-       /* The common I2C client data is placed right before the
-          LM95241-specific data. */
-       new_client = &data->client;
-       i2c_set_clientdata(new_client, data);
-       new_client->addr = address;
-       new_client->adapter = adapter;
-       new_client->driver = &lm95241_driver;
-       new_client->flags = 0;
+               return -ENODEV;
 
        /*
         * Now we do the remaining detection. A negative kind means that
@@ -378,7 +338,7 @@ static int lm95241_detect(struct i2c_adapter *adapter, int address, int kind)
                        dev_dbg(&adapter->dev,
                                "LM95241 detection failed at 0x%02x.\n",
                                address);
-                       goto exit_free;
+                       return -ENODEV;
                }
        }
 
@@ -392,23 +352,32 @@ static int lm95241_detect(struct i2c_adapter *adapter, int address, int kind)
 
                        if (kind <= 0) { /* identification failed */
                                dev_info(&adapter->dev, "Unsupported chip\n");
-                               goto exit_free;
+                               return -ENODEV;
                        }
                }
        }
 
+       /* Fill the i2c board info */
        if (kind == lm95241)
                name = "lm95241";
+       strlcpy(info->type, name, I2C_NAME_SIZE);
+       return 0;
+}
 
-       /* We can fill in the remaining client fields */
-       strlcpy(new_client->name, name, I2C_NAME_SIZE);
-       data->valid = 0;
-       mutex_init(&data->update_lock);
+static int lm95241_probe(struct i2c_client *new_client,
+                        const struct i2c_device_id *id)
+{
+       struct lm95241_data *data;
+       int err;
 
-       /* Tell the I2C layer a new client has arrived */
-       err = i2c_attach_client(new_client);
-       if (err)
-               goto exit_free;
+       data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
+       if (!data) {
+               err = -ENOMEM;
+               goto exit;
+       }
+
+       i2c_set_clientdata(new_client, data);
+       mutex_init(&data->update_lock);
 
        /* Initialize the LM95241 chip */
        lm95241_init_client(new_client);
@@ -416,7 +385,7 @@ static int lm95241_detect(struct i2c_adapter *adapter, int address, int kind)
        /* Register sysfs hooks */
        err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
        if (err)
-               goto exit_detach;
+               goto exit_free;
 
        data->hwmon_dev = hwmon_device_register(&new_client->dev);
        if (IS_ERR(data->hwmon_dev)) {
@@ -428,8 +397,6 @@ static int lm95241_detect(struct i2c_adapter *adapter, int address, int kind)
 
 exit_remove_files:
        sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
-exit_detach:
-       i2c_detach_client(new_client);
 exit_free:
        kfree(data);
 exit:
@@ -456,18 +423,14 @@ static void lm95241_init_client(struct i2c_client *client)
                                  data->model);
 }
 
-static int lm95241_detach_client(struct i2c_client *client)
+static int lm95241_remove(struct i2c_client *client)
 {
        struct lm95241_data *data = i2c_get_clientdata(client);
-       int err;
 
        hwmon_device_unregister(data->hwmon_dev);
        sysfs_remove_group(&client->dev.kobj, &lm95241_group);
 
-       err = i2c_detach_client(client);
-       if (err)
-               return err;
-
+       i2c_set_clientdata(client, NULL);
        kfree(data);
        return 0;
 }
@@ -509,6 +472,25 @@ static struct lm95241_data *lm95241_update_device(struct device *dev)
        return data;
 }
 
+/* Driver data (common to all clients) */
+static const struct i2c_device_id lm95241_id[] = {
+       { "lm95241", lm95241 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, lm95241_id);
+
+static struct i2c_driver lm95241_driver = {
+       .class          = I2C_CLASS_HWMON,
+       .driver = {
+               .name   = "lm95241",
+       },
+       .probe          = lm95241_probe,
+       .remove         = lm95241_remove,
+       .id_table       = lm95241_id,
+       .detect         = lm95241_detect,
+       .address_data   = &addr_data,
+};
+
 static int __init sensors_lm95241_init(void)
 {
        return i2c_add_driver(&lm95241_driver);