|
| 1 | +// Linux SPI bindings |
| 2 | +// |
| 3 | +// Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. |
| 4 | + |
| 5 | +// |
| 6 | +// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause |
| 7 | + |
| 8 | +use bitflags::bitflags; |
| 9 | +use vmm_sys_util::{ioctl_ioc_nr, ioctl_ior_nr, ioctl_iow_nr}; |
| 10 | + |
| 11 | +/// Describes a single SPI transfer |
| 12 | +#[derive(Debug)] |
| 13 | +#[repr(C)] |
| 14 | +pub struct SpiIocTransfer { |
| 15 | + /// Holds pointer to userspace buffer with transmit data, or null |
| 16 | + pub tx_buf: u64, |
| 17 | + /// Holds pointer to userspace buffer for receive data, or null. |
| 18 | + pub rx_buf: u64, |
| 19 | + /// Length of tx and rx buffers, in bytes. |
| 20 | + pub len: u32, |
| 21 | + /// Temporary override of the device's bitrate. |
| 22 | + pub speed_hz: u32, |
| 23 | + /// If nonzero, how long to delay after the last bit transfer |
| 24 | + /// before optionally deselecting the device before the next transfer. |
| 25 | + pub delay_usecs: u16, |
| 26 | + /// Temporary override of the device's wordsize. |
| 27 | + pub bits_per_word: u8, |
| 28 | + /// True to deselect device before starting the next transfer. |
| 29 | + pub cs_change: u8, |
| 30 | + /// Number of bits used for writing. |
| 31 | + pub tx_nbits: u8, |
| 32 | + /// Number of bits used for reading. |
| 33 | + pub rx_nbits: u8, |
| 34 | + /// If nonzero, how long to wait between words within one |
| 35 | + /// transfer. This property needs explicit support in the SPI controller, |
| 36 | + /// otherwise it is silently ignored |
| 37 | + pub word_delay_usecs: u8, |
| 38 | + pub _padding: u8, |
| 39 | +} |
| 40 | + |
| 41 | +/// Linux SPI definitions |
| 42 | +/// IOCTL commands, refer Linux's Documentation/spi/spidev.rst for further details. |
| 43 | +const _IOC_SIZEBITS: u32 = 14; |
| 44 | +const _IOC_SIZESHIFT: u32 = 16; |
| 45 | +const SPI_IOC_MESSAGE_BASE: u32 = 0x40006b00; |
| 46 | + |
| 47 | +ioctl_ior_nr!(SPI_IOC_RD_BITS_PER_WORD, 107, 3, u8); |
| 48 | +ioctl_iow_nr!(SPI_IOC_WR_BITS_PER_WORD, 107, 3, u8); |
| 49 | +ioctl_ior_nr!(SPI_IOC_RD_MAX_SPEED_HZ, 107, 4, u32); |
| 50 | +ioctl_iow_nr!(SPI_IOC_WR_MAX_SPEED_HZ, 107, 4, u32); |
| 51 | +ioctl_ior_nr!(SPI_IOC_RD_MODE32, 107, 5, u32); |
| 52 | +ioctl_iow_nr!(SPI_IOC_WR_MODE32, 107, 5, u32); |
| 53 | + |
| 54 | +// Corresponds to the SPI_IOC_MESSAGE macro in Linux |
| 55 | +pub fn spi_ioc_message(n: u32) -> u64 { |
| 56 | + let mut size: u32 = 0; |
| 57 | + if n * 32 < (1 << _IOC_SIZEBITS) { |
| 58 | + size = n * 32; |
| 59 | + } |
| 60 | + (SPI_IOC_MESSAGE_BASE | (size << _IOC_SIZESHIFT)) as u64 |
| 61 | +} |
| 62 | + |
| 63 | +bitflags! { |
| 64 | + pub struct LnxSpiMode: u32 { |
| 65 | + const CPHA = 1 << 0; |
| 66 | + const CPOL = 1 << 1; |
| 67 | + const CS_HIGH = 1 << 2; |
| 68 | + const LSB_FIRST = 1 << 3; |
| 69 | + const LOOP = 1 << 5; |
| 70 | + const TX_DUAL = 1 << 8; |
| 71 | + const TX_QUAD = 1 << 9; |
| 72 | + const TX_OCTAL = 1 << 13; |
| 73 | + const RX_DUAL = 1 << 10; |
| 74 | + const RX_QUAD = 1 << 11; |
| 75 | + const RX_OCTAL = 1 << 14; |
| 76 | + } |
| 77 | +} |
0 commit comments