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/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 
24 #include <linux/iio/iio.h>
25 
26 #define VL_REG_SYSRANGE_START				0x00
27 
28 #define VL_REG_SYSRANGE_MODE_MASK			GENMASK(3, 0)
29 #define VL_REG_SYSRANGE_MODE_SINGLESHOT			0x00
30 #define VL_REG_SYSRANGE_MODE_START_STOP			BIT(0)
31 #define VL_REG_SYSRANGE_MODE_BACKTOBACK			BIT(1)
32 #define VL_REG_SYSRANGE_MODE_TIMED			BIT(2)
33 #define VL_REG_SYSRANGE_MODE_HISTOGRAM			BIT(3)
34 
35 #define VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO		0x0A
36 #define VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY	BIT(2)
37 
38 #define VL_REG_SYSTEM_INTERRUPT_CLEAR			0x0B
39 
40 #define VL_REG_RESULT_INT_STATUS			0x13
41 #define VL_REG_RESULT_RANGE_STATUS			0x14
42 #define VL_REG_RESULT_RANGE_STATUS_COMPLETE		BIT(0)
43 
44 struct vl53l0x_data {
45 	struct i2c_client *client;
46 	struct completion completion;
47 	struct regulator *vdd_supply;
48 	struct gpio_desc *reset_gpio;
49 };
50 
51 static irqreturn_t vl53l0x_handle_irq(int irq, void *priv)
52 {
53 	struct iio_dev *indio_dev = priv;
54 	struct vl53l0x_data *data = iio_priv(indio_dev);
55 
56 	complete(&data->completion);
57 
58 	return IRQ_HANDLED;
59 }
60 
61 static int vl53l0x_configure_irq(struct i2c_client *client,
62 				 struct iio_dev *indio_dev)
63 {
64 	int irq_flags = irq_get_trigger_type(client->irq);
65 	struct vl53l0x_data *data = iio_priv(indio_dev);
66 	int ret;
67 
68 	if (!irq_flags)
69 		irq_flags = IRQF_TRIGGER_FALLING;
70 
71 	ret = devm_request_irq(&client->dev, client->irq, vl53l0x_handle_irq,
72 			irq_flags, indio_dev->name, indio_dev);
73 	if (ret) {
74 		dev_err(&client->dev, "devm_request_irq error: %d\n", ret);
75 		return ret;
76 	}
77 
78 	ret = i2c_smbus_write_byte_data(data->client,
79 			VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO,
80 			VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
81 	if (ret < 0)
82 		dev_err(&client->dev, "failed to configure IRQ: %d\n", ret);
83 
84 	return ret;
85 }
86 
87 static void vl53l0x_clear_irq(struct vl53l0x_data *data)
88 {
89 	struct device *dev = &data->client->dev;
90 	int ret;
91 
92 	ret = i2c_smbus_write_byte_data(data->client,
93 					VL_REG_SYSTEM_INTERRUPT_CLEAR, 1);
94 	if (ret < 0)
95 		dev_err(dev, "failed to clear error irq: %d\n", ret);
96 
97 	ret = i2c_smbus_write_byte_data(data->client,
98 					VL_REG_SYSTEM_INTERRUPT_CLEAR, 0);
99 	if (ret < 0)
100 		dev_err(dev, "failed to clear range irq: %d\n", ret);
101 
102 	ret = i2c_smbus_read_byte_data(data->client, VL_REG_RESULT_INT_STATUS);
103 	if (ret < 0 || ret & 0x07)
104 		dev_err(dev, "failed to clear irq: %d\n", ret);
105 }
106 
107 static int vl53l0x_read_proximity(struct vl53l0x_data *data,
108 				  const struct iio_chan_spec *chan,
109 				  int *val)
110 {
111 	struct i2c_client *client = data->client;
112 	u16 tries = 20;
113 	u8 buffer[12];
114 	int ret;
115 	unsigned long time_left;
116 
117 	ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1);
118 	if (ret < 0)
119 		return ret;
120 
121 	if (data->client->irq) {
122 		reinit_completion(&data->completion);
123 
124 		time_left = wait_for_completion_timeout(&data->completion, HZ/10);
125 		if (time_left == 0)
126 			return -ETIMEDOUT;
127 
128 		vl53l0x_clear_irq(data);
129 	} else {
130 		do {
131 			ret = i2c_smbus_read_byte_data(client,
132 					       VL_REG_RESULT_RANGE_STATUS);
133 			if (ret < 0)
134 				return ret;
135 
136 			if (ret & VL_REG_RESULT_RANGE_STATUS_COMPLETE)
137 				break;
138 
139 			usleep_range(1000, 5000);
140 		} while (--tries);
141 		if (!tries)
142 			return -ETIMEDOUT;
143 	}
144 
145 	ret = i2c_smbus_read_i2c_block_data(client, VL_REG_RESULT_RANGE_STATUS,
146 					    12, buffer);
147 	if (ret < 0)
148 		return ret;
149 	else if (ret != 12)
150 		return -EREMOTEIO;
151 
152 	/* Values should be between 30~1200 in millimeters. */
153 	*val = (buffer[10] << 8) + buffer[11];
154 
155 	return 0;
156 }
157 
158 static const struct iio_chan_spec vl53l0x_channels[] = {
159 	{
160 		.type = IIO_DISTANCE,
161 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
162 				      BIT(IIO_CHAN_INFO_SCALE),
163 	},
164 };
165 
166 static int vl53l0x_read_raw(struct iio_dev *indio_dev,
167 			    const struct iio_chan_spec *chan,
168 			    int *val, int *val2, long mask)
169 {
170 	struct vl53l0x_data *data = iio_priv(indio_dev);
171 	int ret;
172 
173 	if (chan->type != IIO_DISTANCE)
174 		return -EINVAL;
175 
176 	switch (mask) {
177 	case IIO_CHAN_INFO_RAW:
178 		ret = vl53l0x_read_proximity(data, chan, val);
179 		if (ret < 0)
180 			return ret;
181 
182 		return IIO_VAL_INT;
183 	case IIO_CHAN_INFO_SCALE:
184 		*val = 0;
185 		*val2 = 1000;
186 
187 		return IIO_VAL_INT_PLUS_MICRO;
188 	default:
189 		return -EINVAL;
190 	}
191 }
192 
193 static const struct iio_info vl53l0x_info = {
194 	.read_raw = vl53l0x_read_raw,
195 };
196 
197 static void vl53l0x_power_off(void *_data)
198 {
199 	struct vl53l0x_data *data = _data;
200 
201 	gpiod_set_value_cansleep(data->reset_gpio, 1);
202 
203 	regulator_disable(data->vdd_supply);
204 }
205 
206 static int vl53l0x_power_on(struct vl53l0x_data *data)
207 {
208 	int ret;
209 
210 	ret = regulator_enable(data->vdd_supply);
211 	if (ret)
212 		return ret;
213 
214 	gpiod_set_value_cansleep(data->reset_gpio, 0);
215 
216 	usleep_range(3200, 5000);
217 
218 	return 0;
219 }
220 
221 static int vl53l0x_probe(struct i2c_client *client)
222 {
223 	struct vl53l0x_data *data;
224 	struct iio_dev *indio_dev;
225 	int error;
226 
227 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
228 	if (!indio_dev)
229 		return -ENOMEM;
230 
231 	data = iio_priv(indio_dev);
232 	data->client = client;
233 	i2c_set_clientdata(client, indio_dev);
234 
235 	if (!i2c_check_functionality(client->adapter,
236 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK |
237 				     I2C_FUNC_SMBUS_BYTE_DATA))
238 		return -EOPNOTSUPP;
239 
240 	data->vdd_supply = devm_regulator_get(&client->dev, "vdd");
241 	if (IS_ERR(data->vdd_supply))
242 		return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply),
243 				     "Unable to get VDD regulator\n");
244 
245 	data->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
246 	if (IS_ERR(data->reset_gpio))
247 		return dev_err_probe(&client->dev, PTR_ERR(data->reset_gpio),
248 				     "Cannot get reset GPIO\n");
249 
250 	error = vl53l0x_power_on(data);
251 	if (error)
252 		return dev_err_probe(&client->dev, error,
253 				     "Failed to power on the chip\n");
254 
255 	error = devm_add_action_or_reset(&client->dev, vl53l0x_power_off, data);
256 	if (error)
257 		return dev_err_probe(&client->dev, error,
258 				     "Failed to install poweroff action\n");
259 
260 	indio_dev->name = "vl53l0x";
261 	indio_dev->info = &vl53l0x_info;
262 	indio_dev->channels = vl53l0x_channels;
263 	indio_dev->num_channels = ARRAY_SIZE(vl53l0x_channels);
264 	indio_dev->modes = INDIO_DIRECT_MODE;
265 
266 	/* usage of interrupt is optional */
267 	if (client->irq) {
268 		int ret;
269 
270 		init_completion(&data->completion);
271 
272 		ret = vl53l0x_configure_irq(client, indio_dev);
273 		if (ret)
274 			return ret;
275 	}
276 
277 	return devm_iio_device_register(&client->dev, indio_dev);
278 }
279 
280 static const struct i2c_device_id vl53l0x_id[] = {
281 	{ "vl53l0x", 0 },
282 	{ }
283 };
284 MODULE_DEVICE_TABLE(i2c, vl53l0x_id);
285 
286 static const struct of_device_id st_vl53l0x_dt_match[] = {
287 	{ .compatible = "st,vl53l0x", },
288 	{ }
289 };
290 MODULE_DEVICE_TABLE(of, st_vl53l0x_dt_match);
291 
292 static struct i2c_driver vl53l0x_driver = {
293 	.driver = {
294 		.name = "vl53l0x-i2c",
295 		.of_match_table = st_vl53l0x_dt_match,
296 	},
297 	.probe_new = vl53l0x_probe,
298 	.id_table = vl53l0x_id,
299 };
300 module_i2c_driver(vl53l0x_driver);
301 
302 MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
303 MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
304 MODULE_LICENSE("GPL v2");
305