Skip to contentSkip to conversation

iio: proximity: add PulsedLight LIDAR sensor support

PATCH Superseded Matt Ranostay 5 participants 16 messages Aug 1, 2015 – Aug 3, 2015 10 messages not in the archive

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover iio: proximity: add PulsedLight LIDAR sensor support

PulsedLight "blue label" v2 LIDAR sensor can be polled over 100HZ with the sysfs + hrtimer iio trigger, or using single measurement raw measurment.

This patchset adds support for this range finding LIDAR module, and reports distance back in meters to centimeter resolution with +/- 2.5 CM of accuracy at ~40 meters.

Matt Ranostay (2):
devicetree: add PulsedLight vendor prefix
iio: proximity: add support for PulsedLight LIDAR

 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 drivers/iio/proximity/Kconfig                      |  13 +
 drivers/iio/proximity/Makefile                     |   1 +
 drivers/iio/proximity/lidar.c                      | 309 +++++++++++++++++++++
 4 files changed, 324 insertions(+)
 create mode 100644 drivers/iio/proximity/lidar.c
Signature · 1 line
1.9.1

A message that is not in the archive (sent off-list or not yet received): 1438401484-22101-1-git-send-email-mranostay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH 1/2 devicetree: add PulsedLight vendor prefix

---
 Documentation/devicetree/bindings/vendor-prefixes.txt | 1 +
 1 file changed, 1 insertion(+)
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 181b53e..e466902 100644
@@ -163,6 +163,7 @@ phytec	PHYTEC Messtechnik GmbH
 picochip	Picochip Ltd
 plathome	Plat'Home Co., Ltd.
 pixcir  PIXCIR MICROELECTRONICS Co., Ltd
+pulsedlight	PulsedLight, Inc
 powervr	PowerVR (deprecated, use img)
 qca	Qualcomm Atheros, Inc.
 qcom	Qualcomm Technologies, Inc
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

PATCH 2/2 iio: proximity: add support for PulsedLight LIDAR

In reply to a message not in the archive

Add support for the PulsedLight LIDAR rangefinder sensor which allows high speed (over 300Hz) distance measurements using Barker Coding within 40 meter range.

Support only tested on the "blue label" rev 2, but may work using polling at low sample frequencies on the original version.

---
 drivers/iio/proximity/Kconfig  |  13 ++
 drivers/iio/proximity/Makefile |   1 +
 drivers/iio/proximity/lidar.c  | 309 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 323 insertions(+)
 create mode 100644 drivers/iio/proximity/lidar.c
drivers/iio/proximity/Kconfig +13 -0
index 41a8d8f..8111e11 100644
@@ -20,6 +20,19 @@ endmenu
 
 menu "Proximity sensors"
 
+config LIDAR
+	tristate "PulsedLight LIDAR sensor"
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	select REGMAP_I2C
+	depends on I2C
+	help
+	  Say Y to build a driver for PulsedLight LIDAR range finding
+	  sensor.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called lidar.
+
 config SX9500
 	tristate "SX9500 Semtech proximity sensor"
 	select IIO_BUFFER
drivers/iio/proximity/Makefile +1 -0
index 9818dc5..36a95f3 100644
@@ -4,4 +4,5 @@
 
 # When adding new entries keep the list in alphabetical order
 obj-$(CONFIG_AS3935)		+= as3935.o
+obj-$(CONFIG_LIDAR)		+= lidar.o
 obj-$(CONFIG_SX9500)		+= sx9500.o
drivers/iio/proximity/lidar.c new file +309 -0

309 lines not on this page · raw message

Signature · 1 line
1.9.1

A message that is not in the archive (sent off-list or not yet received): 1438401484-22101-3-git-send-email-mranostay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#5 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

Hi Matt,

Find minor comments.

Quoted 4 lines · Matt Ranostay

Add support for the PulsedLight LIDAR rangefinder sensor which allows high speed (over 300Hz) distance measurements using Barker Coding within 40 meter range.

Quoted 8 lines

Support only tested on the "blue label" rev 2, but may work using polling at low sample frequencies on the original version.

---
  drivers/iio/proximity/Kconfig  |  13 ++
  drivers/iio/proximity/Makefile |   1 +
  drivers/iio/proximity/lidar.c  | 309 +++++++++++++++++++++++++++++++++++++++++

Add ABI documentation Documentation/ABI/testing/sysfs-bus-iio-proximity-lidar

Quoted 8 lines
  3 files changed, 323 insertions(+)

create mode 100644 drivers/iio/proximity/lidar.c

drivers/iio/proximity/Kconfig +0 -0
index 41a8d8f..8111e11 100644

drivers/iio/proximity/Kconfig endmenu · line 27

Quoted 8 lines
drivers/iio/proximity/Kconfig +5 -0
@@ -20,6 +20,19 @@ endmenu
 
  menu "Proximity sensors"
 
+config LIDAR
+	tristate "PulsedLight LIDAR sensor"
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	select REGMAP_I2C

You did not use REGMAP in your driver.
Probably it would be better to use this API in your driver since the chip seems fully SMBUS compatible.

Quoted 48 lines
drivers/iio/proximity/Kconfig +8 -0
+	depends on I2C
+	help
+	  Say Y to build a driver for PulsedLight LIDAR range finding
+	  sensor.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called lidar.
+
  config SX9500
  	tristate "SX9500 Semtech proximity sensor"
  	select IIO_BUFFER
drivers/iio/proximity/Makefile +1 -0
index 9818dc5..36a95f3 100644
@@ -4,4 +4,5 @@
 
  # When adding new entries keep the list in alphabetical order
  obj-$(CONFIG_AS3935)		+= as3935.o
+obj-$(CONFIG_LIDAR)		+= lidar.o
  obj-$(CONFIG_SX9500)		+= sx9500.o
