linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] iio: light: Add support for vishay vcnl4035
@ 2018-06-28 12:30 Parthiban Nallathambi
  2018-06-28 12:30 ` [PATCH 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
                   ` (3 more replies)
  0 siblings, 4 replies; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-06-28 12:30 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, robh+dt
  Cc: linux-iio, linux-kernel, mark.rutland, devicetree, matthias.bgg,
	wd, sbabic, hs, Parthiban Nallathambi

Add support for VCNL4035, which is capable of Ambient light
sensing (ALS) and proximity function. This patch adds support
only for ALS function

Signed-off-by: Parthiban Nallathambi <pn@denx.de>
---
 drivers/iio/light/Kconfig    |  12 +
 drivers/iio/light/Makefile   |   1 +
 drivers/iio/light/vcnl4035.c | 684 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 697 insertions(+)
 create mode 100644 drivers/iio/light/vcnl4035.c

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index c7ef8d1862d6..b7069a4c28a2 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -447,6 +447,18 @@ config VCNL4000
 	 To compile this driver as a module, choose M here: the
 	 module will be called vcnl4000.
 
+config VCNL4035
+	tristate "VCNL4035 combined ALS and proximity sensor"
+	select REGMAP_I2C
+	depends on I2C
+	help
+	 Say Y here if you want to build a driver for the Vishay VCNL4035,
+	 combined ambient light (ALS) and proximity sensor. Currently only ALS
+	 function is available.
+
+	 To compile this driver as a module, choose M here: the
+	 module will be called vcnl4035.
+
 config VEML6070
 	tristate "VEML6070 UV A light sensor"
 	depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 80943af5d627..dce98511a59b 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
 obj-$(CONFIG_TSL4531)		+= tsl4531.o
 obj-$(CONFIG_US5182D)		+= us5182d.o
 obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
+obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
 obj-$(CONFIG_VEML6070)		+= veml6070.o
 obj-$(CONFIG_VL6180)		+= vl6180.o
 obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
new file mode 100644
index 000000000000..fc19856f965b
--- /dev/null
+++ b/drivers/iio/light/vcnl4035.c
@@ -0,0 +1,684 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
+ *
+ * Copyright (c) 2018, DENX Software Engineering GmbH
+ * Author: Parthiban Nallathambi <pn@denx.de>
+ *
+ *
+ * TODO: Proximity
+ */
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/regmap.h>
+#include <linux/pm_runtime.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+#define VCNL4035_DRV_NAME	"vcnl4035"
+#define VCNL4035_IRQ_NAME	"vcnl4035_event"
+#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
+
+/* Device registers */
+#define VCNL4035_ALS_CONF	0x00
+#define VCNL4035_ALS_THDH	0x01
+#define VCNL4035_ALS_THDL	0x02
+#define VCNL4035_ALS_DATA	0x0B
+#define	VCNL4035_INT_FLAG	0x0D
+#define VCNL4035_DEV_ID		0x0E
+
+/* Register masks */
+#define VCNL4035_MODE_ALS_MASK		BIT(0)
+#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
+#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
+#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
+#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
+#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
+
+/* Default values */
+#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
+#define VCNL4035_MODE_ALS_DISABLE	0x00
+#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
+#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
+#define VCNL4035_DEV_ID_VAL		0x80
+#define VCNL4035_ALS_IT_DEFAULT		0x01
+#define VCNL4035_ALS_PERS_DEFAULT	0x00
+#define VCNL4035_ALS_THDH_DEFAULT	5000
+#define VCNL4035_ALS_THDL_DEFAULT	100
+#define VCNL4035_SLEEP_DELAY_MS		2000
+
+struct vcnl4035_data {
+	struct i2c_client *client;
+	struct regmap *regmap;
+	/* protect device settings persistence, integration time, threshold */
+	struct mutex lock;
+	unsigned int als_it_val;
+	unsigned int als_persistence;
+	unsigned int als_thresh_low;
+	unsigned int als_thresh_high;
+	struct iio_trigger *drdy_trigger0;
+	s64 irq_timestamp;
+};
+
+static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
+{
+	int ret;
+	int reg;
+
+	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
+	if (ret < 0)
+		return false;
+	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
+		return true;
+	else
+		return false;
+}
+
+static irqreturn_t vcnl4035_drdy_irq_handler(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	data->irq_timestamp = iio_get_time_ns(indio_dev);
+	return IRQ_WAKE_THREAD;
+}
+
+static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	if (vcnl4035_is_triggered(data)) {
+		iio_trigger_poll_chained(data->drdy_trigger0);
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
+/* Triggered buffer */
+static irqreturn_t vcnl4035_trigger_consumer_store_time(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+
+	if (!iio_trigger_using_own(indio_dev))
+		pf->timestamp = iio_get_time_ns(indio_dev);
+
+	return IRQ_WAKE_THREAD;
+}
+
+static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int als_data;
+	int ret;
+
+	if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
+		pf->timestamp = data->irq_timestamp;
+		data->irq_timestamp = 0;
+	}
+
+	if (!pf->timestamp)
+		pf->timestamp = iio_get_time_ns(indio_dev);
+
+	mutex_lock(&data->lock);
+	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &als_data);
+	mutex_unlock(&data->lock);
+	if (!ret) {
+		als_data = le16_to_cpu(als_data);
+		iio_push_to_buffers_with_timestamp(indio_dev,
+						   &als_data,
+						   pf->timestamp);
+	} else
+		dev_err(&data->client->dev,
+			"Trigger consumer can't read from sensor.\n");
+	pf->timestamp = 0;
+
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
+					bool enable_drdy)
+{
+	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
+					VCNL4035_MODE_ALS_INT_DISABLE;
+	int ret;
+
+	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+				 VCNL4035_MODE_ALS_INT_MASK,
+				 val);
+	if (ret)
+		dev_err(&data->client->dev, "%s failed\n", __func__);
+
+	return ret;
+}
+
+static const struct iio_trigger_ops vcnl4035_trigger_ops = {
+	.set_trigger_state = vcnl4035_als_drdy_set_state,
+};
+
+/*
+ *	Device IT	INT Time(ms)	Scale (lux/step)
+ *	000		50		0.064
+ *	001		100		0.032
+ *	010		200		0.016
+ *	100		400		0.008
+ *	101 - 111	800		0.004
+ * Values are proportial, so ALS INT is selected for input due to
+ * simplicity reason. Integration time value and scaling is
+ * calculated based on device INT value
+ *
+ * Raw value needs to be scaled using ALS STEPS
+ *
+ */
+static int vcnl4035_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan, int *val,
+			    int *val2, long mask)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+	int busy;
+	int raw_data;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		busy = iio_device_claim_direct_mode(indio_dev);
+		if (busy)
+			return -EBUSY;
+
+		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
+		iio_device_release_direct_mode(indio_dev);
+		if (ret < 0)
+			return ret;
+
+		*val = raw_data;
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_INT_TIME:
+		mutex_lock(&data->lock);
+		*val = data->als_it_val * 100;
+		if (!*val)
+			*val = 50;
+		mutex_unlock(&data->lock);
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		mutex_lock(&data->lock);
+		*val = 64;
+		if (!data->als_it_val)
+			*val2 = 1000;
+		else
+			*val2 = data->als_it_val * 2 * 1000;
+		mutex_unlock(&data->lock);
+		return IIO_VAL_FRACTIONAL;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vcnl4035_write_raw(struct iio_dev *indio_dev,
+				struct iio_chan_spec const *chan,
+				int val, int val2, long mask)
+{
+	int ret;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_INT_TIME:
+		if (val <= 0 || val > 800)
+			return -EINVAL;
+		mutex_lock(&data->lock);
+		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+					 VCNL4035_ALS_IT_MASK,
+					 val / 100);
+		if (!ret)
+			data->als_it_val = val / 100;
+		mutex_unlock(&data->lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+	return ret;
+}
+
+/* No direct ABI for persistence and threshold, so eventing */
+static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
+		const struct iio_chan_spec *chan, enum iio_event_type type,
+		enum iio_event_direction dir, enum iio_event_info info,
+		int *val, int *val2)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		switch (dir) {
+		case IIO_EV_DIR_RISING:
+			mutex_lock(&data->lock);
+			*val = data->als_thresh_high;
+			mutex_unlock(&data->lock);
+			break;
+		case IIO_EV_DIR_FALLING:
+			mutex_lock(&data->lock);
+			*val = data->als_thresh_low;
+			mutex_unlock(&data->lock);
+			break;
+		default:
+			return -EINVAL;
+		}
+		break;
+	case IIO_EV_INFO_PERIOD:
+		mutex_lock(&data->lock);
+		*val = data->als_persistence;
+		mutex_unlock(&data->lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return IIO_VAL_INT;
+}
+
+static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
+		const struct iio_chan_spec *chan, enum iio_event_type type,
+		enum iio_event_direction dir, enum iio_event_info info, int val,
+		int val2)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		/* 16 bit threshold range */
+		if (val < 0 || val > 65535)
+			return -EINVAL;
+		if (dir == IIO_EV_DIR_RISING) {
+			if (val < data->als_thresh_low)
+				return -EINVAL;
+			mutex_lock(&data->lock);
+			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
+					   val);
+			mutex_unlock(&data->lock);
+			if (ret)
+				return ret;
+			data->als_thresh_high = val;
+		} else {
+			if (val > data->als_thresh_high)
+				return -EINVAL;
+			mutex_lock(&data->lock);
+			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
+					   val);
+			mutex_unlock(&data->lock);
+			if (ret)
+				return ret;
+			data->als_thresh_low = val;
+		}
+		break;
+	case IIO_EV_INFO_PERIOD:
+		if (val < 0 || val > 3)
+			return -EINVAL;
+		mutex_lock(&data->lock);
+		/* device persistence values 1 2 4 8 mapped with 0 1 2 3 */
+		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+					 VCNL4035_ALS_PERS_MASK, 1 << val);
+		mutex_unlock(&data->lock);
+		if (ret)
+			return ret;
+		data->als_persistence = val;
+		break;
+	default:
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static IIO_CONST_ATTR(als_available_integration_time, "50 100 200 400 800");
+/* device ALS persistence 1 2 4 8 mapped to users 0 1 2 3 */
+static IIO_CONST_ATTR(als_available_persistence, "0 1 2 3");
+static IIO_CONST_ATTR(als_available_threshold_range, "0 65535");
+
+static struct attribute *vcnl4035_attributes[] = {
+	&iio_const_attr_als_available_integration_time.dev_attr.attr,
+	&iio_const_attr_als_available_threshold_range.dev_attr.attr,
+	&iio_const_attr_als_available_persistence.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group vcnl4035_attribute_group = {
+	.attrs = vcnl4035_attributes,
+};
+
+static const struct iio_info vcnl4035_info = {
+	.read_raw		= vcnl4035_read_raw,
+	.write_raw		= vcnl4035_write_raw,
+	.read_event_value	= vcnl4035_read_thresh,
+	.write_event_value	= vcnl4035_write_thresh,
+	.attrs			= &vcnl4035_attribute_group,
+};
+
+enum vcnl4035_scan_index_order {
+	VCNL4035_CHAN_INDEX_LIGHT,
+};
+
+static const unsigned long vcnl4035_available_scan_masks[] = {
+	BIT(VCNL4035_CHAN_INDEX_LIGHT), 0
+};
+
+static const struct iio_event_spec vcnl4035_event_spec[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_RISING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
+	},
+};
+
+static const struct iio_chan_spec vcnl4035_channels[] = {
+	{
+		.type = IIO_INTENSITY,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				BIT(IIO_CHAN_INFO_INT_TIME) |
+				BIT(IIO_CHAN_INFO_SCALE),
+		.event_spec = vcnl4035_event_spec,
+		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
+		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+};
+
+static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
+{
+	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+					VCNL4035_MODE_ALS_MASK,
+					status);
+}
+
+static int vcnl4035_init(struct vcnl4035_data *data)
+{
+	int ret;
+	int id;
+
+	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
+		return ret;
+	}
+
+	if (id != VCNL4035_DEV_ID_VAL) {
+		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
+			id, VCNL4035_DEV_ID_VAL);
+		return -ENODEV;
+	}
+
+#ifndef CONFIG_PM
+	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
+	if (ret < 0)
+		return ret;
+#endif
+	/* set default integration time - 100 ms for ALS */
+	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+				 VCNL4035_ALS_IT_MASK,
+				 VCNL4035_ALS_IT_DEFAULT);
+	if (ret) {
+		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
+		return ret;
+	}
+	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
+
+	/* set default persistence time - 1 for ALS */
+	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+				 VCNL4035_ALS_PERS_MASK,
+				 VCNL4035_ALS_PERS_DEFAULT);
+	if (ret) {
+		pr_err("regmap_update_bits default PERS returned %d\n", ret);
+		return ret;
+	}
+	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
+
+	/* set default HIGH threshold for ALS */
+	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
+				VCNL4035_ALS_THDH_DEFAULT);
+	if (ret) {
+		pr_err("regmap_write default THDH returned %d\n", ret);
+		return ret;
+	}
+	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
+
+	/* set default LOW threshold for ALS */
+	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
+				VCNL4035_ALS_THDL_DEFAULT);
+	if (ret) {
+		pr_err("regmap_write default THDL returned %d\n", ret);
+		return ret;
+	}
+	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
+
+	return 0;
+}
+
+static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case VCNL4035_ALS_CONF:
+	case VCNL4035_DEV_ID:
+		return false;
+	default:
+		return true;
+	}
+}
+
+static const struct regmap_config vcnl4035_regmap_config = {
+	.name		= VCNL4035_REGMAP_NAME,
+	.reg_bits	= 8,
+	.val_bits	= 16,
+	.max_register	= VCNL4035_DEV_ID,
+	.cache_type	= REGCACHE_RBTREE,
+	.volatile_reg	= vcnl4035_is_volatile_reg,
+	.val_format_endian = REGMAP_ENDIAN_LITTLE,
+};
+
+static int vcnl4035_probe(struct i2c_client *client,
+				const struct i2c_device_id *id)
+{
+	struct vcnl4035_data *data;
+	struct iio_dev *indio_dev;
+	struct regmap *regmap;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
+	if (IS_ERR(regmap)) {
+		dev_err(&client->dev, "regmap_init failed!\n");
+		return PTR_ERR(regmap);
+	}
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	data->regmap = regmap;
+	mutex_init(&data->lock);
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &vcnl4035_info;
+	indio_dev->name = VCNL4035_DRV_NAME;
+	indio_dev->channels = vcnl4035_channels;
+	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	ret = vcnl4035_init(data);
+	if (ret < 0) {
+		dev_err(&client->dev, "vcnl4035 chip init failed\n");
+		return ret;
+	}
+
+	ret = pm_runtime_set_active(&client->dev);
+	if (ret < 0)
+		goto fail_poweroff;
+
+	pm_runtime_enable(&client->dev);
+	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
+	pm_runtime_use_autosuspend(&client->dev);
+
+	if (client->irq) {
+		data->drdy_trigger0 = devm_iio_trigger_alloc(
+			indio_dev->dev.parent,
+			"%s-dev%d", indio_dev->name, indio_dev->id);
+		if (!data->drdy_trigger0) {
+			ret = -ENOMEM;
+			goto fail_pm_disable;
+		}
+		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
+		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
+		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
+		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
+
+		/* IRQ to trigger mapping */
+		ret = devm_request_threaded_irq(&client->dev, client->irq,
+			vcnl4035_drdy_irq_handler, vcnl4035_drdy_irq_thread,
+			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
+			VCNL4035_IRQ_NAME, indio_dev);
+		if (ret < 0) {
+			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
+				client->irq);
+			goto fail_pm_disable;
+			}
+
+		ret = devm_iio_trigger_register(indio_dev->dev.parent,
+						data->drdy_trigger0);
+		if (ret) {
+			dev_err(&client->dev, "iio trigger register failed\n");
+			goto fail_pm_disable;
+		}
+
+		/* Trigger setup */
+		ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent,
+			indio_dev,
+			vcnl4035_trigger_consumer_store_time,
+			vcnl4035_trigger_consumer_handler,
+			NULL);
+		if (ret < 0) {
+			dev_err(&client->dev, "iio triggered buffer setup failed\n");
+			goto fail_pm_disable;
+		}
+	}
+
+	ret = iio_device_register(indio_dev);
+	if (ret)
+		goto fail_pm_disable;
+	dev_info(&client->dev, "%s Ambient light/proximity sensor\n",
+						VCNL4035_DRV_NAME);
+	return 0;
+
+fail_pm_disable:
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+fail_poweroff:
+	return vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
+}
+
+static int vcnl4035_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+	iio_device_unregister(indio_dev);
+
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+
+	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
+					VCNL4035_MODE_ALS_DISABLE);
+}
+
+#ifdef CONFIG_PM
+static int vcnl4035_runtime_suspend(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&data->lock);
+	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
+	regcache_mark_dirty(data->regmap);
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+static int vcnl4035_runtime_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+
+	regcache_sync(data->regmap);
+	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
+	if (ret < 0)
+		return ret;
+	/* wait for 1 ALS integration cycle */
+	msleep(data->als_it_val * 100);
+
+	return 0;
+}
+#endif
+
+static const struct dev_pm_ops vcnl4035_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+				pm_runtime_force_resume)
+	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
+			   vcnl4035_runtime_resume, NULL)
+};
+
+static const struct of_device_id vcnl4035_of_match[] = {
+	{ .compatible = "vishay,vcnl4035", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
+
+static const struct i2c_device_id vcnl4035_id[] = {
+	{ "vcnl4035", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
+
+static struct i2c_driver vcnl4035_driver = {
+	.driver = {
+		.name   = VCNL4035_DRV_NAME,
+		.pm	= &vcnl4035_pm_ops,
+		.of_match_table = of_match_ptr(vcnl4035_of_match),
+	},
+	.probe  = vcnl4035_probe,
+	.remove	= vcnl4035_remove,
+	.id_table = vcnl4035_id,
+};
+
+module_i2c_driver(vcnl4035_driver);
+
+MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
+MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
+MODULE_LICENSE("GPL v2");
-- 
2.14.4


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 2/2] iio: light: Add device tree binding for vishay vcnl4035
  2018-06-28 12:30 [PATCH 1/2] iio: light: Add support for vishay vcnl4035 Parthiban Nallathambi
@ 2018-06-28 12:30 ` Parthiban Nallathambi
  2018-06-28 15:35 ` [PATCH 1/2] iio: light: Add support " kbuild test robot
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-06-28 12:30 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, robh+dt
  Cc: linux-iio, linux-kernel, mark.rutland, devicetree, matthias.bgg,
	wd, sbabic, hs, Parthiban Nallathambi

Adding device tree binding for vcnl4035 and vendor
prefix for Vishay Intertechnology

Signed-off-by: Parthiban Nallathambi <pn@denx.de>
---
 .../devicetree/bindings/iio/light/vcnl4035.txt        | 19 +++++++++++++++++++
 Documentation/devicetree/bindings/vendor-prefixes.txt |  1 +
 2 files changed, 20 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/vcnl4035.txt

diff --git a/Documentation/devicetree/bindings/iio/light/vcnl4035.txt b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
new file mode 100644
index 000000000000..7ffdc0246300
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
@@ -0,0 +1,19 @@
+VISHAY VCNL4035 -  Ambient Light and proximity sensor
+
+Link to datasheet: https://www.vishay.com/docs/84251/vcnl4035x01.pdf
+
+Required properties:
+
+	-compatible: should be "vishay,vcnl4035"
+	-reg: I2C address of the sensor, should be 0x60
+	- interrupt-parent: should be the phandle for the interrupt controller
+	- interrupts: interrupt mapping for GPIO IRQ (level active low)
+
+Example:
+
+vcnl4035@60 {
+	compatible = "vishay,vcnl4035";
+	reg = <0x60>;
+	interrupt-parent = <&gpio4>;
+	interrupts = <11 IRQ_TYPE_LEVEL_LOW>;
+};
diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
index 7cad066191ee..3cc46d5735a9 100644
--- a/Documentation/devicetree/bindings/vendor-prefixes.txt
+++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
@@ -395,6 +395,7 @@ v3	V3 Semiconductor
 variscite	Variscite Ltd.
 via	VIA Technologies, Inc.
 virtio	Virtual I/O Device Specification, developed by the OASIS consortium
+vishay	Vishay Intertechnology, Inc
 vivante	Vivante Corporation
 vocore VoCore Studio
 voipac	Voipac Technologies s.r.o.
-- 
2.14.4


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/2] iio: light: Add support for vishay vcnl4035
  2018-06-28 12:30 [PATCH 1/2] iio: light: Add support for vishay vcnl4035 Parthiban Nallathambi
  2018-06-28 12:30 ` [PATCH 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
@ 2018-06-28 15:35 ` kbuild test robot
  2018-06-29 15:38 ` [PATCH v2 " Parthiban Nallathambi
  2018-06-29 17:59 ` [PATCH " Jonathan Cameron
  3 siblings, 0 replies; 19+ messages in thread
From: kbuild test robot @ 2018-06-28 15:35 UTC (permalink / raw)
  To: Parthiban Nallathambi
  Cc: kbuild-all, jic23, knaack.h, lars, pmeerw, robh+dt, linux-iio,
	linux-kernel, mark.rutland, devicetree, matthias.bgg, wd, sbabic,
	hs, Parthiban Nallathambi

Hi Parthiban,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on iio/togreg]
[also build test WARNING on v4.18-rc2 next-20180628]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Parthiban-Nallathambi/iio-light-Add-support-for-vishay-vcnl4035/20180628-213034
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
reproduce:
        # apt-get install sparse
        make ARCH=x86_64 allmodconfig
        make C=1 CF=-D__CHECK_ENDIAN__


sparse warnings: (new ones prefixed by >>)

>> drivers/iio/light/vcnl4035.c:136:28: sparse: cast to restricted __le16

vim +136 drivers/iio/light/vcnl4035.c

   115	
   116	static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
   117	{
   118		struct iio_poll_func *pf = p;
   119		struct iio_dev *indio_dev = pf->indio_dev;
   120		struct vcnl4035_data *data = iio_priv(indio_dev);
   121		int als_data;
   122		int ret;
   123	
   124		if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
   125			pf->timestamp = data->irq_timestamp;
   126			data->irq_timestamp = 0;
   127		}
   128	
   129		if (!pf->timestamp)
   130			pf->timestamp = iio_get_time_ns(indio_dev);
   131	
   132		mutex_lock(&data->lock);
   133		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &als_data);
   134		mutex_unlock(&data->lock);
   135		if (!ret) {
 > 136			als_data = le16_to_cpu(als_data);
   137			iio_push_to_buffers_with_timestamp(indio_dev,
   138							   &als_data,
   139							   pf->timestamp);
   140		} else
   141			dev_err(&data->client->dev,
   142				"Trigger consumer can't read from sensor.\n");
   143		pf->timestamp = 0;
   144	
   145		iio_trigger_notify_done(indio_dev->trig);
   146	
   147		return IRQ_HANDLED;
   148	}
   149	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v2 1/2] iio: light: Add support for vishay vcnl4035
  2018-06-28 12:30 [PATCH 1/2] iio: light: Add support for vishay vcnl4035 Parthiban Nallathambi
  2018-06-28 12:30 ` [PATCH 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
  2018-06-28 15:35 ` [PATCH 1/2] iio: light: Add support " kbuild test robot
