blob: 82391feb683997f6eaecb87ed978b6f8fe3b1f80 [file] [log] [blame]
Manu Abraham8bd135b2007-07-03 09:53:42 -03001/*
2 STB0899 Multistandard Frontend driver
3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
4
5 Copyright (C) ST Microelectronics
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Manu Abraham8bd135b2007-07-03 09:53:42 -030026#include <linux/string.h>
27
28#include <linux/dvb/frontend.h>
29#include "dvb_frontend.h"
30
31#include "stb0899_drv.h"
32#include "stb0899_priv.h"
33#include "stb0899_reg.h"
34
Reinhard Nisslc615a272008-07-09 15:33:38 -030035static unsigned int verbose = 0;//1;
Manu Abraham8bd135b2007-07-03 09:53:42 -030036module_param(verbose, int, 0644);
37
38/* C/N in dB/10, NIRM/NIRL */
39static const struct stb0899_tab stb0899_cn_tab[] = {
40 { 200, 2600 },
41 { 190, 2700 },
42 { 180, 2860 },
43 { 170, 3020 },
44 { 160, 3210 },
45 { 150, 3440 },
46 { 140, 3710 },
47 { 130, 4010 },
48 { 120, 4360 },
49 { 110, 4740 },
50 { 100, 5190 },
51 { 90, 5670 },
52 { 80, 6200 },
53 { 70, 6770 },
54 { 60, 7360 },
55 { 50, 7970 },
56 { 40, 8250 },
57 { 30, 9000 },
58 { 20, 9450 },
59 { 15, 9600 },
60};
61
62/* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
63 * As measured, connected to a modulator.
64 * -8.0 to -50.0 dBm directly connected,
65 * -52.0 to -74.8 with extra attenuation.
66 * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
67 * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
68 */
69static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
Klaus Schmidinger0e377812012-01-24 05:33:53 -030070 { -750, -128 },
Manu Abraham8bd135b2007-07-03 09:53:42 -030071 { -748, -94 },
72 { -745, -92 },
73 { -735, -90 },
74 { -720, -87 },
75 { -670, -77 },
76 { -640, -70 },
77 { -610, -62 },
78 { -600, -60 },
79 { -590, -56 },
80 { -560, -41 },
81 { -540, -25 },
82 { -530, -17 },
83 { -520, -11 },
84 { -500, 1 },
85 { -490, 6 },
86 { -480, 10 },
87 { -440, 22 },
88 { -420, 27 },
89 { -400, 31 },
90 { -380, 34 },
91 { -340, 40 },
92 { -320, 43 },
93 { -280, 48 },
94 { -250, 52 },
95 { -230, 55 },
96 { -180, 61 },
97 { -140, 66 },
98 { -90, 73 },
99 { -80, 74 },
100 { 500, 127 }
101};
102
103/* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
104 * As measured, connected to a modulator.
105 * -8.0 to -50.1 dBm directly connected,
106 * -53.0 to -76.6 with extra attenuation.
107 * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
108 * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
109 */
110static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
111 { 700, 0 },
112 { -80, 3217 },
113 { -150, 3893 },
114 { -190, 4217 },
115 { -240, 4621 },
116 { -280, 4945 },
117 { -320, 5273 },
118 { -350, 5545 },
119 { -370, 5741 },
120 { -410, 6147 },
121 { -450, 6671 },
122 { -490, 7413 },
123 { -501, 7665 },
124 { -530, 8767 },
125 { -560, 10219 },
126 { -580, 10939 },
127 { -590, 11518 },
128 { -600, 11723 },
129 { -650, 12659 },
130 { -690, 13219 },
131 { -730, 13645 },
132 { -750, 13909 },
133 { -766, 14153 },
Klaus Schmidinger0e377812012-01-24 05:33:53 -0300134 { -950, 16383 }
Manu Abraham8bd135b2007-07-03 09:53:42 -0300135};
136
137/* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300138static struct stb0899_tab stb0899_quant_tab[] = {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300139 { 0, 0 },
140 { 0, 100 },
141 { 600, 200 },
142 { 950, 299 },
143 { 1200, 398 },
144 { 1400, 501 },
145 { 1560, 603 },
146 { 1690, 700 },
147 { 1810, 804 },
148 { 1910, 902 },
149 { 2000, 1000 },
150 { 2080, 1096 },
151 { 2160, 1202 },
152 { 2230, 1303 },
153 { 2350, 1496 },
154 { 2410, 1603 },
155 { 2460, 1698 },
156 { 2510, 1799 },
157 { 2600, 1995 },
158 { 2650, 2113 },
159 { 2690, 2213 },
160 { 2720, 2291 },
161 { 2760, 2399 },
162 { 2800, 2512 },
163 { 2860, 2692 },
164 { 2930, 2917 },
165 { 2960, 3020 },
166 { 3010, 3199 },
167 { 3040, 3311 },
168 { 3060, 3388 },
169 { 3120, 3631 },
170 { 3190, 3936 },
171 { 3400, 5012 },
172 { 3610, 6383 },
173 { 3800, 7943 },
174 { 4210, 12735 },
175 { 4500, 17783 },
176 { 4690, 22131 },
177 { 4810, 25410 }
178};
179
180/* DVB-S2 Es/N0 estimate in dB/100 vs read value */
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300181static struct stb0899_tab stb0899_est_tab[] = {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300182 { 0, 0 },
183 { 0, 1 },
184 { 301, 2 },
185 { 1204, 16 },
186 { 1806, 64 },
187 { 2408, 256 },
188 { 2709, 512 },
189 { 3010, 1023 },
190 { 3311, 2046 },
191 { 3612, 4093 },
192 { 3823, 6653 },
193 { 3913, 8185 },
194 { 4010, 10233 },
195 { 4107, 12794 },
196 { 4214, 16368 },
197 { 4266, 18450 },
198 { 4311, 20464 },
199 { 4353, 22542 },
200 { 4391, 24604 },
201 { 4425, 26607 },
202 { 4457, 28642 },
203 { 4487, 30690 },
204 { 4515, 32734 },
205 { 4612, 40926 },
206 { 4692, 49204 },
207 { 4816, 65464 },
208 { 4913, 81846 },
209 { 4993, 98401 },
210 { 5060, 114815 },
211 { 5118, 131220 },
212 { 5200, 158489 },
213 { 5300, 199526 },
214 { 5400, 251189 },
215 { 5500, 316228 },
216 { 5600, 398107 },
217 { 5720, 524807 },
218 { 5721, 526017 },
219};
220
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300221static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300222{
223 int ret;
224
225 u8 b0[] = { reg >> 8, reg & 0xff };
226 u8 buf;
227
228 struct i2c_msg msg[] = {
229 {
230 .addr = state->config->demod_address,
231 .flags = 0,
232 .buf = b0,
233 .len = 2
234 },{
235 .addr = state->config->demod_address,
236 .flags = I2C_M_RD,
237 .buf = &buf,
238 .len = 1
239 }
240 };
241
242 ret = i2c_transfer(state->i2c, msg, 2);
243 if (ret != 2) {
244 if (ret != -ERESTARTSYS)
Reinhard Nisslc615a272008-07-09 15:33:38 -0300245 dprintk(state->verbose, FE_ERROR, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -0300246 "Read error, Reg=[0x%02x], Status=%d",
247 reg, ret);
248
249 return ret < 0 ? ret : -EREMOTEIO;
250 }
Reinhard Nisslc615a272008-07-09 15:33:38 -0300251 if (unlikely(*state->verbose >= FE_DEBUGREG))
252 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300253 reg, buf);
254
Manu Abraham8bd135b2007-07-03 09:53:42 -0300255 return (unsigned int)buf;
256}
257
258int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
259{
260 int result;
261
262 result = _stb0899_read_reg(state, reg);
263 /*
264 * Bug ID 9:
265 * access to 0xf2xx/0xf6xx
266 * must be followed by read from 0xf2ff/0xf6ff.
267 */
268 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
269 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
270 _stb0899_read_reg(state, (reg | 0x00ff));
271
272 return result;
273}
274
275u32 _stb0899_read_s2reg(struct stb0899_state *state,
Igor M. Liplianin886134e2008-10-30 22:04:56 -0300276 u32 stb0899_i2cdev,
277 u32 stb0899_base_addr,
278 u16 stb0899_reg_offset)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300279{
280 int status;
281 u32 data;
282 u8 buf[7] = { 0 };
283 u16 tmpaddr;
284
285 u8 buf_0[] = {
286 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
287 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
288 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
289 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
290 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
291 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
292 };
293 u8 buf_1[] = {
294 0x00, /* 0xf3 Reg Offset */
295 0x00, /* 0x44 Reg Offset */
296 };
297
298 struct i2c_msg msg_0 = {
299 .addr = state->config->demod_address,
300 .flags = 0,
301 .buf = buf_0,
302 .len = 6
303 };
304
305 struct i2c_msg msg_1 = {
306 .addr = state->config->demod_address,
307 .flags = 0,
308 .buf = buf_1,
309 .len = 2
310 };
311
312 struct i2c_msg msg_r = {
313 .addr = state->config->demod_address,
314 .flags = I2C_M_RD,
315 .buf = buf,
316 .len = 4
317 };
318
319 tmpaddr = stb0899_reg_offset & 0xff00;
320 if (!(stb0899_reg_offset & 0x8))
321 tmpaddr = stb0899_reg_offset | 0x20;
322
323 buf_1[0] = GETBYTE(tmpaddr, BYTE1);
324 buf_1[1] = GETBYTE(tmpaddr, BYTE0);
325
326 status = i2c_transfer(state->i2c, &msg_0, 1);
327 if (status < 1) {
328 if (status != -ERESTARTSYS)
329 printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
330 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
331
332 goto err;
333 }
334
335 /* Dummy */
336 status = i2c_transfer(state->i2c, &msg_1, 1);
337 if (status < 1)
338 goto err;
339
340 status = i2c_transfer(state->i2c, &msg_r, 1);
341 if (status < 1)
342 goto err;
343
344 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
345 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
346
347 /* Actual */
348 status = i2c_transfer(state->i2c, &msg_1, 1);
349 if (status < 1) {
350 if (status != -ERESTARTSYS)
351 printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
352 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
353 goto err;
354 }
355
356 status = i2c_transfer(state->i2c, &msg_r, 1);
357 if (status < 1) {
358 if (status != -ERESTARTSYS)
359 printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
360 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
361 return status < 0 ? status : -EREMOTEIO;
362 }
363
364 data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
Reinhard Nisslc615a272008-07-09 15:33:38 -0300365 if (unlikely(*state->verbose >= FE_DEBUGREG))
Manu Abraham8bd135b2007-07-03 09:53:42 -0300366 printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
367 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
368
369 return data;
370
371err:
372 return status < 0 ? status : -EREMOTEIO;
373}
374
375int stb0899_write_s2reg(struct stb0899_state *state,
376 u32 stb0899_i2cdev,
377 u32 stb0899_base_addr,
378 u16 stb0899_reg_offset,
379 u32 stb0899_data)
380{
381 int status;
382
383 /* Base Address Setup */
384 u8 buf_0[] = {
385 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
386 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
387 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
388 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
389 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
390 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
391 };
392 u8 buf_1[] = {
393 0x00, /* 0xf3 Reg Offset */
394 0x00, /* 0x44 Reg Offset */
395 0x00, /* data */
396 0x00, /* data */
397 0x00, /* data */
398 0x00, /* data */
399 };
400
401 struct i2c_msg msg_0 = {
402 .addr = state->config->demod_address,
403 .flags = 0,
404 .buf = buf_0,
405 .len = 6
406 };
407
408 struct i2c_msg msg_1 = {
409 .addr = state->config->demod_address,
410 .flags = 0,
411 .buf = buf_1,
412 .len = 6
413 };
414
415 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
416 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
417 buf_1[2] = GETBYTE(stb0899_data, BYTE0);
418 buf_1[3] = GETBYTE(stb0899_data, BYTE1);
419 buf_1[4] = GETBYTE(stb0899_data, BYTE2);
420 buf_1[5] = GETBYTE(stb0899_data, BYTE3);
421
Reinhard Nisslc615a272008-07-09 15:33:38 -0300422 if (unlikely(*state->verbose >= FE_DEBUGREG))
Manu Abraham8bd135b2007-07-03 09:53:42 -0300423 printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
424 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
425
426 status = i2c_transfer(state->i2c, &msg_0, 1);
427 if (unlikely(status < 1)) {
428 if (status != -ERESTARTSYS)
429 printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
430 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
431 goto err;
432 }
433 status = i2c_transfer(state->i2c, &msg_1, 1);
434 if (unlikely(status < 1)) {
435 if (status != -ERESTARTSYS)
436 printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
437 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
438
439 return status < 0 ? status : -EREMOTEIO;
440 }
441
442 return 0;
443
444err:
445 return status < 0 ? status : -EREMOTEIO;
446}
447
Manu Abraham3d6a3beb2008-01-25 20:39:16 -0300448int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300449{
450 int status;
451
452 u8 b0[] = { reg >> 8, reg & 0xff };
453
454 struct i2c_msg msg[] = {
455 {
456 .addr = state->config->demod_address,
457 .flags = 0,
458 .buf = b0,
459 .len = 2
460 },{
461 .addr = state->config->demod_address,
462 .flags = I2C_M_RD,
463 .buf = buf,
464 .len = count
465 }
466 };
467
468 status = i2c_transfer(state->i2c, msg, 2);
469 if (status != 2) {
470 if (status != -ERESTARTSYS)
471 printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
472 __func__, reg, count, status);
473 goto err;
474 }
475 /*
476 * Bug ID 9:
477 * access to 0xf2xx/0xf6xx
478 * must be followed by read from 0xf2ff/0xf6ff.
479 */
480 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
481 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
482 _stb0899_read_reg(state, (reg | 0x00ff));
483
Reinhard Nisslc615a272008-07-09 15:33:38 -0300484 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300485 int i;
486
487 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
488 for (i = 0; i < count; i++) {
489 printk(" %02x", buf[i]);
490 }
491 printk("\n");
492 }
493
494 return 0;
495err:
496 return status < 0 ? status : -EREMOTEIO;
497}
498
Manu Abraham85eabac2008-01-25 20:28:46 -0300499int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300500{
501 int ret;
502 u8 buf[2 + count];
503 struct i2c_msg i2c_msg = {
504 .addr = state->config->demod_address,
505 .flags = 0,
506 .buf = buf,
507 .len = 2 + count
508 };
509
510 buf[0] = reg >> 8;
511 buf[1] = reg & 0xff;
512 memcpy(&buf[2], data, count);
513
Reinhard Nisslc615a272008-07-09 15:33:38 -0300514 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300515 int i;
516
517 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
518 for (i = 0; i < count; i++)
519 printk(" %02x", data[i]);
520 printk("\n");
521 }
522 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
523
524 /*
525 * Bug ID 9:
526 * access to 0xf2xx/0xf6xx
527 * must be followed by read from 0xf2ff/0xf6ff.
528 */
529 if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
530 stb0899_read_reg(state, (reg | 0x00ff));
531
532 if (ret != 1) {
533 if (ret != -ERESTARTSYS)
Reinhard Nisslc615a272008-07-09 15:33:38 -0300534 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300535 reg, data[0], count, ret);
536 return ret < 0 ? ret : -EREMOTEIO;
537 }
538
539 return 0;
540}
541
542int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
543{
544 return stb0899_write_regs(state, reg, &data, 1);
545}
546
547/*
548 * stb0899_get_mclk
549 * Get STB0899 master clock frequency
550 * ExtClk: external clock frequency (Hz)
551 */
552static u32 stb0899_get_mclk(struct stb0899_state *state)
553{
Manu Abraham72f78412007-11-10 16:59:16 -0300554 u32 mclk = 0, div = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300555
556 div = stb0899_read_reg(state, STB0899_NCOARSE);
557 mclk = (div + 1) * state->config->xtal_freq / 6;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300558 dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300559
560 return mclk;
561}
562
563/*
564 * stb0899_set_mclk
565 * Set STB0899 master Clock frequency
566 * Mclk: demodulator master clock
567 * ExtClk: external clock frequency (Hz)
568 */
569static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
570{
571 struct stb0899_internal *internal = &state->internal;
572 u8 mdiv = 0;
573
Reinhard Nisslc615a272008-07-09 15:33:38 -0300574 dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300575 mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300576 dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300577
578 stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
579 internal->master_clk = stb0899_get_mclk(state);
580
Reinhard Nisslc615a272008-07-09 15:33:38 -0300581 dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300582}
583
Manu Abrahamb168e352007-10-30 19:46:49 -0300584static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
585{
586 struct stb0899_config *config = state->config;
Manu Abraham043a68b2008-01-18 14:15:17 -0300587 const struct stb0899_postproc *postproc = config->postproc;
Manu Abrahamb168e352007-10-30 19:46:49 -0300588
589 /* post process event */
590 if (postproc) {
591 if (enable) {
Manu Abraham9efdd292007-10-30 20:05:59 -0300592 if (postproc[ctl].level == STB0899_GPIOPULLUP)
Manu Abrahamb168e352007-10-30 19:46:49 -0300593 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
594 else
595 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
596 } else {
Manu Abraham9efdd292007-10-30 20:05:59 -0300597 if (postproc[ctl].level == STB0899_GPIOPULLUP)
Manu Abrahamb168e352007-10-30 19:46:49 -0300598 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
599 else
600 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
601 }
602 }
603 return 0;
604}
605
Manu Abraham8bd135b2007-07-03 09:53:42 -0300606static void stb0899_release(struct dvb_frontend *fe)
607{
608 struct stb0899_state *state = fe->demodulator_priv;
609
Reinhard Nisslc615a272008-07-09 15:33:38 -0300610 dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
Manu Abrahamb168e352007-10-30 19:46:49 -0300611 /* post process event */
612 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300613 kfree(state);
614}
615
616/*
617 * stb0899_get_alpha
618 * return: rolloff
619 */
620static int stb0899_get_alpha(struct stb0899_state *state)
621{
622 u8 mode_coeff;
623
624 mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
625
626 if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
627 return 20;
628 else
629 return 35;
630}
631
632/*
633 * stb0899_init_calc
634 */
635static void stb0899_init_calc(struct stb0899_state *state)
636{
637 struct stb0899_internal *internal = &state->internal;
638 int master_clk;
Marko Schluessler7d8f1e52007-10-23 19:56:18 -0300639 u8 agc[2];
Manu Abraham8bd135b2007-07-03 09:53:42 -0300640 u32 reg;
641
642 /* Read registers (in burst mode) */
Manu Abraham8bd135b2007-07-03 09:53:42 -0300643 stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
644
645 /* Initial calculations */
646 master_clk = stb0899_get_mclk(state);
647 internal->t_agc1 = 0;
648 internal->t_agc2 = 0;
649 internal->master_clk = master_clk;
650 internal->mclk = master_clk / 65536L;
651 internal->rolloff = stb0899_get_alpha(state);
652
653 /* DVBS2 Initial calculations */
654 /* Set AGC value to the middle */
655 internal->agc_gain = 8154;
656 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
657 STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
658 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
659
660 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
661 internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
662
663 internal->center_freq = 0;
664 internal->av_frame_coarse = 10;
665 internal->av_frame_fine = 20;
666 internal->step_size = 2;
667/*
668 if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
669 pParams->IQLocked = 0;
670 else
671 pParams->IQLocked = 1;
672*/
673}
674
675static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
676{
677 u8 reg = 0;
678 unsigned long start = jiffies;
679
680 while (1) {
681 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
682 if (!STB0899_GETFIELD(FIFOFULL, reg))
683 break;
684 if ((jiffies - start) > timeout) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300685 dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300686 return -ETIMEDOUT;
687 }
688 }
689
690 return 0;
691}
692
693static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
694{
695 struct stb0899_state *state = fe->demodulator_priv;
696 u8 reg, i;
697
698 if (cmd->msg_len > 8)
699 return -EINVAL;
700
701 /* enable FIFO precharge */
702 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
703 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
704 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
705 for (i = 0; i < cmd->msg_len; i++) {
706 /* wait for FIFO empty */
Hans Petter Selaskyaad04c72011-05-23 11:21:47 -0300707 if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300708 return -ETIMEDOUT;
709
710 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
711 }
712 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
713 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
714 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
Manu Abraham1697c8d2010-11-14 16:01:47 -0300715 msleep(100);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300716 return 0;
717}
718
719static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
720{
721 u8 reg = 0;
722 unsigned long start = jiffies;
723
724 while (!STB0899_GETFIELD(RXEND, reg)) {
725 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
726 if (jiffies - start > timeout) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300727 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300728 return -ETIMEDOUT;
729 }
730 msleep(10);
731 }
732
733 return 0;
734}
735
736static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
737{
738 struct stb0899_state *state = fe->demodulator_priv;
739 u8 reg, length = 0, i;
740 int result;
741
742 if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
743 return -ETIMEDOUT;
744
745 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
746 if (STB0899_GETFIELD(RXEND, reg)) {
747
748 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
749 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
750
751 if (length > sizeof (reply->msg)) {
752 result = -EOVERFLOW;
753 goto exit;
754 }
755 reply->msg_len = length;
756
757 /* extract data */
758 for (i = 0; i < length; i++)
759 reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
760 }
761
762 return 0;
763exit:
764
765 return result;
766}
767
768static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
769{
770 u8 reg = 0;
771 unsigned long start = jiffies;
772
773 while (!STB0899_GETFIELD(TXIDLE, reg)) {
774 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
775 if (jiffies - start > timeout) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300776 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300777 return -ETIMEDOUT;
778 }
779 msleep(10);
780 }
781 return 0;
782}
783
784static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
785{
786 struct stb0899_state *state = fe->demodulator_priv;
787 u8 reg, old_state;
788
789 /* wait for diseqc idle */
790 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
791 return -ETIMEDOUT;
792
793 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
794 old_state = reg;
795 /* set to burst mode */
Sigmund Augdald284e4f2009-01-26 19:08:48 -0300796 STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300797 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
798 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
799 switch (burst) {
800 case SEC_MINI_A:
801 /* unmodulated */
802 stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
803 break;
804 case SEC_MINI_B:
805 /* modulated */
806 stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
807 break;
808 }
809 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
810 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
811 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
812 /* wait for diseqc idle */
813 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
814 return -ETIMEDOUT;
815
816 /* restore state */
817 stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
818
819 return 0;
820}
821
Manu Abraham6c1022c2007-10-05 11:41:19 -0300822static int stb0899_diseqc_init(struct stb0899_state *state)
823{
Manu Abraham3f400922008-10-26 18:28:52 -0300824/*
Manu Abraham6c1022c2007-10-05 11:41:19 -0300825 struct dvb_diseqc_slave_reply rx_data;
Manu Abraham3f400922008-10-26 18:28:52 -0300826*/
Hans Verkuil5becbc52012-05-14 10:22:58 -0300827 u8 f22_tx, reg;
Manu Abraham6c1022c2007-10-05 11:41:19 -0300828
Manu Abraham3f400922008-10-26 18:28:52 -0300829 u32 mclk, tx_freq = 22000;/* count = 0, i; */
Manu Abraham6c1022c2007-10-05 11:41:19 -0300830 reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
831 STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
832 stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
833
834 /* disable Tx spy */
835 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
836 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
837 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
838
839 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
840 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
841 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
842
843 mclk = stb0899_get_mclk(state);
844 f22_tx = mclk / (tx_freq * 32);
845 stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
846 state->rx_freq = 20000;
Manu Abraham6c1022c2007-10-05 11:41:19 -0300847
848 return 0;
849}
Manu Abraham8bd135b2007-07-03 09:53:42 -0300850
851static int stb0899_sleep(struct dvb_frontend *fe)
852{
853 struct stb0899_state *state = fe->demodulator_priv;
Manu Abraham3f400922008-10-26 18:28:52 -0300854/*
Manu Abraham8bd135b2007-07-03 09:53:42 -0300855 u8 reg;
Manu Abraham3f400922008-10-26 18:28:52 -0300856*/
Reinhard Nisslc615a272008-07-09 15:33:38 -0300857 dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
Manu Abrahamb168e352007-10-30 19:46:49 -0300858 /* post process event */
859 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
860
Manu Abraham8bd135b2007-07-03 09:53:42 -0300861 return 0;
862}
863
864static int stb0899_wakeup(struct dvb_frontend *fe)
865{
866 int rc;
867 struct stb0899_state *state = fe->demodulator_priv;
868
Manu Abrahamde29eb82007-09-22 21:39:17 -0300869 if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
870 return rc;
871 /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
872 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
873 return rc;
874 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
875 return rc;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300876
Manu Abrahamb168e352007-10-30 19:46:49 -0300877 /* post process event */
878 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
879
Manu Abraham8bd135b2007-07-03 09:53:42 -0300880 return 0;
881}
882
883static int stb0899_init(struct dvb_frontend *fe)
884{
885 int i;
886 struct stb0899_state *state = fe->demodulator_priv;
887 struct stb0899_config *config = state->config;
888
Reinhard Nisslc615a272008-07-09 15:33:38 -0300889 dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300890
891 /* init device */
Reinhard Nisslc615a272008-07-09 15:33:38 -0300892 dprintk(state->verbose, FE_DEBUG, 1, "init device");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300893 for (i = 0; config->init_dev[i].address != 0xffff; i++)
894 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
895
Reinhard Nisslc615a272008-07-09 15:33:38 -0300896 dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300897 /* init S2 demod */
898 for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
899 stb0899_write_s2reg(state, STB0899_S2DEMOD,
900 config->init_s2_demod[i].base_address,
901 config->init_s2_demod[i].offset,
902 config->init_s2_demod[i].data);
903
Reinhard Nisslc615a272008-07-09 15:33:38 -0300904 dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300905 /* init S1 demod */
906 for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
907 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
908
Reinhard Nisslc615a272008-07-09 15:33:38 -0300909 dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300910 /* init S2 fec */
911 for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
912 stb0899_write_s2reg(state, STB0899_S2FEC,
913 config->init_s2_fec[i].base_address,
914 config->init_s2_fec[i].offset,
915 config->init_s2_fec[i].data);
916
Reinhard Nisslc615a272008-07-09 15:33:38 -0300917 dprintk(state->verbose, FE_DEBUG, 1, "init TST");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300918 /* init test */
919 for (i = 0; config->init_tst[i].address != 0xffff; i++)
920 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
921
922 stb0899_init_calc(state);
Manu Abraham6c1022c2007-10-05 11:41:19 -0300923 stb0899_diseqc_init(state);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300924
925 return 0;
926}
927
928static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
929{
930 int res = 0;
931 int min = 0, med;
932
933 if (val < tab[min].read)
934 res = tab[min].real;
935 else if (val >= tab[max].read)
936 res = tab[max].real;
937 else {
938 while ((max - min) > 1) {
939 med = (max + min) / 2;
940 if (val >= tab[min].read && val < tab[med].read)
941 max = med;
942 else
943 min = med;
944 }
945 res = ((val - tab[min].read) *
946 (tab[max].real - tab[min].real) /
947 (tab[max].read - tab[min].read)) +
948 tab[min].real;
949 }
950
951 return res;
952}
953
954static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
955{
956 struct stb0899_state *state = fe->demodulator_priv;
957 struct stb0899_internal *internal = &state->internal;
958
959 int val;
960 u32 reg;
Klaus Schmidinger0e377812012-01-24 05:33:53 -0300961 *strength = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300962 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -0300963 case SYS_DVBS:
964 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -0300965 if (internal->lock) {
966 reg = stb0899_read_reg(state, STB0899_VSTATUS);
967 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
968
969 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
970 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
971
972 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
973 *strength += 750;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300974 dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300975 val & 0xff, *strength);
976 }
977 }
978 break;
Manu Abraham3f400922008-10-26 18:28:52 -0300979 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -0300980 if (internal->lock) {
Andreas Regelef3d23d2012-02-28 14:40:44 -0300981 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300982 val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
983
984 *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
Klaus Schmidinger0e377812012-01-24 05:33:53 -0300985 *strength += 950;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300986 dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300987 val & 0x3fff, *strength);
988 }
989 break;
990 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -0300991 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -0300992 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300993 }
994
995 return 0;
996}
997
998static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
999{
1000 struct stb0899_state *state = fe->demodulator_priv;
1001 struct stb0899_internal *internal = &state->internal;
1002
1003 unsigned int val, quant, quantn = -1, est, estn = -1;
1004 u8 buf[2];
1005 u32 reg;
1006
Klaus Schmidinger0e377812012-01-24 05:33:53 -03001007 *snr = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001008 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1009 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001010 case SYS_DVBS:
1011 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001012 if (internal->lock) {
1013 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1014
1015 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1016 val = MAKEWORD16(buf[0], buf[1]);
1017
1018 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001019 dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
Manu Abraham8bd135b2007-07-03 09:53:42 -03001020 buf[0], buf[1], val, *snr);
1021 }
1022 }
1023 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001024 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001025 if (internal->lock) {
1026 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1027 quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1028 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1029 est = STB0899_GETFIELD(ESN0_EST, reg);
1030 if (est == 1)
1031 val = 301; /* C/N = 30.1 dB */
1032 else if (est == 2)
1033 val = 270; /* C/N = 27.0 dB */
1034 else {
1035 /* quantn = 100 * log(quant^2) */
1036 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1037 /* estn = 100 * log(est) */
1038 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1039 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1040 val = (quantn - estn) / 10;
1041 }
1042 *snr = val;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001043 dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -03001044 quant, quantn, est, estn, val);
1045 }
1046 break;
1047 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001048 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001049 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001050 }
1051
1052 return 0;
1053}
1054
1055static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1056{
1057 struct stb0899_state *state = fe->demodulator_priv;
1058 struct stb0899_internal *internal = &state->internal;
1059 u8 reg;
1060 *status = 0;
1061
1062 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001063 case SYS_DVBS:
1064 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001065 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1066 if (internal->lock) {
1067 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1068 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1069 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
Andreas Regel91caad32012-02-28 14:40:40 -03001070 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001071
1072 reg = stb0899_read_reg(state, STB0899_PLPARM);
1073 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1074 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1075 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
Manu Abrahamb168e352007-10-30 19:46:49 -03001076 /* post process event */
1077 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001078 }
1079 }
1080 }
1081 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001082 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001083 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1084 if (internal->lock) {
1085 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1086 if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1087 *status |= FE_HAS_CARRIER;
1088 dprintk(state->verbose, FE_DEBUG, 1,
1089 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1090
1091 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1092 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1093 *status |= FE_HAS_LOCK;
1094 dprintk(state->verbose, FE_DEBUG, 1,
1095 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1096
1097 }
1098 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1099 *status |= FE_HAS_VITERBI;
1100 dprintk(state->verbose, FE_DEBUG, 1,
1101 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1102 }
1103 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1104 *status |= FE_HAS_SYNC;
1105 dprintk(state->verbose, FE_DEBUG, 1,
1106 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
Manu Abrahamb168e352007-10-30 19:46:49 -03001107 /* post process event */
1108 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001109 }
1110 }
1111 }
1112 break;
1113 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001114 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001115 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001116 }
1117 return 0;
1118}
1119
1120/*
1121 * stb0899_get_error
1122 * viterbi error for DVB-S/DSS
1123 * packet error for DVB-S2
1124 * Bit Error Rate or Packet Error Rate * 10 ^ 7
1125 */
1126static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1127{
1128 struct stb0899_state *state = fe->demodulator_priv;
1129 struct stb0899_internal *internal = &state->internal;
1130
1131 u8 lsb, msb;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001132
1133 *ber = 0;
1134
1135 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001136 case SYS_DVBS:
1137 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001138 if (internal->lock) {
Klaus Schmidingerfdaaee62012-06-02 11:04:17 -03001139 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1140 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1141 *ber = MAKEWORD16(msb, lsb);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001142 /* Viterbi Check */
1143 if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1144 /* Error Rate */
1145 *ber *= 9766;
1146 /* ber = ber * 10 ^ 7 */
1147 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1148 *ber /= 8;
1149 }
1150 }
1151 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001152 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001153 if (internal->lock) {
Klaus Schmidingerfdaaee62012-06-02 11:04:17 -03001154 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1155 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1156 *ber = MAKEWORD16(msb, lsb);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001157 /* ber = ber * 10 ^ 7 */
1158 *ber *= 10000000;
1159 *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1160 }
1161 break;
1162 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001163 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001164 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001165 }
1166
1167 return 0;
1168}
1169
1170static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
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
1197static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
1198{
1199 struct stb0899_state *state = fe->demodulator_priv;
1200 struct stb0899_internal *internal = &state->internal;
1201
Manu Abrahambaa40e42007-10-05 09:04:32 -03001202 u8 div, reg;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001203
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);
Manu Abrahambaa40e42007-10-05 09:04:32 -03001213 reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1214 STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1215 stb0899_write_reg(state, STB0899_ACRPRESC, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001216 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:
Manu Abraham417dd692007-10-10 06:12:13 -03001222 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001223 }
1224 return 0;
1225}
1226
Manu Abraham40e8ce32008-02-03 19:37:02 -03001227int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001228{
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;
Manu Abraham40e8ce32008-02-03 19:37:02 -03001241 } 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;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001246 }
1247 return 0;
1248err:
Manu Abraham40e8ce32008-02-03 19:37:02 -03001249 dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001250 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
Mauro Carvalho Chehab5506bcb2012-10-27 11:25:28 -03001263static int stb0899_get_dev_id(struct stb0899_state *state)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001264{
1265 u8 chip_id, release;
1266 u16 id;
1267 u32 demod_ver = 0, fec_ver = 0;
Reinhard Nissl56137412008-01-21 16:17:48 -03001268 char demod_str[5] = { 0 };
1269 char fec_str[5] = { 0 };
Manu Abraham8bd135b2007-07-03 09:53:42 -03001270
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
Manu Abrahame399a782008-03-08 20:28:09 -03001295static void stb0899_set_delivery(struct stb0899_state *state)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001296{
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) {
Manu Abraham3f400922008-10-26 18:28:52 -03001304 case SYS_DVBS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001305 dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001306 /* 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);
Manu Abraham18527be2008-01-25 18:25:13 -03001329 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001330
1331 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1332 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001333 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001334 /* 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;
Manu Abraham3f400922008-10-26 18:28:52 -03001361 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001362 /* 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:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001388 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001389 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
Lutz Sammer699cc192011-05-04 07:27:31 -03001415 reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001416 STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
Lutz Sammer699cc192011-05-04 07:27:31 -03001417 stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001418}
1419
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -03001420static enum dvbfe_search stb0899_search(struct dvb_frontend *fe)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001421{
1422 struct stb0899_state *state = fe->demodulator_priv;
1423 struct stb0899_params *i_params = &state->params;
1424 struct stb0899_internal *internal = &state->internal;
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001425 struct stb0899_config *config = state->config;
Manu Abraham3f400922008-10-26 18:28:52 -03001426 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001427
1428 u32 SearchRange, gain;
1429
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -03001430 i_params->freq = props->frequency;
1431 i_params->srate = props->symbol_rate;
Manu Abraham3f400922008-10-26 18:28:52 -03001432 state->delsys = props->delivery_system;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001433 dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001434
Manu Abraham763fbaf2008-01-18 11:28:48 -03001435 SearchRange = 10000000;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001436 dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001437 /* checking Search Range is meaningless for a fixed 3 Mhz */
1438 if (INRANGE(i_params->srate, 1000000, 45000000)) {
Reinhard Nisslc615a272008-07-09 15:33:38 -03001439 dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
Manu Abrahame399a782008-03-08 20:28:09 -03001440 stb0899_set_delivery(state);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001441
1442 if (state->config->tuner_set_rfsiggain) {
1443 if (internal->srate > 15000000)
Manu Abraham3f400922008-10-26 18:28:52 -03001444 gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
Manu Abraham8bd135b2007-07-03 09:53:42 -03001445 else if (internal->srate > 5000000)
Manu Abraham3f400922008-10-26 18:28:52 -03001446 gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
Manu Abraham8bd135b2007-07-03 09:53:42 -03001447 else
Manu Abraham3f400922008-10-26 18:28:52 -03001448 gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
Manu Abraham8bd135b2007-07-03 09:53:42 -03001449 state->config->tuner_set_rfsiggain(fe, gain);
1450 }
1451
1452 if (i_params->srate <= 5000000)
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001453 stb0899_set_mclk(state, config->lo_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001454 else
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001455 stb0899_set_mclk(state, config->hi_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001456
1457 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001458 case SYS_DVBS:
1459 case SYS_DSS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001460 dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001461 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 ? */
Manu Abraham40e8ce32008-02-03 19:37:02 -03001473 /* enable tuner I/O */
1474 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1475
Manu Abraham8bd135b2007-07-03 09:53:42 -03001476 if (state->config->tuner_set_bandwidth)
Reinhard Nissl02ec9d82008-01-18 20:09:52 -03001477 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001478 if (state->config->tuner_get_bandwidth)
1479 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
Manu Abraham40e8ce32008-02-03 19:37:02 -03001480
1481 /* disable tuner I/O */
1482 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1483
Manu Abraham8bd135b2007-07-03 09:53:42 -03001484 /* Set DVB-S1 AGC */
1485 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1486
1487 /* Run the search algorithm */
Reinhard Nisslc615a272008-07-09 15:33:38 -03001488 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001489 if (stb0899_dvbs_algo(state) == RANGEOK) {
1490 internal->lock = 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001491 dprintk(state->verbose, FE_DEBUG, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001492 "-------------------------------------> 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);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001497// 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);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001499
1500 return DVBFE_ALGO_SEARCH_SUCCESS;
1501 } else {
1502 internal->lock = 0;
1503
1504 return DVBFE_ALGO_SEARCH_FAILED;
1505 }
1506 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001507 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001508 internal->freq = i_params->freq;
1509 internal->srate = i_params->srate;
1510 internal->srch_range = SearchRange;
1511
Manu Abraham40e8ce32008-02-03 19:37:02 -03001512 /* enable tuner I/O */
1513 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1514
Manu Abraham8bd135b2007-07-03 09:53:42 -03001515 if (state->config->tuner_set_bandwidth)
Manu Abraham763fbaf2008-01-18 11:28:48 -03001516 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
Manu Abraham8bd135b2007-07-03 09:53:42 -03001517 if (state->config->tuner_get_bandwidth)
1518 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1519
Manu Abraham40e8ce32008-02-03 19:37:02 -03001520 /* disable tuner I/O */
1521 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1522
Manu Abraham8bd135b2007-07-03 09:53:42 -03001523// 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 */
Reinhard Nisslc615a272008-07-09 15:33:38 -03001532 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001533 if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
1534 internal->lock = 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001535 dprintk(state->verbose, FE_DEBUG, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001536 "-------------------------------------> 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:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001550 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001551 return DVBFE_ALGO_SEARCH_INVALID;
1552 }
1553 }
1554
1555 return DVBFE_ALGO_SEARCH_ERROR;
1556}
Manu Abraham8bd135b2007-07-03 09:53:42 -03001557
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001558static int stb0899_get_frontend(struct dvb_frontend *fe)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001559{
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001560 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001561 struct stb0899_state *state = fe->demodulator_priv;
1562 struct stb0899_internal *internal = &state->internal;
1563
Manu Abraham3f400922008-10-26 18:28:52 -03001564 dprintk(state->verbose, FE_DEBUG, 1, "Get params");
Mauro Carvalho Chehab57158362011-12-26 14:16:52 -03001565 p->symbol_rate = internal->srate;
Reinhard Nissl226143f2012-08-18 08:59:57 -03001566 p->frequency = internal->freq;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001567
1568 return 0;
1569}
1570
1571static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1572{
1573 return DVBFE_ALGO_CUSTOM;
1574}
1575
1576static struct dvb_frontend_ops stb0899_ops = {
Mauro Carvalho Chehab7581e612012-01-01 16:11:18 -03001577 .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
Manu Abraham8bd135b2007-07-03 09:53:42 -03001578 .info = {
Manu Abraham3f400922008-10-26 18:28:52 -03001579 .name = "STB0899 Multistandard",
Manu Abraham3f400922008-10-26 18:28:52 -03001580 .frequency_min = 950000,
1581 .frequency_max = 2150000,
1582 .frequency_stepsize = 0,
1583 .frequency_tolerance = 0,
Zoran Turalija55b33182013-06-02 14:56:33 -03001584 .symbol_rate_min = 2000000,
Manu Abraham3f400922008-10-26 18:28:52 -03001585 .symbol_rate_max = 45000000,
1586
1587 .caps = FE_CAN_INVERSION_AUTO |
1588 FE_CAN_FEC_AUTO |
Klaus Schmidingerfaed4aa2008-12-31 14:13:56 -03001589 FE_CAN_2G_MODULATION |
Manu Abraham3f400922008-10-26 18:28:52 -03001590 FE_CAN_QPSK
Manu Abraham8bd135b2007-07-03 09:53:42 -03001591 },
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,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001599
1600 .get_frontend_algo = stb0899_frontend_algo,
1601 .search = stb0899_search,
Mauro Carvalho Chehab57158362011-12-26 14:16:52 -03001602 .get_frontend = stb0899_get_frontend,
Manu Abraham3f400922008-10-26 18:28:52 -03001603
Manu Abraham8bd135b2007-07-03 09:53:42 -03001604
1605 .read_status = stb0899_read_status,
1606 .read_snr = stb0899_read_snr,
1607 .read_signal_strength = stb0899_read_signal_strength,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001608 .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
Reinhard Nisslc615a272008-07-09 15:33:38 -03001626 state->verbose = &verbose;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001627 state->config = config;
1628 state->i2c = i2c;
1629 state->frontend.ops = stb0899_ops;
1630 state->frontend.demodulator_priv = state;
Reinhard Nißl0c1d2b12013-06-02 14:59:00 -03001631 /* use configured inversion as default -- we'll later autodetect inversion */
1632 state->internal.inversion = config->inversion;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001633
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");