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: 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/pm_runtime.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/iio/trigger_consumer.h>
32 #define LIDAR_REG_CONTROL 0x00
33 #define LIDAR_REG_CONTROL_ACQUIRE BIT(2)
35 #define LIDAR_REG_STATUS 0x01
36 #define LIDAR_REG_STATUS_INVALID BIT(3)
37 #define LIDAR_REG_STATUS_READY BIT(0)
39 #define LIDAR_REG_DATA_HBYTE 0x0f
40 #define LIDAR_REG_DATA_LBYTE 0x10
41 #define LIDAR_REG_DATA_WORD_READ BIT(7)
43 #define LIDAR_REG_PWR_CONTROL 0x65
45 #define LIDAR_DRV_NAME "lidar"
48 struct iio_dev
*indio_dev
;
49 struct i2c_client
*client
;
51 int (*xfer
)(struct lidar_data
*data
, u8 reg
, u8
*val
, int len
);
54 u16 buffer
[8]; /* 2 byte distance + 8 byte timestamp */
57 static const struct iio_chan_spec lidar_channels
[] = {
61 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
69 IIO_CHAN_SOFT_TIMESTAMP(1),
72 static int lidar_i2c_xfer(struct lidar_data
*data
, u8 reg
, u8
*val
, int len
)
74 struct i2c_client
*client
= data
->client
;
75 struct i2c_msg msg
[2];
78 msg
[0].addr
= client
->addr
;
79 msg
[0].flags
= client
->flags
| I2C_M_STOP
;
81 msg
[0].buf
= (char *) ®
;
83 msg
[1].addr
= client
->addr
;
84 msg
[1].flags
= client
->flags
| I2C_M_RD
;
86 msg
[1].buf
= (char *) val
;
88 ret
= i2c_transfer(client
->adapter
, msg
, 2);
90 return (ret
== 2) ? 0 : -EIO
;
93 static int lidar_smbus_xfer(struct lidar_data
*data
, u8 reg
, u8
*val
, int len
)
95 struct i2c_client
*client
= data
->client
;
99 * Device needs a STOP condition between address write, and data read
100 * so in turn i2c_smbus_read_byte_data cannot be used
104 ret
= i2c_smbus_write_byte(client
, reg
++);
106 dev_err(&client
->dev
, "cannot write addr value");
110 ret
= i2c_smbus_read_byte(client
);
112 dev_err(&client
->dev
, "cannot read data value");
122 static int lidar_read_byte(struct lidar_data
*data
, u8 reg
)
127 ret
= data
->xfer(data
, reg
, &val
, 1);
134 static inline int lidar_write_control(struct lidar_data
*data
, int val
)
136 return i2c_smbus_write_byte_data(data
->client
, LIDAR_REG_CONTROL
, val
);
139 static inline int lidar_write_power(struct lidar_data
*data
, int val
)
141 return i2c_smbus_write_byte_data(data
->client
,
142 LIDAR_REG_PWR_CONTROL
, val
);
145 static int lidar_read_measurement(struct lidar_data
*data
, u16
*reg
)
147 int ret
= data
->xfer(data
, LIDAR_REG_DATA_HBYTE
|
148 (data
->i2c_enabled
? LIDAR_REG_DATA_WORD_READ
: 0),
152 *reg
= be16_to_cpu(*reg
);
157 static int lidar_get_measurement(struct lidar_data
*data
, u16
*reg
)
159 struct i2c_client
*client
= data
->client
;
163 pm_runtime_get_sync(&client
->dev
);
166 ret
= lidar_write_control(data
, LIDAR_REG_CONTROL_ACQUIRE
);
168 dev_err(&client
->dev
, "cannot send start measurement command");
173 usleep_range(1000, 2000);
175 ret
= lidar_read_byte(data
, LIDAR_REG_STATUS
);
179 /* return -EINVAL since laser is likely pointed out of range */
180 if (ret
& LIDAR_REG_STATUS_INVALID
) {
186 /* sample ready to read */
187 if (!(ret
& LIDAR_REG_STATUS_READY
)) {
188 ret
= lidar_read_measurement(data
, reg
);
193 pm_runtime_mark_last_busy(&client
->dev
);
194 pm_runtime_put_autosuspend(&client
->dev
);
199 static int lidar_read_raw(struct iio_dev
*indio_dev
,
200 struct iio_chan_spec
const *chan
,
201 int *val
, int *val2
, long mask
)
203 struct lidar_data
*data
= iio_priv(indio_dev
);
207 case IIO_CHAN_INFO_RAW
: {
210 if (iio_device_claim_direct_mode(indio_dev
))
213 ret
= lidar_get_measurement(data
, ®
);
218 iio_device_release_direct_mode(indio_dev
);
221 case IIO_CHAN_INFO_SCALE
:
224 ret
= IIO_VAL_INT_PLUS_MICRO
;
231 static irqreturn_t
lidar_trigger_handler(int irq
, void *private)
233 struct iio_poll_func
*pf
= private;
234 struct iio_dev
*indio_dev
= pf
->indio_dev
;
235 struct lidar_data
*data
= iio_priv(indio_dev
);
238 ret
= lidar_get_measurement(data
, data
->buffer
);
240 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
241 iio_get_time_ns(indio_dev
));
242 } else if (ret
!= -EINVAL
) {
243 dev_err(&data
->client
->dev
, "cannot read LIDAR measurement");
246 iio_trigger_notify_done(indio_dev
->trig
);
251 static const struct iio_info lidar_info
= {
252 .driver_module
= THIS_MODULE
,
253 .read_raw
= lidar_read_raw
,
256 static int lidar_probe(struct i2c_client
*client
,
257 const struct i2c_device_id
*id
)
259 struct lidar_data
*data
;
260 struct iio_dev
*indio_dev
;
263 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
266 data
= iio_priv(indio_dev
);
268 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
269 data
->xfer
= lidar_i2c_xfer
;
270 data
->i2c_enabled
= 1;
271 } else if (i2c_check_functionality(client
->adapter
,
272 I2C_FUNC_SMBUS_WORD_DATA
| I2C_FUNC_SMBUS_BYTE
))
273 data
->xfer
= lidar_smbus_xfer
;
277 indio_dev
->info
= &lidar_info
;
278 indio_dev
->name
= LIDAR_DRV_NAME
;
279 indio_dev
->channels
= lidar_channels
;
280 indio_dev
->num_channels
= ARRAY_SIZE(lidar_channels
);
281 indio_dev
->modes
= INDIO_DIRECT_MODE
;
283 i2c_set_clientdata(client
, indio_dev
);
285 data
->client
= client
;
286 data
->indio_dev
= indio_dev
;
288 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
289 lidar_trigger_handler
, NULL
);
293 ret
= iio_device_register(indio_dev
);
295 goto error_unreg_buffer
;
297 pm_runtime_set_autosuspend_delay(&client
->dev
, 1000);
298 pm_runtime_use_autosuspend(&client
->dev
);
300 ret
= pm_runtime_set_active(&client
->dev
);
302 goto error_unreg_buffer
;
303 pm_runtime_enable(&client
->dev
);
305 pm_runtime_mark_last_busy(&client
->dev
);
306 pm_runtime_idle(&client
->dev
);
311 iio_triggered_buffer_cleanup(indio_dev
);
316 static int lidar_remove(struct i2c_client
*client
)
318 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
320 iio_device_unregister(indio_dev
);
321 iio_triggered_buffer_cleanup(indio_dev
);
323 pm_runtime_disable(&client
->dev
);
324 pm_runtime_set_suspended(&client
->dev
);
329 static const struct i2c_device_id lidar_id
[] = {
330 {"lidar-lite-v2", 0},
333 MODULE_DEVICE_TABLE(i2c
, lidar_id
);
335 static const struct of_device_id lidar_dt_ids
[] = {
336 { .compatible
= "pulsedlight,lidar-lite-v2" },
339 MODULE_DEVICE_TABLE(of
, lidar_dt_ids
);
342 static int lidar_pm_runtime_suspend(struct device
*dev
)
344 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
345 struct lidar_data
*data
= iio_priv(indio_dev
);
347 return lidar_write_power(data
, 0x0f);
350 static int lidar_pm_runtime_resume(struct device
*dev
)
352 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
353 struct lidar_data
*data
= iio_priv(indio_dev
);
354 int ret
= lidar_write_power(data
, 0);
356 /* regulator and FPGA needs settling time */
357 usleep_range(15000, 20000);
363 static const struct dev_pm_ops lidar_pm_ops
= {
364 SET_RUNTIME_PM_OPS(lidar_pm_runtime_suspend
,
365 lidar_pm_runtime_resume
, NULL
)
368 static struct i2c_driver lidar_driver
= {
370 .name
= LIDAR_DRV_NAME
,
371 .of_match_table
= of_match_ptr(lidar_dt_ids
),
374 .probe
= lidar_probe
,
375 .remove
= lidar_remove
,
376 .id_table
= lidar_id
,
378 module_i2c_driver(lidar_driver
);
380 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
381 MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
382 MODULE_LICENSE("GPL");