@ 2018-06-29 15:38 ` Parthiban Nallathambi
  2018-06-29 15:38   ` [PATCH v2 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
                     ` (3 more replies)
  2018-06-29 17:59 ` [PATCH " Jonathan Cameron
  3 siblings, 4 replies; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-06-29 15:38 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, robh+dt
  Cc: linux-iio, linux-kernel, mark.rutland, devicetree, matthias.bgg,
	wd, sbabic, hs, Parthiban Nallathambi

Add support for VCNL4035, which is capable of Ambient light
sensing (ALS) and proximity function. This patch adds support
only for ALS function

Signed-off-by: Parthiban Nallathambi <pn@denx.de>
---

Changelog since v1:

1. Fixed 0-day warning on le16_to_cpu usage
2. Persistence value is directly mapped to datasheet's value to
avoid confusions of usage from sysfs
---
 drivers/iio/light/Kconfig    |  12 +
 drivers/iio/light/Makefile   |   1 +
 drivers/iio/light/vcnl4035.c | 682 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 695 insertions(+)
 create mode 100644 drivers/iio/light/vcnl4035.c

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index c7ef8d1862d6..b7069a4c28a2 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -447,6 +447,18 @@ config VCNL4000
 	 To compile this driver as a module, choose M here: the
 	 module will be called vcnl4000.
 
+config VCNL4035
+	tristate "VCNL4035 combined ALS and proximity sensor"
+	select REGMAP_I2C
+	depends on I2C
+	help
+	 Say Y here if you want to build a driver for the Vishay VCNL4035,
+	 combined ambient light (ALS) and proximity sensor. Currently only ALS
+	 function is available.
+
+	 To compile this driver as a module, choose M here: the
+	 module will be called vcnl4035.
+
 config VEML6070
 	tristate "VEML6070 UV A light sensor"
 	depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 80943af5d627..dce98511a59b 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
 obj-$(CONFIG_TSL4531)		+= tsl4531.o
 obj-$(CONFIG_US5182D)		+= us5182d.o
 obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
+obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
 obj-$(CONFIG_VEML6070)		+= veml6070.o
 obj-$(CONFIG_VL6180)		+= vl6180.o
 obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
new file mode 100644
index 000000000000..45eccb462ed0
--- /dev/null
+++ b/drivers/iio/light/vcnl4035.c
@@ -0,0 +1,682 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
+ *
+ * Copyright (c) 2018, DENX Software Engineering GmbH
+ * Author: Parthiban Nallathambi <pn@denx.de>
+ *
+ *
+ * TODO: Proximity
+ */
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/regmap.h>
+#include <linux/pm_runtime.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+#define VCNL4035_DRV_NAME	"vcnl4035"
+#define VCNL4035_IRQ_NAME	"vcnl4035_event"
+#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
+
+/* Device registers */
+#define VCNL4035_ALS_CONF	0x00
+#define VCNL4035_ALS_THDH	0x01
+#define VCNL4035_ALS_THDL	0x02
+#define VCNL4035_ALS_DATA	0x0B
+#define	VCNL4035_INT_FLAG	0x0D
+#define VCNL4035_DEV_ID		0x0E
+
+/* Register masks */
+#define VCNL4035_MODE_ALS_MASK		BIT(0)
+#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
+#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
+#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
+#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
+#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
+
+/* Default values */
+#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
+#define VCNL4035_MODE_ALS_DISABLE	0x00
+#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
+#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
+#define VCNL4035_DEV_ID_VAL		0x80
+#define VCNL4035_ALS_IT_DEFAULT		0x01
+#define VCNL4035_ALS_PERS_DEFAULT	0x00
+#define VCNL4035_ALS_THDH_DEFAULT	5000
+#define VCNL4035_ALS_THDL_DEFAULT	100
+#define VCNL4035_SLEEP_DELAY_MS		2000
+
+struct vcnl4035_data {
+	struct i2c_client *client;
+	struct regmap *regmap;
+	/* protect device settings persistence, integration time, threshold */
+	struct mutex lock;
+	unsigned int als_it_val;
+	unsigned int als_persistence:4;
+	unsigned int als_thresh_low;
+	unsigned int als_thresh_high;
+	struct iio_trigger *drdy_trigger0;
+	s64 irq_timestamp;
+};
+
+static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
+{
+	int ret;
+	int reg;
+
+	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
+	if (ret < 0)
+		return false;
+	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
+		return true;
+	else
+		return false;
+}
+
+static irqreturn_t vcnl4035_drdy_irq_handler(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	data->irq_timestamp = iio_get_time_ns(indio_dev);
+	return IRQ_WAKE_THREAD;
+}
+
+static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	if (vcnl4035_is_triggered(data)) {
+		iio_trigger_poll_chained(data->drdy_trigger0);
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
+/* Triggered buffer */
+static irqreturn_t vcnl4035_trigger_consumer_store_time(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+
+	if (!iio_trigger_using_own(indio_dev))
+		pf->timestamp = iio_get_time_ns(indio_dev);
+
+	return IRQ_WAKE_THREAD;
+}
+
+static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int als_data;
+	int ret;
+
+	if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
+		pf->timestamp = data->irq_timestamp;
+		data->irq_timestamp = 0;
+	}
+
+	if (!pf->timestamp)
+		pf->timestamp = iio_get_time_ns(indio_dev);
+
+	mutex_lock(&data->lock);
+	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &als_data);
+	mutex_unlock(&data->lock);
+	if (!ret)
+		iio_push_to_buffers_with_timestamp(indio_dev,
+						   &als_data,
+						   pf->timestamp);
+	else
+		dev_err(&data->client->dev,
+			"Trigger consumer can't read from sensor.\n");
+	pf->timestamp = 0;
+
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
+					bool enable_drdy)
+{
+	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
+					VCNL4035_MODE_ALS_INT_DISABLE;
+	int ret;
+
+	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+				 VCNL4035_MODE_ALS_INT_MASK,
+				 val);
+	if (ret)
+		dev_err(&data->client->dev, "%s failed\n", __func__);
+
+	return ret;
+}
+
+static const struct iio_trigger_ops vcnl4035_trigger_ops = {
+	.set_trigger_state = vcnl4035_als_drdy_set_state,
+};
+
+/*
+ *	Device IT	INT Time(ms)	Scale (lux/step)
+ *	000		50		0.064
+ *	001		100		0.032
+ *	010		200		0.016
+ *	100		400		0.008
+ *	101 - 111	800		0.004
+ * Values are proportial, so ALS INT is selected for input due to
+ * simplicity reason. Integration time value and scaling is
+ * calculated based on device INT value
+ *
+ * Raw value needs to be scaled using ALS STEPS
+ *
+ */
+static int vcnl4035_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan, int *val,
+			    int *val2, long mask)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+	int busy;
+	int raw_data;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		busy = iio_device_claim_direct_mode(indio_dev);
+		if (busy)
+			return -EBUSY;
+
+		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
+		iio_device_release_direct_mode(indio_dev);
+		if (ret < 0)
+			return ret;
+
+		*val = raw_data;
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_INT_TIME:
+		mutex_lock(&data->lock);
+		*val = data->als_it_val * 100;
+		if (!*val)
+			*val = 50;
+		mutex_unlock(&data->lock);
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		mutex_lock(&data->lock);
+		*val = 64;
+		if (!data->als_it_val)
+			*val2 = 1000;
+		else
+			*val2 = data->als_it_val * 2 * 1000;
+		mutex_unlock(&data->lock);
+		return IIO_VAL_FRACTIONAL;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vcnl4035_write_raw(struct iio_dev *indio_dev,
+				struct iio_chan_spec const *chan,
+				int val, int val2, long mask)
+{
+	int ret;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_INT_TIME:
+		if (val <= 0 || val > 800)
+			return -EINVAL;
+		mutex_lock(&data->lock);
+		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+					 VCNL4035_ALS_IT_MASK,
+					 val / 100);
+		if (!ret)
+			data->als_it_val = val / 100;
+		mutex_unlock(&data->lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+	return ret;
+}
+
+/* No direct ABI for persistence and threshold, so eventing */
+static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
+		const struct iio_chan_spec *chan, enum iio_event_type type,
+		enum iio_event_direction dir, enum iio_event_info info,
+		int *val, int *val2)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		switch (dir) {
+		case IIO_EV_DIR_RISING:
+			mutex_lock(&data->lock);
+			*val = data->als_thresh_high;
+			mutex_unlock(&data->lock);
+			break;
+		case IIO_EV_DIR_FALLING:
+			mutex_lock(&data->lock);
+			*val = data->als_thresh_low;
+			mutex_unlock(&data->lock);
+			break;
+		default:
+			return -EINVAL;
+		}
+		break;
+	case IIO_EV_INFO_PERIOD:
+		mutex_lock(&data->lock);
+		*val = data->als_persistence;
+		mutex_unlock(&data->lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return IIO_VAL_INT;
+}
+
+static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
+		const struct iio_chan_spec *chan, enum iio_event_type type,
+		enum iio_event_direction dir, enum iio_event_info info, int val,
+		int val2)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		/* 16 bit threshold range */
+		if (val < 0 || val > 65535)
+			return -EINVAL;
+		if (dir == IIO_EV_DIR_RISING) {
+			if (val < data->als_thresh_low)
+				return -EINVAL;
+			mutex_lock(&data->lock);
+			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
+					   val);
+			mutex_unlock(&data->lock);
+			if (ret)
+				return ret;
+			data->als_thresh_high = val;
+		} else {
+			if (val > data->als_thresh_high)
+				return -EINVAL;
+			mutex_lock(&data->lock);
+			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
+					   val);
+			mutex_unlock(&data->lock);
+			if (ret)
+				return ret;
+			data->als_thresh_low = val;
+		}
+		break;
+	case IIO_EV_INFO_PERIOD:
+		/* allow only 1 2 4 8 as persistence value */
+		if (val < 0 || val > 8 || (__sw_hweight8(val) != 1))
+			return -EINVAL;
+		mutex_lock(&data->lock);
+		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+					 VCNL4035_ALS_PERS_MASK, val);
+		mutex_unlock(&data->lock);
+		if (ret)
+			return ret;
+		data->als_persistence = val;
+		break;
+	default:
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static IIO_CONST_ATTR(als_available_integration_time, "50 100 200 400 800");
+static IIO_CONST_ATTR(als_available_persistence, "1 2 4 8");
+static IIO_CONST_ATTR(als_available_threshold_range, "0 65535");
+
+static struct attribute *vcnl4035_attributes[] = {
+	&iio_const_attr_als_available_integration_time.dev_attr.attr,
+	&iio_const_attr_als_available_threshold_range.dev_attr.attr,
+	&iio_const_attr_als_available_persistence.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group vcnl4035_attribute_group = {
+	.attrs = vcnl4035_attributes,
+};
+
+static const struct iio_info vcnl4035_info = {
+	.read_raw		= vcnl4035_read_raw,
+	.write_raw		= vcnl4035_write_raw,
+	.read_event_value	= vcnl4035_read_thresh,
+	.write_event_value	= vcnl4035_write_thresh,
+	.attrs			= &vcnl4035_attribute_group,
+};
+
+enum vcnl4035_scan_index_order {
+	VCNL4035_CHAN_INDEX_LIGHT,
+};
+
+static const unsigned long vcnl4035_available_scan_masks[] = {
+	BIT(VCNL4035_CHAN_INDEX_LIGHT), 0
+};
+
+static const struct iio_event_spec vcnl4035_event_spec[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_RISING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
+	},
+};
+
+static const struct iio_chan_spec vcnl4035_channels[] = {
+	{
+		.type = IIO_INTENSITY,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				BIT(IIO_CHAN_INFO_INT_TIME) |
+				BIT(IIO_CHAN_INFO_SCALE),
+		.event_spec = vcnl4035_event_spec,
+		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
+		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+};
+
+static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
+{
+	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+					VCNL4035_MODE_ALS_MASK,
+					status);
+}
+
+static int vcnl4035_init(struct vcnl4035_data *data)
+{
+	int ret;
+	int id;
+
+	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
+		return ret;
+	}
+
+	if (id != VCNL4035_DEV_ID_VAL) {
+		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
+			id, VCNL4035_DEV_ID_VAL);
+		return -ENODEV;
+	}
+
+#ifndef CONFIG_PM
+	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
+	if (ret < 0)
+		return ret;
+#endif
+	/* set default integration time - 100 ms for ALS */
+	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+				 VCNL4035_ALS_IT_MASK,
+				 VCNL4035_ALS_IT_DEFAULT);
+	if (ret) {
+		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
+		return ret;
+	}
+	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
+
+	/* set default persistence time - 1 for ALS */
+	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+				 VCNL4035_ALS_PERS_MASK,
+				 VCNL4035_ALS_PERS_DEFAULT);
+	if (ret) {
+		pr_err("regmap_update_bits default PERS returned %d\n", ret);
+		return ret;
+	}
+	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
+
+	/* set default HIGH threshold for ALS */
+	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
+				VCNL4035_ALS_THDH_DEFAULT);
+	if (ret) {
+		pr_err("regmap_write default THDH returned %d\n", ret);
+		return ret;
+	}
+	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
+
+	/* set default LOW threshold for ALS */
+	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
+				VCNL4035_ALS_THDL_DEFAULT);
+	if (ret) {
+		pr_err("regmap_write default THDL returned %d\n", ret);
+		return ret;
+	}
+	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
+
+	return 0;
+}
+
+static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case VCNL4035_ALS_CONF:
+	case VCNL4035_DEV_ID:
+		return false;
+	default:
+		return true;
+	}
+}
+
+static const struct regmap_config vcnl4035_regmap_config = {
+	.name		= VCNL4035_REGMAP_NAME,
+	.reg_bits	= 8,
+	.val_bits	= 16,
+	.max_register	= VCNL4035_DEV_ID,
+	.cache_type	= REGCACHE_RBTREE,
+	.volatile_reg	= vcnl4035_is_volatile_reg,
+	.val_format_endian = REGMAP_ENDIAN_LITTLE,
+};
+
+static int vcnl4035_probe(struct i2c_client *client,
+				const struct i2c_device_id *id)
+{
+	struct vcnl4035_data *data;
+	struct iio_dev *indio_dev;
+	struct regmap *regmap;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
+	if (IS_ERR(regmap)) {
+		dev_err(&client->dev, "regmap_init failed!\n");
+		return PTR_ERR(regmap);
+	}
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	data->regmap = regmap;
+	mutex_init(&data->lock);
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &vcnl4035_info;
+	indio_dev->name = VCNL4035_DRV_NAME;
+	indio_dev->channels = vcnl4035_channels;
+	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	ret = vcnl4035_init(data);
+	if (ret < 0) {
+		dev_err(&client->dev, "vcnl4035 chip init failed\n");
+		return ret;
+	}
+
+	ret = pm_runtime_set_active(&client->dev);
+	if (ret < 0)
+		goto fail_poweroff;
+
+	pm_runtime_enable(&client->dev);
+	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
+	pm_runtime_use_autosuspend(&client->dev);
+
+	if (client->irq) {
+		data->drdy_trigger0 = devm_iio_trigger_alloc(
+			indio_dev->dev.parent,
+			"%s-dev%d", indio_dev->name, indio_dev->id);
+		if (!data->drdy_trigger0) {
+			ret = -ENOMEM;
+			goto fail_pm_disable;
+		}
+		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
+		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
+		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
+		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
+
+		/* IRQ to trigger mapping */
+		ret = devm_request_threaded_irq(&client->dev, client->irq,
+			vcnl4035_drdy_irq_handler, vcnl4035_drdy_irq_thread,
+			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
+			VCNL4035_IRQ_NAME, indio_dev);
+		if (ret < 0) {
+			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
+				client->irq);
+			goto fail_pm_disable;
+			}
+
+		ret = devm_iio_trigger_register(indio_dev->dev.parent,
+						data->drdy_trigger0);
+		if (ret) {
+			dev_err(&client->dev, "iio trigger register failed\n");
+			goto fail_pm_disable;
+		}
+
+		/* Trigger setup */
+		ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent,
+			indio_dev,
+			vcnl4035_trigger_consumer_store_time,
+			vcnl4035_trigger_consumer_handler,
+			NULL);
+		if (ret < 0) {
+			dev_err(&client->dev, "iio triggered buffer setup failed\n");
+			goto fail_pm_disable;
+		}
+	}
+
+	ret = iio_device_register(indio_dev);
+	if (ret)
+		goto fail_pm_disable;
+	dev_info(&client->dev, "%s Ambient light/proximity sensor\n",
+						VCNL4035_DRV_NAME);
+	return 0;
+
+fail_pm_disable:
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+fail_poweroff:
+	return vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
+}
+
+static int vcnl4035_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+	iio_device_unregister(indio_dev);
+
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+
+	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
+					VCNL4035_MODE_ALS_DISABLE);
+}
+
+#ifdef CONFIG_PM
+static int vcnl4035_runtime_suspend(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&data->lock);
+	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
+	regcache_mark_dirty(data->regmap);
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+static int vcnl4035_runtime_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+
+	regcache_sync(data->regmap);
+	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
+	if (ret < 0)
+		return ret;
+	/* wait for 1 ALS integration cycle */
+	msleep(data->als_it_val * 100);
+
+	return 0;
+}
+#endif
+
+static const struct dev_pm_ops vcnl4035_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+				pm_runtime_force_resume)
+	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
+			   vcnl4035_runtime_resume, NULL)
+};
+
+static const struct of_device_id vcnl4035_of_match[] = {
+	{ .compatible = "vishay,vcnl4035", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
+
+static const struct i2c_device_id vcnl4035_id[] = {
+	{ "vcnl4035", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
+
+static struct i2c_driver vcnl4035_driver = {
+	.driver = {
+		.name   = VCNL4035_DRV_NAME,
+		.pm	= &vcnl4035_pm_ops,
+		.of_match_table = of_match_ptr(vcnl4035_of_match),
+	},
+	.probe  = vcnl4035_probe,
+	.remove	= vcnl4035_remove,
+	.id_table = vcnl4035_id,
+};
+
+module_i2c_driver(vcnl4035_driver);
+
+MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
+MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
+MODULE_LICENSE("GPL v2");
-- 
2.14.4


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 2/2] iio: light: Add device tree binding for vishay vcnl4035
  2018-06-29 15:38 ` [PATCH v2 " Parthiban Nallathambi
@ 2018-06-29 15:38   ` Parthiban Nallathambi
  2018-07-06 20:34     ` Rob Herring
  2018-06-29 23:46   ` [PATCH v2 1/2] iio: light: Add support " kbuild test robot
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-06-29 15:38 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, robh+dt
  Cc: linux-iio, linux-kernel, mark.rutland, devicetree, matthias.bgg,
	wd, sbabic, hs, Parthiban Nallathambi

Adding device tree binding for vcnl4035 and vendor
prefix for Vishay Intertechnology

Signed-off-by: Parthiban Nallathambi <pn@denx.de>
---
 .../devicetree/bindings/iio/light/vcnl4035.txt        | 19 +++++++++++++++++++
 Documentation/devicetree/bindings/vendor-prefixes.txt |  1 +
 2 files changed, 20 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/vcnl4035.txt

diff --git a/Documentation/devicetree/bindings/iio/light/vcnl4035.txt b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
new file mode 100644
index 000000000000..7ffdc0246300
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
@@ -0,0 +1,19 @@
+VISHAY VCNL4035 -  Ambient Light and proximity sensor
+
+Link to datasheet: https://www.vishay.com/docs/84251/vcnl4035x01.pdf
+
+Required properties:
+
+	-compatible: should be "vishay,vcnl4035"
+	-reg: I2C address of the sensor, should be 0x60
+	- interrupt-parent: should be the phandle for the interrupt controller
+	- interrupts: interrupt mapping for GPIO IRQ (level active low)
+
+Example:
+
+vcnl4035@60 {
+	compatible = "vishay,vcnl4035";
+	reg = <0x60>;
+	interrupt-parent = <&gpio4>;
+	interrupts = <11 IRQ_TYPE_LEVEL_LOW>;
+};
diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
index 7cad066191ee..3cc46d5735a9 100644
--- a/Documentation/devicetree/bindings/vendor-prefixes.txt
+++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
@@ -395,6 +395,7 @@ v3	V3 Semiconductor
 variscite	Variscite Ltd.
 via	VIA Technologies, Inc.
 virtio	Virtual I/O Device Specification, developed by the OASIS consortium
+vishay	Vishay Intertechnology, Inc
 vivante	Vivante Corporation
 vocore VoCore Studio
 voipac	Voipac Technologies s.r.o.
-- 
2.14.4


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/2] iio: light: Add support for vishay vcnl4035
  2018-06-28 12:30 [PATCH 1/2] iio: light: Add support for vishay vcnl4035 Parthiban Nallathambi
                   ` (2 preceding siblings ...)
  2018-06-29 15:38 ` [PATCH v2 " Parthiban Nallathambi
@ 2018-06-29 17:59 ` Jonathan Cameron
  2018-07-03 13:35   ` Parthiban Nallathambi
  3 siblings, 1 reply; 19+ messages in thread
From: Jonathan Cameron @ 2018-06-29 17:59 UTC (permalink / raw)
  To: Parthiban Nallathambi
  Cc: jic23, knaack.h, lars, pmeerw, robh+dt, linux-iio, linux-kernel,
	mark.rutland, devicetree, matthias.bgg, wd, sbabic, hs

On Thu, 28 Jun 2018 14:30:20 +0200
Parthiban Nallathambi <pn@denx.de> wrote:

> Add support for VCNL4035, which is capable of Ambient light
> sensing (ALS) and proximity function. This patch adds support
> only for ALS function
> 
> Signed-off-by: Parthiban Nallathambi <pn@denx.de>

Hi,

Looks fairly clean in general, but a few questions and minor suggestions inline.

There is some non standard ABI in here for which I'd like to have seen documentation.

Jonathan
> ---
>  drivers/iio/light/Kconfig    |  12 +
>  drivers/iio/light/Makefile   |   1 +
>  drivers/iio/light/vcnl4035.c | 684 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 697 insertions(+)
>  create mode 100644 drivers/iio/light/vcnl4035.c
> 
> diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
> index c7ef8d1862d6..b7069a4c28a2 100644
> --- a/drivers/iio/light/Kconfig
> +++ b/drivers/iio/light/Kconfig
> @@ -447,6 +447,18 @@ config VCNL4000
>  	 To compile this driver as a module, choose M here: the
>  	 module will be called vcnl4000.
>  
> +config VCNL4035
> +	tristate "VCNL4035 combined ALS and proximity sensor"
> +	select REGMAP_I2C
> +	depends on I2C
> +	help
> +	 Say Y here if you want to build a driver for the Vishay VCNL4035,
> +	 combined ambient light (ALS) and proximity sensor. Currently only ALS
> +	 function is available.
> +
> +	 To compile this driver as a module, choose M here: the
> +	 module will be called vcnl4035.
> +
>  config VEML6070
>  	tristate "VEML6070 UV A light sensor"
>  	depends on I2C
> diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
> index 80943af5d627..dce98511a59b 100644
> --- a/drivers/iio/light/Makefile
> +++ b/drivers/iio/light/Makefile
> @@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
>  obj-$(CONFIG_TSL4531)		+= tsl4531.o
>  obj-$(CONFIG_US5182D)		+= us5182d.o
>  obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
> +obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
>  obj-$(CONFIG_VEML6070)		+= veml6070.o
>  obj-$(CONFIG_VL6180)		+= vl6180.o
>  obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
> diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
> new file mode 100644
> index 000000000000..fc19856f965b
> --- /dev/null
> +++ b/drivers/iio/light/vcnl4035.c
> @@ -0,0 +1,684 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
> + *
> + * Copyright (c) 2018, DENX Software Engineering GmbH
> + * Author: Parthiban Nallathambi <pn@denx.de>
> + *
> + *
> + * TODO: Proximity
> + */
> +#include <linux/module.h>
> +#include <linux/i2c.h>
> +#include <linux/regmap.h>
> +#include <linux/pm_runtime.h>
> +
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/iio/triggered_buffer.h>
> +
> +#define VCNL4035_DRV_NAME	"vcnl4035"
> +#define VCNL4035_IRQ_NAME	"vcnl4035_event"
> +#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
> +
> +/* Device registers */
> +#define VCNL4035_ALS_CONF	0x00
> +#define VCNL4035_ALS_THDH	0x01
> +#define VCNL4035_ALS_THDL	0x02
> +#define VCNL4035_ALS_DATA	0x0B
> +#define	VCNL4035_INT_FLAG	0x0D
> +#define VCNL4035_DEV_ID		0x0E
> +
> +/* Register masks */
> +#define VCNL4035_MODE_ALS_MASK		BIT(0)
> +#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
> +#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
> +#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
> +#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
> +#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
> +
> +/* Default values */
> +#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
> +#define VCNL4035_MODE_ALS_DISABLE	0x00
> +#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
> +#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
> +#define VCNL4035_DEV_ID_VAL		0x80
> +#define VCNL4035_ALS_IT_DEFAULT		0x01
> +#define VCNL4035_ALS_PERS_DEFAULT	0x00
> +#define VCNL4035_ALS_THDH_DEFAULT	5000
> +#define VCNL4035_ALS_THDL_DEFAULT	100
> +#define VCNL4035_SLEEP_DELAY_MS		2000
> +
> +struct vcnl4035_data {
> +	struct i2c_client *client;
> +	struct regmap *regmap;
> +	/* protect device settings persistence, integration time, threshold */
> +	struct mutex lock;
> +	unsigned int als_it_val;
> +	unsigned int als_persistence;
> +	unsigned int als_thresh_low;
> +	unsigned int als_thresh_high;
> +	struct iio_trigger *drdy_trigger0;
> +	s64 irq_timestamp;
> +};
> +
> +static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
> +{
> +	int ret;
> +	int reg;
> +
> +	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
> +	if (ret < 0)
> +		return false;
> +	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
> +		return true;
> +	else
> +		return false;
> +}
> +
> +static irqreturn_t vcnl4035_drdy_irq_handler(int irq, void *private)
> +{
> +	struct iio_dev *indio_dev = private;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	data->irq_timestamp = iio_get_time_ns(indio_dev);

This all seems a bit more complex that I'd expect.
I would like some comments on why it is so complex..

Only reason I can come up with quickly is to handle shared interrupts?
But if that is the case you'll sometimes get the wrong timestamp here.

Otherwise, right now we only have one cause of interrupts and 
might as well just set pf->timestamp directly here.

> +	return IRQ_WAKE_THREAD;
> +}
> +
> +static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
> +{
> +	struct iio_dev *indio_dev = private;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	if (vcnl4035_is_triggered(data)) {
> +		iio_trigger_poll_chained(data->drdy_trigger0);
> +		return IRQ_HANDLED;
> +	}
> +
> +	return IRQ_NONE;
> +}
> +
> +/* Triggered buffer */
> +static irqreturn_t vcnl4035_trigger_consumer_store_time(int irq, void *p)
> +{
> +	struct iio_poll_func *pf = p;
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +
> +	if (!iio_trigger_using_own(indio_dev))
> +		pf->timestamp = iio_get_time_ns(indio_dev);
> +
> +	return IRQ_WAKE_THREAD;
> +}
> +
> +static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
> +{
> +	struct iio_poll_func *pf = p;
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int als_data;
> +	int ret;
> +
> +	if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
> +		pf->timestamp = data->irq_timestamp;
> +		data->irq_timestamp = 0;

If we get here and the path was anything other than via our top half
handler that set this then something odd is going on.

> +	}
> +
> +	if (!pf->timestamp)
> +		pf->timestamp = iio_get_time_ns(indio_dev);

Hmm. Why would we hit here when using our own trigger?  If not make this
a simple else and it will be less confusing.

> +
> +	mutex_lock(&data->lock);
> +	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &als_data);
> +	mutex_unlock(&data->lock);
> +	if (!ret) {
> +		als_data = le16_to_cpu(als_data);
As the autobuilder pointed out, use a local variable of the correct
endianess.

> +		iio_push_to_buffers_with_timestamp(indio_dev,
> +						   &als_data,
> +						   pf->timestamp);
> +	} else
> +		dev_err(&data->client->dev,
> +			"Trigger consumer can't read from sensor.\n");
> +	pf->timestamp = 0;
> +
> +	iio_trigger_notify_done(indio_dev->trig);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
> +					bool enable_drdy)
> +{
> +	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
> +					VCNL4035_MODE_ALS_INT_DISABLE;
> +	int ret;
> +
> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +				 VCNL4035_MODE_ALS_INT_MASK,
> +				 val);
> +	if (ret)
> +		dev_err(&data->client->dev, "%s failed\n", __func__);

I believe you can configure dev_err to output the function name anyway
so not a lot of point of putting it here explicitly.

> +
> +	return ret;
> +}
> +
> +static const struct iio_trigger_ops vcnl4035_trigger_ops = {
> +	.set_trigger_state = vcnl4035_als_drdy_set_state,
> +};
> +
> +/*
> + *	Device IT	INT Time(ms)	Scale (lux/step)
> + *	000		50		0.064
> + *	001		100		0.032
> + *	010		200		0.016
> + *	100		400		0.008
> + *	101 - 111	800		0.004
> + * Values are proportial, so ALS INT is selected for input due to
> + * simplicity reason. Integration time value and scaling is
> + * calculated based on device INT value
> + *
> + * Raw value needs to be scaled using ALS STEPS
> + *
> + */
> +static int vcnl4035_read_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan, int *val,
> +			    int *val2, long mask)
> +{
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +	int busy;
> +	int raw_data;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		busy = iio_device_claim_direct_mode(indio_dev);
> +		if (busy)
> +			return -EBUSY;
why not use ret = iio_device_claim...
and return ret?  (which will be EBUSY if it's busy IIRC)
> +
> +		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
> +		iio_device_release_direct_mode(indio_dev);
> +		if (ret < 0)
> +			return ret;
> +
> +		*val = raw_data;
> +		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_INT_TIME:
> +		mutex_lock(&data->lock);
> +		*val = data->als_it_val * 100;
> +		if (!*val)
> +			*val = 50;
> +		mutex_unlock(&data->lock);
> +		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_SCALE:
> +		mutex_lock(&data->lock);
> +		*val = 64;
> +		if (!data->als_it_val)
> +			*val2 = 1000;
> +		else
> +			*val2 = data->als_it_val * 2 * 1000;
> +		mutex_unlock(&data->lock);
> +		return IIO_VAL_FRACTIONAL;
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int vcnl4035_write_raw(struct iio_dev *indio_dev,
> +				struct iio_chan_spec const *chan,
> +				int val, int val2, long mask)
> +{
> +	int ret;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_INT_TIME:
> +		if (val <= 0 || val > 800)
> +			return -EINVAL;
> +		mutex_lock(&data->lock);
> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +					 VCNL4035_ALS_IT_MASK,
> +					 val / 100);
> +		if (!ret)
> +			data->als_it_val = val / 100;
> +		mutex_unlock(&data->lock);
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +	return ret;
> +}
> +
> +/* No direct ABI for persistence and threshold, so eventing */
> +static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
> +		const struct iio_chan_spec *chan, enum iio_event_type type,
> +		enum iio_event_direction dir, enum iio_event_info info,
> +		int *val, int *val2)
> +{
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	switch (info) {
> +	case IIO_EV_INFO_VALUE:
> +		switch (dir) {
> +		case IIO_EV_DIR_RISING:
> +			mutex_lock(&data->lock);
> +			*val = data->als_thresh_high;
> +			mutex_unlock(&data->lock);
> +			break;
> +		case IIO_EV_DIR_FALLING:
> +			mutex_lock(&data->lock);
> +			*val = data->als_thresh_low;
> +			mutex_unlock(&data->lock);
> +			break;
> +		default:
> +			return -EINVAL;
> +		}
> +		break;
> +	case IIO_EV_INFO_PERIOD:
> +		mutex_lock(&data->lock);
> +		*val = data->als_persistence;
> +		mutex_unlock(&data->lock);

Given mutex is taken in all paths, why not take it outside
the switch?  Mind you I'm not totally sure why we need to take
the lock at all.  We don't take it whilst setting these values.


> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
> +		const struct iio_chan_spec *chan, enum iio_event_type type,
> +		enum iio_event_direction dir, enum iio_event_info info, int val,
> +		int val2)
> +{
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	switch (info) {
> +	case IIO_EV_INFO_VALUE:
> +		/* 16 bit threshold range */
> +		if (val < 0 || val > 65535)
> +			return -EINVAL;
> +		if (dir == IIO_EV_DIR_RISING) {
> +			if (val < data->als_thresh_low)
> +				return -EINVAL;
> +			mutex_lock(&data->lock);
> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
> +					   val);
> +			mutex_unlock(&data->lock);
> +			if (ret)
> +				return ret;
> +			data->als_thresh_high = val;
> +		} else {
> +			if (val > data->als_thresh_high)
> +				return -EINVAL;
> +			mutex_lock(&data->lock);
> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
> +					   val);
> +			mutex_unlock(&data->lock);
> +			if (ret)
> +				return ret;
> +			data->als_thresh_low = val;
> +		}
> +		break;
> +	case IIO_EV_INFO_PERIOD:
> +		if (val < 0 || val > 3)
> +			return -EINVAL;
> +		mutex_lock(&data->lock);
> +		/* device persistence values 1 2 4 8 mapped with 0 1 2 3 */
> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +					 VCNL4035_ALS_PERS_MASK, 1 << val);
> +		mutex_unlock(&data->lock);
> +		if (ret)
> +			return ret;
> +		data->als_persistence = val;
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +	return 0;
> +}
> +
> +static IIO_CONST_ATTR(als_available_integration_time, "50 100 200 400 800");
> +/* device ALS persistence 1 2 4 8 mapped to users 0 1 2 3 */
> +static IIO_CONST_ATTR(als_available_persistence, "0 1 2 3");
> +static IIO_CONST_ATTR(als_available_threshold_range, "0 65535");
> +
> +static struct attribute *vcnl4035_attributes[] = {
> +	&iio_const_attr_als_available_integration_time.dev_attr.attr,

These don't comply with the abi which has available at the end not the start.

> +	&iio_const_attr_als_available_threshold_range.dev_attr.attr,

I'm not even sure what these two are...

All new ABI or ABI that is currently specific to too narrow a range of
parts must be documented in /Documentation/ABI/testing/sysfs-bus-iio-*

> +	&iio_const_attr_als_available_persistence.dev_attr.attr,
> +	NULL,
> +};
> +
> +static const struct attribute_group vcnl4035_attribute_group = {
> +	.attrs = vcnl4035_attributes,
> +};
> +
> +static const struct iio_info vcnl4035_info = {
> +	.read_raw		= vcnl4035_read_raw,
> +	.write_raw		= vcnl4035_write_raw,
> +	.read_event_value	= vcnl4035_read_thresh,
> +	.write_event_value	= vcnl4035_write_thresh,
> +	.attrs			= &vcnl4035_attribute_group,
> +};
> +
> +enum vcnl4035_scan_index_order {
> +	VCNL4035_CHAN_INDEX_LIGHT,
> +};
> +
> +static const unsigned long vcnl4035_available_scan_masks[] = {
> +	BIT(VCNL4035_CHAN_INDEX_LIGHT), 0
> +};
> +
> +static const struct iio_event_spec vcnl4035_event_spec[] = {
> +	{
> +		.type = IIO_EV_TYPE_THRESH,
> +		.dir = IIO_EV_DIR_RISING,
> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
> +	}, {
> +		.type = IIO_EV_TYPE_THRESH,
> +		.dir = IIO_EV_DIR_FALLING,
> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
> +	}, {
> +		.type = IIO_EV_TYPE_THRESH,
> +		.dir = IIO_EV_DIR_EITHER,
> +		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
> +	},
> +};
> +
> +static const struct iio_chan_spec vcnl4035_channels[] = {
> +	{
> +		.type = IIO_INTENSITY,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				BIT(IIO_CHAN_INFO_INT_TIME) |
> +				BIT(IIO_CHAN_INFO_SCALE),
> +		.event_spec = vcnl4035_event_spec,
> +		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
> +		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
> +		.scan_type = {
> +			.sign = 'u',
> +			.realbits = 16,
> +			.storagebits = 16,
> +			.endianness = IIO_LE,
> +		},
> +	},
> +};
> +
> +static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
> +{
> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +					VCNL4035_MODE_ALS_MASK,
> +					status);
> +}
> +
> +static int vcnl4035_init(struct vcnl4035_data *data)
> +{
> +	int ret;
> +	int id;
> +
> +	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
> +	if (ret < 0) {
> +		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
> +		return ret;
> +	}
> +
> +	if (id != VCNL4035_DEV_ID_VAL) {
> +		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
> +			id, VCNL4035_DEV_ID_VAL);
> +		return -ENODEV;
> +	}
> +
> +#ifndef CONFIG_PM
> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
> +	if (ret < 0)
> +		return ret;
> +#endif
> +	/* set default integration time - 100 ms for ALS */
> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +				 VCNL4035_ALS_IT_MASK,
> +				 VCNL4035_ALS_IT_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
> +
> +	/* set default persistence time - 1 for ALS */
> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +				 VCNL4035_ALS_PERS_MASK,
> +				 VCNL4035_ALS_PERS_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_update_bits default PERS returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
> +
> +	/* set default HIGH threshold for ALS */
> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
> +				VCNL4035_ALS_THDH_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_write default THDH returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
> +
> +	/* set default LOW threshold for ALS */
> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
> +				VCNL4035_ALS_THDL_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_write default THDL returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
> +
> +	return 0;
> +}
> +
> +static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
> +{
> +	switch (reg) {
> +	case VCNL4035_ALS_CONF:
> +	case VCNL4035_DEV_ID:
> +		return false;
> +	default:
> +		return true;
> +	}
> +}
> +
> +static const struct regmap_config vcnl4035_regmap_config = {
> +	.name		= VCNL4035_REGMAP_NAME,
> +	.reg_bits	= 8,
> +	.val_bits	= 16,
> +	.max_register	= VCNL4035_DEV_ID,
> +	.cache_type	= REGCACHE_RBTREE,
> +	.volatile_reg	= vcnl4035_is_volatile_reg,
> +	.val_format_endian = REGMAP_ENDIAN_LITTLE,
> +};
> +
> +static int vcnl4035_probe(struct i2c_client *client,
> +				const struct i2c_device_id *id)
> +{
> +	struct vcnl4035_data *data;
> +	struct iio_dev *indio_dev;
> +	struct regmap *regmap;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
> +	if (IS_ERR(regmap)) {
> +		dev_err(&client->dev, "regmap_init failed!\n");
> +		return PTR_ERR(regmap);
> +	}
> +
> +	data = iio_priv(indio_dev);
> +	i2c_set_clientdata(client, indio_dev);
> +	data->client = client;
> +	data->regmap = regmap;
> +	mutex_init(&data->lock);
> +
> +	indio_dev->dev.parent = &client->dev;
> +	indio_dev->info = &vcnl4035_info;
> +	indio_dev->name = VCNL4035_DRV_NAME;
> +	indio_dev->channels = vcnl4035_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +	ret = vcnl4035_init(data);
> +	if (ret < 0) {
> +		dev_err(&client->dev, "vcnl4035 chip init failed\n");
> +		return ret;
> +	}
> +
> +	ret = pm_runtime_set_active(&client->dev);
> +	if (ret < 0)
> +		goto fail_poweroff;
> +
> +	pm_runtime_enable(&client->dev);
> +	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
> +	pm_runtime_use_autosuspend(&client->dev);
> +
> +	if (client->irq) {
> +		data->drdy_trigger0 = devm_iio_trigger_alloc(
> +			indio_dev->dev.parent,
> +			"%s-dev%d", indio_dev->name, indio_dev->id);
> +		if (!data->drdy_trigger0) {
> +			ret = -ENOMEM;
> +			goto fail_pm_disable;
> +		}
> +		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
> +		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
> +		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
> +		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
> +
> +		/* IRQ to trigger mapping */
> +		ret = devm_request_threaded_irq(&client->dev, client->irq,
> +			vcnl4035_drdy_irq_handler, vcnl4035_drdy_irq_thread,
> +			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
> +			VCNL4035_IRQ_NAME, indio_dev);
> +		if (ret < 0) {
> +			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
> +				client->irq);
> +			goto fail_pm_disable;
> +			}
> +
> +		ret = devm_iio_trigger_register(indio_dev->dev.parent,
> +						data->drdy_trigger0);
> +		if (ret) {
> +			dev_err(&client->dev, "iio trigger register failed\n");
> +			goto fail_pm_disable;
> +		}
> +
> +		/* Trigger setup */
> +		ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent,

I would prefer to see a clear unroll in the reverse order of setup.
That means you can't safely mix managed and unmanaged cleanup.  Only use
devm until the point where you have to do something that can't be managed, or
use the devm action approach to move all the cleanup into the managed path.

> +			indio_dev,
> +			vcnl4035_trigger_consumer_store_time,
> +			vcnl4035_trigger_consumer_handler,
> +			NULL);
> +		if (ret < 0) {
> +			dev_err(&client->dev, "iio triggered buffer setup failed\n");
> +			goto fail_pm_disable;
> +		}
> +	}
> +
> +	ret = iio_device_register(indio_dev);
> +	if (ret)
> +		goto fail_pm_disable;
> +	dev_info(&client->dev, "%s Ambient light/proximity sensor\n",
> +						VCNL4035_DRV_NAME);

I'm generally against this sort of print unless it adds real value.
Here it is just printing out something you can easily establish by lots
of other means.  Hence please drop it.

