1aab9ba7eSPeter Meerwald /* 2aab9ba7eSPeter Meerwald * tsl4531.c - Support for TAOS TSL4531 ambient light sensor 3aab9ba7eSPeter Meerwald * 4aab9ba7eSPeter Meerwald * Copyright 2013 Peter Meerwald <pmeerw@pmeerw.net> 5aab9ba7eSPeter Meerwald * 6aab9ba7eSPeter Meerwald * This file is subject to the terms and conditions of version 2 of 7aab9ba7eSPeter Meerwald * the GNU General Public License. See the file COPYING in the main 8aab9ba7eSPeter Meerwald * directory of this archive for more details. 9aab9ba7eSPeter Meerwald * 10aab9ba7eSPeter Meerwald * IIO driver for the TSL4531x family 11aab9ba7eSPeter Meerwald * TSL45311/TSL45313: 7-bit I2C slave address 0x39 12aab9ba7eSPeter Meerwald * TSL45315/TSL45317: 7-bit I2C slave address 0x29 13aab9ba7eSPeter Meerwald * 14aab9ba7eSPeter Meerwald * TODO: single cycle measurement 15aab9ba7eSPeter Meerwald */ 16aab9ba7eSPeter Meerwald 17aab9ba7eSPeter Meerwald #include <linux/module.h> 18aab9ba7eSPeter Meerwald #include <linux/i2c.h> 19aab9ba7eSPeter Meerwald #include <linux/err.h> 20aab9ba7eSPeter Meerwald #include <linux/delay.h> 21aab9ba7eSPeter Meerwald 22aab9ba7eSPeter Meerwald #include <linux/iio/iio.h> 23aab9ba7eSPeter Meerwald #include <linux/iio/sysfs.h> 24aab9ba7eSPeter Meerwald 25aab9ba7eSPeter Meerwald #define TSL4531_DRV_NAME "tsl4531" 26aab9ba7eSPeter Meerwald 276b57573bSPeter Meerwald #define TSL4531_COMMAND BIT(7) 28aab9ba7eSPeter Meerwald 296b57573bSPeter Meerwald #define TSL4531_CONTROL (TSL4531_COMMAND | 0x00) 306b57573bSPeter Meerwald #define TSL4531_CONFIG (TSL4531_COMMAND | 0x01) 316b57573bSPeter Meerwald #define TSL4531_DATA (TSL4531_COMMAND | 0x04) 326b57573bSPeter Meerwald #define TSL4531_ID (TSL4531_COMMAND | 0x0a) 33aab9ba7eSPeter Meerwald 34aab9ba7eSPeter Meerwald /* operating modes in control register */ 35aab9ba7eSPeter Meerwald #define TSL4531_MODE_POWERDOWN 0x00 36aab9ba7eSPeter Meerwald #define TSL4531_MODE_SINGLE_ADC 0x02 37aab9ba7eSPeter Meerwald #define TSL4531_MODE_NORMAL 0x03 38aab9ba7eSPeter Meerwald 39aab9ba7eSPeter Meerwald /* integration time control in config register */ 40aab9ba7eSPeter Meerwald #define TSL4531_TCNTRL_400MS 0x00 41aab9ba7eSPeter Meerwald #define TSL4531_TCNTRL_200MS 0x01 42aab9ba7eSPeter Meerwald #define TSL4531_TCNTRL_100MS 0x02 43aab9ba7eSPeter Meerwald 44aab9ba7eSPeter Meerwald /* part number in id register */ 45aab9ba7eSPeter Meerwald #define TSL45311_ID 0x8 46aab9ba7eSPeter Meerwald #define TSL45313_ID 0x9 47aab9ba7eSPeter Meerwald #define TSL45315_ID 0xa 48aab9ba7eSPeter Meerwald #define TSL45317_ID 0xb 49aab9ba7eSPeter Meerwald #define TSL4531_ID_SHIFT 4 50aab9ba7eSPeter Meerwald 51aab9ba7eSPeter Meerwald struct tsl4531_data { 52aab9ba7eSPeter Meerwald struct i2c_client *client; 53aab9ba7eSPeter Meerwald struct mutex lock; 54aab9ba7eSPeter Meerwald int int_time; 55aab9ba7eSPeter Meerwald }; 56aab9ba7eSPeter Meerwald 57aab9ba7eSPeter Meerwald static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.2 0.4"); 58aab9ba7eSPeter Meerwald 59aab9ba7eSPeter Meerwald static struct attribute *tsl4531_attributes[] = { 60aab9ba7eSPeter Meerwald &iio_const_attr_integration_time_available.dev_attr.attr, 61aab9ba7eSPeter Meerwald NULL 62aab9ba7eSPeter Meerwald }; 63aab9ba7eSPeter Meerwald 64aab9ba7eSPeter Meerwald static const struct attribute_group tsl4531_attribute_group = { 65aab9ba7eSPeter Meerwald .attrs = tsl4531_attributes, 66aab9ba7eSPeter Meerwald }; 67aab9ba7eSPeter Meerwald 68aab9ba7eSPeter Meerwald static const struct iio_chan_spec tsl4531_channels[] = { 69aab9ba7eSPeter Meerwald { 70aab9ba7eSPeter Meerwald .type = IIO_LIGHT, 71aab9ba7eSPeter Meerwald .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 72aab9ba7eSPeter Meerwald BIT(IIO_CHAN_INFO_SCALE) | 73aab9ba7eSPeter Meerwald BIT(IIO_CHAN_INFO_INT_TIME) 74aab9ba7eSPeter Meerwald } 75aab9ba7eSPeter Meerwald }; 76aab9ba7eSPeter Meerwald 77aab9ba7eSPeter Meerwald static int tsl4531_read_raw(struct iio_dev *indio_dev, 78aab9ba7eSPeter Meerwald struct iio_chan_spec const *chan, 79aab9ba7eSPeter Meerwald int *val, int *val2, long mask) 80aab9ba7eSPeter Meerwald { 81aab9ba7eSPeter Meerwald struct tsl4531_data *data = iio_priv(indio_dev); 82aab9ba7eSPeter Meerwald int ret; 83aab9ba7eSPeter Meerwald 84aab9ba7eSPeter Meerwald switch (mask) { 85aab9ba7eSPeter Meerwald case IIO_CHAN_INFO_RAW: 86aab9ba7eSPeter Meerwald ret = i2c_smbus_read_word_data(data->client, 87aab9ba7eSPeter Meerwald TSL4531_DATA); 88aab9ba7eSPeter Meerwald if (ret < 0) 89aab9ba7eSPeter Meerwald return ret; 90aab9ba7eSPeter Meerwald *val = ret; 91aab9ba7eSPeter Meerwald return IIO_VAL_INT; 92aab9ba7eSPeter Meerwald case IIO_CHAN_INFO_SCALE: 93aab9ba7eSPeter Meerwald /* 0.. 1x, 1 .. 2x, 2 .. 4x */ 94aab9ba7eSPeter Meerwald *val = 1 << data->int_time; 95aab9ba7eSPeter Meerwald return IIO_VAL_INT; 96aab9ba7eSPeter Meerwald case IIO_CHAN_INFO_INT_TIME: 97aab9ba7eSPeter Meerwald if (data->int_time == 0) 98aab9ba7eSPeter Meerwald *val2 = 400000; 99aab9ba7eSPeter Meerwald else if (data->int_time == 1) 100aab9ba7eSPeter Meerwald *val2 = 200000; 101aab9ba7eSPeter Meerwald else if (data->int_time == 2) 102aab9ba7eSPeter Meerwald *val2 = 100000; 103aab9ba7eSPeter Meerwald else 104aab9ba7eSPeter Meerwald return -EINVAL; 105aab9ba7eSPeter Meerwald *val = 0; 106aab9ba7eSPeter Meerwald return IIO_VAL_INT_PLUS_MICRO; 107aab9ba7eSPeter Meerwald default: 108aab9ba7eSPeter Meerwald return -EINVAL; 109aab9ba7eSPeter Meerwald } 110aab9ba7eSPeter Meerwald } 111aab9ba7eSPeter Meerwald 112aab9ba7eSPeter Meerwald static int tsl4531_write_raw(struct iio_dev *indio_dev, 113aab9ba7eSPeter Meerwald struct iio_chan_spec const *chan, 114aab9ba7eSPeter Meerwald int val, int val2, long mask) 115aab9ba7eSPeter Meerwald { 116aab9ba7eSPeter Meerwald struct tsl4531_data *data = iio_priv(indio_dev); 117aab9ba7eSPeter Meerwald int int_time, ret; 118aab9ba7eSPeter Meerwald 119aab9ba7eSPeter Meerwald switch (mask) { 120aab9ba7eSPeter Meerwald case IIO_CHAN_INFO_INT_TIME: 121aab9ba7eSPeter Meerwald if (val != 0) 122aab9ba7eSPeter Meerwald return -EINVAL; 123aab9ba7eSPeter Meerwald if (val2 == 400000) 124aab9ba7eSPeter Meerwald int_time = 0; 125aab9ba7eSPeter Meerwald else if (val2 == 200000) 126aab9ba7eSPeter Meerwald int_time = 1; 127aab9ba7eSPeter Meerwald else if (val2 == 100000) 128aab9ba7eSPeter Meerwald int_time = 2; 129aab9ba7eSPeter Meerwald else 130aab9ba7eSPeter Meerwald return -EINVAL; 131aab9ba7eSPeter Meerwald mutex_lock(&data->lock); 132aab9ba7eSPeter Meerwald ret = i2c_smbus_write_byte_data(data->client, 133aab9ba7eSPeter Meerwald TSL4531_CONFIG, int_time); 134aab9ba7eSPeter Meerwald if (ret >= 0) 135aab9ba7eSPeter Meerwald data->int_time = int_time; 136aab9ba7eSPeter Meerwald mutex_unlock(&data->lock); 137aab9ba7eSPeter Meerwald return ret; 138aab9ba7eSPeter Meerwald default: 139aab9ba7eSPeter Meerwald return -EINVAL; 140aab9ba7eSPeter Meerwald } 141aab9ba7eSPeter Meerwald } 142aab9ba7eSPeter Meerwald 143aab9ba7eSPeter Meerwald static const struct iio_info tsl4531_info = { 144aab9ba7eSPeter Meerwald .read_raw = tsl4531_read_raw, 145aab9ba7eSPeter Meerwald .write_raw = tsl4531_write_raw, 146aab9ba7eSPeter Meerwald .attrs = &tsl4531_attribute_group, 147aab9ba7eSPeter Meerwald .driver_module = THIS_MODULE, 148aab9ba7eSPeter Meerwald }; 149aab9ba7eSPeter Meerwald 150aab9ba7eSPeter Meerwald static int tsl4531_check_id(struct i2c_client *client) 151aab9ba7eSPeter Meerwald { 152aab9ba7eSPeter Meerwald int ret = i2c_smbus_read_byte_data(client, TSL4531_ID); 153aab9ba7eSPeter Meerwald if (ret < 0) 154aab9ba7eSPeter Meerwald return ret; 155aab9ba7eSPeter Meerwald 156aab9ba7eSPeter Meerwald switch (ret >> TSL4531_ID_SHIFT) { 157aab9ba7eSPeter Meerwald case TSL45311_ID: 158aab9ba7eSPeter Meerwald case TSL45313_ID: 159aab9ba7eSPeter Meerwald case TSL45315_ID: 160aab9ba7eSPeter Meerwald case TSL45317_ID: 161aab9ba7eSPeter Meerwald return 0; 162*7d87b3c5SDan Carpenter default: 163*7d87b3c5SDan Carpenter return -ENODEV; 164aab9ba7eSPeter Meerwald } 165aab9ba7eSPeter Meerwald } 166aab9ba7eSPeter Meerwald 167aab9ba7eSPeter Meerwald static int tsl4531_probe(struct i2c_client *client, 168aab9ba7eSPeter Meerwald const struct i2c_device_id *id) 169aab9ba7eSPeter Meerwald { 170aab9ba7eSPeter Meerwald struct tsl4531_data *data; 171aab9ba7eSPeter Meerwald struct iio_dev *indio_dev; 172aab9ba7eSPeter Meerwald int ret; 173aab9ba7eSPeter Meerwald 174aab9ba7eSPeter Meerwald indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 175aab9ba7eSPeter Meerwald if (!indio_dev) 176aab9ba7eSPeter Meerwald return -ENOMEM; 177aab9ba7eSPeter Meerwald 178aab9ba7eSPeter Meerwald data = iio_priv(indio_dev); 179aab9ba7eSPeter Meerwald i2c_set_clientdata(client, indio_dev); 180aab9ba7eSPeter Meerwald data->client = client; 181aab9ba7eSPeter Meerwald mutex_init(&data->lock); 182aab9ba7eSPeter Meerwald 183*7d87b3c5SDan Carpenter ret = tsl4531_check_id(client); 184*7d87b3c5SDan Carpenter if (ret) { 185aab9ba7eSPeter Meerwald dev_err(&client->dev, "no TSL4531 sensor\n"); 186*7d87b3c5SDan Carpenter return ret; 187aab9ba7eSPeter Meerwald } 188aab9ba7eSPeter Meerwald 189aab9ba7eSPeter Meerwald ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONTROL, 190aab9ba7eSPeter Meerwald TSL4531_MODE_NORMAL); 191aab9ba7eSPeter Meerwald if (ret < 0) 192aab9ba7eSPeter Meerwald return ret; 193aab9ba7eSPeter Meerwald 194aab9ba7eSPeter Meerwald ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONFIG, 195aab9ba7eSPeter Meerwald TSL4531_TCNTRL_400MS); 196aab9ba7eSPeter Meerwald if (ret < 0) 197aab9ba7eSPeter Meerwald return ret; 198aab9ba7eSPeter Meerwald 199aab9ba7eSPeter Meerwald indio_dev->dev.parent = &client->dev; 200aab9ba7eSPeter Meerwald indio_dev->info = &tsl4531_info; 201aab9ba7eSPeter Meerwald indio_dev->channels = tsl4531_channels; 202aab9ba7eSPeter Meerwald indio_dev->num_channels = ARRAY_SIZE(tsl4531_channels); 203aab9ba7eSPeter Meerwald indio_dev->name = TSL4531_DRV_NAME; 204aab9ba7eSPeter Meerwald indio_dev->modes = INDIO_DIRECT_MODE; 205aab9ba7eSPeter Meerwald 206aab9ba7eSPeter Meerwald return iio_device_register(indio_dev); 207aab9ba7eSPeter Meerwald } 208aab9ba7eSPeter Meerwald 209aab9ba7eSPeter Meerwald static int tsl4531_powerdown(struct i2c_client *client) 210aab9ba7eSPeter Meerwald { 211aab9ba7eSPeter Meerwald return i2c_smbus_write_byte_data(client, TSL4531_CONTROL, 212aab9ba7eSPeter Meerwald TSL4531_MODE_POWERDOWN); 213aab9ba7eSPeter Meerwald } 214aab9ba7eSPeter Meerwald 215aab9ba7eSPeter Meerwald static int tsl4531_remove(struct i2c_client *client) 216aab9ba7eSPeter Meerwald { 217aab9ba7eSPeter Meerwald iio_device_unregister(i2c_get_clientdata(client)); 218aab9ba7eSPeter Meerwald tsl4531_powerdown(client); 219aab9ba7eSPeter Meerwald 220aab9ba7eSPeter Meerwald return 0; 221aab9ba7eSPeter Meerwald } 222aab9ba7eSPeter Meerwald 223aab9ba7eSPeter Meerwald #ifdef CONFIG_PM_SLEEP 224aab9ba7eSPeter Meerwald static int tsl4531_suspend(struct device *dev) 225aab9ba7eSPeter Meerwald { 226aab9ba7eSPeter Meerwald return tsl4531_powerdown(to_i2c_client(dev)); 227aab9ba7eSPeter Meerwald } 228aab9ba7eSPeter Meerwald 229aab9ba7eSPeter Meerwald static int tsl4531_resume(struct device *dev) 230aab9ba7eSPeter Meerwald { 231aab9ba7eSPeter Meerwald return i2c_smbus_write_byte_data(to_i2c_client(dev), TSL4531_CONTROL, 232aab9ba7eSPeter Meerwald TSL4531_MODE_NORMAL); 233aab9ba7eSPeter Meerwald } 234aab9ba7eSPeter Meerwald 235aab9ba7eSPeter Meerwald static SIMPLE_DEV_PM_OPS(tsl4531_pm_ops, tsl4531_suspend, tsl4531_resume); 23603045bcfSDan Murphy #define TSL4531_PM_OPS (&tsl4531_pm_ops) 23703045bcfSDan Murphy #else 23803045bcfSDan Murphy #define TSL4531_PM_OPS NULL 23903045bcfSDan Murphy #endif 240aab9ba7eSPeter Meerwald 241aab9ba7eSPeter Meerwald static const struct i2c_device_id tsl4531_id[] = { 242aab9ba7eSPeter Meerwald { "tsl4531", 0 }, 243aab9ba7eSPeter Meerwald { } 244aab9ba7eSPeter Meerwald }; 245aab9ba7eSPeter Meerwald MODULE_DEVICE_TABLE(i2c, tsl4531_id); 246aab9ba7eSPeter Meerwald 247aab9ba7eSPeter Meerwald static struct i2c_driver tsl4531_driver = { 248aab9ba7eSPeter Meerwald .driver = { 249aab9ba7eSPeter Meerwald .name = TSL4531_DRV_NAME, 25003045bcfSDan Murphy .pm = TSL4531_PM_OPS, 251aab9ba7eSPeter Meerwald }, 252aab9ba7eSPeter Meerwald .probe = tsl4531_probe, 253aab9ba7eSPeter Meerwald .remove = tsl4531_remove, 254aab9ba7eSPeter Meerwald .id_table = tsl4531_id, 255aab9ba7eSPeter Meerwald }; 256aab9ba7eSPeter Meerwald 257aab9ba7eSPeter Meerwald module_i2c_driver(tsl4531_driver); 258aab9ba7eSPeter Meerwald 259aab9ba7eSPeter Meerwald MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 260aab9ba7eSPeter Meerwald MODULE_DESCRIPTION("TAOS TSL4531 ambient light sensors driver"); 261aab9ba7eSPeter Meerwald MODULE_LICENSE("GPL"); 262