drivers/iio/proximity/lidar.c new file +21 -0
new file mode 100644
index 0000000..e56df58
@@ -0,0 +1,309 @@
+/*
+ * lidar.c - Support for PulsedLight LIDAR range finding sensor
+ *
+ * Copyright (C) 2015 Matt Ranostay <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * TODO: runtime pm, interrupt mode, and signal strength reporting
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>

drivers/iio/proximity/lidar.c line 31

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/triggered_buffer.h>

put it in alphabetic order

drivers/iio/proximity/lidar.c line 34

Quoted 3 lines
drivers/iio/proximity/lidar.c new file +3 -0
+
+#define LIDAR_REG_CONTROL	0x00
+#define LIDAR_REG_CONTROL_ACQUIRE	BIT(2)

tab aligned

Quoted 255 lines
drivers/iio/proximity/lidar.c new file +255 -0
+
+#define LIDAR_REG_STATUS	0x01
+#define LIDAR_REG_STATUS_RDY	BIT(0)
+
+#define LIDAR_REG_DATA_HBYTE	0x0f
+#define LIDAR_REG_DATA_LBYTE	0x10
+#define LIDAR_REG_DATA_MAX	0xffff
+
+#define LIDAR_DRV_NAME	"lidar"
+
+struct lidar_data {
+	struct mutex lock;
+	struct iio_dev *indio_dev;
+	struct i2c_client *client;
+
+	/* config */
+	int calib_bias;
+
+	u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */
+};
+
+static const struct iio_chan_spec lidar_channels[] = {
+	{
+		.type = IIO_DISTANCE,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_CALIBBIAS),
+		.scan_index = 0,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 16,
+			.storagebits = 16,
+		},
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1),
+};
+
+static inline int lidar_read_byte(struct lidar_data *data, int reg)
+{
+	struct i2c_client *client = data->client;
+	int ret;
+
+	ret = i2c_smbus_write_byte(client, reg);
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot write addr value");
+		return ret;
+	}
+
+	ret = i2c_smbus_read_byte(client);
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot read data value");
+		return ret;
+	}
+
+	return ret;
+}
+
+static inline int lidar_write_control(struct lidar_data *data, int val)
+{
+	return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL, val);
+}
+
+static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
+{
+	int ret;
+	int val;
+
+	ret = lidar_read_byte(data, LIDAR_REG_DATA_HBYTE);
+	if (ret < 0)
+		return ret;
+	val = ret << 8;
+
+	ret = lidar_read_byte(data, LIDAR_REG_DATA_LBYTE);
+	if (ret < 0)
+		return ret;
+	val |= ret;
+
+	/* correct any possible overflow or underflow */
+	val += data->calib_bias / 10000;
+	if (val < 0)
+		val = 0;
+
+	if (val > LIDAR_REG_DATA_MAX)
+		val = LIDAR_REG_DATA_MAX;
+
+	*reg = val;
+
+	return 0;
+}
+
+static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
+{
+	struct i2c_client *client = data->client;
+	int tries = 10;
+	int ret;
+
+	/* start sample */
+	ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot send start measurement command");
+		return ret;
+	}
+
+	while (tries--) {
+		usleep_range(1000, 2000);
+
+		ret = lidar_read_byte(data, LIDAR_REG_STATUS);
+		if (ret < 0)
+			break;
+
+		/* sample ready to read */
+		if (!(ret & LIDAR_REG_STATUS_RDY)) {
+			ret = lidar_read_measurement(data, reg);
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static int lidar_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct lidar_data *data = iio_priv(indio_dev);
+	int ret = -EINVAL;
+
+	if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW)
+		return -EBUSY;
+
+	mutex_lock(&data->lock);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		u16 reg;
+
+		ret = lidar_get_measurement(data, &reg);
+		if (!ret) {
+			*val = reg / 100;
+			*val2 = (reg % 100) * 10000;
+			ret = IIO_VAL_INT_PLUS_MICRO;
+		}
+		break;
+	}
+	case IIO_CHAN_INFO_CALIBBIAS:
+		*val = 0;
+		*val2 = data->calib_bias;
+		ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
+	}
+
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+static int lidar_write_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int val, int val2, long mask)
+{
+	struct lidar_data *data = iio_priv(indio_dev);
+
+	if (mask == IIO_CHAN_INFO_CALIBBIAS) {
+		if (val != 0)
+			return -EINVAL;
+
+		/* cm increments only */
+		if (val2 % 10000)
+			return -EINVAL;
+
+		mutex_lock(&data->lock);
+		data->calib_bias = val2;
+		mutex_unlock(&data->lock);
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+static irqreturn_t lidar_trigger_handler(int irq, void *private)
+{
+	struct iio_poll_func *pf = private;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct lidar_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = lidar_get_measurement(data, &data->buffer[0]);
+	if (!ret) {
+		iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+						   iio_get_time_ns());
+	} else {
+		dev_err(&data->client->dev, "cannot read LIDAR measurement");
+	}
+
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static struct iio_info lidar_info = {
+	.driver_module = THIS_MODULE,
+	.read_raw = lidar_read_raw,
+	.write_raw = lidar_write_raw,
+};
+
+static int lidar_probe(struct i2c_client *client,
+		       const struct i2c_device_id *id)
+{
+	struct lidar_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	indio_dev->info = &lidar_info;
+	indio_dev->name = LIDAR_DRV_NAME;
+	indio_dev->channels = lidar_channels;
+	indio_dev->num_channels = ARRAY_SIZE(lidar_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+
+	data->client = client;
+	data->indio_dev = indio_dev;
+	mutex_init(&data->lock);
+
+	ret = iio_triggered_buffer_setup(indio_dev, NULL,
+					 lidar_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	ret = iio_device_register(indio_dev);
+	if (ret)
+		goto error_unreg_buffer;
+
+	return 0;
+
+error_unreg_buffer:
+	iio_triggered_buffer_cleanup(indio_dev);
+
+	return ret;
+}
+
+static int lidar_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+	iio_device_unregister(indio_dev);
+	iio_triggered_buffer_cleanup(indio_dev);
+
+	return 0;
+}
+

drivers/iio/proximity/lidar.c line 299

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+static const struct i2c_device_id lidar_id[] = {
+	{"lidar", 0},
+	{ },
+};
+MODULE_DEVICE_TABLE(i2c, lidar_id);
+
+static struct i2c_driver lidar_driver = {
+	.driver = {
+		.name	= LIDAR_DRV_NAME,
+		.owner	= THIS_MODULE,

the .owner field is not needed since it is overridden in i2c_register_driver

drivers/iio/proximity/lidar.c line 309

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+	},
+	.probe		= lidar_probe,
+	.remove		= lidar_remove,
+	.id_table	= lidar_id,
+};
+module_i2c_driver(lidar_driver);
+
+MODULE_AUTHOR("Matt Ranostay <[email protected]>");
+MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
+MODULE_LICENSE("GPL");

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 55BC85AF.1030608-M4DtvfQ/[email protected]

#7 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

Quoted 14 lines · Vladimir Barinov

Hi Matt,

Find minor comments.

Quoted 2 lines · Matt Ranostay

Add support for the PulsedLight LIDAR rangefinder sensor which allows high speed (over 300Hz) distance measurements using Barker Coding

within

Quoted 3 lines

40 meter range.

Support only tested on the "blue label" rev 2, but may work using

polling

Quoted 2 lines

at low sample frequencies on the original version.

Quoted 8 lines
Quoted 5 lines
---
  drivers/iio/proximity/Kconfig  |  13 ++
  drivers/iio/proximity/Makefile |   1 +
  drivers/iio/proximity/lidar.c  | 309

+++++++++++++++++++++++++++++++++++++++++
Add ABI documentation Documentation/ABI/testing/sysfs-bus-iio-proximity-lidar

Why? Reading on phone so may have missed something but all ABI is standard. Covered by top level docs.

Quoted 12 lines

Review

Quoted 4 lines
  3 files changed, 323 insertions(+)

create mode 100644 drivers/iio/proximity/lidar.c

(quoted diff) +0 -0

b/drivers/iio/proximity/Kconfig

Quoted 7 lines

index 41a8d8f..8111e11 100644

drivers/iio/proximity/Kconfig +0 -0
@@ -20,6 +20,19 @@ endmenu
 
  menu "Proximity sensors"
 
Quoted 8 lines

drivers/iio/proximity/Kconfig endmenu · line 27

Quoted 5 lines
drivers/iio/proximity/Kconfig +5 -0
+config LIDAR
+	tristate "PulsedLight LIDAR sensor"
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	select REGMAP_I2C

You did not use REGMAP in your driver.
Probably it would be better to use this API in your driver since the chip seems fully SMBUS compatible.

Unless there is a significant gain don't bother. Now if the device also has an spi bus or the driver does extensive caching it would be worthwhile.

Quoted 351 lines

Review

Quoted 12 lines
drivers/iio/proximity/Kconfig +8 -0
+	depends on I2C
+	help
+	  Say Y to build a driver for PulsedLight LIDAR range finding
+	  sensor.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called lidar.
+
  config SX9500
  	tristate "SX9500 Semtech proximity sensor"
  	select IIO_BUFFER
(quoted diff) +0 -0

b/drivers/iio/proximity/Makefile

Review

Quoted 10 lines

index 9818dc5..36a95f3 100644

drivers/iio/proximity/Makefile +1 -0
@@ -4,4 +4,5 @@
 
  # When adding new entries keep the list in alphabetical order
  obj-$(CONFIG_AS3935)		+= as3935.o
+obj-$(CONFIG_LIDAR)		+= lidar.o
  obj-$(CONFIG_SX9500)		+= sx9500.o
(quoted diff) +0 -0

b/drivers/iio/proximity/lidar.c

drivers/iio/proximity/lidar.c line 6

Quoted 11 lines

new file mode 100644
index 0000000..e56df58

drivers/iio/proximity/lidar.c new file +6 -0
@@ -0,0 +1,309 @@
+/*
+ * lidar.c - Support for PulsedLight LIDAR range finding sensor
+ *
+ * Copyright (C) 2015 Matt Ranostay <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or

modify

drivers/iio/proximity/lidar.c line 7

Quoted 1 line
drivers/iio/proximity/lidar.c new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 14 lines
drivers/iio/proximity/lidar.c new file +14 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * TODO: runtime pm, interrupt mode, and signal strength reporting
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>

drivers/iio/proximity/lidar.c line 31

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/triggered_buffer.h>

put it in alphabetic order

drivers/iio/proximity/lidar.c line 34

Quoted 3 lines
drivers/iio/proximity/lidar.c new file +3 -0
+
+#define LIDAR_REG_CONTROL	0x00
+#define LIDAR_REG_CONTROL_ACQUIRE	BIT(2)

tab aligned

Quoted 47 lines
drivers/iio/proximity/lidar.c new file +47 -0
+
+#define LIDAR_REG_STATUS	0x01
+#define LIDAR_REG_STATUS_RDY	BIT(0)
+
+#define LIDAR_REG_DATA_HBYTE	0x0f
+#define LIDAR_REG_DATA_LBYTE	0x10
+#define LIDAR_REG_DATA_MAX	0xffff
+
+#define LIDAR_DRV_NAME	"lidar"
+
+struct lidar_data {
+	struct mutex lock;
+	struct iio_dev *indio_dev;
+	struct i2c_client *client;
+
+	/* config */
+	int calib_bias;
+
+	u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */
+};
+
+static const struct iio_chan_spec lidar_channels[] = {
+	{
+		.type = IIO_DISTANCE,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_CALIBBIAS),
+		.scan_index = 0,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 16,
+			.storagebits = 16,
+		},
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1),
+};
+
+static inline int lidar_read_byte(struct lidar_data *data, int reg)
+{
+	struct i2c_client *client = data->client;
+	int ret;
+
+	ret = i2c_smbus_write_byte(client, reg);
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot write addr value");
+		return ret;
+	}
+

drivers/iio/proximity/lidar.c

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+	ret = i2c_smbus_read_byte(client);
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot read data value");
+		return ret;
+	}
+
+	return ret;
+}
+
+static inline int lidar_write_control(struct lidar_data *data, int

val)

drivers/iio/proximity/lidar.c line 93

Quoted 2 lines
drivers/iio/proximity/lidar.c new file +2 -0
+{
+	return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL,

val);

Quoted 196 lines
drivers/iio/proximity/lidar.c new file +196 -0
+}
+
+static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
+{
+	int ret;
+	int val;
+
+	ret = lidar_read_byte(data, LIDAR_REG_DATA_HBYTE);
+	if (ret < 0)
+		return ret;
+	val = ret << 8;
+
+	ret = lidar_read_byte(data, LIDAR_REG_DATA_LBYTE);
+	if (ret < 0)
+		return ret;
+	val |= ret;
+
+	/* correct any possible overflow or underflow */
+	val += data->calib_bias / 10000;
+	if (val < 0)
+		val = 0;
+
+	if (val > LIDAR_REG_DATA_MAX)
+		val = LIDAR_REG_DATA_MAX;
+
+	*reg = val;
+
+	return 0;
+}
+
+static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
+{
+	struct i2c_client *client = data->client;
+	int tries = 10;
+	int ret;
+
+	/* start sample */
+	ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot send start measurement command");
+		return ret;
+	}
+
+	while (tries--) {
+		usleep_range(1000, 2000);
+
+		ret = lidar_read_byte(data, LIDAR_REG_STATUS);
+		if (ret < 0)
+			break;
+
+		/* sample ready to read */
+		if (!(ret & LIDAR_REG_STATUS_RDY)) {
+			ret = lidar_read_measurement(data, reg);
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static int lidar_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct lidar_data *data = iio_priv(indio_dev);
+	int ret = -EINVAL;
+
+	if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW)
+		return -EBUSY;
+
+	mutex_lock(&data->lock);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		u16 reg;
+
+		ret = lidar_get_measurement(data, &reg);
+		if (!ret) {
+			*val = reg / 100;
+			*val2 = (reg % 100) * 10000;
+			ret = IIO_VAL_INT_PLUS_MICRO;
+		}
+		break;
+	}
+	case IIO_CHAN_INFO_CALIBBIAS:
+		*val = 0;
+		*val2 = data->calib_bias;
+		ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
+	}
+
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+static int lidar_write_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int val, int val2, long mask)
+{
+	struct lidar_data *data = iio_priv(indio_dev);
+
+	if (mask == IIO_CHAN_INFO_CALIBBIAS) {
+		if (val != 0)
+			return -EINVAL;
+
+		/* cm increments only */
+		if (val2 % 10000)
+			return -EINVAL;
+
+		mutex_lock(&data->lock);
+		data->calib_bias = val2;
+		mutex_unlock(&data->lock);
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+static irqreturn_t lidar_trigger_handler(int irq, void *private)
+{
+	struct iio_poll_func *pf = private;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct lidar_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = lidar_get_measurement(data, &data->buffer[0]);
+	if (!ret) {
+		iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+						   iio_get_time_ns());
+	} else {
+		dev_err(&data->client->dev, "cannot read LIDAR measurement");
+	}
+
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static struct iio_info lidar_info = {
+	.driver_module = THIS_MODULE,
+	.read_raw = lidar_read_raw,
+	.write_raw = lidar_write_raw,
+};
+
+static int lidar_probe(struct i2c_client *client,
+		       const struct i2c_device_id *id)
+{
+	struct lidar_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	indio_dev->info = &lidar_info;
+	indio_dev->name = LIDAR_DRV_NAME;
+	indio_dev->channels = lidar_channels;
+	indio_dev->num_channels = ARRAY_SIZE(lidar_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+
+	data->client = client;
+	data->indio_dev = indio_dev;
+	mutex_init(&data->lock);
+
+	ret = iio_triggered_buffer_setup(indio_dev, NULL,
+					 lidar_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	ret = iio_device_register(indio_dev);
+	if (ret)
+		goto error_unreg_buffer;
+
+	return 0;
+
+error_unreg_buffer:
+	iio_triggered_buffer_cleanup(indio_dev);
+
+	return ret;
+}
+
+static int lidar_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+	iio_device_unregister(indio_dev);
+	iio_triggered_buffer_cleanup(indio_dev);
+
+	return 0;
+}
+

drivers/iio/proximity/lidar.c line 299

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+static const struct i2c_device_id lidar_id[] = {
+	{"lidar", 0},
+	{ },
+};
+MODULE_DEVICE_TABLE(i2c, lidar_id);
+
+static struct i2c_driver lidar_driver = {
+	.driver = {
+		.name	= LIDAR_DRV_NAME,
+		.owner	= THIS_MODULE,

the .owner field is not needed since it is overridden in i2c_register_driver

drivers/iio/proximity/lidar.c line 309

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+	},
+	.probe		= lidar_probe,
+	.remove		= lidar_remove,
+	.id_table	= lidar_id,
+};
+module_i2c_driver(lidar_driver);
+
+MODULE_AUTHOR("Matt Ranostay <[email protected]>");
+MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
+MODULE_LICENSE("GPL");

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.

A message that is not in the archive (sent off-list or not yet received): 791628AC-C26A-4D88-8A5B-54B2E4EED75B-tko9wxEg+fIOOJlXag/[email protected]

#9 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

Quoted 23 lines · Jonathan Cameron
Quoted 19 lines · Vladimir Barinov

Hi Matt,

Find minor comments.

Quoted 2 lines · Matt Ranostay

Add support for the PulsedLight LIDAR rangefinder sensor which allows high speed (over 300Hz) distance measurements using Barker Coding

within

Quoted 3 lines

40 meter range.

Support only tested on the "blue label" rev 2, but may work using

polling

Quoted 7 lines

at low sample frequencies on the original version.

---
  drivers/iio/proximity/Kconfig  |  13 ++
  drivers/iio/proximity/Makefile |   1 +
  drivers/iio/proximity/lidar.c  | 309
Quoted 5 lines
Quoted 3 lines

+++++++++++++++++++++++++++++++++++++++++
Add ABI documentation Documentation/ABI/testing/sysfs-bus-iio-proximity-lidar

Why? Reading on phone so may have missed something but all ABI is standard. Covered by top level docs.

Only thing that would be documented that IIO_DISTANCE is in meters. But seems standard.

Quoted 21 lines
Quoted 13 lines

Review

Quoted 5 lines
  3 files changed, 323 insertions(+)

create mode 100644 drivers/iio/proximity/lidar.c

(quoted diff) +0 -0

b/drivers/iio/proximity/Kconfig

Quoted 7 lines

index 41a8d8f..8111e11 100644

drivers/iio/proximity/Kconfig +0 -0
@@ -20,6 +20,19 @@ endmenu
 
  menu "Proximity sensors"
 
Quoted 8 lines

drivers/iio/proximity/Kconfig endmenu · line 27

Quoted 5 lines
drivers/iio/proximity/Kconfig +5 -0
+config LIDAR
+    tristate "PulsedLight LIDAR sensor"
+    select IIO_BUFFER
+    select IIO_TRIGGERED_BUFFER
+    select REGMAP_I2C

You did not use REGMAP in your driver.
Probably it would be better to use this API in your driver since the chip seems fully SMBUS compatible.

Quoted 2 lines

Unless there is a significant gain don't bother. Now if the device also has an spi bus or the driver does extensive caching it would be worthwhile.

Correctly, none of the the data is cacheable so regmap doesn't make sense here.

Quoted 355 lines
Quoted 337 lines

Review

Quoted 13 lines
drivers/iio/proximity/Kconfig +8 -0
+    depends on I2C
+    help
+      Say Y to build a driver for PulsedLight LIDAR range finding
+      sensor.
+
+      To compile this driver as a module, choose M here: the
+      module will be called lidar.
+
  config SX9500
     tristate "SX9500 Semtech proximity sensor"
     select IIO_BUFFER
(quoted diff) +0 -0

b/drivers/iio/proximity/Makefile

Review

Quoted 10 lines

index 9818dc5..36a95f3 100644

drivers/iio/proximity/Makefile +1 -0
@@ -4,4 +4,5 @@
 
  # When adding new entries keep the list in alphabetical order
  obj-$(CONFIG_AS3935)               += as3935.o
+obj-$(CONFIG_LIDAR)         += lidar.o
  obj-$(CONFIG_SX9500)               += sx9500.o
(quoted diff) +0 -0

b/drivers/iio/proximity/lidar.c

drivers/iio/proximity/lidar.c line 6

Quoted 11 lines

new file mode 100644
index 0000000..e56df58

drivers/iio/proximity/lidar.c new file +6 -0
@@ -0,0 +1,309 @@
+/*
+ * lidar.c - Support for PulsedLight LIDAR range finding sensor
+ *
+ * Copyright (C) 2015 Matt Ranostay <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or

modify

drivers/iio/proximity/lidar.c line 7

Quoted 1 line
drivers/iio/proximity/lidar.c new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 14 lines
drivers/iio/proximity/lidar.c new file +14 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * TODO: runtime pm, interrupt mode, and signal strength reporting
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>

drivers/iio/proximity/lidar.c line 31

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/triggered_buffer.h>

put it in alphabetic order

drivers/iio/proximity/lidar.c line 34

Quoted 3 lines
drivers/iio/proximity/lidar.c new file +3 -0
+
+#define LIDAR_REG_CONTROL   0x00
+#define LIDAR_REG_CONTROL_ACQUIRE   BIT(2)

tab aligned

Quoted 47 lines
drivers/iio/proximity/lidar.c new file +47 -0
+
+#define LIDAR_REG_STATUS    0x01
+#define LIDAR_REG_STATUS_RDY        BIT(0)
+
+#define LIDAR_REG_DATA_HBYTE        0x0f
+#define LIDAR_REG_DATA_LBYTE        0x10
+#define LIDAR_REG_DATA_MAX  0xffff
+
+#define LIDAR_DRV_NAME      "lidar"
+
+struct lidar_data {
+    struct mutex lock;
+    struct iio_dev *indio_dev;
+    struct i2c_client *client;
+
+    /* config */
+    int calib_bias;
+
+    u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */
+};
+
+static const struct iio_chan_spec lidar_channels[] = {
+    {
+            .type = IIO_DISTANCE,
+            .info_mask_separate =
+                    BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_CALIBBIAS),
+            .scan_index = 0,
+            .scan_type = {
+                    .sign = 'u',
+                    .realbits = 16,
+                    .storagebits = 16,
+            },
+    },
+    IIO_CHAN_SOFT_TIMESTAMP(1),
+};
+
+static inline int lidar_read_byte(struct lidar_data *data, int reg)
+{
+    struct i2c_client *client = data->client;
+    int ret;
+
+    ret = i2c_smbus_write_byte(client, reg);
+    if (ret < 0) {
+            dev_err(&client->dev, "cannot write addr value");
+            return ret;
+    }
+

drivers/iio/proximity/lidar.c

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+    ret = i2c_smbus_read_byte(client);
+    if (ret < 0) {
+            dev_err(&client->dev, "cannot read data value");
+            return ret;
+    }
+
+    return ret;
+}
+
+static inline int lidar_write_control(struct lidar_data *data, int

val)

drivers/iio/proximity/lidar.c line 93

Quoted 2 lines
drivers/iio/proximity/lidar.c new file +2 -0
+{
+    return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL,

val);

Quoted 196 lines
drivers/iio/proximity/lidar.c new file +196 -0
+}
+
+static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
+{
+    int ret;
+    int val;
+
+    ret = lidar_read_byte(data, LIDAR_REG_DATA_HBYTE);
+    if (ret < 0)
+            return ret;
+    val = ret << 8;
+
+    ret = lidar_read_byte(data, LIDAR_REG_DATA_LBYTE);
+    if (ret < 0)
+            return ret;
+    val |= ret;
+
+    /* correct any possible overflow or underflow */
+    val += data->calib_bias / 10000;
+    if (val < 0)
+            val = 0;
+
+    if (val > LIDAR_REG_DATA_MAX)
+            val = LIDAR_REG_DATA_MAX;
+
+    *reg = val;
+
+    return 0;
+}
+
+static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
+{
+    struct i2c_client *client = data->client;
+    int tries = 10;
+    int ret;
+
+    /* start sample */
+    ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
+    if (ret < 0) {
+            dev_err(&client->dev, "cannot send start measurement command");
+            return ret;
+    }
+
+    while (tries--) {
+            usleep_range(1000, 2000);
+
+            ret = lidar_read_byte(data, LIDAR_REG_STATUS);
+            if (ret < 0)
+                    break;
+
+            /* sample ready to read */
+            if (!(ret & LIDAR_REG_STATUS_RDY)) {
+                    ret = lidar_read_measurement(data, reg);
+                    break;
+            }
+    }
+
+    return ret;
+}
+
+static int lidar_read_raw(struct iio_dev *indio_dev,
+                      struct iio_chan_spec const *chan,
+                      int *val, int *val2, long mask)
+{
+    struct lidar_data *data = iio_priv(indio_dev);
+    int ret = -EINVAL;
+
+    if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW)
+            return -EBUSY;
+
+    mutex_lock(&data->lock);
+
+    switch (mask) {
+    case IIO_CHAN_INFO_RAW: {
+            u16 reg;
+
+            ret = lidar_get_measurement(data, &reg);
+            if (!ret) {
+                    *val = reg / 100;
+                    *val2 = (reg % 100) * 10000;
+                    ret = IIO_VAL_INT_PLUS_MICRO;
+            }
+            break;
+    }
+    case IIO_CHAN_INFO_CALIBBIAS:
+            *val = 0;
+            *val2 = data->calib_bias;
+            ret = IIO_VAL_INT_PLUS_MICRO;
+            break;
+    }
+
+    mutex_unlock(&data->lock);
+
+    return ret;
+}
+
+static int lidar_write_raw(struct iio_dev *indio_dev,
+                       struct iio_chan_spec const *chan,
+                       int val, int val2, long mask)
+{
+    struct lidar_data *data = iio_priv(indio_dev);
+
+    if (mask == IIO_CHAN_INFO_CALIBBIAS) {
+            if (val != 0)
+                    return -EINVAL;
+
+            /* cm increments only */
+            if (val2 % 10000)
+                    return -EINVAL;
+
+            mutex_lock(&data->lock);
+            data->calib_bias = val2;
+            mutex_unlock(&data->lock);
+            return 0;
+    }
+
+    return -EINVAL;
+}
+
+static irqreturn_t lidar_trigger_handler(int irq, void *private)
+{
+    struct iio_poll_func *pf = private;
+    struct iio_dev *indio_dev = pf->indio_dev;
+    struct lidar_data *data = iio_priv(indio_dev);
+    int ret;
+
+    ret = lidar_get_measurement(data, &data->buffer[0]);
+    if (!ret) {
+            iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+                                               iio_get_time_ns());
+    } else {
+            dev_err(&data->client->dev, "cannot read LIDAR measurement");
+    }
+
+    iio_trigger_notify_done(indio_dev->trig);
+
+    return IRQ_HANDLED;
+}
+
+static struct iio_info lidar_info = {
+    .driver_module = THIS_MODULE,
+    .read_raw = lidar_read_raw,
+    .write_raw = lidar_write_raw,
+};
+
+static int lidar_probe(struct i2c_client *client,
+                   const struct i2c_device_id *id)
+{
+    struct lidar_data *data;
+    struct iio_dev *indio_dev;
+    int ret;
+
+    indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+    if (!indio_dev)
+            return -ENOMEM;
+
+    indio_dev->info = &lidar_info;
+    indio_dev->name = LIDAR_DRV_NAME;
+    indio_dev->channels = lidar_channels;
+    indio_dev->num_channels = ARRAY_SIZE(lidar_channels);
+    indio_dev->modes = INDIO_DIRECT_MODE;
+
+    data = iio_priv(indio_dev);
+    i2c_set_clientdata(client, indio_dev);
+
+    data->client = client;
+    data->indio_dev = indio_dev;
+    mutex_init(&data->lock);
+
+    ret = iio_triggered_buffer_setup(indio_dev, NULL,
+                                     lidar_trigger_handler, NULL);
+    if (ret)
+            return ret;
+
+    ret = iio_device_register(indio_dev);
+    if (ret)
+            goto error_unreg_buffer;
+
+    return 0;
+
+error_unreg_buffer:
+    iio_triggered_buffer_cleanup(indio_dev);
+
+    return ret;
+}
+
+static int lidar_remove(struct i2c_client *client)
+{
+    struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+    iio_device_unregister(indio_dev);
+    iio_triggered_buffer_cleanup(indio_dev);
+
+    return 0;
+}
+

