1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/slab.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
29
30#include "dvb_frontend.h"
31#include "cx24110.h"
32
33
34struct cx24110_state {
35
36 struct i2c_adapter* i2c;
37
38 const struct cx24110_config* config;
39
40 struct dvb_frontend frontend;
41
42 u32 lastber;
43 u32 lastbler;
44 u32 lastesn0;
45};
46
47static int debug;
48#define dprintk(args...) \
49 do { \
50 if (debug) printk(KERN_DEBUG "cx24110: " args); \
51 } while (0)
52
53static struct {u8 reg; u8 data;} cx24110_regdata[]=
54
55
56 {{0x09,0x01},
57 {0x09,0x00},
58 {0x01,0xe8},
59 {0x02,0x17},
60 {0x03,0x29},
61 {0x05,0x03},
62 {0x06,0xa5},
63 {0x07,0x01},
64 {0x0a,0x00},
65 {0x0b,0x01},
66
67 {0x0c,0x11},
68 {0x0d,0x6f},
69 {0x10,0x40},
70
71
72 {0x15,0xff},
73
74
75 {0x16,0x00},
76 {0x17,0x04},
77 {0x18,0xae},
78
79
80
81 {0x21,0x10},
82
83 {0x23,0x18},
84
85
86 {0x24,0x24},
87
88
89 {0x35,0x40},
90 {0x36,0xff},
91 {0x37,0x00},
92 {0x38,0x07},
93
94
95 {0x41,0x00},
96 {0x42,0x00},
97 {0x43,0x00},
98
99
100 {0x56,0x4d},
101
102 {0x57,0x00},
103 {0x61,0x95},
104 {0x62,0x05},
105 {0x63,0x00},
106 {0x64,0x20},
107 {0x6d,0x30},
108 {0x70,0x15},
109 {0x73,0x00},
110 {0x74,0x00},
111 {0x75,0x00}
112
113 };
114
115
116static int cx24110_writereg (struct cx24110_state* state, int reg, int data)
117{
118 u8 buf [] = { reg, data };
119 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
120 int err;
121
122 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
123 dprintk ("%s: writereg error (err == %i, reg == 0x%02x,"
124 " data == 0x%02x)\n", __func__, err, reg, data);
125 return -EREMOTEIO;
126 }
127
128 return 0;
129}
130
131static int cx24110_readreg (struct cx24110_state* state, u8 reg)
132{
133 int ret;
134 u8 b0 [] = { reg };
135 u8 b1 [] = { 0 };
136 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
137 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
138
139 ret = i2c_transfer(state->i2c, msg, 2);
140
141 if (ret != 2) return ret;
142
143 return b1[0];
144}
145
146static int cx24110_set_inversion (struct cx24110_state* state, fe_spectral_inversion_t inversion)
147{
148
149
150 switch (inversion) {
151 case INVERSION_OFF:
152 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x1);
153
154 cx24110_writereg(state,0x5,cx24110_readreg(state,0x5)&0xf7);
155
156 cx24110_writereg(state,0x22,cx24110_readreg(state,0x22)&0xef);
157
158
159
160 break;
161 case INVERSION_ON:
162 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x1);
163
164 cx24110_writereg(state,0x5,cx24110_readreg(state,0x5)|0x08);
165
166 cx24110_writereg(state,0x22,cx24110_readreg(state,0x22)|0x10);
167
168 break;
169 case INVERSION_AUTO:
170 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)&0xfe);
171
172 break;
173 default:
174 return -EINVAL;
175 }
176
177 return 0;
178}
179
180static int cx24110_set_fec (struct cx24110_state* state, fe_code_rate_t fec)
181{
182
183
184 static const int rate[]={-1,1,2,3,5,7,-1};
185 static const int g1[]={-1,0x01,0x02,0x05,0x15,0x45,-1};
186 static const int g2[]={-1,0x01,0x03,0x06,0x1a,0x7a,-1};
187
188
189
190
191
192 if (fec>FEC_AUTO)
193 fec=FEC_AUTO;
194
195 if (fec==FEC_AUTO) {
196 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)&0xdf);
197
198 cx24110_writereg(state,0x18,0xae);
199
200 cx24110_writereg(state,0x05,(cx24110_readreg(state,0x05)&0xf0)|0x3);
201
202 cx24110_writereg(state,0x22,(cx24110_readreg(state,0x22)&0xf0)|0x3);
203
204 cx24110_writereg(state,0x1a,0x05); cx24110_writereg(state,0x1b,0x06);
205
206 return 0;
207 } else {
208 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x20);
209
210 if(rate[fec]>0) {
211 cx24110_writereg(state,0x05,(cx24110_readreg(state,0x05)&0xf0)|rate[fec]);
212
213 cx24110_writereg(state,0x22,(cx24110_readreg(state,0x22)&0xf0)|rate[fec]);
214
215 cx24110_writereg(state,0x1a,g1[fec]);
216 cx24110_writereg(state,0x1b,g2[fec]);
217
218 } else
219 return -EOPNOTSUPP;
220
221 }
222 return 0;
223}
224
225static fe_code_rate_t cx24110_get_fec (struct cx24110_state* state)
226{
227 int i;
228
229 i=cx24110_readreg(state,0x22)&0x0f;
230 if(!(i&0x08)) {
231 return FEC_1_2 + i - 1;
232 } else {
233
234
235
236
237 return FEC_NONE;
238 }
239}
240
241static int cx24110_set_symbolrate (struct cx24110_state* state, u32 srate)
242{
243
244 u32 ratio;
245 u32 tmp, fclk, BDRI;
246
247 static const u32 bands[]={5000000UL,15000000UL,90999000UL/2};
248 int i;
249
250 dprintk("cx24110 debug: entering %s(%d)\n",__func__,srate);
251 if (srate>90999000UL/2)
252 srate=90999000UL/2;
253 if (srate<500000)
254 srate=500000;
255
256 for(i = 0; (i < ARRAY_SIZE(bands)) && (srate>bands[i]); i++)
257 ;
258
259
260
261 tmp=cx24110_readreg(state,0x07)&0xfc;
262 if(srate<90999000UL/4) {
263 cx24110_writereg(state,0x07,tmp);
264 cx24110_writereg(state,0x06,0x78);
265 fclk=90999000UL/2;
266 } else if(srate<60666000UL/2) {
267 cx24110_writereg(state,0x07,tmp|0x1);
268 cx24110_writereg(state,0x06,0xa5);
269 fclk=60666000UL;
270 } else if(srate<80888000UL/2) {
271 cx24110_writereg(state,0x07,tmp|0x2);
272 cx24110_writereg(state,0x06,0x87);
273 fclk=80888000UL;
274 } else {
275 cx24110_writereg(state,0x07,tmp|0x3);
276 cx24110_writereg(state,0x06,0x78);
277 fclk=90999000UL;
278 }
279 dprintk("cx24110 debug: fclk %d Hz\n",fclk);
280
281
282
283
284
285
286
287
288 tmp=srate<<6;
289 BDRI=fclk>>2;
290 ratio=(tmp/BDRI);
291
292 tmp=(tmp%BDRI)<<8;
293 ratio=(ratio<<8)+(tmp/BDRI);
294
295 tmp=(tmp%BDRI)<<8;
296 ratio=(ratio<<8)+(tmp/BDRI);
297
298 tmp=(tmp%BDRI)<<1;
299 ratio=(ratio<<1)+(tmp/BDRI);
300
301 dprintk("srate= %d (range %d, up to %d)\n", srate,i,bands[i]);
302 dprintk("fclk = %d\n", fclk);
303 dprintk("ratio= %08x\n", ratio);
304
305 cx24110_writereg(state, 0x1, (ratio>>16)&0xff);
306 cx24110_writereg(state, 0x2, (ratio>>8)&0xff);
307 cx24110_writereg(state, 0x3, (ratio)&0xff);
308
309 return 0;
310
311}
312
313static int _cx24110_pll_write (struct dvb_frontend* fe, const u8 buf[], int len)
314{
315 struct cx24110_state *state = fe->demodulator_priv;
316
317 if (len != 3)
318 return -EINVAL;
319
320
321
322
323
324 cx24110_writereg(state,0x6d,0x30);
325 cx24110_writereg(state,0x70,0x15);
326
327
328 while (cx24110_readreg(state,0x6d)&0x80)
329 cx24110_writereg(state,0x72,0);
330
331
332 cx24110_writereg(state,0x72,buf[0]);
333
334
335 while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
336 ;
337
338
339 cx24110_writereg(state,0x72,buf[1]);
340 while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
341 ;
342
343
344 cx24110_writereg(state,0x72,buf[2]);
345 while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
346 ;
347
348
349 cx24110_writereg(state,0x6d,0x32);
350 cx24110_writereg(state,0x6d,0x30);
351
352 return 0;
353}
354
355static int cx24110_initfe(struct dvb_frontend* fe)
356{
357 struct cx24110_state *state = fe->demodulator_priv;
358
359 int i;
360
361 dprintk("%s: init chip\n", __func__);
362
363 for(i = 0; i < ARRAY_SIZE(cx24110_regdata); i++) {
364 cx24110_writereg(state, cx24110_regdata[i].reg, cx24110_regdata[i].data);
365 }
366
367 return 0;
368}
369
370static int cx24110_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
371{
372 struct cx24110_state *state = fe->demodulator_priv;
373
374 switch (voltage) {
375 case SEC_VOLTAGE_13:
376 return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&0x3b)|0xc0);
377 case SEC_VOLTAGE_18:
378 return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&0x3b)|0x40);
379 default:
380 return -EINVAL;
381 };
382}
383
384static int cx24110_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
385{
386 int rv, bit;
387 struct cx24110_state *state = fe->demodulator_priv;
388 unsigned long timeout;
389
390 if (burst == SEC_MINI_A)
391 bit = 0x00;
392 else if (burst == SEC_MINI_B)
393 bit = 0x08;
394 else
395 return -EINVAL;
396
397 rv = cx24110_readreg(state, 0x77);
398 if (!(rv & 0x04))
399 cx24110_writereg(state, 0x77, rv | 0x04);
400
401 rv = cx24110_readreg(state, 0x76);
402 cx24110_writereg(state, 0x76, ((rv & 0x90) | 0x40 | bit));
403 timeout = jiffies + msecs_to_jiffies(100);
404 while (!time_after(jiffies, timeout) && !(cx24110_readreg(state, 0x76) & 0x40))
405 ;
406
407 return 0;
408}
409
410static int cx24110_send_diseqc_msg(struct dvb_frontend* fe,
411 struct dvb_diseqc_master_cmd *cmd)
412{
413 int i, rv;
414 struct cx24110_state *state = fe->demodulator_priv;
415 unsigned long timeout;
416
417 if (cmd->msg_len < 3 || cmd->msg_len > 6)
418 return -EINVAL;
419
420 for (i = 0; i < cmd->msg_len; i++)
421 cx24110_writereg(state, 0x79 + i, cmd->msg[i]);
422
423 rv = cx24110_readreg(state, 0x77);
424 if (rv & 0x04) {
425 cx24110_writereg(state, 0x77, rv & ~0x04);
426 msleep(30);
427 }
428
429 rv = cx24110_readreg(state, 0x76);
430
431 cx24110_writereg(state, 0x76, ((rv & 0x90) | 0x40) | ((cmd->msg_len-3) & 3));
432 timeout = jiffies + msecs_to_jiffies(100);
433 while (!time_after(jiffies, timeout) && !(cx24110_readreg(state, 0x76) & 0x40))
434 ;
435
436 return 0;
437}
438
439static int cx24110_read_status(struct dvb_frontend* fe, fe_status_t* status)
440{
441 struct cx24110_state *state = fe->demodulator_priv;
442
443 int sync = cx24110_readreg (state, 0x55);
444
445 *status = 0;
446
447 if (sync & 0x10)
448 *status |= FE_HAS_SIGNAL;
449
450 if (sync & 0x08)
451 *status |= FE_HAS_CARRIER;
452
453 sync = cx24110_readreg (state, 0x08);
454
455 if (sync & 0x40)
456 *status |= FE_HAS_VITERBI;
457
458 if (sync & 0x20)
459 *status |= FE_HAS_SYNC;
460
461 if ((sync & 0x60) == 0x60)
462 *status |= FE_HAS_LOCK;
463
464 return 0;
465}
466
467static int cx24110_read_ber(struct dvb_frontend* fe, u32* ber)
468{
469 struct cx24110_state *state = fe->demodulator_priv;
470
471
472 if(cx24110_readreg(state,0x24)&0x10) {
473
474 cx24110_writereg(state,0x24,0x04);
475 state->lastber=cx24110_readreg(state,0x25)|
476 (cx24110_readreg(state,0x26)<<8);
477 cx24110_writereg(state,0x24,0x04);
478 cx24110_writereg(state,0x24,0x14);
479 }
480 *ber = state->lastber;
481
482 return 0;
483}
484
485static int cx24110_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
486{
487 struct cx24110_state *state = fe->demodulator_priv;
488
489
490 u8 signal = cx24110_readreg (state, 0x27)+128;
491 *signal_strength = (signal << 8) | signal;
492
493 return 0;
494}
495
496static int cx24110_read_snr(struct dvb_frontend* fe, u16* snr)
497{
498 struct cx24110_state *state = fe->demodulator_priv;
499
500
501 if(cx24110_readreg(state,0x6a)&0x80) {
502
503 state->lastesn0=cx24110_readreg(state,0x69)|
504 (cx24110_readreg(state,0x68)<<8);
505 cx24110_writereg(state,0x6a,0x84);
506 }
507 *snr = state->lastesn0;
508
509 return 0;
510}
511
512static int cx24110_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
513{
514 struct cx24110_state *state = fe->demodulator_priv;
515
516 if(cx24110_readreg(state,0x10)&0x40) {
517
518 cx24110_writereg(state,0x10,0x60);
519 (void)(cx24110_readreg(state, 0x12) |
520 (cx24110_readreg(state, 0x13) << 8) |
521 (cx24110_readreg(state, 0x14) << 16));
522 cx24110_writereg(state,0x10,0x70);
523 state->lastbler=cx24110_readreg(state,0x12)|
524 (cx24110_readreg(state,0x13)<<8)|
525 (cx24110_readreg(state,0x14)<<16);
526 cx24110_writereg(state,0x10,0x20);
527 }
528 *ucblocks = state->lastbler;
529
530 return 0;
531}
532
533static int cx24110_set_frontend(struct dvb_frontend *fe)
534{
535 struct cx24110_state *state = fe->demodulator_priv;
536 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
537
538 if (fe->ops.tuner_ops.set_params) {
539 fe->ops.tuner_ops.set_params(fe);
540 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
541 }
542
543 cx24110_set_inversion(state, p->inversion);
544 cx24110_set_fec(state, p->fec_inner);
545 cx24110_set_symbolrate(state, p->symbol_rate);
546 cx24110_writereg(state,0x04,0x05);
547
548 return 0;
549}
550
551static int cx24110_get_frontend(struct dvb_frontend *fe)
552{
553 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
554 struct cx24110_state *state = fe->demodulator_priv;
555 s32 afc; unsigned sclk;
556
557
558
559 sclk = cx24110_readreg (state, 0x07) & 0x03;
560
561
562 if (sclk==0) sclk=90999000L/2L;
563 else if (sclk==1) sclk=60666000L;
564 else if (sclk==2) sclk=80888000L;
565 else sclk=90999000L;
566 sclk>>=8;
567 afc = sclk*(cx24110_readreg (state, 0x44)&0x1f)+
568 ((sclk*cx24110_readreg (state, 0x45))>>8)+
569 ((sclk*cx24110_readreg (state, 0x46))>>16);
570
571 p->frequency += afc;
572 p->inversion = (cx24110_readreg (state, 0x22) & 0x10) ?
573 INVERSION_ON : INVERSION_OFF;
574 p->fec_inner = cx24110_get_fec(state);
575
576 return 0;
577}
578
579static int cx24110_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
580{
581 struct cx24110_state *state = fe->demodulator_priv;
582
583 return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&~0x10)|(((tone==SEC_TONE_ON))?0x10:0));
584}
585
586static void cx24110_release(struct dvb_frontend* fe)
587{
588 struct cx24110_state* state = fe->demodulator_priv;
589 kfree(state);
590}
591
592static struct dvb_frontend_ops cx24110_ops;
593
594struct dvb_frontend* cx24110_attach(const struct cx24110_config* config,
595 struct i2c_adapter* i2c)
596{
597 struct cx24110_state* state = NULL;
598 int ret;
599
600
601 state = kzalloc(sizeof(struct cx24110_state), GFP_KERNEL);
602 if (state == NULL) goto error;
603
604
605 state->config = config;
606 state->i2c = i2c;
607 state->lastber = 0;
608 state->lastbler = 0;
609 state->lastesn0 = 0;
610
611
612 ret = cx24110_readreg(state, 0x00);
613 if ((ret != 0x5a) && (ret != 0x69)) goto error;
614
615
616 memcpy(&state->frontend.ops, &cx24110_ops, sizeof(struct dvb_frontend_ops));
617 state->frontend.demodulator_priv = state;
618 return &state->frontend;
619
620error:
621 kfree(state);
622 return NULL;
623}
624
625static struct dvb_frontend_ops cx24110_ops = {
626 .delsys = { SYS_DVBS },
627 .info = {
628 .name = "Conexant CX24110 DVB-S",
629 .frequency_min = 950000,
630 .frequency_max = 2150000,
631 .frequency_stepsize = 1011,
632 .frequency_tolerance = 29500,
633 .symbol_rate_min = 1000000,
634 .symbol_rate_max = 45000000,
635 .caps = FE_CAN_INVERSION_AUTO |
636 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
637 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
638 FE_CAN_QPSK | FE_CAN_RECOVER
639 },
640
641 .release = cx24110_release,
642
643 .init = cx24110_initfe,
644 .write = _cx24110_pll_write,
645 .set_frontend = cx24110_set_frontend,
646 .get_frontend = cx24110_get_frontend,
647 .read_status = cx24110_read_status,
648 .read_ber = cx24110_read_ber,
649 .read_signal_strength = cx24110_read_signal_strength,
650 .read_snr = cx24110_read_snr,
651 .read_ucblocks = cx24110_read_ucblocks,
652
653 .diseqc_send_master_cmd = cx24110_send_diseqc_msg,
654 .set_tone = cx24110_set_tone,
655 .set_voltage = cx24110_set_voltage,
656 .diseqc_send_burst = cx24110_diseqc_send_burst,
657};
658
659module_param(debug, int, 0644);
660MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
661
662MODULE_DESCRIPTION("Conexant CX24110 DVB-S Demodulator driver");
663MODULE_AUTHOR("Peter Hettkamp");
664MODULE_LICENSE("GPL");
665
666EXPORT_SYMBOL(cx24110_attach);
667