Home | History | Annotate | Line # | Download | only in spi
spivar.h revision 1.10.6.1
      1 /* $NetBSD: spivar.h,v 1.10.6.1 2021/05/18 23:48:16 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5  * Copyright (c) 2006 Garrett D'Amore.
      6  * All rights reserved.
      7  *
      8  * Portions of this code were written by Garrett D'Amore for the
      9  * Champaign-Urbana Community Wireless Network Project.
     10  *
     11  * Redistribution and use in source and binary forms, with or
     12  * without modification, are permitted provided that the following
     13  * conditions are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above
     17  *    copyright notice, this list of conditions and the following
     18  *    disclaimer in the documentation and/or other materials provided
     19  *    with the distribution.
     20  * 3. All advertising materials mentioning features or use of this
     21  *    software must display the following acknowledgements:
     22  *      This product includes software developed by the Urbana-Champaign
     23  *      Independent Media Center.
     24  *	This product includes software developed by Garrett D'Amore.
     25  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26  *    D'Amore's name may not be used to endorse or promote products
     27  *    derived from this software without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42  */
     43 
     44 #ifndef	_DEV_SPI_SPIVAR_H_
     45 #define	_DEV_SPI_SPIVAR_H_
     46 
     47 #include <sys/queue.h>
     48 
     49 /*
     50  * Serial Peripheral Interface bus.  This is a 4-wire bus common for
     51  * connecting flash, clocks, sensors, and various other low-speed
     52  * peripherals.
     53  */
     54 
     55 struct spi_handle;
     56 struct spi_transfer;
     57 
     58 /*
     59  * De facto standard latching modes.
     60  */
     61 #define	SPI_MODE_0	0	/* CPOL = 0, CPHA = 0 */
     62 #define	SPI_MODE_1	1	/* CPOL = 0, CPHA = 1 */
     63 #define	SPI_MODE_2	2	/* CPOL = 1, CPHA = 0 */
     64 #define	SPI_MODE_3	3	/* CPOL = 1, CPHA = 1 */
     65 /* Philips' Microwire is just Mode 0 */
     66 #define	SPI_MODE_MICROWIRE	SPI_MODE_0
     67 
     68 struct spi_controller {
     69 	void	*sct_cookie;	/* controller private data */
     70 	int	sct_nslaves;
     71 	int	(*sct_configure)(void *, int, int, int);
     72 	int	(*sct_transfer)(void *, struct spi_transfer *);
     73 };
     74 
     75 int spibus_print(void *, const char *);
     76 
     77 /* one per chip select */
     78 struct spibus_attach_args {
     79 	struct spi_controller	*sba_controller;
     80 };
     81 
     82 struct spi_attach_args {
     83 	struct spi_handle	*sa_handle;
     84 
     85 	/* only set if using direct config */
     86 	const char	*sa_name;	/* name of the device */
     87 	const char	*sa_clist;	/* compatible strlist */
     88 	size_t		sa_clist_size;	/* size of compatible strlist */
     89 	devhandle_t	sa_devhandle;	/* device handle for the device */
     90 };
     91 
     92 /*
     93  * spi-enumerate-devices device call
     94  *
     95  *	Enumerates the devices connected to the SPI bus, filling out
     96  *	the spi_attach_args and invoking the callback for each one.
     97  *	If the callback returns true, then enumeration continues.  If
     98  *	the callback returns false, enumeration is stopped.
     99  */
    100 struct spi_enumerate_devices_args {
    101 	struct spi_attach_args *sa;
    102 	bool (*callback)(device_t, struct spi_enumerate_devices_args *);
    103 	int chip_select;
    104 };
    105 
    106 /*
    107  * This is similar in some respects to struct buf, but we cannot use
    108  * that structure because it was not designed to support full-duplex
    109  * IO.
    110  */
    111 struct spi_chunk {
    112 	struct spi_chunk *chunk_next;
    113 	int		chunk_count;
    114 	uint8_t		*chunk_read;
    115 	const uint8_t	*chunk_write;
    116 	/* for private use by framework and bus driver */
    117 	uint8_t		*chunk_rptr;
    118 	const uint8_t	*chunk_wptr;
    119 	int		chunk_rresid;
    120 	int		chunk_wresid;
    121 };
    122 
    123 struct spi_transfer {
    124 	struct spi_chunk *st_chunks;		/* chained bufs */
    125 	SIMPLEQ_ENTRY(spi_transfer) st_chain;	/* chain of submitted jobs */
    126 	volatile int	st_flags;
    127 	int		st_errno;
    128 	int		st_slave;
    129 	void		*st_private;
    130 	void		(*st_done)(struct spi_transfer *);
    131 	kmutex_t	st_lock;
    132 	kcondvar_t	st_cv;
    133 	void		*st_busprivate;
    134 	void		*st_spiprivate;
    135 };
    136 
    137 /* declare a list of transfers */
    138 SIMPLEQ_HEAD(spi_transq, spi_transfer);
    139 
    140 #define	spi_transq_init(q)	\
    141 	SIMPLEQ_INIT(q)
    142 
    143 #define	spi_transq_enqueue(q, trans)	\
    144 	SIMPLEQ_INSERT_TAIL(q, trans, st_chain)
    145 
    146 #define	spi_transq_dequeue(q)		\
    147 	SIMPLEQ_REMOVE_HEAD(q, st_chain)
    148 
    149 #define	spi_transq_first(q)		\
    150 	SIMPLEQ_FIRST(q)
    151 
    152 #define	SPI_F_DONE		0x0001
    153 #define	SPI_F_ERROR		0x0002
    154 
    155 int spi_compatible_match(const struct spi_attach_args *, const cfdata_t,
    156 			  const struct device_compatible_entry *);
    157 int spi_configure(struct spi_handle *, int, int);
    158 int spi_transfer(struct spi_handle *, struct spi_transfer *);
    159 void spi_transfer_init(struct spi_transfer *);
    160 void spi_chunk_init(struct spi_chunk *, int, const uint8_t *, uint8_t *);
    161 void spi_transfer_add(struct spi_transfer *, struct spi_chunk *);
    162 void spi_wait(struct spi_transfer *);
    163 void spi_done(struct spi_transfer *, int);
    164 
    165 /* convenience wrappers */
    166 int spi_send(struct spi_handle *, int, const uint8_t *);
    167 int spi_recv(struct spi_handle *, int, uint8_t *);
    168 int spi_send_recv(struct spi_handle *, int, const uint8_t *, int, uint8_t *);
    169 
    170 #endif	/* _DEV_SPI_SPIVAR_H_ */
    171