Skip to contentSkip to conversation

iio: proximity: Add Cytron HC-SR04 support

RFC PATCH In review Paweł Wiśniewski 4 participants 9 messages Jul 16 – Jul 18

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover iio: proximity: Add Cytron HC-SR04 support

Add Device Tree binding and driver match support for the Cytron HC-SR04 ultrasonic distance sensor.

The HC-SR04 uses separate trigger and echo GPIOs. A trigger pulse of at least 10 us starts a measurement, and the echo pulse width reports the ultrasonic round-trip time. This matches the protocol already implemented by the SRF04 IIO driver.

The series adds the Cytron vendor prefix, documents the cytron,hc-sr04 compatible, and uses the existing SRF04 configuration in the driver.

The Cytron vendor attribution is based on the HC-SR04 Product User's Manual V1.0 supplied by the retailer. The manual was prepared by Cytron Technologies Sdn. Bhd. and contains Cytron warranty and support information.

This is sent as an RFC because the vendor attribution and compatible name should be reviewed.

Testing:
- make dt_binding_check: PASS
- srf04 module build: PASS
- Tested on Raspberry Pi 3 Model B+
- Kernel: 6.18.34+rpt-rpi-v8
- Device instantiated using only cytron,hc-sr04
- Trigger connected to GPIO17
- Echo connected to GPIO27 through a 5 V to 3.3 V resistor divider
- Driver bound and registered an IIO device
- in_distance_raw returned changing distance readings

Paweł Wiśniewski (3):
dt-bindings: vendor-prefixes: Add Cytron Technologies
dt-bindings: iio: proximity: Add Cytron HC-SR04
iio: proximity: srf04: Add Cytron HC-SR04 support

 .../devicetree/bindings/iio/proximity/devantech-srf04.yaml   | 5 ++++-
 Documentation/devicetree/bindings/vendor-prefixes.yaml       | 2 ++
 drivers/iio/proximity/Kconfig                                | 1 +
 drivers/iio/proximity/srf04.c                                | 1 +
 4 files changed, 8 insertions(+), 1 deletion(-)

base-commit: 37e2f878a7a660a216cc7a60459995fefd150f25

Signature · 1 line
2.47.3
#1 lore raw

RFC PATCH 1/3 dt-bindings: vendor-prefixes: Add Cytron Technologies

Add a vendor prefix for Cytron Technologies Sdn. Bhd.

The prefix is used by the HC-SR04 ultrasonic ranging sensor binding.

Assisted-by ChatGPT:GPT-5.6 Thinking
---
 Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index 396044f368e7..cd4674569133 100644
@@ -397,6 +397,8 @@ patternProperties:
     description: CUI Devices
   "^cypress,.*":
     description: Cypress Semiconductor Corporation
+  "^cytron,.*":
+    description: Cytron Technologies Sdn. Bhd.
   "^cyx,.*":
     description: Shenzhen CYX Industrial Co., Ltd
   "^cznic,.*":
Signature · 1 line
2.47.3
#2 lore raw

RFC PATCH 2/3 dt-bindings: iio: proximity: Add Cytron HC-SR04

In reply to Paweł Wiśniewski

The Cytron HC-SR04 starts a ranging cycle with an active-high trigger pulse of at least 10 us and reports the acoustic round-trip time as the width of an active-high echo pulse.

Add the cytron,hc-sr04 compatible and a reference to the product manual. Update the trigger GPIO description to account for the pulse durations used by the supported devices.

The interface matches the two-GPIO protocol implemented by the existing SRF04 driver.

Assisted-by ChatGPT:GPT-5.6 Thinking
---
 .../devicetree/bindings/iio/proximity/devantech-srf04.yaml   | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/proximity/devantech-srf04.yaml +4 -1
index ce795279839e..f4237ae612ab 100644
@@ -4,7 +4,7 @@
 $id: http://devicetree.org/schemas/iio/proximity/devantech-srf04.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: Devantech SRF04 and Maxbotix mb1000 ultrasonic range finder
+title: Devantech SRF04 and Maxbotix mb1000 and Cytron HC-SR04 ultrasonic range finder
 
 maintainers:
   - Andreas Klinger <[email protected]>
