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