From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE51430AD1F for ; Mon, 10 Nov 2025 11:31:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762774282; cv=none; b=cv8kJjJwLMN6rjjE9fVFr0u3Ngfr6AGa+3o1q8Afu80yTElEg4NO0MAIoISBam+UCCcNS/Ok4vWJHleBI7M10I0Hv37Ea7kb2sxb5w7Lv7E0T3fwpz/KOQyNfRDRAAff9B373nlYeCbqMErzdbOCswFM8OzARv6zfT5JJre2kYA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762774282; c=relaxed/simple; bh=QDgyZn87hGLt4zqNVLGdGA9KZtHVZaERp/iNSIowMsA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s6l0Ytpc518tMu0xJ1SgN05whNu2vzO+Prj3QL//pXmP1dFNNOv2AhP3O4E0gove9t6nPcuT2ZGvZCSskjySCgW0Yl+ezdGo8y9+ExJ2KJfokdacjEI+qa5HgptzYA4EJEi0px6jNJ39mt4vzzK2WPdSZtNciZ2CRlfzK6Hwwaw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=msWDt2hS; arc=none smtp.client-ip=209.85.221.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="msWDt2hS" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-429c4c65485so2055954f8f.0 for ; Mon, 10 Nov 2025 03:31:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762774279; x=1763379079; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+B5LEwdFrUWa2nsD2z+5oWtZAFge/Sr1BBSlvPtJNts=; b=msWDt2hSRTzVdXLtqk4AurIvHeiEYrhsxft0i3HOokAE7sMP9QxgaPAx9WeEEYmc7V rr6j6cWdUvGBNl2JKtqSRhjFAOq7ZkltG27+FRUIMDtN9LshbBNztbM27JMK5y08EGeT WbfmuAPhxvp0blPm2BT60VWGEVNBtUpjYct4Qz2K+pZDxWnGiRN2Ht4TbgOqI3c3g9Rm biC7I94Ie8JaqMciODVnPHsVUJdqu6bJAy5epsARgNeg7q9rBkzPhIGPZbPTZEKSNYdk 5aXa0zM49ibAP02E+8jTb+ve1W+zyEx3ilcgIvJ9hn4pbAGORUFTPAIaBwz/+eW0HdUa g6Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762774279; x=1763379079; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=+B5LEwdFrUWa2nsD2z+5oWtZAFge/Sr1BBSlvPtJNts=; b=j5nOZ0OmpsC0MMzLb4QwqblDSX//ITGiw9rk3p0uZcnGTc2X90v1gSxbXDHrk6aY2p spKZJbqqjHXkbmeB3YfiQ3qIm2hC36JbsRJ9LEPUX0Ze+zmccbb6G0IaVK00mE9sJ6ou 1AQvhYQg88H5TFxXu70RNpLcTJ6S+SXXN1uboWsioiCbiJO72NKqDyvjnLPP3Wr3YzQa vvPJkt+XTPw+ICkpY5BbMEWNXNRyowdX/TJd6UbVwtLRVVb0odh8AqE1KuCG8yr8fqQY jXZVQtgYxX4RCp9vG7pC0wBxaWiJsapawMZSAxSYkwt3yIsrD6fp+00Bot+GDOh7g7A1 bzuA== X-Forwarded-Encrypted: i=1; AJvYcCVeX6KoDMK5Fqq1KBOrK4GsPDEzrXbx1eo41uHYW75DD34WDaE+i9nKS274Prb0wXzMqPFKtNUG5FGmhM02Xg==@vger.kernel.org X-Gm-Message-State: AOJu0YyYZwlwlfd0hHTbY6zCR33TerSrxuJGUEMSf7j/F6pFZFjr4FYo hCni1lHTFISqdspbA30ohhhZC0wS7RB3d+xp439lI1U/q8+eQ1tHsgD6 X-Gm-Gg: ASbGnctONJ9dMv75bJyfXPemGr+t19AOkARiL33HeXSAm//+xY9EdllyyZHUph06+wh 4C8rRBs3BvkLxT0qsjzkpHCiJQBkIBC8h61qlWEfZuV9D/MK9QB0ERfGYl5L61AUH+sdiQ4gr6r +/2QSMEJJN8m13jXi5XYSo6+npH0fpf1itm6mgv8pgjgsCBT2TSYBHXMCtfNLw1Knn88D3qeZtO 4w60HIb3xoR3DcwzJr147+jHE96ANiqL53JFpC2Mb2X0G6JsBGbYUGxF0kyzFKG1JXMi9e+OJIQ YGY5lnlQOwALsowmhnFXgXaYrlehqPwRbl+9r3XSULrj9fAoqWAchLDwxEKnxkBr2lHRABhddEp tTFPn/0qoEWkek5EMw8vItaI67sRdHzapnm8EdLm8YtK7//S+4X/xoshQa9AxXoDdUQibDqCiv/ kUpLamWzvj2w+pkU8wxggDpSZj3A== X-Google-Smtp-Source: AGHT+IEhi5skpO8ovjV3Z3ijxfBPFuObtlXjH9GjayH0SxEofIgcBu601R+yhpFJGs3AHgakPJ96qw== X-Received: by 2002:a05:6000:25c4:b0:42b:41a6:a670 with SMTP id ffacd0b85a97d-42b41a6a685mr74859f8f.54.1762774278734; Mon, 10 Nov 2025 03:31:18 -0800 (PST) Received: from Vasilio.Home ([176.26.203.25]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-42ac679d544sm22516706f8f.46.2025.11.10.03.31.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 03:31:18 -0800 (PST) From: Igor Korotin To: Miguel Ojeda , Alex Gaynor , Wolfram Sang Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , Viresh Kumar , Asahi Lina , Wedson Almeida Filho , Alex Hung , Tamir Duberstein , Xiangfei Ding , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-i2c@vger.kernel.org Subject: [PATCH v7 3/4] samples: rust: add Rust I2C sample driver Date: Mon, 10 Nov 2025 11:31:16 +0000 Message-ID: <20251110113116.51743-1-igor.korotin.linux@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251110112437.50405-1-igor.korotin.linux@gmail.com> References: <20251110112437.50405-1-igor.korotin.linux@gmail.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add a new `rust_driver_i2c` sample, showing how to create a new i2c driver using ACPI/OF/Legacy ID tables. Signed-off-by: Igor Korotin --- MAINTAINERS | 1 + samples/rust/Kconfig | 11 ++++++ samples/rust/Makefile | 1 + samples/rust/rust_driver_i2c.rs | 69 +++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 samples/rust/rust_driver_i2c.rs diff --git a/MAINTAINERS b/MAINTAINERS index 828f6d8b1c32..641757beb56b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11744,6 +11744,7 @@ R: Daniel Almeida L: rust-for-linux@vger.kernel.org S: Maintained F: rust/kernel/i2c.rs +F: samples/rust/rust_driver_i2c.rs I2C SUBSYSTEM HOST DRIVERS M: Andi Shyti diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index c376eb899b7a..181466319588 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -84,6 +84,17 @@ config SAMPLE_RUST_DEBUGFS_SCOPED If unsure, say N. +config SAMPLE_RUST_DRIVER_I2C + tristate "I2C Driver" + depends on I2C=y + help + This option builds the Rust I2C driver sample. + + To compile this as a module, choose M here: + the module will be called rust_driver_i2c. + + If unsure, say N. + config SAMPLE_RUST_DRIVER_PCI tristate "PCI Driver" depends on PCI diff --git a/samples/rust/Makefile b/samples/rust/Makefile index cf8422f8f219..aec865e2adef 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o obj-$(CONFIG_SAMPLE_RUST_DEBUGFS) += rust_debugfs.o obj-$(CONFIG_SAMPLE_RUST_DEBUGFS_SCOPED) += rust_debugfs_scoped.o obj-$(CONFIG_SAMPLE_RUST_DMA) += rust_dma.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C) += rust_driver_i2c.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o diff --git a/samples/rust/rust_driver_i2c.rs b/samples/rust/rust_driver_i2c.rs new file mode 100644 index 000000000000..79d0055793d7 --- /dev/null +++ b/samples/rust/rust_driver_i2c.rs @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust I2C driver sample. + +use kernel::{acpi, c_str, device::Core, i2c, of, prelude::*, types::ARef}; + +struct SampleDriver { + _idev: ARef, +} + +kernel::acpi_device_table! { + ACPI_TABLE, + MODULE_ACPI_TABLE, + ::IdInfo, + [(acpi::DeviceId::new(c_str!("LNUXBEEF")), 0)] +} + +kernel::i2c_device_table! { + I2C_TABLE, + MODULE_I2C_TABLE, + ::IdInfo, + [(i2c::DeviceId::new(c_str!("rust_driver_i2c")), 0)] +} + +kernel::of_device_table! { + OF_TABLE, + MODULE_OF_TABLE, + ::IdInfo, + [(of::DeviceId::new(c_str!("test,rust_driver_i2c")), 0)] +} + +impl i2c::Driver for SampleDriver { + type IdInfo = u32; + + const ACPI_ID_TABLE: Option> = Some(&ACPI_TABLE); + const I2C_ID_TABLE: Option> = Some(&I2C_TABLE); + const OF_ID_TABLE: Option> = Some(&OF_TABLE); + + fn probe( + idev: &i2c::I2cClient, + info: Option<&Self::IdInfo>, + ) -> impl PinInit { + let dev = idev.as_ref(); + + dev_info!(dev, "Probe Rust I2C driver sample.\n"); + + if let Some(info) = info { + dev_info!(dev, "Probed with info: '{}'.\n", info); + } + + Ok(Self { _idev: idev.into() }) + } + + fn shutdown(idev: &i2c::I2cClient, _this: Pin<&Self>) { + dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n"); + } + + fn unbind(idev: &i2c::I2cClient, _this: Pin<&Self>) { + dev_info!(idev.as_ref(), "Unbind Rust I2C driver sample.\n"); + } +} + +kernel::module_i2c_driver! { + type: SampleDriver, + name: "rust_driver_i2c", + authors: ["Igor Korotin"], + description: "Rust I2C driver", + license: "GPL v2", +} -- 2.43.0