mtv23x_port.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. *
  3. * File name: mtv23x_port.c
  4. *
  5. * Description : User-supplied Routines for RAONTECH TV Services.
  6. *
  7. * Copyright (C) (2014, RAONTECH)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation version 2.
  12. *
  13. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  14. * kind, whether express or implied; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/spi/spi.h>
  20. #include "isdbt.h"
  21. #include "isdbt_port_mtv23x.h"
  22. /* Declares a variable of gurad object if neccessry. */
  23. #if defined(RTV_IF_SPI) || defined(RTV_IF_EBI2)
  24. #if defined(__KERNEL__)
  25. struct mutex raontv_guard;
  26. #elif defined(WINCE) || defined(WINDOWS) || defined(WIN32)
  27. CRITICAL_SECTION raontv_guard;
  28. #else
  29. /* non-OS and RTOS. */
  30. #endif
  31. #endif
  32. #if defined(RTV_IF_SPI) || defined(RTV_IF_SPI_TSIFx)
  33. #define SPI_CMD_SIZE MTV23X_SPI_CMD_SIZE
  34. #define SPI_SPEED_NORMAL_Hz 10000000//6000000//15000000 //12000000
  35. #define SPI_SPEED_HIGH_Hz 32000000//45000000 //12000000
  36. static struct spi_device *mtv_spi;
  37. void mtv23x_set_port_if(unsigned long interface)
  38. {
  39. mtv_spi = (struct spi_device *)interface;
  40. }
  41. unsigned char mtv23x_spi_read(unsigned char page, unsigned char reg)
  42. {
  43. int ret;
  44. u8 out_buf[4], in_buf[4];
  45. struct spi_message msg;
  46. struct spi_transfer msg_xfer = {
  47. .tx_buf = out_buf,
  48. .rx_buf = in_buf,
  49. .len = 4,
  50. .cs_change = 0,
  51. .delay_usecs = 0
  52. };
  53. #ifdef RTV_SPI_HIGH_SPEED_ENABLE
  54. if (g_bRtvSpiHighSpeed == TRUE)
  55. msg_xfer.speed_hz = SPI_SPEED_HIGH_Hz;
  56. else
  57. msg_xfer.speed_hz = SPI_SPEED_NORMAL_Hz;
  58. #endif
  59. out_buf[0] = 0x90 | page;
  60. out_buf[1] = reg;
  61. out_buf[2] = 1; /* Read size */
  62. spi_message_init(&msg);
  63. spi_message_add_tail(&msg_xfer, &msg);
  64. ret = spi_sync(mtv_spi, &msg);
  65. if (ret) {
  66. DMBERR("error: %d\n", ret);
  67. return 0xFF;
  68. }
  69. #if 0
  70. DMBMSG("0x%02X 0x%02X 0x%02X 0x%02X\n",
  71. in_buf[0], in_buf[1], in_buf[2], in_buf[3]);
  72. #endif
  73. return in_buf[SPI_CMD_SIZE];
  74. }
  75. void mtv23x_spi_read_burst(unsigned char page, unsigned char reg,
  76. unsigned char *buf, int size)
  77. {
  78. int ret;
  79. u8 out_buf[SPI_CMD_SIZE];
  80. struct spi_message msg;
  81. struct spi_transfer msg_xfer0 = {
  82. .tx_buf = out_buf,
  83. .rx_buf = buf,
  84. .len = SPI_CMD_SIZE,
  85. .cs_change = 0,
  86. .delay_usecs = 0
  87. };
  88. struct spi_transfer msg_xfer1 = {
  89. .tx_buf = buf,
  90. .rx_buf = buf,
  91. .len = size,
  92. .cs_change = 0,
  93. .delay_usecs = 0
  94. };
  95. #ifdef RTV_SPI_HIGH_SPEED_ENABLE
  96. if (g_bRtvSpiHighSpeed == TRUE)
  97. msg_xfer.speed_hz = SPI_SPEED_HIGH_Hz;
  98. else
  99. msg_xfer.speed_hz = SPI_SPEED_NORMAL_Hz;
  100. if (g_bRtvSpiHighSpeed == TRUE)
  101. msg_xfer1.speed_hz = SPI_SPEED_HIGH_Hz;
  102. else
  103. msg_xfer1.speed_hz = SPI_SPEED_NORMAL_Hz;
  104. #endif
  105. if (page > 15) { /* 0 ~ 15: not SPI memory */
  106. out_buf[0] = 0xA0; /* Memory read */
  107. out_buf[1] = 0x00;
  108. out_buf[2] = 188; /* Fix */
  109. } else {
  110. out_buf[0] = 0x90 | page; /* Register read */
  111. out_buf[1] = reg;
  112. out_buf[2] = size;
  113. }
  114. spi_message_init(&msg);
  115. spi_message_add_tail(&msg_xfer0, &msg);
  116. spi_message_add_tail(&msg_xfer1, &msg);
  117. ret = spi_sync(mtv_spi, &msg);
  118. if (ret)
  119. DMBERR("1 error: %d\n", ret);
  120. }
  121. void mtv23x_spi_write(unsigned char page, unsigned char reg, unsigned char val)
  122. {
  123. u8 out_buf[4];
  124. u8 in_buf[4];
  125. struct spi_message msg;
  126. struct spi_transfer msg_xfer = {
  127. .tx_buf = out_buf,
  128. .rx_buf = in_buf,
  129. .len = 4,
  130. .cs_change = 0,
  131. .delay_usecs = 0
  132. };
  133. int ret;
  134. #ifdef RTV_SPI_HIGH_SPEED_ENABLE
  135. if (g_bRtvSpiHighSpeed == TRUE)
  136. msg_xfer.speed_hz = SPI_SPEED_HIGH_Hz;
  137. else
  138. msg_xfer.speed_hz = SPI_SPEED_NORMAL_Hz;
  139. #endif
  140. out_buf[0] = 0x80 | page;
  141. out_buf[1] = reg;
  142. out_buf[2] = 1; /* size */
  143. out_buf[3] = val;
  144. spi_message_init(&msg);
  145. spi_message_add_tail(&msg_xfer, &msg);
  146. ret = spi_sync(mtv_spi, &msg);
  147. if (ret)
  148. DMBERR("error: %d\n", ret);
  149. }
  150. #endif /* #if defined(RTV_IF_SPI) */