.\" $NetBSD: spi.9,v 1.2 2019/02/23 17:37:10 wiz Exp $
.\"
.\" Copyright (c) 2019 The NetBSD Foundation
.\" All rights reserved.
.\"
.\" Written by Michael van Elst
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd February 23, 2019
.Dt SPI 9
.Os
.Sh NAME
.Nm spi_configure ,
.Nm spi_transfer ,
.Nm spi_transfer_init ,
.Nm spi_chunk_init ,
.Nm spi_transfer_add ,
.Nm spi_wait ,
.Nm spi_done ,
.Nm spi_send ,
.Nm spi_recv ,
.Nm spi_send_recv
.Nd Serial Peripheral Interface (SPI) kernel interface
.Sh SYNOPSIS
.In dev/spi/spivar.h
.Ft int
.Fo spi_configure
.Fa "struct spi_handle *sh"
.Fa "int mode"
.Fa "int speed"
.Fc
.Ft int
.Fo spi_transfer
.Fa "struct spi_handle *sh"
.Fa "struct spi_transfer *st"
.Fc
.Ft void
.Fo spi_transfer_init
.Fa "struct spi_transfer *st"
.Fc
.Ft void
.Fo spi_chunk_init
.Fa "struct spi_chunk *chunk"
.Fa "int cnt"
.Fa "const uint8_t *wptr"
.Fa "uint8_t *rptr"
.Fc
.Ft void
.Fo spi_transfer_add
.Fa "struct spi_transfer *st"
.Fa "struct spi_chunk *chunk"
.Fc
.Ft void
.Fo spi_wait
.Fa "struct spi_transfer *st"
.Fc
.Ft void
.Fo spi_done
.Fa "struct spi_transfer *st"
.Fa "int err"
.Fc
.Ft int
.Fo spi_recv
.Fa "struct spi_handle *sh"
.Fa "int cnt"
.Fa "uint8_t *data"
.Fc
.Ft int
.Fo spi_send
.Fa "struct spi_handle *sh"
.Fa "int cnt"
.Fa "const uint8_t *data"
.Fc
.Ft int
.Fo spi_send_recv
.Fa "struct spi_handle *sh"
.Fa "int scnt"
.Fa "const uint8_t *snd"
.Fa "int rcnt"
.Fa "const uint8_t *rcv"
.Fc
.Sh DESCRIPTION
SPI is a 4-wire synchronous full-duplex serial bus.
It is commonly used for connecting devices such as EEPROMs,
displays, and other types of integrated circuits.
The
.Nm spi
interface provides a means of communicating with SPI-connected devices.
.Sh FUNCTIONS
The following functions comprise the API provided to drivers of
SPI-connected devices.
.Pp
The
.Fa struct spi_handle
corresponding to the device is passed in the driver attachment.
.Bl -tag -width spi_transfer_init
.It Fn spi_configure "sh" "mode" "speed"
Sets mode and speed for subsequent communication with a SPI slave.
.It Fn spi_transfer "sh" "st"
Queue transfer to SPI controller.
.Fn spi_transfer
returns an errno value when the transfer couldn't be queued.
.It Fn spi_transfer_init "st"
Initialize a transfer structure.
.It Fn spi_chunk_init "chunk" "cnt" "wptr" rptr"
Initialize a chunk structure, each chunk corresponds to
a possibly bi-directional transfer where the same amount
of bytes is shifted in and out.
.It Fn spi_transfer_add "st" "chunk"
Append a chunk to transfer structure.
.It Fn spi_wait "st"
Wait for a transfer to complete.
When the transfer has failed for some reason, the field
.Va st->st_errno
is set to a non-zero value.
.It Fn spi_done "st" "err"
Called back machine-dependent backend to signal completion
of a transfer.
.El
.Pp
For simplicity there are convenience functions that combine
common operations.
These functions return an errno value when the transfer failed.
.Bl -tag -width spi_transfer_init
.It Fn spi_recv "sh" "cnt" "data"
Prepares a chunk for receiving data, queues a transfer and
waits for it to complete.
.It Fn spi_send "sh" "cnt" "data"
Prepares a chunk for sending data, queues a transfer and
waits for it to complete.
.It Fn spi_send_recv "sh" "scnt" "snd" "rcnt" "rcv"
Prepares two chunks for sending data first and then receiving
an answer, queues a transfer and waits for it to complete.
This is not a full-duplex operation.
.El
.Sh SEE ALSO
.Xr spi 4
.Sh HISTORY
The
.Nm spi
API first appeared in
.Nx 4.0 .