pulsedlight-lidar-lite-v2.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * pulsedlight-lidar-lite-v2.c - Support for PulsedLight LIDAR sensor
  3. *
  4. * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * TODO: runtime pm, interrupt mode, and signal strength reporting
  17. */
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/i2c.h>
  21. #include <linux/delay.h>
  22. #include <linux/module.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/sysfs.h>
  25. #include <linux/iio/buffer.h>
  26. #include <linux/iio/trigger.h>
  27. #include <linux/iio/triggered_buffer.h>
  28. #include <linux/iio/trigger_consumer.h>
  29. #define LIDAR_REG_CONTROL 0x00
  30. #define LIDAR_REG_CONTROL_ACQUIRE BIT(2)
  31. #define LIDAR_REG_STATUS 0x01
  32. #define LIDAR_REG_STATUS_INVALID BIT(3)
  33. #define LIDAR_REG_STATUS_READY BIT(0)
  34. #define LIDAR_REG_DATA_HBYTE 0x0f
  35. #define LIDAR_REG_DATA_LBYTE 0x10
  36. #define LIDAR_DRV_NAME "lidar"
  37. struct lidar_data {
  38. struct iio_dev *indio_dev;
  39. struct i2c_client *client;
  40. u16 buffer[8]; /* 2 byte distance + 8 byte timestamp */
  41. };
  42. static const struct iio_chan_spec lidar_channels[] = {
  43. {
  44. .type = IIO_DISTANCE,
  45. .info_mask_separate =
  46. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  47. .scan_index = 0,
  48. .scan_type = {
  49. .sign = 'u',
  50. .realbits = 16,
  51. .storagebits = 16,
  52. },
  53. },
  54. IIO_CHAN_SOFT_TIMESTAMP(1),
  55. };
  56. static int lidar_read_byte(struct lidar_data *data, int reg)
  57. {
  58. struct i2c_client *client = data->client;
  59. int ret;
  60. /*
  61. * Device needs a STOP condition between address write, and data read
  62. * so in turn i2c_smbus_read_byte_data cannot be used
  63. */
  64. ret = i2c_smbus_write_byte(client, reg);
  65. if (ret < 0) {
  66. dev_err(&client->dev, "cannot write addr value");
  67. return ret;
  68. }
  69. ret = i2c_smbus_read_byte(client);
  70. if (ret < 0)
  71. dev_err(&client->dev, "cannot read data value");
  72. return ret;
  73. }
  74. static inline int lidar_write_control(struct lidar_data *data, int val)
  75. {
  76. return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL, val);
  77. }
  78. static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
  79. {
  80. int ret;
  81. int val;
  82. ret = lidar_read_byte(data, LIDAR_REG_DATA_HBYTE);
  83. if (ret < 0)
  84. return ret;
  85. val = ret << 8;
  86. ret = lidar_read_byte(data, LIDAR_REG_DATA_LBYTE);
  87. if (ret < 0)
  88. return ret;
  89. val |= ret;
  90. *reg = val;
  91. return 0;
  92. }
  93. static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
  94. {
  95. struct i2c_client *client = data->client;
  96. int tries = 10;
  97. int ret;
  98. /* start sample */
  99. ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
  100. if (ret < 0) {
  101. dev_err(&client->dev, "cannot send start measurement command");
  102. return ret;
  103. }
  104. while (tries--) {
  105. usleep_range(1000, 2000);
  106. ret = lidar_read_byte(data, LIDAR_REG_STATUS);
  107. if (ret < 0)
  108. break;
  109. /* return -EINVAL since laser is likely pointed out of range */
  110. if (ret & LIDAR_REG_STATUS_INVALID) {
  111. *reg = 0;
  112. ret = -EINVAL;
  113. break;
  114. }
  115. /* sample ready to read */
  116. if (!(ret & LIDAR_REG_STATUS_READY)) {
  117. ret = lidar_read_measurement(data, reg);
  118. break;
  119. }
  120. ret = -EIO;
  121. }
  122. return ret;
  123. }
  124. static int lidar_read_raw(struct iio_dev *indio_dev,
  125. struct iio_chan_spec const *chan,
  126. int *val, int *val2, long mask)
  127. {
  128. struct lidar_data *data = iio_priv(indio_dev);
  129. int ret = -EINVAL;
  130. mutex_lock(&indio_dev->mlock);
  131. if (iio_buffer_enabled(indio_dev) && mask == IIO_CHAN_INFO_RAW) {
  132. ret = -EBUSY;
  133. goto error_busy;
  134. }
  135. switch (mask) {
  136. case IIO_CHAN_INFO_RAW: {
  137. u16 reg;
  138. ret = lidar_get_measurement(data, &reg);
  139. if (!ret) {
  140. *val = reg;
  141. ret = IIO_VAL_INT;
  142. }
  143. break;
  144. }
  145. case IIO_CHAN_INFO_SCALE:
  146. *val = 0;
  147. *val2 = 10000;
  148. ret = IIO_VAL_INT_PLUS_MICRO;
  149. break;
  150. }
  151. error_busy:
  152. mutex_unlock(&indio_dev->mlock);
  153. return ret;
  154. }
  155. static irqreturn_t lidar_trigger_handler(int irq, void *private)
  156. {
  157. struct iio_poll_func *pf = private;
  158. struct iio_dev *indio_dev = pf->indio_dev;
  159. struct lidar_data *data = iio_priv(indio_dev);
  160. int ret;
  161. ret = lidar_get_measurement(data, data->buffer);
  162. if (!ret) {
  163. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  164. iio_get_time_ns());
  165. } else if (ret != -EINVAL) {
  166. dev_err(&data->client->dev, "cannot read LIDAR measurement");
  167. }
  168. iio_trigger_notify_done(indio_dev->trig);
  169. return IRQ_HANDLED;
  170. }
  171. static const struct iio_info lidar_info = {
  172. .driver_module = THIS_MODULE,
  173. .read_raw = lidar_read_raw,
  174. };
  175. static int lidar_probe(struct i2c_client *client,
  176. const struct i2c_device_id *id)
  177. {
  178. struct lidar_data *data;
  179. struct iio_dev *indio_dev;
  180. int ret;
  181. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  182. if (!indio_dev)
  183. return -ENOMEM;
  184. indio_dev->info = &lidar_info;
  185. indio_dev->name = LIDAR_DRV_NAME;
  186. indio_dev->channels = lidar_channels;
  187. indio_dev->num_channels = ARRAY_SIZE(lidar_channels);
  188. indio_dev->modes = INDIO_DIRECT_MODE;
  189. data = iio_priv(indio_dev);
  190. i2c_set_clientdata(client, indio_dev);
  191. data->client = client;
  192. data->indio_dev = indio_dev;
  193. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  194. lidar_trigger_handler, NULL);
  195. if (ret)
  196. return ret;
  197. ret = iio_device_register(indio_dev);
  198. if (ret)
  199. goto error_unreg_buffer;
  200. return 0;
  201. error_unreg_buffer:
  202. iio_triggered_buffer_cleanup(indio_dev);
  203. return ret;
  204. }
  205. static int lidar_remove(struct i2c_client *client)
  206. {
  207. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  208. iio_device_unregister(indio_dev);
  209. iio_triggered_buffer_cleanup(indio_dev);
  210. return 0;
  211. }
  212. static const struct i2c_device_id lidar_id[] = {
  213. {"lidar-lite-v2", 0},
  214. { },
  215. };
  216. MODULE_DEVICE_TABLE(i2c, lidar_id);
  217. static const struct of_device_id lidar_dt_ids[] = {
  218. { .compatible = "pulsedlight,lidar-lite-v2" },
  219. { }
  220. };
  221. MODULE_DEVICE_TABLE(of, lidar_dt_ids);
  222. static struct i2c_driver lidar_driver = {
  223. .driver = {
  224. .name = LIDAR_DRV_NAME,
  225. .of_match_table = of_match_ptr(lidar_dt_ids),
  226. },
  227. .probe = lidar_probe,
  228. .remove = lidar_remove,
  229. .id_table = lidar_id,
  230. };
  231. module_i2c_driver(lidar_driver);
  232. MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
  233. MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
  234. MODULE_LICENSE("GPL");