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/i2c.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 
22 #include <linux/iio/iio.h>
23 
24 #define VL_REG_SYSRANGE_START				0x00
25 
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)
32 
33 #define VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO		0x0A
34 #define VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY	BIT(2)
35 
36 #define VL_REG_SYSTEM_INTERRUPT_CLEAR			0x0B
37 
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)
41 
42 struct vl53l0x_data {
43 	struct i2c_client *client;
44 	struct completion completion;
45 };
46 
47 static irqreturn_t vl53l0x_handle_irq(int irq, void *priv)
48 {
49 	struct iio_dev *indio_dev = priv;
50 	struct vl53l0x_data *data = iio_priv(indio_dev);
51 
52 	complete(&data->completion);
53 
54 	return IRQ_HANDLED;
55 }
56 
57 static int vl53l0x_configure_irq(struct i2c_client *client,
58 				 struct iio_dev *indio_dev)
59 {
60 	struct vl53l0x_data *data = iio_priv(indio_dev);
61 	int ret;
62 
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;
68 	}
69 
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);
75 
76 	return ret;
77 }
78 
79 static void vl53l0x_clear_irq(struct vl53l0x_data *data)
80 {
81 	struct device *dev = &data->client->dev;
82 	int ret;
83 
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);
88 
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);
93 
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);
97 }
98 
99 static int vl53l0x_read_proximity(struct vl53l0x_data *data,
100 				  const struct iio_chan_spec *chan,
101 				  int *val)
102 {
103 	struct i2c_client *client = data->client;
104 	u16 tries = 20;
105 	u8 buffer[12];
106 	int ret;
107 
108 	ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1);
109 	if (ret < 0)
110 		return ret;
111 
112 	if (data->client->irq) {
113 		reinit_completion(&data->completion);
114 
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;
120 
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;
128 
129 			if (ret & VL_REG_RESULT_RANGE_STATUS_COMPLETE)
130 				break;
131 
132 			usleep_range(1000, 5000);
133 		} while (--tries);
134 		if (!tries)
135 			return -ETIMEDOUT;
136 	}
137 
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;
144 
145 	/* Values should be between 30~1200 in millimeters. */
146 	*val = (buffer[10] << 8) + buffer[11];
147 
148 	return 0;
149 }
150 
151 static const struct iio_chan_spec vl53l0x_channels[] = {
152 	{
153 		.type = IIO_DISTANCE,
154 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
155 				      BIT(IIO_CHAN_INFO_SCALE),
156 	},
157 };
158 
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)
162 {
163 	struct vl53l0x_data *data = iio_priv(indio_dev);
164 	int ret;
165 
166 	if (chan->type != IIO_DISTANCE)
167 		return -EINVAL;
168 
169 	switch (mask) {
170 	case IIO_CHAN_INFO_RAW:
171 		ret = vl53l0x_read_proximity(data, chan, val);
172 		if (ret < 0)
173 			return ret;
174 
175 		return IIO_VAL_INT;
176 	case IIO_CHAN_INFO_SCALE:
177 		*val = 0;
178 		*val2 = 1000;
179 
180 		return IIO_VAL_INT_PLUS_MICRO;
181 	default:
182 		return -EINVAL;
183 	}
184 }
185 
186 static const struct iio_info vl53l0x_info = {
187 	.read_raw = vl53l0x_read_raw,
188 };
189 
190 static int vl53l0x_probe(struct i2c_client *client)
191 {
192 	struct vl53l0x_data *data;
193 	struct iio_dev *indio_dev;
194 
195 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
196 	if (!indio_dev)
197 		return -ENOMEM;
198 
199 	data = iio_priv(indio_dev);
200 	data->client = client;
201 	i2c_set_clientdata(client, indio_dev);
202 
203 	if (!i2c_check_functionality(client->adapter,
204 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK |
205 				     I2C_FUNC_SMBUS_BYTE_DATA))
206 		return -EOPNOTSUPP;
207 
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;
213 
214 	/* usage of interrupt is optional */
215 	if (client->irq) {
216 		int ret;
217 
218 		init_completion(&data->completion);
219 
220 		ret = vl53l0x_configure_irq(client, indio_dev);
221 		if (ret)
222 			return ret;
223 	}
224 
225 	return devm_iio_device_register(&client->dev, indio_dev);
226 }
227 
228 static const struct i2c_device_id vl53l0x_id[] = {
229 	{ "vl53l0x", 0 },
230 	{ }
231 };
232 MODULE_DEVICE_TABLE(i2c, vl53l0x_id);
233 
234 static const struct of_device_id st_vl53l0x_dt_match[] = {
235 	{ .compatible = "st,vl53l0x", },
236 	{ }
237 };
238 MODULE_DEVICE_TABLE(of, st_vl53l0x_dt_match);
239 
240 static struct i2c_driver vl53l0x_driver = {
241 	.driver = {
242 		.name = "vl53l0x-i2c",
243 		.of_match_table = st_vl53l0x_dt_match,
244 	},
245 	.probe_new = vl53l0x_probe,
246 	.id_table = vl53l0x_id,
247 };
248 module_i2c_driver(vl53l0x_driver);
249 
250 MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
251 MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
252 MODULE_LICENSE("GPL v2");
253