Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/rpi-6.6.y' into rpi-6.6.y
Browse files Browse the repository at this point in the history
  • Loading branch information
creatoy committed Jul 27, 2024
2 parents 48d587b + b69efd3 commit 9c45bde
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
Empty file modified code_init.sh
100644 → 100755
Empty file.
16 changes: 13 additions & 3 deletions drivers/i2c/busses/i2c_bcm2835_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,12 @@ module! {
},
}

// test
#[allow(unused)]
fn bcm2835_i2c_writel(i2c_dev: &mut Bcm2835I2cDev, reg: usize, val: u32) {
let i2c_reg = i2c_dev.regs.get();
let addr = i2c_reg.wrapping_add(reg);
unsafe { bindings::writel(val as _, addr as _) }
}

#[allow(unused)]
fn bcm2835_i2c_readl(i2c_dev: &mut Bcm2835I2cDev, reg: usize) -> u32 {
let i2c_reg = i2c_dev.regs.get();
let addr = i2c_reg.wrapping_add(reg);
Expand Down Expand Up @@ -199,6 +196,19 @@ fn clk_bcm2835_i2c_set_rate(hw: &mut ClkHw, rate: u32, parent_rate: u32) -> Resu
Ok(())
}

fn clk_bcm2835_i2c_round_rate(hw: &mut ClkHw, rate: u64, parent_rate: &mut u64) -> u64 {
let divider = clk_bcm2835_i2c_calc_divider(rate, *parent_rate).unwrap();

*parent_rate.div_ceil(divider)
}

fn clk_bcm2835_i2c_recalc_rate(hw: &mut ClkHw, parent_rate: u64) -> u64 {
let div = to_clk_bcm2835_i2c(hw);
let divider = bcm2835_i2c_readl(&mut div.i2c_dev, BCM2835_I2C_DIV);

parent_rate.div_ceil(divider)
}

impl kernel::Module for Bcm2835I2cDevice {
fn init(_module: &'static ThisModule) -> Result<Self> {
pr_info!("BCM2835 i2c bus device driver (init)\n");
Expand Down
103 changes: 103 additions & 0 deletions rust/kernel/clk_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// SPDX-License-Identifier: GPL-2.0

//! Common clock framework.
//!
//! C header: [`include/linux/clk.h`](../../../../include/linux/clk-provider.h)
use crate::{
bindings,
error::{to_result, Result},
types::Opaque,
};

/// Represents `struct clk_core`
///
/// # Invariants
pub struct ClkCore(Opaque<bindings::clk_core>);

/// Represents `struct clk_rate_request`
pub struct ClkRateRequest(bindings::clk_rate_request);

/// Represents `struct clk_duty`
pub struct ClkDuty(bindings::clk_duty);

/// Represents `struct clk_hw`
///
/// # Invariants
///
/// The pointer is valid.
pub struct ClkHw(bindings::clk_hw);

impl ClkHw {
/// Create ClkHw from raw ptr
pub fn from_raw<'a>(ptr: *mut bindings::clk_hw) -> &'a mut Self {
let ptr = ptr.cast::<Self>();
unsafe { &mut *ptr }
}

/// Returns a raw pointer to the inner C struct.
#[inline]
pub fn as_ptr(&self) -> *mut bindings::clk_hw {
self.0.get()
}

// How to implement clk_hw api?
/*
pub fn prepare_enable(&mut self) -> Result {
// SAFETY: call ffi and ptr is valid
unsafe {
to_result(bindings::clk_ops::prepare(self.as_ptr()))?;
let ret = to_result(bindings::clk_ops::enable(self.as_ptr()));
if ret.is_err() {
bindings::clk_ops::unprepare(self.as_ptr());
return ret;
}
}
Ok(())
}
*/

// Unsafe: Argumets Wrapped in CStr.
unsafe fn register_clkdev(&mut self, con_id: &'static CStr, dev_id: &'static CStr) -> i32 {
unsafe {
bindings::clk_hw_register_clkdev(
self.0.get(),
con_id.as_char_ptr(),
dev_id.as_char_ptr(),
)
}
}
}

/*
impl Drop for ClkHw {
fn drop(&mut self) {
// SAFETY: Type Invariant ptr is valid.
unsafe {
bindings::clk_ops::terminate(self.as_ptr());
}
}
}
*/

// TODO: Implement clk_hw_register_clkdev
// unsafe fn clk_hw_register_clkdev() -> Result {}
// TODO: Implement devm_clk_hw_register
// unsafe fn devm_clk_hw_register() -> Result {}

/// Represents `struct clk_ops`
pub struct ClkOps(bindings::clk_ops);

// TODO: Create (new) from functions ptr
impl ClkOps {
// TODO!
pub fn from_raw(ptr: *const bindings::clk_ops) -> Self {
let ptr = ptr.cast::<Self>();
unsafe { &*ptr }
}

#[inline]
pub fn as_ptr(&self) -> *mut bindings::clk_ops {
self.0.get()
}
}
1 change: 1 addition & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod amba;
mod build_assert;
pub mod clk;
pub mod clk_hw;
pub mod clk_provider;
pub mod completion;
pub mod cred;
pub mod delay;
Expand Down

0 comments on commit 9c45bde

Please sign in to comment.