| 1 | |
| 2 | |
| 3 | |
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use crate::{
|
| 10 | bindings,
|
| 11 | container_of,
|
| 12 | device::{self, Bound},
|
| 13 | devres,
|
| 14 | error::{self, to_result},
|
| 15 | prelude::*,
|
| 16 | sync::aref::{ARef, AlwaysRefCounted},
|
| 17 | types::Opaque, |
| 18 | };
|
| 19 | use core::{
|
| 20 | marker::PhantomData,
|
| 21 | ops::Deref,
|
| 22 | ptr::NonNull, |
| 23 | };
|
| 24 |
|
| 25 | |
| 26 | |
| 27 | #[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
|
| 28 | pub struct Waveform {
|
| 29 | |
| 30 | pub period_length_ns: u64,
|
| 31 |
|
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | pub duty_length_ns: u64,
|
| 37 |
|
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | pub duty_offset_ns: u64,
|
| 44 | }
|
| 45 |
|
| 46 | impl From<bindings::pwm_waveform> for Waveform {
|
| 47 | fn from(wf: bindings::pwm_waveform) -> Self {
|
| 48 | Waveform {
|
| 49 | period_length_ns: wf.period_length_ns,
|
| 50 | duty_length_ns: wf.duty_length_ns,
|
| 51 | duty_offset_ns: wf.duty_offset_ns,
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | impl From<Waveform> for bindings::pwm_waveform {
|
| 57 | fn from(wf: Waveform) -> Self {
|
| 58 | bindings::pwm_waveform {
|
| 59 | period_length_ns: wf.period_length_ns,
|
| 60 | duty_length_ns: wf.duty_length_ns,
|
| 61 | duty_offset_ns: wf.duty_offset_ns,
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | |
| 67 | #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
| 68 | pub enum RoundingOutcome {
|
| 69 | |
| 70 | ExactOrRoundedDown,
|
| 71 |
|
| 72 | |
| 73 | RoundedUp,
|
| 74 | }
|
| 75 |
|
| 76 | |
| 77 | #[repr(transparent)]
|
| 78 | pub struct Device(Opaque<bindings::pwm_device>);
|
| 79 |
|
| 80 | impl Device {
|
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | pub(crate) unsafe fn from_raw<'a>(ptr: *mut bindings::pwm_device) -> &'a Self {
|
| 88 | |
| 89 | |
| 90 | unsafe { &*ptr.cast::<Self>() }
|
| 91 | }
|
| 92 |
|
| 93 | |
| 94 | fn as_raw(&self) -> *mut bindings::pwm_device {
|
| 95 | self.0.get()
|
| 96 | }
|
| 97 |
|
| 98 | |
| 99 | pub fn hwpwm(&self) -> u32 {
|
| 100 | |
| 101 | unsafe { (*self.as_raw()).hwpwm }
|
| 102 | }
|
| 103 |
|
| 104 | |
| 105 | pub fn chip<T: PwmOps>(&self) -> &Chip<T> {
|
| 106 | |
| 107 | |
| 108 | |
| 109 | unsafe { Chip::<T>::from_raw((*self.as_raw()).chip) }
|
| 110 | }
|
| 111 |
|
| 112 | |
| 113 | pub fn label(&self) -> Option<&CStr> {
|
| 114 | |
| 115 | let label_ptr = unsafe { (*self.as_raw()).label };
|
| 116 | if label_ptr.is_null() {
|
| 117 | return None;
|
| 118 | }
|
| 119 |
|
| 120 | |
| 121 | |
| 122 | Some(unsafe { CStr::from_char_ptr(label_ptr) })
|
| 123 | }
|
| 124 |
|
| 125 | |
| 126 | pub fn set_waveform(&self, wf: &Waveform, exact: bool) -> Result {
|
| 127 | let c_wf = bindings::pwm_waveform::from(*wf);
|
| 128 |
|
| 129 | |
| 130 | |
| 131 | |
| 132 | to_result(unsafe { bindings::pwm_set_waveform_might_sleep(self.as_raw(), &c_wf, exact) })
|
| 133 | }
|
| 134 |
|
| 135 | |
| 136 | |
| 137 | pub fn round_waveform(&self, wf: &mut Waveform) -> Result<RoundingOutcome> {
|
| 138 | let mut c_wf = bindings::pwm_waveform::from(*wf);
|
| 139 |
|
| 140 | |
| 141 | |
| 142 | |
| 143 | let ret = unsafe { bindings::pwm_round_waveform_might_sleep(self.as_raw(), &mut c_wf) };
|
| 144 |
|
| 145 | to_result(ret)?;
|
| 146 |
|
| 147 | *wf = Waveform::from(c_wf);
|
| 148 |
|
| 149 | if ret == 1 {
|
| 150 | Ok(RoundingOutcome::RoundedUp)
|
| 151 | } else {
|
| 152 | Ok(RoundingOutcome::ExactOrRoundedDown)
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | |
| 157 | pub fn get_waveform(&self) -> Result<Waveform> {
|
| 158 | let mut c_wf = bindings::pwm_waveform::default();
|
| 159 |
|
| 160 | |
| 161 | |
| 162 | to_result(unsafe { bindings::pwm_get_waveform_might_sleep(self.as_raw(), &mut c_wf) })?;
|
| 163 |
|
| 164 | Ok(Waveform::from(c_wf))
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | |
| 169 | #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
| 170 | pub struct RoundedWaveform<WfHw> {
|
| 171 | |
| 172 | pub status: c_int,
|
| 173 | |
| 174 | pub hardware_waveform: WfHw,
|
| 175 | }
|
| 176 |
|
| 177 | |
| 178 | pub trait PwmOps: 'static + Send + Sync + Sized {
|
| 179 | |
| 180 | |
| 181 | |
| 182 | type WfHw: Copy + Default;
|
| 183 |
|
| 184 | |
| 185 | fn request(_chip: &Chip<Self>, _pwm: &Device, _parent_dev: &device::Device<Bound>) -> Result {
|
| 186 | Ok(())
|
| 187 | }
|
| 188 |
|
| 189 | |
| 190 | fn capture(
|
| 191 | _chip: &Chip<Self>,
|
| 192 | _pwm: &Device,
|
| 193 | _result: &mut bindings::pwm_capture,
|
| 194 | _timeout: usize,
|
| 195 | _parent_dev: &device::Device<Bound>,
|
| 196 | ) -> Result {
|
| 197 | Err(ENOTSUPP)
|
| 198 | }
|
| 199 |
|
| 200 | |
| 201 | |
| 202 | fn round_waveform_tohw(
|
| 203 | _chip: &Chip<Self>,
|
| 204 | _pwm: &Device,
|
| 205 | _wf: &Waveform,
|
| 206 | ) -> Result<RoundedWaveform<Self::WfHw>> {
|
| 207 | Err(ENOTSUPP)
|
| 208 | }
|
| 209 |
|
| 210 | |
| 211 | |
| 212 | fn round_waveform_fromhw(
|
| 213 | _chip: &Chip<Self>,
|
| 214 | _pwm: &Device,
|
| 215 | _wfhw: &Self::WfHw,
|
| 216 | _wf: &mut Waveform,
|
| 217 | ) -> Result {
|
| 218 | Err(ENOTSUPP)
|
| 219 | }
|
| 220 |
|
| 221 | |
| 222 | fn read_waveform(
|
| 223 | _chip: &Chip<Self>,
|
| 224 | _pwm: &Device,
|
| 225 | _parent_dev: &device::Device<Bound>,
|
| 226 | ) -> Result<Self::WfHw> {
|
| 227 | Err(ENOTSUPP)
|
| 228 | }
|
| 229 |
|
| 230 | |
| 231 | fn write_waveform(
|
| 232 | _chip: &Chip<Self>,
|
| 233 | _pwm: &Device,
|
| 234 | _wfhw: &Self::WfHw,
|
| 235 | _parent_dev: &device::Device<Bound>,
|
| 236 | ) -> Result {
|
| 237 | Err(ENOTSUPP)
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | |
| 242 | struct Adapter<T: PwmOps> {
|
| 243 | _p: PhantomData<T>,
|
| 244 | }
|
| 245 |
|
| 246 | impl<T: PwmOps> Adapter<T> {
|
| 247 | const VTABLE: PwmOpsVTable = create_pwm_ops::<T>();
|
| 248 |
|
| 249 | |
| 250 | |
| 251 | |
| 252 | unsafe fn serialize_wfhw(wfhw: &T::WfHw, wfhw_ptr: *mut c_void) -> Result {
|
| 253 | let size = core::mem::size_of::<T::WfHw>();
|
| 254 |
|
| 255 | build_assert!(size <= bindings::PWM_WFHWSIZE as usize);
|
| 256 |
|
| 257 | |
| 258 | unsafe {
|
| 259 | core::ptr::copy_nonoverlapping(
|
| 260 | core::ptr::from_ref::<T::WfHw>(wfhw).cast::<u8>(),
|
| 261 | wfhw_ptr.cast::<u8>(),
|
| 262 | size,
|
| 263 | )
|
| 264 | };
|
| 265 |
|
| 266 | Ok(())
|
| 267 | }
|
| 268 |
|
| 269 | |
| 270 | |
| 271 | |
| 272 | unsafe fn deserialize_wfhw(wfhw_ptr: *const c_void) -> Result<T::WfHw> {
|
| 273 | let size = core::mem::size_of::<T::WfHw>();
|
| 274 |
|
| 275 | build_assert!(size <= bindings::PWM_WFHWSIZE as usize);
|
| 276 |
|
| 277 | let mut wfhw = T::WfHw::default();
|
| 278 | |
| 279 | unsafe {
|
| 280 | core::ptr::copy_nonoverlapping(
|
| 281 | wfhw_ptr.cast::<u8>(),
|
| 282 | core::ptr::from_mut::<T::WfHw>(&mut wfhw).cast::<u8>(),
|
| 283 | size,
|
| 284 | )
|
| 285 | };
|
| 286 |
|
| 287 | Ok(wfhw)
|
| 288 | }
|
| 289 |
|
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | unsafe extern "C" fn release_callback(dev: *mut bindings::device) {
|
| 296 | |
| 297 | |
| 298 | |
| 299 | let c_chip_ptr = unsafe { container_of!(dev, bindings::pwm_chip, dev) };
|
| 300 |
|
| 301 | |
| 302 | |
| 303 | let drvdata_ptr = unsafe { bindings::pwmchip_get_drvdata(c_chip_ptr) };
|
| 304 |
|
| 305 | |
| 306 | unsafe { core::ptr::drop_in_place(drvdata_ptr.cast::<T>()) };
|
| 307 |
|
| 308 | |
| 309 | |
| 310 | |
| 311 | unsafe { bindings::pwmchip_release(dev) };
|
| 312 | }
|
| 313 |
|
| 314 | |
| 315 | |
| 316 | |
| 317 | unsafe extern "C" fn request_callback(
|
| 318 | chip_ptr: *mut bindings::pwm_chip,
|
| 319 | pwm_ptr: *mut bindings::pwm_device,
|
| 320 | ) -> c_int {
|
| 321 | |
| 322 | let (chip, pwm) = unsafe { (Chip::<T>::from_raw(chip_ptr), Device::from_raw(pwm_ptr)) };
|
| 323 |
|
| 324 | |
| 325 | let bound_parent = unsafe { chip.bound_parent_device() };
|
| 326 | match T::request(chip, pwm, bound_parent) {
|
| 327 | Ok(()) => 0,
|
| 328 | Err(e) => e.to_errno(),
|
| 329 | }
|
| 330 | }
|
| 331 |
|
| 332 | |
| 333 | |
| 334 | |
| 335 | unsafe extern "C" fn capture_callback(
|
| 336 | chip_ptr: *mut bindings::pwm_chip,
|
| 337 | pwm_ptr: *mut bindings::pwm_device,
|
| 338 | res: *mut bindings::pwm_capture,
|
| 339 | timeout: usize,
|
| 340 | ) -> c_int {
|
| 341 | |
| 342 | |
| 343 | let (chip, pwm, result) = unsafe {
|
| 344 | (
|
| 345 | Chip::<T>::from_raw(chip_ptr),
|
| 346 | Device::from_raw(pwm_ptr),
|
| 347 | &mut *res,
|
| 348 | )
|
| 349 | };
|
| 350 |
|
| 351 | |
| 352 | let bound_parent = unsafe { chip.bound_parent_device() };
|
| 353 | match T::capture(chip, pwm, result, timeout, bound_parent) {
|
| 354 | Ok(()) => 0,
|
| 355 | Err(e) => e.to_errno(),
|
| 356 | }
|
| 357 | }
|
| 358 |
|
| 359 | |
| 360 | |
| 361 | |
| 362 | unsafe extern "C" fn round_waveform_tohw_callback(
|
| 363 | chip_ptr: *mut bindings::pwm_chip,
|
| 364 | pwm_ptr: *mut bindings::pwm_device,
|
| 365 | wf_ptr: *const bindings::pwm_waveform,
|
| 366 | wfhw_ptr: *mut c_void,
|
| 367 | ) -> c_int {
|
| 368 | |
| 369 | |
| 370 | let (chip, pwm, wf) = unsafe {
|
| 371 | (
|
| 372 | Chip::<T>::from_raw(chip_ptr),
|
| 373 | Device::from_raw(pwm_ptr),
|
| 374 | Waveform::from(*wf_ptr),
|
| 375 | )
|
| 376 | };
|
| 377 | match T::round_waveform_tohw(chip, pwm, &wf) {
|
| 378 | Ok(rounded) => {
|
| 379 | |
| 380 | if unsafe { Self::serialize_wfhw(&rounded.hardware_waveform, wfhw_ptr) }.is_err() {
|
| 381 | return EINVAL.to_errno();
|
| 382 | }
|
| 383 | rounded.status
|
| 384 | }
|
| 385 | Err(e) => e.to_errno(),
|
| 386 | }
|
| 387 | }
|
| 388 |
|
| 389 | |
| 390 | |
| 391 | |
| 392 | unsafe extern "C" fn round_waveform_fromhw_callback(
|
| 393 | chip_ptr: *mut bindings::pwm_chip,
|
| 394 | pwm_ptr: *mut bindings::pwm_device,
|
| 395 | wfhw_ptr: *const c_void,
|
| 396 | wf_ptr: *mut bindings::pwm_waveform,
|
| 397 | ) -> c_int {
|
| 398 | |
| 399 | |
| 400 | let (chip, pwm) = unsafe { (Chip::<T>::from_raw(chip_ptr), Device::from_raw(pwm_ptr)) };
|
| 401 | |
| 402 | let wfhw = match unsafe { Self::deserialize_wfhw(wfhw_ptr) } {
|
| 403 | Ok(v) => v,
|
| 404 | Err(e) => return e.to_errno(),
|
| 405 | };
|
| 406 |
|
| 407 | let mut rust_wf = Waveform::default();
|
| 408 | match T::round_waveform_fromhw(chip, pwm, &wfhw, &mut rust_wf) {
|
| 409 | Ok(()) => {
|
| 410 | |
| 411 | unsafe { *wf_ptr = rust_wf.into() };
|
| 412 | 0
|
| 413 | }
|
| 414 | Err(e) => e.to_errno(),
|
| 415 | }
|
| 416 | }
|
| 417 |
|
| 418 | |
| 419 | |
| 420 | |
| 421 | unsafe extern "C" fn read_waveform_callback(
|
| 422 | chip_ptr: *mut bindings::pwm_chip,
|
| 423 | pwm_ptr: *mut bindings::pwm_device,
|
| 424 | wfhw_ptr: *mut c_void,
|
| 425 | ) -> c_int {
|
| 426 | |
| 427 | |
| 428 | let (chip, pwm) = unsafe { (Chip::<T>::from_raw(chip_ptr), Device::from_raw(pwm_ptr)) };
|
| 429 |
|
| 430 | |
| 431 | let bound_parent = unsafe { chip.bound_parent_device() };
|
| 432 | match T::read_waveform(chip, pwm, bound_parent) {
|
| 433 | |
| 434 | Ok(wfhw) => match unsafe { Self::serialize_wfhw(&wfhw, wfhw_ptr) } {
|
| 435 | Ok(()) => 0,
|
| 436 | Err(e) => e.to_errno(),
|
| 437 | },
|
| 438 | Err(e) => e.to_errno(),
|
| 439 | }
|
| 440 | }
|
| 441 |
|
| 442 | |
| 443 | |
| 444 | |
| 445 | unsafe extern "C" fn write_waveform_callback(
|
| 446 | chip_ptr: *mut bindings::pwm_chip,
|
| 447 | pwm_ptr: *mut bindings::pwm_device,
|
| 448 | wfhw_ptr: *const c_void,
|
| 449 | ) -> c_int {
|
| 450 | |
| 451 | |
| 452 | let (chip, pwm) = unsafe { (Chip::<T>::from_raw(chip_ptr), Device::from_raw(pwm_ptr)) };
|
| 453 |
|
| 454 | |
| 455 | let bound_parent = unsafe { chip.bound_parent_device() };
|
| 456 |
|
| 457 | |
| 458 | let wfhw = match unsafe { Self::deserialize_wfhw(wfhw_ptr) } {
|
| 459 | Ok(v) => v,
|
| 460 | Err(e) => return e.to_errno(),
|
| 461 | };
|
| 462 | match T::write_waveform(chip, pwm, &wfhw, bound_parent) {
|
| 463 | Ok(()) => 0,
|
| 464 | Err(e) => e.to_errno(),
|
| 465 | }
|
| 466 | }
|
| 467 | }
|
| 468 |
|
| 469 | |
| 470 | |
| 471 | #[repr(transparent)]
|
| 472 | pub struct PwmOpsVTable(bindings::pwm_ops);
|
| 473 |
|
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | unsafe impl Send for PwmOpsVTable {}
|
| 479 |
|
| 480 | |
| 481 | |
| 482 | |
| 483 | unsafe impl Sync for PwmOpsVTable {}
|
| 484 |
|
| 485 | impl PwmOpsVTable {
|
| 486 | |
| 487 | pub(crate) fn as_raw(&self) -> *const bindings::pwm_ops {
|
| 488 | &self.0
|
| 489 | }
|
| 490 | }
|
| 491 |
|
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | pub const fn create_pwm_ops<T: PwmOps>() -> PwmOpsVTable {
|
| 497 | let mut ops: bindings::pwm_ops = pin_init::zeroed();
|
| 498 |
|
| 499 | ops.request = Some(Adapter::<T>::request_callback);
|
| 500 | ops.capture = Some(Adapter::<T>::capture_callback);
|
| 501 |
|
| 502 | ops.round_waveform_tohw = Some(Adapter::<T>::round_waveform_tohw_callback);
|
| 503 | ops.round_waveform_fromhw = Some(Adapter::<T>::round_waveform_fromhw_callback);
|
| 504 | ops.read_waveform = Some(Adapter::<T>::read_waveform_callback);
|
| 505 | ops.write_waveform = Some(Adapter::<T>::write_waveform_callback);
|
| 506 | ops.sizeof_wfhw = core::mem::size_of::<T::WfHw>();
|
| 507 |
|
| 508 | PwmOpsVTable(ops)
|
| 509 | }
|
| 510 |
|
| 511 | |
| 512 | #[repr(transparent)]
|
| 513 | pub struct Chip<T: PwmOps>(Opaque<bindings::pwm_chip>, PhantomData<T>);
|
| 514 |
|
| 515 | impl<T: PwmOps> Chip<T> {
|
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | pub(crate) unsafe fn from_raw<'a>(ptr: *mut bindings::pwm_chip) -> &'a Self {
|
| 523 | |
| 524 | |
| 525 | unsafe { &*ptr.cast::<Self>() }
|
| 526 | }
|
| 527 |
|
| 528 | |
| 529 | pub(crate) fn as_raw(&self) -> *mut bindings::pwm_chip {
|
| 530 | self.0.get()
|
| 531 | }
|
| 532 |
|
| 533 | |
| 534 | pub fn num_channels(&self) -> u32 {
|
| 535 | |
| 536 | unsafe { (*self.as_raw()).npwm }
|
| 537 | }
|
| 538 |
|
| 539 | |
| 540 | pub fn is_atomic(&self) -> bool {
|
| 541 | |
| 542 | unsafe { (*self.as_raw()).atomic }
|
| 543 | }
|
| 544 |
|
| 545 | |
| 546 | pub fn device(&self) -> &device::Device {
|
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | unsafe { device::Device::from_raw(&raw mut (*self.as_raw()).dev) }
|
| 555 | }
|
| 556 |
|
| 557 | |
| 558 | pub fn drvdata(&self) -> &T {
|
| 559 | |
| 560 | |
| 561 | unsafe { &*bindings::pwmchip_get_drvdata(self.as_raw()).cast::<T>() }
|
| 562 | }
|
| 563 |
|
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | unsafe fn bound_parent_device(&self) -> &device::Device<Bound> {
|
| 571 | |
| 572 | let parent = unsafe { self.device().parent().unwrap_unchecked() };
|
| 573 |
|
| 574 | |
| 575 | |
| 576 | unsafe { parent.as_bound() }
|
| 577 | }
|
| 578 |
|
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | #[allow(clippy::new_ret_no_self)]
|
| 584 | pub fn new<'a>(
|
| 585 | parent_dev: &'a device::Device<Bound>,
|
| 586 | num_channels: u32,
|
| 587 | data: impl pin_init::PinInit<T, Error>,
|
| 588 | ) -> Result<UnregisteredChip<'a, T>> {
|
| 589 | let sizeof_priv = core::mem::size_of::<T>();
|
| 590 | |
| 591 | let c_chip_ptr_raw =
|
| 592 | unsafe { bindings::pwmchip_alloc(parent_dev.as_raw(), num_channels, sizeof_priv) };
|
| 593 |
|
| 594 | let c_chip_ptr: *mut bindings::pwm_chip = error::from_err_ptr(c_chip_ptr_raw)?;
|
| 595 |
|
| 596 | |
| 597 | |
| 598 | let drvdata_ptr = unsafe { bindings::pwmchip_get_drvdata(c_chip_ptr) };
|
| 599 |
|
| 600 | |
| 601 | unsafe { pin_init::raw_try_init(drvdata_ptr.cast(), data) }.inspect_err(|_| {
|
| 602 | |
| 603 | |
| 604 | unsafe { bindings::pwmchip_put(c_chip_ptr) }
|
| 605 | })?;
|
| 606 |
|
| 607 | |
| 608 | unsafe { (*c_chip_ptr).dev.release = Some(Adapter::<T>::release_callback) };
|
| 609 |
|
| 610 | |
| 611 | |
| 612 | |
| 613 | unsafe { (*c_chip_ptr).ops = Adapter::<T>::VTABLE.as_raw() };
|
| 614 |
|
| 615 | |
| 616 | |
| 617 | |
| 618 | let chip_ptr_as_self = c_chip_ptr.cast::<Self>();
|
| 619 |
|
| 620 | |
| 621 | |
| 622 | |
| 623 | let chip = unsafe { ARef::from_raw(NonNull::new_unchecked(chip_ptr_as_self)) };
|
| 624 |
|
| 625 | Ok(UnregisteredChip { chip, parent_dev })
|
| 626 | }
|
| 627 | }
|
| 628 |
|
| 629 | |
| 630 | unsafe impl<T: PwmOps> AlwaysRefCounted for Chip<T> {
|
| 631 | #[inline]
|
| 632 | fn inc_ref(&self) {
|
| 633 | |
| 634 | |
| 635 | unsafe { bindings::get_device(&raw mut (*self.0.get()).dev) };
|
| 636 | }
|
| 637 |
|
| 638 | #[inline]
|
| 639 | unsafe fn dec_ref(obj: NonNull<Chip<T>>) {
|
| 640 | let c_chip_ptr = obj.cast::<bindings::pwm_chip>().as_ptr();
|
| 641 |
|
| 642 | |
| 643 | |
| 644 | unsafe { bindings::put_device(&raw mut (*c_chip_ptr).dev) };
|
| 645 | }
|
| 646 | }
|
| 647 |
|
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | unsafe impl<T: PwmOps> Send for Chip<T> {}
|
| 653 |
|
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | unsafe impl<T: PwmOps> Sync for Chip<T> {}
|
| 659 |
|
| 660 | |
| 661 | pub struct UnregisteredChip<'a, T: PwmOps> {
|
| 662 | chip: ARef<Chip<T>>,
|
| 663 | parent_dev: &'a device::Device<Bound>,
|
| 664 | }
|
| 665 |
|
| 666 | impl<T: PwmOps> UnregisteredChip<'_, T> {
|
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | pub fn register(self) -> Result<ARef<Chip<T>>> {
|
| 674 | let c_chip_ptr = self.chip.as_raw();
|
| 675 |
|
| 676 | |
| 677 | |
| 678 | to_result(unsafe { bindings::__pwmchip_add(c_chip_ptr, core::ptr::null_mut()) })?;
|
| 679 |
|
| 680 | let registration = Registration {
|
| 681 | chip: ARef::clone(&self.chip),
|
| 682 | };
|
| 683 |
|
| 684 | devres::register(self.parent_dev, registration, GFP_KERNEL)?;
|
| 685 |
|
| 686 | Ok(self.chip)
|
| 687 | }
|
| 688 | }
|
| 689 |
|
| 690 | impl<T: PwmOps> Deref for UnregisteredChip<'_, T> {
|
| 691 | type Target = Chip<T>;
|
| 692 |
|
| 693 | fn deref(&self) -> &Self::Target {
|
| 694 | &self.chip
|
| 695 | }
|
| 696 | }
|
| 697 |
|
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | struct Registration<T: PwmOps> {
|
| 704 | chip: ARef<Chip<T>>,
|
| 705 | }
|
| 706 |
|
| 707 | impl<T: PwmOps> Drop for Registration<T> {
|
| 708 | fn drop(&mut self) {
|
| 709 | let chip_raw = self.chip.as_raw();
|
| 710 |
|
| 711 | |
| 712 | |
| 713 | |
| 714 | unsafe { bindings::pwmchip_remove(chip_raw) };
|
| 715 | }
|
| 716 | }
|
| 717 |
|
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | #[macro_export]
|
| 732 | macro_rules! module_pwm_platform_driver {
|
| 733 | ($($user_args:tt)*) => {
|
| 734 | $crate::module_platform_driver! {
|
| 735 | $($user_args)*
|
| 736 | imports_ns: ["PWM"],
|
| 737 | }
|
| 738 | };
|
| 739 | }
|