Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * tsl4531.c - Support for TAOS TSL4531 ambient light sensor
0004  *
0005  * Copyright 2013 Peter Meerwald <pmeerw@pmeerw.net>
0006  *
0007  * IIO driver for the TSL4531x family
0008  *   TSL45311/TSL45313: 7-bit I2C slave address 0x39
0009  *   TSL45315/TSL45317: 7-bit I2C slave address 0x29
0010  *
0011  * TODO: single cycle measurement
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/i2c.h>
0016 #include <linux/err.h>
0017 #include <linux/delay.h>
0018 
0019 #include <linux/iio/iio.h>
0020 #include <linux/iio/sysfs.h>
0021 
0022 #define TSL4531_DRV_NAME "tsl4531"
0023 
0024 #define TSL4531_COMMAND BIT(7)
0025 
0026 #define TSL4531_CONTROL (TSL4531_COMMAND | 0x00)
0027 #define TSL4531_CONFIG (TSL4531_COMMAND | 0x01)
0028 #define TSL4531_DATA (TSL4531_COMMAND | 0x04)
0029 #define TSL4531_ID (TSL4531_COMMAND | 0x0a)
0030 
0031 /* operating modes in control register */
0032 #define TSL4531_MODE_POWERDOWN 0x00
0033 #define TSL4531_MODE_SINGLE_ADC 0x02
0034 #define TSL4531_MODE_NORMAL 0x03
0035 
0036 /* integration time control in config register */
0037 #define TSL4531_TCNTRL_400MS 0x00
0038 #define TSL4531_TCNTRL_200MS 0x01
0039 #define TSL4531_TCNTRL_100MS 0x02
0040 
0041 /* part number in id register */
0042 #define TSL45311_ID 0x8
0043 #define TSL45313_ID 0x9
0044 #define TSL45315_ID 0xa
0045 #define TSL45317_ID 0xb
0046 #define TSL4531_ID_SHIFT 4
0047 
0048 struct tsl4531_data {
0049     struct i2c_client *client;
0050     struct mutex lock;
0051     int int_time;
0052 };
0053 
0054 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.2 0.4");
0055 
0056 static struct attribute *tsl4531_attributes[] = {
0057     &iio_const_attr_integration_time_available.dev_attr.attr,
0058     NULL
0059 };
0060 
0061 static const struct attribute_group tsl4531_attribute_group = {
0062     .attrs = tsl4531_attributes,
0063 };
0064 
0065 static const struct iio_chan_spec tsl4531_channels[] = {
0066     {
0067         .type = IIO_LIGHT,
0068         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0069             BIT(IIO_CHAN_INFO_SCALE) |
0070             BIT(IIO_CHAN_INFO_INT_TIME)
0071     }
0072 };
0073 
0074 static int tsl4531_read_raw(struct iio_dev *indio_dev,
0075                 struct iio_chan_spec const *chan,
0076                 int *val, int *val2, long mask)
0077 {
0078     struct tsl4531_data *data = iio_priv(indio_dev);
0079     int ret;
0080 
0081     switch (mask) {
0082     case IIO_CHAN_INFO_RAW:
0083         ret = i2c_smbus_read_word_data(data->client,
0084             TSL4531_DATA);
0085         if (ret < 0)
0086             return ret;
0087         *val = ret;
0088         return IIO_VAL_INT;
0089     case IIO_CHAN_INFO_SCALE:
0090         /* 0.. 1x, 1 .. 2x, 2 .. 4x */
0091         *val = 1 << data->int_time;
0092         return IIO_VAL_INT;
0093     case IIO_CHAN_INFO_INT_TIME:
0094         if (data->int_time == 0)
0095             *val2 = 400000;
0096         else if (data->int_time == 1)
0097             *val2 = 200000;
0098         else if (data->int_time == 2)
0099             *val2 = 100000;
0100         else
0101             return -EINVAL;
0102         *val = 0;
0103         return IIO_VAL_INT_PLUS_MICRO;
0104     default:
0105         return -EINVAL;
0106     }
0107 }
0108 
0109 static int tsl4531_write_raw(struct iio_dev *indio_dev,
0110                  struct iio_chan_spec const *chan,
0111                  int val, int val2, long mask)
0112 {
0113     struct tsl4531_data *data = iio_priv(indio_dev);
0114     int int_time, ret;
0115 
0116     switch (mask) {
0117     case IIO_CHAN_INFO_INT_TIME:
0118         if (val != 0)
0119             return -EINVAL;
0120         if (val2 == 400000)
0121             int_time = 0;
0122         else if (val2 == 200000)
0123             int_time = 1;
0124         else if (val2 == 100000)
0125             int_time = 2;
0126         else
0127             return -EINVAL;
0128         mutex_lock(&data->lock);
0129         ret = i2c_smbus_write_byte_data(data->client,
0130             TSL4531_CONFIG, int_time);
0131         if (ret >= 0)
0132             data->int_time = int_time;
0133         mutex_unlock(&data->lock);
0134         return ret;
0135     default:
0136         return -EINVAL;
0137     }
0138 }
0139 
0140 static const struct iio_info tsl4531_info = {
0141     .read_raw = tsl4531_read_raw,
0142     .write_raw = tsl4531_write_raw,
0143     .attrs = &tsl4531_attribute_group,
0144 };
0145 
0146 static int tsl4531_check_id(struct i2c_client *client)
0147 {
0148     int ret = i2c_smbus_read_byte_data(client, TSL4531_ID);
0149     if (ret < 0)
0150         return ret;
0151 
0152     switch (ret >> TSL4531_ID_SHIFT) {
0153     case TSL45311_ID:
0154     case TSL45313_ID:
0155     case TSL45315_ID:
0156     case TSL45317_ID:
0157         return 0;
0158     default:
0159         return -ENODEV;
0160     }
0161 }
0162 
0163 static int tsl4531_probe(struct i2c_client *client,
0164               const struct i2c_device_id *id)
0165 {
0166     struct tsl4531_data *data;
0167     struct iio_dev *indio_dev;
0168     int ret;
0169 
0170     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
0171     if (!indio_dev)
0172         return -ENOMEM;
0173 
0174     data = iio_priv(indio_dev);
0175     i2c_set_clientdata(client, indio_dev);
0176     data->client = client;
0177     mutex_init(&data->lock);
0178 
0179     ret = tsl4531_check_id(client);
0180     if (ret) {
0181         dev_err(&client->dev, "no TSL4531 sensor\n");
0182         return ret;
0183     }
0184 
0185     ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONTROL,
0186         TSL4531_MODE_NORMAL);
0187     if (ret < 0)
0188         return ret;
0189 
0190     ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONFIG,
0191         TSL4531_TCNTRL_400MS);
0192     if (ret < 0)
0193         return ret;
0194 
0195     indio_dev->info = &tsl4531_info;
0196     indio_dev->channels = tsl4531_channels;
0197     indio_dev->num_channels = ARRAY_SIZE(tsl4531_channels);
0198     indio_dev->name = TSL4531_DRV_NAME;
0199     indio_dev->modes = INDIO_DIRECT_MODE;
0200 
0201     return iio_device_register(indio_dev);
0202 }
0203 
0204 static int tsl4531_powerdown(struct i2c_client *client)
0205 {
0206     return i2c_smbus_write_byte_data(client, TSL4531_CONTROL,
0207         TSL4531_MODE_POWERDOWN);
0208 }
0209 
0210 static int tsl4531_remove(struct i2c_client *client)
0211 {
0212     iio_device_unregister(i2c_get_clientdata(client));
0213     tsl4531_powerdown(client);
0214 
0215     return 0;
0216 }
0217 
0218 static int tsl4531_suspend(struct device *dev)
0219 {
0220     return tsl4531_powerdown(to_i2c_client(dev));
0221 }
0222 
0223 static int tsl4531_resume(struct device *dev)
0224 {
0225     return i2c_smbus_write_byte_data(to_i2c_client(dev), TSL4531_CONTROL,
0226         TSL4531_MODE_NORMAL);
0227 }
0228 
0229 static DEFINE_SIMPLE_DEV_PM_OPS(tsl4531_pm_ops, tsl4531_suspend,
0230                 tsl4531_resume);
0231 
0232 static const struct i2c_device_id tsl4531_id[] = {
0233     { "tsl4531", 0 },
0234     { }
0235 };
0236 MODULE_DEVICE_TABLE(i2c, tsl4531_id);
0237 
0238 static struct i2c_driver tsl4531_driver = {
0239     .driver = {
0240         .name   = TSL4531_DRV_NAME,
0241         .pm = pm_sleep_ptr(&tsl4531_pm_ops),
0242     },
0243     .probe  = tsl4531_probe,
0244     .remove = tsl4531_remove,
0245     .id_table = tsl4531_id,
0246 };
0247 
0248 module_i2c_driver(tsl4531_driver);
0249 
0250 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
0251 MODULE_DESCRIPTION("TAOS TSL4531 ambient light sensors driver");
0252 MODULE_LICENSE("GPL");