drivers/iio/proximity/lidar.c line 299

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+static const struct i2c_device_id lidar_id[] = {
+    {"lidar", 0},
+    { },
+};
+MODULE_DEVICE_TABLE(i2c, lidar_id);
+
+static struct i2c_driver lidar_driver = {
+    .driver = {
+            .name   = LIDAR_DRV_NAME,
+            .owner  = THIS_MODULE,

the .owner field is not needed since it is overridden in i2c_register_driver

Quoted 15 lines

drivers/iio/proximity/lidar.c line 309

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+    },
+    .probe          = lidar_probe,
+    .remove         = lidar_remove,
+    .id_table       = lidar_id,
+};
+module_i2c_driver(lidar_driver);
+
+MODULE_AUTHOR("Matt Ranostay <[email protected]>");
+MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
+MODULE_LICENSE("GPL");

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

A message that is not in the archive (sent off-list or not yet received): CAKzfze9D6NYgJoT-271GUj1ei-PwUfHwJ38w8uMcgPoQz6yyUQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#11 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

Quoted 24 lines · Matt Ranostay
Quoted 22 lines · Jonathan Cameron
Quoted 19 lines · Vladimir Barinov

Hi Matt,

Find minor comments.

Quoted 2 lines · Matt Ranostay

Add support for the PulsedLight LIDAR rangefinder sensor which allows high speed (over 300Hz) distance measurements using Barker Coding

within

Quoted 3 lines

40 meter range.

Support only tested on the "blue label" rev 2, but may work using

polling

Quoted 7 lines

at low sample frequencies on the original version.

---
   drivers/iio/proximity/Kconfig  |  13 ++
   drivers/iio/proximity/Makefile |   1 +
   drivers/iio/proximity/lidar.c  | 309
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

+++++++++++++++++++++++++++++++++++++++++
Add ABI documentation Documentation/ABI/testing/sysfs-bus-iio-proximity-lidar