> +	return 0;
> +
> +fail_pm_disable:
> +	pm_runtime_disable(&client->dev);
> +	pm_runtime_set_suspended(&client->dev);
> +	pm_runtime_put_noidle(&client->dev);
> +fail_poweroff:
> +	return vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
> +}
> +
> +static int vcnl4035_remove(struct i2c_client *client)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
> +
> +	iio_device_unregister(indio_dev);
> +
> +	pm_runtime_disable(&client->dev);
> +	pm_runtime_set_suspended(&client->dev);
> +	pm_runtime_put_noidle(&client->dev);
> +
> +	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
> +					VCNL4035_MODE_ALS_DISABLE);
> +}
> +
> +#ifdef CONFIG_PM
> +static int vcnl4035_runtime_suspend(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	mutex_lock(&data->lock);
> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
> +	regcache_mark_dirty(data->regmap);
> +	mutex_unlock(&data->lock);
> +
> +	return ret;
> +}
> +
> +static int vcnl4035_runtime_resume(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	regcache_sync(data->regmap);
> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
> +	if (ret < 0)
> +		return ret;
> +	/* wait for 1 ALS integration cycle */
> +	msleep(data->als_it_val * 100);
> +
> +	return 0;
> +}
> +#endif
> +
> +static const struct dev_pm_ops vcnl4035_pm_ops = {
> +	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
> +				pm_runtime_force_resume)
> +	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
> +			   vcnl4035_runtime_resume, NULL)
> +};
> +
> +static const struct of_device_id vcnl4035_of_match[] = {
> +	{ .compatible = "vishay,vcnl4035", },
> +	{ },
> +};
> +MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
> +
> +static const struct i2c_device_id vcnl4035_id[] = {
> +	{ "vcnl4035", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
> +
> +static struct i2c_driver vcnl4035_driver = {
> +	.driver = {
> +		.name   = VCNL4035_DRV_NAME,
> +		.pm	= &vcnl4035_pm_ops,
> +		.of_match_table = of_match_ptr(vcnl4035_of_match),
> +	},
> +	.probe  = vcnl4035_probe,
> +	.remove	= vcnl4035_remove,
> +	.id_table = vcnl4035_id,
> +};
> +
> +module_i2c_driver(vcnl4035_driver);
> +
> +MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
> +MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
> +MODULE_LICENSE("GPL v2");



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 1/2] iio: light: Add support for vishay vcnl4035
  2018-06-29 15:38 ` [PATCH v2 " Parthiban Nallathambi
  2018-06-29 15:38   ` [PATCH v2 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
@ 2018-06-29 23:46   ` kbuild test robot
  2018-06-30  9:51   ` Peter Meerwald-Stadler
  2018-08-02  9:52   ` [PATCH v3 " Parthiban Nallathambi
  3 siblings, 0 replies; 19+ messages in thread
From: kbuild test robot @ 2018-06-29 23:46 UTC (permalink / raw)
  To: Parthiban Nallathambi
  Cc: kbuild-all, jic23, knaack.h, lars, pmeerw, robh+dt, linux-iio,
	linux-kernel, mark.rutland, devicetree, matthias.bgg, wd, sbabic,
	hs, Parthiban Nallathambi

[-- Attachment #1: Type: text/plain, Size: 1295 bytes --]

Hi Parthiban,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on iio/togreg]
[also build test ERROR on v4.18-rc2 next-20180629]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Parthiban-Nallathambi/iio-light-Add-support-for-vishay-vcnl4035/20180630-021705
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
config: ia64-allmodconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 8.1.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        GCC_VERSION=8.1.0 make.cross ARCH=ia64 

All errors (new ones prefixed by >>):

   ERROR: "ia64_delay_loop" [drivers/spi/spi-thunderx.ko] undefined!
   ERROR: "__sw_hweight8" [drivers/net/wireless/mediatek/mt76/mt76.ko] undefined!
   ERROR: "ia64_delay_loop" [drivers/net/phy/mdio-cavium.ko] undefined!
>> ERROR: "__sw_hweight8" [drivers/iio/light/vcnl4035.ko] undefined!

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 50877 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 1/2] iio: light: Add support for vishay vcnl4035
  2018-06-29 15:38 ` [PATCH v2 " Parthiban Nallathambi
  2018-06-29 15:38   ` [PATCH v2 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
  2018-06-29 23:46   ` [PATCH v2 1/2] iio: light: Add support " kbuild test robot
@ 2018-06-30  9:51   ` Peter Meerwald-Stadler
  2018-07-03 12:58     ` Parthiban Nallathambi
  2018-08-02  9:52   ` [PATCH v3 " Parthiban Nallathambi
  3 siblings, 1 reply; 19+ messages in thread
From: Peter Meerwald-Stadler @ 2018-06-30  9:51 UTC (permalink / raw)
  To: Parthiban Nallathambi
  Cc: jic23, knaack.h, lars, robh+dt, linux-iio, linux-kernel,
	mark.rutland, devicetree, matthias.bgg, wd, sbabic, hs


> Add support for VCNL4035, which is capable of Ambient light
> sensing (ALS) and proximity function. This patch adds support
> only for ALS function

comments below

a lot of effort is in getting a timestamp; is it worth it for ALS 
measurement? also the implementation is quite involved, what is the idea?
 
> Signed-off-by: Parthiban Nallathambi <pn@denx.de>
> ---
> 
> Changelog since v1:
> 
> 1. Fixed 0-day warning on le16_to_cpu usage
> 2. Persistence value is directly mapped to datasheet's value to
> avoid confusions of usage from sysfs
> ---
>  drivers/iio/light/Kconfig    |  12 +
>  drivers/iio/light/Makefile   |   1 +
>  drivers/iio/light/vcnl4035.c | 682 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 695 insertions(+)
>  create mode 100644 drivers/iio/light/vcnl4035.c
> 
> diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
> index c7ef8d1862d6..b7069a4c28a2 100644
> --- a/drivers/iio/light/Kconfig
> +++ b/drivers/iio/light/Kconfig
> @@ -447,6 +447,18 @@ config VCNL4000
>  	 To compile this driver as a module, choose M here: the
>  	 module will be called vcnl4000.
>  
> +config VCNL4035
> +	tristate "VCNL4035 combined ALS and proximity sensor"
> +	select REGMAP_I2C
> +	depends on I2C
> +	help
> +	 Say Y here if you want to build a driver for the Vishay VCNL4035,
> +	 combined ambient light (ALS) and proximity sensor. Currently only ALS
> +	 function is available.
> +
> +	 To compile this driver as a module, choose M here: the
> +	 module will be called vcnl4035.
> +
>  config VEML6070
>  	tristate "VEML6070 UV A light sensor"
>  	depends on I2C
> diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
> index 80943af5d627..dce98511a59b 100644
> --- a/drivers/iio/light/Makefile
> +++ b/drivers/iio/light/Makefile
> @@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
>  obj-$(CONFIG_TSL4531)		+= tsl4531.o
>  obj-$(CONFIG_US5182D)		+= us5182d.o
>  obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
> +obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
>  obj-$(CONFIG_VEML6070)		+= veml6070.o
>  obj-$(CONFIG_VL6180)		+= vl6180.o
>  obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
> diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
> new file mode 100644
> index 000000000000..45eccb462ed0
> --- /dev/null
> +++ b/drivers/iio/light/vcnl4035.c
> @@ -0,0 +1,682 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
> + *
> + * Copyright (c) 2018, DENX Software Engineering GmbH
> + * Author: Parthiban Nallathambi <pn@denx.de>
> + *
> + *
> + * TODO: Proximity
> + */
> +#include <linux/module.h>
> +#include <linux/i2c.h>
> +#include <linux/regmap.h>
> +#include <linux/pm_runtime.h>
> +
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/iio/triggered_buffer.h>
> +
> +#define VCNL4035_DRV_NAME	"vcnl4035"
> +#define VCNL4035_IRQ_NAME	"vcnl4035_event"
> +#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
> +
> +/* Device registers */
> +#define VCNL4035_ALS_CONF	0x00
> +#define VCNL4035_ALS_THDH	0x01
> +#define VCNL4035_ALS_THDL	0x02
> +#define VCNL4035_ALS_DATA	0x0B
> +#define	VCNL4035_INT_FLAG	0x0D

there seems to be a tab before VCNL4035_INT_FLAG, should be space

> +#define VCNL4035_DEV_ID		0x0E
> +
> +/* Register masks */
> +#define VCNL4035_MODE_ALS_MASK		BIT(0)
> +#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
> +#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
> +#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
> +#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
> +#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
> +
> +/* Default values */
> +#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
> +#define VCNL4035_MODE_ALS_DISABLE	0x00
> +#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
> +#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
> +#define VCNL4035_DEV_ID_VAL		0x80
> +#define VCNL4035_ALS_IT_DEFAULT		0x01
> +#define VCNL4035_ALS_PERS_DEFAULT	0x00
> +#define VCNL4035_ALS_THDH_DEFAULT	5000
> +#define VCNL4035_ALS_THDL_DEFAULT	100
> +#define VCNL4035_SLEEP_DELAY_MS		2000
> +
> +struct vcnl4035_data {
> +	struct i2c_client *client;
> +	struct regmap *regmap;
> +	/* protect device settings persistence, integration time, threshold */
> +	struct mutex lock;
> +	unsigned int als_it_val;
> +	unsigned int als_persistence:4;
> +	unsigned int als_thresh_low;
> +	unsigned int als_thresh_high;
> +	struct iio_trigger *drdy_trigger0;
> +	s64 irq_timestamp;
> +};
> +
> +static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
> +{
> +	int ret;
> +	int reg;
> +
> +	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
> +	if (ret < 0)
> +		return false;
> +	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
> +		return true;
> +	else
> +		return false;
> +}
> +
> +static irqreturn_t vcnl4035_drdy_irq_handler(int irq, void *private)
> +{
> +	struct iio_dev *indio_dev = private;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	data->irq_timestamp = iio_get_time_ns(indio_dev);
> +	return IRQ_WAKE_THREAD;
> +}
> +
> +static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
> +{
> +	struct iio_dev *indio_dev = private;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	if (vcnl4035_is_triggered(data)) {
> +		iio_trigger_poll_chained(data->drdy_trigger0);
> +		return IRQ_HANDLED;
> +	}
> +
> +	return IRQ_NONE;
> +}
> +
> +/* Triggered buffer */
> +static irqreturn_t vcnl4035_trigger_consumer_store_time(int irq, void *p)
> +{
> +	struct iio_poll_func *pf = p;
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +
> +	if (!iio_trigger_using_own(indio_dev))
> +		pf->timestamp = iio_get_time_ns(indio_dev);
> +
> +	return IRQ_WAKE_THREAD;
> +}
> +
> +static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
> +{
> +	struct iio_poll_func *pf = p;
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int als_data;
> +	int ret;
> +
> +	if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
> +		pf->timestamp = data->irq_timestamp;
> +		data->irq_timestamp = 0;
> +	}
> +
> +	if (!pf->timestamp)
> +		pf->timestamp = iio_get_time_ns(indio_dev);
> +
> +	mutex_lock(&data->lock);

what is the purpose of the lock?
the comment in vcnl4035_data doesn't mention it either

> +	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &als_data);
> +	mutex_unlock(&data->lock);
> +	if (!ret)
> +		iio_push_to_buffers_with_timestamp(indio_dev,
> +						   &als_data,

als_data needs to be large enough to hold the timestamp, see documentation 
of iio_push_to_buffers_with_timestamp()

> +						   pf->timestamp);
> +	else
> +		dev_err(&data->client->dev,
> +			"Trigger consumer can't read from sensor.\n");
> +	pf->timestamp = 0;
> +
> +	iio_trigger_notify_done(indio_dev->trig);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
> +					bool enable_drdy)
> +{
> +	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
> +					VCNL4035_MODE_ALS_INT_DISABLE;
> +	int ret;
> +
> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +				 VCNL4035_MODE_ALS_INT_MASK,
> +				 val);
> +	if (ret)
> +		dev_err(&data->client->dev, "%s failed\n", __func__);
> +
> +	return ret;
> +}
> +
> +static const struct iio_trigger_ops vcnl4035_trigger_ops = {
> +	.set_trigger_state = vcnl4035_als_drdy_set_state,
> +};
> +
> +/*
> + *	Device IT	INT Time(ms)	Scale (lux/step)

maybe space before (ms)?

> + *	000		50		0.064
> + *	001		100		0.032
> + *	010		200		0.016
> + *	100		400		0.008
> + *	101 - 111	800		0.004
> + * Values are proportial, so ALS INT is selected for input due to

proportional

> + * simplicity reason. Integration time value and scaling is
> + * calculated based on device INT value
> + *
> + * Raw value needs to be scaled using ALS STEPS

steps

> + *
> + */
> +static int vcnl4035_read_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan, int *val,
> +			    int *val2, long mask)
> +{
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +	int busy;

needed? can 'ret' be used?

> +	int raw_data;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		busy = iio_device_claim_direct_mode(indio_dev);
> +		if (busy)
> +			return -EBUSY;
> +
> +		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
> +		iio_device_release_direct_mode(indio_dev);
> +		if (ret < 0)
> +			return ret;
> +
> +		*val = raw_data;
> +		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_INT_TIME:
> +		mutex_lock(&data->lock);
> +		*val = data->als_it_val * 100;
> +		if (!*val)

maybe check data->als_it_val and then set *val accordingly as done below

> +			*val = 50;
> +		mutex_unlock(&data->lock);
> +		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_SCALE:
> +		mutex_lock(&data->lock);
> +		*val = 64;
> +		if (!data->als_it_val)
> +			*val2 = 1000;
> +		else
> +			*val2 = data->als_it_val * 2 * 1000;
> +		mutex_unlock(&data->lock);
> +		return IIO_VAL_FRACTIONAL;
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int vcnl4035_write_raw(struct iio_dev *indio_dev,
> +				struct iio_chan_spec const *chan,
> +				int val, int val2, long mask)
> +{
> +	int ret;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_INT_TIME:
> +		if (val <= 0 || val > 800)
> +			return -EINVAL;
> +		mutex_lock(&data->lock);
> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +					 VCNL4035_ALS_IT_MASK,
> +					 val / 100);
> +		if (!ret)
> +			data->als_it_val = val / 100;
> +		mutex_unlock(&data->lock);
> +		break;

return ret;
directly here

> +	default:
> +		return -EINVAL;
> +	}
> +	return ret;
> +}
> +
> +/* No direct ABI for persistence and threshold, so eventing */
> +static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
> +		const struct iio_chan_spec *chan, enum iio_event_type type,
> +		enum iio_event_direction dir, enum iio_event_info info,
> +		int *val, int *val2)
> +{
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	switch (info) {
> +	case IIO_EV_INFO_VALUE:
> +		switch (dir) {
> +		case IIO_EV_DIR_RISING:
> +			mutex_lock(&data->lock);
> +			*val = data->als_thresh_high;
> +			mutex_unlock(&data->lock);
> +			break;
> +		case IIO_EV_DIR_FALLING:
> +			mutex_lock(&data->lock);
> +			*val = data->als_thresh_low;
> +			mutex_unlock(&data->lock);
> +			break;
> +		default:
> +			return -EINVAL;
> +		}
> +		break;
> +	case IIO_EV_INFO_PERIOD:
> +		mutex_lock(&data->lock);
> +		*val = data->als_persistence;
> +		mutex_unlock(&data->lock);
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +
> +	return IIO_VAL_INT;

move it up, instead of the 
break;

> +}
> +
> +static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
> +		const struct iio_chan_spec *chan, enum iio_event_type type,
> +		enum iio_event_direction dir, enum iio_event_info info, int val,
> +		int val2)
> +{
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	switch (info) {
> +	case IIO_EV_INFO_VALUE:
> +		/* 16 bit threshold range */
> +		if (val < 0 || val > 65535)
> +			return -EINVAL;
> +		if (dir == IIO_EV_DIR_RISING) {
> +			if (val < data->als_thresh_low)
> +				return -EINVAL;
> +			mutex_lock(&data->lock);
> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
> +					   val);

isn't the purpose of the lock to have 
data->als_thresh_high = val;
in the same critical section?

here and similarly below

> +			mutex_unlock(&data->lock);
> +			if (ret)
> +				return ret;
> +			data->als_thresh_high = val;
> +		} else {
> +			if (val > data->als_thresh_high)
> +				return -EINVAL;
> +			mutex_lock(&data->lock);
> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
> +					   val);
> +			mutex_unlock(&data->lock);
> +			if (ret)
> +				return ret;
> +			data->als_thresh_low = val;
> +		}
> +		break;
> +	case IIO_EV_INFO_PERIOD:
> +		/* allow only 1 2 4 8 as persistence value */
> +		if (val < 0 || val > 8 || (__sw_hweight8(val) != 1))

parenthesis around last condition not needed

> +			return -EINVAL;
> +		mutex_lock(&data->lock);
> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +					 VCNL4035_ALS_PERS_MASK, val);
> +		mutex_unlock(&data->lock);
> +		if (ret)
> +			return ret;
> +		data->als_persistence = val;
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +	return 0;
> +}
> +
> +static IIO_CONST_ATTR(als_available_integration_time, "50 100 200 400 800");
> +static IIO_CONST_ATTR(als_available_persistence, "1 2 4 8");
> +static IIO_CONST_ATTR(als_available_threshold_range, "0 65535");
> +
> +static struct attribute *vcnl4035_attributes[] = {
> +	&iio_const_attr_als_available_integration_time.dev_attr.attr,
> +	&iio_const_attr_als_available_threshold_range.dev_attr.attr,
> +	&iio_const_attr_als_available_persistence.dev_attr.attr,
> +	NULL,
> +};
> +
> +static const struct attribute_group vcnl4035_attribute_group = {
> +	.attrs = vcnl4035_attributes,
> +};
> +
> +static const struct iio_info vcnl4035_info = {
> +	.read_raw		= vcnl4035_read_raw,
> +	.write_raw		= vcnl4035_write_raw,
> +	.read_event_value	= vcnl4035_read_thresh,
> +	.write_event_value	= vcnl4035_write_thresh,
> +	.attrs			= &vcnl4035_attribute_group,
> +};
> +
> +enum vcnl4035_scan_index_order {
> +	VCNL4035_CHAN_INDEX_LIGHT,
> +};
> +
> +static const unsigned long vcnl4035_available_scan_masks[] = {
> +	BIT(VCNL4035_CHAN_INDEX_LIGHT), 0

I'd put 0 in a separate line so that it can be extended with a single line 
patch (extreme nitpick)

> +};
> +
> +static const struct iio_event_spec vcnl4035_event_spec[] = {
> +	{
> +		.type = IIO_EV_TYPE_THRESH,
> +		.dir = IIO_EV_DIR_RISING,
> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
> +	}, {
> +		.type = IIO_EV_TYPE_THRESH,
> +		.dir = IIO_EV_DIR_FALLING,
> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
> +	}, {
> +		.type = IIO_EV_TYPE_THRESH,
> +		.dir = IIO_EV_DIR_EITHER,
> +		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
> +	},
> +};
> +
> +static const struct iio_chan_spec vcnl4035_channels[] = {
> +	{
> +		.type = IIO_INTENSITY,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				BIT(IIO_CHAN_INFO_INT_TIME) |
> +				BIT(IIO_CHAN_INFO_SCALE),
> +		.event_spec = vcnl4035_event_spec,
> +		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
> +		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
> +		.scan_type = {
> +			.sign = 'u',
> +			.realbits = 16,
> +			.storagebits = 16,
> +			.endianness = IIO_LE,
> +		},
> +	},
> +};
> +
> +static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
> +{
> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +					VCNL4035_MODE_ALS_MASK,
> +					status);
> +}
> +
> +static int vcnl4035_init(struct vcnl4035_data *data)
> +{
> +	int ret;
> +	int id;
> +
> +	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
> +	if (ret < 0) {
> +		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
> +		return ret;
> +	}
> +
> +	if (id != VCNL4035_DEV_ID_VAL) {
> +		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
> +			id, VCNL4035_DEV_ID_VAL);
> +		return -ENODEV;
> +	}
> +
> +#ifndef CONFIG_PM
> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
> +	if (ret < 0)
> +		return ret;
> +#endif
> +	/* set default integration time - 100 ms for ALS */
> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +				 VCNL4035_ALS_IT_MASK,
> +				 VCNL4035_ALS_IT_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
> +
> +	/* set default persistence time - 1 for ALS */
> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +				 VCNL4035_ALS_PERS_MASK,
> +				 VCNL4035_ALS_PERS_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_update_bits default PERS returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
> +
> +	/* set default HIGH threshold for ALS */
> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
> +				VCNL4035_ALS_THDH_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_write default THDH returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
> +
> +	/* set default LOW threshold for ALS */
> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
> +				VCNL4035_ALS_THDL_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_write default THDL returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
> +
> +	return 0;
> +}
> +
> +static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
> +{
> +	switch (reg) {
> +	case VCNL4035_ALS_CONF:
> +	case VCNL4035_DEV_ID:
> +		return false;
> +	default:
> +		return true;
> +	}
> +}
> +
> +static const struct regmap_config vcnl4035_regmap_config = {
> +	.name		= VCNL4035_REGMAP_NAME,
> +	.reg_bits	= 8,
> +	.val_bits	= 16,
> +	.max_register	= VCNL4035_DEV_ID,
> +	.cache_type	= REGCACHE_RBTREE,
> +	.volatile_reg	= vcnl4035_is_volatile_reg,
> +	.val_format_endian = REGMAP_ENDIAN_LITTLE,
> +};
> +
> +static int vcnl4035_probe(struct i2c_client *client,
> +				const struct i2c_device_id *id)
> +{
> +	struct vcnl4035_data *data;
> +	struct iio_dev *indio_dev;
> +	struct regmap *regmap;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
> +	if (IS_ERR(regmap)) {
> +		dev_err(&client->dev, "regmap_init failed!\n");
> +		return PTR_ERR(regmap);
> +	}
> +
> +	data = iio_priv(indio_dev);
> +	i2c_set_clientdata(client, indio_dev);
> +	data->client = client;
> +	data->regmap = regmap;
> +	mutex_init(&data->lock);
> +
> +	indio_dev->dev.parent = &client->dev;
> +	indio_dev->info = &vcnl4035_info;
> +	indio_dev->name = VCNL4035_DRV_NAME;
> +	indio_dev->channels = vcnl4035_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +	ret = vcnl4035_init(data);
> +	if (ret < 0) {
> +		dev_err(&client->dev, "vcnl4035 chip init failed\n");
> +		return ret;
> +	}
> +
> +	ret = pm_runtime_set_active(&client->dev);
> +	if (ret < 0)
> +		goto fail_poweroff;
> +
> +	pm_runtime_enable(&client->dev);
> +	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
> +	pm_runtime_use_autosuspend(&client->dev);
> +
> +	if (client->irq) {
> +		data->drdy_trigger0 = devm_iio_trigger_alloc(
> +			indio_dev->dev.parent,
> +			"%s-dev%d", indio_dev->name, indio_dev->id);
> +		if (!data->drdy_trigger0) {
> +			ret = -ENOMEM;
> +			goto fail_pm_disable;
> +		}
> +		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
> +		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
> +		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
> +		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
> +
> +		/* IRQ to trigger mapping */
> +		ret = devm_request_threaded_irq(&client->dev, client->irq,
> +			vcnl4035_drdy_irq_handler, vcnl4035_drdy_irq_thread,
> +			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
> +			VCNL4035_IRQ_NAME, indio_dev);
> +		if (ret < 0) {
> +			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
> +				client->irq);
> +			goto fail_pm_disable;
> +			}
> +
> +		ret = devm_iio_trigger_register(indio_dev->dev.parent,
> +						data->drdy_trigger0);
> +		if (ret) {
> +			dev_err(&client->dev, "iio trigger register failed\n");
> +			goto fail_pm_disable;
> +		}
> +
> +		/* Trigger setup */
> +		ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent,
> +			indio_dev,
> +			vcnl4035_trigger_consumer_store_time,
> +			vcnl4035_trigger_consumer_handler,
> +			NULL);
> +		if (ret < 0) {
> +			dev_err(&client->dev, "iio triggered buffer setup failed\n");
> +			goto fail_pm_disable;
> +		}
> +	}
> +
> +	ret = iio_device_register(indio_dev);
> +	if (ret)
> +		goto fail_pm_disable;
> +	dev_info(&client->dev, "%s Ambient light/proximity sensor\n",

no need for this logging

> +						VCNL4035_DRV_NAME);
> +	return 0;
> +
> +fail_pm_disable:
> +	pm_runtime_disable(&client->dev);
> +	pm_runtime_set_suspended(&client->dev);
> +	pm_runtime_put_noidle(&client->dev);
> +fail_poweroff:
> +	return vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);

I think we want to return the 'ret' value indicating the original failure, 
not the return value of vcnl4035_set_als_power_state() which is likely success

> +}
> +
> +static int vcnl4035_remove(struct i2c_client *client)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
> +
> +	iio_device_unregister(indio_dev);
> +
> +	pm_runtime_disable(&client->dev);
> +	pm_runtime_set_suspended(&client->dev);
> +	pm_runtime_put_noidle(&client->dev);
> +
> +	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
> +					VCNL4035_MODE_ALS_DISABLE);
> +}
> +
> +#ifdef CONFIG_PM
> +static int vcnl4035_runtime_suspend(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	mutex_lock(&data->lock);
> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
> +	regcache_mark_dirty(data->regmap);
> +	mutex_unlock(&data->lock);
> +
> +	return ret;
> +}
> +
> +static int vcnl4035_runtime_resume(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	regcache_sync(data->regmap);
> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
> +	if (ret < 0)
> +		return ret;
> +	/* wait for 1 ALS integration cycle */
> +	msleep(data->als_it_val * 100);
> +
> +	return 0;
> +}
> +#endif
> +
> +static const struct dev_pm_ops vcnl4035_pm_ops = {
> +	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
> +				pm_runtime_force_resume)
> +	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
> +			   vcnl4035_runtime_resume, NULL)
> +};
> +
> +static const struct of_device_id vcnl4035_of_match[] = {
> +	{ .compatible = "vishay,vcnl4035", },
> +	{ },
> +};
> +MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
> +
> +static const struct i2c_device_id vcnl4035_id[] = {
> +	{ "vcnl4035", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
> +
> +static struct i2c_driver vcnl4035_driver = {
> +	.driver = {
> +		.name   = VCNL4035_DRV_NAME,
> +		.pm	= &vcnl4035_pm_ops,
> +		.of_match_table = of_match_ptr(vcnl4035_of_match),
> +	},
> +	.probe  = vcnl4035_probe,
> +	.remove	= vcnl4035_remove,
> +	.id_table = vcnl4035_id,
> +};
> +
> +module_i2c_driver(vcnl4035_driver);
> +
> +MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
> +MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
> +MODULE_LICENSE("GPL v2");
> 

-- 

Peter Meerwald-Stadler
Mobile: +43 664 24 44 418

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 1/2] iio: light: Add support for vishay vcnl4035
  2018-06-30  9:51   ` Peter Meerwald-Stadler
@ 2018-07-03 12:58     ` Parthiban Nallathambi
  0 siblings, 0 replies; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-07-03 12:58 UTC (permalink / raw)
  To: Peter Meerwald-Stadler
  Cc: pn, jic23, knaack.h, lars, robh+dt, linux-iio, linux-kernel,
	mark.rutland, devicetree, matthias.bgg, wd, sbabic, hs

Hello Peter,

Thanks for your comments. Will send v3 with comments fixed.

On 06/30/2018 11:51 AM, Peter Meerwald-Stadler wrote:
> 
>> Add support for VCNL4035, which is capable of Ambient light
>> sensing (ALS) and proximity function. This patch adds support
>> only for ALS function
> 
> comments below
> 
> a lot of effort is in getting a timestamp; is it worth it for ALS
> measurement? also the implementation is quite involved, what is the idea?

Initial idea was to include proximity function together with ALS and
hence the complexity.

Yes, timestamp is not required considering only ALS data. I have removed
it now. Will send the cleaned up patch in v3.

>   
>> Signed-off-by: Parthiban Nallathambi <pn@denx.de>
>> ---
>>
>> Changelog since v1:
>>
>> 1. Fixed 0-day warning on le16_to_cpu usage
>> 2. Persistence value is directly mapped to datasheet's value to
>> avoid confusions of usage from sysfs
>> ---
>>   drivers/iio/light/Kconfig    |  12 +
>>   drivers/iio/light/Makefile   |   1 +
>>   drivers/iio/light/vcnl4035.c | 682 +++++++++++++++++++++++++++++++++++++++++++
>>   3 files changed, 695 insertions(+)
>>   create mode 100644 drivers/iio/light/vcnl4035.c
>>
>> diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
>> index c7ef8d1862d6..b7069a4c28a2 100644
>> --- a/drivers/iio/light/Kconfig
>> +++ b/drivers/iio/light/Kconfig
>> @@ -447,6 +447,18 @@ config VCNL4000
>>   	 To compile this driver as a module, choose M here: the
>>   	 module will be called vcnl4000.
>>   
>> +config VCNL4035
>> +	tristate "VCNL4035 combined ALS and proximity sensor"
>> +	select REGMAP_I2C
>> +	depends on I2C
>> +	help
>> +	 Say Y here if you want to build a driver for the Vishay VCNL4035,
>> +	 combined ambient light (ALS) and proximity sensor. Currently only ALS
>> +	 function is available.
>> +
>> +	 To compile this driver as a module, choose M here: the
>> +	 module will be called vcnl4035.
>> +
>>   config VEML6070
>>   	tristate "VEML6070 UV A light sensor"
>>   	depends on I2C
>> diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
>> index 80943af5d627..dce98511a59b 100644
>> --- a/drivers/iio/light/Makefile
>> +++ b/drivers/iio/light/Makefile
>> @@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
>>   obj-$(CONFIG_TSL4531)		+= tsl4531.o
>>   obj-$(CONFIG_US5182D)		+= us5182d.o
>>   obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
>> +obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
>>   obj-$(CONFIG_VEML6070)		+= veml6070.o
>>   obj-$(CONFIG_VL6180)		+= vl6180.o
>>   obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
>> diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
>> new file mode 100644
>> index 000000000000..45eccb462ed0
>> --- /dev/null
>> +++ b/drivers/iio/light/vcnl4035.c
>> @@ -0,0 +1,682 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
>> + *
>> + * Copyright (c) 2018, DENX Software Engineering GmbH
>> + * Author: Parthiban Nallathambi <pn@denx.de>
>> + *
>> + *
>> + * TODO: Proximity
>> + */
>> +#include <linux/module.h>
>> +#include <linux/i2c.h>
>> +#include <linux/regmap.h>
>> +#include <linux/pm_runtime.h>
>> +
>> +#include <linux/iio/iio.h>
>> +#include <linux/iio/sysfs.h>
>> +#include <linux/iio/buffer.h>
>> +#include <linux/iio/trigger.h>
>> +#include <linux/iio/trigger_consumer.h>
>> +#include <linux/iio/triggered_buffer.h>
>> +
>> +#define VCNL4035_DRV_NAME	"vcnl4035"
>> +#define VCNL4035_IRQ_NAME	"vcnl4035_event"
>> +#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
>> +
>> +/* Device registers */
>> +#define VCNL4035_ALS_CONF	0x00
>> +#define VCNL4035_ALS_THDH	0x01
>> +#define VCNL4035_ALS_THDL	0x02
>> +#define VCNL4035_ALS_DATA	0x0B
>> +#define	VCNL4035_INT_FLAG	0x0D
> 
> there seems to be a tab before VCNL4035_INT_FLAG, should be space

Sure, thanks. Replace with space.

> 
>> +#define VCNL4035_DEV_ID		0x0E
>> +
>> +/* Register masks */
>> +#define VCNL4035_MODE_ALS_MASK		BIT(0)
>> +#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
>> +#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
>> +#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
>> +#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
>> +#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
>> +
>> +/* Default values */
>> +#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
>> +#define VCNL4035_MODE_ALS_DISABLE	0x00
>> +#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
>> +#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
>> +#define VCNL4035_DEV_ID_VAL		0x80
>> +#define VCNL4035_ALS_IT_DEFAULT		0x01
>> +#define VCNL4035_ALS_PERS_DEFAULT	0x00
>> +#define VCNL4035_ALS_THDH_DEFAULT	5000
>> +#define VCNL4035_ALS_THDL_DEFAULT	100
>> +#define VCNL4035_SLEEP_DELAY_MS		2000
>> +
>> +struct vcnl4035_data {
>> +	struct i2c_client *client;
>> +	struct regmap *regmap;
>> +	/* protect device settings persistence, integration time, threshold */
>> +	struct mutex lock;
>> +	unsigned int als_it_val;
>> +	unsigned int als_persistence:4;
>> +	unsigned int als_thresh_low;
>> +	unsigned int als_thresh_high;
>> +	struct iio_trigger *drdy_trigger0;
>> +	s64 irq_timestamp;
>> +};
>> +
>> +static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
>> +{
>> +	int ret;
>> +	int reg;
>> +
>> +	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
>> +	if (ret < 0)
>> +		return false;
>> +	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
>> +		return true;
>> +	else
>> +		return false;
>> +}
>> +
>> +static irqreturn_t vcnl4035_drdy_irq_handler(int irq, void *private)
>> +{
>> +	struct iio_dev *indio_dev = private;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	data->irq_timestamp = iio_get_time_ns(indio_dev);
>> +	return IRQ_WAKE_THREAD;
>> +}
>> +
>> +static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
>> +{
>> +	struct iio_dev *indio_dev = private;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	if (vcnl4035_is_triggered(data)) {
>> +		iio_trigger_poll_chained(data->drdy_trigger0);
>> +		return IRQ_HANDLED;
>> +	}
>> +
>> +	return IRQ_NONE;
>> +}
>> +
>> +/* Triggered buffer */
>> +static irqreturn_t vcnl4035_trigger_consumer_store_time(int irq, void *p)
>> +{
>> +	struct iio_poll_func *pf = p;
>> +	struct iio_dev *indio_dev = pf->indio_dev;
>> +
>> +	if (!iio_trigger_using_own(indio_dev))
>> +		pf->timestamp = iio_get_time_ns(indio_dev);
>> +
>> +	return IRQ_WAKE_THREAD;
>> +}
>> +
>> +static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
>> +{
>> +	struct iio_poll_func *pf = p;
>> +	struct iio_dev *indio_dev = pf->indio_dev;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int als_data;
>> +	int ret;
>> +
>> +	if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
>> +		pf->timestamp = data->irq_timestamp;
>> +		data->irq_timestamp = 0;
>> +	}
>> +
>> +	if (!pf->timestamp)
>> +		pf->timestamp = iio_get_time_ns(indio_dev);
>> +
>> +	mutex_lock(&data->lock);
> 
> what is the purpose of the lock?
> the comment in vcnl4035_data doesn't mention it either

Device parameters are proportional, so lock it introduced to protect
across scaling factor, integration time, so that raw read and INT read
process it with correct/new integration time and scaling factor.

But there isn't any need to protect the data read from the register. I
will remove this lock here.

> 
>> +	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &als_data);
>> +	mutex_unlock(&data->lock);
>> +	if (!ret)
>> +		iio_push_to_buffers_with_timestamp(indio_dev,
>> +						   &als_data,
> 
> als_data needs to be large enough to hold the timestamp, see documentation
> of iio_push_to_buffers_with_timestamp()

Timestamp is removed, so I have used iio_push_to_buffers to push the raw
ALS value.

> 
>> +						   pf->timestamp);
>> +	else
>> +		dev_err(&data->client->dev,
>> +			"Trigger consumer can't read from sensor.\n");
>> +	pf->timestamp = 0;
>> +
>> +	iio_trigger_notify_done(indio_dev->trig);
>> +
>> +	return IRQ_HANDLED;
>> +}
>> +
>> +static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
>> +					bool enable_drdy)
>> +{
>> +	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
>> +					VCNL4035_MODE_ALS_INT_DISABLE;
>> +	int ret;
>> +
>> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +				 VCNL4035_MODE_ALS_INT_MASK,
>> +				 val);
>> +	if (ret)
>> +		dev_err(&data->client->dev, "%s failed\n", __func__);
>> +
>> +	return ret;
>> +}
>> +
>> +static const struct iio_trigger_ops vcnl4035_trigger_ops = {
>> +	.set_trigger_state = vcnl4035_als_drdy_set_state,
>> +};
>> +
>> +/*
>> + *	Device IT	INT Time(ms)	Scale (lux/step)
> 
> maybe space before (ms)?

Sure, thanks.

> 
>> + *	000		50		0.064
>> + *	001		100		0.032
>> + *	010		200		0.016
>> + *	100		400		0.008
>> + *	101 - 111	800		0.004
>> + * Values are proportial, so ALS INT is selected for input due to
> 
> proportional

Typo corrected.

> 
>> + * simplicity reason. Integration time value and scaling is
>> + * calculated based on device INT value
>> + *
>> + * Raw value needs to be scaled using ALS STEPS
> 
> steps

Fixed it, thanks.

> 
>> + *
>> + */
>> +static int vcnl4035_read_raw(struct iio_dev *indio_dev,
>> +			    struct iio_chan_spec const *chan, int *val,
>> +			    int *val2, long mask)
>> +{
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +	int busy;
> 
> needed? can 'ret' be used?

busy is removed. ret is used and directly returned, thanks

> 
>> +	int raw_data;
>> +
>> +	switch (mask) {
>> +	case IIO_CHAN_INFO_RAW:
>> +		busy = iio_device_claim_direct_mode(indio_dev);
>> +		if (busy)
>> +			return -EBUSY;
>> +
>> +		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
>> +		iio_device_release_direct_mode(indio_dev);
>> +		if (ret < 0)
>> +			return ret;
>> +
>> +		*val = raw_data;
>> +		return IIO_VAL_INT;
>> +	case IIO_CHAN_INFO_INT_TIME:
>> +		mutex_lock(&data->lock);
>> +		*val = data->als_it_val * 100;
>> +		if (!*val)
> 
> maybe check data->als_it_val and then set *val accordingly as done below

Ok, default is updated to 50 and condition check done.

> 
>> +			*val = 50;
>> +		mutex_unlock(&data->lock);
>> +		return IIO_VAL_INT;
>> +	case IIO_CHAN_INFO_SCALE:
>> +		mutex_lock(&data->lock);
>> +		*val = 64;
>> +		if (!data->als_it_val)
>> +			*val2 = 1000;
>> +		else
>> +			*val2 = data->als_it_val * 2 * 1000;
>> +		mutex_unlock(&data->lock);
>> +		return IIO_VAL_FRACTIONAL;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +}
>> +
>> +static int vcnl4035_write_raw(struct iio_dev *indio_dev,
>> +				struct iio_chan_spec const *chan,
>> +				int val, int val2, long mask)
>> +{
>> +	int ret;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	switch (mask) {
>> +	case IIO_CHAN_INFO_INT_TIME:
>> +		if (val <= 0 || val > 800)
>> +			return -EINVAL;
>> +		mutex_lock(&data->lock);
>> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +					 VCNL4035_ALS_IT_MASK,
>> +					 val / 100);
>> +		if (!ret)
>> +			data->als_it_val = val / 100;
>> +		mutex_unlock(&data->lock);
>> +		break;
> 
> return ret;
> directly here

Sure.

> 
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +	return ret;
>> +}
>> +
>> +/* No direct ABI for persistence and threshold, so eventing */
>> +static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
>> +		const struct iio_chan_spec *chan, enum iio_event_type type,
>> +		enum iio_event_direction dir, enum iio_event_info info,
>> +		int *val, int *val2)
>> +{
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	switch (info) {
>> +	case IIO_EV_INFO_VALUE:
>> +		switch (dir) {
>> +		case IIO_EV_DIR_RISING:
>> +			mutex_lock(&data->lock);
>> +			*val = data->als_thresh_high;
>> +			mutex_unlock(&data->lock);
>> +			break;
>> +		case IIO_EV_DIR_FALLING:
>> +			mutex_lock(&data->lock);
>> +			*val = data->als_thresh_low;
>> +			mutex_unlock(&data->lock);
>> +			break;
>> +		default:
>> +			return -EINVAL;
>> +		}
>> +		break;
>> +	case IIO_EV_INFO_PERIOD:
>> +		mutex_lock(&data->lock);
>> +		*val = data->als_persistence;
>> +		mutex_unlock(&data->lock);
>> +		break;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +
>> +	return IIO_VAL_INT;
> 
> move it up, instead of the
> break;

Sure.

> 
>> +}
>> +
>> +static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
>> +		const struct iio_chan_spec *chan, enum iio_event_type type,
>> +		enum iio_event_direction dir, enum iio_event_info info, int val,
>> +		int val2)
>> +{
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +
>> +	switch (info) {
>> +	case IIO_EV_INFO_VALUE:
>> +		/* 16 bit threshold range */
>> +		if (val < 0 || val > 65535)
>> +			return -EINVAL;
>> +		if (dir == IIO_EV_DIR_RISING) {
>> +			if (val < data->als_thresh_low)
>> +				return -EINVAL;
>> +			mutex_lock(&data->lock);
>> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
>> +					   val);
> 
> isn't the purpose of the lock to have
> data->als_thresh_high = val;
> in the same critical section?
> 
> here and similarly below

Yes, missed this sync. Will move this part inside lock.

> 
>> +			mutex_unlock(&data->lock);
>> +			if (ret)
>> +				return ret;
>> +			data->als_thresh_high = val;
>> +		} else {
>> +			if (val > data->als_thresh_high)
>> +				return -EINVAL;
>> +			mutex_lock(&data->lock);
>> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
>> +					   val);
>> +			mutex_unlock(&data->lock);
>> +			if (ret)
>> +				return ret;
>> +			data->als_thresh_low = val;
>> +		}
>> +		break;
>> +	case IIO_EV_INFO_PERIOD:
>> +		/* allow only 1 2 4 8 as persistence value */
>> +		if (val < 0 || val > 8 || (__sw_hweight8(val) != 1))
> 
> parenthesis around last condition not needed

Sure, removed.

> 
>> +			return -EINVAL;
>> +		mutex_lock(&data->lock);
>> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +					 VCNL4035_ALS_PERS_MASK, val);
>> +		mutex_unlock(&data->lock);
>> +		if (ret)
>> +			return ret;
>> +		data->als_persistence = val;
>> +		break;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +	return 0;
>> +}
>> +
>> +static IIO_CONST_ATTR(als_available_integration_time, "50 100 200 400 800");
>> +static IIO_CONST_ATTR(als_available_persistence, "1 2 4 8");
>> +static IIO_CONST_ATTR(als_available_threshold_range, "0 65535");
>> +
>> +static struct attribute *vcnl4035_attributes[] = {
>> +	&iio_const_attr_als_available_integration_time.dev_attr.attr,
>> +	&iio_const_attr_als_available_threshold_range.dev_attr.attr,
>> +	&iio_const_attr_als_available_persistence.dev_attr.attr,
>> +	NULL,
>> +};
>> +
>> +static const struct attribute_group vcnl4035_attribute_group = {
>> +	.attrs = vcnl4035_attributes,
>> +};
>> +
>> +static const struct iio_info vcnl4035_info = {
>> +	.read_raw		= vcnl4035_read_raw,
>> +	.write_raw		= vcnl4035_write_raw,
>> +	.read_event_value	= vcnl4035_read_thresh,
>> +	.write_event_value	= vcnl4035_write_thresh,
>> +	.attrs			= &vcnl4035_attribute_group,
>> +};
>> +
>> +enum vcnl4035_scan_index_order {
>> +	VCNL4035_CHAN_INDEX_LIGHT,
>> +};
>> +
>> +static const unsigned long vcnl4035_available_scan_masks[] = {
>> +	BIT(VCNL4035_CHAN_INDEX_LIGHT), 0
> 
> I'd put 0 in a separate line so that it can be extended with a single line
> patch (extreme nitpick)

Sure, make sense when extending it for proximity.

> 
>> +};
>> +
>> +static const struct iio_event_spec vcnl4035_event_spec[] = {
>> +	{
>> +		.type = IIO_EV_TYPE_THRESH,
>> +		.dir = IIO_EV_DIR_RISING,
>> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
>> +	}, {
>> +		.type = IIO_EV_TYPE_THRESH,
>> +		.dir = IIO_EV_DIR_FALLING,
>> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
>> +	}, {
>> +		.type = IIO_EV_TYPE_THRESH,
>> +		.dir = IIO_EV_DIR_EITHER,
>> +		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
>> +	},
>> +};
>> +
>> +static const struct iio_chan_spec vcnl4035_channels[] = {
>> +	{
>> +		.type = IIO_INTENSITY,
>> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
>> +				BIT(IIO_CHAN_INFO_INT_TIME) |
>> +				BIT(IIO_CHAN_INFO_SCALE),
>> +		.event_spec = vcnl4035_event_spec,
>> +		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
>> +		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
>> +		.scan_type = {
>> +			.sign = 'u',
>> +			.realbits = 16,
>> +			.storagebits = 16,
>> +			.endianness = IIO_LE,
>> +		},
>> +	},
>> +};
>> +
>> +static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
>> +{
>> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +					VCNL4035_MODE_ALS_MASK,
>> +					status);
>> +}
>> +
>> +static int vcnl4035_init(struct vcnl4035_data *data)
>> +{
>> +	int ret;
>> +	int id;
>> +
>> +	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
>> +	if (ret < 0) {
>> +		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
>> +		return ret;
>> +	}
>> +
>> +	if (id != VCNL4035_DEV_ID_VAL) {
>> +		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
>> +			id, VCNL4035_DEV_ID_VAL);
>> +		return -ENODEV;
>> +	}
>> +
>> +#ifndef CONFIG_PM
>> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
>> +	if (ret < 0)
>> +		return ret;
>> +#endif
>> +	/* set default integration time - 100 ms for ALS */
>> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +				 VCNL4035_ALS_IT_MASK,
>> +				 VCNL4035_ALS_IT_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
>> +
>> +	/* set default persistence time - 1 for ALS */
>> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +				 VCNL4035_ALS_PERS_MASK,
>> +				 VCNL4035_ALS_PERS_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_update_bits default PERS returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
>> +
>> +	/* set default HIGH threshold for ALS */
>> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
>> +				VCNL4035_ALS_THDH_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_write default THDH returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
>> +
>> +	/* set default LOW threshold for ALS */
>> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
>> +				VCNL4035_ALS_THDL_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_write default THDL returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
>> +
>> +	return 0;
>> +}
>> +
>> +static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
>> +{
>> +	switch (reg) {
>> +	case VCNL4035_ALS_CONF:
>> +	case VCNL4035_DEV_ID:
>> +		return false;
>> +	default:
>> +		return true;
>> +	}
>> +}
>> +
>> +static const struct regmap_config vcnl4035_regmap_config = {
>> +	.name		= VCNL4035_REGMAP_NAME,
>> +	.reg_bits	= 8,
>> +	.val_bits	= 16,
>> +	.max_register	= VCNL4035_DEV_ID,
>> +	.cache_type	= REGCACHE_RBTREE,
>> +	.volatile_reg	= vcnl4035_is_volatile_reg,
>> +	.val_format_endian = REGMAP_ENDIAN_LITTLE,
>> +};
>> +
>> +static int vcnl4035_probe(struct i2c_client *client,
>> +				const struct i2c_device_id *id)
>> +{
>> +	struct vcnl4035_data *data;
>> +	struct iio_dev *indio_dev;
>> +	struct regmap *regmap;
>> +	int ret;
>> +
>> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
>> +	if (!indio_dev)
>> +		return -ENOMEM;
>> +
>> +	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
>> +	if (IS_ERR(regmap)) {
>> +		dev_err(&client->dev, "regmap_init failed!\n");
>> +		return PTR_ERR(regmap);
>> +	}
>> +
>> +	data = iio_priv(indio_dev);
>> +	i2c_set_clientdata(client, indio_dev);
>> +	data->client = client;
>> +	data->regmap = regmap;
>> +	mutex_init(&data->lock);
>> +
>> +	indio_dev->dev.parent = &client->dev;
>> +	indio_dev->info = &vcnl4035_info;
>> +	indio_dev->name = VCNL4035_DRV_NAME;
>> +	indio_dev->channels = vcnl4035_channels;
>> +	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>> +
>> +	ret = vcnl4035_init(data);
>> +	if (ret < 0) {
>> +		dev_err(&client->dev, "vcnl4035 chip init failed\n");
>> +		return ret;
>> +	}
>> +
>> +	ret = pm_runtime_set_active(&client->dev);
>> +	if (ret < 0)
>> +		goto fail_poweroff;
>> +
>> +	pm_runtime_enable(&client->dev);
>> +	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
>> +	pm_runtime_use_autosuspend(&client->dev);
>> +
>> +	if (client->irq) {
>> +		data->drdy_trigger0 = devm_iio_trigger_alloc(
>> +			indio_dev->dev.parent,
>> +			"%s-dev%d", indio_dev->name, indio_dev->id);
>> +		if (!data->drdy_trigger0) {
>> +			ret = -ENOMEM;
>> +			goto fail_pm_disable;
>> +		}
>> +		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
>> +		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
>> +		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
>> +		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
>> +
>> +		/* IRQ to trigger mapping */
>> +		ret = devm_request_threaded_irq(&client->dev, client->irq,
>> +			vcnl4035_drdy_irq_handler, vcnl4035_drdy_irq_thread,
>> +			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
>> +			VCNL4035_IRQ_NAME, indio_dev);
>> +		if (ret < 0) {
>> +			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
>> +				client->irq);
>> +			goto fail_pm_disable;
>> +			}
>> +
>> +		ret = devm_iio_trigger_register(indio_dev->dev.parent,
>> +						data->drdy_trigger0);
>> +		if (ret) {
>> +			dev_err(&client->dev, "iio trigger register failed\n");
>> +			goto fail_pm_disable;
>> +		}
>> +
>> +		/* Trigger setup */
>> +		ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent,
>> +			indio_dev,
>> +			vcnl4035_trigger_consumer_store_time,
>> +			vcnl4035_trigger_consumer_handler,
>> +			NULL);
>> +		if (ret < 0) {
>> +			dev_err(&client->dev, "iio triggered buffer setup failed\n");
>> +			goto fail_pm_disable;
>> +		}
>> +	}
>> +
>> +	ret = iio_device_register(indio_dev);
>> +	if (ret)
>> +		goto fail_pm_disable;
>> +	dev_info(&client->dev, "%s Ambient light/proximity sensor\n",
> 
> no need for this logging

Sure, removed it.

> 
>> +						VCNL4035_DRV_NAME);
>> +	return 0;
>> +
>> +fail_pm_disable:
>> +	pm_runtime_disable(&client->dev);
>> +	pm_runtime_set_suspended(&client->dev);
>> +	pm_runtime_put_noidle(&client->dev);
>> +fail_poweroff:
>> +	return vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
> 
> I think we want to return the 'ret' value indicating the original failure,
> not the return value of vcnl4035_set_als_power_state() which is likely success

Yes, will move the power state above and will return ret here. Thanks.

> 
>> +}
>> +
>> +static int vcnl4035_remove(struct i2c_client *client)
>> +{
>> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
>> +
>> +	iio_device_unregister(indio_dev);
>> +
>> +	pm_runtime_disable(&client->dev);
>> +	pm_runtime_set_suspended(&client->dev);
>> +	pm_runtime_put_noidle(&client->dev);
>> +
>> +	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
>> +					VCNL4035_MODE_ALS_DISABLE);
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int vcnl4035_runtime_suspend(struct device *dev)
>> +{
>> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +
>> +	mutex_lock(&data->lock);
>> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
>> +	regcache_mark_dirty(data->regmap);
>> +	mutex_unlock(&data->lock);
>> +
>> +	return ret;
>> +}
>> +
>> +static int vcnl4035_runtime_resume(struct device *dev)
>> +{
>> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +
>> +	regcache_sync(data->regmap);
>> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
>> +	if (ret < 0)
>> +		return ret;
>> +	/* wait for 1 ALS integration cycle */
>> +	msleep(data->als_it_val * 100);
>> +
>> +	return 0;
>> +}
>> +#endif
>> +
>> +static const struct dev_pm_ops vcnl4035_pm_ops = {
>> +	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
>> +				pm_runtime_force_resume)
>> +	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
>> +			   vcnl4035_runtime_resume, NULL)
>> +};
>> +
>> +static const struct of_device_id vcnl4035_of_match[] = {
>> +	{ .compatible = "vishay,vcnl4035", },
>> +	{ },
>> +};
>> +MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
>> +
>> +static const struct i2c_device_id vcnl4035_id[] = {
>> +	{ "vcnl4035", 0 },
>> +	{ }
>> +};
>> +MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
>> +
>> +static struct i2c_driver vcnl4035_driver = {
>> +	.driver = {
>> +		.name   = VCNL4035_DRV_NAME,
>> +		.pm	= &vcnl4035_pm_ops,
>> +		.of_match_table = of_match_ptr(vcnl4035_of_match),
>> +	},
>> +	.probe  = vcnl4035_probe,
>> +	.remove	= vcnl4035_remove,
>> +	.id_table = vcnl4035_id,
>> +};
>> +
>> +module_i2c_driver(vcnl4035_driver);
>> +
>> +MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
>> +MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
>> +MODULE_LICENSE("GPL v2");
>>
> 

-- 
Thanks,
Parthiban Nallathambi

DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/2] iio: light: Add support for vishay vcnl4035
  2018-06-29 17:59 ` [PATCH " Jonathan Cameron
@ 2018-07-03 13:35   ` Parthiban Nallathambi
  2018-07-07 16:23     ` Jonathan Cameron
  0 siblings, 1 reply; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-07-03 13:35 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: pn, jic23, knaack.h, lars, pmeerw, robh+dt, linux-iio,
	linux-kernel, mark.rutland, devicetree, matthias.bgg, wd, sbabic,
	hs

Hello Jonathan,

Thanks for your comments. Will send fixed v3 patch.

On 06/29/2018 07:59 PM, Jonathan Cameron wrote:
> On Thu, 28 Jun 2018 14:30:20 +0200
> Parthiban Nallathambi <pn@denx.de> wrote:
> 
>> Add support for VCNL4035, which is capable of Ambient light
>> sensing (ALS) and proximity function. This patch adds support
>> only for ALS function
>>
>> Signed-off-by: Parthiban Nallathambi <pn@denx.de>
> 
> Hi,
> 
> Looks fairly clean in general, but a few questions and minor suggestions inline.
> 
> There is some non standard ABI in here for which I'd like to have seen documentation.
> 
> Jonathan
>> ---
>>   drivers/iio/light/Kconfig    |  12 +
>>   drivers/iio/light/Makefile   |   1 +
>>   drivers/iio/light/vcnl4035.c | 684 +++++++++++++++++++++++++++++++++++++++++++
>>   3 files changed, 697 insertions(+)
>>   create mode 100644 drivers/iio/light/vcnl4035.c
>>
>> diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
>> index c7ef8d1862d6..b7069a4c28a2 100644
>> --- a/drivers/iio/light/Kconfig
>> +++ b/drivers/iio/light/Kconfig
>> @@ -447,6 +447,18 @@ config VCNL4000
>>   	 To compile this driver as a module, choose M here: the
>>   	 module will be called vcnl4000.
>>   
>> +config VCNL4035
>> +	tristate "VCNL4035 combined ALS and proximity sensor"
>> +	select REGMAP_I2C
>> +	depends on I2C
>> +	help
>> +	 Say Y here if you want to build a driver for the Vishay VCNL4035,
>> +	 combined ambient light (ALS) and proximity sensor. Currently only ALS
>> +	 function is available.
>> +
>> +	 To compile this driver as a module, choose M here: the
>> +	 module will be called vcnl4035.
>> +
>>   config VEML6070
>>   	tristate "VEML6070 UV A light sensor"
>>   	depends on I2C
>> diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
>> index 80943af5d627..dce98511a59b 100644
>> --- a/drivers/iio/light/Makefile
>> +++ b/drivers/iio/light/Makefile
>> @@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
>>   obj-$(CONFIG_TSL4531)		+= tsl4531.o
>>   obj-$(CONFIG_US5182D)		+= us5182d.o
>>   obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
>> +obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
>>   obj-$(CONFIG_VEML6070)		+= veml6070.o
>>   obj-$(CONFIG_VL6180)		+= vl6180.o
>>   obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
>> diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
>> new file mode 100644
>> index 000000000000..fc19856f965b
>> --- /dev/null
>> +++ b/drivers/iio/light/vcnl4035.c
>> @@ -0,0 +1,684 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
>> + *
>> + * Copyright (c) 2018, DENX Software Engineering GmbH
>> + * Author: Parthiban Nallathambi <pn@denx.de>
>> + *
>> + *
>> + * TODO: Proximity
>> + */
>> +#include <linux/module.h>
>> +#include <linux/i2c.h>
>> +#include <linux/regmap.h>
>> +#include <linux/pm_runtime.h>
>> +
>> +#include <linux/iio/iio.h>
>> +#include <linux/iio/sysfs.h>
>> +#include <linux/iio/buffer.h>
>> +#include <linux/iio/trigger.h>
>> +#include <linux/iio/trigger_consumer.h>
>> +#include <linux/iio/triggered_buffer.h>
>> +
>> +#define VCNL4035_DRV_NAME	"vcnl4035"
>> +#define VCNL4035_IRQ_NAME	"vcnl4035_event"
>> +#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
>> +
>> +/* Device registers */
>> +#define VCNL4035_ALS_CONF	0x00
>> +#define VCNL4035_ALS_THDH	0x01
>> +#define VCNL4035_ALS_THDL	0x02
>> +#define VCNL4035_ALS_DATA	0x0B
>> +#define	VCNL4035_INT_FLAG	0x0D
>> +#define VCNL4035_DEV_ID		0x0E
>> +
>> +/* Register masks */
>> +#define VCNL4035_MODE_ALS_MASK		BIT(0)
>> +#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
>> +#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
>> +#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
>> +#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
>> +#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
>> +
>> +/* Default values */
>> +#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
>> +#define VCNL4035_MODE_ALS_DISABLE	0x00
>> +#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
>> +#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
>> +#define VCNL4035_DEV_ID_VAL		0x80
>> +#define VCNL4035_ALS_IT_DEFAULT		0x01
>> +#define VCNL4035_ALS_PERS_DEFAULT	0x00
>> +#define VCNL4035_ALS_THDH_DEFAULT	5000
>> +#define VCNL4035_ALS_THDL_DEFAULT	100
>> +#define VCNL4035_SLEEP_DELAY_MS		2000
>> +
>> +struct vcnl4035_data {
>> +	struct i2c_client *client;
>> +	struct regmap *regmap;
>> +	/* protect device settings persistence, integration time, threshold */
>> +	struct mutex lock;
>> +	unsigned int als_it_val;
>> +	unsigned int als_persistence;
>> +	unsigned int als_thresh_low;
>> +	unsigned int als_thresh_high;
>> +	struct iio_trigger *drdy_trigger0;
>> +	s64 irq_timestamp;
>> +};
>> +
>> +static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
>> +{
>> +	int ret;
>> +	int reg;
>> +
>> +	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
>> +	if (ret < 0)
>> +		return false;
>> +	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
>> +		return true;
>> +	else
>> +		return false;
>> +}
>> +
>> +static irqreturn_t vcnl4035_drdy_irq_handler(int irq, void *private)
>> +{
>> +	struct iio_dev *indio_dev = private;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	data->irq_timestamp = iio_get_time_ns(indio_dev);
> 
> This all seems a bit more complex that I'd expect.
> I would like some comments on why it is so complex..
> 
> Only reason I can come up with quickly is to handle shared interrupts?
> But if that is the case you'll sometimes get the wrong timestamp here.
> 
> Otherwise, right now we only have one cause of interrupts and
> might as well just set pf->timestamp directly here.
> 

Based on Peter's comments, timestamp is ignored as the driver only
supports ALS measurement. So I will remove this part of handling and
will have only bottom half checking the INT flag and trigger the
consumer, thanks.

>> +	return IRQ_WAKE_THREAD;
>> +}
>> +
>> +static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
>> +{
>> +	struct iio_dev *indio_dev = private;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	if (vcnl4035_is_triggered(data)) {
>> +		iio_trigger_poll_chained(data->drdy_trigger0);
>> +		return IRQ_HANDLED;
>> +	}
>> +
>> +	return IRQ_NONE;
>> +}
>> +
>> +/* Triggered buffer */
>> +static irqreturn_t vcnl4035_trigger_consumer_store_time(int irq, void *p)
>> +{
>> +	struct iio_poll_func *pf = p;
>> +	struct iio_dev *indio_dev = pf->indio_dev;
>> +
>> +	if (!iio_trigger_using_own(indio_dev))
>> +		pf->timestamp = iio_get_time_ns(indio_dev);
>> +
>> +	return IRQ_WAKE_THREAD;
>> +}
>> +
>> +static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
>> +{
>> +	struct iio_poll_func *pf = p;
>> +	struct iio_dev *indio_dev = pf->indio_dev;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int als_data;
>> +	int ret;
>> +
>> +	if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
>> +		pf->timestamp = data->irq_timestamp;
>> +		data->irq_timestamp = 0;
> 
> If we get here and the path was anything other than via our top half
> handler that set this then something odd is going on.

As mentioned above, timestamp is removed.

> 
>> +	}
>> +
>> +	if (!pf->timestamp)
>> +		pf->timestamp = iio_get_time_ns(indio_dev);
> 
> Hmm. Why would we hit here when using our own trigger?  If not make this
> a simple else and it will be less confusing.

As mentioned above, timestamp is removed.

> 
>> +
>> +	mutex_lock(&data->lock);
>> +	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &als_data);
>> +	mutex_unlock(&data->lock);
>> +	if (!ret) {
>> +		als_data = le16_to_cpu(als_data);
> As the autobuilder pointed out, use a local variable of the correct
> endianess.

Fixed it in v2 already. Will remove the timestamp part and use
iio_push_to_buffers in v3, thanks.

> 
>> +		iio_push_to_buffers_with_timestamp(indio_dev,
>> +						   &als_data,
>> +						   pf->timestamp);
>> +	} else
>> +		dev_err(&data->client->dev,
>> +			"Trigger consumer can't read from sensor.\n");
>> +	pf->timestamp = 0;
>> +
>> +	iio_trigger_notify_done(indio_dev->trig);
>> +
>> +	return IRQ_HANDLED;
>> +}
>> +
>> +static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
>> +					bool enable_drdy)
>> +{
>> +	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
>> +					VCNL4035_MODE_ALS_INT_DISABLE;
>> +	int ret;
>> +
>> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +				 VCNL4035_MODE_ALS_INT_MASK,
>> +				 val);
>> +	if (ret)
>> +		dev_err(&data->client->dev, "%s failed\n", __func__);
> 
> I believe you can configure dev_err to output the function name anyway
> so not a lot of point of putting it here explicitly.

Yes, returned directly from regmap_update_bits now. Thanks

> 
>> +
>> +	return ret;
>> +}
>> +
>> +static const struct iio_trigger_ops vcnl4035_trigger_ops = {
>> +	.set_trigger_state = vcnl4035_als_drdy_set_state,
>> +};
>> +
>> +/*
>> + *	Device IT	INT Time(ms)	Scale (lux/step)
>> + *	000		50		0.064
>> + *	001		100		0.032
>> + *	010		200		0.016
>> + *	100		400		0.008
>> + *	101 - 111	800		0.004
>> + * Values are proportial, so ALS INT is selected for input due to
>> + * simplicity reason. Integration time value and scaling is
>> + * calculated based on device INT value
>> + *
>> + * Raw value needs to be scaled using ALS STEPS
>> + *
>> + */
>> +static int vcnl4035_read_raw(struct iio_dev *indio_dev,
>> +			    struct iio_chan_spec const *chan, int *val,
>> +			    int *val2, long mask)
>> +{
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +	int busy;
>> +	int raw_data;
>> +
>> +	switch (mask) {
>> +	case IIO_CHAN_INFO_RAW:
>> +		busy = iio_device_claim_direct_mode(indio_dev);
>> +		if (busy)
>> +			return -EBUSY;
> why not use ret = iio_device_claim...
> and return ret?  (which will be EBUSY if it's busy IIRC)

Ok, busy is removed. returned ret directly here.

>> +
>> +		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
>> +		iio_device_release_direct_mode(indio_dev);
>> +		if (ret < 0)
>> +			return ret;
>> +
>> +		*val = raw_data;
>> +		return IIO_VAL_INT;
>> +	case IIO_CHAN_INFO_INT_TIME:
>> +		mutex_lock(&data->lock);
>> +		*val = data->als_it_val * 100;
>> +		if (!*val)
>> +			*val = 50;
>> +		mutex_unlock(&data->lock);
>> +		return IIO_VAL_INT;
>> +	case IIO_CHAN_INFO_SCALE:
>> +		mutex_lock(&data->lock);
>> +		*val = 64;
>> +		if (!data->als_it_val)
>> +			*val2 = 1000;
>> +		else
>> +			*val2 = data->als_it_val * 2 * 1000;
>> +		mutex_unlock(&data->lock);
>> +		return IIO_VAL_FRACTIONAL;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +}
>> +
>> +static int vcnl4035_write_raw(struct iio_dev *indio_dev,
>> +				struct iio_chan_spec const *chan,
>> +				int val, int val2, long mask)
>> +{
>> +	int ret;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	switch (mask) {
>> +	case IIO_CHAN_INFO_INT_TIME:
>> +		if (val <= 0 || val > 800)
>> +			return -EINVAL;
>> +		mutex_lock(&data->lock);
>> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +					 VCNL4035_ALS_IT_MASK,
>> +					 val / 100);
>> +		if (!ret)
>> +			data->als_it_val = val / 100;
>> +		mutex_unlock(&data->lock);
>> +		break;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +	return ret;
>> +}
>> +
>> +/* No direct ABI for persistence and threshold, so eventing */
>> +static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
>> +		const struct iio_chan_spec *chan, enum iio_event_type type,
>> +		enum iio_event_direction dir, enum iio_event_info info,
>> +		int *val, int *val2)
>> +{
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	switch (info) {
>> +	case IIO_EV_INFO_VALUE:
>> +		switch (dir) {
>> +		case IIO_EV_DIR_RISING:
>> +			mutex_lock(&data->lock);
>> +			*val = data->als_thresh_high;
>> +			mutex_unlock(&data->lock);
>> +			break;
>> +		case IIO_EV_DIR_FALLING:
>> +			mutex_lock(&data->lock);
>> +			*val = data->als_thresh_low;
>> +			mutex_unlock(&data->lock);
>> +			break;
>> +		default:
>> +			return -EINVAL;
>> +		}
>> +		break;
>> +	case IIO_EV_INFO_PERIOD:
>> +		mutex_lock(&data->lock);
>> +		*val = data->als_persistence;
>> +		mutex_unlock(&data->lock);
> 
> Given mutex is taken in all paths, why not take it outside
> the switch?  Mind you I'm not totally sure why we need to take
> the lock at all.  We don't take it whilst setting these values.

Lock is introduced to protect the device parameters as both integration
time and scaling factor is proportionally calculated. This makes raw
read and INT read sync with the new/latest updated value of device.

I will move the locking outside the switch.

> 
> 
>> +		break;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +
>> +	return IIO_VAL_INT;
>> +}
>> +
>> +static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
>> +		const struct iio_chan_spec *chan, enum iio_event_type type,
>> +		enum iio_event_direction dir, enum iio_event_info info, int val,
>> +		int val2)
>> +{
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +
>> +	switch (info) {
>> +	case IIO_EV_INFO_VALUE:
>> +		/* 16 bit threshold range */
>> +		if (val < 0 || val > 65535)
>> +			return -EINVAL;
>> +		if (dir == IIO_EV_DIR_RISING) {
>> +			if (val < data->als_thresh_low)
>> +				return -EINVAL;
>> +			mutex_lock(&data->lock);
>> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
>> +					   val);
>> +			mutex_unlock(&data->lock);
>> +			if (ret)
>> +				return ret;
>> +			data->als_thresh_high = val;
>> +		} else {
>> +			if (val > data->als_thresh_high)
>> +				return -EINVAL;
>> +			mutex_lock(&data->lock);
>> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
>> +					   val);
>> +			mutex_unlock(&data->lock);
>> +			if (ret)
>> +				return ret;
>> +			data->als_thresh_low = val;
>> +		}
>> +		break;
>> +	case IIO_EV_INFO_PERIOD:
>> +		if (val < 0 || val > 3)
>> +			return -EINVAL;
>> +		mutex_lock(&data->lock);
>> +		/* device persistence values 1 2 4 8 mapped with 0 1 2 3 */
>> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +					 VCNL4035_ALS_PERS_MASK, 1 << val);
>> +		mutex_unlock(&data->lock);
>> +		if (ret)
>> +			return ret;
>> +		data->als_persistence = val;
>> +		break;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +	return 0;
>> +}
>> +
>> +static IIO_CONST_ATTR(als_available_integration_time, "50 100 200 400 800");
>> +/* device ALS persistence 1 2 4 8 mapped to users 0 1 2 3 */
>> +static IIO_CONST_ATTR(als_available_persistence, "0 1 2 3");
>> +static IIO_CONST_ATTR(als_available_threshold_range, "0 65535");
>> +
>> +static struct attribute *vcnl4035_attributes[] = {
>> +	&iio_const_attr_als_available_integration_time.dev_attr.attr,
> 
> These don't comply with the abi which has available at the end not the start.

Sorry, so this needs to be moved to begining or start of the file.

> 
>> +	&iio_const_attr_als_available_threshold_range.dev_attr.attr,
> 
> I'm not even sure what these two are...
> 
> All new ABI or ABI that is currently specific to too narrow a range of
> parts must be documented in /Documentation/ABI/testing/sysfs-bus-iio-*

Threshold range: Defines the lower and upper threshold ALS value. This
internally used by the device before triggering the interrupt i.e,
interrupt will be triggered only of the ALS values falls below or above
the threshold range here.

Persistence value: How many intergration cycle the ALS values stays
lower or uppers then the threshold value.

Should I need to include a new ABI to handle this case or introduce a
driver specific sysfs entry to control theses values is fine.

I have seen your comments about TSL2591 in the ML. So adding a new ABI
for the time PERIOD (in different form - persistence) does not make
sense. But this value lowers the amount of interrupt which is generated
by the device.

> 
>> +	&iio_const_attr_als_available_persistence.dev_attr.attr,
>> +	NULL,
>> +};
>> +
>> +static const struct attribute_group vcnl4035_attribute_group = {
>> +	.attrs = vcnl4035_attributes,
>> +};
>> +
>> +static const struct iio_info vcnl4035_info = {
>> +	.read_raw		= vcnl4035_read_raw,
>> +	.write_raw		= vcnl4035_write_raw,
>> +	.read_event_value	= vcnl4035_read_thresh,
>> +	.write_event_value	= vcnl4035_write_thresh,
>> +	.attrs			= &vcnl4035_attribute_group,
>> +};
>> +
>> +enum vcnl4035_scan_index_order {
>> +	VCNL4035_CHAN_INDEX_LIGHT,
>> +};
>> +
>> +static const unsigned long vcnl4035_available_scan_masks[] = {
>> +	BIT(VCNL4035_CHAN_INDEX_LIGHT), 0
>> +};
>> +
>> +static const struct iio_event_spec vcnl4035_event_spec[] = {
>> +	{
>> +		.type = IIO_EV_TYPE_THRESH,
>> +		.dir = IIO_EV_DIR_RISING,
>> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
>> +	}, {
>> +		.type = IIO_EV_TYPE_THRESH,
>> +		.dir = IIO_EV_DIR_FALLING,
>> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
>> +	}, {
>> +		.type = IIO_EV_TYPE_THRESH,
>> +		.dir = IIO_EV_DIR_EITHER,
>> +		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
>> +	},
>> +};
>> +
>> +static const struct iio_chan_spec vcnl4035_channels[] = {
>> +	{
>> +		.type = IIO_INTENSITY,
>> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
>> +				BIT(IIO_CHAN_INFO_INT_TIME) |
>> +				BIT(IIO_CHAN_INFO_SCALE),
>> +		.event_spec = vcnl4035_event_spec,
>> +		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
>> +		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
>> +		.scan_type = {
>> +			.sign = 'u',
>> +			.realbits = 16,
>> +			.storagebits = 16,
>> +			.endianness = IIO_LE,
>> +		},
>> +	},
>> +};
>> +
>> +static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
>> +{
>> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +					VCNL4035_MODE_ALS_MASK,
>> +					status);
>> +}
>> +
>> +static int vcnl4035_init(struct vcnl4035_data *data)
>> +{
>> +	int ret;
>> +	int id;
>> +
>> +	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
>> +	if (ret < 0) {
>> +		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
>> +		return ret;
>> +	}
>> +
>> +	if (id != VCNL4035_DEV_ID_VAL) {
>> +		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
>> +			id, VCNL4035_DEV_ID_VAL);
>> +		return -ENODEV;
>> +	}
>> +
>> +#ifndef CONFIG_PM
>> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
>> +	if (ret < 0)
>> +		return ret;
>> +#endif
>> +	/* set default integration time - 100 ms for ALS */
>> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +				 VCNL4035_ALS_IT_MASK,
>> +				 VCNL4035_ALS_IT_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
>> +
>> +	/* set default persistence time - 1 for ALS */
>> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +				 VCNL4035_ALS_PERS_MASK,
>> +				 VCNL4035_ALS_PERS_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_update_bits default PERS returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
>> +
>> +	/* set default HIGH threshold for ALS */
>> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
>> +				VCNL4035_ALS_THDH_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_write default THDH returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
>> +
>> +	/* set default LOW threshold for ALS */
>> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
>> +				VCNL4035_ALS_THDL_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_write default THDL returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
>> +
>> +	return 0;
>> +}
>> +
>> +static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
>> +{
>> +	switch (reg) {
>> +	case VCNL4035_ALS_CONF:
>> +	case VCNL4035_DEV_ID:
>> +		return false;
>> +	default:
>> +		return true;
>> +	}
>> +}
>> +
>> +static const struct regmap_config vcnl4035_regmap_config = {
>> +	.name		= VCNL4035_REGMAP_NAME,
>> +	.reg_bits	= 8,
>> +	.val_bits	= 16,
>> +	.max_register	= VCNL4035_DEV_ID,
>> +	.cache_type	= REGCACHE_RBTREE,
>> +	.volatile_reg	= vcnl4035_is_volatile_reg,
>> +	.val_format_endian = REGMAP_ENDIAN_LITTLE,
>> +};
>> +
>> +static int vcnl4035_probe(struct i2c_client *client,
>> +				const struct i2c_device_id *id)
>> +{
>> +	struct vcnl4035_data *data;
>> +	struct iio_dev *indio_dev;
>> +	struct regmap *regmap;
>> +	int ret;
>> +
>> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
>> +	if (!indio_dev)
>> +		return -ENOMEM;
>> +
>> +	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
>> +	if (IS_ERR(regmap)) {
>> +		dev_err(&client->dev, "regmap_init failed!\n");
>> +		return PTR_ERR(regmap);
>> +	}
>> +
>> +	data = iio_priv(indio_dev);
>> +	i2c_set_clientdata(client, indio_dev);
>> +	data->client = client;
>> +	data->regmap = regmap;
>> +	mutex_init(&data->lock);
>> +
>> +	indio_dev->dev.parent = &client->dev;
>> +	indio_dev->info = &vcnl4035_info;
>> +	indio_dev->name = VCNL4035_DRV_NAME;
>> +	indio_dev->channels = vcnl4035_channels;
>> +	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>> +
>> +	ret = vcnl4035_init(data);
>> +	if (ret < 0) {
>> +		dev_err(&client->dev, "vcnl4035 chip init failed\n");
>> +		return ret;
>> +	}
>> +
>> +	ret = pm_runtime_set_active(&client->dev);
>> +	if (ret < 0)
>> +		goto fail_poweroff;
>> +
>> +	pm_runtime_enable(&client->dev);
>> +	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
>> +	pm_runtime_use_autosuspend(&client->dev);
>> +
>> +	if (client->irq) {
>> +		data->drdy_trigger0 = devm_iio_trigger_alloc(
>> +			indio_dev->dev.parent,
>> +			"%s-dev%d", indio_dev->name, indio_dev->id);
>> +		if (!data->drdy_trigger0) {
>> +			ret = -ENOMEM;
>> +			goto fail_pm_disable;
>> +		}
>> +		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
>> +		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
>> +		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
>> +		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
>> +
>> +		/* IRQ to trigger mapping */
>> +		ret = devm_request_threaded_irq(&client->dev, client->irq,
>> +			vcnl4035_drdy_irq_handler, vcnl4035_drdy_irq_thread,
>> +			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
>> +			VCNL4035_IRQ_NAME, indio_dev);
>> +		if (ret < 0) {
>> +			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
>> +				client->irq);
>> +			goto fail_pm_disable;
>> +			}
>> +
>> +		ret = devm_iio_trigger_register(indio_dev->dev.parent,
>> +						data->drdy_trigger0);
>> +		if (ret) {
>> +			dev_err(&client->dev, "iio trigger register failed\n");
>> +			goto fail_pm_disable;
>> +		}
>> +
>> +		/* Trigger setup */
>> +		ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent,
> 
> I would prefer to see a clear unroll in the reverse order of setup.
> That means you can't safely mix managed and unmanaged cleanup.  Only use
> devm until the point where you have to do something that can't be managed, or
> use the devm action approach to move all the cleanup into the managed path.

Sure. I have moved this as unmanaged setup and also the trigger_register
part and moved it above. Will clean the buffer setup explicit in failure

> 
>> +			indio_dev,
>> +			vcnl4035_trigger_consumer_store_time,
>> +			vcnl4035_trigger_consumer_handler,
>> +			NULL);
>> +		if (ret < 0) {
>> +			dev_err(&client->dev, "iio triggered buffer setup failed\n");
>> +			goto fail_pm_disable;
>> +		}
>> +	}
>> +
>> +	ret = iio_device_register(indio_dev);
>> +	if (ret)
>> +		goto fail_pm_disable;
>> +	dev_info(&client->dev, "%s Ambient light/proximity sensor\n",
>> +						VCNL4035_DRV_NAME);
> 
> I'm generally against this sort of print unless it adds real value.
> Here it is just printing out something you can easily establish by lots
> of other means.  Hence please drop it.

