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/i2c.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
22 #include <linux/iio/iio.h>
24 #define VL_REG_SYSRANGE_START 0x00
26 #define VL_REG_SYSRANGE_MODE_MASK GENMASK(3, 0)
27 #define VL_REG_SYSRANGE_MODE_SINGLESHOT 0x00
28 #define VL_REG_SYSRANGE_MODE_START_STOP BIT(0)
29 #define VL_REG_SYSRANGE_MODE_BACKTOBACK BIT(1)
30 #define VL_REG_SYSRANGE_MODE_TIMED BIT(2)
31 #define VL_REG_SYSRANGE_MODE_HISTOGRAM BIT(3)
33 #define VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0A
34 #define VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY BIT(2)
36 #define VL_REG_SYSTEM_INTERRUPT_CLEAR 0x0B
38 #define VL_REG_RESULT_INT_STATUS 0x13
39 #define VL_REG_RESULT_RANGE_STATUS 0x14
40 #define VL_REG_RESULT_RANGE_STATUS_COMPLETE BIT(0)
43 struct i2c_client *client;
44 struct completion completion;
47 static irqreturn_t vl53l0x_handle_irq(int irq, void *priv)
49 struct iio_dev *indio_dev = priv;
50 struct vl53l0x_data *data = iio_priv(indio_dev);
52 complete(&data->completion);
57 static int vl53l0x_configure_irq(struct i2c_client *client,
58 struct iio_dev *indio_dev)
60 struct vl53l0x_data *data = iio_priv(indio_dev);
63 ret = devm_request_irq(&client->dev, client->irq, vl53l0x_handle_irq,
64 IRQF_TRIGGER_FALLING, indio_dev->name, indio_dev);
66 dev_err(&client->dev, "devm_request_irq error: %d\n", ret);
70 ret = i2c_smbus_write_byte_data(data->client,
71 VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO,
72 VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
74 dev_err(&client->dev, "failed to configure IRQ: %d\n", ret);
79 static void vl53l0x_clear_irq(struct vl53l0x_data *data)
81 struct device *dev = &data->client->dev;
84 ret = i2c_smbus_write_byte_data(data->client,
85 VL_REG_SYSTEM_INTERRUPT_CLEAR, 1);
87 dev_err(dev, "failed to clear error irq: %d\n", ret);
89 ret = i2c_smbus_write_byte_data(data->client,
90 VL_REG_SYSTEM_INTERRUPT_CLEAR, 0);
92 dev_err(dev, "failed to clear range irq: %d\n", ret);
94 ret = i2c_smbus_read_byte_data(data->client, VL_REG_RESULT_INT_STATUS);
95 if (ret < 0 || ret & 0x07)
96 dev_err(dev, "failed to clear irq: %d\n", ret);
99 static int vl53l0x_read_proximity(struct vl53l0x_data *data,
100 const struct iio_chan_spec *chan,
103 struct i2c_client *client = data->client;
107 unsigned long time_left;
109 ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1);
113 if (data->client->irq) {
114 reinit_completion(&data->completion);
116 time_left = wait_for_completion_timeout(&data->completion, HZ/10);
120 vl53l0x_clear_irq(data);
123 ret = i2c_smbus_read_byte_data(client,
124 VL_REG_RESULT_RANGE_STATUS);
128 if (ret & VL_REG_RESULT_RANGE_STATUS_COMPLETE)
131 usleep_range(1000, 5000);
137 ret = i2c_smbus_read_i2c_block_data(client, VL_REG_RESULT_RANGE_STATUS,
144 /* Values should be between 30~1200 in millimeters. */
145 *val = (buffer[10] << 8) + buffer[11];
150 static const struct iio_chan_spec vl53l0x_channels[] = {
152 .type = IIO_DISTANCE,
153 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
154 BIT(IIO_CHAN_INFO_SCALE),
158 static int vl53l0x_read_raw(struct iio_dev *indio_dev,
159 const struct iio_chan_spec *chan,
160 int *val, int *val2, long mask)
162 struct vl53l0x_data *data = iio_priv(indio_dev);
165 if (chan->type != IIO_DISTANCE)
169 case IIO_CHAN_INFO_RAW:
170 ret = vl53l0x_read_proximity(data, chan, val);
175 case IIO_CHAN_INFO_SCALE:
179 return IIO_VAL_INT_PLUS_MICRO;
185 static const struct iio_info vl53l0x_info = {
186 .read_raw = vl53l0x_read_raw,
189 static int vl53l0x_probe(struct i2c_client *client)
191 struct vl53l0x_data *data;
192 struct iio_dev *indio_dev;
194 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
198 data = iio_priv(indio_dev);
199 data->client = client;
200 i2c_set_clientdata(client, indio_dev);
202 if (!i2c_check_functionality(client->adapter,
203 I2C_FUNC_SMBUS_READ_I2C_BLOCK |
204 I2C_FUNC_SMBUS_BYTE_DATA))
207 indio_dev->name = "vl53l0x";
208 indio_dev->info = &vl53l0x_info;
209 indio_dev->channels = vl53l0x_channels;
210 indio_dev->num_channels = ARRAY_SIZE(vl53l0x_channels);
211 indio_dev->modes = INDIO_DIRECT_MODE;
213 /* usage of interrupt is optional */
217 init_completion(&data->completion);
219 ret = vl53l0x_configure_irq(client, indio_dev);
224 return devm_iio_device_register(&client->dev, indio_dev);
227 static const struct i2c_device_id vl53l0x_id[] = {
231 MODULE_DEVICE_TABLE(i2c, vl53l0x_id);
233 static const struct of_device_id st_vl53l0x_dt_match[] = {
234 { .compatible = "st,vl53l0x", },
237 MODULE_DEVICE_TABLE(of, st_vl53l0x_dt_match);
239 static struct i2c_driver vl53l0x_driver = {
241 .name = "vl53l0x-i2c",
242 .of_match_table = st_vl53l0x_dt_match,
244 .probe_new = vl53l0x_probe,
245 .id_table = vl53l0x_id,
247 module_i2c_driver(vl53l0x_driver);
249 MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
250 MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
251 MODULE_LICENSE("GPL v2");