WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / proximity / vl53l0x-i2c.c
blobcf38144b6f95447bfdb2802debb7ba0b695a6d0c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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)
42 struct vl53l0x_data {
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);
54 return IRQ_HANDLED;
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);
61 int ret;
63 ret = devm_request_irq(&client->dev, client->irq, vl53l0x_handle_irq,
64 IRQF_TRIGGER_FALLING, indio_dev->name, indio_dev);
65 if (ret) {
66 dev_err(&client->dev, "devm_request_irq error: %d\n", ret);
67 return 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);
73 if (ret < 0)
74 dev_err(&client->dev, "failed to configure IRQ: %d\n", ret);
76 return ret;
79 static void vl53l0x_clear_irq(struct vl53l0x_data *data)
81 struct device *dev = &data->client->dev;
82 int ret;
84 ret = i2c_smbus_write_byte_data(data->client,
85 VL_REG_SYSTEM_INTERRUPT_CLEAR, 1);
86 if (ret < 0)
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);
91 if (ret < 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,
101 int *val)
103 struct i2c_client *client = data->client;
104 u16 tries = 20;
105 u8 buffer[12];
106 int ret;
108 ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1);
109 if (ret < 0)
110 return ret;
112 if (data->client->irq) {
113 reinit_completion(&data->completion);
115 ret = wait_for_completion_timeout(&data->completion, HZ/10);
116 if (ret < 0)
117 return ret;
118 else if (ret == 0)
119 return -ETIMEDOUT;
121 vl53l0x_clear_irq(data);
122 } else {
123 do {
124 ret = i2c_smbus_read_byte_data(client,
125 VL_REG_RESULT_RANGE_STATUS);
126 if (ret < 0)
127 return ret;
129 if (ret & VL_REG_RESULT_RANGE_STATUS_COMPLETE)
130 break;
132 usleep_range(1000, 5000);
133 } while (--tries);
134 if (!tries)
135 return -ETIMEDOUT;
138 ret = i2c_smbus_read_i2c_block_data(client, VL_REG_RESULT_RANGE_STATUS,
139 12, buffer);
140 if (ret < 0)
141 return ret;
142 else if (ret != 12)
143 return -EREMOTEIO;
145 /* Values should be between 30~1200 in millimeters. */
146 *val = (buffer[10] << 8) + buffer[11];
148 return 0;
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);
164 int ret;
166 if (chan->type != IIO_DISTANCE)
167 return -EINVAL;
169 switch (mask) {
170 case IIO_CHAN_INFO_RAW:
171 ret = vl53l0x_read_proximity(data, chan, val);
172 if (ret < 0)
173 return ret;
175 return IIO_VAL_INT;
176 case IIO_CHAN_INFO_SCALE:
177 *val = 0;
178 *val2 = 1000;
180 return IIO_VAL_INT_PLUS_MICRO;
181 default:
182 return -EINVAL;
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));
196 if (!indio_dev)
197 return -ENOMEM;
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))
206 return -EOPNOTSUPP;
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 */
215 if (client->irq) {
216 int ret;
218 init_completion(&data->completion);
220 ret = vl53l0x_configure_irq(client, indio_dev);
221 if (ret)
222 return ret;
225 return devm_iio_device_register(&client->dev, indio_dev);
228 static const struct i2c_device_id vl53l0x_id[] = {
229 { "vl53l0x", 0},
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 = {
241 .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");