1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
// SPDX-License-Identifier: GPL-2.0
//! Clock abstractions.
//!
//! C header: [`include/linux/clk.h`](srctree/include/linux/clk.h)
//!
//! Reference: <https://docs.kernel.org/driver-api/clk.html>
use crate::ffi::c_ulong;
/// The frequency unit.
///
/// Represents a frequency in hertz, wrapping a [`c_ulong`] value.
///
/// ## Examples
///
/// ```
/// use kernel::clk::Hertz;
///
/// let hz = 1_000_000_000;
/// let rate = Hertz(hz);
///
/// assert_eq!(rate.as_hz(), hz);
/// assert_eq!(rate, Hertz(hz));
/// assert_eq!(rate, Hertz::from_khz(hz / 1_000));
/// assert_eq!(rate, Hertz::from_mhz(hz / 1_000_000));
/// assert_eq!(rate, Hertz::from_ghz(hz / 1_000_000_000));
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Hertz(pub c_ulong);
impl Hertz {
/// Create a new instance from kilohertz (kHz)
pub fn from_khz(khz: c_ulong) -> Self {
Self(khz * 1_000)
}
/// Create a new instance from megahertz (MHz)
pub fn from_mhz(mhz: c_ulong) -> Self {
Self(mhz * 1_000_000)
}
/// Create a new instance from gigahertz (GHz)
pub fn from_ghz(ghz: c_ulong) -> Self {
Self(ghz * 1_000_000_000)
}
/// Get the frequency in hertz
pub fn as_hz(&self) -> c_ulong {
self.0
}
/// Get the frequency in kilohertz
pub fn as_khz(&self) -> c_ulong {
self.0 / 1_000
}
/// Get the frequency in megahertz
pub fn as_mhz(&self) -> c_ulong {
self.0 / 1_000_000
}
/// Get the frequency in gigahertz
pub fn as_ghz(&self) -> c_ulong {
self.0 / 1_000_000_000
}
}
impl From<Hertz> for c_ulong {
fn from(freq: Hertz) -> Self {
freq.0
}
}
#[cfg(CONFIG_COMMON_CLK)]
mod common_clk {
use super::Hertz;
use crate::{
device::Device,
error::{from_err_ptr, to_result, Result},
prelude::*,
};
use core::{ops::Deref, ptr};
/// A reference-counted clock.
///
/// Rust abstraction for the C [`struct clk`].
///
/// # Invariants
///
/// A [`Clk`] instance holds either a pointer to a valid [`struct clk`] created by the C
/// portion of the kernel or a NULL pointer.
///
/// Instances of this type are reference-counted. Calling [`Clk::get`] ensures that the
/// allocation remains valid for the lifetime of the [`Clk`].
///
/// ## Examples
///
/// The following example demonstrates how to obtain and configure a clock for a device.
///
/// ```
/// use kernel::c_str;
/// use kernel::clk::{Clk, Hertz};
/// use kernel::device::Device;
/// use kernel::error::Result;
///
/// fn configure_clk(dev: &Device) -> Result {
/// let clk = Clk::get(dev, Some(c_str!("apb_clk")))?;
///
/// clk.prepare_enable()?;
///
/// let expected_rate = Hertz::from_ghz(1);
///
/// if clk.rate() != expected_rate {
/// clk.set_rate(expected_rate)?;
/// }
///
/// clk.disable_unprepare();
/// Ok(())
/// }
/// ```
///
/// [`struct clk`]: https://docs.kernel.org/driver-api/clk.html
#[repr(transparent)]
pub struct Clk(*mut bindings::clk);
impl Clk {
/// Gets [`Clk`] corresponding to a [`Device`] and a connection id.
///
/// Equivalent to the kernel's [`clk_get`] API.
///
/// [`clk_get`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_get
pub fn get(dev: &Device, name: Option<&CStr>) -> Result<Self> {
let con_id = if let Some(name) = name {
name.as_ptr()
} else {
ptr::null()
};
// SAFETY: It is safe to call [`clk_get`] for a valid device pointer.
//
// INVARIANT: The reference-count is decremented when [`Clk`] goes out of scope.
Ok(Self(from_err_ptr(unsafe {
bindings::clk_get(dev.as_raw(), con_id)
})?))
}
/// Obtain the raw [`struct clk`] pointer.
#[inline]
pub fn as_raw(&self) -> *mut bindings::clk {
self.0
}
/// Enable the clock.
///
/// Equivalent to the kernel's [`clk_enable`] API.
///
/// [`clk_enable`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_enable
#[inline]
pub fn enable(&self) -> Result {
// SAFETY: By the type invariants, self.as_raw() is a valid argument for
// [`clk_enable`].
to_result(unsafe { bindings::clk_enable(self.as_raw()) })
}
/// Disable the clock.
///
/// Equivalent to the kernel's [`clk_disable`] API.
///
/// [`clk_disable`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_disable
#[inline]
pub fn disable(&self) {
// SAFETY: By the type invariants, self.as_raw() is a valid argument for
// [`clk_disable`].
unsafe { bindings::clk_disable(self.as_raw()) };
}
/// Prepare the clock.
///
/// Equivalent to the kernel's [`clk_prepare`] API.
///
/// [`clk_prepare`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_prepare
#[inline]
pub fn prepare(&self) -> Result {
// SAFETY: By the type invariants, self.as_raw() is a valid argument for
// [`clk_prepare`].
to_result(unsafe { bindings::clk_prepare(self.as_raw()) })
}
/// Unprepare the clock.
///
/// Equivalent to the kernel's [`clk_unprepare`] API.
///
/// [`clk_unprepare`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_unprepare
#[inline]
pub fn unprepare(&self) {
// SAFETY: By the type invariants, self.as_raw() is a valid argument for
// [`clk_unprepare`].
unsafe { bindings::clk_unprepare(self.as_raw()) };
}
/// Prepare and enable the clock.
///
/// Equivalent to calling [`Clk::prepare`] followed by [`Clk::enable`].
#[inline]
pub fn prepare_enable(&self) -> Result {
// SAFETY: By the type invariants, self.as_raw() is a valid argument for
// [`clk_prepare_enable`].
to_result(unsafe { bindings::clk_prepare_enable(self.as_raw()) })
}
/// Disable and unprepare the clock.
///
/// Equivalent to calling [`Clk::disable`] followed by [`Clk::unprepare`].
#[inline]
pub fn disable_unprepare(&self) {
// SAFETY: By the type invariants, self.as_raw() is a valid argument for
// [`clk_disable_unprepare`].
unsafe { bindings::clk_disable_unprepare(self.as_raw()) };
}
/// Get clock's rate.
///
/// Equivalent to the kernel's [`clk_get_rate`] API.
///
/// [`clk_get_rate`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_get_rate
#[inline]
pub fn rate(&self) -> Hertz {
// SAFETY: By the type invariants, self.as_raw() is a valid argument for
// [`clk_get_rate`].
Hertz(unsafe { bindings::clk_get_rate(self.as_raw()) })
}
/// Set clock's rate.
///
/// Equivalent to the kernel's [`clk_set_rate`] API.
///
/// [`clk_set_rate`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_set_rate
#[inline]
pub fn set_rate(&self, rate: Hertz) -> Result {
// SAFETY: By the type invariants, self.as_raw() is a valid argument for
// [`clk_set_rate`].
to_result(unsafe { bindings::clk_set_rate(self.as_raw(), rate.as_hz()) })
}
}
impl Drop for Clk {
fn drop(&mut self) {
// SAFETY: By the type invariants, self.as_raw() is a valid argument for [`clk_put`].
unsafe { bindings::clk_put(self.as_raw()) };
}
}
/// A reference-counted optional clock.
///
/// A lightweight wrapper around an optional [`Clk`]. An [`OptionalClk`] represents a [`Clk`]
/// that a driver can function without but may improve performance or enable additional
/// features when available.
///
/// # Invariants
///
/// An [`OptionalClk`] instance encapsulates a [`Clk`] with either a valid [`struct clk`] or
/// `NULL` pointer.
///
/// Instances of this type are reference-counted. Calling [`OptionalClk::get`] ensures that the
/// allocation remains valid for the lifetime of the [`OptionalClk`].
///
/// ## Examples
///
/// The following example demonstrates how to obtain and configure an optional clock for a
/// device. The code functions correctly whether or not the clock is available.
///
/// ```
/// use kernel::c_str;
/// use kernel::clk::{OptionalClk, Hertz};
/// use kernel::device::Device;
/// use kernel::error::Result;
///
/// fn configure_clk(dev: &Device) -> Result {
/// let clk = OptionalClk::get(dev, Some(c_str!("apb_clk")))?;
///
/// clk.prepare_enable()?;
///
/// let expected_rate = Hertz::from_ghz(1);
///
/// if clk.rate() != expected_rate {
/// clk.set_rate(expected_rate)?;
/// }
///
/// clk.disable_unprepare();
/// Ok(())
/// }
/// ```
///
/// [`struct clk`]: https://docs.kernel.org/driver-api/clk.html
pub struct OptionalClk(Clk);
impl OptionalClk {
/// Gets [`OptionalClk`] corresponding to a [`Device`] and a connection id.
///
/// Equivalent to the kernel's [`clk_get_optional`] API.
///
/// [`clk_get_optional`]:
/// https://docs.kernel.org/core-api/kernel-api.html#c.clk_get_optional
pub fn get(dev: &Device, name: Option<&CStr>) -> Result<Self> {
let con_id = if let Some(name) = name {
name.as_ptr()
} else {
ptr::null()
};
// SAFETY: It is safe to call [`clk_get_optional`] for a valid device pointer.
//
// INVARIANT: The reference-count is decremented when [`OptionalClk`] goes out of
// scope.
Ok(Self(Clk(from_err_ptr(unsafe {
bindings::clk_get_optional(dev.as_raw(), con_id)
})?)))
}
}
// Make [`OptionalClk`] behave like [`Clk`].
impl Deref for OptionalClk {
type Target = Clk;
fn deref(&self) -> &Clk {
&self.0
}
}
}
#[cfg(CONFIG_COMMON_CLK)]
pub use common_clk::*;
|