iio: position: add Rust driver for ams AS5600
Subsystems (4)
v2 cover iio: position: add Rust driver for ams AS5600
This is v2 of the Rust driver for the ams AS5600 12-bit magnetic rotary position sensor. v1 was a probe-only stub; this version adds minimal Rust IIO abstractions and exposes in_angl_raw and in_angl_scale via sysfs.
Changes since RFC v1:
- Moved magnet validation from probe() to read_raw() per Jonathan's feedback. probe() now only verifies I2C communication.
- Added minimal Rust IIO abstractions (rust/kernel/iio.rs) wrapping iio_device_alloc, iio_device_register, and the read_raw callback.
- Added OF device table for devicetree matching (ams,as5600).
- Replaced hex bit masks with kernel::bits::bit_u8() per Miguel's pointer.
- Replaced magic numbers with bindings constants
(iio_chan_info_enum_*, INDIO_DIRECT_MODE).
- Downgraded log messages to dev_dbg!(), removed unbind noise.
- Added devicetree binding documentation.
- iio_info vtable is now a compile-time const.
- Uses iio_device_alloc with PinnedDrop instead of devm_* to avoid lifetime conflicts with Rust's ownership model.
The IIO abstraction design was informed by earlier unpublished work from Brandon Saint-John.
The IIO abstraction is intentionally minimal: it supports read_raw with IIO_VAL_INT, IIO_VAL_INT_PLUS_NANO, IIO_VAL_INT_PLUS_MICRO, and IIO_VAL_FRACTIONAL. write_raw and buffer support are left for future work.
Muchamad Coirul Anwar (4):
i2c: rust: add smbus_read_byte_data and smbus_read_word_data
rust: add minimal IIO subsystem abstractions
dt-bindings: iio: position: add ams,as5600
iio: position: add Rust driver for ams AS5600
---
Tested on BeagleBone Black (AM335x), kernel v7.0.0-rc3, AS5600 on i2c-2 (0x36) at 3.3V, diametric neodymium magnet.
$ cat /sys/bus/iio/devices/iio:device0/in_angl_raw 3296 $ cat /sys/bus/iio/devices/iio:device0/in_angl_raw 1675 $ cat /sys/bus/iio/devices/iio:device0/in_angl_raw 468
$ cat /sys/bus/iio/devices/iio:device0/in_angl_scale 0.001533981
Magnet removed:
$ cat /sys/bus/iio/devices/iio:device0/in_angl_raw cat: '/sys/bus/iio/devices/iio:device0/in_angl_raw': No data available
Muchamad Coirul Anwar (4):
i2c: rust: add smbus_read_byte_data and smbus_read_word_data
rust: add minimal IIO subsystem abstractions
dt-bindings: iio: position: add ams,as5600
iio: position: add Rust driver for ams AS5600
.../bindings/iio/position/ams,as5600.yaml | 40 ++++ drivers/iio/position/Kconfig | 14 ++ drivers/iio/position/Makefile | 1 + drivers/iio/position/as5600.rs | 129 +++++ rust/bindings/bindings_helper.h | 2 + rust/kernel/i2c.rs | 24 + rust/kernel/iio.rs | 224 ++++++++++ rust/kernel/lib.rs | 2 + 8 files changed, 436 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/position/ams,as5600.yaml create mode 100644 drivers/iio/position/as5600.rs create mode 100644 rust/kernel/iio.rs
Signature · 1 line
2.50.0
RFC PATCH v2 1/4 i2c: rust: add smbus_read_byte_data and smbus_read_word_data
--- rust/kernel/i2c.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
rust/kernel/i2c.rs +24 -0
@@ -477,6 +477,30 @@ impl<Ctx: device::DeviceContext> I2cClient<Ctx> {fn as_raw (&self) -> *mut bindings::i2c_client { self.0 .get () } + +/// Reads a single byte from a register via SMBus. +pub fn smbus_read_byte_data (&self, reg:u8 ) ->Result <u8 > { +// SAFETY: `self.as_raw()` is a valid pointer to a `struct i2c_client` +// by the type invariant of `I2cClient`. +let ret =unsafe { bindings::i2c_smbus_read_byte_data (self.as_raw (), reg) }; +if ret <0 { +Err (Error ::from_errno (ret)) + }else { +Ok (retas u8 ) + } + } + +/// Reads a 16-bit word from a register via SMBus. +pub fn smbus_read_word_data (&self, reg:u8 ) ->Result <u16 > { +// SAFETY: `self.as_raw()` is a valid pointer to a `struct i2c_client` +// by the type invariant of `I2cClient`. +let ret =unsafe { bindings::i2c_smbus_read_word_data (self.as_raw (), reg) }; +if ret <0 { +Err (Error ::from_errno (ret)) + }else { +Ok (retas u16 ) + } + } }// SAFETY: `I2cClient` is a transparent wrapper of `struct i2c_client`.
Signature · 1 line
2.50.0
Igor Korotin
maintainerRe: [RFC PATCH v2 1/4] i2c: rust: add smbus_read_byte_data and smbus_read_word_data
Hello Muchamad
Quoted 30 lines · Muchamad Coirul Anwar
Signed-off-by Muchamad Coirul Anwar <[email protected]>--- rust/kernel/i2c.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)rust/kernel/i2c.rs +17
-0@@ -477,6 +477,30 @@ impl<Ctx: device::DeviceContext> I2cClient<Ctx> {fn as_raw (&self) -> *mut bindings::i2c_client { self.0 .get () } + +/// Reads a single byte from a register via SMBus. +pub fn smbus_read_byte_data (&self, reg:u8 ) ->Result <u8 > { +// SAFETY: `self.as_raw()` is a valid pointer to a `struct i2c_client` +// by the type invariant of `I2cClient`. +let ret =unsafe { bindings::i2c_smbus_read_byte_data (self.as_raw (), reg) }; +if ret <0 { +Err (Error ::from_errno (ret)) + }else { +Ok (retas u8 ) + } + } + +/// Reads a 16-bit word from a register via SMBus. +pub fn smbus_read_word_data (&self, reg:u8 ) ->Result <u16 > { +// SAFETY: `self.as_raw()` is a valid pointer to a `struct i2c_client` +// by the type invariant of `I2cClient`.
rust/kernel/i2c.rs impl<Ctx: device::DeviceContext> I2cClient<Ctx> { · line 506
Quoted 10 lines
rust/kernel/i2c.rs +7
-0+let ret =unsafe { bindings::i2c_smbus_read_word_data (self.as_raw (), reg) }; +if ret <0 { +Err (Error ::from_errno (ret)) + }else { +Ok (retas u16 ) + } + } }// SAFETY: `I2cClient` is a transparent wrapper of `struct i2c_client`.
Thanks for the patch. However, we've previously agreed [1] that I2cClient should implement the IO trait [2] rather than adding standalone methods like these. This patch would need to be reworked in that direction.
If you'd like to take that on, feel free to submit a patch series implementing the IO trait instead.
Cheers
Igor
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
Re: [RFC PATCH v2 1/4] i2c: rust: add smbus_read_byte_data and smbus_read_word_data
Quoted 8 lines · Igor Korotin
Hello Muchamad
Quoted 4 lines · Muchamad Coirul Anwar
Signed-off-by Muchamad Coirul Anwar <[email protected]>--- rust/kernel/i2c.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
[snip]
Quoted 6 lines
Thanks for the patch. However, we've previously agreed [1] that I2cClient should implement the IO trait [2] rather than adding standalone methods like these. This patch would need to be reworked in that direction.
Quoted 8 lines
If you'd like to take that on, feel free to submit a patch series implementing the IO trait instead.
Cheers
Igor[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git/commit/?h=driver-core-testing&id=121d87b28e1d9061d3aaa156c43a627d3cb5e620
Hi Igor,
Thanks for the review. It makes sense that having the AS5600 as the first consumer of the Io trait would be cleaner than using standalone methods. I'll rework this accordingly.
Quick question on logistics: would you prefer the Io trait patches as a separate series, or bundled as prerequisites in my v3 for the AS5600 driver?
Regards,
M. Coirul A.
Re: [RFC PATCH v2 1/4] i2c: rust: add smbus_read_byte_data and smbus_read_word_data
Quoted 3 lines · Muchamad Coirul Anwar
Quick question on logistics: would you prefer the Io trait patches as a separate series, or bundled as prerequisites in my v3 for the AS5600 driver?
Bundled patch is pretty fine by me
Thanks
Igor
RFC PATCH v2 2/4 rust: add minimal IIO subsystem abstractions
In reply to Muchamad Coirul Anwar--- rust/bindings/bindings_helper.h | 2 + rust/kernel/iio.rs | 224 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 3 files changed, 228 insertions(+) create mode 100644 rust/kernel/iio.rs
rust/bindings/bindings_helper.h +2 -0
@@ -58,6 +58,8 @@#include <linux/firmware.h> #include <linux/fs.h> #include <linux/i2c.h> +#include <linux/iio/iio.h> +#include <linux/iio/types.h> #include <linux/interrupt.h> #include <linux/io-pgtable.h> #include <linux/ioport.h>
rust/kernel/iio.rs new file +224 -0
224 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -99,6 +99,8 @@#[cfg( CONFIG_I2C = "y" )] pub mod i2c;pub mod id_pool; +#[cfg( CONFIG_IIO )] +pub mod iio;#[doc(hidden)] pub mod impl_flags;pub mod init;
Signature · 1 line
2.50.0
RFC PATCH v2 3/4 dt-bindings: iio: position: add ams,as5600
In reply to Muchamad Coirul Anwar--- .../bindings/iio/position/ams,as5600.yaml | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/position/ams,as5600.yaml
Documentation/devicetree/bindings/iio/position/ams,as5600.yaml new file +40 -0
@@ -0,0 +1,40 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/position/ams,as5600.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ams AS5600 Magnetic Rotary Position Sensor + +maintainers: + - Muchamad Coirul Anwar <[email protected]> + +description: | + The ams AS5600 is a 12-bit programmable contactless + magnetic rotary position sensor communicating over I2C. + The device address is fixed at 0x36. + +properties: + compatible: + const: ams,as5600 + + reg: + const: 0x36 + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + position-sensor@36 { + compatible = "ams,as5600"; + reg = <0x36>; + }; + };
Signature · 1 line
2.50.0
Re: [RFC PATCH v2 3/4] dt-bindings: iio: position: add ams,as5600
Quoted 1 line · Muchamad Coirul Anwar
Signed-off-by Muchamad Coirul Anwar <[email protected]>
I guess this is still not ready and correctly marked as RFC, so I will drop it from DT patchwork.
Best regards,
Krzysztof
RFC PATCH v2 4/4 iio: position: add Rust driver for ams AS5600
In reply to Muchamad Coirul Anwar--- drivers/iio/position/Kconfig | 14 ++++ drivers/iio/position/Makefile | 1 + drivers/iio/position/as5600.rs | 129 +++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 drivers/iio/position/as5600.rs
drivers/iio/position/Kconfig +14 -0
@@ -6,6 +6,20 @@ 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 with IIO channel support + (in_angl_raw and in_angl_scale). + + This is a Rust driver that exposes the 12-bit raw angle + and radian scale via the IIO subsystem. + + 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 +129 -0
129 lines not on this page · raw message
Signature · 1 line
2.50.0