vp7045-fe.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* DVB frontend part of the Linux driver for TwinhanDTV Alpha/MagicBoxII USB2.0
  2. * DVB-T receiver.
  3. *
  4. * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
  5. *
  6. * Thanks to Twinhan who kindly provided hardware and information.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation, version 2.
  11. *
  12. * see Documentation/dvb/README.dvb-usb for more information
  13. *
  14. */
  15. #include "vp7045.h"
  16. /* It is a Zarlink MT352 within a Samsung Tuner (DNOS404ZH102A) - 040929 - AAT
  17. *
  18. * Programming is hidden inside the firmware, so set_frontend is very easy.
  19. * Even though there is a Firmware command that one can use to access the demod
  20. * via its registers. This is used for status information.
  21. */
  22. struct vp7045_fe_state {
  23. struct dvb_frontend fe;
  24. struct dvb_usb_device *d;
  25. };
  26. static int vp7045_fe_read_status(struct dvb_frontend* fe, fe_status_t *status)
  27. {
  28. struct vp7045_fe_state *state = fe->demodulator_priv;
  29. u8 s0 = vp7045_read_reg(state->d,0x00),
  30. s1 = vp7045_read_reg(state->d,0x01),
  31. s3 = vp7045_read_reg(state->d,0x03);
  32. *status = 0;
  33. if (s0 & (1 << 4))
  34. *status |= FE_HAS_CARRIER;
  35. if (s0 & (1 << 1))
  36. *status |= FE_HAS_VITERBI;
  37. if (s0 & (1 << 5))
  38. *status |= FE_HAS_LOCK;
  39. if (s1 & (1 << 1))
  40. *status |= FE_HAS_SYNC;
  41. if (s3 & (1 << 6))
  42. *status |= FE_HAS_SIGNAL;
  43. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  44. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  45. *status &= ~FE_HAS_LOCK;
  46. return 0;
  47. }
  48. static int vp7045_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  49. {
  50. struct vp7045_fe_state *state = fe->demodulator_priv;
  51. *ber = (vp7045_read_reg(state->d, 0x0D) << 16) |
  52. (vp7045_read_reg(state->d, 0x0E) << 8) |
  53. vp7045_read_reg(state->d, 0x0F);
  54. return 0;
  55. }
  56. static int vp7045_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  57. {
  58. struct vp7045_fe_state *state = fe->demodulator_priv;
  59. *unc = (vp7045_read_reg(state->d, 0x10) << 8) |
  60. vp7045_read_reg(state->d, 0x11);
  61. return 0;
  62. }
  63. static int vp7045_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  64. {
  65. struct vp7045_fe_state *state = fe->demodulator_priv;
  66. u16 signal = (vp7045_read_reg(state->d, 0x14) << 8) |
  67. vp7045_read_reg(state->d, 0x15);
  68. *strength = ~signal;
  69. return 0;
  70. }
  71. static int vp7045_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  72. {
  73. struct vp7045_fe_state *state = fe->demodulator_priv;
  74. u8 _snr = vp7045_read_reg(state->d, 0x09);
  75. *snr = (_snr << 8) | _snr;
  76. return 0;
  77. }
  78. static int vp7045_fe_init(struct dvb_frontend* fe)
  79. {
  80. return 0;
  81. }
  82. static int vp7045_fe_sleep(struct dvb_frontend* fe)
  83. {
  84. return 0;
  85. }
  86. static int vp7045_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  87. {
  88. tune->min_delay_ms = 800;
  89. return 0;
  90. }
  91. static int vp7045_fe_set_frontend(struct dvb_frontend* fe,
  92. struct dvb_frontend_parameters *fep)
  93. {
  94. struct vp7045_fe_state *state = fe->demodulator_priv;
  95. u8 buf[5];
  96. u32 freq = fep->frequency / 1000;
  97. buf[0] = (freq >> 16) & 0xff;
  98. buf[1] = (freq >> 8) & 0xff;
  99. buf[2] = freq & 0xff;
  100. buf[3] = 0;
  101. switch (fep->u.ofdm.bandwidth) {
  102. case BANDWIDTH_8_MHZ: buf[4] = 8; break;
  103. case BANDWIDTH_7_MHZ: buf[4] = 7; break;
  104. case BANDWIDTH_6_MHZ: buf[4] = 6; break;
  105. case BANDWIDTH_AUTO: return -EOPNOTSUPP;
  106. default:
  107. return -EINVAL;
  108. }
  109. vp7045_usb_op(state->d,LOCK_TUNER_COMMAND,buf,5,NULL,0,200);
  110. return 0;
  111. }
  112. static int vp7045_fe_get_frontend(struct dvb_frontend* fe,
  113. struct dvb_frontend_parameters *fep)
  114. {
  115. return 0;
  116. }
  117. static void vp7045_fe_release(struct dvb_frontend* fe)
  118. {
  119. struct vp7045_fe_state *state = fe->demodulator_priv;
  120. kfree(state);
  121. }
  122. static struct dvb_frontend_ops vp7045_fe_ops;
  123. struct dvb_frontend * vp7045_fe_attach(struct dvb_usb_device *d)
  124. {
  125. struct vp7045_fe_state *s = kzalloc(sizeof(struct vp7045_fe_state), GFP_KERNEL);
  126. if (s == NULL)
  127. goto error;
  128. s->d = d;
  129. memcpy(&s->fe.ops, &vp7045_fe_ops, sizeof(struct dvb_frontend_ops));
  130. s->fe.demodulator_priv = s;
  131. return &s->fe;
  132. error:
  133. return NULL;
  134. }
  135. static struct dvb_frontend_ops vp7045_fe_ops = {
  136. .info = {
  137. .name = "Twinhan VP7045/46 USB DVB-T",
  138. .type = FE_OFDM,
  139. .frequency_min = 44250000,
  140. .frequency_max = 867250000,
  141. .frequency_stepsize = 1000,
  142. .caps = FE_CAN_INVERSION_AUTO |
  143. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  144. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  145. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  146. FE_CAN_TRANSMISSION_MODE_AUTO |
  147. FE_CAN_GUARD_INTERVAL_AUTO |
  148. FE_CAN_RECOVER |
  149. FE_CAN_HIERARCHY_AUTO,
  150. },
  151. .release = vp7045_fe_release,
  152. .init = vp7045_fe_init,
  153. .sleep = vp7045_fe_sleep,
  154. .set_frontend = vp7045_fe_set_frontend,
  155. .get_frontend = vp7045_fe_get_frontend,
  156. .get_tune_settings = vp7045_fe_get_tune_settings,
  157. .read_status = vp7045_fe_read_status,
  158. .read_ber = vp7045_fe_read_ber,
  159. .read_signal_strength = vp7045_fe_read_signal_strength,
  160. .read_snr = vp7045_fe_read_snr,
  161. .read_ucblocks = vp7045_fe_read_unc_blocks,
  162. };