mb1232.c 6.4 KB

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