Thanks, removed it.

> 
>> +	return 0;
>> +
>> +fail_pm_disable:
>> +	pm_runtime_disable(&client->dev);
>> +	pm_runtime_set_suspended(&client->dev);
>> +	pm_runtime_put_noidle(&client->dev);
>> +fail_poweroff:
>> +	return vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
>> +}
>> +
>> +static int vcnl4035_remove(struct i2c_client *client)
>> +{
>> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
>> +
>> +	iio_device_unregister(indio_dev);
>> +
>> +	pm_runtime_disable(&client->dev);
>> +	pm_runtime_set_suspended(&client->dev);
>> +	pm_runtime_put_noidle(&client->dev);
>> +
>> +	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
>> +					VCNL4035_MODE_ALS_DISABLE);
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int vcnl4035_runtime_suspend(struct device *dev)
>> +{
>> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +
>> +	mutex_lock(&data->lock);
>> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
>> +	regcache_mark_dirty(data->regmap);
>> +	mutex_unlock(&data->lock);
>> +
>> +	return ret;
>> +}
>> +
>> +static int vcnl4035_runtime_resume(struct device *dev)
>> +{
>> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +
>> +	regcache_sync(data->regmap);
>> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
>> +	if (ret < 0)
>> +		return ret;
>> +	/* wait for 1 ALS integration cycle */
>> +	msleep(data->als_it_val * 100);
>> +
>> +	return 0;
>> +}
>> +#endif
>> +
>> +static const struct dev_pm_ops vcnl4035_pm_ops = {
>> +	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
>> +				pm_runtime_force_resume)
>> +	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
>> +			   vcnl4035_runtime_resume, NULL)
>> +};
>> +
>> +static const struct of_device_id vcnl4035_of_match[] = {
>> +	{ .compatible = "vishay,vcnl4035", },
>> +	{ },
>> +};
>> +MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
>> +
>> +static const struct i2c_device_id vcnl4035_id[] = {
>> +	{ "vcnl4035", 0 },
>> +	{ }
>> +};
>> +MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
>> +
>> +static struct i2c_driver vcnl4035_driver = {
>> +	.driver = {
>> +		.name   = VCNL4035_DRV_NAME,
>> +		.pm	= &vcnl4035_pm_ops,
>> +		.of_match_table = of_match_ptr(vcnl4035_of_match),
>> +	},
>> +	.probe  = vcnl4035_probe,
>> +	.remove	= vcnl4035_remove,
>> +	.id_table = vcnl4035_id,
>> +};
>> +
>> +module_i2c_driver(vcnl4035_driver);
>> +
>> +MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
>> +MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
>> +MODULE_LICENSE("GPL v2");
> 
> 
> 

-- 
Thanks,
Parthiban Nallathambi

DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 2/2] iio: light: Add device tree binding for vishay vcnl4035
  2018-06-29 15:38   ` [PATCH v2 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
@ 2018-07-06 20:34     ` Rob Herring
  2018-07-07 17:06       ` Parthiban Nallathambi
  0 siblings, 1 reply; 19+ messages in thread
From: Rob Herring @ 2018-07-06 20:34 UTC (permalink / raw)
  To: Parthiban Nallathambi
  Cc: jic23, knaack.h, lars, pmeerw, linux-iio, linux-kernel,
	mark.rutland, devicetree, matthias.bgg, wd, sbabic, hs

On Fri, Jun 29, 2018 at 05:38:02PM +0200, Parthiban Nallathambi wrote:
> Adding device tree binding for vcnl4035 and vendor
> prefix for Vishay Intertechnology
> 
> Signed-off-by: Parthiban Nallathambi <pn@denx.de>
> ---
>  .../devicetree/bindings/iio/light/vcnl4035.txt        | 19 +++++++++++++++++++
>  Documentation/devicetree/bindings/vendor-prefixes.txt |  1 +
>  2 files changed, 20 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/light/vcnl4035.txt
> 
> diff --git a/Documentation/devicetree/bindings/iio/light/vcnl4035.txt b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
> new file mode 100644
> index 000000000000..7ffdc0246300
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
> @@ -0,0 +1,19 @@
> +VISHAY VCNL4035 -  Ambient Light and proximity sensor
> +
> +Link to datasheet: https://www.vishay.com/docs/84251/vcnl4035x01.pdf
> +
> +Required properties:
> +
> +	-compatible: should be "vishay,vcnl4035"
> +	-reg: I2C address of the sensor, should be 0x60
> +	- interrupt-parent: should be the phandle for the interrupt controller

Don't need to document this as it could be in a parent node.

> +	- interrupts: interrupt mapping for GPIO IRQ (level active low)
> +
> +Example:
> +
> +vcnl4035@60 {

light-sensor@60

> +	compatible = "vishay,vcnl4035";
> +	reg = <0x60>;
> +	interrupt-parent = <&gpio4>;
> +	interrupts = <11 IRQ_TYPE_LEVEL_LOW>;
> +};
> diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
> index 7cad066191ee..3cc46d5735a9 100644
> --- a/Documentation/devicetree/bindings/vendor-prefixes.txt
> +++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
> @@ -395,6 +395,7 @@ v3	V3 Semiconductor
>  variscite	Variscite Ltd.
>  via	VIA Technologies, Inc.
>  virtio	Virtual I/O Device Specification, developed by the OASIS consortium
> +vishay	Vishay Intertechnology, Inc
>  vivante	Vivante Corporation
>  vocore VoCore Studio
>  voipac	Voipac Technologies s.r.o.
> -- 
> 2.14.4
> 

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/2] iio: light: Add support for vishay vcnl4035
  2018-07-03 13:35   ` Parthiban Nallathambi
@ 2018-07-07 16:23     ` Jonathan Cameron
  0 siblings, 0 replies; 19+ messages in thread
From: Jonathan Cameron @ 2018-07-07 16:23 UTC (permalink / raw)
  To: Parthiban Nallathambi
  Cc: Jonathan Cameron, knaack.h, lars, pmeerw, robh+dt, linux-iio,
	linux-kernel, mark.rutland, devicetree, matthias.bgg, wd, sbabic,
	hs

On Tue, 3 Jul 2018 15:35:05 +0200
Parthiban Nallathambi <pn@denx.de> wrote:

> Hello Jonathan,
Hi Parthiban,

Comments inline.
...
> >> +static irqreturn_t vcnl4035_drdy_irq_handler(int irq, void *private)
> >> +{
> >> +	struct iio_dev *indio_dev = private;
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +
> >> +	data->irq_timestamp = iio_get_time_ns(indio_dev);  
> > 
> > This all seems a bit more complex that I'd expect.
> > I would like some comments on why it is so complex..
> > 
> > Only reason I can come up with quickly is to handle shared interrupts?
> > But if that is the case you'll sometimes get the wrong timestamp here.
> > 
> > Otherwise, right now we only have one cause of interrupts and
> > might as well just set pf->timestamp directly here.
> >   
> 
> Based on Peter's comments, timestamp is ignored as the driver only
> supports ALS measurement. So I will remove this part of handling and
> will have only bottom half checking the INT flag and trigger the
> consumer, thanks.
> 
> >> +	return IRQ_WAKE_THREAD;
> >> +}
> >> +
> >> +static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
> >> +{
> >> +	struct iio_dev *indio_dev = private;
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +
> >> +	if (vcnl4035_is_triggered(data)) {
> >> +		iio_trigger_poll_chained(data->drdy_trigger0);
> >> +		return IRQ_HANDLED;
> >> +	}
> >> +
> >> +	return IRQ_NONE;
> >> +}
> >> +
> >> +/* Triggered buffer */
> >> +static irqreturn_t vcnl4035_trigger_consumer_store_time(int irq, void *p)
> >> +{
> >> +	struct iio_poll_func *pf = p;
> >> +	struct iio_dev *indio_dev = pf->indio_dev;
> >> +
> >> +	if (!iio_trigger_using_own(indio_dev))
> >> +		pf->timestamp = iio_get_time_ns(indio_dev);
> >> +
> >> +	return IRQ_WAKE_THREAD;
> >> +}
> >> +
> >> +static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
> >> +{
> >> +	struct iio_poll_func *pf = p;
> >> +	struct iio_dev *indio_dev = pf->indio_dev;
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +	int als_data;
> >> +	int ret;
> >> +
> >> +	if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
> >> +		pf->timestamp = data->irq_timestamp;
> >> +		data->irq_timestamp = 0;  
> > 
> > If we get here and the path was anything other than via our top half
> > handler that set this then something odd is going on.  
> 
> As mentioned above, timestamp is removed.
> 
> >   
> >> +	}
> >> +
> >> +	if (!pf->timestamp)
> >> +		pf->timestamp = iio_get_time_ns(indio_dev);  
> > 
> > Hmm. Why would we hit here when using our own trigger?  If not make this
> > a simple else and it will be less confusing.  
> 
> As mentioned above, timestamp is removed.
> 
> >   
> >> +
> >> +	mutex_lock(&data->lock);
> >> +	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &als_data);
> >> +	mutex_unlock(&data->lock);
> >> +	if (!ret) {
> >> +		als_data = le16_to_cpu(als_data);  
> > As the autobuilder pointed out, use a local variable of the correct
> > endianess.  
> 
> Fixed it in v2 already. Will remove the timestamp part and use
> iio_push_to_buffers in v3, thanks.
> 

I'll look at v3 later. I'm not totally sure why supporting ALS only
means we shouldn't have a timestamp - perhaps the code will make it clear!

...
> >> +
> >> +/* No direct ABI for persistence and threshold, so eventing */
> >> +static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
> >> +		const struct iio_chan_spec *chan, enum iio_event_type type,
> >> +		enum iio_event_direction dir, enum iio_event_info info,
> >> +		int *val, int *val2)
> >> +{
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +
> >> +	switch (info) {
> >> +	case IIO_EV_INFO_VALUE:
> >> +		switch (dir) {
> >> +		case IIO_EV_DIR_RISING:
> >> +			mutex_lock(&data->lock);
> >> +			*val = data->als_thresh_high;
> >> +			mutex_unlock(&data->lock);
> >> +			break;
> >> +		case IIO_EV_DIR_FALLING:
> >> +			mutex_lock(&data->lock);
> >> +			*val = data->als_thresh_low;
> >> +			mutex_unlock(&data->lock);
> >> +			break;
> >> +		default:
> >> +			return -EINVAL;
> >> +		}
> >> +		break;
> >> +	case IIO_EV_INFO_PERIOD:
> >> +		mutex_lock(&data->lock);
> >> +		*val = data->als_persistence;
> >> +		mutex_unlock(&data->lock);  
> > 
> > Given mutex is taken in all paths, why not take it outside
> > the switch?  Mind you I'm not totally sure why we need to take
> > the lock at all.  We don't take it whilst setting these values.  
> 
> Lock is introduced to protect the device parameters as both integration
> time and scaling factor is proportionally calculated. This makes raw
> read and INT read sync with the new/latest updated value of device.

All we ensure here is that we don't get a transient value.  It's still
possible for the read raw or interrupt to occur on either side
of this.  I'm not seeing how the lock is preventing us getting an
incorrect state (i.e. a different one from what we are expecting)?

All of these assignments are single value that will be atomic anyway.
You'll only see one value or the other when reading - never a mixture
of the two!
> 
> I will move the locking outside the switch.
> 
> > 
...
> >> +static IIO_CONST_ATTR(als_available_integration_time, "50 100 200 400 800");
> >> +/* device ALS persistence 1 2 4 8 mapped to users 0 1 2 3 */
> >> +static IIO_CONST_ATTR(als_available_persistence, "0 1 2 3");
> >> +static IIO_CONST_ATTR(als_available_threshold_range, "0 65535");
> >> +
> >> +static struct attribute *vcnl4035_attributes[] = {
> >> +	&iio_const_attr_als_available_integration_time.dev_attr.attr,  
> > 
> > These don't comply with the abi which has available at the end not the start.  
> 
> Sorry, so this needs to be moved to begining or start of the file.
Sorry, I was unclear.  The issue is that _available is always a postfix.

So even if we accept the rest of the naming it should be
als_persistence_available etc.

Note these are non standard abi however..

> 
> >   
> >> +	&iio_const_attr_als_available_threshold_range.dev_attr.attr,  
> > 
> > I'm not even sure what these two are...
> > 
> > All new ABI or ABI that is currently specific to too narrow a range of
> > parts must be documented in /Documentation/ABI/testing/sysfs-bus-iio-*  
> 
> Threshold range: Defines the lower and upper threshold ALS value. This
> internally used by the device before triggering the interrupt i.e,
> interrupt will be triggered only of the ALS values falls below or above
> the threshold range here.

So in reality this device is generate threshold events, we are just using
them to indicate new data ready?   Please handle as explicit events
in IIO.  If you want to also use them as a trigger, then that can be
done by basically disabling the events if the trigger is enabled and
the other way around.

> 
> Persistence value: How many intergration cycle the ALS values stays
> lower or uppers then the threshold value.

This is very much an event property.  See the existing ABI defined
for those.  It might need extending, I haven't checked.

> 
> Should I need to include a new ABI to handle this case or introduce a
> driver specific sysfs entry to control theses values is fine.

It depends.  The vast majority of the time, for something fairly generic
we want to bring it into the main ABI.  That makes it useful for generic
userspace code.   Just occasionally we'll have something so far from
standard that generic code is never going to use it (often around wierd
calibration requirements) so in those cases, just a properly defined
(documented) sysfs interface is fine.

> 
> I have seen your comments about TSL2591 in the ML. So adding a new ABI
> for the time PERIOD (in different form - persistence) does not make
> sense. But this value lowers the amount of interrupt which is generated
> by the device.

This depends on whether you are intending it to be a 'data ready trigger'
in which case what you are actually controlling is the effective sampling
rate (so present it as that) or you are intending it as a threshold
interrupt control, in which case present it as a control on the generated
event, not the data capture itself.
> 
> >   
> >> +	&iio_const_attr_als_available_persistence.dev_attr.attr,
> >> +	NULL,
> >> +};
> >> +
...

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 2/2] iio: light: Add device tree binding for vishay vcnl4035
  2018-07-06 20:34     ` Rob Herring
