Skip to contentSkip to conversation

iio: proximity: add PulsedLight LIDAR sensor support

PATCH Superseded Matt Ranostay 2 participants 5 messages Aug 5, 2015 – Aug 12, 2015 3 messages not in the archive

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover iio: proximity: add PulsedLight LIDAR sensor support

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.

Changes from v3:
* removed rogue stray newline
* made buffer parameter 16 bytes to fit alignment

Matt Ranostay (2):
devicetree: add PulsedLight vendor + device docs
iio: proximity: add support for PulsedLight LIDAR

 .../devicetree/bindings/i2c/trivial-devices.txt    |   1 +
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 drivers/iio/proximity/Kconfig                      |  12 +
 drivers/iio/proximity/Makefile                     |   1 +
 drivers/iio/proximity/lidar.c                      | 294 +++++++++++++++++++++
 5 files changed, 309 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): 1438749676-28781-1-git-send-email-mranostay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH v4 1/2 devicetree: add PulsedLight vendor + device docs

Add pulsedlight vendor to vendor-prefixes.txt, and LIDAR device documentation to trivial-devices.txt

---
 Documentation/devicetree/bindings/i2c/trivial-devices.txt | 1 +
 Documentation/devicetree/bindings/vendor-prefixes.txt     | 1 +
 2 files changed, 2 insertions(+)
Documentation/devicetree/bindings/i2c/trivial-devices.txt +1 -0
index d77d412..9d0e781 100644
@@ -80,6 +80,7 @@ oki,ml86v7667		OKI ML86V7667 video decoder
 ovti,ov5642		OV5642: Color CMOS QSXGA (5-megapixel) Image Sensor with OmniBSI and Embedded TrueFocus
 pericom,pt7c4338	Real-time Clock Module
 plx,pex8648		48-Lane, 12-Port PCI Express Gen 2 (5.0 GT/s) Switch
+pulsedlight,lidar	Pulsedlight LIDAR range-finding sensor
 ramtron,24c64		i2c serial eeprom  (24cxx)
 ricoh,r2025sd		I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
 ricoh,r2221tl		I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 6284568..0f318c3 100644
@@ -164,6 +164,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 · 1 line
1.9.1
#3 lore raw

PATCH v4 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 low sample frequencies on the original version.

---
 drivers/iio/proximity/Kconfig  |  12 ++
 drivers/iio/proximity/Makefile |   1 +
 drivers/iio/proximity/lidar.c  | 294 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 307 insertions(+)
 create mode 100644 drivers/iio/proximity/lidar.c
drivers/iio/proximity/Kconfig +12 -0
index 41a8d8f..e64fc5b 100644
@@ -20,6 +20,18 @@ endmenu
 
 menu "Proximity sensors"
 
+config LIDAR
+	tristate "PulsedLight LIDAR sensor"
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	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 +294 -0

294 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): 1438749676-28781-3-git-send-email-mranostay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#5 lore raw

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

Quoted 8 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.

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

Couple of bits inline. Only one that really matters is I suspect that simply calling it lidar (and naming the file lidar.c) is going to give us problems if they or someone else produces a similar lidar device in future.

Perhaps file name as pulsedlight-lidar-lite-v2.c
and name as lidar-lite-v2 for device tree etc.

Otherwise, nice little driver for a nice looking bit of kit.

Thanks,

Jonathan

Quoted 118 lines
---
 drivers/iio/proximity/Kconfig  |  12 ++
 drivers/iio/proximity/Makefile |   1 +
 drivers/iio/proximity/lidar.c  | 294 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 307 insertions(+)
 create mode 100644 drivers/iio/proximity/lidar.c
drivers/iio/proximity/Kconfig +12 -0
index 41a8d8f..e64fc5b 100644
@@ -20,6 +20,18 @@ endmenu
 
 menu "Proximity sensors"
 
