drivers/spi/spi-apple.c
Source file repositories/reference/linux-study-clean/drivers/spi/spi-apple.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/spi/spi-apple.c- Extension
.c- Size
- 14703 bytes
- Lines
- 531
- Domain
- Driver Families
- Bucket
- drivers/spi
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bitfield.hlinux/bits.hlinux/clk.hlinux/interrupt.hlinux/io.hlinux/module.hlinux/of.hlinux/platform_device.hlinux/pm_runtime.hlinux/spi/spi.h
Detected Declarations
struct apple_spifunction reg_writefunction reg_readfunction reg_maskfunction apple_spi_initfunction apple_spi_prepare_messagefunction apple_spi_set_csfunction apple_spi_prep_transferfunction apple_spi_irqfunction apple_spi_waitfunction apple_spi_txfunction apple_spi_rxfunction apple_spi_transfer_onefunction apple_spi_probe
Annotated Snippet
struct apple_spi {
void __iomem *regs; /* MMIO register address */
struct clk *clk; /* bus clock */
struct completion done; /* wake-up from interrupt */
};
static inline void reg_write(struct apple_spi *spi, int offset, u32 value)
{
writel_relaxed(value, spi->regs + offset);
}
static inline u32 reg_read(struct apple_spi *spi, int offset)
{
return readl_relaxed(spi->regs + offset);
}
static inline void reg_mask(struct apple_spi *spi, int offset, u32 clear, u32 set)
{
u32 val = reg_read(spi, offset);
val &= ~clear;
val |= set;
reg_write(spi, offset, val);
}
static void apple_spi_init(struct apple_spi *spi)
{
/* Set CS high (inactive) and disable override and auto-CS */
reg_write(spi, APPLE_SPI_PIN, APPLE_SPI_PIN_CS);
reg_mask(spi, APPLE_SPI_SHIFTCFG, APPLE_SPI_SHIFTCFG_OVERRIDE_CS, 0);
reg_mask(spi, APPLE_SPI_PINCFG, APPLE_SPI_PINCFG_CS_IDLE_VAL, APPLE_SPI_PINCFG_KEEP_CS);
/* Reset FIFOs */
reg_write(spi, APPLE_SPI_CTRL, APPLE_SPI_CTRL_RX_RESET | APPLE_SPI_CTRL_TX_RESET);
/* Configure defaults */
reg_write(spi, APPLE_SPI_CFG,
FIELD_PREP(APPLE_SPI_CFG_FIFO_THRESH, APPLE_SPI_CFG_FIFO_THRESH_8B) |
FIELD_PREP(APPLE_SPI_CFG_MODE, APPLE_SPI_CFG_MODE_IRQ) |
FIELD_PREP(APPLE_SPI_CFG_WORD_SIZE, APPLE_SPI_CFG_WORD_SIZE_8B));
/* Disable IRQs */
reg_write(spi, APPLE_SPI_IE_FIFO, 0);
reg_write(spi, APPLE_SPI_IE_XFER, 0);
/* Disable delays */
reg_write(spi, APPLE_SPI_DELAY_PRE, 0);
reg_write(spi, APPLE_SPI_DELAY_POST, 0);
}
static int apple_spi_prepare_message(struct spi_controller *ctlr, struct spi_message *msg)
{
struct apple_spi *spi = spi_controller_get_devdata(ctlr);
struct spi_device *device = msg->spi;
u32 cfg = ((device->mode & SPI_CPHA ? APPLE_SPI_CFG_CPHA : 0) |
(device->mode & SPI_CPOL ? APPLE_SPI_CFG_CPOL : 0) |
(device->mode & SPI_LSB_FIRST ? APPLE_SPI_CFG_LSB_FIRST : 0));
/* Update core config */
reg_mask(spi, APPLE_SPI_CFG,
APPLE_SPI_CFG_CPHA | APPLE_SPI_CFG_CPOL | APPLE_SPI_CFG_LSB_FIRST, cfg);
return 0;
}
static void apple_spi_set_cs(struct spi_device *device, bool is_high)
{
struct apple_spi *spi = spi_controller_get_devdata(device->controller);
reg_mask(spi, APPLE_SPI_PIN, APPLE_SPI_PIN_CS, is_high ? APPLE_SPI_PIN_CS : 0);
}
static bool apple_spi_prep_transfer(struct apple_spi *spi, struct spi_transfer *t)
{
u32 cr, fifo_threshold;
/* Calculate and program the clock rate */
cr = DIV_ROUND_UP(clk_get_rate(spi->clk), t->speed_hz);
reg_write(spi, APPLE_SPI_CLKDIV, min_t(u32, cr, APPLE_SPI_CLKDIV_MAX));
/* Update bits per word */
reg_mask(spi, APPLE_SPI_SHIFTCFG, APPLE_SPI_SHIFTCFG_BITS,
FIELD_PREP(APPLE_SPI_SHIFTCFG_BITS, t->bits_per_word));
/* We will want to poll if the time we need to wait is
* less than the context switching time.
* Let's call that threshold 5us. The operation will take:
* bits_per_word * fifo_threshold / hz <= 5 * 10^-6
* 200000 * bits_per_word * fifo_threshold <= hz
Annotation
- Immediate include surface: `linux/bitfield.h`, `linux/bits.h`, `linux/clk.h`, `linux/interrupt.h`, `linux/io.h`, `linux/module.h`, `linux/of.h`, `linux/platform_device.h`.
- Detected declarations: `struct apple_spi`, `function reg_write`, `function reg_read`, `function reg_mask`, `function apple_spi_init`, `function apple_spi_prepare_message`, `function apple_spi_set_cs`, `function apple_spi_prep_transfer`, `function apple_spi_irq`, `function apple_spi_wait`.
- Atlas domain: Driver Families / drivers/spi.
- Implementation status: source implementation candidate.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.