1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/init.h>
23#include <linux/jiffies.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/string.h>
28
29#include <linux/dvb/frontend.h>
30#include "dvb_frontend.h"
31
32#include "stb0899_drv.h"
33#include "stb0899_priv.h"
34#include "stb0899_reg.h"
35
36
37#define MAX_XFER_SIZE 64
38
39static unsigned int verbose = 0;
40module_param(verbose, int, 0644);
41
42
43static const struct stb0899_tab stb0899_cn_tab[] = {
44 { 200, 2600 },
45 { 190, 2700 },
46 { 180, 2860 },
47 { 170, 3020 },
48 { 160, 3210 },
49 { 150, 3440 },
50 { 140, 3710 },
51 { 130, 4010 },
52 { 120, 4360 },
53 { 110, 4740 },
54 { 100, 5190 },
55 { 90, 5670 },
56 { 80, 6200 },
57 { 70, 6770 },
58 { 60, 7360 },
59 { 50, 7970 },
60 { 40, 8250 },
61 { 30, 9000 },
62 { 20, 9450 },
63 { 15, 9600 },
64};
65
66
67
68
69
70
71
72
73static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
74 { -750, -128 },
75 { -748, -94 },
76 { -745, -92 },
77 { -735, -90 },
78 { -720, -87 },
79 { -670, -77 },
80 { -640, -70 },
81 { -610, -62 },
82 { -600, -60 },
83 { -590, -56 },
84 { -560, -41 },
85 { -540, -25 },
86 { -530, -17 },
87 { -520, -11 },
88 { -500, 1 },
89 { -490, 6 },
90 { -480, 10 },
91 { -440, 22 },
92 { -420, 27 },
93 { -400, 31 },
94 { -380, 34 },
95 { -340, 40 },
96 { -320, 43 },
97 { -280, 48 },
98 { -250, 52 },
99 { -230, 55 },
100 { -180, 61 },
101 { -140, 66 },
102 { -90, 73 },
103 { -80, 74 },
104 { 500, 127 }
105};
106
107
108
109
110
111
112
113
114static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
115 { 700, 0 },
116 { -80, 3217 },
117 { -150, 3893 },
118 { -190, 4217 },
119 { -240, 4621 },
120 { -280, 4945 },
121 { -320, 5273 },
122 { -350, 5545 },
123 { -370, 5741 },
124 { -410, 6147 },
125 { -450, 6671 },
126 { -490, 7413 },
127 { -501, 7665 },
128 { -530, 8767 },
129 { -560, 10219 },
130 { -580, 10939 },
131 { -590, 11518 },
132 { -600, 11723 },
133 { -650, 12659 },
134 { -690, 13219 },
135 { -730, 13645 },
136 { -750, 13909 },
137 { -766, 14153 },
138 { -950, 16383 }
139};
140
141
142static struct stb0899_tab stb0899_quant_tab[] = {
143 { 0, 0 },
144 { 0, 100 },
145 { 600, 200 },
146 { 950, 299 },
147 { 1200, 398 },
148 { 1400, 501 },
149 { 1560, 603 },
150 { 1690, 700 },
151 { 1810, 804 },
152 { 1910, 902 },
153 { 2000, 1000 },
154 { 2080, 1096 },
155 { 2160, 1202 },
156 { 2230, 1303 },
157 { 2350, 1496 },
158 { 2410, 1603 },
159 { 2460, 1698 },
160 { 2510, 1799 },
161 { 2600, 1995 },
162 { 2650, 2113 },
163 { 2690, 2213 },
164 { 2720, 2291 },
165 { 2760, 2399 },
166 { 2800, 2512 },
167 { 2860, 2692 },
168 { 2930, 2917 },
169 { 2960, 3020 },
170 { 3010, 3199 },
171 { 3040, 3311 },
172 { 3060, 3388 },
173 { 3120, 3631 },
174 { 3190, 3936 },
175 { 3400, 5012 },
176 { 3610, 6383 },
177 { 3800, 7943 },
178 { 4210, 12735 },
179 { 4500, 17783 },
180 { 4690, 22131 },
181 { 4810, 25410 }
182};
183
184
185static struct stb0899_tab stb0899_est_tab[] = {
186 { 0, 0 },
187 { 0, 1 },
188 { 301, 2 },
189 { 1204, 16 },
190 { 1806, 64 },
191 { 2408, 256 },
192 { 2709, 512 },
193 { 3010, 1023 },
194 { 3311, 2046 },
195 { 3612, 4093 },
196 { 3823, 6653 },
197 { 3913, 8185 },
198 { 4010, 10233 },
199 { 4107, 12794 },
200 { 4214, 16368 },
201 { 4266, 18450 },
202 { 4311, 20464 },
203 { 4353, 22542 },
204 { 4391, 24604 },
205 { 4425, 26607 },
206 { 4457, 28642 },
207 { 4487, 30690 },
208 { 4515, 32734 },
209 { 4612, 40926 },
210 { 4692, 49204 },
211 { 4816, 65464 },
212 { 4913, 81846 },
213 { 4993, 98401 },
214 { 5060, 114815 },
215 { 5118, 131220 },
216 { 5200, 158489 },
217 { 5300, 199526 },
218 { 5400, 251189 },
219 { 5500, 316228 },
220 { 5600, 398107 },
221 { 5720, 524807 },
222 { 5721, 526017 },
223};
224
225static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
226{
227 int ret;
228
229 u8 b0[] = { reg >> 8, reg & 0xff };
230 u8 buf;
231
232 struct i2c_msg msg[] = {
233 {
234 .addr = state->config->demod_address,
235 .flags = 0,
236 .buf = b0,
237 .len = 2
238 },{
239 .addr = state->config->demod_address,
240 .flags = I2C_M_RD,
241 .buf = &buf,
242 .len = 1
243 }
244 };
245
246 ret = i2c_transfer(state->i2c, msg, 2);
247 if (ret != 2) {
248 if (ret != -ERESTARTSYS)
249 dprintk(state->verbose, FE_ERROR, 1,
250 "Read error, Reg=[0x%02x], Status=%d",
251 reg, ret);
252
253 return ret < 0 ? ret : -EREMOTEIO;
254 }
255 if (unlikely(*state->verbose >= FE_DEBUGREG))
256 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
257 reg, buf);
258
259 return (unsigned int)buf;
260}
261
262int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
263{
264 int result;
265
266 result = _stb0899_read_reg(state, reg);
267
268
269
270
271
272 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
273 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
274 _stb0899_read_reg(state, (reg | 0x00ff));
275
276 return result;
277}
278
279u32 _stb0899_read_s2reg(struct stb0899_state *state,
280 u32 stb0899_i2cdev,
281 u32 stb0899_base_addr,
282 u16 stb0899_reg_offset)
283{
284 int status;
285 u32 data;
286 u8 buf[7] = { 0 };
287 u16 tmpaddr;
288
289 u8 buf_0[] = {
290 GETBYTE(stb0899_i2cdev, BYTE1),
291 GETBYTE(stb0899_i2cdev, BYTE0),
292 GETBYTE(stb0899_base_addr, BYTE0),
293 GETBYTE(stb0899_base_addr, BYTE1),
294 GETBYTE(stb0899_base_addr, BYTE2),
295 GETBYTE(stb0899_base_addr, BYTE3),
296 };
297 u8 buf_1[] = {
298 0x00,
299 0x00,
300 };
301
302 struct i2c_msg msg_0 = {
303 .addr = state->config->demod_address,
304 .flags = 0,
305 .buf = buf_0,
306 .len = 6
307 };
308
309 struct i2c_msg msg_1 = {
310 .addr = state->config->demod_address,
311 .flags = 0,
312 .buf = buf_1,
313 .len = 2
314 };
315
316 struct i2c_msg msg_r = {
317 .addr = state->config->demod_address,
318 .flags = I2C_M_RD,
319 .buf = buf,
320 .len = 4
321 };
322
323 tmpaddr = stb0899_reg_offset & 0xff00;
324 if (!(stb0899_reg_offset & 0x8))
325 tmpaddr = stb0899_reg_offset | 0x20;
326
327 buf_1[0] = GETBYTE(tmpaddr, BYTE1);
328 buf_1[1] = GETBYTE(tmpaddr, BYTE0);
329
330 status = i2c_transfer(state->i2c, &msg_0, 1);
331 if (status < 1) {
332 if (status != -ERESTARTSYS)
333 printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
334 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
335
336 goto err;
337 }
338
339
340 status = i2c_transfer(state->i2c, &msg_1, 1);
341 if (status < 1)
342 goto err;
343
344 status = i2c_transfer(state->i2c, &msg_r, 1);
345 if (status < 1)
346 goto err;
347
348 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
349 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
350
351
352 status = i2c_transfer(state->i2c, &msg_1, 1);
353 if (status < 1) {
354 if (status != -ERESTARTSYS)
355 printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
356 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
357 goto err;
358 }
359
360 status = i2c_transfer(state->i2c, &msg_r, 1);
361 if (status < 1) {
362 if (status != -ERESTARTSYS)
363 printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
364 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
365 return status < 0 ? status : -EREMOTEIO;
366 }
367
368 data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
369 if (unlikely(*state->verbose >= FE_DEBUGREG))
370 printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
371 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
372
373 return data;
374
375err:
376 return status < 0 ? status : -EREMOTEIO;
377}
378
379int stb0899_write_s2reg(struct stb0899_state *state,
380 u32 stb0899_i2cdev,
381 u32 stb0899_base_addr,
382 u16 stb0899_reg_offset,
383 u32 stb0899_data)
384{
385 int status;
386
387
388 u8 buf_0[] = {
389 GETBYTE(stb0899_i2cdev, BYTE1),
390 GETBYTE(stb0899_i2cdev, BYTE0),
391 GETBYTE(stb0899_base_addr, BYTE0),
392 GETBYTE(stb0899_base_addr, BYTE1),
393 GETBYTE(stb0899_base_addr, BYTE2),
394 GETBYTE(stb0899_base_addr, BYTE3),
395 };
396 u8 buf_1[] = {
397 0x00,
398 0x00,
399 0x00,
400 0x00,
401 0x00,
402 0x00,
403 };
404
405 struct i2c_msg msg_0 = {
406 .addr = state->config->demod_address,
407 .flags = 0,
408 .buf = buf_0,
409 .len = 6
410 };
411
412 struct i2c_msg msg_1 = {
413 .addr = state->config->demod_address,
414 .flags = 0,
415 .buf = buf_1,
416 .len = 6
417 };
418
419 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
420 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
421 buf_1[2] = GETBYTE(stb0899_data, BYTE0);
422 buf_1[3] = GETBYTE(stb0899_data, BYTE1);
423 buf_1[4] = GETBYTE(stb0899_data, BYTE2);
424 buf_1[5] = GETBYTE(stb0899_data, BYTE3);
425
426 if (unlikely(*state->verbose >= FE_DEBUGREG))
427 printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
428 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
429
430 status = i2c_transfer(state->i2c, &msg_0, 1);
431 if (unlikely(status < 1)) {
432 if (status != -ERESTARTSYS)
433 printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
434 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
435 goto err;
436 }
437 status = i2c_transfer(state->i2c, &msg_1, 1);
438 if (unlikely(status < 1)) {
439 if (status != -ERESTARTSYS)
440 printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
441 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
442
443 return status < 0 ? status : -EREMOTEIO;
444 }
445
446 return 0;
447
448err:
449 return status < 0 ? status : -EREMOTEIO;
450}
451
452int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
453{
454 int status;
455
456 u8 b0[] = { reg >> 8, reg & 0xff };
457
458 struct i2c_msg msg[] = {
459 {
460 .addr = state->config->demod_address,
461 .flags = 0,
462 .buf = b0,
463 .len = 2
464 },{
465 .addr = state->config->demod_address,
466 .flags = I2C_M_RD,
467 .buf = buf,
468 .len = count
469 }
470 };
471
472 status = i2c_transfer(state->i2c, msg, 2);
473 if (status != 2) {
474 if (status != -ERESTARTSYS)
475 printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
476 __func__, reg, count, status);
477 goto err;
478 }
479
480
481
482
483
484 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
485 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
486 _stb0899_read_reg(state, (reg | 0x00ff));
487
488 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
489 int i;
490
491 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
492 for (i = 0; i < count; i++) {
493 printk(" %02x", buf[i]);
494 }
495 printk("\n");
496 }
497
498 return 0;
499err:
500 return status < 0 ? status : -EREMOTEIO;
501}
502
503int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
504{
505 int ret;
506 u8 buf[MAX_XFER_SIZE];
507 struct i2c_msg i2c_msg = {
508 .addr = state->config->demod_address,
509 .flags = 0,
510 .buf = buf,
511 .len = 2 + count
512 };
513
514 if (2 + count > sizeof(buf)) {
515 printk(KERN_WARNING
516 "%s: i2c wr reg=%04x: len=%d is too big!\n",
517 KBUILD_MODNAME, reg, count);
518 return -EINVAL;
519 }
520
521 buf[0] = reg >> 8;
522 buf[1] = reg & 0xff;
523 memcpy(&buf[2], data, count);
524
525 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
526 int i;
527
528 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
529 for (i = 0; i < count; i++)
530 printk(" %02x", data[i]);
531 printk("\n");
532 }
533 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
534
535
536
537
538
539
540 if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
541 stb0899_read_reg(state, (reg | 0x00ff));
542
543 if (ret != 1) {
544 if (ret != -ERESTARTSYS)
545 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
546 reg, data[0], count, ret);
547 return ret < 0 ? ret : -EREMOTEIO;
548 }
549
550 return 0;
551}
552
553int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
554{
555 return stb0899_write_regs(state, reg, &data, 1);
556}
557
558
559
560
561
562
563static u32 stb0899_get_mclk(struct stb0899_state *state)
564{
565 u32 mclk = 0, div = 0;
566
567 div = stb0899_read_reg(state, STB0899_NCOARSE);
568 mclk = (div + 1) * state->config->xtal_freq / 6;
569 dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
570
571 return mclk;
572}
573
574
575
576
577
578
579
580static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
581{
582 struct stb0899_internal *internal = &state->internal;
583 u8 mdiv = 0;
584
585 dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
586 mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
587 dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
588
589 stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
590 internal->master_clk = stb0899_get_mclk(state);
591
592 dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
593}
594
595static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
596{
597 struct stb0899_config *config = state->config;
598 const struct stb0899_postproc *postproc = config->postproc;
599
600
601 if (postproc) {
602 if (enable) {
603 if (postproc[ctl].level == STB0899_GPIOPULLUP)
604 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
605 else
606 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
607 } else {
608 if (postproc[ctl].level == STB0899_GPIOPULLUP)
609 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
610 else
611 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
612 }
613 }
614 return 0;
615}
616
617static void stb0899_release(struct dvb_frontend *fe)
618{
619 struct stb0899_state *state = fe->demodulator_priv;
620
621 dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
622
623 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
624 kfree(state);
625}
626
627
628
629
630
631static int stb0899_get_alpha(struct stb0899_state *state)
632{
633 u8 mode_coeff;
634
635 mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
636
637 if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
638 return 20;
639 else
640 return 35;
641}
642
643
644
645
646static void stb0899_init_calc(struct stb0899_state *state)
647{
648 struct stb0899_internal *internal = &state->internal;
649 int master_clk;
650 u8 agc[2];
651 u32 reg;
652
653
654 stb0899_read_regs(state, STB0899_AGC1REF, agc, 2);
655
656
657 master_clk = stb0899_get_mclk(state);
658 internal->t_agc1 = 0;
659 internal->t_agc2 = 0;
660 internal->master_clk = master_clk;
661 internal->mclk = master_clk / 65536L;
662 internal->rolloff = stb0899_get_alpha(state);
663
664
665
666 internal->agc_gain = 8154;
667 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
668 STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
669 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
670
671 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
672 internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
673
674 internal->center_freq = 0;
675 internal->av_frame_coarse = 10;
676 internal->av_frame_fine = 20;
677 internal->step_size = 2;
678
679
680
681
682
683
684}
685
686static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
687{
688 u8 reg = 0;
689 unsigned long start = jiffies;
690
691 while (1) {
692 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
693 if (!STB0899_GETFIELD(FIFOFULL, reg))
694 break;
695 if (time_after(jiffies, start + timeout)) {
696 dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
697 return -ETIMEDOUT;
698 }
699 }
700
701 return 0;
702}
703
704static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
705{
706 struct stb0899_state *state = fe->demodulator_priv;
707 u8 reg, i;
708
709 if (cmd->msg_len > sizeof(cmd->msg))
710 return -EINVAL;
711
712
713 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
714 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
715 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
716 for (i = 0; i < cmd->msg_len; i++) {
717
718 if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
719 return -ETIMEDOUT;
720
721 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
722 }
723 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
724 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
725 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
726 msleep(100);
727 return 0;
728}
729
730static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
731{
732 u8 reg = 0;
733 unsigned long start = jiffies;
734
735 while (!STB0899_GETFIELD(RXEND, reg)) {
736 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
737 if (time_after(jiffies, start + timeout)) {
738 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
739 return -ETIMEDOUT;
740 }
741 msleep(10);
742 }
743
744 return 0;
745}
746
747static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
748{
749 struct stb0899_state *state = fe->demodulator_priv;
750 u8 reg, length = 0, i;
751 int result;
752
753 if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
754 return -ETIMEDOUT;
755
756 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
757 if (STB0899_GETFIELD(RXEND, reg)) {
758
759 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
760 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
761
762 if (length > sizeof (reply->msg)) {
763 result = -EOVERFLOW;
764 goto exit;
765 }
766 reply->msg_len = length;
767
768
769 for (i = 0; i < length; i++)
770 reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
771 }
772
773 return 0;
774exit:
775
776 return result;
777}
778
779static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
780{
781 u8 reg = 0;
782 unsigned long start = jiffies;
783
784 while (!STB0899_GETFIELD(TXIDLE, reg)) {
785 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
786 if (time_after(jiffies, start + timeout)) {
787 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
788 return -ETIMEDOUT;
789 }
790 msleep(10);
791 }
792 return 0;
793}
794
795static int stb0899_send_diseqc_burst(struct dvb_frontend *fe,
796 enum fe_sec_mini_cmd burst)
797{
798 struct stb0899_state *state = fe->demodulator_priv;
799 u8 reg, old_state;
800
801
802 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
803 return -ETIMEDOUT;
804
805 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
806 old_state = reg;
807
808 STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
809 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
810 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
811 switch (burst) {
812 case SEC_MINI_A:
813
814 stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
815 break;
816 case SEC_MINI_B:
817
818 stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
819 break;
820 }
821 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
822 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
823 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
824
825 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
826 return -ETIMEDOUT;
827
828
829 stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
830
831 return 0;
832}
833
834static int stb0899_diseqc_init(struct stb0899_state *state)
835{
836
837
838
839 u8 f22_tx, reg;
840
841 u32 mclk, tx_freq = 22000;
842 reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
843 STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
844 stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
845
846
847 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
848 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
849 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
850
851 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
852 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
853 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
854
855 mclk = stb0899_get_mclk(state);
856 f22_tx = mclk / (tx_freq * 32);
857 stb0899_write_reg(state, STB0899_DISF22, f22_tx);
858 state->rx_freq = 20000;
859
860 return 0;
861}
862
863static int stb0899_sleep(struct dvb_frontend *fe)
864{
865 struct stb0899_state *state = fe->demodulator_priv;
866
867
868
869 dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
870
871 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
872
873 return 0;
874}
875
876static int stb0899_wakeup(struct dvb_frontend *fe)
877{
878 int rc;
879 struct stb0899_state *state = fe->demodulator_priv;
880
881 if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
882 return rc;
883
884 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
885 return rc;
886 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
887 return rc;
888
889
890 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
891
892 return 0;
893}
894
895static int stb0899_init(struct dvb_frontend *fe)
896{
897 int i;
898 struct stb0899_state *state = fe->demodulator_priv;
899 struct stb0899_config *config = state->config;
900
901 dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
902
903
904 dprintk(state->verbose, FE_DEBUG, 1, "init device");
905 for (i = 0; config->init_dev[i].address != 0xffff; i++)
906 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
907
908 dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
909
910 for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
911 stb0899_write_s2reg(state, STB0899_S2DEMOD,
912 config->init_s2_demod[i].base_address,
913 config->init_s2_demod[i].offset,
914 config->init_s2_demod[i].data);
915
916 dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
917
918 for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
919 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
920
921 dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
922
923 for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
924 stb0899_write_s2reg(state, STB0899_S2FEC,
925 config->init_s2_fec[i].base_address,
926 config->init_s2_fec[i].offset,
927 config->init_s2_fec[i].data);
928
929 dprintk(state->verbose, FE_DEBUG, 1, "init TST");
930
931 for (i = 0; config->init_tst[i].address != 0xffff; i++)
932 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
933
934 stb0899_init_calc(state);
935 stb0899_diseqc_init(state);
936
937 return 0;
938}
939
940static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
941{
942 int res = 0;
943 int min = 0, med;
944
945 if (val < tab[min].read)
946 res = tab[min].real;
947 else if (val >= tab[max].read)
948 res = tab[max].real;
949 else {
950 while ((max - min) > 1) {
951 med = (max + min) / 2;
952 if (val >= tab[min].read && val < tab[med].read)
953 max = med;
954 else
955 min = med;
956 }
957 res = ((val - tab[min].read) *
958 (tab[max].real - tab[min].real) /
959 (tab[max].read - tab[min].read)) +
960 tab[min].real;
961 }
962
963 return res;
964}
965
966static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
967{
968 struct stb0899_state *state = fe->demodulator_priv;
969 struct stb0899_internal *internal = &state->internal;
970
971 int val;
972 u32 reg;
973 *strength = 0;
974 switch (state->delsys) {
975 case SYS_DVBS:
976 case SYS_DSS:
977 if (internal->lock) {
978 reg = stb0899_read_reg(state, STB0899_VSTATUS);
979 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
980
981 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
982 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
983
984 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
985 *strength += 750;
986 dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
987 val & 0xff, *strength);
988 }
989 }
990 break;
991 case SYS_DVBS2:
992 if (internal->lock) {
993 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN);
994 val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
995
996 *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
997 *strength += 950;
998 dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
999 val & 0x3fff, *strength);
1000 }
1001 break;
1002 default:
1003 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1004 return -EINVAL;
1005 }
1006
1007 return 0;
1008}
1009
1010static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
1011{
1012 struct stb0899_state *state = fe->demodulator_priv;
1013 struct stb0899_internal *internal = &state->internal;
1014
1015 unsigned int val, quant, quantn = -1, est, estn = -1;
1016 u8 buf[2];
1017 u32 reg;
1018
1019 *snr = 0;
1020 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1021 switch (state->delsys) {
1022 case SYS_DVBS:
1023 case SYS_DSS:
1024 if (internal->lock) {
1025 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1026
1027 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1028 val = MAKEWORD16(buf[0], buf[1]);
1029
1030 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
1031 dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
1032 buf[0], buf[1], val, *snr);
1033 }
1034 }
1035 break;
1036 case SYS_DVBS2:
1037 if (internal->lock) {
1038 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1039 quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1040 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1041 est = STB0899_GETFIELD(ESN0_EST, reg);
1042 if (est == 1)
1043 val = 301;
1044 else if (est == 2)
1045 val = 270;
1046 else {
1047
1048 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1049
1050 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1051
1052 val = (quantn - estn) / 10;
1053 }
1054 *snr = val;
1055 dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
1056 quant, quantn, est, estn, val);
1057 }
1058 break;
1059 default:
1060 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1061 return -EINVAL;
1062 }
1063
1064 return 0;
1065}
1066
1067static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1068{
1069 struct stb0899_state *state = fe->demodulator_priv;
1070 struct stb0899_internal *internal = &state->internal;
1071 u8 reg;
1072 *status = 0;
1073
1074 switch (state->delsys) {
1075 case SYS_DVBS:
1076 case SYS_DSS:
1077 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1078 if (internal->lock) {
1079 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1080 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1081 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1082 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
1083
1084 reg = stb0899_read_reg(state, STB0899_PLPARM);
1085 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1086 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1087 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
1088
1089 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1090 }
1091 }
1092 }
1093 break;
1094 case SYS_DVBS2:
1095 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1096 if (internal->lock) {
1097 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1098 if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1099 *status |= FE_HAS_CARRIER;
1100 dprintk(state->verbose, FE_DEBUG, 1,
1101 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1102
1103 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1104 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1105 *status |= FE_HAS_LOCK;
1106 dprintk(state->verbose, FE_DEBUG, 1,
1107 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1108
1109 }
1110 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1111 *status |= FE_HAS_VITERBI;
1112 dprintk(state->verbose, FE_DEBUG, 1,
1113 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1114 }
1115 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1116 *status |= FE_HAS_SYNC;
1117 dprintk(state->verbose, FE_DEBUG, 1,
1118 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1119
1120 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1121 }
1122 }
1123 }
1124 break;
1125 default:
1126 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1127 return -EINVAL;
1128 }
1129 return 0;
1130}
1131
1132
1133
1134
1135
1136
1137
1138static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1139{
1140 struct stb0899_state *state = fe->demodulator_priv;
1141 struct stb0899_internal *internal = &state->internal;
1142
1143 u8 lsb, msb;
1144
1145 *ber = 0;
1146
1147 switch (state->delsys) {
1148 case SYS_DVBS:
1149 case SYS_DSS:
1150 if (internal->lock) {
1151 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1152 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1153 *ber = MAKEWORD16(msb, lsb);
1154
1155 if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1156
1157 *ber *= 9766;
1158
1159 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1160 *ber /= 8;
1161 }
1162 }
1163 break;
1164 case SYS_DVBS2:
1165 if (internal->lock) {
1166 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1167 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1168 *ber = MAKEWORD16(msb, lsb);
1169
1170 *ber *= 10000000;
1171 *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1172 }
1173 break;
1174 default:
1175 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1176 return -EINVAL;
1177 }
1178
1179 return 0;
1180}
1181
1182static int stb0899_set_voltage(struct dvb_frontend *fe,
1183 enum fe_sec_voltage voltage)
1184{
1185 struct stb0899_state *state = fe->demodulator_priv;
1186
1187 switch (voltage) {
1188 case SEC_VOLTAGE_13:
1189 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1190 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1191 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1192 break;
1193 case SEC_VOLTAGE_18:
1194 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1195 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1196 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1197 break;
1198 case SEC_VOLTAGE_OFF:
1199 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1200 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1201 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1202 break;
1203 default:
1204 return -EINVAL;
1205 }
1206
1207 return 0;
1208}
1209
1210static int stb0899_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
1211{
1212 struct stb0899_state *state = fe->demodulator_priv;
1213 struct stb0899_internal *internal = &state->internal;
1214
1215 u8 div, reg;
1216
1217
1218 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1219 return -ETIMEDOUT;
1220
1221 switch (tone) {
1222 case SEC_TONE_ON:
1223 div = (internal->master_clk / 100) / 5632;
1224 div = (div + 5) / 10;
1225 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
1226 reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1227 STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1228 stb0899_write_reg(state, STB0899_ACRPRESC, reg);
1229 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1230 break;
1231 case SEC_TONE_OFF:
1232 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1233 break;
1234 default:
1235 return -EINVAL;
1236 }
1237 return 0;
1238}
1239
1240int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1241{
1242 int i2c_stat;
1243 struct stb0899_state *state = fe->demodulator_priv;
1244
1245 i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1246 if (i2c_stat < 0)
1247 goto err;
1248
1249 if (enable) {
1250 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1251 i2c_stat |= STB0899_I2CTON;
1252 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1253 goto err;
1254 } else {
1255 dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
1256 i2c_stat &= ~STB0899_I2CTON;
1257 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1258 goto err;
1259 }
1260 return 0;
1261err:
1262 dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
1263 return -EREMOTEIO;
1264}
1265
1266
1267static inline void CONVERT32(u32 x, char *str)
1268{
1269 *str++ = (x >> 24) & 0xff;
1270 *str++ = (x >> 16) & 0xff;
1271 *str++ = (x >> 8) & 0xff;
1272 *str++ = (x >> 0) & 0xff;
1273 *str = '\0';
1274}
1275
1276static int stb0899_get_dev_id(struct stb0899_state *state)
1277{
1278 u8 chip_id, release;
1279 u16 id;
1280 u32 demod_ver = 0, fec_ver = 0;
1281 char demod_str[5] = { 0 };
1282 char fec_str[5] = { 0 };
1283
1284 id = stb0899_read_reg(state, STB0899_DEV_ID);
1285 dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1286 chip_id = STB0899_GETFIELD(CHIP_ID, id);
1287 release = STB0899_GETFIELD(CHIP_REL, id);
1288
1289 dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1290 chip_id, release);
1291
1292 CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1293
1294 demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1295 dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1296 CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1297 fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1298 if (! (chip_id > 0)) {
1299 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1300
1301 return -ENODEV;
1302 }
1303 dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1304
1305 return 0;
1306}
1307
1308static void stb0899_set_delivery(struct stb0899_state *state)
1309{
1310 u8 reg;
1311 u8 stop_clk[2];
1312
1313 stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1314 stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1315
1316 switch (state->delsys) {
1317 case SYS_DVBS:
1318 dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
1319
1320 reg = stb0899_read_reg(state, STB0899_FECM);
1321 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1322 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1323 stb0899_write_reg(state, STB0899_FECM, reg);
1324
1325 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1326 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1327 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1328 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1329
1330 reg = stb0899_read_reg(state, STB0899_TSTRES);
1331 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1332 stb0899_write_reg(state, STB0899_TSTRES, reg);
1333
1334 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1335 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1336 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1337
1338 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1339 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1340
1341 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
1342 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1343
1344 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1345 break;
1346 case SYS_DVBS2:
1347
1348 reg = stb0899_read_reg(state, STB0899_FECM);
1349 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1350 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1351 stb0899_write_reg(state, STB0899_FECM, reg);
1352
1353 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1354 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1355 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1356 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1357
1358 reg = stb0899_read_reg(state, STB0899_TSTRES);
1359 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1360 stb0899_write_reg(state, STB0899_TSTRES, reg);
1361
1362 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1363 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1364 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1365
1366 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1367 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1368
1369 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1370 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1371
1372 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1373 break;
1374 case SYS_DSS:
1375
1376 reg = stb0899_read_reg(state, STB0899_FECM);
1377 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1378 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1379 stb0899_write_reg(state, STB0899_FECM, reg);
1380
1381 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1382 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1383 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1384
1385 reg = stb0899_read_reg(state, STB0899_TSTRES);
1386 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1387 stb0899_write_reg(state, STB0899_TSTRES, reg);
1388
1389 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1390 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1391 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1392
1393 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1394 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1395
1396 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1397
1398 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1399 break;
1400 default:
1401 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1402 break;
1403 }
1404 STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1405 stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1406}
1407
1408
1409
1410
1411
1412static void stb0899_set_iterations(struct stb0899_state *state)
1413{
1414 struct stb0899_internal *internal = &state->internal;
1415 struct stb0899_config *config = state->config;
1416
1417 s32 iter_scale;
1418 u32 reg;
1419
1420 iter_scale = 17 * (internal->master_clk / 1000);
1421 iter_scale += 410000;
1422 iter_scale /= (internal->srate / 1000000);
1423 iter_scale /= 1000;
1424
1425 if (iter_scale > config->ldpc_max_iter)
1426 iter_scale = config->ldpc_max_iter;
1427
1428 reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER);
1429 STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1430 stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1431}
1432
1433static enum dvbfe_search stb0899_search(struct dvb_frontend *fe)
1434{
1435 struct stb0899_state *state = fe->demodulator_priv;
1436 struct stb0899_params *i_params = &state->params;
1437 struct stb0899_internal *internal = &state->internal;
1438 struct stb0899_config *config = state->config;
1439 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
1440
1441 u32 SearchRange, gain;
1442
1443 i_params->freq = props->frequency;
1444 i_params->srate = props->symbol_rate;
1445 state->delsys = props->delivery_system;
1446 dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1447
1448 SearchRange = 10000000;
1449 dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
1450
1451 if (INRANGE(i_params->srate, 1000000, 45000000)) {
1452 dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
1453 stb0899_set_delivery(state);
1454
1455 if (state->config->tuner_set_rfsiggain) {
1456 if (internal->srate > 15000000)
1457 gain = 8;
1458 else if (internal->srate > 5000000)
1459 gain = 12;
1460 else
1461 gain = 14;
1462 state->config->tuner_set_rfsiggain(fe, gain);
1463 }
1464
1465 if (i_params->srate <= 5000000)
1466 stb0899_set_mclk(state, config->lo_clk);
1467 else
1468 stb0899_set_mclk(state, config->hi_clk);
1469
1470 switch (state->delsys) {
1471 case SYS_DVBS:
1472 case SYS_DSS:
1473 dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
1474 internal->freq = i_params->freq;
1475 internal->srate = i_params->srate;
1476
1477
1478
1479
1480
1481
1482 internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
1483 internal->derot_percent = 30;
1484
1485
1486
1487 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1488
1489 if (state->config->tuner_set_bandwidth)
1490 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
1491 if (state->config->tuner_get_bandwidth)
1492 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1493
1494
1495 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1496
1497
1498 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1499
1500
1501 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
1502 if (stb0899_dvbs_algo(state) == RANGEOK) {
1503 internal->lock = 1;
1504 dprintk(state->verbose, FE_DEBUG, 1,
1505 "-------------------------------------> DVB-S LOCK !");
1506
1507
1508
1509
1510
1511
1512
1513 return DVBFE_ALGO_SEARCH_SUCCESS;
1514 } else {
1515 internal->lock = 0;
1516
1517 return DVBFE_ALGO_SEARCH_FAILED;
1518 }
1519 break;
1520 case SYS_DVBS2:
1521 internal->freq = i_params->freq;
1522 internal->srate = i_params->srate;
1523 internal->srch_range = SearchRange;
1524
1525
1526 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1527
1528 if (state->config->tuner_set_bandwidth)
1529 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
1530 if (state->config->tuner_get_bandwidth)
1531 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1532
1533
1534 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1535
1536
1537
1538
1539 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1540
1541
1542 stb0899_set_iterations(state);
1543
1544
1545 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
1546 if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
1547 internal->lock = 1;
1548 dprintk(state->verbose, FE_DEBUG, 1,
1549 "-------------------------------------> DVB-S2 LOCK !");
1550
1551
1552
1553
1554
1555 return DVBFE_ALGO_SEARCH_SUCCESS;
1556 } else {
1557 internal->lock = 0;
1558
1559 return DVBFE_ALGO_SEARCH_FAILED;
1560 }
1561 break;
1562 default:
1563 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1564 return DVBFE_ALGO_SEARCH_INVALID;
1565 }
1566 }
1567
1568 return DVBFE_ALGO_SEARCH_ERROR;
1569}
1570
1571static int stb0899_get_frontend(struct dvb_frontend *fe)
1572{
1573 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1574 struct stb0899_state *state = fe->demodulator_priv;
1575 struct stb0899_internal *internal = &state->internal;
1576
1577 dprintk(state->verbose, FE_DEBUG, 1, "Get params");
1578 p->symbol_rate = internal->srate;
1579 p->frequency = internal->freq;
1580
1581 return 0;
1582}
1583
1584static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1585{
1586 return DVBFE_ALGO_CUSTOM;
1587}
1588
1589static struct dvb_frontend_ops stb0899_ops = {
1590 .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
1591 .info = {
1592 .name = "STB0899 Multistandard",
1593 .frequency_min = 950000,
1594 .frequency_max = 2150000,
1595 .frequency_stepsize = 0,
1596 .frequency_tolerance = 0,
1597 .symbol_rate_min = 5000000,
1598 .symbol_rate_max = 45000000,
1599
1600 .caps = FE_CAN_INVERSION_AUTO |
1601 FE_CAN_FEC_AUTO |
1602 FE_CAN_2G_MODULATION |
1603 FE_CAN_QPSK
1604 },
1605
1606 .release = stb0899_release,
1607 .init = stb0899_init,
1608 .sleep = stb0899_sleep,
1609
1610
1611 .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
1612
1613 .get_frontend_algo = stb0899_frontend_algo,
1614 .search = stb0899_search,
1615 .get_frontend = stb0899_get_frontend,
1616
1617
1618 .read_status = stb0899_read_status,
1619 .read_snr = stb0899_read_snr,
1620 .read_signal_strength = stb0899_read_signal_strength,
1621 .read_ber = stb0899_read_ber,
1622
1623 .set_voltage = stb0899_set_voltage,
1624 .set_tone = stb0899_set_tone,
1625
1626 .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
1627 .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
1628 .diseqc_send_burst = stb0899_send_diseqc_burst,
1629};
1630
1631struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1632{
1633 struct stb0899_state *state = NULL;
1634
1635 state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1636 if (state == NULL)
1637 goto error;
1638
1639 state->verbose = &verbose;
1640 state->config = config;
1641 state->i2c = i2c;
1642 state->frontend.ops = stb0899_ops;
1643 state->frontend.demodulator_priv = state;
1644
1645 state->internal.inversion = config->inversion;
1646
1647 stb0899_wakeup(&state->frontend);
1648 if (stb0899_get_dev_id(state) == -ENODEV) {
1649 printk("%s: Exiting .. !\n", __func__);
1650 goto error;
1651 }
1652
1653 printk("%s: Attaching STB0899 \n", __func__);
1654 return &state->frontend;
1655
1656error:
1657 kfree(state);
1658 return NULL;
1659}
1660EXPORT_SYMBOL(stb0899_attach);
1661MODULE_PARM_DESC(verbose, "Set Verbosity level");
1662MODULE_AUTHOR("Manu Abraham");
1663MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1664MODULE_LICENSE("GPL");
1665