vp7045-fe.c 4.7 KB

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