[RFC PATCH v2 1/4] i2c: rust: add smbus_read_byte_data and smbus_read_word_data

Muchamad Coirul Anwar
2026-04-29T13:22:54Z

rust/kernel/i2c.rs | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index 7b908f0c5a58..6eaea1158fda 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -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(ret as 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(ret as u16)
+ }
+ }
}

// SAFETY: `I2cClient` is a transparent wrapper of `struct i2c_client`.
--
2.50.0