2 * pulsedlight-lidar-lite-v2.c - Support for PulsedLight LIDAR sensor
4 * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * TODO: runtime pm, interrupt mode, and signal strength reporting
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
22 #include <linux/delay.h>
23 #include <linux/module.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/triggered_buffer.h>
29 #include <linux/iio/trigger_consumer.h>
31 #define LIDAR_REG_CONTROL 0x00
32 #define LIDAR_REG_CONTROL_ACQUIRE BIT(2)
34 #define LIDAR_REG_STATUS 0x01
35 #define LIDAR_REG_STATUS_INVALID BIT(3)
36 #define LIDAR_REG_STATUS_READY BIT(0)
38 #define LIDAR_REG_DATA_HBYTE 0x0f
39 #define LIDAR_REG_DATA_LBYTE 0x10
41 #define LIDAR_DRV_NAME "lidar"
44 struct iio_dev
*indio_dev
;
45 struct i2c_client
*client
;
47 u16 buffer
[8]; /* 2 byte distance + 8 byte timestamp */
50 static const struct iio_chan_spec lidar_channels
[] = {
54 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
62 IIO_CHAN_SOFT_TIMESTAMP(1),
65 static int lidar_read_byte(struct lidar_data
*data
, int reg
)
67 struct i2c_client
*client
= data
->client
;
71 * Device needs a STOP condition between address write, and data read
72 * so in turn i2c_smbus_read_byte_data cannot be used
75 ret
= i2c_smbus_write_byte(client
, reg
);
77 dev_err(&client
->dev
, "cannot write addr value");
81 ret
= i2c_smbus_read_byte(client
);
83 dev_err(&client
->dev
, "cannot read data value");
88 static inline int lidar_write_control(struct lidar_data
*data
, int val
)
90 return i2c_smbus_write_byte_data(data
->client
, LIDAR_REG_CONTROL
, val
);
93 static int lidar_read_measurement(struct lidar_data
*data
, u16
*reg
)
98 ret
= lidar_read_byte(data
, LIDAR_REG_DATA_HBYTE
);
103 ret
= lidar_read_byte(data
, LIDAR_REG_DATA_LBYTE
);
113 static int lidar_get_measurement(struct lidar_data
*data
, u16
*reg
)
115 struct i2c_client
*client
= data
->client
;
120 ret
= lidar_write_control(data
, LIDAR_REG_CONTROL_ACQUIRE
);
122 dev_err(&client
->dev
, "cannot send start measurement command");
127 usleep_range(1000, 2000);
129 ret
= lidar_read_byte(data
, LIDAR_REG_STATUS
);
133 /* return 0 since laser is likely pointed out of range */
134 if (ret
& LIDAR_REG_STATUS_INVALID
) {
140 /* sample ready to read */
141 if (!(ret
& LIDAR_REG_STATUS_READY
)) {
142 ret
= lidar_read_measurement(data
, reg
);
151 static int lidar_read_raw(struct iio_dev
*indio_dev
,
152 struct iio_chan_spec
const *chan
,
153 int *val
, int *val2
, long mask
)
155 struct lidar_data
*data
= iio_priv(indio_dev
);
158 mutex_lock(&indio_dev
->mlock
);
160 if (iio_buffer_enabled(indio_dev
) && mask
== IIO_CHAN_INFO_RAW
) {
166 case IIO_CHAN_INFO_RAW
: {
169 ret
= lidar_get_measurement(data
, ®
);
176 case IIO_CHAN_INFO_SCALE
:
179 ret
= IIO_VAL_INT_PLUS_MICRO
;
184 mutex_unlock(&indio_dev
->mlock
);
189 static irqreturn_t
lidar_trigger_handler(int irq
, void *private)
191 struct iio_poll_func
*pf
= private;
192 struct iio_dev
*indio_dev
= pf
->indio_dev
;
193 struct lidar_data
*data
= iio_priv(indio_dev
);
196 ret
= lidar_get_measurement(data
, data
->buffer
);
198 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
201 dev_err(&data
->client
->dev
, "cannot read LIDAR measurement");
204 iio_trigger_notify_done(indio_dev
->trig
);
209 static const struct iio_info lidar_info
= {
210 .driver_module
= THIS_MODULE
,
211 .read_raw
= lidar_read_raw
,
214 static int lidar_probe(struct i2c_client
*client
,
215 const struct i2c_device_id
*id
)
217 struct lidar_data
*data
;
218 struct iio_dev
*indio_dev
;
221 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
225 indio_dev
->info
= &lidar_info
;
226 indio_dev
->name
= LIDAR_DRV_NAME
;
227 indio_dev
->channels
= lidar_channels
;
228 indio_dev
->num_channels
= ARRAY_SIZE(lidar_channels
);
229 indio_dev
->modes
= INDIO_DIRECT_MODE
;
231 data
= iio_priv(indio_dev
);
232 i2c_set_clientdata(client
, indio_dev
);
234 data
->client
= client
;
235 data
->indio_dev
= indio_dev
;
237 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
238 lidar_trigger_handler
, NULL
);
242 ret
= iio_device_register(indio_dev
);
244 goto error_unreg_buffer
;
249 iio_triggered_buffer_cleanup(indio_dev
);
254 static int lidar_remove(struct i2c_client
*client
)
256 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
258 iio_device_unregister(indio_dev
);
259 iio_triggered_buffer_cleanup(indio_dev
);
264 static const struct i2c_device_id lidar_id
[] = {
265 {"lidar-lite-v2", 0},
268 MODULE_DEVICE_TABLE(i2c
, lidar_id
);
270 static const struct of_device_id lidar_dt_ids
[] = {
271 { .compatible
= "pulsedlight,lidar-lite-v2" },
274 MODULE_DEVICE_TABLE(of
, lidar_dt_ids
);
276 static struct i2c_driver lidar_driver
= {
278 .name
= LIDAR_DRV_NAME
,
279 .of_match_table
= of_match_ptr(lidar_dt_ids
),
281 .probe
= lidar_probe
,
282 .remove
= lidar_remove
,
283 .id_table
= lidar_id
,
285 module_i2c_driver(lidar_driver
);
287 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
288 MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
289 MODULE_LICENSE("GPL");