1 // SPDX-License-Identifier: GPL-2.0
3 * Support for ST VL53L0X FlightSense ToF Ranging Sensor on a i2c bus.
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>
9 * Datasheet available at
10 * <https://www.st.com/resource/en/datasheet/vl53l0x.pdf>
12 * Default 7-bit i2c slave address 0x29.
14 * TODO: FIFO buffer, continuous mode, range selection, sensor ID check.
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>
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>
31 #define VL_REG_SYSRANGE_START 0x00
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)
40 #define VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0A
41 #define VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY BIT(2)
43 #define VL_REG_SYSTEM_INTERRUPT_CLEAR 0x0B
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)
50 #define VL53L0X_MODEL_ID_VAL 0xEE
51 #define VL53L0X_CONTINUOUS_MODE 0x02
52 #define VL53L0X_SINGLE_MODE 0x01
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
;
63 aligned_s64 timestamp
;
67 static int vl53l0x_clear_irq(struct vl53l0x_data
*data
)
70 ret
= i2c_smbus_write_byte_data(data
->client
,
71 VL_REG_SYSTEM_INTERRUPT_CLEAR
, 1);
73 dev_err(&data
->client
->dev
, "failed to clear irq: %d\n", ret
);
80 static irqreturn_t
vl53l0x_trigger_handler(int irq
, void *priv
)
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
);
88 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
89 VL_REG_RESULT_RANGE_STATUS
,
90 sizeof(buffer
), buffer
);
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
));
100 iio_trigger_notify_done(indio_dev
->trig
);
101 vl53l0x_clear_irq(data
);
106 static irqreturn_t
vl53l0x_threaded_irq(int irq
, void *priv
)
108 struct iio_dev
*indio_dev
= priv
;
109 struct vl53l0x_data
*data
= iio_priv(indio_dev
);
111 if (iio_buffer_enabled(indio_dev
))
112 iio_trigger_poll_nested(indio_dev
->trig
);
114 complete(&data
->completion
);
119 static int vl53l0x_configure_irq(struct i2c_client
*client
,
120 struct iio_dev
*indio_dev
)
122 int irq_flags
= irq_get_trigger_type(client
->irq
);
123 struct vl53l0x_data
*data
= iio_priv(indio_dev
);
127 irq_flags
= IRQF_TRIGGER_FALLING
;
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
);
133 dev_err(&client
->dev
, "devm_request_irq error: %d\n", ret
);
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
);
141 dev_err(&client
->dev
, "failed to configure IRQ: %d\n", ret
);
146 static int vl53l0x_read_proximity(struct vl53l0x_data
*data
,
147 const struct iio_chan_spec
*chan
,
150 struct i2c_client
*client
= data
->client
;
154 unsigned long time_left
;
156 ret
= i2c_smbus_write_byte_data(client
, VL_REG_SYSRANGE_START
, 1);
160 if (data
->client
->irq
) {
161 reinit_completion(&data
->completion
);
163 time_left
= wait_for_completion_timeout(&data
->completion
, HZ
/10);
167 ret
= vl53l0x_clear_irq(data
);
172 ret
= i2c_smbus_read_byte_data(client
,
173 VL_REG_RESULT_RANGE_STATUS
);
177 if (ret
& VL_REG_RESULT_RANGE_STATUS_COMPLETE
)
180 usleep_range(1000, 5000);
186 ret
= i2c_smbus_read_i2c_block_data(client
, VL_REG_RESULT_RANGE_STATUS
,
193 /* Values should be between 30~1200 in millimeters. */
194 *val
= get_unaligned_be16(&buffer
[10]);
199 static const struct iio_chan_spec vl53l0x_channels
[] = {
201 .type
= IIO_DISTANCE
,
202 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
203 BIT(IIO_CHAN_INFO_SCALE
),
211 IIO_CHAN_SOFT_TIMESTAMP(1),
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
)
218 struct vl53l0x_data
*data
= iio_priv(indio_dev
);
221 if (chan
->type
!= IIO_DISTANCE
)
225 case IIO_CHAN_INFO_RAW
:
226 ret
= vl53l0x_read_proximity(data
, chan
, val
);
231 case IIO_CHAN_INFO_SCALE
:
235 return IIO_VAL_INT_PLUS_MICRO
;
241 static int vl53l0x_validate_trigger(struct iio_dev
*indio_dev
, struct iio_trigger
*trig
)
243 struct vl53l0x_data
*data
= iio_priv(indio_dev
);
245 return data
->trig
== trig
? 0 : -EINVAL
;
248 static const struct iio_info vl53l0x_info
= {
249 .read_raw
= vl53l0x_read_raw
,
250 .validate_trigger
= vl53l0x_validate_trigger
,
253 static void vl53l0x_power_off(void *_data
)
255 struct vl53l0x_data
*data
= _data
;
257 gpiod_set_value_cansleep(data
->reset_gpio
, 1);
259 regulator_disable(data
->vdd_supply
);
262 static int vl53l0x_power_on(struct vl53l0x_data
*data
)
266 ret
= regulator_enable(data
->vdd_supply
);
270 gpiod_set_value_cansleep(data
->reset_gpio
, 0);
272 usleep_range(3200, 5000);
277 static int vl53l0x_buffer_postenable(struct iio_dev
*indio_dev
)
279 struct vl53l0x_data
*data
= iio_priv(indio_dev
);
281 return i2c_smbus_write_byte_data(data
->client
, VL_REG_SYSRANGE_START
,
282 VL53L0X_CONTINUOUS_MODE
);
285 static int vl53l0x_buffer_postdisable(struct iio_dev
*indio_dev
)
287 struct vl53l0x_data
*data
= iio_priv(indio_dev
);
290 ret
= i2c_smbus_write_byte_data(data
->client
, VL_REG_SYSRANGE_START
,
291 VL53L0X_SINGLE_MODE
);
295 /* Let the ongoing reading finish */
296 reinit_completion(&data
->completion
);
297 wait_for_completion_timeout(&data
->completion
, HZ
/ 10);
299 return vl53l0x_clear_irq(data
);
302 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops
= {
303 .postenable
= &vl53l0x_buffer_postenable
,
304 .postdisable
= &vl53l0x_buffer_postdisable
,
307 static const struct iio_trigger_ops vl53l0x_trigger_ops
= {
308 .validate_device
= iio_trigger_validate_own_device
,
311 static int vl53l0x_probe(struct i2c_client
*client
)
313 struct vl53l0x_data
*data
;
314 struct iio_dev
*indio_dev
;
318 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
322 data
= iio_priv(indio_dev
);
323 data
->client
= client
;
324 i2c_set_clientdata(client
, indio_dev
);
326 if (!i2c_check_functionality(client
->adapter
,
327 I2C_FUNC_SMBUS_READ_I2C_BLOCK
|
328 I2C_FUNC_SMBUS_BYTE_DATA
))
331 ret
= i2c_smbus_read_byte_data(data
->client
, VL_REG_IDENTIFICATION_MODEL_ID
);
335 if (ret
!= VL53L0X_MODEL_ID_VAL
)
336 dev_info(&client
->dev
, "Unknown model id: 0x%x", ret
);
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");
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");
348 error
= vl53l0x_power_on(data
);
350 return dev_err_probe(&client
->dev
, error
,
351 "Failed to power on the chip\n");
353 error
= devm_add_action_or_reset(&client
->dev
, vl53l0x_power_off
, data
);
355 return dev_err_probe(&client
->dev
, error
,
356 "Failed to install poweroff action\n");
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
;
364 /* usage of interrupt is optional */
366 init_completion(&data
->completion
);
368 data
->trig
= devm_iio_trigger_alloc(&client
->dev
, "%s-dev%d",
370 iio_device_id(indio_dev
));
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
);
380 indio_dev
->trig
= iio_trigger_get(data
->trig
);
382 ret
= vl53l0x_configure_irq(client
, indio_dev
);
386 ret
= devm_iio_triggered_buffer_setup(&client
->dev
,
389 &vl53l0x_trigger_handler
,
390 &iio_triggered_buffer_setup_ops
);
395 return devm_iio_device_register(&client
->dev
, indio_dev
);
398 static const struct i2c_device_id vl53l0x_id
[] = {
402 MODULE_DEVICE_TABLE(i2c
, vl53l0x_id
);
404 static const struct of_device_id st_vl53l0x_dt_match
[] = {
405 { .compatible
= "st,vl53l0x", },
408 MODULE_DEVICE_TABLE(of
, st_vl53l0x_dt_match
);
410 static struct i2c_driver vl53l0x_driver
= {
412 .name
= "vl53l0x-i2c",
413 .of_match_table
= st_vl53l0x_dt_match
,
415 .probe
= vl53l0x_probe
,
416 .id_table
= vl53l0x_id
,
418 module_i2c_driver(vl53l0x_driver
);
420 MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
421 MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
422 MODULE_LICENSE("GPL v2");