spi.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Common SPI Interface: Controller-specific definitions
  3. *
  4. * (C) Copyright 2001
  5. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #ifndef _SPI_H_
  10. #define _SPI_H_
  11. #include <comdef.h>
  12. #define SPI_DATEMODE_32 32
  13. #define SPI_DATAMODE_16 16
  14. #define SPI_DATAMODE_8 8
  15. /* SPI mode flags */
  16. #define SPI_CPHA 0x01 /* clock phase */
  17. #define SPI_CPOL 0x02 /* clock polarity */
  18. #define SPI_MODE_0 (0|0) /* (original MicroWire) */
  19. #define SPI_MODE_1 (0|SPI_CPHA)
  20. #define SPI_MODE_2 (SPI_CPOL|0)
  21. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  22. #define SPI_CS_HIGH 0x04 /* CS active high */
  23. #define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */
  24. #define SPI_3WIRE 0x10 /* SI/SO signals shared */
  25. #define SPI_LOOP 0x20 /* loopback mode */
  26. #define SPI_SLAVE 0x40 /* slave mode */
  27. #define SPI_PREAMBLE 0x80 /* Skip preamble bytes */
  28. /* SPI transfer flags */
  29. #define SPI_XFER_BEGIN 0x01 /* Assert CS before transfer */
  30. #define SPI_XFER_END 0x02 /* Deassert CS after transfer */
  31. #define SPI_XFER_MMAP 0x08 /* Memory Mapped start */
  32. #define SPI_XFER_MMAP_END 0x10 /* Memory Mapped End */
  33. #define SPI_XFER_ONCE (SPI_XFER_BEGIN | SPI_XFER_END)
  34. #define SPI_XFER_U_PAGE (1 << 5)
  35. /* SPI TX operation modes */
  36. #define SPI_OPM_TX_QPP 1 << 0
  37. /* SPI RX operation modes */
  38. #define SPI_OPM_RX_AS 1 << 0
  39. #define SPI_OPM_RX_DOUT 1 << 1
  40. #define SPI_OPM_RX_DIO 1 << 2
  41. #define SPI_OPM_RX_QOF 1 << 3
  42. #define SPI_OPM_RX_QIOF 1 << 4
  43. #define SPI_OPM_RX_EXTN SPI_OPM_RX_AS | SPI_OPM_RX_DOUT | \
  44. SPI_OPM_RX_DIO | SPI_OPM_RX_QOF | \
  45. SPI_OPM_RX_QIOF
  46. /* SPI bus connection options */
  47. #define SPI_CONN_DUAL_SHARED 1 << 0
  48. #define SPI_CONN_DUAL_SEPARATED 1 << 1
  49. /* Header byte that marks the start of the message */
  50. #define SPI_PREAMBLE_END_BYTE 0xec
  51. #define SPI_DEFAULT_WORDLEN 8
  52. /*-----------------------------------------------------------------------
  53. * Representation of a SPI slave, i.e. what we're communicating with.
  54. *
  55. * Drivers are expected to extend this with controller-specific data.
  56. *
  57. * bus: ID of the bus that the slave is attached to.
  58. * cs: ID of the chip select connected to the slave.
  59. */
  60. struct spi_slave {
  61. unsigned int bus;
  62. unsigned int bus_width;
  63. #define NOT_SWAP_MODE 0
  64. #define SWAP_MODE 1
  65. unsigned int bus_order;
  66. unsigned int cs;
  67. };
  68. struct spi_operation{
  69. struct spi_slave* (*setup_slave)(unsigned int bus, unsigned int cs,
  70. unsigned int max_hz, u32 mode, u32 fifo_width);
  71. int (*spi_xfer)(struct spi_slave *slave, unsigned int bitlen, void *dout,
  72. void *din, unsigned long flags,int bit_mode);
  73. };
  74. /**
  75. * Initialization, must be called once on start up.
  76. *
  77. */
  78. void spi_init(void);
  79. /**
  80. * spi_do_alloc_slave - Allocate a new SPI slave (internal)
  81. *
  82. * Allocate and zero all fields in the spi slave, and set the bus/chip
  83. * select. Use the helper macro spi_alloc_slave() to call this.
  84. *
  85. * @offset: Offset of struct spi_slave within slave structure.
  86. * @size: Size of slave structure.
  87. * @bus: Bus ID of the slave chip.
  88. * @cs: Chip select ID of the slave chip on the specified bus.
  89. */
  90. void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
  91. unsigned int cs);
  92. /**
  93. * spi_alloc_slave - Allocate a new SPI slave
  94. *
  95. * Allocate and zero all fields in the spi slave, and set the bus/chip
  96. * select.
  97. *
  98. * @_struct: Name of structure to allocate (e.g. struct tegra_spi).
  99. * This structure must contain a member 'struct spi_slave *slave'.
  100. * @bus: Bus ID of the slave chip.
  101. * @cs: Chip select ID of the slave chip on the specified bus.
  102. */
  103. #define spi_alloc_slave(_struct, bus, cs) \
  104. spi_do_alloc_slave(offsetof(_struct, slave), \
  105. sizeof(_struct), bus, cs)
  106. /**
  107. * spi_alloc_slave_base - Allocate a new SPI slave with no private data
  108. *
  109. * Allocate and zero all fields in the spi slave, and set the bus/chip
  110. * select.
  111. *
  112. * @bus: Bus ID of the slave chip.
  113. * @cs: Chip select ID of the slave chip on the specified bus.
  114. */
  115. #define spi_alloc_slave_base(bus, cs) \
  116. spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
  117. int spi_register(unsigned int bus, struct spi_operation *operation);
  118. /**
  119. * Set up communications parameters for a SPI slave.
  120. *
  121. * This must be called once for each slave. Note that this function
  122. * usually doesn't touch any actual hardware, it only initializes the
  123. * contents of spi_slave so that the hardware can be easily
  124. * initialized later.
  125. *
  126. * @bus: Bus ID of the slave chip.
  127. * @cs: Chip select ID of the slave chip on the specified bus.
  128. * @max_hz: Maximum SCK rate in Hz.
  129. * @mode: Clock polarity, clock phase and other parameters.
  130. *
  131. * Returns: A spi_slave reference that can be used in subsequent SPI
  132. * calls, or NULL if one or more of the parameters are not supported.
  133. */
  134. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  135. unsigned int max_hz, unsigned int mode, unsigned int bus_width);
  136. /**
  137. * Free any memory associated with a SPI slave.
  138. *
  139. * @slave: The SPI slave
  140. */
  141. /**
  142. * SPI transfer
  143. *
  144. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  145. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  146. *
  147. * The source of the outgoing bits is the "dout" parameter and the
  148. * destination of the input bits is the "din" parameter. Note that "dout"
  149. * and "din" can point to the same memory location, in which case the
  150. * input data overwrites the output data (since both are buffered by
  151. * temporary variables, this is OK).
  152. *
  153. * spi_xfer() interface:
  154. * @slave: The SPI slave which will be sending/receiving the data.
  155. * @bitlen: How many bits to write and read.
  156. * @dout: Pointer to a string of bits to send out. The bits are
  157. * held in a byte array and are sent MSB first.
  158. * @din: Pointer to a string of bits that will be filled in.
  159. * @flags: A bitwise combination of SPI_XFER_* flags.
  160. *
  161. * Returns: 0 on success, not 0 on failure
  162. */
  163. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, void *dout,
  164. void *din, unsigned long flags, int bit_mode);
  165. #endif /* _SPI_H_ */