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