0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/delay.h>
0018 #include <linux/gpio/consumer.h>
0019 #include <linux/i2c.h>
0020 #include <linux/irq.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/module.h>
0023
0024 #include <linux/iio/iio.h>
0025
0026 #define VL_REG_SYSRANGE_START 0x00
0027
0028 #define VL_REG_SYSRANGE_MODE_MASK GENMASK(3, 0)
0029 #define VL_REG_SYSRANGE_MODE_SINGLESHOT 0x00
0030 #define VL_REG_SYSRANGE_MODE_START_STOP BIT(0)
0031 #define VL_REG_SYSRANGE_MODE_BACKTOBACK BIT(1)
0032 #define VL_REG_SYSRANGE_MODE_TIMED BIT(2)
0033 #define VL_REG_SYSRANGE_MODE_HISTOGRAM BIT(3)
0034
0035 #define VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0A
0036 #define VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY BIT(2)
0037
0038 #define VL_REG_SYSTEM_INTERRUPT_CLEAR 0x0B
0039
0040 #define VL_REG_RESULT_INT_STATUS 0x13
0041 #define VL_REG_RESULT_RANGE_STATUS 0x14
0042 #define VL_REG_RESULT_RANGE_STATUS_COMPLETE BIT(0)
0043
0044 struct vl53l0x_data {
0045 struct i2c_client *client;
0046 struct completion completion;
0047 struct regulator *vdd_supply;
0048 struct gpio_desc *reset_gpio;
0049 };
0050
0051 static irqreturn_t vl53l0x_handle_irq(int irq, void *priv)
0052 {
0053 struct iio_dev *indio_dev = priv;
0054 struct vl53l0x_data *data = iio_priv(indio_dev);
0055
0056 complete(&data->completion);
0057
0058 return IRQ_HANDLED;
0059 }
0060
0061 static int vl53l0x_configure_irq(struct i2c_client *client,
0062 struct iio_dev *indio_dev)
0063 {
0064 int irq_flags = irq_get_trigger_type(client->irq);
0065 struct vl53l0x_data *data = iio_priv(indio_dev);
0066 int ret;
0067
0068 if (!irq_flags)
0069 irq_flags = IRQF_TRIGGER_FALLING;
0070
0071 ret = devm_request_irq(&client->dev, client->irq, vl53l0x_handle_irq,
0072 irq_flags, indio_dev->name, indio_dev);
0073 if (ret) {
0074 dev_err(&client->dev, "devm_request_irq error: %d\n", ret);
0075 return ret;
0076 }
0077
0078 ret = i2c_smbus_write_byte_data(data->client,
0079 VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO,
0080 VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
0081 if (ret < 0)
0082 dev_err(&client->dev, "failed to configure IRQ: %d\n", ret);
0083
0084 return ret;
0085 }
0086
0087 static void vl53l0x_clear_irq(struct vl53l0x_data *data)
0088 {
0089 struct device *dev = &data->client->dev;
0090 int ret;
0091
0092 ret = i2c_smbus_write_byte_data(data->client,
0093 VL_REG_SYSTEM_INTERRUPT_CLEAR, 1);
0094 if (ret < 0)
0095 dev_err(dev, "failed to clear error irq: %d\n", ret);
0096
0097 ret = i2c_smbus_write_byte_data(data->client,
0098 VL_REG_SYSTEM_INTERRUPT_CLEAR, 0);
0099 if (ret < 0)
0100 dev_err(dev, "failed to clear range irq: %d\n", ret);
0101
0102 ret = i2c_smbus_read_byte_data(data->client, VL_REG_RESULT_INT_STATUS);
0103 if (ret < 0 || ret & 0x07)
0104 dev_err(dev, "failed to clear irq: %d\n", ret);
0105 }
0106
0107 static int vl53l0x_read_proximity(struct vl53l0x_data *data,
0108 const struct iio_chan_spec *chan,
0109 int *val)
0110 {
0111 struct i2c_client *client = data->client;
0112 u16 tries = 20;
0113 u8 buffer[12];
0114 int ret;
0115 unsigned long time_left;
0116
0117 ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1);
0118 if (ret < 0)
0119 return ret;
0120
0121 if (data->client->irq) {
0122 reinit_completion(&data->completion);
0123
0124 time_left = wait_for_completion_timeout(&data->completion, HZ/10);
0125 if (time_left == 0)
0126 return -ETIMEDOUT;
0127
0128 vl53l0x_clear_irq(data);
0129 } else {
0130 do {
0131 ret = i2c_smbus_read_byte_data(client,
0132 VL_REG_RESULT_RANGE_STATUS);
0133 if (ret < 0)
0134 return ret;
0135
0136 if (ret & VL_REG_RESULT_RANGE_STATUS_COMPLETE)
0137 break;
0138
0139 usleep_range(1000, 5000);
0140 } while (--tries);
0141 if (!tries)
0142 return -ETIMEDOUT;
0143 }
0144
0145 ret = i2c_smbus_read_i2c_block_data(client, VL_REG_RESULT_RANGE_STATUS,
0146 12, buffer);
0147 if (ret < 0)
0148 return ret;
0149 else if (ret != 12)
0150 return -EREMOTEIO;
0151
0152
0153 *val = (buffer[10] << 8) + buffer[11];
0154
0155 return 0;
0156 }
0157
0158 static const struct iio_chan_spec vl53l0x_channels[] = {
0159 {
0160 .type = IIO_DISTANCE,
0161 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0162 BIT(IIO_CHAN_INFO_SCALE),
0163 },
0164 };
0165
0166 static int vl53l0x_read_raw(struct iio_dev *indio_dev,
0167 const struct iio_chan_spec *chan,
0168 int *val, int *val2, long mask)
0169 {
0170 struct vl53l0x_data *data = iio_priv(indio_dev);
0171 int ret;
0172
0173 if (chan->type != IIO_DISTANCE)
0174 return -EINVAL;
0175
0176 switch (mask) {
0177 case IIO_CHAN_INFO_RAW:
0178 ret = vl53l0x_read_proximity(data, chan, val);
0179 if (ret < 0)
0180 return ret;
0181
0182 return IIO_VAL_INT;
0183 case IIO_CHAN_INFO_SCALE:
0184 *val = 0;
0185 *val2 = 1000;
0186
0187 return IIO_VAL_INT_PLUS_MICRO;
0188 default:
0189 return -EINVAL;
0190 }
0191 }
0192
0193 static const struct iio_info vl53l0x_info = {
0194 .read_raw = vl53l0x_read_raw,
0195 };
0196
0197 static void vl53l0x_power_off(void *_data)
0198 {
0199 struct vl53l0x_data *data = _data;
0200
0201 gpiod_set_value_cansleep(data->reset_gpio, 1);
0202
0203 regulator_disable(data->vdd_supply);
0204 }
0205
0206 static int vl53l0x_power_on(struct vl53l0x_data *data)
0207 {
0208 int ret;
0209
0210 ret = regulator_enable(data->vdd_supply);
0211 if (ret)
0212 return ret;
0213
0214 gpiod_set_value_cansleep(data->reset_gpio, 0);
0215
0216 usleep_range(3200, 5000);
0217
0218 return 0;
0219 }
0220
0221 static int vl53l0x_probe(struct i2c_client *client)
0222 {
0223 struct vl53l0x_data *data;
0224 struct iio_dev *indio_dev;
0225 int error;
0226
0227 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
0228 if (!indio_dev)
0229 return -ENOMEM;
0230
0231 data = iio_priv(indio_dev);
0232 data->client = client;
0233 i2c_set_clientdata(client, indio_dev);
0234
0235 if (!i2c_check_functionality(client->adapter,
0236 I2C_FUNC_SMBUS_READ_I2C_BLOCK |
0237 I2C_FUNC_SMBUS_BYTE_DATA))
0238 return -EOPNOTSUPP;
0239
0240 data->vdd_supply = devm_regulator_get(&client->dev, "vdd");
0241 if (IS_ERR(data->vdd_supply))
0242 return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply),
0243 "Unable to get VDD regulator\n");
0244
0245 data->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
0246 if (IS_ERR(data->reset_gpio))
0247 return dev_err_probe(&client->dev, PTR_ERR(data->reset_gpio),
0248 "Cannot get reset GPIO\n");
0249
0250 error = vl53l0x_power_on(data);
0251 if (error)
0252 return dev_err_probe(&client->dev, error,
0253 "Failed to power on the chip\n");
0254
0255 error = devm_add_action_or_reset(&client->dev, vl53l0x_power_off, data);
0256 if (error)
0257 return dev_err_probe(&client->dev, error,
0258 "Failed to install poweroff action\n");
0259
0260 indio_dev->name = "vl53l0x";
0261 indio_dev->info = &vl53l0x_info;
0262 indio_dev->channels = vl53l0x_channels;
0263 indio_dev->num_channels = ARRAY_SIZE(vl53l0x_channels);
0264 indio_dev->modes = INDIO_DIRECT_MODE;
0265
0266
0267 if (client->irq) {
0268 int ret;
0269
0270 init_completion(&data->completion);
0271
0272 ret = vl53l0x_configure_irq(client, indio_dev);
0273 if (ret)
0274 return ret;
0275 }
0276
0277 return devm_iio_device_register(&client->dev, indio_dev);
0278 }
0279
0280 static const struct i2c_device_id vl53l0x_id[] = {
0281 { "vl53l0x", 0 },
0282 { }
0283 };
0284 MODULE_DEVICE_TABLE(i2c, vl53l0x_id);
0285
0286 static const struct of_device_id st_vl53l0x_dt_match[] = {
0287 { .compatible = "st,vl53l0x", },
0288 { }
0289 };
0290 MODULE_DEVICE_TABLE(of, st_vl53l0x_dt_match);
0291
0292 static struct i2c_driver vl53l0x_driver = {
0293 .driver = {
0294 .name = "vl53l0x-i2c",
0295 .of_match_table = st_vl53l0x_dt_match,
0296 },
0297 .probe_new = vl53l0x_probe,
0298 .id_table = vl53l0x_id,
0299 };
0300 module_i2c_driver(vl53l0x_driver);
0301
0302 MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
0303 MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
0304 MODULE_LICENSE("GPL v2");