fci_tun.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*****************************************************************************
  2. Copyright(c) 2013 FCI Inc. All Rights Reserved
  3. File name : tuner.c
  4. Description : tuner control driver source file
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. History :
  17. ----------------------------------------------------------------------
  18. *******************************************************************************/
  19. #include <linux/kernel.h>
  20. #include "fci_types.h"
  21. #include "fci_tun.h"
  22. #include "fci_hpi.h"
  23. #include "fci_hal.h"
  24. #include "fc8080_bb.h"
  25. #include "fc8080_tun.h"
  26. #include "fc8080_regs.h"
  27. #if (FC8080_FREQ_XTAL == 16000)
  28. #define CLOCK_RATIO 34359738 /* 33554432 * ((float) 1.024) */
  29. #elif (FC8080_FREQ_XTAL == 16384)
  30. #define CLOCK_RATIO 33554432 /* 33554432 * ((float) 1.) */
  31. #elif (FC8080_FREQ_XTAL == 19200)
  32. #define CLOCK_RATIO 28631996 /* 33554432 * ((float) 0.8533) */
  33. #elif (FC8080_FREQ_XTAL == 24000)
  34. #define CLOCK_RATIO 22347251 /* 33554432 * ((float) 0.666) */
  35. #elif (FC8080_FREQ_XTAL == 24576)
  36. #define CLOCK_RATIO 33554432 /* 33554432 * ((float) 1.) */
  37. #elif (FC8080_FREQ_XTAL == 26000)
  38. #define CLOCK_RATIO 31715649 /* 33554432 * ((float) 0.9452) */
  39. #elif (FC8080_FREQ_XTAL == 27000)
  40. #define CLOCK_RATIO 30541244 /* 33554432 * ((float) 0.9102) */
  41. #elif (FC8080_FREQ_XTAL == 27120)
  42. #define CLOCK_RATIO 30403670 /* 33554432 * ((float) 0.9061) */
  43. #elif (FC8080_FREQ_XTAL == 32000)
  44. #define CLOCK_RATIO 22347251 /* 33554432 * ((float) 0.666) */
  45. #elif (FC8080_FREQ_XTAL == 38400)
  46. #define CLOCK_RATIO 28631996 /* 33554432 * ((float) 0.8533) */
  47. #endif
  48. struct tuner_i2c_driver {
  49. s32 (*init)(HANDLE handle, s32 speed, s32 slaveaddr);
  50. s32 (*read)(HANDLE handle, u8 chip, u8 addr , u8 addr_len, u8 *data,
  51. u8 len);
  52. s32 (*write)(HANDLE handle, u8 chip, u8 addr , u8 addr_len, u8 *data,
  53. u8 len);
  54. s32 (*deinit)(HANDLE handle);
  55. };
  56. struct tuner_driver {
  57. s32 (*init)(HANDLE handle, enum band_type band);
  58. s32 (*set_freq)(HANDLE handle, enum band_type band, u32 freq);
  59. s32 (*get_rssi)(HANDLE handle, s32 *rssi);
  60. s32 (*deinit)(HANDLE handle);
  61. };
  62. static struct tuner_i2c_driver fcihpi = {
  63. &fci_hpi_init,
  64. &fci_hpi_read,
  65. &fci_hpi_write,
  66. &fci_hpi_deinit
  67. };
  68. static struct tuner_driver fc8080_tuner = {
  69. &fc8080_tuner_init,
  70. &fc8080_set_freq,
  71. &fc8080_get_rssi,
  72. &fc8080_tuner_deinit
  73. };
  74. static struct tuner_i2c_driver *tuner_ctrl = &fcihpi;
  75. static struct tuner_driver *tuner = &fc8080_tuner;
  76. static enum band_type tuner_band = BAND3_TYPE;
  77. u8 tuner_addr = 0x00;
  78. s32 tuner_ctrl_select(HANDLE handle, enum i2c_type type)
  79. {
  80. switch (type) {
  81. case FCI_HPI_TYPE:
  82. tuner_ctrl = &fcihpi;
  83. break;
  84. default:
  85. return BBM_E_TN_CTRL_SELECT;
  86. }
  87. if (tuner_ctrl->init(handle, 400, 0))
  88. return BBM_E_TN_CTRL_INIT;
  89. return BBM_OK;
  90. }
  91. s32 tuner_i2c_read(HANDLE handle, u8 addr, u8 addr_len, u8 *data, u8 len)
  92. {
  93. if (tuner_ctrl->read(handle, tuner_addr, addr, addr_len, data, len))
  94. return BBM_E_TN_READ;
  95. return BBM_OK;
  96. }
  97. s32 tuner_i2c_write(HANDLE handle, u8 addr, u8 addr_len, u8 *data, u8 len)
  98. {
  99. if (tuner_ctrl->write(handle, tuner_addr, addr, addr_len, data, len))
  100. return BBM_E_TN_WRITE;
  101. return BBM_OK;
  102. }
  103. s32 tuner_set_freq(HANDLE handle, u32 freq)
  104. {
  105. s32 res = BBM_NOK;
  106. u8 tmp;
  107. #ifdef FC8080_I2C
  108. u16 buf_en = 0;
  109. #endif
  110. if (tuner == NULL) {
  111. printk(KERN_DEBUG "TDMB : TUNER NULL ERROR\n");
  112. return BBM_NOK;
  113. }
  114. #ifdef FC8080_I2C
  115. bbm_word_read(handle, BBM_BUF_ENABLE, &buf_en);
  116. bbm_word_write(handle, BBM_BUF_ENABLE, buf_en & 0x0ff);
  117. #endif
  118. res = tuner->set_freq(handle, tuner_band, freq);
  119. if (res != BBM_OK) {
  120. printk(KERN_DEBUG "TDMB : TUNER res ERROR\n");
  121. return BBM_NOK;
  122. }
  123. tmp = (u8) (CLOCK_RATIO / freq);
  124. bbm_write(handle, BBM_INV_CARRIER_FREQ, tmp);
  125. fc8080_reset(handle);
  126. #ifdef FC8080_I2C
  127. bbm_word_write(handle, BBM_BUF_ENABLE, buf_en);
  128. #endif
  129. return res;
  130. }
  131. s32 tuner_select(HANDLE handle, enum product_type product, enum band_type band)
  132. {
  133. switch (product) {
  134. case FC8080_TUNER:
  135. tuner = &fc8080_tuner;
  136. tuner_band = (enum band_type) band;
  137. tuner_addr = 0x00;
  138. tuner_ctrl_select(handle, FCI_HPI_TYPE);
  139. bbm_write(handle, BBM_QDD_COMMAND, 0xc4);
  140. break;
  141. default:
  142. return BBM_E_TN_SELECT;
  143. }
  144. if (tuner->init == NULL)
  145. return BBM_E_TN_SELECT;
  146. if (tuner->init(handle, tuner_band))
  147. return BBM_E_TN_INIT;
  148. return BBM_OK;
  149. }
  150. s32 tuner_get_rssi(HANDLE handle, s32 *rssi)
  151. {
  152. if (tuner->get_rssi(handle, rssi))
  153. return BBM_E_TN_RSSI;
  154. return BBM_OK;
  155. }