spidev.h 4.9 KB

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