+config LIDAR
+	tristate "PulsedLight LIDAR sensor"
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	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 +72 -0
new file mode 100644
index 0000000..fdec9a2
@@ -0,0 +1,294 @@
+/*
+ * 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/err.h>
+#include <linux/init.h>
+#include <linux/i2c.h>
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/module.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/iio/trigger_consumer.h>
+
+#define LIDAR_REG_CONTROL		0x00
+#define LIDAR_REG_CONTROL_ACQUIRE	BIT(2)
+
+#define LIDAR_REG_STATUS		0x01
+#define LIDAR_REG_STATUS_INVALID	BIT(3)
+#define LIDAR_REG_STATUS_READY		BIT(0)
+
+#define LIDAR_REG_DATA_HBYTE	0x0f
+#define LIDAR_REG_DATA_LBYTE	0x10
+
+#define LIDAR_DRV_NAME "lidar"
+
+struct lidar_data {
+	struct mutex lock;
+	struct iio_dev *indio_dev;
+	struct i2c_client *client;
+
+	u16 buffer[8]; /* 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_SCALE),
+		.scan_index = 0,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 16,
+			.storagebits = 16,
+		},
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1),
+};
+
+static int lidar_read_byte(struct lidar_data *data, int reg)
+{
+	struct i2c_client *client = data->client;
+	int ret;
+

drivers/iio/proximity/lidar.c line 82

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+	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;

Drop this return ret as otherwise various static checkers will moan at me (falling through gives the same result)

Quoted 8 lines
drivers/iio/proximity/lidar.c new file +8 -0
+	}
+
+	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);

drivers/iio/proximity/lidar.c line 100

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+}
+
+static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
+{
+	int ret;
+	int val;
+
+	/*
+	 * Device needs a STOP condition between address write, and data read
+	 * so in turn i2c_smbus_read_byte_data cannot be used

Good documentation of this. However, I'd move it to inside the read_byte function above rather than here.

Quoted 91 lines
drivers/iio/proximity/lidar.c new file +91 -0
+	 */
+
+	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;
+	*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;
+
+		/* return 0 since laser is likely pointed out of range */
+		if (ret & LIDAR_REG_STATUS_INVALID) {
+			*reg = 0;
+			ret = 0;
+			break;
+		}
+
+		/* sample ready to read */
+		if (!(ret & LIDAR_REG_STATUS_READY)) {
+			ret = lidar_read_measurement(data, reg);
+			break;
+		}
+		ret = -EIO;
+	}
+
+	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;
+
+	mutex_lock(&data->lock);
+
+	if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW) {
+		ret = -EBUSY;
+		goto error_busy;
+	}
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		u16 reg;
+
+		ret = lidar_get_measurement(data, &reg);
+		if (!ret) {
+			*val = reg;
+			ret = IIO_VAL_INT;
+		}
+		break;
+	}
+	case IIO_CHAN_INFO_SCALE:
+		*val = 0;
+		*val2 = 10000;
+		ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
+	}
+
+error_busy:
+	mutex_unlock(&data->lock);
+
+	return ret;

drivers/iio/proximity/lidar.c line 201

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+}
+
+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]);

data->buffer should be the same pointer unless I'm going crazy (always a posibility)

Quoted 66 lines
drivers/iio/proximity/lidar.c new file +66 -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 const struct iio_info lidar_info = {
+	.driver_module = THIS_MODULE,
+	.read_raw = lidar_read_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 277

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 const struct of_device_id lidar_dt_ids[] = {
+	{ .compatible = "pulsedlight,lidar" },

Not a more descriptive name? Website suggests
things like
lidar-lite-v2

I just worry they'll come out with a completely incompatible interface to some new unit that is also a lidar and we'll be forever confused!

Quoted 18 lines
drivers/iio/proximity/lidar.c new file +17 -0
+	{ }
+};
+
+static struct i2c_driver lidar_driver = {
+	.driver = {
+		.name	= LIDAR_DRV_NAME,
+		.of_match_table	= of_match_ptr(lidar_dt_ids),
+	},
+	.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");

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

#7 lore raw

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

Quoted 8 lines · Jonathan Cameron
Quoted 7 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.

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

Quoted 8 lines
Quoted 1 line

Couple of bits inline. Only one that really matters is I suspect that simply calling it lidar (and naming the file lidar.c) is going to give us problems if they or someone else produces a similar lidar device in future.

Perhaps file name as pulsedlight-lidar-lite-v2.c
and name as lidar-lite-v2 for device tree etc.

Got it. Pretty 80% the v1 will suck at anything over 10 Hz... however the v2 I have running at 120Hz with high correlation settings possible (default) which is required for a 3d scan of a room/artifact :).

Quoted 128 lines

Otherwise, nice little driver for a nice looking bit of kit.

Thanks,

Jonathan

Quoted 122 lines
---
 drivers/iio/proximity/Kconfig  |  12 ++
 drivers/iio/proximity/Makefile |   1 +
 drivers/iio/proximity/lidar.c  | 294 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 307 insertions(+)
 create mode 100644 drivers/iio/proximity/lidar.c
drivers/iio/proximity/Kconfig +12 -0
index 41a8d8f..e64fc5b 100644
@@ -20,6 +20,18 @@ endmenu
 
 menu "Proximity sensors"
 
+config LIDAR
+     tristate "PulsedLight LIDAR sensor"
+     select IIO_BUFFER
+     select IIO_TRIGGERED_BUFFER
+     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 +76 -0
new file mode 100644
index 0000000..fdec9a2
@@ -0,0 +1,294 @@
+/*
+ * 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/err.h>
+#include <linux/init.h>
+#include <linux/i2c.h>
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/module.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/iio/trigger_consumer.h>
+
+#define LIDAR_REG_CONTROL            0x00
+#define LIDAR_REG_CONTROL_ACQUIRE    BIT(2)
+
+#define LIDAR_REG_STATUS             0x01
+#define LIDAR_REG_STATUS_INVALID     BIT(3)
+#define LIDAR_REG_STATUS_READY               BIT(0)
+
+#define LIDAR_REG_DATA_HBYTE 0x0f
+#define LIDAR_REG_DATA_LBYTE 0x10
+
+#define LIDAR_DRV_NAME "lidar"
+
+struct lidar_data {
+     struct mutex lock;
+     struct iio_dev *indio_dev;
+     struct i2c_client *client;
+
+     u16 buffer[8]; /* 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_SCALE),
+             .scan_index = 0,
+             .scan_type = {
+                     .sign = 'u',
+                     .realbits = 16,
+                     .storagebits = 16,
+             },
+     },
+     IIO_CHAN_SOFT_TIMESTAMP(1),
+};
+
+static 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;
Quoted 8 lines

drivers/iio/proximity/lidar.c line 82

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

Drop this return ret as otherwise various static checkers will moan at me (falling through gives the same result)

Ok seems pedantic but meh, will drop :)

Quoted 115 lines
Quoted 8 lines
drivers/iio/proximity/lidar.c new file +8 -0
+     }
+
+     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);

drivers/iio/proximity/lidar.c line 100

Quoted 10 lines
drivers/iio/proximity/lidar.c new file +10 -0
+}
+
+static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
+{
+     int ret;
+     int val;
+
+     /*
+      * Device needs a STOP condition between address write, and data read
+      * so in turn i2c_smbus_read_byte_data cannot be used

Good documentation of this. However, I'd move it to inside the read_byte function above rather than here.

Quoted 95 lines
drivers/iio/proximity/lidar.c new file +95 -0
+      */
+
+     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;
+     *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;
+
+             /* return 0 since laser is likely pointed out of range */
+             if (ret & LIDAR_REG_STATUS_INVALID) {
+                     *reg = 0;
+                     ret = 0;
+                     break;
+             }
+
+             /* sample ready to read */
+             if (!(ret & LIDAR_REG_STATUS_READY)) {
+                     ret = lidar_read_measurement(data, reg);
+                     break;
+             }
+             ret = -EIO;
+     }
+
+     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;
+
+     mutex_lock(&data->lock);
+
+     if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW) {
+             ret = -EBUSY;
+             goto error_busy;
+     }
+
+     switch (mask) {
+     case IIO_CHAN_INFO_RAW: {
+             u16 reg;
+
+             ret = lidar_get_measurement(data, &reg);
+             if (!ret) {
+                     *val = reg;
+                     ret = IIO_VAL_INT;
+             }
+             break;
+     }
+     case IIO_CHAN_INFO_SCALE:
+             *val = 0;
+             *val2 = 10000;
+             ret = IIO_VAL_INT_PLUS_MICRO;
+             break;
+     }
+
+error_busy:
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+static irqreturn_t lidar_trigger_handler(int irq, void *private)
+{
Quoted 8 lines

drivers/iio/proximity/lidar.c line 201

Quoted 6 lines
drivers/iio/proximity/lidar.c new file +6 -0
+     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]);

data->buffer should be the same pointer unless I'm going crazy (always a posibility)

Yeah it would be.. this is an artifact when I didn't have the buffer size aligned correctly. Will fix.

Quoted 81 lines
Quoted 66 lines
drivers/iio/proximity/lidar.c new file +66 -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 const struct iio_info lidar_info = {
+     .driver_module = THIS_MODULE,
+     .read_raw = lidar_read_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 277

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 const struct of_device_id lidar_dt_ids[] = {
+     { .compatible = "pulsedlight,lidar" },

Not a more descriptive name? Website suggests
things like
lidar-lite-v2

Quoted 3 lines

I just worry they'll come out with a completely incompatible interface to some new unit that is also a lidar and we'll be forever confused!

Good point noted.

Quoted 20 lines
Quoted 19 lines
drivers/iio/proximity/lidar.c new file +17 -0
+     { }
+};
+
+static struct i2c_driver lidar_driver = {
+     .driver = {
+             .name   = LIDAR_DRV_NAME,
+             .of_match_table = of_match_ptr(lidar_dt_ids),
+     },
+     .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");