Merge remote-tracking branch 'staging/staging-next'
[deliverable/linux.git] / drivers / iio / light / vcnl4000.c
index c9d85bbc9230059d4483567150b7838f01c65c54..360b6e98137acbfbd604e6492687f0b34f424de5 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * vcnl4000.c - Support for Vishay VCNL4000 combined ambient light and
- * proximity sensor
+ * vcnl4000.c - Support for Vishay VCNL4000/4010/4020 combined ambient
+ * light and proximity sensor
  *
  * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net>
  *
@@ -13,6 +13,8 @@
  * TODO:
  *   allow to adjust IR current
  *   proximity threshold and event handling
+ *   periodic ALS/proximity measurement (VCNL4010/20)
+ *   interrupts (VCNL4010/20)
  */
 
 #include <linux/module.h>
@@ -24,6 +26,8 @@
 #include <linux/iio/sysfs.h>
 
 #define VCNL4000_DRV_NAME "vcnl4000"
+#define VCNL4000_ID            0x01
+#define VCNL4010_ID            0x02 /* for VCNL4020, VCNL4010 */
 
 #define VCNL4000_COMMAND       0x80 /* Command register */
 #define VCNL4000_PROD_REV      0x81 /* Product ID and Revision ID */
 #define VCNL4000_PS_MOD_ADJ    0x8a /* Proximity modulator timing adjustment */
 
 /* Bit masks for COMMAND register */
-#define VCNL4000_AL_RDY                0x40 /* ALS data ready? */
-#define VCNL4000_PS_RDY                0x20 /* proximity data ready? */
-#define VCNL4000_AL_OD         0x10 /* start on-demand ALS measurement */
-#define VCNL4000_PS_OD         0x08 /* start on-demand proximity measurement */
+#define VCNL4000_AL_RDY                BIT(6) /* ALS data ready? */
+#define VCNL4000_PS_RDY                BIT(5) /* proximity data ready? */
+#define VCNL4000_AL_OD         BIT(4) /* start on-demand ALS measurement */
+#define VCNL4000_PS_OD         BIT(3) /* start on-demand proximity measurement */
 
 struct vcnl4000_data {
        struct i2c_client *client;
+       struct mutex lock;
 };
 
 static const struct i2c_device_id vcnl4000_id[] = {
@@ -59,16 +64,18 @@ static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
        __be16 buf;
        int ret;
 
+       mutex_lock(&data->lock);
+
        ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
                                        req_mask);
        if (ret < 0)
-               return ret;
+               goto fail;
 
        /* wait for data to become ready */
        while (tries--) {
                ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND);
                if (ret < 0)
-                       return ret;
+                       goto fail;
                if (ret & rdy_mask)
                        break;
                msleep(20); /* measurement takes up to 100 ms */
@@ -77,17 +84,23 @@ static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
        if (tries < 0) {
                dev_err(&data->client->dev,
                        "vcnl4000_measure() failed, data not ready\n");
-               return -EIO;
+               ret = -EIO;
+               goto fail;
        }
 
        ret = i2c_smbus_read_i2c_block_data(data->client,
                data_reg, sizeof(buf), (u8 *) &buf);
        if (ret < 0)
-               return ret;
+               goto fail;
 
+       mutex_unlock(&data->lock);
        *val = be16_to_cpu(buf);
 
        return 0;
+
+fail:
+       mutex_unlock(&data->lock);
+       return ret;
 }
 
 static const struct iio_chan_spec vcnl4000_channels[] = {
@@ -105,7 +118,7 @@ static int vcnl4000_read_raw(struct iio_dev *indio_dev,
                                struct iio_chan_spec const *chan,
                                int *val, int *val2, long mask)
 {
-       int ret = -EINVAL;
+       int ret;
        struct vcnl4000_data *data = iio_priv(indio_dev);
 
        switch (mask) {
@@ -117,32 +130,27 @@ static int vcnl4000_read_raw(struct iio_dev *indio_dev,
                                VCNL4000_AL_RESULT_HI, val);
                        if (ret < 0)
                                return ret;
-                       ret = IIO_VAL_INT;
-                       break;
+                       return IIO_VAL_INT;
                case IIO_PROXIMITY:
                        ret = vcnl4000_measure(data,
                                VCNL4000_PS_OD, VCNL4000_PS_RDY,
                                VCNL4000_PS_RESULT_HI, val);
                        if (ret < 0)
                                return ret;
-                       ret = IIO_VAL_INT;
-                       break;
+                       return IIO_VAL_INT;
                default:
-                       break;
+                       return -EINVAL;
                }
-               break;
        case IIO_CHAN_INFO_SCALE:
-               if (chan->type == IIO_LIGHT) {
-                       *val = 0;
-                       *val2 = 250000;
-                       ret = IIO_VAL_INT_PLUS_MICRO;
-               }
-               break;
+               if (chan->type != IIO_LIGHT)
+                       return -EINVAL;
+
+               *val = 0;
+               *val2 = 250000;
+               return IIO_VAL_INT_PLUS_MICRO;
        default:
-               break;
+               return -EINVAL;
        }
-
-       return ret;
 }
 
 static const struct iio_info vcnl4000_info = {
@@ -155,7 +163,7 @@ static int vcnl4000_probe(struct i2c_client *client,
 {
        struct vcnl4000_data *data;
        struct iio_dev *indio_dev;
-       int ret;
+       int ret, prod_id;
 
        indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
        if (!indio_dev)
@@ -164,13 +172,19 @@ static int vcnl4000_probe(struct i2c_client *client,
        data = iio_priv(indio_dev);
        i2c_set_clientdata(client, indio_dev);
        data->client = client;
+       mutex_init(&data->lock);
 
        ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV);
        if (ret < 0)
                return ret;
 
-       dev_info(&client->dev, "VCNL4000 Ambient light/proximity sensor, Prod %02x, Rev: %02x\n",
-               ret >> 4, ret & 0xf);
+       prod_id = ret >> 4;
+       if (prod_id != VCNL4010_ID && prod_id != VCNL4000_ID)
+               return -ENODEV;
+
+       dev_dbg(&client->dev, "%s Ambient light/proximity sensor, Rev: %02x\n",
+               (prod_id == VCNL4010_ID) ? "VCNL4010/4020" : "VCNL4000",
+               ret & 0xf);
 
        indio_dev->dev.parent = &client->dev;
        indio_dev->info = &vcnl4000_info;
This page took 0.027238 seconds and 5 git commands to generate.