Auto-update from upstream
[linux-2.6] / drivers / media / dvb / dvb-usb / vp7045-fe.c
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
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
24 struct vp7045_fe_state {
25         struct dvb_frontend fe;
26         struct dvb_usb_device *d;
27 };
28
29
30 static int vp7045_fe_read_status(struct dvb_frontend* fe, fe_status_t *status)
31 {
32         struct vp7045_fe_state *state = fe->demodulator_priv;
33         u8 s0 = vp7045_read_reg(state->d,0x00),
34            s1 = vp7045_read_reg(state->d,0x01),
35            s3 = vp7045_read_reg(state->d,0x03);
36
37         *status = 0;
38         if (s0 & (1 << 4))
39                 *status |= FE_HAS_CARRIER;
40         if (s0 & (1 << 1))
41                 *status |= FE_HAS_VITERBI;
42         if (s0 & (1 << 5))
43                 *status |= FE_HAS_LOCK;
44         if (s1 & (1 << 1))
45                 *status |= FE_HAS_SYNC;
46         if (s3 & (1 << 6))
47                 *status |= FE_HAS_SIGNAL;
48
49         if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
50                         (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
51                 *status &= ~FE_HAS_LOCK;
52
53         return 0;
54 }
55
56 static int vp7045_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
57 {
58         struct vp7045_fe_state *state = fe->demodulator_priv;
59         *ber = (vp7045_read_reg(state->d, 0x0D) << 16) |
60                (vp7045_read_reg(state->d, 0x0E) << 8) |
61                 vp7045_read_reg(state->d, 0x0F);
62         return 0;
63 }
64
65 static int vp7045_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
66 {
67         struct vp7045_fe_state *state = fe->demodulator_priv;
68         *unc = (vp7045_read_reg(state->d, 0x10) << 8) |
69                     vp7045_read_reg(state->d, 0x11);
70         return 0;
71 }
72
73 static int vp7045_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
74 {
75         struct vp7045_fe_state *state = fe->demodulator_priv;
76         u16 signal = (vp7045_read_reg(state->d, 0x14) << 8) |
77                 vp7045_read_reg(state->d, 0x15);
78
79         *strength = ~signal;
80         return 0;
81 }
82
83 static int vp7045_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
84 {
85         struct vp7045_fe_state *state = fe->demodulator_priv;
86         u8 _snr = vp7045_read_reg(state->d, 0x09);
87         *snr = (_snr << 8) | _snr;
88         return 0;
89 }
90
91 static int vp7045_fe_init(struct dvb_frontend* fe)
92 {
93         return 0;
94 }
95
96 static int vp7045_fe_sleep(struct dvb_frontend* fe)
97 {
98         return 0;
99 }
100
101 static int vp7045_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
102 {
103         tune->min_delay_ms = 800;
104         return 0;
105 }
106
107 static int vp7045_fe_set_frontend(struct dvb_frontend* fe,
108                                   struct dvb_frontend_parameters *fep)
109 {
110         struct vp7045_fe_state *state = fe->demodulator_priv;
111         u8 buf[5];
112         u32 freq = fep->frequency / 1000;
113
114         buf[0] = (freq >> 16) & 0xff;
115         buf[1] = (freq >>  8) & 0xff;
116         buf[2] =  freq        & 0xff;
117         buf[3] = 0;
118
119         switch (fep->u.ofdm.bandwidth) {
120                 case BANDWIDTH_8_MHZ: buf[4] = 8; break;
121                 case BANDWIDTH_7_MHZ: buf[4] = 7; break;
122                 case BANDWIDTH_6_MHZ: buf[4] = 6; break;
123                 case BANDWIDTH_AUTO: return -EOPNOTSUPP;
124                 default:
125                         return -EINVAL;
126         }
127
128         vp7045_usb_op(state->d,LOCK_TUNER_COMMAND,buf,5,NULL,0,200);
129         return 0;
130 }
131
132 static int vp7045_fe_get_frontend(struct dvb_frontend* fe,
133                                   struct dvb_frontend_parameters *fep)
134 {
135         return 0;
136 }
137
138 static void vp7045_fe_release(struct dvb_frontend* fe)
139 {
140         struct vp7045_fe_state *state = fe->demodulator_priv;
141         kfree(state);
142 }
143
144 static struct dvb_frontend_ops vp7045_fe_ops;
145
146 struct dvb_frontend * vp7045_fe_attach(struct dvb_usb_device *d)
147 {
148         struct vp7045_fe_state *s = kmalloc(sizeof(struct vp7045_fe_state), GFP_KERNEL);
149         if (s == NULL)
150                 goto error;
151         memset(s,0,sizeof(struct vp7045_fe_state));
152
153         s->d = d;
154         s->fe.ops = &vp7045_fe_ops;
155         s->fe.demodulator_priv = s;
156
157         goto success;
158 error:
159         return NULL;
160 success:
161         return &s->fe;
162 }
163
164
165 static struct dvb_frontend_ops vp7045_fe_ops = {
166         .info = {
167                 .name                   = "Twinhan VP7045/46 USB DVB-T",
168                 .type                   = FE_OFDM,
169                 .frequency_min          = 44250000,
170                 .frequency_max          = 867250000,
171                 .frequency_stepsize     = 1000,
172                 .caps = FE_CAN_INVERSION_AUTO |
173                                 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
174                                 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
175                                 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
176                                 FE_CAN_TRANSMISSION_MODE_AUTO |
177                                 FE_CAN_GUARD_INTERVAL_AUTO |
178                                 FE_CAN_RECOVER |
179                                 FE_CAN_HIERARCHY_AUTO,
180         },
181
182         .release = vp7045_fe_release,
183
184         .init = vp7045_fe_init,
185         .sleep = vp7045_fe_sleep,
186
187         .set_frontend = vp7045_fe_set_frontend,
188         .get_frontend = vp7045_fe_get_frontend,
189         .get_tune_settings = vp7045_fe_get_tune_settings,
190
191         .read_status = vp7045_fe_read_status,
192         .read_ber = vp7045_fe_read_ber,
193         .read_signal_strength = vp7045_fe_read_signal_strength,
194         .read_snr = vp7045_fe_read_snr,
195         .read_ucblocks = vp7045_fe_read_unc_blocks,
196 };