@ 2018-07-07 17:06       ` Parthiban Nallathambi
  0 siblings, 0 replies; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-07-07 17:06 UTC (permalink / raw)
  To: Rob Herring
  Cc: jic23, knaack.h, lars, pmeerw, linux-iio, linux-kernel,
	mark.rutland, devicetree, matthias.bgg, wd, sbabic, hs, pn

Hi Rob,

On 07/06/2018 10:34 PM, Rob Herring wrote:
> On Fri, Jun 29, 2018 at 05:38:02PM +0200, Parthiban Nallathambi wrote:
>> Adding device tree binding for vcnl4035 and vendor
>> prefix for Vishay Intertechnology
>>
>> Signed-off-by: Parthiban Nallathambi <pn@denx.de>
>> ---
>>  .../devicetree/bindings/iio/light/vcnl4035.txt        | 19 +++++++++++++++++++
>>  Documentation/devicetree/bindings/vendor-prefixes.txt |  1 +
>>  2 files changed, 20 insertions(+)
>>  create mode 100644 Documentation/devicetree/bindings/iio/light/vcnl4035.txt
>>
>> diff --git a/Documentation/devicetree/bindings/iio/light/vcnl4035.txt b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
>> new file mode 100644
>> index 000000000000..7ffdc0246300
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
>> @@ -0,0 +1,19 @@
>> +VISHAY VCNL4035 -  Ambient Light and proximity sensor
>> +
>> +Link to datasheet: https://www.vishay.com/docs/84251/vcnl4035x01.pdf
>> +
>> +Required properties:
>> +
>> +	-compatible: should be "vishay,vcnl4035"
>> +	-reg: I2C address of the sensor, should be 0x60
>> +	- interrupt-parent: should be the phandle for the interrupt controller
> 
> Don't need to document this as it could be in a parent node.

Will remove it in v3, thanks.

> 
>> +	- interrupts: interrupt mapping for GPIO IRQ (level active low)
>> +
>> +Example:
>> +
>> +vcnl4035@60 {
> 
> light-sensor@60

Sure, thanks.

> 
>> +	compatible = "vishay,vcnl4035";
>> +	reg = <0x60>;
>> +	interrupt-parent = <&gpio4>;
>> +	interrupts = <11 IRQ_TYPE_LEVEL_LOW>;
>> +};
>> diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
>> index 7cad066191ee..3cc46d5735a9 100644
>> --- a/Documentation/devicetree/bindings/vendor-prefixes.txt
>> +++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
>> @@ -395,6 +395,7 @@ v3	V3 Semiconductor
>>  variscite	Variscite Ltd.
>>  via	VIA Technologies, Inc.
>>  virtio	Virtual I/O Device Specification, developed by the OASIS consortium
>> +vishay	Vishay Intertechnology, Inc
>>  vivante	Vivante Corporation
>>  vocore VoCore Studio
>>  voipac	Voipac Technologies s.r.o.
>> -- 
>> 2.14.4
>>
> 

-- 
Thanks,
Parthiban N

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v3 1/2] iio: light: Add support for vishay vcnl4035
  2018-06-29 15:38 ` [PATCH v2 " Parthiban Nallathambi
                     ` (2 preceding siblings ...)
  2018-06-30  9:51   ` Peter Meerwald-Stadler
@ 2018-08-02  9:52   ` Parthiban Nallathambi
  2018-08-02  9:52     ` [PATCH v3 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
  2018-08-02 12:49     ` [PATCH v3 1/2] iio: light: Add support " Jonathan Cameron
  3 siblings, 2 replies; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-08-02  9:52 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, robh+dt
  Cc: linux-iio, linux-kernel, mark.rutland, devicetree, matthias.bgg,
	wd, sbabic, hs, Parthiban Nallathambi

Add support for VCNL4035, which is capable of Ambient light
sensing (ALS) and proximity function. This patch adds support
only for ALS function

Signed-off-by: Parthiban Nallathambi <pn@denx.de>
-------

Changelog since v1:

1. Fixed 0-day warning on le16_to_cpu usage
2. Persistence value is directly mapped to datasheet's value to
avoid confusions of usage from sysfs

Changelog in v3:
- Usage of lock is not needed, removed mutex locking
- ALS threshold and persistence configuration available
as events and threshold events are notified to userspace
- Usage of devm_ is re-ordered and exit handling updated
- Complexity in timestamp calculation is removed and used
iio_get_time_ns
---
 drivers/iio/light/Kconfig    |  12 +
 drivers/iio/light/Makefile   |   1 +
 drivers/iio/light/vcnl4035.c | 619 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 632 insertions(+)
 create mode 100644 drivers/iio/light/vcnl4035.c

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index c7ef8d1862d6..b7069a4c28a2 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -447,6 +447,18 @@ config VCNL4000
 	 To compile this driver as a module, choose M here: the
 	 module will be called vcnl4000.
 
+config VCNL4035
+	tristate "VCNL4035 combined ALS and proximity sensor"
+	select REGMAP_I2C
+	depends on I2C
+	help
+	 Say Y here if you want to build a driver for the Vishay VCNL4035,
+	 combined ambient light (ALS) and proximity sensor. Currently only ALS
+	 function is available.
+
+	 To compile this driver as a module, choose M here: the
+	 module will be called vcnl4035.
+
 config VEML6070
 	tristate "VEML6070 UV A light sensor"
 	depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 80943af5d627..dce98511a59b 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
 obj-$(CONFIG_TSL4531)		+= tsl4531.o
 obj-$(CONFIG_US5182D)		+= us5182d.o
 obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
+obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
 obj-$(CONFIG_VEML6070)		+= veml6070.o
 obj-$(CONFIG_VL6180)		+= vl6180.o
 obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
new file mode 100644
index 000000000000..d2b5ae32baa8
--- /dev/null
+++ b/drivers/iio/light/vcnl4035.c
@@ -0,0 +1,619 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
+ *
+ * Copyright (c) 2018, DENX Software Engineering GmbH
+ * Author: Parthiban Nallathambi <pn@denx.de>
+ *
+ *
+ * TODO: Proximity
+ */
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
+#include <linux/pm_runtime.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/events.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+#define VCNL4035_DRV_NAME	"vcnl4035"
+#define VCNL4035_IRQ_NAME	"vcnl4035_event"
+#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
+
+/* Device registers */
+#define VCNL4035_ALS_CONF	0x00
+#define VCNL4035_ALS_THDH	0x01
+#define VCNL4035_ALS_THDL	0x02
+#define VCNL4035_ALS_DATA	0x0B
+#define VCNL4035_INT_FLAG	0x0D
+#define VCNL4035_DEV_ID		0x0E
+
+/* Register masks */
+#define VCNL4035_MODE_ALS_MASK		BIT(0)
+#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
+#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
+#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
+#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
+#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
+
+/* Default values */
+#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
+#define VCNL4035_MODE_ALS_DISABLE	0x00
+#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
+#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
+#define VCNL4035_DEV_ID_VAL		0x80
+#define VCNL4035_ALS_IT_DEFAULT		0x01
+#define VCNL4035_ALS_PERS_DEFAULT	0x00
+#define VCNL4035_ALS_THDH_DEFAULT	5000
+#define VCNL4035_ALS_THDL_DEFAULT	100
+#define VCNL4035_SLEEP_DELAY_MS		2000
+
+struct vcnl4035_data {
+	struct i2c_client *client;
+	struct regmap *regmap;
+	unsigned int als_it_val;
+	unsigned int als_persistence:4;
+	unsigned int als_thresh_low;
+	unsigned int als_thresh_high;
+	struct iio_trigger *drdy_trigger0;
+};
+
+static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
+{
+	int ret;
+	int reg;
+
+	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
+	if (ret < 0)
+		return false;
+	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
+		return true;
+	else
+		return false;
+}
+
+static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	if (vcnl4035_is_triggered(data)) {
+		iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
+							0,
+							IIO_EV_TYPE_THRESH,
+							IIO_EV_DIR_EITHER),
+				iio_get_time_ns(indio_dev));
+		iio_trigger_poll_chained(data->drdy_trigger0);
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
+/* Triggered buffer */
+static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	u16 buffer[ALIGN(sizeof(u16), sizeof(s64)) + sizeof(s64)];
+	int ret;
+
+	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, (int *)buffer);
+	if (!ret)
+		iio_push_to_buffers_with_timestamp(indio_dev, buffer,
+						iio_get_time_ns(indio_dev));
+	else
+		dev_err(&data->client->dev,
+			"Trigger consumer can't read from sensor.\n");
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
+					bool enable_drdy)
+{
+	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
+					VCNL4035_MODE_ALS_INT_DISABLE;
+
+	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+				 VCNL4035_MODE_ALS_INT_MASK,
+				 val);
+}
+
+static const struct iio_trigger_ops vcnl4035_trigger_ops = {
+	.validate_device = iio_trigger_validate_own_device,
+	.set_trigger_state = vcnl4035_als_drdy_set_state,
+};
+
+/*
+ *	Device IT	INT Time (ms)	Scale (lux/step)
+ *	000		50		0.064
+ *	001		100		0.032
+ *	010		200		0.016
+ *	100		400		0.008
+ *	101 - 111	800		0.004
+ * Values are proportional, so ALS INT is selected for input due to
+ * simplicity reason. Integration time value and scaling is
+ * calculated based on device INT value
+ *
+ * Raw value needs to be scaled using ALS steps
+ *
+ */
+static int vcnl4035_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan, int *val,
+			    int *val2, long mask)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+	int raw_data;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		ret = iio_device_claim_direct_mode(indio_dev);
+		if (ret)
+			return -ret;
+
+		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
+		iio_device_release_direct_mode(indio_dev);
+		if (ret < 0)
+			return ret;
+
+		*val = raw_data;
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_INT_TIME:
+		*val = 50;
+		if (!data->als_it_val)
+			*val = data->als_it_val * 100;
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		*val = 64;
+		if (!data->als_it_val)
+			*val2 = 1000;
+		else
+			*val2 = data->als_it_val * 2 * 1000;
+		return IIO_VAL_FRACTIONAL;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int vcnl4035_write_raw(struct iio_dev *indio_dev,
+				struct iio_chan_spec const *chan,
+				int val, int val2, long mask)
+{
+	int ret;
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_INT_TIME:
+		if (val <= 0 || val > 800)
+			return -EINVAL;
+		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+					 VCNL4035_ALS_IT_MASK,
+					 val / 100);
+		if (!ret)
+			data->als_it_val = val / 100;
+		return ret;
+	default:
+		return -EINVAL;
+	}
+}
+
+/* No direct ABI for persistence and threshold, so eventing */
+static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
+		const struct iio_chan_spec *chan, enum iio_event_type type,
+		enum iio_event_direction dir, enum iio_event_info info,
+		int *val, int *val2)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		switch (dir) {
+		case IIO_EV_DIR_RISING:
+			*val = data->als_thresh_high;
+			return IIO_VAL_INT;
+		case IIO_EV_DIR_FALLING:
+			*val = data->als_thresh_low;
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+		break;
+	case IIO_EV_INFO_PERIOD:
+		*val = data->als_persistence;
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+
+}
+
+static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
+		const struct iio_chan_spec *chan, enum iio_event_type type,
+		enum iio_event_direction dir, enum iio_event_info info, int val,
+		int val2)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		/* 16 bit threshold range 0 - 65535 */
+		if (val < 0 || val > 65535)
+			return -EINVAL;
+		if (dir == IIO_EV_DIR_RISING) {
+			if (val < data->als_thresh_low)
+				return -EINVAL;
+			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
+					   val);
+			if (!ret)
+				data->als_thresh_high = val;
+		} else {
+			if (val > data->als_thresh_high)
+				return -EINVAL;
+			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
+					   val);
+			if (!ret)
+				data->als_thresh_low = val;
+		}
+		return ret;
+	case IIO_EV_INFO_PERIOD:
+		/* allow only 1 2 4 8 as persistence value */
+		if (val < 0 || val > 8 || __sw_hweight8(val) != 1)
+			return -EINVAL;
+		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+					 VCNL4035_ALS_PERS_MASK, val);
+		if (!ret)
+			data->als_persistence = val;
+		return ret;
+	default:
+		return -EINVAL;
+	}
+}
+
+static IIO_CONST_ATTR_INT_TIME_AVAIL("50 100 200 400 800");
+
+static struct attribute *vcnl4035_attributes[] = {
+	&iio_const_attr_integration_time_available.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group vcnl4035_attribute_group = {
+	.attrs = vcnl4035_attributes,
+};
+
+static const struct iio_info vcnl4035_info = {
+	.read_raw		= vcnl4035_read_raw,
+	.write_raw		= vcnl4035_write_raw,
+	.read_event_value	= vcnl4035_read_thresh,
+	.write_event_value	= vcnl4035_write_thresh,
+	.attrs			= &vcnl4035_attribute_group,
+};
+
+enum vcnl4035_scan_index_order {
+	VCNL4035_CHAN_INDEX_LIGHT,
+};
+
+static const unsigned long vcnl4035_available_scan_masks[] = {
+	BIT(VCNL4035_CHAN_INDEX_LIGHT),
+	0
+};
+
+static const struct iio_event_spec vcnl4035_event_spec[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_RISING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
+	},
+};
+
+static const struct iio_chan_spec vcnl4035_channels[] = {
+	{
+		.type = IIO_INTENSITY,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				BIT(IIO_CHAN_INFO_INT_TIME) |
+				BIT(IIO_CHAN_INFO_SCALE),
+		.event_spec = vcnl4035_event_spec,
+		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
+		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+};
+
+static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
+{
+	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+					VCNL4035_MODE_ALS_MASK,
+					status);
+}
+
+static int vcnl4035_init(struct vcnl4035_data *data)
+{
+	int ret;
+	int id;
+
+	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
+		return ret;
+	}
+
+	if (id != VCNL4035_DEV_ID_VAL) {
+		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
+			id, VCNL4035_DEV_ID_VAL);
+		return -ENODEV;
+	}
+
+#ifndef CONFIG_PM
+	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
+	if (ret < 0)
+		return ret;
+#endif
+	/* set default integration time - 100 ms for ALS */
+	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+				 VCNL4035_ALS_IT_MASK,
+				 VCNL4035_ALS_IT_DEFAULT);
+	if (ret) {
+		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
+		return ret;
+	}
+	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
+
+	/* set default persistence time - 1 for ALS */
+	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
+				 VCNL4035_ALS_PERS_MASK,
+				 VCNL4035_ALS_PERS_DEFAULT);
+	if (ret) {
+		pr_err("regmap_update_bits default PERS returned %d\n", ret);
+		return ret;
+	}
+	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
+
+	/* set default HIGH threshold for ALS */
+	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
+				VCNL4035_ALS_THDH_DEFAULT);
+	if (ret) {
+		pr_err("regmap_write default THDH returned %d\n", ret);
+		return ret;
+	}
+	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
+
+	/* set default LOW threshold for ALS */
+	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
+				VCNL4035_ALS_THDL_DEFAULT);
+	if (ret) {
+		pr_err("regmap_write default THDL returned %d\n", ret);
+		return ret;
+	}
+	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
+
+	return 0;
+}
+
+static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case VCNL4035_ALS_CONF:
+	case VCNL4035_DEV_ID:
+		return false;
+	default:
+		return true;
+	}
+}
+
+static const struct regmap_config vcnl4035_regmap_config = {
+	.name		= VCNL4035_REGMAP_NAME,
+	.reg_bits	= 8,
+	.val_bits	= 16,
+	.max_register	= VCNL4035_DEV_ID,
+	.cache_type	= REGCACHE_RBTREE,
+	.volatile_reg	= vcnl4035_is_volatile_reg,
+	.val_format_endian = REGMAP_ENDIAN_LITTLE,
+};
+
+static int vcnl4035_probe(struct i2c_client *client,
+				const struct i2c_device_id *id)
+{
+	struct vcnl4035_data *data;
+	struct iio_dev *indio_dev;
+	struct regmap *regmap;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
+	if (IS_ERR(regmap)) {
+		dev_err(&client->dev, "regmap_init failed!\n");
+		return PTR_ERR(regmap);
+	}
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	data->regmap = regmap;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &vcnl4035_info;
+	indio_dev->name = VCNL4035_DRV_NAME;
+	indio_dev->channels = vcnl4035_channels;
+	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	ret = vcnl4035_init(data);
+	if (ret < 0) {
+		dev_err(&client->dev, "vcnl4035 chip init failed\n");
+		return ret;
+	}
+
+	ret = pm_runtime_set_active(&client->dev);
+	if (ret < 0)
+		goto fail_poweroff;
+
+	pm_runtime_enable(&client->dev);
+	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
+	pm_runtime_use_autosuspend(&client->dev);
+
+	if (client->irq) {
+		data->drdy_trigger0 = devm_iio_trigger_alloc(
+			indio_dev->dev.parent,
+			"%s-dev%d", indio_dev->name, indio_dev->id);
+		if (!data->drdy_trigger0) {
+			ret = -ENOMEM;
+			goto fail_pm_disable;
+		}
+		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
+		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
+		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
+		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
+		ret = iio_trigger_register(data->drdy_trigger0);
+		if (ret) {
+			dev_err(&client->dev, "iio trigger register failed\n");
+			goto fail_pm_disable;
+		}
+
+		/* Trigger setup */
+		ret = iio_triggered_buffer_setup(indio_dev, NULL,
+					vcnl4035_trigger_consumer_handler,
+					NULL);
+		if (ret < 0) {
+			dev_err(&client->dev, "iio triggered buffer setup failed\n");
+			goto fail_buffer_clean;
+		}
+
+		/* IRQ to trigger mapping */
+		ret = devm_request_threaded_irq(&client->dev, client->irq,
+			NULL, vcnl4035_drdy_irq_thread,
+			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
+			VCNL4035_IRQ_NAME, indio_dev);
+		if (ret < 0) {
+			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
+				client->irq);
+			goto fail_buffer_clean;
+			}
+	}
+
+	ret = iio_device_register(indio_dev);
+	if (ret)
+		goto fail_buffer_clean;
+	return 0;
+
+fail_buffer_clean:
+	iio_trigger_unregister(data->drdy_trigger0);
+	iio_triggered_buffer_cleanup(indio_dev);
+fail_pm_disable:
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+fail_poweroff:
+	vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
+	return ret;
+}
+
+static int vcnl4035_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+
+	iio_device_unregister(indio_dev);
+	iio_trigger_unregister(data->drdy_trigger0);
+	iio_triggered_buffer_cleanup(indio_dev);
+
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+
+	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
+					VCNL4035_MODE_ALS_DISABLE);
+}
+
+#ifdef CONFIG_PM
+static int vcnl4035_runtime_suspend(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
+	regcache_mark_dirty(data->regmap);
+
+	return ret;
+}
+
+static int vcnl4035_runtime_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+
+	regcache_sync(data->regmap);
+	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
+	if (ret < 0)
+		return ret;
+	/* wait for 1 ALS integration cycle */
+	msleep(data->als_it_val * 100);
+
+	return 0;
+}
+#endif
+
+static const struct dev_pm_ops vcnl4035_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+				pm_runtime_force_resume)
+	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
+			   vcnl4035_runtime_resume, NULL)
+};
+
+static const struct of_device_id vcnl4035_of_match[] = {
+	{ .compatible = "vishay,vcnl4035", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
+
+static const struct i2c_device_id vcnl4035_id[] = {
+	{ "vcnl4035", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
+
+static struct i2c_driver vcnl4035_driver = {
+	.driver = {
+		.name   = VCNL4035_DRV_NAME,
+		.pm	= &vcnl4035_pm_ops,
+		.of_match_table = of_match_ptr(vcnl4035_of_match),
+	},
+	.probe  = vcnl4035_probe,
+	.remove	= vcnl4035_remove,
+	.id_table = vcnl4035_id,
+};
+
+module_i2c_driver(vcnl4035_driver);
+
+MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
+MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
+MODULE_LICENSE("GPL v2");
-- 
2.14.4


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v3 2/2] iio: light: Add device tree binding for vishay vcnl4035
  2018-08-02  9:52   ` [PATCH v3 " Parthiban Nallathambi
@ 2018-08-02  9:52     ` Parthiban Nallathambi
  2018-08-02 12:51       ` Jonathan Cameron
  2018-08-02 12:49     ` [PATCH v3 1/2] iio: light: Add support " Jonathan Cameron
  1 sibling, 1 reply; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-08-02  9:52 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, robh+dt
  Cc: linux-iio, linux-kernel, mark.rutland, devicetree, matthias.bgg,
	wd, sbabic, hs, Parthiban Nallathambi

Adding device tree binding for vcnl4035 and vendor
prefix for Vishay Intertechnology

Signed-off-by: Parthiban Nallathambi <pn@denx.de>
-------

Changelog in v3:
- removed interrupt-parent property reference in documentation
- renamed vcnl4035 to light-sensor
---
 .../devicetree/bindings/iio/light/vcnl4035.txt         | 18 ++++++++++++++++++
 Documentation/devicetree/bindings/vendor-prefixes.txt  |  1 +
 2 files changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/vcnl4035.txt

diff --git a/Documentation/devicetree/bindings/iio/light/vcnl4035.txt b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
new file mode 100644
index 000000000000..871f995f7b35
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
@@ -0,0 +1,18 @@
+VISHAY VCNL4035 -  Ambient Light and proximity sensor
+
+Link to datasheet: https://www.vishay.com/docs/84251/vcnl4035x01.pdf
+
+Required properties:
+
+	-compatible: should be "vishay,vcnl4035"
+	-reg: I2C address of the sensor, should be 0x60
+	- interrupts: interrupt mapping for GPIO IRQ (level active low)
+
+Example:
+
+light-sensor@60 {
+	compatible = "vishay,vcnl4035";
+	reg = <0x60>;
+	interrupt-parent = <&gpio4>;
+	interrupts = <11 IRQ_TYPE_LEVEL_LOW>;
+};
diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
index 7cad066191ee..3cc46d5735a9 100644
--- a/Documentation/devicetree/bindings/vendor-prefixes.txt
+++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
@@ -395,6 +395,7 @@ v3	V3 Semiconductor
 variscite	Variscite Ltd.
 via	VIA Technologies, Inc.
 virtio	Virtual I/O Device Specification, developed by the OASIS consortium
+vishay	Vishay Intertechnology, Inc
 vivante	Vivante Corporation
 vocore VoCore Studio
 voipac	Voipac Technologies s.r.o.
-- 
2.14.4


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH v3 1/2] iio: light: Add support for vishay vcnl4035
  2018-08-02  9:52   ` [PATCH v3 " Parthiban Nallathambi
  2018-08-02  9:52     ` [PATCH v3 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
@ 2018-08-02 12:49     ` Jonathan Cameron
  2018-08-02 14:01       ` Parthiban Nallathambi
  1 sibling, 1 reply; 19+ messages in thread
From: Jonathan Cameron @ 2018-08-02 12:49 UTC (permalink / raw)
  To: Parthiban Nallathambi
  Cc: jic23, knaack.h, lars, pmeerw, robh+dt, linux-iio, linux-kernel,
	mark.rutland, devicetree, matthias.bgg, wd, sbabic, hs

On Thu, 2 Aug 2018 11:52:29 +0200
Parthiban Nallathambi <pn@denx.de> wrote:

> Add support for VCNL4035, which is capable of Ambient light
> sensing (ALS) and proximity function. This patch adds support
> only for ALS function
> 
> Signed-off-by: Parthiban Nallathambi <pn@denx.de>
Hi Parthiban,

Please avoid replying to a previous thread when sending a new version. 
It tends to lead to several things:

1. Really deep threads that are very hard to follow.
2. In some email clients, your email appears many pages back in the history.


Anyhow...

I'm confused how the runtime pm in here is supposed to work. There seems to none
of the usual calls to turn the device on when needed.

I was expected a pm_runtime_get_sync or similar in the read_raw path.

A few other bits and bobs inline.

Thanks,

Jonathan

> -------
> 
> Changelog since v1:
> 
> 1. Fixed 0-day warning on le16_to_cpu usage
> 2. Persistence value is directly mapped to datasheet's value to
> avoid confusions of usage from sysfs
> 
> Changelog in v3:
> - Usage of lock is not needed, removed mutex locking
> - ALS threshold and persistence configuration available
> as events and threshold events are notified to userspace
> - Usage of devm_ is re-ordered and exit handling updated
> - Complexity in timestamp calculation is removed and used
> iio_get_time_ns
> ---
>  drivers/iio/light/Kconfig    |  12 +
>  drivers/iio/light/Makefile   |   1 +
>  drivers/iio/light/vcnl4035.c | 619 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 632 insertions(+)
>  create mode 100644 drivers/iio/light/vcnl4035.c
> 
> diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
> index c7ef8d1862d6..b7069a4c28a2 100644
> --- a/drivers/iio/light/Kconfig
> +++ b/drivers/iio/light/Kconfig
> @@ -447,6 +447,18 @@ config VCNL4000
>  	 To compile this driver as a module, choose M here: the
>  	 module will be called vcnl4000.
>  
> +config VCNL4035
> +	tristate "VCNL4035 combined ALS and proximity sensor"
> +	select REGMAP_I2C
> +	depends on I2C
> +	help
> +	 Say Y here if you want to build a driver for the Vishay VCNL4035,
> +	 combined ambient light (ALS) and proximity sensor. Currently only ALS
> +	 function is available.
> +
> +	 To compile this driver as a module, choose M here: the
> +	 module will be called vcnl4035.
> +
>  config VEML6070
>  	tristate "VEML6070 UV A light sensor"
>  	depends on I2C
> diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
> index 80943af5d627..dce98511a59b 100644
> --- a/drivers/iio/light/Makefile
> +++ b/drivers/iio/light/Makefile
> @@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
>  obj-$(CONFIG_TSL4531)		+= tsl4531.o
>  obj-$(CONFIG_US5182D)		+= us5182d.o
>  obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
> +obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
>  obj-$(CONFIG_VEML6070)		+= veml6070.o
>  obj-$(CONFIG_VL6180)		+= vl6180.o
>  obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
> diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
> new file mode 100644
> index 000000000000..d2b5ae32baa8
> --- /dev/null
> +++ b/drivers/iio/light/vcnl4035.c
> @@ -0,0 +1,619 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
> + *
> + * Copyright (c) 2018, DENX Software Engineering GmbH
> + * Author: Parthiban Nallathambi <pn@denx.de>
> + *
> + *
> + * TODO: Proximity
> + */
> +#include <linux/module.h>
> +#include <linux/i2c.h>
> +#include <linux/bitops.h>
> +#include <linux/regmap.h>
> +#include <linux/pm_runtime.h>
> +
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
> +#include <linux/iio/events.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/iio/triggered_buffer.h>
> +
> +#define VCNL4035_DRV_NAME	"vcnl4035"
> +#define VCNL4035_IRQ_NAME	"vcnl4035_event"
> +#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
> +
> +/* Device registers */
> +#define VCNL4035_ALS_CONF	0x00
> +#define VCNL4035_ALS_THDH	0x01
> +#define VCNL4035_ALS_THDL	0x02
> +#define VCNL4035_ALS_DATA	0x0B
> +#define VCNL4035_INT_FLAG	0x0D
> +#define VCNL4035_DEV_ID		0x0E
> +
> +/* Register masks */
> +#define VCNL4035_MODE_ALS_MASK		BIT(0)
> +#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
> +#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
> +#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
> +#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
> +#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
> +
> +/* Default values */
> +#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
> +#define VCNL4035_MODE_ALS_DISABLE	0x00
> +#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
> +#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
> +#define VCNL4035_DEV_ID_VAL		0x80
> +#define VCNL4035_ALS_IT_DEFAULT		0x01
> +#define VCNL4035_ALS_PERS_DEFAULT	0x00
> +#define VCNL4035_ALS_THDH_DEFAULT	5000
> +#define VCNL4035_ALS_THDL_DEFAULT	100
> +#define VCNL4035_SLEEP_DELAY_MS		2000
> +
> +struct vcnl4035_data {
> +	struct i2c_client *client;
> +	struct regmap *regmap;
> +	unsigned int als_it_val;
> +	unsigned int als_persistence:4;
> +	unsigned int als_thresh_low;
> +	unsigned int als_thresh_high;
> +	struct iio_trigger *drdy_trigger0;
> +};
> +
> +static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
> +{
> +	int ret;
> +	int reg;
> +
> +	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
> +	if (ret < 0)
> +		return false;
> +	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
> +		return true;
> +	else
> +		return false;
> +}
> +
> +static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
> +{
> +	struct iio_dev *indio_dev = private;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	if (vcnl4035_is_triggered(data)) {
> +		iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
> +							0,
> +							IIO_EV_TYPE_THRESH,
> +							IIO_EV_DIR_EITHER),
> +				iio_get_time_ns(indio_dev));
> +		iio_trigger_poll_chained(data->drdy_trigger0);

Hmm. So a single interrupt is used both as a trigger and as a threshold event.
A little unusual but fair enough.

> +		return IRQ_HANDLED;
> +	}
> +
> +	return IRQ_NONE;
> +}
> +
> +/* Triggered buffer */
> +static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
> +{
> +	struct iio_poll_func *pf = p;
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	u16 buffer[ALIGN(sizeof(u16), sizeof(s64)) + sizeof(s64)];
> +	int ret;
> +
> +	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, (int *)buffer);
> +	if (!ret)
> +		iio_push_to_buffers_with_timestamp(indio_dev, buffer,
> +						iio_get_time_ns(indio_dev));
> +	else
> +		dev_err(&data->client->dev,
> +			"Trigger consumer can't read from sensor.\n");
> +	iio_trigger_notify_done(indio_dev->trig);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
> +					bool enable_drdy)
> +{
> +	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
> +					VCNL4035_MODE_ALS_INT_DISABLE;
> +
> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +				 VCNL4035_MODE_ALS_INT_MASK,
> +				 val);
> +}
> +
> +static const struct iio_trigger_ops vcnl4035_trigger_ops = {
> +	.validate_device = iio_trigger_validate_own_device,
> +	.set_trigger_state = vcnl4035_als_drdy_set_state,
> +};
> +
> +/*
> + *	Device IT	INT Time (ms)	Scale (lux/step)
> + *	000		50		0.064
> + *	001		100		0.032
> + *	010		200		0.016
> + *	100		400		0.008
> + *	101 - 111	800		0.004
> + * Values are proportional, so ALS INT is selected for input due to
> + * simplicity reason. Integration time value and scaling is
> + * calculated based on device INT value
> + *
> + * Raw value needs to be scaled using ALS steps
> + *
> + */
> +static int vcnl4035_read_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan, int *val,
> +			    int *val2, long mask)
> +{
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +	int raw_data;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		ret = iio_device_claim_direct_mode(indio_dev);
> +		if (ret)
> +			return -ret;
> +
> +		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
> +		iio_device_release_direct_mode(indio_dev);
> +		if (ret < 0)
> +			return ret;
> +
> +		*val = raw_data;
> +		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_INT_TIME:
> +		*val = 50;
> +		if (!data->als_it_val)
> +			*val = data->als_it_val * 100;
> +		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_SCALE:
> +		*val = 64;
> +		if (!data->als_it_val)
> +			*val2 = 1000;
> +		else
> +			*val2 = data->als_it_val * 2 * 1000;
> +		return IIO_VAL_FRACTIONAL;
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int vcnl4035_write_raw(struct iio_dev *indio_dev,
> +				struct iio_chan_spec const *chan,
> +				int val, int val2, long mask)
> +{
> +	int ret;
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_INT_TIME:
> +		if (val <= 0 || val > 800)
> +			return -EINVAL;
> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +					 VCNL4035_ALS_IT_MASK,
> +					 val / 100);
> +		if (!ret)
> +			data->als_it_val = val / 100;
> +		return ret;
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +/* No direct ABI for persistence and threshold, so eventing */
> +static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
> +		const struct iio_chan_spec *chan, enum iio_event_type type,
> +		enum iio_event_direction dir, enum iio_event_info info,
> +		int *val, int *val2)
> +{
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	switch (info) {
> +	case IIO_EV_INFO_VALUE:
> +		switch (dir) {
> +		case IIO_EV_DIR_RISING:
> +			*val = data->als_thresh_high;
> +			return IIO_VAL_INT;
> +		case IIO_EV_DIR_FALLING:
> +			*val = data->als_thresh_low;
> +			return IIO_VAL_INT;
> +		default:
> +			return -EINVAL;
> +		}
> +		break;
> +	case IIO_EV_INFO_PERIOD:
> +		*val = data->als_persistence;
> +		return IIO_VAL_INT;
> +	default:
> +		return -EINVAL;
> +	}
> +
> +}
> +
> +static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
> +		const struct iio_chan_spec *chan, enum iio_event_type type,
> +		enum iio_event_direction dir, enum iio_event_info info, int val,
> +		int val2)
> +{
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	switch (info) {
> +	case IIO_EV_INFO_VALUE:
> +		/* 16 bit threshold range 0 - 65535 */
> +		if (val < 0 || val > 65535)
> +			return -EINVAL;
> +		if (dir == IIO_EV_DIR_RISING) {
> +			if (val < data->als_thresh_low)
> +				return -EINVAL;
> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
> +					   val);
> +			if (!ret)
> +				data->als_thresh_high = val;
> +		} else {
> +			if (val > data->als_thresh_high)
> +				return -EINVAL;
> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
> +					   val);
> +			if (!ret)
> +				data->als_thresh_low = val;
> +		}
> +		return ret;
> +	case IIO_EV_INFO_PERIOD:
> +		/* allow only 1 2 4 8 as persistence value */
> +		if (val < 0 || val > 8 || __sw_hweight8(val) != 1)
> +			return -EINVAL;
> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +					 VCNL4035_ALS_PERS_MASK, val);
> +		if (!ret)
> +			data->als_persistence = val;
> +		return ret;
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static IIO_CONST_ATTR_INT_TIME_AVAIL("50 100 200 400 800");
> +
> +static struct attribute *vcnl4035_attributes[] = {
> +	&iio_const_attr_integration_time_available.dev_attr.attr,
> +	NULL,
> +};
> +
> +static const struct attribute_group vcnl4035_attribute_group = {
> +	.attrs = vcnl4035_attributes,
> +};
> +
> +static const struct iio_info vcnl4035_info = {
> +	.read_raw		= vcnl4035_read_raw,
> +	.write_raw		= vcnl4035_write_raw,
> +	.read_event_value	= vcnl4035_read_thresh,
> +	.write_event_value	= vcnl4035_write_thresh,
> +	.attrs			= &vcnl4035_attribute_group,
> +};
> +
> +enum vcnl4035_scan_index_order {
> +	VCNL4035_CHAN_INDEX_LIGHT,
> +};
> +
> +static const unsigned long vcnl4035_available_scan_masks[] = {
> +	BIT(VCNL4035_CHAN_INDEX_LIGHT),

You only need to provide this if there restrictions on which channels can
be run together.  Here there do not seem to be (as there is only one channel)
so you don't need it.

> +	0
> +};
> +
> +static const struct iio_event_spec vcnl4035_event_spec[] = {
> +	{
> +		.type = IIO_EV_TYPE_THRESH,
> +		.dir = IIO_EV_DIR_RISING,
> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
> +	}, {
> +		.type = IIO_EV_TYPE_THRESH,
> +		.dir = IIO_EV_DIR_FALLING,
> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
> +	}, {
> +		.type = IIO_EV_TYPE_THRESH,
> +		.dir = IIO_EV_DIR_EITHER,
> +		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
> +	},
> +};
> +
> +static const struct iio_chan_spec vcnl4035_channels[] = {
> +	{
> +		.type = IIO_INTENSITY,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				BIT(IIO_CHAN_INFO_INT_TIME) |
> +				BIT(IIO_CHAN_INFO_SCALE),
> +		.event_spec = vcnl4035_event_spec,
> +		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
> +		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
> +		.scan_type = {
> +			.sign = 'u',
> +			.realbits = 16,
> +			.storagebits = 16,
> +			.endianness = IIO_LE,
> +		},
> +	},
> +};
> +
> +static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
> +{
> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +					VCNL4035_MODE_ALS_MASK,
> +					status);
> +}
> +
> +static int vcnl4035_init(struct vcnl4035_data *data)
> +{
> +	int ret;
> +	int id;
> +
> +	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
> +	if (ret < 0) {
> +		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
> +		return ret;
> +	}
> +
> +	if (id != VCNL4035_DEV_ID_VAL) {
> +		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
> +			id, VCNL4035_DEV_ID_VAL);
> +		return -ENODEV;
> +	}
> +
> +#ifndef CONFIG_PM
> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
> +	if (ret < 0)
> +		return ret;
> +#endif
> +	/* set default integration time - 100 ms for ALS */
> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +				 VCNL4035_ALS_IT_MASK,
> +				 VCNL4035_ALS_IT_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
> +
> +	/* set default persistence time - 1 for ALS */
> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> +				 VCNL4035_ALS_PERS_MASK,
> +				 VCNL4035_ALS_PERS_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_update_bits default PERS returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
> +
> +	/* set default HIGH threshold for ALS */
> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
> +				VCNL4035_ALS_THDH_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_write default THDH returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
> +
> +	/* set default LOW threshold for ALS */
> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
> +				VCNL4035_ALS_THDL_DEFAULT);
> +	if (ret) {
> +		pr_err("regmap_write default THDL returned %d\n", ret);
> +		return ret;
> +	}
> +	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
> +
> +	return 0;
> +}
> +
> +static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
> +{
> +	switch (reg) {
> +	case VCNL4035_ALS_CONF:
> +	case VCNL4035_DEV_ID:
> +		return false;
> +	default:
> +		return true;
> +	}
> +}
> +
> +static const struct regmap_config vcnl4035_regmap_config = {
> +	.name		= VCNL4035_REGMAP_NAME,
> +	.reg_bits	= 8,
> +	.val_bits	= 16,
> +	.max_register	= VCNL4035_DEV_ID,
> +	.cache_type	= REGCACHE_RBTREE,
> +	.volatile_reg	= vcnl4035_is_volatile_reg,
> +	.val_format_endian = REGMAP_ENDIAN_LITTLE,
> +};
> +
> +static int vcnl4035_probe(struct i2c_client *client,
> +				const struct i2c_device_id *id)
> +{
> +	struct vcnl4035_data *data;
> +	struct iio_dev *indio_dev;
> +	struct regmap *regmap;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
> +	if (IS_ERR(regmap)) {
> +		dev_err(&client->dev, "regmap_init failed!\n");
> +		return PTR_ERR(regmap);
> +	}
> +
> +	data = iio_priv(indio_dev);
> +	i2c_set_clientdata(client, indio_dev);
> +	data->client = client;
> +	data->regmap = regmap;
> +
> +	indio_dev->dev.parent = &client->dev;
> +	indio_dev->info = &vcnl4035_info;
> +	indio_dev->name = VCNL4035_DRV_NAME;
> +	indio_dev->channels = vcnl4035_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +	ret = vcnl4035_init(data);
> +	if (ret < 0) {
> +		dev_err(&client->dev, "vcnl4035 chip init failed\n");
> +		return ret;
> +	}
> +
> +	ret = pm_runtime_set_active(&client->dev);
> +	if (ret < 0)
> +		goto fail_poweroff;
> +
> +	pm_runtime_enable(&client->dev);
> +	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
> +	pm_runtime_use_autosuspend(&client->dev);
> +
> +	if (client->irq) {
> +		data->drdy_trigger0 = devm_iio_trigger_alloc(
> +			indio_dev->dev.parent,
> +			"%s-dev%d", indio_dev->name, indio_dev->id);
> +		if (!data->drdy_trigger0) {
> +			ret = -ENOMEM;
> +			goto fail_pm_disable;
> +		}
> +		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
> +		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
> +		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
> +		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
> +		ret = iio_trigger_register(data->drdy_trigger0);
> +		if (ret) {
> +			dev_err(&client->dev, "iio trigger register failed\n");
> +			goto fail_pm_disable;
> +		}
> +
> +		/* Trigger setup */
> +		ret = iio_triggered_buffer_setup(indio_dev, NULL,
> +					vcnl4035_trigger_consumer_handler,
> +					NULL);
> +		if (ret < 0) {
> +			dev_err(&client->dev, "iio triggered buffer setup failed\n");
> +			goto fail_buffer_clean;
This flow doesn't look right.  iio_triggered_buffer_setup should be cleaning up
after itself if an error occurs within it.

As such you shouldn't be calling iio_triggered_buffer_cleanup.

> +		}
> +
> +		/* IRQ to trigger mapping */
> +		ret = devm_request_threaded_irq(&client->dev, client->irq,
> +			NULL, vcnl4035_drdy_irq_thread,
> +			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
> +			VCNL4035_IRQ_NAME, indio_dev);
> +		if (ret < 0) {
> +			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
> +				client->irq);
> +			goto fail_buffer_clean;
> +			}
> +	}
> +
> +	ret = iio_device_register(indio_dev);
> +	if (ret)
> +		goto fail_buffer_clean;
> +	return 0;
> +
> +fail_buffer_clean:
> +	iio_trigger_unregister(data->drdy_trigger0);
This ordering should be the reverse of the above, so the trigger unregister
should be after the buffer_cleanup call.

> +	iio_triggered_buffer_cleanup(indio_dev);
> +fail_pm_disable:
> +	pm_runtime_disable(&client->dev);
> +	pm_runtime_set_suspended(&client->dev);
> +	pm_runtime_put_noidle(&client->dev);
> +fail_poweroff:
> +	vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
> +	return ret;
> +}
> +
> +static int vcnl4035_remove(struct i2c_client *client)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +
> +	iio_device_unregister(indio_dev);
> +	iio_trigger_unregister(data->drdy_trigger0);

As with the error handling above, this is not in reverse order
of how it is setup in probe which fails the 'obviously correct' test.

> +	iio_triggered_buffer_cleanup(indio_dev);
> +
> +	pm_runtime_disable(&client->dev);
> +	pm_runtime_set_suspended(&client->dev);
> +	pm_runtime_put_noidle(&client->dev);
> +
> +	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
> +					VCNL4035_MODE_ALS_DISABLE);
> +}
> +
> +#ifdef CONFIG_PM
> +static int vcnl4035_runtime_suspend(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
> +	regcache_mark_dirty(data->regmap);
> +
> +	return ret;
> +}
> +
> +static int vcnl4035_runtime_resume(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	regcache_sync(data->regmap);
> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
> +	if (ret < 0)
> +		return ret;
> +	/* wait for 1 ALS integration cycle */
> +	msleep(data->als_it_val * 100);
> +
> +	return 0;
> +}
> +#endif
> +
> +static const struct dev_pm_ops vcnl4035_pm_ops = {
> +	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
> +				pm_runtime_force_resume)
> +	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
> +			   vcnl4035_runtime_resume, NULL)
> +};
> +
> +static const struct of_device_id vcnl4035_of_match[] = {
> +	{ .compatible = "vishay,vcnl4035", },
> +	{ },
> +};
> +MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
> +
> +static const struct i2c_device_id vcnl4035_id[] = {
> +	{ "vcnl4035", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
> +
> +static struct i2c_driver vcnl4035_driver = {
> +	.driver = {
> +		.name   = VCNL4035_DRV_NAME,
> +		.pm	= &vcnl4035_pm_ops,
> +		.of_match_table = of_match_ptr(vcnl4035_of_match),
> +	},
> +	.probe  = vcnl4035_probe,
> +	.remove	= vcnl4035_remove,
> +	.id_table = vcnl4035_id,
> +};
> +
> +module_i2c_driver(vcnl4035_driver);
> +
> +MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
> +MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
> +MODULE_LICENSE("GPL v2");



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v3 2/2] iio: light: Add device tree binding for vishay vcnl4035
  2018-08-02  9:52     ` [PATCH v3 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
@ 2018-08-02 12:51       ` Jonathan Cameron
  0 siblings, 0 replies; 19+ messages in thread
From: Jonathan Cameron @ 2018-08-02 12:51 UTC (permalink / raw)
  To: Parthiban Nallathambi
  Cc: jic23, knaack.h, lars, pmeerw, robh+dt, linux-iio, linux-kernel,
	mark.rutland, devicetree, matthias.bgg, wd, sbabic, hs

On Thu, 2 Aug 2018 11:52:30 +0200
Parthiban Nallathambi <pn@denx.de> wrote:

> Adding device tree binding for vcnl4035 and vendor
> prefix for Vishay Intertechnology
> 
> Signed-off-by: Parthiban Nallathambi <pn@denx.de>
> -------
Generally don't add your own cut line, just use the one that is already there below.

One minor comment inline.
> 
> Changelog in v3:
> - removed interrupt-parent property reference in documentation
> - renamed vcnl4035 to light-sensor
> ---
>  .../devicetree/bindings/iio/light/vcnl4035.txt         | 18 ++++++++++++++++++
>  Documentation/devicetree/bindings/vendor-prefixes.txt  |  1 +
>  2 files changed, 19 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/light/vcnl4035.txt
> 
> diff --git a/Documentation/devicetree/bindings/iio/light/vcnl4035.txt b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
> new file mode 100644
> index 000000000000..871f995f7b35
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/light/vcnl4035.txt
> @@ -0,0 +1,18 @@
> +VISHAY VCNL4035 -  Ambient Light and proximity sensor
> +
> +Link to datasheet: https://www.vishay.com/docs/84251/vcnl4035x01.pdf
> +
> +Required properties:
> +
> +	-compatible: should be "vishay,vcnl4035"
> +	-reg: I2C address of the sensor, should be 0x60

Bit random on the spacing after -.

> +	- interrupts: interrupt mapping for GPIO IRQ (level active low)
> +
> +Example:
> +
> +light-sensor@60 {
> +	compatible = "vishay,vcnl4035";
> +	reg = <0x60>;
> +	interrupt-parent = <&gpio4>;
> +	interrupts = <11 IRQ_TYPE_LEVEL_LOW>;
> +};
> diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
> index 7cad066191ee..3cc46d5735a9 100644
> --- a/Documentation/devicetree/bindings/vendor-prefixes.txt
> +++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
> @@ -395,6 +395,7 @@ v3	V3 Semiconductor
>  variscite	Variscite Ltd.
>  via	VIA Technologies, Inc.
>  virtio	Virtual I/O Device Specification, developed by the OASIS consortium
> +vishay	Vishay Intertechnology, Inc
>  vivante	Vivante Corporation
>  vocore VoCore Studio
>  voipac	Voipac Technologies s.r.o.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v3 1/2] iio: light: Add support for vishay vcnl4035
  2018-08-02 12:49     ` [PATCH v3 1/2] iio: light: Add support " Jonathan Cameron
@ 2018-08-02 14:01       ` Parthiban Nallathambi
  2018-08-03 22:16         ` Jonathan Cameron
  0 siblings, 1 reply; 19+ messages in thread
From: Parthiban Nallathambi @ 2018-08-02 14:01 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: pn, jic23, knaack.h, lars, pmeerw, robh+dt, linux-iio,
	linux-kernel, mark.rutland, devicetree, matthias.bgg, wd, sbabic,
	hs

Hi Jonathan,

General query, for threshold events and persistence value (period) we
don't have any direct MACROS similar to IIO_CONST_ATTR_INT_TIME_AVAIL
as ABI to show the available/range of values.

For most light and proximity sensors, I could see this value of
threshold configuration to limit the amount of interrupts. But there
isn't direct way to show the available range for threshold or periods
available in sysfs.

Can we consider presenting it as separate device attribute? Like,

For threshold:
in_intensity_thresh_range_available
in_proximity_thresh_range_available

For period:
in_device_persistence/period_available


On 08/02/2018 02:49 PM, Jonathan Cameron wrote:
> On Thu, 2 Aug 2018 11:52:29 +0200
> Parthiban Nallathambi <pn@denx.de> wrote:
> 
>> Add support for VCNL4035, which is capable of Ambient light
>> sensing (ALS) and proximity function. This patch adds support
>> only for ALS function
>>
>> Signed-off-by: Parthiban Nallathambi <pn@denx.de>
> Hi Parthiban,
> 
> Please avoid replying to a previous thread when sending a new version.
> It tends to lead to several things:
> 
> 1. Really deep threads that are very hard to follow.
> 2. In some email clients, your email appears many pages back in the history.
> 

Sure, will send as fresh v4 next.

> 
> Anyhow...
> 
> I'm confused how the runtime pm in here is supposed to work. There seems to none
> of the usual calls to turn the device on when needed.
> 
> I was expected a pm_runtime_get_sync or similar in the read_raw path.

Yes, I missed it. Thanks, will add in v4.

> 
> A few other bits and bobs inline.
> 
> Thanks,
> 
> Jonathan
> 
>> -------
>>
>> Changelog since v1:
>>
>> 1. Fixed 0-day warning on le16_to_cpu usage
>> 2. Persistence value is directly mapped to datasheet's value to
>> avoid confusions of usage from sysfs
>>
>> Changelog in v3:
>> - Usage of lock is not needed, removed mutex locking
>> - ALS threshold and persistence configuration available
>> as events and threshold events are notified to userspace
>> - Usage of devm_ is re-ordered and exit handling updated
>> - Complexity in timestamp calculation is removed and used
>> iio_get_time_ns
>> ---
>>   drivers/iio/light/Kconfig    |  12 +
>>   drivers/iio/light/Makefile   |   1 +
>>   drivers/iio/light/vcnl4035.c | 619 +++++++++++++++++++++++++++++++++++++++++++
>>   3 files changed, 632 insertions(+)
>>   create mode 100644 drivers/iio/light/vcnl4035.c
>>
>> diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
>> index c7ef8d1862d6..b7069a4c28a2 100644
>> --- a/drivers/iio/light/Kconfig
>> +++ b/drivers/iio/light/Kconfig
>> @@ -447,6 +447,18 @@ config VCNL4000
>>   	 To compile this driver as a module, choose M here: the
>>   	 module will be called vcnl4000.
>>   
>> +config VCNL4035
>> +	tristate "VCNL4035 combined ALS and proximity sensor"
>> +	select REGMAP_I2C
>> +	depends on I2C
>> +	help
>> +	 Say Y here if you want to build a driver for the Vishay VCNL4035,
>> +	 combined ambient light (ALS) and proximity sensor. Currently only ALS
>> +	 function is available.
>> +
>> +	 To compile this driver as a module, choose M here: the
>> +	 module will be called vcnl4035.
>> +
>>   config VEML6070
>>   	tristate "VEML6070 UV A light sensor"
>>   	depends on I2C
>> diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
>> index 80943af5d627..dce98511a59b 100644
>> --- a/drivers/iio/light/Makefile
>> +++ b/drivers/iio/light/Makefile
>> @@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
>>   obj-$(CONFIG_TSL4531)		+= tsl4531.o
>>   obj-$(CONFIG_US5182D)		+= us5182d.o
>>   obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
>> +obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
>>   obj-$(CONFIG_VEML6070)		+= veml6070.o
>>   obj-$(CONFIG_VL6180)		+= vl6180.o
>>   obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
>> diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
>> new file mode 100644
>> index 000000000000..d2b5ae32baa8
>> --- /dev/null
>> +++ b/drivers/iio/light/vcnl4035.c
>> @@ -0,0 +1,619 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
>> + *
>> + * Copyright (c) 2018, DENX Software Engineering GmbH
>> + * Author: Parthiban Nallathambi <pn@denx.de>
>> + *
>> + *
>> + * TODO: Proximity
>> + */
>> +#include <linux/module.h>
>> +#include <linux/i2c.h>
>> +#include <linux/bitops.h>
>> +#include <linux/regmap.h>
>> +#include <linux/pm_runtime.h>
>> +
>> +#include <linux/iio/iio.h>
>> +#include <linux/iio/sysfs.h>
>> +#include <linux/iio/events.h>
>> +#include <linux/iio/buffer.h>
>> +#include <linux/iio/trigger.h>
>> +#include <linux/iio/trigger_consumer.h>
>> +#include <linux/iio/triggered_buffer.h>
>> +
>> +#define VCNL4035_DRV_NAME	"vcnl4035"
>> +#define VCNL4035_IRQ_NAME	"vcnl4035_event"
>> +#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
>> +
>> +/* Device registers */
>> +#define VCNL4035_ALS_CONF	0x00
>> +#define VCNL4035_ALS_THDH	0x01
>> +#define VCNL4035_ALS_THDL	0x02
>> +#define VCNL4035_ALS_DATA	0x0B
>> +#define VCNL4035_INT_FLAG	0x0D
>> +#define VCNL4035_DEV_ID		0x0E
>> +
>> +/* Register masks */
>> +#define VCNL4035_MODE_ALS_MASK		BIT(0)
>> +#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
>> +#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
>> +#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
>> +#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
>> +#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
>> +
>> +/* Default values */
>> +#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
>> +#define VCNL4035_MODE_ALS_DISABLE	0x00
>> +#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
>> +#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
>> +#define VCNL4035_DEV_ID_VAL		0x80
>> +#define VCNL4035_ALS_IT_DEFAULT		0x01
>> +#define VCNL4035_ALS_PERS_DEFAULT	0x00
>> +#define VCNL4035_ALS_THDH_DEFAULT	5000
>> +#define VCNL4035_ALS_THDL_DEFAULT	100
>> +#define VCNL4035_SLEEP_DELAY_MS		2000
>> +
>> +struct vcnl4035_data {
>> +	struct i2c_client *client;
>> +	struct regmap *regmap;
>> +	unsigned int als_it_val;
>> +	unsigned int als_persistence:4;
>> +	unsigned int als_thresh_low;
>> +	unsigned int als_thresh_high;
>> +	struct iio_trigger *drdy_trigger0;
>> +};
>> +
>> +static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
>> +{
>> +	int ret;
>> +	int reg;
>> +
>> +	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
>> +	if (ret < 0)
>> +		return false;
>> +	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
>> +		return true;
>> +	else
>> +		return false;
>> +}
>> +
>> +static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
>> +{
>> +	struct iio_dev *indio_dev = private;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	if (vcnl4035_is_triggered(data)) {
>> +		iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
>> +							0,
>> +							IIO_EV_TYPE_THRESH,
>> +							IIO_EV_DIR_EITHER),
>> +				iio_get_time_ns(indio_dev));
>> +		iio_trigger_poll_chained(data->drdy_trigger0);
> 
> Hmm. So a single interrupt is used both as a trigger and as a threshold event.
> A little unusual but fair enough.
> 
>> +		return IRQ_HANDLED;
>> +	}
>> +
>> +	return IRQ_NONE;
>> +}
>> +
>> +/* Triggered buffer */
>> +static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
>> +{
>> +	struct iio_poll_func *pf = p;
>> +	struct iio_dev *indio_dev = pf->indio_dev;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	u16 buffer[ALIGN(sizeof(u16), sizeof(s64)) + sizeof(s64)];
>> +	int ret;
>> +
>> +	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, (int *)buffer);
>> +	if (!ret)
>> +		iio_push_to_buffers_with_timestamp(indio_dev, buffer,
>> +						iio_get_time_ns(indio_dev));
>> +	else
>> +		dev_err(&data->client->dev,
>> +			"Trigger consumer can't read from sensor.\n");
>> +	iio_trigger_notify_done(indio_dev->trig);
>> +
>> +	return IRQ_HANDLED;
>> +}
>> +
>> +static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
>> +					bool enable_drdy)
>> +{
>> +	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
>> +					VCNL4035_MODE_ALS_INT_DISABLE;
>> +
>> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +				 VCNL4035_MODE_ALS_INT_MASK,
>> +				 val);
>> +}
>> +
>> +static const struct iio_trigger_ops vcnl4035_trigger_ops = {
>> +	.validate_device = iio_trigger_validate_own_device,
>> +	.set_trigger_state = vcnl4035_als_drdy_set_state,
>> +};
>> +
>> +/*
>> + *	Device IT	INT Time (ms)	Scale (lux/step)
>> + *	000		50		0.064
>> + *	001		100		0.032
>> + *	010		200		0.016
>> + *	100		400		0.008
>> + *	101 - 111	800		0.004
>> + * Values are proportional, so ALS INT is selected for input due to
>> + * simplicity reason. Integration time value and scaling is
>> + * calculated based on device INT value
>> + *
>> + * Raw value needs to be scaled using ALS steps
>> + *
>> + */
>> +static int vcnl4035_read_raw(struct iio_dev *indio_dev,
>> +			    struct iio_chan_spec const *chan, int *val,
>> +			    int *val2, long mask)
>> +{
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +	int raw_data;
>> +
>> +	switch (mask) {
>> +	case IIO_CHAN_INFO_RAW:
>> +		ret = iio_device_claim_direct_mode(indio_dev);
>> +		if (ret)
>> +			return -ret;
>> +
>> +		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
>> +		iio_device_release_direct_mode(indio_dev);
>> +		if (ret < 0)
>> +			return ret;
>> +
>> +		*val = raw_data;
>> +		return IIO_VAL_INT;
>> +	case IIO_CHAN_INFO_INT_TIME:
>> +		*val = 50;
>> +		if (!data->als_it_val)
>> +			*val = data->als_it_val * 100;
>> +		return IIO_VAL_INT;
>> +	case IIO_CHAN_INFO_SCALE:
>> +		*val = 64;
>> +		if (!data->als_it_val)
>> +			*val2 = 1000;
>> +		else
>> +			*val2 = data->als_it_val * 2 * 1000;
>> +		return IIO_VAL_FRACTIONAL;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +}
>> +
>> +static int vcnl4035_write_raw(struct iio_dev *indio_dev,
>> +				struct iio_chan_spec const *chan,
>> +				int val, int val2, long mask)
>> +{
>> +	int ret;
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	switch (mask) {
>> +	case IIO_CHAN_INFO_INT_TIME:
>> +		if (val <= 0 || val > 800)
>> +			return -EINVAL;
>> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +					 VCNL4035_ALS_IT_MASK,
>> +					 val / 100);
>> +		if (!ret)
>> +			data->als_it_val = val / 100;
>> +		return ret;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +}
>> +
>> +/* No direct ABI for persistence and threshold, so eventing */
>> +static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
>> +		const struct iio_chan_spec *chan, enum iio_event_type type,
>> +		enum iio_event_direction dir, enum iio_event_info info,
>> +		int *val, int *val2)
>> +{
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	switch (info) {
>> +	case IIO_EV_INFO_VALUE:
>> +		switch (dir) {
>> +		case IIO_EV_DIR_RISING:
>> +			*val = data->als_thresh_high;
>> +			return IIO_VAL_INT;
>> +		case IIO_EV_DIR_FALLING:
>> +			*val = data->als_thresh_low;
>> +			return IIO_VAL_INT;
>> +		default:
>> +			return -EINVAL;
>> +		}
>> +		break;
>> +	case IIO_EV_INFO_PERIOD:
>> +		*val = data->als_persistence;
>> +		return IIO_VAL_INT;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +
>> +}
>> +
>> +static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
>> +		const struct iio_chan_spec *chan, enum iio_event_type type,
>> +		enum iio_event_direction dir, enum iio_event_info info, int val,
>> +		int val2)
>> +{
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +
>> +	switch (info) {
>> +	case IIO_EV_INFO_VALUE:
>> +		/* 16 bit threshold range 0 - 65535 */
>> +		if (val < 0 || val > 65535)
>> +			return -EINVAL;
>> +		if (dir == IIO_EV_DIR_RISING) {
>> +			if (val < data->als_thresh_low)
>> +				return -EINVAL;
>> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
>> +					   val);
>> +			if (!ret)
>> +				data->als_thresh_high = val;
>> +		} else {
>> +			if (val > data->als_thresh_high)
>> +				return -EINVAL;
>> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
>> +					   val);
>> +			if (!ret)
>> +				data->als_thresh_low = val;
>> +		}
>> +		return ret;
>> +	case IIO_EV_INFO_PERIOD:
>> +		/* allow only 1 2 4 8 as persistence value */
>> +		if (val < 0 || val > 8 || __sw_hweight8(val) != 1)
>> +			return -EINVAL;
>> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +					 VCNL4035_ALS_PERS_MASK, val);
>> +		if (!ret)
>> +			data->als_persistence = val;
>> +		return ret;
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +}
>> +
>> +static IIO_CONST_ATTR_INT_TIME_AVAIL("50 100 200 400 800");
>> +
>> +static struct attribute *vcnl4035_attributes[] = {
>> +	&iio_const_attr_integration_time_available.dev_attr.attr,
>> +	NULL,
>> +};
>> +
>> +static const struct attribute_group vcnl4035_attribute_group = {
>> +	.attrs = vcnl4035_attributes,
>> +};
>> +
>> +static const struct iio_info vcnl4035_info = {
>> +	.read_raw		= vcnl4035_read_raw,
>> +	.write_raw		= vcnl4035_write_raw,
>> +	.read_event_value	= vcnl4035_read_thresh,
>> +	.write_event_value	= vcnl4035_write_thresh,
>> +	.attrs			= &vcnl4035_attribute_group,
>> +};
>> +
>> +enum vcnl4035_scan_index_order {
>> +	VCNL4035_CHAN_INDEX_LIGHT,
>> +};
>> +
>> +static const unsigned long vcnl4035_available_scan_masks[] = {
>> +	BIT(VCNL4035_CHAN_INDEX_LIGHT),
> 
> You only need to provide this if there restrictions on which channels can
> be run together.  Here there do not seem to be (as there is only one channel)
> so you don't need it.

Same device supports proximity as well, so will extend it. Yes, this is
not required until proximity comes in place. Will remove it now.

> 
>> +	0
>> +};
>> +
>> +static const struct iio_event_spec vcnl4035_event_spec[] = {
>> +	{
>> +		.type = IIO_EV_TYPE_THRESH,
>> +		.dir = IIO_EV_DIR_RISING,
>> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
>> +	}, {
>> +		.type = IIO_EV_TYPE_THRESH,
>> +		.dir = IIO_EV_DIR_FALLING,
>> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
>> +	}, {
>> +		.type = IIO_EV_TYPE_THRESH,
>> +		.dir = IIO_EV_DIR_EITHER,
>> +		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
>> +	},
>> +};
>> +
>> +static const struct iio_chan_spec vcnl4035_channels[] = {
>> +	{
>> +		.type = IIO_INTENSITY,
>> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
>> +				BIT(IIO_CHAN_INFO_INT_TIME) |
>> +				BIT(IIO_CHAN_INFO_SCALE),
>> +		.event_spec = vcnl4035_event_spec,
>> +		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
>> +		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
>> +		.scan_type = {
>> +			.sign = 'u',
>> +			.realbits = 16,
>> +			.storagebits = 16,
>> +			.endianness = IIO_LE,
>> +		},
>> +	},
>> +};
>> +
>> +static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
>> +{
>> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +					VCNL4035_MODE_ALS_MASK,
>> +					status);
>> +}
>> +
>> +static int vcnl4035_init(struct vcnl4035_data *data)
>> +{
>> +	int ret;
>> +	int id;
>> +
>> +	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
>> +	if (ret < 0) {
>> +		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
>> +		return ret;
>> +	}
>> +
>> +	if (id != VCNL4035_DEV_ID_VAL) {
>> +		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
>> +			id, VCNL4035_DEV_ID_VAL);
>> +		return -ENODEV;
>> +	}
>> +
>> +#ifndef CONFIG_PM
>> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
>> +	if (ret < 0)
>> +		return ret;
>> +#endif
>> +	/* set default integration time - 100 ms for ALS */
>> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +				 VCNL4035_ALS_IT_MASK,
>> +				 VCNL4035_ALS_IT_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
>> +
>> +	/* set default persistence time - 1 for ALS */
>> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
>> +				 VCNL4035_ALS_PERS_MASK,
>> +				 VCNL4035_ALS_PERS_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_update_bits default PERS returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
>> +
>> +	/* set default HIGH threshold for ALS */
>> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
>> +				VCNL4035_ALS_THDH_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_write default THDH returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
>> +
>> +	/* set default LOW threshold for ALS */
>> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
>> +				VCNL4035_ALS_THDL_DEFAULT);
>> +	if (ret) {
>> +		pr_err("regmap_write default THDL returned %d\n", ret);
>> +		return ret;
>> +	}
>> +	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
>> +
>> +	return 0;
>> +}
>> +
>> +static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
>> +{
>> +	switch (reg) {
>> +	case VCNL4035_ALS_CONF:
>> +	case VCNL4035_DEV_ID:
>> +		return false;
>> +	default:
>> +		return true;
>> +	}
>> +}
>> +
>> +static const struct regmap_config vcnl4035_regmap_config = {
>> +	.name		= VCNL4035_REGMAP_NAME,
>> +	.reg_bits	= 8,
>> +	.val_bits	= 16,
>> +	.max_register	= VCNL4035_DEV_ID,
>> +	.cache_type	= REGCACHE_RBTREE,
>> +	.volatile_reg	= vcnl4035_is_volatile_reg,
>> +	.val_format_endian = REGMAP_ENDIAN_LITTLE,
>> +};
>> +
>> +static int vcnl4035_probe(struct i2c_client *client,
>> +				const struct i2c_device_id *id)
>> +{
>> +	struct vcnl4035_data *data;
>> +	struct iio_dev *indio_dev;
>> +	struct regmap *regmap;
>> +	int ret;
>> +
>> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
>> +	if (!indio_dev)
>> +		return -ENOMEM;
>> +
>> +	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
>> +	if (IS_ERR(regmap)) {
>> +		dev_err(&client->dev, "regmap_init failed!\n");
>> +		return PTR_ERR(regmap);
>> +	}
>> +
>> +	data = iio_priv(indio_dev);
>> +	i2c_set_clientdata(client, indio_dev);
>> +	data->client = client;
>> +	data->regmap = regmap;
>> +
>> +	indio_dev->dev.parent = &client->dev;
>> +	indio_dev->info = &vcnl4035_info;
>> +	indio_dev->name = VCNL4035_DRV_NAME;
>> +	indio_dev->channels = vcnl4035_channels;
>> +	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>> +
>> +	ret = vcnl4035_init(data);
>> +	if (ret < 0) {
>> +		dev_err(&client->dev, "vcnl4035 chip init failed\n");
>> +		return ret;
>> +	}
>> +
>> +	ret = pm_runtime_set_active(&client->dev);
>> +	if (ret < 0)
>> +		goto fail_poweroff;
>> +
>> +	pm_runtime_enable(&client->dev);
>> +	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
>> +	pm_runtime_use_autosuspend(&client->dev);
>> +
>> +	if (client->irq) {
>> +		data->drdy_trigger0 = devm_iio_trigger_alloc(
>> +			indio_dev->dev.parent,
>> +			"%s-dev%d", indio_dev->name, indio_dev->id);
>> +		if (!data->drdy_trigger0) {
>> +			ret = -ENOMEM;
>> +			goto fail_pm_disable;
>> +		}
>> +		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
>> +		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
>> +		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
>> +		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
>> +		ret = iio_trigger_register(data->drdy_trigger0);
>> +		if (ret) {
>> +			dev_err(&client->dev, "iio trigger register failed\n");
>> +			goto fail_pm_disable;
>> +		}
>> +
>> +		/* Trigger setup */
>> +		ret = iio_triggered_buffer_setup(indio_dev, NULL,
>> +					vcnl4035_trigger_consumer_handler,
>> +					NULL);
>> +		if (ret < 0) {
>> +			dev_err(&client->dev, "iio triggered buffer setup failed\n");
>> +			goto fail_buffer_clean;
> This flow doesn't look right.  iio_triggered_buffer_setup should be cleaning up
> after itself if an error occurs within it.
> 
> As such you shouldn't be calling iio_triggered_buffer_cleanup.

Will re-order this, if not with new goto label.

> 
>> +		}
>> +
>> +		/* IRQ to trigger mapping */
>> +		ret = devm_request_threaded_irq(&client->dev, client->irq,
>> +			NULL, vcnl4035_drdy_irq_thread,
>> +			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
>> +			VCNL4035_IRQ_NAME, indio_dev);
>> +		if (ret < 0) {
>> +			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
>> +				client->irq);
>> +			goto fail_buffer_clean;
>> +			}
>> +	}
>> +
>> +	ret = iio_device_register(indio_dev);
>> +	if (ret)
>> +		goto fail_buffer_clean;
>> +	return 0;
>> +
>> +fail_buffer_clean:
>> +	iio_trigger_unregister(data->drdy_trigger0);
> This ordering should be the reverse of the above, so the trigger unregister
> should be after the buffer_cleanup call.