Why? Reading on phone so may have missed something but all ABI is standard. Covered by top level docs.

Only thing that would be documented that IIO_DISTANCE is in meters. But seems standard.

I understand.
I was thinking that reading in_distance_raw units needs to be docimented (looking on the sample sysfs-bus-iio-proximity-as3935)

Regards,
Vladimir

#12 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

In reply to a message not in the archive
Quoted 25 lines · Matt Ranostay
Quoted 23 lines · Jonathan Cameron
Quoted 19 lines · Vladimir Barinov

Hi Matt,

Find minor comments.

Quoted 2 lines · Matt Ranostay

Add support for the PulsedLight LIDAR rangefinder sensor which allows high speed (over 300Hz) distance measurements using Barker Coding

within

Quoted 3 lines

40 meter range.

Support only tested on the "blue label" rev 2, but may work using

polling

Quoted 7 lines

at low sample frequencies on the original version.

---
  drivers/iio/proximity/Kconfig  |  13 ++
  drivers/iio/proximity/Makefile |   1 +
  drivers/iio/proximity/lidar.c  | 309
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

+++++++++++++++++++++++++++++++++++++++++
Add ABI documentation Documentation/ABI/testing/sysfs-bus-iio-proximity-lidar

Why? Reading on phone so may have missed something but all ABI is standard. Covered by top level docs.

Only thing that would be documented that IIO_DISTANCE is in meters. But seems standard.

This is the perfect point to move that out to the top level docs (two drivers using it now).

Quoted 386 lines
Quoted 21 lines
Quoted 13 lines

Review

Quoted 5 lines
  3 files changed, 323 insertions(+)

create mode 100644 drivers/iio/proximity/lidar.c

(quoted diff) +0 -0

b/drivers/iio/proximity/Kconfig

Quoted 7 lines

index 41a8d8f..8111e11 100644

drivers/iio/proximity/Kconfig +0 -0
@@ -20,6 +20,19 @@ endmenu
 
  menu "Proximity sensors"
 
Quoted 8 lines

drivers/iio/proximity/Kconfig endmenu · line 27

Quoted 5 lines
drivers/iio/proximity/Kconfig +5 -0
+config LIDAR
+    tristate "PulsedLight LIDAR sensor"
+    select IIO_BUFFER
+    select IIO_TRIGGERED_BUFFER
+    select REGMAP_I2C

You did not use REGMAP in your driver.
Probably it would be better to use this API in your driver since the chip seems fully SMBUS compatible.

Quoted 2 lines

Unless there is a significant gain don't bother. Now if the device also has an spi bus or the driver does extensive caching it would be worthwhile.

Correctly, none of the the data is cacheable so regmap doesn't make sense here.

Quoted 353 lines
Quoted 337 lines

Review

Quoted 13 lines
drivers/iio/proximity/Kconfig +8 -0
+    depends on I2C
+    help
+      Say Y to build a driver for PulsedLight LIDAR range finding
+      sensor.
+
+      To compile this driver as a module, choose M here: the
+      module will be called lidar.
+
  config SX9500
     tristate "SX9500 Semtech proximity sensor"
     select IIO_BUFFER
(quoted diff) +0 -0

b/drivers/iio/proximity/Makefile

Review

Quoted 10 lines

index 9818dc5..36a95f3 100644

drivers/iio/proximity/Makefile +1 -0
@@ -4,4 +4,5 @@
 
  # When adding new entries keep the list in alphabetical order
  obj-$(CONFIG_AS3935)               += as3935.o
+obj-$(CONFIG_LIDAR)         += lidar.o
  obj-$(CONFIG_SX9500)               += sx9500.o
(quoted diff) +0 -0

b/drivers/iio/proximity/lidar.c

drivers/iio/proximity/lidar.c line 6

Quoted 11 lines

new file mode 100644
index 0000000..e56df58

drivers/iio/proximity/lidar.c new file +6 -0
@@ -0,0 +1,309 @@
+/*
+ * lidar.c - Support for PulsedLight LIDAR range finding sensor
+ *
+ * Copyright (C) 2015 Matt Ranostay <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or

modify

drivers/iio/proximity/lidar.c line 7

Quoted 1 line
drivers/iio/proximity/lidar.c new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 14 lines
drivers/iio/proximity/lidar.c new file +14 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * TODO: runtime pm, interrupt mode, and signal strength reporting
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>

drivers/iio/proximity/lidar.c line 31

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/triggered_buffer.h>

put it in alphabetic order

drivers/iio/proximity/lidar.c line 34

Quoted 3 lines
drivers/iio/proximity/lidar.c new file +3 -0
+
+#define LIDAR_REG_CONTROL   0x00
+#define LIDAR_REG_CONTROL_ACQUIRE   BIT(2)

tab aligned

Quoted 47 lines
drivers/iio/proximity/lidar.c new file +47 -0
+
+#define LIDAR_REG_STATUS    0x01
+#define LIDAR_REG_STATUS_RDY        BIT(0)
+
+#define LIDAR_REG_DATA_HBYTE        0x0f
+#define LIDAR_REG_DATA_LBYTE        0x10
+#define LIDAR_REG_DATA_MAX  0xffff
+
+#define LIDAR_DRV_NAME      "lidar"
+
+struct lidar_data {
+    struct mutex lock;
+    struct iio_dev *indio_dev;
+    struct i2c_client *client;
+
+    /* config */
+    int calib_bias;
+
+    u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */
+};
+
+static const struct iio_chan_spec lidar_channels[] = {
+    {
+            .type = IIO_DISTANCE,
+            .info_mask_separate =
+                    BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_CALIBBIAS),
+            .scan_index = 0,
+            .scan_type = {
+                    .sign = 'u',
+                    .realbits = 16,
+                    .storagebits = 16,
+            },
+    },
+    IIO_CHAN_SOFT_TIMESTAMP(1),
+};
+
+static inline int lidar_read_byte(struct lidar_data *data, int reg)
+{
+    struct i2c_client *client = data->client;
+    int ret;
+
+    ret = i2c_smbus_write_byte(client, reg);
+    if (ret < 0) {
+            dev_err(&client->dev, "cannot write addr value");
+            return ret;
+    }
+

drivers/iio/proximity/lidar.c line 91

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+    ret = i2c_smbus_read_byte(client);
+    if (ret < 0) {
+            dev_err(&client->dev, "cannot read data value");
+            return ret;
+    }
+
+    return ret;
+}
+
+static inline int lidar_write_control(struct lidar_data *data, int

val)

drivers/iio/proximity/lidar.c line 93

Quoted 2 lines
drivers/iio/proximity/lidar.c new file +2 -0
+{
+    return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL,

val);

