Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[safe/jmp/linux-2.6] / drivers / i2c / chips / tsl2550.c
index ef80330..b96f302 100644 (file)
@@ -27,7 +27,7 @@
 #include <linux/delay.h>
 
 #define TSL2550_DRV_NAME       "tsl2550"
-#define DRIVER_VERSION         "1.1.0"
+#define DRIVER_VERSION         "1.1.2"
 
 /*
  * Defines
@@ -189,13 +189,16 @@ static int tsl2550_calculate_lux(u8 ch0, u8 ch1)
        u8 r = 128;
 
        /* Avoid division by 0 and count 1 cannot be greater than count 0 */
-       if (c0 && (c1 <= c0))
-               r = c1 * 128 / c0;
+       if (c1 <= c0)
+               if (c0) {
+                       r = c1 * 128 / c0;
+
+                       /* Calculate LUX */
+                       lux = ((c0 - c1) * ratio_lut[r]) / 256;
+               } else
+                       lux = 0;
        else
-               return -1;
-
-       /* Calculate LUX */
-       lux = ((c0 - c1) * ratio_lut[r]) / 256;
+               return -EAGAIN;
 
        /* LUX range check */
        return lux > TSL2550_MAX_LUX ? TSL2550_MAX_LUX : lux;
@@ -333,13 +336,30 @@ static const struct attribute_group tsl2550_attr_group = {
  * Initialization function
  */
 
-static void tsl2550_init_client(struct i2c_client *client)
+static int tsl2550_init_client(struct i2c_client *client)
 {
        struct tsl2550_data *data = i2c_get_clientdata(client);
+       int err;
+
+       /*
+        * Probe the chip. To do so we try to power up the device and then to
+        * read back the 0x03 code
+        */
+       err = i2c_smbus_write_byte(client, TSL2550_POWER_UP);
+       if (err < 0)
+               return err;
+       mdelay(1);
+       if (i2c_smbus_read_byte(client) != TSL2550_POWER_UP)
+               return -ENODEV;
+       data->power_state = 1;
 
-       /* Power up the device and set the default operating mode */
-       tsl2550_set_power_state(client, 1);
-       tsl2550_set_operating_mode(client, data->operating_mode);
+       /* Set the default operating mode */
+       err = i2c_smbus_write_byte(client,
+                                  TSL2550_MODE_RANGE[data->operating_mode]);
+       if (err < 0)
+               return err;
+
+       return 0;
 }
 
 /*
@@ -347,7 +367,8 @@ static void tsl2550_init_client(struct i2c_client *client)
  */
 
 static struct i2c_driver tsl2550_driver;
-static int __devinit tsl2550_probe(struct i2c_client *client)
+static int __devinit tsl2550_probe(struct i2c_client *client,
+                                  const struct i2c_device_id *id)
 {
        struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
        struct tsl2550_data *data;
@@ -381,24 +402,12 @@ static int __devinit tsl2550_probe(struct i2c_client *client)
        dev_info(&client->dev, "%s operating mode\n",
                        data->operating_mode ? "extended" : "standard");
 
-       /*
-        * Probe the chip. To do so we try to power up the device and then to
-        * read back the 0x03 code
-        */
-       err = i2c_smbus_write_byte(client, TSL2550_POWER_UP);
-       if (err < 0)
-               goto exit_kfree;
-       mdelay(1);
-       err = i2c_smbus_read_byte(client);
-       if (err != TSL2550_POWER_UP) {
-               err = -ENODEV;
-               goto exit_kfree;
-       }
-
        mutex_init(&data->update_lock);
 
        /* Initialize the TSL2550 chip */
-       tsl2550_init_client(client);
+       err = tsl2550_init_client(client);
+       if (err)
+               goto exit_kfree;
 
        /* Register sysfs hooks */
        err = sysfs_create_group(&client->dev.kobj, &tsl2550_attr_group);
@@ -427,13 +436,41 @@ static int __devexit tsl2550_remove(struct i2c_client *client)
        return 0;
 }
 
+#ifdef CONFIG_PM
+
+static int tsl2550_suspend(struct i2c_client *client, pm_message_t mesg)
+{
+       return tsl2550_set_power_state(client, 0);
+}
+
+static int tsl2550_resume(struct i2c_client *client)
+{
+       return tsl2550_set_power_state(client, 1);
+}
+
+#else
+
+#define tsl2550_suspend                NULL
+#define tsl2550_resume         NULL
+
+#endif /* CONFIG_PM */
+
+static const struct i2c_device_id tsl2550_id[] = {
+       { "tsl2550", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, tsl2550_id);
+
 static struct i2c_driver tsl2550_driver = {
        .driver = {
                .name   = TSL2550_DRV_NAME,
                .owner  = THIS_MODULE,
        },
+       .suspend = tsl2550_suspend,
+       .resume = tsl2550_resume,
        .probe  = tsl2550_probe,
        .remove = __devexit_p(tsl2550_remove),
+       .id_table = tsl2550_id,
 };
 
 static int __init tsl2550_init(void)
@@ -449,6 +486,7 @@ static void __exit tsl2550_exit(void)
 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
 MODULE_DESCRIPTION("TSL2550 ambient light sensor driver");
 MODULE_LICENSE("GPL");
+MODULE_VERSION(DRIVER_VERSION);
 
 module_init(tsl2550_init);
 module_exit(tsl2550_exit);