1 // SPDX-License-Identifier: GPL-2.0-only
3 * srf08.c - Support for Devantech SRFxx ultrasonic ranger
5 * actually supported are srf02, srf08, srf10
7 * Copyright (c) 2016, 2017 Andreas Klinger <ak@it-klinger.de>
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
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/bitops.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
26 /* registers of SRF08 device */
27 #define SRF08_WRITE_COMMAND 0x00 /* Command Register */
28 #define SRF08_WRITE_MAX_GAIN 0x01 /* Max Gain Register: 0 .. 31 */
29 #define SRF08_WRITE_RANGE 0x02 /* Range Register: 0 .. 255 */
30 #define SRF08_READ_SW_REVISION 0x00 /* Software Revision */
31 #define SRF08_READ_LIGHT 0x01 /* Light Sensor during last echo */
32 #define SRF08_READ_ECHO_1_HIGH 0x02 /* Range of first echo received */
33 #define SRF08_READ_ECHO_1_LOW 0x03 /* Range of first echo received */
35 #define SRF08_CMD_RANGING_CM 0x51 /* Ranging Mode - Result in cm */
37 enum srf08_sensor_type {
44 struct srf08_chip_info {
45 const int *sensitivity_avail;
46 int num_sensitivity_avail;
47 int sensitivity_default;
49 /* default value of Range in mm */
54 struct i2c_client *client;
57 * Gain in the datasheet is called sensitivity here to distinct it
58 * from the gain used with amplifiers of adc's
62 /* max. Range in mm */
66 /* Ensure timestamp is naturally aligned */
69 s64 timestamp __aligned(8);
73 enum srf08_sensor_type sensor_type;
75 /* Chip-specific information */
76 const struct srf08_chip_info *chip_info;
80 * in the documentation one can read about the "Gain" of the device
81 * which is used here for amplifying the signal and filtering out unwanted
83 * But with ADC's this term is already used differently and that's why it
84 * is called "Sensitivity" here.
86 static const struct srf08_chip_info srf02_chip_info = {
87 .sensitivity_avail = NULL,
88 .num_sensitivity_avail = 0,
89 .sensitivity_default = 0,
94 static const int srf08_sensitivity_avail[] = {
95 94, 97, 100, 103, 107, 110, 114, 118,
96 123, 128, 133, 139, 145, 152, 159, 168,
97 177, 187, 199, 212, 227, 245, 265, 288,
98 317, 352, 395, 450, 524, 626, 777, 1025
101 static const struct srf08_chip_info srf08_chip_info = {
102 .sensitivity_avail = srf08_sensitivity_avail,
103 .num_sensitivity_avail = ARRAY_SIZE(srf08_sensitivity_avail),
104 .sensitivity_default = 1025,
106 .range_default = 6020,
109 static const int srf10_sensitivity_avail[] = {
110 40, 40, 50, 60, 70, 80, 100, 120,
111 140, 200, 250, 300, 350, 400, 500, 600,
115 static const struct srf08_chip_info srf10_chip_info = {
116 .sensitivity_avail = srf10_sensitivity_avail,
117 .num_sensitivity_avail = ARRAY_SIZE(srf10_sensitivity_avail),
118 .sensitivity_default = 700,
120 .range_default = 6020,
123 static int srf08_read_ranging(struct srf08_data *data)
125 struct i2c_client *client = data->client;
129 mutex_lock(&data->lock);
131 ret = i2c_smbus_write_byte_data(data->client,
132 SRF08_WRITE_COMMAND, SRF08_CMD_RANGING_CM);
134 dev_err(&client->dev, "write command - err: %d\n", ret);
135 mutex_unlock(&data->lock);
140 * we read here until a correct version number shows up as
141 * suggested by the documentation
143 * with an ultrasonic speed of 343 m/s and a roundtrip of it
144 * sleep the expected duration and try to read from the device
145 * if nothing useful is read try it in a shorter grid
147 * polling for not more than 20 ms should be enough
149 waittime = 1 + data->range_mm / 172;
151 for (i = 0; i < 4; i++) {
152 ret = i2c_smbus_read_byte_data(data->client,
153 SRF08_READ_SW_REVISION);
155 /* check if a valid version number is read */
156 if (ret < 255 && ret > 0)
161 if (ret >= 255 || ret <= 0) {
162 dev_err(&client->dev, "device not ready\n");
163 mutex_unlock(&data->lock);
167 ret = i2c_smbus_read_word_swapped(data->client,
168 SRF08_READ_ECHO_1_HIGH);
170 dev_err(&client->dev, "cannot read distance: ret=%d\n", ret);
171 mutex_unlock(&data->lock);
175 mutex_unlock(&data->lock);
180 static irqreturn_t srf08_trigger_handler(int irq, void *p)
182 struct iio_poll_func *pf = p;
183 struct iio_dev *indio_dev = pf->indio_dev;
184 struct srf08_data *data = iio_priv(indio_dev);
187 sensor_data = srf08_read_ranging(data);
191 mutex_lock(&data->lock);
193 data->scan.chan = sensor_data;
194 iio_push_to_buffers_with_timestamp(indio_dev,
195 &data->scan, pf->timestamp);
197 mutex_unlock(&data->lock);
199 iio_trigger_notify_done(indio_dev->trig);
203 static int srf08_read_raw(struct iio_dev *indio_dev,
204 struct iio_chan_spec const *channel, int *val,
205 int *val2, long mask)
207 struct srf08_data *data = iio_priv(indio_dev);
210 if (channel->type != IIO_DISTANCE)
214 case IIO_CHAN_INFO_RAW:
215 ret = srf08_read_ranging(data);
220 case IIO_CHAN_INFO_SCALE:
224 return IIO_VAL_INT_PLUS_MICRO;
230 static ssize_t srf08_show_range_mm_available(struct device *dev,
231 struct device_attribute *attr, char *buf)
233 return sprintf(buf, "[0.043 0.043 11.008]\n");
236 static IIO_DEVICE_ATTR(sensor_max_range_available, S_IRUGO,
237 srf08_show_range_mm_available, NULL, 0);
239 static ssize_t srf08_show_range_mm(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
243 struct srf08_data *data = iio_priv(indio_dev);
245 return sprintf(buf, "%d.%03d\n", data->range_mm / 1000,
246 data->range_mm % 1000);
250 * set the range of the sensor to an even multiple of 43 mm
251 * which corresponds to 1 LSB in the register
253 * register value corresponding range
260 static ssize_t srf08_write_range_mm(struct srf08_data *data, unsigned int val)
263 struct i2c_client *client = data->client;
270 if (mod || (ret < 0) || (ret > 255))
275 mutex_lock(&data->lock);
277 ret = i2c_smbus_write_byte_data(client, SRF08_WRITE_RANGE, regval);
279 dev_err(&client->dev, "write_range - err: %d\n", ret);
280 mutex_unlock(&data->lock);
284 data->range_mm = val;
286 mutex_unlock(&data->lock);
291 static ssize_t srf08_store_range_mm(struct device *dev,
292 struct device_attribute *attr,
293 const char *buf, size_t len)
295 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
296 struct srf08_data *data = iio_priv(indio_dev);
300 ret = iio_str_to_fixpoint(buf, 100, &integer, &fract);
304 ret = srf08_write_range_mm(data, integer * 1000 + fract);
311 static IIO_DEVICE_ATTR(sensor_max_range, S_IRUGO | S_IWUSR,
312 srf08_show_range_mm, srf08_store_range_mm, 0);
314 static ssize_t srf08_show_sensitivity_available(struct device *dev,
315 struct device_attribute *attr, char *buf)
318 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
319 struct srf08_data *data = iio_priv(indio_dev);
321 for (i = 0; i < data->chip_info->num_sensitivity_avail; i++)
322 if (data->chip_info->sensitivity_avail[i])
323 len += sprintf(buf + len, "%d ",
324 data->chip_info->sensitivity_avail[i]);
326 len += sprintf(buf + len, "\n");
331 static IIO_DEVICE_ATTR(sensor_sensitivity_available, S_IRUGO,
332 srf08_show_sensitivity_available, NULL, 0);
334 static ssize_t srf08_show_sensitivity(struct device *dev,
335 struct device_attribute *attr, char *buf)
337 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
338 struct srf08_data *data = iio_priv(indio_dev);
341 len = sprintf(buf, "%d\n", data->sensitivity);
346 static ssize_t srf08_write_sensitivity(struct srf08_data *data,
349 struct i2c_client *client = data->client;
356 for (i = 0; i < data->chip_info->num_sensitivity_avail; i++)
357 if (val && (val == data->chip_info->sensitivity_avail[i])) {
362 if (i >= data->chip_info->num_sensitivity_avail)
365 mutex_lock(&data->lock);
367 ret = i2c_smbus_write_byte_data(client, SRF08_WRITE_MAX_GAIN, regval);
369 dev_err(&client->dev, "write_sensitivity - err: %d\n", ret);
370 mutex_unlock(&data->lock);
374 data->sensitivity = val;
376 mutex_unlock(&data->lock);
381 static ssize_t srf08_store_sensitivity(struct device *dev,
382 struct device_attribute *attr,
383 const char *buf, size_t len)
385 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
386 struct srf08_data *data = iio_priv(indio_dev);
390 ret = kstrtouint(buf, 10, &val);
394 ret = srf08_write_sensitivity(data, val);
401 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
402 srf08_show_sensitivity, srf08_store_sensitivity, 0);
404 static struct attribute *srf08_attributes[] = {
405 &iio_dev_attr_sensor_max_range.dev_attr.attr,
406 &iio_dev_attr_sensor_max_range_available.dev_attr.attr,
407 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
408 &iio_dev_attr_sensor_sensitivity_available.dev_attr.attr,
412 static const struct attribute_group srf08_attribute_group = {
413 .attrs = srf08_attributes,
416 static const struct iio_chan_spec srf08_channels[] = {
418 .type = IIO_DISTANCE,
419 .info_mask_separate =
420 BIT(IIO_CHAN_INFO_RAW) |
421 BIT(IIO_CHAN_INFO_SCALE),
427 .endianness = IIO_CPU,
430 IIO_CHAN_SOFT_TIMESTAMP(1),
433 static const struct iio_info srf08_info = {
434 .read_raw = srf08_read_raw,
435 .attrs = &srf08_attribute_group,
439 * srf02 don't have an adjustable range or sensitivity,
440 * so we don't need attributes at all
442 static const struct iio_info srf02_info = {
443 .read_raw = srf08_read_raw,
446 static int srf08_probe(struct i2c_client *client,
447 const struct i2c_device_id *id)
449 struct iio_dev *indio_dev;
450 struct srf08_data *data;
453 if (!i2c_check_functionality(client->adapter,
454 I2C_FUNC_SMBUS_READ_BYTE_DATA |
455 I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
456 I2C_FUNC_SMBUS_READ_WORD_DATA))
459 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
463 data = iio_priv(indio_dev);
464 i2c_set_clientdata(client, indio_dev);
465 data->client = client;
466 data->sensor_type = (enum srf08_sensor_type)id->driver_data;
468 switch (data->sensor_type) {
470 data->chip_info = &srf02_chip_info;
471 indio_dev->info = &srf02_info;
474 data->chip_info = &srf08_chip_info;
475 indio_dev->info = &srf08_info;
478 data->chip_info = &srf10_chip_info;
479 indio_dev->info = &srf08_info;
485 indio_dev->name = id->name;
486 indio_dev->modes = INDIO_DIRECT_MODE;
487 indio_dev->channels = srf08_channels;
488 indio_dev->num_channels = ARRAY_SIZE(srf08_channels);
490 mutex_init(&data->lock);
492 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
493 iio_pollfunc_store_time, srf08_trigger_handler, NULL);
495 dev_err(&client->dev, "setup of iio triggered buffer failed\n");
499 if (data->chip_info->range_default) {
501 * set default range of device in mm here
502 * these register values cannot be read from the hardware
503 * therefore set driver specific default values
505 * srf02 don't have a default value so it'll be omitted
507 ret = srf08_write_range_mm(data,
508 data->chip_info->range_default);
513 if (data->chip_info->sensitivity_default) {
515 * set default sensitivity of device here
516 * these register values cannot be read from the hardware
517 * therefore set driver specific default values
519 * srf02 don't have a default value so it'll be omitted
521 ret = srf08_write_sensitivity(data,
522 data->chip_info->sensitivity_default);
527 return devm_iio_device_register(&client->dev, indio_dev);
530 static const struct of_device_id of_srf08_match[] = {
531 { .compatible = "devantech,srf02", (void *)SRF02},
532 { .compatible = "devantech,srf08", (void *)SRF08},
533 { .compatible = "devantech,srf10", (void *)SRF10},
537 MODULE_DEVICE_TABLE(of, of_srf08_match);
539 static const struct i2c_device_id srf08_id[] = {
545 MODULE_DEVICE_TABLE(i2c, srf08_id);
547 static struct i2c_driver srf08_driver = {
550 .of_match_table = of_srf08_match,
552 .probe = srf08_probe,
553 .id_table = srf08_id,
555 module_i2c_driver(srf08_driver);
557 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
558 MODULE_DESCRIPTION("Devantech SRF02/SRF08/SRF10 i2c ultrasonic ranger driver");
559 MODULE_LICENSE("GPL");