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
;
108 ret
= i2c_smbus_write_byte_data(client
, VL_REG_SYSRANGE_START
, 1);
112 if (data
->client
->irq
) {
113 reinit_completion(&data
->completion
);
115 ret
= wait_for_completion_timeout(&data
->completion
, HZ
/10);
121 vl53l0x_clear_irq(data
);
124 ret
= i2c_smbus_read_byte_data(client
,
125 VL_REG_RESULT_RANGE_STATUS
);
129 if (ret
& VL_REG_RESULT_RANGE_STATUS_COMPLETE
)
132 usleep_range(1000, 5000);
138 ret
= i2c_smbus_read_i2c_block_data(client
, VL_REG_RESULT_RANGE_STATUS
,
145 /* Values should be between 30~1200 in millimeters. */
146 *val
= (buffer
[10] << 8) + buffer
[11];
151 static const struct iio_chan_spec vl53l0x_channels
[] = {
153 .type
= IIO_DISTANCE
,
154 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
155 BIT(IIO_CHAN_INFO_SCALE
),
159 static int vl53l0x_read_raw(struct iio_dev
*indio_dev
,
160 const struct iio_chan_spec
*chan
,
161 int *val
, int *val2
, long mask
)
163 struct vl53l0x_data
*data
= iio_priv(indio_dev
);
166 if (chan
->type
!= IIO_DISTANCE
)
170 case IIO_CHAN_INFO_RAW
:
171 ret
= vl53l0x_read_proximity(data
, chan
, val
);
176 case IIO_CHAN_INFO_SCALE
:
180 return IIO_VAL_INT_PLUS_MICRO
;
186 static const struct iio_info vl53l0x_info
= {
187 .read_raw
= vl53l0x_read_raw
,
190 static int vl53l0x_probe(struct i2c_client
*client
)
192 struct vl53l0x_data
*data
;
193 struct iio_dev
*indio_dev
;
195 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
199 data
= iio_priv(indio_dev
);
200 data
->client
= client
;
201 i2c_set_clientdata(client
, indio_dev
);
203 if (!i2c_check_functionality(client
->adapter
,
204 I2C_FUNC_SMBUS_READ_I2C_BLOCK
|
205 I2C_FUNC_SMBUS_BYTE_DATA
))
208 indio_dev
->name
= "vl53l0x";
209 indio_dev
->info
= &vl53l0x_info
;
210 indio_dev
->channels
= vl53l0x_channels
;
211 indio_dev
->num_channels
= ARRAY_SIZE(vl53l0x_channels
);
212 indio_dev
->modes
= INDIO_DIRECT_MODE
;
214 /* usage of interrupt is optional */
218 init_completion(&data
->completion
);
220 ret
= vl53l0x_configure_irq(client
, indio_dev
);
225 return devm_iio_device_register(&client
->dev
, indio_dev
);
228 static const struct i2c_device_id vl53l0x_id
[] = {
232 MODULE_DEVICE_TABLE(i2c
, vl53l0x_id
);
234 static const struct of_device_id st_vl53l0x_dt_match
[] = {
235 { .compatible
= "st,vl53l0x", },
238 MODULE_DEVICE_TABLE(of
, st_vl53l0x_dt_match
);
240 static struct i2c_driver vl53l0x_driver
= {
242 .name
= "vl53l0x-i2c",
243 .of_match_table
= st_vl53l0x_dt_match
,
245 .probe_new
= vl53l0x_probe
,
246 .id_table
= vl53l0x_id
,
248 module_i2c_driver(vl53l0x_driver
);
250 MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
251 MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
252 MODULE_LICENSE("GPL v2");