Sure, thanks.

> 
>> +	iio_triggered_buffer_cleanup(indio_dev);
>> +fail_pm_disable:
>> +	pm_runtime_disable(&client->dev);
>> +	pm_runtime_set_suspended(&client->dev);
>> +	pm_runtime_put_noidle(&client->dev);
>> +fail_poweroff:
>> +	vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
>> +	return ret;
>> +}
>> +
>> +static int vcnl4035_remove(struct i2c_client *client)
>> +{
>> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +
>> +	iio_device_unregister(indio_dev);
>> +	iio_trigger_unregister(data->drdy_trigger0);
> 
> As with the error handling above, this is not in reverse order
> of how it is setup in probe which fails the 'obviously correct' test.
> 
>> +	iio_triggered_buffer_cleanup(indio_dev);
>> +
>> +	pm_runtime_disable(&client->dev);
>> +	pm_runtime_set_suspended(&client->dev);
>> +	pm_runtime_put_noidle(&client->dev);
>> +
>> +	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
>> +					VCNL4035_MODE_ALS_DISABLE);
>> +}
>> +
>> +#ifdef CONFIG_PM
>> +static int vcnl4035_runtime_suspend(struct device *dev)
>> +{
>> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +
>> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
>> +	regcache_mark_dirty(data->regmap);
>> +
>> +	return ret;
>> +}
>> +
>> +static int vcnl4035_runtime_resume(struct device *dev)
>> +{
>> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
>> +	struct vcnl4035_data *data = iio_priv(indio_dev);
>> +	int ret;
>> +
>> +	regcache_sync(data->regmap);
>> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
>> +	if (ret < 0)
>> +		return ret;
>> +	/* wait for 1 ALS integration cycle */
>> +	msleep(data->als_it_val * 100);
>> +
>> +	return 0;
>> +}
>> +#endif
>> +
>> +static const struct dev_pm_ops vcnl4035_pm_ops = {
>> +	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
>> +				pm_runtime_force_resume)
>> +	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
>> +			   vcnl4035_runtime_resume, NULL)
>> +};
>> +
>> +static const struct of_device_id vcnl4035_of_match[] = {
>> +	{ .compatible = "vishay,vcnl4035", },
>> +	{ },
>> +};
>> +MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
>> +
>> +static const struct i2c_device_id vcnl4035_id[] = {
>> +	{ "vcnl4035", 0 },
>> +	{ }
>> +};
>> +MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
>> +
>> +static struct i2c_driver vcnl4035_driver = {
>> +	.driver = {
>> +		.name   = VCNL4035_DRV_NAME,
>> +		.pm	= &vcnl4035_pm_ops,
>> +		.of_match_table = of_match_ptr(vcnl4035_of_match),
>> +	},
>> +	.probe  = vcnl4035_probe,
>> +	.remove	= vcnl4035_remove,
>> +	.id_table = vcnl4035_id,
>> +};
>> +
>> +module_i2c_driver(vcnl4035_driver);
>> +
>> +MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
>> +MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
>> +MODULE_LICENSE("GPL v2");
> 
> 

