vserver 2.0 rc7
[linux-2.6.git] / drivers / media / dvb / frontends / cx22700.c
1 /*
2     Conexant cx22700 DVB OFDM demodulator driver
3
4     Copyright (C) 2001-2002 Convergence Integrated Media GmbH
5         Holger Waechtler <holger@convergence.de>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 */
22
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
29 #include "dvb_frontend.h"
30 #include "cx22700.h"
31
32
33 struct cx22700_state {
34
35         struct i2c_adapter* i2c;
36
37         struct dvb_frontend_ops ops;
38
39         const struct cx22700_config* config;
40
41         struct dvb_frontend frontend;
42 };
43
44
45 static int debug;
46 #define dprintk(args...) \
47         do { \
48                 if (debug) printk(KERN_DEBUG "cx22700: " args); \
49         } while (0)
50
51 static u8 init_tab [] = {
52         0x04, 0x10,
53         0x05, 0x09,
54         0x06, 0x00,
55         0x08, 0x04,
56         0x09, 0x00,
57         0x0a, 0x01,
58         0x15, 0x40,
59         0x16, 0x10,
60         0x17, 0x87,
61         0x18, 0x17,
62         0x1a, 0x10,
63         0x25, 0x04,
64         0x2e, 0x00,
65         0x39, 0x00,
66         0x3a, 0x04,
67         0x45, 0x08,
68         0x46, 0x02,
69         0x47, 0x05,
70 };
71
72
73 static int cx22700_writereg (struct cx22700_state* state, u8 reg, u8 data)
74 {
75         int ret;
76         u8 buf [] = { reg, data };
77         struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
78
79         dprintk ("%s\n", __FUNCTION__);
80
81         ret = i2c_transfer (state->i2c, &msg, 1);
82
83         if (ret != 1)
84                 printk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
85                         __FUNCTION__, reg, data, ret);
86
87         return (ret != 1) ? -1 : 0;
88 }
89
90 static int cx22700_readreg (struct cx22700_state* state, u8 reg)
91 {
92         int ret;
93         u8 b0 [] = { reg };
94         u8 b1 [] = { 0 };
95         struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
96                            { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
97
98         dprintk ("%s\n", __FUNCTION__);
99
100         ret = i2c_transfer (state->i2c, msg, 2);
101
102         if (ret != 2) return -EIO;
103
104         return b1[0];
105 }
106
107 static int cx22700_set_inversion (struct cx22700_state* state, int inversion)
108 {
109         u8 val;
110
111         dprintk ("%s\n", __FUNCTION__);
112
113         switch (inversion) {
114         case INVERSION_AUTO:
115                 return -EOPNOTSUPP;
116         case INVERSION_ON:
117                 val = cx22700_readreg (state, 0x09);
118                 return cx22700_writereg (state, 0x09, val | 0x01);
119         case INVERSION_OFF:
120                 val = cx22700_readreg (state, 0x09);
121                 return cx22700_writereg (state, 0x09, val & 0xfe);
122         default:
123                 return -EINVAL;
124         }
125 }
126
127 static int cx22700_set_tps (struct cx22700_state *state, struct dvb_ofdm_parameters *p)
128 {
129         static const u8 qam_tab [4] = { 0, 1, 0, 2 };
130         static const u8 fec_tab [6] = { 0, 1, 2, 0, 3, 4 };
131         u8 val;
132
133         dprintk ("%s\n", __FUNCTION__);
134
135         if (p->code_rate_HP < FEC_1_2 || p->code_rate_HP > FEC_7_8)
136                 return -EINVAL;
137
138         if (p->code_rate_LP < FEC_1_2 || p->code_rate_LP > FEC_7_8)
139
140         if (p->code_rate_HP == FEC_4_5 || p->code_rate_LP == FEC_4_5)
141                 return -EINVAL;
142
143         if (p->guard_interval < GUARD_INTERVAL_1_32 ||
144             p->guard_interval > GUARD_INTERVAL_1_4)
145                 return -EINVAL;
146
147         if (p->transmission_mode != TRANSMISSION_MODE_2K &&
148             p->transmission_mode != TRANSMISSION_MODE_8K)
149                 return -EINVAL;
150
151         if (p->constellation != QPSK &&
152             p->constellation != QAM_16 &&
153             p->constellation != QAM_64)
154                 return -EINVAL;
155
156         if (p->hierarchy_information < HIERARCHY_NONE ||
157             p->hierarchy_information > HIERARCHY_4)
158                 return -EINVAL;
159
160         if (p->bandwidth < BANDWIDTH_8_MHZ && p->bandwidth > BANDWIDTH_6_MHZ)
161                 return -EINVAL;
162
163         if (p->bandwidth == BANDWIDTH_7_MHZ)
164                 cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 | 0x10));
165         else
166                 cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 & ~0x10));
167
168         val = qam_tab[p->constellation - QPSK];
169         val |= p->hierarchy_information - HIERARCHY_NONE;
170
171         cx22700_writereg (state, 0x04, val);
172
173         val = fec_tab[p->code_rate_HP - FEC_1_2] << 3;
174         val |= fec_tab[p->code_rate_LP - FEC_1_2];
175
176         cx22700_writereg (state, 0x05, val);
177
178         val = (p->guard_interval - GUARD_INTERVAL_1_32) << 2;
179         val |= p->transmission_mode - TRANSMISSION_MODE_2K;
180
181         cx22700_writereg (state, 0x06, val);
182
183         cx22700_writereg (state, 0x08, 0x04 | 0x02);  /* use user tps parameters */
184         cx22700_writereg (state, 0x08, 0x04);         /* restart aquisition */
185
186         return 0;
187 }
188
189 static int cx22700_get_tps (struct cx22700_state* state, struct dvb_ofdm_parameters *p)
190 {
191         static const fe_modulation_t qam_tab [3] = { QPSK, QAM_16, QAM_64 };
192         static const fe_code_rate_t fec_tab [5] = { FEC_1_2, FEC_2_3, FEC_3_4,
193                                                     FEC_5_6, FEC_7_8 };
194         u8 val;
195
196         dprintk ("%s\n", __FUNCTION__);
197
198         if (!(cx22700_readreg(state, 0x07) & 0x20))  /*  tps valid? */
199                 return -EAGAIN;
200
201         val = cx22700_readreg (state, 0x01);
202
203         if ((val & 0x7) > 4)
204                 p->hierarchy_information = HIERARCHY_AUTO;
205         else
206                 p->hierarchy_information = HIERARCHY_NONE + (val & 0x7);
207
208         if (((val >> 3) & 0x3) > 2)
209                 p->constellation = QAM_AUTO;
210         else
211                 p->constellation = qam_tab[(val >> 3) & 0x3];
212
213         val = cx22700_readreg (state, 0x02);
214
215         if (((val >> 3) & 0x07) > 4)
216                 p->code_rate_HP = FEC_AUTO;
217         else
218                 p->code_rate_HP = fec_tab[(val >> 3) & 0x07];
219
220         if ((val & 0x07) > 4)
221                 p->code_rate_LP = FEC_AUTO;
222         else
223                 p->code_rate_LP = fec_tab[val & 0x07];
224
225         val = cx22700_readreg (state, 0x03);
226
227         p->guard_interval = GUARD_INTERVAL_1_32 + ((val >> 6) & 0x3);
228         p->transmission_mode = TRANSMISSION_MODE_2K + ((val >> 5) & 0x1);
229
230         return 0;
231 }
232
233 static int cx22700_init (struct dvb_frontend* fe)
234
235 {       struct cx22700_state* state = fe->demodulator_priv;
236         int i;
237
238         dprintk("cx22700_init: init chip\n");
239
240         cx22700_writereg (state, 0x00, 0x02);   /*  soft reset */
241         cx22700_writereg (state, 0x00, 0x00);
242
243         msleep(10);
244
245         for (i=0; i<sizeof(init_tab); i+=2)
246                 cx22700_writereg (state, init_tab[i], init_tab[i+1]);
247
248         cx22700_writereg (state, 0x00, 0x01);
249
250         if (state->config->pll_init) {
251                 cx22700_writereg (state, 0x0a, 0x00);  /* open i2c bus switch */
252                 state->config->pll_init(fe);
253                 cx22700_writereg (state, 0x0a, 0x01);  /* close i2c bus switch */
254         }
255
256         return 0;
257 }
258
259 static int cx22700_read_status(struct dvb_frontend* fe, fe_status_t* status)
260 {
261         struct cx22700_state* state = fe->demodulator_priv;
262
263         u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
264                    | (cx22700_readreg (state, 0x0e) << 1);
265         u8 sync = cx22700_readreg (state, 0x07);
266
267         *status = 0;
268
269         if (rs_ber < 0xff00)
270                 *status |= FE_HAS_SIGNAL;
271
272         if (sync & 0x20)
273                 *status |= FE_HAS_CARRIER;
274
275         if (sync & 0x10)
276                 *status |= FE_HAS_VITERBI;
277
278         if (sync & 0x10)
279                 *status |= FE_HAS_SYNC;
280
281         if (*status == 0x0f)
282                 *status |= FE_HAS_LOCK;
283
284         return 0;
285 }
286
287 static int cx22700_read_ber(struct dvb_frontend* fe, u32* ber)
288 {
289         struct cx22700_state* state = fe->demodulator_priv;
290
291         *ber = cx22700_readreg (state, 0x0c) & 0x7f;
292         cx22700_writereg (state, 0x0c, 0x00);
293
294         return 0;
295 }
296
297 static int cx22700_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
298 {
299         struct cx22700_state* state = fe->demodulator_priv;
300
301         u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
302                    | (cx22700_readreg (state, 0x0e) << 1);
303         *signal_strength = ~rs_ber;
304
305         return 0;
306 }
307
308 static int cx22700_read_snr(struct dvb_frontend* fe, u16* snr)
309 {
310         struct cx22700_state* state = fe->demodulator_priv;
311
312         u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
313                    | (cx22700_readreg (state, 0x0e) << 1);
314         *snr = ~rs_ber;
315
316         return 0;
317 }
318
319 static int cx22700_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
320 {
321         struct cx22700_state* state = fe->demodulator_priv;
322
323         *ucblocks = cx22700_readreg (state, 0x0f);
324         cx22700_writereg (state, 0x0f, 0x00);
325
326         return 0;
327 }
328
329 static int cx22700_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
330 {
331         struct cx22700_state* state = fe->demodulator_priv;
332
333         cx22700_writereg (state, 0x00, 0x02); /* XXX CHECKME: soft reset*/
334         cx22700_writereg (state, 0x00, 0x00);
335
336         cx22700_writereg (state, 0x0a, 0x00);  /* open i2c bus switch */
337         state->config->pll_set(fe, p);
338         cx22700_writereg (state, 0x0a, 0x01);  /* close i2c bus switch */
339         cx22700_set_inversion (state, p->inversion);
340         cx22700_set_tps (state, &p->u.ofdm);
341         cx22700_writereg (state, 0x37, 0x01);  /* PAL loop filter off */
342         cx22700_writereg (state, 0x00, 0x01);  /* restart acquire */
343
344         return 0;
345 }
346
347 static int cx22700_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
348 {
349         struct cx22700_state* state = fe->demodulator_priv;
350         u8 reg09 = cx22700_readreg (state, 0x09);
351
352         p->inversion = reg09 & 0x1 ? INVERSION_ON : INVERSION_OFF;
353         return cx22700_get_tps (state, &p->u.ofdm);
354 }
355
356 static int cx22700_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
357 {
358         fesettings->min_delay_ms = 150;
359         fesettings->step_size = 166667;
360         fesettings->max_drift = 166667*2;
361         return 0;
362 }
363
364 static void cx22700_release(struct dvb_frontend* fe)
365 {
366         struct cx22700_state* state = fe->demodulator_priv;
367         kfree(state);
368 }
369
370 static struct dvb_frontend_ops cx22700_ops;
371
372 struct dvb_frontend* cx22700_attach(const struct cx22700_config* config,
373                                     struct i2c_adapter* i2c)
374 {
375         struct cx22700_state* state = NULL;
376
377         /* allocate memory for the internal state */
378         state = kmalloc(sizeof(struct cx22700_state), GFP_KERNEL);
379         if (state == NULL) goto error;
380
381         /* setup the state */
382         state->config = config;
383         state->i2c = i2c;
384         memcpy(&state->ops, &cx22700_ops, sizeof(struct dvb_frontend_ops));
385
386         /* check if the demod is there */
387         if (cx22700_readreg(state, 0x07) < 0) goto error;
388
389         /* create dvb_frontend */
390         state->frontend.ops = &state->ops;
391         state->frontend.demodulator_priv = state;
392         return &state->frontend;
393
394 error:
395         kfree(state);
396         return NULL;
397 }
398
399 static struct dvb_frontend_ops cx22700_ops = {
400
401         .info = {
402                 .name                   = "Conexant CX22700 DVB-T",
403                 .type                   = FE_OFDM,
404                 .frequency_min          = 470000000,
405                 .frequency_max          = 860000000,
406                 .frequency_stepsize     = 166667,
407                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
408                       FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
409                       FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
410                       FE_CAN_RECOVER
411         },
412
413         .release = cx22700_release,
414
415         .init = cx22700_init,
416
417         .set_frontend = cx22700_set_frontend,
418         .get_frontend = cx22700_get_frontend,
419         .get_tune_settings = cx22700_get_tune_settings,
420
421         .read_status = cx22700_read_status,
422         .read_ber = cx22700_read_ber,
423         .read_signal_strength = cx22700_read_signal_strength,
424         .read_snr = cx22700_read_snr,
425         .read_ucblocks = cx22700_read_ucblocks,
426 };
427
428 module_param(debug, int, 0644);
429 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
430
431 MODULE_DESCRIPTION("Conexant CX22700 DVB-T Demodulator driver");
432 MODULE_AUTHOR("Holger Waechtler");
433 MODULE_LICENSE("GPL");
434
435 EXPORT_SYMBOL(cx22700_attach);