spi.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * (C) Copyright 2001
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef _SPI_H_
  8. #define _SPI_H_
  9. /* Controller-specific definitions: */
  10. /* SPI mode flags */
  11. #define SPI_CPHA 0x01 /* clock phase */
  12. #define SPI_CPOL 0x02 /* clock polarity */
  13. #define SPI_MODE_0 (0|0) /* (original MicroWire) */
  14. #define SPI_MODE_1 (0|SPI_CPHA)
  15. #define SPI_MODE_2 (SPI_CPOL|0)
  16. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  17. #define SPI_CS_HIGH 0x04 /* CS active high */
  18. #define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */
  19. #define SPI_3WIRE 0x10 /* SI/SO signals shared */
  20. #define SPI_LOOP 0x20 /* loopback mode */
  21. #define SPI_SLAVE 0x40 /* slave mode */
  22. #define SPI_PREAMBLE 0x80 /* Skip preamble bytes */
  23. /* SPI transfer flags */
  24. #define SPI_XFER_BEGIN 0x01 /* Assert CS before transfer */
  25. #define SPI_XFER_END 0x02 /* Deassert CS after transfer */
  26. /* Header byte that marks the start of the message */
  27. #define SPI_PREAMBLE_END_BYTE 0xec
  28. /*-----------------------------------------------------------------------
  29. * Representation of a SPI slave, i.e. what we're communicating with.
  30. *
  31. * Drivers are expected to extend this with controller-specific data.
  32. *
  33. * bus: ID of the bus that the slave is attached to.
  34. * cs: ID of the chip select connected to the slave.
  35. * max_write_size: If non-zero, the maximum number of bytes which can
  36. * be written at once, excluding command bytes.
  37. */
  38. struct spi_slave {
  39. unsigned int bus;
  40. unsigned int cs;
  41. unsigned int max_write_size;
  42. };
  43. /*-----------------------------------------------------------------------
  44. * Initialization, must be called once on start up.
  45. *
  46. * TODO: I don't think we really need this.
  47. */
  48. void spi_init(void);
  49. /**
  50. * spi_do_alloc_slave - Allocate a new SPI slave (internal)
  51. *
  52. * Allocate and zero all fields in the spi slave, and set the bus/chip
  53. * select. Use the helper macro spi_alloc_slave() to call this.
  54. *
  55. * @offset: Offset of struct spi_slave within slave structure
  56. * @size: Size of slave structure
  57. * @bus: Bus ID of the slave chip.
  58. * @cs: Chip select ID of the slave chip on the specified bus.
  59. */
  60. void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
  61. unsigned int cs);
  62. /**
  63. * spi_alloc_slave - Allocate a new SPI slave
  64. *
  65. * Allocate and zero all fields in the spi slave, and set the bus/chip
  66. * select.
  67. *
  68. * @_struct: Name of structure to allocate (e.g. struct tegra_spi). This
  69. * structure must contain a member 'struct spi_slave *slave'.
  70. * @bus: Bus ID of the slave chip.
  71. * @cs: Chip select ID of the slave chip on the specified bus.
  72. */
  73. #define spi_alloc_slave(_struct, bus, cs) \
  74. spi_do_alloc_slave(offsetof(_struct, slave), \
  75. sizeof(_struct), bus, cs)
  76. /**
  77. * spi_alloc_slave_base - Allocate a new SPI slave with no private data
  78. *
  79. * Allocate and zero all fields in the spi slave, and set the bus/chip
  80. * select.
  81. *
  82. * @bus: Bus ID of the slave chip.
  83. * @cs: Chip select ID of the slave chip on the specified bus.
  84. */
  85. #define spi_alloc_slave_base(bus, cs) \
  86. spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
  87. /*-----------------------------------------------------------------------
  88. * Set up communications parameters for a SPI slave.
  89. *
  90. * This must be called once for each slave. Note that this function
  91. * usually doesn't touch any actual hardware, it only initializes the
  92. * contents of spi_slave so that the hardware can be easily
  93. * initialized later.
  94. *
  95. * bus: Bus ID of the slave chip.
  96. * cs: Chip select ID of the slave chip on the specified bus.
  97. * max_hz: Maximum SCK rate in Hz.
  98. * mode: Clock polarity, clock phase and other parameters.
  99. *
  100. * Returns: A spi_slave reference that can be used in subsequent SPI
  101. * calls, or NULL if one or more of the parameters are not supported.
  102. */
  103. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  104. unsigned int max_hz, unsigned int mode);
  105. /*-----------------------------------------------------------------------
  106. * Free any memory associated with a SPI slave.
  107. *
  108. * slave: The SPI slave
  109. */
  110. void spi_free_slave(struct spi_slave *slave);
  111. /*-----------------------------------------------------------------------
  112. * Claim the bus and prepare it for communication with a given slave.
  113. *
  114. * This must be called before doing any transfers with a SPI slave. It
  115. * will enable and initialize any SPI hardware as necessary, and make
  116. * sure that the SCK line is in the correct idle state. It is not
  117. * allowed to claim the same bus for several slaves without releasing
  118. * the bus in between.
  119. *
  120. * slave: The SPI slave
  121. *
  122. * Returns: 0 if the bus was claimed successfully, or a negative value
  123. * if it wasn't.
  124. */
  125. int spi_claim_bus(struct spi_slave *slave);
  126. /*-----------------------------------------------------------------------
  127. * Release the SPI bus
  128. *
  129. * This must be called once for every call to spi_claim_bus() after
  130. * all transfers have finished. It may disable any SPI hardware as
  131. * appropriate.
  132. *
  133. * slave: The SPI slave
  134. */
  135. void spi_release_bus(struct spi_slave *slave);
  136. /*-----------------------------------------------------------------------
  137. * SPI transfer
  138. *
  139. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  140. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  141. *
  142. * The source of the outgoing bits is the "dout" parameter and the
  143. * destination of the input bits is the "din" parameter. Note that "dout"
  144. * and "din" can point to the same memory location, in which case the
  145. * input data overwrites the output data (since both are buffered by
  146. * temporary variables, this is OK).
  147. *
  148. * spi_xfer() interface:
  149. * slave: The SPI slave which will be sending/receiving the data.
  150. * bitlen: How many bits to write and read.
  151. * dout: Pointer to a string of bits to send out. The bits are
  152. * held in a byte array and are sent MSB first.
  153. * din: Pointer to a string of bits that will be filled in.
  154. * flags: A bitwise combination of SPI_XFER_* flags.
  155. *
  156. * Returns: 0 on success, not 0 on failure
  157. */
  158. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  159. void *din, unsigned long flags);
  160. /*-----------------------------------------------------------------------
  161. * Determine if a SPI chipselect is valid.
  162. * This function is provided by the board if the low-level SPI driver
  163. * needs it to determine if a given chipselect is actually valid.
  164. *
  165. * Returns: 1 if bus:cs identifies a valid chip on this board, 0
  166. * otherwise.
  167. */
  168. int spi_cs_is_valid(unsigned int bus, unsigned int cs);
  169. /*-----------------------------------------------------------------------
  170. * Activate a SPI chipselect.
  171. * This function is provided by the board code when using a driver
  172. * that can't control its chipselects automatically (e.g.
  173. * common/soft_spi.c). When called, it should activate the chip select
  174. * to the device identified by "slave".
  175. */
  176. void spi_cs_activate(struct spi_slave *slave);
  177. /*-----------------------------------------------------------------------
  178. * Deactivate a SPI chipselect.
  179. * This function is provided by the board code when using a driver
  180. * that can't control its chipselects automatically (e.g.
  181. * common/soft_spi.c). When called, it should deactivate the chip
  182. * select to the device identified by "slave".
  183. */
  184. void spi_cs_deactivate(struct spi_slave *slave);
  185. /*-----------------------------------------------------------------------
  186. * Set transfer speed.
  187. * This sets a new speed to be applied for next spi_xfer().
  188. * slave: The SPI slave
  189. * hz: The transfer speed
  190. */
  191. void spi_set_speed(struct spi_slave *slave, uint hz);
  192. /*-----------------------------------------------------------------------
  193. * Write 8 bits, then read 8 bits.
  194. * slave: The SPI slave we're communicating with
  195. * byte: Byte to be written
  196. *
  197. * Returns: The value that was read, or a negative value on error.
  198. *
  199. * TODO: This function probably shouldn't be inlined.
  200. */
  201. static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
  202. {
  203. unsigned char dout[2];
  204. unsigned char din[2];
  205. int ret;
  206. dout[0] = byte;
  207. dout[1] = 0;
  208. ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
  209. return ret < 0 ? ret : din[1];
  210. }
  211. /**
  212. * Set up a SPI slave for a particular device tree node
  213. *
  214. * This calls spi_setup_slave() with the correct bus number. Call
  215. * spi_free_slave() to free it later.
  216. *
  217. * @param blob Device tree blob
  218. * @param node SPI peripheral node to use
  219. * @param cs Chip select to use
  220. * @param max_hz Maximum SCK rate in Hz (0 for default)
  221. * @param mode Clock polarity, clock phase and other parameters
  222. * @return pointer to new spi_slave structure
  223. */
  224. struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
  225. unsigned int cs, unsigned int max_hz, unsigned int mode);
  226. #endif /* _SPI_H_ */