[PATCH] fbdev: neofb: Driver cleanups
[powerpc.git] / drivers / hwmon / ds1621.c
index 5360d58..203f9c7 100644 (file)
 #include <linux/slab.h>
 #include <linux/jiffies.h>
 #include <linux/i2c.h>
-#include <linux/i2c-sensor.h>
+#include <linux/hwmon.h>
+#include <linux/err.h>
 #include "lm75.h"
 
 /* Addresses to scan */
 static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
                                        0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
-static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
 
 /* Insmod parameters */
-SENSORS_INSMOD_1(ds1621);
+I2C_CLIENT_INSMOD_1(ds1621);
 static int polarity = -1;
 module_param(polarity, int, 0);
 MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
@@ -71,6 +71,7 @@ MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low")
 /* Each client has this additional data */
 struct ds1621_data {
        struct i2c_client client;
+       struct class_device *class_dev;
        struct semaphore update_lock;
        char valid;                     /* !=0 if following fields are valid */
        unsigned long last_updated;     /* In jiffies */
@@ -88,10 +89,10 @@ static struct ds1621_data *ds1621_update_client(struct device *dev);
 
 /* This is the driver that will be inserted */
 static struct i2c_driver ds1621_driver = {
-       .owner          = THIS_MODULE,
-       .name           = "ds1621",
+       .driver = {
+               .name   = "ds1621",
+       },
        .id             = I2C_DRIVERID_DS1621,
-       .flags          = I2C_DF_NOTIFY,
        .attach_adapter = ds1621_attach_adapter,
        .detach_client  = ds1621_detach_client,
 };
@@ -179,12 +180,14 @@ static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
 
 static int ds1621_attach_adapter(struct i2c_adapter *adapter)
 {
-       return i2c_detect(adapter, &addr_data, ds1621_detect);
+       if (!(adapter->class & I2C_CLASS_HWMON))
+               return 0;
+       return i2c_probe(adapter, &addr_data, ds1621_detect);
 }
 
-/* This function is called by i2c_detect */
-int ds1621_detect(struct i2c_adapter *adapter, int address,
-                  int kind)
+/* This function is called by i2c_probe */
+static int ds1621_detect(struct i2c_adapter *adapter, int address,
+                        int kind)
 {
        int conf, temp;
        struct i2c_client *new_client;
@@ -199,11 +202,10 @@ int ds1621_detect(struct i2c_adapter *adapter, int address,
        /* 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 ds1621_{read,write}_value. */
-       if (!(data = kmalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
+       if (!(data = kzalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
                err = -ENOMEM;
                goto exit;
        }
-       memset(data, 0, sizeof(struct ds1621_data));
        
        new_client = &data->client;
        i2c_set_clientdata(new_client, data);
@@ -250,6 +252,12 @@ int ds1621_detect(struct i2c_adapter *adapter, int address,
        ds1621_init_client(new_client);
 
        /* Register sysfs hooks */
+       data->class_dev = hwmon_device_register(&new_client->dev);
+       if (IS_ERR(data->class_dev)) {
+               err = PTR_ERR(data->class_dev);
+               goto exit_detach;
+       }
+
        device_create_file(&new_client->dev, &dev_attr_alarms);
        device_create_file(&new_client->dev, &dev_attr_temp1_input);
        device_create_file(&new_client->dev, &dev_attr_temp1_min);
@@ -259,6 +267,8 @@ int ds1621_detect(struct i2c_adapter *adapter, int address,
 
 /* OK, this is not exactly good programming practice, usually. But it is
    very code-efficient in this case. */
+      exit_detach:
+       i2c_detach_client(new_client);
       exit_free:
        kfree(data);
       exit:
@@ -267,15 +277,15 @@ int ds1621_detect(struct i2c_adapter *adapter, int address,
 
 static int ds1621_detach_client(struct i2c_client *client)
 {
+       struct ds1621_data *data = i2c_get_clientdata(client);
        int err;
 
-       if ((err = i2c_detach_client(client))) {
-               dev_err(&client->dev, "Client deregistration failed, "
-                       "client not detached.\n");
+       hwmon_device_unregister(data->class_dev);
+
+       if ((err = i2c_detach_client(client)))
                return err;
-       }
 
-       kfree(i2c_get_clientdata(client));
+       kfree(data);
 
        return 0;
 }