@@ -21,9 +21,12 @@ description: |
 
   https://www.maxbotix.com/documents/LV-MaxSonar-EZ_Datasheet.pdf
 
+  https://botland.com.pl/index.php?controller=attachment&id_attachment=476
+
 properties:
   compatible:
     enum:
+      - cytron,hc-sr04
       - devantech,srf04
       - maxbotix,mb1000
       - maxbotix,mb1010
Signature · 1 line
2.47.3
#3 lore raw

Re: [RFC PATCH 2/3] dt-bindings: iio: proximity: Add Cytron HC-SR04

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit message claims to update the trigger GPIO description, but the patch does not…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The commit message claims to update the trigger GPIO description, but the patch does not actually modify it. --

--- Patch [2]: [RFC PATCH 2/3] dt-bindings: iio: proximity: Add Cytron HC-SR04 --- commit 319a9ebb7272df447013bc727d4e781b829dfcb0
Author: Paweł Wiśniewski <[email protected]>

dt-bindings: iio: proximity: Add Cytron HC-SR04

This commit documents the Cytron HC-SR04 ultrasonic range finder by adding the cytron,hc-sr04 compatible string and a reference link to the existing devantech-srf04 binding.

Quoted 3 lines

Add the cytron,hc-sr04 compatible and a reference to the product manual. Update the trigger GPIO description to account for the pulse durations used by the supported devices.

[Severity: Medium]
The commit message mentions an update to the trigger GPIO description to account for pulse durations used by the supported devices, but the patch does not appear to modify the trig-gpios property in the Documentation/devicetree/bindings/iio/proximity/devantech-srf04.yaml file. Was this documentation change accidentally omitted from the diff?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [RFC PATCH 2/3] dt-bindings: iio: proximity: Add Cytron HC-SR04

In reply to Paweł Wiśniewski
Quoted 10 lines · On Thu, 16 Jul 2026 19:35:55 +0200 Paweł Wiśniewski <[email protected]>…

The Cytron HC-SR04 starts a ranging cycle with an active-high trigger pulse of at least 10 us and reports the acoustic round-trip time as the width of an active-high echo pulse.

Add the cytron,hc-sr04 compatible and a reference to the product manual. Update the trigger GPIO description to account for the pulse durations used by the supported devices.

The interface matches the two-GPIO protocol implemented by the existing SRF04 driver.

Is it suited to using a fallback compatible to one of those already supported parts? This should say where things are different.

Quoted 12 lines
Assisted-by ChatGPT:GPT-5.6 Thinking
---
 .../devicetree/bindings/iio/proximity/devantech-srf04.yaml   | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/proximity/devantech-srf04.yaml +0 -0
index ce795279839e..f4237ae612ab 100644

Documentation/devicetree/bindings/iio/proximity/devantech-srf04.yaml line 7

Quoted 5 lines
Documentation/devicetree/bindings/iio/proximity/devantech-srf04.yaml +1 -1
@@ -4,7 +4,7 @@
 $id: http://devicetree.org/schemas/iio/proximity/devantech-srf04.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: Devantech SRF04 and Maxbotix mb1000 ultrasonic range finder
+title: Devantech SRF04 and Maxbotix mb1000 and Cytron HC-SR04 ultrasonic range finder

If we were going to keep the list going it would need to be:
title: Devantech SRF04, Maxbotix mb1000 and Cytron HC-SR04 ultrasonic range finder

However, previous experience suggests that sooner or later it is better to just jump to Devantech SRF04 and similar.

Quoted 16 lines
Documentation/devicetree/bindings/iio/proximity/devantech-srf04.yaml +3 -0
 maintainers:
   - Andreas Klinger <[email protected]>
@@ -21,9 +21,12 @@ description: |
 
   https://www.maxbotix.com/documents/LV-MaxSonar-EZ_Datasheet.pdf
 
+  https://botland.com.pl/index.php?controller=attachment&id_attachment=476
+
 properties:
   compatible:
     enum:
