-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstb0899_drv.c
1741 lines (1469 loc) · 50.3 KB
/
stb0899_drv.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
STB0899 Multistandard Frontend driver
Copyright (C) Manu Abraham ([email protected])
Copyright (C) ST Microelectronics
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/dvb/frontend.h>
#include <media/dvb_frontend.h>
#include "stb0899_drv.h"
#include "stb0899_priv.h"
#include "stb0899_reg.h"
/* Max transfer size done by I2C transfer functions */
#define MAX_XFER_SIZE 64
static unsigned int verbose = 0;//1;
module_param(verbose, int, 0644);
/* C/N in dB/10, NIRM/NIRL */
static const struct stb0899_tab stb0899_cn_tab[] = {
{ 200, 2600 },
{ 190, 2700 },
{ 180, 2860 },
{ 170, 3020 },
{ 160, 3210 },
{ 150, 3440 },
{ 140, 3710 },
{ 130, 4010 },
{ 120, 4360 },
{ 110, 4740 },
{ 100, 5190 },
{ 90, 5670 },
{ 80, 6200 },
{ 70, 6770 },
{ 60, 7360 },
{ 50, 7970 },
{ 40, 8250 },
{ 30, 9000 },
{ 20, 9450 },
{ 15, 9600 },
};
/* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
* As measured, connected to a modulator.
* -8.0 to -50.0 dBm directly connected,
* -52.0 to -74.8 with extra attenuation.
* Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
* Crude linear extrapolation below -84.8dBm and above -8.0dBm.
*/
static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
{ -750, -128 },
{ -748, -94 },
{ -745, -92 },
{ -735, -90 },
{ -720, -87 },
{ -670, -77 },
{ -640, -70 },
{ -610, -62 },
{ -600, -60 },
{ -590, -56 },
{ -560, -41 },
{ -540, -25 },
{ -530, -17 },
{ -520, -11 },
{ -500, 1 },
{ -490, 6 },
{ -480, 10 },
{ -440, 22 },
{ -420, 27 },
{ -400, 31 },
{ -380, 34 },
{ -340, 40 },
{ -320, 43 },
{ -280, 48 },
{ -250, 52 },
{ -230, 55 },
{ -180, 61 },
{ -140, 66 },
{ -90, 73 },
{ -80, 74 },
{ 500, 127 }
};
/* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
* As measured, connected to a modulator.
* -8.0 to -50.1 dBm directly connected,
* -53.0 to -76.6 with extra attenuation.
* Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
* Crude linear extrapolation below -76.6dBm and above -8.0dBm.
*/
static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
{ 700, 0 },
{ -80, 3217 },
{ -150, 3893 },
{ -190, 4217 },
{ -240, 4621 },
{ -280, 4945 },
{ -320, 5273 },
{ -350, 5545 },
{ -370, 5741 },
{ -410, 6147 },
{ -450, 6671 },
{ -490, 7413 },
{ -501, 7665 },
{ -530, 8767 },
{ -560, 10219 },
{ -580, 10939 },
{ -590, 11518 },
{ -600, 11723 },
{ -650, 12659 },
{ -690, 13219 },
{ -730, 13645 },
{ -750, 13909 },
{ -766, 14153 },
{ -950, 16383 }
};
/* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
static struct stb0899_tab stb0899_quant_tab[] = {
// out in
{ 0, 0 },
{ 0, 100 },
{ 600, 200 }, // out = 1000 * log10int (in/100)
{ 950, 299 },
{ 1200, 398 },
{ 1400, 501 },
{ 1560, 603 },
{ 1690, 700 },
{ 1810, 804 },
{ 1910, 902 },
{ 2000, 1000 },
{ 2080, 1096 },
{ 2160, 1202 },
{ 2230, 1303 },
{ 2350, 1496 },
{ 2410, 1603 },
{ 2460, 1698 },
{ 2510, 1799 },
{ 2600, 1995 },
{ 2650, 2113 },
{ 2690, 2213 },
{ 2720, 2291 },
{ 2760, 2399 },
{ 2800, 2512 },
{ 2860, 2692 },
{ 2930, 2917 },
{ 2960, 3020 },
{ 3010, 3199 },
{ 3040, 3311 },
{ 3060, 3388 },
{ 3120, 3631 },
{ 3190, 3936 },
{ 3400, 5012 },
{ 3610, 6383 },
{ 3800, 7943 },
{ 4210, 12735 },
{ 4500, 17783 },
{ 4690, 22131 },
{ 4810, 25410 }
};
/* DVB-S2 Es/N0 estimate in dB/100 vs read value */
static struct stb0899_tab stb0899_est_tab[] = {
{ 0, 0 },
{ 0, 1 },
{ 301, 2 },
{ 1204, 16 },
{ 1806, 64 },
{ 2408, 256 },
{ 2709, 512 },
{ 3010, 1023 },
{ 3311, 2046 },
{ 3612, 4093 },
{ 3823, 6653 },
{ 3913, 8185 },
{ 4010, 10233 },
{ 4107, 12794 },
{ 4214, 16368 },
{ 4266, 18450 },
{ 4311, 20464 },
{ 4353, 22542 },
{ 4391, 24604 },
{ 4425, 26607 },
{ 4457, 28642 },
{ 4487, 30690 },
{ 4515, 32734 },
{ 4612, 40926 },
{ 4692, 49204 },
{ 4816, 65464 },
{ 4913, 81846 },
{ 4993, 98401 },
{ 5060, 114815 },
{ 5118, 131220 },
{ 5200, 158489 },
{ 5300, 199526 },
{ 5400, 251189 },
{ 5500, 316228 },
{ 5600, 398107 },
{ 5720, 524807 },
{ 5721, 526017 },
};
static u32 LastBaseAdress=0xffffffff;
static u16 LastPointer=0xffff;
static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
{
int ret;
u8 b0[] = { reg >> 8, reg & 0xff };
u8 buf;
struct i2c_msg msg[] = {
{
.addr = state->config->demod_address,
.flags = 0,
.buf = b0,
.len = 2
},{
.addr = state->config->demod_address,
.flags = I2C_M_RD,
.buf = &buf,
.len = 1
}
};
ret = i2c_transfer(state->i2c, msg, 2);
if (ret != 2) {
if (ret != -ERESTARTSYS)
dprintk(state->verbose, FE_ERROR, 1,
"Read error, Reg=[0x%02x], Status=%d",
reg, ret);
return ret < 0 ? ret : -EREMOTEIO;
}
if (unlikely(*state->verbose >= FE_DEBUGREG))
dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
reg, buf);
return (unsigned int)buf;
}
int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
{
int result;
result = _stb0899_read_reg(state, reg);
/*
* Bug ID 9:
* access to 0xf2xx/0xf6xx
* must be followed by read from 0xf2ff/0xf6ff.
*/
if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
(((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
_stb0899_read_reg(state, (reg | 0x00ff));
return result;
}
u32 _stb0899_read_s2reg(struct stb0899_state *state,
u32 stb0899_i2cdev,
u32 stb0899_base_addr,
u16 stb0899_reg_offset)
{
int status;
u32 data;
u8 buf[7] = { 0 };
u16 tmpaddr;
u8 buf_0[] = {
GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
};
u8 buf_1[] = {
0x00, /* 0xf3 Reg Offset */
0x00, /* 0x44 Reg Offset */
};
struct i2c_msg msg_0 = {
.addr = state->config->demod_address,
.flags = 0,
.buf = buf_0,
.len = 6
};
struct i2c_msg msg_1 = {
.addr = state->config->demod_address,
.flags = 0,
.buf = buf_1,
.len = 2
};
struct i2c_msg msg_r = {
.addr = state->config->demod_address,
.flags = I2C_M_RD,
.buf = buf,
.len = 4
};
tmpaddr = stb0899_reg_offset & 0xff00;
if (!(stb0899_reg_offset & 0x8))
tmpaddr = stb0899_reg_offset | 0x20;
buf_1[0] = GETBYTE(tmpaddr, BYTE1);
buf_1[1] = GETBYTE(tmpaddr, BYTE0);
if((stb0899_i2cdev != LastPointer) || (stb0899_base_addr != LastBaseAdress)) // do only if new PointerRegAddr
{
status = i2c_transfer(state->i2c, &msg_0, 1);
if (status < 1) {
if (status != -ERESTARTSYS)
printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
__func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
goto err;
}
LastPointer = stb0899_i2cdev;
LastBaseAdress = stb0899_base_addr;
}
/* Dummy */
status = i2c_transfer(state->i2c, &msg_1, 1);
if (status < 1)
goto err;
status = i2c_transfer(state->i2c, &msg_r, 1);
if (status < 1)
goto err;
buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
/* Actual */
status = i2c_transfer(state->i2c, &msg_1, 1);
if (status < 1) {
if (status != -ERESTARTSYS)
printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
__func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
goto err;
}
status = i2c_transfer(state->i2c, &msg_r, 1);
if (status < 1) {
if (status != -ERESTARTSYS)
printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
__func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
return status < 0 ? status : -EREMOTEIO;
}
data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
if (unlikely(*state->verbose >= FE_DEBUGREG))
printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
__func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
return data;
err:
return status < 0 ? status : -EREMOTEIO;
}
int stb0899_write_s2reg(struct stb0899_state *state,
u32 stb0899_i2cdev,
u32 stb0899_base_addr,
u16 stb0899_reg_offset,
u32 stb0899_data)
{
int status;
/* Base Address Setup */
u8 buf_0[] = {
GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
};
u8 buf_1[] = {
0x00, /* 0xf3 Reg Offset */
0x00, /* 0x44 Reg Offset */
0x00, /* data */
0x00, /* data */
0x00, /* data */
0x00, /* data */
};
struct i2c_msg msg_0 = {
.addr = state->config->demod_address,
.flags = 0,
.buf = buf_0,
.len = 6
};
struct i2c_msg msg_1 = {
.addr = state->config->demod_address,
.flags = 0,
.buf = buf_1,
.len = 6
};
buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
buf_1[2] = GETBYTE(stb0899_data, BYTE0);
buf_1[3] = GETBYTE(stb0899_data, BYTE1);
buf_1[4] = GETBYTE(stb0899_data, BYTE2);
buf_1[5] = GETBYTE(stb0899_data, BYTE3);
if (unlikely(*state->verbose >= FE_DEBUGREG))
printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
__func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
if((stb0899_i2cdev != LastPointer) || (stb0899_base_addr != LastBaseAdress)) // do only if new PointerRegAddr
{
status = i2c_transfer(state->i2c, &msg_0, 1);
if (unlikely(status < 1)) {
if (status != -ERESTARTSYS)
printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
__func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
goto err;
}
LastPointer = stb0899_i2cdev;
LastBaseAdress = stb0899_base_addr;
}
status = i2c_transfer(state->i2c, &msg_1, 1);
if (unlikely(status < 1)) {
if (status != -ERESTARTSYS)
printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
__func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
return status < 0 ? status : -EREMOTEIO;
}
return 0;
err:
return status < 0 ? status : -EREMOTEIO;
}
int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
{
int status;
u8 b0[] = { reg >> 8, reg & 0xff };
struct i2c_msg msg[] = {
{
.addr = state->config->demod_address,
.flags = 0,
.buf = b0,
.len = 2
},{
.addr = state->config->demod_address,
.flags = I2C_M_RD,
.buf = buf,
.len = count
}
};
status = i2c_transfer(state->i2c, msg, 2);
if (status != 2) {
if (status != -ERESTARTSYS)
printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
__func__, reg, count, status);
goto err;
}
/*
* Bug ID 9:
* access to 0xf2xx/0xf6xx
* must be followed by read from 0xf2ff/0xf6ff.
*/
if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
(((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
_stb0899_read_reg(state, (reg | 0x00ff));
if (unlikely(*state->verbose >= FE_DEBUGREG)) {
int i;
printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
for (i = 0; i < count; i++) {
printk(" %02x", buf[i]);
}
}
return 0;
err:
return status < 0 ? status : -EREMOTEIO;
}
int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
{
int ret;
u8 buf[MAX_XFER_SIZE];
struct i2c_msg i2c_msg = {
.addr = state->config->demod_address,
.flags = 0,
.buf = buf,
.len = 2 + count
};
if (2 + count > sizeof(buf)) {
printk(KERN_WARNING
"%s: i2c wr reg=%04x: len=%d is too big!\n",
KBUILD_MODNAME, reg, count);
return -EINVAL;
}
buf[0] = reg >> 8;
buf[1] = reg & 0xff;
memcpy(&buf[2], data, count);
if (unlikely(*state->verbose >= FE_DEBUGREG)) {
int i;
printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
for (i = 0; i < count; i++)
printk(" %02x", data[i]);
}
ret = i2c_transfer(state->i2c, &i2c_msg, 1);
/*
* Bug ID 9:
* access to 0xf2xx/0xf6xx
* must be followed by read from 0xf2ff/0xf6ff. ---> not need for write !!!
*/
if (ret != 1) {
if (ret != -ERESTARTSYS)
dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
reg, data[0], count, ret);
return ret < 0 ? ret : -EREMOTEIO;
}
return 0;
}
int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
{
u8 tmp = data;
return stb0899_write_regs(state, reg, &tmp, 1);
}
/*
* stb0899_get_mclk
* Get STB0899 master clock frequency
* ExtClk: external clock frequency (Hz)
*/
static u32 stb0899_get_mclk(struct stb0899_state *state)
{
u32 mclk = 0, div = 0;
div = stb0899_read_reg(state, STB0899_NCOARSE);
mclk = (div + 1) * state->config->xtal_freq / 6;
dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
return mclk;
}
/*
* stb0899_set_mclk
* Set STB0899 master Clock frequency
* Mclk: demodulator master clock
* ExtClk: external clock frequency (Hz)
*/
static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
{
struct stb0899_internal *internal = &state->internal;
u8 mdiv = 0;
dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
internal->master_clk = stb0899_get_mclk(state);
dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
}
static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable) // ctl = 0 for power, 1 for lock
{
struct stb0899_config *config = state->config;
const struct stb0899_postproc *postproc = config->postproc;
/* post process event */
if (postproc) {
if (enable) {
if (postproc[ctl].level == STB0899_GPIOPULLUP) // 0x01 /* Output device is connected to Vdd */
stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
else
stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
} else {
if (postproc[ctl].level == STB0899_GPIOPULLUP)
stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
else
stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
}
}
return 0;
}
static void stb0899_detach(struct dvb_frontend *fe)
{
struct stb0899_state *state = fe->demodulator_priv;
dprintk(state->verbose, FE_DEBUG, 1, "Why detach Frontend?");
}
static int stb0899_init(struct dvb_frontend *fe)
{
struct stb0899_state *state = fe->demodulator_priv;
dprintk(state->verbose, FE_DEBUG, 1, "Init done in attach!");
return 0;
}
/*
static void stb0899_release(struct dvb_frontend *fe)
{
struct stb0899_state *state = fe->demodulator_priv;
dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
kfree(state);
}
*/
/*
* stb0899_get_alpha
* return: rolloff
*/
static int stb0899_get_alpha(struct stb0899_state *state)
{
u8 mode_coeff;
mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
return 20;
else
return 35;
}
/*
* stb0899_init_calc
*/
static void stb0899_init_calc(struct stb0899_state *state)
{
struct stb0899_internal *internal = &state->internal;
int master_clk;
u8 agc[2];
u32 reg;
u32 tmp;
stb0899_read_reg(state, STB0899_AGC1CN); /* AGC1CN added in TT */
/* Read registers (in burst mode) */
stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
/* Initial calculations */
master_clk = stb0899_get_mclk(state);
internal->t_agc1 = 0; /* Agc1 time constant (ms) */ // used in s1 algo hum pb here
internal->t_agc2 = 0; /* Agc2 time constant (ms) */
internal->master_clk = master_clk;
internal->mclk = master_clk / 65536L;
internal->rolloff = stb0899_get_alpha(state); // MODECOEFF
/* DVBS2 Initial calculations */
/* Set AGC value to the middle */
internal->agc_gain = 8154;
tmp = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
reg=tmp;
STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain); // modified
stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
internal->center_freq = 0;
internal->av_frame_coarse = 10;
internal->av_frame_fine = 20;
internal->step_size = 2;
/*
if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
pParams->IQLocked = 0;
else
pParams->IQLocked = 1;
*/
}
static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
{
u8 reg = 0;
unsigned long start = jiffies;
while (1) {
reg = stb0899_read_reg(state, STB0899_DISSTATUS);
if (!STB0899_GETFIELD(FIFOFULL, reg))
break;
if (time_after(jiffies, start + timeout)) {
dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
return -ETIMEDOUT;
}
}
return 0;
}
static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
{
struct stb0899_state *state = fe->demodulator_priv;
u8 reg, i;
if (cmd->msg_len > sizeof(cmd->msg))
return -EINVAL;
/* enable FIFO precharge */
reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
for (i = 0; i < cmd->msg_len; i++) {
/* wait for FIFO empty */
if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
return -ETIMEDOUT;
stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
}
reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
msleep(100);
return 0;
}
static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
{
u8 reg = 0;
unsigned long start = jiffies;
while (!STB0899_GETFIELD(RXEND, reg)) {
reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
if (time_after(jiffies, start + timeout)) {
dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
return -ETIMEDOUT;
}
msleep(10);
}
return 0;
}
static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
{
struct stb0899_state *state = fe->demodulator_priv;
u8 reg, length = 0, i;
int result;
if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
return -ETIMEDOUT;
reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
if (STB0899_GETFIELD(RXEND, reg)) {
reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
length = STB0899_GETFIELD(FIFOBYTENBR, reg);
if (length > sizeof (reply->msg)) {
result = -EOVERFLOW;
goto exit;
}
reply->msg_len = length;
/* extract data */
for (i = 0; i < length; i++)
reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
}
return 0;
exit:
return result;
}
static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
{
u8 reg = 0;
unsigned long start = jiffies;
while (!STB0899_GETFIELD(TXIDLE, reg)) {
reg = stb0899_read_reg(state, STB0899_DISSTATUS);
if (time_after(jiffies, start + timeout)) {
dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
return -ETIMEDOUT;
}
msleep(10);
}
return 0;
}
static int stb0899_send_diseqc_burst(struct dvb_frontend *fe,
enum fe_sec_mini_cmd burst)
{
struct stb0899_state *state = fe->demodulator_priv;
// u8 reg, old_state;
dprintk(state->verbose, FE_ERROR, 1, "send_diseqc_burst called !!!");
/* if (stb0899_wait_diseqc_txidle(state, 100) < 0) // wait for diseqc idle
return -ETIMEDOUT;
reg = stb0899_read_reg(state, STB0899_DISCNTRL1); // p137
old_state = reg;
STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03); // set to burst mode
STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01); // or 7
stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
switch (burst) {
case SEC_MINI_A:
stb0899_write_reg(state, STB0899_DISFIFO, 0x00); // unmodulated
break;
case SEC_MINI_B:
stb0899_write_reg(state, STB0899_DISFIFO, 0xff); // modulated
break;
}
reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
if (stb0899_wait_diseqc_txidle(state, 100) < 0) // wait for diseqc idle
return -ETIMEDOUT;
stb0899_write_reg(state, STB0899_DISCNTRL1, old_state); // restore state
*/
return 0;
}
static int stb0899_diseqc_init(struct stb0899_state *state) // FE_STB0899_DiseqcInit
{
/*
struct dvb_diseqc_slave_reply rx_data;
*/
u8 f22_tx, reg;
u32 mclk, tx_freq = 22000;/* count = 0, i; */
reg = stb0899_read_reg(state, STB0899_DISCNTRL2); // p138
STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
/* disable Tx spy */
reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
mclk = stb0899_get_mclk(state);
f22_tx = mclk / (tx_freq * 32);
stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq p140 */
state->rx_freq = 20000; // 22000 ??
return 0;
}
static int stb0899_sleep(struct dvb_frontend *fe)
{
struct stb0899_state *state = fe->demodulator_priv;
dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
/* post process event */
stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
return 0;
}
static int stb0899_wakeup(struct dvb_frontend *fe)
{
struct stb0899_state *state = fe->demodulator_priv;
dprintk(state->verbose, FE_DEBUG, 1, "Do Wakeup .. (Power on .. :-))");
/* post process event */
stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
return 0;
}
static void stb0899_set_delivery(struct stb0899_state *state)
{
u8 reg;
u8 stop_clk[2];
stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
switch (state->delsys) {
case SYS_DVBS:
dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
/* FECM/Viterbi ON */
reg = stb0899_read_reg(state, STB0899_FECM);
STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
stb0899_write_reg(state, STB0899_FECM, reg);
stb0899_write_reg(state, STB0899_RSULC, 0xb1);
stb0899_write_reg(state, STB0899_TSULC, 0x40);
stb0899_write_reg(state, STB0899_RSLLC, 0x42);
stb0899_write_reg(state, STB0899_TSLPL, 0x12);
reg = stb0899_read_reg(state, STB0899_TSTRES);
STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
stb0899_write_reg(state, STB0899_TSTRES, reg);
STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
break;
case SYS_DVBS2:
/* FECM/Viterbi OFF */
reg = stb0899_read_reg(state, STB0899_FECM);
STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
stb0899_write_reg(state, STB0899_FECM, reg);
stb0899_write_reg(state, STB0899_RSULC, 0xb1);
stb0899_write_reg(state, STB0899_TSULC, 0x42);
stb0899_write_reg(state, STB0899_RSLLC, 0x40);
stb0899_write_reg(state, STB0899_TSLPL, 0x02);
reg = stb0899_read_reg(state, STB0899_TSTRES);
STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
stb0899_write_reg(state, STB0899_TSTRES, reg);
STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
break;
case SYS_DSS:
/* FECM/Viterbi ON */
reg = stb0899_read_reg(state, STB0899_FECM);
STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
stb0899_write_reg(state, STB0899_FECM, reg);
stb0899_write_reg(state, STB0899_RSULC, 0xa1);
stb0899_write_reg(state, STB0899_TSULC, 0x61);
stb0899_write_reg(state, STB0899_RSLLC, 0x42);
reg = stb0899_read_reg(state, STB0899_TSTRES);
STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
stb0899_write_reg(state, STB0899_TSTRES, reg);