blob: db5dde3215f02039ecdd0180bfebe667d639ce71 [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>
Asaf Vertz3edd59a2014-12-14 03:49:47 -030023#include <linux/jiffies.h>
Manu Abraham8bd135b2007-07-03 09:53:42 -030024#include <linux/kernel.h>
25#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Manu Abraham8bd135b2007-07-03 09:53:42 -030027#include <linux/string.h>
28
29#include <linux/dvb/frontend.h>
30#include "dvb_frontend.h"
31
32#include "stb0899_drv.h"
33#include "stb0899_priv.h"
34#include "stb0899_reg.h"
35
Mauro Carvalho Chehabba474642013-11-02 05:14:58 -030036/* Max transfer size done by I2C transfer functions */
37#define MAX_XFER_SIZE 64
38
Reinhard Nisslc615a272008-07-09 15:33:38 -030039static unsigned int verbose = 0;//1;
Manu Abraham8bd135b2007-07-03 09:53:42 -030040module_param(verbose, int, 0644);
41
42/* C/N in dB/10, NIRM/NIRL */
43static const struct stb0899_tab stb0899_cn_tab[] = {
44 { 200, 2600 },
45 { 190, 2700 },
46 { 180, 2860 },
47 { 170, 3020 },
48 { 160, 3210 },
49 { 150, 3440 },
50 { 140, 3710 },
51 { 130, 4010 },
52 { 120, 4360 },
53 { 110, 4740 },
54 { 100, 5190 },
55 { 90, 5670 },
56 { 80, 6200 },
57 { 70, 6770 },
58 { 60, 7360 },
59 { 50, 7970 },
60 { 40, 8250 },
61 { 30, 9000 },
62 { 20, 9450 },
63 { 15, 9600 },
64};
65
66/* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
67 * As measured, connected to a modulator.
68 * -8.0 to -50.0 dBm directly connected,
69 * -52.0 to -74.8 with extra attenuation.
70 * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
71 * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
72 */
73static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
Klaus Schmidinger0e377812012-01-24 05:33:53 -030074 { -750, -128 },
Manu Abraham8bd135b2007-07-03 09:53:42 -030075 { -748, -94 },
76 { -745, -92 },
77 { -735, -90 },
78 { -720, -87 },
79 { -670, -77 },
80 { -640, -70 },
81 { -610, -62 },
82 { -600, -60 },
83 { -590, -56 },
84 { -560, -41 },
85 { -540, -25 },
86 { -530, -17 },
87 { -520, -11 },
88 { -500, 1 },
89 { -490, 6 },
90 { -480, 10 },
91 { -440, 22 },
92 { -420, 27 },
93 { -400, 31 },
94 { -380, 34 },
95 { -340, 40 },
96 { -320, 43 },
97 { -280, 48 },
98 { -250, 52 },
99 { -230, 55 },
100 { -180, 61 },
101 { -140, 66 },
102 { -90, 73 },
103 { -80, 74 },
104 { 500, 127 }
105};
106
107/* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
108 * As measured, connected to a modulator.
109 * -8.0 to -50.1 dBm directly connected,
110 * -53.0 to -76.6 with extra attenuation.
111 * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
112 * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
113 */
114static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
115 { 700, 0 },
116 { -80, 3217 },
117 { -150, 3893 },
118 { -190, 4217 },
119 { -240, 4621 },
120 { -280, 4945 },
121 { -320, 5273 },
122 { -350, 5545 },
123 { -370, 5741 },
124 { -410, 6147 },
125 { -450, 6671 },
126 { -490, 7413 },
127 { -501, 7665 },
128 { -530, 8767 },
129 { -560, 10219 },
130 { -580, 10939 },
131 { -590, 11518 },
132 { -600, 11723 },
133 { -650, 12659 },
134 { -690, 13219 },
135 { -730, 13645 },
136 { -750, 13909 },
137 { -766, 14153 },
Klaus Schmidinger0e377812012-01-24 05:33:53 -0300138 { -950, 16383 }
Manu Abraham8bd135b2007-07-03 09:53:42 -0300139};
140
141/* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300142static struct stb0899_tab stb0899_quant_tab[] = {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300143 { 0, 0 },
144 { 0, 100 },
145 { 600, 200 },
146 { 950, 299 },
147 { 1200, 398 },
148 { 1400, 501 },
149 { 1560, 603 },
150 { 1690, 700 },
151 { 1810, 804 },
152 { 1910, 902 },
153 { 2000, 1000 },
154 { 2080, 1096 },
155 { 2160, 1202 },
156 { 2230, 1303 },
157 { 2350, 1496 },
158 { 2410, 1603 },
159 { 2460, 1698 },
160 { 2510, 1799 },
161 { 2600, 1995 },
162 { 2650, 2113 },
163 { 2690, 2213 },
164 { 2720, 2291 },
165 { 2760, 2399 },
166 { 2800, 2512 },
167 { 2860, 2692 },
168 { 2930, 2917 },
169 { 2960, 3020 },
170 { 3010, 3199 },
171 { 3040, 3311 },
172 { 3060, 3388 },
173 { 3120, 3631 },
174 { 3190, 3936 },
175 { 3400, 5012 },
176 { 3610, 6383 },
177 { 3800, 7943 },
178 { 4210, 12735 },
179 { 4500, 17783 },
180 { 4690, 22131 },
181 { 4810, 25410 }
182};
183
184/* DVB-S2 Es/N0 estimate in dB/100 vs read value */
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300185static struct stb0899_tab stb0899_est_tab[] = {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300186 { 0, 0 },
187 { 0, 1 },
188 { 301, 2 },
189 { 1204, 16 },
190 { 1806, 64 },
191 { 2408, 256 },
192 { 2709, 512 },
193 { 3010, 1023 },
194 { 3311, 2046 },
195 { 3612, 4093 },
196 { 3823, 6653 },
197 { 3913, 8185 },
198 { 4010, 10233 },
199 { 4107, 12794 },
200 { 4214, 16368 },
201 { 4266, 18450 },
202 { 4311, 20464 },
203 { 4353, 22542 },
204 { 4391, 24604 },
205 { 4425, 26607 },
206 { 4457, 28642 },
207 { 4487, 30690 },
208 { 4515, 32734 },
209 { 4612, 40926 },
210 { 4692, 49204 },
211 { 4816, 65464 },
212 { 4913, 81846 },
213 { 4993, 98401 },
214 { 5060, 114815 },
215 { 5118, 131220 },
216 { 5200, 158489 },
217 { 5300, 199526 },
218 { 5400, 251189 },
219 { 5500, 316228 },
220 { 5600, 398107 },
221 { 5720, 524807 },
222 { 5721, 526017 },
223};
224
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300225static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300226{
227 int ret;
228
229 u8 b0[] = { reg >> 8, reg & 0xff };
230 u8 buf;
231
232 struct i2c_msg msg[] = {
233 {
234 .addr = state->config->demod_address,
235 .flags = 0,
236 .buf = b0,
237 .len = 2
238 },{
239 .addr = state->config->demod_address,
240 .flags = I2C_M_RD,
241 .buf = &buf,
242 .len = 1
243 }
244 };
245
246 ret = i2c_transfer(state->i2c, msg, 2);
247 if (ret != 2) {
248 if (ret != -ERESTARTSYS)
Reinhard Nisslc615a272008-07-09 15:33:38 -0300249 dprintk(state->verbose, FE_ERROR, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -0300250 "Read error, Reg=[0x%02x], Status=%d",
251 reg, ret);
252
253 return ret < 0 ? ret : -EREMOTEIO;
254 }
Reinhard Nisslc615a272008-07-09 15:33:38 -0300255 if (unlikely(*state->verbose >= FE_DEBUGREG))
256 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300257 reg, buf);
258
Manu Abraham8bd135b2007-07-03 09:53:42 -0300259 return (unsigned int)buf;
260}
261
262int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
263{
264 int result;
265
266 result = _stb0899_read_reg(state, reg);
267 /*
268 * Bug ID 9:
269 * access to 0xf2xx/0xf6xx
270 * must be followed by read from 0xf2ff/0xf6ff.
271 */
272 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
273 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
274 _stb0899_read_reg(state, (reg | 0x00ff));
275
276 return result;
277}
278
279u32 _stb0899_read_s2reg(struct stb0899_state *state,
Igor M. Liplianin886134e2008-10-30 22:04:56 -0300280 u32 stb0899_i2cdev,
281 u32 stb0899_base_addr,
282 u16 stb0899_reg_offset)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300283{
284 int status;
285 u32 data;
286 u8 buf[7] = { 0 };
287 u16 tmpaddr;
288
289 u8 buf_0[] = {
290 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
291 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
292 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
293 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
294 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
295 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
296 };
297 u8 buf_1[] = {
298 0x00, /* 0xf3 Reg Offset */
299 0x00, /* 0x44 Reg Offset */
300 };
301
302 struct i2c_msg msg_0 = {
303 .addr = state->config->demod_address,
304 .flags = 0,
305 .buf = buf_0,
306 .len = 6
307 };
308
309 struct i2c_msg msg_1 = {
310 .addr = state->config->demod_address,
311 .flags = 0,
312 .buf = buf_1,
313 .len = 2
314 };
315
316 struct i2c_msg msg_r = {
317 .addr = state->config->demod_address,
318 .flags = I2C_M_RD,
319 .buf = buf,
320 .len = 4
321 };
322
323 tmpaddr = stb0899_reg_offset & 0xff00;
324 if (!(stb0899_reg_offset & 0x8))
325 tmpaddr = stb0899_reg_offset | 0x20;
326
327 buf_1[0] = GETBYTE(tmpaddr, BYTE1);
328 buf_1[1] = GETBYTE(tmpaddr, BYTE0);
329
330 status = i2c_transfer(state->i2c, &msg_0, 1);
331 if (status < 1) {
332 if (status != -ERESTARTSYS)
333 printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
334 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
335
336 goto err;
337 }
338
339 /* Dummy */
340 status = i2c_transfer(state->i2c, &msg_1, 1);
341 if (status < 1)
342 goto err;
343
344 status = i2c_transfer(state->i2c, &msg_r, 1);
345 if (status < 1)
346 goto err;
347
348 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
349 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
350
351 /* Actual */
352 status = i2c_transfer(state->i2c, &msg_1, 1);
353 if (status < 1) {
354 if (status != -ERESTARTSYS)
355 printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
356 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
357 goto err;
358 }
359
360 status = i2c_transfer(state->i2c, &msg_r, 1);
361 if (status < 1) {
362 if (status != -ERESTARTSYS)
363 printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
364 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
365 return status < 0 ? status : -EREMOTEIO;
366 }
367
368 data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
Reinhard Nisslc615a272008-07-09 15:33:38 -0300369 if (unlikely(*state->verbose >= FE_DEBUGREG))
Manu Abraham8bd135b2007-07-03 09:53:42 -0300370 printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
371 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
372
373 return data;
374
375err:
376 return status < 0 ? status : -EREMOTEIO;
377}
378
379int stb0899_write_s2reg(struct stb0899_state *state,
380 u32 stb0899_i2cdev,
381 u32 stb0899_base_addr,
382 u16 stb0899_reg_offset,
383 u32 stb0899_data)
384{
385 int status;
386
387 /* Base Address Setup */
388 u8 buf_0[] = {
389 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
390 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
391 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
392 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
393 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
394 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
395 };
396 u8 buf_1[] = {
397 0x00, /* 0xf3 Reg Offset */
398 0x00, /* 0x44 Reg Offset */
399 0x00, /* data */
400 0x00, /* data */
401 0x00, /* data */
402 0x00, /* data */
403 };
404
405 struct i2c_msg msg_0 = {
406 .addr = state->config->demod_address,
407 .flags = 0,
408 .buf = buf_0,
409 .len = 6
410 };
411
412 struct i2c_msg msg_1 = {
413 .addr = state->config->demod_address,
414 .flags = 0,
415 .buf = buf_1,
416 .len = 6
417 };
418
419 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
420 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
421 buf_1[2] = GETBYTE(stb0899_data, BYTE0);
422 buf_1[3] = GETBYTE(stb0899_data, BYTE1);
423 buf_1[4] = GETBYTE(stb0899_data, BYTE2);
424 buf_1[5] = GETBYTE(stb0899_data, BYTE3);
425
Reinhard Nisslc615a272008-07-09 15:33:38 -0300426 if (unlikely(*state->verbose >= FE_DEBUGREG))
Manu Abraham8bd135b2007-07-03 09:53:42 -0300427 printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
428 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
429
430 status = i2c_transfer(state->i2c, &msg_0, 1);
431 if (unlikely(status < 1)) {
432 if (status != -ERESTARTSYS)
433 printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
434 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
435 goto err;
436 }
437 status = i2c_transfer(state->i2c, &msg_1, 1);
438 if (unlikely(status < 1)) {
439 if (status != -ERESTARTSYS)
440 printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
441 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
442
443 return status < 0 ? status : -EREMOTEIO;
444 }
445
446 return 0;
447
448err:
449 return status < 0 ? status : -EREMOTEIO;
450}
451
Manu Abraham3d6a3beb2008-01-25 20:39:16 -0300452int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300453{
454 int status;
455
456 u8 b0[] = { reg >> 8, reg & 0xff };
457
458 struct i2c_msg msg[] = {
459 {
460 .addr = state->config->demod_address,
461 .flags = 0,
462 .buf = b0,
463 .len = 2
464 },{
465 .addr = state->config->demod_address,
466 .flags = I2C_M_RD,
467 .buf = buf,
468 .len = count
469 }
470 };
471
472 status = i2c_transfer(state->i2c, msg, 2);
473 if (status != 2) {
474 if (status != -ERESTARTSYS)
475 printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
476 __func__, reg, count, status);
477 goto err;
478 }
479 /*
480 * Bug ID 9:
481 * access to 0xf2xx/0xf6xx
482 * must be followed by read from 0xf2ff/0xf6ff.
483 */
484 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
485 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
486 _stb0899_read_reg(state, (reg | 0x00ff));
487
Mauro Carvalho Chehaba8cd47d2016-10-13 15:39:45 -0300488 dprintk(state->verbose, FE_DEBUGREG, 1,
489 "%s [0x%04x]: %*ph", __func__, reg, count, buf);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300490
491 return 0;
492err:
493 return status < 0 ? status : -EREMOTEIO;
494}
495
Manu Abraham85eabac2008-01-25 20:28:46 -0300496int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300497{
498 int ret;
Mauro Carvalho Chehabba474642013-11-02 05:14:58 -0300499 u8 buf[MAX_XFER_SIZE];
Manu Abraham8bd135b2007-07-03 09:53:42 -0300500 struct i2c_msg i2c_msg = {
501 .addr = state->config->demod_address,
502 .flags = 0,
503 .buf = buf,
504 .len = 2 + count
505 };
506
Mauro Carvalho Chehabba474642013-11-02 05:14:58 -0300507 if (2 + count > sizeof(buf)) {
508 printk(KERN_WARNING
509 "%s: i2c wr reg=%04x: len=%d is too big!\n",
510 KBUILD_MODNAME, reg, count);
511 return -EINVAL;
512 }
513
Manu Abraham8bd135b2007-07-03 09:53:42 -0300514 buf[0] = reg >> 8;
515 buf[1] = reg & 0xff;
516 memcpy(&buf[2], data, count);
517
Mauro Carvalho Chehaba8cd47d2016-10-13 15:39:45 -0300518 dprintk(state->verbose, FE_DEBUGREG, 1,
519 "%s [0x%04x]: %*ph", __func__, reg, count, data);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300520 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
521
522 /*
523 * Bug ID 9:
524 * access to 0xf2xx/0xf6xx
525 * must be followed by read from 0xf2ff/0xf6ff.
526 */
527 if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
528 stb0899_read_reg(state, (reg | 0x00ff));
529
530 if (ret != 1) {
531 if (ret != -ERESTARTSYS)
Reinhard Nisslc615a272008-07-09 15:33:38 -0300532 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300533 reg, data[0], count, ret);
534 return ret < 0 ? ret : -EREMOTEIO;
535 }
536
537 return 0;
538}
539
540int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
541{
Arnd Bergmann3cd890d2017-11-30 11:55:46 -0500542 u8 tmp = data;
543 return stb0899_write_regs(state, reg, &tmp, 1);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300544}
545
546/*
547 * stb0899_get_mclk
548 * Get STB0899 master clock frequency
549 * ExtClk: external clock frequency (Hz)
550 */
551static u32 stb0899_get_mclk(struct stb0899_state *state)
552{
Manu Abraham72f78412007-11-10 16:59:16 -0300553 u32 mclk = 0, div = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300554
555 div = stb0899_read_reg(state, STB0899_NCOARSE);
556 mclk = (div + 1) * state->config->xtal_freq / 6;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300557 dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300558
559 return mclk;
560}
561
562/*
563 * stb0899_set_mclk
564 * Set STB0899 master Clock frequency
565 * Mclk: demodulator master clock
566 * ExtClk: external clock frequency (Hz)
567 */
568static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
569{
570 struct stb0899_internal *internal = &state->internal;
571 u8 mdiv = 0;
572
Reinhard Nisslc615a272008-07-09 15:33:38 -0300573 dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300574 mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300575 dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300576
577 stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
578 internal->master_clk = stb0899_get_mclk(state);
579
Reinhard Nisslc615a272008-07-09 15:33:38 -0300580 dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300581}
582
Manu Abrahamb168e352007-10-30 19:46:49 -0300583static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
584{
585 struct stb0899_config *config = state->config;
Manu Abraham043a68b2008-01-18 14:15:17 -0300586 const struct stb0899_postproc *postproc = config->postproc;
Manu Abrahamb168e352007-10-30 19:46:49 -0300587
588 /* post process event */
589 if (postproc) {
590 if (enable) {
Manu Abraham9efdd292007-10-30 20:05:59 -0300591 if (postproc[ctl].level == STB0899_GPIOPULLUP)
Manu Abrahamb168e352007-10-30 19:46:49 -0300592 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
593 else
594 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
595 } else {
Manu Abraham9efdd292007-10-30 20:05:59 -0300596 if (postproc[ctl].level == STB0899_GPIOPULLUP)
Manu Abrahamb168e352007-10-30 19:46:49 -0300597 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
598 else
599 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
600 }
601 }
602 return 0;
603}
604
Max Kellermannf686c142016-08-09 18:32:46 -0300605static void stb0899_detach(struct dvb_frontend *fe)
606{
607 struct stb0899_state *state = fe->demodulator_priv;
608
609 /* post process event */
610 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
611}
612
Manu Abraham8bd135b2007-07-03 09:53:42 -0300613static void stb0899_release(struct dvb_frontend *fe)
614{
615 struct stb0899_state *state = fe->demodulator_priv;
616
Reinhard Nisslc615a272008-07-09 15:33:38 -0300617 dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300618 kfree(state);
619}
620
621/*
622 * stb0899_get_alpha
623 * return: rolloff
624 */
625static int stb0899_get_alpha(struct stb0899_state *state)
626{
627 u8 mode_coeff;
628
629 mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
630
631 if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
632 return 20;
633 else
634 return 35;
635}
636
637/*
638 * stb0899_init_calc
639 */
640static void stb0899_init_calc(struct stb0899_state *state)
641{
642 struct stb0899_internal *internal = &state->internal;
643 int master_clk;
Marko Schluessler7d8f1e52007-10-23 19:56:18 -0300644 u8 agc[2];
Manu Abraham8bd135b2007-07-03 09:53:42 -0300645 u32 reg;
646
647 /* Read registers (in burst mode) */
Manu Abraham8bd135b2007-07-03 09:53:42 -0300648 stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
649
650 /* Initial calculations */
651 master_clk = stb0899_get_mclk(state);
652 internal->t_agc1 = 0;
653 internal->t_agc2 = 0;
654 internal->master_clk = master_clk;
655 internal->mclk = master_clk / 65536L;
656 internal->rolloff = stb0899_get_alpha(state);
657
658 /* DVBS2 Initial calculations */
659 /* Set AGC value to the middle */
660 internal->agc_gain = 8154;
661 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
662 STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
663 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
664
665 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
666 internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
667
668 internal->center_freq = 0;
669 internal->av_frame_coarse = 10;
670 internal->av_frame_fine = 20;
671 internal->step_size = 2;
672/*
673 if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
674 pParams->IQLocked = 0;
675 else
676 pParams->IQLocked = 1;
677*/
678}
679
680static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
681{
682 u8 reg = 0;
683 unsigned long start = jiffies;
684
685 while (1) {
686 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
687 if (!STB0899_GETFIELD(FIFOFULL, reg))
688 break;
Asaf Vertz3edd59a2014-12-14 03:49:47 -0300689 if (time_after(jiffies, start + timeout)) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300690 dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300691 return -ETIMEDOUT;
692 }
693 }
694
695 return 0;
696}
697
698static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
699{
700 struct stb0899_state *state = fe->demodulator_priv;
701 u8 reg, i;
702
Mauro Carvalho Chehabb9f62ff2014-11-05 09:34:12 -0200703 if (cmd->msg_len > sizeof(cmd->msg))
Manu Abraham8bd135b2007-07-03 09:53:42 -0300704 return -EINVAL;
705
706 /* enable FIFO precharge */
707 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
708 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
709 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
710 for (i = 0; i < cmd->msg_len; i++) {
711 /* wait for FIFO empty */
Hans Petter Selaskyaad04c72011-05-23 11:21:47 -0300712 if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300713 return -ETIMEDOUT;
714
715 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
716 }
717 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
718 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
719 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
Manu Abraham1697c8d2010-11-14 16:01:47 -0300720 msleep(100);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300721 return 0;
722}
723
724static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
725{
726 u8 reg = 0;
727 unsigned long start = jiffies;
728
729 while (!STB0899_GETFIELD(RXEND, reg)) {
730 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
Asaf Vertz3edd59a2014-12-14 03:49:47 -0300731 if (time_after(jiffies, start + timeout)) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300732 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300733 return -ETIMEDOUT;
734 }
735 msleep(10);
736 }
737
738 return 0;
739}
740
741static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
742{
743 struct stb0899_state *state = fe->demodulator_priv;
744 u8 reg, length = 0, i;
745 int result;
746
747 if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
748 return -ETIMEDOUT;
749
750 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
751 if (STB0899_GETFIELD(RXEND, reg)) {
752
753 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
754 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
755
756 if (length > sizeof (reply->msg)) {
757 result = -EOVERFLOW;
758 goto exit;
759 }
760 reply->msg_len = length;
761
762 /* extract data */
763 for (i = 0; i < length; i++)
764 reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
765 }
766
767 return 0;
768exit:
769
770 return result;
771}
772
773static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
774{
775 u8 reg = 0;
776 unsigned long start = jiffies;
777
778 while (!STB0899_GETFIELD(TXIDLE, reg)) {
779 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
Asaf Vertz3edd59a2014-12-14 03:49:47 -0300780 if (time_after(jiffies, start + timeout)) {
Reinhard Nisslc615a272008-07-09 15:33:38 -0300781 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300782 return -ETIMEDOUT;
783 }
784 msleep(10);
785 }
786 return 0;
787}
788
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300789static int stb0899_send_diseqc_burst(struct dvb_frontend *fe,
790 enum fe_sec_mini_cmd burst)
Manu Abraham8bd135b2007-07-03 09:53:42 -0300791{
792 struct stb0899_state *state = fe->demodulator_priv;
793 u8 reg, old_state;
794
795 /* wait for diseqc idle */
796 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
797 return -ETIMEDOUT;
798
799 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
800 old_state = reg;
801 /* set to burst mode */
Sigmund Augdald284e4f2009-01-26 19:08:48 -0300802 STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300803 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
804 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
805 switch (burst) {
806 case SEC_MINI_A:
807 /* unmodulated */
808 stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
809 break;
810 case SEC_MINI_B:
811 /* modulated */
812 stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
813 break;
814 }
815 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
816 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
817 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
818 /* wait for diseqc idle */
819 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
820 return -ETIMEDOUT;
821
822 /* restore state */
823 stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
824
825 return 0;
826}
827
Manu Abraham6c1022c2007-10-05 11:41:19 -0300828static int stb0899_diseqc_init(struct stb0899_state *state)
829{
Manu Abraham3f400922008-10-26 18:28:52 -0300830/*
Manu Abraham6c1022c2007-10-05 11:41:19 -0300831 struct dvb_diseqc_slave_reply rx_data;
Manu Abraham3f400922008-10-26 18:28:52 -0300832*/
Hans Verkuil5becbc52012-05-14 10:22:58 -0300833 u8 f22_tx, reg;
Manu Abraham6c1022c2007-10-05 11:41:19 -0300834
Manu Abraham3f400922008-10-26 18:28:52 -0300835 u32 mclk, tx_freq = 22000;/* count = 0, i; */
Manu Abraham6c1022c2007-10-05 11:41:19 -0300836 reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
837 STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
838 stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
839
840 /* disable Tx spy */
841 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
842 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
843 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
844
845 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
846 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
847 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
848
849 mclk = stb0899_get_mclk(state);
850 f22_tx = mclk / (tx_freq * 32);
851 stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
852 state->rx_freq = 20000;
Manu Abraham6c1022c2007-10-05 11:41:19 -0300853
854 return 0;
855}
Manu Abraham8bd135b2007-07-03 09:53:42 -0300856
857static int stb0899_sleep(struct dvb_frontend *fe)
858{
859 struct stb0899_state *state = fe->demodulator_priv;
Manu Abraham3f400922008-10-26 18:28:52 -0300860/*
Manu Abraham8bd135b2007-07-03 09:53:42 -0300861 u8 reg;
Manu Abraham3f400922008-10-26 18:28:52 -0300862*/
Reinhard Nisslc615a272008-07-09 15:33:38 -0300863 dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
Manu Abrahamb168e352007-10-30 19:46:49 -0300864 /* post process event */
865 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
866
Manu Abraham8bd135b2007-07-03 09:53:42 -0300867 return 0;
868}
869
870static int stb0899_wakeup(struct dvb_frontend *fe)
871{
872 int rc;
873 struct stb0899_state *state = fe->demodulator_priv;
874
Manu Abrahamde29eb82007-09-22 21:39:17 -0300875 if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
876 return rc;
877 /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
878 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
879 return rc;
880 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
881 return rc;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300882
Manu Abrahamb168e352007-10-30 19:46:49 -0300883 /* post process event */
884 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
885
Manu Abraham8bd135b2007-07-03 09:53:42 -0300886 return 0;
887}
888
889static int stb0899_init(struct dvb_frontend *fe)
890{
891 int i;
892 struct stb0899_state *state = fe->demodulator_priv;
893 struct stb0899_config *config = state->config;
894
Reinhard Nisslc615a272008-07-09 15:33:38 -0300895 dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300896
897 /* init device */
Reinhard Nisslc615a272008-07-09 15:33:38 -0300898 dprintk(state->verbose, FE_DEBUG, 1, "init device");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300899 for (i = 0; config->init_dev[i].address != 0xffff; i++)
900 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
901
Reinhard Nisslc615a272008-07-09 15:33:38 -0300902 dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300903 /* init S2 demod */
904 for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
905 stb0899_write_s2reg(state, STB0899_S2DEMOD,
906 config->init_s2_demod[i].base_address,
907 config->init_s2_demod[i].offset,
908 config->init_s2_demod[i].data);
909
Reinhard Nisslc615a272008-07-09 15:33:38 -0300910 dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300911 /* init S1 demod */
912 for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
913 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
914
Reinhard Nisslc615a272008-07-09 15:33:38 -0300915 dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300916 /* init S2 fec */
917 for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
918 stb0899_write_s2reg(state, STB0899_S2FEC,
919 config->init_s2_fec[i].base_address,
920 config->init_s2_fec[i].offset,
921 config->init_s2_fec[i].data);
922
Reinhard Nisslc615a272008-07-09 15:33:38 -0300923 dprintk(state->verbose, FE_DEBUG, 1, "init TST");
Manu Abraham8bd135b2007-07-03 09:53:42 -0300924 /* init test */
925 for (i = 0; config->init_tst[i].address != 0xffff; i++)
926 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
927
928 stb0899_init_calc(state);
Manu Abraham6c1022c2007-10-05 11:41:19 -0300929 stb0899_diseqc_init(state);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300930
931 return 0;
932}
933
934static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
935{
936 int res = 0;
937 int min = 0, med;
938
939 if (val < tab[min].read)
940 res = tab[min].real;
941 else if (val >= tab[max].read)
942 res = tab[max].real;
943 else {
944 while ((max - min) > 1) {
945 med = (max + min) / 2;
946 if (val >= tab[min].read && val < tab[med].read)
947 max = med;
948 else
949 min = med;
950 }
951 res = ((val - tab[min].read) *
952 (tab[max].real - tab[min].real) /
953 (tab[max].read - tab[min].read)) +
954 tab[min].real;
955 }
956
957 return res;
958}
959
960static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
961{
962 struct stb0899_state *state = fe->demodulator_priv;
963 struct stb0899_internal *internal = &state->internal;
964
965 int val;
966 u32 reg;
Klaus Schmidinger0e377812012-01-24 05:33:53 -0300967 *strength = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300968 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -0300969 case SYS_DVBS:
970 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -0300971 if (internal->lock) {
972 reg = stb0899_read_reg(state, STB0899_VSTATUS);
973 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
974
975 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
976 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
977
978 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
979 *strength += 750;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300980 dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300981 val & 0xff, *strength);
982 }
983 }
984 break;
Manu Abraham3f400922008-10-26 18:28:52 -0300985 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -0300986 if (internal->lock) {
Andreas Regelef3d23de2012-02-28 14:40:44 -0300987 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300988 val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
989
990 *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
Klaus Schmidinger0e377812012-01-24 05:33:53 -0300991 *strength += 950;
Reinhard Nisslc615a272008-07-09 15:33:38 -0300992 dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
Manu Abraham8bd135b2007-07-03 09:53:42 -0300993 val & 0x3fff, *strength);
994 }
995 break;
996 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -0300997 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -0300998 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300999 }
1000
1001 return 0;
1002}
1003
1004static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
1005{
1006 struct stb0899_state *state = fe->demodulator_priv;
1007 struct stb0899_internal *internal = &state->internal;
1008
1009 unsigned int val, quant, quantn = -1, est, estn = -1;
1010 u8 buf[2];
1011 u32 reg;
1012
Klaus Schmidinger0e377812012-01-24 05:33:53 -03001013 *snr = 0;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001014 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1015 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001016 case SYS_DVBS:
1017 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001018 if (internal->lock) {
1019 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1020
1021 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1022 val = MAKEWORD16(buf[0], buf[1]);
1023
1024 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001025 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 -03001026 buf[0], buf[1], val, *snr);
1027 }
1028 }
1029 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001030 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001031 if (internal->lock) {
1032 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1033 quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1034 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1035 est = STB0899_GETFIELD(ESN0_EST, reg);
1036 if (est == 1)
1037 val = 301; /* C/N = 30.1 dB */
1038 else if (est == 2)
1039 val = 270; /* C/N = 27.0 dB */
1040 else {
1041 /* quantn = 100 * log(quant^2) */
1042 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1043 /* estn = 100 * log(est) */
1044 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1045 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1046 val = (quantn - estn) / 10;
1047 }
1048 *snr = val;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001049 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 -03001050 quant, quantn, est, estn, val);
1051 }
1052 break;
1053 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001054 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001055 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001056 }
1057
1058 return 0;
1059}
1060
1061static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1062{
1063 struct stb0899_state *state = fe->demodulator_priv;
1064 struct stb0899_internal *internal = &state->internal;
1065 u8 reg;
1066 *status = 0;
1067
1068 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001069 case SYS_DVBS:
1070 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001071 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1072 if (internal->lock) {
1073 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1074 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1075 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
Andreas Regel91caad32012-02-28 14:40:40 -03001076 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001077
1078 reg = stb0899_read_reg(state, STB0899_PLPARM);
1079 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1080 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1081 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
Manu Abrahamb168e352007-10-30 19:46:49 -03001082 /* post process event */
1083 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001084 }
1085 }
1086 }
1087 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001088 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001089 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1090 if (internal->lock) {
1091 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1092 if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1093 *status |= FE_HAS_CARRIER;
1094 dprintk(state->verbose, FE_DEBUG, 1,
1095 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1096
1097 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1098 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1099 *status |= FE_HAS_LOCK;
1100 dprintk(state->verbose, FE_DEBUG, 1,
1101 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1102
1103 }
1104 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1105 *status |= FE_HAS_VITERBI;
1106 dprintk(state->verbose, FE_DEBUG, 1,
1107 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1108 }
1109 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1110 *status |= FE_HAS_SYNC;
1111 dprintk(state->verbose, FE_DEBUG, 1,
1112 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
Manu Abrahamb168e352007-10-30 19:46:49 -03001113 /* post process event */
1114 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001115 }
1116 }
1117 }
1118 break;
1119 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001120 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001121 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001122 }
1123 return 0;
1124}
1125
1126/*
1127 * stb0899_get_error
1128 * viterbi error for DVB-S/DSS
1129 * packet error for DVB-S2
1130 * Bit Error Rate or Packet Error Rate * 10 ^ 7
1131 */
1132static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1133{
1134 struct stb0899_state *state = fe->demodulator_priv;
1135 struct stb0899_internal *internal = &state->internal;
1136
1137 u8 lsb, msb;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001138
1139 *ber = 0;
1140
1141 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001142 case SYS_DVBS:
1143 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001144 if (internal->lock) {
Klaus Schmidingerfdaaee62012-06-02 11:04:17 -03001145 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1146 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1147 *ber = MAKEWORD16(msb, lsb);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001148 /* Viterbi Check */
1149 if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1150 /* Error Rate */
1151 *ber *= 9766;
1152 /* ber = ber * 10 ^ 7 */
1153 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1154 *ber /= 8;
1155 }
1156 }
1157 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001158 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001159 if (internal->lock) {
Klaus Schmidingerfdaaee62012-06-02 11:04:17 -03001160 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1161 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1162 *ber = MAKEWORD16(msb, lsb);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001163 /* ber = ber * 10 ^ 7 */
1164 *ber *= 10000000;
1165 *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1166 }
1167 break;
1168 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001169 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
Manu Abraham417dd692007-10-10 06:12:13 -03001170 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001171 }
1172
1173 return 0;
1174}
1175
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001176static int stb0899_set_voltage(struct dvb_frontend *fe,
1177 enum fe_sec_voltage voltage)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001178{
1179 struct stb0899_state *state = fe->demodulator_priv;
1180
1181 switch (voltage) {
1182 case SEC_VOLTAGE_13:
1183 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1184 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1185 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1186 break;
1187 case SEC_VOLTAGE_18:
1188 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1189 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1190 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1191 break;
1192 case SEC_VOLTAGE_OFF:
1193 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1194 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1195 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1196 break;
1197 default:
1198 return -EINVAL;
1199 }
1200
1201 return 0;
1202}
1203
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001204static int stb0899_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001205{
1206 struct stb0899_state *state = fe->demodulator_priv;
1207 struct stb0899_internal *internal = &state->internal;
1208
Manu Abrahambaa40e42007-10-05 09:04:32 -03001209 u8 div, reg;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001210
1211 /* wait for diseqc idle */
1212 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1213 return -ETIMEDOUT;
1214
1215 switch (tone) {
1216 case SEC_TONE_ON:
1217 div = (internal->master_clk / 100) / 5632;
1218 div = (div + 5) / 10;
1219 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
Manu Abrahambaa40e42007-10-05 09:04:32 -03001220 reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1221 STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1222 stb0899_write_reg(state, STB0899_ACRPRESC, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001223 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1224 break;
1225 case SEC_TONE_OFF:
1226 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1227 break;
1228 default:
Manu Abraham417dd692007-10-10 06:12:13 -03001229 return -EINVAL;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001230 }
1231 return 0;
1232}
1233
Manu Abraham40e8ce32008-02-03 19:37:02 -03001234int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001235{
1236 int i2c_stat;
1237 struct stb0899_state *state = fe->demodulator_priv;
1238
1239 i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1240 if (i2c_stat < 0)
1241 goto err;
1242
1243 if (enable) {
1244 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1245 i2c_stat |= STB0899_I2CTON;
1246 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1247 goto err;
Manu Abraham40e8ce32008-02-03 19:37:02 -03001248 } else {
1249 dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
1250 i2c_stat &= ~STB0899_I2CTON;
1251 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1252 goto err;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001253 }
1254 return 0;
1255err:
Manu Abraham40e8ce32008-02-03 19:37:02 -03001256 dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001257 return -EREMOTEIO;
1258}
1259
1260
1261static inline void CONVERT32(u32 x, char *str)
1262{
1263 *str++ = (x >> 24) & 0xff;
1264 *str++ = (x >> 16) & 0xff;
1265 *str++ = (x >> 8) & 0xff;
1266 *str++ = (x >> 0) & 0xff;
1267 *str = '\0';
1268}
1269
Mauro Carvalho Chehab5506bcb2012-10-27 11:25:28 -03001270static int stb0899_get_dev_id(struct stb0899_state *state)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001271{
1272 u8 chip_id, release;
1273 u16 id;
1274 u32 demod_ver = 0, fec_ver = 0;
Reinhard Nissl56137412008-01-21 16:17:48 -03001275 char demod_str[5] = { 0 };
1276 char fec_str[5] = { 0 };
Manu Abraham8bd135b2007-07-03 09:53:42 -03001277
1278 id = stb0899_read_reg(state, STB0899_DEV_ID);
1279 dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1280 chip_id = STB0899_GETFIELD(CHIP_ID, id);
1281 release = STB0899_GETFIELD(CHIP_REL, id);
1282
1283 dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1284 chip_id, release);
1285
1286 CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1287
1288 demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1289 dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1290 CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1291 fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1292 if (! (chip_id > 0)) {
1293 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1294
1295 return -ENODEV;
1296 }
1297 dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1298
1299 return 0;
1300}
1301
Manu Abrahame399a782008-03-08 20:28:09 -03001302static void stb0899_set_delivery(struct stb0899_state *state)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001303{
1304 u8 reg;
1305 u8 stop_clk[2];
1306
1307 stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1308 stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1309
1310 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001311 case SYS_DVBS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001312 dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001313 /* FECM/Viterbi ON */
1314 reg = stb0899_read_reg(state, STB0899_FECM);
1315 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1316 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1317 stb0899_write_reg(state, STB0899_FECM, reg);
1318
1319 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1320 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1321 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1322 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1323
1324 reg = stb0899_read_reg(state, STB0899_TSTRES);
1325 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1326 stb0899_write_reg(state, STB0899_TSTRES, reg);
1327
1328 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1329 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1330 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1331
1332 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1333 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1334
1335 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
Manu Abraham18527be2008-01-25 18:25:13 -03001336 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001337
1338 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1339 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001340 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001341 /* FECM/Viterbi OFF */
1342 reg = stb0899_read_reg(state, STB0899_FECM);
1343 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1344 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1345 stb0899_write_reg(state, STB0899_FECM, reg);
1346
1347 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1348 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1349 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1350 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1351
1352 reg = stb0899_read_reg(state, STB0899_TSTRES);
1353 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1354 stb0899_write_reg(state, STB0899_TSTRES, reg);
1355
1356 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1357 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1358 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1359
1360 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1361 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1362
1363 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1364 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1365
1366 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1367 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001368 case SYS_DSS:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001369 /* FECM/Viterbi ON */
1370 reg = stb0899_read_reg(state, STB0899_FECM);
1371 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1372 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1373 stb0899_write_reg(state, STB0899_FECM, reg);
1374
1375 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1376 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1377 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1378
1379 reg = stb0899_read_reg(state, STB0899_TSTRES);
1380 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1381 stb0899_write_reg(state, STB0899_TSTRES, reg);
1382
1383 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1384 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1385 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1386
1387 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1388 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1389
1390 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1391
1392 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1393 break;
1394 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001395 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001396 break;
1397 }
1398 STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1399 stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1400}
1401
1402/*
1403 * stb0899_set_iterations
1404 * set the LDPC iteration scale function
1405 */
1406static void stb0899_set_iterations(struct stb0899_state *state)
1407{
1408 struct stb0899_internal *internal = &state->internal;
1409 struct stb0899_config *config = state->config;
1410
1411 s32 iter_scale;
1412 u32 reg;
1413
1414 iter_scale = 17 * (internal->master_clk / 1000);
1415 iter_scale += 410000;
1416 iter_scale /= (internal->srate / 1000000);
1417 iter_scale /= 1000;
1418
1419 if (iter_scale > config->ldpc_max_iter)
1420 iter_scale = config->ldpc_max_iter;
1421
Lutz Sammer699cc192011-05-04 07:27:31 -03001422 reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001423 STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
Lutz Sammer699cc192011-05-04 07:27:31 -03001424 stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001425}
1426
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -03001427static enum dvbfe_search stb0899_search(struct dvb_frontend *fe)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001428{
1429 struct stb0899_state *state = fe->demodulator_priv;
1430 struct stb0899_params *i_params = &state->params;
1431 struct stb0899_internal *internal = &state->internal;
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001432 struct stb0899_config *config = state->config;
Manu Abraham3f400922008-10-26 18:28:52 -03001433 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001434
1435 u32 SearchRange, gain;
1436
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -03001437 i_params->freq = props->frequency;
1438 i_params->srate = props->symbol_rate;
Manu Abraham3f400922008-10-26 18:28:52 -03001439 state->delsys = props->delivery_system;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001440 dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001441
Manu Abraham763fbaf2008-01-18 11:28:48 -03001442 SearchRange = 10000000;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001443 dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001444 /* checking Search Range is meaningless for a fixed 3 Mhz */
1445 if (INRANGE(i_params->srate, 1000000, 45000000)) {
Reinhard Nisslc615a272008-07-09 15:33:38 -03001446 dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
Manu Abrahame399a782008-03-08 20:28:09 -03001447 stb0899_set_delivery(state);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001448
1449 if (state->config->tuner_set_rfsiggain) {
1450 if (internal->srate > 15000000)
Manu Abraham3f400922008-10-26 18:28:52 -03001451 gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
Manu Abraham8bd135b2007-07-03 09:53:42 -03001452 else if (internal->srate > 5000000)
Manu Abraham3f400922008-10-26 18:28:52 -03001453 gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
Manu Abraham8bd135b2007-07-03 09:53:42 -03001454 else
Manu Abraham3f400922008-10-26 18:28:52 -03001455 gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
Manu Abraham8bd135b2007-07-03 09:53:42 -03001456 state->config->tuner_set_rfsiggain(fe, gain);
1457 }
1458
1459 if (i_params->srate <= 5000000)
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001460 stb0899_set_mclk(state, config->lo_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001461 else
Manu Abrahamb91a7cb2008-03-04 19:19:58 -03001462 stb0899_set_mclk(state, config->hi_clk);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001463
1464 switch (state->delsys) {
Manu Abraham3f400922008-10-26 18:28:52 -03001465 case SYS_DVBS:
1466 case SYS_DSS:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001467 dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001468 internal->freq = i_params->freq;
1469 internal->srate = i_params->srate;
1470 /*
1471 * search = user search range +
1472 * 500Khz +
1473 * 2 * Tuner_step_size +
1474 * 10% of the symbol rate
1475 */
1476 internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
1477 internal->derot_percent = 30;
1478
1479 /* What to do for tuners having no bandwidth setup ? */
Manu Abraham40e8ce32008-02-03 19:37:02 -03001480 /* enable tuner I/O */
1481 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1482
Manu Abraham8bd135b2007-07-03 09:53:42 -03001483 if (state->config->tuner_set_bandwidth)
Reinhard Nissl02ec9d82008-01-18 20:09:52 -03001484 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001485 if (state->config->tuner_get_bandwidth)
1486 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
Manu Abraham40e8ce32008-02-03 19:37:02 -03001487
1488 /* disable tuner I/O */
1489 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1490
Manu Abraham8bd135b2007-07-03 09:53:42 -03001491 /* Set DVB-S1 AGC */
1492 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1493
1494 /* Run the search algorithm */
Reinhard Nisslc615a272008-07-09 15:33:38 -03001495 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001496 if (stb0899_dvbs_algo(state) == RANGEOK) {
1497 internal->lock = 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001498 dprintk(state->verbose, FE_DEBUG, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001499 "-------------------------------------> DVB-S LOCK !");
1500
1501// stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
1502// internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1503// internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
Reinhard Nisslc615a272008-07-09 15:33:38 -03001504// dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1505// dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001506
1507 return DVBFE_ALGO_SEARCH_SUCCESS;
1508 } else {
1509 internal->lock = 0;
1510
1511 return DVBFE_ALGO_SEARCH_FAILED;
1512 }
1513 break;
Manu Abraham3f400922008-10-26 18:28:52 -03001514 case SYS_DVBS2:
Manu Abraham8bd135b2007-07-03 09:53:42 -03001515 internal->freq = i_params->freq;
1516 internal->srate = i_params->srate;
1517 internal->srch_range = SearchRange;
1518
Manu Abraham40e8ce32008-02-03 19:37:02 -03001519 /* enable tuner I/O */
1520 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1521
Manu Abraham8bd135b2007-07-03 09:53:42 -03001522 if (state->config->tuner_set_bandwidth)
Manu Abraham763fbaf2008-01-18 11:28:48 -03001523 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
Manu Abraham8bd135b2007-07-03 09:53:42 -03001524 if (state->config->tuner_get_bandwidth)
1525 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1526
Manu Abraham40e8ce32008-02-03 19:37:02 -03001527 /* disable tuner I/O */
1528 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1529
Manu Abraham8bd135b2007-07-03 09:53:42 -03001530// pParams->SpectralInv = pSearch->IQ_Inversion;
1531
1532 /* Set DVB-S2 AGC */
1533 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1534
1535 /* Set IterScale =f(MCLK,SYMB) */
1536 stb0899_set_iterations(state);
1537
1538 /* Run the search algorithm */
Reinhard Nisslc615a272008-07-09 15:33:38 -03001539 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001540 if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
1541 internal->lock = 1;
Reinhard Nisslc615a272008-07-09 15:33:38 -03001542 dprintk(state->verbose, FE_DEBUG, 1,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001543 "-------------------------------------> DVB-S2 LOCK !");
1544
1545// stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
1546// internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1547// internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1548
1549 return DVBFE_ALGO_SEARCH_SUCCESS;
1550 } else {
1551 internal->lock = 0;
1552
1553 return DVBFE_ALGO_SEARCH_FAILED;
1554 }
1555 break;
1556 default:
Reinhard Nisslc615a272008-07-09 15:33:38 -03001557 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
Manu Abraham8bd135b2007-07-03 09:53:42 -03001558 return DVBFE_ALGO_SEARCH_INVALID;
1559 }
1560 }
1561
1562 return DVBFE_ALGO_SEARCH_ERROR;
1563}
Manu Abraham8bd135b2007-07-03 09:53:42 -03001564
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02001565static int stb0899_get_frontend(struct dvb_frontend *fe,
1566 struct dtv_frontend_properties *p)
Manu Abraham8bd135b2007-07-03 09:53:42 -03001567{
1568 struct stb0899_state *state = fe->demodulator_priv;
1569 struct stb0899_internal *internal = &state->internal;
1570
Manu Abraham3f400922008-10-26 18:28:52 -03001571 dprintk(state->verbose, FE_DEBUG, 1, "Get params");
Mauro Carvalho Chehab57158362011-12-26 14:16:52 -03001572 p->symbol_rate = internal->srate;
Reinhard Nissl226143f2012-08-18 08:59:57 -03001573 p->frequency = internal->freq;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001574
1575 return 0;
1576}
1577
1578static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1579{
1580 return DVBFE_ALGO_CUSTOM;
1581}
1582
Max Kellermannbd336e62016-08-09 18:32:21 -03001583static const struct dvb_frontend_ops stb0899_ops = {
Mauro Carvalho Chehab7581e612012-01-01 16:11:18 -03001584 .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
Manu Abraham8bd135b2007-07-03 09:53:42 -03001585 .info = {
Manu Abraham3f400922008-10-26 18:28:52 -03001586 .name = "STB0899 Multistandard",
Manu Abraham3f400922008-10-26 18:28:52 -03001587 .frequency_min = 950000,
1588 .frequency_max = 2150000,
1589 .frequency_stepsize = 0,
1590 .frequency_tolerance = 0,
Mauro Carvalho Chehab07ecbf22013-07-05 10:31:43 -03001591 .symbol_rate_min = 5000000,
Manu Abraham3f400922008-10-26 18:28:52 -03001592 .symbol_rate_max = 45000000,
1593
1594 .caps = FE_CAN_INVERSION_AUTO |
1595 FE_CAN_FEC_AUTO |
Klaus Schmidingerfaed4aa2008-12-31 14:13:56 -03001596 FE_CAN_2G_MODULATION |
Manu Abraham3f400922008-10-26 18:28:52 -03001597 FE_CAN_QPSK
Manu Abraham8bd135b2007-07-03 09:53:42 -03001598 },
1599
Max Kellermannf686c142016-08-09 18:32:46 -03001600 .detach = stb0899_detach,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001601 .release = stb0899_release,
1602 .init = stb0899_init,
1603 .sleep = stb0899_sleep,
1604// .wakeup = stb0899_wakeup,
1605
1606 .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001607
1608 .get_frontend_algo = stb0899_frontend_algo,
1609 .search = stb0899_search,
Mauro Carvalho Chehab57158362011-12-26 14:16:52 -03001610 .get_frontend = stb0899_get_frontend,
Manu Abraham3f400922008-10-26 18:28:52 -03001611
Manu Abraham8bd135b2007-07-03 09:53:42 -03001612
1613 .read_status = stb0899_read_status,
1614 .read_snr = stb0899_read_snr,
1615 .read_signal_strength = stb0899_read_signal_strength,
Manu Abraham8bd135b2007-07-03 09:53:42 -03001616 .read_ber = stb0899_read_ber,
1617
1618 .set_voltage = stb0899_set_voltage,
1619 .set_tone = stb0899_set_tone,
1620
1621 .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
1622 .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
1623 .diseqc_send_burst = stb0899_send_diseqc_burst,
1624};
1625
1626struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1627{
1628 struct stb0899_state *state = NULL;
1629
1630 state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1631 if (state == NULL)
1632 goto error;
1633
Reinhard Nisslc615a272008-07-09 15:33:38 -03001634 state->verbose = &verbose;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001635 state->config = config;
1636 state->i2c = i2c;
1637 state->frontend.ops = stb0899_ops;
1638 state->frontend.demodulator_priv = state;
Reinhard Nißl0c1d2b12013-06-02 14:59:00 -03001639 /* use configured inversion as default -- we'll later autodetect inversion */
1640 state->internal.inversion = config->inversion;
Manu Abraham8bd135b2007-07-03 09:53:42 -03001641
1642 stb0899_wakeup(&state->frontend);
1643 if (stb0899_get_dev_id(state) == -ENODEV) {
1644 printk("%s: Exiting .. !\n", __func__);
1645 goto error;
1646 }
1647
1648 printk("%s: Attaching STB0899 \n", __func__);
1649 return &state->frontend;
1650
1651error:
1652 kfree(state);
1653 return NULL;
1654}
1655EXPORT_SYMBOL(stb0899_attach);
1656MODULE_PARM_DESC(verbose, "Set Verbosity level");
1657MODULE_AUTHOR("Manu Abraham");
1658MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1659MODULE_LICENSE("GPL");