srf08.c 14 KB

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