kspi.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include<linux/string.h>
  2. #include "defines.h"
  3. #include "kspi.h"
  4. #include "kfile.h"
  5. #include "ktiva.h"
  6. /////////////////////////////////////////////////////////////////////////////
  7. /////////////////////////////////////////////////////////////////////////////
  8. int kspi_write_mode(struct file *pf, unsigned char mode)
  9. {
  10. int ret;
  11. if((ret = kf_ioctl(pf, SPI_IOC_WR_MODE, (unsigned long)&mode)) < 0)
  12. {
  13. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  14. }
  15. return ret;
  16. }
  17. /////////////////////////////////////////////////////////////////////////////
  18. int kspi_read_mode(struct file *pf, unsigned char *mode)
  19. {
  20. int ret;
  21. if((ret = kf_ioctl(pf, SPI_IOC_RD_MODE, (unsigned long)mode)) < 0)
  22. {
  23. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  24. }
  25. return ret;
  26. }
  27. /////////////////////////////////////////////////////////////////////////////
  28. int kspi_write_bits_per_word(struct file *pf, unsigned char bits)
  29. {
  30. int ret;
  31. if((ret = kf_ioctl(pf, SPI_IOC_WR_BITS_PER_WORD, (unsigned long)&bits)) < 0)
  32. {
  33. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  34. }
  35. return ret;
  36. }
  37. /////////////////////////////////////////////////////////////////////////////
  38. int kspi_read_bits_per_word(struct file *pf, unsigned char *bits)
  39. {
  40. int ret;
  41. if((ret = kf_ioctl(pf, SPI_IOC_RD_BITS_PER_WORD, (unsigned long)bits)) < 0)
  42. {
  43. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  44. }
  45. return ret;
  46. }
  47. /////////////////////////////////////////////////////////////////////////////
  48. int kspi_write_max_speed_hz(struct file *pf, unsigned int speed)
  49. {
  50. int ret;
  51. if((ret = kf_ioctl(pf, SPI_IOC_WR_MAX_SPEED_HZ, (unsigned long)&speed)) < 0)
  52. {
  53. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  54. }
  55. return ret;
  56. }
  57. /////////////////////////////////////////////////////////////////////////////
  58. int kspi_read_max_speed_hz(struct file *pf, unsigned int *speed)
  59. {
  60. int ret;
  61. if((ret = kf_ioctl(pf, SPI_IOC_RD_MAX_SPEED_HZ, (unsigned long)speed)) < 0)
  62. {
  63. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  64. }
  65. return ret;
  66. }
  67. /////////////////////////////////////////////////////////////////////////////
  68. /////////////////////////////////////////////////////////////////////////////
  69. /////////////////////////////////////////////////////////////////////////////
  70. int kspi_tx(struct file *pf, const void *pData, size_t nCbData)
  71. {
  72. int ret;
  73. struct spi_ioc_transfer tr =
  74. {
  75. .tx_buf = (unsigned long)pData,
  76. .rx_buf = 0,
  77. .len = nCbData,
  78. .delay_usecs = 0,
  79. .speed_hz = _SPI_SPEED_HZ,
  80. .bits_per_word = _SPI_BITS_PER_WORD,
  81. };
  82. if((ret = kf_ioctl(pf, SPI_IOC_MESSAGE(1), (unsigned long)&tr)) < 0)
  83. KALERT("%s failed: %d\n", __FUNCTION__, ret);
  84. return ret;
  85. }
  86. /////////////////////////////////////////////////////////////////////////////
  87. int kspi_rx(struct file *pf, void *pData, size_t nCbData)
  88. {
  89. int ret;
  90. struct spi_ioc_transfer tr =
  91. {
  92. .tx_buf = 0,
  93. .rx_buf = (unsigned long)pData,
  94. .len = nCbData,
  95. .delay_usecs = 0,
  96. .speed_hz = _SPI_SPEED_HZ,
  97. .bits_per_word = _SPI_BITS_PER_WORD,
  98. };
  99. if((ret = kf_ioctl(pf, SPI_IOC_MESSAGE(1), (unsigned long)&tr)) < 0)
  100. KALERT("%s failed: %d\n", __FUNCTION__, ret);
  101. return ret;
  102. }
  103. /////////////////////////////////////////////////////////////////////////////
  104. int kspi_tx_rx(struct file *pf, const void *pTx, void *pRx, size_t nCb)
  105. {
  106. int ret;
  107. struct spi_ioc_transfer tr =
  108. {
  109. .tx_buf = (unsigned long)pTx,
  110. .rx_buf = (unsigned long)pRx,
  111. .len = nCb,
  112. .delay_usecs = 0,
  113. .speed_hz = _SPI_SPEED_HZ,
  114. .bits_per_word = _SPI_BITS_PER_WORD,
  115. };
  116. if((ret = kf_ioctl(pf, SPI_IOC_MESSAGE(1), (unsigned long)&tr)) < 0)
  117. KALERT("%s failed: %d\n", __FUNCTION__, ret);
  118. return ret;
  119. }
  120. /////////////////////////////////////////////////////////////////////////////
  121. int kspi_rx_byte(struct file *pf, unsigned char *rx)
  122. {
  123. return kspi_rx(pf, rx, 1);
  124. }
  125. /////////////////////////////////////////////////////////////////////////////
  126. int kspi_tx_byte(struct file *pf, unsigned char tx)
  127. {
  128. return kspi_tx(pf, &tx, 1);
  129. }
  130. /////////////////////////////////////////////////////////////////////////////
  131. int kspi_tx_rx_byte(struct file *pf, unsigned char tx, unsigned char *rx)
  132. {
  133. return kspi_tx_rx(pf, &tx, rx, 1);
  134. }