Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / media / dvb / frontends / cx22702.c
1 /*
2     Conexant 22702 DVB OFDM demodulator driver
3
4     based on:
5         Alps TDMB7 DVB OFDM demodulator driver
6
7     Copyright (C) 2001-2002 Convergence Integrated Media GmbH
8           Holger Waechtler <holger@convergence.de>
9
10     Copyright (C) 2004 Steven Toth <stoth@hauppauge.com>
11
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU General Public License for more details.
21
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 */
27
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include "dvb_frontend.h"
35 #include "dvb-pll.h"
36 #include "cx22702.h"
37
38
39 struct cx22702_state {
40
41         struct i2c_adapter* i2c;
42
43         struct dvb_frontend_ops ops;
44
45         /* configuration settings */
46         const struct cx22702_config* config;
47
48         struct dvb_frontend frontend;
49
50         /* previous uncorrected block counter */
51         u8 prevUCBlocks;
52 };
53
54 static int debug = 0;
55 #define dprintk if (debug) printk
56
57 /* Register values to initialise the demod */
58 static u8 init_tab [] = {
59         0x00, 0x00, /* Stop aquisition */
60         0x0B, 0x06,
61         0x09, 0x01,
62         0x0D, 0x41,
63         0x16, 0x32,
64         0x20, 0x0A,
65         0x21, 0x17,
66         0x24, 0x3e,
67         0x26, 0xff,
68         0x27, 0x10,
69         0x28, 0x00,
70         0x29, 0x00,
71         0x2a, 0x10,
72         0x2b, 0x00,
73         0x2c, 0x10,
74         0x2d, 0x00,
75         0x48, 0xd4,
76         0x49, 0x56,
77         0x6b, 0x1e,
78         0xc8, 0x02,
79         0xf9, 0x00,
80         0xfa, 0x00,
81         0xfb, 0x00,
82         0xfc, 0x00,
83         0xfd, 0x00,
84 };
85
86 static int cx22702_writereg (struct cx22702_state* state, u8 reg, u8 data)
87 {
88         int ret;
89         u8 buf [] = { reg, data };
90         struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
91
92         ret = i2c_transfer(state->i2c, &msg, 1);
93
94         if (ret != 1)
95                 printk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
96                         __FUNCTION__, reg, data, ret);
97
98         return (ret != 1) ? -1 : 0;
99 }
100
101 static u8 cx22702_readreg (struct cx22702_state* state, u8 reg)
102 {
103         int ret;
104         u8 b0 [] = { reg };
105         u8 b1 [] = { 0 };
106
107         struct i2c_msg msg [] = {
108                 { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
109                 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
110
111         ret = i2c_transfer(state->i2c, msg, 2);
112
113         if (ret != 2)
114                 printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
115
116         return b1[0];
117 }
118
119 static int cx22702_set_inversion (struct cx22702_state *state, int inversion)
120 {
121         u8 val;
122
123         switch (inversion) {
124
125                 case INVERSION_AUTO:
126                         return -EOPNOTSUPP;
127
128                 case INVERSION_ON:
129                         val = cx22702_readreg (state, 0x0C);
130                         return cx22702_writereg (state, 0x0C, val | 0x01);
131
132                 case INVERSION_OFF:
133                         val = cx22702_readreg (state, 0x0C);
134                         return cx22702_writereg (state, 0x0C, val & 0xfe);
135
136                 default:
137                         return -EINVAL;
138
139         }
140
141 }
142
143 /* Retrieve the demod settings */
144 static int cx22702_get_tps (struct cx22702_state *state, struct dvb_ofdm_parameters *p)
145 {
146         u8 val;
147
148         /* Make sure the TPS regs are valid */
149         if (!(cx22702_readreg(state, 0x0A) & 0x20))
150                 return -EAGAIN;
151
152         val = cx22702_readreg (state, 0x01);
153         switch( (val&0x18)>>3) {
154                 case 0: p->constellation =   QPSK; break;
155                 case 1: p->constellation = QAM_16; break;
156                 case 2: p->constellation = QAM_64; break;
157         }
158         switch( val&0x07 ) {
159                 case 0: p->hierarchy_information = HIERARCHY_NONE; break;
160                 case 1: p->hierarchy_information =    HIERARCHY_1; break;
161                 case 2: p->hierarchy_information =    HIERARCHY_2; break;
162                 case 3: p->hierarchy_information =    HIERARCHY_4; break;
163         }
164
165
166         val = cx22702_readreg (state, 0x02);
167         switch( (val&0x38)>>3 ) {
168                 case 0: p->code_rate_HP = FEC_1_2; break;
169                 case 1: p->code_rate_HP = FEC_2_3; break;
170                 case 2: p->code_rate_HP = FEC_3_4; break;
171                 case 3: p->code_rate_HP = FEC_5_6; break;
172                 case 4: p->code_rate_HP = FEC_7_8; break;
173         }
174         switch( val&0x07 ) {
175                 case 0: p->code_rate_LP = FEC_1_2; break;
176                 case 1: p->code_rate_LP = FEC_2_3; break;
177                 case 2: p->code_rate_LP = FEC_3_4; break;
178                 case 3: p->code_rate_LP = FEC_5_6; break;
179                 case 4: p->code_rate_LP = FEC_7_8; break;
180         }
181
182
183         val = cx22702_readreg (state, 0x03);
184         switch( (val&0x0c)>>2 ) {
185                 case 0: p->guard_interval = GUARD_INTERVAL_1_32; break;
186                 case 1: p->guard_interval = GUARD_INTERVAL_1_16; break;
187                 case 2: p->guard_interval =  GUARD_INTERVAL_1_8; break;
188                 case 3: p->guard_interval =  GUARD_INTERVAL_1_4; break;
189         }
190         switch( val&0x03 ) {
191                 case 0: p->transmission_mode = TRANSMISSION_MODE_2K; break;
192                 case 1: p->transmission_mode = TRANSMISSION_MODE_8K; break;
193         }
194
195         return 0;
196 }
197
198 static int cx22702_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
199 {
200         struct cx22702_state* state = fe->demodulator_priv;
201         dprintk ("%s(%d)\n", __FUNCTION__, enable);
202         if (enable)
203                 return cx22702_writereg (state, 0x0D, cx22702_readreg(state, 0x0D) & 0xfe);
204         else
205                 return cx22702_writereg (state, 0x0D, cx22702_readreg(state, 0x0D) | 1);
206 }
207
208 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
209 static int cx22702_set_tps (struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
210 {
211         u8 val;
212         struct cx22702_state* state = fe->demodulator_priv;
213
214         /* set PLL */
215         cx22702_i2c_gate_ctrl(fe, 1);
216         if (state->config->pll_set) {
217                 state->config->pll_set(fe, p);
218         } else if (state->config->pll_desc) {
219                 u8 pllbuf[4];
220                 struct i2c_msg msg = { .addr = state->config->pll_address,
221                                        .buf = pllbuf, .len = 4 };
222                 dvb_pll_configure(state->config->pll_desc, pllbuf,
223                                   p->frequency,
224                                   p->u.ofdm.bandwidth);
225                 i2c_transfer(state->i2c, &msg, 1);
226         } else {
227                 BUG();
228         }
229         cx22702_i2c_gate_ctrl(fe, 0);
230
231         /* set inversion */
232         cx22702_set_inversion (state, p->inversion);
233
234         /* set bandwidth */
235         switch(p->u.ofdm.bandwidth) {
236         case BANDWIDTH_6_MHZ:
237                 cx22702_writereg(state, 0x0C, (cx22702_readreg(state, 0x0C) & 0xcf) | 0x20 );
238                 break;
239         case BANDWIDTH_7_MHZ:
240                 cx22702_writereg(state, 0x0C, (cx22702_readreg(state, 0x0C) & 0xcf) | 0x10 );
241                 break;
242         case BANDWIDTH_8_MHZ:
243                 cx22702_writereg(state, 0x0C, cx22702_readreg(state, 0x0C) &0xcf );
244                 break;
245         default:
246                 dprintk ("%s: invalid bandwidth\n",__FUNCTION__);
247                 return -EINVAL;
248         }
249
250
251         p->u.ofdm.code_rate_LP = FEC_AUTO; //temp hack as manual not working
252
253         /* use auto configuration? */
254         if((p->u.ofdm.hierarchy_information==HIERARCHY_AUTO) ||
255            (p->u.ofdm.constellation==QAM_AUTO) ||
256            (p->u.ofdm.code_rate_HP==FEC_AUTO) ||
257            (p->u.ofdm.code_rate_LP==FEC_AUTO) ||
258            (p->u.ofdm.guard_interval==GUARD_INTERVAL_AUTO) ||
259            (p->u.ofdm.transmission_mode==TRANSMISSION_MODE_AUTO) ) {
260
261                 /* TPS Source - use hardware driven values */
262                 cx22702_writereg(state, 0x06, 0x10);
263                 cx22702_writereg(state, 0x07, 0x9);
264                 cx22702_writereg(state, 0x08, 0xC1);
265                 cx22702_writereg(state, 0x0B, cx22702_readreg(state, 0x0B) & 0xfc );
266                 cx22702_writereg(state, 0x0C, (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40 );
267                 cx22702_writereg(state, 0x00, 0x01); /* Begin aquisition */
268                 dprintk("%s: Autodetecting\n",__FUNCTION__);
269                 return 0;
270         }
271
272         /* manually programmed values */
273         val=0;
274         switch(p->u.ofdm.constellation) {
275                 case   QPSK: val = (val&0xe7); break;
276                 case QAM_16: val = (val&0xe7)|0x08; break;
277                 case QAM_64: val = (val&0xe7)|0x10; break;
278                 default:
279                         dprintk ("%s: invalid constellation\n",__FUNCTION__);
280                         return -EINVAL;
281         }
282         switch(p->u.ofdm.hierarchy_information) {
283                 case HIERARCHY_NONE: val = (val&0xf8); break;
284                 case    HIERARCHY_1: val = (val&0xf8)|1; break;
285                 case    HIERARCHY_2: val = (val&0xf8)|2; break;
286                 case    HIERARCHY_4: val = (val&0xf8)|3; break;
287                 default:
288                         dprintk ("%s: invalid hierarchy\n",__FUNCTION__);
289                         return -EINVAL;
290         }
291         cx22702_writereg (state, 0x06, val);
292
293         val=0;
294         switch(p->u.ofdm.code_rate_HP) {
295                 case FEC_NONE:
296                 case FEC_1_2: val = (val&0xc7); break;
297                 case FEC_2_3: val = (val&0xc7)|0x08; break;
298                 case FEC_3_4: val = (val&0xc7)|0x10; break;
299                 case FEC_5_6: val = (val&0xc7)|0x18; break;
300                 case FEC_7_8: val = (val&0xc7)|0x20; break;
301                 default:
302                         dprintk ("%s: invalid code_rate_HP\n",__FUNCTION__);
303                         return -EINVAL;
304         }
305         switch(p->u.ofdm.code_rate_LP) {
306                 case FEC_NONE:
307                 case FEC_1_2: val = (val&0xf8); break;
308                 case FEC_2_3: val = (val&0xf8)|1; break;
309                 case FEC_3_4: val = (val&0xf8)|2; break;
310                 case FEC_5_6: val = (val&0xf8)|3; break;
311                 case FEC_7_8: val = (val&0xf8)|4; break;
312                 default:
313                         dprintk ("%s: invalid code_rate_LP\n",__FUNCTION__);
314                         return -EINVAL;
315         }
316         cx22702_writereg (state, 0x07, val);
317
318         val=0;
319         switch(p->u.ofdm.guard_interval) {
320                 case GUARD_INTERVAL_1_32: val = (val&0xf3); break;
321                 case GUARD_INTERVAL_1_16: val = (val&0xf3)|0x04; break;
322                 case  GUARD_INTERVAL_1_8: val = (val&0xf3)|0x08; break;
323                 case  GUARD_INTERVAL_1_4: val = (val&0xf3)|0x0c; break;
324                 default:
325                         dprintk ("%s: invalid guard_interval\n",__FUNCTION__);
326                         return -EINVAL;
327         }
328         switch(p->u.ofdm.transmission_mode) {
329                 case TRANSMISSION_MODE_2K: val = (val&0xfc); break;
330                 case TRANSMISSION_MODE_8K: val = (val&0xfc)|1; break;
331                 default:
332                         dprintk ("%s: invalid transmission_mode\n",__FUNCTION__);
333                         return -EINVAL;
334         }
335         cx22702_writereg(state, 0x08, val);
336         cx22702_writereg(state, 0x0B, (cx22702_readreg(state, 0x0B) & 0xfc) | 0x02 );
337         cx22702_writereg(state, 0x0C, (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40 );
338
339         /* Begin channel aquisition */
340         cx22702_writereg(state, 0x00, 0x01);
341
342         return 0;
343 }
344
345 /* Reset the demod hardware and reset all of the configuration registers
346    to a default state. */
347 static int cx22702_init (struct dvb_frontend* fe)
348 {
349         int i;
350         struct cx22702_state* state = fe->demodulator_priv;
351
352         cx22702_writereg (state, 0x00, 0x02);
353
354         msleep(10);
355
356         for (i=0; i<sizeof(init_tab); i+=2)
357                 cx22702_writereg (state, init_tab[i], init_tab[i+1]);
358
359         cx22702_writereg (state, 0xf8, (state->config->output_mode << 1) & 0x02);
360
361         /* init PLL */
362         if (state->config->pll_init)
363                 state->config->pll_init(fe);
364
365         cx22702_i2c_gate_ctrl(fe, 0);
366
367         return 0;
368 }
369
370 static int cx22702_read_status(struct dvb_frontend* fe, fe_status_t* status)
371 {
372         struct cx22702_state* state = fe->demodulator_priv;
373         u8 reg0A;
374         u8 reg23;
375
376         *status = 0;
377
378         reg0A = cx22702_readreg (state, 0x0A);
379         reg23 = cx22702_readreg (state, 0x23);
380
381         dprintk ("%s: status demod=0x%02x agc=0x%02x\n"
382                 ,__FUNCTION__,reg0A,reg23);
383
384         if(reg0A & 0x10) {
385                 *status |= FE_HAS_LOCK;
386                 *status |= FE_HAS_VITERBI;
387                 *status |= FE_HAS_SYNC;
388         }
389
390         if(reg0A & 0x20)
391                 *status |= FE_HAS_CARRIER;
392
393         if(reg23 < 0xf0)
394                 *status |= FE_HAS_SIGNAL;
395
396         return 0;
397 }
398
399 static int cx22702_read_ber(struct dvb_frontend* fe, u32* ber)
400 {
401         struct cx22702_state* state = fe->demodulator_priv;
402
403         if(cx22702_readreg (state, 0xE4) & 0x02) {
404                 /* Realtime statistics */
405                 *ber = (cx22702_readreg (state, 0xDE) & 0x7F) << 7
406                         | (cx22702_readreg (state, 0xDF)&0x7F);
407         } else {
408                 /* Averagtine statistics */
409                 *ber = (cx22702_readreg (state, 0xDE) & 0x7F) << 7
410                         | cx22702_readreg (state, 0xDF);
411         }
412
413         return 0;
414 }
415
416 static int cx22702_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
417 {
418         struct cx22702_state* state = fe->demodulator_priv;
419
420         *signal_strength = cx22702_readreg (state, 0x23);
421
422         return 0;
423 }
424
425 static int cx22702_read_snr(struct dvb_frontend* fe, u16* snr)
426 {
427         struct cx22702_state* state = fe->demodulator_priv;
428
429         u16 rs_ber=0;
430         if(cx22702_readreg (state, 0xE4) & 0x02) {
431                 /* Realtime statistics */
432                 rs_ber = (cx22702_readreg (state, 0xDE) & 0x7F) << 7
433                         | (cx22702_readreg (state, 0xDF)& 0x7F);
434         } else {
435                 /* Averagine statistics */
436                 rs_ber = (cx22702_readreg (state, 0xDE) & 0x7F) << 8
437                         | cx22702_readreg (state, 0xDF);
438         }
439         *snr = ~rs_ber;
440
441         return 0;
442 }
443
444 static int cx22702_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
445 {
446         struct cx22702_state* state = fe->demodulator_priv;
447
448         u8 _ucblocks;
449
450         /* RS Uncorrectable Packet Count then reset */
451         _ucblocks = cx22702_readreg (state, 0xE3);
452         if (state->prevUCBlocks < _ucblocks)
453                 *ucblocks = (_ucblocks - state->prevUCBlocks);
454         else
455                 *ucblocks = state->prevUCBlocks - _ucblocks;
456         state->prevUCBlocks = _ucblocks;
457
458         return 0;
459 }
460
461 static int cx22702_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
462 {
463         struct cx22702_state* state = fe->demodulator_priv;
464
465         u8 reg0C = cx22702_readreg (state, 0x0C);
466
467         p->inversion = reg0C & 0x1 ? INVERSION_ON : INVERSION_OFF;
468         return cx22702_get_tps (state, &p->u.ofdm);
469 }
470
471 static int cx22702_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
472 {
473         tune->min_delay_ms = 1000;
474         return 0;
475 }
476
477 static void cx22702_release(struct dvb_frontend* fe)
478 {
479         struct cx22702_state* state = fe->demodulator_priv;
480         kfree(state);
481 }
482
483 static struct dvb_frontend_ops cx22702_ops;
484
485 struct dvb_frontend* cx22702_attach(const struct cx22702_config* config,
486                                     struct i2c_adapter* i2c)
487 {
488         struct cx22702_state* state = NULL;
489
490         /* allocate memory for the internal state */
491         state = kmalloc(sizeof(struct cx22702_state), GFP_KERNEL);
492         if (state == NULL)
493                 goto error;
494
495         /* setup the state */
496         state->config = config;
497         state->i2c = i2c;
498         memcpy(&state->ops, &cx22702_ops, sizeof(struct dvb_frontend_ops));
499         state->prevUCBlocks = 0;
500
501         /* check if the demod is there */
502         if (cx22702_readreg(state, 0x1f) != 0x3)
503                 goto error;
504
505         /* create dvb_frontend */
506         state->frontend.ops = &state->ops;
507         state->frontend.demodulator_priv = state;
508         return &state->frontend;
509
510 error:
511         kfree(state);
512         return NULL;
513 }
514
515 static struct dvb_frontend_ops cx22702_ops = {
516
517         .info = {
518                 .name                   = "Conexant CX22702 DVB-T",
519                 .type                   = FE_OFDM,
520                 .frequency_min          = 177000000,
521                 .frequency_max          = 858000000,
522                 .frequency_stepsize     = 166666,
523                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
524                 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
525                 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
526                 FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
527                 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
528         },
529
530         .release = cx22702_release,
531
532         .init = cx22702_init,
533
534         .set_frontend = cx22702_set_tps,
535         .get_frontend = cx22702_get_frontend,
536         .get_tune_settings = cx22702_get_tune_settings,
537
538         .read_status = cx22702_read_status,
539         .read_ber = cx22702_read_ber,
540         .read_signal_strength = cx22702_read_signal_strength,
541         .read_snr = cx22702_read_snr,
542         .read_ucblocks = cx22702_read_ucblocks,
543         .i2c_gate_ctrl = cx22702_i2c_gate_ctrl,
544 };
545
546 module_param(debug, int, 0644);
547 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
548
549 MODULE_DESCRIPTION("Conexant CX22702 DVB-T Demodulator driver");
550 MODULE_AUTHOR("Steven Toth");
551 MODULE_LICENSE("GPL");
552
553 EXPORT_SYMBOL(cx22702_attach);