blob: cf787f5fc00cc4bc7485cb25bbc081e924e557ab [file] [log] [blame]
Manu Abraham8bd135b2007-07-03 09:53:42 -03001/*
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
Reinhard Nisslc615a272008-07-09 15:33:38 -030034static unsigned int verbose = 0;//1;
Manu Abraham8bd135b2007-07-03 09:53:42 -030035module_param(verbose, int, 0644);
36
37/* C/N in dB/10, NIRM/NIRL */
38static 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 */
68static 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 */
109static 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*/
137struct 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 */
180struct 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
220int _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)
Reinhard Nisslc615a272008-07-09 15:33:38 -0300244 dprintk(state->verbose, FE_ERROR, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -0300245 "Read error, Reg=[0x%02x], Status=%d",
246 reg, ret);
247
248 return ret < 0 ? ret : -EREMOTEIO;
249 }
Reinhard Nisslc615a272008-07-09 15:33:38 -0300250 if (unlikely(*state->verbose >= FE_DEBUGREG))
251 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300252 reg, buf);
253
Manu Abraham8bd135b2007-07-03 09:53:42 -0300254 return (unsigned int)buf;
255}
256
257int 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
274u32 _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]);
Reinhard Nisslc615a272008-07-09 15:33:38 -0300364 if (unlikely(*state->verbose >= FE_DEBUGREG))
Manu Abraham8bd135b2007-07-03 09:53:42 -0300365 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
370err:
371 return status < 0 ? status : -EREMOTEIO;
372}
373
374int 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
Reinhard Nisslc615a272008-07-09 15:33:38 -0300421 if (unlikely(*state->verbose >= FE_DEBUGREG))
Manu Abraham8bd135b2007-07-03 09:53:42 -0300422 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
443err:
444 return status < 0 ? status : -EREMOTEIO;
445}
446
Manu Abraham3d6a3beb2008-01-25 20:39:16 -0300447int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300448{
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
Reinhard Nisslc615a272008-07-09 15:33:38 -0300483 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300484 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;
494err:
495 return status < 0 ? status : -EREMOTEIO;
496}
497
Manu Abraham85eabac2008-01-25 20:28:46 -0300498int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300499{
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
Reinhard Nisslc615a272008-07-09 15:33:38 -0300513 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300514 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)
Reinhard Nisslc615a272008-07-09 15:33:38 -0300533 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300534 reg, data[0], count, ret);
535 return ret < 0 ? ret : -EREMOTEIO;
536 }
537
538 return 0;
539}
540
541int 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 */
551static u32 stb0899_get_mclk(struct stb0899_state *state)
552{
Manu Abraham72f78412007-11-10 16:59:16 -0300553 u32 mclk = 0, div = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300554
555 div = stb0899_read_reg(state, STB0899_NCOARSE);
556 mclk = (div + 1) * state->config->xtal_freq / 6;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300557 dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300558
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 */
568static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
569{
570 struct stb0899_internal *internal = &state->internal;
571 u8 mdiv = 0;
572
Reinhard Nisslc615a272008-07-09 15:33:38 -0300573 dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300574 mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300575 dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300576
577 stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
578 internal->master_clk = stb0899_get_mclk(state);
579
Reinhard Nisslc615a272008-07-09 15:33:38 -0300580 dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300581}
582
Manu Abrahamb168e352007-10-30 19:46:49 -0300583static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
584{
585 struct stb0899_config *config = state->config;
Manu Abraham043a68b2008-01-18 14:15:17 -0300586 const struct stb0899_postproc *postproc = config->postproc;
Manu Abrahamb168e352007-10-30 19:46:49 -0300587
588 /* post process event */
589 if (postproc) {
590 if (enable) {
Manu Abraham9efdd292007-10-30 20:05:59 -0300591 if (postproc[ctl].level == STB0899_GPIOPULLUP)
Manu Abrahamb168e352007-10-30 19:46:49 -0300592 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
593 else
594 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
595 } else {
Manu Abraham9efdd292007-10-30 20:05:59 -0300596 if (postproc[ctl].level == STB0899_GPIOPULLUP)
Manu Abrahamb168e352007-10-30 19:46:49 -0300597 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
598 else
599 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
600 }
601 }
602 return 0;
603}
604
Manu Abraham8bd135b2007-07-03 09:53:42 -0300605static void stb0899_release(struct dvb_frontend *fe)
606{
607 struct stb0899_state *state = fe->demodulator_priv;
608
Reinhard Nisslc615a272008-07-09 15:33:38 -0300609 dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
Manu Abrahamb168e352007-10-30 19:46:49 -0300610 /* post process event */
611 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300612 kfree(state);
613}
614
615/*
616 * stb0899_get_alpha
617 * return: rolloff
618 */
619static int stb0899_get_alpha(struct stb0899_state *state)
620{
621 u8 mode_coeff;
622
623 mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
624
625 if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
626 return 20;
627 else
628 return 35;
629}
630
631/*
632 * stb0899_init_calc
633 */
634static void stb0899_init_calc(struct stb0899_state *state)
635{
636 struct stb0899_internal *internal = &state->internal;
637 int master_clk;
Marko Schluessler7d8f1e52007-10-23 19:56:18 -0300638 u8 agc[2];
Manu Abraham8bd135b2007-07-03 09:53:42 -0300639 u8 agc1cn;
640 u32 reg;
641
642 /* Read registers (in burst mode) */
643 agc1cn = stb0899_read_reg(state, STB0899_AGC1CN);
644 stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
645
646 /* Initial calculations */
647 master_clk = stb0899_get_mclk(state);
648 internal->t_agc1 = 0;
649 internal->t_agc2 = 0;
650 internal->master_clk = master_clk;
651 internal->mclk = master_clk / 65536L;
652 internal->rolloff = stb0899_get_alpha(state);
653
654 /* DVBS2 Initial calculations */
655 /* Set AGC value to the middle */
656 internal->agc_gain = 8154;
657 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
658 STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
659 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
660
661 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
662 internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
663
664 internal->center_freq = 0;
665 internal->av_frame_coarse = 10;
666 internal->av_frame_fine = 20;
667 internal->step_size = 2;
668/*
669 if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
670 pParams->IQLocked = 0;
671 else
672 pParams->IQLocked = 1;
673*/
674}
675
676static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
677{
678 u8 reg = 0;
679 unsigned long start = jiffies;
680
681 while (1) {
682 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
683 if (!STB0899_GETFIELD(FIFOFULL, reg))
684 break;
685 if ((jiffies - start) > timeout) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300686 dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300687 return -ETIMEDOUT;
688 }
689 }
690
691 return 0;
692}
693
694static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
695{
696 struct stb0899_state *state = fe->demodulator_priv;
697 u8 reg, i;
698
699 if (cmd->msg_len > 8)
700 return -EINVAL;
701
702 /* enable FIFO precharge */
703 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
704 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
705 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
706 for (i = 0; i < cmd->msg_len; i++) {
707 /* wait for FIFO empty */
708 if (stb0899_wait_diseqc_fifo_empty(state, 10) < 0)
709 return -ETIMEDOUT;
710
711 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
712 }
713 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
714 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
715 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
716
717 return 0;
718}
719
720static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
721{
722 u8 reg = 0;
723 unsigned long start = jiffies;
724
725 while (!STB0899_GETFIELD(RXEND, reg)) {
726 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
727 if (jiffies - start > timeout) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300728 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300729 return -ETIMEDOUT;
730 }
731 msleep(10);
732 }
733
734 return 0;
735}
736
737static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
738{
739 struct stb0899_state *state = fe->demodulator_priv;
740 u8 reg, length = 0, i;
741 int result;
742
743 if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
744 return -ETIMEDOUT;
745
746 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
747 if (STB0899_GETFIELD(RXEND, reg)) {
748
749 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
750 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
751
752 if (length > sizeof (reply->msg)) {
753 result = -EOVERFLOW;
754 goto exit;
755 }
756 reply->msg_len = length;
757
758 /* extract data */
759 for (i = 0; i < length; i++)
760 reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
761 }
762
763 return 0;
764exit:
765
766 return result;
767}
768
769static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
770{
771 u8 reg = 0;
772 unsigned long start = jiffies;
773
774 while (!STB0899_GETFIELD(TXIDLE, reg)) {
775 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
776 if (jiffies - start > timeout) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300777 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300778 return -ETIMEDOUT;
779 }
780 msleep(10);
781 }
782 return 0;
783}
784
785static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
786{
787 struct stb0899_state *state = fe->demodulator_priv;
788 u8 reg, old_state;
789
790 /* wait for diseqc idle */
791 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
792 return -ETIMEDOUT;
793
794 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
795 old_state = reg;
796 /* set to burst mode */
797 STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x02);
798 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
799 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
800 switch (burst) {
801 case SEC_MINI_A:
802 /* unmodulated */
803 stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
804 break;
805 case SEC_MINI_B:
806 /* modulated */
807 stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
808 break;
809 }
810 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
811 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
812 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
813 /* wait for diseqc idle */
814 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
815 return -ETIMEDOUT;
816
817 /* restore state */
818 stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
819
820 return 0;
821}
822
Manu Abraham6c1022c2007-10-05 11:41:19 -0300823static int stb0899_diseqc_init(struct stb0899_state *state)
824{
825 struct dvb_diseqc_master_cmd tx_data;
826 struct dvb_diseqc_slave_reply rx_data;
827
828 u8 f22_tx, f22_rx, reg;
829
830 u32 mclk, tx_freq = 22000, count = 0, i;
831
832 u32 trial = 0; /* try max = 2 (try 20khz and 17.5 khz) */
833 u32 ret_1 = 0; /* 20 Khz status */
834 u32 ret_2 = 0; /* 17.5 Khz status */
835
836 tx_data.msg[0] = 0xe2;
837 tx_data.msg_len = 3;
838 reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
839 STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
840 stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
841
842 /* disable Tx spy */
843 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
844 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
845 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
846
847 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
848 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
849 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
850
851 mclk = stb0899_get_mclk(state);
852 f22_tx = mclk / (tx_freq * 32);
853 stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
854 state->rx_freq = 20000;
855 f22_rx = mclk / (state->rx_freq * 32);
856
857 return 0;
858}
Manu Abraham8bd135b2007-07-03 09:53:42 -0300859
860static int stb0899_sleep(struct dvb_frontend *fe)
861{
862 struct stb0899_state *state = fe->demodulator_priv;
863 u8 reg;
864
Reinhard Nisslc615a272008-07-09 15:33:38 -0300865 dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
Manu Abrahamb168e352007-10-30 19:46:49 -0300866 /* post process event */
867 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
868
Manu Abraham8bd135b2007-07-03 09:53:42 -0300869 return 0;
870}
871
872static int stb0899_wakeup(struct dvb_frontend *fe)
873{
874 int rc;
875 struct stb0899_state *state = fe->demodulator_priv;
876
Manu Abrahamde29eb82007-09-22 21:39:17 -0300877 if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
878 return rc;
879 /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
880 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
881 return rc;
882 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
883 return rc;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300884
Manu Abrahamb168e352007-10-30 19:46:49 -0300885 /* post process event */
886 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
887
Manu Abraham8bd135b2007-07-03 09:53:42 -0300888 return 0;
889}
890
891static int stb0899_init(struct dvb_frontend *fe)
892{
893 int i;
894 struct stb0899_state *state = fe->demodulator_priv;
895 struct stb0899_config *config = state->config;
896
Reinhard Nisslc615a272008-07-09 15:33:38 -0300897 dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300898// mutex_init(&state->search_lock);
899
900 /* init device */
Reinhard Nisslc615a272008-07-09 15:33:38 -0300901 dprintk(state->verbose, FE_DEBUG, 1, "init device");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300902 for (i = 0; config->init_dev[i].address != 0xffff; i++)
903 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
904
Reinhard Nisslc615a272008-07-09 15:33:38 -0300905 dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300906 /* init S2 demod */
907 for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
908 stb0899_write_s2reg(state, STB0899_S2DEMOD,
909 config->init_s2_demod[i].base_address,
910 config->init_s2_demod[i].offset,
911 config->init_s2_demod[i].data);
912
Reinhard Nisslc615a272008-07-09 15:33:38 -0300913 dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300914 /* init S1 demod */
915 for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
916 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
917
Reinhard Nisslc615a272008-07-09 15:33:38 -0300918 dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300919 /* init S2 fec */
920 for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
921 stb0899_write_s2reg(state, STB0899_S2FEC,
922 config->init_s2_fec[i].base_address,
923 config->init_s2_fec[i].offset,
924 config->init_s2_fec[i].data);
925
Reinhard Nisslc615a272008-07-09 15:33:38 -0300926 dprintk(state->verbose, FE_DEBUG, 1, "init TST");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300927 /* init test */
928 for (i = 0; config->init_tst[i].address != 0xffff; i++)
929 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
930
931 stb0899_init_calc(state);
Manu Abraham6c1022c2007-10-05 11:41:19 -0300932 stb0899_diseqc_init(state);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300933
934 return 0;
935}
936
937static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
938{
939 int res = 0;
940 int min = 0, med;
941
942 if (val < tab[min].read)
943 res = tab[min].real;
944 else if (val >= tab[max].read)
945 res = tab[max].real;
946 else {
947 while ((max - min) > 1) {
948 med = (max + min) / 2;
949 if (val >= tab[min].read && val < tab[med].read)
950 max = med;
951 else
952 min = med;
953 }
954 res = ((val - tab[min].read) *
955 (tab[max].real - tab[min].real) /
956 (tab[max].read - tab[min].read)) +
957 tab[min].real;
958 }
959
960 return res;
961}
962
963static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
964{
965 struct stb0899_state *state = fe->demodulator_priv;
966 struct stb0899_internal *internal = &state->internal;
967
968 int val;
969 u32 reg;
970 switch (state->delsys) {
971 case DVBFE_DELSYS_DVBS:
972 case DVBFE_DELSYS_DSS:
973 if (internal->lock) {
974 reg = stb0899_read_reg(state, STB0899_VSTATUS);
975 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
976
977 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
978 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
979
980 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
981 *strength += 750;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300982 dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300983 val & 0xff, *strength);
984 }
985 }
986 break;
987 case DVBFE_DELSYS_DVBS2:
988 if (internal->lock) {
989 reg = STB0899_READ_S2REG(STB0899_DEMOD, IF_AGC_GAIN);
990 val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
991
992 *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
993 *strength += 750;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300994 dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300995 val & 0x3fff, *strength);
996 }
997 break;
998 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -0300999 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001000 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001001 }
1002
1003 return 0;
1004}
1005
1006static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
1007{
1008 struct stb0899_state *state = fe->demodulator_priv;
1009 struct stb0899_internal *internal = &state->internal;
1010
1011 unsigned int val, quant, quantn = -1, est, estn = -1;
1012 u8 buf[2];
1013 u32 reg;
1014
1015 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1016 switch (state->delsys) {
1017 case DVBFE_DELSYS_DVBS:
1018 case DVBFE_DELSYS_DSS:
1019 if (internal->lock) {
1020 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1021
1022 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1023 val = MAKEWORD16(buf[0], buf[1]);
1024
1025 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001026 dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
Manu Abraham8bd135b2007-07-03 09:53:42 -03001027 buf[0], buf[1], val, *snr);
1028 }
1029 }
1030 break;
1031 case DVBFE_DELSYS_DVBS2:
1032 if (internal->lock) {
1033 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1034 quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1035 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1036 est = STB0899_GETFIELD(ESN0_EST, reg);
1037 if (est == 1)
1038 val = 301; /* C/N = 30.1 dB */
1039 else if (est == 2)
1040 val = 270; /* C/N = 27.0 dB */
1041 else {
1042 /* quantn = 100 * log(quant^2) */
1043 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1044 /* estn = 100 * log(est) */
1045 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1046 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1047 val = (quantn - estn) / 10;
1048 }
1049 *snr = val;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001050 dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -03001051 quant, quantn, est, estn, val);
1052 }
1053 break;
1054 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001055 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001056 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001057 }
1058
1059 return 0;
1060}
1061
1062static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1063{
1064 struct stb0899_state *state = fe->demodulator_priv;
1065 struct stb0899_internal *internal = &state->internal;
1066 u8 reg;
1067 *status = 0;
1068
1069 switch (state->delsys) {
1070 case DVBFE_DELSYS_DVBS:
1071 case DVBFE_DELSYS_DSS:
1072 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1073 if (internal->lock) {
1074 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1075 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1076 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1077 *status |= FE_HAS_CARRIER | FE_HAS_LOCK;
1078
1079 reg = stb0899_read_reg(state, STB0899_PLPARM);
1080 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1081 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1082 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
Manu Abrahamb168e352007-10-30 19:46:49 -03001083 /* post process event */
1084 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001085 }
1086 }
1087 }
1088 break;
1089 case DVBFE_DELSYS_DVBS2:
1090 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1091 if (internal->lock) {
1092 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1093 if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1094 *status |= FE_HAS_CARRIER;
1095 dprintk(state->verbose, FE_DEBUG, 1,
1096 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1097
1098 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1099 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1100 *status |= FE_HAS_LOCK;
1101 dprintk(state->verbose, FE_DEBUG, 1,
1102 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1103
1104 }
1105 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1106 *status |= FE_HAS_VITERBI;
1107 dprintk(state->verbose, FE_DEBUG, 1,
1108 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1109 }
1110 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1111 *status |= FE_HAS_SYNC;
1112 dprintk(state->verbose, FE_DEBUG, 1,
1113 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
Manu Abrahamb168e352007-10-30 19:46:49 -03001114 /* post process event */
1115 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001116 }
1117 }
1118 }
1119 break;
1120 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001121 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001122 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001123 }
1124 return 0;
1125}
1126
1127/*
1128 * stb0899_get_error
1129 * viterbi error for DVB-S/DSS
1130 * packet error for DVB-S2
1131 * Bit Error Rate or Packet Error Rate * 10 ^ 7
1132 */
1133static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1134{
1135 struct stb0899_state *state = fe->demodulator_priv;
1136 struct stb0899_internal *internal = &state->internal;
1137
1138 u8 lsb, msb;
1139 u32 i;
1140
1141 *ber = 0;
1142
1143 switch (state->delsys) {
1144 case DVBFE_DELSYS_DVBS:
1145 case DVBFE_DELSYS_DSS:
1146 if (internal->lock) {
1147 /* average 5 BER values */
1148 for (i = 0; i < 5; i++) {
1149 msleep(100);
1150 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1151 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1152 *ber += MAKEWORD16(msb, lsb);
1153 }
1154 *ber /= 5;
1155 /* Viterbi Check */
1156 if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1157 /* Error Rate */
1158 *ber *= 9766;
1159 /* ber = ber * 10 ^ 7 */
1160 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1161 *ber /= 8;
1162 }
1163 }
1164 break;
1165 case DVBFE_DELSYS_DVBS2:
1166 if (internal->lock) {
1167 /* Average 5 PER values */
1168 for (i = 0; i < 5; i++) {
1169 msleep(100);
1170 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1171 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1172 *ber += MAKEWORD16(msb, lsb);
1173 }
1174 /* ber = ber * 10 ^ 7 */
1175 *ber *= 10000000;
1176 *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1177 }
1178 break;
1179 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001180 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001181 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001182 }
1183
1184 return 0;
1185}
1186
1187static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
1188{
1189 struct stb0899_state *state = fe->demodulator_priv;
1190
1191 switch (voltage) {
1192 case SEC_VOLTAGE_13:
1193 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1194 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1195 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1196 break;
1197 case SEC_VOLTAGE_18:
1198 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1199 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1200 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1201 break;
1202 case SEC_VOLTAGE_OFF:
1203 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1204 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1205 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1206 break;
1207 default:
1208 return -EINVAL;
1209 }
1210
1211 return 0;
1212}
1213
1214static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
1215{
1216 struct stb0899_state *state = fe->demodulator_priv;
1217 struct stb0899_internal *internal = &state->internal;
1218
Manu Abrahambaa40e42007-10-05 09:04:32 -03001219 u8 div, reg;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001220
1221 /* wait for diseqc idle */
1222 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1223 return -ETIMEDOUT;
1224
1225 switch (tone) {
1226 case SEC_TONE_ON:
1227 div = (internal->master_clk / 100) / 5632;
1228 div = (div + 5) / 10;
1229 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
Manu Abrahambaa40e42007-10-05 09:04:32 -03001230 reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1231 STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1232 stb0899_write_reg(state, STB0899_ACRPRESC, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001233 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1234 break;
1235 case SEC_TONE_OFF:
1236 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1237 break;
1238 default:
Manu Abraham417dd692007-10-10 06:12:13 -03001239 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001240 }
1241 return 0;
1242}
1243
Manu Abraham40e8ce32008-02-03 19:37:02 -03001244int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001245{
1246 int i2c_stat;
1247 struct stb0899_state *state = fe->demodulator_priv;
1248
1249 i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1250 if (i2c_stat < 0)
1251 goto err;
1252
1253 if (enable) {
1254 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1255 i2c_stat |= STB0899_I2CTON;
1256 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1257 goto err;
Manu Abraham40e8ce32008-02-03 19:37:02 -03001258 } else {
1259 dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
1260 i2c_stat &= ~STB0899_I2CTON;
1261 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1262 goto err;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001263 }
1264 return 0;
1265err:
Manu Abraham40e8ce32008-02-03 19:37:02 -03001266 dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001267 return -EREMOTEIO;
1268}
1269
1270
1271static inline void CONVERT32(u32 x, char *str)
1272{
1273 *str++ = (x >> 24) & 0xff;
1274 *str++ = (x >> 16) & 0xff;
1275 *str++ = (x >> 8) & 0xff;
1276 *str++ = (x >> 0) & 0xff;
1277 *str = '\0';
1278}
1279
1280int stb0899_get_dev_id(struct stb0899_state *state)
1281{
1282 u8 chip_id, release;
1283 u16 id;
1284 u32 demod_ver = 0, fec_ver = 0;
Reinhard Nissl56137412008-01-21 16:17:48 -03001285 char demod_str[5] = { 0 };
1286 char fec_str[5] = { 0 };
Manu Abraham8bd135b2007-07-03 09:53:42 -03001287
1288 id = stb0899_read_reg(state, STB0899_DEV_ID);
1289 dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1290 chip_id = STB0899_GETFIELD(CHIP_ID, id);
1291 release = STB0899_GETFIELD(CHIP_REL, id);
1292
1293 dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1294 chip_id, release);
1295
1296 CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1297
1298 demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1299 dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1300 CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1301 fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1302 if (! (chip_id > 0)) {
1303 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1304
1305 return -ENODEV;
1306 }
1307 dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1308
1309 return 0;
1310}
1311
1312static const struct dvbfe_info dvbs_info = {
1313 .name = "STB0899 DVB-S",
Manu Abraham8bd135b2007-07-03 09:53:42 -03001314 .delsys = {
1315 .dvbs.modulation = DVBFE_MOD_QPSK,
1316 .dvbs.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
1317 DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
1318 DVBFE_FEC_6_7
1319 },
1320
1321 .frequency_min = 950000,
1322 .frequency_max = 2150000,
1323 .frequency_step = 0,
1324 .symbol_rate_min = 1000000,
1325 .symbol_rate_max = 45000000,
1326 .symbol_rate_tolerance = 0
1327};
1328
1329static const struct dvbfe_info dss_info = {
1330 .name = "STB0899 DSS",
Manu Abraham8bd135b2007-07-03 09:53:42 -03001331 .delsys = {
1332 .dss.modulation = DVBFE_MOD_BPSK | DVBFE_MOD_QPSK,
1333 .dss.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
1334 DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
1335 DVBFE_FEC_6_7
1336 },
1337
1338 .frequency_min = 950000,
1339 .frequency_max = 2150000,
1340 .frequency_step = 0,
1341 .symbol_rate_min = 1000000,
1342 .symbol_rate_max = 45000000,
1343 .symbol_rate_tolerance = 0
1344};
1345
1346static const struct dvbfe_info dvbs2_info = {
1347 .name = "STB0899 DVB-S2",
Manu Abraham8bd135b2007-07-03 09:53:42 -03001348 .delsys = {
1349 .dvbs2.modulation = DVBFE_MOD_QPSK | DVBFE_MOD_8PSK |
1350 DVBFE_MOD_16APSK | DVBFE_MOD_32APSK,
1351
1352 .dvbs2.fec = DVBFE_FEC_1_4 | DVBFE_FEC_1_3 |
1353 DVBFE_FEC_2_5 | DVBFE_FEC_1_2 |
1354 DVBFE_FEC_3_5 | DVBFE_FEC_2_3 |
1355 DVBFE_FEC_3_4 | DVBFE_FEC_4_5 |
1356 DVBFE_FEC_5_6 | DVBFE_FEC_8_9 |
1357 DVBFE_FEC_9_10,
1358 },
1359
1360 .frequency_min = 950000,
1361 .frequency_max = 2150000,
1362 .frequency_step = 0,
1363 .symbol_rate_min = 1000000,
1364 .symbol_rate_max = 45000000,
1365 .symbol_rate_tolerance = 0
1366};
1367
1368static int stb0899_get_info(struct dvb_frontend *fe, struct dvbfe_info *fe_info)
1369{
1370 struct stb0899_state *state = fe->demodulator_priv;
1371
Reinhard Nisslc615a272008-07-09 15:33:38 -03001372 dprintk(state->verbose, FE_DEBUG, 1, "Get Info");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001373
Manu Abraham8bd135b2007-07-03 09:53:42 -03001374 switch (state->delsys) {
1375 case DVBFE_DELSYS_DVBS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001376 dprintk(state->verbose, FE_ERROR, 1, "Querying DVB-S info");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001377 memcpy(fe_info, &dvbs_info, sizeof (struct dvbfe_info));
1378 break;
1379 case DVBFE_DELSYS_DSS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001380 dprintk(state->verbose, FE_ERROR, 1, "Querying DSS info");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001381 memcpy(fe_info, &dss_info, sizeof (struct dvbfe_info));
1382 break;
1383 case DVBFE_DELSYS_DVBS2:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001384 dprintk(state->verbose, FE_ERROR, 1, "Querying DVB-S2 info");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001385 memcpy(fe_info, &dvbs2_info, sizeof (struct dvbfe_info));
1386 break;
1387 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001388 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001389 return -EINVAL;
1390 }
Reinhard Nisslc615a272008-07-09 15:33:38 -03001391 dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001392
1393 return 0;
1394}
1395
1396static int stb0899_get_delsys(struct dvb_frontend *fe, enum dvbfe_delsys *fe_delsys)
1397{
1398 *fe_delsys = DVBFE_DELSYS_DVBS | DVBFE_DELSYS_DSS | DVBFE_DELSYS_DVBS2;
1399
1400 return 0;
1401}
1402
Manu Abrahame399a782008-03-08 20:28:09 -03001403static void stb0899_set_delivery(struct stb0899_state *state)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001404{
1405 u8 reg;
1406 u8 stop_clk[2];
1407
1408 stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1409 stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1410
1411 switch (state->delsys) {
1412 case DVBFE_DELSYS_DVBS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001413 dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001414 /* FECM/Viterbi ON */
1415 reg = stb0899_read_reg(state, STB0899_FECM);
1416 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1417 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1418 stb0899_write_reg(state, STB0899_FECM, reg);
1419
1420 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1421 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1422 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1423 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1424
1425 reg = stb0899_read_reg(state, STB0899_TSTRES);
1426 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1427 stb0899_write_reg(state, STB0899_TSTRES, reg);
1428
1429 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1430 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1431 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1432
1433 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1434 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1435
1436 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
Manu Abraham18527be2008-01-25 18:25:13 -03001437 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001438
1439 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1440 break;
1441 case DVBFE_DELSYS_DVBS2:
1442 /* FECM/Viterbi OFF */
1443 reg = stb0899_read_reg(state, STB0899_FECM);
1444 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1445 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1446 stb0899_write_reg(state, STB0899_FECM, reg);
1447
1448 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1449 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1450 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1451 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1452
1453 reg = stb0899_read_reg(state, STB0899_TSTRES);
1454 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1455 stb0899_write_reg(state, STB0899_TSTRES, reg);
1456
1457 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1458 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1459 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1460
1461 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1462 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1463
1464 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1465 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1466
1467 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1468 break;
1469 case DVBFE_DELSYS_DSS:
1470 /* FECM/Viterbi ON */
1471 reg = stb0899_read_reg(state, STB0899_FECM);
1472 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1473 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1474 stb0899_write_reg(state, STB0899_FECM, reg);
1475
1476 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1477 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1478 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1479
1480 reg = stb0899_read_reg(state, STB0899_TSTRES);
1481 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1482 stb0899_write_reg(state, STB0899_TSTRES, reg);
1483
1484 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1485 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1486 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1487
1488 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1489 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1490
1491 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1492
1493 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1494 break;
1495 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001496 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001497 break;
1498 }
1499 STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1500 stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1501}
1502
1503/*
1504 * stb0899_set_iterations
1505 * set the LDPC iteration scale function
1506 */
1507static void stb0899_set_iterations(struct stb0899_state *state)
1508{
1509 struct stb0899_internal *internal = &state->internal;
1510 struct stb0899_config *config = state->config;
1511
1512 s32 iter_scale;
1513 u32 reg;
1514
1515 iter_scale = 17 * (internal->master_clk / 1000);
1516 iter_scale += 410000;
1517 iter_scale /= (internal->srate / 1000000);
1518 iter_scale /= 1000;
1519
1520 if (iter_scale > config->ldpc_max_iter)
1521 iter_scale = config->ldpc_max_iter;
1522
1523 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, MAX_ITER);
1524 STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1525 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1526}
1527
1528static enum dvbfe_search stb0899_search(struct dvb_frontend *fe, struct dvbfe_params *params)
1529{
1530 struct stb0899_state *state = fe->demodulator_priv;
1531 struct stb0899_params *i_params = &state->params;
1532 struct stb0899_internal *internal = &state->internal;
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001533 struct stb0899_config *config = state->config;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001534
1535 u32 SearchRange, gain;
1536
Manu Abraham54fb1222008-03-01 13:54:28 -03001537 i_params->freq = params->frequency;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001538 switch (state->delsys) {
1539 case DVBFE_DELSYS_DVBS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001540 dprintk(state->verbose, FE_ERROR, 1, "set DVB-S params");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001541 i_params->srate = params->delsys.dvbs.symbol_rate;
1542 break;
1543 case DVBFE_DELSYS_DSS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001544 dprintk(state->verbose, FE_ERROR, 1, "set DSS params");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001545 i_params->srate = params->delsys.dss.symbol_rate;
1546 break;
1547 case DVBFE_DELSYS_DVBS2:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001548 dprintk(state->verbose, FE_ERROR, 1, "set DVB-S2 params");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001549 i_params->srate = params->delsys.dvbs2.symbol_rate;
1550 break;
1551 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001552 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001553 return -EINVAL;
1554 }
Reinhard Nisslc615a272008-07-09 15:33:38 -03001555 dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001556
Manu Abraham763fbaf2008-01-18 11:28:48 -03001557 SearchRange = 10000000;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001558 dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001559 /* checking Search Range is meaningless for a fixed 3 Mhz */
1560 if (INRANGE(i_params->srate, 1000000, 45000000)) {
Reinhard Nisslc615a272008-07-09 15:33:38 -03001561 dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
Manu Abrahame399a782008-03-08 20:28:09 -03001562 stb0899_set_delivery(state);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001563
1564 if (state->config->tuner_set_rfsiggain) {
1565 if (internal->srate > 15000000)
1566 gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
1567 else if (internal->srate > 5000000)
1568 gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
1569 else
1570 gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
1571 state->config->tuner_set_rfsiggain(fe, gain);
1572 }
1573
1574 if (i_params->srate <= 5000000)
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001575 stb0899_set_mclk(state, config->lo_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001576 else
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001577 stb0899_set_mclk(state, config->hi_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001578
1579 switch (state->delsys) {
1580 case DVBFE_DELSYS_DVBS:
1581 case DVBFE_DELSYS_DSS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001582 dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001583 internal->freq = i_params->freq;
1584 internal->srate = i_params->srate;
1585 /*
1586 * search = user search range +
1587 * 500Khz +
1588 * 2 * Tuner_step_size +
1589 * 10% of the symbol rate
1590 */
1591 internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
1592 internal->derot_percent = 30;
1593
1594 /* What to do for tuners having no bandwidth setup ? */
Manu Abraham40e8ce32008-02-03 19:37:02 -03001595 /* enable tuner I/O */
1596 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1597
Manu Abraham8bd135b2007-07-03 09:53:42 -03001598 if (state->config->tuner_set_bandwidth)
Reinhard Nissl02ec9d82008-01-18 20:09:52 -03001599 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001600 if (state->config->tuner_get_bandwidth)
1601 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
Manu Abraham40e8ce32008-02-03 19:37:02 -03001602
1603 /* disable tuner I/O */
1604 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1605
Manu Abraham8bd135b2007-07-03 09:53:42 -03001606 /* Set DVB-S1 AGC */
1607 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1608
1609 /* Run the search algorithm */
Reinhard Nisslc615a272008-07-09 15:33:38 -03001610 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001611 if (stb0899_dvbs_algo(state) == RANGEOK) {
1612 internal->lock = 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001613 dprintk(state->verbose, FE_DEBUG, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001614 "-------------------------------------> DVB-S LOCK !");
1615
1616// stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
1617// internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1618// internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001619// dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1620// dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001621
1622 return DVBFE_ALGO_SEARCH_SUCCESS;
1623 } else {
1624 internal->lock = 0;
1625
1626 return DVBFE_ALGO_SEARCH_FAILED;
1627 }
1628 break;
1629 case DVBFE_DELSYS_DVBS2:
1630 internal->freq = i_params->freq;
1631 internal->srate = i_params->srate;
1632 internal->srch_range = SearchRange;
1633
Manu Abraham40e8ce32008-02-03 19:37:02 -03001634 /* enable tuner I/O */
1635 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1636
Manu Abraham8bd135b2007-07-03 09:53:42 -03001637 if (state->config->tuner_set_bandwidth)
Manu Abraham763fbaf2008-01-18 11:28:48 -03001638 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
Manu Abraham8bd135b2007-07-03 09:53:42 -03001639 if (state->config->tuner_get_bandwidth)
1640 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1641
Manu Abraham40e8ce32008-02-03 19:37:02 -03001642 /* disable tuner I/O */
1643 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1644
Manu Abraham8bd135b2007-07-03 09:53:42 -03001645// pParams->SpectralInv = pSearch->IQ_Inversion;
1646
1647 /* Set DVB-S2 AGC */
1648 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1649
1650 /* Set IterScale =f(MCLK,SYMB) */
1651 stb0899_set_iterations(state);
1652
1653 /* Run the search algorithm */
Reinhard Nisslc615a272008-07-09 15:33:38 -03001654 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001655 if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
1656 internal->lock = 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001657 dprintk(state->verbose, FE_DEBUG, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001658 "-------------------------------------> DVB-S2 LOCK !");
1659
1660// stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
1661// internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1662// internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1663
1664 return DVBFE_ALGO_SEARCH_SUCCESS;
1665 } else {
1666 internal->lock = 0;
1667
1668 return DVBFE_ALGO_SEARCH_FAILED;
1669 }
1670 break;
1671 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001672 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001673 return DVBFE_ALGO_SEARCH_INVALID;
1674 }
1675 }
1676
1677 return DVBFE_ALGO_SEARCH_ERROR;
1678}
1679
1680static enum stb0899_status stb0899_track_carrier(struct stb0899_state *state)
1681{
1682 u8 reg;
1683
1684 reg = stb0899_read_reg(state, STB0899_DSTATUS);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001685 dprintk(state->verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001686 if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
Reinhard Nisslc615a272008-07-09 15:33:38 -03001687 dprintk(state->verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001688 return CARRIEROK;
1689 } else {
Reinhard Nisslc615a272008-07-09 15:33:38 -03001690 dprintk(state->verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001691 return NOCARRIER;
1692 }
1693
1694 return NOCARRIER;
1695}
1696
1697static enum stb0899_status stb0899_get_ifagc(struct stb0899_state *state)
1698{
1699 u8 reg;
1700
1701 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001702 dprintk(state->verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001703 if (STB0899_GETFIELD(IF_AGC_LOCK, reg)) {
Reinhard Nisslc615a272008-07-09 15:33:38 -03001704 dprintk(state->verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001705 return AGC1OK;
1706 } else {
Reinhard Nisslc615a272008-07-09 15:33:38 -03001707 dprintk(state->verbose, FE_DEBUG, 1, "------------->IF AGC LOCK LOST !");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001708 return NOAGC1;
1709 }
1710
1711 return NOAGC1;
1712}
1713
1714static int stb0899_get_s1fec(struct stb0899_internal *internal, enum dvbfe_fec *fec)
1715{
1716 switch (internal->fecrate) {
1717 case STB0899_FEC_1_2:
1718 *fec = DVBFE_FEC_1_2;
1719 break;
1720 case STB0899_FEC_2_3:
1721 *fec = DVBFE_FEC_2_3;
1722 break;
1723 case STB0899_FEC_3_4:
1724 *fec = DVBFE_FEC_3_4;
1725 break;
1726 case STB0899_FEC_5_6:
1727 *fec = DVBFE_FEC_5_6;
1728 break;
1729 case STB0899_FEC_6_7:
1730 *fec = DVBFE_FEC_6_7;
1731 break;
1732 case STB0899_FEC_7_8:
1733 *fec = DVBFE_FEC_7_8;
1734 break;
1735 default:
1736 return -EINVAL;
1737 }
1738
1739 return 0;
1740}
1741
1742static int stb0899_get_modcod(struct stb0899_internal *internal, struct dvbs2_params *params)
1743{
1744 switch (internal->modcod) {
1745 case STB0899_DUMMY_PLF:
1746 params->modulation = DVBFE_MOD_NONE;
1747 params->fec = DVBFE_FEC_NONE;
1748 break;
1749 case STB0899_QPSK_14:
1750 params->modulation = DVBFE_MOD_QPSK;
1751 params->fec = DVBFE_FEC_1_4;
1752 break;
1753 case STB0899_QPSK_13:
1754 params->modulation = DVBFE_MOD_QPSK;
1755 params->fec = DVBFE_FEC_1_3;
1756 break;
1757 case STB0899_QPSK_25:
1758 params->modulation = DVBFE_MOD_QPSK;
1759 params->fec = DVBFE_FEC_2_5;
1760 break;
1761 case STB0899_QPSK_12:
1762 params->modulation = DVBFE_MOD_QPSK;
1763 params->fec = DVBFE_FEC_1_2;
1764 break;
1765 case STB0899_QPSK_35:
1766 params->modulation = DVBFE_MOD_QPSK;
1767 params->fec = DVBFE_FEC_3_5;
1768 break;
1769 case STB0899_QPSK_23:
1770 params->modulation = DVBFE_MOD_QPSK;
1771 params->fec = DVBFE_FEC_2_3;
1772 break;
1773 case STB0899_QPSK_34:
1774 params->modulation = DVBFE_MOD_QPSK;
1775 params->fec = DVBFE_FEC_3_4;
1776 break;
1777 case STB0899_QPSK_45:
1778 params->modulation = DVBFE_MOD_QPSK;
1779 params->fec = DVBFE_FEC_4_5;
1780 break;
1781 case STB0899_QPSK_56:
1782 params->modulation = DVBFE_MOD_QPSK;
1783 params->fec = DVBFE_FEC_5_6;
1784 break;
1785 case STB0899_QPSK_89:
1786 params->modulation = DVBFE_MOD_QPSK;
1787 params->fec = DVBFE_FEC_8_9;
1788 break;
1789 case STB0899_QPSK_910:
1790 params->modulation = DVBFE_MOD_QPSK;
1791 params->fec = DVBFE_FEC_9_10;
1792 break;
1793 case STB0899_8PSK_35:
1794 params->modulation = DVBFE_MOD_8PSK;
1795 params->fec = DVBFE_FEC_3_5;
1796 break;
1797 case STB0899_8PSK_23:
1798 params->modulation = DVBFE_MOD_8PSK;
1799 params->fec = DVBFE_FEC_2_3;
1800 break;
1801 case STB0899_8PSK_34:
1802 params->modulation = DVBFE_MOD_8PSK;
1803 params->fec = DVBFE_FEC_3_4;
1804 break;
1805 case STB0899_8PSK_56:
1806 params->modulation = DVBFE_MOD_8PSK;
1807 params->fec = DVBFE_FEC_5_6;
1808 break;
1809 case STB0899_8PSK_89:
1810 params->modulation = DVBFE_MOD_8PSK;
1811 params->fec = DVBFE_FEC_8_9;
1812 break;
1813 case STB0899_8PSK_910:
1814 params->modulation = DVBFE_MOD_8PSK;
1815 params->fec = DVBFE_FEC_9_10;
1816 break;
1817 case STB0899_16APSK_23:
1818 params->modulation = DVBFE_MOD_16APSK;
1819 params->fec = DVBFE_FEC_2_3;
1820 break;
1821 case STB0899_16APSK_34:
1822 params->modulation = DVBFE_MOD_16APSK;
1823 params->fec = DVBFE_FEC_3_4;
1824 break;
1825 case STB0899_16APSK_45:
1826 params->modulation = DVBFE_MOD_16APSK;
1827 params->fec = DVBFE_FEC_4_5;
1828 break;
1829 case STB0899_16APSK_56:
1830 params->modulation = DVBFE_MOD_16APSK;
1831 params->fec = DVBFE_FEC_5_6;
1832 break;
1833 case STB0899_16APSK_89:
1834 params->modulation = DVBFE_MOD_16APSK;
1835 params->fec = DVBFE_FEC_8_9;
1836 break;
1837 case STB0899_16APSK_910:
1838 params->modulation = DVBFE_MOD_16APSK;
1839 params->fec = DVBFE_FEC_9_10;
1840 break;
1841 case STB0899_32APSK_34:
1842 params->modulation = DVBFE_MOD_32APSK;
1843 params->fec = DVBFE_FEC_3_4;
1844 break;
1845 case STB0899_32APSK_45:
1846 params->modulation = DVBFE_MOD_32APSK;
1847 params->fec = DVBFE_FEC_4_5;
1848 break;
1849 case STB0899_32APSK_56:
1850 params->modulation = DVBFE_MOD_32APSK;
1851 params->fec = DVBFE_FEC_5_6;
1852 break;
1853 case STB0899_32APSK_89:
1854 params->modulation = DVBFE_MOD_32APSK;
1855 params->fec = DVBFE_FEC_8_9;
1856 break;
1857 case STB0899_32APSK_910:
1858 params->modulation = DVBFE_MOD_32APSK;
1859 params->fec = DVBFE_FEC_9_10;
1860 break;
1861 default:
1862 return -EINVAL;
1863 }
1864
1865 return 0;
1866}
1867
1868/*
1869 * stb0899_track
1870 * periodically check the signal level against a specified
1871 * threshold level and perform derotator centering.
1872 * called once we have a lock from a succesful search
1873 * event.
1874 *
1875 * Will be called periodically called to maintain the
1876 * lock.
1877 *
1878 * Will be used to get parameters as well as info from
1879 * the decoded baseband header
1880 *
1881 * Once a new lock has established, the internal state
1882 * frequency (internal->freq) is updated
1883 */
Manu Abraham795b72b2007-07-02 09:01:48 -03001884static int stb0899_track(struct dvb_frontend *fe, struct dvbfe_params *params, int *delay)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001885{
Manu Abraham795b72b2007-07-02 09:01:48 -03001886 u32 lock_lost;
1887
Manu Abraham8bd135b2007-07-03 09:53:42 -03001888 struct stb0899_state *state = fe->demodulator_priv;
1889 struct stb0899_internal *internal = &state->internal;
1890
Manu Abraham795b72b2007-07-02 09:01:48 -03001891
1892 *delay = HZ/10;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001893
1894 return 0;
1895}
1896
1897static int stb0899_get_params(struct dvb_frontend *fe, struct dvbfe_params *params)
1898{
1899 struct stb0899_state *state = fe->demodulator_priv;
1900 struct stb0899_internal *internal = &state->internal;
1901
1902 params->frequency = internal->freq;
1903 params->inversion = internal->inversion;
1904 params->delivery = state->delsys;
1905 switch (state->delsys) {
1906 case DVBFE_DELSYS_DVBS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001907 dprintk(state->verbose, FE_DEBUG, 1, "Get DVB-S params");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001908 params->delsys.dvbs.symbol_rate = internal->srate;
1909 params->delsys.dvbs.modulation = DVBFE_MOD_QPSK;
1910 break;
1911 case DVBFE_DELSYS_DSS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001912 dprintk(state->verbose, FE_DEBUG, 1, "Get DSS params");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001913 params->delsys.dss.symbol_rate = internal->srate;
1914 params->delsys.dss.modulation = DVBFE_MOD_QPSK;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001915 break;
1916 case DVBFE_DELSYS_DVBS2:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001917 dprintk(state->verbose, FE_DEBUG, 1, "Get DVB-S2 params");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001918 params->delsys.dvbs2.symbol_rate = internal->srate;
1919 break;
1920 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001921 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001922 return -EINVAL;
1923 }
1924
1925 return 0;
1926}
1927
1928static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1929{
1930 return DVBFE_ALGO_CUSTOM;
1931}
1932
Manu Abrahame399a782008-03-08 20:28:09 -03001933static int stb0899_set_delsys(struct dvb_frontend *fe, enum dvbfe_delsys delsys)
1934{
1935 struct stb0899_state *state = fe->demodulator_priv;
1936
1937 state->delsys = delsys;
1938 return 0;
1939}
1940
Manu Abraham8bd135b2007-07-03 09:53:42 -03001941static struct dvb_frontend_ops stb0899_ops = {
1942
1943 .info = {
1944 .name = "STB0899 Multistandard",
1945 },
1946
1947 .release = stb0899_release,
1948 .init = stb0899_init,
1949 .sleep = stb0899_sleep,
1950// .wakeup = stb0899_wakeup,
1951
1952 .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
1953 .get_info = stb0899_get_info,
1954 .get_delsys = stb0899_get_delsys,
Manu Abrahame399a782008-03-08 20:28:09 -03001955 .set_delsys = stb0899_set_delsys,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001956
1957 .get_frontend_algo = stb0899_frontend_algo,
1958 .search = stb0899_search,
1959 .track = stb0899_track,
1960 .get_params = stb0899_get_params,
1961
1962 .read_status = stb0899_read_status,
1963 .read_snr = stb0899_read_snr,
1964 .read_signal_strength = stb0899_read_signal_strength,
1965 .read_status = stb0899_read_status,
1966 .read_ber = stb0899_read_ber,
1967
1968 .set_voltage = stb0899_set_voltage,
1969 .set_tone = stb0899_set_tone,
1970
1971 .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
1972 .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
1973 .diseqc_send_burst = stb0899_send_diseqc_burst,
1974};
1975
1976struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1977{
1978 struct stb0899_state *state = NULL;
Manu Abrahamb48e2de2007-09-24 19:51:32 -03001979 enum stb0899_inversion inversion;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001980
1981 state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1982 if (state == NULL)
1983 goto error;
1984
Manu Abrahamb48e2de2007-09-24 19:51:32 -03001985 inversion = config->inversion;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001986 state->verbose = &verbose;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001987 state->config = config;
1988 state->i2c = i2c;
1989 state->frontend.ops = stb0899_ops;
1990 state->frontend.demodulator_priv = state;
Manu Abrahamb48e2de2007-09-24 19:51:32 -03001991 state->internal.inversion = inversion;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001992
1993 stb0899_wakeup(&state->frontend);
1994 if (stb0899_get_dev_id(state) == -ENODEV) {
1995 printk("%s: Exiting .. !\n", __func__);
1996 goto error;
1997 }
1998
1999 printk("%s: Attaching STB0899 \n", __func__);
2000 return &state->frontend;
2001
2002error:
2003 kfree(state);
2004 return NULL;
2005}
2006EXPORT_SYMBOL(stb0899_attach);
2007MODULE_PARM_DESC(verbose, "Set Verbosity level");
2008MODULE_AUTHOR("Manu Abraham");
2009MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
2010MODULE_LICENSE("GPL");