| 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 <[email protected]> |
| 7 |
* Copyright (C) 2020 Ivan Drobyshevskyi <[email protected]> |
| 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 |
#include <linux/unaligned.h> |
| 24 |
|
| 25 |
#include <linux/iio/iio.h> |
| 26 |
#include <linux/iio/buffer.h> |
| 27 |
#include <linux/iio/trigger.h> |
| 28 |
#include <linux/iio/trigger_consumer.h> |
| 29 |
#include <linux/iio/triggered_buffer.h> |
| 30 |
|
| 31 |
#define VL_REG_SYSRANGE_START 0x00 |
| 32 |
|
| 33 |
#define VL_REG_SYSRANGE_MODE_MASK GENMASK(3, 0) |
| 34 |
#define VL_REG_SYSRANGE_MODE_SINGLESHOT 0x00 |
| 35 |
#define VL_REG_SYSRANGE_MODE_START_STOP BIT(0) |
| 36 |
#define VL_REG_SYSRANGE_MODE_BACKTOBACK BIT(1) |
| 37 |
#define VL_REG_SYSRANGE_MODE_TIMED BIT(2) |
| 38 |
#define VL_REG_SYSRANGE_MODE_HISTOGRAM BIT(3) |
| 39 |
|
| 40 |
#define VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0A |
| 41 |
#define VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY BIT(2) |
| 42 |
|
| 43 |
#define VL_REG_SYSTEM_INTERRUPT_CLEAR 0x0B |
| 44 |
|
| 45 |
#define VL_REG_RESULT_INT_STATUS 0x13 |
| 46 |
#define VL_REG_RESULT_RANGE_STATUS 0x14 |
| 47 |
#define VL_REG_IDENTIFICATION_MODEL_ID 0xC0 |
| 48 |
#define VL_REG_RESULT_RANGE_STATUS_COMPLETE BIT(0) |
| 49 |
|
| 50 |
#define VL53L0X_MODEL_ID_VAL 0xEE |
| 51 |
#define VL53L0X_CONTINUOUS_MODE 0x02 |
| 52 |
#define VL53L0X_SINGLE_MODE 0x01 |
| 53 |
|
| 54 |
struct vl53l0x_data { |
| 55 |
struct i2c_client *client; |
| 56 |
struct completion completion; |
| 57 |
struct regulator *vdd_supply; |
| 58 |
struct gpio_desc *reset_gpio; |
| 59 |
struct iio_trigger *trig; |
| 60 |
}; |
| 61 |
|
| 62 |
static int vl53l0x_clear_irq(struct vl53l0x_data *data) |
| 63 |
{ int ret; |
| 64 |
|
| 65 |
ret = i2c_smbus_write_byte_data(data->client, |
| 66 |
VL_REG_SYSTEM_INTERRUPT_CLEAR, 1); |
| 67 |
if (ret < 0) { |
| 68 |
dev_err(&data->client->dev, "failed to clear irq: %d\n", ret); |
| 69 |
return -EINVAL; |
| 70 |
} |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
static irqreturn_t vl53l0x_trigger_handler(int irq, void *priv) |
| 76 |
{ |
| 77 |
struct iio_poll_func *pf = priv; |
| 78 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 79 |
struct vl53l0x_data *data = iio_priv(indio_dev); |
| 80 |
u8 buffer[12]; |
| 81 |
int ret; |
| 82 |
struct { |
| 83 |
u16 chan; |
| 84 |
aligned_s64 timestamp; |
| 85 |
} scan = { }; |
| 86 |
|
| 87 |
ret = i2c_smbus_read_i2c_block_data(data->client, |
| 88 |
VL_REG_RESULT_RANGE_STATUS, |
| 89 |
sizeof(buffer), buffer); |
| 90 |
if (ret < 0) |
| 91 |
return ret; |
| 92 |
else if (ret != 12) |
| 93 |
return -EREMOTEIO; |
| 94 |
|
| 95 |
scan.chan = get_unaligned_be16(&buffer[10]); |
| 96 |
iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), |
| 97 |
iio_get_time_ns(indio_dev)); |
| 98 |
|
| 99 |
iio_trigger_notify_done(indio_dev->trig); |
| 100 |
vl53l0x_clear_irq(data); |
| 101 |
|
| 102 |
return IRQ_HANDLED; |
| 103 |
} |
| 104 |
|
| 105 |
static irqreturn_t vl53l0x_threaded_irq(int irq, void *priv) |
| 106 |
{ |
| 107 |
struct iio_dev *indio_dev = priv; |
| 108 |
struct vl53l0x_data *data = iio_priv(indio_dev); |
| 109 |
|
| 110 |
if (iio_buffer_enabled(indio_dev)) |
| 111 |
iio_trigger_poll_nested(indio_dev->trig); |
| 112 |
else |
| 113 |
complete(&data->completion); |
| 114 |
|
| 115 |
return IRQ_HANDLED; |
| 116 |
} |
| 117 |
|
| 118 |
static int vl53l0x_configure_irq(struct i2c_client *client, |
| 119 |
struct iio_dev *indio_dev) |
| 120 |
{ |
| 121 |
int irq_flags = irq_get_trigger_type(client->irq); |
| 122 |
struct vl53l0x_data *data = iio_priv(indio_dev); |
| 123 |
int ret; |
| 124 |
|
| 125 |
if (!irq_flags) |
| 126 |
irq_flags = IRQF_TRIGGER_FALLING; |
| 127 |
|
| 128 |
ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 129 |
NULL, vl53l0x_threaded_irq, |
| 130 |
irq_flags | IRQF_ONESHOT, indio_dev->name, indio_dev); |
| 131 |
if (ret) { |
| 132 |
dev_err(&client->dev, "devm_request_irq error: %d\n", ret); |
| 133 |
return ret; |
| 134 |
} |
| 135 |
|
| 136 |
ret = i2c_smbus_write_byte_data(data->client, |
| 137 |
VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO, |
| 138 |
VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); |
| 139 |
if (ret < 0) |
| 140 |
dev_err(&client->dev, "failed to configure IRQ: %d\n", ret); |
| 141 |
|
| 142 |
return ret; |
| 143 |
} |
| 144 |
|
| 145 |
static int vl53l0x_read_proximity(struct vl53l0x_data *data, |
| 146 |
const struct iio_chan_spec *chan, |
| 147 |
int *val) |
| 148 |
{ |
| 149 |
struct i2c_client *client = data->client; |
| 150 |
u16 tries = 20; |
| 151 |
u8 buffer[12]; |
| 152 |
int ret; |
| 153 |
unsigned long time_left; |
| 154 |
|
| 155 |
ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1); |
| 156 |
if (ret < 0) |
| 157 |
return ret; |
| 158 |
|
| 159 |
if (data->client->irq) { |
| 160 |
reinit_completion(&data->completion); |
| 161 |
|
| 162 |
time_left = wait_for_completion_timeout(&data->completion, HZ/10); |
| 163 |
if (time_left == 0) |
| 164 |
return -ETIMEDOUT; |
| 165 |
|
| 166 |
ret = vl53l0x_clear_irq(data); |
| 167 |
if (ret < 0) |
| 168 |
return ret; |
| 169 |
} else { |
| 170 |
do { |
| 171 |
ret = i2c_smbus_read_byte_data(client, |
| 172 |
VL_REG_RESULT_RANGE_STATUS); |
| 173 |
if (ret < 0) |
| 174 |
return ret; |
| 175 |
|
| 176 |
if (ret & VL_REG_RESULT_RANGE_STATUS_COMPLETE) |
| 177 |
break; |
| 178 |
|
| 179 |
usleep_range(1000, 5000); |
| 180 |
} while (--tries); |
| 181 |
if (!tries) |
| 182 |
return -ETIMEDOUT; |
| 183 |
} |
| 184 |
|
| 185 |
ret = i2c_smbus_read_i2c_block_data(client, VL_REG_RESULT_RANGE_STATUS, |
| 186 |
12, buffer); |
| 187 |
if (ret < 0) |
| 188 |
return ret; |
| 189 |
else if (ret != 12) |
| 190 |
return -EREMOTEIO; |
| 191 |
|
| 192 |
/* Values should be between 30~1200 in millimeters. */ |
| 193 |
*val = get_unaligned_be16(&buffer[10]); |
| 194 |
|
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
static const struct iio_chan_spec vl53l0x_channels[] = { |
| 199 |
{ |
| 200 |
.type = IIO_DISTANCE, |
| 201 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 202 |
BIT(IIO_CHAN_INFO_SCALE), |
| 203 |
.scan_index = 0, |
| 204 |
.scan_type = { |
| 205 |
.sign = 'u', |
| 206 |
.realbits = 12, |
| 207 |
.storagebits = 16, |
| 208 |
}, |
| 209 |
}, |
| 210 |
IIO_CHAN_SOFT_TIMESTAMP(1), |
| 211 |
}; |
| 212 |
|
| 213 |
static int vl53l0x_read_raw(struct iio_dev *indio_dev, |
| 214 |
const struct iio_chan_spec *chan, |
| 215 |
int *val, int *val2, long mask) |
| 216 |
{ |
| 217 |
struct vl53l0x_data *data = iio_priv(indio_dev); |
| 218 |
int ret; |
| 219 |
|
| 220 |
if (chan->type != IIO_DISTANCE) |
| 221 |
return -EINVAL; |
| 222 |
|
| 223 |
switch (mask) { |
| 224 |
case IIO_CHAN_INFO_RAW: |
| 225 |
ret = vl53l0x_read_proximity(data, chan, val); |
| 226 |
if (ret < 0) |
| 227 |
return ret; |
| 228 |
|
| 229 |
return IIO_VAL_INT; |
| 230 |
case IIO_CHAN_INFO_SCALE: |
| 231 |
*val = 0; |
| 232 |
*val2 = 1000; |
| 233 |
|
| 234 |
return IIO_VAL_INT_PLUS_MICRO; |
| 235 |
default: |
| 236 |
return -EINVAL; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
static int vl53l0x_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig) |
| 241 |
{ |
| 242 |
struct vl53l0x_data *data = iio_priv(indio_dev); |
| 243 |
|
| 244 |
return data->trig == trig ? 0 : -EINVAL; |
| 245 |
} |
| 246 |
|
| 247 |
static const struct iio_info vl53l0x_info = { |
| 248 |
.read_raw = vl53l0x_read_raw, |
| 249 |
.validate_trigger = vl53l0x_validate_trigger, |
| 250 |
}; |
| 251 |
|
| 252 |
static void vl53l0x_power_off(void *_data) |
| 253 |
{ |
| 254 |
struct vl53l0x_data *data = _data; |
| 255 |
|
| 256 |
gpiod_set_value_cansleep(data->reset_gpio, 1); |
| 257 |
|
| 258 |
regulator_disable(data->vdd_supply); |
| 259 |
} |
| 260 |
|
| 261 |
static int vl53l0x_power_on(struct vl53l0x_data *data) |
| 262 |
{ |
| 263 |
int ret; |
| 264 |
|
| 265 |
ret = regulator_enable(data->vdd_supply); |
| 266 |
if (ret) |
| 267 |
return ret; |
| 268 |
|
| 269 |
gpiod_set_value_cansleep(data->reset_gpio, 0); |
| 270 |
|
| 271 |
usleep_range(3200, 5000); |
| 272 |
|
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
static int vl53l0x_buffer_postenable(struct iio_dev *indio_dev) |
| 277 |
{ |
| 278 |
struct vl53l0x_data *data = iio_priv(indio_dev); |
| 279 |
|
| 280 |
return i2c_smbus_write_byte_data(data->client, VL_REG_SYSRANGE_START, |
| 281 |
VL53L0X_CONTINUOUS_MODE); |
| 282 |
} |
| 283 |
|
| 284 |
static int vl53l0x_buffer_postdisable(struct iio_dev *indio_dev) |
| 285 |
{ |
| 286 |
struct vl53l0x_data *data = iio_priv(indio_dev); |
| 287 |
int ret; |
| 288 |
|
| 289 |
ret = i2c_smbus_write_byte_data(data->client, VL_REG_SYSRANGE_START, |
| 290 |
VL53L0X_SINGLE_MODE); |
| 291 |
if (ret < 0) |
| 292 |
return ret; |
| 293 |
|
| 294 |
/* Let the ongoing reading finish */ |
| 295 |
reinit_completion(&data->completion); |
| 296 |
wait_for_completion_timeout(&data->completion, HZ / 10); |
| 297 |
|
| 298 |
return vl53l0x_clear_irq(data); |
| 299 |
} |
| 300 |
|
| 301 |
static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = { |
| 302 |
.postenable = &vl53l0x_buffer_postenable, |
| 303 |
.postdisable = &vl53l0x_buffer_postdisable, |
| 304 |
}; |
| 305 |
|
| 306 |
static const struct iio_trigger_ops vl53l0x_trigger_ops = { |
| 307 |
.validate_device = iio_trigger_validate_own_device, |
| 308 |
}; |
| 309 |
|
| 310 |
static int vl53l0x_probe(struct i2c_client *client) |
| 311 |
{ |
| 312 |
struct vl53l0x_data *data; |
| 313 |
struct iio_dev *indio_dev; |
| 314 |
int ret; |
| 315 |
|
| 316 |
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 317 |
if (!indio_dev) |
| 318 |
return -ENOMEM; |
| 319 |
|
| 320 |
data = iio_priv(indio_dev); |
| 321 |
data->client = client; |
| 322 |
i2c_set_clientdata(client, indio_dev); |
| 323 |
|
| 324 |
if (!i2c_check_functionality(client->adapter, |
| 325 |
I2C_FUNC_SMBUS_READ_I2C_BLOCK | |
| 326 |
I2C_FUNC_SMBUS_BYTE_DATA)) |
| 327 |
return -EOPNOTSUPP; |
| 328 |
|
| 329 |
ret = i2c_smbus_read_byte_data(data->client, VL_REG_IDENTIFICATION_MODEL_ID); |
| 330 |
if (ret < 0) |
| 331 |
return -EINVAL; |
| 332 |
|
| 333 |
if (ret != VL53L0X_MODEL_ID_VAL) |
| 334 |
dev_info(&client->dev, "Unknown model id: 0x%x", ret); |
| 335 |
|
| 336 |
data->vdd_supply = devm_regulator_get(&client->dev, "vdd"); |
| 337 |
if (IS_ERR(data->vdd_supply)) |
| 338 |
return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply), |
| 339 |
"Unable to get VDD regulator\n"); |
| 340 |
|
| 341 |
data->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH); |
| 342 |
if (IS_ERR(data->reset_gpio)) |
| 343 |
return dev_err_probe(&client->dev, PTR_ERR(data->reset_gpio), |
| 344 |
"Cannot get reset GPIO\n"); |
| 345 |
|
| 346 |
ret = vl53l0x_power_on(data); |
| 347 |
if (ret) |
| 348 |
return dev_err_probe(&client->dev, ret, |
| 349 |
"Failed to power on the chip\n"); |
| 350 |
|
| 351 |
ret = devm_add_action_or_reset(&client->dev, vl53l0x_power_off, data); |
| 352 |
if (ret) |
| 353 |
return ret; |
| 354 |
|
| 355 |
indio_dev->name = "vl53l0x"; |
| 356 |
indio_dev->info = &vl53l0x_info; |
| 357 |
indio_dev->channels = vl53l0x_channels; |
| 358 |
indio_dev->num_channels = ARRAY_SIZE(vl53l0x_channels); |
| 359 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 360 |
|
| 361 |
/* usage of interrupt is optional */ |
| 362 |
if (client->irq) { |
| 363 |
init_completion(&data->completion); |
| 364 |
|
| 365 |
data->trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d", |
| 366 |
indio_dev->name, |
| 367 |
iio_device_id(indio_dev)); |
| 368 |
if (!data->trig) |
| 369 |
return -ENOMEM; |
| 370 |
|
| 371 |
data->trig->ops = &vl53l0x_trigger_ops; |
| 372 |
iio_trigger_set_drvdata(data->trig, indio_dev); |
| 373 |
ret = devm_iio_trigger_register(&client->dev, data->trig); |
| 374 |
if (ret) |
| 375 |
return ret; |
| 376 |
|
| 377 |
indio_dev->trig = iio_trigger_get(data->trig); |
| 378 |
|
| 379 |
ret = vl53l0x_configure_irq(client, indio_dev); |
| 380 |
if (ret) |
| 381 |
return ret; |
| 382 |
|
| 383 |
ret = devm_iio_triggered_buffer_setup(&client->dev, |
| 384 |
indio_dev, |
| 385 |
NULL, |
| 386 |
&vl53l0x_trigger_handler, |
| 387 |
&iio_triggered_buffer_setup_ops); |
| 388 |
if (ret) |
| 389 |
return ret; |
| 390 |
} |
| 391 |
|
| 392 |
return devm_iio_device_register(&client->dev, indio_dev); |
| 393 |
} |
| 394 |
|
| 395 |
static const struct i2c_device_id vl53l0x_id[] = { |
| 396 |
{ "vl53l0x" }, |
| 397 |
{ } |
| 398 |
}; |
| 399 |
MODULE_DEVICE_TABLE(i2c, vl53l0x_id); |
| 400 |
|
| 401 |
static const struct of_device_id st_vl53l0x_dt_match[] = { |
| 402 |
{ .compatible = "st,vl53l0x", }, |
| 403 |
{ } |
| 404 |
}; |
| 405 |
MODULE_DEVICE_TABLE(of, st_vl53l0x_dt_match); |
| 406 |
|
| 407 |
static struct i2c_driver vl53l0x_driver = { |
| 408 |
.driver = { |
| 409 |
.name = "vl53l0x-i2c", |
| 410 |
.of_match_table = st_vl53l0x_dt_match, |
| 411 |
}, |
| 412 |
.probe = vl53l0x_probe, |
| 413 |
.id_table = vl53l0x_id, |
| 414 |
}; |
| 415 |
module_i2c_driver(vl53l0x_driver); |
| 416 |
|
| 417 |
MODULE_AUTHOR("Song Qiang <[email protected]>"); |
| 418 |
MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver"); |
| 419 |
MODULE_LICENSE("GPL v2"); |