Quoted 196 lines
drivers/iio/proximity/lidar.c new file +196 -0
+}
+
+static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
+{
+    int ret;
+    int val;
+
+    ret = lidar_read_byte(data, LIDAR_REG_DATA_HBYTE);
+    if (ret < 0)
+            return ret;
+    val = ret << 8;
+
+    ret = lidar_read_byte(data, LIDAR_REG_DATA_LBYTE);
+    if (ret < 0)
+            return ret;
+    val |= ret;
+
+    /* correct any possible overflow or underflow */
+    val += data->calib_bias / 10000;
+    if (val < 0)
+            val = 0;
+
+    if (val > LIDAR_REG_DATA_MAX)
+            val = LIDAR_REG_DATA_MAX;
+
+    *reg = val;
+
+    return 0;
+}
+
+static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
+{
+    struct i2c_client *client = data->client;
+    int tries = 10;
+    int ret;
+
+    /* start sample */
+    ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
+    if (ret < 0) {
+            dev_err(&client->dev, "cannot send start measurement command");
+            return ret;
+    }
+
+    while (tries--) {
+            usleep_range(1000, 2000);
+
+            ret = lidar_read_byte(data, LIDAR_REG_STATUS);
+            if (ret < 0)
+                    break;
+
+            /* sample ready to read */
+            if (!(ret & LIDAR_REG_STATUS_RDY)) {
+                    ret = lidar_read_measurement(data, reg);
+                    break;
+            }
+    }
+
+    return ret;
+}
+
+static int lidar_read_raw(struct iio_dev *indio_dev,
+                      struct iio_chan_spec const *chan,
+                      int *val, int *val2, long mask)
+{
+    struct lidar_data *data = iio_priv(indio_dev);
+    int ret = -EINVAL;
+
+    if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW)
+            return -EBUSY;
+
+    mutex_lock(&data->lock);
+
+    switch (mask) {
+    case IIO_CHAN_INFO_RAW: {
+            u16 reg;
+
+            ret = lidar_get_measurement(data, &reg);
+            if (!ret) {
+                    *val = reg / 100;
+                    *val2 = (reg % 100) * 10000;
+                    ret = IIO_VAL_INT_PLUS_MICRO;
+            }
+            break;
+    }
+    case IIO_CHAN_INFO_CALIBBIAS:
+            *val = 0;
+            *val2 = data->calib_bias;
+            ret = IIO_VAL_INT_PLUS_MICRO;
+            break;
+    }
+
+    mutex_unlock(&data->lock);
+
+    return ret;
+}
+
+static int lidar_write_raw(struct iio_dev *indio_dev,
+                       struct iio_chan_spec const *chan,
+                       int val, int val2, long mask)
+{
+    struct lidar_data *data = iio_priv(indio_dev);
+
+    if (mask == IIO_CHAN_INFO_CALIBBIAS) {
+            if (val != 0)
+                    return -EINVAL;
+
+            /* cm increments only */
+            if (val2 % 10000)
+                    return -EINVAL;
+
+            mutex_lock(&data->lock);
+            data->calib_bias = val2;
+            mutex_unlock(&data->lock);
+            return 0;
+    }
+
+    return -EINVAL;
+}
+
+static irqreturn_t lidar_trigger_handler(int irq, void *private)
+{
+    struct iio_poll_func *pf = private;
+    struct iio_dev *indio_dev = pf->indio_dev;
+    struct lidar_data *data = iio_priv(indio_dev);
+    int ret;
+
+    ret = lidar_get_measurement(data, &data->buffer[0]);
+    if (!ret) {
+            iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+                                               iio_get_time_ns());
+    } else {
+            dev_err(&data->client->dev, "cannot read LIDAR measurement");
+    }
+
+    iio_trigger_notify_done(indio_dev->trig);
+
+    return IRQ_HANDLED;
+}
+
+static struct iio_info lidar_info = {
+    .driver_module = THIS_MODULE,
+    .read_raw = lidar_read_raw,
+    .write_raw = lidar_write_raw,
+};
+
+static int lidar_probe(struct i2c_client *client,
+                   const struct i2c_device_id *id)
+{
+    struct lidar_data *data;
+    struct iio_dev *indio_dev;
+    int ret;
+
+    indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+    if (!indio_dev)
+            return -ENOMEM;
+
+    indio_dev->info = &lidar_info;
+    indio_dev->name = LIDAR_DRV_NAME;
+    indio_dev->channels = lidar_channels;
+    indio_dev->num_channels = ARRAY_SIZE(lidar_channels);
+    indio_dev->modes = INDIO_DIRECT_MODE;
+
+    data = iio_priv(indio_dev);
+    i2c_set_clientdata(client, indio_dev);
+
+    data->client = client;
+    data->indio_dev = indio_dev;
+    mutex_init(&data->lock);
+
+    ret = iio_triggered_buffer_setup(indio_dev, NULL,
+                                     lidar_trigger_handler, NULL);
+    if (ret)
+            return ret;
+
+    ret = iio_device_register(indio_dev);
+    if (ret)
+            goto error_unreg_buffer;
+
+    return 0;
+
+error_unreg_buffer:
+    iio_triggered_buffer_cleanup(indio_dev);
+
+    return ret;
+}
+
+static int lidar_remove(struct i2c_client *client)
+{
+    struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+    iio_device_unregister(indio_dev);
+    iio_triggered_buffer_cleanup(indio_dev);
+
+    return 0;
+}
+

drivers/iio/proximity/lidar.c line 299

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+static const struct i2c_device_id lidar_id[] = {
+    {"lidar", 0},
+    { },
+};
+MODULE_DEVICE_TABLE(i2c, lidar_id);
+
+static struct i2c_driver lidar_driver = {
+    .driver = {
+            .name   = LIDAR_DRV_NAME,
+            .owner  = THIS_MODULE,

the .owner field is not needed since it is overridden in i2c_register_driver

Quoted 15 lines

drivers/iio/proximity/lidar.c line 309

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+    },
+    .probe          = lidar_probe,
+    .remove         = lidar_remove,
+    .id_table       = lidar_id,
+};
+module_i2c_driver(lidar_driver);
+
+MODULE_AUTHOR("Matt Ranostay <[email protected]>");
+MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
+MODULE_LICENSE("GPL");

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 2 lines

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#13 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

In reply to a message not in the archive

On 08/01/2015 05:58 AM, Matt Ranostay wrote:
[...]

drivers/iio/proximity/lidar.c line 53

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+
+struct lidar_data {
+	struct mutex lock;
+	struct iio_dev *indio_dev;
+	struct i2c_client *client;
+
+	/* config */
+	int calib_bias;
+
+	u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */

Needs to be in its own cacheline to avoid issues if the I2C controller is using DMA.

	u16 buffer[5] ____cacheline_aligned;
Quoted 1 line

+};

[...]

Quoted 1 line

+static inline int lidar_read_byte(struct lidar_data *data, int reg)

I'd drop the inline. The compiler is smart enough to figure out whether it makes sense to inline it or not.

Quoted 5 lines
drivers/iio/proximity/lidar.c new file +5 -0
+{
+	struct i2c_client *client = data->client;
+	int ret;
+
+	ret = i2c_smbus_write_byte(client, reg);

drivers/iio/proximity/lidar.c line 86

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot write addr value");
+		return ret;
+	}
+
+	ret = i2c_smbus_read_byte(client);
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot read data value");
+		return ret;
+	}

Instead of using a write_byte/read_byte combination rather use i2c_smbus_read_byte_data(). I think that will do the same, but in one atomic operation.

Quoted 3 lines
drivers/iio/proximity/lidar.c new file +3 -0
+
+	return ret;
+}

[...]

Quoted 7 lines
drivers/iio/proximity/lidar.c new file +7 -0
+static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
+{
+	int ret;
+	int val;
+
+	ret = lidar_read_byte(data, LIDAR_REG_DATA_HBYTE);
+	if (ret < 0)

drivers/iio/proximity/lidar.c line 112

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+		return ret;
+	val = ret << 8;
+
+	ret = lidar_read_byte(data, LIDAR_REG_DATA_LBYTE);
+	if (ret < 0)
+		return ret;
+	val |= ret;
+
+	/* correct any possible overflow or underflow */
+	val += data->calib_bias / 10000;

Typically calib bias is only meant for a offset correction that is done in hardware rather than in software. What's the usecase for doing it in kernelspace rather than in userspace?

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+	if (val < 0)
+		val = 0;
+
+	if (val > LIDAR_REG_DATA_MAX)
+		val = LIDAR_REG_DATA_MAX;
+
+	*reg = val;
+
+	return 0;
+}

[...]

Quoted 11 lines
drivers/iio/proximity/lidar.c new file +11 -0
+static int lidar_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct lidar_data *data = iio_priv(indio_dev);
+	int ret = -EINVAL;
+
+	if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW)
+		return -EBUSY;
+
+	mutex_lock(&data->lock);

drivers/iio/proximity/lidar.c line 174

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		u16 reg;
+
+		ret = lidar_get_measurement(data, &reg);
+		if (!ret) {
+			*val = reg / 100;
+			*val2 = (reg % 100) * 10000;
+			ret = IIO_VAL_INT_PLUS_MICRO;

The raw attribute is supposed to return the raw value as a IIO_VAL_INT. A scale attribute should be used to indicate the conversion factor to get the proper unit.

Quoted 14 lines
drivers/iio/proximity/lidar.c new file +14 -0
+		}
+		break;
+	}
+	case IIO_CHAN_INFO_CALIBBIAS:
+		*val = 0;
+		*val2 = data->calib_bias;
+		ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
+	}
+
+	mutex_unlock(&data->lock);
+
+	return ret;
+}

[...]

Quoted 1 line

+static struct iio_info lidar_info = {

const

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+	.driver_module = THIS_MODULE,
+	.read_raw = lidar_read_raw,
+	.write_raw = lidar_write_raw,
+};

[...]

drivers/iio/proximity/lidar.c line 299

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+static struct i2c_driver lidar_driver = {
+	.driver = {
+		.name	= LIDAR_DRV_NAME,
+		.owner	= THIS_MODULE,

You added a DT vendor prefix, but there is no of match table for the driver.

Quoted 6 lines
drivers/iio/proximity/lidar.c new file +6 -0
+	},
+	.probe		= lidar_probe,
+	.remove		= lidar_remove,
+	.id_table	= lidar_id,
+};
+module_i2c_driver(lidar_driver);

A message that is not in the archive (sent off-list or not yet received): 55BDE5F8.9090201-Qo5EllUWu/[email protected]

#15 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

Quoted 15 lines · Lars-Peter Clausen

On 08/01/2015 05:58 AM, Matt Ranostay wrote:
[...]

drivers/iio/proximity/lidar.c line 53

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+
+struct lidar_data {
+	struct mutex lock;
+	struct iio_dev *indio_dev;
+	struct i2c_client *client;
+
+	/* config */
+	int calib_bias;
+
+	u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */

Needs to be in its own cacheline to avoid issues if the I2C controller is using DMA.

Would do if spi, but in the case of i2c I thought all bus drivers were obliged to deal with this rather than leaving it to the client drivers?

Has this changed?

Quoted 135 lines
	u16 buffer[5] ____cacheline_aligned;
Quoted 1 line

+};

[...]

Quoted 1 line

+static inline int lidar_read_byte(struct lidar_data *data, int reg)

I'd drop the inline. The compiler is smart enough to figure out whether it makes sense to inline it or not.

