5
5
//! C header: [`include/linux/gpio/driver.h`](../../../../include/linux/gpio/driver.h)
6
6
7
7
use crate :: {
8
- bindings, device, error:: code:: * , error:: from_kernel_result, types :: PointerWrapper , Error ,
9
- Result ,
8
+ bindings, device, error:: code:: * , error:: from_kernel_result, sync :: LockClassKey ,
9
+ types :: PointerWrapper , Error , Result ,
10
10
} ;
11
11
use core:: {
12
12
cell:: UnsafeCell ,
@@ -120,6 +120,27 @@ macro_rules! declare_gpio_chip_operations {
120
120
}
121
121
122
122
/// A registration of a gpio chip.
123
+ ///
124
+ /// # Examples
125
+ ///
126
+ /// The following example registers an empty gpio chip.
127
+ ///
128
+ /// ```
129
+ /// # use kernel::prelude::*;
130
+ /// use kernel::{device::RawDevice, gpio::{self, Registration}};
131
+ ///
132
+ /// struct MyGpioChip;
133
+ /// impl gpio::Chip for MyGpioChip {
134
+ /// type Data = ();
135
+ /// kernel::declare_gpio_chip_operations!();
136
+ /// }
137
+ ///
138
+ /// fn example(parent: &dyn RawDevice) -> Result<Pin<Box<Registration<MyGpioChip>>>> {
139
+ /// let mut r = Pin::from(Box::try_new(Registration::new())?);
140
+ /// kernel::gpio_chip_register!(r.as_mut(), 32, None, parent, ())?;
141
+ /// Ok(r)
142
+ /// }
143
+ /// ```
123
144
pub struct Registration < T : Chip > {
124
145
gc : UnsafeCell < bindings:: gpio_chip > ,
125
146
parent : Option < device:: Device > ,
@@ -141,12 +162,16 @@ impl<T: Chip> Registration<T> {
141
162
}
142
163
143
164
/// Registers a gpio chip with the rest of the kernel.
165
+ ///
166
+ /// Users are encouraged to use the [`gpio_chip_register`] macro because it automatically
167
+ /// defines the lock classes and calls the registration function.
144
168
pub fn register (
145
169
self : Pin < & mut Self > ,
146
170
gpio_count : u16 ,
147
171
base : Option < i32 > ,
148
172
parent : & dyn device:: RawDevice ,
149
173
data : T :: Data ,
174
+ lock_keys : [ & ' static LockClassKey ; 2 ] ,
150
175
) -> Result {
151
176
if self . parent . is_some ( ) {
152
177
// Already registered.
@@ -197,8 +222,8 @@ impl<T: Chip> Registration<T> {
197
222
bindings:: gpiochip_add_data_with_key (
198
223
this. gc . get ( ) ,
199
224
data_pointer as _ ,
200
- core :: ptr :: null_mut ( ) ,
201
- core :: ptr :: null_mut ( ) ,
225
+ lock_keys [ 0 ] . get ( ) ,
226
+ lock_keys [ 1 ] . get ( ) ,
202
227
)
203
228
} ;
204
229
if ret < 0 {
@@ -247,6 +272,25 @@ impl<T: Chip> Drop for Registration<T> {
247
272
}
248
273
}
249
274
275
+ /// Registers a gpio chip with the rest of the kernel.
276
+ ///
277
+ /// It automatically defines the required lock classes.
278
+ #[ macro_export]
279
+ macro_rules! gpio_chip_register {
280
+ ( $reg: expr, $count: expr, $base: expr, $parent: expr, $data: expr $( , ) ?) => { {
281
+ static CLASS1 : $crate:: sync:: LockClassKey = $crate:: sync:: LockClassKey :: new( ) ;
282
+ static CLASS2 : $crate:: sync:: LockClassKey = $crate:: sync:: LockClassKey :: new( ) ;
283
+ $crate:: gpio:: Registration :: register(
284
+ $reg,
285
+ $count,
286
+ $base,
287
+ $parent,
288
+ $data,
289
+ [ & CLASS1 , & CLASS2 ] ,
290
+ )
291
+ } } ;
292
+ }
293
+
250
294
unsafe extern "C" fn get_direction_callback < T : Chip > (
251
295
gc : * mut bindings:: gpio_chip ,
252
296
offset : core:: ffi:: c_uint ,
@@ -340,13 +384,17 @@ mod irqchip {
340
384
}
341
385
342
386
/// Registers a gpio chip and its irq chip with the rest of the kernel.
387
+ ///
388
+ /// Users are encouraged to use the [`gpio_irq_chip_register`] macro because it
389
+ /// automatically defines the lock classes and calls the registration function.
343
390
pub fn register < U : irq:: Chip < Data = T :: Data > > (
344
391
mut self : Pin < & mut Self > ,
345
392
gpio_count : u16 ,
346
393
base : Option < i32 > ,
347
394
parent : & dyn device:: RawDevice ,
348
395
data : T :: Data ,
349
396
parent_irq : u32 ,
397
+ lock_keys : [ & ' static LockClassKey ; 2 ] ,
350
398
) -> Result {
351
399
if self . reg . parent . is_some ( ) {
352
400
// Already registered.
@@ -384,7 +432,7 @@ mod irqchip {
384
432
385
433
// SAFETY: `reg` is pinned when `self` is.
386
434
let pinned = unsafe { self . map_unchecked_mut ( |r| & mut r. reg ) } ;
387
- pinned. register ( gpio_count, base, parent, data)
435
+ pinned. register ( gpio_count, base, parent, data, lock_keys )
388
436
}
389
437
}
390
438
@@ -475,4 +523,25 @@ mod irqchip {
475
523
T :: set_wake ( data, irq_data, on)
476
524
}
477
525
}
526
+
527
+ /// Registers a gpio chip and its irq chip with the rest of the kernel.
528
+ ///
529
+ /// It automatically defines the required lock classes.
530
+ #[ macro_export]
531
+ macro_rules! gpio_irq_chip_register {
532
+ ( $reg: expr, $irqchip: ty, $count: expr, $base: expr, $parent: expr, $data: expr,
533
+ $parent_irq: expr $( , ) ?) => { {
534
+ static CLASS1 : $crate:: sync:: LockClassKey = $crate:: sync:: LockClassKey :: new( ) ;
535
+ static CLASS2 : $crate:: sync:: LockClassKey = $crate:: sync:: LockClassKey :: new( ) ;
536
+ $crate:: gpio:: RegistrationWithIrqChip :: register:: <$irqchip>(
537
+ $reg,
538
+ $count,
539
+ $base,
540
+ $parent,
541
+ $data,
542
+ $parent_irq,
543
+ [ & CLASS1 , & CLASS2 ] ,
544
+ )
545
+ } } ;
546
+ }
478
547
}
0 commit comments