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
42struct vl53l0x_data {
43	struct i2c_client *client;
44	struct completion completion;
45};
46
47static 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
57static 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
79static 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
99static 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	unsigned long time_left;
108
109	ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1);
110	if (ret < 0)
111		return ret;
112
113	if (data->client->irq) {
114		reinit_completion(&data->completion);
115
116		time_left = wait_for_completion_timeout(&data->completion, HZ/10);
117		if (time_left == 0)
118			return -ETIMEDOUT;
119
120		vl53l0x_clear_irq(data);
121	} else {
122		do {
123			ret = i2c_smbus_read_byte_data(client,
124					       VL_REG_RESULT_RANGE_STATUS);
125			if (ret < 0)
126				return ret;
127
128			if (ret & VL_REG_RESULT_RANGE_STATUS_COMPLETE)
129				break;
130
131			usleep_range(1000, 5000);
132		} while (--tries);
133		if (!tries)
134			return -ETIMEDOUT;
135	}
136
137	ret = i2c_smbus_read_i2c_block_data(client, VL_REG_RESULT_RANGE_STATUS,
138					    12, buffer);
139	if (ret < 0)
140		return ret;
141	else if (ret != 12)
142		return -EREMOTEIO;
143
144	/* Values should be between 30~1200 in millimeters. */
145	*val = (buffer[10] << 8) + buffer[11];
146
147	return 0;
148}
149
150static const struct iio_chan_spec vl53l0x_channels[] = {
151	{
152		.type = IIO_DISTANCE,
153		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
154				      BIT(IIO_CHAN_INFO_SCALE),
155	},
156};
157
158static int vl53l0x_read_raw(struct iio_dev *indio_dev,
159			    const struct iio_chan_spec *chan,
160			    int *val, int *val2, long mask)
161{
162	struct vl53l0x_data *data = iio_priv(indio_dev);
163	int ret;
164
165	if (chan->type != IIO_DISTANCE)
166		return -EINVAL;
167
168	switch (mask) {
169	case IIO_CHAN_INFO_RAW:
170		ret = vl53l0x_read_proximity(data, chan, val);
171		if (ret < 0)
172			return ret;
173
174		return IIO_VAL_INT;
175	case IIO_CHAN_INFO_SCALE:
176		*val = 0;
177		*val2 = 1000;
178
179		return IIO_VAL_INT_PLUS_MICRO;
180	default:
181		return -EINVAL;
182	}
183}
184
185static const struct iio_info vl53l0x_info = {
186	.read_raw = vl53l0x_read_raw,
187};
188
189static int vl53l0x_probe(struct i2c_client *client)
190{
191	struct vl53l0x_data *data;
192	struct iio_dev *indio_dev;
193
194	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
195	if (!indio_dev)
196		return -ENOMEM;
197
198	data = iio_priv(indio_dev);
199	data->client = client;
200	i2c_set_clientdata(client, indio_dev);
201
202	if (!i2c_check_functionality(client->adapter,
203				     I2C_FUNC_SMBUS_READ_I2C_BLOCK |
204				     I2C_FUNC_SMBUS_BYTE_DATA))
205		return -EOPNOTSUPP;
206
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;
212
213	/* usage of interrupt is optional */
214	if (client->irq) {
215		int ret;
216
217		init_completion(&data->completion);
218
219		ret = vl53l0x_configure_irq(client, indio_dev);
220		if (ret)
221			return ret;
222	}
223
224	return devm_iio_device_register(&client->dev, indio_dev);
225}
226
227static const struct of_device_id st_vl53l0x_dt_match[] = {
228	{ .compatible = "st,vl53l0x", },
229	{ }
230};
231MODULE_DEVICE_TABLE(of, st_vl53l0x_dt_match);
232
233static struct i2c_driver vl53l0x_driver = {
234	.driver = {
235		.name = "vl53l0x-i2c",
236		.of_match_table = st_vl53l0x_dt_match,
237	},
238	.probe_new = vl53l0x_probe,
239};
240module_i2c_driver(vl53l0x_driver);
241
242MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
243MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
244MODULE_LICENSE("GPL v2");
245