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