blob: c7c4d33d340f0adfa701464b0569728156f3b2f5 [file] [log] [blame]
Song Qiang6eb17c62018-09-18 16:24:21 +08001// 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>
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +03007 * Copyright (C) 2020 Ivan Drobyshevskyi <drobyshevskyi@gmail.com>
Song Qiang6eb17c62018-09-18 16:24:21 +08008 *
9 * Datasheet available at
10 * <https://www.st.com/resource/en/datasheet/vl53l0x.pdf>
11 *
12 * Default 7-bit i2c slave address 0x29.
13 *
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +030014 * TODO: FIFO buffer, continuous mode, range selection, sensor ID check.
Song Qiang6eb17c62018-09-18 16:24:21 +080015 */
16
17#include <linux/delay.h>
Markuss Brokse8941aa2022-05-23 20:53:43 +030018#include <linux/gpio/consumer.h>
Song Qiang6eb17c62018-09-18 16:24:21 +080019#include <linux/i2c.h>
Markuss Broks76d1eb02022-05-23 20:53:41 +030020#include <linux/irq.h>
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +030021#include <linux/interrupt.h>
Song Qiang6eb17c62018-09-18 16:24:21 +080022#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
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +030035#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
Song Qiang6eb17c62018-09-18 16:24:21 +080040#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
44struct vl53l0x_data {
45 struct i2c_client *client;
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +030046 struct completion completion;
Markuss Broksd3d6dba2022-05-23 20:53:42 +030047 struct regulator *vdd_supply;
Markuss Brokse8941aa2022-05-23 20:53:43 +030048 struct gpio_desc *reset_gpio;
Song Qiang6eb17c62018-09-18 16:24:21 +080049};
50
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +030051static 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
61static int vl53l0x_configure_irq(struct i2c_client *client,
62 struct iio_dev *indio_dev)
63{
Markuss Broks76d1eb02022-05-23 20:53:41 +030064 int irq_flags = irq_get_trigger_type(client->irq);
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +030065 struct vl53l0x_data *data = iio_priv(indio_dev);
66 int ret;
67
Markuss Broks76d1eb02022-05-23 20:53:41 +030068 if (!irq_flags)
69 irq_flags = IRQF_TRIGGER_FALLING;
70
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +030071 ret = devm_request_irq(&client->dev, client->irq, vl53l0x_handle_irq,
Markuss Broks76d1eb02022-05-23 20:53:41 +030072 irq_flags, indio_dev->name, indio_dev);
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +030073 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
87static 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
Song Qiang6eb17c62018-09-18 16:24:21 +0800107static 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;
Miaoqian Lin50f29592022-04-12 06:42:09 +0000115 unsigned long time_left;
Song Qiang6eb17c62018-09-18 16:24:21 +0800116
117 ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1);
118 if (ret < 0)
119 return ret;
120
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +0300121 if (data->client->irq) {
122 reinit_completion(&data->completion);
123
Miaoqian Lin50f29592022-04-12 06:42:09 +0000124 time_left = wait_for_completion_timeout(&data->completion, HZ/10);
125 if (time_left == 0)
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +0300126 return -ETIMEDOUT;
Song Qiang6eb17c62018-09-18 16:24:21 +0800127
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +0300128 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;
Song Qiang6eb17c62018-09-18 16:24:21 +0800135
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +0300136 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 }
Song Qiang6eb17c62018-09-18 16:24:21 +0800144
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
158static 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
166static 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
193static const struct iio_info vl53l0x_info = {
194 .read_raw = vl53l0x_read_raw,
195};
196
Markuss Broksd3d6dba2022-05-23 20:53:42 +0300197static void vl53l0x_power_off(void *_data)
198{
199 struct vl53l0x_data *data = _data;
200
Markuss Brokse8941aa2022-05-23 20:53:43 +0300201 gpiod_set_value_cansleep(data->reset_gpio, 1);
202
Markuss Broksd3d6dba2022-05-23 20:53:42 +0300203 regulator_disable(data->vdd_supply);
204}
205
206static 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
Markuss Brokse8941aa2022-05-23 20:53:43 +0300214 gpiod_set_value_cansleep(data->reset_gpio, 0);
215
Markuss Broksd3d6dba2022-05-23 20:53:42 +0300216 usleep_range(3200, 5000);
217
218 return 0;
219}
220
Song Qiang6eb17c62018-09-18 16:24:21 +0800221static int vl53l0x_probe(struct i2c_client *client)
222{
223 struct vl53l0x_data *data;
224 struct iio_dev *indio_dev;
Markuss Broksd3d6dba2022-05-23 20:53:42 +0300225 int error;
Song Qiang6eb17c62018-09-18 16:24:21 +0800226
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
Luca Weiss82c89362022-06-14 13:20:50 +0200240 data->vdd_supply = devm_regulator_get(&client->dev, "vdd");
Markuss Broksd3d6dba2022-05-23 20:53:42 +0300241 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
Markuss Brokse8941aa2022-05-23 20:53:43 +0300245 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
Markuss Broksd3d6dba2022-05-23 20:53:42 +0300250 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
Song Qiang6eb17c62018-09-18 16:24:21 +0800260 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
Ivan Drobyshevskyi3cef2e32020-09-16 10:44:58 +0300266 /* 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
Song Qiang6eb17c62018-09-18 16:24:21 +0800277 return devm_iio_device_register(&client->dev, indio_dev);
278}
279
Vaishnav M A8bb97ba2020-10-19 01:21:02 +0530280static const struct i2c_device_id vl53l0x_id[] = {
Jonathan Cameron86eae302021-12-30 17:49:10 +0000281 { "vl53l0x", 0 },
Vaishnav M A8bb97ba2020-10-19 01:21:02 +0530282 { }
283};
284MODULE_DEVICE_TABLE(i2c, vl53l0x_id);
285
Song Qiang6eb17c62018-09-18 16:24:21 +0800286static const struct of_device_id st_vl53l0x_dt_match[] = {
287 { .compatible = "st,vl53l0x", },
288 { }
289};
290MODULE_DEVICE_TABLE(of, st_vl53l0x_dt_match);
291
292static 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,
Vaishnav M A8bb97ba2020-10-19 01:21:02 +0530298 .id_table = vl53l0x_id,
Song Qiang6eb17c62018-09-18 16:24:21 +0800299};
300module_i2c_driver(vl53l0x_driver);
301
302MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
303MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
304MODULE_LICENSE("GPL v2");