xref: /openbsd/sys/dev/spi/spivar.h (revision fe922775)
1 /* $OpenBSD: spivar.h,v 1.2 2021/10/31 15:12:00 kettenis Exp $ */
2 /*
3  * Copyright (c) 2018 Patrick Wildt <patrick@blueri.se>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 struct spi_config {
19 	int		 sc_cs;
20 	int		 sc_flags;
21 #define SPI_CONFIG_CPOL		(1 << 0)
22 #define SPI_CONFIG_CPHA		(1 << 1)
23 #define SPI_CONFIG_CS_HIGH	(1 << 2)
24 	int		 sc_bpw;
25 	uint32_t	 sc_freq;
26 	u_int		 sc_cs_delay;
27 };
28 
29 #define SPI_KEEP_CS		(1 << 0)
30 
31 typedef struct spi_controller {
32 	void		*sc_cookie;
33 	void		(*sc_config)(void *, struct spi_config *);
34 	int		(*sc_transfer)(void *, char *, char *, int, int);
35 	int		(*sc_acquire_bus)(void *, int);
36 	void		(*sc_release_bus)(void *, int);
37 } *spi_tag_t;
38 
39 struct spi_attach_args {
40 	spi_tag_t	 sa_tag;
41 	char		*sa_name;
42 	void		*sa_cookie;
43 };
44 
45 #define	spi_config(sc, config)						\
46 	(*(sc)->sc_config)((sc)->sc_cookie, (config))
47 #define	spi_read(sc, data, len)						\
48 	(*(sc)->sc_transfer)((sc)->sc_cookie, NULL, (data), (len), 0)
49 #define	spi_write(sc, data, len)					\
50 	(*(sc)->sc_transfer)((sc)->sc_cookie, (data), NULL, (len), 0)
51 #define	spi_transfer(sc, out, in, len, flags)				\
52 	(*(sc)->sc_transfer)((sc)->sc_cookie, (out), (in), (len), (flags))
53 #define	spi_acquire_bus(sc, flags)					\
54 	(*(sc)->sc_acquire_bus)((sc)->sc_cookie, (flags))
55 #define	spi_release_bus(sc, flags)					\
56 	(*(sc)->sc_release_bus)((sc)->sc_cookie, (flags))
57