mb1232.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * mb1232.c - Support for MaxBotix I2CXL-MaxSonar-EZ series ultrasonic
  4. * ranger with i2c interface
  5. * actually tested with mb1232 type
  6. *
  7. * Copyright (c) 2019 Andreas Klinger <ak@it-klinger.de>
  8. *
  9. * For details about the device see:
  10. * https://www.maxbotix.com/documents/I2CXL-MaxSonar-EZ_Datasheet.pdf
  11. */
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/bitops.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/buffer.h>
  21. #include <linux/iio/trigger_consumer.h>
  22. #include <linux/iio/triggered_buffer.h>
  23. /* registers of MaxSonar device */
  24. #define MB1232_RANGE_COMMAND 0x51 /* Command for reading range */
  25. #define MB1232_ADDR_UNLOCK_1 0xAA /* Command 1 for changing address */
  26. #define MB1232_ADDR_UNLOCK_2 0xA5 /* Command 2 for changing address */
  27. struct mb1232_data {
  28. struct i2c_client *client;
  29. struct mutex lock;
  30. /*
  31. * optionally a gpio can be used to announce when ranging has
  32. * finished
  33. * since we are just using the falling trigger of it we request
  34. * only the interrupt for announcing when data is ready to be read
  35. */
  36. struct completion ranging;
  37. int irqnr;
  38. /* Ensure correct alignment of data to push to IIO buffer */
  39. struct {
  40. s16 distance;
  41. s64 ts __aligned(8);
  42. } scan;
  43. };
  44. static irqreturn_t mb1232_handle_irq(int irq, void *dev_id)
  45. {
  46. struct iio_dev *indio_dev = dev_id;
  47. struct mb1232_data *data = iio_priv(indio_dev);
  48. complete(&data->ranging);
  49. return IRQ_HANDLED;
  50. }
  51. static s16 mb1232_read_distance(struct mb1232_data *data)
  52. {
  53. struct i2c_client *client = data->client;
  54. int ret;
  55. s16 distance;
  56. __be16 buf;
  57. mutex_lock(&data->lock);
  58. reinit_completion(&data->ranging);
  59. ret = i2c_smbus_write_byte(client, MB1232_RANGE_COMMAND);
  60. if (ret < 0) {
  61. dev_err(&client->dev, "write command - err: %d\n", ret);
  62. goto error_unlock;
  63. }
  64. if (data->irqnr >= 0) {
  65. /* it cannot take more than 100 ms */
  66. ret = wait_for_completion_killable_timeout(&data->ranging,
  67. HZ/10);
  68. if (ret < 0)
  69. goto error_unlock;
  70. else if (ret == 0) {
  71. ret = -ETIMEDOUT;
  72. goto error_unlock;
  73. }
  74. } else {
  75. /* use simple sleep if announce irq is not connected */
  76. msleep(15);
  77. }
  78. ret = i2c_master_recv(client, (char *)&buf, sizeof(buf));
  79. if (ret < 0) {
  80. dev_err(&client->dev, "i2c_master_recv: ret=%d\n", ret);
  81. goto error_unlock;
  82. }
  83. distance = __be16_to_cpu(buf);
  84. /* check for not returning misleading error codes */
  85. if (distance < 0) {
  86. dev_err(&client->dev, "distance=%d\n", distance);
  87. ret = -EINVAL;
  88. goto error_unlock;
  89. }
  90. mutex_unlock(&data->lock);
  91. return distance;
  92. error_unlock:
  93. mutex_unlock(&data->lock);
  94. return ret;
  95. }
  96. static irqreturn_t mb1232_trigger_handler(int irq, void *p)
  97. {
  98. struct iio_poll_func *pf = p;
  99. struct iio_dev *indio_dev = pf->indio_dev;
  100. struct mb1232_data *data = iio_priv(indio_dev);
  101. data->scan.distance = mb1232_read_distance(data);
  102. if (data->scan.distance < 0)
  103. goto err;
  104. iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
  105. pf->timestamp);
  106. err:
  107. iio_trigger_notify_done(indio_dev->trig);
  108. return IRQ_HANDLED;
  109. }
  110. static int mb1232_read_raw(struct iio_dev *indio_dev,
  111. struct iio_chan_spec const *channel, int *val,
  112. int *val2, long mask)
  113. {
  114. struct mb1232_data *data = iio_priv(indio_dev);
  115. int ret;
  116. if (channel->type != IIO_DISTANCE)
  117. return -EINVAL;
  118. switch (mask) {
  119. case IIO_CHAN_INFO_RAW:
  120. ret = mb1232_read_distance(data);
  121. if (ret < 0)
  122. return ret;
  123. *val = ret;
  124. return IIO_VAL_INT;
  125. case IIO_CHAN_INFO_SCALE:
  126. /* 1 LSB is 1 cm */
  127. *val = 0;
  128. *val2 = 10000;
  129. return IIO_VAL_INT_PLUS_MICRO;
  130. default:
  131. return -EINVAL;
  132. }
  133. }
  134. static const struct iio_chan_spec mb1232_channels[] = {
  135. {
  136. .type = IIO_DISTANCE,
  137. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  138. BIT(IIO_CHAN_INFO_SCALE),
  139. .scan_index = 0,
  140. .scan_type = {
  141. .sign = 's',
  142. .realbits = 16,
  143. .storagebits = 16,
  144. .endianness = IIO_CPU,
  145. },
  146. },
  147. IIO_CHAN_SOFT_TIMESTAMP(1),
  148. };
  149. static const struct iio_info mb1232_info = {
  150. .read_raw = mb1232_read_raw,
  151. };
  152. static int mb1232_probe(struct i2c_client *client,
  153. const struct i2c_device_id *id)
  154. {
  155. struct iio_dev *indio_dev;
  156. struct mb1232_data *data;
  157. int ret;
  158. struct device *dev = &client->dev;
  159. if (!i2c_check_functionality(client->adapter,
  160. I2C_FUNC_SMBUS_READ_BYTE |
  161. I2C_FUNC_SMBUS_WRITE_BYTE))
  162. return -ENODEV;
  163. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  164. if (!indio_dev)
  165. return -ENOMEM;
  166. data = iio_priv(indio_dev);
  167. i2c_set_clientdata(client, indio_dev);
  168. data->client = client;
  169. indio_dev->info = &mb1232_info;
  170. indio_dev->name = id->name;
  171. indio_dev->modes = INDIO_DIRECT_MODE;
  172. indio_dev->channels = mb1232_channels;
  173. indio_dev->num_channels = ARRAY_SIZE(mb1232_channels);
  174. mutex_init(&data->lock);
  175. init_completion(&data->ranging);
  176. data->irqnr = irq_of_parse_and_map(dev->of_node, 0);
  177. if (data->irqnr <= 0) {
  178. /* usage of interrupt is optional */
  179. data->irqnr = -1;
  180. } else {
  181. ret = devm_request_irq(dev, data->irqnr, mb1232_handle_irq,
  182. IRQF_TRIGGER_FALLING, id->name, indio_dev);
  183. if (ret < 0) {
  184. dev_err(dev, "request_irq: %d\n", ret);
  185. return ret;
  186. }
  187. }
  188. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  189. iio_pollfunc_store_time, mb1232_trigger_handler, NULL);
  190. if (ret < 0) {
  191. dev_err(dev, "setup of iio triggered buffer failed\n");
  192. return ret;
  193. }
  194. return devm_iio_device_register(dev, indio_dev);
  195. }
  196. static const struct of_device_id of_mb1232_match[] = {
  197. { .compatible = "maxbotix,mb1202", },
  198. { .compatible = "maxbotix,mb1212", },
  199. { .compatible = "maxbotix,mb1222", },
  200. { .compatible = "maxbotix,mb1232", },
  201. { .compatible = "maxbotix,mb1242", },
  202. { .compatible = "maxbotix,mb7040", },
  203. { .compatible = "maxbotix,mb7137", },
  204. {},
  205. };
  206. MODULE_DEVICE_TABLE(of, of_mb1232_match);
  207. static const struct i2c_device_id mb1232_id[] = {
  208. { "maxbotix-mb1202", },
  209. { "maxbotix-mb1212", },
  210. { "maxbotix-mb1222", },
  211. { "maxbotix-mb1232", },
  212. { "maxbotix-mb1242", },
  213. { "maxbotix-mb7040", },
  214. { "maxbotix-mb7137", },
  215. { }
  216. };
  217. MODULE_DEVICE_TABLE(i2c, mb1232_id);
  218. static struct i2c_driver mb1232_driver = {
  219. .driver = {
  220. .name = "maxbotix-mb1232",
  221. .of_match_table = of_mb1232_match,
  222. },
  223. .probe = mb1232_probe,
  224. .id_table = mb1232_id,
  225. };
  226. module_i2c_driver(mb1232_driver);
  227. MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
  228. MODULE_DESCRIPTION("Maxbotix I2CXL-MaxSonar i2c ultrasonic ranger driver");
  229. MODULE_LICENSE("GPL");