iio: position: rust: add initial AS5600 driver
RFC PATCH 1/1 iio: position: rust: add initial AS5600 driver
This RFC introduces an initial Rust implementation for the ams OSRAM AS5600 magnetic rotary position sensor. The current scope covers I2C communication, device probing, and strict parsing of the AGC (Automatic Gain Control) hardware status.
For context, a C-based driver was proposed by Frank Zago in 2021 (Link: https://lore.kernel.org/linux-iio/[email protected]/), but it was not merged. This fresh Rust implementation takes a stricter approach to probing by enforcing mandatory validation of the STATUS (0x0B) register. It gracefully aborts with -ENODEV if a valid diametrical magnet is not detected.
I have intentionally deferred exposing the IIO channels (in_angl_raw and scale) to avoid writing raw unsafe C FFI bindings. Full sysfs support will be added in a V2 once the safe Rust IIO abstractions land upstream.
Testing was performed on kernel v7.0.0-rc3 using a BeagleBone Black (AM335x) on i2c-2 (0x36) operating at 3.3V.
Testing logs verifying probe behavior and AGC logic via sysfs bind/unbind:
No magnet or magnetic field out of range:
$ echo 2-0036 > /sys/bus/i2c/drivers/as5600/bind [ 1928.138249] as5600 2-0036: AS5600: No magnet detected
(Probe aborted, safely returns -ENODEV)
Magnet pushed too far (weak field):
$ echo 2-0036 > /sys/bus/i2c/drivers/as5600/bind [ 1966.130616] as5600 2-0036: AS5600: Magnet too weak [ 1966.135628] as5600 2-0036: AS5600: Sensor probed, driver ready
Magnet placed too close (strong field):
$ echo 2-0036 > /sys/bus/i2c/drivers/as5600/bind [ 1988.541979] as5600 2-0036: AS5600: Magnet too strong [ 1988.547183] as5600 2-0036: AS5600: Sensor probed, driver ready
Optimal diametrical spot:
$ echo 2-0036 > /sys/bus/i2c/drivers/as5600/bind [ 1977.527490] as5600 2-0036: AS5600: Sensor probed, driver ready
--- drivers/iio/position/Kconfig | 13 +++++++ drivers/iio/position/Makefile | 1 + drivers/iio/position/as5600.rs | 64 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 drivers/iio/position/as5600.rs
drivers/iio/position/Kconfig +13 -0
@@ -6,6 +6,19 @@ menu "Linear and angular position sensors" +config AS5600 + tristate "ams AS5600 magnetic rotary position sensor" + depends on I2C && RUST + help + Say Y here to build support for the ams AS5600 12-bit + magnetic rotary position sensor. + + This driver provides hardware validation and I2C probing + in Rust for the AS5600. + + To compile this driver as a module, choose M here: the + module will be called as5600. + config IQS624_POS tristate "Azoteq IQS624/625 angular position sensors" depends on MFD_IQS62X || COMPILE_TEST
drivers/iio/position/Makefile +1 -0
@@ -4,5 +4,6 @@ # When adding new entries keep the list in alphabetical order +obj-$(CONFIG_AS5600) += as5600.o obj-$(CONFIG_HID_SENSOR_CUSTOM_INTEL_HINGE) += hid-sensor-custom-intel-hinge.o obj-$(CONFIG_IQS624_POS) += iqs624-pos.o
drivers/iio/position/as5600.rs new file +64 -0
64 lines not on this page · raw message
Signature · 1 line
2.50.0
Jonathan Cameron
maintainerQuoted 5 lines · On Sun, 19 Apr 2026 22:13:27 +0700 Muchamad Coirul Anwar…
This RFC introduces an initial Rust implementation for the ams OSRAM AS5600 magnetic rotary position sensor. The current scope covers I2C communication, device probing, and strict parsing of the AGC (Automatic Gain Control) hardware status.
For context, a C-based driver was proposed by Frank Zago in 2021 (Link: https://lore.kernel.org/linux-iio/[email protected]/), but it was not merged. This fresh Rust implementation takes a stricter approach to probing by enforcing mandatory validation of the STATUS (
0x0B) register. It gracefully aborts with-ENODEVif a valid diametrical magnet is not detected.I have intentionally deferred exposing the IIO channels (
in_angl_rawand scale) to avoid writing raw unsafe C FFI bindings. Full sysfs support will be added in a V2 once the safe Rust IIO abstractions land upstream.
Have those been posted anywhere? Or are you working on them? I've kind of been wondering if anyone was interested in rust bindings for IIO drivers (and dreaming of having the time to write them), so this suggests that you are at least.
In the mean time this stub of a driver isn't particularly interesting to review. My concern is the common one on whether we will have enough folk familiar with both rust and IIO to review drivers. Hopefully we will but my recent experience with trying to get some other rust code reviewed hasn't been that great (that one needed rust and crypto protocols).
A few really minor things inline..
Quoted 15 lines
Testing was performed on kernel v7.0.0-rc3 using a BeagleBone Black (AM335x) on i2c-2 (
0x36) operating at 3.3V.Testing logs verifying probe behavior and AGC logic via sysfs bind/unbind:
No magnet or magnetic field out of range:
$ echo 2-0036 > /sys/bus/i2c/drivers/as5600/bind [ 1928.138249] as5600 2-0036: AS5600: No magnet detected(Probe aborted, safely returns
-ENODEV)
Magnet pushed too far (weak field):$ echo 2-0036 > /sys/bus/i2c/drivers/as5600/bind [ 1966.130616] as5600 2-0036: AS5600: Magnet too weak [ 1966.135628] as5600 2-0036: AS5600: Sensor probed, driver ready
Quoted 8 lines
Magnet placed too close (strong field):
$ echo 2-0036 > /sys/bus/i2c/drivers/as5600/bind [ 1988.541979] as5600 2-0036: AS5600: Magnet too strong [ 1988.547183] as5600 2-0036: AS5600: Sensor probed, driver readyOptimal diametrical spot:
$ echo 2-0036 > /sys/bus/i2c/drivers/as5600/bind [ 1977.527490] as5600 2-0036: AS5600: Sensor probed, driver ready
Push testing logs into a cover letter or under the --- so there is no chance we end up with them in the git log where they are too verbose.
Quoted 55 lines
Signed-off-by Muchamad Coirul Anwar <[email protected]>--- drivers/iio/position/Kconfig | 13 +++++++ drivers/iio/position/Makefile | 1 + drivers/iio/position/as5600.rs | 64 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 drivers/iio/position/as5600.rsdrivers/iio/position/Kconfig +13
-0@@ -6,6 +6,19 @@ menu "Linear and angular position sensors" +config AS5600 + tristate "ams AS5600 magnetic rotary position sensor" + depends on I2C && RUST + help + Say Y here to build support for the ams AS5600 12-bit + magnetic rotary position sensor. + + This driver provides hardware validation and I2C probing + in Rust for the AS5600. + + To compile this driver as a module, choose M here: the + module will be called as5600. + config IQS624_POS tristate "Azoteq IQS624/625 angular position sensors" depends on MFD_IQS62X || COMPILE_TESTdrivers/iio/position/Makefile +1
-0@@ -4,5 +4,6 @@ # When adding new entries keep the list in alphabetical order +obj-$(CONFIG_AS5600) += as5600.o obj-$(CONFIG_HID_SENSOR_CUSTOM_INTEL_HINGE) += hid-sensor-custom-intel-hinge.o obj-$(CONFIG_IQS624_POS) += iqs624-pos.odrivers/iio/position/as5600.rs new file +5
-0@@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-only +// Copyright (C) 2026 Muchamad Coirul Anwar <[email protected]> +//! Driver for ams AS5600 12-bit magnetic rotary position sensor. +//! +//! Datasheet: https://ams.com/documents/20143/36005/AS5600_DS000365_5-00.pdf
drivers/iio/position/as5600.rs line 15
Quoted 10 lines
drivers/iio/position/as5600.rs new file +10
-0+ +use kernel::device::Core ; +use kernel::i2c; +use kernel::module_i2c_driver; +use kernel::prelude::*; + +// AS5600 register addresses (from datasheet v1.06) +const AS5600_REG_STATUS :u8 =0x0B ; +const AS5600_REG_RAW_ANGLE_H :u8 =0x0C ;// High nibble [11:8] in bits [3:0] +const AS5600_REG_RAW_ANGLE_L :u8 =0x0D ;// Low byte [7:0]
If the detail in the comments is useful, I'd suggest some consts for masks to extract the appropriate bits (from the _H register anyway)
I'd rather see a definition we can use than a comment telling us where things are.
drivers/iio/position/as5600.rs line 20
Quoted 5 lines
drivers/iio/position/as5600.rs new file +5
-0+ +// STATUS register bit masks +const AS5600_STATUS_MH :u8 =0x08 ;// Magnet too strong (AGC minimum gain overflow) +const AS5600_STATUS_ML :u8 =0x10 ;// Magnet too weak (AGC maximum gain overflow) +const AS5600_STATUS_MD :u8 =0x20 ;// Magnet detected
Do we have a rust equivalent of BIT()? That tends to be easier to compare with datasheets that don't use hex for this.
Quoted 20 lines
drivers/iio/position/as5600.rs new file +20
-0+ +module_i2c_driver! { +type :As5600 , + name:"as5600" , + authors: ["Muchamad Coirul Anwar" ], + description:"I2C Driver for ams OSRAM AS5600 Magnetic Rotary Position Sensor" , + license:"GPL" , +} + +kernel::i2c_device_table!( +I2C_TABLE , +MODULE_I2C_TABLE , + <As5600 as i2c::Driver >::IdInfo , + [(i2c::DeviceId ::new (c"as5600" ), ())] +); + +struct As5600 {} + +impl i2c::Driver for As5600 { +type IdInfo = ();
drivers/iio/position/as5600.rs line 50
Quoted 10 lines
drivers/iio/position/as5600.rs new file +10
-0+const I2C_ID_TABLE :Option <i2c::IdTable <Self ::IdInfo >> =Some (&I2C_TABLE ); + +fn probe ( + dev: &i2c::I2cClient <Core >, + _id_info:Option <&Self ::IdInfo >, + ) ->impl PinInit <Self ,Error > { +let status = dev.smbus_read_byte_data (AS5600_REG_STATUS )?; +if (status &AS5600_STATUS_MD ) ==0 { +dev_err! (dev.as_ref (),"AS5600: No magnet detected\n" ); +return Err (ENODEV );
ENODEV seems a bit too much of an indication the device is missing rather than the magnet. From my vague understanding of how these are used, the magnet may not always be present so perhaps failing on this at probe is too strong? If it does make sense to fail, then fail on the strong / weak as well if that means the outputs are useless.
drivers/iio/position/as5600.rs line 57
Quoted 7 lines
drivers/iio/position/as5600.rs new file +7
-0+ }else if (status &AS5600_STATUS_MH ) !=0 { +dev_warn! (dev.as_ref (),"AS5600: Magnet too strong\n" ); + }else if (status &AS5600_STATUS_ML ) !=0 { +dev_warn! (dev.as_ref (),"AS5600: Magnet too weak\n" ); + } + +dev_info! (dev.as_ref (),"AS5600: Sensor probed, driver ready\n" );
Too noisy. dev_dbg!() at most even when doing development (assuming that exists
- this is only the second bit of rust I've ever reviewed ;)
drivers/iio/position/as5600.rs line 62
Quoted 5 lines
drivers/iio/position/as5600.rs new file +5
-0+Ok ::<_ ,Error >(As5600 {}) + } + +fn unbind (dev: &i2c::I2cClient <Core >, _this:Pin <&Self >) { +dev_info! (dev.as_ref (),"AS5600 Sensor Driver Unbound\n" );
Definitely not, even in an RFC patch. These sorts of things can end up making it all the way to the upstream driver so dev_dbg!() only.
Quoted 2 lines
drivers/iio/position/as5600.rs new file +2
-0+ } +}
Quoted 3 lines · Jonathan Cameron
Do we have a rust equivalent of
BIT()? That tends to be easier to compare with datasheets that don't use hex for this.
We do! Please see:
https://rust.docs.kernel.org/kernel/bits/
Cheers,
Miguel
Hi Jonathan, Miguel,
Thank you both for taking the time to review this early RFC. I truly appreciate the architectural pointers and the lessons on kernel driver culture. It has been incredibly helpful.
Quoted 8 lines · On Mon, 20 Apr 2026 16:49:00 +0200 Miguel Ojeda…
Quoted 3 lines · On Mon, 20 Apr 2026 01:33:00 +0100 Jonathan Cameron <[email protected]> wrote:
Do we have a rust equivalent of
BIT()? That tends to be easier to compare with datasheets that don't use hex for this.We do! Please see:
Thanks for the pointer, Miguel. I will update the constants to use kernel::bits in the next iteration so they map cleanly to the datasheet.
Quoted 8 lines · On Mon, 20 Apr 2026 01:33:00 +0100 Jonathan Cameron <[email protected]> wrote:
Have those been posted anywhere? Or are you working on them? I've kind of been wondering if anyone was interested in rust bindings for IIO drivers (and dreaming of having the time to write them), so this suggests that you are at least.
In the mean time this stub of a driver isn't particularly interesting to review. My concern is the common one on whether we will have enough folk familiar with both rust and IIO to review drivers.
To my knowledge, there are no official Rust IIO abstractions posted yet. I initially planned to wait for them, but seeing the interest, I will take the initiative to draft a minimal safe wrapper for iio_dev and iio_info.
For v2, my goal is to implement just enough of the abstraction to support the read_raw callback, allowing the AS5600 driver to properly expose in_angl_raw and in_angl_scale. I will submit the IIO abstractions as the first patch in the v2 series so the core design can be reviewed independently.
Quoted 2 lines
Push testing logs into a cover letter or under the --- so there is no chance we end up with them in the git log where they are too verbose.
Noted. All verbose testing logs will be moved below the --- line or into a cover letter in v2 to keep the commit history clean.
Quoted 5 lines
ENODEV seems a bit too much of an indication the device is missing rather than the magnet. From my vague understanding of how these are used, the magnet may not always be present so perhaps failing on this at probe is too strong? If it does make sense to fail, then fail on the strong / weak as well if that means the outputs are useless.
This is a great architectural point. I missed the perspective that the physical magnet is an external factor, whereas the I2C chip is the actual device being probed. I will remove the -ENODEV abort from probe(). The driver will initialize successfully as long as the I2C communication is verified. The AGC status and magnet validation will be deferred to the read_raw callback instead.
Quoted 5 lines
Too noisy. dev_dbg!() at most even when doing development (assuming that exists
- this is only the second bit of rust I've ever reviewed ;)Definitely not, even in an RFC patch. These sorts of things can end up making it all the way to the upstream driver so dev_dbg!() only.
Understood. I will downgrade the dev_info! calls to dev_dbg! and entirely drop the noise inside the unbind function.
Thanks again for the guidance. I will start working on the IIO wrappers and the v2 patch series.
Best regards,
Muchamad Coirul Anwar
Pada Sel, 21 Apr 2026 pukul 16.49 Miguel Ojeda
<[email protected]> menulis:
Quoted 12 lines · Jonathan Cameron
Quoted 3 lines
Do we have a rust equivalent of
BIT()? That tends to be easier to compare with datasheets that don't use hex for this.We do! Please see:
https://rust.docs.kernel.org/kernel/bits/
Cheers,
Miguel
Quoted 4 lines · Muchamad Coirul Anwar
Thanks for the pointer, Miguel. I will update the constants to use
kernel::bitsin the next iteration so they map cleanly to the datasheet.
You're welcome! By the way, eventually I2C etc. may also want to consider a more convenient way of matching a datasheet / defining a register map, like we do with register! for MMIO and PCI config space:
https://rust.docs.kernel.org/next/kernel/io/register/
Cheers,
Miguel