1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * pulsedlight-lidar-lite-v2.c - Support for PulsedLight LIDAR sensor
4  *
5  * Copyright (C) 2015, 2017-2018
6  * Author: Matt Ranostay <matt.ranostay@konsulko.com>
7  *
8  * TODO: interrupt mode, and signal strength reporting
9  */
10 
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/iio/trigger_consumer.h>
24 
25 #define LIDAR_REG_CONTROL		0x00
26 #define LIDAR_REG_CONTROL_ACQUIRE	BIT(2)
27 
28 #define LIDAR_REG_STATUS		0x01
29 #define LIDAR_REG_STATUS_INVALID	BIT(3)
30 #define LIDAR_REG_STATUS_READY		BIT(0)
31 
32 #define LIDAR_REG_DATA_HBYTE		0x0f
33 #define LIDAR_REG_DATA_LBYTE		0x10
34 #define LIDAR_REG_DATA_WORD_READ	BIT(7)
35 
36 #define LIDAR_REG_PWR_CONTROL	0x65
37 
38 #define LIDAR_DRV_NAME "lidar"
39 
40 struct lidar_data {
41 	struct iio_dev *indio_dev;
42 	struct i2c_client *client;
43 
44 	int (*xfer)(struct lidar_data *data, u8 reg, u8 *val, int len);
45 	int i2c_enabled;
46 
47 	u16 buffer[8]; /* 2 byte distance + 8 byte timestamp */
48 };
49 
50 static const struct iio_chan_spec lidar_channels[] = {
51 	{
52 		.type = IIO_DISTANCE,
53 		.info_mask_separate =
54 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
55 		.scan_index = 0,
56 		.scan_type = {
57 			.sign = 'u',
58 			.realbits = 16,
59 			.storagebits = 16,
60 		},
61 	},
62 	IIO_CHAN_SOFT_TIMESTAMP(1),
63 };
64 
lidar_i2c_xfer(struct lidar_data * data,u8 reg,u8 * val,int len)65 static int lidar_i2c_xfer(struct lidar_data *data, u8 reg, u8 *val, int len)
66 {
67 	struct i2c_client *client = data->client;
68 	struct i2c_msg msg[2];
69 	int ret;
70 
71 	msg[0].addr = client->addr;
72 	msg[0].flags = client->flags | I2C_M_STOP;
73 	msg[0].len = 1;
74 	msg[0].buf  = (char *) &reg;
75 
76 	msg[1].addr = client->addr;
77 	msg[1].flags = client->flags | I2C_M_RD;
78 	msg[1].len = len;
79 	msg[1].buf = (char *) val;
80 
81 	ret = i2c_transfer(client->adapter, msg, 2);
82 
83 	return (ret == 2) ? 0 : -EIO;
84 }
85 
lidar_smbus_xfer(struct lidar_data * data,u8 reg,u8 * val,int len)86 static int lidar_smbus_xfer(struct lidar_data *data, u8 reg, u8 *val, int len)
87 {
88 	struct i2c_client *client = data->client;
89 	int ret;
90 
91 	/*
92 	 * Device needs a STOP condition between address write, and data read
93 	 * so in turn i2c_smbus_read_byte_data cannot be used
94 	 */
95 
96 	while (len--) {
97 		ret = i2c_smbus_write_byte(client, reg++);
98 		if (ret < 0) {
99 			dev_err(&client->dev, "cannot write addr value");
100 			return ret;
101 		}
102 
103 		ret = i2c_smbus_read_byte(client);
104 		if (ret < 0) {
105 			dev_err(&client->dev, "cannot read data value");
106 			return ret;
107 		}
108 
109 		*(val++) = ret;
110 	}
111 
112 	return 0;
113 }
114 
lidar_read_byte(struct lidar_data * data,u8 reg)115 static int lidar_read_byte(struct lidar_data *data, u8 reg)
116 {
117 	int ret;
118 	u8 val;
119 
120 	ret = data->xfer(data, reg, &val, 1);
121 	if (ret < 0)
122 		return ret;
123 
124 	return val;
125 }
126 
lidar_write_control(struct lidar_data * data,int val)127 static inline int lidar_write_control(struct lidar_data *data, int val)
128 {
129 	return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL, val);
130 }
131 
lidar_write_power(struct lidar_data * data,int val)132 static inline int lidar_write_power(struct lidar_data *data, int val)
133 {
134 	return i2c_smbus_write_byte_data(data->client,
135 					 LIDAR_REG_PWR_CONTROL, val);
136 }
137 
lidar_read_measurement(struct lidar_data * data,u16 * reg)138 static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
139 {
140 	__be16 value;
141 	int ret = data->xfer(data, LIDAR_REG_DATA_HBYTE |
142 			(data->i2c_enabled ? LIDAR_REG_DATA_WORD_READ : 0),
143 			(u8 *) &value, 2);
144 
145 	if (!ret)
146 		*reg = be16_to_cpu(value);
147 
148 	return ret;
149 }
150 
lidar_get_measurement(struct lidar_data * data,u16 * reg)151 static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
152 {
153 	struct i2c_client *client = data->client;
154 	int tries = 10;
155 	int ret;
156 
157 	pm_runtime_get_sync(&client->dev);
158 
159 	/* start sample */
160 	ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
161 	if (ret < 0) {
162 		dev_err(&client->dev, "cannot send start measurement command");
163 		return ret;
164 	}
165 
166 	while (tries--) {
167 		usleep_range(1000, 2000);
168 
169 		ret = lidar_read_byte(data, LIDAR_REG_STATUS);
170 		if (ret < 0)
171 			break;
172 
173 		/* return -EINVAL since laser is likely pointed out of range */
174 		if (ret & LIDAR_REG_STATUS_INVALID) {
175 			*reg = 0;
176 			ret = -EINVAL;
177 			break;
178 		}
179 
180 		/* sample ready to read */
181 		if (!(ret & LIDAR_REG_STATUS_READY)) {
182 			ret = lidar_read_measurement(data, reg);
183 			break;
184 		}
185 		ret = -EIO;
186 	}
187 	pm_runtime_mark_last_busy(&client->dev);
188 	pm_runtime_put_autosuspend(&client->dev);
189 
190 	return ret;
191 }
192 
lidar_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)193 static int lidar_read_raw(struct iio_dev *indio_dev,
194 			  struct iio_chan_spec const *chan,
195 			  int *val, int *val2, long mask)
196 {
197 	struct lidar_data *data = iio_priv(indio_dev);
198 	int ret = -EINVAL;
199 
200 	switch (mask) {
201 	case IIO_CHAN_INFO_RAW: {
202 		u16 reg;
203 
204 		if (iio_device_claim_direct_mode(indio_dev))
205 			return -EBUSY;
206 
207 		ret = lidar_get_measurement(data, &reg);
208 		if (!ret) {
209 			*val = reg;
210 			ret = IIO_VAL_INT;
211 		}
212 		iio_device_release_direct_mode(indio_dev);
213 		break;
214 	}
215 	case IIO_CHAN_INFO_SCALE:
216 		*val = 0;
217 		*val2 = 10000;
218 		ret = IIO_VAL_INT_PLUS_MICRO;
219 		break;
220 	}
221 
222 	return ret;
223 }
224 
lidar_trigger_handler(int irq,void * private)225 static irqreturn_t lidar_trigger_handler(int irq, void *private)
226 {
227 	struct iio_poll_func *pf = private;
228 	struct iio_dev *indio_dev = pf->indio_dev;
229 	struct lidar_data *data = iio_priv(indio_dev);
230 	int ret;
231 
232 	ret = lidar_get_measurement(data, data->buffer);
233 	if (!ret) {
234 		iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
235 						   iio_get_time_ns(indio_dev));
236 	} else if (ret != -EINVAL) {
237 		dev_err(&data->client->dev, "cannot read LIDAR measurement");
238 	}
239 
240 	iio_trigger_notify_done(indio_dev->trig);
241 
242 	return IRQ_HANDLED;
243 }
244 
245 static const struct iio_info lidar_info = {
246 	.read_raw = lidar_read_raw,
247 };
248 
lidar_probe(struct i2c_client * client,const struct i2c_device_id * id)249 static int lidar_probe(struct i2c_client *client,
250 		       const struct i2c_device_id *id)
251 {
252 	struct lidar_data *data;
253 	struct iio_dev *indio_dev;
254 	int ret;
255 
256 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
257 	if (!indio_dev)
258 		return -ENOMEM;
259 	data = iio_priv(indio_dev);
260 
261 	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
262 		data->xfer = lidar_i2c_xfer;
263 		data->i2c_enabled = 1;
264 	} else if (i2c_check_functionality(client->adapter,
265 				I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE))
266 		data->xfer = lidar_smbus_xfer;
267 	else
268 		return -EOPNOTSUPP;
269 
270 	indio_dev->info = &lidar_info;
271 	indio_dev->name = LIDAR_DRV_NAME;
272 	indio_dev->channels = lidar_channels;
273 	indio_dev->num_channels = ARRAY_SIZE(lidar_channels);
274 	indio_dev->modes = INDIO_DIRECT_MODE;
275 
276 	i2c_set_clientdata(client, indio_dev);
277 
278 	data->client = client;
279 	data->indio_dev = indio_dev;
280 
281 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
282 					 lidar_trigger_handler, NULL);
283 	if (ret)
284 		return ret;
285 
286 	ret = iio_device_register(indio_dev);
287 	if (ret)
288 		goto error_unreg_buffer;
289 
290 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
291 	pm_runtime_use_autosuspend(&client->dev);
292 
293 	ret = pm_runtime_set_active(&client->dev);
294 	if (ret)
295 		goto error_unreg_buffer;
296 	pm_runtime_enable(&client->dev);
297 	pm_runtime_idle(&client->dev);
298 
299 	return 0;
300 
301 error_unreg_buffer:
302 	iio_triggered_buffer_cleanup(indio_dev);
303 
304 	return ret;
305 }
306 
lidar_remove(struct i2c_client * client)307 static int lidar_remove(struct i2c_client *client)
308 {
309 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
310 
311 	iio_device_unregister(indio_dev);
312 	iio_triggered_buffer_cleanup(indio_dev);
313 
314 	pm_runtime_disable(&client->dev);
315 	pm_runtime_set_suspended(&client->dev);
316 
317 	return 0;
318 }
319 
320 static const struct i2c_device_id lidar_id[] = {
321 	{"lidar-lite-v2", 0},
322 	{"lidar-lite-v3", 0},
323 	{ },
324 };
325 MODULE_DEVICE_TABLE(i2c, lidar_id);
326 
327 static const struct of_device_id lidar_dt_ids[] = {
328 	{ .compatible = "pulsedlight,lidar-lite-v2" },
329 	{ .compatible = "grmn,lidar-lite-v3" },
330 	{ }
331 };
332 MODULE_DEVICE_TABLE(of, lidar_dt_ids);
333 
334 #ifdef CONFIG_PM
lidar_pm_runtime_suspend(struct device * dev)335 static int lidar_pm_runtime_suspend(struct device *dev)
336 {
337 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
338 	struct lidar_data *data = iio_priv(indio_dev);
339 
340 	return lidar_write_power(data, 0x0f);
341 }
342 
lidar_pm_runtime_resume(struct device * dev)343 static int lidar_pm_runtime_resume(struct device *dev)
344 {
345 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
346 	struct lidar_data *data = iio_priv(indio_dev);
347 	int ret = lidar_write_power(data, 0);
348 
349 	/* regulator and FPGA needs settling time */
350 	usleep_range(15000, 20000);
351 
352 	return ret;
353 }
354 #endif
355 
356 static const struct dev_pm_ops lidar_pm_ops = {
357 	SET_RUNTIME_PM_OPS(lidar_pm_runtime_suspend,
358 			   lidar_pm_runtime_resume, NULL)
359 };
360 
361 static struct i2c_driver lidar_driver = {
362 	.driver = {
363 		.name	= LIDAR_DRV_NAME,
364 		.of_match_table	= lidar_dt_ids,
365 		.pm	= &lidar_pm_ops,
366 	},
367 	.probe		= lidar_probe,
368 	.remove		= lidar_remove,
369 	.id_table	= lidar_id,
370 };
371 module_i2c_driver(lidar_driver);
372 
373 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
374 MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
375 MODULE_LICENSE("GPL");
376