Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *    Support for OR51211 (pcHDTV HD-2000) - VSB
0004  *
0005  *    Copyright (C) 2005 Kirk Lapray <kirk_lapray@bigfoot.com>
0006  *
0007  *    Based on code from Jack Kelliher (kelliher@xmission.com)
0008  *                           Copyright (C) 2002 & pcHDTV, inc.
0009 */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
0012 
0013 /*
0014  * This driver needs external firmware. Please use the command
0015  * "<kerneldir>/scripts/get_dvb_firmware or51211" to
0016  * download/extract it, and then copy it to /usr/lib/hotplug/firmware
0017  * or /lib/firmware (depending on configuration of firmware hotplug).
0018  */
0019 #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw"
0020 
0021 #include <linux/kernel.h>
0022 #include <linux/module.h>
0023 #include <linux/device.h>
0024 #include <linux/firmware.h>
0025 #include <linux/string.h>
0026 #include <linux/slab.h>
0027 #include <asm/byteorder.h>
0028 
0029 #include <media/dvb_math.h>
0030 #include <media/dvb_frontend.h>
0031 #include "or51211.h"
0032 
0033 static int debug;
0034 #define dprintk(args...) \
0035     do { if (debug) pr_debug(args); } while (0)
0036 
0037 static u8 run_buf[] = {0x7f,0x01};
0038 static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
0039 
0040 struct or51211_state {
0041 
0042     struct i2c_adapter* i2c;
0043 
0044     /* Configuration settings */
0045     const struct or51211_config* config;
0046 
0047     struct dvb_frontend frontend;
0048     struct bt878* bt;
0049 
0050     /* Demodulator private data */
0051     u8 initialized:1;
0052     u32 snr; /* Result of last SNR calculation */
0053 
0054     /* Tuner private data */
0055     u32 current_frequency;
0056 };
0057 
0058 static int i2c_writebytes (struct or51211_state* state, u8 reg, const u8 *buf,
0059                int len)
0060 {
0061     int err;
0062     struct i2c_msg msg;
0063     msg.addr    = reg;
0064     msg.flags   = 0;
0065     msg.len     = len;
0066     msg.buf     = (u8 *)buf;
0067 
0068     if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
0069         pr_warn("error (addr %02x, err == %i)\n", reg, err);
0070         return -EREMOTEIO;
0071     }
0072 
0073     return 0;
0074 }
0075 
0076 static int i2c_readbytes(struct or51211_state *state, u8 reg, u8 *buf, int len)
0077 {
0078     int err;
0079     struct i2c_msg msg;
0080     msg.addr    = reg;
0081     msg.flags   = I2C_M_RD;
0082     msg.len     = len;
0083     msg.buf     = buf;
0084 
0085     if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
0086         pr_warn("error (addr %02x, err == %i)\n", reg, err);
0087         return -EREMOTEIO;
0088     }
0089 
0090     return 0;
0091 }
0092 
0093 static int or51211_load_firmware (struct dvb_frontend* fe,
0094                   const struct firmware *fw)
0095 {
0096     struct or51211_state* state = fe->demodulator_priv;
0097     u8 tudata[585];
0098     int i;
0099 
0100     dprintk("Firmware is %zu bytes\n", fw->size);
0101 
0102     /* Get eprom data */
0103     tudata[0] = 17;
0104     if (i2c_writebytes(state,0x50,tudata,1)) {
0105         pr_warn("error eprom addr\n");
0106         return -1;
0107     }
0108     if (i2c_readbytes(state,0x50,&tudata[145],192)) {
0109         pr_warn("error eprom\n");
0110         return -1;
0111     }
0112 
0113     /* Create firmware buffer */
0114     for (i = 0; i < 145; i++)
0115         tudata[i] = fw->data[i];
0116 
0117     for (i = 0; i < 248; i++)
0118         tudata[i+337] = fw->data[145+i];
0119 
0120     state->config->reset(fe);
0121 
0122     if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
0123         pr_warn("error 1\n");
0124         return -1;
0125     }
0126     msleep(1);
0127 
0128     if (i2c_writebytes(state,state->config->demod_address,
0129                &fw->data[393],8125)) {
0130         pr_warn("error 2\n");
0131         return -1;
0132     }
0133     msleep(1);
0134 
0135     if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
0136         pr_warn("error 3\n");
0137         return -1;
0138     }
0139 
0140     /* Wait at least 5 msec */
0141     msleep(10);
0142     if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
0143         pr_warn("error 4\n");
0144         return -1;
0145     }
0146     msleep(10);
0147 
0148     pr_info("Done.\n");
0149     return 0;
0150 };
0151 
0152 static int or51211_setmode(struct dvb_frontend* fe, int mode)
0153 {
0154     struct or51211_state* state = fe->demodulator_priv;
0155     u8 rec_buf[14];
0156 
0157     state->config->setmode(fe, mode);
0158 
0159     if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
0160         pr_warn("error 1\n");
0161         return -1;
0162     }
0163 
0164     /* Wait at least 5 msec */
0165     msleep(10);
0166     if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
0167         pr_warn("error 2\n");
0168         return -1;
0169     }
0170 
0171     msleep(10);
0172 
0173     /* Set operation mode in Receiver 1 register;
0174      * type 1:
0175      * data 0x50h  Automatic sets receiver channel conditions
0176      *             Automatic NTSC rejection filter
0177      *             Enable  MPEG serial data output
0178      *             MPEG2tr
0179      *             High tuner phase noise
0180      *             normal +/-150kHz Carrier acquisition range
0181      */
0182     if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
0183         pr_warn("error 3\n");
0184         return -1;
0185     }
0186 
0187     rec_buf[0] = 0x04;
0188     rec_buf[1] = 0x00;
0189     rec_buf[2] = 0x03;
0190     rec_buf[3] = 0x00;
0191     msleep(20);
0192     if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
0193         pr_warn("error 5\n");
0194     }
0195     msleep(3);
0196     if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
0197         pr_warn("error 6\n");
0198         return -1;
0199     }
0200     dprintk("rec status %02x %02x\n", rec_buf[10], rec_buf[11]);
0201 
0202     return 0;
0203 }
0204 
0205 static int or51211_set_parameters(struct dvb_frontend *fe)
0206 {
0207     struct dtv_frontend_properties *p = &fe->dtv_property_cache;
0208     struct or51211_state* state = fe->demodulator_priv;
0209 
0210     /* Change only if we are actually changing the channel */
0211     if (state->current_frequency != p->frequency) {
0212         if (fe->ops.tuner_ops.set_params) {
0213             fe->ops.tuner_ops.set_params(fe);
0214             if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
0215         }
0216 
0217         /* Set to ATSC mode */
0218         or51211_setmode(fe,0);
0219 
0220         /* Update current frequency */
0221         state->current_frequency = p->frequency;
0222     }
0223     return 0;
0224 }
0225 
0226 static int or51211_read_status(struct dvb_frontend *fe, enum fe_status *status)
0227 {
0228     struct or51211_state* state = fe->demodulator_priv;
0229     unsigned char rec_buf[2];
0230     unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
0231     *status = 0;
0232 
0233     /* Receiver Status */
0234     if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
0235         pr_warn("write error\n");
0236         return -1;
0237     }
0238     msleep(3);
0239     if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
0240         pr_warn("read error\n");
0241         return -1;
0242     }
0243     dprintk("%x %x\n", rec_buf[0], rec_buf[1]);
0244 
0245     if (rec_buf[0] &  0x01) { /* Receiver Lock */
0246         *status |= FE_HAS_SIGNAL;
0247         *status |= FE_HAS_CARRIER;
0248         *status |= FE_HAS_VITERBI;
0249         *status |= FE_HAS_SYNC;
0250         *status |= FE_HAS_LOCK;
0251     }
0252     return 0;
0253 }
0254 
0255 /* Calculate SNR estimation (scaled by 2^24)
0256 
0257    8-VSB SNR equation from Oren datasheets
0258 
0259    For 8-VSB:
0260      SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
0261 
0262    We re-write the snr equation as:
0263      SNR * 2^24 = 10*(c - 2*intlog10(MSE))
0264    Where for 8-VSB, c = log10(219037.9454) * 2^24 */
0265 
0266 static u32 calculate_snr(u32 mse, u32 c)
0267 {
0268     if (mse == 0) /* No signal */
0269         return 0;
0270 
0271     mse = 2*intlog10(mse);
0272     if (mse > c) {
0273         /* Negative SNR, which is possible, but realisticly the
0274         demod will lose lock before the signal gets this bad.  The
0275         API only allows for unsigned values, so just return 0 */
0276         return 0;
0277     }
0278     return 10*(c - mse);
0279 }
0280 
0281 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
0282 {
0283     struct or51211_state* state = fe->demodulator_priv;
0284     u8 rec_buf[2];
0285     u8 snd_buf[3];
0286 
0287     /* SNR after Equalizer */
0288     snd_buf[0] = 0x04;
0289     snd_buf[1] = 0x00;
0290     snd_buf[2] = 0x04;
0291 
0292     if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
0293         pr_warn("error writing snr reg\n");
0294         return -1;
0295     }
0296     if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
0297         pr_warn("read_status read error\n");
0298         return -1;
0299     }
0300 
0301     state->snr = calculate_snr(rec_buf[0], 89599047);
0302     *snr = (state->snr) >> 16;
0303 
0304     dprintk("noise = 0x%02x, snr = %d.%02d dB\n", rec_buf[0],
0305         state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
0306 
0307     return 0;
0308 }
0309 
0310 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
0311 {
0312     /* Calculate Strength from SNR up to 35dB */
0313     /* Even though the SNR can go higher than 35dB, there is some comfort */
0314     /* factor in having a range of strong signals that can show at 100%   */
0315     struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
0316     u16 snr;
0317     int ret;
0318 
0319     ret = fe->ops.read_snr(fe, &snr);
0320     if (ret != 0)
0321         return ret;
0322     /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
0323     /* scale the range 0 - 35*2^24 into 0 - 65535 */
0324     if (state->snr >= 8960 * 0x10000)
0325         *strength = 0xffff;
0326     else
0327         *strength = state->snr / 8960;
0328 
0329     return 0;
0330 }
0331 
0332 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
0333 {
0334     *ber = -ENOSYS;
0335     return 0;
0336 }
0337 
0338 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
0339 {
0340     *ucblocks = -ENOSYS;
0341     return 0;
0342 }
0343 
0344 static int or51211_sleep(struct dvb_frontend* fe)
0345 {
0346     return 0;
0347 }
0348 
0349 static int or51211_init(struct dvb_frontend* fe)
0350 {
0351     struct or51211_state* state = fe->demodulator_priv;
0352     const struct or51211_config* config = state->config;
0353     const struct firmware* fw;
0354     unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
0355     unsigned char rec_buf[14];
0356     int ret,i;
0357 
0358     if (!state->initialized) {
0359         /* Request the firmware, this will block until it uploads */
0360         pr_info("Waiting for firmware upload (%s)...\n",
0361             OR51211_DEFAULT_FIRMWARE);
0362         ret = config->request_firmware(fe, &fw,
0363                            OR51211_DEFAULT_FIRMWARE);
0364         pr_info("Got Hotplug firmware\n");
0365         if (ret) {
0366             pr_warn("No firmware uploaded (timeout or file not found?)\n");
0367             return ret;
0368         }
0369 
0370         ret = or51211_load_firmware(fe, fw);
0371         release_firmware(fw);
0372         if (ret) {
0373             pr_warn("Writing firmware to device failed!\n");
0374             return ret;
0375         }
0376         pr_info("Firmware upload complete.\n");
0377 
0378         /* Set operation mode in Receiver 1 register;
0379          * type 1:
0380          * data 0x50h  Automatic sets receiver channel conditions
0381          *             Automatic NTSC rejection filter
0382          *             Enable  MPEG serial data output
0383          *             MPEG2tr
0384          *             High tuner phase noise
0385          *             normal +/-150kHz Carrier acquisition range
0386          */
0387         if (i2c_writebytes(state,state->config->demod_address,
0388                    cmd_buf,3)) {
0389             pr_warn("Load DVR Error 5\n");
0390             return -1;
0391         }
0392 
0393         /* Read back ucode version to besure we loaded correctly */
0394         /* and are really up and running */
0395         rec_buf[0] = 0x04;
0396         rec_buf[1] = 0x00;
0397         rec_buf[2] = 0x03;
0398         rec_buf[3] = 0x00;
0399         msleep(30);
0400         if (i2c_writebytes(state,state->config->demod_address,
0401                    rec_buf,3)) {
0402             pr_warn("Load DVR Error A\n");
0403             return -1;
0404         }
0405         msleep(3);
0406         if (i2c_readbytes(state,state->config->demod_address,
0407                   &rec_buf[10],2)) {
0408             pr_warn("Load DVR Error B\n");
0409             return -1;
0410         }
0411 
0412         rec_buf[0] = 0x04;
0413         rec_buf[1] = 0x00;
0414         rec_buf[2] = 0x01;
0415         rec_buf[3] = 0x00;
0416         msleep(20);
0417         if (i2c_writebytes(state,state->config->demod_address,
0418                    rec_buf,3)) {
0419             pr_warn("Load DVR Error C\n");
0420             return -1;
0421         }
0422         msleep(3);
0423         if (i2c_readbytes(state,state->config->demod_address,
0424                   &rec_buf[12],2)) {
0425             pr_warn("Load DVR Error D\n");
0426             return -1;
0427         }
0428 
0429         for (i = 0; i < 8; i++)
0430             rec_buf[i]=0xed;
0431 
0432         for (i = 0; i < 5; i++) {
0433             msleep(30);
0434             get_ver_buf[4] = i+1;
0435             if (i2c_writebytes(state,state->config->demod_address,
0436                        get_ver_buf,5)) {
0437                 pr_warn("Load DVR Error 6 - %d\n", i);
0438                 return -1;
0439             }
0440             msleep(3);
0441 
0442             if (i2c_readbytes(state,state->config->demod_address,
0443                       &rec_buf[i*2],2)) {
0444                 pr_warn("Load DVR Error 7 - %d\n", i);
0445                 return -1;
0446             }
0447             /* If we didn't receive the right index, try again */
0448             if ((int)rec_buf[i*2+1]!=i+1){
0449               i--;
0450             }
0451         }
0452         dprintk("read_fwbits %10ph\n", rec_buf);
0453 
0454         pr_info("ver TU%02x%02x%02x VSB mode %02x Status %02x\n",
0455             rec_buf[2], rec_buf[4], rec_buf[6], rec_buf[12],
0456             rec_buf[10]);
0457 
0458         rec_buf[0] = 0x04;
0459         rec_buf[1] = 0x00;
0460         rec_buf[2] = 0x03;
0461         rec_buf[3] = 0x00;
0462         msleep(20);
0463         if (i2c_writebytes(state,state->config->demod_address,
0464                    rec_buf,3)) {
0465             pr_warn("Load DVR Error 8\n");
0466             return -1;
0467         }
0468         msleep(20);
0469         if (i2c_readbytes(state,state->config->demod_address,
0470                   &rec_buf[8],2)) {
0471             pr_warn("Load DVR Error 9\n");
0472             return -1;
0473         }
0474         state->initialized = 1;
0475     }
0476 
0477     return 0;
0478 }
0479 
0480 static int or51211_get_tune_settings(struct dvb_frontend* fe,
0481                      struct dvb_frontend_tune_settings* fesettings)
0482 {
0483     fesettings->min_delay_ms = 500;
0484     fesettings->step_size = 0;
0485     fesettings->max_drift = 0;
0486     return 0;
0487 }
0488 
0489 static void or51211_release(struct dvb_frontend* fe)
0490 {
0491     struct or51211_state* state = fe->demodulator_priv;
0492     state->config->sleep(fe);
0493     kfree(state);
0494 }
0495 
0496 static const struct dvb_frontend_ops or51211_ops;
0497 
0498 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
0499                     struct i2c_adapter* i2c)
0500 {
0501     struct or51211_state* state = NULL;
0502 
0503     /* Allocate memory for the internal state */
0504     state = kzalloc(sizeof(struct or51211_state), GFP_KERNEL);
0505     if (state == NULL)
0506         return NULL;
0507 
0508     /* Setup the state */
0509     state->config = config;
0510     state->i2c = i2c;
0511     state->initialized = 0;
0512     state->current_frequency = 0;
0513 
0514     /* Create dvb_frontend */
0515     memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
0516     state->frontend.demodulator_priv = state;
0517     return &state->frontend;
0518 }
0519 
0520 static const struct dvb_frontend_ops or51211_ops = {
0521     .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
0522     .info = {
0523         .name                  = "Oren OR51211 VSB Frontend",
0524         .frequency_min_hz      =  44 * MHz,
0525         .frequency_max_hz      = 958 * MHz,
0526         .frequency_stepsize_hz = 166666,
0527         .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
0528             FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
0529             FE_CAN_8VSB
0530     },
0531 
0532     .release = or51211_release,
0533 
0534     .init = or51211_init,
0535     .sleep = or51211_sleep,
0536 
0537     .set_frontend = or51211_set_parameters,
0538     .get_tune_settings = or51211_get_tune_settings,
0539 
0540     .read_status = or51211_read_status,
0541     .read_ber = or51211_read_ber,
0542     .read_signal_strength = or51211_read_signal_strength,
0543     .read_snr = or51211_read_snr,
0544     .read_ucblocks = or51211_read_ucblocks,
0545 };
0546 
0547 module_param(debug, int, 0644);
0548 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
0549 
0550 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
0551 MODULE_AUTHOR("Kirk Lapray");
0552 MODULE_LICENSE("GPL");
0553 
0554 EXPORT_SYMBOL(or51211_attach);
0555