Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     STB0899 Multistandard Frontend driver
0004     Copyright (C) Manu Abraham (abraham.manu@gmail.com)
0005 
0006     Copyright (C) ST Microelectronics
0007 
0008 */
0009 
0010 #include <linux/init.h>
0011 #include <linux/jiffies.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/string.h>
0016 
0017 #include <linux/dvb/frontend.h>
0018 #include <media/dvb_frontend.h>
0019 
0020 #include "stb0899_drv.h"
0021 #include "stb0899_priv.h"
0022 #include "stb0899_reg.h"
0023 
0024 /* Max transfer size done by I2C transfer functions */
0025 #define MAX_XFER_SIZE  64
0026 
0027 static unsigned int verbose = 0;//1;
0028 module_param(verbose, int, 0644);
0029 
0030 /* C/N in dB/10, NIRM/NIRL */
0031 static const struct stb0899_tab stb0899_cn_tab[] = {
0032     { 200,  2600 },
0033     { 190,  2700 },
0034     { 180,  2860 },
0035     { 170,  3020 },
0036     { 160,  3210 },
0037     { 150,  3440 },
0038     { 140,  3710 },
0039     { 130,  4010 },
0040     { 120,  4360 },
0041     { 110,  4740 },
0042     { 100,  5190 },
0043     { 90,   5670 },
0044     { 80,   6200 },
0045     { 70,   6770 },
0046     { 60,   7360 },
0047     { 50,   7970 },
0048     { 40,   8250 },
0049     { 30,   9000 },
0050     { 20,   9450 },
0051     { 15,   9600 },
0052 };
0053 
0054 /* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
0055  * As measured, connected to a modulator.
0056  * -8.0 to -50.0 dBm directly connected,
0057  * -52.0 to -74.8 with extra attenuation.
0058  * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
0059  * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
0060  */
0061 static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
0062     { -750, -128 },
0063     { -748,  -94 },
0064     { -745,  -92 },
0065     { -735,  -90 },
0066     { -720,  -87 },
0067     { -670,  -77 },
0068     { -640,  -70 },
0069     { -610,  -62 },
0070     { -600,  -60 },
0071     { -590,  -56 },
0072     { -560,  -41 },
0073     { -540,  -25 },
0074     { -530,  -17 },
0075     { -520,  -11 },
0076     { -500,    1 },
0077     { -490,    6 },
0078     { -480,   10 },
0079     { -440,   22 },
0080     { -420,   27 },
0081     { -400,   31 },
0082     { -380,   34 },
0083     { -340,   40 },
0084     { -320,   43 },
0085     { -280,   48 },
0086     { -250,   52 },
0087     { -230,   55 },
0088     { -180,   61 },
0089     { -140,   66 },
0090     {  -90,   73 },
0091     {  -80,   74 },
0092     {  500,  127 }
0093 };
0094 
0095 /* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
0096  * As measured, connected to a modulator.
0097  * -8.0 to -50.1 dBm directly connected,
0098  * -53.0 to -76.6 with extra attenuation.
0099  * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
0100  * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
0101  */
0102 static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
0103     {  700,     0 },
0104     {  -80,  3217 },
0105     { -150,  3893 },
0106     { -190,  4217 },
0107     { -240,  4621 },
0108     { -280,  4945 },
0109     { -320,  5273 },
0110     { -350,  5545 },
0111     { -370,  5741 },
0112     { -410,  6147 },
0113     { -450,  6671 },
0114     { -490,  7413 },
0115     { -501,  7665 },
0116     { -530,  8767 },
0117     { -560, 10219 },
0118     { -580, 10939 },
0119     { -590, 11518 },
0120     { -600, 11723 },
0121     { -650, 12659 },
0122     { -690, 13219 },
0123     { -730, 13645 },
0124     { -750, 13909 },
0125     { -766, 14153 },
0126     { -950, 16383 }
0127 };
0128 
0129 /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
0130 static struct stb0899_tab stb0899_quant_tab[] = {
0131     {    0,     0 },
0132     {    0,   100 },
0133     {  600,   200 },
0134     {  950,   299 },
0135     { 1200,   398 },
0136     { 1400,   501 },
0137     { 1560,   603 },
0138     { 1690,   700 },
0139     { 1810,   804 },
0140     { 1910,   902 },
0141     { 2000,  1000 },
0142     { 2080,  1096 },
0143     { 2160,  1202 },
0144     { 2230,  1303 },
0145     { 2350,  1496 },
0146     { 2410,  1603 },
0147     { 2460,  1698 },
0148     { 2510,  1799 },
0149     { 2600,  1995 },
0150     { 2650,  2113 },
0151     { 2690,  2213 },
0152     { 2720,  2291 },
0153     { 2760,  2399 },
0154     { 2800,  2512 },
0155     { 2860,  2692 },
0156     { 2930,  2917 },
0157     { 2960,  3020 },
0158     { 3010,  3199 },
0159     { 3040,  3311 },
0160     { 3060,  3388 },
0161     { 3120,  3631 },
0162     { 3190,  3936 },
0163     { 3400,  5012 },
0164     { 3610,  6383 },
0165     { 3800,  7943 },
0166     { 4210, 12735 },
0167     { 4500, 17783 },
0168     { 4690, 22131 },
0169     { 4810, 25410 }
0170 };
0171 
0172 /* DVB-S2 Es/N0 estimate in dB/100 vs read value */
0173 static struct stb0899_tab stb0899_est_tab[] = {
0174     {    0,      0 },
0175     {    0,      1 },
0176     {  301,      2 },
0177     { 1204,     16 },
0178     { 1806,     64 },
0179     { 2408,    256 },
0180     { 2709,    512 },
0181     { 3010,   1023 },
0182     { 3311,   2046 },
0183     { 3612,   4093 },
0184     { 3823,   6653 },
0185     { 3913,   8185 },
0186     { 4010,  10233 },
0187     { 4107,  12794 },
0188     { 4214,  16368 },
0189     { 4266,  18450 },
0190     { 4311,  20464 },
0191     { 4353,  22542 },
0192     { 4391,  24604 },
0193     { 4425,  26607 },
0194     { 4457,  28642 },
0195     { 4487,  30690 },
0196     { 4515,  32734 },
0197     { 4612,  40926 },
0198     { 4692,  49204 },
0199     { 4816,  65464 },
0200     { 4913,  81846 },
0201     { 4993,  98401 },
0202     { 5060, 114815 },
0203     { 5118, 131220 },
0204     { 5200, 158489 },
0205     { 5300, 199526 },
0206     { 5400, 251189 },
0207     { 5500, 316228 },
0208     { 5600, 398107 },
0209     { 5720, 524807 },
0210     { 5721, 526017 },
0211 };
0212 
0213 static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
0214 {
0215     int ret;
0216 
0217     u8 b0[] = { reg >> 8, reg & 0xff };
0218     u8 buf;
0219 
0220     struct i2c_msg msg[] = {
0221         {
0222             .addr   = state->config->demod_address,
0223             .flags  = 0,
0224             .buf    = b0,
0225             .len    = 2
0226         },{
0227             .addr   = state->config->demod_address,
0228             .flags  = I2C_M_RD,
0229             .buf    = &buf,
0230             .len    = 1
0231         }
0232     };
0233 
0234     ret = i2c_transfer(state->i2c, msg, 2);
0235     if (ret != 2) {
0236         if (ret != -ERESTARTSYS)
0237             dprintk(state->verbose, FE_ERROR, 1,
0238                 "Read error, Reg=[0x%02x], Status=%d",
0239                 reg, ret);
0240 
0241         return ret < 0 ? ret : -EREMOTEIO;
0242     }
0243     if (unlikely(*state->verbose >= FE_DEBUGREG))
0244         dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
0245             reg, buf);
0246 
0247     return (unsigned int)buf;
0248 }
0249 
0250 int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
0251 {
0252     int result;
0253 
0254     result = _stb0899_read_reg(state, reg);
0255     /*
0256      * Bug ID 9:
0257      * access to 0xf2xx/0xf6xx
0258      * must be followed by read from 0xf2ff/0xf6ff.
0259      */
0260     if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
0261         (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
0262         _stb0899_read_reg(state, (reg | 0x00ff));
0263 
0264     return result;
0265 }
0266 
0267 u32 _stb0899_read_s2reg(struct stb0899_state *state,
0268             u32 stb0899_i2cdev,
0269             u32 stb0899_base_addr,
0270             u16 stb0899_reg_offset)
0271 {
0272     int status;
0273     u32 data;
0274     u8 buf[7] = { 0 };
0275     u16 tmpaddr;
0276 
0277     u8 buf_0[] = {
0278         GETBYTE(stb0899_i2cdev, BYTE1),     /* 0xf3 S2 Base Address (MSB)   */
0279         GETBYTE(stb0899_i2cdev, BYTE0),     /* 0xfc S2 Base Address (LSB)   */
0280         GETBYTE(stb0899_base_addr, BYTE0),  /* 0x00 Base Address (LSB)  */
0281         GETBYTE(stb0899_base_addr, BYTE1),  /* 0x04 Base Address (LSB)  */
0282         GETBYTE(stb0899_base_addr, BYTE2),  /* 0x00 Base Address (MSB)  */
0283         GETBYTE(stb0899_base_addr, BYTE3),  /* 0x00 Base Address (MSB)  */
0284     };
0285     u8 buf_1[] = {
0286         0x00,   /* 0xf3 Reg Offset  */
0287         0x00,   /* 0x44 Reg Offset  */
0288     };
0289 
0290     struct i2c_msg msg_0 = {
0291         .addr   = state->config->demod_address,
0292         .flags  = 0,
0293         .buf    = buf_0,
0294         .len    = 6
0295     };
0296 
0297     struct i2c_msg msg_1 = {
0298         .addr   = state->config->demod_address,
0299         .flags  = 0,
0300         .buf    = buf_1,
0301         .len    = 2
0302     };
0303 
0304     struct i2c_msg msg_r = {
0305         .addr   = state->config->demod_address,
0306         .flags  = I2C_M_RD,
0307         .buf    = buf,
0308         .len    = 4
0309     };
0310 
0311     tmpaddr = stb0899_reg_offset & 0xff00;
0312     if (!(stb0899_reg_offset & 0x8))
0313         tmpaddr = stb0899_reg_offset | 0x20;
0314 
0315     buf_1[0] = GETBYTE(tmpaddr, BYTE1);
0316     buf_1[1] = GETBYTE(tmpaddr, BYTE0);
0317 
0318     status = i2c_transfer(state->i2c, &msg_0, 1);
0319     if (status < 1) {
0320         if (status != -ERESTARTSYS)
0321             printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
0322                    __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
0323 
0324         goto err;
0325     }
0326 
0327     /* Dummy    */
0328     status = i2c_transfer(state->i2c, &msg_1, 1);
0329     if (status < 1)
0330         goto err;
0331 
0332     status = i2c_transfer(state->i2c, &msg_r, 1);
0333     if (status < 1)
0334         goto err;
0335 
0336     buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
0337     buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
0338 
0339     /* Actual   */
0340     status = i2c_transfer(state->i2c, &msg_1, 1);
0341     if (status < 1) {
0342         if (status != -ERESTARTSYS)
0343             printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
0344                    __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
0345         goto err;
0346     }
0347 
0348     status = i2c_transfer(state->i2c, &msg_r, 1);
0349     if (status < 1) {
0350         if (status != -ERESTARTSYS)
0351             printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
0352                    __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
0353         return status < 0 ? status : -EREMOTEIO;
0354     }
0355 
0356     data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
0357     if (unlikely(*state->verbose >= FE_DEBUGREG))
0358         printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
0359                __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
0360 
0361     return data;
0362 
0363 err:
0364     return status < 0 ? status : -EREMOTEIO;
0365 }
0366 
0367 int stb0899_write_s2reg(struct stb0899_state *state,
0368             u32 stb0899_i2cdev,
0369             u32 stb0899_base_addr,
0370             u16 stb0899_reg_offset,
0371             u32 stb0899_data)
0372 {
0373     int status;
0374 
0375     /* Base Address Setup   */
0376     u8 buf_0[] = {
0377         GETBYTE(stb0899_i2cdev, BYTE1),     /* 0xf3 S2 Base Address (MSB)   */
0378         GETBYTE(stb0899_i2cdev, BYTE0),     /* 0xfc S2 Base Address (LSB)   */
0379         GETBYTE(stb0899_base_addr, BYTE0),  /* 0x00 Base Address (LSB)  */
0380         GETBYTE(stb0899_base_addr, BYTE1),  /* 0x04 Base Address (LSB)  */
0381         GETBYTE(stb0899_base_addr, BYTE2),  /* 0x00 Base Address (MSB)  */
0382         GETBYTE(stb0899_base_addr, BYTE3),  /* 0x00 Base Address (MSB)  */
0383     };
0384     u8 buf_1[] = {
0385         0x00,   /* 0xf3 Reg Offset  */
0386         0x00,   /* 0x44 Reg Offset  */
0387         0x00,   /* data         */
0388         0x00,   /* data         */
0389         0x00,   /* data         */
0390         0x00,   /* data         */
0391     };
0392 
0393     struct i2c_msg msg_0 = {
0394         .addr   = state->config->demod_address,
0395         .flags  = 0,
0396         .buf    = buf_0,
0397         .len    = 6
0398     };
0399 
0400     struct i2c_msg msg_1 = {
0401         .addr   = state->config->demod_address,
0402         .flags  = 0,
0403         .buf    = buf_1,
0404         .len    = 6
0405     };
0406 
0407     buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
0408     buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
0409     buf_1[2] = GETBYTE(stb0899_data, BYTE0);
0410     buf_1[3] = GETBYTE(stb0899_data, BYTE1);
0411     buf_1[4] = GETBYTE(stb0899_data, BYTE2);
0412     buf_1[5] = GETBYTE(stb0899_data, BYTE3);
0413 
0414     if (unlikely(*state->verbose >= FE_DEBUGREG))
0415         printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
0416                __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
0417 
0418     status = i2c_transfer(state->i2c, &msg_0, 1);
0419     if (unlikely(status < 1)) {
0420         if (status != -ERESTARTSYS)
0421             printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
0422                    __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
0423         goto err;
0424     }
0425     status = i2c_transfer(state->i2c, &msg_1, 1);
0426     if (unlikely(status < 1)) {
0427         if (status != -ERESTARTSYS)
0428             printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
0429                    __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
0430 
0431         return status < 0 ? status : -EREMOTEIO;
0432     }
0433 
0434     return 0;
0435 
0436 err:
0437     return status < 0 ? status : -EREMOTEIO;
0438 }
0439 
0440 int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
0441 {
0442     int status;
0443 
0444     u8 b0[] = { reg >> 8, reg & 0xff };
0445 
0446     struct i2c_msg msg[] = {
0447         {
0448             .addr   = state->config->demod_address,
0449             .flags  = 0,
0450             .buf    = b0,
0451             .len    = 2
0452         },{
0453             .addr   = state->config->demod_address,
0454             .flags  = I2C_M_RD,
0455             .buf    = buf,
0456             .len    = count
0457         }
0458     };
0459 
0460     status = i2c_transfer(state->i2c, msg, 2);
0461     if (status != 2) {
0462         if (status != -ERESTARTSYS)
0463             printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
0464                    __func__, reg, count, status);
0465         goto err;
0466     }
0467     /*
0468      * Bug ID 9:
0469      * access to 0xf2xx/0xf6xx
0470      * must be followed by read from 0xf2ff/0xf6ff.
0471      */
0472     if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
0473         (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
0474         _stb0899_read_reg(state, (reg | 0x00ff));
0475 
0476     dprintk(state->verbose, FE_DEBUGREG, 1,
0477         "%s [0x%04x]: %*ph", __func__, reg, count, buf);
0478 
0479     return 0;
0480 err:
0481     return status < 0 ? status : -EREMOTEIO;
0482 }
0483 
0484 int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
0485 {
0486     int ret;
0487     u8 buf[MAX_XFER_SIZE];
0488     struct i2c_msg i2c_msg = {
0489         .addr   = state->config->demod_address,
0490         .flags  = 0,
0491         .buf    = buf,
0492         .len    = 2 + count
0493     };
0494 
0495     if (2 + count > sizeof(buf)) {
0496         printk(KERN_WARNING
0497                "%s: i2c wr reg=%04x: len=%d is too big!\n",
0498                KBUILD_MODNAME, reg, count);
0499         return -EINVAL;
0500     }
0501 
0502     buf[0] = reg >> 8;
0503     buf[1] = reg & 0xff;
0504     memcpy(&buf[2], data, count);
0505 
0506     dprintk(state->verbose, FE_DEBUGREG, 1,
0507         "%s [0x%04x]: %*ph", __func__, reg, count, data);
0508     ret = i2c_transfer(state->i2c, &i2c_msg, 1);
0509 
0510     /*
0511      * Bug ID 9:
0512      * access to 0xf2xx/0xf6xx
0513      * must be followed by read from 0xf2ff/0xf6ff.
0514      */
0515     if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
0516         stb0899_read_reg(state, (reg | 0x00ff));
0517 
0518     if (ret != 1) {
0519         if (ret != -ERESTARTSYS)
0520             dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
0521                 reg, data[0], count, ret);
0522         return ret < 0 ? ret : -EREMOTEIO;
0523     }
0524 
0525     return 0;
0526 }
0527 
0528 int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
0529 {
0530     u8 tmp = data;
0531     return stb0899_write_regs(state, reg, &tmp, 1);
0532 }
0533 
0534 /*
0535  * stb0899_get_mclk
0536  * Get STB0899 master clock frequency
0537  * ExtClk: external clock frequency (Hz)
0538  */
0539 static u32 stb0899_get_mclk(struct stb0899_state *state)
0540 {
0541     u32 mclk = 0, div = 0;
0542 
0543     div = stb0899_read_reg(state, STB0899_NCOARSE);
0544     mclk = (div + 1) * state->config->xtal_freq / 6;
0545     dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
0546 
0547     return mclk;
0548 }
0549 
0550 /*
0551  * stb0899_set_mclk
0552  * Set STB0899 master Clock frequency
0553  * Mclk: demodulator master clock
0554  * ExtClk: external clock frequency (Hz)
0555  */
0556 static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
0557 {
0558     struct stb0899_internal *internal = &state->internal;
0559     u8 mdiv = 0;
0560 
0561     dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
0562     mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
0563     dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
0564 
0565     stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
0566     internal->master_clk = stb0899_get_mclk(state);
0567 
0568     dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
0569 }
0570 
0571 static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
0572 {
0573     struct stb0899_config *config       = state->config;
0574     const struct stb0899_postproc *postproc = config->postproc;
0575 
0576     /* post process event */
0577     if (postproc) {
0578         if (enable) {
0579             if (postproc[ctl].level == STB0899_GPIOPULLUP)
0580                 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
0581             else
0582                 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
0583         } else {
0584             if (postproc[ctl].level == STB0899_GPIOPULLUP)
0585                 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
0586             else
0587                 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
0588         }
0589     }
0590     return 0;
0591 }
0592 
0593 static void stb0899_detach(struct dvb_frontend *fe)
0594 {
0595     struct stb0899_state *state = fe->demodulator_priv;
0596 
0597     /* post process event */
0598     stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
0599 }
0600 
0601 static void stb0899_release(struct dvb_frontend *fe)
0602 {
0603     struct stb0899_state *state = fe->demodulator_priv;
0604 
0605     dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
0606     kfree(state);
0607 }
0608 
0609 /*
0610  * stb0899_get_alpha
0611  * return: rolloff
0612  */
0613 static int stb0899_get_alpha(struct stb0899_state *state)
0614 {
0615     u8 mode_coeff;
0616 
0617     mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
0618 
0619     if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
0620         return 20;
0621     else
0622         return 35;
0623 }
0624 
0625 /*
0626  * stb0899_init_calc
0627  */
0628 static void stb0899_init_calc(struct stb0899_state *state)
0629 {
0630     struct stb0899_internal *internal = &state->internal;
0631     int master_clk;
0632     u8 agc[2];
0633     u32 reg;
0634 
0635     /* Read registers (in burst mode)   */
0636     stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O   */
0637 
0638     /* Initial calculations */
0639     master_clk          = stb0899_get_mclk(state);
0640     internal->t_agc1        = 0;
0641     internal->t_agc2        = 0;
0642     internal->master_clk        = master_clk;
0643     internal->mclk          = master_clk / 65536L;
0644     internal->rolloff       = stb0899_get_alpha(state);
0645 
0646     /* DVBS2 Initial calculations   */
0647     /* Set AGC value to the middle  */
0648     internal->agc_gain      = 8154;
0649     reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
0650     STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
0651     stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
0652 
0653     reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
0654     internal->rrc_alpha     = STB0899_GETFIELD(RRC_ALPHA, reg);
0655 
0656     internal->center_freq       = 0;
0657     internal->av_frame_coarse   = 10;
0658     internal->av_frame_fine     = 20;
0659     internal->step_size     = 2;
0660 /*
0661     if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
0662         pParams->IQLocked = 0;
0663     else
0664         pParams->IQLocked = 1;
0665 */
0666 }
0667 
0668 static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
0669 {
0670     u8 reg = 0;
0671     unsigned long start = jiffies;
0672 
0673     while (1) {
0674         reg = stb0899_read_reg(state, STB0899_DISSTATUS);
0675         if (!STB0899_GETFIELD(FIFOFULL, reg))
0676             break;
0677         if (time_after(jiffies, start + timeout)) {
0678             dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
0679             return -ETIMEDOUT;
0680         }
0681     }
0682 
0683     return 0;
0684 }
0685 
0686 static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
0687 {
0688     struct stb0899_state *state = fe->demodulator_priv;
0689     u8 reg, i;
0690 
0691     if (cmd->msg_len > sizeof(cmd->msg))
0692         return -EINVAL;
0693 
0694     /* enable FIFO precharge    */
0695     reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
0696     STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
0697     stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
0698     for (i = 0; i < cmd->msg_len; i++) {
0699         /* wait for FIFO empty  */
0700         if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
0701             return -ETIMEDOUT;
0702 
0703         stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
0704     }
0705     reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
0706     STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
0707     stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
0708     msleep(100);
0709     return 0;
0710 }
0711 
0712 static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
0713 {
0714     u8 reg = 0;
0715     unsigned long start = jiffies;
0716 
0717     while (!STB0899_GETFIELD(RXEND, reg)) {
0718         reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
0719         if (time_after(jiffies, start + timeout)) {
0720             dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
0721             return -ETIMEDOUT;
0722         }
0723         msleep(10);
0724     }
0725 
0726     return 0;
0727 }
0728 
0729 static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
0730 {
0731     struct stb0899_state *state = fe->demodulator_priv;
0732     u8 reg, length = 0, i;
0733     int result;
0734 
0735     if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
0736         return -ETIMEDOUT;
0737 
0738     reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
0739     if (STB0899_GETFIELD(RXEND, reg)) {
0740 
0741         reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
0742         length = STB0899_GETFIELD(FIFOBYTENBR, reg);
0743 
0744         if (length > sizeof (reply->msg)) {
0745             result = -EOVERFLOW;
0746             goto exit;
0747         }
0748         reply->msg_len = length;
0749 
0750         /* extract data */
0751         for (i = 0; i < length; i++)
0752             reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
0753     }
0754 
0755     return 0;
0756 exit:
0757 
0758     return result;
0759 }
0760 
0761 static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
0762 {
0763     u8 reg = 0;
0764     unsigned long start = jiffies;
0765 
0766     while (!STB0899_GETFIELD(TXIDLE, reg)) {
0767         reg = stb0899_read_reg(state, STB0899_DISSTATUS);
0768         if (time_after(jiffies, start + timeout)) {
0769             dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
0770             return -ETIMEDOUT;
0771         }
0772         msleep(10);
0773     }
0774     return 0;
0775 }
0776 
0777 static int stb0899_send_diseqc_burst(struct dvb_frontend *fe,
0778                      enum fe_sec_mini_cmd burst)
0779 {
0780     struct stb0899_state *state = fe->demodulator_priv;
0781     u8 reg, old_state;
0782 
0783     /* wait for diseqc idle */
0784     if (stb0899_wait_diseqc_txidle(state, 100) < 0)
0785         return -ETIMEDOUT;
0786 
0787     reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
0788     old_state = reg;
0789     /* set to burst mode    */
0790     STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
0791     STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
0792     stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
0793     switch (burst) {
0794     case SEC_MINI_A:
0795         /* unmodulated  */
0796         stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
0797         break;
0798     case SEC_MINI_B:
0799         /* modulated    */
0800         stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
0801         break;
0802     }
0803     reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
0804     STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
0805     stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
0806     /* wait for diseqc idle */
0807     if (stb0899_wait_diseqc_txidle(state, 100) < 0)
0808         return -ETIMEDOUT;
0809 
0810     /* restore state    */
0811     stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
0812 
0813     return 0;
0814 }
0815 
0816 static int stb0899_diseqc_init(struct stb0899_state *state)
0817 {
0818 /*
0819     struct dvb_diseqc_slave_reply rx_data;
0820 */
0821     u8 f22_tx, reg;
0822 
0823     u32 mclk, tx_freq = 22000;/* count = 0, i; */
0824     reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
0825     STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
0826     stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
0827 
0828     /* disable Tx spy   */
0829     reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
0830     STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
0831     stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
0832 
0833     reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
0834     STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
0835     stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
0836 
0837     mclk = stb0899_get_mclk(state);
0838     f22_tx = mclk / (tx_freq * 32);
0839     stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
0840     state->rx_freq = 20000;
0841 
0842     return 0;
0843 }
0844 
0845 static int stb0899_sleep(struct dvb_frontend *fe)
0846 {
0847     struct stb0899_state *state = fe->demodulator_priv;
0848 /*
0849     u8 reg;
0850 */
0851     dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
0852     /* post process event */
0853     stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
0854 
0855     return 0;
0856 }
0857 
0858 static int stb0899_wakeup(struct dvb_frontend *fe)
0859 {
0860     int rc;
0861     struct stb0899_state *state = fe->demodulator_priv;
0862 
0863     if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
0864         return rc;
0865     /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
0866     if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
0867         return rc;
0868     if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
0869         return rc;
0870 
0871     /* post process event */
0872     stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
0873 
0874     return 0;
0875 }
0876 
0877 static int stb0899_init(struct dvb_frontend *fe)
0878 {
0879     int i;
0880     struct stb0899_state *state = fe->demodulator_priv;
0881     struct stb0899_config *config = state->config;
0882 
0883     dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
0884 
0885     /* init device      */
0886     dprintk(state->verbose, FE_DEBUG, 1, "init device");
0887     for (i = 0; config->init_dev[i].address != 0xffff; i++)
0888         stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
0889 
0890     dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
0891     /* init S2 demod    */
0892     for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
0893         stb0899_write_s2reg(state, STB0899_S2DEMOD,
0894                     config->init_s2_demod[i].base_address,
0895                     config->init_s2_demod[i].offset,
0896                     config->init_s2_demod[i].data);
0897 
0898     dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
0899     /* init S1 demod    */
0900     for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
0901         stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
0902 
0903     dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
0904     /* init S2 fec      */
0905     for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
0906         stb0899_write_s2reg(state, STB0899_S2FEC,
0907                     config->init_s2_fec[i].base_address,
0908                     config->init_s2_fec[i].offset,
0909                     config->init_s2_fec[i].data);
0910 
0911     dprintk(state->verbose, FE_DEBUG, 1, "init TST");
0912     /* init test        */
0913     for (i = 0; config->init_tst[i].address != 0xffff; i++)
0914         stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
0915 
0916     stb0899_init_calc(state);
0917     stb0899_diseqc_init(state);
0918 
0919     return 0;
0920 }
0921 
0922 static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
0923 {
0924     int res = 0;
0925     int min = 0, med;
0926 
0927     if (val < tab[min].read)
0928         res = tab[min].real;
0929     else if (val >= tab[max].read)
0930         res = tab[max].real;
0931     else {
0932         while ((max - min) > 1) {
0933             med = (max + min) / 2;
0934             if (val >= tab[min].read && val < tab[med].read)
0935                 max = med;
0936             else
0937                 min = med;
0938         }
0939         res = ((val - tab[min].read) *
0940                (tab[max].real - tab[min].real) /
0941                (tab[max].read - tab[min].read)) +
0942             tab[min].real;
0943     }
0944 
0945     return res;
0946 }
0947 
0948 static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
0949 {
0950     struct stb0899_state *state     = fe->demodulator_priv;
0951     struct stb0899_internal *internal   = &state->internal;
0952 
0953     int val;
0954     u32 reg;
0955     *strength = 0;
0956     switch (state->delsys) {
0957     case SYS_DVBS:
0958     case SYS_DSS:
0959         if (internal->lock) {
0960             reg  = stb0899_read_reg(state, STB0899_VSTATUS);
0961             if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
0962 
0963                 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
0964                 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
0965 
0966                 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
0967                 *strength += 750;
0968                 dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
0969                     val & 0xff, *strength);
0970             }
0971         }
0972         break;
0973     case SYS_DVBS2:
0974         if (internal->lock) {
0975             reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN);
0976             val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
0977 
0978             *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
0979             *strength += 950;
0980             dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
0981                 val & 0x3fff, *strength);
0982         }
0983         break;
0984     default:
0985         dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
0986         return -EINVAL;
0987     }
0988 
0989     return 0;
0990 }
0991 
0992 static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
0993 {
0994     struct stb0899_state *state     = fe->demodulator_priv;
0995     struct stb0899_internal *internal   = &state->internal;
0996 
0997     unsigned int val, quant, quantn = -1, est, estn = -1;
0998     u8 buf[2];
0999     u32 reg;
1000 
1001     *snr = 0;
1002     reg  = stb0899_read_reg(state, STB0899_VSTATUS);
1003     switch (state->delsys) {
1004     case SYS_DVBS:
1005     case SYS_DSS:
1006         if (internal->lock) {
1007             if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1008 
1009                 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1010                 val = MAKEWORD16(buf[0], buf[1]);
1011 
1012                 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
1013                 dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
1014                     buf[0], buf[1], val, *snr);
1015             }
1016         }
1017         break;
1018     case SYS_DVBS2:
1019         if (internal->lock) {
1020             reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1021             quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1022             reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1023             est = STB0899_GETFIELD(ESN0_EST, reg);
1024             if (est == 1)
1025                 val = 301; /* C/N = 30.1 dB */
1026             else if (est == 2)
1027                 val = 270; /* C/N = 27.0 dB */
1028             else {
1029                 /* quantn = 100 * log(quant^2) */
1030                 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1031                 /* estn = 100 * log(est) */
1032                 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1033                 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1034                 val = (quantn - estn) / 10;
1035             }
1036             *snr = val;
1037             dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
1038                 quant, quantn, est, estn, val);
1039         }
1040         break;
1041     default:
1042         dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1043         return -EINVAL;
1044     }
1045 
1046     return 0;
1047 }
1048 
1049 static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1050 {
1051     struct stb0899_state *state     = fe->demodulator_priv;
1052     struct stb0899_internal *internal   = &state->internal;
1053     u8 reg;
1054     *status = 0;
1055 
1056     switch (state->delsys) {
1057     case SYS_DVBS:
1058     case SYS_DSS:
1059         dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1060         if (internal->lock) {
1061             reg  = stb0899_read_reg(state, STB0899_VSTATUS);
1062             if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1063                 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1064                 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
1065 
1066                 reg = stb0899_read_reg(state, STB0899_PLPARM);
1067                 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1068                     dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1069                     *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
1070                     /* post process event */
1071                     stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1072                 }
1073             }
1074         }
1075         break;
1076     case SYS_DVBS2:
1077         dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1078         if (internal->lock) {
1079             reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1080             if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1081                 *status |= FE_HAS_CARRIER;
1082                 dprintk(state->verbose, FE_DEBUG, 1,
1083                     "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1084 
1085                 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1086                 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1087                     *status |= FE_HAS_LOCK;
1088                     dprintk(state->verbose, FE_DEBUG, 1,
1089                         "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1090 
1091                 }
1092                 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1093                     *status |= FE_HAS_VITERBI;
1094                     dprintk(state->verbose, FE_DEBUG, 1,
1095                         "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1096                 }
1097                 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1098                     *status |= FE_HAS_SYNC;
1099                     dprintk(state->verbose, FE_DEBUG, 1,
1100                         "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1101                     /* post process event */
1102                     stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1103                 }
1104             }
1105         }
1106         break;
1107     default:
1108         dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1109         return -EINVAL;
1110     }
1111     return 0;
1112 }
1113 
1114 /*
1115  * stb0899_get_error
1116  * viterbi error for DVB-S/DSS
1117  * packet error for DVB-S2
1118  * Bit Error Rate or Packet Error Rate * 10 ^ 7
1119  */
1120 static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1121 {
1122     struct stb0899_state *state     = fe->demodulator_priv;
1123     struct stb0899_internal *internal   = &state->internal;
1124 
1125     u8  lsb, msb;
1126 
1127     *ber = 0;
1128 
1129     switch (state->delsys) {
1130     case SYS_DVBS:
1131     case SYS_DSS:
1132         if (internal->lock) {
1133             lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1134             msb = stb0899_read_reg(state, STB0899_ECNT1M);
1135             *ber = MAKEWORD16(msb, lsb);
1136             /* Viterbi Check    */
1137             if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1138                 /* Error Rate       */
1139                 *ber *= 9766;
1140                 /* ber = ber * 10 ^ 7   */
1141                 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1142                 *ber /= 8;
1143             }
1144         }
1145         break;
1146     case SYS_DVBS2:
1147         if (internal->lock) {
1148             lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1149             msb = stb0899_read_reg(state, STB0899_ECNT1M);
1150             *ber = MAKEWORD16(msb, lsb);
1151             /* ber = ber * 10 ^ 7   */
1152             *ber *= 10000000;
1153             *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1154         }
1155         break;
1156     default:
1157         dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1158         return -EINVAL;
1159     }
1160 
1161     return 0;
1162 }
1163 
1164 static int stb0899_set_voltage(struct dvb_frontend *fe,
1165                    enum fe_sec_voltage voltage)
1166 {
1167     struct stb0899_state *state = fe->demodulator_priv;
1168 
1169     switch (voltage) {
1170     case SEC_VOLTAGE_13:
1171         stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1172         stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1173         stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1174         break;
1175     case SEC_VOLTAGE_18:
1176         stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1177         stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1178         stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1179         break;
1180     case SEC_VOLTAGE_OFF:
1181         stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1182         stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1183         stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1184         break;
1185     default:
1186         return -EINVAL;
1187     }
1188 
1189     return 0;
1190 }
1191 
1192 static int stb0899_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
1193 {
1194     struct stb0899_state *state = fe->demodulator_priv;
1195     struct stb0899_internal *internal = &state->internal;
1196 
1197     u8 div, reg;
1198 
1199     /* wait for diseqc idle */
1200     if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1201         return -ETIMEDOUT;
1202 
1203     switch (tone) {
1204     case SEC_TONE_ON:
1205         div = (internal->master_clk / 100) / 5632;
1206         div = (div + 5) / 10;
1207         stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
1208         reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1209         STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1210         stb0899_write_reg(state, STB0899_ACRPRESC, reg);
1211         stb0899_write_reg(state, STB0899_ACRDIV1, div);
1212         break;
1213     case SEC_TONE_OFF:
1214         stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1215         break;
1216     default:
1217         return -EINVAL;
1218     }
1219     return 0;
1220 }
1221 
1222 int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1223 {
1224     int i2c_stat;
1225     struct stb0899_state *state = fe->demodulator_priv;
1226 
1227     i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1228     if (i2c_stat < 0)
1229         goto err;
1230 
1231     if (enable) {
1232         dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1233         i2c_stat |=  STB0899_I2CTON;
1234         if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1235             goto err;
1236     } else {
1237         dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
1238         i2c_stat &= ~STB0899_I2CTON;
1239         if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1240             goto err;
1241     }
1242     return 0;
1243 err:
1244     dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
1245     return -EREMOTEIO;
1246 }
1247 
1248 
1249 static inline void CONVERT32(u32 x, char *str)
1250 {
1251     *str++  = (x >> 24) & 0xff;
1252     *str++  = (x >> 16) & 0xff;
1253     *str++  = (x >>  8) & 0xff;
1254     *str++  = (x >>  0) & 0xff;
1255     *str    = '\0';
1256 }
1257 
1258 static int stb0899_get_dev_id(struct stb0899_state *state)
1259 {
1260     u8 chip_id, release;
1261     u16 id;
1262     u32 demod_ver = 0, fec_ver = 0;
1263     char demod_str[5] = { 0 };
1264     char fec_str[5] = { 0 };
1265 
1266     id = stb0899_read_reg(state, STB0899_DEV_ID);
1267     dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1268     chip_id = STB0899_GETFIELD(CHIP_ID, id);
1269     release = STB0899_GETFIELD(CHIP_REL, id);
1270 
1271     dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1272         chip_id, release);
1273 
1274     CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1275 
1276     demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1277     dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1278     CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1279     fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1280     if (! (chip_id > 0)) {
1281         dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1282 
1283         return -ENODEV;
1284     }
1285     dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1286 
1287     return 0;
1288 }
1289 
1290 static void stb0899_set_delivery(struct stb0899_state *state)
1291 {
1292     u8 reg;
1293     u8 stop_clk[2];
1294 
1295     stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1296     stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1297 
1298     switch (state->delsys) {
1299     case SYS_DVBS:
1300         dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
1301         /* FECM/Viterbi ON  */
1302         reg = stb0899_read_reg(state, STB0899_FECM);
1303         STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1304         STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1305         stb0899_write_reg(state, STB0899_FECM, reg);
1306 
1307         stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1308         stb0899_write_reg(state, STB0899_TSULC, 0x40);
1309         stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1310         stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1311 
1312         reg = stb0899_read_reg(state, STB0899_TSTRES);
1313         STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1314         stb0899_write_reg(state, STB0899_TSTRES, reg);
1315 
1316         STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1317         STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1318         STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1319 
1320         STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1321         STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1322 
1323         STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
1324         STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1325 
1326         STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1327         break;
1328     case SYS_DVBS2:
1329         /* FECM/Viterbi OFF */
1330         reg = stb0899_read_reg(state, STB0899_FECM);
1331         STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1332         STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1333         stb0899_write_reg(state, STB0899_FECM, reg);
1334 
1335         stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1336         stb0899_write_reg(state, STB0899_TSULC, 0x42);
1337         stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1338         stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1339 
1340         reg = stb0899_read_reg(state, STB0899_TSTRES);
1341         STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1342         stb0899_write_reg(state, STB0899_TSTRES, reg);
1343 
1344         STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1345         STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1346         STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1347 
1348         STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1349         STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1350 
1351         STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1352         STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1353 
1354         STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1355         break;
1356     case SYS_DSS:
1357         /* FECM/Viterbi ON  */
1358         reg = stb0899_read_reg(state, STB0899_FECM);
1359         STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1360         STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1361         stb0899_write_reg(state, STB0899_FECM, reg);
1362 
1363         stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1364         stb0899_write_reg(state, STB0899_TSULC, 0x61);
1365         stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1366 
1367         reg = stb0899_read_reg(state, STB0899_TSTRES);
1368         STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1369         stb0899_write_reg(state, STB0899_TSTRES, reg);
1370 
1371         STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1372         STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1373         STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1374 
1375         STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1376         STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1377 
1378         STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1379 
1380         STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1381         break;
1382     default:
1383         dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1384         break;
1385     }
1386     STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1387     stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1388 }
1389 
1390 /*
1391  * stb0899_set_iterations
1392  * set the LDPC iteration scale function
1393  */
1394 static void stb0899_set_iterations(struct stb0899_state *state)
1395 {
1396     struct stb0899_internal *internal = &state->internal;
1397     struct stb0899_config *config = state->config;
1398 
1399     s32 iter_scale;
1400     u32 reg;
1401 
1402     iter_scale = 17 * (internal->master_clk / 1000);
1403     iter_scale += 410000;
1404     iter_scale /= (internal->srate / 1000000);
1405     iter_scale /= 1000;
1406 
1407     if (iter_scale > config->ldpc_max_iter)
1408         iter_scale = config->ldpc_max_iter;
1409 
1410     reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER);
1411     STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1412     stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1413 }
1414 
1415 static enum dvbfe_search stb0899_search(struct dvb_frontend *fe)
1416 {
1417     struct stb0899_state *state = fe->demodulator_priv;
1418     struct stb0899_params *i_params = &state->params;
1419     struct stb0899_internal *internal = &state->internal;
1420     struct stb0899_config *config = state->config;
1421     struct dtv_frontend_properties *props = &fe->dtv_property_cache;
1422 
1423     u32 SearchRange, gain;
1424 
1425     i_params->freq  = props->frequency;
1426     i_params->srate = props->symbol_rate;
1427     state->delsys = props->delivery_system;
1428     dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1429 
1430     SearchRange = 10000000;
1431     dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
1432     /* checking Search Range is meaningless for a fixed 3 Mhz           */
1433     if (INRANGE(i_params->srate, 1000000, 45000000)) {
1434         dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
1435         stb0899_set_delivery(state);
1436 
1437         if (state->config->tuner_set_rfsiggain) {
1438             if (internal->srate > 15000000)
1439                 gain =  8; /* 15Mb < srate < 45Mb, gain = 8dB   */
1440             else if (internal->srate > 5000000)
1441                 gain = 12; /*  5Mb < srate < 15Mb, gain = 12dB  */
1442             else
1443                 gain = 14; /*  1Mb < srate <  5Mb, gain = 14db  */
1444             state->config->tuner_set_rfsiggain(fe, gain);
1445         }
1446 
1447         if (i_params->srate <= 5000000)
1448             stb0899_set_mclk(state, config->lo_clk);
1449         else
1450             stb0899_set_mclk(state, config->hi_clk);
1451 
1452         switch (state->delsys) {
1453         case SYS_DVBS:
1454         case SYS_DSS:
1455             dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
1456             internal->freq  = i_params->freq;
1457             internal->srate = i_params->srate;
1458             /*
1459              * search = user search range +
1460              *      500Khz +
1461              *      2 * Tuner_step_size +
1462              *      10% of the symbol rate
1463              */
1464             internal->srch_range    = SearchRange + 1500000 + (i_params->srate / 5);
1465             internal->derot_percent = 30;
1466 
1467             /* What to do for tuners having no bandwidth setup ?    */
1468             /* enable tuner I/O */
1469             stb0899_i2c_gate_ctrl(&state->frontend, 1);
1470 
1471             if (state->config->tuner_set_bandwidth)
1472                 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
1473             if (state->config->tuner_get_bandwidth)
1474                 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1475 
1476             /* disable tuner I/O */
1477             stb0899_i2c_gate_ctrl(&state->frontend, 0);
1478 
1479             /* Set DVB-S1 AGC       */
1480             stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1481 
1482             /* Run the search algorithm */
1483             dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
1484             if (stb0899_dvbs_algo(state)    == RANGEOK) {
1485                 internal->lock      = 1;
1486                 dprintk(state->verbose, FE_DEBUG, 1,
1487                     "-------------------------------------> DVB-S LOCK !");
1488 
1489 //              stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
1490 //              internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1491 //              internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1492 //              dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1493 //              dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
1494 
1495                 return DVBFE_ALGO_SEARCH_SUCCESS;
1496             } else {
1497                 internal->lock      = 0;
1498 
1499                 return DVBFE_ALGO_SEARCH_FAILED;
1500             }
1501             break;
1502         case SYS_DVBS2:
1503             internal->freq          = i_params->freq;
1504             internal->srate         = i_params->srate;
1505             internal->srch_range        = SearchRange;
1506 
1507             /* enable tuner I/O */
1508             stb0899_i2c_gate_ctrl(&state->frontend, 1);
1509 
1510             if (state->config->tuner_set_bandwidth)
1511                 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
1512             if (state->config->tuner_get_bandwidth)
1513                 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1514 
1515             /* disable tuner I/O */
1516             stb0899_i2c_gate_ctrl(&state->frontend, 0);
1517 
1518 //          pParams->SpectralInv        = pSearch->IQ_Inversion;
1519 
1520             /* Set DVB-S2 AGC       */
1521             stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1522 
1523             /* Set IterScale =f(MCLK,SYMB)  */
1524             stb0899_set_iterations(state);
1525 
1526             /* Run the search algorithm */
1527             dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
1528             if (stb0899_dvbs2_algo(state)   == DVBS2_FEC_LOCK) {
1529                 internal->lock      = 1;
1530                 dprintk(state->verbose, FE_DEBUG, 1,
1531                     "-------------------------------------> DVB-S2 LOCK !");
1532 
1533 //              stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors  */
1534 //              internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1535 //              internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1536 
1537                 return DVBFE_ALGO_SEARCH_SUCCESS;
1538             } else {
1539                 internal->lock      = 0;
1540 
1541                 return DVBFE_ALGO_SEARCH_FAILED;
1542             }
1543             break;
1544         default:
1545             dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1546             return DVBFE_ALGO_SEARCH_INVALID;
1547         }
1548     }
1549 
1550     return DVBFE_ALGO_SEARCH_ERROR;
1551 }
1552 
1553 static int stb0899_get_frontend(struct dvb_frontend *fe,
1554                 struct dtv_frontend_properties *p)
1555 {
1556     struct stb0899_state *state     = fe->demodulator_priv;
1557     struct stb0899_internal *internal   = &state->internal;
1558 
1559     dprintk(state->verbose, FE_DEBUG, 1, "Get params");
1560     p->symbol_rate = internal->srate;
1561     p->frequency = internal->freq;
1562 
1563     return 0;
1564 }
1565 
1566 static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1567 {
1568     return DVBFE_ALGO_CUSTOM;
1569 }
1570 
1571 static const struct dvb_frontend_ops stb0899_ops = {
1572     .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
1573     .info = {
1574         .name           = "STB0899 Multistandard",
1575         .frequency_min_hz   =  950 * MHz,
1576         .frequency_max_hz   = 2150 * MHz,
1577         .symbol_rate_min    =  5000000,
1578         .symbol_rate_max    = 45000000,
1579 
1580         .caps           = FE_CAN_INVERSION_AUTO |
1581                       FE_CAN_FEC_AUTO   |
1582                       FE_CAN_2G_MODULATION  |
1583                       FE_CAN_QPSK
1584     },
1585 
1586     .detach             = stb0899_detach,
1587     .release            = stb0899_release,
1588     .init               = stb0899_init,
1589     .sleep              = stb0899_sleep,
1590 //  .wakeup             = stb0899_wakeup,
1591 
1592     .i2c_gate_ctrl          = stb0899_i2c_gate_ctrl,
1593 
1594     .get_frontend_algo      = stb0899_frontend_algo,
1595     .search             = stb0899_search,
1596     .get_frontend                   = stb0899_get_frontend,
1597 
1598 
1599     .read_status            = stb0899_read_status,
1600     .read_snr           = stb0899_read_snr,
1601     .read_signal_strength       = stb0899_read_signal_strength,
1602     .read_ber           = stb0899_read_ber,
1603 
1604     .set_voltage            = stb0899_set_voltage,
1605     .set_tone           = stb0899_set_tone,
1606 
1607     .diseqc_send_master_cmd     = stb0899_send_diseqc_msg,
1608     .diseqc_recv_slave_reply    = stb0899_recv_slave_reply,
1609     .diseqc_send_burst      = stb0899_send_diseqc_burst,
1610 };
1611 
1612 struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1613 {
1614     struct stb0899_state *state = NULL;
1615 
1616     state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1617     if (state == NULL)
1618         goto error;
1619 
1620     state->verbose              = &verbose;
1621     state->config               = config;
1622     state->i2c              = i2c;
1623     state->frontend.ops         = stb0899_ops;
1624     state->frontend.demodulator_priv    = state;
1625     /* use configured inversion as default -- we'll later autodetect inversion */
1626     state->internal.inversion       = config->inversion;
1627 
1628     stb0899_wakeup(&state->frontend);
1629     if (stb0899_get_dev_id(state) == -ENODEV) {
1630         printk("%s: Exiting .. !\n", __func__);
1631         goto error;
1632     }
1633 
1634     printk("%s: Attaching STB0899 \n", __func__);
1635     return &state->frontend;
1636 
1637 error:
1638     kfree(state);
1639     return NULL;
1640 }
1641 EXPORT_SYMBOL(stb0899_attach);
1642 MODULE_PARM_DESC(verbose, "Set Verbosity level");
1643 MODULE_AUTHOR("Manu Abraham");
1644 MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1645 MODULE_LICENSE("GPL");