+      - cytron,hc-sr04
       - devantech,srf04
       - maxbotix,mb1000
       - maxbotix,mb1010
#5 lore raw

RFC PATCH 3/3 iio: proximity: srf04: Add Cytron HC-SR04 support

In reply to Paweł Wiśniewski

Tested on a Raspberry Pi 3 Model B+ running 6.18.34+rpt-rpi-v8. The sensor was instantiated using only the cytron,hc-sr04 compatible, with trigger connected to GPIO17 and echo connected to GPIO27 through a 5 V to 3.3 V resistor divider. The driver bound to the device, registered an IIO device, and returned changing distance readings through in_distance_raw.

Assisted-by ChatGPT:GPT-5.6 Thinking
---
 drivers/iio/proximity/Kconfig | 1 +
 drivers/iio/proximity/srf04.c | 1 +
 2 files changed, 2 insertions(+)
drivers/iio/proximity/Kconfig +1 -0
index bb77fad2a1b3..89e9d57ceba6 100644
@@ -139,6 +139,7 @@ config SRF04
 	  ranger sensor. This driver can be used to measure the distance
 	  of objects. It is using two GPIOs.
 	  Actually Supported types are:
+	  - Cytron HC-SR04
 	  - Devantech SRF04
 	  - Maxbotix mb1000
 	  - Maxbotix mb1010
