pulsedlight-lidar-lite-v2.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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: 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/pm_runtime.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. #include <linux/iio/buffer.h>
  27. #include <linux/iio/trigger.h>
  28. #include <linux/iio/triggered_buffer.h>
  29. #include <linux/iio/trigger_consumer.h>
  30. #define LIDAR_REG_CONTROL 0x00
  31. #define LIDAR_REG_CONTROL_ACQUIRE BIT(2)
  32. #define LIDAR_REG_STATUS 0x01
  33. #define LIDAR_REG_STATUS_INVALID BIT(3)
  34. #define LIDAR_REG_STATUS_READY BIT(0)
  35. #define LIDAR_REG_DATA_HBYTE 0x0f
  36. #define LIDAR_REG_DATA_LBYTE 0x10
  37. #define LIDAR_REG_DATA_WORD_READ BIT(7)
  38. #define LIDAR_REG_PWR_CONTROL 0x65
  39. #define LIDAR_DRV_NAME "lidar"
  40. struct lidar_data {
  41. struct iio_dev *indio_dev;
  42. struct i2c_client *client;
  43. int (*xfer)(struct lidar_data *data, u8 reg, u8 *val, int len);
  44. int i2c_enabled;
  45. u16 buffer[8]; /* 2 byte distance + 8 byte timestamp */
  46. };
  47. static const struct iio_chan_spec lidar_channels[] = {
  48. {
  49. .type = IIO_DISTANCE,
  50. .info_mask_separate =
  51. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  52. .scan_index = 0,
  53. .scan_type = {
  54. .sign = 'u',
  55. .realbits = 16,
  56. .storagebits = 16,
  57. },
  58. },
  59. IIO_CHAN_SOFT_TIMESTAMP(1),
  60. };
  61. static int lidar_i2c_xfer(struct lidar_data *data, u8 reg, u8 *val, int len)
  62. {
  63. struct i2c_client *client = data->client;
  64. struct i2c_msg msg[2];
  65. int ret;
  66. msg[0].addr = client->addr;
  67. msg[0].flags = client->flags | I2C_M_STOP;
  68. msg[0].len = 1;
  69. msg[0].buf = (char *) &reg;
  70. msg[1].addr = client->addr;
  71. msg[1].flags = client->flags | I2C_M_RD;
  72. msg[1].len = len;
  73. msg[1].buf = (char *) val;
  74. ret = i2c_transfer(client->adapter, msg, 2);
  75. return (ret == 2) ? 0 : -EIO;
  76. }
  77. static int lidar_smbus_xfer(struct lidar_data *data, u8 reg, u8 *val, int len)
  78. {
  79. struct i2c_client *client = data->client;
  80. int ret;
  81. /*
  82. * Device needs a STOP condition between address write, and data read
  83. * so in turn i2c_smbus_read_byte_data cannot be used
  84. */
  85. while (len--) {
  86. ret = i2c_smbus_write_byte(client, reg++);
  87. if (ret < 0) {
  88. dev_err(&client->dev, "cannot write addr value");
  89. return ret;
  90. }
  91. ret = i2c_smbus_read_byte(client);
  92. if (ret < 0) {
  93. dev_err(&client->dev, "cannot read data value");
  94. return ret;
  95. }
  96. *(val++) = ret;
  97. }
  98. return 0;
  99. }
  100. static int lidar_read_byte(struct lidar_data *data, u8 reg)
  101. {
  102. int ret;
  103. u8 val;
  104. ret = data->xfer(data, reg, &val, 1);
  105. if (ret < 0)
  106. return ret;
  107. return val;
  108. }
  109. static inline int lidar_write_control(struct lidar_data *data, int val)
  110. {
  111. return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL, val);
  112. }
  113. static inline int lidar_write_power(struct lidar_data *data, int val)
  114. {
  115. return i2c_smbus_write_byte_data(data->client,
  116. LIDAR_REG_PWR_CONTROL, val);
  117. }
  118. static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
  119. {
  120. int ret = data->xfer(data, LIDAR_REG_DATA_HBYTE |
  121. (data->i2c_enabled ? LIDAR_REG_DATA_WORD_READ : 0),
  122. (u8 *) reg, 2);
  123. if (!ret)
  124. *reg = be16_to_cpu(*reg);
  125. return ret;
  126. }
  127. static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
  128. {
  129. struct i2c_client *client = data->client;
  130. int tries = 10;
  131. int ret;
  132. pm_runtime_get_sync(&client->dev);
  133. /* start sample */
  134. ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
  135. if (ret < 0) {
  136. dev_err(&client->dev, "cannot send start measurement command");
  137. return ret;
  138. }
  139. while (tries--) {
  140. usleep_range(1000, 2000);
  141. ret = lidar_read_byte(data, LIDAR_REG_STATUS);
  142. if (ret < 0)
  143. break;
  144. /* return -EINVAL since laser is likely pointed out of range */
  145. if (ret & LIDAR_REG_STATUS_INVALID) {
  146. *reg = 0;
  147. ret = -EINVAL;
  148. break;
  149. }
  150. /* sample ready to read */
  151. if (!(ret & LIDAR_REG_STATUS_READY)) {
  152. ret = lidar_read_measurement(data, reg);
  153. break;
  154. }
  155. ret = -EIO;
  156. }
  157. pm_runtime_mark_last_busy(&client->dev);
  158. pm_runtime_put_autosuspend(&client->dev);
  159. return ret;
  160. }
  161. static int lidar_read_raw(struct iio_dev *indio_dev,
  162. struct iio_chan_spec const *chan,
  163. int *val, int *val2, long mask)
  164. {
  165. struct lidar_data *data = iio_priv(indio_dev);
  166. int ret = -EINVAL;
  167. switch (mask) {
  168. case IIO_CHAN_INFO_RAW: {
  169. u16 reg;
  170. if (iio_device_claim_direct_mode(indio_dev))
  171. return -EBUSY;
  172. ret = lidar_get_measurement(data, &reg);
  173. if (!ret) {
  174. *val = reg;
  175. ret = IIO_VAL_INT;
  176. }
  177. iio_device_release_direct_mode(indio_dev);
  178. break;
  179. }
  180. case IIO_CHAN_INFO_SCALE:
  181. *val = 0;
  182. *val2 = 10000;
  183. ret = IIO_VAL_INT_PLUS_MICRO;
  184. break;
  185. }
  186. return ret;
  187. }
  188. static irqreturn_t lidar_trigger_handler(int irq, void *private)
  189. {
  190. struct iio_poll_func *pf = private;
  191. struct iio_dev *indio_dev = pf->indio_dev;
  192. struct lidar_data *data = iio_priv(indio_dev);
  193. int ret;
  194. ret = lidar_get_measurement(data, data->buffer);
  195. if (!ret) {
  196. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  197. iio_get_time_ns(indio_dev));
  198. } else if (ret != -EINVAL) {
  199. dev_err(&data->client->dev, "cannot read LIDAR measurement");
  200. }
  201. iio_trigger_notify_done(indio_dev->trig);
  202. return IRQ_HANDLED;
  203. }
  204. static const struct iio_info lidar_info = {
  205. .driver_module = THIS_MODULE,
  206. .read_raw = lidar_read_raw,
  207. };
  208. static int lidar_probe(struct i2c_client *client,
  209. const struct i2c_device_id *id)
  210. {
  211. struct lidar_data *data;
  212. struct iio_dev *indio_dev;
  213. int ret;
  214. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  215. if (!indio_dev)
  216. return -ENOMEM;
  217. data = iio_priv(indio_dev);
  218. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  219. data->xfer = lidar_i2c_xfer;
  220. data->i2c_enabled = 1;
  221. } else if (i2c_check_functionality(client->adapter,
  222. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE))
  223. data->xfer = lidar_smbus_xfer;
  224. else
  225. return -EOPNOTSUPP;
  226. indio_dev->info = &lidar_info;
  227. indio_dev->name = LIDAR_DRV_NAME;
  228. indio_dev->channels = lidar_channels;
  229. indio_dev->num_channels = ARRAY_SIZE(lidar_channels);
  230. indio_dev->modes = INDIO_DIRECT_MODE;
  231. i2c_set_clientdata(client, indio_dev);
  232. data->client = client;
  233. data->indio_dev = indio_dev;
  234. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  235. lidar_trigger_handler, NULL);
  236. if (ret)
  237. return ret;
  238. ret = iio_device_register(indio_dev);
  239. if (ret)
  240. goto error_unreg_buffer;
  241. pm_runtime_set_autosuspend_delay(&client->dev, 1000);
  242. pm_runtime_use_autosuspend(&client->dev);
  243. ret = pm_runtime_set_active(&client->dev);
  244. if (ret)
  245. goto error_unreg_buffer;
  246. pm_runtime_enable(&client->dev);
  247. pm_runtime_mark_last_busy(&client->dev);
  248. pm_runtime_idle(&client->dev);
  249. return 0;
  250. error_unreg_buffer:
  251. iio_triggered_buffer_cleanup(indio_dev);
  252. return ret;
  253. }
  254. static int lidar_remove(struct i2c_client *client)
  255. {
  256. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  257. iio_device_unregister(indio_dev);
  258. iio_triggered_buffer_cleanup(indio_dev);
  259. pm_runtime_disable(&client->dev);
  260. pm_runtime_set_suspended(&client->dev);
  261. return 0;
  262. }
  263. static const struct i2c_device_id lidar_id[] = {
  264. {"lidar-lite-v2", 0},
  265. { },
  266. };
  267. MODULE_DEVICE_TABLE(i2c, lidar_id);
  268. static const struct of_device_id lidar_dt_ids[] = {
  269. { .compatible = "pulsedlight,lidar-lite-v2" },
  270. { }
  271. };
  272. MODULE_DEVICE_TABLE(of, lidar_dt_ids);
  273. #ifdef CONFIG_PM
  274. static int lidar_pm_runtime_suspend(struct device *dev)
  275. {
  276. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  277. struct lidar_data *data = iio_priv(indio_dev);
  278. return lidar_write_power(data, 0x0f);
  279. }
  280. static int lidar_pm_runtime_resume(struct device *dev)
  281. {
  282. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  283. struct lidar_data *data = iio_priv(indio_dev);
  284. int ret = lidar_write_power(data, 0);
  285. /* regulator and FPGA needs settling time */
  286. usleep_range(15000, 20000);
  287. return ret;
  288. }
  289. #endif
  290. static const struct dev_pm_ops lidar_pm_ops = {
  291. SET_RUNTIME_PM_OPS(lidar_pm_runtime_suspend,
  292. lidar_pm_runtime_resume, NULL)
  293. };
  294. static struct i2c_driver lidar_driver = {
  295. .driver = {
  296. .name = LIDAR_DRV_NAME,
  297. .of_match_table = of_match_ptr(lidar_dt_ids),
  298. .pm = &lidar_pm_ops,
  299. },
  300. .probe = lidar_probe,
  301. .remove = lidar_remove,
  302. .id_table = lidar_id,
  303. };
  304. module_i2c_driver(lidar_driver);
  305. MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
  306. MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
  307. MODULE_LICENSE("GPL");