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