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