Re: [RFC PATCH v6 0/4] iio: position: add Rust driver for ams AS5600

From: Muchamad Coirul Anwar

Date: Fri Nov 06 2026 - 10:54:23 EST


On Tue, 3 Nov 2026 at 16:53, Shuai Liu <847807867@xxxxxx> wrote:

> 1. clippy warnings
>
> `make LLVM=1 CLIPPY=1` reports four warnings on this series, which the
> Rust CI gates on. Three are `clippy::ptr_as_ptr` in the IIO abstraction
> and one is `clippy::useless_conversion` in the driver:
>
> --- a/rust/kernel/iio.rs
> +++ b/rust/kernel/iio.rs
> @@ -202,1 +202,1 @@
> - let priv_ptr = unsafe { (*indio_dev).priv_ as *mut T };
> + let priv_ptr = unsafe { (*indio_dev).priv_.cast::<T>() };
>
> (the same pattern at lines 334 and 397; line 334 is already inside
> an unsafe block and just needs `.cast::<T>()`)
>
> --- a/drivers/iio/position/as5600.rs
> +++ b/drivers/iio/position/as5600.rs
> @@ -133,1 +133,1 @@
> - Ok(IioVal::Int(angle as i32))
> + Ok(IioVal::Int(i32::from(angle)))

Hi Shuai

Thanks for catching the Clippy warnings. I reproduced and fixed them,
and the Clippy build now completes without warnings. I also replaced
the ignored, AS5600-specific example with a generic `no_run` example,
which is now compile-checked without being run.

> 2. ignored doctest in the SMBus word helper
>
> The example on `smbus_read_word_swapped()` (rust/kernel/i2c.rs:841) is
> marked "ignore" and references driver-specific facts (the AS5600).
> Ignored examples are not compiled or run by rusttest, so the helper
> currently ships with no tested usage. A generic, compilable example
> works here:
>
> --- a/rust/kernel/i2c.rs
> +++ b/rust/kernel/i2c.rs
> @@ -841,8 +841,15 @@
> /// ```
> -/// // AS5600 stores the 12-bit raw angle at command byte 0x0C with its
> -/// // high byte first, so the SMBus (low byte first) word result must
> -/// // be swapped.
> -/// let raw = client.smbus_read_word_swapped(0x0C)?;
> -/// # Ok::<(), Error>(())
> +/// use kernel::{i2c::I2cClient, prelude::*};
> +///
> +/// fn read_hi_first_register(client: &I2cClient<kernel::device::Bound>) -> Result<u16> {
> +/// // A device whose 16-bit register selected by command byte 0x0C
> +/// // transmits the high byte first needs the SMBus (low byte first)
> +/// // word swapped.
> +/// client.smbus_read_word_swapped(0x0C)
> +/// }
> /// ```
>
> 3. repeated unsafe blocks in the SMBus access helpers
>
> Patch 2 implements the `FallibleIoCapable` impls for `u8`/`u16` with
> five near-identical unsafe blocks wrapping `bindings::i2c_smbus_*`
> calls plus error conversion. Extracting four small safe helpers would
> put the safety argument in one place and turn each call site into a
> one-liner:
>
> fn smbus_read_byte(client: &I2cClient<device::Bound>, reg: u8)
> -> Result<u8>;
> fn smbus_write_byte(client: &I2cClient<device::Bound>, reg: u8, value: u8)
> -> Result;
> fn smbus_read_word_data(client: &I2cClient<device::Bound>, reg: u8)
> -> Result<u16>;
> fn smbus_write_word_data(client: &I2cClient<device::Bound>, reg: u8, value: u16)
> -> Result;
>
> For example, `io_try_write` for `u8` then reduces to
> `smbus_write_byte(client, reg, value)`. Happy to send the full diff if
> that is useful as a starting point.
>
> With 1 and 2 applied (and 3 sketched on top), `make LLVM=1 CLIPPY=1
> drivers/iio/position/as5600.o` builds with no warnings and the rusttest
> doctests pass.

I also followed your I2C refactoring suggestion and added the four safe
helpers as methods on `I2cClient<device::Bound>`.

Thanks for taking the time to review this. I'll Cc you when I send v7.

Thanks,
Coirul

> Thanks,
> Shuai Liu
>