blob: b8825b18c003a32c52066f616b26ef0b95758f21 [file] [log] [blame]
Johannes Stezenbach776338e2005-06-23 22:02:35 -07001/* 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
17/* It is a Zarlink MT352 within a Samsung Tuner (DNOS404ZH102A) - 040929 - AAT
18 *
19 * Programming is hidden inside the firmware, so set_frontend is very easy.
20 * Even though there is a Firmware command that one can use to access the demod
21 * via its registers. This is used for status information.
22 */
23
24struct vp7045_fe_state {
25 struct dvb_frontend fe;
26 struct dvb_usb_device *d;
27};
28
Johannes Stezenbach776338e2005-06-23 22:02:35 -070029static int vp7045_fe_read_status(struct dvb_frontend* fe, fe_status_t *status)
30{
31 struct vp7045_fe_state *state = fe->demodulator_priv;
32 u8 s0 = vp7045_read_reg(state->d,0x00),
33 s1 = vp7045_read_reg(state->d,0x01),
34 s3 = vp7045_read_reg(state->d,0x03);
35
36 *status = 0;
37 if (s0 & (1 << 4))
38 *status |= FE_HAS_CARRIER;
39 if (s0 & (1 << 1))
40 *status |= FE_HAS_VITERBI;
41 if (s0 & (1 << 5))
42 *status |= FE_HAS_LOCK;
43 if (s1 & (1 << 1))
44 *status |= FE_HAS_SYNC;
45 if (s3 & (1 << 6))
46 *status |= FE_HAS_SIGNAL;
47
48 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
49 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
50 *status &= ~FE_HAS_LOCK;
51
52 return 0;
53}
54
55static int vp7045_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
56{
57 struct vp7045_fe_state *state = fe->demodulator_priv;
58 *ber = (vp7045_read_reg(state->d, 0x0D) << 16) |
59 (vp7045_read_reg(state->d, 0x0E) << 8) |
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080060 vp7045_read_reg(state->d, 0x0F);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070061 return 0;
62}
63
64static int vp7045_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
65{
66 struct vp7045_fe_state *state = fe->demodulator_priv;
67 *unc = (vp7045_read_reg(state->d, 0x10) << 8) |
68 vp7045_read_reg(state->d, 0x11);
69 return 0;
70}
71
72static int vp7045_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
73{
74 struct vp7045_fe_state *state = fe->demodulator_priv;
75 u16 signal = (vp7045_read_reg(state->d, 0x14) << 8) |
76 vp7045_read_reg(state->d, 0x15);
77
78 *strength = ~signal;
79 return 0;
80}
81
82static int vp7045_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
83{
84 struct vp7045_fe_state *state = fe->demodulator_priv;
85 u8 _snr = vp7045_read_reg(state->d, 0x09);
86 *snr = (_snr << 8) | _snr;
87 return 0;
88}
89
90static int vp7045_fe_init(struct dvb_frontend* fe)
91{
92 return 0;
93}
94
95static int vp7045_fe_sleep(struct dvb_frontend* fe)
96{
97 return 0;
98}
99
100static int vp7045_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
101{
102 tune->min_delay_ms = 800;
103 return 0;
104}
105
Mauro Carvalho Chehabe4b40032011-12-26 16:00:42 -0300106static int vp7045_fe_set_frontend(struct dvb_frontend *fe)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700107{
Mauro Carvalho Chehabe4b40032011-12-26 16:00:42 -0300108 struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700109 struct vp7045_fe_state *state = fe->demodulator_priv;
110 u8 buf[5];
111 u32 freq = fep->frequency / 1000;
112
113 buf[0] = (freq >> 16) & 0xff;
114 buf[1] = (freq >> 8) & 0xff;
115 buf[2] = freq & 0xff;
116 buf[3] = 0;
117
Mauro Carvalho Chehabe4b40032011-12-26 16:00:42 -0300118 switch (fep->bandwidth_hz) {
119 case 8000000:
120 buf[4] = 8;
121 break;
122 case 7000000:
123 buf[4] = 7;
124 break;
125 case 6000000:
126 buf[4] = 6;
127 break;
128 default:
129 return -EINVAL;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700130 }
131
132 vp7045_usb_op(state->d,LOCK_TUNER_COMMAND,buf,5,NULL,0,200);
133 return 0;
134}
135
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700136static void vp7045_fe_release(struct dvb_frontend* fe)
137{
138 struct vp7045_fe_state *state = fe->demodulator_priv;
139 kfree(state);
140}
141
142static struct dvb_frontend_ops vp7045_fe_ops;
143
144struct dvb_frontend * vp7045_fe_attach(struct dvb_usb_device *d)
145{
Panagiotis Issaris74081872006-01-11 19:40:56 -0200146 struct vp7045_fe_state *s = kzalloc(sizeof(struct vp7045_fe_state), GFP_KERNEL);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700147 if (s == NULL)
148 goto error;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700149
150 s->d = d;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300151 memcpy(&s->fe.ops, &vp7045_fe_ops, sizeof(struct dvb_frontend_ops));
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700152 s->fe.demodulator_priv = s;
153
Trent Piepho83977032006-05-12 20:36:24 -0300154 return &s->fe;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700155error:
156 return NULL;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700157}
158
159
160static struct dvb_frontend_ops vp7045_fe_ops = {
Mauro Carvalho Chehabe4b40032011-12-26 16:00:42 -0300161 .delsys = { SYS_DVBT },
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700162 .info = {
163 .name = "Twinhan VP7045/46 USB DVB-T",
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700164 .frequency_min = 44250000,
165 .frequency_max = 867250000,
166 .frequency_stepsize = 1000,
167 .caps = FE_CAN_INVERSION_AUTO |
168 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
169 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
170 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
171 FE_CAN_TRANSMISSION_MODE_AUTO |
172 FE_CAN_GUARD_INTERVAL_AUTO |
173 FE_CAN_RECOVER |
174 FE_CAN_HIERARCHY_AUTO,
175 },
176
177 .release = vp7045_fe_release,
178
179 .init = vp7045_fe_init,
180 .sleep = vp7045_fe_sleep,
181
Mauro Carvalho Chehabe4b40032011-12-26 16:00:42 -0300182 .set_frontend = vp7045_fe_set_frontend,
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700183 .get_tune_settings = vp7045_fe_get_tune_settings,
184
185 .read_status = vp7045_fe_read_status,
186 .read_ber = vp7045_fe_read_ber,
187 .read_signal_strength = vp7045_fe_read_signal_strength,
188 .read_snr = vp7045_fe_read_snr,
189 .read_ucblocks = vp7045_fe_read_unc_blocks,
190};