Quoted 5 lines
drivers/iio/proximity/lidar.c new file +5 -0
+{
+	struct i2c_client *client = data->client;
+	int ret;
+
+	ret = i2c_smbus_write_byte(client, reg);

drivers/iio/proximity/lidar.c line 86

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot write addr value");
+		return ret;
+	}
+
+	ret = i2c_smbus_read_byte(client);
+	if (ret < 0) {
+		dev_err(&client->dev, "cannot read data value");
+		return ret;
+	}

Instead of using a write_byte/read_byte combination rather use i2c_smbus_read_byte_data(). I think that will do the same, but in one atomic operation.

Quoted 3 lines
drivers/iio/proximity/lidar.c new file +3 -0
+
+	return ret;
+}

[...]

Quoted 7 lines
drivers/iio/proximity/lidar.c new file +7 -0
+static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
+{
+	int ret;
+	int val;
+
+	ret = lidar_read_byte(data, LIDAR_REG_DATA_HBYTE);
+	if (ret < 0)

drivers/iio/proximity/lidar.c line 112

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+		return ret;
+	val = ret << 8;
+
+	ret = lidar_read_byte(data, LIDAR_REG_DATA_LBYTE);
+	if (ret < 0)
+		return ret;
+	val |= ret;
+
+	/* correct any possible overflow or underflow */
+	val += data->calib_bias / 10000;

Typically calib bias is only meant for a offset correction that is done in hardware rather than in software. What's the usecase for doing it in kernelspace rather than in userspace?

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+	if (val < 0)
+		val = 0;
+
+	if (val > LIDAR_REG_DATA_MAX)
+		val = LIDAR_REG_DATA_MAX;
+
+	*reg = val;
+
+	return 0;
+}

[...]

Quoted 11 lines
drivers/iio/proximity/lidar.c new file +11 -0
+static int lidar_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct lidar_data *data = iio_priv(indio_dev);
+	int ret = -EINVAL;
+
+	if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW)
+		return -EBUSY;
+
+	mutex_lock(&data->lock);

drivers/iio/proximity/lidar.c line 174

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		u16 reg;
+
+		ret = lidar_get_measurement(data, &reg);
+		if (!ret) {
+			*val = reg / 100;
+			*val2 = (reg % 100) * 10000;
+			ret = IIO_VAL_INT_PLUS_MICRO;

The raw attribute is supposed to return the raw value as a IIO_VAL_INT. A scale attribute should be used to indicate the conversion factor to get the proper unit.

Quoted 14 lines
drivers/iio/proximity/lidar.c new file +14 -0
+		}
+		break;
+	}
+	case IIO_CHAN_INFO_CALIBBIAS:
+		*val = 0;
+		*val2 = data->calib_bias;
+		ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
+	}
+
+	mutex_unlock(&data->lock);
+
+	return ret;
+}

[...]

Quoted 1 line

+static struct iio_info lidar_info = {

const

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+	.driver_module = THIS_MODULE,
+	.read_raw = lidar_read_raw,
+	.write_raw = lidar_write_raw,
+};

[...]

drivers/iio/proximity/lidar.c line 299

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+static struct i2c_driver lidar_driver = {
+	.driver = {
+		.name	= LIDAR_DRV_NAME,
+		.owner	= THIS_MODULE,

You added a DT vendor prefix, but there is no of match table for the driver.

drivers/iio/proximity/lidar.c line 305

Quoted 6 lines
drivers/iio/proximity/lidar.c new file +6 -0
+	},
+	.probe		= lidar_probe,
+	.remove		= lidar_remove,
+	.id_table	= lidar_id,
+};
+module_i2c_driver(lidar_driver);

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#17 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

Quoted 16 lines · Jonathan Cameron
Quoted 15 lines · Lars-Peter Clausen

On 08/01/2015 05:58 AM, Matt Ranostay wrote:
[...]