-- 
Thanks,
Parthiban Nallathambi

DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v3 1/2] iio: light: Add support for vishay vcnl4035
  2018-08-02 14:01       ` Parthiban Nallathambi
@ 2018-08-03 22:16         ` Jonathan Cameron
  0 siblings, 0 replies; 19+ messages in thread
From: Jonathan Cameron @ 2018-08-03 22:16 UTC (permalink / raw)
  To: Parthiban Nallathambi
  Cc: Jonathan Cameron, knaack.h, lars, pmeerw, robh+dt, linux-iio,
	linux-kernel, mark.rutland, devicetree, matthias.bgg, wd, sbabic,
	hs

On Thu, 2 Aug 2018 16:01:32 +0200
Parthiban Nallathambi <pn@denx.de> wrote:

> Hi Jonathan,
> 
> General query, for threshold events and persistence value (period) we
> don't have any direct MACROS similar to IIO_CONST_ATTR_INT_TIME_AVAIL
> as ABI to show the available/range of values.
> 
> For most light and proximity sensors, I could see this value of
> threshold configuration to limit the amount of interrupts. But there
> isn't direct way to show the available range for threshold or periods
> available in sysfs.
> 
> Can we consider presenting it as separate device attribute? Like,
> 
> For threshold:
> in_intensity_thresh_range_available
> in_proximity_thresh_range_available

It's an event attribute but I think you did that for later versions so
guess you already figured this out.

Don't worry about lack of macros, it is a bit random whether we bother
with them or do it long hand.

For normal attribute (not event ones) we can do it programatically but
I've not been enforcing this as the interface is a bit non obvious and
I promised to document it but still haven't done so.

I don't currently propose to add anything similar for event attributes
as there are far fewer of them, but we might eventually.

see get_available callbacks...

> 
> For period:
> in_device_persistence/period_available
> 
> 
> On 08/02/2018 02:49 PM, Jonathan Cameron wrote:
> > On Thu, 2 Aug 2018 11:52:29 +0200
> > Parthiban Nallathambi <pn@denx.de> wrote:
> >   
> >> Add support for VCNL4035, which is capable of Ambient light
> >> sensing (ALS) and proximity function. This patch adds support
> >> only for ALS function
> >>
> >> Signed-off-by: Parthiban Nallathambi <pn@denx.de>  
> > Hi Parthiban,
> > 
> > Please avoid replying to a previous thread when sending a new version.
> > It tends to lead to several things:
> > 
> > 1. Really deep threads that are very hard to follow.
> > 2. In some email clients, your email appears many pages back in the history.
> >   
> 
> Sure, will send as fresh v4 next.
> 
> > 
> > Anyhow...
> > 
> > I'm confused how the runtime pm in here is supposed to work. There seems to none
> > of the usual calls to turn the device on when needed.
> > 
> > I was expected a pm_runtime_get_sync or similar in the read_raw path.  
> 
> Yes, I missed it. Thanks, will add in v4.
> 
> > 
> > A few other bits and bobs inline.
> > 
> > Thanks,
> > 
> > Jonathan
> >   
> >> -------
> >>
> >> Changelog since v1:
> >>
> >> 1. Fixed 0-day warning on le16_to_cpu usage
> >> 2. Persistence value is directly mapped to datasheet's value to
> >> avoid confusions of usage from sysfs
> >>
> >> Changelog in v3:
> >> - Usage of lock is not needed, removed mutex locking
> >> - ALS threshold and persistence configuration available
> >> as events and threshold events are notified to userspace
> >> - Usage of devm_ is re-ordered and exit handling updated
> >> - Complexity in timestamp calculation is removed and used
> >> iio_get_time_ns
> >> ---
> >>   drivers/iio/light/Kconfig    |  12 +
> >>   drivers/iio/light/Makefile   |   1 +
> >>   drivers/iio/light/vcnl4035.c | 619 +++++++++++++++++++++++++++++++++++++++++++
> >>   3 files changed, 632 insertions(+)
> >>   create mode 100644 drivers/iio/light/vcnl4035.c
> >>
> >> diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
> >> index c7ef8d1862d6..b7069a4c28a2 100644
> >> --- a/drivers/iio/light/Kconfig
> >> +++ b/drivers/iio/light/Kconfig
> >> @@ -447,6 +447,18 @@ config VCNL4000
> >>   	 To compile this driver as a module, choose M here: the
> >>   	 module will be called vcnl4000.
> >>   
> >> +config VCNL4035
> >> +	tristate "VCNL4035 combined ALS and proximity sensor"
> >> +	select REGMAP_I2C
> >> +	depends on I2C
> >> +	help
> >> +	 Say Y here if you want to build a driver for the Vishay VCNL4035,
> >> +	 combined ambient light (ALS) and proximity sensor. Currently only ALS
> >> +	 function is available.
> >> +
> >> +	 To compile this driver as a module, choose M here: the
> >> +	 module will be called vcnl4035.
> >> +
> >>   config VEML6070
> >>   	tristate "VEML6070 UV A light sensor"
> >>   	depends on I2C
> >> diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
> >> index 80943af5d627..dce98511a59b 100644
> >> --- a/drivers/iio/light/Makefile
> >> +++ b/drivers/iio/light/Makefile
> >> @@ -44,6 +44,7 @@ obj-$(CONFIG_TSL2772)		+= tsl2772.o
> >>   obj-$(CONFIG_TSL4531)		+= tsl4531.o
> >>   obj-$(CONFIG_US5182D)		+= us5182d.o
> >>   obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
> >> +obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
> >>   obj-$(CONFIG_VEML6070)		+= veml6070.o
> >>   obj-$(CONFIG_VL6180)		+= vl6180.o
> >>   obj-$(CONFIG_ZOPT2201)		+= zopt2201.o
> >> diff --git a/drivers/iio/light/vcnl4035.c b/drivers/iio/light/vcnl4035.c
> >> new file mode 100644
> >> index 000000000000..d2b5ae32baa8
> >> --- /dev/null
> >> +++ b/drivers/iio/light/vcnl4035.c
> >> @@ -0,0 +1,619 @@
> >> +// SPDX-License-Identifier: GPL-2.0
> >> +/*
> >> + * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
> >> + *
> >> + * Copyright (c) 2018, DENX Software Engineering GmbH
> >> + * Author: Parthiban Nallathambi <pn@denx.de>
> >> + *
> >> + *
> >> + * TODO: Proximity
> >> + */
> >> +#include <linux/module.h>
> >> +#include <linux/i2c.h>
> >> +#include <linux/bitops.h>
> >> +#include <linux/regmap.h>
> >> +#include <linux/pm_runtime.h>
> >> +
> >> +#include <linux/iio/iio.h>
> >> +#include <linux/iio/sysfs.h>
> >> +#include <linux/iio/events.h>
> >> +#include <linux/iio/buffer.h>
> >> +#include <linux/iio/trigger.h>
> >> +#include <linux/iio/trigger_consumer.h>
> >> +#include <linux/iio/triggered_buffer.h>
> >> +
> >> +#define VCNL4035_DRV_NAME	"vcnl4035"
> >> +#define VCNL4035_IRQ_NAME	"vcnl4035_event"
> >> +#define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
> >> +
> >> +/* Device registers */
> >> +#define VCNL4035_ALS_CONF	0x00
> >> +#define VCNL4035_ALS_THDH	0x01
> >> +#define VCNL4035_ALS_THDL	0x02
> >> +#define VCNL4035_ALS_DATA	0x0B
> >> +#define VCNL4035_INT_FLAG	0x0D
> >> +#define VCNL4035_DEV_ID		0x0E
> >> +
> >> +/* Register masks */
> >> +#define VCNL4035_MODE_ALS_MASK		BIT(0)
> >> +#define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
> >> +#define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
> >> +#define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
> >> +#define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
> >> +#define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
> >> +
> >> +/* Default values */
> >> +#define VCNL4035_MODE_ALS_ENABLE	BIT(0)
> >> +#define VCNL4035_MODE_ALS_DISABLE	0x00
> >> +#define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
> >> +#define VCNL4035_MODE_ALS_INT_DISABLE	0x00
> >> +#define VCNL4035_DEV_ID_VAL		0x80
> >> +#define VCNL4035_ALS_IT_DEFAULT		0x01
> >> +#define VCNL4035_ALS_PERS_DEFAULT	0x00
> >> +#define VCNL4035_ALS_THDH_DEFAULT	5000
> >> +#define VCNL4035_ALS_THDL_DEFAULT	100
> >> +#define VCNL4035_SLEEP_DELAY_MS		2000
> >> +
> >> +struct vcnl4035_data {
> >> +	struct i2c_client *client;
> >> +	struct regmap *regmap;
> >> +	unsigned int als_it_val;
> >> +	unsigned int als_persistence:4;
> >> +	unsigned int als_thresh_low;
> >> +	unsigned int als_thresh_high;
> >> +	struct iio_trigger *drdy_trigger0;
> >> +};
> >> +
> >> +static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
> >> +{
> >> +	int ret;
> >> +	int reg;
> >> +
> >> +	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
> >> +	if (ret < 0)
> >> +		return false;
> >> +	if (reg & (VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK))
> >> +		return true;
> >> +	else
> >> +		return false;
> >> +}
> >> +
> >> +static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
> >> +{
> >> +	struct iio_dev *indio_dev = private;
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +
> >> +	if (vcnl4035_is_triggered(data)) {
> >> +		iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
> >> +							0,
> >> +							IIO_EV_TYPE_THRESH,
> >> +							IIO_EV_DIR_EITHER),
> >> +				iio_get_time_ns(indio_dev));
> >> +		iio_trigger_poll_chained(data->drdy_trigger0);  
> > 
> > Hmm. So a single interrupt is used both as a trigger and as a threshold event.
> > A little unusual but fair enough.
> >   
> >> +		return IRQ_HANDLED;
> >> +	}
> >> +
> >> +	return IRQ_NONE;
> >> +}
> >> +
> >> +/* Triggered buffer */
> >> +static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
> >> +{
> >> +	struct iio_poll_func *pf = p;
> >> +	struct iio_dev *indio_dev = pf->indio_dev;
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +	u16 buffer[ALIGN(sizeof(u16), sizeof(s64)) + sizeof(s64)];
> >> +	int ret;
> >> +
> >> +	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, (int *)buffer);
> >> +	if (!ret)
> >> +		iio_push_to_buffers_with_timestamp(indio_dev, buffer,
> >> +						iio_get_time_ns(indio_dev));
> >> +	else
> >> +		dev_err(&data->client->dev,
> >> +			"Trigger consumer can't read from sensor.\n");
> >> +	iio_trigger_notify_done(indio_dev->trig);
> >> +
> >> +	return IRQ_HANDLED;
> >> +}
> >> +
> >> +static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
> >> +					bool enable_drdy)
> >> +{
> >> +	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
> >> +					VCNL4035_MODE_ALS_INT_DISABLE;
> >> +
> >> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> >> +				 VCNL4035_MODE_ALS_INT_MASK,
> >> +				 val);
> >> +}
> >> +
> >> +static const struct iio_trigger_ops vcnl4035_trigger_ops = {
> >> +	.validate_device = iio_trigger_validate_own_device,
> >> +	.set_trigger_state = vcnl4035_als_drdy_set_state,
> >> +};
> >> +
> >> +/*
> >> + *	Device IT	INT Time (ms)	Scale (lux/step)
> >> + *	000		50		0.064
> >> + *	001		100		0.032
> >> + *	010		200		0.016
> >> + *	100		400		0.008
> >> + *	101 - 111	800		0.004
> >> + * Values are proportional, so ALS INT is selected for input due to
> >> + * simplicity reason. Integration time value and scaling is
> >> + * calculated based on device INT value
> >> + *
> >> + * Raw value needs to be scaled using ALS steps
> >> + *
> >> + */
> >> +static int vcnl4035_read_raw(struct iio_dev *indio_dev,
> >> +			    struct iio_chan_spec const *chan, int *val,
> >> +			    int *val2, long mask)
> >> +{
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +	int ret;
> >> +	int raw_data;
> >> +
> >> +	switch (mask) {
> >> +	case IIO_CHAN_INFO_RAW:
> >> +		ret = iio_device_claim_direct_mode(indio_dev);
> >> +		if (ret)
> >> +			return -ret;
> >> +
> >> +		ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &raw_data);
> >> +		iio_device_release_direct_mode(indio_dev);
> >> +		if (ret < 0)
> >> +			return ret;
> >> +
> >> +		*val = raw_data;
> >> +		return IIO_VAL_INT;
> >> +	case IIO_CHAN_INFO_INT_TIME:
> >> +		*val = 50;
> >> +		if (!data->als_it_val)
> >> +			*val = data->als_it_val * 100;
> >> +		return IIO_VAL_INT;
> >> +	case IIO_CHAN_INFO_SCALE:
> >> +		*val = 64;
> >> +		if (!data->als_it_val)
> >> +			*val2 = 1000;
> >> +		else
> >> +			*val2 = data->als_it_val * 2 * 1000;
> >> +		return IIO_VAL_FRACTIONAL;
> >> +	default:
> >> +		return -EINVAL;
> >> +	}
> >> +}
> >> +
> >> +static int vcnl4035_write_raw(struct iio_dev *indio_dev,
> >> +				struct iio_chan_spec const *chan,
> >> +				int val, int val2, long mask)
> >> +{
> >> +	int ret;
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +
> >> +	switch (mask) {
> >> +	case IIO_CHAN_INFO_INT_TIME:
> >> +		if (val <= 0 || val > 800)
> >> +			return -EINVAL;
> >> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> >> +					 VCNL4035_ALS_IT_MASK,
> >> +					 val / 100);
> >> +		if (!ret)
> >> +			data->als_it_val = val / 100;
> >> +		return ret;
> >> +	default:
> >> +		return -EINVAL;
> >> +	}
> >> +}
> >> +
> >> +/* No direct ABI for persistence and threshold, so eventing */
> >> +static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
> >> +		const struct iio_chan_spec *chan, enum iio_event_type type,
> >> +		enum iio_event_direction dir, enum iio_event_info info,
> >> +		int *val, int *val2)
> >> +{
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +
> >> +	switch (info) {
> >> +	case IIO_EV_INFO_VALUE:
> >> +		switch (dir) {
> >> +		case IIO_EV_DIR_RISING:
> >> +			*val = data->als_thresh_high;
> >> +			return IIO_VAL_INT;
> >> +		case IIO_EV_DIR_FALLING:
> >> +			*val = data->als_thresh_low;
> >> +			return IIO_VAL_INT;
> >> +		default:
> >> +			return -EINVAL;
> >> +		}
> >> +		break;
> >> +	case IIO_EV_INFO_PERIOD:
> >> +		*val = data->als_persistence;
> >> +		return IIO_VAL_INT;
> >> +	default:
> >> +		return -EINVAL;
> >> +	}
> >> +
> >> +}
> >> +
> >> +static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
> >> +		const struct iio_chan_spec *chan, enum iio_event_type type,
> >> +		enum iio_event_direction dir, enum iio_event_info info, int val,
> >> +		int val2)
> >> +{
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +	int ret;
> >> +
> >> +	switch (info) {
> >> +	case IIO_EV_INFO_VALUE:
> >> +		/* 16 bit threshold range 0 - 65535 */
> >> +		if (val < 0 || val > 65535)
> >> +			return -EINVAL;
> >> +		if (dir == IIO_EV_DIR_RISING) {
> >> +			if (val < data->als_thresh_low)
> >> +				return -EINVAL;
> >> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
> >> +					   val);
> >> +			if (!ret)
> >> +				data->als_thresh_high = val;
> >> +		} else {
> >> +			if (val > data->als_thresh_high)
> >> +				return -EINVAL;
> >> +			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
> >> +					   val);
> >> +			if (!ret)
> >> +				data->als_thresh_low = val;
> >> +		}
> >> +		return ret;
> >> +	case IIO_EV_INFO_PERIOD:
> >> +		/* allow only 1 2 4 8 as persistence value */
> >> +		if (val < 0 || val > 8 || __sw_hweight8(val) != 1)
> >> +			return -EINVAL;
> >> +		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> >> +					 VCNL4035_ALS_PERS_MASK, val);
> >> +		if (!ret)
> >> +			data->als_persistence = val;
> >> +		return ret;
> >> +	default:
> >> +		return -EINVAL;
> >> +	}
> >> +}
> >> +
> >> +static IIO_CONST_ATTR_INT_TIME_AVAIL("50 100 200 400 800");
> >> +
> >> +static struct attribute *vcnl4035_attributes[] = {
> >> +	&iio_const_attr_integration_time_available.dev_attr.attr,
> >> +	NULL,
> >> +};
> >> +
> >> +static const struct attribute_group vcnl4035_attribute_group = {
> >> +	.attrs = vcnl4035_attributes,
> >> +};
> >> +
> >> +static const struct iio_info vcnl4035_info = {
> >> +	.read_raw		= vcnl4035_read_raw,
> >> +	.write_raw		= vcnl4035_write_raw,
> >> +	.read_event_value	= vcnl4035_read_thresh,
> >> +	.write_event_value	= vcnl4035_write_thresh,
> >> +	.attrs			= &vcnl4035_attribute_group,
> >> +};
> >> +
> >> +enum vcnl4035_scan_index_order {
> >> +	VCNL4035_CHAN_INDEX_LIGHT,
> >> +};
> >> +
> >> +static const unsigned long vcnl4035_available_scan_masks[] = {
> >> +	BIT(VCNL4035_CHAN_INDEX_LIGHT),  
> > 
> > You only need to provide this if there restrictions on which channels can
> > be run together.  Here there do not seem to be (as there is only one channel)
> > so you don't need it.  
> 
> Same device supports proximity as well, so will extend it. Yes, this is
> not required until proximity comes in place. Will remove it now.
> 
> >   
> >> +	0
> >> +};
> >> +
> >> +static const struct iio_event_spec vcnl4035_event_spec[] = {
> >> +	{
> >> +		.type = IIO_EV_TYPE_THRESH,
> >> +		.dir = IIO_EV_DIR_RISING,
> >> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
> >> +	}, {
> >> +		.type = IIO_EV_TYPE_THRESH,
> >> +		.dir = IIO_EV_DIR_FALLING,
> >> +		.mask_separate = BIT(IIO_EV_INFO_VALUE),
> >> +	}, {
> >> +		.type = IIO_EV_TYPE_THRESH,
> >> +		.dir = IIO_EV_DIR_EITHER,
> >> +		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
> >> +	},
> >> +};
> >> +
> >> +static const struct iio_chan_spec vcnl4035_channels[] = {
> >> +	{
> >> +		.type = IIO_INTENSITY,
> >> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> >> +				BIT(IIO_CHAN_INFO_INT_TIME) |
> >> +				BIT(IIO_CHAN_INFO_SCALE),
> >> +		.event_spec = vcnl4035_event_spec,
> >> +		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
> >> +		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
> >> +		.scan_type = {
> >> +			.sign = 'u',
> >> +			.realbits = 16,
> >> +			.storagebits = 16,
> >> +			.endianness = IIO_LE,
> >> +		},
> >> +	},
> >> +};
> >> +
> >> +static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
> >> +{
> >> +	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> >> +					VCNL4035_MODE_ALS_MASK,
> >> +					status);
> >> +}
> >> +
> >> +static int vcnl4035_init(struct vcnl4035_data *data)
> >> +{
> >> +	int ret;
> >> +	int id;
> >> +
> >> +	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
> >> +	if (ret < 0) {
> >> +		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
> >> +		return ret;
> >> +	}
> >> +
> >> +	if (id != VCNL4035_DEV_ID_VAL) {
> >> +		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
> >> +			id, VCNL4035_DEV_ID_VAL);
> >> +		return -ENODEV;
> >> +	}
> >> +
> >> +#ifndef CONFIG_PM
> >> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
> >> +	if (ret < 0)
> >> +		return ret;
> >> +#endif
> >> +	/* set default integration time - 100 ms for ALS */
> >> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> >> +				 VCNL4035_ALS_IT_MASK,
> >> +				 VCNL4035_ALS_IT_DEFAULT);
> >> +	if (ret) {
> >> +		pr_err("regmap_update_bits default ALS IT returned %d\n", ret);
> >> +		return ret;
> >> +	}
> >> +	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
> >> +
> >> +	/* set default persistence time - 1 for ALS */
> >> +	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
> >> +				 VCNL4035_ALS_PERS_MASK,
> >> +				 VCNL4035_ALS_PERS_DEFAULT);
> >> +	if (ret) {
> >> +		pr_err("regmap_update_bits default PERS returned %d\n", ret);
> >> +		return ret;
> >> +	}
> >> +	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
> >> +
> >> +	/* set default HIGH threshold for ALS */
> >> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
> >> +				VCNL4035_ALS_THDH_DEFAULT);
> >> +	if (ret) {
> >> +		pr_err("regmap_write default THDH returned %d\n", ret);
> >> +		return ret;
> >> +	}
> >> +	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
> >> +
> >> +	/* set default LOW threshold for ALS */
> >> +	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
> >> +				VCNL4035_ALS_THDL_DEFAULT);
> >> +	if (ret) {
> >> +		pr_err("regmap_write default THDL returned %d\n", ret);
> >> +		return ret;
> >> +	}
> >> +	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
> >> +{
> >> +	switch (reg) {
> >> +	case VCNL4035_ALS_CONF:
> >> +	case VCNL4035_DEV_ID:
> >> +		return false;
> >> +	default:
> >> +		return true;
> >> +	}
> >> +}
> >> +
> >> +static const struct regmap_config vcnl4035_regmap_config = {
> >> +	.name		= VCNL4035_REGMAP_NAME,
> >> +	.reg_bits	= 8,
> >> +	.val_bits	= 16,
> >> +	.max_register	= VCNL4035_DEV_ID,
> >> +	.cache_type	= REGCACHE_RBTREE,
> >> +	.volatile_reg	= vcnl4035_is_volatile_reg,
> >> +	.val_format_endian = REGMAP_ENDIAN_LITTLE,
> >> +};
> >> +
> >> +static int vcnl4035_probe(struct i2c_client *client,
> >> +				const struct i2c_device_id *id)
> >> +{
> >> +	struct vcnl4035_data *data;
> >> +	struct iio_dev *indio_dev;
> >> +	struct regmap *regmap;
> >> +	int ret;
> >> +
> >> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
> >> +	if (!indio_dev)
> >> +		return -ENOMEM;
> >> +
> >> +	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
> >> +	if (IS_ERR(regmap)) {
> >> +		dev_err(&client->dev, "regmap_init failed!\n");
> >> +		return PTR_ERR(regmap);
> >> +	}
> >> +
> >> +	data = iio_priv(indio_dev);
> >> +	i2c_set_clientdata(client, indio_dev);
> >> +	data->client = client;
> >> +	data->regmap = regmap;
> >> +
> >> +	indio_dev->dev.parent = &client->dev;
> >> +	indio_dev->info = &vcnl4035_info;
> >> +	indio_dev->name = VCNL4035_DRV_NAME;
> >> +	indio_dev->channels = vcnl4035_channels;
> >> +	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
> >> +	indio_dev->modes = INDIO_DIRECT_MODE;
> >> +
> >> +	ret = vcnl4035_init(data);
> >> +	if (ret < 0) {
> >> +		dev_err(&client->dev, "vcnl4035 chip init failed\n");
> >> +		return ret;
> >> +	}
> >> +
> >> +	ret = pm_runtime_set_active(&client->dev);
> >> +	if (ret < 0)
> >> +		goto fail_poweroff;
> >> +
> >> +	pm_runtime_enable(&client->dev);
> >> +	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
> >> +	pm_runtime_use_autosuspend(&client->dev);
> >> +
> >> +	if (client->irq) {
> >> +		data->drdy_trigger0 = devm_iio_trigger_alloc(
> >> +			indio_dev->dev.parent,
> >> +			"%s-dev%d", indio_dev->name, indio_dev->id);
> >> +		if (!data->drdy_trigger0) {
> >> +			ret = -ENOMEM;
> >> +			goto fail_pm_disable;
> >> +		}
> >> +		data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
> >> +		data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
> >> +		indio_dev->available_scan_masks = vcnl4035_available_scan_masks;
> >> +		iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
> >> +		ret = iio_trigger_register(data->drdy_trigger0);
> >> +		if (ret) {
> >> +			dev_err(&client->dev, "iio trigger register failed\n");
> >> +			goto fail_pm_disable;
> >> +		}
> >> +
> >> +		/* Trigger setup */
> >> +		ret = iio_triggered_buffer_setup(indio_dev, NULL,
> >> +					vcnl4035_trigger_consumer_handler,
> >> +					NULL);
> >> +		if (ret < 0) {
> >> +			dev_err(&client->dev, "iio triggered buffer setup failed\n");
> >> +			goto fail_buffer_clean;  
> > This flow doesn't look right.  iio_triggered_buffer_setup should be cleaning up
> > after itself if an error occurs within it.
> > 
> > As such you shouldn't be calling iio_triggered_buffer_cleanup.  
> 
> Will re-order this, if not with new goto label.
> 
> >   
> >> +		}
> >> +
> >> +		/* IRQ to trigger mapping */
> >> +		ret = devm_request_threaded_irq(&client->dev, client->irq,
> >> +			NULL, vcnl4035_drdy_irq_thread,
> >> +			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
> >> +			VCNL4035_IRQ_NAME, indio_dev);
> >> +		if (ret < 0) {
> >> +			dev_err(&client->dev, "request irq %d for trigger0 failed\n",
> >> +				client->irq);
> >> +			goto fail_buffer_clean;
> >> +			}
> >> +	}
> >> +
> >> +	ret = iio_device_register(indio_dev);
> >> +	if (ret)
> >> +		goto fail_buffer_clean;
> >> +	return 0;
> >> +
> >> +fail_buffer_clean:
> >> +	iio_trigger_unregister(data->drdy_trigger0);  
> > This ordering should be the reverse of the above, so the trigger unregister
> > should be after the buffer_cleanup call.  
> 
> Sure, thanks.
> 
> >   
> >> +	iio_triggered_buffer_cleanup(indio_dev);
> >> +fail_pm_disable:
> >> +	pm_runtime_disable(&client->dev);
> >> +	pm_runtime_set_suspended(&client->dev);
> >> +	pm_runtime_put_noidle(&client->dev);
> >> +fail_poweroff:
> >> +	vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
> >> +	return ret;
> >> +}
> >> +
> >> +static int vcnl4035_remove(struct i2c_client *client)
> >> +{
> >> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +
> >> +	iio_device_unregister(indio_dev);
> >> +	iio_trigger_unregister(data->drdy_trigger0);  
> > 
> > As with the error handling above, this is not in reverse order
> > of how it is setup in probe which fails the 'obviously correct' test.
> >   
> >> +	iio_triggered_buffer_cleanup(indio_dev);
> >> +
> >> +	pm_runtime_disable(&client->dev);
> >> +	pm_runtime_set_suspended(&client->dev);
> >> +	pm_runtime_put_noidle(&client->dev);
> >> +
> >> +	return vcnl4035_set_als_power_state(iio_priv(indio_dev),
> >> +					VCNL4035_MODE_ALS_DISABLE);
> >> +}
> >> +
> >> +#ifdef CONFIG_PM
> >> +static int vcnl4035_runtime_suspend(struct device *dev)
> >> +{
> >> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +	int ret;
> >> +
> >> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
> >> +	regcache_mark_dirty(data->regmap);
> >> +
> >> +	return ret;
> >> +}
> >> +
> >> +static int vcnl4035_runtime_resume(struct device *dev)
> >> +{
> >> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> >> +	struct vcnl4035_data *data = iio_priv(indio_dev);
> >> +	int ret;
> >> +
> >> +	regcache_sync(data->regmap);
> >> +	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
> >> +	if (ret < 0)
> >> +		return ret;
> >> +	/* wait for 1 ALS integration cycle */
> >> +	msleep(data->als_it_val * 100);
> >> +
> >> +	return 0;
> >> +}
> >> +#endif
> >> +
> >> +static const struct dev_pm_ops vcnl4035_pm_ops = {
> >> +	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
> >> +				pm_runtime_force_resume)
> >> +	SET_RUNTIME_PM_OPS(vcnl4035_runtime_suspend,
> >> +			   vcnl4035_runtime_resume, NULL)
> >> +};
> >> +
> >> +static const struct of_device_id vcnl4035_of_match[] = {
> >> +	{ .compatible = "vishay,vcnl4035", },
> >> +	{ },
> >> +};
> >> +MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
> >> +
> >> +static const struct i2c_device_id vcnl4035_id[] = {
> >> +	{ "vcnl4035", 0 },
> >> +	{ }
> >> +};
> >> +MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
> >> +
> >> +static struct i2c_driver vcnl4035_driver = {
> >> +	.driver = {
> >> +		.name   = VCNL4035_DRV_NAME,
> >> +		.pm	= &vcnl4035_pm_ops,
> >> +		.of_match_table = of_match_ptr(vcnl4035_of_match),
> >> +	},
> >> +	.probe  = vcnl4035_probe,
> >> +	.remove	= vcnl4035_remove,
> >> +	.id_table = vcnl4035_id,
> >> +};
> >> +
> >> +module_i2c_driver(vcnl4035_driver);
> >> +
> >> +MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
> >> +MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
> >> +MODULE_LICENSE("GPL v2");  
> > 
> >   
> 


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2018-08-03 22:16 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-28 12:30 [PATCH 1/2] iio: light: Add support for vishay vcnl4035 Parthiban Nallathambi
2018-06-28 12:30 ` [PATCH 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
2018-06-28 15:35 ` [PATCH 1/2] iio: light: Add support " kbuild test robot
2018-06-29 15:38 ` [PATCH v2 " Parthiban Nallathambi
2018-06-29 15:38   ` [PATCH v2 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
2018-07-06 20:34     ` Rob Herring
2018-07-07 17:06       ` Parthiban Nallathambi
2018-06-29 23:46   ` [PATCH v2 1/2] iio: light: Add support " kbuild test robot
2018-06-30  9:51   ` Peter Meerwald-Stadler
2018-07-03 12:58     ` Parthiban Nallathambi
2018-08-02  9:52   ` [PATCH v3 " Parthiban Nallathambi
2018-08-02  9:52     ` [PATCH v3 2/2] iio: light: Add device tree binding " Parthiban Nallathambi
2018-08-02 12:51       ` Jonathan Cameron
2018-08-02 12:49     ` [PATCH v3 1/2] iio: light: Add support " Jonathan Cameron
2018-08-02 14:01       ` Parthiban Nallathambi
2018-08-03 22:16         ` Jonathan Cameron
2018-06-29 17:59 ` [PATCH " Jonathan Cameron
2018-07-03 13:35   ` Parthiban Nallathambi
2018-07-07 16:23     ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).