vl53l0x-i2c.c 7.3 KB

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