xref: /linux/drivers/iio/proximity/vl53l0x-i2c.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for ST VL53L0X FlightSense ToF Ranging Sensor on a i2c bus.
4  *
5  * Copyright (C) 2016 STMicroelectronics Imaging Division.
6  * Copyright (C) 2018 Song Qiang <songqiang1304521@gmail.com>
7  * Copyright (C) 2020 Ivan Drobyshevskyi <drobyshevskyi@gmail.com>
8  *
9  * Datasheet available at
10  * <https://www.st.com/resource/en/datasheet/vl53l0x.pdf>
11  *
12  * Default 7-bit i2c slave address 0x29.
13  *
14  * TODO: FIFO buffer, continuous mode, range selection, sensor ID check.
15  */
16 
17 #include <linux/delay.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/unaligned.h>
24 
25 #include <linux/iio/iio.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30 
31 #define VL_REG_SYSRANGE_START				0x00
32 
33 #define VL_REG_SYSRANGE_MODE_MASK			GENMASK(3, 0)
34 #define VL_REG_SYSRANGE_MODE_SINGLESHOT			0x00
35 #define VL_REG_SYSRANGE_MODE_START_STOP			BIT(0)
36 #define VL_REG_SYSRANGE_MODE_BACKTOBACK			BIT(1)
37 #define VL_REG_SYSRANGE_MODE_TIMED			BIT(2)
38 #define VL_REG_SYSRANGE_MODE_HISTOGRAM			BIT(3)
39 
40 #define VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO		0x0A
41 #define VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY	BIT(2)
42 
43 #define VL_REG_SYSTEM_INTERRUPT_CLEAR			0x0B
44 
45 #define VL_REG_RESULT_INT_STATUS			0x13
46 #define VL_REG_RESULT_RANGE_STATUS			0x14
47 #define VL_REG_IDENTIFICATION_MODEL_ID			0xC0
48 #define VL_REG_RESULT_RANGE_STATUS_COMPLETE		BIT(0)
49 
50 #define VL53L0X_MODEL_ID_VAL				0xEE
51 #define VL53L0X_CONTINUOUS_MODE				0x02
52 #define VL53L0X_SINGLE_MODE				0x01
53 
54 struct vl53l0x_data {
55 	struct i2c_client *client;
56 	struct completion completion;
57 	struct regulator *vdd_supply;
58 	struct gpio_desc *reset_gpio;
59 	struct iio_trigger *trig;
60 
61 	struct {
62 		u16 chan;
63 		aligned_s64 timestamp;
64 	} scan;
65 };
66 
67 static int vl53l0x_clear_irq(struct vl53l0x_data *data)
68 {	int ret;
69 
70 	ret = i2c_smbus_write_byte_data(data->client,
71 					VL_REG_SYSTEM_INTERRUPT_CLEAR, 1);
72 	if (ret < 0) {
73 		dev_err(&data->client->dev, "failed to clear irq: %d\n", ret);
74 		return -EINVAL;
75 	}
76 
77 	return 0;
78 }
79 
80 static irqreturn_t vl53l0x_trigger_handler(int irq, void *priv)
81 {
82 	struct iio_poll_func *pf = priv;
83 	struct iio_dev *indio_dev = pf->indio_dev;
84 	struct vl53l0x_data *data = iio_priv(indio_dev);
85 	u8 buffer[12];
86 	int ret;
87 
88 	ret = i2c_smbus_read_i2c_block_data(data->client,
89 					VL_REG_RESULT_RANGE_STATUS,
90 					sizeof(buffer), buffer);
91 	if (ret < 0)
92 		return ret;
93 	else if (ret != 12)
94 		return -EREMOTEIO;
95 
96 	data->scan.chan = get_unaligned_be16(&buffer[10]);
97 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
98 					iio_get_time_ns(indio_dev));
99 
100 	iio_trigger_notify_done(indio_dev->trig);
101 	vl53l0x_clear_irq(data);
102 
103 	return IRQ_HANDLED;
104 }
105 
106 static irqreturn_t vl53l0x_threaded_irq(int irq, void *priv)
107 {
108 	struct iio_dev *indio_dev = priv;
109 	struct vl53l0x_data *data = iio_priv(indio_dev);
110 
111 	if (iio_buffer_enabled(indio_dev))
112 		iio_trigger_poll_nested(indio_dev->trig);
113 	else
114 		complete(&data->completion);
115 
116 	return IRQ_HANDLED;
117 }
118 
119 static int vl53l0x_configure_irq(struct i2c_client *client,
120 				 struct iio_dev *indio_dev)
121 {
122 	int irq_flags = irq_get_trigger_type(client->irq);
123 	struct vl53l0x_data *data = iio_priv(indio_dev);
124 	int ret;
125 
126 	if (!irq_flags)
127 		irq_flags = IRQF_TRIGGER_FALLING;
128 
129 	ret = devm_request_threaded_irq(&client->dev, client->irq,
130 			NULL, vl53l0x_threaded_irq,
131 			irq_flags | IRQF_ONESHOT, indio_dev->name, indio_dev);
132 	if (ret) {
133 		dev_err(&client->dev, "devm_request_irq error: %d\n", ret);
134 		return ret;
135 	}
136 
137 	ret = i2c_smbus_write_byte_data(data->client,
138 			VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO,
139 			VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
140 	if (ret < 0)
141 		dev_err(&client->dev, "failed to configure IRQ: %d\n", ret);
142 
143 	return ret;
144 }
145 
146 static int vl53l0x_read_proximity(struct vl53l0x_data *data,
147 				  const struct iio_chan_spec *chan,
148 				  int *val)
149 {
150 	struct i2c_client *client = data->client;
151 	u16 tries = 20;
152 	u8 buffer[12];
153 	int ret;
154 	unsigned long time_left;
155 
156 	ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1);
157 	if (ret < 0)
158 		return ret;
159 
160 	if (data->client->irq) {
161 		reinit_completion(&data->completion);
162 
163 		time_left = wait_for_completion_timeout(&data->completion, HZ/10);
164 		if (time_left == 0)
165 			return -ETIMEDOUT;
166 
167 		ret = vl53l0x_clear_irq(data);
168 		if (ret < 0)
169 			return ret;
170 	} else {
171 		do {
172 			ret = i2c_smbus_read_byte_data(client,
173 					       VL_REG_RESULT_RANGE_STATUS);
174 			if (ret < 0)
175 				return ret;
176 
177 			if (ret & VL_REG_RESULT_RANGE_STATUS_COMPLETE)
178 				break;
179 
180 			usleep_range(1000, 5000);
181 		} while (--tries);
182 		if (!tries)
183 			return -ETIMEDOUT;
184 	}
185 
186 	ret = i2c_smbus_read_i2c_block_data(client, VL_REG_RESULT_RANGE_STATUS,
187 					    12, buffer);
188 	if (ret < 0)
189 		return ret;
190 	else if (ret != 12)
191 		return -EREMOTEIO;
192 
193 	/* Values should be between 30~1200 in millimeters. */
194 	*val = get_unaligned_be16(&buffer[10]);
195 
196 	return 0;
197 }
198 
199 static const struct iio_chan_spec vl53l0x_channels[] = {
200 	{
201 		.type = IIO_DISTANCE,
202 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
203 				      BIT(IIO_CHAN_INFO_SCALE),
204 		.scan_index = 0,
205 		.scan_type = {
206 			.sign = 'u',
207 			.realbits = 12,
208 			.storagebits = 16,
209 		},
210 	},
211 	IIO_CHAN_SOFT_TIMESTAMP(1),
212 };
213 
214 static int vl53l0x_read_raw(struct iio_dev *indio_dev,
215 			    const struct iio_chan_spec *chan,
216 			    int *val, int *val2, long mask)
217 {
218 	struct vl53l0x_data *data = iio_priv(indio_dev);
219 	int ret;
220 
221 	if (chan->type != IIO_DISTANCE)
222 		return -EINVAL;
223 
224 	switch (mask) {
225 	case IIO_CHAN_INFO_RAW:
226 		ret = vl53l0x_read_proximity(data, chan, val);
227 		if (ret < 0)
228 			return ret;
229 
230 		return IIO_VAL_INT;
231 	case IIO_CHAN_INFO_SCALE:
232 		*val = 0;
233 		*val2 = 1000;
234 
235 		return IIO_VAL_INT_PLUS_MICRO;
236 	default:
237 		return -EINVAL;
238 	}
239 }
240 
241 static int vl53l0x_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
242 {
243 	struct vl53l0x_data *data = iio_priv(indio_dev);
244 
245 	return data->trig == trig ? 0 : -EINVAL;
246 }
247 
248 static const struct iio_info vl53l0x_info = {
249 	.read_raw = vl53l0x_read_raw,
250 	.validate_trigger = vl53l0x_validate_trigger,
251 };
252 
253 static void vl53l0x_power_off(void *_data)
254 {
255 	struct vl53l0x_data *data = _data;
256 
257 	gpiod_set_value_cansleep(data->reset_gpio, 1);
258 
259 	regulator_disable(data->vdd_supply);
260 }
261 
262 static int vl53l0x_power_on(struct vl53l0x_data *data)
263 {
264 	int ret;
265 
266 	ret = regulator_enable(data->vdd_supply);
267 	if (ret)
268 		return ret;
269 
270 	gpiod_set_value_cansleep(data->reset_gpio, 0);
271 
272 	usleep_range(3200, 5000);
273 
274 	return 0;
275 }
276 
277 static int vl53l0x_buffer_postenable(struct iio_dev *indio_dev)
278 {
279 	struct vl53l0x_data *data = iio_priv(indio_dev);
280 
281 	return i2c_smbus_write_byte_data(data->client, VL_REG_SYSRANGE_START,
282 						VL53L0X_CONTINUOUS_MODE);
283 }
284 
285 static int vl53l0x_buffer_postdisable(struct iio_dev *indio_dev)
286 {
287 	struct vl53l0x_data *data = iio_priv(indio_dev);
288 	int ret;
289 
290 	ret = i2c_smbus_write_byte_data(data->client, VL_REG_SYSRANGE_START,
291 						VL53L0X_SINGLE_MODE);
292 	if (ret < 0)
293 		return ret;
294 
295 	/* Let the ongoing reading finish */
296 	reinit_completion(&data->completion);
297 	wait_for_completion_timeout(&data->completion, HZ / 10);
298 
299 	return vl53l0x_clear_irq(data);
300 }
301 
302 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
303 	.postenable = &vl53l0x_buffer_postenable,
304 	.postdisable = &vl53l0x_buffer_postdisable,
305 };
306 
307 static const struct iio_trigger_ops vl53l0x_trigger_ops = {
308 	.validate_device = iio_trigger_validate_own_device,
309 };
310 
311 static int vl53l0x_probe(struct i2c_client *client)
312 {
313 	struct vl53l0x_data *data;
314 	struct iio_dev *indio_dev;
315 	int error;
316 	int ret;
317 
318 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
319 	if (!indio_dev)
320 		return -ENOMEM;
321 
322 	data = iio_priv(indio_dev);
323 	data->client = client;
324 	i2c_set_clientdata(client, indio_dev);
325 
326 	if (!i2c_check_functionality(client->adapter,
327 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK |
328 				     I2C_FUNC_SMBUS_BYTE_DATA))
329 		return -EOPNOTSUPP;
330 
331 	ret = i2c_smbus_read_byte_data(data->client, VL_REG_IDENTIFICATION_MODEL_ID);
332 	if (ret < 0)
333 		return -EINVAL;
334 
335 	if (ret != VL53L0X_MODEL_ID_VAL)
336 		dev_info(&client->dev, "Unknown model id: 0x%x", ret);
337 
338 	data->vdd_supply = devm_regulator_get(&client->dev, "vdd");
339 	if (IS_ERR(data->vdd_supply))
340 		return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply),
341 				     "Unable to get VDD regulator\n");
342 
343 	data->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
344 	if (IS_ERR(data->reset_gpio))
345 		return dev_err_probe(&client->dev, PTR_ERR(data->reset_gpio),
346 				     "Cannot get reset GPIO\n");
347 
348 	error = vl53l0x_power_on(data);
349 	if (error)
350 		return dev_err_probe(&client->dev, error,
351 				     "Failed to power on the chip\n");
352 
353 	error = devm_add_action_or_reset(&client->dev, vl53l0x_power_off, data);
354 	if (error)
355 		return dev_err_probe(&client->dev, error,
356 				     "Failed to install poweroff action\n");
357 
358 	indio_dev->name = "vl53l0x";
359 	indio_dev->info = &vl53l0x_info;
360 	indio_dev->channels = vl53l0x_channels;
361 	indio_dev->num_channels = ARRAY_SIZE(vl53l0x_channels);
362 	indio_dev->modes = INDIO_DIRECT_MODE;
363 
364 	/* usage of interrupt is optional */
365 	if (client->irq) {
366 		init_completion(&data->completion);
367 
368 		data->trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
369 						indio_dev->name,
370 						iio_device_id(indio_dev));
371 		if (!data->trig)
372 			return -ENOMEM;
373 
374 		data->trig->ops = &vl53l0x_trigger_ops;
375 		iio_trigger_set_drvdata(data->trig, indio_dev);
376 		ret = devm_iio_trigger_register(&client->dev, data->trig);
377 		if (ret)
378 			return ret;
379 
380 		indio_dev->trig = iio_trigger_get(data->trig);
381 
382 		ret = vl53l0x_configure_irq(client, indio_dev);
383 		if (ret)
384 			return ret;
385 
386 		ret = devm_iio_triggered_buffer_setup(&client->dev,
387 					indio_dev,
388 					NULL,
389 					&vl53l0x_trigger_handler,
390 					&iio_triggered_buffer_setup_ops);
391 		if (ret)
392 			return ret;
393 	}
394 
395 	return devm_iio_device_register(&client->dev, indio_dev);
396 }
397 
398 static const struct i2c_device_id vl53l0x_id[] = {
399 	{ "vl53l0x" },
400 	{ }
401 };
402 MODULE_DEVICE_TABLE(i2c, vl53l0x_id);
403 
404 static const struct of_device_id st_vl53l0x_dt_match[] = {
405 	{ .compatible = "st,vl53l0x", },
406 	{ }
407 };
408 MODULE_DEVICE_TABLE(of, st_vl53l0x_dt_match);
409 
410 static struct i2c_driver vl53l0x_driver = {
411 	.driver = {
412 		.name = "vl53l0x-i2c",
413 		.of_match_table = st_vl53l0x_dt_match,
414 	},
415 	.probe = vl53l0x_probe,
416 	.id_table = vl53l0x_id,
417 };
418 module_i2c_driver(vl53l0x_driver);
419 
420 MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
421 MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
422 MODULE_LICENSE("GPL v2");
423