vp7045-fe.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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,
  27. enum fe_status *status)
  28. {
  29. struct vp7045_fe_state *state = fe->demodulator_priv;
  30. u8 s0 = vp7045_read_reg(state->d,0x00),
  31. s1 = vp7045_read_reg(state->d,0x01),
  32. s3 = vp7045_read_reg(state->d,0x03);
  33. *status = 0;
  34. if (s0 & (1 << 4))
  35. *status |= FE_HAS_CARRIER;
  36. if (s0 & (1 << 1))
  37. *status |= FE_HAS_VITERBI;
  38. if (s0 & (1 << 5))
  39. *status |= FE_HAS_LOCK;
  40. if (s1 & (1 << 1))
  41. *status |= FE_HAS_SYNC;
  42. if (s3 & (1 << 6))
  43. *status |= FE_HAS_SIGNAL;
  44. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  45. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  46. *status &= ~FE_HAS_LOCK;
  47. return 0;
  48. }
  49. static int vp7045_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  50. {
  51. struct vp7045_fe_state *state = fe->demodulator_priv;
  52. *ber = (vp7045_read_reg(state->d, 0x0D) << 16) |
  53. (vp7045_read_reg(state->d, 0x0E) << 8) |
  54. vp7045_read_reg(state->d, 0x0F);
  55. return 0;
  56. }
  57. static int vp7045_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  58. {
  59. struct vp7045_fe_state *state = fe->demodulator_priv;
  60. *unc = (vp7045_read_reg(state->d, 0x10) << 8) |
  61. vp7045_read_reg(state->d, 0x11);
  62. return 0;
  63. }
  64. static int vp7045_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  65. {
  66. struct vp7045_fe_state *state = fe->demodulator_priv;
  67. u16 signal = (vp7045_read_reg(state->d, 0x14) << 8) |
  68. vp7045_read_reg(state->d, 0x15);
  69. *strength = ~signal;
  70. return 0;
  71. }
  72. static int vp7045_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  73. {
  74. struct vp7045_fe_state *state = fe->demodulator_priv;
  75. u8 _snr = vp7045_read_reg(state->d, 0x09);
  76. *snr = (_snr << 8) | _snr;
  77. return 0;
  78. }
  79. static int vp7045_fe_init(struct dvb_frontend* fe)
  80. {
  81. return 0;
  82. }
  83. static int vp7045_fe_sleep(struct dvb_frontend* fe)
  84. {
  85. return 0;
  86. }
  87. static int vp7045_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  88. {
  89. tune->min_delay_ms = 800;
  90. return 0;
  91. }
  92. static int vp7045_fe_set_frontend(struct dvb_frontend *fe)
  93. {
  94. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  95. struct vp7045_fe_state *state = fe->demodulator_priv;
  96. u8 buf[5];
  97. u32 freq = fep->frequency / 1000;
  98. buf[0] = (freq >> 16) & 0xff;
  99. buf[1] = (freq >> 8) & 0xff;
  100. buf[2] = freq & 0xff;
  101. buf[3] = 0;
  102. switch (fep->bandwidth_hz) {
  103. case 8000000:
  104. buf[4] = 8;
  105. break;
  106. case 7000000:
  107. buf[4] = 7;
  108. break;
  109. case 6000000:
  110. buf[4] = 6;
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. vp7045_usb_op(state->d,LOCK_TUNER_COMMAND,buf,5,NULL,0,200);
  116. return 0;
  117. }
  118. static void vp7045_fe_release(struct dvb_frontend* fe)
  119. {
  120. struct vp7045_fe_state *state = fe->demodulator_priv;
  121. kfree(state);
  122. }
  123. static struct dvb_frontend_ops vp7045_fe_ops;
  124. struct dvb_frontend * vp7045_fe_attach(struct dvb_usb_device *d)
  125. {
  126. struct vp7045_fe_state *s = kzalloc(sizeof(struct vp7045_fe_state), GFP_KERNEL);
  127. if (s == NULL)
  128. goto error;
  129. s->d = d;
  130. memcpy(&s->fe.ops, &vp7045_fe_ops, sizeof(struct dvb_frontend_ops));
  131. s->fe.demodulator_priv = s;
  132. return &s->fe;
  133. error:
  134. return NULL;
  135. }
  136. static struct dvb_frontend_ops vp7045_fe_ops = {
  137. .delsys = { SYS_DVBT },
  138. .info = {
  139. .name = "Twinhan VP7045/46 USB DVB-T",
  140. .frequency_min = 44250000,
  141. .frequency_max = 867250000,
  142. .frequency_stepsize = 1000,
  143. .caps = FE_CAN_INVERSION_AUTO |
  144. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  145. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  146. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  147. FE_CAN_TRANSMISSION_MODE_AUTO |
  148. FE_CAN_GUARD_INTERVAL_AUTO |
  149. FE_CAN_RECOVER |
  150. FE_CAN_HIERARCHY_AUTO,
  151. },
  152. .release = vp7045_fe_release,
  153. .init = vp7045_fe_init,
  154. .sleep = vp7045_fe_sleep,
  155. .set_frontend = vp7045_fe_set_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. };