Linux LED subsystem development
 help / color / mirror / Atom feed
From: Markus Probst <markus.probst@posteo.de>
To: Lee Jones <lee@kernel.org>, Pavel Machek <pavel@kernel.org>,
	Danilo Krummrich <dakr@kernel.org>,
	Miguel Ojeda <ojeda@kernel.org>,
	Alex Gaynor <alex.gaynor@gmail.com>,
	Igor Korotin <igor.korotin.linux@gmail.com>
Cc: Markus Probst <markus.probst@posteo.de>,
	Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	"Liam R. Howlett" <Liam.Howlett@oracle.com>,
	Uladzislau Rezki <urezki@gmail.com>,
	Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>,
	bjorn3_gh@protonmail.com, Benno Lossin <lossin@kernel.org>,
	Andreas Hindborg <a.hindborg@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>,
	Trevor Gross <tmgross@umich.edu>,
	Daniel Almeida <daniel.almeida@collabora.com>,
	linux-leds@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 1/4] rust: i2c: add read and write byte data abstractions
Date: Wed, 08 Oct 2025 18:10:43 +0000	[thread overview]
Message-ID: <20251008181027.662616-2-markus.probst@posteo.de> (raw)
In-Reply-To: <20251008181027.662616-1-markus.probst@posteo.de>

In addition to the core abstractions, implement

* `i2c::I2cClient::read_byte` - read a byte from the i2c client
* `i2c::I2cClient::write_byte` - write a byte to the i2c client

* `i2c::I2cClient::read_byte_data` - read byte data from the i2c client
* `i2c::I2cClient::write_byte_data` - write byte data to the i2c client

* `i2c::I2cClient::read_word_data` - read word data from the i2c client
* `i2c::I2cClient::write_word_data` - write word data to the i2c client

Signed-off-by: Markus Probst <markus.probst@posteo.de>
---
 rust/kernel/i2c.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index 73858aecc131..57b02b65f90f 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -463,6 +463,51 @@ impl<Ctx: device::DeviceContext> I2cClient<Ctx> {
     fn as_raw(&self) -> *mut bindings::i2c_client {
         self.0.get()
     }
+
+    /// Reads a byte from the i2c client
+    pub fn read_byte(&self) -> Result<u8> {
+        // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
+        // `struct i2c_client`.
+        let value = unsafe { bindings::i2c_smbus_read_byte(self.as_raw()) };
+        to_result(value).map(|()| value as u8)
+    }
+
+    /// Writes a byte to the i2c client
+    pub fn write_byte(&self, value: u8) -> Result<()> {
+        // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
+        // `struct i2c_client`.
+        to_result(unsafe { bindings::i2c_smbus_write_byte(self.as_raw(), value) })
+    }
+
+    /// Reads byte data from the i2c client
+    pub fn read_byte_data(&self, reg: u8) -> Result<u8> {
+        // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
+        // `struct i2c_client`.
+        let value = unsafe { bindings::i2c_smbus_read_byte_data(self.as_raw(), reg) };
+        to_result(value).map(|()| value as u8)
+    }
+
+    /// Writes byte data to the i2c client
+    pub fn write_byte_data(&self, reg: u8, value: u8) -> Result<()> {
+        // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
+        // `struct i2c_client`.
+        to_result(unsafe { bindings::i2c_smbus_write_byte_data(self.as_raw(), reg, value) })
+    }
+
+    /// Reads word data from the i2c client
+    pub fn read_word_data(&self, reg: u8) -> Result<u16> {
+        // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
+        // `struct i2c_client`.
+        let value = unsafe { bindings::i2c_smbus_read_word_data(self.as_raw(), reg) };
+        to_result(value).map(|()| value as u16)
+    }
+
+    /// Writes word data to the i2c client
+    pub fn write_word_data(&self, reg: u8, value: u16) -> Result<()> {
+        // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
+        // `struct i2c_client`.
+        to_result(unsafe { bindings::i2c_smbus_write_word_data(self.as_raw(), reg, value) })
+    }
 }
 
 // SAFETY: `I2cClient` is a transparent wrapper of a type that doesn't depend on `I2cClient`'s generic
-- 
2.49.1


  reply	other threads:[~2025-10-08 18:10 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-08 18:10 [PATCH 0/4] Add first led driver written in Rust Markus Probst
2025-10-08 18:10 ` Markus Probst [this message]
2025-10-08 18:10   ` [PATCH 2/4] rust: add pinned wrapper of Vec Markus Probst
2025-10-08 18:10     ` [PATCH 3/4] rust: leds: add basic led classdev abstractions Markus Probst
2025-10-08 18:10       ` [PATCH 4/4] leds: add driver for synology atmega1608 controlled LEDs Markus Probst
2025-10-09 12:20         ` Danilo Krummrich
2025-10-09 12:30           ` Markus Probst
2025-10-09 12:41             ` Danilo Krummrich
2025-10-09 13:11               ` Alexandre Courbot
2025-11-23 20:00         ` Pavel Machek
2025-11-23 22:45           ` Markus Probst
2025-10-09 11:36       ` [PATCH 3/4] rust: leds: add basic led classdev abstractions Danilo Krummrich
2025-11-23 19:47       ` Pavel Machek
2025-10-09 11:23     ` [PATCH 2/4] rust: add pinned wrapper of Vec Alice Ryhl
2025-10-09 12:06       ` Markus Probst
2025-10-09 11:29   ` [PATCH 1/4] rust: i2c: add read and write byte data abstractions Alice Ryhl
2025-10-09 12:08   ` Danilo Krummrich
2025-11-23 19:44   ` Pavel Machek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251008181027.662616-2-markus.probst@posteo.de \
    --to=markus.probst@posteo.de \
    --cc=Liam.Howlett@oracle.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=igor.korotin.linux@gmail.com \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=pavel@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=urezki@gmail.com \
    --cc=vbabka@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox