[ARM] pxa: update defconfig for Verdex Pro
[linux-2.6/verdex.git] / drivers / media / dvb / frontends / stb0899_drv.c
bloba04c782fff8d7466f118d04708cf6a9e0779d427
1 /*
2 STB0899 Multistandard Frontend driver
3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
5 Copyright (C) ST Microelectronics
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.
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.
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.
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/string.h>
27 #include <linux/dvb/frontend.h>
28 #include "dvb_frontend.h"
30 #include "stb0899_drv.h"
31 #include "stb0899_priv.h"
32 #include "stb0899_reg.h"
34 static unsigned int verbose = 0;//1;
35 module_param(verbose, int, 0644);
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 },
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.
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 }
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.
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 }
136 /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
137 static 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 }
179 /* DVB-S2 Es/N0 estimate in dB/100 vs read value */
180 static 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 },
220 static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
222 int ret;
224 u8 b0[] = { reg >> 8, reg & 0xff };
225 u8 buf;
227 struct i2c_msg msg[] = {
229 .addr = state->config->demod_address,
230 .flags = 0,
231 .buf = b0,
232 .len = 2
234 .addr = state->config->demod_address,
235 .flags = I2C_M_RD,
236 .buf = &buf,
237 .len = 1
241 ret = i2c_transfer(state->i2c, msg, 2);
242 if (ret != 2) {
243 if (ret != -ERESTARTSYS)
244 dprintk(state->verbose, FE_ERROR, 1,
245 "Read error, Reg=[0x%02x], Status=%d",
246 reg, ret);
248 return ret < 0 ? ret : -EREMOTEIO;
250 if (unlikely(*state->verbose >= FE_DEBUGREG))
251 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
252 reg, buf);
254 return (unsigned int)buf;
257 int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
259 int result;
261 result = _stb0899_read_reg(state, reg);
263 * Bug ID 9:
264 * access to 0xf2xx/0xf6xx
265 * must be followed by read from 0xf2ff/0xf6ff.
267 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
268 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
269 _stb0899_read_reg(state, (reg | 0x00ff));
271 return result;
274 u32 _stb0899_read_s2reg(struct stb0899_state *state,
275 u32 stb0899_i2cdev,
276 u32 stb0899_base_addr,
277 u16 stb0899_reg_offset)
279 int status;
280 u32 data;
281 u8 buf[7] = { 0 };
282 u16 tmpaddr;
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) */
292 u8 buf_1[] = {
293 0x00, /* 0xf3 Reg Offset */
294 0x00, /* 0x44 Reg Offset */
297 struct i2c_msg msg_0 = {
298 .addr = state->config->demod_address,
299 .flags = 0,
300 .buf = buf_0,
301 .len = 6
304 struct i2c_msg msg_1 = {
305 .addr = state->config->demod_address,
306 .flags = 0,
307 .buf = buf_1,
308 .len = 2
311 struct i2c_msg msg_r = {
312 .addr = state->config->demod_address,
313 .flags = I2C_M_RD,
314 .buf = buf,
315 .len = 4
318 tmpaddr = stb0899_reg_offset & 0xff00;
319 if (!(stb0899_reg_offset & 0x8))
320 tmpaddr = stb0899_reg_offset | 0x20;
322 buf_1[0] = GETBYTE(tmpaddr, BYTE1);
323 buf_1[1] = GETBYTE(tmpaddr, BYTE0);
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);
331 goto err;
334 /* Dummy */
335 status = i2c_transfer(state->i2c, &msg_1, 1);
336 if (status < 1)
337 goto err;
339 status = i2c_transfer(state->i2c, &msg_r, 1);
340 if (status < 1)
341 goto err;
343 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
344 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
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;
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;
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);
368 return data;
370 err:
371 return status < 0 ? status : -EREMOTEIO;
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)
380 int status;
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) */
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 */
400 struct i2c_msg msg_0 = {
401 .addr = state->config->demod_address,
402 .flags = 0,
403 .buf = buf_0,
404 .len = 6
407 struct i2c_msg msg_1 = {
408 .addr = state->config->demod_address,
409 .flags = 0,
410 .buf = buf_1,
411 .len = 6
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);
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);
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;
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);
438 return status < 0 ? status : -EREMOTEIO;
441 return 0;
443 err:
444 return status < 0 ? status : -EREMOTEIO;
447 int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
449 int status;
451 u8 b0[] = { reg >> 8, reg & 0xff };
453 struct i2c_msg msg[] = {
455 .addr = state->config->demod_address,
456 .flags = 0,
457 .buf = b0,
458 .len = 2
460 .addr = state->config->demod_address,
461 .flags = I2C_M_RD,
462 .buf = buf,
463 .len = count
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;
475 * Bug ID 9:
476 * access to 0xf2xx/0xf6xx
477 * must be followed by read from 0xf2ff/0xf6ff.
479 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
480 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
481 _stb0899_read_reg(state, (reg | 0x00ff));
483 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
484 int i;
486 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
487 for (i = 0; i < count; i++) {
488 printk(" %02x", buf[i]);
490 printk("\n");
493 return 0;
494 err:
495 return status < 0 ? status : -EREMOTEIO;
498 int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
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
509 buf[0] = reg >> 8;
510 buf[1] = reg & 0xff;
511 memcpy(&buf[2], data, count);
513 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
514 int i;
516 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
517 for (i = 0; i < count; i++)
518 printk(" %02x", data[i]);
519 printk("\n");
521 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
524 * Bug ID 9:
525 * access to 0xf2xx/0xf6xx
526 * must be followed by read from 0xf2ff/0xf6ff.
528 if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
529 stb0899_read_reg(state, (reg | 0x00ff));
531 if (ret != 1) {
532 if (ret != -ERESTARTSYS)
533 dprintk(state->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;
538 return 0;
541 int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
543 return stb0899_write_regs(state, reg, &data, 1);
547 * stb0899_get_mclk
548 * Get STB0899 master clock frequency
549 * ExtClk: external clock frequency (Hz)
551 static u32 stb0899_get_mclk(struct stb0899_state *state)
553 u32 mclk = 0, div = 0;
555 div = stb0899_read_reg(state, STB0899_NCOARSE);
556 mclk = (div + 1) * state->config->xtal_freq / 6;
557 dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
559 return mclk;
563 * stb0899_set_mclk
564 * Set STB0899 master Clock frequency
565 * Mclk: demodulator master clock
566 * ExtClk: external clock frequency (Hz)
568 static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
570 struct stb0899_internal *internal = &state->internal;
571 u8 mdiv = 0;
573 dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
574 mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
575 dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
577 stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
578 internal->master_clk = stb0899_get_mclk(state);
580 dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
583 static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
585 struct stb0899_config *config = state->config;
586 const struct stb0899_postproc *postproc = config->postproc;
588 /* post process event */
589 if (postproc) {
590 if (enable) {
591 if (postproc[ctl].level == STB0899_GPIOPULLUP)
592 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
593 else
594 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
595 } else {
596 if (postproc[ctl].level == STB0899_GPIOPULLUP)
597 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
598 else
599 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
602 return 0;
605 static void stb0899_release(struct dvb_frontend *fe)
607 struct stb0899_state *state = fe->demodulator_priv;
609 dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
610 /* post process event */
611 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
612 kfree(state);
616 * stb0899_get_alpha
617 * return: rolloff
619 static int stb0899_get_alpha(struct stb0899_state *state)
621 u8 mode_coeff;
623 mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
625 if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
626 return 20;
627 else
628 return 35;
632 * stb0899_init_calc
634 static void stb0899_init_calc(struct stb0899_state *state)
636 struct stb0899_internal *internal = &state->internal;
637 int master_clk;
638 u8 agc[2];
639 u8 agc1cn;
640 u32 reg;
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 */
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);
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);
661 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
662 internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
664 internal->center_freq = 0;
665 internal->av_frame_coarse = 10;
666 internal->av_frame_fine = 20;
667 internal->step_size = 2;
669 if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
670 pParams->IQLocked = 0;
671 else
672 pParams->IQLocked = 1;
676 static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
678 u8 reg = 0;
679 unsigned long start = jiffies;
681 while (1) {
682 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
683 if (!STB0899_GETFIELD(FIFOFULL, reg))
684 break;
685 if ((jiffies - start) > timeout) {
686 dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
687 return -ETIMEDOUT;
691 return 0;
694 static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
696 struct stb0899_state *state = fe->demodulator_priv;
697 u8 reg, i;
699 if (cmd->msg_len > 8)
700 return -EINVAL;
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;
711 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
713 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
714 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
715 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
717 return 0;
720 static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
722 u8 reg = 0;
723 unsigned long start = jiffies;
725 while (!STB0899_GETFIELD(RXEND, reg)) {
726 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
727 if (jiffies - start > timeout) {
728 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
729 return -ETIMEDOUT;
731 msleep(10);
734 return 0;
737 static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
739 struct stb0899_state *state = fe->demodulator_priv;
740 u8 reg, length = 0, i;
741 int result;
743 if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
744 return -ETIMEDOUT;
746 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
747 if (STB0899_GETFIELD(RXEND, reg)) {
749 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
750 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
752 if (length > sizeof (reply->msg)) {
753 result = -EOVERFLOW;
754 goto exit;
756 reply->msg_len = length;
758 /* extract data */
759 for (i = 0; i < length; i++)
760 reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
763 return 0;
764 exit:
766 return result;
769 static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
771 u8 reg = 0;
772 unsigned long start = jiffies;
774 while (!STB0899_GETFIELD(TXIDLE, reg)) {
775 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
776 if (jiffies - start > timeout) {
777 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
778 return -ETIMEDOUT;
780 msleep(10);
782 return 0;
785 static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
787 struct stb0899_state *state = fe->demodulator_priv;
788 u8 reg, old_state;
790 /* wait for diseqc idle */
791 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
792 return -ETIMEDOUT;
794 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
795 old_state = reg;
796 /* set to burst mode */
797 STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
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;
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;
817 /* restore state */
818 stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
820 return 0;
823 static int stb0899_diseqc_init(struct stb0899_state *state)
825 struct dvb_diseqc_master_cmd tx_data;
827 struct dvb_diseqc_slave_reply rx_data;
829 u8 f22_tx, f22_rx, reg;
831 u32 mclk, tx_freq = 22000;/* count = 0, i; */
832 tx_data.msg[0] = 0xe2;
833 tx_data.msg_len = 3;
834 reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
835 STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
836 stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
838 /* disable Tx spy */
839 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
840 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
841 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
843 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
844 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
845 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
847 mclk = stb0899_get_mclk(state);
848 f22_tx = mclk / (tx_freq * 32);
849 stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
850 state->rx_freq = 20000;
851 f22_rx = mclk / (state->rx_freq * 32);
853 return 0;
856 static int stb0899_sleep(struct dvb_frontend *fe)
858 struct stb0899_state *state = fe->demodulator_priv;
860 u8 reg;
862 dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
863 /* post process event */
864 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
866 return 0;
869 static int stb0899_wakeup(struct dvb_frontend *fe)
871 int rc;
872 struct stb0899_state *state = fe->demodulator_priv;
874 if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
875 return rc;
876 /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
877 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
878 return rc;
879 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
880 return rc;
882 /* post process event */
883 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
885 return 0;
888 static int stb0899_init(struct dvb_frontend *fe)
890 int i;
891 struct stb0899_state *state = fe->demodulator_priv;
892 struct stb0899_config *config = state->config;
894 dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
896 /* init device */
897 dprintk(state->verbose, FE_DEBUG, 1, "init device");
898 for (i = 0; config->init_dev[i].address != 0xffff; i++)
899 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
901 dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
902 /* init S2 demod */
903 for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
904 stb0899_write_s2reg(state, STB0899_S2DEMOD,
905 config->init_s2_demod[i].base_address,
906 config->init_s2_demod[i].offset,
907 config->init_s2_demod[i].data);
909 dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
910 /* init S1 demod */
911 for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
912 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
914 dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
915 /* init S2 fec */
916 for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
917 stb0899_write_s2reg(state, STB0899_S2FEC,
918 config->init_s2_fec[i].base_address,
919 config->init_s2_fec[i].offset,
920 config->init_s2_fec[i].data);
922 dprintk(state->verbose, FE_DEBUG, 1, "init TST");
923 /* init test */
924 for (i = 0; config->init_tst[i].address != 0xffff; i++)
925 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
927 stb0899_init_calc(state);
928 stb0899_diseqc_init(state);
930 return 0;
933 static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
935 int res = 0;
936 int min = 0, med;
938 if (val < tab[min].read)
939 res = tab[min].real;
940 else if (val >= tab[max].read)
941 res = tab[max].real;
942 else {
943 while ((max - min) > 1) {
944 med = (max + min) / 2;
945 if (val >= tab[min].read && val < tab[med].read)
946 max = med;
947 else
948 min = med;
950 res = ((val - tab[min].read) *
951 (tab[max].real - tab[min].real) /
952 (tab[max].read - tab[min].read)) +
953 tab[min].real;
956 return res;
959 static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
961 struct stb0899_state *state = fe->demodulator_priv;
962 struct stb0899_internal *internal = &state->internal;
964 int val;
965 u32 reg;
966 switch (state->delsys) {
967 case SYS_DVBS:
968 case SYS_DSS:
969 if (internal->lock) {
970 reg = stb0899_read_reg(state, STB0899_VSTATUS);
971 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
973 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
974 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
976 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
977 *strength += 750;
978 dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
979 val & 0xff, *strength);
982 break;
983 case SYS_DVBS2:
984 if (internal->lock) {
985 reg = STB0899_READ_S2REG(STB0899_DEMOD, IF_AGC_GAIN);
986 val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
988 *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
989 *strength += 750;
990 dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
991 val & 0x3fff, *strength);
993 break;
994 default:
995 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
996 return -EINVAL;
999 return 0;
1002 static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
1004 struct stb0899_state *state = fe->demodulator_priv;
1005 struct stb0899_internal *internal = &state->internal;
1007 unsigned int val, quant, quantn = -1, est, estn = -1;
1008 u8 buf[2];
1009 u32 reg;
1011 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1012 switch (state->delsys) {
1013 case SYS_DVBS:
1014 case SYS_DSS:
1015 if (internal->lock) {
1016 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1018 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1019 val = MAKEWORD16(buf[0], buf[1]);
1021 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
1022 dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
1023 buf[0], buf[1], val, *snr);
1026 break;
1027 case SYS_DVBS2:
1028 if (internal->lock) {
1029 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1030 quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1031 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1032 est = STB0899_GETFIELD(ESN0_EST, reg);
1033 if (est == 1)
1034 val = 301; /* C/N = 30.1 dB */
1035 else if (est == 2)
1036 val = 270; /* C/N = 27.0 dB */
1037 else {
1038 /* quantn = 100 * log(quant^2) */
1039 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1040 /* estn = 100 * log(est) */
1041 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1042 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1043 val = (quantn - estn) / 10;
1045 *snr = val;
1046 dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
1047 quant, quantn, est, estn, val);
1049 break;
1050 default:
1051 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1052 return -EINVAL;
1055 return 0;
1058 static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1060 struct stb0899_state *state = fe->demodulator_priv;
1061 struct stb0899_internal *internal = &state->internal;
1062 u8 reg;
1063 *status = 0;
1065 switch (state->delsys) {
1066 case SYS_DVBS:
1067 case SYS_DSS:
1068 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1069 if (internal->lock) {
1070 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1071 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1072 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1073 *status |= FE_HAS_CARRIER | FE_HAS_LOCK;
1075 reg = stb0899_read_reg(state, STB0899_PLPARM);
1076 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1077 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1078 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
1079 /* post process event */
1080 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1084 break;
1085 case SYS_DVBS2:
1086 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1087 if (internal->lock) {
1088 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1089 if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1090 *status |= FE_HAS_CARRIER;
1091 dprintk(state->verbose, FE_DEBUG, 1,
1092 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1094 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1095 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1096 *status |= FE_HAS_LOCK;
1097 dprintk(state->verbose, FE_DEBUG, 1,
1098 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1101 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1102 *status |= FE_HAS_VITERBI;
1103 dprintk(state->verbose, FE_DEBUG, 1,
1104 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1106 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1107 *status |= FE_HAS_SYNC;
1108 dprintk(state->verbose, FE_DEBUG, 1,
1109 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1110 /* post process event */
1111 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1115 break;
1116 default:
1117 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1118 return -EINVAL;
1120 return 0;
1124 * stb0899_get_error
1125 * viterbi error for DVB-S/DSS
1126 * packet error for DVB-S2
1127 * Bit Error Rate or Packet Error Rate * 10 ^ 7
1129 static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1131 struct stb0899_state *state = fe->demodulator_priv;
1132 struct stb0899_internal *internal = &state->internal;
1134 u8 lsb, msb;
1135 u32 i;
1137 *ber = 0;
1139 switch (state->delsys) {
1140 case SYS_DVBS:
1141 case SYS_DSS:
1142 if (internal->lock) {
1143 /* average 5 BER values */
1144 for (i = 0; i < 5; i++) {
1145 msleep(100);
1146 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1147 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1148 *ber += MAKEWORD16(msb, lsb);
1150 *ber /= 5;
1151 /* Viterbi Check */
1152 if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1153 /* Error Rate */
1154 *ber *= 9766;
1155 /* ber = ber * 10 ^ 7 */
1156 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1157 *ber /= 8;
1160 break;
1161 case SYS_DVBS2:
1162 if (internal->lock) {
1163 /* Average 5 PER values */
1164 for (i = 0; i < 5; i++) {
1165 msleep(100);
1166 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1167 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1168 *ber += MAKEWORD16(msb, lsb);
1170 /* ber = ber * 10 ^ 7 */
1171 *ber *= 10000000;
1172 *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1174 break;
1175 default:
1176 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1177 return -EINVAL;
1180 return 0;
1183 static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
1185 struct stb0899_state *state = fe->demodulator_priv;
1187 switch (voltage) {
1188 case SEC_VOLTAGE_13:
1189 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1190 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1191 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1192 break;
1193 case SEC_VOLTAGE_18:
1194 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1195 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1196 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1197 break;
1198 case SEC_VOLTAGE_OFF:
1199 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1200 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1201 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1202 break;
1203 default:
1204 return -EINVAL;
1207 return 0;
1210 static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
1212 struct stb0899_state *state = fe->demodulator_priv;
1213 struct stb0899_internal *internal = &state->internal;
1215 u8 div, reg;
1217 /* wait for diseqc idle */
1218 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1219 return -ETIMEDOUT;
1221 switch (tone) {
1222 case SEC_TONE_ON:
1223 div = (internal->master_clk / 100) / 5632;
1224 div = (div + 5) / 10;
1225 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
1226 reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1227 STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1228 stb0899_write_reg(state, STB0899_ACRPRESC, reg);
1229 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1230 break;
1231 case SEC_TONE_OFF:
1232 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1233 break;
1234 default:
1235 return -EINVAL;
1237 return 0;
1240 int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1242 int i2c_stat;
1243 struct stb0899_state *state = fe->demodulator_priv;
1245 i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1246 if (i2c_stat < 0)
1247 goto err;
1249 if (enable) {
1250 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1251 i2c_stat |= STB0899_I2CTON;
1252 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1253 goto err;
1254 } else {
1255 dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
1256 i2c_stat &= ~STB0899_I2CTON;
1257 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1258 goto err;
1260 return 0;
1261 err:
1262 dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
1263 return -EREMOTEIO;
1267 static inline void CONVERT32(u32 x, char *str)
1269 *str++ = (x >> 24) & 0xff;
1270 *str++ = (x >> 16) & 0xff;
1271 *str++ = (x >> 8) & 0xff;
1272 *str++ = (x >> 0) & 0xff;
1273 *str = '\0';
1276 int stb0899_get_dev_id(struct stb0899_state *state)
1278 u8 chip_id, release;
1279 u16 id;
1280 u32 demod_ver = 0, fec_ver = 0;
1281 char demod_str[5] = { 0 };
1282 char fec_str[5] = { 0 };
1284 id = stb0899_read_reg(state, STB0899_DEV_ID);
1285 dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1286 chip_id = STB0899_GETFIELD(CHIP_ID, id);
1287 release = STB0899_GETFIELD(CHIP_REL, id);
1289 dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1290 chip_id, release);
1292 CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1294 demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1295 dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1296 CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1297 fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1298 if (! (chip_id > 0)) {
1299 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1301 return -ENODEV;
1303 dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1305 return 0;
1308 static void stb0899_set_delivery(struct stb0899_state *state)
1310 u8 reg;
1311 u8 stop_clk[2];
1313 stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1314 stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1316 switch (state->delsys) {
1317 case SYS_DVBS:
1318 dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
1319 /* FECM/Viterbi ON */
1320 reg = stb0899_read_reg(state, STB0899_FECM);
1321 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1322 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1323 stb0899_write_reg(state, STB0899_FECM, reg);
1325 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1326 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1327 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1328 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1330 reg = stb0899_read_reg(state, STB0899_TSTRES);
1331 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1332 stb0899_write_reg(state, STB0899_TSTRES, reg);
1334 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1335 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1336 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1338 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1339 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1341 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
1342 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1344 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1345 break;
1346 case SYS_DVBS2:
1347 /* FECM/Viterbi OFF */
1348 reg = stb0899_read_reg(state, STB0899_FECM);
1349 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1350 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1351 stb0899_write_reg(state, STB0899_FECM, reg);
1353 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1354 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1355 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1356 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1358 reg = stb0899_read_reg(state, STB0899_TSTRES);
1359 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1360 stb0899_write_reg(state, STB0899_TSTRES, reg);
1362 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1363 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1364 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1366 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1367 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1369 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1370 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1372 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1373 break;
1374 case SYS_DSS:
1375 /* FECM/Viterbi ON */
1376 reg = stb0899_read_reg(state, STB0899_FECM);
1377 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1378 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1379 stb0899_write_reg(state, STB0899_FECM, reg);
1381 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1382 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1383 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1385 reg = stb0899_read_reg(state, STB0899_TSTRES);
1386 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1387 stb0899_write_reg(state, STB0899_TSTRES, reg);
1389 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1390 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1391 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1393 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1394 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1396 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1398 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1399 break;
1400 default:
1401 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1402 break;
1404 STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1405 stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1409 * stb0899_set_iterations
1410 * set the LDPC iteration scale function
1412 static void stb0899_set_iterations(struct stb0899_state *state)
1414 struct stb0899_internal *internal = &state->internal;
1415 struct stb0899_config *config = state->config;
1417 s32 iter_scale;
1418 u32 reg;
1420 iter_scale = 17 * (internal->master_clk / 1000);
1421 iter_scale += 410000;
1422 iter_scale /= (internal->srate / 1000000);
1423 iter_scale /= 1000;
1425 if (iter_scale > config->ldpc_max_iter)
1426 iter_scale = config->ldpc_max_iter;
1428 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, MAX_ITER);
1429 STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1430 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1433 static enum dvbfe_search stb0899_search(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
1435 struct stb0899_state *state = fe->demodulator_priv;
1436 struct stb0899_params *i_params = &state->params;
1437 struct stb0899_internal *internal = &state->internal;
1438 struct stb0899_config *config = state->config;
1439 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
1441 u32 SearchRange, gain;
1443 i_params->freq = p->frequency;
1444 i_params->srate = p->u.qpsk.symbol_rate;
1445 state->delsys = props->delivery_system;
1446 dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1448 SearchRange = 10000000;
1449 dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
1450 /* checking Search Range is meaningless for a fixed 3 Mhz */
1451 if (INRANGE(i_params->srate, 1000000, 45000000)) {
1452 dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
1453 stb0899_set_delivery(state);
1455 if (state->config->tuner_set_rfsiggain) {
1456 if (internal->srate > 15000000)
1457 gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
1458 else if (internal->srate > 5000000)
1459 gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
1460 else
1461 gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
1462 state->config->tuner_set_rfsiggain(fe, gain);
1465 if (i_params->srate <= 5000000)
1466 stb0899_set_mclk(state, config->lo_clk);
1467 else
1468 stb0899_set_mclk(state, config->hi_clk);
1470 switch (state->delsys) {
1471 case SYS_DVBS:
1472 case SYS_DSS:
1473 dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
1474 internal->freq = i_params->freq;
1475 internal->srate = i_params->srate;
1477 * search = user search range +
1478 * 500Khz +
1479 * 2 * Tuner_step_size +
1480 * 10% of the symbol rate
1482 internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
1483 internal->derot_percent = 30;
1485 /* What to do for tuners having no bandwidth setup ? */
1486 /* enable tuner I/O */
1487 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1489 if (state->config->tuner_set_bandwidth)
1490 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
1491 if (state->config->tuner_get_bandwidth)
1492 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1494 /* disable tuner I/O */
1495 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1497 /* Set DVB-S1 AGC */
1498 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1500 /* Run the search algorithm */
1501 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
1502 if (stb0899_dvbs_algo(state) == RANGEOK) {
1503 internal->lock = 1;
1504 dprintk(state->verbose, FE_DEBUG, 1,
1505 "-------------------------------------> DVB-S LOCK !");
1507 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
1508 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1509 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1510 // dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1511 // dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
1513 return DVBFE_ALGO_SEARCH_SUCCESS;
1514 } else {
1515 internal->lock = 0;
1517 return DVBFE_ALGO_SEARCH_FAILED;
1519 break;
1520 case SYS_DVBS2:
1521 internal->freq = i_params->freq;
1522 internal->srate = i_params->srate;
1523 internal->srch_range = SearchRange;
1525 /* enable tuner I/O */
1526 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1528 if (state->config->tuner_set_bandwidth)
1529 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
1530 if (state->config->tuner_get_bandwidth)
1531 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1533 /* disable tuner I/O */
1534 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1536 // pParams->SpectralInv = pSearch->IQ_Inversion;
1538 /* Set DVB-S2 AGC */
1539 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1541 /* Set IterScale =f(MCLK,SYMB) */
1542 stb0899_set_iterations(state);
1544 /* Run the search algorithm */
1545 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
1546 if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
1547 internal->lock = 1;
1548 dprintk(state->verbose, FE_DEBUG, 1,
1549 "-------------------------------------> DVB-S2 LOCK !");
1551 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
1552 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1553 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1555 return DVBFE_ALGO_SEARCH_SUCCESS;
1556 } else {
1557 internal->lock = 0;
1559 return DVBFE_ALGO_SEARCH_FAILED;
1561 break;
1562 default:
1563 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1564 return DVBFE_ALGO_SEARCH_INVALID;
1568 return DVBFE_ALGO_SEARCH_ERROR;
1571 * stb0899_track
1572 * periodically check the signal level against a specified
1573 * threshold level and perform derotator centering.
1574 * called once we have a lock from a succesful search
1575 * event.
1577 * Will be called periodically called to maintain the
1578 * lock.
1580 * Will be used to get parameters as well as info from
1581 * the decoded baseband header
1583 * Once a new lock has established, the internal state
1584 * frequency (internal->freq) is updated
1586 static int stb0899_track(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
1588 return 0;
1591 static int stb0899_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
1593 struct stb0899_state *state = fe->demodulator_priv;
1594 struct stb0899_internal *internal = &state->internal;
1596 dprintk(state->verbose, FE_DEBUG, 1, "Get params");
1597 p->u.qpsk.symbol_rate = internal->srate;
1599 return 0;
1602 static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1604 return DVBFE_ALGO_CUSTOM;
1607 static struct dvb_frontend_ops stb0899_ops = {
1609 .info = {
1610 .name = "STB0899 Multistandard",
1611 .type = FE_QPSK,
1612 .frequency_min = 950000,
1613 .frequency_max = 2150000,
1614 .frequency_stepsize = 0,
1615 .frequency_tolerance = 0,
1616 .symbol_rate_min = 5000000,
1617 .symbol_rate_max = 45000000,
1619 .caps = FE_CAN_INVERSION_AUTO |
1620 FE_CAN_FEC_AUTO |
1621 FE_CAN_2G_MODULATION |
1622 FE_CAN_QPSK
1625 .release = stb0899_release,
1626 .init = stb0899_init,
1627 .sleep = stb0899_sleep,
1628 // .wakeup = stb0899_wakeup,
1630 .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
1632 .get_frontend_algo = stb0899_frontend_algo,
1633 .search = stb0899_search,
1634 .track = stb0899_track,
1635 .get_frontend = stb0899_get_frontend,
1638 .read_status = stb0899_read_status,
1639 .read_snr = stb0899_read_snr,
1640 .read_signal_strength = stb0899_read_signal_strength,
1641 .read_ber = stb0899_read_ber,
1643 .set_voltage = stb0899_set_voltage,
1644 .set_tone = stb0899_set_tone,
1646 .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
1647 .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
1648 .diseqc_send_burst = stb0899_send_diseqc_burst,
1651 struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1653 struct stb0899_state *state = NULL;
1654 enum stb0899_inversion inversion;
1656 state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1657 if (state == NULL)
1658 goto error;
1660 inversion = config->inversion;
1661 state->verbose = &verbose;
1662 state->config = config;
1663 state->i2c = i2c;
1664 state->frontend.ops = stb0899_ops;
1665 state->frontend.demodulator_priv = state;
1666 state->internal.inversion = inversion;
1668 stb0899_wakeup(&state->frontend);
1669 if (stb0899_get_dev_id(state) == -ENODEV) {
1670 printk("%s: Exiting .. !\n", __func__);
1671 goto error;
1674 printk("%s: Attaching STB0899 \n", __func__);
1675 return &state->frontend;
1677 error:
1678 kfree(state);
1679 return NULL;
1681 EXPORT_SYMBOL(stb0899_attach);
1682 MODULE_PARM_DESC(verbose, "Set Verbosity level");
1683 MODULE_AUTHOR("Manu Abraham");
1684 MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1685 MODULE_LICENSE("GPL");