spidev.h 4.6 KB

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