srf08.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * srf08.c - Support for Devantech SRF08 ultrasonic ranger
  3. *
  4. * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * For details about the device see:
  11. * http://www.robot-electronics.co.uk/htm/srf08tech.html
  12. */
  13. #include <linux/err.h>
  14. #include <linux/i2c.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. /* registers of SRF08 device */
  21. #define SRF08_WRITE_COMMAND 0x00 /* Command Register */
  22. #define SRF08_WRITE_MAX_GAIN 0x01 /* Max Gain Register: 0 .. 31 */
  23. #define SRF08_WRITE_RANGE 0x02 /* Range Register: 0 .. 255 */
  24. #define SRF08_READ_SW_REVISION 0x00 /* Software Revision */
  25. #define SRF08_READ_LIGHT 0x01 /* Light Sensor during last echo */
  26. #define SRF08_READ_ECHO_1_HIGH 0x02 /* Range of first echo received */
  27. #define SRF08_READ_ECHO_1_LOW 0x03 /* Range of first echo received */
  28. #define SRF08_CMD_RANGING_CM 0x51 /* Ranging Mode - Result in cm */
  29. #define SRF08_DEFAULT_GAIN 1025 /* default analogue value of Gain */
  30. #define SRF08_DEFAULT_RANGE 6020 /* default value of Range in mm */
  31. struct srf08_data {
  32. struct i2c_client *client;
  33. int sensitivity; /* Gain */
  34. int range_mm; /* max. Range in mm */
  35. struct mutex lock;
  36. };
  37. /*
  38. * in the documentation one can read about the "Gain" of the device
  39. * which is used here for amplifying the signal and filtering out unwanted
  40. * ones.
  41. * But with ADC's this term is already used differently and that's why it
  42. * is called "Sensitivity" here.
  43. */
  44. static const int srf08_sensitivity[] = {
  45. 94, 97, 100, 103, 107, 110, 114, 118,
  46. 123, 128, 133, 139, 145, 152, 159, 168,
  47. 177, 187, 199, 212, 227, 245, 265, 288,
  48. 317, 352, 395, 450, 524, 626, 777, 1025 };
  49. static int srf08_read_ranging(struct srf08_data *data)
  50. {
  51. struct i2c_client *client = data->client;
  52. int ret, i;
  53. int waittime;
  54. mutex_lock(&data->lock);
  55. ret = i2c_smbus_write_byte_data(data->client,
  56. SRF08_WRITE_COMMAND, SRF08_CMD_RANGING_CM);
  57. if (ret < 0) {
  58. dev_err(&client->dev, "write command - err: %d\n", ret);
  59. mutex_unlock(&data->lock);
  60. return ret;
  61. }
  62. /*
  63. * we read here until a correct version number shows up as
  64. * suggested by the documentation
  65. *
  66. * with an ultrasonic speed of 343 m/s and a roundtrip of it
  67. * sleep the expected duration and try to read from the device
  68. * if nothing useful is read try it in a shorter grid
  69. *
  70. * polling for not more than 20 ms should be enough
  71. */
  72. waittime = 1 + data->range_mm / 172;
  73. msleep(waittime);
  74. for (i = 0; i < 4; i++) {
  75. ret = i2c_smbus_read_byte_data(data->client,
  76. SRF08_READ_SW_REVISION);
  77. /* check if a valid version number is read */
  78. if (ret < 255 && ret > 0)
  79. break;
  80. msleep(5);
  81. }
  82. if (ret >= 255 || ret <= 0) {
  83. dev_err(&client->dev, "device not ready\n");
  84. mutex_unlock(&data->lock);
  85. return -EIO;
  86. }
  87. ret = i2c_smbus_read_word_swapped(data->client,
  88. SRF08_READ_ECHO_1_HIGH);
  89. if (ret < 0) {
  90. dev_err(&client->dev, "cannot read distance: ret=%d\n", ret);
  91. mutex_unlock(&data->lock);
  92. return ret;
  93. }
  94. mutex_unlock(&data->lock);
  95. return ret;
  96. }
  97. static int srf08_read_raw(struct iio_dev *indio_dev,
  98. struct iio_chan_spec const *channel, int *val,
  99. int *val2, long mask)
  100. {
  101. struct srf08_data *data = iio_priv(indio_dev);
  102. int ret;
  103. if (channel->type != IIO_DISTANCE)
  104. return -EINVAL;
  105. switch (mask) {
  106. case IIO_CHAN_INFO_RAW:
  107. ret = srf08_read_ranging(data);
  108. if (ret < 0)
  109. return ret;
  110. *val = ret;
  111. return IIO_VAL_INT;
  112. case IIO_CHAN_INFO_SCALE:
  113. /* 1 LSB is 1 cm */
  114. *val = 0;
  115. *val2 = 10000;
  116. return IIO_VAL_INT_PLUS_MICRO;
  117. default:
  118. return -EINVAL;
  119. }
  120. }
  121. static ssize_t srf08_show_range_mm_available(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. return sprintf(buf, "[0.043 0.043 11.008]\n");
  125. }
  126. static IIO_DEVICE_ATTR(sensor_max_range_available, S_IRUGO,
  127. srf08_show_range_mm_available, NULL, 0);
  128. static ssize_t srf08_show_range_mm(struct device *dev,
  129. struct device_attribute *attr, char *buf)
  130. {
  131. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  132. struct srf08_data *data = iio_priv(indio_dev);
  133. return sprintf(buf, "%d.%03d\n", data->range_mm / 1000,
  134. data->range_mm % 1000);
  135. }
  136. /*
  137. * set the range of the sensor to an even multiple of 43 mm
  138. * which corresponds to 1 LSB in the register
  139. *
  140. * register value corresponding range
  141. * 0x00 43 mm
  142. * 0x01 86 mm
  143. * 0x02 129 mm
  144. * ...
  145. * 0xFF 11008 mm
  146. */
  147. static ssize_t srf08_write_range_mm(struct srf08_data *data, unsigned int val)
  148. {
  149. int ret;
  150. struct i2c_client *client = data->client;
  151. unsigned int mod;
  152. u8 regval;
  153. ret = val / 43 - 1;
  154. mod = val % 43;
  155. if (mod || (ret < 0) || (ret > 255))
  156. return -EINVAL;
  157. regval = ret;
  158. mutex_lock(&data->lock);
  159. ret = i2c_smbus_write_byte_data(client, SRF08_WRITE_RANGE, regval);
  160. if (ret < 0) {
  161. dev_err(&client->dev, "write_range - err: %d\n", ret);
  162. mutex_unlock(&data->lock);
  163. return ret;
  164. }
  165. data->range_mm = val;
  166. mutex_unlock(&data->lock);
  167. return 0;
  168. }
  169. static ssize_t srf08_store_range_mm(struct device *dev,
  170. struct device_attribute *attr,
  171. const char *buf, size_t len)
  172. {
  173. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  174. struct srf08_data *data = iio_priv(indio_dev);
  175. int ret;
  176. int integer, fract;
  177. ret = iio_str_to_fixpoint(buf, 100, &integer, &fract);
  178. if (ret)
  179. return ret;
  180. ret = srf08_write_range_mm(data, integer * 1000 + fract);
  181. if (ret < 0)
  182. return ret;
  183. return len;
  184. }
  185. static IIO_DEVICE_ATTR(sensor_max_range, S_IRUGO | S_IWUSR,
  186. srf08_show_range_mm, srf08_store_range_mm, 0);
  187. static ssize_t srf08_show_sensitivity_available(struct device *dev,
  188. struct device_attribute *attr, char *buf)
  189. {
  190. int i, len = 0;
  191. for (i = 0; i < ARRAY_SIZE(srf08_sensitivity); i++)
  192. len += sprintf(buf + len, "%d ", srf08_sensitivity[i]);
  193. len += sprintf(buf + len, "\n");
  194. return len;
  195. }
  196. static IIO_DEVICE_ATTR(sensor_sensitivity_available, S_IRUGO,
  197. srf08_show_sensitivity_available, NULL, 0);
  198. static ssize_t srf08_show_sensitivity(struct device *dev,
  199. struct device_attribute *attr, char *buf)
  200. {
  201. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  202. struct srf08_data *data = iio_priv(indio_dev);
  203. int len;
  204. len = sprintf(buf, "%d\n", data->sensitivity);
  205. return len;
  206. }
  207. static ssize_t srf08_write_sensitivity(struct srf08_data *data,
  208. unsigned int val)
  209. {
  210. struct i2c_client *client = data->client;
  211. int ret, i;
  212. u8 regval;
  213. for (i = 0; i < ARRAY_SIZE(srf08_sensitivity); i++)
  214. if (val == srf08_sensitivity[i]) {
  215. regval = i;
  216. break;
  217. }
  218. if (i >= ARRAY_SIZE(srf08_sensitivity))
  219. return -EINVAL;
  220. mutex_lock(&data->lock);
  221. ret = i2c_smbus_write_byte_data(client,
  222. SRF08_WRITE_MAX_GAIN, regval);
  223. if (ret < 0) {
  224. dev_err(&client->dev, "write_sensitivity - err: %d\n", ret);
  225. mutex_unlock(&data->lock);
  226. return ret;
  227. }
  228. data->sensitivity = val;
  229. mutex_unlock(&data->lock);
  230. return 0;
  231. }
  232. static ssize_t srf08_store_sensitivity(struct device *dev,
  233. struct device_attribute *attr,
  234. const char *buf, size_t len)
  235. {
  236. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  237. struct srf08_data *data = iio_priv(indio_dev);
  238. int ret;
  239. unsigned int val;
  240. ret = kstrtouint(buf, 10, &val);
  241. if (ret)
  242. return ret;
  243. ret = srf08_write_sensitivity(data, val);
  244. if (ret < 0)
  245. return ret;
  246. return len;
  247. }
  248. static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
  249. srf08_show_sensitivity, srf08_store_sensitivity, 0);
  250. static struct attribute *srf08_attributes[] = {
  251. &iio_dev_attr_sensor_max_range.dev_attr.attr,
  252. &iio_dev_attr_sensor_max_range_available.dev_attr.attr,
  253. &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
  254. &iio_dev_attr_sensor_sensitivity_available.dev_attr.attr,
  255. NULL,
  256. };
  257. static const struct attribute_group srf08_attribute_group = {
  258. .attrs = srf08_attributes,
  259. };
  260. static const struct iio_chan_spec srf08_channels[] = {
  261. {
  262. .type = IIO_DISTANCE,
  263. .info_mask_separate =
  264. BIT(IIO_CHAN_INFO_RAW) |
  265. BIT(IIO_CHAN_INFO_SCALE),
  266. },
  267. };
  268. static const struct iio_info srf08_info = {
  269. .read_raw = srf08_read_raw,
  270. .attrs = &srf08_attribute_group,
  271. .driver_module = THIS_MODULE,
  272. };
  273. static int srf08_probe(struct i2c_client *client,
  274. const struct i2c_device_id *id)
  275. {
  276. struct iio_dev *indio_dev;
  277. struct srf08_data *data;
  278. int ret;
  279. if (!i2c_check_functionality(client->adapter,
  280. I2C_FUNC_SMBUS_READ_BYTE_DATA |
  281. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  282. I2C_FUNC_SMBUS_READ_WORD_DATA))
  283. return -ENODEV;
  284. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  285. if (!indio_dev)
  286. return -ENOMEM;
  287. data = iio_priv(indio_dev);
  288. i2c_set_clientdata(client, indio_dev);
  289. data->client = client;
  290. indio_dev->name = "srf08";
  291. indio_dev->dev.parent = &client->dev;
  292. indio_dev->modes = INDIO_DIRECT_MODE;
  293. indio_dev->info = &srf08_info;
  294. indio_dev->channels = srf08_channels;
  295. indio_dev->num_channels = ARRAY_SIZE(srf08_channels);
  296. mutex_init(&data->lock);
  297. /*
  298. * set default values of device here
  299. * these register values cannot be read from the hardware
  300. * therefore set driver specific default values
  301. */
  302. ret = srf08_write_range_mm(data, SRF08_DEFAULT_RANGE);
  303. if (ret < 0)
  304. return ret;
  305. ret = srf08_write_sensitivity(data, SRF08_DEFAULT_GAIN);
  306. if (ret < 0)
  307. return ret;
  308. return devm_iio_device_register(&client->dev, indio_dev);
  309. }
  310. static const struct i2c_device_id srf08_id[] = {
  311. { "srf08", 0 },
  312. { }
  313. };
  314. MODULE_DEVICE_TABLE(i2c, srf08_id);
  315. static struct i2c_driver srf08_driver = {
  316. .driver = {
  317. .name = "srf08",
  318. },
  319. .probe = srf08_probe,
  320. .id_table = srf08_id,
  321. };
  322. module_i2c_driver(srf08_driver);
  323. MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
  324. MODULE_DESCRIPTION("Devantech SRF08 ultrasonic ranger driver");
  325. MODULE_LICENSE("GPL");