1*53ee8cc1Swenshuai.xi /* 2*53ee8cc1Swenshuai.xi * include/linux/spi/spidev.h 3*53ee8cc1Swenshuai.xi * 4*53ee8cc1Swenshuai.xi * Copyright (C) 2006 SWAPP 5*53ee8cc1Swenshuai.xi * Andrea Paterniani <a.paterniani@swapp-eng.it> 6*53ee8cc1Swenshuai.xi * 7*53ee8cc1Swenshuai.xi * This program is free software; you can redistribute it and/or modify 8*53ee8cc1Swenshuai.xi * it under the terms of the GNU General Public License as published by 9*53ee8cc1Swenshuai.xi * the Free Software Foundation; either version 2 of the License, or 10*53ee8cc1Swenshuai.xi * (at your option) any later version. 11*53ee8cc1Swenshuai.xi * 12*53ee8cc1Swenshuai.xi * This program is distributed in the hope that it will be useful, 13*53ee8cc1Swenshuai.xi * but WITHOUT ANY WARRANTY; without even the implied warranty of 14*53ee8cc1Swenshuai.xi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*53ee8cc1Swenshuai.xi * GNU General Public License for more details. 16*53ee8cc1Swenshuai.xi * 17*53ee8cc1Swenshuai.xi * You should have received a copy of the GNU General Public License 18*53ee8cc1Swenshuai.xi * along with this program; if not, write to the Free Software 19*53ee8cc1Swenshuai.xi * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20*53ee8cc1Swenshuai.xi */ 21*53ee8cc1Swenshuai.xi 22*53ee8cc1Swenshuai.xi #ifndef SPIDEV_H 23*53ee8cc1Swenshuai.xi #define SPIDEV_H 24*53ee8cc1Swenshuai.xi 25*53ee8cc1Swenshuai.xi 26*53ee8cc1Swenshuai.xi /* User space versions of kernel symbols for SPI clocking modes, 27*53ee8cc1Swenshuai.xi * matching <linux/spi/spi.h> 28*53ee8cc1Swenshuai.xi */ 29*53ee8cc1Swenshuai.xi 30*53ee8cc1Swenshuai.xi #define SPI_CPHA 0x01 31*53ee8cc1Swenshuai.xi #define SPI_CPOL 0x02 32*53ee8cc1Swenshuai.xi 33*53ee8cc1Swenshuai.xi #define SPI_MODE_0 (0|0) 34*53ee8cc1Swenshuai.xi #define SPI_MODE_1 (0|SPI_CPHA) 35*53ee8cc1Swenshuai.xi #define SPI_MODE_2 (SPI_CPOL|0) 36*53ee8cc1Swenshuai.xi #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 37*53ee8cc1Swenshuai.xi 38*53ee8cc1Swenshuai.xi #define SPI_CS_HIGH 0x04 39*53ee8cc1Swenshuai.xi #define SPI_LSB_FIRST 0x08 40*53ee8cc1Swenshuai.xi #define SPI_3WIRE 0x10 41*53ee8cc1Swenshuai.xi #define SPI_LOOP 0x20 42*53ee8cc1Swenshuai.xi 43*53ee8cc1Swenshuai.xi /*---------------------------------------------------------------------------*/ 44*53ee8cc1Swenshuai.xi 45*53ee8cc1Swenshuai.xi /* IOCTL commands */ 46*53ee8cc1Swenshuai.xi 47*53ee8cc1Swenshuai.xi #define SPI_IOC_MAGIC 'k' 48*53ee8cc1Swenshuai.xi 49*53ee8cc1Swenshuai.xi /** 50*53ee8cc1Swenshuai.xi * struct spi_ioc_transfer - describes a single SPI transfer 51*53ee8cc1Swenshuai.xi * @tx_buf: Holds pointer to userspace buffer with transmit data, or null. 52*53ee8cc1Swenshuai.xi * If no data is provided, zeroes are shifted out. 53*53ee8cc1Swenshuai.xi * @rx_buf: Holds pointer to userspace buffer for receive data, or null. 54*53ee8cc1Swenshuai.xi * @len: Length of tx and rx buffers, in bytes. 55*53ee8cc1Swenshuai.xi * @speed_hz: Temporary override of the device's bitrate. 56*53ee8cc1Swenshuai.xi * @bits_per_word: Temporary override of the device's wordsize. 57*53ee8cc1Swenshuai.xi * @delay_usecs: If nonzero, how long to delay after the last bit transfer 58*53ee8cc1Swenshuai.xi * before optionally deselecting the device before the next transfer. 59*53ee8cc1Swenshuai.xi * @cs_change: True to deselect device before starting the next transfer. 60*53ee8cc1Swenshuai.xi * 61*53ee8cc1Swenshuai.xi * This structure is mapped directly to the kernel spi_transfer structure; 62*53ee8cc1Swenshuai.xi * the fields have the same meanings, except of course that the pointers 63*53ee8cc1Swenshuai.xi * are in a different address space (and may be of different sizes in some 64*53ee8cc1Swenshuai.xi * cases, such as 32-bit i386 userspace over a 64-bit x86_64 kernel). 65*53ee8cc1Swenshuai.xi * Zero-initialize the structure, including currently unused fields, to 66*53ee8cc1Swenshuai.xi * accomodate potential future updates. 67*53ee8cc1Swenshuai.xi * 68*53ee8cc1Swenshuai.xi * SPI_IOC_MESSAGE gives userspace the equivalent of kernel spi_sync(). 69*53ee8cc1Swenshuai.xi * Pass it an array of related transfers, they'll execute together. 70*53ee8cc1Swenshuai.xi * Each transfer may be half duplex (either direction) or full duplex. 71*53ee8cc1Swenshuai.xi * 72*53ee8cc1Swenshuai.xi * struct spi_ioc_transfer mesg[4]; 73*53ee8cc1Swenshuai.xi * ... 74*53ee8cc1Swenshuai.xi * status = ioctl(fd, SPI_IOC_MESSAGE(4), mesg); 75*53ee8cc1Swenshuai.xi * 76*53ee8cc1Swenshuai.xi * So for example one transfer might send a nine bit command (right aligned 77*53ee8cc1Swenshuai.xi * in a 16-bit word), the next could read a block of 8-bit data before 78*53ee8cc1Swenshuai.xi * terminating that command by temporarily deselecting the chip; the next 79*53ee8cc1Swenshuai.xi * could send a different nine bit command (re-selecting the chip), and the 80*53ee8cc1Swenshuai.xi * last transfer might write some register values. 81*53ee8cc1Swenshuai.xi */ 82*53ee8cc1Swenshuai.xi struct spi_ioc_transfer { 83*53ee8cc1Swenshuai.xi __u64 tx_buf; 84*53ee8cc1Swenshuai.xi __u64 rx_buf; 85*53ee8cc1Swenshuai.xi 86*53ee8cc1Swenshuai.xi __u32 len; 87*53ee8cc1Swenshuai.xi __u32 speed_hz; 88*53ee8cc1Swenshuai.xi 89*53ee8cc1Swenshuai.xi __u16 delay_usecs; 90*53ee8cc1Swenshuai.xi __u8 bits_per_word; 91*53ee8cc1Swenshuai.xi __u8 cs_change; 92*53ee8cc1Swenshuai.xi __u32 pad; 93*53ee8cc1Swenshuai.xi 94*53ee8cc1Swenshuai.xi /* If the contents of 'struct spi_ioc_transfer' ever change 95*53ee8cc1Swenshuai.xi * incompatibly, then the ioctl number (currently 0) must change; 96*53ee8cc1Swenshuai.xi * ioctls with constant size fields get a bit more in the way of 97*53ee8cc1Swenshuai.xi * error checking than ones (like this) where that field varies. 98*53ee8cc1Swenshuai.xi * 99*53ee8cc1Swenshuai.xi * NOTE: struct layout is the same in 64bit and 32bit userspace. 100*53ee8cc1Swenshuai.xi */ 101*53ee8cc1Swenshuai.xi }; 102*53ee8cc1Swenshuai.xi 103*53ee8cc1Swenshuai.xi /* not all platforms use <asm-generic/ioctl.h> or _IOC_TYPECHECK() ... */ 104*53ee8cc1Swenshuai.xi #define SPI_MSGSIZE(N) \ 105*53ee8cc1Swenshuai.xi ((((N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \ 106*53ee8cc1Swenshuai.xi ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0) 107*53ee8cc1Swenshuai.xi #define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)]) 108*53ee8cc1Swenshuai.xi 109*53ee8cc1Swenshuai.xi 110*53ee8cc1Swenshuai.xi /* Read / Write of SPI mode (SPI_MODE_0..SPI_MODE_3) */ 111*53ee8cc1Swenshuai.xi #define SPI_IOC_RD_MODE _IOR(SPI_IOC_MAGIC, 1, __u8) 112*53ee8cc1Swenshuai.xi #define SPI_IOC_WR_MODE _IOW(SPI_IOC_MAGIC, 1, __u8) 113*53ee8cc1Swenshuai.xi 114*53ee8cc1Swenshuai.xi /* Read / Write SPI bit justification */ 115*53ee8cc1Swenshuai.xi #define SPI_IOC_RD_LSB_FIRST _IOR(SPI_IOC_MAGIC, 2, __u8) 116*53ee8cc1Swenshuai.xi #define SPI_IOC_WR_LSB_FIRST _IOW(SPI_IOC_MAGIC, 2, __u8) 117*53ee8cc1Swenshuai.xi 118*53ee8cc1Swenshuai.xi /* Read / Write SPI device word length (1..N) */ 119*53ee8cc1Swenshuai.xi #define SPI_IOC_RD_BITS_PER_WORD _IOR(SPI_IOC_MAGIC, 3, __u8) 120*53ee8cc1Swenshuai.xi #define SPI_IOC_WR_BITS_PER_WORD _IOW(SPI_IOC_MAGIC, 3, __u8) 121*53ee8cc1Swenshuai.xi 122*53ee8cc1Swenshuai.xi /* Read / Write SPI device default max speed hz */ 123*53ee8cc1Swenshuai.xi #define SPI_IOC_RD_MAX_SPEED_HZ _IOR(SPI_IOC_MAGIC, 4, __u32) 124*53ee8cc1Swenshuai.xi #define SPI_IOC_WR_MAX_SPEED_HZ _IOW(SPI_IOC_MAGIC, 4, __u32) 125*53ee8cc1Swenshuai.xi 126*53ee8cc1Swenshuai.xi 127*53ee8cc1Swenshuai.xi 128*53ee8cc1Swenshuai.xi #endif /* SPIDEV_H */ 129