Commit | Line | Data |
---|---|---|
8bd135ba MA |
1 | /* |
2 | STB0899 Multistandard Frontend driver | |
3 | Copyright (C) Manu Abraham (abraham.manu@gmail.com) | |
4 | ||
5 | Copyright (C) ST Microelectronics | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
21 | ||
22 | #include <linux/init.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/string.h> | |
26 | ||
27 | #include <linux/dvb/frontend.h> | |
28 | #include "dvb_frontend.h" | |
29 | ||
30 | #include "stb0899_drv.h" | |
31 | #include "stb0899_priv.h" | |
32 | #include "stb0899_reg.h" | |
33 | ||
34 | static unsigned int verbose = 5; | |
35 | module_param(verbose, int, 0644); | |
36 | ||
37 | /* C/N in dB/10, NIRM/NIRL */ | |
38 | static const struct stb0899_tab stb0899_cn_tab[] = { | |
39 | { 200, 2600 }, | |
40 | { 190, 2700 }, | |
41 | { 180, 2860 }, | |
42 | { 170, 3020 }, | |
43 | { 160, 3210 }, | |
44 | { 150, 3440 }, | |
45 | { 140, 3710 }, | |
46 | { 130, 4010 }, | |
47 | { 120, 4360 }, | |
48 | { 110, 4740 }, | |
49 | { 100, 5190 }, | |
50 | { 90, 5670 }, | |
51 | { 80, 6200 }, | |
52 | { 70, 6770 }, | |
53 | { 60, 7360 }, | |
54 | { 50, 7970 }, | |
55 | { 40, 8250 }, | |
56 | { 30, 9000 }, | |
57 | { 20, 9450 }, | |
58 | { 15, 9600 }, | |
59 | }; | |
60 | ||
61 | /* DVB-S AGCIQ_VALUE vs. signal level in dBm/10. | |
62 | * As measured, connected to a modulator. | |
63 | * -8.0 to -50.0 dBm directly connected, | |
64 | * -52.0 to -74.8 with extra attenuation. | |
65 | * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm. | |
66 | * Crude linear extrapolation below -84.8dBm and above -8.0dBm. | |
67 | */ | |
68 | static const struct stb0899_tab stb0899_dvbsrf_tab[] = { | |
69 | { -950, -128 }, | |
70 | { -748, -94 }, | |
71 | { -745, -92 }, | |
72 | { -735, -90 }, | |
73 | { -720, -87 }, | |
74 | { -670, -77 }, | |
75 | { -640, -70 }, | |
76 | { -610, -62 }, | |
77 | { -600, -60 }, | |
78 | { -590, -56 }, | |
79 | { -560, -41 }, | |
80 | { -540, -25 }, | |
81 | { -530, -17 }, | |
82 | { -520, -11 }, | |
83 | { -500, 1 }, | |
84 | { -490, 6 }, | |
85 | { -480, 10 }, | |
86 | { -440, 22 }, | |
87 | { -420, 27 }, | |
88 | { -400, 31 }, | |
89 | { -380, 34 }, | |
90 | { -340, 40 }, | |
91 | { -320, 43 }, | |
92 | { -280, 48 }, | |
93 | { -250, 52 }, | |
94 | { -230, 55 }, | |
95 | { -180, 61 }, | |
96 | { -140, 66 }, | |
97 | { -90, 73 }, | |
98 | { -80, 74 }, | |
99 | { 500, 127 } | |
100 | }; | |
101 | ||
102 | /* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10. | |
103 | * As measured, connected to a modulator. | |
104 | * -8.0 to -50.1 dBm directly connected, | |
105 | * -53.0 to -76.6 with extra attenuation. | |
106 | * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm. | |
107 | * Crude linear extrapolation below -76.6dBm and above -8.0dBm. | |
108 | */ | |
109 | static const struct stb0899_tab stb0899_dvbs2rf_tab[] = { | |
110 | { 700, 0 }, | |
111 | { -80, 3217 }, | |
112 | { -150, 3893 }, | |
113 | { -190, 4217 }, | |
114 | { -240, 4621 }, | |
115 | { -280, 4945 }, | |
116 | { -320, 5273 }, | |
117 | { -350, 5545 }, | |
118 | { -370, 5741 }, | |
119 | { -410, 6147 }, | |
120 | { -450, 6671 }, | |
121 | { -490, 7413 }, | |
122 | { -501, 7665 }, | |
123 | { -530, 8767 }, | |
124 | { -560, 10219 }, | |
125 | { -580, 10939 }, | |
126 | { -590, 11518 }, | |
127 | { -600, 11723 }, | |
128 | { -650, 12659 }, | |
129 | { -690, 13219 }, | |
130 | { -730, 13645 }, | |
131 | { -750, 13909 }, | |
132 | { -766, 14153 }, | |
133 | { -999, 16383 } | |
134 | }; | |
135 | ||
136 | /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/ | |
137 | struct stb0899_tab stb0899_quant_tab[] = { | |
138 | { 0, 0 }, | |
139 | { 0, 100 }, | |
140 | { 600, 200 }, | |
141 | { 950, 299 }, | |
142 | { 1200, 398 }, | |
143 | { 1400, 501 }, | |
144 | { 1560, 603 }, | |
145 | { 1690, 700 }, | |
146 | { 1810, 804 }, | |
147 | { 1910, 902 }, | |
148 | { 2000, 1000 }, | |
149 | { 2080, 1096 }, | |
150 | { 2160, 1202 }, | |
151 | { 2230, 1303 }, | |
152 | { 2350, 1496 }, | |
153 | { 2410, 1603 }, | |
154 | { 2460, 1698 }, | |
155 | { 2510, 1799 }, | |
156 | { 2600, 1995 }, | |
157 | { 2650, 2113 }, | |
158 | { 2690, 2213 }, | |
159 | { 2720, 2291 }, | |
160 | { 2760, 2399 }, | |
161 | { 2800, 2512 }, | |
162 | { 2860, 2692 }, | |
163 | { 2930, 2917 }, | |
164 | { 2960, 3020 }, | |
165 | { 3010, 3199 }, | |
166 | { 3040, 3311 }, | |
167 | { 3060, 3388 }, | |
168 | { 3120, 3631 }, | |
169 | { 3190, 3936 }, | |
170 | { 3400, 5012 }, | |
171 | { 3610, 6383 }, | |
172 | { 3800, 7943 }, | |
173 | { 4210, 12735 }, | |
174 | { 4500, 17783 }, | |
175 | { 4690, 22131 }, | |
176 | { 4810, 25410 } | |
177 | }; | |
178 | ||
179 | /* DVB-S2 Es/N0 estimate in dB/100 vs read value */ | |
180 | struct stb0899_tab stb0899_est_tab[] = { | |
181 | { 0, 0 }, | |
182 | { 0, 1 }, | |
183 | { 301, 2 }, | |
184 | { 1204, 16 }, | |
185 | { 1806, 64 }, | |
186 | { 2408, 256 }, | |
187 | { 2709, 512 }, | |
188 | { 3010, 1023 }, | |
189 | { 3311, 2046 }, | |
190 | { 3612, 4093 }, | |
191 | { 3823, 6653 }, | |
192 | { 3913, 8185 }, | |
193 | { 4010, 10233 }, | |
194 | { 4107, 12794 }, | |
195 | { 4214, 16368 }, | |
196 | { 4266, 18450 }, | |
197 | { 4311, 20464 }, | |
198 | { 4353, 22542 }, | |
199 | { 4391, 24604 }, | |
200 | { 4425, 26607 }, | |
201 | { 4457, 28642 }, | |
202 | { 4487, 30690 }, | |
203 | { 4515, 32734 }, | |
204 | { 4612, 40926 }, | |
205 | { 4692, 49204 }, | |
206 | { 4816, 65464 }, | |
207 | { 4913, 81846 }, | |
208 | { 4993, 98401 }, | |
209 | { 5060, 114815 }, | |
210 | { 5118, 131220 }, | |
211 | { 5200, 158489 }, | |
212 | { 5300, 199526 }, | |
213 | { 5400, 251189 }, | |
214 | { 5500, 316228 }, | |
215 | { 5600, 398107 }, | |
216 | { 5720, 524807 }, | |
217 | { 5721, 526017 }, | |
218 | }; | |
219 | ||
220 | int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg) | |
221 | { | |
222 | int ret; | |
223 | ||
224 | u8 b0[] = { reg >> 8, reg & 0xff }; | |
225 | u8 buf; | |
226 | ||
227 | struct i2c_msg msg[] = { | |
228 | { | |
229 | .addr = state->config->demod_address, | |
230 | .flags = 0, | |
231 | .buf = b0, | |
232 | .len = 2 | |
233 | },{ | |
234 | .addr = state->config->demod_address, | |
235 | .flags = I2C_M_RD, | |
236 | .buf = &buf, | |
237 | .len = 1 | |
238 | } | |
239 | }; | |
240 | ||
241 | ret = i2c_transfer(state->i2c, msg, 2); | |
242 | if (ret != 2) { | |
243 | if (ret != -ERESTARTSYS) | |
244 | dprintk(verbose, FE_ERROR, 1, | |
245 | "Read error, Reg=[0x%02x], Status=%d", | |
246 | reg, ret); | |
247 | ||
248 | return ret < 0 ? ret : -EREMOTEIO; | |
249 | } | |
250 | if (unlikely(verbose >= FE_DEBUGREG)) | |
251 | dprintk(verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x", | |
252 | reg, buf); | |
253 | ||
8bd135ba MA |
254 | return (unsigned int)buf; |
255 | } | |
256 | ||
257 | int stb0899_read_reg(struct stb0899_state *state, unsigned int reg) | |
258 | { | |
259 | int result; | |
260 | ||
261 | result = _stb0899_read_reg(state, reg); | |
262 | /* | |
263 | * Bug ID 9: | |
264 | * access to 0xf2xx/0xf6xx | |
265 | * must be followed by read from 0xf2ff/0xf6ff. | |
266 | */ | |
267 | if ((reg != 0xf2ff) && (reg != 0xf6ff) && | |
268 | (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600))) | |
269 | _stb0899_read_reg(state, (reg | 0x00ff)); | |
270 | ||
271 | return result; | |
272 | } | |
273 | ||
274 | u32 _stb0899_read_s2reg(struct stb0899_state *state, | |
275 | u32 stb0899_i2cdev, | |
276 | u32 stb0899_base_addr, | |
277 | u16 stb0899_reg_offset) | |
278 | { | |
279 | int status; | |
280 | u32 data; | |
281 | u8 buf[7] = { 0 }; | |
282 | u16 tmpaddr; | |
283 | ||
284 | u8 buf_0[] = { | |
285 | GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */ | |
286 | GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */ | |
287 | GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */ | |
288 | GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */ | |
289 | GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */ | |
290 | GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */ | |
291 | }; | |
292 | u8 buf_1[] = { | |
293 | 0x00, /* 0xf3 Reg Offset */ | |
294 | 0x00, /* 0x44 Reg Offset */ | |
295 | }; | |
296 | ||
297 | struct i2c_msg msg_0 = { | |
298 | .addr = state->config->demod_address, | |
299 | .flags = 0, | |
300 | .buf = buf_0, | |
301 | .len = 6 | |
302 | }; | |
303 | ||
304 | struct i2c_msg msg_1 = { | |
305 | .addr = state->config->demod_address, | |
306 | .flags = 0, | |
307 | .buf = buf_1, | |
308 | .len = 2 | |
309 | }; | |
310 | ||
311 | struct i2c_msg msg_r = { | |
312 | .addr = state->config->demod_address, | |
313 | .flags = I2C_M_RD, | |
314 | .buf = buf, | |
315 | .len = 4 | |
316 | }; | |
317 | ||
318 | tmpaddr = stb0899_reg_offset & 0xff00; | |
319 | if (!(stb0899_reg_offset & 0x8)) | |
320 | tmpaddr = stb0899_reg_offset | 0x20; | |
321 | ||
322 | buf_1[0] = GETBYTE(tmpaddr, BYTE1); | |
323 | buf_1[1] = GETBYTE(tmpaddr, BYTE0); | |
324 | ||
325 | status = i2c_transfer(state->i2c, &msg_0, 1); | |
326 | if (status < 1) { | |
327 | if (status != -ERESTARTSYS) | |
328 | printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n", | |
329 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status); | |
330 | ||
331 | goto err; | |
332 | } | |
333 | ||
334 | /* Dummy */ | |
335 | status = i2c_transfer(state->i2c, &msg_1, 1); | |
336 | if (status < 1) | |
337 | goto err; | |
338 | ||
339 | status = i2c_transfer(state->i2c, &msg_r, 1); | |
340 | if (status < 1) | |
341 | goto err; | |
342 | ||
343 | buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1); | |
344 | buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0); | |
345 | ||
346 | /* Actual */ | |
347 | status = i2c_transfer(state->i2c, &msg_1, 1); | |
348 | if (status < 1) { | |
349 | if (status != -ERESTARTSYS) | |
350 | printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n", | |
351 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status); | |
352 | goto err; | |
353 | } | |
354 | ||
355 | status = i2c_transfer(state->i2c, &msg_r, 1); | |
356 | if (status < 1) { | |
357 | if (status != -ERESTARTSYS) | |
358 | printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n", | |
359 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status); | |
360 | return status < 0 ? status : -EREMOTEIO; | |
361 | } | |
362 | ||
363 | data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]); | |
364 | if (unlikely(state->verbose >= FE_DEBUGREG)) | |
365 | printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n", | |
366 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data); | |
367 | ||
368 | return data; | |
369 | ||
370 | err: | |
371 | return status < 0 ? status : -EREMOTEIO; | |
372 | } | |
373 | ||
374 | int stb0899_write_s2reg(struct stb0899_state *state, | |
375 | u32 stb0899_i2cdev, | |
376 | u32 stb0899_base_addr, | |
377 | u16 stb0899_reg_offset, | |
378 | u32 stb0899_data) | |
379 | { | |
380 | int status; | |
381 | ||
382 | /* Base Address Setup */ | |
383 | u8 buf_0[] = { | |
384 | GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */ | |
385 | GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */ | |
386 | GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */ | |
387 | GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */ | |
388 | GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */ | |
389 | GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */ | |
390 | }; | |
391 | u8 buf_1[] = { | |
392 | 0x00, /* 0xf3 Reg Offset */ | |
393 | 0x00, /* 0x44 Reg Offset */ | |
394 | 0x00, /* data */ | |
395 | 0x00, /* data */ | |
396 | 0x00, /* data */ | |
397 | 0x00, /* data */ | |
398 | }; | |
399 | ||
400 | struct i2c_msg msg_0 = { | |
401 | .addr = state->config->demod_address, | |
402 | .flags = 0, | |
403 | .buf = buf_0, | |
404 | .len = 6 | |
405 | }; | |
406 | ||
407 | struct i2c_msg msg_1 = { | |
408 | .addr = state->config->demod_address, | |
409 | .flags = 0, | |
410 | .buf = buf_1, | |
411 | .len = 6 | |
412 | }; | |
413 | ||
414 | buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1); | |
415 | buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0); | |
416 | buf_1[2] = GETBYTE(stb0899_data, BYTE0); | |
417 | buf_1[3] = GETBYTE(stb0899_data, BYTE1); | |
418 | buf_1[4] = GETBYTE(stb0899_data, BYTE2); | |
419 | buf_1[5] = GETBYTE(stb0899_data, BYTE3); | |
420 | ||
421 | if (unlikely(state->verbose >= FE_DEBUGREG)) | |
422 | printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n", | |
423 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data); | |
424 | ||
425 | status = i2c_transfer(state->i2c, &msg_0, 1); | |
426 | if (unlikely(status < 1)) { | |
427 | if (status != -ERESTARTSYS) | |
428 | printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n", | |
429 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status); | |
430 | goto err; | |
431 | } | |
432 | status = i2c_transfer(state->i2c, &msg_1, 1); | |
433 | if (unlikely(status < 1)) { | |
434 | if (status != -ERESTARTSYS) | |
435 | printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n", | |
436 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status); | |
437 | ||
438 | return status < 0 ? status : -EREMOTEIO; | |
439 | } | |
440 | ||
441 | return 0; | |
442 | ||
443 | err: | |
444 | return status < 0 ? status : -EREMOTEIO; | |
445 | } | |
446 | ||
447 | int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, size_t count) | |
448 | { | |
449 | int status; | |
450 | ||
451 | u8 b0[] = { reg >> 8, reg & 0xff }; | |
452 | ||
453 | struct i2c_msg msg[] = { | |
454 | { | |
455 | .addr = state->config->demod_address, | |
456 | .flags = 0, | |
457 | .buf = b0, | |
458 | .len = 2 | |
459 | },{ | |
460 | .addr = state->config->demod_address, | |
461 | .flags = I2C_M_RD, | |
462 | .buf = buf, | |
463 | .len = count | |
464 | } | |
465 | }; | |
466 | ||
467 | status = i2c_transfer(state->i2c, msg, 2); | |
468 | if (status != 2) { | |
469 | if (status != -ERESTARTSYS) | |
470 | printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n", | |
471 | __func__, reg, count, status); | |
472 | goto err; | |
473 | } | |
474 | /* | |
475 | * Bug ID 9: | |
476 | * access to 0xf2xx/0xf6xx | |
477 | * must be followed by read from 0xf2ff/0xf6ff. | |
478 | */ | |
479 | if ((reg != 0xf2ff) && (reg != 0xf6ff) && | |
480 | (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600))) | |
481 | _stb0899_read_reg(state, (reg | 0x00ff)); | |
482 | ||
483 | if (unlikely(state->verbose >= FE_DEBUGREG)) { | |
484 | int i; | |
485 | ||
486 | printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg); | |
487 | for (i = 0; i < count; i++) { | |
488 | printk(" %02x", buf[i]); | |
489 | } | |
490 | printk("\n"); | |
491 | } | |
492 | ||
493 | return 0; | |
494 | err: | |
495 | return status < 0 ? status : -EREMOTEIO; | |
496 | } | |
497 | ||
498 | int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, size_t count) | |
499 | { | |
500 | int ret; | |
501 | u8 buf[2 + count]; | |
502 | struct i2c_msg i2c_msg = { | |
503 | .addr = state->config->demod_address, | |
504 | .flags = 0, | |
505 | .buf = buf, | |
506 | .len = 2 + count | |
507 | }; | |
508 | ||
509 | buf[0] = reg >> 8; | |
510 | buf[1] = reg & 0xff; | |
511 | memcpy(&buf[2], data, count); | |
512 | ||
513 | if (unlikely(state->verbose >= FE_DEBUGREG)) { | |
514 | int i; | |
515 | ||
516 | printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg); | |
517 | for (i = 0; i < count; i++) | |
518 | printk(" %02x", data[i]); | |
519 | printk("\n"); | |
520 | } | |
521 | ret = i2c_transfer(state->i2c, &i2c_msg, 1); | |
522 | ||
523 | /* | |
524 | * Bug ID 9: | |
525 | * access to 0xf2xx/0xf6xx | |
526 | * must be followed by read from 0xf2ff/0xf6ff. | |
527 | */ | |
528 | if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600))) | |
529 | stb0899_read_reg(state, (reg | 0x00ff)); | |
530 | ||
531 | if (ret != 1) { | |
532 | if (ret != -ERESTARTSYS) | |
533 | dprintk(verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d", | |
534 | reg, data[0], count, ret); | |
535 | return ret < 0 ? ret : -EREMOTEIO; | |
536 | } | |
537 | ||
538 | return 0; | |
539 | } | |
540 | ||
541 | int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data) | |
542 | { | |
543 | return stb0899_write_regs(state, reg, &data, 1); | |
544 | } | |
545 | ||
546 | /* | |
547 | * stb0899_get_mclk | |
548 | * Get STB0899 master clock frequency | |
549 | * ExtClk: external clock frequency (Hz) | |
550 | */ | |
551 | static u32 stb0899_get_mclk(struct stb0899_state *state) | |
552 | { | |
553 | u32 mclk = 90000000, div = 0; | |
554 | ||
555 | div = stb0899_read_reg(state, STB0899_NCOARSE); | |
556 | mclk = (div + 1) * state->config->xtal_freq / 6; | |
557 | dprintk(verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk); | |
558 | ||
559 | return mclk; | |
560 | } | |
561 | ||
562 | /* | |
563 | * stb0899_set_mclk | |
564 | * Set STB0899 master Clock frequency | |
565 | * Mclk: demodulator master clock | |
566 | * ExtClk: external clock frequency (Hz) | |
567 | */ | |
568 | static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk) | |
569 | { | |
570 | struct stb0899_internal *internal = &state->internal; | |
571 | u8 mdiv = 0; | |
572 | ||
573 | dprintk(verbose, FE_DEBUG, 1, "state->config=%p", state->config); | |
574 | mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1; | |
575 | dprintk(verbose, FE_DEBUG, 1, "mdiv=%d", mdiv); | |
576 | ||
577 | stb0899_write_reg(state, STB0899_NCOARSE, mdiv); | |
578 | internal->master_clk = stb0899_get_mclk(state); | |
579 | ||
580 | dprintk(verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk); | |
581 | } | |
582 | ||
583 | static void stb0899_release(struct dvb_frontend *fe) | |
584 | { | |
585 | struct stb0899_state *state = fe->demodulator_priv; | |
586 | ||
587 | dprintk(verbose, FE_DEBUG, 1, "Release Frontend"); | |
588 | kfree(state); | |
589 | } | |
590 | ||
591 | /* | |
592 | * stb0899_get_alpha | |
593 | * return: rolloff | |
594 | */ | |
595 | static int stb0899_get_alpha(struct stb0899_state *state) | |
596 | { | |
597 | u8 mode_coeff; | |
598 | ||
599 | mode_coeff = stb0899_read_reg(state, STB0899_DEMOD); | |
600 | ||
601 | if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1) | |
602 | return 20; | |
603 | else | |
604 | return 35; | |
605 | } | |
606 | ||
607 | /* | |
608 | * stb0899_init_calc | |
609 | */ | |
610 | static void stb0899_init_calc(struct stb0899_state *state) | |
611 | { | |
612 | struct stb0899_internal *internal = &state->internal; | |
613 | int master_clk; | |
614 | u8 agc[1]; | |
615 | u8 agc1cn; | |
616 | u32 reg; | |
617 | ||
618 | /* Read registers (in burst mode) */ | |
619 | agc1cn = stb0899_read_reg(state, STB0899_AGC1CN); | |
620 | stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */ | |
621 | ||
622 | /* Initial calculations */ | |
623 | master_clk = stb0899_get_mclk(state); | |
624 | internal->t_agc1 = 0; | |
625 | internal->t_agc2 = 0; | |
626 | internal->master_clk = master_clk; | |
627 | internal->mclk = master_clk / 65536L; | |
628 | internal->rolloff = stb0899_get_alpha(state); | |
629 | ||
630 | /* DVBS2 Initial calculations */ | |
631 | /* Set AGC value to the middle */ | |
632 | internal->agc_gain = 8154; | |
633 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL); | |
634 | STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain); | |
635 | stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg); | |
636 | ||
637 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA); | |
638 | internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg); | |
639 | ||
640 | internal->center_freq = 0; | |
641 | internal->av_frame_coarse = 10; | |
642 | internal->av_frame_fine = 20; | |
643 | internal->step_size = 2; | |
644 | /* | |
645 | if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO)) | |
646 | pParams->IQLocked = 0; | |
647 | else | |
648 | pParams->IQLocked = 1; | |
649 | */ | |
650 | } | |
651 | ||
652 | static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout) | |
653 | { | |
654 | u8 reg = 0; | |
655 | unsigned long start = jiffies; | |
656 | ||
657 | while (1) { | |
658 | reg = stb0899_read_reg(state, STB0899_DISSTATUS); | |
659 | if (!STB0899_GETFIELD(FIFOFULL, reg)) | |
660 | break; | |
661 | if ((jiffies - start) > timeout) { | |
662 | dprintk(verbose, FE_ERROR, 1, "timed out !!"); | |
663 | return -ETIMEDOUT; | |
664 | } | |
665 | } | |
666 | ||
667 | return 0; | |
668 | } | |
669 | ||
670 | static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd) | |
671 | { | |
672 | struct stb0899_state *state = fe->demodulator_priv; | |
673 | u8 reg, i; | |
674 | ||
675 | if (cmd->msg_len > 8) | |
676 | return -EINVAL; | |
677 | ||
678 | /* enable FIFO precharge */ | |
679 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); | |
680 | STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1); | |
681 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); | |
682 | for (i = 0; i < cmd->msg_len; i++) { | |
683 | /* wait for FIFO empty */ | |
684 | if (stb0899_wait_diseqc_fifo_empty(state, 10) < 0) | |
685 | return -ETIMEDOUT; | |
686 | ||
687 | stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]); | |
688 | } | |
689 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); | |
690 | STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0); | |
691 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); | |
692 | ||
693 | return 0; | |
694 | } | |
695 | ||
696 | static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout) | |
697 | { | |
698 | u8 reg = 0; | |
699 | unsigned long start = jiffies; | |
700 | ||
701 | while (!STB0899_GETFIELD(RXEND, reg)) { | |
702 | reg = stb0899_read_reg(state, STB0899_DISRX_ST0); | |
703 | if (jiffies - start > timeout) { | |
704 | dprintk(verbose, FE_ERROR, 1, "timed out!!"); | |
705 | return -ETIMEDOUT; | |
706 | } | |
707 | msleep(10); | |
708 | } | |
709 | ||
710 | return 0; | |
711 | } | |
712 | ||
713 | static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply) | |
714 | { | |
715 | struct stb0899_state *state = fe->demodulator_priv; | |
716 | u8 reg, length = 0, i; | |
717 | int result; | |
718 | ||
719 | if (stb0899_wait_diseqc_rxidle(state, 100) < 0) | |
720 | return -ETIMEDOUT; | |
721 | ||
722 | reg = stb0899_read_reg(state, STB0899_DISRX_ST0); | |
723 | if (STB0899_GETFIELD(RXEND, reg)) { | |
724 | ||
725 | reg = stb0899_read_reg(state, STB0899_DISRX_ST1); | |
726 | length = STB0899_GETFIELD(FIFOBYTENBR, reg); | |
727 | ||
728 | if (length > sizeof (reply->msg)) { | |
729 | result = -EOVERFLOW; | |
730 | goto exit; | |
731 | } | |
732 | reply->msg_len = length; | |
733 | ||
734 | /* extract data */ | |
735 | for (i = 0; i < length; i++) | |
736 | reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO); | |
737 | } | |
738 | ||
739 | return 0; | |
740 | exit: | |
741 | ||
742 | return result; | |
743 | } | |
744 | ||
745 | static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout) | |
746 | { | |
747 | u8 reg = 0; | |
748 | unsigned long start = jiffies; | |
749 | ||
750 | while (!STB0899_GETFIELD(TXIDLE, reg)) { | |
751 | reg = stb0899_read_reg(state, STB0899_DISSTATUS); | |
752 | if (jiffies - start > timeout) { | |
753 | dprintk(verbose, FE_ERROR, 1, "timed out!!"); | |
754 | return -ETIMEDOUT; | |
755 | } | |
756 | msleep(10); | |
757 | } | |
758 | return 0; | |
759 | } | |
760 | ||
761 | static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst) | |
762 | { | |
763 | struct stb0899_state *state = fe->demodulator_priv; | |
764 | u8 reg, old_state; | |
765 | ||
766 | /* wait for diseqc idle */ | |
767 | if (stb0899_wait_diseqc_txidle(state, 100) < 0) | |
768 | return -ETIMEDOUT; | |
769 | ||
770 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); | |
771 | old_state = reg; | |
772 | /* set to burst mode */ | |
773 | STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x02); | |
774 | STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01); | |
775 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); | |
776 | switch (burst) { | |
777 | case SEC_MINI_A: | |
778 | /* unmodulated */ | |
779 | stb0899_write_reg(state, STB0899_DISFIFO, 0x00); | |
780 | break; | |
781 | case SEC_MINI_B: | |
782 | /* modulated */ | |
783 | stb0899_write_reg(state, STB0899_DISFIFO, 0xff); | |
784 | break; | |
785 | } | |
786 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); | |
787 | STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00); | |
788 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); | |
789 | /* wait for diseqc idle */ | |
790 | if (stb0899_wait_diseqc_txidle(state, 100) < 0) | |
791 | return -ETIMEDOUT; | |
792 | ||
793 | /* restore state */ | |
794 | stb0899_write_reg(state, STB0899_DISCNTRL1, old_state); | |
795 | ||
796 | return 0; | |
797 | } | |
798 | ||
6c1022cb MA |
799 | static int stb0899_diseqc_init(struct stb0899_state *state) |
800 | { | |
801 | struct dvb_diseqc_master_cmd tx_data; | |
802 | struct dvb_diseqc_slave_reply rx_data; | |
803 | ||
804 | u8 f22_tx, f22_rx, reg; | |
805 | ||
806 | u32 mclk, tx_freq = 22000, count = 0, i; | |
807 | ||
808 | u32 trial = 0; /* try max = 2 (try 20khz and 17.5 khz) */ | |
809 | u32 ret_1 = 0; /* 20 Khz status */ | |
810 | u32 ret_2 = 0; /* 17.5 Khz status */ | |
811 | ||
812 | tx_data.msg[0] = 0xe2; | |
813 | tx_data.msg_len = 3; | |
814 | reg = stb0899_read_reg(state, STB0899_DISCNTRL2); | |
815 | STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0); | |
816 | stb0899_write_reg(state, STB0899_DISCNTRL2, reg); | |
817 | ||
818 | /* disable Tx spy */ | |
819 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); | |
820 | STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1); | |
821 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); | |
822 | ||
823 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); | |
824 | STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0); | |
825 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); | |
826 | ||
827 | mclk = stb0899_get_mclk(state); | |
828 | f22_tx = mclk / (tx_freq * 32); | |
829 | stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */ | |
830 | state->rx_freq = 20000; | |
831 | f22_rx = mclk / (state->rx_freq * 32); | |
832 | ||
833 | return 0; | |
834 | } | |
8bd135ba MA |
835 | |
836 | static int stb0899_sleep(struct dvb_frontend *fe) | |
837 | { | |
838 | struct stb0899_state *state = fe->demodulator_priv; | |
839 | u8 reg; | |
840 | ||
841 | dprintk(verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))"); | |
8bd135ba MA |
842 | return 0; |
843 | } | |
844 | ||
845 | static int stb0899_wakeup(struct dvb_frontend *fe) | |
846 | { | |
847 | int rc; | |
848 | struct stb0899_state *state = fe->demodulator_priv; | |
849 | ||
de29eb82 MA |
850 | if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI))) |
851 | return rc; | |
852 | /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */ | |
853 | if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00))) | |
854 | return rc; | |
855 | if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00))) | |
856 | return rc; | |
8bd135ba MA |
857 | |
858 | return 0; | |
859 | } | |
860 | ||
861 | static int stb0899_init(struct dvb_frontend *fe) | |
862 | { | |
863 | int i; | |
864 | struct stb0899_state *state = fe->demodulator_priv; | |
865 | struct stb0899_config *config = state->config; | |
866 | ||
867 | dprintk(verbose, FE_DEBUG, 1, "Initializing STB0899 ... "); | |
868 | // mutex_init(&state->search_lock); | |
869 | ||
870 | /* init device */ | |
871 | dprintk(verbose, FE_DEBUG, 1, "init device"); | |
872 | for (i = 0; config->init_dev[i].address != 0xffff; i++) | |
873 | stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data); | |
874 | ||
875 | dprintk(verbose, FE_DEBUG, 1, "init S2 demod"); | |
876 | /* init S2 demod */ | |
877 | for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++) | |
878 | stb0899_write_s2reg(state, STB0899_S2DEMOD, | |
879 | config->init_s2_demod[i].base_address, | |
880 | config->init_s2_demod[i].offset, | |
881 | config->init_s2_demod[i].data); | |
882 | ||
883 | dprintk(verbose, FE_DEBUG, 1, "init S1 demod"); | |
884 | /* init S1 demod */ | |
885 | for (i = 0; config->init_s1_demod[i].address != 0xffff; i++) | |
886 | stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data); | |
887 | ||
888 | dprintk(verbose, FE_DEBUG, 1, "init S2 FEC"); | |
889 | /* init S2 fec */ | |
890 | for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++) | |
891 | stb0899_write_s2reg(state, STB0899_S2FEC, | |
892 | config->init_s2_fec[i].base_address, | |
893 | config->init_s2_fec[i].offset, | |
894 | config->init_s2_fec[i].data); | |
895 | ||
896 | dprintk(verbose, FE_DEBUG, 1, "init TST"); | |
897 | /* init test */ | |
898 | for (i = 0; config->init_tst[i].address != 0xffff; i++) | |
899 | stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data); | |
900 | ||
901 | stb0899_init_calc(state); | |
6c1022cb | 902 | stb0899_diseqc_init(state); |
8bd135ba MA |
903 | |
904 | return 0; | |
905 | } | |
906 | ||
907 | static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val) | |
908 | { | |
909 | int res = 0; | |
910 | int min = 0, med; | |
911 | ||
912 | if (val < tab[min].read) | |
913 | res = tab[min].real; | |
914 | else if (val >= tab[max].read) | |
915 | res = tab[max].real; | |
916 | else { | |
917 | while ((max - min) > 1) { | |
918 | med = (max + min) / 2; | |
919 | if (val >= tab[min].read && val < tab[med].read) | |
920 | max = med; | |
921 | else | |
922 | min = med; | |
923 | } | |
924 | res = ((val - tab[min].read) * | |
925 | (tab[max].real - tab[min].real) / | |
926 | (tab[max].read - tab[min].read)) + | |
927 | tab[min].real; | |
928 | } | |
929 | ||
930 | return res; | |
931 | } | |
932 | ||
933 | static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength) | |
934 | { | |
935 | struct stb0899_state *state = fe->demodulator_priv; | |
936 | struct stb0899_internal *internal = &state->internal; | |
937 | ||
938 | int val; | |
939 | u32 reg; | |
940 | switch (state->delsys) { | |
941 | case DVBFE_DELSYS_DVBS: | |
942 | case DVBFE_DELSYS_DSS: | |
943 | if (internal->lock) { | |
944 | reg = stb0899_read_reg(state, STB0899_VSTATUS); | |
945 | if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) { | |
946 | ||
947 | reg = stb0899_read_reg(state, STB0899_AGCIQIN); | |
948 | val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg); | |
949 | ||
950 | *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val); | |
951 | *strength += 750; | |
952 | dprintk(verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm", | |
953 | val & 0xff, *strength); | |
954 | } | |
955 | } | |
956 | break; | |
957 | case DVBFE_DELSYS_DVBS2: | |
958 | if (internal->lock) { | |
959 | reg = STB0899_READ_S2REG(STB0899_DEMOD, IF_AGC_GAIN); | |
960 | val = STB0899_GETFIELD(IF_AGC_GAIN, reg); | |
961 | ||
962 | *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val); | |
963 | *strength += 750; | |
964 | dprintk(verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm", | |
965 | val & 0x3fff, *strength); | |
966 | } | |
967 | break; | |
968 | default: | |
969 | dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system"); | |
970 | break; | |
971 | } | |
972 | ||
973 | return 0; | |
974 | } | |
975 | ||
976 | static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr) | |
977 | { | |
978 | struct stb0899_state *state = fe->demodulator_priv; | |
979 | struct stb0899_internal *internal = &state->internal; | |
980 | ||
981 | unsigned int val, quant, quantn = -1, est, estn = -1; | |
982 | u8 buf[2]; | |
983 | u32 reg; | |
984 | ||
985 | reg = stb0899_read_reg(state, STB0899_VSTATUS); | |
986 | switch (state->delsys) { | |
987 | case DVBFE_DELSYS_DVBS: | |
988 | case DVBFE_DELSYS_DSS: | |
989 | if (internal->lock) { | |
990 | if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) { | |
991 | ||
992 | stb0899_read_regs(state, STB0899_NIRM, buf, 2); | |
993 | val = MAKEWORD16(buf[0], buf[1]); | |
994 | ||
995 | *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val); | |
996 | dprintk(verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n", | |
997 | buf[0], buf[1], val, *snr); | |
998 | } | |
999 | } | |
1000 | break; | |
1001 | case DVBFE_DELSYS_DVBS2: | |
1002 | if (internal->lock) { | |
1003 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1); | |
1004 | quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg); | |
1005 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2); | |
1006 | est = STB0899_GETFIELD(ESN0_EST, reg); | |
1007 | if (est == 1) | |
1008 | val = 301; /* C/N = 30.1 dB */ | |
1009 | else if (est == 2) | |
1010 | val = 270; /* C/N = 27.0 dB */ | |
1011 | else { | |
1012 | /* quantn = 100 * log(quant^2) */ | |
1013 | quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100); | |
1014 | /* estn = 100 * log(est) */ | |
1015 | estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est); | |
1016 | /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */ | |
1017 | val = (quantn - estn) / 10; | |
1018 | } | |
1019 | *snr = val; | |
1020 | dprintk(verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm", | |
1021 | quant, quantn, est, estn, val); | |
1022 | } | |
1023 | break; | |
1024 | default: | |
1025 | dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system"); | |
1026 | break; | |
1027 | } | |
1028 | ||
1029 | return 0; | |
1030 | } | |
1031 | ||
1032 | static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status) | |
1033 | { | |
1034 | struct stb0899_state *state = fe->demodulator_priv; | |
1035 | struct stb0899_internal *internal = &state->internal; | |
1036 | u8 reg; | |
1037 | *status = 0; | |
1038 | ||
1039 | switch (state->delsys) { | |
1040 | case DVBFE_DELSYS_DVBS: | |
1041 | case DVBFE_DELSYS_DSS: | |
1042 | dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS"); | |
1043 | if (internal->lock) { | |
1044 | reg = stb0899_read_reg(state, STB0899_VSTATUS); | |
1045 | if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) { | |
1046 | dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK"); | |
1047 | *status |= FE_HAS_CARRIER | FE_HAS_LOCK; | |
1048 | ||
1049 | reg = stb0899_read_reg(state, STB0899_PLPARM); | |
1050 | if (STB0899_GETFIELD(VITCURPUN, reg)) { | |
1051 | dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC"); | |
1052 | *status |= FE_HAS_VITERBI | FE_HAS_SYNC; | |
1053 | } | |
1054 | } | |
1055 | } | |
1056 | break; | |
1057 | case DVBFE_DELSYS_DVBS2: | |
1058 | dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2"); | |
1059 | if (internal->lock) { | |
1060 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2); | |
1061 | if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) { | |
1062 | *status |= FE_HAS_CARRIER; | |
1063 | dprintk(state->verbose, FE_DEBUG, 1, | |
1064 | "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER"); | |
1065 | ||
1066 | reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1); | |
1067 | if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) { | |
1068 | *status |= FE_HAS_LOCK; | |
1069 | dprintk(state->verbose, FE_DEBUG, 1, | |
1070 | "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK"); | |
1071 | ||
1072 | } | |
1073 | if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) { | |
1074 | *status |= FE_HAS_VITERBI; | |
1075 | dprintk(state->verbose, FE_DEBUG, 1, | |
1076 | "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI"); | |
1077 | } | |
1078 | if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) { | |
1079 | *status |= FE_HAS_SYNC; | |
1080 | dprintk(state->verbose, FE_DEBUG, 1, | |
1081 | "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC"); | |
1082 | } | |
1083 | } | |
1084 | } | |
1085 | break; | |
1086 | default: | |
1087 | dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system"); | |
1088 | break; | |
1089 | } | |
1090 | return 0; | |
1091 | } | |
1092 | ||
1093 | /* | |
1094 | * stb0899_get_error | |
1095 | * viterbi error for DVB-S/DSS | |
1096 | * packet error for DVB-S2 | |
1097 | * Bit Error Rate or Packet Error Rate * 10 ^ 7 | |
1098 | */ | |
1099 | static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber) | |
1100 | { | |
1101 | struct stb0899_state *state = fe->demodulator_priv; | |
1102 | struct stb0899_internal *internal = &state->internal; | |
1103 | ||
1104 | u8 lsb, msb; | |
1105 | u32 i; | |
1106 | ||
1107 | *ber = 0; | |
1108 | ||
1109 | switch (state->delsys) { | |
1110 | case DVBFE_DELSYS_DVBS: | |
1111 | case DVBFE_DELSYS_DSS: | |
1112 | if (internal->lock) { | |
1113 | /* average 5 BER values */ | |
1114 | for (i = 0; i < 5; i++) { | |
1115 | msleep(100); | |
1116 | lsb = stb0899_read_reg(state, STB0899_ECNT1L); | |
1117 | msb = stb0899_read_reg(state, STB0899_ECNT1M); | |
1118 | *ber += MAKEWORD16(msb, lsb); | |
1119 | } | |
1120 | *ber /= 5; | |
1121 | /* Viterbi Check */ | |
1122 | if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) { | |
1123 | /* Error Rate */ | |
1124 | *ber *= 9766; | |
1125 | /* ber = ber * 10 ^ 7 */ | |
1126 | *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl)))); | |
1127 | *ber /= 8; | |
1128 | } | |
1129 | } | |
1130 | break; | |
1131 | case DVBFE_DELSYS_DVBS2: | |
1132 | if (internal->lock) { | |
1133 | /* Average 5 PER values */ | |
1134 | for (i = 0; i < 5; i++) { | |
1135 | msleep(100); | |
1136 | lsb = stb0899_read_reg(state, STB0899_ECNT1L); | |
1137 | msb = stb0899_read_reg(state, STB0899_ECNT1M); | |
1138 | *ber += MAKEWORD16(msb, lsb); | |
1139 | } | |
1140 | /* ber = ber * 10 ^ 7 */ | |
1141 | *ber *= 10000000; | |
1142 | *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl)))); | |
1143 | } | |
1144 | break; | |
1145 | default: | |
1146 | dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system"); | |
1147 | } | |
1148 | ||
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage) | |
1153 | { | |
1154 | struct stb0899_state *state = fe->demodulator_priv; | |
1155 | ||
1156 | switch (voltage) { | |
1157 | case SEC_VOLTAGE_13: | |
1158 | stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82); | |
1159 | stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02); | |
1160 | stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00); | |
1161 | break; | |
1162 | case SEC_VOLTAGE_18: | |
1163 | stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02); | |
1164 | stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02); | |
1165 | stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82); | |
1166 | break; | |
1167 | case SEC_VOLTAGE_OFF: | |
1168 | stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82); | |
1169 | stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82); | |
1170 | stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82); | |
1171 | break; | |
1172 | default: | |
1173 | return -EINVAL; | |
1174 | } | |
1175 | ||
1176 | return 0; | |
1177 | } | |
1178 | ||
1179 | static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone) | |
1180 | { | |
1181 | struct stb0899_state *state = fe->demodulator_priv; | |
1182 | struct stb0899_internal *internal = &state->internal; | |
1183 | ||
baa40e48 | 1184 | u8 div, reg; |
8bd135ba MA |
1185 | |
1186 | /* wait for diseqc idle */ | |
1187 | if (stb0899_wait_diseqc_txidle(state, 100) < 0) | |
1188 | return -ETIMEDOUT; | |
1189 | ||
1190 | switch (tone) { | |
1191 | case SEC_TONE_ON: | |
1192 | div = (internal->master_clk / 100) / 5632; | |
1193 | div = (div + 5) / 10; | |
1194 | stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66); | |
baa40e48 MA |
1195 | reg = stb0899_read_reg(state, STB0899_ACRPRESC); |
1196 | STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03); | |
1197 | stb0899_write_reg(state, STB0899_ACRPRESC, reg); | |
8bd135ba MA |
1198 | stb0899_write_reg(state, STB0899_ACRDIV1, div); |
1199 | break; | |
1200 | case SEC_TONE_OFF: | |
1201 | stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20); | |
1202 | break; | |
1203 | default: | |
1204 | break; | |
1205 | } | |
1206 | return 0; | |
1207 | } | |
1208 | ||
1209 | static int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) | |
1210 | { | |
1211 | int i2c_stat; | |
1212 | struct stb0899_state *state = fe->demodulator_priv; | |
1213 | ||
1214 | i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT); | |
1215 | if (i2c_stat < 0) | |
1216 | goto err; | |
1217 | ||
1218 | if (enable) { | |
1219 | dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ..."); | |
1220 | i2c_stat |= STB0899_I2CTON; | |
1221 | if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0) | |
1222 | goto err; | |
1223 | } | |
1224 | return 0; | |
1225 | err: | |
1226 | dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater enable failed"); | |
1227 | return -EREMOTEIO; | |
1228 | } | |
1229 | ||
1230 | ||
1231 | static inline void CONVERT32(u32 x, char *str) | |
1232 | { | |
1233 | *str++ = (x >> 24) & 0xff; | |
1234 | *str++ = (x >> 16) & 0xff; | |
1235 | *str++ = (x >> 8) & 0xff; | |
1236 | *str++ = (x >> 0) & 0xff; | |
1237 | *str = '\0'; | |
1238 | } | |
1239 | ||
1240 | int stb0899_get_dev_id(struct stb0899_state *state) | |
1241 | { | |
1242 | u8 chip_id, release; | |
1243 | u16 id; | |
1244 | u32 demod_ver = 0, fec_ver = 0; | |
1245 | char demod_str[4] = { 0 }; | |
1246 | char fec_str[4] = { 0 }; | |
1247 | ||
1248 | id = stb0899_read_reg(state, STB0899_DEV_ID); | |
1249 | dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id); | |
1250 | chip_id = STB0899_GETFIELD(CHIP_ID, id); | |
1251 | release = STB0899_GETFIELD(CHIP_REL, id); | |
1252 | ||
1253 | dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]", | |
1254 | chip_id, release); | |
1255 | ||
1256 | CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str); | |
1257 | ||
1258 | demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID); | |
1259 | dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver); | |
1260 | CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str); | |
1261 | fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG); | |
1262 | if (! (chip_id > 0)) { | |
1263 | dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899"); | |
1264 | ||
1265 | return -ENODEV; | |
1266 | } | |
1267 | dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver); | |
1268 | ||
1269 | return 0; | |
1270 | } | |
1271 | ||
1272 | static const struct dvbfe_info dvbs_info = { | |
1273 | .name = "STB0899 DVB-S", | |
1274 | .delivery = DVBFE_DELSYS_DVBS, | |
1275 | .delsys = { | |
1276 | .dvbs.modulation = DVBFE_MOD_QPSK, | |
1277 | .dvbs.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 | | |
1278 | DVBFE_FEC_3_4 | DVBFE_FEC_5_6 | | |
1279 | DVBFE_FEC_6_7 | |
1280 | }, | |
1281 | ||
1282 | .frequency_min = 950000, | |
1283 | .frequency_max = 2150000, | |
1284 | .frequency_step = 0, | |
1285 | .symbol_rate_min = 1000000, | |
1286 | .symbol_rate_max = 45000000, | |
1287 | .symbol_rate_tolerance = 0 | |
1288 | }; | |
1289 | ||
1290 | static const struct dvbfe_info dss_info = { | |
1291 | .name = "STB0899 DSS", | |
1292 | .delivery = DVBFE_DELSYS_DSS, | |
1293 | .delsys = { | |
1294 | .dss.modulation = DVBFE_MOD_BPSK | DVBFE_MOD_QPSK, | |
1295 | .dss.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 | | |
1296 | DVBFE_FEC_3_4 | DVBFE_FEC_5_6 | | |
1297 | DVBFE_FEC_6_7 | |
1298 | }, | |
1299 | ||
1300 | .frequency_min = 950000, | |
1301 | .frequency_max = 2150000, | |
1302 | .frequency_step = 0, | |
1303 | .symbol_rate_min = 1000000, | |
1304 | .symbol_rate_max = 45000000, | |
1305 | .symbol_rate_tolerance = 0 | |
1306 | }; | |
1307 | ||
1308 | static const struct dvbfe_info dvbs2_info = { | |
1309 | .name = "STB0899 DVB-S2", | |
1310 | .delivery = DVBFE_DELSYS_DVBS2, | |
1311 | .delsys = { | |
1312 | .dvbs2.modulation = DVBFE_MOD_QPSK | DVBFE_MOD_8PSK | | |
1313 | DVBFE_MOD_16APSK | DVBFE_MOD_32APSK, | |
1314 | ||
1315 | .dvbs2.fec = DVBFE_FEC_1_4 | DVBFE_FEC_1_3 | | |
1316 | DVBFE_FEC_2_5 | DVBFE_FEC_1_2 | | |
1317 | DVBFE_FEC_3_5 | DVBFE_FEC_2_3 | | |
1318 | DVBFE_FEC_3_4 | DVBFE_FEC_4_5 | | |
1319 | DVBFE_FEC_5_6 | DVBFE_FEC_8_9 | | |
1320 | DVBFE_FEC_9_10, | |
1321 | }, | |
1322 | ||
1323 | .frequency_min = 950000, | |
1324 | .frequency_max = 2150000, | |
1325 | .frequency_step = 0, | |
1326 | .symbol_rate_min = 1000000, | |
1327 | .symbol_rate_max = 45000000, | |
1328 | .symbol_rate_tolerance = 0 | |
1329 | }; | |
1330 | ||
1331 | static int stb0899_get_info(struct dvb_frontend *fe, struct dvbfe_info *fe_info) | |
1332 | { | |
1333 | struct stb0899_state *state = fe->demodulator_priv; | |
1334 | ||
1335 | dprintk(verbose, FE_DEBUG, 1, "Get Info"); | |
1336 | ||
1337 | state->delsys = fe_info->delivery; | |
1338 | switch (state->delsys) { | |
1339 | case DVBFE_DELSYS_DVBS: | |
1340 | dprintk(verbose, FE_ERROR, 1, "Querying DVB-S info"); | |
1341 | memcpy(fe_info, &dvbs_info, sizeof (struct dvbfe_info)); | |
1342 | break; | |
1343 | case DVBFE_DELSYS_DSS: | |
1344 | dprintk(verbose, FE_ERROR, 1, "Querying DSS info"); | |
1345 | memcpy(fe_info, &dss_info, sizeof (struct dvbfe_info)); | |
1346 | break; | |
1347 | case DVBFE_DELSYS_DVBS2: | |
1348 | dprintk(verbose, FE_ERROR, 1, "Querying DVB-S2 info"); | |
1349 | memcpy(fe_info, &dvbs2_info, sizeof (struct dvbfe_info)); | |
1350 | break; | |
1351 | default: | |
1352 | dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system"); | |
1353 | return -EINVAL; | |
1354 | } | |
1355 | dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys); | |
1356 | ||
1357 | return 0; | |
1358 | } | |
1359 | ||
1360 | static int stb0899_get_delsys(struct dvb_frontend *fe, enum dvbfe_delsys *fe_delsys) | |
1361 | { | |
1362 | *fe_delsys = DVBFE_DELSYS_DVBS | DVBFE_DELSYS_DSS | DVBFE_DELSYS_DVBS2; | |
1363 | ||
1364 | return 0; | |
1365 | } | |
1366 | ||
1367 | void stb0899_set_delsys(struct stb0899_state *state) | |
1368 | { | |
1369 | u8 reg; | |
1370 | u8 stop_clk[2]; | |
1371 | ||
1372 | stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1); | |
1373 | stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2); | |
1374 | ||
1375 | switch (state->delsys) { | |
1376 | case DVBFE_DELSYS_DVBS: | |
1377 | dprintk(verbose, FE_DEBUG, 1, "Delivery System -- DVB-S"); | |
1378 | /* FECM/Viterbi ON */ | |
1379 | reg = stb0899_read_reg(state, STB0899_FECM); | |
1380 | STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0); | |
1381 | STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1); | |
1382 | stb0899_write_reg(state, STB0899_FECM, reg); | |
1383 | ||
1384 | stb0899_write_reg(state, STB0899_RSULC, 0xb1); | |
1385 | stb0899_write_reg(state, STB0899_TSULC, 0x40); | |
1386 | stb0899_write_reg(state, STB0899_RSLLC, 0x42); | |
1387 | stb0899_write_reg(state, STB0899_TSLPL, 0x12); | |
1388 | ||
1389 | reg = stb0899_read_reg(state, STB0899_TSTRES); | |
1390 | STB0899_SETFIELD_VAL(FRESLDPC, reg, 1); | |
1391 | stb0899_write_reg(state, STB0899_TSTRES, reg); | |
1392 | ||
1393 | STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1); | |
1394 | STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1); | |
1395 | STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1); | |
1396 | ||
1397 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1); | |
1398 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1); | |
1399 | ||
1400 | STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1); | |
1401 | STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 1); | |
1402 | ||
1403 | STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1); | |
1404 | break; | |
1405 | case DVBFE_DELSYS_DVBS2: | |
1406 | /* FECM/Viterbi OFF */ | |
1407 | reg = stb0899_read_reg(state, STB0899_FECM); | |
1408 | STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0); | |
1409 | STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0); | |
1410 | stb0899_write_reg(state, STB0899_FECM, reg); | |
1411 | ||
1412 | stb0899_write_reg(state, STB0899_RSULC, 0xb1); | |
1413 | stb0899_write_reg(state, STB0899_TSULC, 0x42); | |
1414 | stb0899_write_reg(state, STB0899_RSLLC, 0x40); | |
1415 | stb0899_write_reg(state, STB0899_TSLPL, 0x02); | |
1416 | ||
1417 | reg = stb0899_read_reg(state, STB0899_TSTRES); | |
1418 | STB0899_SETFIELD_VAL(FRESLDPC, reg, 0); | |
1419 | stb0899_write_reg(state, STB0899_TSTRES, reg); | |
1420 | ||
1421 | STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1); | |
1422 | STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0); | |
1423 | STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0); | |
1424 | ||
1425 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0); | |
1426 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0); | |
1427 | ||
1428 | STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0); | |
1429 | STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0); | |
1430 | ||
1431 | STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0); | |
1432 | break; | |
1433 | case DVBFE_DELSYS_DSS: | |
1434 | /* FECM/Viterbi ON */ | |
1435 | reg = stb0899_read_reg(state, STB0899_FECM); | |
1436 | STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1); | |
1437 | STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1); | |
1438 | stb0899_write_reg(state, STB0899_FECM, reg); | |
1439 | ||
1440 | stb0899_write_reg(state, STB0899_RSULC, 0xa1); | |
1441 | stb0899_write_reg(state, STB0899_TSULC, 0x61); | |
1442 | stb0899_write_reg(state, STB0899_RSLLC, 0x42); | |
1443 | ||
1444 | reg = stb0899_read_reg(state, STB0899_TSTRES); | |
1445 | STB0899_SETFIELD_VAL(FRESLDPC, reg, 1); | |
1446 | stb0899_write_reg(state, STB0899_TSTRES, reg); | |
1447 | ||
1448 | STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1); | |
1449 | STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1); | |
1450 | STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1); | |
1451 | ||
1452 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1); | |
1453 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1); | |
1454 | ||
1455 | STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0); | |
1456 | ||
1457 | STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1); | |
1458 | break; | |
1459 | default: | |
1460 | dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system"); | |
1461 | break; | |
1462 | } | |
1463 | STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0); | |
1464 | stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2); | |
1465 | } | |
1466 | ||
1467 | /* | |
1468 | * stb0899_set_iterations | |
1469 | * set the LDPC iteration scale function | |
1470 | */ | |
1471 | static void stb0899_set_iterations(struct stb0899_state *state) | |
1472 | { | |
1473 | struct stb0899_internal *internal = &state->internal; | |
1474 | struct stb0899_config *config = state->config; | |
1475 | ||
1476 | s32 iter_scale; | |
1477 | u32 reg; | |
1478 | ||
1479 | iter_scale = 17 * (internal->master_clk / 1000); | |
1480 | iter_scale += 410000; | |
1481 | iter_scale /= (internal->srate / 1000000); | |
1482 | iter_scale /= 1000; | |
1483 | ||
1484 | if (iter_scale > config->ldpc_max_iter) | |
1485 | iter_scale = config->ldpc_max_iter; | |
1486 | ||
1487 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, MAX_ITER); | |
1488 | STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale); | |
1489 | stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg); | |
1490 | } | |
1491 | ||
1492 | static enum dvbfe_search stb0899_search(struct dvb_frontend *fe, struct dvbfe_params *params) | |
1493 | { | |
1494 | struct stb0899_state *state = fe->demodulator_priv; | |
1495 | struct stb0899_params *i_params = &state->params; | |
1496 | struct stb0899_internal *internal = &state->internal; | |
1497 | ||
1498 | u32 SearchRange, gain; | |
1499 | ||
1500 | switch (state->delsys) { | |
1501 | case DVBFE_DELSYS_DVBS: | |
1502 | dprintk(verbose, FE_ERROR, 1, "set DVB-S params"); | |
1503 | i_params->freq = params->frequency; | |
1504 | i_params->srate = params->delsys.dvbs.symbol_rate; | |
1505 | break; | |
1506 | case DVBFE_DELSYS_DSS: | |
1507 | dprintk(verbose, FE_ERROR, 1, "set DSS params"); | |
1508 | i_params->freq = params->frequency; | |
1509 | i_params->srate = params->delsys.dss.symbol_rate; | |
1510 | break; | |
1511 | case DVBFE_DELSYS_DVBS2: | |
1512 | dprintk(verbose, FE_ERROR, 1, "set DVB-S2 params"); | |
1513 | i_params->freq = params->frequency; | |
1514 | i_params->srate = params->delsys.dvbs2.symbol_rate; | |
1515 | break; | |
1516 | default: | |
1517 | dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system"); | |
1518 | return -EINVAL; | |
1519 | } | |
1520 | dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys); | |
1521 | ||
d98c0285 MA |
1522 | SearchRange = 3000000; /* Search Bandwidth (3 Mhz, was initially 10 Mhz) */ |
1523 | // SearchRange = 10000000; /* Search Bandwidth (3 Mhz, was initially 10 Mhz) */ | |
8bd135ba MA |
1524 | dprintk(verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate); |
1525 | /* checking Search Range is meaningless for a fixed 3 Mhz */ | |
1526 | if (INRANGE(i_params->srate, 1000000, 45000000)) { | |
1527 | dprintk(verbose, FE_DEBUG, 1, "Parameters IN RANGE"); | |
1528 | stb0899_set_delsys(state); | |
1529 | ||
1530 | if (state->config->tuner_set_rfsiggain) { | |
1531 | if (internal->srate > 15000000) | |
1532 | gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */ | |
1533 | else if (internal->srate > 5000000) | |
1534 | gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */ | |
1535 | else | |
1536 | gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */ | |
1537 | state->config->tuner_set_rfsiggain(fe, gain); | |
1538 | } | |
1539 | ||
1540 | if (i_params->srate <= 5000000) | |
1541 | stb0899_set_mclk(state, 76500000); | |
1542 | else | |
e0ee77ac | 1543 | stb0899_set_mclk(state, 90000000); |
8bd135ba MA |
1544 | |
1545 | switch (state->delsys) { | |
1546 | case DVBFE_DELSYS_DVBS: | |
1547 | case DVBFE_DELSYS_DSS: | |
1548 | dprintk(verbose, FE_DEBUG, 1, "DVB-S delivery system"); | |
1549 | internal->freq = i_params->freq; | |
1550 | internal->srate = i_params->srate; | |
1551 | /* | |
1552 | * search = user search range + | |
1553 | * 500Khz + | |
1554 | * 2 * Tuner_step_size + | |
1555 | * 10% of the symbol rate | |
1556 | */ | |
1557 | internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5); | |
1558 | internal->derot_percent = 30; | |
1559 | ||
1560 | /* What to do for tuners having no bandwidth setup ? */ | |
1561 | if (state->config->tuner_set_bandwidth) | |
d98c0285 | 1562 | state->config->tuner_set_bandwidth(fe, (135 * (stb0899_carr_width(state) + SearchRange)) / 100); |
8bd135ba MA |
1563 | if (state->config->tuner_get_bandwidth) |
1564 | state->config->tuner_get_bandwidth(fe, &internal->tuner_bw); | |
1565 | /* Set DVB-S1 AGC */ | |
1566 | stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11); | |
1567 | ||
1568 | /* Run the search algorithm */ | |
1569 | dprintk(verbose, FE_DEBUG, 1, "running DVB-S search algo .."); | |
1570 | if (stb0899_dvbs_algo(state) == RANGEOK) { | |
1571 | internal->lock = 1; | |
1572 | dprintk(verbose, FE_DEBUG, 1, | |
1573 | "-------------------------------------> DVB-S LOCK !"); | |
1574 | ||
1575 | // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */ | |
1576 | // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS); | |
1577 | // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1); | |
1578 | // dprintk(verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status); | |
1579 | // dprintk(verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl); | |
1580 | ||
1581 | return DVBFE_ALGO_SEARCH_SUCCESS; | |
1582 | } else { | |
1583 | internal->lock = 0; | |
1584 | ||
1585 | return DVBFE_ALGO_SEARCH_FAILED; | |
1586 | } | |
1587 | break; | |
1588 | case DVBFE_DELSYS_DVBS2: | |
1589 | internal->freq = i_params->freq; | |
1590 | internal->srate = i_params->srate; | |
1591 | internal->srch_range = SearchRange; | |
1592 | ||
1593 | if (state->config->tuner_set_bandwidth) | |
d98c0285 | 1594 | state->config->tuner_set_bandwidth(fe, (135 * (stb0899_carr_width(state) + SearchRange)) / 100); |
8bd135ba MA |
1595 | if (state->config->tuner_get_bandwidth) |
1596 | state->config->tuner_get_bandwidth(fe, &internal->tuner_bw); | |
1597 | ||
1598 | // pParams->SpectralInv = pSearch->IQ_Inversion; | |
1599 | ||
1600 | /* Set DVB-S2 AGC */ | |
1601 | stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c); | |
1602 | ||
1603 | /* Set IterScale =f(MCLK,SYMB) */ | |
1604 | stb0899_set_iterations(state); | |
1605 | ||
1606 | /* Run the search algorithm */ | |
1607 | dprintk(verbose, FE_DEBUG, 1, "running DVB-S2 search algo .."); | |
1608 | if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) { | |
1609 | internal->lock = 1; | |
1610 | dprintk(verbose, FE_DEBUG, 1, | |
1611 | "-------------------------------------> DVB-S2 LOCK !"); | |
1612 | ||
1613 | // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */ | |
1614 | // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS); | |
1615 | // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1); | |
1616 | ||
1617 | return DVBFE_ALGO_SEARCH_SUCCESS; | |
1618 | } else { | |
1619 | internal->lock = 0; | |
1620 | ||
1621 | return DVBFE_ALGO_SEARCH_FAILED; | |
1622 | } | |
1623 | break; | |
1624 | default: | |
1625 | dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system"); | |
1626 | return DVBFE_ALGO_SEARCH_INVALID; | |
1627 | } | |
1628 | } | |
1629 | ||
1630 | return DVBFE_ALGO_SEARCH_ERROR; | |
1631 | } | |
1632 | ||
1633 | static enum stb0899_status stb0899_track_carrier(struct stb0899_state *state) | |
1634 | { | |
1635 | u8 reg; | |
1636 | ||
1637 | reg = stb0899_read_reg(state, STB0899_DSTATUS); | |
1638 | dprintk(verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg); | |
1639 | if (STB0899_GETFIELD(CARRIER_FOUND, reg)) { | |
1640 | dprintk(verbose, FE_DEBUG, 1, "-------------> CARRIEROK !"); | |
1641 | return CARRIEROK; | |
1642 | } else { | |
1643 | dprintk(verbose, FE_DEBUG, 1, "-------------> NOCARRIER !"); | |
1644 | return NOCARRIER; | |
1645 | } | |
1646 | ||
1647 | return NOCARRIER; | |
1648 | } | |
1649 | ||
1650 | static enum stb0899_status stb0899_get_ifagc(struct stb0899_state *state) | |
1651 | { | |
1652 | u8 reg; | |
1653 | ||
1654 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS); | |
1655 | dprintk(verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg); | |
1656 | if (STB0899_GETFIELD(IF_AGC_LOCK, reg)) { | |
1657 | dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !"); | |
1658 | return AGC1OK; | |
1659 | } else { | |
1660 | dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCK LOST !"); | |
1661 | return NOAGC1; | |
1662 | } | |
1663 | ||
1664 | return NOAGC1; | |
1665 | } | |
1666 | ||
1667 | static int stb0899_get_s1fec(struct stb0899_internal *internal, enum dvbfe_fec *fec) | |
1668 | { | |
1669 | switch (internal->fecrate) { | |
1670 | case STB0899_FEC_1_2: | |
1671 | *fec = DVBFE_FEC_1_2; | |
1672 | break; | |
1673 | case STB0899_FEC_2_3: | |
1674 | *fec = DVBFE_FEC_2_3; | |
1675 | break; | |
1676 | case STB0899_FEC_3_4: | |
1677 | *fec = DVBFE_FEC_3_4; | |
1678 | break; | |
1679 | case STB0899_FEC_5_6: | |
1680 | *fec = DVBFE_FEC_5_6; | |
1681 | break; | |
1682 | case STB0899_FEC_6_7: | |
1683 | *fec = DVBFE_FEC_6_7; | |
1684 | break; | |
1685 | case STB0899_FEC_7_8: | |
1686 | *fec = DVBFE_FEC_7_8; | |
1687 | break; | |
1688 | default: | |
1689 | return -EINVAL; | |
1690 | } | |
1691 | ||
1692 | return 0; | |
1693 | } | |
1694 | ||
1695 | static int stb0899_get_modcod(struct stb0899_internal *internal, struct dvbs2_params *params) | |
1696 | { | |
1697 | switch (internal->modcod) { | |
1698 | case STB0899_DUMMY_PLF: | |
1699 | params->modulation = DVBFE_MOD_NONE; | |
1700 | params->fec = DVBFE_FEC_NONE; | |
1701 | break; | |
1702 | case STB0899_QPSK_14: | |
1703 | params->modulation = DVBFE_MOD_QPSK; | |
1704 | params->fec = DVBFE_FEC_1_4; | |
1705 | break; | |
1706 | case STB0899_QPSK_13: | |
1707 | params->modulation = DVBFE_MOD_QPSK; | |
1708 | params->fec = DVBFE_FEC_1_3; | |
1709 | break; | |
1710 | case STB0899_QPSK_25: | |
1711 | params->modulation = DVBFE_MOD_QPSK; | |
1712 | params->fec = DVBFE_FEC_2_5; | |
1713 | break; | |
1714 | case STB0899_QPSK_12: | |
1715 | params->modulation = DVBFE_MOD_QPSK; | |
1716 | params->fec = DVBFE_FEC_1_2; | |
1717 | break; | |
1718 | case STB0899_QPSK_35: | |
1719 | params->modulation = DVBFE_MOD_QPSK; | |
1720 | params->fec = DVBFE_FEC_3_5; | |
1721 | break; | |
1722 | case STB0899_QPSK_23: | |
1723 | params->modulation = DVBFE_MOD_QPSK; | |
1724 | params->fec = DVBFE_FEC_2_3; | |
1725 | break; | |
1726 | case STB0899_QPSK_34: | |
1727 | params->modulation = DVBFE_MOD_QPSK; | |
1728 | params->fec = DVBFE_FEC_3_4; | |
1729 | break; | |
1730 | case STB0899_QPSK_45: | |
1731 | params->modulation = DVBFE_MOD_QPSK; | |
1732 | params->fec = DVBFE_FEC_4_5; | |
1733 | break; | |
1734 | case STB0899_QPSK_56: | |
1735 | params->modulation = DVBFE_MOD_QPSK; | |
1736 | params->fec = DVBFE_FEC_5_6; | |
1737 | break; | |
1738 | case STB0899_QPSK_89: | |
1739 | params->modulation = DVBFE_MOD_QPSK; | |
1740 | params->fec = DVBFE_FEC_8_9; | |
1741 | break; | |
1742 | case STB0899_QPSK_910: | |
1743 | params->modulation = DVBFE_MOD_QPSK; | |
1744 | params->fec = DVBFE_FEC_9_10; | |
1745 | break; | |
1746 | case STB0899_8PSK_35: | |
1747 | params->modulation = DVBFE_MOD_8PSK; | |
1748 | params->fec = DVBFE_FEC_3_5; | |
1749 | break; | |
1750 | case STB0899_8PSK_23: | |
1751 | params->modulation = DVBFE_MOD_8PSK; | |
1752 | params->fec = DVBFE_FEC_2_3; | |
1753 | break; | |
1754 | case STB0899_8PSK_34: | |
1755 | params->modulation = DVBFE_MOD_8PSK; | |
1756 | params->fec = DVBFE_FEC_3_4; | |
1757 | break; | |
1758 | case STB0899_8PSK_56: | |
1759 | params->modulation = DVBFE_MOD_8PSK; | |
1760 | params->fec = DVBFE_FEC_5_6; | |
1761 | break; | |
1762 | case STB0899_8PSK_89: | |
1763 | params->modulation = DVBFE_MOD_8PSK; | |
1764 | params->fec = DVBFE_FEC_8_9; | |
1765 | break; | |
1766 | case STB0899_8PSK_910: | |
1767 | params->modulation = DVBFE_MOD_8PSK; | |
1768 | params->fec = DVBFE_FEC_9_10; | |
1769 | break; | |
1770 | case STB0899_16APSK_23: | |
1771 | params->modulation = DVBFE_MOD_16APSK; | |
1772 | params->fec = DVBFE_FEC_2_3; | |
1773 | break; | |
1774 | case STB0899_16APSK_34: | |
1775 | params->modulation = DVBFE_MOD_16APSK; | |
1776 | params->fec = DVBFE_FEC_3_4; | |
1777 | break; | |
1778 | case STB0899_16APSK_45: | |
1779 | params->modulation = DVBFE_MOD_16APSK; | |
1780 | params->fec = DVBFE_FEC_4_5; | |
1781 | break; | |
1782 | case STB0899_16APSK_56: | |
1783 | params->modulation = DVBFE_MOD_16APSK; | |
1784 | params->fec = DVBFE_FEC_5_6; | |
1785 | break; | |
1786 | case STB0899_16APSK_89: | |
1787 | params->modulation = DVBFE_MOD_16APSK; | |
1788 | params->fec = DVBFE_FEC_8_9; | |
1789 | break; | |
1790 | case STB0899_16APSK_910: | |
1791 | params->modulation = DVBFE_MOD_16APSK; | |
1792 | params->fec = DVBFE_FEC_9_10; | |
1793 | break; | |
1794 | case STB0899_32APSK_34: | |
1795 | params->modulation = DVBFE_MOD_32APSK; | |
1796 | params->fec = DVBFE_FEC_3_4; | |
1797 | break; | |
1798 | case STB0899_32APSK_45: | |
1799 | params->modulation = DVBFE_MOD_32APSK; | |
1800 | params->fec = DVBFE_FEC_4_5; | |
1801 | break; | |
1802 | case STB0899_32APSK_56: | |
1803 | params->modulation = DVBFE_MOD_32APSK; | |
1804 | params->fec = DVBFE_FEC_5_6; | |
1805 | break; | |
1806 | case STB0899_32APSK_89: | |
1807 | params->modulation = DVBFE_MOD_32APSK; | |
1808 | params->fec = DVBFE_FEC_8_9; | |
1809 | break; | |
1810 | case STB0899_32APSK_910: | |
1811 | params->modulation = DVBFE_MOD_32APSK; | |
1812 | params->fec = DVBFE_FEC_9_10; | |
1813 | break; | |
1814 | default: | |
1815 | return -EINVAL; | |
1816 | } | |
1817 | ||
1818 | return 0; | |
1819 | } | |
1820 | ||
1821 | /* | |
1822 | * stb0899_track | |
1823 | * periodically check the signal level against a specified | |
1824 | * threshold level and perform derotator centering. | |
1825 | * called once we have a lock from a succesful search | |
1826 | * event. | |
1827 | * | |
1828 | * Will be called periodically called to maintain the | |
1829 | * lock. | |
1830 | * | |
1831 | * Will be used to get parameters as well as info from | |
1832 | * the decoded baseband header | |
1833 | * | |
1834 | * Once a new lock has established, the internal state | |
1835 | * frequency (internal->freq) is updated | |
1836 | */ | |
795b72b2 | 1837 | static int stb0899_track(struct dvb_frontend *fe, struct dvbfe_params *params, int *delay) |
8bd135ba | 1838 | { |
795b72b2 MA |
1839 | u32 lock_lost; |
1840 | ||
8bd135ba MA |
1841 | struct stb0899_state *state = fe->demodulator_priv; |
1842 | struct stb0899_internal *internal = &state->internal; | |
1843 | ||
795b72b2 MA |
1844 | lock_lost = STB0899_READ_S2REG(STB0899_S2DEMOD, LOCK_LOST); |
1845 | dprintk(verbose, FE_DEBUG, 1, "Lock Lost=[0x%02x]\n", lock_lost); | |
1846 | if (STB0899_GETFIELD(LOCK_LOST, lock_lost)) | |
1847 | dprintk(verbose, FE_ERROR, 1, "Demodulator LOST LOCK !\n"); | |
1848 | ||
1849 | *delay = HZ/10; | |
8bd135ba MA |
1850 | |
1851 | return 0; | |
1852 | } | |
1853 | ||
1854 | static int stb0899_get_params(struct dvb_frontend *fe, struct dvbfe_params *params) | |
1855 | { | |
1856 | struct stb0899_state *state = fe->demodulator_priv; | |
1857 | struct stb0899_internal *internal = &state->internal; | |
1858 | ||
1859 | params->frequency = internal->freq; | |
1860 | params->inversion = internal->inversion; | |
1861 | params->delivery = state->delsys; | |
1862 | switch (state->delsys) { | |
1863 | case DVBFE_DELSYS_DVBS: | |
1864 | dprintk(verbose, FE_DEBUG, 1, "Get DVB-S params"); | |
1865 | params->delsys.dvbs.symbol_rate = internal->srate; | |
1866 | params->delsys.dvbs.modulation = DVBFE_MOD_QPSK; | |
1867 | break; | |
1868 | case DVBFE_DELSYS_DSS: | |
1869 | dprintk(verbose, FE_DEBUG, 1, "Get DSS params"); | |
1870 | params->delsys.dss.symbol_rate = internal->srate; | |
1871 | params->delsys.dss.modulation = DVBFE_MOD_QPSK; | |
8bd135ba MA |
1872 | break; |
1873 | case DVBFE_DELSYS_DVBS2: | |
1874 | dprintk(verbose, FE_DEBUG, 1, "Get DVB-S2 params"); | |
1875 | params->delsys.dvbs2.symbol_rate = internal->srate; | |
1876 | break; | |
1877 | default: | |
1878 | dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system"); | |
1879 | return -EINVAL; | |
1880 | } | |
1881 | ||
1882 | return 0; | |
1883 | } | |
1884 | ||
1885 | static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe) | |
1886 | { | |
1887 | return DVBFE_ALGO_CUSTOM; | |
1888 | } | |
1889 | ||
1890 | static struct dvb_frontend_ops stb0899_ops = { | |
1891 | ||
1892 | .info = { | |
1893 | .name = "STB0899 Multistandard", | |
1894 | }, | |
1895 | ||
1896 | .release = stb0899_release, | |
1897 | .init = stb0899_init, | |
1898 | .sleep = stb0899_sleep, | |
1899 | // .wakeup = stb0899_wakeup, | |
1900 | ||
1901 | .i2c_gate_ctrl = stb0899_i2c_gate_ctrl, | |
1902 | .get_info = stb0899_get_info, | |
1903 | .get_delsys = stb0899_get_delsys, | |
1904 | ||
1905 | .get_frontend_algo = stb0899_frontend_algo, | |
1906 | .search = stb0899_search, | |
1907 | .track = stb0899_track, | |
1908 | .get_params = stb0899_get_params, | |
1909 | ||
1910 | .read_status = stb0899_read_status, | |
1911 | .read_snr = stb0899_read_snr, | |
1912 | .read_signal_strength = stb0899_read_signal_strength, | |
1913 | .read_status = stb0899_read_status, | |
1914 | .read_ber = stb0899_read_ber, | |
1915 | ||
1916 | .set_voltage = stb0899_set_voltage, | |
1917 | .set_tone = stb0899_set_tone, | |
1918 | ||
1919 | .diseqc_send_master_cmd = stb0899_send_diseqc_msg, | |
1920 | .diseqc_recv_slave_reply = stb0899_recv_slave_reply, | |
1921 | .diseqc_send_burst = stb0899_send_diseqc_burst, | |
1922 | }; | |
1923 | ||
1924 | struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c) | |
1925 | { | |
1926 | struct stb0899_state *state = NULL; | |
b48e2de3 | 1927 | enum stb0899_inversion inversion; |
8bd135ba MA |
1928 | |
1929 | state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL); | |
1930 | if (state == NULL) | |
1931 | goto error; | |
1932 | ||
b48e2de3 | 1933 | inversion = config->inversion; |
8bd135ba MA |
1934 | state->verbose = verbose; |
1935 | state->config = config; | |
1936 | state->i2c = i2c; | |
1937 | state->frontend.ops = stb0899_ops; | |
1938 | state->frontend.demodulator_priv = state; | |
b48e2de3 | 1939 | state->internal.inversion = inversion; |
8bd135ba MA |
1940 | |
1941 | stb0899_wakeup(&state->frontend); | |
1942 | if (stb0899_get_dev_id(state) == -ENODEV) { | |
1943 | printk("%s: Exiting .. !\n", __func__); | |
1944 | goto error; | |
1945 | } | |
1946 | ||
1947 | printk("%s: Attaching STB0899 \n", __func__); | |
1948 | return &state->frontend; | |
1949 | ||
1950 | error: | |
1951 | kfree(state); | |
1952 | return NULL; | |
1953 | } | |
1954 | EXPORT_SYMBOL(stb0899_attach); | |
1955 | MODULE_PARM_DESC(verbose, "Set Verbosity level"); | |
1956 | MODULE_AUTHOR("Manu Abraham"); | |
1957 | MODULE_DESCRIPTION("STB0899 Multi-Std frontend"); | |
1958 | MODULE_LICENSE("GPL"); |