srf08.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * srf08.c - Support for Devantech SRFxx ultrasonic ranger
  4. * with i2c interface
  5. * actually supported are srf02, srf08, srf10
  6. *
  7. * Copyright (c) 2016, 2017 Andreas Klinger <ak@it-klinger.de>
  8. *
  9. * For details about the device see:
  10. * https://www.robot-electronics.co.uk/htm/srf08tech.html
  11. * https://www.robot-electronics.co.uk/htm/srf10tech.htm
  12. * https://www.robot-electronics.co.uk/htm/srf02tech.htm
  13. */
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/delay.h>
  17. #include <linux/module.h>
  18. #include <linux/bitops.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 SRF08 device */
  25. #define SRF08_WRITE_COMMAND 0x00 /* Command Register */
  26. #define SRF08_WRITE_MAX_GAIN 0x01 /* Max Gain Register: 0 .. 31 */
  27. #define SRF08_WRITE_RANGE 0x02 /* Range Register: 0 .. 255 */
  28. #define SRF08_READ_SW_REVISION 0x00 /* Software Revision */
  29. #define SRF08_READ_LIGHT 0x01 /* Light Sensor during last echo */
  30. #define SRF08_READ_ECHO_1_HIGH 0x02 /* Range of first echo received */
  31. #define SRF08_READ_ECHO_1_LOW 0x03 /* Range of first echo received */
  32. #define SRF08_CMD_RANGING_CM 0x51 /* Ranging Mode - Result in cm */
  33. enum srf08_sensor_type {
  34. SRF02,
  35. SRF08,
  36. SRF10,
  37. SRF_MAX_TYPE
  38. };
  39. struct srf08_chip_info {
  40. const int *sensitivity_avail;
  41. int num_sensitivity_avail;
  42. int sensitivity_default;
  43. /* default value of Range in mm */
  44. int range_default;
  45. };
  46. struct srf08_data {
  47. struct i2c_client *client;
  48. /*
  49. * Gain in the datasheet is called sensitivity here to distinct it
  50. * from the gain used with amplifiers of adc's
  51. */
  52. int sensitivity;
  53. /* max. Range in mm */
  54. int range_mm;
  55. struct mutex lock;
  56. /* Ensure timestamp is naturally aligned */
  57. struct {
  58. s16 chan;
  59. s64 timestamp __aligned(8);
  60. } scan;
  61. /* Sensor-Type */
  62. enum srf08_sensor_type sensor_type;
  63. /* Chip-specific information */
  64. const struct srf08_chip_info *chip_info;
  65. };
  66. /*
  67. * in the documentation one can read about the "Gain" of the device
  68. * which is used here for amplifying the signal and filtering out unwanted
  69. * ones.
  70. * But with ADC's this term is already used differently and that's why it
  71. * is called "Sensitivity" here.
  72. */
  73. static const struct srf08_chip_info srf02_chip_info = {
  74. .sensitivity_avail = NULL,
  75. .num_sensitivity_avail = 0,
  76. .sensitivity_default = 0,
  77. .range_default = 0,
  78. };
  79. static const int srf08_sensitivity_avail[] = {
  80. 94, 97, 100, 103, 107, 110, 114, 118,
  81. 123, 128, 133, 139, 145, 152, 159, 168,
  82. 177, 187, 199, 212, 227, 245, 265, 288,
  83. 317, 352, 395, 450, 524, 626, 777, 1025
  84. };
  85. static const struct srf08_chip_info srf08_chip_info = {
  86. .sensitivity_avail = srf08_sensitivity_avail,
  87. .num_sensitivity_avail = ARRAY_SIZE(srf08_sensitivity_avail),
  88. .sensitivity_default = 1025,
  89. .range_default = 6020,
  90. };
  91. static const int srf10_sensitivity_avail[] = {
  92. 40, 40, 50, 60, 70, 80, 100, 120,
  93. 140, 200, 250, 300, 350, 400, 500, 600,
  94. 700,
  95. };
  96. static const struct srf08_chip_info srf10_chip_info = {
  97. .sensitivity_avail = srf10_sensitivity_avail,
  98. .num_sensitivity_avail = ARRAY_SIZE(srf10_sensitivity_avail),
  99. .sensitivity_default = 700,
  100. .range_default = 6020,
  101. };
  102. static int srf08_read_ranging(struct srf08_data *data)
  103. {
  104. struct i2c_client *client = data->client;
  105. int ret, i;
  106. int waittime;
  107. mutex_lock(&data->lock);
  108. ret = i2c_smbus_write_byte_data(data->client,
  109. SRF08_WRITE_COMMAND, SRF08_CMD_RANGING_CM);
  110. if (ret < 0) {
  111. dev_err(&client->dev, "write command - err: %d\n", ret);
  112. mutex_unlock(&data->lock);
  113. return ret;
  114. }
  115. /*
  116. * we read here until a correct version number shows up as
  117. * suggested by the documentation
  118. *
  119. * with an ultrasonic speed of 343 m/s and a roundtrip of it
  120. * sleep the expected duration and try to read from the device
  121. * if nothing useful is read try it in a shorter grid
  122. *
  123. * polling for not more than 20 ms should be enough
  124. */
  125. waittime = 1 + data->range_mm / 172;
  126. msleep(waittime);
  127. for (i = 0; i < 4; i++) {
  128. ret = i2c_smbus_read_byte_data(data->client,
  129. SRF08_READ_SW_REVISION);
  130. /* check if a valid version number is read */
  131. if (ret < 255 && ret > 0)
  132. break;
  133. msleep(5);
  134. }
  135. if (ret >= 255 || ret <= 0) {
  136. dev_err(&client->dev, "device not ready\n");
  137. mutex_unlock(&data->lock);
  138. return -EIO;
  139. }
  140. ret = i2c_smbus_read_word_swapped(data->client,
  141. SRF08_READ_ECHO_1_HIGH);
  142. if (ret < 0) {
  143. dev_err(&client->dev, "cannot read distance: ret=%d\n", ret);
  144. mutex_unlock(&data->lock);
  145. return ret;
  146. }
  147. mutex_unlock(&data->lock);
  148. return ret;
  149. }
  150. static irqreturn_t srf08_trigger_handler(int irq, void *p)
  151. {
  152. struct iio_poll_func *pf = p;
  153. struct iio_dev *indio_dev = pf->indio_dev;
  154. struct srf08_data *data = iio_priv(indio_dev);
  155. s16 sensor_data;
  156. sensor_data = srf08_read_ranging(data);
  157. if (sensor_data < 0)
  158. goto err;
  159. mutex_lock(&data->lock);
  160. data->scan.chan = sensor_data;
  161. iio_push_to_buffers_with_timestamp(indio_dev,
  162. &data->scan, pf->timestamp);
  163. mutex_unlock(&data->lock);
  164. err:
  165. iio_trigger_notify_done(indio_dev->trig);
  166. return IRQ_HANDLED;
  167. }
  168. static int srf08_read_raw(struct iio_dev *indio_dev,
  169. struct iio_chan_spec const *channel, int *val,
  170. int *val2, long mask)
  171. {
  172. struct srf08_data *data = iio_priv(indio_dev);
  173. int ret;
  174. if (channel->type != IIO_DISTANCE)
  175. return -EINVAL;
  176. switch (mask) {
  177. case IIO_CHAN_INFO_RAW:
  178. ret = srf08_read_ranging(data);
  179. if (ret < 0)
  180. return ret;
  181. *val = ret;
  182. return IIO_VAL_INT;
  183. case IIO_CHAN_INFO_SCALE:
  184. /* 1 LSB is 1 cm */
  185. *val = 0;
  186. *val2 = 10000;
  187. return IIO_VAL_INT_PLUS_MICRO;
  188. default:
  189. return -EINVAL;
  190. }
  191. }
  192. static ssize_t srf08_show_range_mm_available(struct device *dev,
  193. struct device_attribute *attr, char *buf)
  194. {
  195. return sprintf(buf, "[0.043 0.043 11.008]\n");
  196. }
  197. static IIO_DEVICE_ATTR(sensor_max_range_available, S_IRUGO,
  198. srf08_show_range_mm_available, NULL, 0);
  199. static ssize_t srf08_show_range_mm(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  203. struct srf08_data *data = iio_priv(indio_dev);
  204. return sprintf(buf, "%d.%03d\n", data->range_mm / 1000,
  205. data->range_mm % 1000);
  206. }
  207. /*
  208. * set the range of the sensor to an even multiple of 43 mm
  209. * which corresponds to 1 LSB in the register
  210. *
  211. * register value corresponding range
  212. * 0x00 43 mm
  213. * 0x01 86 mm
  214. * 0x02 129 mm
  215. * ...
  216. * 0xFF 11008 mm
  217. */
  218. static ssize_t srf08_write_range_mm(struct srf08_data *data, unsigned int val)
  219. {
  220. int ret;
  221. struct i2c_client *client = data->client;
  222. unsigned int mod;
  223. u8 regval;
  224. ret = val / 43 - 1;
  225. mod = val % 43;
  226. if (mod || (ret < 0) || (ret > 255))
  227. return -EINVAL;
  228. regval = ret;
  229. mutex_lock(&data->lock);
  230. ret = i2c_smbus_write_byte_data(client, SRF08_WRITE_RANGE, regval);
  231. if (ret < 0) {
  232. dev_err(&client->dev, "write_range - err: %d\n", ret);
  233. mutex_unlock(&data->lock);
  234. return ret;
  235. }
  236. data->range_mm = val;
  237. mutex_unlock(&data->lock);
  238. return 0;
  239. }
  240. static ssize_t srf08_store_range_mm(struct device *dev,
  241. struct device_attribute *attr,
  242. const char *buf, size_t len)
  243. {
  244. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  245. struct srf08_data *data = iio_priv(indio_dev);
  246. int ret;
  247. int integer, fract;
  248. ret = iio_str_to_fixpoint(buf, 100, &integer, &fract);
  249. if (ret)
  250. return ret;
  251. ret = srf08_write_range_mm(data, integer * 1000 + fract);
  252. if (ret < 0)
  253. return ret;
  254. return len;
  255. }
  256. static IIO_DEVICE_ATTR(sensor_max_range, S_IRUGO | S_IWUSR,
  257. srf08_show_range_mm, srf08_store_range_mm, 0);
  258. static ssize_t srf08_show_sensitivity_available(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. int i, len = 0;
  262. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  263. struct srf08_data *data = iio_priv(indio_dev);
  264. for (i = 0; i < data->chip_info->num_sensitivity_avail; i++)
  265. if (data->chip_info->sensitivity_avail[i])
  266. len += sprintf(buf + len, "%d ",
  267. data->chip_info->sensitivity_avail[i]);
  268. len += sprintf(buf + len, "\n");
  269. return len;
  270. }
  271. static IIO_DEVICE_ATTR(sensor_sensitivity_available, S_IRUGO,
  272. srf08_show_sensitivity_available, NULL, 0);
  273. static ssize_t srf08_show_sensitivity(struct device *dev,
  274. struct device_attribute *attr, char *buf)
  275. {
  276. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  277. struct srf08_data *data = iio_priv(indio_dev);
  278. int len;
  279. len = sprintf(buf, "%d\n", data->sensitivity);
  280. return len;
  281. }
  282. static ssize_t srf08_write_sensitivity(struct srf08_data *data,
  283. unsigned int val)
  284. {
  285. struct i2c_client *client = data->client;
  286. int ret, i;
  287. u8 regval;
  288. if (!val)
  289. return -EINVAL;
  290. for (i = 0; i < data->chip_info->num_sensitivity_avail; i++)
  291. if (val && (val == data->chip_info->sensitivity_avail[i])) {
  292. regval = i;
  293. break;
  294. }
  295. if (i >= data->chip_info->num_sensitivity_avail)
  296. return -EINVAL;
  297. mutex_lock(&data->lock);
  298. ret = i2c_smbus_write_byte_data(client, SRF08_WRITE_MAX_GAIN, regval);
  299. if (ret < 0) {
  300. dev_err(&client->dev, "write_sensitivity - err: %d\n", ret);
  301. mutex_unlock(&data->lock);
  302. return ret;
  303. }
  304. data->sensitivity = val;
  305. mutex_unlock(&data->lock);
  306. return 0;
  307. }
  308. static ssize_t srf08_store_sensitivity(struct device *dev,
  309. struct device_attribute *attr,
  310. const char *buf, size_t len)
  311. {
  312. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  313. struct srf08_data *data = iio_priv(indio_dev);
  314. int ret;
  315. unsigned int val;
  316. ret = kstrtouint(buf, 10, &val);
  317. if (ret)
  318. return ret;
  319. ret = srf08_write_sensitivity(data, val);
  320. if (ret < 0)
  321. return ret;
  322. return len;
  323. }
  324. static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
  325. srf08_show_sensitivity, srf08_store_sensitivity, 0);
  326. static struct attribute *srf08_attributes[] = {
  327. &iio_dev_attr_sensor_max_range.dev_attr.attr,
  328. &iio_dev_attr_sensor_max_range_available.dev_attr.attr,
  329. &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
  330. &iio_dev_attr_sensor_sensitivity_available.dev_attr.attr,
  331. NULL,
  332. };
  333. static const struct attribute_group srf08_attribute_group = {
  334. .attrs = srf08_attributes,
  335. };
  336. static const struct iio_chan_spec srf08_channels[] = {
  337. {
  338. .type = IIO_DISTANCE,
  339. .info_mask_separate =
  340. BIT(IIO_CHAN_INFO_RAW) |
  341. BIT(IIO_CHAN_INFO_SCALE),
  342. .scan_index = 0,
  343. .scan_type = {
  344. .sign = 's',
  345. .realbits = 16,
  346. .storagebits = 16,
  347. .endianness = IIO_CPU,
  348. },
  349. },
  350. IIO_CHAN_SOFT_TIMESTAMP(1),
  351. };
  352. static const struct iio_info srf08_info = {
  353. .read_raw = srf08_read_raw,
  354. .attrs = &srf08_attribute_group,
  355. };
  356. /*
  357. * srf02 don't have an adjustable range or sensitivity,
  358. * so we don't need attributes at all
  359. */
  360. static const struct iio_info srf02_info = {
  361. .read_raw = srf08_read_raw,
  362. };
  363. static int srf08_probe(struct i2c_client *client,
  364. const struct i2c_device_id *id)
  365. {
  366. struct iio_dev *indio_dev;
  367. struct srf08_data *data;
  368. int ret;
  369. if (!i2c_check_functionality(client->adapter,
  370. I2C_FUNC_SMBUS_READ_BYTE_DATA |
  371. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  372. I2C_FUNC_SMBUS_READ_WORD_DATA))
  373. return -ENODEV;
  374. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  375. if (!indio_dev)
  376. return -ENOMEM;
  377. data = iio_priv(indio_dev);
  378. i2c_set_clientdata(client, indio_dev);
  379. data->client = client;
  380. data->sensor_type = (enum srf08_sensor_type)id->driver_data;
  381. switch (data->sensor_type) {
  382. case SRF02:
  383. data->chip_info = &srf02_chip_info;
  384. indio_dev->info = &srf02_info;
  385. break;
  386. case SRF08:
  387. data->chip_info = &srf08_chip_info;
  388. indio_dev->info = &srf08_info;
  389. break;
  390. case SRF10:
  391. data->chip_info = &srf10_chip_info;
  392. indio_dev->info = &srf08_info;
  393. break;
  394. default:
  395. return -EINVAL;
  396. }
  397. indio_dev->name = id->name;
  398. indio_dev->modes = INDIO_DIRECT_MODE;
  399. indio_dev->channels = srf08_channels;
  400. indio_dev->num_channels = ARRAY_SIZE(srf08_channels);
  401. mutex_init(&data->lock);
  402. ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
  403. iio_pollfunc_store_time, srf08_trigger_handler, NULL);
  404. if (ret < 0) {
  405. dev_err(&client->dev, "setup of iio triggered buffer failed\n");
  406. return ret;
  407. }
  408. if (data->chip_info->range_default) {
  409. /*
  410. * set default range of device in mm here
  411. * these register values cannot be read from the hardware
  412. * therefore set driver specific default values
  413. *
  414. * srf02 don't have a default value so it'll be omitted
  415. */
  416. ret = srf08_write_range_mm(data,
  417. data->chip_info->range_default);
  418. if (ret < 0)
  419. return ret;
  420. }
  421. if (data->chip_info->sensitivity_default) {
  422. /*
  423. * set default sensitivity of device here
  424. * these register values cannot be read from the hardware
  425. * therefore set driver specific default values
  426. *
  427. * srf02 don't have a default value so it'll be omitted
  428. */
  429. ret = srf08_write_sensitivity(data,
  430. data->chip_info->sensitivity_default);
  431. if (ret < 0)
  432. return ret;
  433. }
  434. return devm_iio_device_register(&client->dev, indio_dev);
  435. }
  436. static const struct of_device_id of_srf08_match[] = {
  437. { .compatible = "devantech,srf02", (void *)SRF02},
  438. { .compatible = "devantech,srf08", (void *)SRF08},
  439. { .compatible = "devantech,srf10", (void *)SRF10},
  440. {},
  441. };
  442. MODULE_DEVICE_TABLE(of, of_srf08_match);
  443. static const struct i2c_device_id srf08_id[] = {
  444. { "srf02", SRF02 },
  445. { "srf08", SRF08 },
  446. { "srf10", SRF10 },
  447. { }
  448. };
  449. MODULE_DEVICE_TABLE(i2c, srf08_id);
  450. static struct i2c_driver srf08_driver = {
  451. .driver = {
  452. .name = "srf08",
  453. .of_match_table = of_srf08_match,
  454. },
  455. .probe = srf08_probe,
  456. .id_table = srf08_id,
  457. };
  458. module_i2c_driver(srf08_driver);
  459. MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
  460. MODULE_DESCRIPTION("Devantech SRF02/SRF08/SRF10 i2c ultrasonic ranger driver");
  461. MODULE_LICENSE("GPL");