drivers/iio/proximity/lidar.c line 53

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+
+struct lidar_data {
+	struct mutex lock;
+	struct iio_dev *indio_dev;
+	struct i2c_client *client;
+
+	/* config */
+	int calib_bias;
+
+	u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */

Needs to be in its own cacheline to avoid issues if the I2C controller is using DMA.

Quoted 2 lines

Would do if spi, but in the case of i2c I thought all bus drivers were obliged to deal with this rather than leaving it to the client drivers?

Where did you find this? There definitely seem to be I2C drivers which directly map the i2c_msg buffers.

But I just realized that in this case the buffer is not passed to the i2c driver anyway so we should be fine.

- Lars

A message that is not in the archive (sent off-list or not yet received): 55BE63F2.5080204-Qo5EllUWu/[email protected]

#19 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

Quoted 17 lines · Lars-Peter Clausen
Quoted 16 lines · Jonathan Cameron
Quoted 15 lines · Lars-Peter Clausen

On 08/01/2015 05:58 AM, Matt Ranostay wrote:
[...]

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+struct lidar_data {
+	struct mutex lock;
+	struct iio_dev *indio_dev;
+	struct i2c_client *client;
+
+	/* config */
+	int calib_bias;
+
+	u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */

Needs to be in its own cacheline to avoid issues if the I2C controller is using DMA.

Quoted 5 lines
Quoted 2 lines

Would do if spi, but in the case of i2c I thought all bus drivers were obliged to deal with this rather than leaving it to the client drivers?

Where did you find this? There definitely seem to be I2C drivers which directly map the i2c_msg buffers.

Right now I can't track down an explicit reference. However, all sorts of nastiness occurs if we end up using the i2c_smbus_xfer_emulated as that uses local buffers on the stack.

Hmm. Will look into this more when I get a chance. We have a lot of i2c drivers that don't have aligned buffers...

Quoted 11 lines

But I just realized that in this case the buffer is not passed to the i2c driver anyway so we should be fine.

- Lars

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#20 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

In reply to a message not in the archive
Quoted 15 lines · Lars-Peter Clausen

On 08/01/2015 05:58 AM, Matt Ranostay wrote:
[...]

drivers/iio/proximity/lidar.c line 53

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+
+struct lidar_data {
+     struct mutex lock;
+     struct iio_dev *indio_dev;
+     struct i2c_client *client;
+
+     /* config */
+     int calib_bias;
+
+     u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */

Needs to be in its own cacheline to avoid issues if the I2C controller is using DMA.

Ah though this was only needed for SPI.

Quoted 10 lines
        u16 buffer[5] ____cacheline_aligned;
Quoted 1 line

+};

[...]

Quoted 1 line

+static inline int lidar_read_byte(struct lidar_data *data, int reg)

I'd drop the inline. The compiler is smart enough to figure out whether it makes sense to inline it or not.

Got it.

Quoted 11 lines
Quoted 11 lines
drivers/iio/proximity/lidar.c new file +11 -0
+{
+     struct i2c_client *client = data->client;
+     int ret;
+
+     ret = i2c_smbus_write_byte(client, reg);
+     if (ret < 0) {
+             dev_err(&client->dev, "cannot write addr value");
+             return ret;
+     }
+
+     ret = i2c_smbus_read_byte(client);
Quoted 8 lines

drivers/iio/proximity/lidar.c line 86

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+     if (ret < 0) {
+             dev_err(&client->dev, "cannot read data value");
+             return ret;
+     }

Instead of using a write_byte/read_byte combination rather use i2c_smbus_read_byte_data(). I think that will do the same, but in one atomic operation.

Yes I would normally do that but this device doesn't seem to support that functionally, always returns zeros.

Quoted 18 lines
Quoted 4 lines
drivers/iio/proximity/lidar.c new file +3 -0
+
+     return ret;
+}

[...]

Quoted 13 lines
drivers/iio/proximity/lidar.c new file +13 -0
+static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
+{
+     int ret;
+     int val;
+
+     ret = lidar_read_byte(data, LIDAR_REG_DATA_HBYTE);
+     if (ret < 0)
+             return ret;
+     val = ret << 8;
+
+     ret = lidar_read_byte(data, LIDAR_REG_DATA_LBYTE);
+     if (ret < 0)
+             return ret;
Quoted 8 lines

drivers/iio/proximity/lidar.c line 112

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+     val |= ret;
+
+     /* correct any possible overflow or underflow */
+     val += data->calib_bias / 10000;

Typically calib bias is only meant for a offset correction that is done in hardware rather than in software. What's the usecase for doing it in kernelspace rather than in userspace?

There is hardware functionality for this but do does nothing more than add/subtraction to the final result.
Could just drop it completely.

Quoted 29 lines
Quoted 11 lines
drivers/iio/proximity/lidar.c new file +10 -0
+     if (val < 0)
+             val = 0;
+
+     if (val > LIDAR_REG_DATA_MAX)
+             val = LIDAR_REG_DATA_MAX;
+
+     *reg = val;
+
+     return 0;
+}

[...]

Quoted 17 lines
drivers/iio/proximity/lidar.c new file +17 -0
+static int lidar_read_raw(struct iio_dev *indio_dev,
+                       struct iio_chan_spec const *chan,
+                       int *val, int *val2, long mask)
+{
+     struct lidar_data *data = iio_priv(indio_dev);
+     int ret = -EINVAL;
+
+     if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW)
+             return -EBUSY;
+
+     mutex_lock(&data->lock);
+
+     switch (mask) {
+     case IIO_CHAN_INFO_RAW: {
+             u16 reg;
+
+             ret = lidar_get_measurement(data, &reg);
Quoted 8 lines

drivers/iio/proximity/lidar.c line 174

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+             if (!ret) {
+                     *val = reg / 100;
+                     *val2 = (reg % 100) * 10000;
+                     ret = IIO_VAL_INT_PLUS_MICRO;

The raw attribute is supposed to return the raw value as a IIO_VAL_INT. A scale attribute should be used to indicate the conversion factor to get the proper unit.

Ok makes sense.

Quoted 24 lines
Quoted 15 lines
drivers/iio/proximity/lidar.c new file +14 -0
+             }
+             break;
+     }
+     case IIO_CHAN_INFO_CALIBBIAS:
+             *val = 0;
+             *val2 = data->calib_bias;
+             ret = IIO_VAL_INT_PLUS_MICRO;
+             break;
+     }
+
+     mutex_unlock(&data->lock);
+
+     return ret;
+}

[...]

Quoted 1 line

+static struct iio_info lidar_info = {

const

drivers/iio/proximity/lidar.c line 237

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+     .driver_module = THIS_MODULE,
+     .read_raw = lidar_read_raw,
+     .write_raw = lidar_write_raw,
+};
Quoted 7 lines

[...]

drivers/iio/proximity/lidar.c line 299

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+static struct i2c_driver lidar_driver = {
+     .driver = {
+             .name   = LIDAR_DRV_NAME,
+             .owner  = THIS_MODULE,

You added a DT vendor prefix, but there is no of match table for the driver.

So without of_match_table it isn't needed to have a vendor id? "pulsedlight,lidar" maps to the i2c_device_id

Quoted 11 lines

drivers/iio/proximity/lidar.c line 305

Quoted 7 lines
drivers/iio/proximity/lidar.c new file +6 -0
+     },
+     .probe          = lidar_probe,
+     .remove         = lidar_remove,
+     .id_table       = lidar_id,
+};
+module_i2c_driver(lidar_driver);

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CAKzfze_ZNeSBo_hKn4ju1uNM4iyaFrvGo89Dv_80DK3Tfb24yg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#22 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

Quoted 17 lines · Matt Ranostay
Quoted 15 lines · Lars-Peter Clausen

On 08/01/2015 05:58 AM, Matt Ranostay wrote:
[...]

drivers/iio/proximity/lidar.c line 53

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+
+struct lidar_data {
+     struct mutex lock;
+     struct iio_dev *indio_dev;
+     struct i2c_client *client;
+
+     /* config */
+     int calib_bias;
+
+     u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */

Needs to be in its own cacheline to avoid issues if the I2C controller is using DMA.

Quoted 1 line

Ah though this was only needed for SPI.

At least some I2C master drivers directly use the buffer for DMA. But I was being stupid here anyway, you don't actually pass the buffer itself to the I2C transfer functions so everything is fine as it was.

Quoted 32 lines
Quoted 11 lines

        u16 buffer[5] ____cacheline_aligned;
Quoted 1 line

+};

[...]

Quoted 1 line

+static inline int lidar_read_byte(struct lidar_data *data, int reg)

I'd drop the inline. The compiler is smart enough to figure out whether it makes sense to inline it or not.

Got it.

Quoted 11 lines
Quoted 11 lines
drivers/iio/proximity/lidar.c new file +11 -0
+{
+     struct i2c_client *client = data->client;
+     int ret;
+
+     ret = i2c_smbus_write_byte(client, reg);
+     if (ret < 0) {
+             dev_err(&client->dev, "cannot write addr value");
+             return ret;
+     }
+
+     ret = i2c_smbus_read_byte(client);
Quoted 8 lines

drivers/iio/proximity/lidar.c line 86

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+     if (ret < 0) {
+             dev_err(&client->dev, "cannot read data value");
+             return ret;
+     }

Instead of using a write_byte/read_byte combination rather use i2c_smbus_read_byte_data(). I think that will do the same, but in one atomic operation.

Quoted 2 lines

Yes I would normally do that but this device doesn't seem to support that functionally, always returns zeros.

That's an interesting device. Maybe add a comment explaining the oddity. I'm sure I'm not the only one who'll wonder about this.

[...]

Quoted 9 lines
Quoted 6 lines

drivers/iio/proximity/lidar.c line 299

Quoted 4 lines
drivers/iio/proximity/lidar.c new file +4 -0
+static struct i2c_driver lidar_driver = {
+     .driver = {
+             .name   = LIDAR_DRV_NAME,
+             .owner  = THIS_MODULE,

You added a DT vendor prefix, but there is no of match table for the driver.

So without of_match_table it isn't needed to have a vendor id? "pulsedlight,lidar" maps to the i2c_device_id

I thinking the other way around. If you intend to instantiate the device via devicetree it is better to explicit add a of_device_id table rather than relying on the implicit mechanism that uses i2c_device_id.

You should also add an entry for the device to Documentation/devicetree/bindings/i2c/trivial-devices.txt

A message that is not in the archive (sent off-list or not yet received): 55BF1F9D.30207-Qo5EllUWu/[email protected]

#24 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

Quoted 18 lines · Lars-Peter Clausen
Quoted 17 lines · Matt Ranostay
Quoted 15 lines · Lars-Peter Clausen

On 08/01/2015 05:58 AM, Matt Ranostay wrote:
[...]

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+struct lidar_data {
+     struct mutex lock;
+     struct iio_dev *indio_dev;
+     struct i2c_client *client;
+
+     /* config */
+     int calib_bias;
+
+     u16 buffer[5]; /* 2 byte distance + 8 byte timestamp */

Needs to be in its own cacheline to avoid issues if the I2C controller is using DMA.

Quoted 5 lines
Quoted 1 line

Ah though this was only needed for SPI.

At least some I2C master drivers directly use the buffer for DMA. But I was being stupid here anyway, you don't actually pass the buffer itself to the I2C transfer functions so everything is fine as it was.

Will back that change out in v3 :).

Quoted 33 lines
Quoted 33 lines
Quoted 12 lines


        u16 buffer[5] ____cacheline_aligned;
Quoted 1 line

+};

[...]

Quoted 1 line

+static inline int lidar_read_byte(struct lidar_data *data, int reg)

I'd drop the inline. The compiler is smart enough to figure out whether it makes sense to inline it or not.

Got it.

Quoted 19 lines
Quoted 5 lines
(quoted diff) +5 -0
+{
+     struct i2c_client *client = data->client;
+     int ret;
+
+     ret = i2c_smbus_write_byte(client, reg);

Review

Quoted 10 lines
(quoted diff) +10 -0
+     if (ret < 0) {
+             dev_err(&client->dev, "cannot write addr value");
+             return ret;
+     }
+
+     ret = i2c_smbus_read_byte(client);
+     if (ret < 0) {
+             dev_err(&client->dev, "cannot read data value");
+             return ret;
+     }

Instead of using a write_byte/read_byte combination rather use i2c_smbus_read_byte_data(). I think that will do the same, but in one atomic operation.

Quoted 5 lines
Quoted 2 lines

Yes I would normally do that but this device doesn't seem to support that functionally, always returns zeros.

That's an interesting device. Maybe add a comment explaining the oddity. I'm sure I'm not the only one who'll wonder about this.

Ok will do. It seems to need a STOP condition between the read and write. Hence why i2c_smbus_read_byte_data doesn't work.

Quoted 12 lines

[...]

Quoted 9 lines
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+static struct i2c_driver lidar_driver = {
+     .driver = {
+             .name   = LIDAR_DRV_NAME,
+             .owner  = THIS_MODULE,

You added a DT vendor prefix, but there is no of match table for the driver.

So without of_match_table it isn't needed to have a vendor id? "pulsedlight,lidar" maps to the i2c_device_id

Quoted 6 lines

I thinking the other way around. If you intend to instantiate the device via devicetree it is better to explicit add a of_device_id table rather than relying on the implicit mechanism that uses i2c_device_id.

You should also add an entry for the device to Documentation/devicetree/bindings/i2c/trivial-devices.txt

Ok seems logical will do in v3.

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#25 lore raw

Re: [PATCH 2/2] iio: proximity: add support for PulsedLight LIDAR

In reply to a message not in the archive

drivers/iio/proximity/lidar.c line 162

Quoted 9 lines · Matt Ranostay
drivers/iio/proximity/lidar.c new file +9 -0
+static int lidar_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct lidar_data *data = iio_priv(indio_dev);
+	int ret = -EINVAL;
+
+	if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW)
+		return -EBUSY;

For this to work without race conditions you need to hold the indio_dev->mlock while doing the check and until the conversion is done.

Quoted 15 lines
drivers/iio/proximity/lidar.c new file +15 -0
+
+	mutex_lock(&data->lock);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		u16 reg;
+
+		ret = lidar_get_measurement(data, &reg);
+		if (!ret) {
+			*val = reg / 100;
+			*val2 = (reg % 100) * 10000;
+			ret = IIO_VAL_INT_PLUS_MICRO;
+		}
+		break;
+	}