]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/dvb/frontends/cx22702.c
V4L/DVB: cx22702: Drop useless initializations to 0
[net-next-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@linuxtv.org>
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 "cx22702.h"
36
37 struct cx22702_state {
38
39         struct i2c_adapter *i2c;
40
41         /* configuration settings */
42         const struct cx22702_config *config;
43
44         struct dvb_frontend frontend;
45
46         /* previous uncorrected block counter */
47         u8 prevUCBlocks;
48 };
49
50 static int debug;
51 module_param(debug, int, 0644);
52 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
53
54 #define dprintk if (debug) printk
55
56 /* Register values to initialise the demod */
57 static u8 init_tab[] = {
58         0x00, 0x00, /* Stop aquisition */
59         0x0B, 0x06,
60         0x09, 0x01,
61         0x0D, 0x41,
62         0x16, 0x32,
63         0x20, 0x0A,
64         0x21, 0x17,
65         0x24, 0x3e,
66         0x26, 0xff,
67         0x27, 0x10,
68         0x28, 0x00,
69         0x29, 0x00,
70         0x2a, 0x10,
71         0x2b, 0x00,
72         0x2c, 0x10,
73         0x2d, 0x00,
74         0x48, 0xd4,
75         0x49, 0x56,
76         0x6b, 0x1e,
77         0xc8, 0x02,
78         0xf9, 0x00,
79         0xfa, 0x00,
80         0xfb, 0x00,
81         0xfc, 0x00,
82         0xfd, 0x00,
83 };
84
85 static int cx22702_writereg(struct cx22702_state *state, u8 reg, u8 data)
86 {
87         int ret;
88         u8 buf[] = { reg, data };
89         struct i2c_msg msg = {
90                 .addr = state->config->demod_address, .flags = 0,
91                         .buf = buf, .len = 2 };
92
93         ret = i2c_transfer(state->i2c, &msg, 1);
94
95         if (unlikely(ret != 1)) {
96                 printk(KERN_ERR
97                         "%s: error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
98                         __func__, reg, data, ret);
99                 return -1;
100         }
101
102         return 0;
103 }
104
105 static u8 cx22702_readreg(struct cx22702_state *state, u8 reg)
106 {
107         int ret;
108         u8 data;
109
110         struct i2c_msg msg[] = {
111                 { .addr = state->config->demod_address, .flags = 0,
112                         .buf = &reg, .len = 1 },
113                 { .addr = state->config->demod_address, .flags = I2C_M_RD,
114                         .buf = &data, .len = 1 } };
115
116         ret = i2c_transfer(state->i2c, msg, 2);
117
118         if (unlikely(ret != 2)) {
119                 printk(KERN_ERR "%s: error (reg == 0x%02x, ret == %i)\n",
120                         __func__, reg, ret);
121                 return 0;
122         }
123
124         return data;
125 }
126
127 static int cx22702_set_inversion(struct cx22702_state *state, int inversion)
128 {
129         u8 val;
130
131         switch (inversion) {
132         case INVERSION_AUTO:
133                 return -EOPNOTSUPP;
134         case INVERSION_ON:
135                 val = cx22702_readreg(state, 0x0C);
136                 return cx22702_writereg(state, 0x0C, val | 0x01);
137         case INVERSION_OFF:
138                 val = cx22702_readreg(state, 0x0C);
139                 return cx22702_writereg(state, 0x0C, val & 0xfe);
140         default:
141                 return -EINVAL;
142         }
143
144 }
145
146 /* Retrieve the demod settings */
147 static int cx22702_get_tps(struct cx22702_state *state,
148         struct dvb_ofdm_parameters *p)
149 {
150         u8 val;
151
152         /* Make sure the TPS regs are valid */
153         if (!(cx22702_readreg(state, 0x0A) & 0x20))
154                 return -EAGAIN;
155
156         val = cx22702_readreg(state, 0x01);
157         switch ((val & 0x18) >> 3) {
158         case 0:
159                 p->constellation = QPSK;
160                 break;
161         case 1:
162                 p->constellation = QAM_16;
163                 break;
164         case 2:
165                 p->constellation = QAM_64;
166                 break;
167         }
168         switch (val & 0x07) {
169         case 0:
170                 p->hierarchy_information = HIERARCHY_NONE;
171                 break;
172         case 1:
173                 p->hierarchy_information = HIERARCHY_1;
174                 break;
175         case 2:
176                 p->hierarchy_information = HIERARCHY_2;
177                 break;
178         case 3:
179                 p->hierarchy_information = HIERARCHY_4;
180                 break;
181         }
182
183
184         val = cx22702_readreg(state, 0x02);
185         switch ((val & 0x38) >> 3) {
186         case 0:
187                 p->code_rate_HP = FEC_1_2;
188                 break;
189         case 1:
190                 p->code_rate_HP = FEC_2_3;
191                 break;
192         case 2:
193                 p->code_rate_HP = FEC_3_4;
194                 break;
195         case 3:
196                 p->code_rate_HP = FEC_5_6;
197                 break;
198         case 4:
199                 p->code_rate_HP = FEC_7_8;
200                 break;
201         }
202         switch (val & 0x07) {
203         case 0:
204                 p->code_rate_LP = FEC_1_2;
205                 break;
206         case 1:
207                 p->code_rate_LP = FEC_2_3;
208                 break;
209         case 2:
210                 p->code_rate_LP = FEC_3_4;
211                 break;
212         case 3:
213                 p->code_rate_LP = FEC_5_6;
214                 break;
215         case 4:
216                 p->code_rate_LP = FEC_7_8;
217                 break;
218         }
219
220         val = cx22702_readreg(state, 0x03);
221         switch ((val & 0x0c) >> 2) {
222         case 0:
223                 p->guard_interval = GUARD_INTERVAL_1_32;
224                 break;
225         case 1:
226                 p->guard_interval = GUARD_INTERVAL_1_16;
227                 break;
228         case 2:
229                 p->guard_interval = GUARD_INTERVAL_1_8;
230                 break;
231         case 3:
232                 p->guard_interval = GUARD_INTERVAL_1_4;
233                 break;
234         }
235         switch (val & 0x03) {
236         case 0:
237                 p->transmission_mode = TRANSMISSION_MODE_2K;
238                 break;
239         case 1:
240                 p->transmission_mode = TRANSMISSION_MODE_8K;
241                 break;
242         }
243
244         return 0;
245 }
246
247 static int cx22702_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
248 {
249         struct cx22702_state *state = fe->demodulator_priv;
250         dprintk("%s(%d)\n", __func__, enable);
251         if (enable)
252                 return cx22702_writereg(state, 0x0D,
253                         cx22702_readreg(state, 0x0D) & 0xfe);
254         else
255                 return cx22702_writereg(state, 0x0D,
256                         cx22702_readreg(state, 0x0D) | 1);
257 }
258
259 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
260 static int cx22702_set_tps(struct dvb_frontend *fe,
261         struct dvb_frontend_parameters *p)
262 {
263         u8 val;
264         struct cx22702_state *state = fe->demodulator_priv;
265
266         if (fe->ops.tuner_ops.set_params) {
267                 fe->ops.tuner_ops.set_params(fe, p);
268                 if (fe->ops.i2c_gate_ctrl)
269                         fe->ops.i2c_gate_ctrl(fe, 0);
270         }
271
272         /* set inversion */
273         cx22702_set_inversion(state, p->inversion);
274
275         /* set bandwidth */
276         switch (p->u.ofdm.bandwidth) {
277         case BANDWIDTH_6_MHZ:
278                 cx22702_writereg(state, 0x0C,
279                         (cx22702_readreg(state, 0x0C) & 0xcf) | 0x20);
280                 break;
281         case BANDWIDTH_7_MHZ:
282                 cx22702_writereg(state, 0x0C,
283                         (cx22702_readreg(state, 0x0C) & 0xcf) | 0x10);
284                 break;
285         case BANDWIDTH_8_MHZ:
286                 cx22702_writereg(state, 0x0C,
287                         cx22702_readreg(state, 0x0C) & 0xcf);
288                 break;
289         default:
290                 dprintk("%s: invalid bandwidth\n", __func__);
291                 return -EINVAL;
292         }
293
294         p->u.ofdm.code_rate_LP = FEC_AUTO; /* temp hack as manual not working */
295
296         /* use auto configuration? */
297         if ((p->u.ofdm.hierarchy_information == HIERARCHY_AUTO) ||
298            (p->u.ofdm.constellation == QAM_AUTO) ||
299            (p->u.ofdm.code_rate_HP == FEC_AUTO) ||
300            (p->u.ofdm.code_rate_LP == FEC_AUTO) ||
301            (p->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO) ||
302            (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO)) {
303
304                 /* TPS Source - use hardware driven values */
305                 cx22702_writereg(state, 0x06, 0x10);
306                 cx22702_writereg(state, 0x07, 0x9);
307                 cx22702_writereg(state, 0x08, 0xC1);
308                 cx22702_writereg(state, 0x0B, cx22702_readreg(state, 0x0B)
309                         & 0xfc);
310                 cx22702_writereg(state, 0x0C,
311                         (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40);
312                 cx22702_writereg(state, 0x00, 0x01); /* Begin aquisition */
313                 dprintk("%s: Autodetecting\n", __func__);
314                 return 0;
315         }
316
317         /* manually programmed values */
318         val = 0;
319         switch (p->u.ofdm.constellation) {
320         case QPSK:
321                 val = (val & 0xe7);
322                 break;
323         case QAM_16:
324                 val = (val & 0xe7) | 0x08;
325                 break;
326         case QAM_64:
327                 val = (val & 0xe7) | 0x10;
328                 break;
329         default:
330                 dprintk("%s: invalid constellation\n", __func__);
331                 return -EINVAL;
332         }
333         switch (p->u.ofdm.hierarchy_information) {
334         case HIERARCHY_NONE:
335                 val = (val & 0xf8);
336                 break;
337         case HIERARCHY_1:
338                 val = (val & 0xf8) | 1;
339                 break;
340         case HIERARCHY_2:
341                 val = (val & 0xf8) | 2;
342                 break;
343         case HIERARCHY_4:
344                 val = (val & 0xf8) | 3;
345                 break;
346         default:
347                 dprintk("%s: invalid hierarchy\n", __func__);
348                 return -EINVAL;
349         }
350         cx22702_writereg(state, 0x06, val);
351
352         val = 0;
353         switch (p->u.ofdm.code_rate_HP) {
354         case FEC_NONE:
355         case FEC_1_2:
356                 val = (val & 0xc7);
357                 break;
358         case FEC_2_3:
359                 val = (val & 0xc7) | 0x08;
360                 break;
361         case FEC_3_4:
362                 val = (val & 0xc7) | 0x10;
363                 break;
364         case FEC_5_6:
365                 val = (val & 0xc7) | 0x18;
366                 break;
367         case FEC_7_8:
368                 val = (val & 0xc7) | 0x20;
369                 break;
370         default:
371                 dprintk("%s: invalid code_rate_HP\n", __func__);
372                 return -EINVAL;
373         }
374         switch (p->u.ofdm.code_rate_LP) {
375         case FEC_NONE:
376         case FEC_1_2:
377                 val = (val & 0xf8);
378                 break;
379         case FEC_2_3:
380                 val = (val & 0xf8) | 1;
381                 break;
382         case FEC_3_4:
383                 val = (val & 0xf8) | 2;
384                 break;
385         case FEC_5_6:
386                 val = (val & 0xf8) | 3;
387                 break;
388         case FEC_7_8:
389                 val = (val & 0xf8) | 4;
390                 break;
391         default:
392                 dprintk("%s: invalid code_rate_LP\n", __func__);
393                 return -EINVAL;
394         }
395         cx22702_writereg(state, 0x07, val);
396
397         val = 0;
398         switch (p->u.ofdm.guard_interval) {
399         case GUARD_INTERVAL_1_32:
400                 val = (val & 0xf3);
401                 break;
402         case GUARD_INTERVAL_1_16:
403                 val = (val & 0xf3) | 0x04;
404                 break;
405         case GUARD_INTERVAL_1_8:
406                 val = (val & 0xf3) | 0x08;
407                 break;
408         case GUARD_INTERVAL_1_4:
409                 val = (val & 0xf3) | 0x0c;
410                 break;
411         default:
412                 dprintk("%s: invalid guard_interval\n", __func__);
413                 return -EINVAL;
414         }
415         switch (p->u.ofdm.transmission_mode) {
416         case TRANSMISSION_MODE_2K:
417                 val = (val & 0xfc);
418                 break;
419         case TRANSMISSION_MODE_8K:
420                 val = (val & 0xfc) | 1;
421                 break;
422         default:
423                 dprintk("%s: invalid transmission_mode\n", __func__);
424                 return -EINVAL;
425         }
426         cx22702_writereg(state, 0x08, val);
427         cx22702_writereg(state, 0x0B,
428                 (cx22702_readreg(state, 0x0B) & 0xfc) | 0x02);
429         cx22702_writereg(state, 0x0C,
430                 (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40);
431
432         /* Begin channel aquisition */
433         cx22702_writereg(state, 0x00, 0x01);
434
435         return 0;
436 }
437
438 /* Reset the demod hardware and reset all of the configuration registers
439    to a default state. */
440 static int cx22702_init(struct dvb_frontend *fe)
441 {
442         int i;
443         struct cx22702_state *state = fe->demodulator_priv;
444
445         cx22702_writereg(state, 0x00, 0x02);
446
447         msleep(10);
448
449         for (i = 0; i < ARRAY_SIZE(init_tab); i += 2)
450                 cx22702_writereg(state, init_tab[i], init_tab[i + 1]);
451
452         cx22702_writereg(state, 0xf8, (state->config->output_mode << 1)
453                 & 0x02);
454
455         cx22702_i2c_gate_ctrl(fe, 0);
456
457         return 0;
458 }
459
460 static int cx22702_read_status(struct dvb_frontend *fe, fe_status_t *status)
461 {
462         struct cx22702_state *state = fe->demodulator_priv;
463         u8 reg0A;
464         u8 reg23;
465
466         *status = 0;
467
468         reg0A = cx22702_readreg(state, 0x0A);
469         reg23 = cx22702_readreg(state, 0x23);
470
471         dprintk("%s: status demod=0x%02x agc=0x%02x\n"
472                 , __func__, reg0A, reg23);
473
474         if (reg0A & 0x10) {
475                 *status |= FE_HAS_LOCK;
476                 *status |= FE_HAS_VITERBI;
477                 *status |= FE_HAS_SYNC;
478         }
479
480         if (reg0A & 0x20)
481                 *status |= FE_HAS_CARRIER;
482
483         if (reg23 < 0xf0)
484                 *status |= FE_HAS_SIGNAL;
485
486         return 0;
487 }
488
489 static int cx22702_read_ber(struct dvb_frontend *fe, u32 *ber)
490 {
491         struct cx22702_state *state = fe->demodulator_priv;
492
493         if (cx22702_readreg(state, 0xE4) & 0x02) {
494                 /* Realtime statistics */
495                 *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
496                         | (cx22702_readreg(state, 0xDF) & 0x7F);
497         } else {
498                 /* Averagtine statistics */
499                 *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
500                         | cx22702_readreg(state, 0xDF);
501         }
502
503         return 0;
504 }
505
506 static int cx22702_read_signal_strength(struct dvb_frontend *fe,
507         u16 *signal_strength)
508 {
509         struct cx22702_state *state = fe->demodulator_priv;
510
511         u16 rs_ber;
512         rs_ber = cx22702_readreg(state, 0x23);
513         *signal_strength = (rs_ber << 8) | rs_ber;
514
515         return 0;
516 }
517
518 static int cx22702_read_snr(struct dvb_frontend *fe, u16 *snr)
519 {
520         struct cx22702_state *state = fe->demodulator_priv;
521
522         u16 rs_ber;
523         if (cx22702_readreg(state, 0xE4) & 0x02) {
524                 /* Realtime statistics */
525                 rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
526                         | (cx22702_readreg(state, 0xDF) & 0x7F);
527         } else {
528                 /* Averagine statistics */
529                 rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 8
530                         | cx22702_readreg(state, 0xDF);
531         }
532         *snr = ~rs_ber;
533
534         return 0;
535 }
536
537 static int cx22702_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
538 {
539         struct cx22702_state *state = fe->demodulator_priv;
540
541         u8 _ucblocks;
542
543         /* RS Uncorrectable Packet Count then reset */
544         _ucblocks = cx22702_readreg(state, 0xE3);
545         if (state->prevUCBlocks < _ucblocks)
546                 *ucblocks = (_ucblocks - state->prevUCBlocks);
547         else
548                 *ucblocks = state->prevUCBlocks - _ucblocks;
549         state->prevUCBlocks = _ucblocks;
550
551         return 0;
552 }
553
554 static int cx22702_get_frontend(struct dvb_frontend *fe,
555         struct dvb_frontend_parameters *p)
556 {
557         struct cx22702_state *state = fe->demodulator_priv;
558
559         u8 reg0C = cx22702_readreg(state, 0x0C);
560
561         p->inversion = reg0C & 0x1 ? INVERSION_ON : INVERSION_OFF;
562         return cx22702_get_tps(state, &p->u.ofdm);
563 }
564
565 static int cx22702_get_tune_settings(struct dvb_frontend *fe,
566         struct dvb_frontend_tune_settings *tune)
567 {
568         tune->min_delay_ms = 1000;
569         return 0;
570 }
571
572 static void cx22702_release(struct dvb_frontend *fe)
573 {
574         struct cx22702_state *state = fe->demodulator_priv;
575         kfree(state);
576 }
577
578 static struct dvb_frontend_ops cx22702_ops;
579
580 struct dvb_frontend *cx22702_attach(const struct cx22702_config *config,
581         struct i2c_adapter *i2c)
582 {
583         struct cx22702_state *state = NULL;
584
585         /* allocate memory for the internal state */
586         state = kzalloc(sizeof(struct cx22702_state), GFP_KERNEL);
587         if (state == NULL)
588                 goto error;
589
590         /* setup the state */
591         state->config = config;
592         state->i2c = i2c;
593
594         /* check if the demod is there */
595         if (cx22702_readreg(state, 0x1f) != 0x3)
596                 goto error;
597
598         /* create dvb_frontend */
599         memcpy(&state->frontend.ops, &cx22702_ops,
600                 sizeof(struct dvb_frontend_ops));
601         state->frontend.demodulator_priv = state;
602         return &state->frontend;
603
604 error:
605         kfree(state);
606         return NULL;
607 }
608 EXPORT_SYMBOL(cx22702_attach);
609
610 static struct dvb_frontend_ops cx22702_ops = {
611
612         .info = {
613                 .name                   = "Conexant CX22702 DVB-T",
614                 .type                   = FE_OFDM,
615                 .frequency_min          = 177000000,
616                 .frequency_max          = 858000000,
617                 .frequency_stepsize     = 166666,
618                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
619                 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
620                 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
621                 FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
622                 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
623         },
624
625         .release = cx22702_release,
626
627         .init = cx22702_init,
628         .i2c_gate_ctrl = cx22702_i2c_gate_ctrl,
629
630         .set_frontend = cx22702_set_tps,
631         .get_frontend = cx22702_get_frontend,
632         .get_tune_settings = cx22702_get_tune_settings,
633
634         .read_status = cx22702_read_status,
635         .read_ber = cx22702_read_ber,
636         .read_signal_strength = cx22702_read_signal_strength,
637         .read_snr = cx22702_read_snr,
638         .read_ucblocks = cx22702_read_ucblocks,
639 };
640
641 MODULE_DESCRIPTION("Conexant CX22702 DVB-T Demodulator driver");
642 MODULE_AUTHOR("Steven Toth");
643 MODULE_LICENSE("GPL");