blob: 07cd5ea7a03811c1c01204ac9eb6b42458bb2a6f [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
Mauro Carvalho Chehabba474642013-11-02 05:14:58 -030035/* Max transfer size done by I2C transfer functions */
36#define MAX_XFER_SIZE 64
37
Reinhard Nisslc615a272008-07-09 15:33:38 -030038static unsigned int verbose = 0;//1;
Manu Abraham8bd135b2007-07-03 09:53:42 -030039module_param(verbose, int, 0644);
40
41/* C/N in dB/10, NIRM/NIRL */
42static const struct stb0899_tab stb0899_cn_tab[] = {
43 { 200, 2600 },
44 { 190, 2700 },
45 { 180, 2860 },
46 { 170, 3020 },
47 { 160, 3210 },
48 { 150, 3440 },
49 { 140, 3710 },
50 { 130, 4010 },
51 { 120, 4360 },
52 { 110, 4740 },
53 { 100, 5190 },
54 { 90, 5670 },
55 { 80, 6200 },
56 { 70, 6770 },
57 { 60, 7360 },
58 { 50, 7970 },
59 { 40, 8250 },
60 { 30, 9000 },
61 { 20, 9450 },
62 { 15, 9600 },
63};
64
65/* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
66 * As measured, connected to a modulator.
67 * -8.0 to -50.0 dBm directly connected,
68 * -52.0 to -74.8 with extra attenuation.
69 * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
70 * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
71 */
72static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
Klaus Schmidinger0e377812012-01-24 05:33:53 -030073 { -750, -128 },
Manu Abraham8bd135b2007-07-03 09:53:42 -030074 { -748, -94 },
75 { -745, -92 },
76 { -735, -90 },
77 { -720, -87 },
78 { -670, -77 },
79 { -640, -70 },
80 { -610, -62 },
81 { -600, -60 },
82 { -590, -56 },
83 { -560, -41 },
84 { -540, -25 },
85 { -530, -17 },
86 { -520, -11 },
87 { -500, 1 },
88 { -490, 6 },
89 { -480, 10 },
90 { -440, 22 },
91 { -420, 27 },
92 { -400, 31 },
93 { -380, 34 },
94 { -340, 40 },
95 { -320, 43 },
96 { -280, 48 },
97 { -250, 52 },
98 { -230, 55 },
99 { -180, 61 },
100 { -140, 66 },
101 { -90, 73 },
102 { -80, 74 },
103 { 500, 127 }
104};
105
106/* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
107 * As measured, connected to a modulator.
108 * -8.0 to -50.1 dBm directly connected,
109 * -53.0 to -76.6 with extra attenuation.
110 * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
111 * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
112 */
113static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
114 { 700, 0 },
115 { -80, 3217 },
116 { -150, 3893 },
117 { -190, 4217 },
118 { -240, 4621 },
119 { -280, 4945 },
120 { -320, 5273 },
121 { -350, 5545 },
122 { -370, 5741 },
123 { -410, 6147 },
124 { -450, 6671 },
125 { -490, 7413 },
126 { -501, 7665 },
127 { -530, 8767 },
128 { -560, 10219 },
129 { -580, 10939 },
130 { -590, 11518 },
131 { -600, 11723 },
132 { -650, 12659 },
133 { -690, 13219 },
134 { -730, 13645 },
135 { -750, 13909 },
136 { -766, 14153 },
Klaus Schmidinger0e377812012-01-24 05:33:53 -0300137 { -950, 16383 }
Manu Abraham8bd135b2007-07-03 09:53:42 -0300138};
139
140/* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300141static struct stb0899_tab stb0899_quant_tab[] = {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300142 { 0, 0 },
143 { 0, 100 },
144 { 600, 200 },
145 { 950, 299 },
146 { 1200, 398 },
147 { 1400, 501 },
148 { 1560, 603 },
149 { 1690, 700 },
150 { 1810, 804 },
151 { 1910, 902 },
152 { 2000, 1000 },
153 { 2080, 1096 },
154 { 2160, 1202 },
155 { 2230, 1303 },
156 { 2350, 1496 },
157 { 2410, 1603 },
158 { 2460, 1698 },
159 { 2510, 1799 },
160 { 2600, 1995 },
161 { 2650, 2113 },
162 { 2690, 2213 },
163 { 2720, 2291 },
164 { 2760, 2399 },
165 { 2800, 2512 },
166 { 2860, 2692 },
167 { 2930, 2917 },
168 { 2960, 3020 },
169 { 3010, 3199 },
170 { 3040, 3311 },
171 { 3060, 3388 },
172 { 3120, 3631 },
173 { 3190, 3936 },
174 { 3400, 5012 },
175 { 3610, 6383 },
176 { 3800, 7943 },
177 { 4210, 12735 },
178 { 4500, 17783 },
179 { 4690, 22131 },
180 { 4810, 25410 }
181};
182
183/* DVB-S2 Es/N0 estimate in dB/100 vs read value */
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300184static struct stb0899_tab stb0899_est_tab[] = {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300185 { 0, 0 },
186 { 0, 1 },
187 { 301, 2 },
188 { 1204, 16 },
189 { 1806, 64 },
190 { 2408, 256 },
191 { 2709, 512 },
192 { 3010, 1023 },
193 { 3311, 2046 },
194 { 3612, 4093 },
195 { 3823, 6653 },
196 { 3913, 8185 },
197 { 4010, 10233 },
198 { 4107, 12794 },
199 { 4214, 16368 },
200 { 4266, 18450 },
201 { 4311, 20464 },
202 { 4353, 22542 },
203 { 4391, 24604 },
204 { 4425, 26607 },
205 { 4457, 28642 },
206 { 4487, 30690 },
207 { 4515, 32734 },
208 { 4612, 40926 },
209 { 4692, 49204 },
210 { 4816, 65464 },
211 { 4913, 81846 },
212 { 4993, 98401 },
213 { 5060, 114815 },
214 { 5118, 131220 },
215 { 5200, 158489 },
216 { 5300, 199526 },
217 { 5400, 251189 },
218 { 5500, 316228 },
219 { 5600, 398107 },
220 { 5720, 524807 },
221 { 5721, 526017 },
222};
223
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300224static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300225{
226 int ret;
227
228 u8 b0[] = { reg >> 8, reg & 0xff };
229 u8 buf;
230
231 struct i2c_msg msg[] = {
232 {
233 .addr = state->config->demod_address,
234 .flags = 0,
235 .buf = b0,
236 .len = 2
237 },{
238 .addr = state->config->demod_address,
239 .flags = I2C_M_RD,
240 .buf = &buf,
241 .len = 1
242 }
243 };
244
245 ret = i2c_transfer(state->i2c, msg, 2);
246 if (ret != 2) {
247 if (ret != -ERESTARTSYS)
Reinhard Nisslc615a272008-07-09 15:33:38 -0300248 dprintk(state->verbose, FE_ERROR, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -0300249 "Read error, Reg=[0x%02x], Status=%d",
250 reg, ret);
251
252 return ret < 0 ? ret : -EREMOTEIO;
253 }
Reinhard Nisslc615a272008-07-09 15:33:38 -0300254 if (unlikely(*state->verbose >= FE_DEBUGREG))
255 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300256 reg, buf);
257
Manu Abraham8bd135b2007-07-03 09:53:42 -0300258 return (unsigned int)buf;
259}
260
261int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
262{
263 int result;
264
265 result = _stb0899_read_reg(state, reg);
266 /*
267 * Bug ID 9:
268 * access to 0xf2xx/0xf6xx
269 * must be followed by read from 0xf2ff/0xf6ff.
270 */
271 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
272 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
273 _stb0899_read_reg(state, (reg | 0x00ff));
274
275 return result;
276}
277
278u32 _stb0899_read_s2reg(struct stb0899_state *state,
Igor M. Liplianin886134e2008-10-30 22:04:56 -0300279 u32 stb0899_i2cdev,
280 u32 stb0899_base_addr,
281 u16 stb0899_reg_offset)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300282{
283 int status;
284 u32 data;
285 u8 buf[7] = { 0 };
286 u16 tmpaddr;
287
288 u8 buf_0[] = {
289 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
290 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
291 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
292 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
293 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
294 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
295 };
296 u8 buf_1[] = {
297 0x00, /* 0xf3 Reg Offset */
298 0x00, /* 0x44 Reg Offset */
299 };
300
301 struct i2c_msg msg_0 = {
302 .addr = state->config->demod_address,
303 .flags = 0,
304 .buf = buf_0,
305 .len = 6
306 };
307
308 struct i2c_msg msg_1 = {
309 .addr = state->config->demod_address,
310 .flags = 0,
311 .buf = buf_1,
312 .len = 2
313 };
314
315 struct i2c_msg msg_r = {
316 .addr = state->config->demod_address,
317 .flags = I2C_M_RD,
318 .buf = buf,
319 .len = 4
320 };
321
322 tmpaddr = stb0899_reg_offset & 0xff00;
323 if (!(stb0899_reg_offset & 0x8))
324 tmpaddr = stb0899_reg_offset | 0x20;
325
326 buf_1[0] = GETBYTE(tmpaddr, BYTE1);
327 buf_1[1] = GETBYTE(tmpaddr, BYTE0);
328
329 status = i2c_transfer(state->i2c, &msg_0, 1);
330 if (status < 1) {
331 if (status != -ERESTARTSYS)
332 printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
333 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
334
335 goto err;
336 }
337
338 /* Dummy */
339 status = i2c_transfer(state->i2c, &msg_1, 1);
340 if (status < 1)
341 goto err;
342
343 status = i2c_transfer(state->i2c, &msg_r, 1);
344 if (status < 1)
345 goto err;
346
347 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
348 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
349
350 /* Actual */
351 status = i2c_transfer(state->i2c, &msg_1, 1);
352 if (status < 1) {
353 if (status != -ERESTARTSYS)
354 printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
355 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
356 goto err;
357 }
358
359 status = i2c_transfer(state->i2c, &msg_r, 1);
360 if (status < 1) {
361 if (status != -ERESTARTSYS)
362 printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
363 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
364 return status < 0 ? status : -EREMOTEIO;
365 }
366
367 data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
Reinhard Nisslc615a272008-07-09 15:33:38 -0300368 if (unlikely(*state->verbose >= FE_DEBUGREG))
Manu Abraham8bd135b2007-07-03 09:53:42 -0300369 printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
370 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
371
372 return data;
373
374err:
375 return status < 0 ? status : -EREMOTEIO;
376}
377
378int stb0899_write_s2reg(struct stb0899_state *state,
379 u32 stb0899_i2cdev,
380 u32 stb0899_base_addr,
381 u16 stb0899_reg_offset,
382 u32 stb0899_data)
383{
384 int status;
385
386 /* Base Address Setup */
387 u8 buf_0[] = {
388 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
389 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
390 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
391 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
392 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
393 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
394 };
395 u8 buf_1[] = {
396 0x00, /* 0xf3 Reg Offset */
397 0x00, /* 0x44 Reg Offset */
398 0x00, /* data */
399 0x00, /* data */
400 0x00, /* data */
401 0x00, /* data */
402 };
403
404 struct i2c_msg msg_0 = {
405 .addr = state->config->demod_address,
406 .flags = 0,
407 .buf = buf_0,
408 .len = 6
409 };
410
411 struct i2c_msg msg_1 = {
412 .addr = state->config->demod_address,
413 .flags = 0,
414 .buf = buf_1,
415 .len = 6
416 };
417
418 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
419 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
420 buf_1[2] = GETBYTE(stb0899_data, BYTE0);
421 buf_1[3] = GETBYTE(stb0899_data, BYTE1);
422 buf_1[4] = GETBYTE(stb0899_data, BYTE2);
423 buf_1[5] = GETBYTE(stb0899_data, BYTE3);
424
Reinhard Nisslc615a272008-07-09 15:33:38 -0300425 if (unlikely(*state->verbose >= FE_DEBUGREG))
Manu Abraham8bd135b2007-07-03 09:53:42 -0300426 printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
427 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
428
429 status = i2c_transfer(state->i2c, &msg_0, 1);
430 if (unlikely(status < 1)) {
431 if (status != -ERESTARTSYS)
432 printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
433 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
434 goto err;
435 }
436 status = i2c_transfer(state->i2c, &msg_1, 1);
437 if (unlikely(status < 1)) {
438 if (status != -ERESTARTSYS)
439 printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
440 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
441
442 return status < 0 ? status : -EREMOTEIO;
443 }
444
445 return 0;
446
447err:
448 return status < 0 ? status : -EREMOTEIO;
449}
450
Manu Abraham3d6a3beb2008-01-25 20:39:16 -0300451int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300452{
453 int status;
454
455 u8 b0[] = { reg >> 8, reg & 0xff };
456
457 struct i2c_msg msg[] = {
458 {
459 .addr = state->config->demod_address,
460 .flags = 0,
461 .buf = b0,
462 .len = 2
463 },{
464 .addr = state->config->demod_address,
465 .flags = I2C_M_RD,
466 .buf = buf,
467 .len = count
468 }
469 };
470
471 status = i2c_transfer(state->i2c, msg, 2);
472 if (status != 2) {
473 if (status != -ERESTARTSYS)
474 printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
475 __func__, reg, count, status);
476 goto err;
477 }
478 /*
479 * Bug ID 9:
480 * access to 0xf2xx/0xf6xx
481 * must be followed by read from 0xf2ff/0xf6ff.
482 */
483 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
484 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
485 _stb0899_read_reg(state, (reg | 0x00ff));
486
Reinhard Nisslc615a272008-07-09 15:33:38 -0300487 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300488 int i;
489
490 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
491 for (i = 0; i < count; i++) {
492 printk(" %02x", buf[i]);
493 }
494 printk("\n");
495 }
496
497 return 0;
498err:
499 return status < 0 ? status : -EREMOTEIO;
500}
501
Manu Abraham85eabac2008-01-25 20:28:46 -0300502int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300503{
504 int ret;
Mauro Carvalho Chehabba474642013-11-02 05:14:58 -0300505 u8 buf[MAX_XFER_SIZE];
Manu Abraham8bd135b2007-07-03 09:53:42 -0300506 struct i2c_msg i2c_msg = {
507 .addr = state->config->demod_address,
508 .flags = 0,
509 .buf = buf,
510 .len = 2 + count
511 };
512
Mauro Carvalho Chehabba474642013-11-02 05:14:58 -0300513 if (2 + count > sizeof(buf)) {
514 printk(KERN_WARNING
515 "%s: i2c wr reg=%04x: len=%d is too big!\n",
516 KBUILD_MODNAME, reg, count);
517 return -EINVAL;
518 }
519
Manu Abraham8bd135b2007-07-03 09:53:42 -0300520 buf[0] = reg >> 8;
521 buf[1] = reg & 0xff;
522 memcpy(&buf[2], data, count);
523
Reinhard Nisslc615a272008-07-09 15:33:38 -0300524 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300525 int i;
526
527 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
528 for (i = 0; i < count; i++)
529 printk(" %02x", data[i]);
530 printk("\n");
531 }
532 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
533
534 /*
535 * Bug ID 9:
536 * access to 0xf2xx/0xf6xx
537 * must be followed by read from 0xf2ff/0xf6ff.
538 */
539 if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
540 stb0899_read_reg(state, (reg | 0x00ff));
541
542 if (ret != 1) {
543 if (ret != -ERESTARTSYS)
Reinhard Nisslc615a272008-07-09 15:33:38 -0300544 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300545 reg, data[0], count, ret);
546 return ret < 0 ? ret : -EREMOTEIO;
547 }
548
549 return 0;
550}
551
552int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
553{
554 return stb0899_write_regs(state, reg, &data, 1);
555}
556
557/*
558 * stb0899_get_mclk
559 * Get STB0899 master clock frequency
560 * ExtClk: external clock frequency (Hz)
561 */
562static u32 stb0899_get_mclk(struct stb0899_state *state)
563{
Manu Abraham72f78412007-11-10 16:59:16 -0300564 u32 mclk = 0, div = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300565
566 div = stb0899_read_reg(state, STB0899_NCOARSE);
567 mclk = (div + 1) * state->config->xtal_freq / 6;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300568 dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300569
570 return mclk;
571}
572
573/*
574 * stb0899_set_mclk
575 * Set STB0899 master Clock frequency
576 * Mclk: demodulator master clock
577 * ExtClk: external clock frequency (Hz)
578 */
579static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
580{
581 struct stb0899_internal *internal = &state->internal;
582 u8 mdiv = 0;
583
Reinhard Nisslc615a272008-07-09 15:33:38 -0300584 dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300585 mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300586 dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300587
588 stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
589 internal->master_clk = stb0899_get_mclk(state);
590
Reinhard Nisslc615a272008-07-09 15:33:38 -0300591 dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300592}
593
Manu Abrahamb168e352007-10-30 19:46:49 -0300594static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
595{
596 struct stb0899_config *config = state->config;
Manu Abraham043a68b2008-01-18 14:15:17 -0300597 const struct stb0899_postproc *postproc = config->postproc;
Manu Abrahamb168e352007-10-30 19:46:49 -0300598
599 /* post process event */
600 if (postproc) {
601 if (enable) {
Manu Abraham9efdd292007-10-30 20:05:59 -0300602 if (postproc[ctl].level == STB0899_GPIOPULLUP)
Manu Abrahamb168e352007-10-30 19:46:49 -0300603 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
604 else
605 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
606 } else {
Manu Abraham9efdd292007-10-30 20:05:59 -0300607 if (postproc[ctl].level == STB0899_GPIOPULLUP)
Manu Abrahamb168e352007-10-30 19:46:49 -0300608 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
609 else
610 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
611 }
612 }
613 return 0;
614}
615
Manu Abraham8bd135b2007-07-03 09:53:42 -0300616static void stb0899_release(struct dvb_frontend *fe)
617{
618 struct stb0899_state *state = fe->demodulator_priv;
619
Reinhard Nisslc615a272008-07-09 15:33:38 -0300620 dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
Manu Abrahamb168e352007-10-30 19:46:49 -0300621 /* post process event */
622 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300623 kfree(state);
624}
625
626/*
627 * stb0899_get_alpha
628 * return: rolloff
629 */
630static int stb0899_get_alpha(struct stb0899_state *state)
631{
632 u8 mode_coeff;
633
634 mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
635
636 if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
637 return 20;
638 else
639 return 35;
640}
641
642/*
643 * stb0899_init_calc
644 */
645static void stb0899_init_calc(struct stb0899_state *state)
646{
647 struct stb0899_internal *internal = &state->internal;
648 int master_clk;
Marko Schluessler7d8f1e52007-10-23 19:56:18 -0300649 u8 agc[2];
Manu Abraham8bd135b2007-07-03 09:53:42 -0300650 u32 reg;
651
652 /* Read registers (in burst mode) */
Manu Abraham8bd135b2007-07-03 09:53:42 -0300653 stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
654
655 /* Initial calculations */
656 master_clk = stb0899_get_mclk(state);
657 internal->t_agc1 = 0;
658 internal->t_agc2 = 0;
659 internal->master_clk = master_clk;
660 internal->mclk = master_clk / 65536L;
661 internal->rolloff = stb0899_get_alpha(state);
662
663 /* DVBS2 Initial calculations */
664 /* Set AGC value to the middle */
665 internal->agc_gain = 8154;
666 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
667 STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
668 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
669
670 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
671 internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
672
673 internal->center_freq = 0;
674 internal->av_frame_coarse = 10;
675 internal->av_frame_fine = 20;
676 internal->step_size = 2;
677/*
678 if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
679 pParams->IQLocked = 0;
680 else
681 pParams->IQLocked = 1;
682*/
683}
684
685static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
686{
687 u8 reg = 0;
688 unsigned long start = jiffies;
689
690 while (1) {
691 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
692 if (!STB0899_GETFIELD(FIFOFULL, reg))
693 break;
694 if ((jiffies - start) > timeout) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300695 dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300696 return -ETIMEDOUT;
697 }
698 }
699
700 return 0;
701}
702
703static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
704{
705 struct stb0899_state *state = fe->demodulator_priv;
706 u8 reg, i;
707
708 if (cmd->msg_len > 8)
709 return -EINVAL;
710
711 /* enable FIFO precharge */
712 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
713 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
714 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
715 for (i = 0; i < cmd->msg_len; i++) {
716 /* wait for FIFO empty */
Hans Petter Selaskyaad04c72011-05-23 11:21:47 -0300717 if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300718 return -ETIMEDOUT;
719
720 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
721 }
722 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
723 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
724 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
Manu Abraham1697c8d2010-11-14 16:01:47 -0300725 msleep(100);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300726 return 0;
727}
728
729static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
730{
731 u8 reg = 0;
732 unsigned long start = jiffies;
733
734 while (!STB0899_GETFIELD(RXEND, reg)) {
735 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
736 if (jiffies - start > timeout) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300737 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300738 return -ETIMEDOUT;
739 }
740 msleep(10);
741 }
742
743 return 0;
744}
745
746static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
747{
748 struct stb0899_state *state = fe->demodulator_priv;
749 u8 reg, length = 0, i;
750 int result;
751
752 if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
753 return -ETIMEDOUT;
754
755 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
756 if (STB0899_GETFIELD(RXEND, reg)) {
757
758 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
759 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
760
761 if (length > sizeof (reply->msg)) {
762 result = -EOVERFLOW;
763 goto exit;
764 }
765 reply->msg_len = length;
766
767 /* extract data */
768 for (i = 0; i < length; i++)
769 reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
770 }
771
772 return 0;
773exit:
774
775 return result;
776}
777
778static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
779{
780 u8 reg = 0;
781 unsigned long start = jiffies;
782
783 while (!STB0899_GETFIELD(TXIDLE, reg)) {
784 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
785 if (jiffies - start > timeout) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300786 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300787 return -ETIMEDOUT;
788 }
789 msleep(10);
790 }
791 return 0;
792}
793
794static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
795{
796 struct stb0899_state *state = fe->demodulator_priv;
797 u8 reg, old_state;
798
799 /* wait for diseqc idle */
800 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
801 return -ETIMEDOUT;
802
803 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
804 old_state = reg;
805 /* set to burst mode */
Sigmund Augdald284e4f2009-01-26 19:08:48 -0300806 STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300807 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
808 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
809 switch (burst) {
810 case SEC_MINI_A:
811 /* unmodulated */
812 stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
813 break;
814 case SEC_MINI_B:
815 /* modulated */
816 stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
817 break;
818 }
819 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
820 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
821 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
822 /* wait for diseqc idle */
823 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
824 return -ETIMEDOUT;
825
826 /* restore state */
827 stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
828
829 return 0;
830}
831
Manu Abraham6c1022c2007-10-05 11:41:19 -0300832static int stb0899_diseqc_init(struct stb0899_state *state)
833{
Manu Abraham3f400922008-10-26 18:28:52 -0300834/*
Manu Abraham6c1022c2007-10-05 11:41:19 -0300835 struct dvb_diseqc_slave_reply rx_data;
Manu Abraham3f400922008-10-26 18:28:52 -0300836*/
Hans Verkuil5becbc52012-05-14 10:22:58 -0300837 u8 f22_tx, reg;
Manu Abraham6c1022c2007-10-05 11:41:19 -0300838
Manu Abraham3f400922008-10-26 18:28:52 -0300839 u32 mclk, tx_freq = 22000;/* count = 0, i; */
Manu Abraham6c1022c2007-10-05 11:41:19 -0300840 reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
841 STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
842 stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
843
844 /* disable Tx spy */
845 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
846 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
847 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
848
849 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
850 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
851 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
852
853 mclk = stb0899_get_mclk(state);
854 f22_tx = mclk / (tx_freq * 32);
855 stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
856 state->rx_freq = 20000;
Manu Abraham6c1022c2007-10-05 11:41:19 -0300857
858 return 0;
859}
Manu Abraham8bd135b2007-07-03 09:53:42 -0300860
861static int stb0899_sleep(struct dvb_frontend *fe)
862{
863 struct stb0899_state *state = fe->demodulator_priv;
Manu Abraham3f400922008-10-26 18:28:52 -0300864/*
Manu Abraham8bd135b2007-07-03 09:53:42 -0300865 u8 reg;
Manu Abraham3f400922008-10-26 18:28:52 -0300866*/
Reinhard Nisslc615a272008-07-09 15:33:38 -0300867 dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
Manu Abrahamb168e352007-10-30 19:46:49 -0300868 /* post process event */
869 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
870
Manu Abraham8bd135b2007-07-03 09:53:42 -0300871 return 0;
872}
873
874static int stb0899_wakeup(struct dvb_frontend *fe)
875{
876 int rc;
877 struct stb0899_state *state = fe->demodulator_priv;
878
Manu Abrahamde29eb82007-09-22 21:39:17 -0300879 if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
880 return rc;
881 /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
882 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
883 return rc;
884 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
885 return rc;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300886
Manu Abrahamb168e352007-10-30 19:46:49 -0300887 /* post process event */
888 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
889
Manu Abraham8bd135b2007-07-03 09:53:42 -0300890 return 0;
891}
892
893static int stb0899_init(struct dvb_frontend *fe)
894{
895 int i;
896 struct stb0899_state *state = fe->demodulator_priv;
897 struct stb0899_config *config = state->config;
898
Reinhard Nisslc615a272008-07-09 15:33:38 -0300899 dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300900
901 /* init device */
Reinhard Nisslc615a272008-07-09 15:33:38 -0300902 dprintk(state->verbose, FE_DEBUG, 1, "init device");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300903 for (i = 0; config->init_dev[i].address != 0xffff; i++)
904 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
905
Reinhard Nisslc615a272008-07-09 15:33:38 -0300906 dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300907 /* init S2 demod */
908 for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
909 stb0899_write_s2reg(state, STB0899_S2DEMOD,
910 config->init_s2_demod[i].base_address,
911 config->init_s2_demod[i].offset,
912 config->init_s2_demod[i].data);
913
Reinhard Nisslc615a272008-07-09 15:33:38 -0300914 dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300915 /* init S1 demod */
916 for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
917 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
918
Reinhard Nisslc615a272008-07-09 15:33:38 -0300919 dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300920 /* init S2 fec */
921 for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
922 stb0899_write_s2reg(state, STB0899_S2FEC,
923 config->init_s2_fec[i].base_address,
924 config->init_s2_fec[i].offset,
925 config->init_s2_fec[i].data);
926
Reinhard Nisslc615a272008-07-09 15:33:38 -0300927 dprintk(state->verbose, FE_DEBUG, 1, "init TST");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300928 /* init test */
929 for (i = 0; config->init_tst[i].address != 0xffff; i++)
930 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
931
932 stb0899_init_calc(state);
Manu Abraham6c1022c2007-10-05 11:41:19 -0300933 stb0899_diseqc_init(state);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300934
935 return 0;
936}
937
938static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
939{
940 int res = 0;
941 int min = 0, med;
942
943 if (val < tab[min].read)
944 res = tab[min].real;
945 else if (val >= tab[max].read)
946 res = tab[max].real;
947 else {
948 while ((max - min) > 1) {
949 med = (max + min) / 2;
950 if (val >= tab[min].read && val < tab[med].read)
951 max = med;
952 else
953 min = med;
954 }
955 res = ((val - tab[min].read) *
956 (tab[max].real - tab[min].real) /
957 (tab[max].read - tab[min].read)) +
958 tab[min].real;
959 }
960
961 return res;
962}
963
964static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
965{
966 struct stb0899_state *state = fe->demodulator_priv;
967 struct stb0899_internal *internal = &state->internal;
968
969 int val;
970 u32 reg;
Klaus Schmidinger0e377812012-01-24 05:33:53 -0300971 *strength = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300972 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -0300973 case SYS_DVBS:
974 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -0300975 if (internal->lock) {
976 reg = stb0899_read_reg(state, STB0899_VSTATUS);
977 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
978
979 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
980 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
981
982 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
983 *strength += 750;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300984 dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300985 val & 0xff, *strength);
986 }
987 }
988 break;
Manu Abraham3f400922008-10-26 18:28:52 -0300989 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -0300990 if (internal->lock) {
Andreas Regelef3d23d2012-02-28 14:40:44 -0300991 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300992 val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
993
994 *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
Klaus Schmidinger0e377812012-01-24 05:33:53 -0300995 *strength += 950;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300996 dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300997 val & 0x3fff, *strength);
998 }
999 break;
1000 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001001 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001002 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001003 }
1004
1005 return 0;
1006}
1007
1008static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
1009{
1010 struct stb0899_state *state = fe->demodulator_priv;
1011 struct stb0899_internal *internal = &state->internal;
1012
1013 unsigned int val, quant, quantn = -1, est, estn = -1;
1014 u8 buf[2];
1015 u32 reg;
1016
Klaus Schmidinger0e377812012-01-24 05:33:53 -03001017 *snr = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001018 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1019 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001020 case SYS_DVBS:
1021 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001022 if (internal->lock) {
1023 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1024
1025 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1026 val = MAKEWORD16(buf[0], buf[1]);
1027
1028 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001029 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 -03001030 buf[0], buf[1], val, *snr);
1031 }
1032 }
1033 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001034 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001035 if (internal->lock) {
1036 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1037 quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1038 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1039 est = STB0899_GETFIELD(ESN0_EST, reg);
1040 if (est == 1)
1041 val = 301; /* C/N = 30.1 dB */
1042 else if (est == 2)
1043 val = 270; /* C/N = 27.0 dB */
1044 else {
1045 /* quantn = 100 * log(quant^2) */
1046 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1047 /* estn = 100 * log(est) */
1048 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1049 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1050 val = (quantn - estn) / 10;
1051 }
1052 *snr = val;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001053 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 -03001054 quant, quantn, est, estn, val);
1055 }
1056 break;
1057 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001058 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001059 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001060 }
1061
1062 return 0;
1063}
1064
1065static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1066{
1067 struct stb0899_state *state = fe->demodulator_priv;
1068 struct stb0899_internal *internal = &state->internal;
1069 u8 reg;
1070 *status = 0;
1071
1072 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001073 case SYS_DVBS:
1074 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001075 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1076 if (internal->lock) {
1077 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1078 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1079 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
Andreas Regel91caad32012-02-28 14:40:40 -03001080 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001081
1082 reg = stb0899_read_reg(state, STB0899_PLPARM);
1083 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1084 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1085 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
Manu Abrahamb168e352007-10-30 19:46:49 -03001086 /* post process event */
1087 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001088 }
1089 }
1090 }
1091 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001092 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001093 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1094 if (internal->lock) {
1095 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1096 if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1097 *status |= FE_HAS_CARRIER;
1098 dprintk(state->verbose, FE_DEBUG, 1,
1099 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1100
1101 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1102 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1103 *status |= FE_HAS_LOCK;
1104 dprintk(state->verbose, FE_DEBUG, 1,
1105 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1106
1107 }
1108 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1109 *status |= FE_HAS_VITERBI;
1110 dprintk(state->verbose, FE_DEBUG, 1,
1111 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1112 }
1113 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1114 *status |= FE_HAS_SYNC;
1115 dprintk(state->verbose, FE_DEBUG, 1,
1116 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
Manu Abrahamb168e352007-10-30 19:46:49 -03001117 /* post process event */
1118 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001119 }
1120 }
1121 }
1122 break;
1123 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001124 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001125 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001126 }
1127 return 0;
1128}
1129
1130/*
1131 * stb0899_get_error
1132 * viterbi error for DVB-S/DSS
1133 * packet error for DVB-S2
1134 * Bit Error Rate or Packet Error Rate * 10 ^ 7
1135 */
1136static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1137{
1138 struct stb0899_state *state = fe->demodulator_priv;
1139 struct stb0899_internal *internal = &state->internal;
1140
1141 u8 lsb, msb;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001142
1143 *ber = 0;
1144
1145 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001146 case SYS_DVBS:
1147 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001148 if (internal->lock) {
Klaus Schmidingerfdaaee62012-06-02 11:04:17 -03001149 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1150 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1151 *ber = MAKEWORD16(msb, lsb);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001152 /* Viterbi Check */
1153 if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1154 /* Error Rate */
1155 *ber *= 9766;
1156 /* ber = ber * 10 ^ 7 */
1157 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1158 *ber /= 8;
1159 }
1160 }
1161 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001162 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001163 if (internal->lock) {
Klaus Schmidingerfdaaee62012-06-02 11:04:17 -03001164 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1165 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1166 *ber = MAKEWORD16(msb, lsb);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001167 /* ber = ber * 10 ^ 7 */
1168 *ber *= 10000000;
1169 *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1170 }
1171 break;
1172 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001173 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001174 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001175 }
1176
1177 return 0;
1178}
1179
1180static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
1181{
1182 struct stb0899_state *state = fe->demodulator_priv;
1183
1184 switch (voltage) {
1185 case SEC_VOLTAGE_13:
1186 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1187 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1188 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1189 break;
1190 case SEC_VOLTAGE_18:
1191 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1192 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1193 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1194 break;
1195 case SEC_VOLTAGE_OFF:
1196 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1197 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1198 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1199 break;
1200 default:
1201 return -EINVAL;
1202 }
1203
1204 return 0;
1205}
1206
1207static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
1208{
1209 struct stb0899_state *state = fe->demodulator_priv;
1210 struct stb0899_internal *internal = &state->internal;
1211
Manu Abrahambaa40e42007-10-05 09:04:32 -03001212 u8 div, reg;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001213
1214 /* wait for diseqc idle */
1215 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1216 return -ETIMEDOUT;
1217
1218 switch (tone) {
1219 case SEC_TONE_ON:
1220 div = (internal->master_clk / 100) / 5632;
1221 div = (div + 5) / 10;
1222 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
Manu Abrahambaa40e42007-10-05 09:04:32 -03001223 reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1224 STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1225 stb0899_write_reg(state, STB0899_ACRPRESC, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001226 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1227 break;
1228 case SEC_TONE_OFF:
1229 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1230 break;
1231 default:
Manu Abraham417dd692007-10-10 06:12:13 -03001232 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001233 }
1234 return 0;
1235}
1236
Manu Abraham40e8ce32008-02-03 19:37:02 -03001237int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001238{
1239 int i2c_stat;
1240 struct stb0899_state *state = fe->demodulator_priv;
1241
1242 i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1243 if (i2c_stat < 0)
1244 goto err;
1245
1246 if (enable) {
1247 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1248 i2c_stat |= STB0899_I2CTON;
1249 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1250 goto err;
Manu Abraham40e8ce32008-02-03 19:37:02 -03001251 } else {
1252 dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
1253 i2c_stat &= ~STB0899_I2CTON;
1254 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1255 goto err;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001256 }
1257 return 0;
1258err:
Manu Abraham40e8ce32008-02-03 19:37:02 -03001259 dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001260 return -EREMOTEIO;
1261}
1262
1263
1264static inline void CONVERT32(u32 x, char *str)
1265{
1266 *str++ = (x >> 24) & 0xff;
1267 *str++ = (x >> 16) & 0xff;
1268 *str++ = (x >> 8) & 0xff;
1269 *str++ = (x >> 0) & 0xff;
1270 *str = '\0';
1271}
1272
Mauro Carvalho Chehab5506bcb2012-10-27 11:25:28 -03001273static int stb0899_get_dev_id(struct stb0899_state *state)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001274{
1275 u8 chip_id, release;
1276 u16 id;
1277 u32 demod_ver = 0, fec_ver = 0;
Reinhard Nissl56137412008-01-21 16:17:48 -03001278 char demod_str[5] = { 0 };
1279 char fec_str[5] = { 0 };
Manu Abraham8bd135b2007-07-03 09:53:42 -03001280
1281 id = stb0899_read_reg(state, STB0899_DEV_ID);
1282 dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1283 chip_id = STB0899_GETFIELD(CHIP_ID, id);
1284 release = STB0899_GETFIELD(CHIP_REL, id);
1285
1286 dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1287 chip_id, release);
1288
1289 CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1290
1291 demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1292 dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1293 CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1294 fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1295 if (! (chip_id > 0)) {
1296 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1297
1298 return -ENODEV;
1299 }
1300 dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1301
1302 return 0;
1303}
1304
Manu Abrahame399a782008-03-08 20:28:09 -03001305static void stb0899_set_delivery(struct stb0899_state *state)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001306{
1307 u8 reg;
1308 u8 stop_clk[2];
1309
1310 stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1311 stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1312
1313 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001314 case SYS_DVBS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001315 dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001316 /* FECM/Viterbi ON */
1317 reg = stb0899_read_reg(state, STB0899_FECM);
1318 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1319 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1320 stb0899_write_reg(state, STB0899_FECM, reg);
1321
1322 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1323 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1324 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1325 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1326
1327 reg = stb0899_read_reg(state, STB0899_TSTRES);
1328 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1329 stb0899_write_reg(state, STB0899_TSTRES, reg);
1330
1331 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1332 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1333 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1334
1335 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1336 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1337
1338 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
Manu Abraham18527be2008-01-25 18:25:13 -03001339 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001340
1341 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1342 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001343 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001344 /* FECM/Viterbi OFF */
1345 reg = stb0899_read_reg(state, STB0899_FECM);
1346 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1347 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1348 stb0899_write_reg(state, STB0899_FECM, reg);
1349
1350 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1351 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1352 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1353 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1354
1355 reg = stb0899_read_reg(state, STB0899_TSTRES);
1356 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1357 stb0899_write_reg(state, STB0899_TSTRES, reg);
1358
1359 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1360 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1361 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1362
1363 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1364 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1365
1366 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1367 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1368
1369 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1370 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001371 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001372 /* FECM/Viterbi ON */
1373 reg = stb0899_read_reg(state, STB0899_FECM);
1374 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1375 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1376 stb0899_write_reg(state, STB0899_FECM, reg);
1377
1378 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1379 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1380 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1381
1382 reg = stb0899_read_reg(state, STB0899_TSTRES);
1383 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1384 stb0899_write_reg(state, STB0899_TSTRES, reg);
1385
1386 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1387 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1388 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1389
1390 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1391 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1392
1393 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1394
1395 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1396 break;
1397 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001398 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001399 break;
1400 }
1401 STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1402 stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1403}
1404
1405/*
1406 * stb0899_set_iterations
1407 * set the LDPC iteration scale function
1408 */
1409static void stb0899_set_iterations(struct stb0899_state *state)
1410{
1411 struct stb0899_internal *internal = &state->internal;
1412 struct stb0899_config *config = state->config;
1413
1414 s32 iter_scale;
1415 u32 reg;
1416
1417 iter_scale = 17 * (internal->master_clk / 1000);
1418 iter_scale += 410000;
1419 iter_scale /= (internal->srate / 1000000);
1420 iter_scale /= 1000;
1421
1422 if (iter_scale > config->ldpc_max_iter)
1423 iter_scale = config->ldpc_max_iter;
1424
Lutz Sammer699cc192011-05-04 07:27:31 -03001425 reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001426 STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
Lutz Sammer699cc192011-05-04 07:27:31 -03001427 stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001428}
1429
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -03001430static enum dvbfe_search stb0899_search(struct dvb_frontend *fe)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001431{
1432 struct stb0899_state *state = fe->demodulator_priv;
1433 struct stb0899_params *i_params = &state->params;
1434 struct stb0899_internal *internal = &state->internal;
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001435 struct stb0899_config *config = state->config;
Manu Abraham3f400922008-10-26 18:28:52 -03001436 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001437
1438 u32 SearchRange, gain;
1439
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -03001440 i_params->freq = props->frequency;
1441 i_params->srate = props->symbol_rate;
Manu Abraham3f400922008-10-26 18:28:52 -03001442 state->delsys = props->delivery_system;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001443 dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001444
Manu Abraham763fbaf2008-01-18 11:28:48 -03001445 SearchRange = 10000000;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001446 dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001447 /* checking Search Range is meaningless for a fixed 3 Mhz */
1448 if (INRANGE(i_params->srate, 1000000, 45000000)) {
Reinhard Nisslc615a272008-07-09 15:33:38 -03001449 dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
Manu Abrahame399a782008-03-08 20:28:09 -03001450 stb0899_set_delivery(state);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001451
1452 if (state->config->tuner_set_rfsiggain) {
1453 if (internal->srate > 15000000)
Manu Abraham3f400922008-10-26 18:28:52 -03001454 gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
Manu Abraham8bd135b2007-07-03 09:53:42 -03001455 else if (internal->srate > 5000000)
Manu Abraham3f400922008-10-26 18:28:52 -03001456 gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
Manu Abraham8bd135b2007-07-03 09:53:42 -03001457 else
Manu Abraham3f400922008-10-26 18:28:52 -03001458 gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
Manu Abraham8bd135b2007-07-03 09:53:42 -03001459 state->config->tuner_set_rfsiggain(fe, gain);
1460 }
1461
1462 if (i_params->srate <= 5000000)
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001463 stb0899_set_mclk(state, config->lo_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001464 else
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001465 stb0899_set_mclk(state, config->hi_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001466
1467 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001468 case SYS_DVBS:
1469 case SYS_DSS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001470 dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001471 internal->freq = i_params->freq;
1472 internal->srate = i_params->srate;
1473 /*
1474 * search = user search range +
1475 * 500Khz +
1476 * 2 * Tuner_step_size +
1477 * 10% of the symbol rate
1478 */
1479 internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
1480 internal->derot_percent = 30;
1481
1482 /* What to do for tuners having no bandwidth setup ? */
Manu Abraham40e8ce32008-02-03 19:37:02 -03001483 /* enable tuner I/O */
1484 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1485
Manu Abraham8bd135b2007-07-03 09:53:42 -03001486 if (state->config->tuner_set_bandwidth)
Reinhard Nissl02ec9d82008-01-18 20:09:52 -03001487 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001488 if (state->config->tuner_get_bandwidth)
1489 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
Manu Abraham40e8ce32008-02-03 19:37:02 -03001490
1491 /* disable tuner I/O */
1492 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1493
Manu Abraham8bd135b2007-07-03 09:53:42 -03001494 /* Set DVB-S1 AGC */
1495 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1496
1497 /* Run the search algorithm */
Reinhard Nisslc615a272008-07-09 15:33:38 -03001498 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001499 if (stb0899_dvbs_algo(state) == RANGEOK) {
1500 internal->lock = 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001501 dprintk(state->verbose, FE_DEBUG, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001502 "-------------------------------------> DVB-S LOCK !");
1503
1504// stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
1505// internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1506// internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001507// dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1508// dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001509
1510 return DVBFE_ALGO_SEARCH_SUCCESS;
1511 } else {
1512 internal->lock = 0;
1513
1514 return DVBFE_ALGO_SEARCH_FAILED;
1515 }
1516 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001517 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001518 internal->freq = i_params->freq;
1519 internal->srate = i_params->srate;
1520 internal->srch_range = SearchRange;
1521
Manu Abraham40e8ce32008-02-03 19:37:02 -03001522 /* enable tuner I/O */
1523 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1524
Manu Abraham8bd135b2007-07-03 09:53:42 -03001525 if (state->config->tuner_set_bandwidth)
Manu Abraham763fbaf2008-01-18 11:28:48 -03001526 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
Manu Abraham8bd135b2007-07-03 09:53:42 -03001527 if (state->config->tuner_get_bandwidth)
1528 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1529
Manu Abraham40e8ce32008-02-03 19:37:02 -03001530 /* disable tuner I/O */
1531 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1532
Manu Abraham8bd135b2007-07-03 09:53:42 -03001533// pParams->SpectralInv = pSearch->IQ_Inversion;
1534
1535 /* Set DVB-S2 AGC */
1536 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1537
1538 /* Set IterScale =f(MCLK,SYMB) */
1539 stb0899_set_iterations(state);
1540
1541 /* Run the search algorithm */
Reinhard Nisslc615a272008-07-09 15:33:38 -03001542 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001543 if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
1544 internal->lock = 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001545 dprintk(state->verbose, FE_DEBUG, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001546 "-------------------------------------> DVB-S2 LOCK !");
1547
1548// stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
1549// internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1550// internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1551
1552 return DVBFE_ALGO_SEARCH_SUCCESS;
1553 } else {
1554 internal->lock = 0;
1555
1556 return DVBFE_ALGO_SEARCH_FAILED;
1557 }
1558 break;
1559 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001560 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001561 return DVBFE_ALGO_SEARCH_INVALID;
1562 }
1563 }
1564
1565 return DVBFE_ALGO_SEARCH_ERROR;
1566}
Manu Abraham8bd135b2007-07-03 09:53:42 -03001567
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001568static int stb0899_get_frontend(struct dvb_frontend *fe)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001569{
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001570 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001571 struct stb0899_state *state = fe->demodulator_priv;
1572 struct stb0899_internal *internal = &state->internal;
1573
Manu Abraham3f400922008-10-26 18:28:52 -03001574 dprintk(state->verbose, FE_DEBUG, 1, "Get params");
Mauro Carvalho Chehab57158362011-12-26 14:16:52 -03001575 p->symbol_rate = internal->srate;
Reinhard Nissl226143f2012-08-18 08:59:57 -03001576 p->frequency = internal->freq;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001577
1578 return 0;
1579}
1580
1581static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1582{
1583 return DVBFE_ALGO_CUSTOM;
1584}
1585
1586static struct dvb_frontend_ops stb0899_ops = {
Mauro Carvalho Chehab7581e612012-01-01 16:11:18 -03001587 .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
Manu Abraham8bd135b2007-07-03 09:53:42 -03001588 .info = {
Manu Abraham3f400922008-10-26 18:28:52 -03001589 .name = "STB0899 Multistandard",
Manu Abraham3f400922008-10-26 18:28:52 -03001590 .frequency_min = 950000,
1591 .frequency_max = 2150000,
1592 .frequency_stepsize = 0,
1593 .frequency_tolerance = 0,
Mauro Carvalho Chehab07ecbf22013-07-05 10:31:43 -03001594 .symbol_rate_min = 5000000,
Manu Abraham3f400922008-10-26 18:28:52 -03001595 .symbol_rate_max = 45000000,
1596
1597 .caps = FE_CAN_INVERSION_AUTO |
1598 FE_CAN_FEC_AUTO |
Klaus Schmidingerfaed4aa2008-12-31 14:13:56 -03001599 FE_CAN_2G_MODULATION |
Manu Abraham3f400922008-10-26 18:28:52 -03001600 FE_CAN_QPSK
Manu Abraham8bd135b2007-07-03 09:53:42 -03001601 },
1602
1603 .release = stb0899_release,
1604 .init = stb0899_init,
1605 .sleep = stb0899_sleep,
1606// .wakeup = stb0899_wakeup,
1607
1608 .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001609
1610 .get_frontend_algo = stb0899_frontend_algo,
1611 .search = stb0899_search,
Mauro Carvalho Chehab57158362011-12-26 14:16:52 -03001612 .get_frontend = stb0899_get_frontend,
Manu Abraham3f400922008-10-26 18:28:52 -03001613
Manu Abraham8bd135b2007-07-03 09:53:42 -03001614
1615 .read_status = stb0899_read_status,
1616 .read_snr = stb0899_read_snr,
1617 .read_signal_strength = stb0899_read_signal_strength,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001618 .read_ber = stb0899_read_ber,
1619
1620 .set_voltage = stb0899_set_voltage,
1621 .set_tone = stb0899_set_tone,
1622
1623 .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
1624 .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
1625 .diseqc_send_burst = stb0899_send_diseqc_burst,
1626};
1627
1628struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1629{
1630 struct stb0899_state *state = NULL;
1631
1632 state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1633 if (state == NULL)
1634 goto error;
1635
Reinhard Nisslc615a272008-07-09 15:33:38 -03001636 state->verbose = &verbose;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001637 state->config = config;
1638 state->i2c = i2c;
1639 state->frontend.ops = stb0899_ops;
1640 state->frontend.demodulator_priv = state;
Reinhard Nißl0c1d2b12013-06-02 14:59:00 -03001641 /* use configured inversion as default -- we'll later autodetect inversion */
1642 state->internal.inversion = config->inversion;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001643
1644 stb0899_wakeup(&state->frontend);
1645 if (stb0899_get_dev_id(state) == -ENODEV) {
1646 printk("%s: Exiting .. !\n", __func__);
1647 goto error;
1648 }
1649
1650 printk("%s: Attaching STB0899 \n", __func__);
1651 return &state->frontend;
1652
1653error:
1654 kfree(state);
1655 return NULL;
1656}
1657EXPORT_SYMBOL(stb0899_attach);
1658MODULE_PARM_DESC(verbose, "Set Verbosity level");
1659MODULE_AUTHOR("Manu Abraham");
1660MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1661MODULE_LICENSE("GPL");