2 * srf08.c - Support for Devantech SRFxx ultrasonic ranger
4 * actually supported are srf02, srf08, srf10
6 * Copyright (c) 2016, 2017 Andreas Klinger <ak@it-klinger.de>
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.
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
18 #include <linux/err.h>
19 #include <linux/i2c.h>
20 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/bitops.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/trigger_consumer.h>
27 #include <linux/iio/triggered_buffer.h>
29 /* registers of SRF08 device */
30 #define SRF08_WRITE_COMMAND 0x00 /* Command Register */
31 #define SRF08_WRITE_MAX_GAIN 0x01 /* Max Gain Register: 0 .. 31 */
32 #define SRF08_WRITE_RANGE 0x02 /* Range Register: 0 .. 255 */
33 #define SRF08_READ_SW_REVISION 0x00 /* Software Revision */
34 #define SRF08_READ_LIGHT 0x01 /* Light Sensor during last echo */
35 #define SRF08_READ_ECHO_1_HIGH 0x02 /* Range of first echo received */
36 #define SRF08_READ_ECHO_1_LOW 0x03 /* Range of first echo received */
38 #define SRF08_CMD_RANGING_CM 0x51 /* Ranging Mode - Result in cm */
40 enum srf08_sensor_type
{
47 struct srf08_chip_info
{
48 const int *sensitivity_avail
;
49 int num_sensitivity_avail
;
50 int sensitivity_default
;
52 /* default value of Range in mm */
57 struct i2c_client
*client
;
60 * Gain in the datasheet is called sensitivity here to distinct it
61 * from the gain used with amplifiers of adc's
65 /* max. Range in mm */
71 * 1x16-bit channel + 3x16 padding + 4x16 timestamp
76 enum srf08_sensor_type sensor_type
;
78 /* Chip-specific information */
79 const struct srf08_chip_info
*chip_info
;
83 * in the documentation one can read about the "Gain" of the device
84 * which is used here for amplifying the signal and filtering out unwanted
86 * But with ADC's this term is already used differently and that's why it
87 * is called "Sensitivity" here.
89 static const struct srf08_chip_info srf02_chip_info
= {
90 .sensitivity_avail
= NULL
,
91 .num_sensitivity_avail
= 0,
92 .sensitivity_default
= 0,
97 static const int srf08_sensitivity_avail
[] = {
98 94, 97, 100, 103, 107, 110, 114, 118,
99 123, 128, 133, 139, 145, 152, 159, 168,
100 177, 187, 199, 212, 227, 245, 265, 288,
101 317, 352, 395, 450, 524, 626, 777, 1025
104 static const struct srf08_chip_info srf08_chip_info
= {
105 .sensitivity_avail
= srf08_sensitivity_avail
,
106 .num_sensitivity_avail
= ARRAY_SIZE(srf08_sensitivity_avail
),
107 .sensitivity_default
= 1025,
109 .range_default
= 6020,
112 static const int srf10_sensitivity_avail
[] = {
113 40, 40, 50, 60, 70, 80, 100, 120,
114 140, 200, 250, 300, 350, 400, 500, 600,
118 static const struct srf08_chip_info srf10_chip_info
= {
119 .sensitivity_avail
= srf10_sensitivity_avail
,
120 .num_sensitivity_avail
= ARRAY_SIZE(srf10_sensitivity_avail
),
121 .sensitivity_default
= 700,
123 .range_default
= 6020,
126 static int srf08_read_ranging(struct srf08_data
*data
)
128 struct i2c_client
*client
= data
->client
;
132 mutex_lock(&data
->lock
);
134 ret
= i2c_smbus_write_byte_data(data
->client
,
135 SRF08_WRITE_COMMAND
, SRF08_CMD_RANGING_CM
);
137 dev_err(&client
->dev
, "write command - err: %d\n", ret
);
138 mutex_unlock(&data
->lock
);
143 * we read here until a correct version number shows up as
144 * suggested by the documentation
146 * with an ultrasonic speed of 343 m/s and a roundtrip of it
147 * sleep the expected duration and try to read from the device
148 * if nothing useful is read try it in a shorter grid
150 * polling for not more than 20 ms should be enough
152 waittime
= 1 + data
->range_mm
/ 172;
154 for (i
= 0; i
< 4; i
++) {
155 ret
= i2c_smbus_read_byte_data(data
->client
,
156 SRF08_READ_SW_REVISION
);
158 /* check if a valid version number is read */
159 if (ret
< 255 && ret
> 0)
164 if (ret
>= 255 || ret
<= 0) {
165 dev_err(&client
->dev
, "device not ready\n");
166 mutex_unlock(&data
->lock
);
170 ret
= i2c_smbus_read_word_swapped(data
->client
,
171 SRF08_READ_ECHO_1_HIGH
);
173 dev_err(&client
->dev
, "cannot read distance: ret=%d\n", ret
);
174 mutex_unlock(&data
->lock
);
178 mutex_unlock(&data
->lock
);
183 static irqreturn_t
srf08_trigger_handler(int irq
, void *p
)
185 struct iio_poll_func
*pf
= p
;
186 struct iio_dev
*indio_dev
= pf
->indio_dev
;
187 struct srf08_data
*data
= iio_priv(indio_dev
);
190 sensor_data
= srf08_read_ranging(data
);
194 mutex_lock(&data
->lock
);
196 data
->buffer
[0] = sensor_data
;
197 iio_push_to_buffers_with_timestamp(indio_dev
,
198 data
->buffer
, pf
->timestamp
);
200 mutex_unlock(&data
->lock
);
202 iio_trigger_notify_done(indio_dev
->trig
);
206 static int srf08_read_raw(struct iio_dev
*indio_dev
,
207 struct iio_chan_spec
const *channel
, int *val
,
208 int *val2
, long mask
)
210 struct srf08_data
*data
= iio_priv(indio_dev
);
213 if (channel
->type
!= IIO_DISTANCE
)
217 case IIO_CHAN_INFO_RAW
:
218 ret
= srf08_read_ranging(data
);
223 case IIO_CHAN_INFO_SCALE
:
227 return IIO_VAL_INT_PLUS_MICRO
;
233 static ssize_t
srf08_show_range_mm_available(struct device
*dev
,
234 struct device_attribute
*attr
, char *buf
)
236 return sprintf(buf
, "[0.043 0.043 11.008]\n");
239 static IIO_DEVICE_ATTR(sensor_max_range_available
, S_IRUGO
,
240 srf08_show_range_mm_available
, NULL
, 0);
242 static ssize_t
srf08_show_range_mm(struct device
*dev
,
243 struct device_attribute
*attr
, char *buf
)
245 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
246 struct srf08_data
*data
= iio_priv(indio_dev
);
248 return sprintf(buf
, "%d.%03d\n", data
->range_mm
/ 1000,
249 data
->range_mm
% 1000);
253 * set the range of the sensor to an even multiple of 43 mm
254 * which corresponds to 1 LSB in the register
256 * register value corresponding range
263 static ssize_t
srf08_write_range_mm(struct srf08_data
*data
, unsigned int val
)
266 struct i2c_client
*client
= data
->client
;
273 if (mod
|| (ret
< 0) || (ret
> 255))
278 mutex_lock(&data
->lock
);
280 ret
= i2c_smbus_write_byte_data(client
, SRF08_WRITE_RANGE
, regval
);
282 dev_err(&client
->dev
, "write_range - err: %d\n", ret
);
283 mutex_unlock(&data
->lock
);
287 data
->range_mm
= val
;
289 mutex_unlock(&data
->lock
);
294 static ssize_t
srf08_store_range_mm(struct device
*dev
,
295 struct device_attribute
*attr
,
296 const char *buf
, size_t len
)
298 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
299 struct srf08_data
*data
= iio_priv(indio_dev
);
303 ret
= iio_str_to_fixpoint(buf
, 100, &integer
, &fract
);
307 ret
= srf08_write_range_mm(data
, integer
* 1000 + fract
);
314 static IIO_DEVICE_ATTR(sensor_max_range
, S_IRUGO
| S_IWUSR
,
315 srf08_show_range_mm
, srf08_store_range_mm
, 0);
317 static ssize_t
srf08_show_sensitivity_available(struct device
*dev
,
318 struct device_attribute
*attr
, char *buf
)
321 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
322 struct srf08_data
*data
= iio_priv(indio_dev
);
324 for (i
= 0; i
< data
->chip_info
->num_sensitivity_avail
; i
++)
325 if (data
->chip_info
->sensitivity_avail
[i
])
326 len
+= sprintf(buf
+ len
, "%d ",
327 data
->chip_info
->sensitivity_avail
[i
]);
329 len
+= sprintf(buf
+ len
, "\n");
334 static IIO_DEVICE_ATTR(sensor_sensitivity_available
, S_IRUGO
,
335 srf08_show_sensitivity_available
, NULL
, 0);
337 static ssize_t
srf08_show_sensitivity(struct device
*dev
,
338 struct device_attribute
*attr
, char *buf
)
340 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
341 struct srf08_data
*data
= iio_priv(indio_dev
);
344 len
= sprintf(buf
, "%d\n", data
->sensitivity
);
349 static ssize_t
srf08_write_sensitivity(struct srf08_data
*data
,
352 struct i2c_client
*client
= data
->client
;
359 for (i
= 0; i
< data
->chip_info
->num_sensitivity_avail
; i
++)
360 if (val
&& (val
== data
->chip_info
->sensitivity_avail
[i
])) {
365 if (i
>= data
->chip_info
->num_sensitivity_avail
)
368 mutex_lock(&data
->lock
);
370 ret
= i2c_smbus_write_byte_data(client
, SRF08_WRITE_MAX_GAIN
, regval
);
372 dev_err(&client
->dev
, "write_sensitivity - err: %d\n", ret
);
373 mutex_unlock(&data
->lock
);
377 data
->sensitivity
= val
;
379 mutex_unlock(&data
->lock
);
384 static ssize_t
srf08_store_sensitivity(struct device
*dev
,
385 struct device_attribute
*attr
,
386 const char *buf
, size_t len
)
388 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
389 struct srf08_data
*data
= iio_priv(indio_dev
);
393 ret
= kstrtouint(buf
, 10, &val
);
397 ret
= srf08_write_sensitivity(data
, val
);
404 static IIO_DEVICE_ATTR(sensor_sensitivity
, S_IRUGO
| S_IWUSR
,
405 srf08_show_sensitivity
, srf08_store_sensitivity
, 0);
407 static struct attribute
*srf08_attributes
[] = {
408 &iio_dev_attr_sensor_max_range
.dev_attr
.attr
,
409 &iio_dev_attr_sensor_max_range_available
.dev_attr
.attr
,
410 &iio_dev_attr_sensor_sensitivity
.dev_attr
.attr
,
411 &iio_dev_attr_sensor_sensitivity_available
.dev_attr
.attr
,
415 static const struct attribute_group srf08_attribute_group
= {
416 .attrs
= srf08_attributes
,
419 static const struct iio_chan_spec srf08_channels
[] = {
421 .type
= IIO_DISTANCE
,
422 .info_mask_separate
=
423 BIT(IIO_CHAN_INFO_RAW
) |
424 BIT(IIO_CHAN_INFO_SCALE
),
430 .endianness
= IIO_CPU
,
433 IIO_CHAN_SOFT_TIMESTAMP(1),
436 static const struct iio_info srf08_info
= {
437 .read_raw
= srf08_read_raw
,
438 .attrs
= &srf08_attribute_group
,
442 * srf02 don't have an adjustable range or sensitivity,
443 * so we don't need attributes at all
445 static const struct iio_info srf02_info
= {
446 .read_raw
= srf08_read_raw
,
449 static int srf08_probe(struct i2c_client
*client
,
450 const struct i2c_device_id
*id
)
452 struct iio_dev
*indio_dev
;
453 struct srf08_data
*data
;
456 if (!i2c_check_functionality(client
->adapter
,
457 I2C_FUNC_SMBUS_READ_BYTE_DATA
|
458 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
459 I2C_FUNC_SMBUS_READ_WORD_DATA
))
462 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
466 data
= iio_priv(indio_dev
);
467 i2c_set_clientdata(client
, indio_dev
);
468 data
->client
= client
;
469 data
->sensor_type
= (enum srf08_sensor_type
)id
->driver_data
;
471 switch (data
->sensor_type
) {
473 data
->chip_info
= &srf02_chip_info
;
474 indio_dev
->info
= &srf02_info
;
477 data
->chip_info
= &srf08_chip_info
;
478 indio_dev
->info
= &srf08_info
;
481 data
->chip_info
= &srf10_chip_info
;
482 indio_dev
->info
= &srf08_info
;
488 indio_dev
->name
= id
->name
;
489 indio_dev
->dev
.parent
= &client
->dev
;
490 indio_dev
->modes
= INDIO_DIRECT_MODE
;
491 indio_dev
->channels
= srf08_channels
;
492 indio_dev
->num_channels
= ARRAY_SIZE(srf08_channels
);
494 mutex_init(&data
->lock
);
496 ret
= devm_iio_triggered_buffer_setup(&client
->dev
, indio_dev
,
497 iio_pollfunc_store_time
, srf08_trigger_handler
, NULL
);
499 dev_err(&client
->dev
, "setup of iio triggered buffer failed\n");
503 if (data
->chip_info
->range_default
) {
505 * set default range of device in mm here
506 * these register values cannot be read from the hardware
507 * therefore set driver specific default values
509 * srf02 don't have a default value so it'll be omitted
511 ret
= srf08_write_range_mm(data
,
512 data
->chip_info
->range_default
);
517 if (data
->chip_info
->sensitivity_default
) {
519 * set default sensitivity of device here
520 * these register values cannot be read from the hardware
521 * therefore set driver specific default values
523 * srf02 don't have a default value so it'll be omitted
525 ret
= srf08_write_sensitivity(data
,
526 data
->chip_info
->sensitivity_default
);
531 return devm_iio_device_register(&client
->dev
, indio_dev
);
534 static const struct of_device_id of_srf08_match
[] = {
535 { .compatible
= "devantech,srf02", (void *)SRF02
},
536 { .compatible
= "devantech,srf08", (void *)SRF08
},
537 { .compatible
= "devantech,srf10", (void *)SRF10
},
541 MODULE_DEVICE_TABLE(of
, of_srf08_match
);
543 static const struct i2c_device_id srf08_id
[] = {
549 MODULE_DEVICE_TABLE(i2c
, srf08_id
);
551 static struct i2c_driver srf08_driver
= {
554 .of_match_table
= of_srf08_match
,
556 .probe
= srf08_probe
,
557 .id_table
= srf08_id
,
559 module_i2c_driver(srf08_driver
);
561 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
562 MODULE_DESCRIPTION("Devantech SRF02/SRF08/SRF10 i2c ultrasonic ranger driver");
563 MODULE_LICENSE("GPL");