* [PATCH 0/3] iio: new color light sensor drivers
@ 2014-07-05 12:38 Peter Meerwald
2014-07-05 12:38 ` [PATCH 1/3] iio:tcs3472: Check for buffer enabled and locking Peter Meerwald
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Peter Meerwald @ 2014-07-05 12:38 UTC (permalink / raw)
To: linux-iio; +Cc: jic23
first patch is a cleanup/bug fix (locking around dependent
I2C communication)
patch 2 and 3 add new drivers for two digital color sensors
(Intersil isl29125 and AMS/TAOS TCS3414)
Peter Meerwald (3):
iio:tcs3472: Check for buffer enabled and locking
iio: Add Intersil isl29125 digital color light sensor driver
iio: Add driver for AMS/TAOS tcs3414 digital color sensor
drivers/iio/light/Kconfig | 24 +++
drivers/iio/light/Makefile | 2 +
drivers/iio/light/isl29125.c | 347 ++++++++++++++++++++++++++++++++++++
drivers/iio/light/tcs3414.c | 405 +++++++++++++++++++++++++++++++++++++++++++
drivers/iio/light/tcs3472.c | 11 +-
5 files changed, 788 insertions(+), 1 deletion(-)
create mode 100644 drivers/iio/light/isl29125.c
create mode 100644 drivers/iio/light/tcs3414.c
--
1.9.1
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 1/3] iio:tcs3472: Check for buffer enabled and locking 2014-07-05 12:38 [PATCH 0/3] iio: new color light sensor drivers Peter Meerwald @ 2014-07-05 12:38 ` Peter Meerwald 2014-07-07 8:00 ` Jonathan Cameron 2014-07-05 12:38 ` [PATCH 2/3] iio: Add Intersil isl29125 digital color light sensor driver Peter Meerwald 2014-07-05 12:38 ` [PATCH 3/3] iio: Add driver for AMS/TAOS tcs3414 digital color sensor Peter Meerwald 2 siblings, 1 reply; 7+ messages in thread From: Peter Meerwald @ 2014-07-05 12:38 UTC (permalink / raw) To: linux-iio; +Cc: jic23, Peter Meerwald From: Peter Meerwald <p.meerwald@bct-electronic.com> Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> --- drivers/iio/light/tcs3472.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/iio/light/tcs3472.c b/drivers/iio/light/tcs3472.c index fe063a0..7525699 100644 --- a/drivers/iio/light/tcs3472.c +++ b/drivers/iio/light/tcs3472.c @@ -52,6 +52,7 @@ struct tcs3472_data { struct i2c_client *client; + struct mutex lock; u8 enable; u8 control; u8 atime; @@ -116,10 +117,17 @@ static int tcs3472_read_raw(struct iio_dev *indio_dev, switch (mask) { case IIO_CHAN_INFO_RAW: + if (iio_buffer_enabled(indio_dev)) + return -EBUSY; + + mutex_lock(&data->lock); ret = tcs3472_req_data(data); - if (ret < 0) + if (ret < 0) { + mutex_unlock(&data->lock); return ret; + } ret = i2c_smbus_read_word_data(data->client, chan->address); + mutex_unlock(&data->lock); if (ret < 0) return ret; *val = ret; @@ -255,6 +263,7 @@ static int tcs3472_probe(struct i2c_client *client, data = iio_priv(indio_dev); i2c_set_clientdata(client, indio_dev); data->client = client; + mutex_init(&data->lock); indio_dev->dev.parent = &client->dev; indio_dev->info = &tcs3472_info; -- 1.9.1 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/3] iio:tcs3472: Check for buffer enabled and locking 2014-07-05 12:38 ` [PATCH 1/3] iio:tcs3472: Check for buffer enabled and locking Peter Meerwald @ 2014-07-07 8:00 ` Jonathan Cameron 0 siblings, 0 replies; 7+ messages in thread From: Jonathan Cameron @ 2014-07-07 8:00 UTC (permalink / raw) To: Peter Meerwald, linux-iio On 05/07/14 13:38, Peter Meerwald wrote: > From: Peter Meerwald <p.meerwald@bct-electronic.com> > > Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> Applied to the fixes-togreg branch of iio.git Thanks Peter, > --- > drivers/iio/light/tcs3472.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/drivers/iio/light/tcs3472.c b/drivers/iio/light/tcs3472.c > index fe063a0..7525699 100644 > --- a/drivers/iio/light/tcs3472.c > +++ b/drivers/iio/light/tcs3472.c > @@ -52,6 +52,7 @@ > > struct tcs3472_data { > struct i2c_client *client; > + struct mutex lock; > u8 enable; > u8 control; > u8 atime; > @@ -116,10 +117,17 @@ static int tcs3472_read_raw(struct iio_dev *indio_dev, > > switch (mask) { > case IIO_CHAN_INFO_RAW: > + if (iio_buffer_enabled(indio_dev)) > + return -EBUSY; > + > + mutex_lock(&data->lock); > ret = tcs3472_req_data(data); > - if (ret < 0) > + if (ret < 0) { > + mutex_unlock(&data->lock); > return ret; > + } > ret = i2c_smbus_read_word_data(data->client, chan->address); > + mutex_unlock(&data->lock); > if (ret < 0) > return ret; > *val = ret; > @@ -255,6 +263,7 @@ static int tcs3472_probe(struct i2c_client *client, > data = iio_priv(indio_dev); > i2c_set_clientdata(client, indio_dev); > data->client = client; > + mutex_init(&data->lock); > > indio_dev->dev.parent = &client->dev; > indio_dev->info = &tcs3472_info; > ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/3] iio: Add Intersil isl29125 digital color light sensor driver 2014-07-05 12:38 [PATCH 0/3] iio: new color light sensor drivers Peter Meerwald 2014-07-05 12:38 ` [PATCH 1/3] iio:tcs3472: Check for buffer enabled and locking Peter Meerwald @ 2014-07-05 12:38 ` Peter Meerwald 2014-07-07 8:15 ` Jonathan Cameron 2014-07-05 12:38 ` [PATCH 3/3] iio: Add driver for AMS/TAOS tcs3414 digital color sensor Peter Meerwald 2 siblings, 1 reply; 7+ messages in thread From: Peter Meerwald @ 2014-07-05 12:38 UTC (permalink / raw) To: linux-iio; +Cc: jic23, Peter Meerwald datasheet: http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29125.pdf Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> --- drivers/iio/light/Kconfig | 12 ++ drivers/iio/light/Makefile | 1 + drivers/iio/light/isl29125.c | 347 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 360 insertions(+) create mode 100644 drivers/iio/light/isl29125.c diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig index c89740d..7d83dca 100644 --- a/drivers/iio/light/Kconfig +++ b/drivers/iio/light/Kconfig @@ -62,6 +62,18 @@ config GP2AP020A00F To compile this driver as a module, choose M here: the module will be called gp2ap020a00f. +config ISL29125 + tristate "Intersil ISL29125 digital color light sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + help + Say Y here if you want to build a driver for the Intersil ISL29125 + RGB light sensor for I2C. + + To compile this driver as a module, choose M here: the module will be + called isl29125. + config HID_SENSOR_ALS depends on HID_SENSOR_HUB select IIO_BUFFER diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile index 3eb36e5..f3d1857 100644 --- a/drivers/iio/light/Makefile +++ b/drivers/iio/light/Makefile @@ -10,6 +10,7 @@ obj-$(CONFIG_CM36651) += cm36651.o obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o +obj-$(CONFIG_ISL29125) += isl29125.o obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.o obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o diff --git a/drivers/iio/light/isl29125.c b/drivers/iio/light/isl29125.c new file mode 100644 index 0000000..c82f4a6 --- /dev/null +++ b/drivers/iio/light/isl29125.c @@ -0,0 +1,347 @@ +/* + * isl29125.c - Support for Intersil ISL29125 RGB light sensor + * + * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net> + * + * This file is subject to the terms and conditions of version 2 of + * the GNU General Public License. See the file COPYING in the main + * directory of this archive for more details. + * + * RGB light sensor with 16-bit channels for red, green, blue); + * 7-bit I2C slave address 0x44 + * + * TODO: interrupt support, IR compensation, thresholds, 12bit + */ + +#include <linux/module.h> +#include <linux/i2c.h> +#include <linux/delay.h> +#include <linux/pm.h> + +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/buffer.h> +#include <linux/iio/triggered_buffer.h> + +#define ISL29125_DRV_NAME "isl29125" + +#define ISL29125_DEVICE_ID 0x00 +#define ISL29125_CONF1 0x01 +#define ISL29125_CONF2 0x02 +#define ISL29125_CONF3 0x03 +#define ISL29125_STATUS 0x08 +#define ISL29125_GREEN_DATA 0x09 +#define ISL29125_RED_DATA 0x0b +#define ISL29125_BLUE_DATA 0x0d + +#define ISL29125_ID 0x7d + +#define ISL29125_MODE_MASK GENMASK(2, 0) +#define ISL29125_MODE_PD 0x0 +#define ISL29125_MODE_G 0x1 +#define ISL29125_MODE_R 0x2 +#define ISL29125_MODE_B 0x3 +#define ISL29125_MODE_RGB 0x5 + +#define ISL29125_MODE_RANGE BIT(3) + +#define ISL29125_STATUS_CONV BIT(1) + +struct isl29125_data { + struct i2c_client *client; + struct mutex lock; + u8 conf1; + u16 buffer[8]; /* 3x 16-bit, padding, 8 bytes timestamp */ +}; + +#define ISL29125_CHANNEL(_color, _si) { \ + .type = IIO_INTENSITY, \ + .modified = 1, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .channel2 = IIO_MOD_LIGHT_##_color, \ + .scan_index = _si, \ + .scan_type = { \ + .sign = 'u', \ + .realbits = 16, \ + .storagebits = 16, \ + .endianness = IIO_CPU, \ + }, \ +} + +static const struct iio_chan_spec isl29125_channels[] = { + ISL29125_CHANNEL(GREEN, 0), + ISL29125_CHANNEL(RED, 1), + ISL29125_CHANNEL(BLUE, 2), + IIO_CHAN_SOFT_TIMESTAMP(3), +}; + +static const struct { + u8 mode, data; +} isl29125_regs[] = { + {ISL29125_MODE_G, ISL29125_GREEN_DATA}, + {ISL29125_MODE_R, ISL29125_RED_DATA}, + {ISL29125_MODE_B, ISL29125_BLUE_DATA}, +}; + +static int isl29125_read_data(struct isl29125_data *data, int si) +{ + int tries = 5; + int ret; + + ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, + data->conf1 | isl29125_regs[si].mode); + if (ret < 0) + return ret; + + msleep(101); + + while (tries--) { + ret = i2c_smbus_read_byte_data(data->client, ISL29125_STATUS); + if (ret < 0) + goto fail; + if (ret & ISL29125_STATUS_CONV) + break; + msleep(20); + } + + if (tries < 0) { + dev_err(&data->client->dev, "data not ready\n"); + ret = -EIO; + goto fail; + } + + ret = i2c_smbus_read_word_data(data->client, isl29125_regs[si].data); + +fail: + i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, data->conf1); + return ret; +} + +static int isl29125_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct isl29125_data *data = iio_priv(indio_dev); + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + if (iio_buffer_enabled(indio_dev)) + return -EBUSY; + mutex_lock(&data->lock); + ret = isl29125_read_data(data, chan->scan_index); + mutex_unlock(&data->lock); + if (ret < 0) + return ret; + *val = ret; + return IIO_VAL_INT; + case IIO_CHAN_INFO_SCALE: + *val = 0; + if (data->conf1 & ISL29125_MODE_RANGE) + *val2 = 152590; /* 10k lux full range */ + else + *val2 = 5722; /* 375 lux full range */ + return IIO_VAL_INT_PLUS_MICRO; + } + return -EINVAL; +} + +static int isl29125_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct isl29125_data *data = iio_priv(indio_dev); + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + if (val != 0) + return -EINVAL; + if (val2 == 152590) + data->conf1 |= ISL29125_MODE_RANGE; + else if (val2 == 5722) + data->conf1 &= ~ISL29125_MODE_RANGE; + else + return -EINVAL; + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, + data->conf1); + default: + return -EINVAL; + } +} + +static irqreturn_t isl29125_trigger_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct isl29125_data *data = iio_priv(indio_dev); + int i, j = 0; + + for_each_set_bit(i, indio_dev->active_scan_mask, + indio_dev->masklength) { + int ret = i2c_smbus_read_word_data(data->client, + isl29125_regs[i].data); + if (ret < 0) + goto done; + + data->buffer[j++] = ret; + } + + iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, + iio_get_time_ns()); + +done: + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + +static const struct iio_info isl29125_info = { + .read_raw = isl29125_read_raw, + .write_raw = isl29125_write_raw, + .driver_module = THIS_MODULE, +}; + +static int isl29125_buffer_preenable(struct iio_dev *indio_dev) +{ + struct isl29125_data *data = iio_priv(indio_dev); + + data->conf1 |= ISL29125_MODE_RGB; + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, + data->conf1); +} + +static int isl29125_buffer_predisable(struct iio_dev *indio_dev) +{ + struct isl29125_data *data = iio_priv(indio_dev); + int ret; + + ret = iio_triggered_buffer_predisable(indio_dev); + if (ret < 0) + return ret; + + data->conf1 &= ~ISL29125_MODE_MASK; + data->conf1 |= ISL29125_MODE_PD; + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, + data->conf1); +} + +static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops = { + .preenable = isl29125_buffer_preenable, + .postenable = &iio_triggered_buffer_postenable, + .predisable = isl29125_buffer_predisable, +}; + +static int isl29125_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct isl29125_data *data; + struct iio_dev *indio_dev; + int ret; + + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); + if (indio_dev == NULL) + return -ENOMEM; + + data = iio_priv(indio_dev); + i2c_set_clientdata(client, indio_dev); + data->client = client; + mutex_init(&data->lock); + + indio_dev->dev.parent = &client->dev; + indio_dev->info = &isl29125_info; + indio_dev->name = ISL29125_DRV_NAME; + indio_dev->channels = isl29125_channels; + indio_dev->num_channels = ARRAY_SIZE(isl29125_channels); + indio_dev->modes = INDIO_DIRECT_MODE; + + ret = i2c_smbus_read_byte_data(data->client, ISL29125_DEVICE_ID); + if (ret < 0) + return ret; + if (ret != ISL29125_ID) + return -ENODEV; + + data->conf1 = ISL29125_MODE_PD | ISL29125_MODE_RANGE; + ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, + data->conf1); + if (ret < 0) + return ret; + + ret = i2c_smbus_write_byte_data(data->client, ISL29125_STATUS, 0); + if (ret < 0) + return ret; + + ret = iio_triggered_buffer_setup(indio_dev, NULL, + isl29125_trigger_handler, &isl29125_buffer_setup_ops); + if (ret < 0) + return ret; + + ret = iio_device_register(indio_dev); + if (ret < 0) + goto buffer_cleanup; + + return 0; + +buffer_cleanup: + iio_triggered_buffer_cleanup(indio_dev); + return ret; +} + +static int isl29125_powerdown(struct isl29125_data *data) +{ + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, + (data->conf1 & ~ISL29125_MODE_MASK) | ISL29125_MODE_PD); +} + +static int isl29125_remove(struct i2c_client *client) +{ + struct iio_dev *indio_dev = i2c_get_clientdata(client); + + iio_device_unregister(indio_dev); + iio_triggered_buffer_cleanup(indio_dev); + isl29125_powerdown(iio_priv(indio_dev)); + + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int isl29125_suspend(struct device *dev) +{ + struct isl29125_data *data = iio_priv(i2c_get_clientdata( + to_i2c_client(dev))); + return isl29125_powerdown(data); +} + +static int isl29125_resume(struct device *dev) +{ + struct isl29125_data *data = iio_priv(i2c_get_clientdata( + to_i2c_client(dev))); + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, + data->conf1); +} +#endif + +static SIMPLE_DEV_PM_OPS(isl29125_pm_ops, isl29125_suspend, isl29125_resume); + +static const struct i2c_device_id isl29125_id[] = { + { "isl29125", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, isl29125_id); + +static struct i2c_driver isl29125_driver = { + .driver = { + .name = ISL29125_DRV_NAME, + .pm = &isl29125_pm_ops, + .owner = THIS_MODULE, + }, + .probe = isl29125_probe, + .remove = isl29125_remove, + .id_table = isl29125_id, +}; +module_i2c_driver(isl29125_driver); + +MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); +MODULE_DESCRIPTION("ISL29125 RGB light sensor driver"); +MODULE_LICENSE("GPL"); -- 1.9.1 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 2/3] iio: Add Intersil isl29125 digital color light sensor driver 2014-07-05 12:38 ` [PATCH 2/3] iio: Add Intersil isl29125 digital color light sensor driver Peter Meerwald @ 2014-07-07 8:15 ` Jonathan Cameron 0 siblings, 0 replies; 7+ messages in thread From: Jonathan Cameron @ 2014-07-07 8:15 UTC (permalink / raw) To: Peter Meerwald, linux-iio On 05/07/14 13:38, Peter Meerwald wrote: > datasheet: http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29125.pdf > > Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> Nice driver and very clean. Applied to the togreg branch of iio.git Thanks, Jonathan > --- > drivers/iio/light/Kconfig | 12 ++ > drivers/iio/light/Makefile | 1 + > drivers/iio/light/isl29125.c | 347 +++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 360 insertions(+) > create mode 100644 drivers/iio/light/isl29125.c > > diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig > index c89740d..7d83dca 100644 > --- a/drivers/iio/light/Kconfig > +++ b/drivers/iio/light/Kconfig > @@ -62,6 +62,18 @@ config GP2AP020A00F > To compile this driver as a module, choose M here: the > module will be called gp2ap020a00f. > > +config ISL29125 > + tristate "Intersil ISL29125 digital color light sensor" > + depends on I2C > + select IIO_BUFFER > + select IIO_TRIGGERED_BUFFER > + help > + Say Y here if you want to build a driver for the Intersil ISL29125 > + RGB light sensor for I2C. > + > + To compile this driver as a module, choose M here: the module will be > + called isl29125. > + > config HID_SENSOR_ALS > depends on HID_SENSOR_HUB > select IIO_BUFFER > diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile > index 3eb36e5..f3d1857 100644 > --- a/drivers/iio/light/Makefile > +++ b/drivers/iio/light/Makefile > @@ -10,6 +10,7 @@ obj-$(CONFIG_CM36651) += cm36651.o > obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o > obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o > obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o > +obj-$(CONFIG_ISL29125) += isl29125.o > obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o > obj-$(CONFIG_LTR501) += ltr501.o > obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o > diff --git a/drivers/iio/light/isl29125.c b/drivers/iio/light/isl29125.c > new file mode 100644 > index 0000000..c82f4a6 > --- /dev/null > +++ b/drivers/iio/light/isl29125.c > @@ -0,0 +1,347 @@ > +/* > + * isl29125.c - Support for Intersil ISL29125 RGB light sensor > + * > + * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net> > + * > + * This file is subject to the terms and conditions of version 2 of > + * the GNU General Public License. See the file COPYING in the main > + * directory of this archive for more details. > + * > + * RGB light sensor with 16-bit channels for red, green, blue); > + * 7-bit I2C slave address 0x44 > + * > + * TODO: interrupt support, IR compensation, thresholds, 12bit > + */ > + > +#include <linux/module.h> > +#include <linux/i2c.h> > +#include <linux/delay.h> > +#include <linux/pm.h> > + > +#include <linux/iio/iio.h> > +#include <linux/iio/sysfs.h> > +#include <linux/iio/trigger_consumer.h> > +#include <linux/iio/buffer.h> > +#include <linux/iio/triggered_buffer.h> > + > +#define ISL29125_DRV_NAME "isl29125" > + > +#define ISL29125_DEVICE_ID 0x00 > +#define ISL29125_CONF1 0x01 > +#define ISL29125_CONF2 0x02 > +#define ISL29125_CONF3 0x03 > +#define ISL29125_STATUS 0x08 > +#define ISL29125_GREEN_DATA 0x09 > +#define ISL29125_RED_DATA 0x0b > +#define ISL29125_BLUE_DATA 0x0d > + > +#define ISL29125_ID 0x7d > + > +#define ISL29125_MODE_MASK GENMASK(2, 0) > +#define ISL29125_MODE_PD 0x0 > +#define ISL29125_MODE_G 0x1 > +#define ISL29125_MODE_R 0x2 > +#define ISL29125_MODE_B 0x3 > +#define ISL29125_MODE_RGB 0x5 > + > +#define ISL29125_MODE_RANGE BIT(3) > + > +#define ISL29125_STATUS_CONV BIT(1) > + > +struct isl29125_data { > + struct i2c_client *client; > + struct mutex lock; > + u8 conf1; > + u16 buffer[8]; /* 3x 16-bit, padding, 8 bytes timestamp */ > +}; > + > +#define ISL29125_CHANNEL(_color, _si) { \ > + .type = IIO_INTENSITY, \ > + .modified = 1, \ > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ > + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ > + .channel2 = IIO_MOD_LIGHT_##_color, \ > + .scan_index = _si, \ > + .scan_type = { \ > + .sign = 'u', \ > + .realbits = 16, \ > + .storagebits = 16, \ > + .endianness = IIO_CPU, \ > + }, \ > +} > + > +static const struct iio_chan_spec isl29125_channels[] = { > + ISL29125_CHANNEL(GREEN, 0), > + ISL29125_CHANNEL(RED, 1), > + ISL29125_CHANNEL(BLUE, 2), > + IIO_CHAN_SOFT_TIMESTAMP(3), > +}; > + > +static const struct { > + u8 mode, data; > +} isl29125_regs[] = { > + {ISL29125_MODE_G, ISL29125_GREEN_DATA}, > + {ISL29125_MODE_R, ISL29125_RED_DATA}, > + {ISL29125_MODE_B, ISL29125_BLUE_DATA}, > +}; > + > +static int isl29125_read_data(struct isl29125_data *data, int si) > +{ > + int tries = 5; > + int ret; > + > + ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, > + data->conf1 | isl29125_regs[si].mode); > + if (ret < 0) > + return ret; > + > + msleep(101); > + > + while (tries--) { > + ret = i2c_smbus_read_byte_data(data->client, ISL29125_STATUS); > + if (ret < 0) > + goto fail; > + if (ret & ISL29125_STATUS_CONV) > + break; > + msleep(20); > + } > + > + if (tries < 0) { > + dev_err(&data->client->dev, "data not ready\n"); > + ret = -EIO; > + goto fail; > + } > + > + ret = i2c_smbus_read_word_data(data->client, isl29125_regs[si].data); > + > +fail: > + i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, data->conf1); > + return ret; > +} > + > +static int isl29125_read_raw(struct iio_dev *indio_dev, > + struct iio_chan_spec const *chan, > + int *val, int *val2, long mask) > +{ > + struct isl29125_data *data = iio_priv(indio_dev); > + int ret; > + > + switch (mask) { > + case IIO_CHAN_INFO_RAW: > + if (iio_buffer_enabled(indio_dev)) > + return -EBUSY; > + mutex_lock(&data->lock); > + ret = isl29125_read_data(data, chan->scan_index); > + mutex_unlock(&data->lock); > + if (ret < 0) > + return ret; > + *val = ret; > + return IIO_VAL_INT; > + case IIO_CHAN_INFO_SCALE: > + *val = 0; > + if (data->conf1 & ISL29125_MODE_RANGE) > + *val2 = 152590; /* 10k lux full range */ > + else > + *val2 = 5722; /* 375 lux full range */ > + return IIO_VAL_INT_PLUS_MICRO; > + } > + return -EINVAL; > +} > + > +static int isl29125_write_raw(struct iio_dev *indio_dev, > + struct iio_chan_spec const *chan, > + int val, int val2, long mask) > +{ > + struct isl29125_data *data = iio_priv(indio_dev); > + > + switch (mask) { > + case IIO_CHAN_INFO_SCALE: > + if (val != 0) > + return -EINVAL; > + if (val2 == 152590) > + data->conf1 |= ISL29125_MODE_RANGE; > + else if (val2 == 5722) > + data->conf1 &= ~ISL29125_MODE_RANGE; > + else > + return -EINVAL; > + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, > + data->conf1); > + default: > + return -EINVAL; > + } > +} > + > +static irqreturn_t isl29125_trigger_handler(int irq, void *p) > +{ > + struct iio_poll_func *pf = p; > + struct iio_dev *indio_dev = pf->indio_dev; > + struct isl29125_data *data = iio_priv(indio_dev); > + int i, j = 0; > + > + for_each_set_bit(i, indio_dev->active_scan_mask, > + indio_dev->masklength) { > + int ret = i2c_smbus_read_word_data(data->client, > + isl29125_regs[i].data); > + if (ret < 0) > + goto done; > + > + data->buffer[j++] = ret; > + } > + > + iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, > + iio_get_time_ns()); > + > +done: > + iio_trigger_notify_done(indio_dev->trig); > + > + return IRQ_HANDLED; > +} > + > +static const struct iio_info isl29125_info = { > + .read_raw = isl29125_read_raw, > + .write_raw = isl29125_write_raw, > + .driver_module = THIS_MODULE, > +}; > + > +static int isl29125_buffer_preenable(struct iio_dev *indio_dev) > +{ > + struct isl29125_data *data = iio_priv(indio_dev); > + > + data->conf1 |= ISL29125_MODE_RGB; > + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, > + data->conf1); > +} > + > +static int isl29125_buffer_predisable(struct iio_dev *indio_dev) > +{ > + struct isl29125_data *data = iio_priv(indio_dev); > + int ret; > + > + ret = iio_triggered_buffer_predisable(indio_dev); > + if (ret < 0) > + return ret; > + > + data->conf1 &= ~ISL29125_MODE_MASK; > + data->conf1 |= ISL29125_MODE_PD; > + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, > + data->conf1); > +} > + > +static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops = { > + .preenable = isl29125_buffer_preenable, > + .postenable = &iio_triggered_buffer_postenable, > + .predisable = isl29125_buffer_predisable, > +}; > + > +static int isl29125_probe(struct i2c_client *client, > + const struct i2c_device_id *id) > +{ > + struct isl29125_data *data; > + struct iio_dev *indio_dev; > + int ret; > + > + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); > + if (indio_dev == NULL) > + return -ENOMEM; > + > + data = iio_priv(indio_dev); > + i2c_set_clientdata(client, indio_dev); > + data->client = client; > + mutex_init(&data->lock); > + > + indio_dev->dev.parent = &client->dev; > + indio_dev->info = &isl29125_info; > + indio_dev->name = ISL29125_DRV_NAME; > + indio_dev->channels = isl29125_channels; > + indio_dev->num_channels = ARRAY_SIZE(isl29125_channels); > + indio_dev->modes = INDIO_DIRECT_MODE; > + > + ret = i2c_smbus_read_byte_data(data->client, ISL29125_DEVICE_ID); > + if (ret < 0) > + return ret; > + if (ret != ISL29125_ID) > + return -ENODEV; > + > + data->conf1 = ISL29125_MODE_PD | ISL29125_MODE_RANGE; > + ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, > + data->conf1); > + if (ret < 0) > + return ret; > + > + ret = i2c_smbus_write_byte_data(data->client, ISL29125_STATUS, 0); > + if (ret < 0) > + return ret; > + > + ret = iio_triggered_buffer_setup(indio_dev, NULL, > + isl29125_trigger_handler, &isl29125_buffer_setup_ops); > + if (ret < 0) > + return ret; > + > + ret = iio_device_register(indio_dev); > + if (ret < 0) > + goto buffer_cleanup; > + > + return 0; > + > +buffer_cleanup: > + iio_triggered_buffer_cleanup(indio_dev); > + return ret; > +} > + > +static int isl29125_powerdown(struct isl29125_data *data) > +{ > + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, > + (data->conf1 & ~ISL29125_MODE_MASK) | ISL29125_MODE_PD); > +} > + > +static int isl29125_remove(struct i2c_client *client) > +{ > + struct iio_dev *indio_dev = i2c_get_clientdata(client); > + > + iio_device_unregister(indio_dev); > + iio_triggered_buffer_cleanup(indio_dev); > + isl29125_powerdown(iio_priv(indio_dev)); > + > + return 0; > +} > + > +#ifdef CONFIG_PM_SLEEP > +static int isl29125_suspend(struct device *dev) > +{ > + struct isl29125_data *data = iio_priv(i2c_get_clientdata( > + to_i2c_client(dev))); > + return isl29125_powerdown(data); > +} > + > +static int isl29125_resume(struct device *dev) > +{ > + struct isl29125_data *data = iio_priv(i2c_get_clientdata( > + to_i2c_client(dev))); > + return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, > + data->conf1); > +} > +#endif > + > +static SIMPLE_DEV_PM_OPS(isl29125_pm_ops, isl29125_suspend, isl29125_resume); > + > +static const struct i2c_device_id isl29125_id[] = { > + { "isl29125", 0 }, > + { } > +}; > +MODULE_DEVICE_TABLE(i2c, isl29125_id); > + > +static struct i2c_driver isl29125_driver = { > + .driver = { > + .name = ISL29125_DRV_NAME, > + .pm = &isl29125_pm_ops, > + .owner = THIS_MODULE, > + }, > + .probe = isl29125_probe, > + .remove = isl29125_remove, > + .id_table = isl29125_id, > +}; > +module_i2c_driver(isl29125_driver); > + > +MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); > +MODULE_DESCRIPTION("ISL29125 RGB light sensor driver"); > +MODULE_LICENSE("GPL"); > ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/3] iio: Add driver for AMS/TAOS tcs3414 digital color sensor 2014-07-05 12:38 [PATCH 0/3] iio: new color light sensor drivers Peter Meerwald 2014-07-05 12:38 ` [PATCH 1/3] iio:tcs3472: Check for buffer enabled and locking Peter Meerwald 2014-07-05 12:38 ` [PATCH 2/3] iio: Add Intersil isl29125 digital color light sensor driver Peter Meerwald @ 2014-07-05 12:38 ` Peter Meerwald 2014-07-07 8:33 ` Jonathan Cameron 2 siblings, 1 reply; 7+ messages in thread From: Peter Meerwald @ 2014-07-05 12:38 UTC (permalink / raw) To: linux-iio; +Cc: jic23, Peter Meerwald 16-bit digital color sensor with red, green, blue and clear channel datasheet: http://ams.com/eng/content/download/250258/975997/TCS3414_Datasheet_EN_v1.pdf Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> --- drivers/iio/light/Kconfig | 12 ++ drivers/iio/light/Makefile | 1 + drivers/iio/light/tcs3414.c | 405 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 418 insertions(+) create mode 100644 drivers/iio/light/tcs3414.c diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig index 7d83dca..bf05ca5 100644 --- a/drivers/iio/light/Kconfig +++ b/drivers/iio/light/Kconfig @@ -128,6 +128,18 @@ config LTR501 This driver can also be built as a module. If so, the module will be called ltr501. +config TCS3414 + tristate "TAOS TCS3414 digital color sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + help + If you say yes here you get support for the TAOS TCS3414 + family of digital color sensors. + + This driver can also be built as a module. If so, the module + will be called tcs3414. + config TCS3472 tristate "TAOS TCS3472 color light-to-digital converter" depends on I2C diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile index f3d1857..8b8c09f 100644 --- a/drivers/iio/light/Makefile +++ b/drivers/iio/light/Makefile @@ -14,6 +14,7 @@ obj-$(CONFIG_ISL29125) += isl29125.o obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.o obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o +obj-$(CONFIG_TCS3414) += tcs3414.o obj-$(CONFIG_TCS3472) += tcs3472.o obj-$(CONFIG_TSL4531) += tsl4531.o obj-$(CONFIG_VCNL4000) += vcnl4000.o diff --git a/drivers/iio/light/tcs3414.c b/drivers/iio/light/tcs3414.c new file mode 100644 index 0000000..a9e449b --- /dev/null +++ b/drivers/iio/light/tcs3414.c @@ -0,0 +1,405 @@ +/* + * tcs3414.c - Support for TAOS TCS3414 digital color sensor + * + * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net> + * + * This file is subject to the terms and conditions of version 2 of + * the GNU General Public License. See the file COPYING in the main + * directory of this archive for more details. + * + * Digital color sensor with 16-bit channels for red, green, blue, clear); + * 7-bit I2C slave address 0x39 (TCS3414) or 0x29, 0x49, 0x59 (TCS3413, + * TCS3415, TCS3416, resp.) + * + * TODO: sync, interrupt support, thresholds, prescaler + */ + +#include <linux/module.h> +#include <linux/i2c.h> +#include <linux/delay.h> +#include <linux/pm.h> + +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/buffer.h> +#include <linux/iio/triggered_buffer.h> + +#define TCS3414_DRV_NAME "tcs3414" + +#define TCS3414_COMMAND BIT(7) +#define TCS3414_COMMAND_WORD (TCS3414_COMMAND | BIT(5)) + +#define TCS3414_CONTROL (TCS3414_COMMAND | 0x00) +#define TCS3414_TIMING (TCS3414_COMMAND | 0x01) +#define TCS3414_ID (TCS3414_COMMAND | 0x04) +#define TCS3414_GAIN (TCS3414_COMMAND | 0x07) +#define TCS3414_DATA_GREEN (TCS3414_COMMAND_WORD | 0x10) +#define TCS3414_DATA_RED (TCS3414_COMMAND_WORD | 0x12) +#define TCS3414_DATA_BLUE (TCS3414_COMMAND_WORD | 0x14) +#define TCS3414_DATA_CLEAR (TCS3414_COMMAND_WORD | 0x16) + +#define TCS3414_CONTROL_ADC_VALID BIT(4) +#define TCS3414_CONTROL_ADC_EN BIT(1) +#define TCS3414_CONTROL_POWER BIT(0) + +#define TCS3414_INTEG_MASK GENMASK(1, 0) +#define TCS3414_INTEG_12MS 0x0 +#define TCS3414_INTEG_100MS 0x1 +#define TCS3414_INTEG_400MS 0x2 + +#define TCS3414_GAIN_MASK GENMASK(5, 4) +#define TCS3414_GAIN_SHIFT 4 + +struct tcs3414_data { + struct i2c_client *client; + struct mutex lock; + u8 control; + u8 gain; + u8 timing; + u16 buffer[8]; /* 4x 16-bit + 8 bytes timestamp */ +}; + +#define TCS3414_CHANNEL(_color, _si, _addr) { \ + .type = IIO_INTENSITY, \ + .modified = 1, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ + BIT(IIO_CHAN_INFO_INT_TIME), \ + .channel2 = IIO_MOD_LIGHT_##_color, \ + .address = _addr, \ + .scan_index = _si, \ + .scan_type = { \ + .sign = 'u', \ + .realbits = 16, \ + .storagebits = 16, \ + .endianness = IIO_CPU, \ + }, \ +} + +/* scale factors: 1/gain */ +static const int tcs3414_scales[][2] = { + {1, 0}, {0, 250000}, {0, 62500}, {0, 15625} +}; + +/* integration time in ms */ +static const int tcs3414_times[] = { 12, 100, 400 }; + +static const struct iio_chan_spec tcs3414_channels[] = { + TCS3414_CHANNEL(GREEN, 0, TCS3414_DATA_GREEN), + TCS3414_CHANNEL(RED, 1, TCS3414_DATA_RED), + TCS3414_CHANNEL(BLUE, 2, TCS3414_DATA_BLUE), + TCS3414_CHANNEL(CLEAR, 3, TCS3414_DATA_CLEAR), + IIO_CHAN_SOFT_TIMESTAMP(4), +}; + +static int tcs3414_req_data(struct tcs3414_data *data) +{ + int tries = 25; + int ret; + + ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, + data->control | TCS3414_CONTROL_ADC_EN); + if (ret < 0) + return ret; + + while (tries--) { + ret = i2c_smbus_read_byte_data(data->client, TCS3414_CONTROL); + if (ret < 0) + return ret; + if (ret & TCS3414_CONTROL_ADC_VALID) + break; + msleep(20); + } + + ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, + data->control); + if (ret < 0) + return ret; + + if (tries < 0) { + dev_err(&data->client->dev, "data not ready\n"); + return -EIO; + } + + return 0; +} + +static int tcs3414_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct tcs3414_data *data = iio_priv(indio_dev); + int i, ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + if (iio_buffer_enabled(indio_dev)) + return -EBUSY; + mutex_lock(&data->lock); + ret = tcs3414_req_data(data); + if (ret < 0) { + mutex_unlock(&data->lock); + return ret; + } + ret = i2c_smbus_read_word_data(data->client, chan->address); + mutex_unlock(&data->lock); + if (ret < 0) + return ret; + *val = ret; + return IIO_VAL_INT; + case IIO_CHAN_INFO_SCALE: + i = (data->gain & TCS3414_GAIN_MASK) >> TCS3414_GAIN_SHIFT; + *val = tcs3414_scales[i][0]; + *val2 = tcs3414_scales[i][1]; + return IIO_VAL_INT_PLUS_MICRO; + case IIO_CHAN_INFO_INT_TIME: + *val = 0; + *val2 = tcs3414_times[data->timing & TCS3414_INTEG_MASK] * 1000; + return IIO_VAL_INT_PLUS_MICRO; + } + return -EINVAL; +} + +static int tcs3414_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct tcs3414_data *data = iio_priv(indio_dev); + int i; + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + for (i = 0; i < ARRAY_SIZE(tcs3414_scales); i++) { + if (val == tcs3414_scales[i][0] && + val2 == tcs3414_scales[i][1]) { + data->gain &= ~TCS3414_GAIN_MASK; + data->gain |= i << TCS3414_GAIN_SHIFT; + return i2c_smbus_write_byte_data( + data->client, TCS3414_GAIN, + data->gain); + } + } + return -EINVAL; + case IIO_CHAN_INFO_INT_TIME: + if (val != 0) + return -EINVAL; + for (i = 0; i < ARRAY_SIZE(tcs3414_times); i++) { + if (val == tcs3414_times[i] * 1000) { + data->timing &= ~TCS3414_INTEG_MASK; + data->timing |= i; + return i2c_smbus_write_byte_data( + data->client, TCS3414_TIMING, + data->timing); + } + } + return -EINVAL; + default: + return -EINVAL; + } +} + +static irqreturn_t tcs3414_trigger_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct tcs3414_data *data = iio_priv(indio_dev); + int i, j = 0; + + for_each_set_bit(i, indio_dev->active_scan_mask, + indio_dev->masklength) { + int ret = i2c_smbus_read_word_data(data->client, + TCS3414_DATA_GREEN + 2*i); + if (ret < 0) + goto done; + + data->buffer[j++] = ret; + } + + iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, + iio_get_time_ns()); + +done: + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + +static IIO_CONST_ATTR(scale_available, "1 0.25 0.0625 0.015625"); +static IIO_CONST_ATTR_INT_TIME_AVAIL("0.012 0.1 0.4"); + +static struct attribute *tcs3414_attributes[] = { + &iio_const_attr_scale_available.dev_attr.attr, + &iio_const_attr_integration_time_available.dev_attr.attr, + NULL +}; + +static const struct attribute_group tcs3414_attribute_group = { + .attrs = tcs3414_attributes, +}; + +static const struct iio_info tcs3414_info = { + .read_raw = tcs3414_read_raw, + .write_raw = tcs3414_write_raw, + .attrs = &tcs3414_attribute_group, + .driver_module = THIS_MODULE, +}; + +static int tcs3414_buffer_preenable(struct iio_dev *indio_dev) +{ + struct tcs3414_data *data = iio_priv(indio_dev); + + data->control |= TCS3414_CONTROL_ADC_EN; + return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, + data->control); +} + +static int tcs3414_buffer_predisable(struct iio_dev *indio_dev) +{ + struct tcs3414_data *data = iio_priv(indio_dev); + int ret; + + ret = iio_triggered_buffer_predisable(indio_dev); + if (ret < 0) + return ret; + + data->control &= ~TCS3414_CONTROL_ADC_EN; + return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, + data->control); +} + +static const struct iio_buffer_setup_ops tcs3414_buffer_setup_ops = { + .preenable = tcs3414_buffer_preenable, + .postenable = &iio_triggered_buffer_postenable, + .predisable = tcs3414_buffer_predisable, +}; + +static int tcs3414_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct tcs3414_data *data; + struct iio_dev *indio_dev; + int ret; + + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); + if (indio_dev == NULL) + return -ENOMEM; + + data = iio_priv(indio_dev); + i2c_set_clientdata(client, indio_dev); + data->client = client; + mutex_init(&data->lock); + + indio_dev->dev.parent = &client->dev; + indio_dev->info = &tcs3414_info; + indio_dev->name = TCS3414_DRV_NAME; + indio_dev->channels = tcs3414_channels; + indio_dev->num_channels = ARRAY_SIZE(tcs3414_channels); + indio_dev->modes = INDIO_DIRECT_MODE; + + ret = i2c_smbus_read_byte_data(data->client, TCS3414_ID); + if (ret < 0) + return ret; + + switch (ret & 0xf0) { + case 0x00: + dev_info(&client->dev, "TCS3404 found\n"); + break; + case 0x10: + dev_info(&client->dev, "TCS3413/14/15/16 found\n"); + break; + default: + return -ENODEV; + } + + data->control = TCS3414_CONTROL_POWER; + ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, + data->control); + if (ret < 0) + return ret; + + data->timing = TCS3414_INTEG_12MS; /* free running */ + ret = i2c_smbus_write_byte_data(data->client, TCS3414_TIMING, + data->timing); + if (ret < 0) + return ret; + + ret = i2c_smbus_read_byte_data(data->client, TCS3414_GAIN); + if (ret < 0) + return ret; + data->gain = ret; + + ret = iio_triggered_buffer_setup(indio_dev, NULL, + tcs3414_trigger_handler, &tcs3414_buffer_setup_ops); + if (ret < 0) + return ret; + + ret = iio_device_register(indio_dev); + if (ret < 0) + goto buffer_cleanup; + + return 0; + +buffer_cleanup: + iio_triggered_buffer_cleanup(indio_dev); + return ret; +} + +static int tcs3414_powerdown(struct tcs3414_data *data) +{ + return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, + data->control & ~(TCS3414_CONTROL_POWER | + TCS3414_CONTROL_ADC_EN)); +} + +static int tcs3414_remove(struct i2c_client *client) +{ + struct iio_dev *indio_dev = i2c_get_clientdata(client); + + iio_device_unregister(indio_dev); + iio_triggered_buffer_cleanup(indio_dev); + tcs3414_powerdown(iio_priv(indio_dev)); + + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int tcs3414_suspend(struct device *dev) +{ + struct tcs3414_data *data = iio_priv(i2c_get_clientdata( + to_i2c_client(dev))); + return tcs3414_powerdown(data); +} + +static int tcs3414_resume(struct device *dev) +{ + struct tcs3414_data *data = iio_priv(i2c_get_clientdata( + to_i2c_client(dev))); + return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, + data->control); +} +#endif + +static SIMPLE_DEV_PM_OPS(tcs3414_pm_ops, tcs3414_suspend, tcs3414_resume); + +static const struct i2c_device_id tcs3414_id[] = { + { "tcs3414", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, tcs3414_id); + +static struct i2c_driver tcs3414_driver = { + .driver = { + .name = TCS3414_DRV_NAME, + .pm = &tcs3414_pm_ops, + .owner = THIS_MODULE, + }, + .probe = tcs3414_probe, + .remove = tcs3414_remove, + .id_table = tcs3414_id, +}; +module_i2c_driver(tcs3414_driver); + +MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); +MODULE_DESCRIPTION("TCS3414 digital color sensors driver"); +MODULE_LICENSE("GPL"); -- 1.9.1 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 3/3] iio: Add driver for AMS/TAOS tcs3414 digital color sensor 2014-07-05 12:38 ` [PATCH 3/3] iio: Add driver for AMS/TAOS tcs3414 digital color sensor Peter Meerwald @ 2014-07-07 8:33 ` Jonathan Cameron 0 siblings, 0 replies; 7+ messages in thread From: Jonathan Cameron @ 2014-07-07 8:33 UTC (permalink / raw) To: Peter Meerwald, linux-iio On 05/07/14 13:38, Peter Meerwald wrote: > 16-bit digital color sensor with red, green, blue and clear channel > > datasheet: http://ams.com/eng/content/download/250258/975997/TCS3414_Datasheet_EN_v1.pdf > > Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> Another nice clean driver, Applied to the togreg branch of iio.git initially pushed out as staging. Thanks, > --- > drivers/iio/light/Kconfig | 12 ++ > drivers/iio/light/Makefile | 1 + > drivers/iio/light/tcs3414.c | 405 ++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 418 insertions(+) > create mode 100644 drivers/iio/light/tcs3414.c > > diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig > index 7d83dca..bf05ca5 100644 > --- a/drivers/iio/light/Kconfig > +++ b/drivers/iio/light/Kconfig > @@ -128,6 +128,18 @@ config LTR501 > This driver can also be built as a module. If so, the module > will be called ltr501. > > +config TCS3414 > + tristate "TAOS TCS3414 digital color sensor" > + depends on I2C > + select IIO_BUFFER > + select IIO_TRIGGERED_BUFFER > + help > + If you say yes here you get support for the TAOS TCS3414 > + family of digital color sensors. > + > + This driver can also be built as a module. If so, the module > + will be called tcs3414. > + > config TCS3472 > tristate "TAOS TCS3472 color light-to-digital converter" > depends on I2C > diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile > index f3d1857..8b8c09f 100644 > --- a/drivers/iio/light/Makefile > +++ b/drivers/iio/light/Makefile > @@ -14,6 +14,7 @@ obj-$(CONFIG_ISL29125) += isl29125.o > obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o > obj-$(CONFIG_LTR501) += ltr501.o > obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o > +obj-$(CONFIG_TCS3414) += tcs3414.o > obj-$(CONFIG_TCS3472) += tcs3472.o > obj-$(CONFIG_TSL4531) += tsl4531.o > obj-$(CONFIG_VCNL4000) += vcnl4000.o > diff --git a/drivers/iio/light/tcs3414.c b/drivers/iio/light/tcs3414.c > new file mode 100644 > index 0000000..a9e449b > --- /dev/null > +++ b/drivers/iio/light/tcs3414.c > @@ -0,0 +1,405 @@ > +/* > + * tcs3414.c - Support for TAOS TCS3414 digital color sensor > + * > + * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net> > + * > + * This file is subject to the terms and conditions of version 2 of > + * the GNU General Public License. See the file COPYING in the main > + * directory of this archive for more details. > + * > + * Digital color sensor with 16-bit channels for red, green, blue, clear); > + * 7-bit I2C slave address 0x39 (TCS3414) or 0x29, 0x49, 0x59 (TCS3413, > + * TCS3415, TCS3416, resp.) > + * > + * TODO: sync, interrupt support, thresholds, prescaler > + */ > + > +#include <linux/module.h> > +#include <linux/i2c.h> > +#include <linux/delay.h> > +#include <linux/pm.h> > + > +#include <linux/iio/iio.h> > +#include <linux/iio/sysfs.h> > +#include <linux/iio/trigger_consumer.h> > +#include <linux/iio/buffer.h> > +#include <linux/iio/triggered_buffer.h> > + > +#define TCS3414_DRV_NAME "tcs3414" > + > +#define TCS3414_COMMAND BIT(7) > +#define TCS3414_COMMAND_WORD (TCS3414_COMMAND | BIT(5)) > + > +#define TCS3414_CONTROL (TCS3414_COMMAND | 0x00) > +#define TCS3414_TIMING (TCS3414_COMMAND | 0x01) > +#define TCS3414_ID (TCS3414_COMMAND | 0x04) > +#define TCS3414_GAIN (TCS3414_COMMAND | 0x07) > +#define TCS3414_DATA_GREEN (TCS3414_COMMAND_WORD | 0x10) > +#define TCS3414_DATA_RED (TCS3414_COMMAND_WORD | 0x12) > +#define TCS3414_DATA_BLUE (TCS3414_COMMAND_WORD | 0x14) > +#define TCS3414_DATA_CLEAR (TCS3414_COMMAND_WORD | 0x16) > + > +#define TCS3414_CONTROL_ADC_VALID BIT(4) > +#define TCS3414_CONTROL_ADC_EN BIT(1) > +#define TCS3414_CONTROL_POWER BIT(0) > + > +#define TCS3414_INTEG_MASK GENMASK(1, 0) > +#define TCS3414_INTEG_12MS 0x0 > +#define TCS3414_INTEG_100MS 0x1 > +#define TCS3414_INTEG_400MS 0x2 > + > +#define TCS3414_GAIN_MASK GENMASK(5, 4) > +#define TCS3414_GAIN_SHIFT 4 > + > +struct tcs3414_data { > + struct i2c_client *client; > + struct mutex lock; > + u8 control; > + u8 gain; > + u8 timing; > + u16 buffer[8]; /* 4x 16-bit + 8 bytes timestamp */ > +}; > + > +#define TCS3414_CHANNEL(_color, _si, _addr) { \ > + .type = IIO_INTENSITY, \ > + .modified = 1, \ > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ > + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ > + BIT(IIO_CHAN_INFO_INT_TIME), \ > + .channel2 = IIO_MOD_LIGHT_##_color, \ > + .address = _addr, \ > + .scan_index = _si, \ > + .scan_type = { \ > + .sign = 'u', \ > + .realbits = 16, \ > + .storagebits = 16, \ > + .endianness = IIO_CPU, \ > + }, \ > +} > + > +/* scale factors: 1/gain */ > +static const int tcs3414_scales[][2] = { > + {1, 0}, {0, 250000}, {0, 62500}, {0, 15625} > +}; > + > +/* integration time in ms */ > +static const int tcs3414_times[] = { 12, 100, 400 }; > + > +static const struct iio_chan_spec tcs3414_channels[] = { > + TCS3414_CHANNEL(GREEN, 0, TCS3414_DATA_GREEN), > + TCS3414_CHANNEL(RED, 1, TCS3414_DATA_RED), > + TCS3414_CHANNEL(BLUE, 2, TCS3414_DATA_BLUE), > + TCS3414_CHANNEL(CLEAR, 3, TCS3414_DATA_CLEAR), > + IIO_CHAN_SOFT_TIMESTAMP(4), > +}; > + > +static int tcs3414_req_data(struct tcs3414_data *data) > +{ > + int tries = 25; > + int ret; > + > + ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, > + data->control | TCS3414_CONTROL_ADC_EN); > + if (ret < 0) > + return ret; > + > + while (tries--) { > + ret = i2c_smbus_read_byte_data(data->client, TCS3414_CONTROL); > + if (ret < 0) > + return ret; > + if (ret & TCS3414_CONTROL_ADC_VALID) > + break; > + msleep(20); > + } > + > + ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, > + data->control); > + if (ret < 0) > + return ret; > + > + if (tries < 0) { > + dev_err(&data->client->dev, "data not ready\n"); > + return -EIO; > + } > + > + return 0; > +} > + > +static int tcs3414_read_raw(struct iio_dev *indio_dev, > + struct iio_chan_spec const *chan, > + int *val, int *val2, long mask) > +{ > + struct tcs3414_data *data = iio_priv(indio_dev); > + int i, ret; > + > + switch (mask) { > + case IIO_CHAN_INFO_RAW: > + if (iio_buffer_enabled(indio_dev)) > + return -EBUSY; > + mutex_lock(&data->lock); > + ret = tcs3414_req_data(data); > + if (ret < 0) { > + mutex_unlock(&data->lock); > + return ret; > + } > + ret = i2c_smbus_read_word_data(data->client, chan->address); > + mutex_unlock(&data->lock); > + if (ret < 0) > + return ret; > + *val = ret; > + return IIO_VAL_INT; > + case IIO_CHAN_INFO_SCALE: > + i = (data->gain & TCS3414_GAIN_MASK) >> TCS3414_GAIN_SHIFT; > + *val = tcs3414_scales[i][0]; > + *val2 = tcs3414_scales[i][1]; > + return IIO_VAL_INT_PLUS_MICRO; > + case IIO_CHAN_INFO_INT_TIME: > + *val = 0; > + *val2 = tcs3414_times[data->timing & TCS3414_INTEG_MASK] * 1000; > + return IIO_VAL_INT_PLUS_MICRO; > + } > + return -EINVAL; > +} > + > +static int tcs3414_write_raw(struct iio_dev *indio_dev, > + struct iio_chan_spec const *chan, > + int val, int val2, long mask) > +{ > + struct tcs3414_data *data = iio_priv(indio_dev); > + int i; > + > + switch (mask) { > + case IIO_CHAN_INFO_SCALE: > + for (i = 0; i < ARRAY_SIZE(tcs3414_scales); i++) { > + if (val == tcs3414_scales[i][0] && > + val2 == tcs3414_scales[i][1]) { > + data->gain &= ~TCS3414_GAIN_MASK; > + data->gain |= i << TCS3414_GAIN_SHIFT; > + return i2c_smbus_write_byte_data( > + data->client, TCS3414_GAIN, > + data->gain); > + } > + } > + return -EINVAL; > + case IIO_CHAN_INFO_INT_TIME: > + if (val != 0) > + return -EINVAL; > + for (i = 0; i < ARRAY_SIZE(tcs3414_times); i++) { > + if (val == tcs3414_times[i] * 1000) { > + data->timing &= ~TCS3414_INTEG_MASK; > + data->timing |= i; > + return i2c_smbus_write_byte_data( > + data->client, TCS3414_TIMING, > + data->timing); > + } > + } > + return -EINVAL; > + default: > + return -EINVAL; > + } > +} > + > +static irqreturn_t tcs3414_trigger_handler(int irq, void *p) > +{ > + struct iio_poll_func *pf = p; > + struct iio_dev *indio_dev = pf->indio_dev; > + struct tcs3414_data *data = iio_priv(indio_dev); > + int i, j = 0; > + > + for_each_set_bit(i, indio_dev->active_scan_mask, > + indio_dev->masklength) { > + int ret = i2c_smbus_read_word_data(data->client, > + TCS3414_DATA_GREEN + 2*i); > + if (ret < 0) > + goto done; > + > + data->buffer[j++] = ret; > + } > + > + iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, > + iio_get_time_ns()); > + > +done: > + iio_trigger_notify_done(indio_dev->trig); > + > + return IRQ_HANDLED; > +} > + > +static IIO_CONST_ATTR(scale_available, "1 0.25 0.0625 0.015625"); > +static IIO_CONST_ATTR_INT_TIME_AVAIL("0.012 0.1 0.4"); > + > +static struct attribute *tcs3414_attributes[] = { > + &iio_const_attr_scale_available.dev_attr.attr, > + &iio_const_attr_integration_time_available.dev_attr.attr, > + NULL > +}; > + > +static const struct attribute_group tcs3414_attribute_group = { > + .attrs = tcs3414_attributes, > +}; > + > +static const struct iio_info tcs3414_info = { > + .read_raw = tcs3414_read_raw, > + .write_raw = tcs3414_write_raw, > + .attrs = &tcs3414_attribute_group, > + .driver_module = THIS_MODULE, > +}; > + > +static int tcs3414_buffer_preenable(struct iio_dev *indio_dev) > +{ > + struct tcs3414_data *data = iio_priv(indio_dev); > + > + data->control |= TCS3414_CONTROL_ADC_EN; > + return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, > + data->control); > +} > + > +static int tcs3414_buffer_predisable(struct iio_dev *indio_dev) > +{ > + struct tcs3414_data *data = iio_priv(indio_dev); > + int ret; > + > + ret = iio_triggered_buffer_predisable(indio_dev); > + if (ret < 0) > + return ret; > + > + data->control &= ~TCS3414_CONTROL_ADC_EN; > + return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, > + data->control); > +} > + > +static const struct iio_buffer_setup_ops tcs3414_buffer_setup_ops = { > + .preenable = tcs3414_buffer_preenable, > + .postenable = &iio_triggered_buffer_postenable, > + .predisable = tcs3414_buffer_predisable, > +}; > + > +static int tcs3414_probe(struct i2c_client *client, > + const struct i2c_device_id *id) > +{ > + struct tcs3414_data *data; > + struct iio_dev *indio_dev; > + int ret; > + > + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); > + if (indio_dev == NULL) > + return -ENOMEM; > + > + data = iio_priv(indio_dev); > + i2c_set_clientdata(client, indio_dev); > + data->client = client; > + mutex_init(&data->lock); > + > + indio_dev->dev.parent = &client->dev; > + indio_dev->info = &tcs3414_info; > + indio_dev->name = TCS3414_DRV_NAME; > + indio_dev->channels = tcs3414_channels; > + indio_dev->num_channels = ARRAY_SIZE(tcs3414_channels); > + indio_dev->modes = INDIO_DIRECT_MODE; > + > + ret = i2c_smbus_read_byte_data(data->client, TCS3414_ID); > + if (ret < 0) > + return ret; > + > + switch (ret & 0xf0) { > + case 0x00: > + dev_info(&client->dev, "TCS3404 found\n"); > + break; > + case 0x10: > + dev_info(&client->dev, "TCS3413/14/15/16 found\n"); > + break; > + default: > + return -ENODEV; > + } > + > + data->control = TCS3414_CONTROL_POWER; > + ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, > + data->control); > + if (ret < 0) > + return ret; > + > + data->timing = TCS3414_INTEG_12MS; /* free running */ > + ret = i2c_smbus_write_byte_data(data->client, TCS3414_TIMING, > + data->timing); > + if (ret < 0) > + return ret; > + > + ret = i2c_smbus_read_byte_data(data->client, TCS3414_GAIN); > + if (ret < 0) > + return ret; > + data->gain = ret; > + > + ret = iio_triggered_buffer_setup(indio_dev, NULL, > + tcs3414_trigger_handler, &tcs3414_buffer_setup_ops); > + if (ret < 0) > + return ret; > + > + ret = iio_device_register(indio_dev); > + if (ret < 0) > + goto buffer_cleanup; > + > + return 0; > + > +buffer_cleanup: > + iio_triggered_buffer_cleanup(indio_dev); > + return ret; > +} > + > +static int tcs3414_powerdown(struct tcs3414_data *data) > +{ > + return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, > + data->control & ~(TCS3414_CONTROL_POWER | > + TCS3414_CONTROL_ADC_EN)); > +} > + > +static int tcs3414_remove(struct i2c_client *client) > +{ > + struct iio_dev *indio_dev = i2c_get_clientdata(client); > + > + iio_device_unregister(indio_dev); > + iio_triggered_buffer_cleanup(indio_dev); > + tcs3414_powerdown(iio_priv(indio_dev)); > + > + return 0; > +} > + > +#ifdef CONFIG_PM_SLEEP > +static int tcs3414_suspend(struct device *dev) > +{ > + struct tcs3414_data *data = iio_priv(i2c_get_clientdata( > + to_i2c_client(dev))); > + return tcs3414_powerdown(data); > +} > + > +static int tcs3414_resume(struct device *dev) > +{ > + struct tcs3414_data *data = iio_priv(i2c_get_clientdata( > + to_i2c_client(dev))); > + return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, > + data->control); > +} > +#endif > + > +static SIMPLE_DEV_PM_OPS(tcs3414_pm_ops, tcs3414_suspend, tcs3414_resume); > + > +static const struct i2c_device_id tcs3414_id[] = { > + { "tcs3414", 0 }, > + { } > +}; > +MODULE_DEVICE_TABLE(i2c, tcs3414_id); > + > +static struct i2c_driver tcs3414_driver = { > + .driver = { > + .name = TCS3414_DRV_NAME, > + .pm = &tcs3414_pm_ops, > + .owner = THIS_MODULE, > + }, > + .probe = tcs3414_probe, > + .remove = tcs3414_remove, > + .id_table = tcs3414_id, > +}; > +module_i2c_driver(tcs3414_driver); > + > +MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); > +MODULE_DESCRIPTION("TCS3414 digital color sensors driver"); > +MODULE_LICENSE("GPL"); > ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2014-07-07 8:31 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2014-07-05 12:38 [PATCH 0/3] iio: new color light sensor drivers Peter Meerwald 2014-07-05 12:38 ` [PATCH 1/3] iio:tcs3472: Check for buffer enabled and locking Peter Meerwald 2014-07-07 8:00 ` Jonathan Cameron 2014-07-05 12:38 ` [PATCH 2/3] iio: Add Intersil isl29125 digital color light sensor driver Peter Meerwald 2014-07-07 8:15 ` Jonathan Cameron 2014-07-05 12:38 ` [PATCH 3/3] iio: Add driver for AMS/TAOS tcs3414 digital color sensor Peter Meerwald 2014-07-07 8:33 ` Jonathan Cameron
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.