drivers/iio/proximity/srf04.c +1 -0
index 7be50bdebfcb..f5ee7d6572b5 100644
@@ -231,6 +231,7 @@ static const struct iio_chan_spec srf04_chan_spec[] = {
 };
 
 static const struct of_device_id of_srf04_match[] = {
+	{ .compatible = "cytron,hc-sr04", .data = &srf04_cfg },
 	{ .compatible = "devantech,srf04", .data = &srf04_cfg },
 	{ .compatible = "maxbotix,mb1000", .data = &mb_lv_cfg },
 	{ .compatible = "maxbotix,mb1010", .data = &mb_lv_cfg },
Signature · 1 line
2.47.3
#6 lore raw

Re: [RFC PATCH 3/3] iio: proximity: srf04: Add Cytron HC-SR04 support

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Unchecked return value from device_get_match_data() can cause a NULL pointer dereference.
- [High] Interrupt handler may miss the rising edge of the echo pulse due to interrupt latency. --

--- Patch [3]: [RFC PATCH 3/3] iio: proximity: srf04: Add Cytron HC-SR04 support --- commit e27499a683a294e308b2d89b6fa06d4fd87a1cef
Author: Paweł Wiśniewski <[email protected]>

iio: proximity: srf04: Add Cytron HC-SR04 support

This commit adds support for the Cytron HC-SR04 sensor to the srf04 driver. It introduces the new compatible string "cytron,hc-sr04" and updates the Kconfig to list the supported device.

Quoted 4 lines
drivers/iio/proximity/srf04.c +0 -0
index 7be50bdebfcbc..f5ee7d6572b5b 100644

[ ... ]

drivers/iio/proximity/srf04.c static const struct iio_chan_spec srf04_chan_spec[] = { · line 234

Quoted 5 lines
drivers/iio/proximity/srf04.c +1 -0
@@ -231,6 +231,7 @@ static const struct iio_chan_spec srf04_chan_spec[] = {
 };
 
 static const struct of_device_id of_srf04_match[] = {
+	{ .compatible = "cytron,hc-sr04", .data = &srf04_cfg },

[Severity: High]
This is a pre-existing issue, and was not introduced by the patch under review, but could the unchecked return value from device_get_match_data() cause a NULL pointer dereference?

In drivers/iio/proximity/srf04.c:srf04_probe(), the return value is assigned without validation:

    data->cfg = device_get_match_data(dev);

If the driver is instantiated via legacy platform device registration, device_get_match_data() will return NULL. Later, in srf04_read(), this could be dereferenced:

    udelay(data->cfg->trigger_pulse_us);

Can this result in a kernel panic when a user reads from the IIO device?

[Severity: High]
This is a pre-existing issue, and was not introduced by the patch under review, but could the interrupt handler miss the rising edge of the echo pulse due to interrupt latency?

In drivers/iio/proximity/srf04.c:srf04_handle_irq(), the handler relies on the live GPIO state:

    if (gpiod_get_value(data->gpiod_echo)) {

If an object is close, the echo pulse is very short. If the system's interrupt latency exceeds this duration, the GPIO will already have transitioned back to low by the time the handler executes for the rising edge. The handler will read low, complete the falling completion, and ignore the rising edge.

This would cause the read operation to time out and fail in srf04_read():

    ret = wait_for_completion_killable_timeout(&data->rising, HZ/50);

Could this cause the sensor to consistently fail to measure short distances and return -ETIMEDOUT to userspace?

Quoted 2 lines
drivers/iio/proximity/srf04.c +0 -0
 	{ .compatible = "devantech,srf04", .data = &srf04_cfg },
 	{ .compatible = "maxbotix,mb1000", .data = &mb_lv_cfg },
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

Re: [RFC PATCH 3/3] iio: proximity: srf04: Add Cytron HC-SR04 support

In reply to Paweł Wiśniewski
Quoted 9 lines · On Thu, 16 Jul 2026 19:35:56 +0200 Paweł Wiśniewski <[email protected]>…

Tested on a Raspberry Pi 3 Model B+ running 6.18.34+rpt-rpi-v8. The sensor was instantiated using only the cytron,hc-sr04 compatible, with trigger connected to GPIO17 and echo connected to GPIO27 through a 5 V to 3.3 V resistor divider. The driver bound to the device, registered an IIO device, and returned changing distance readings through in_distance_raw.

Assisted-by ChatGPT:GPT-5.6 Thinking

This suggests that it is very much suited to a fallback compatible to the Devantech SRF04 (see comment I made on the binding).

With that in place, we probably don't need a driver change.

Thanks,

Jonathan

Quoted 29 lines
---
 drivers/iio/proximity/Kconfig | 1 +
 drivers/iio/proximity/srf04.c | 1 +
 2 files changed, 2 insertions(+)
drivers/iio/proximity/Kconfig +1 -0
index bb77fad2a1b3..89e9d57ceba6 100644
@@ -139,6 +139,7 @@ config SRF04
 	  ranger sensor. This driver can be used to measure the distance
 	  of objects. It is using two GPIOs.
 	  Actually Supported types are:
+	  - Cytron HC-SR04
 	  - Devantech SRF04
 	  - Maxbotix mb1000
 	  - Maxbotix mb1010
drivers/iio/proximity/srf04.c +1 -0
index 7be50bdebfcb..f5ee7d6572b5 100644
@@ -231,6 +231,7 @@ static const struct iio_chan_spec srf04_chan_spec[] = {
 };
 
 static const struct of_device_id of_srf04_match[] = {
+	{ .compatible = "cytron,hc-sr04", .data = &srf04_cfg },
 	{ .compatible = "devantech,srf04", .data = &srf04_cfg },
 	{ .compatible = "maxbotix,mb1000", .data = &mb_lv_cfg },
 	{ .compatible = "maxbotix,mb1010", .data = &mb_lv_cfg },
#8 lore raw
In reply to Paweł Wiśniewski
Quoted 12 lines · Paweł Wiśniewski

Add Device Tree binding and driver match support for the Cytron HC-SR04 ultrasonic distance sensor.

The HC-SR04 uses separate trigger and echo GPIOs. A trigger pulse of at least 10 us starts a measurement, and the echo pulse width reports the ultrasonic round-trip time. This matches the protocol already implemented by the SRF04 IIO driver.

The series adds the Cytron vendor prefix, documents the cytron,hc-sr04 compatible, and uses the existing SRF04 configuration in the driver.

Quoted 7 lines

The Cytron vendor attribution is based on the HC-SR04 Product User's Manual V1.0 supplied by the retailer. The manual was prepared by Cytron Technologies Sdn. Bhd. and contains Cytron warranty and support information.

This is sent as an RFC because the vendor attribution and compatible name should be reviewed.

But it's normal process, no? In any case the code in the driver is fine.

Signature · 2 lines
With Best Regards,
Andy Shevchenko