vl53l0x-i2c.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include <linux/delay.h>
  17. #include <linux/i2c.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/iio/iio.h>
  21. #define VL_REG_SYSRANGE_START 0x00
  22. #define VL_REG_SYSRANGE_MODE_MASK GENMASK(3, 0)
  23. #define VL_REG_SYSRANGE_MODE_SINGLESHOT 0x00
  24. #define VL_REG_SYSRANGE_MODE_START_STOP BIT(0)
  25. #define VL_REG_SYSRANGE_MODE_BACKTOBACK BIT(1)
  26. #define VL_REG_SYSRANGE_MODE_TIMED BIT(2)
  27. #define VL_REG_SYSRANGE_MODE_HISTOGRAM BIT(3)
  28. #define VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0A
  29. #define VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY BIT(2)
  30. #define VL_REG_SYSTEM_INTERRUPT_CLEAR 0x0B
  31. #define VL_REG_RESULT_INT_STATUS 0x13
  32. #define VL_REG_RESULT_RANGE_STATUS 0x14
  33. #define VL_REG_RESULT_RANGE_STATUS_COMPLETE BIT(0)
  34. struct vl53l0x_data {
  35. struct i2c_client *client;
  36. struct completion completion;
  37. };
  38. static irqreturn_t vl53l0x_handle_irq(int irq, void *priv)
  39. {
  40. struct iio_dev *indio_dev = priv;
  41. struct vl53l0x_data *data = iio_priv(indio_dev);
  42. complete(&data->completion);
  43. return IRQ_HANDLED;
  44. }
  45. static int vl53l0x_configure_irq(struct i2c_client *client,
  46. struct iio_dev *indio_dev)
  47. {
  48. struct vl53l0x_data *data = iio_priv(indio_dev);
  49. int ret;
  50. ret = devm_request_irq(&client->dev, client->irq, vl53l0x_handle_irq,
  51. IRQF_TRIGGER_FALLING, indio_dev->name, indio_dev);
  52. if (ret) {
  53. dev_err(&client->dev, "devm_request_irq error: %d\n", ret);
  54. return ret;
  55. }
  56. ret = i2c_smbus_write_byte_data(data->client,
  57. VL_REG_SYSTEM_INTERRUPT_CONFIG_GPIO,
  58. VL_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
  59. if (ret < 0)
  60. dev_err(&client->dev, "failed to configure IRQ: %d\n", ret);
  61. return ret;
  62. }
  63. static void vl53l0x_clear_irq(struct vl53l0x_data *data)
  64. {
  65. struct device *dev = &data->client->dev;
  66. int ret;
  67. ret = i2c_smbus_write_byte_data(data->client,
  68. VL_REG_SYSTEM_INTERRUPT_CLEAR, 1);
  69. if (ret < 0)
  70. dev_err(dev, "failed to clear error irq: %d\n", ret);
  71. ret = i2c_smbus_write_byte_data(data->client,
  72. VL_REG_SYSTEM_INTERRUPT_CLEAR, 0);
  73. if (ret < 0)
  74. dev_err(dev, "failed to clear range irq: %d\n", ret);
  75. ret = i2c_smbus_read_byte_data(data->client, VL_REG_RESULT_INT_STATUS);
  76. if (ret < 0 || ret & 0x07)
  77. dev_err(dev, "failed to clear irq: %d\n", ret);
  78. }
  79. static int vl53l0x_read_proximity(struct vl53l0x_data *data,
  80. const struct iio_chan_spec *chan,
  81. int *val)
  82. {
  83. struct i2c_client *client = data->client;
  84. u16 tries = 20;
  85. u8 buffer[12];
  86. int ret;
  87. ret = i2c_smbus_write_byte_data(client, VL_REG_SYSRANGE_START, 1);
  88. if (ret < 0)
  89. return ret;
  90. if (data->client->irq) {
  91. reinit_completion(&data->completion);
  92. ret = wait_for_completion_timeout(&data->completion, HZ/10);
  93. if (ret < 0)
  94. return ret;
  95. else if (ret == 0)
  96. return -ETIMEDOUT;
  97. vl53l0x_clear_irq(data);
  98. } else {
  99. do {
  100. ret = i2c_smbus_read_byte_data(client,
  101. VL_REG_RESULT_RANGE_STATUS);
  102. if (ret < 0)
  103. return ret;
  104. if (ret & VL_REG_RESULT_RANGE_STATUS_COMPLETE)
  105. break;
  106. usleep_range(1000, 5000);
  107. } while (--tries);
  108. if (!tries)
  109. return -ETIMEDOUT;
  110. }
  111. ret = i2c_smbus_read_i2c_block_data(client, VL_REG_RESULT_RANGE_STATUS,
  112. 12, buffer);
  113. if (ret < 0)
  114. return ret;
  115. else if (ret != 12)
  116. return -EREMOTEIO;
  117. /* Values should be between 30~1200 in millimeters. */
  118. *val = (buffer[10] << 8) + buffer[11];
  119. return 0;
  120. }
  121. static const struct iio_chan_spec vl53l0x_channels[] = {
  122. {
  123. .type = IIO_DISTANCE,
  124. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  125. BIT(IIO_CHAN_INFO_SCALE),
  126. },
  127. };
  128. static int vl53l0x_read_raw(struct iio_dev *indio_dev,
  129. const struct iio_chan_spec *chan,
  130. int *val, int *val2, long mask)
  131. {
  132. struct vl53l0x_data *data = iio_priv(indio_dev);
  133. int ret;
  134. if (chan->type != IIO_DISTANCE)
  135. return -EINVAL;
  136. switch (mask) {
  137. case IIO_CHAN_INFO_RAW:
  138. ret = vl53l0x_read_proximity(data, chan, val);
  139. if (ret < 0)
  140. return ret;
  141. return IIO_VAL_INT;
  142. case IIO_CHAN_INFO_SCALE:
  143. *val = 0;
  144. *val2 = 1000;
  145. return IIO_VAL_INT_PLUS_MICRO;
  146. default:
  147. return -EINVAL;
  148. }
  149. }
  150. static const struct iio_info vl53l0x_info = {
  151. .read_raw = vl53l0x_read_raw,
  152. };
  153. static int vl53l0x_probe(struct i2c_client *client)
  154. {
  155. struct vl53l0x_data *data;
  156. struct iio_dev *indio_dev;
  157. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  158. if (!indio_dev)
  159. return -ENOMEM;
  160. data = iio_priv(indio_dev);
  161. data->client = client;
  162. i2c_set_clientdata(client, indio_dev);
  163. if (!i2c_check_functionality(client->adapter,
  164. I2C_FUNC_SMBUS_READ_I2C_BLOCK |
  165. I2C_FUNC_SMBUS_BYTE_DATA))
  166. return -EOPNOTSUPP;
  167. indio_dev->name = "vl53l0x";
  168. indio_dev->info = &vl53l0x_info;
  169. indio_dev->channels = vl53l0x_channels;
  170. indio_dev->num_channels = ARRAY_SIZE(vl53l0x_channels);
  171. indio_dev->modes = INDIO_DIRECT_MODE;
  172. /* usage of interrupt is optional */
  173. if (client->irq) {
  174. int ret;
  175. init_completion(&data->completion);
  176. ret = vl53l0x_configure_irq(client, indio_dev);
  177. if (ret)
  178. return ret;
  179. }
  180. return devm_iio_device_register(&client->dev, indio_dev);
  181. }
  182. static const struct of_device_id st_vl53l0x_dt_match[] = {
  183. { .compatible = "st,vl53l0x", },
  184. { }
  185. };
  186. MODULE_DEVICE_TABLE(of, st_vl53l0x_dt_match);
  187. static struct i2c_driver vl53l0x_driver = {
  188. .driver = {
  189. .name = "vl53l0x-i2c",
  190. .of_match_table = st_vl53l0x_dt_match,
  191. },
  192. .probe_new = vl53l0x_probe,
  193. };
  194. module_i2c_driver(vl53l0x_driver);
  195. MODULE_AUTHOR("Song Qiang <songqiang1304521@gmail.com>");
  196. MODULE_DESCRIPTION("ST vl53l0x ToF ranging sensor driver");
  197. MODULE_LICENSE("GPL v2");