From: Johannes Thoma
The HC-SR04 is an ultrasonic distance sensor attached to two GPIO
pins. The driver based on Industrial I/O (iio) subsystem and is
controlled via configfs and sysfs. It supports an (in theory) unlimited
number of HC-SR04 devices.
Datasheet to the device can be found at:
http://www.micropik.com/PDF/HCSR04.pdf
Signed-off-by: Johannes Thoma
---
MAINTAINERS | 7 +
drivers/iio/Kconfig | 1 +
drivers/iio/Makefile | 1 +
drivers/iio/ultrasonic-distance/Kconfig | 16 ++
drivers/iio/ultrasonic-distance/Makefile | 6 +
drivers/iio/ultrasonic-distance/hc-sr04.c | 460 ++
6 files changed, 491 insertions(+)
create mode 100644 drivers/iio/ultrasonic-distance/Kconfig
create mode 100644 drivers/iio/ultrasonic-distance/Makefile
create mode 100644 drivers/iio/ultrasonic-distance/hc-sr04.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 9c567a4..fabb338 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4988,6 +4988,13 @@ W:
http://www.kernel.org/pub/linux/kernel/people/fseidel/hdaps/
S: Maintained
F: drivers/platform/x86/hdaps.c
+
+HC-SR04 ULTRASONIC DISTANCE SENSOR DRIVER
+M: Johannes Thoma
+S: Maintained
+F: drivers/iio/ultrasonic-distance/hc-sr04.c
+
+
HDPVR USB VIDEO ENCODER DRIVER
M: Hans Verkuil
L: linux-me...@vger.kernel.org
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index 505e921..3c82aad 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -82,5 +82,6 @@ source "drivers/iio/potentiometer/Kconfig"
source "drivers/iio/pressure/Kconfig"
source "drivers/iio/proximity/Kconfig"
source "drivers/iio/temperature/Kconfig"
+source "drivers/iio/ultrasonic-distance/Kconfig"
endif # IIO
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index 20f6490..0f1c00c 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -32,3 +32,4 @@ obj-y += pressure/
obj-y += proximity/
obj-y += temperature/
obj-y += trigger/
+obj-y += ultrasonic-distance/
diff --git a/drivers/iio/ultrasonic-distance/Kconfig
b/drivers/iio/ultrasonic-distance/Kconfig
new file mode 100644
index 000..9d442dd
--- /dev/null
+++ b/drivers/iio/ultrasonic-distance/Kconfig
@@ -0,0 +1,16 @@
+#
+# Ultrasonic range sensors
+#
+
+menu "Ultrasonic ranger devices"
+
+config HC_SR04
+ tristate "HC-SR04 ultrasonic distance sensor on GPIO"
+ depends on GPIOLIB && SYSFS
+ help
+ Say Y here if you want to support the HC-SR04 ultrasonic distance
+ sensor which is attached on two runtime-configurable GPIO pins.
+
+ To compile this driver as a module, choose M here: the
+ module will be called hc-sr04.
+endmenu
diff --git a/drivers/iio/ultrasonic-distance/Makefile
b/drivers/iio/ultrasonic-distance/Makefile
new file mode 100644
index 000..1f01d50c
--- /dev/null
+++ b/drivers/iio/ultrasonic-distance/Makefile
@@ -0,0 +1,6 @@
+#
+# Makefile for IIO proximity sensors
+#
+
+# When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_HC_SR04) += hc-sr04.o
diff --git a/drivers/iio/ultrasonic-distance/hc-sr04.c
b/drivers/iio/ultrasonic-distance/hc-sr04.c
new file mode 100644
index 000..e5af647
--- /dev/null
+++ b/drivers/iio/ultrasonic-distance/hc-sr04.c
@@ -0,0 +1,460 @@
+/*
+ * hc-sr04.c - Support for HC-SR04 ultrasonic range sensor
+ *
+ * Copyright (C) 2016 Johannes Thoma
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/* Precise measurements of time delta between sending a trigger signal
+ * to the HC-SR04 distance sensor and receiving the echo signal from
+ * the sensor back. This has to be precise in the usecs range. We
+ * use trigger interrupts to measure the signal, so no busy wait :)
+ *
+ * This supports an (in theory) unlimited number of HC-SR04 devices.
+ * It uses IIO software triggers to interface with userland.
+ *
+ * To configure a device do a
+ *
+ *mkdir /config/iio/triggers/hc-sr04/sensor0
+ *
+ * (you need to mount configfs to /config first)
+ *
+ * Then configure the ECHO and TRIG pins (this also accepts symbolic names
+ * configured in the device tree)
+ *
+ *echo 23 > /config/iio/triggers/hc-sr04/sensor0/trig_pin
+ *echo 24 > /config/iio/triggers/hc-sr04/sensor0/echo_pin
+ *
+ * Then you can measure distance with:
+ *
+ *cat /sys/devices/trigger0/measure
+ *
+ * (trigger0 is the device name as reported by
+ * /config/iio/triggers/hc-sr04/sensor0/dev_name
+ *
+ * To convert to cent