174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29a0bf528SMauro Carvalho Chehab /*
39a0bf528SMauro Carvalho Chehab Driver for ST STV0299 demodulator
49a0bf528SMauro Carvalho Chehab
59a0bf528SMauro Carvalho Chehab Copyright (C) 2001-2002 Convergence Integrated Media GmbH
69a0bf528SMauro Carvalho Chehab <ralph@convergence.de>,
79a0bf528SMauro Carvalho Chehab <holger@convergence.de>,
89a0bf528SMauro Carvalho Chehab <js@convergence.de>
99a0bf528SMauro Carvalho Chehab
109a0bf528SMauro Carvalho Chehab
119a0bf528SMauro Carvalho Chehab Philips SU1278/SH
129a0bf528SMauro Carvalho Chehab
139a0bf528SMauro Carvalho Chehab Copyright (C) 2002 by Peter Schildmann <peter.schildmann@web.de>
149a0bf528SMauro Carvalho Chehab
159a0bf528SMauro Carvalho Chehab
169a0bf528SMauro Carvalho Chehab LG TDQF-S001F
179a0bf528SMauro Carvalho Chehab
189a0bf528SMauro Carvalho Chehab Copyright (C) 2002 Felix Domke <tmbinc@elitedvb.net>
199a0bf528SMauro Carvalho Chehab & Andreas Oberritter <obi@linuxtv.org>
209a0bf528SMauro Carvalho Chehab
219a0bf528SMauro Carvalho Chehab
229a0bf528SMauro Carvalho Chehab Support for Samsung TBMU24112IMB used on Technisat SkyStar2 rev. 2.6B
239a0bf528SMauro Carvalho Chehab
249a0bf528SMauro Carvalho Chehab Copyright (C) 2003 Vadim Catana <skystar@moldova.cc>:
259a0bf528SMauro Carvalho Chehab
269a0bf528SMauro Carvalho Chehab Support for Philips SU1278 on Technotrend hardware
279a0bf528SMauro Carvalho Chehab
289a0bf528SMauro Carvalho Chehab Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
299a0bf528SMauro Carvalho Chehab
309a0bf528SMauro Carvalho Chehab
319a0bf528SMauro Carvalho Chehab */
329a0bf528SMauro Carvalho Chehab
339a0bf528SMauro Carvalho Chehab #include <linux/init.h>
349a0bf528SMauro Carvalho Chehab #include <linux/kernel.h>
359056a23bSTina Ruchandani #include <linux/ktime.h>
369a0bf528SMauro Carvalho Chehab #include <linux/module.h>
379a0bf528SMauro Carvalho Chehab #include <linux/string.h>
389a0bf528SMauro Carvalho Chehab #include <linux/slab.h>
399a0bf528SMauro Carvalho Chehab #include <linux/jiffies.h>
409a0bf528SMauro Carvalho Chehab #include <asm/div64.h>
419a0bf528SMauro Carvalho Chehab
42fada1935SMauro Carvalho Chehab #include <media/dvb_frontend.h>
439a0bf528SMauro Carvalho Chehab #include "stv0299.h"
449a0bf528SMauro Carvalho Chehab
459a0bf528SMauro Carvalho Chehab struct stv0299_state {
469a0bf528SMauro Carvalho Chehab struct i2c_adapter* i2c;
479a0bf528SMauro Carvalho Chehab const struct stv0299_config* config;
489a0bf528SMauro Carvalho Chehab struct dvb_frontend frontend;
499a0bf528SMauro Carvalho Chehab
509a0bf528SMauro Carvalho Chehab u8 initialised:1;
519a0bf528SMauro Carvalho Chehab u32 tuner_frequency;
529a0bf528SMauro Carvalho Chehab u32 symbol_rate;
530df289a2SMauro Carvalho Chehab enum fe_code_rate fec_inner;
549a0bf528SMauro Carvalho Chehab int errmode;
559a0bf528SMauro Carvalho Chehab u32 ucblocks;
569a0bf528SMauro Carvalho Chehab u8 mcr_reg;
579a0bf528SMauro Carvalho Chehab };
589a0bf528SMauro Carvalho Chehab
599a0bf528SMauro Carvalho Chehab #define STATUS_BER 0
609a0bf528SMauro Carvalho Chehab #define STATUS_UCBLOCKS 1
619a0bf528SMauro Carvalho Chehab
629a0bf528SMauro Carvalho Chehab static int debug;
639a0bf528SMauro Carvalho Chehab static int debug_legacy_dish_switch;
649a0bf528SMauro Carvalho Chehab #define dprintk(args...) \
659a0bf528SMauro Carvalho Chehab do { \
669a0bf528SMauro Carvalho Chehab if (debug) printk(KERN_DEBUG "stv0299: " args); \
679a0bf528SMauro Carvalho Chehab } while (0)
689a0bf528SMauro Carvalho Chehab
699a0bf528SMauro Carvalho Chehab
stv0299_writeregI(struct stv0299_state * state,u8 reg,u8 data)709a0bf528SMauro Carvalho Chehab static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data)
719a0bf528SMauro Carvalho Chehab {
729a0bf528SMauro Carvalho Chehab int ret;
739a0bf528SMauro Carvalho Chehab u8 buf [] = { reg, data };
749a0bf528SMauro Carvalho Chehab struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
759a0bf528SMauro Carvalho Chehab
769a0bf528SMauro Carvalho Chehab ret = i2c_transfer (state->i2c, &msg, 1);
779a0bf528SMauro Carvalho Chehab
789a0bf528SMauro Carvalho Chehab if (ret != 1)
794bd69e7bSMauro Carvalho Chehab dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
804bd69e7bSMauro Carvalho Chehab __func__, reg, data, ret);
819a0bf528SMauro Carvalho Chehab
829a0bf528SMauro Carvalho Chehab return (ret != 1) ? -EREMOTEIO : 0;
839a0bf528SMauro Carvalho Chehab }
849a0bf528SMauro Carvalho Chehab
stv0299_write(struct dvb_frontend * fe,const u8 buf[],int len)859a0bf528SMauro Carvalho Chehab static int stv0299_write(struct dvb_frontend* fe, const u8 buf[], int len)
869a0bf528SMauro Carvalho Chehab {
879a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
889a0bf528SMauro Carvalho Chehab
899a0bf528SMauro Carvalho Chehab if (len != 2)
909a0bf528SMauro Carvalho Chehab return -EINVAL;
919a0bf528SMauro Carvalho Chehab
929a0bf528SMauro Carvalho Chehab return stv0299_writeregI(state, buf[0], buf[1]);
939a0bf528SMauro Carvalho Chehab }
949a0bf528SMauro Carvalho Chehab
stv0299_readreg(struct stv0299_state * state,u8 reg)959a0bf528SMauro Carvalho Chehab static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
969a0bf528SMauro Carvalho Chehab {
979a0bf528SMauro Carvalho Chehab int ret;
989a0bf528SMauro Carvalho Chehab u8 b0 [] = { reg };
999a0bf528SMauro Carvalho Chehab u8 b1 [] = { 0 };
1009a0bf528SMauro Carvalho Chehab struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
1019a0bf528SMauro Carvalho Chehab { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
1029a0bf528SMauro Carvalho Chehab
1039a0bf528SMauro Carvalho Chehab ret = i2c_transfer (state->i2c, msg, 2);
1049a0bf528SMauro Carvalho Chehab
1059a0bf528SMauro Carvalho Chehab if (ret != 2)
1069a0bf528SMauro Carvalho Chehab dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
1079a0bf528SMauro Carvalho Chehab __func__, reg, ret);
1089a0bf528SMauro Carvalho Chehab
1099a0bf528SMauro Carvalho Chehab return b1[0];
1109a0bf528SMauro Carvalho Chehab }
1119a0bf528SMauro Carvalho Chehab
stv0299_readregs(struct stv0299_state * state,u8 reg1,u8 * b,u8 len)1129a0bf528SMauro Carvalho Chehab static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
1139a0bf528SMauro Carvalho Chehab {
1149a0bf528SMauro Carvalho Chehab int ret;
1159a0bf528SMauro Carvalho Chehab struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®1, .len = 1 },
1169a0bf528SMauro Carvalho Chehab { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
1179a0bf528SMauro Carvalho Chehab
1189a0bf528SMauro Carvalho Chehab ret = i2c_transfer (state->i2c, msg, 2);
1199a0bf528SMauro Carvalho Chehab
1209a0bf528SMauro Carvalho Chehab if (ret != 2)
1219a0bf528SMauro Carvalho Chehab dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
1229a0bf528SMauro Carvalho Chehab
1239a0bf528SMauro Carvalho Chehab return ret == 2 ? 0 : ret;
1249a0bf528SMauro Carvalho Chehab }
1259a0bf528SMauro Carvalho Chehab
stv0299_set_FEC(struct stv0299_state * state,enum fe_code_rate fec)1260df289a2SMauro Carvalho Chehab static int stv0299_set_FEC(struct stv0299_state *state, enum fe_code_rate fec)
1279a0bf528SMauro Carvalho Chehab {
1289a0bf528SMauro Carvalho Chehab dprintk ("%s\n", __func__);
1299a0bf528SMauro Carvalho Chehab
1309a0bf528SMauro Carvalho Chehab switch (fec) {
1319a0bf528SMauro Carvalho Chehab case FEC_AUTO:
1329a0bf528SMauro Carvalho Chehab {
1339a0bf528SMauro Carvalho Chehab return stv0299_writeregI (state, 0x31, 0x1f);
1349a0bf528SMauro Carvalho Chehab }
1359a0bf528SMauro Carvalho Chehab case FEC_1_2:
1369a0bf528SMauro Carvalho Chehab {
1379a0bf528SMauro Carvalho Chehab return stv0299_writeregI (state, 0x31, 0x01);
1389a0bf528SMauro Carvalho Chehab }
1399a0bf528SMauro Carvalho Chehab case FEC_2_3:
1409a0bf528SMauro Carvalho Chehab {
1419a0bf528SMauro Carvalho Chehab return stv0299_writeregI (state, 0x31, 0x02);
1429a0bf528SMauro Carvalho Chehab }
1439a0bf528SMauro Carvalho Chehab case FEC_3_4:
1449a0bf528SMauro Carvalho Chehab {
1459a0bf528SMauro Carvalho Chehab return stv0299_writeregI (state, 0x31, 0x04);
1469a0bf528SMauro Carvalho Chehab }
1479a0bf528SMauro Carvalho Chehab case FEC_5_6:
1489a0bf528SMauro Carvalho Chehab {
1499a0bf528SMauro Carvalho Chehab return stv0299_writeregI (state, 0x31, 0x08);
1509a0bf528SMauro Carvalho Chehab }
1519a0bf528SMauro Carvalho Chehab case FEC_7_8:
1529a0bf528SMauro Carvalho Chehab {
1539a0bf528SMauro Carvalho Chehab return stv0299_writeregI (state, 0x31, 0x10);
1549a0bf528SMauro Carvalho Chehab }
1559a0bf528SMauro Carvalho Chehab default:
1569a0bf528SMauro Carvalho Chehab {
1579a0bf528SMauro Carvalho Chehab return -EINVAL;
1589a0bf528SMauro Carvalho Chehab }
1599a0bf528SMauro Carvalho Chehab }
1609a0bf528SMauro Carvalho Chehab }
1619a0bf528SMauro Carvalho Chehab
stv0299_get_fec(struct stv0299_state * state)1620df289a2SMauro Carvalho Chehab static enum fe_code_rate stv0299_get_fec(struct stv0299_state *state)
1639a0bf528SMauro Carvalho Chehab {
164f99c4ad3SColin Ian King static const enum fe_code_rate fec_tab[] = {
165f99c4ad3SColin Ian King FEC_2_3, FEC_3_4, FEC_5_6, FEC_7_8, FEC_1_2
166f99c4ad3SColin Ian King };
1679a0bf528SMauro Carvalho Chehab u8 index;
1689a0bf528SMauro Carvalho Chehab
1699a0bf528SMauro Carvalho Chehab dprintk ("%s\n", __func__);
1709a0bf528SMauro Carvalho Chehab
1719a0bf528SMauro Carvalho Chehab index = stv0299_readreg (state, 0x1b);
1729a0bf528SMauro Carvalho Chehab index &= 0x7;
1739a0bf528SMauro Carvalho Chehab
1749a0bf528SMauro Carvalho Chehab if (index > 4)
1759a0bf528SMauro Carvalho Chehab return FEC_AUTO;
1769a0bf528SMauro Carvalho Chehab
1779a0bf528SMauro Carvalho Chehab return fec_tab [index];
1789a0bf528SMauro Carvalho Chehab }
1799a0bf528SMauro Carvalho Chehab
stv0299_wait_diseqc_fifo(struct stv0299_state * state,int timeout)1809a0bf528SMauro Carvalho Chehab static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
1819a0bf528SMauro Carvalho Chehab {
1829a0bf528SMauro Carvalho Chehab unsigned long start = jiffies;
1839a0bf528SMauro Carvalho Chehab
1849a0bf528SMauro Carvalho Chehab dprintk ("%s\n", __func__);
1859a0bf528SMauro Carvalho Chehab
1869a0bf528SMauro Carvalho Chehab while (stv0299_readreg(state, 0x0a) & 1) {
1870ce32a2cSWang Qing if (time_is_before_jiffies(start + timeout)) {
1889a0bf528SMauro Carvalho Chehab dprintk ("%s: timeout!!\n", __func__);
1899a0bf528SMauro Carvalho Chehab return -ETIMEDOUT;
1909a0bf528SMauro Carvalho Chehab }
1919a0bf528SMauro Carvalho Chehab msleep(10);
192c2c1b415SPeter Senna Tschudin }
1939a0bf528SMauro Carvalho Chehab
1949a0bf528SMauro Carvalho Chehab return 0;
1959a0bf528SMauro Carvalho Chehab }
1969a0bf528SMauro Carvalho Chehab
stv0299_wait_diseqc_idle(struct stv0299_state * state,int timeout)1979a0bf528SMauro Carvalho Chehab static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
1989a0bf528SMauro Carvalho Chehab {
1999a0bf528SMauro Carvalho Chehab unsigned long start = jiffies;
2009a0bf528SMauro Carvalho Chehab
2019a0bf528SMauro Carvalho Chehab dprintk ("%s\n", __func__);
2029a0bf528SMauro Carvalho Chehab
2039a0bf528SMauro Carvalho Chehab while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
2040ce32a2cSWang Qing if (time_is_before_jiffies(start + timeout)) {
2059a0bf528SMauro Carvalho Chehab dprintk ("%s: timeout!!\n", __func__);
2069a0bf528SMauro Carvalho Chehab return -ETIMEDOUT;
2079a0bf528SMauro Carvalho Chehab }
2089a0bf528SMauro Carvalho Chehab msleep(10);
209c2c1b415SPeter Senna Tschudin }
2109a0bf528SMauro Carvalho Chehab
2119a0bf528SMauro Carvalho Chehab return 0;
2129a0bf528SMauro Carvalho Chehab }
2139a0bf528SMauro Carvalho Chehab
stv0299_set_symbolrate(struct dvb_frontend * fe,u32 srate)2149a0bf528SMauro Carvalho Chehab static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
2159a0bf528SMauro Carvalho Chehab {
2169a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
2179a0bf528SMauro Carvalho Chehab u64 big = srate;
2189a0bf528SMauro Carvalho Chehab u32 ratio;
2199a0bf528SMauro Carvalho Chehab
2209a0bf528SMauro Carvalho Chehab // check rate is within limits
2219a0bf528SMauro Carvalho Chehab if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
2229a0bf528SMauro Carvalho Chehab
2239a0bf528SMauro Carvalho Chehab // calculate value to program
2249a0bf528SMauro Carvalho Chehab big = big << 20;
2259a0bf528SMauro Carvalho Chehab big += (state->config->mclk-1); // round correctly
2269a0bf528SMauro Carvalho Chehab do_div(big, state->config->mclk);
2279a0bf528SMauro Carvalho Chehab ratio = big << 4;
2289a0bf528SMauro Carvalho Chehab
2299a0bf528SMauro Carvalho Chehab return state->config->set_symbol_rate(fe, srate, ratio);
2309a0bf528SMauro Carvalho Chehab }
2319a0bf528SMauro Carvalho Chehab
stv0299_get_symbolrate(struct stv0299_state * state)2329a0bf528SMauro Carvalho Chehab static int stv0299_get_symbolrate (struct stv0299_state* state)
2339a0bf528SMauro Carvalho Chehab {
2349a0bf528SMauro Carvalho Chehab u32 Mclk = state->config->mclk / 4096L;
2359a0bf528SMauro Carvalho Chehab u32 srate;
2369a0bf528SMauro Carvalho Chehab s32 offset;
2379a0bf528SMauro Carvalho Chehab u8 sfr[3];
2389a0bf528SMauro Carvalho Chehab s8 rtf;
2399a0bf528SMauro Carvalho Chehab
2409a0bf528SMauro Carvalho Chehab dprintk ("%s\n", __func__);
2419a0bf528SMauro Carvalho Chehab
2429a0bf528SMauro Carvalho Chehab stv0299_readregs (state, 0x1f, sfr, 3);
2439a0bf528SMauro Carvalho Chehab stv0299_readregs (state, 0x1a, (u8 *)&rtf, 1);
2449a0bf528SMauro Carvalho Chehab
2459a0bf528SMauro Carvalho Chehab srate = (sfr[0] << 8) | sfr[1];
2469a0bf528SMauro Carvalho Chehab srate *= Mclk;
2479a0bf528SMauro Carvalho Chehab srate /= 16;
2489a0bf528SMauro Carvalho Chehab srate += (sfr[2] >> 4) * Mclk / 256;
2499a0bf528SMauro Carvalho Chehab offset = (s32) rtf * (srate / 4096L);
2509a0bf528SMauro Carvalho Chehab offset /= 128;
2519a0bf528SMauro Carvalho Chehab
2529a0bf528SMauro Carvalho Chehab dprintk ("%s : srate = %i\n", __func__, srate);
2539a0bf528SMauro Carvalho Chehab dprintk ("%s : ofset = %i\n", __func__, offset);
2549a0bf528SMauro Carvalho Chehab
2559a0bf528SMauro Carvalho Chehab srate += offset;
2569a0bf528SMauro Carvalho Chehab
2579a0bf528SMauro Carvalho Chehab srate += 1000;
2589a0bf528SMauro Carvalho Chehab srate /= 2000;
2599a0bf528SMauro Carvalho Chehab srate *= 2000;
2609a0bf528SMauro Carvalho Chehab
2619a0bf528SMauro Carvalho Chehab return srate;
2629a0bf528SMauro Carvalho Chehab }
2639a0bf528SMauro Carvalho Chehab
stv0299_send_diseqc_msg(struct dvb_frontend * fe,struct dvb_diseqc_master_cmd * m)2649a0bf528SMauro Carvalho Chehab static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
2659a0bf528SMauro Carvalho Chehab struct dvb_diseqc_master_cmd *m)
2669a0bf528SMauro Carvalho Chehab {
2679a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
2689a0bf528SMauro Carvalho Chehab u8 val;
2699a0bf528SMauro Carvalho Chehab int i;
2709a0bf528SMauro Carvalho Chehab
2719a0bf528SMauro Carvalho Chehab dprintk ("%s\n", __func__);
2729a0bf528SMauro Carvalho Chehab
2739a0bf528SMauro Carvalho Chehab if (stv0299_wait_diseqc_idle (state, 100) < 0)
2749a0bf528SMauro Carvalho Chehab return -ETIMEDOUT;
2759a0bf528SMauro Carvalho Chehab
2769a0bf528SMauro Carvalho Chehab val = stv0299_readreg (state, 0x08);
2779a0bf528SMauro Carvalho Chehab
2789a0bf528SMauro Carvalho Chehab if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6)) /* DiSEqC mode */
2799a0bf528SMauro Carvalho Chehab return -EREMOTEIO;
2809a0bf528SMauro Carvalho Chehab
2819a0bf528SMauro Carvalho Chehab for (i=0; i<m->msg_len; i++) {
2829a0bf528SMauro Carvalho Chehab if (stv0299_wait_diseqc_fifo (state, 100) < 0)
2839a0bf528SMauro Carvalho Chehab return -ETIMEDOUT;
2849a0bf528SMauro Carvalho Chehab
2859a0bf528SMauro Carvalho Chehab if (stv0299_writeregI (state, 0x09, m->msg[i]))
2869a0bf528SMauro Carvalho Chehab return -EREMOTEIO;
2879a0bf528SMauro Carvalho Chehab }
2889a0bf528SMauro Carvalho Chehab
2899a0bf528SMauro Carvalho Chehab if (stv0299_wait_diseqc_idle (state, 100) < 0)
2909a0bf528SMauro Carvalho Chehab return -ETIMEDOUT;
2919a0bf528SMauro Carvalho Chehab
2929a0bf528SMauro Carvalho Chehab return 0;
2939a0bf528SMauro Carvalho Chehab }
2949a0bf528SMauro Carvalho Chehab
stv0299_send_diseqc_burst(struct dvb_frontend * fe,enum fe_sec_mini_cmd burst)2950df289a2SMauro Carvalho Chehab static int stv0299_send_diseqc_burst(struct dvb_frontend *fe,
2960df289a2SMauro Carvalho Chehab enum fe_sec_mini_cmd burst)
2979a0bf528SMauro Carvalho Chehab {
2989a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
2999a0bf528SMauro Carvalho Chehab u8 val;
3009a0bf528SMauro Carvalho Chehab
3019a0bf528SMauro Carvalho Chehab dprintk ("%s\n", __func__);
3029a0bf528SMauro Carvalho Chehab
3039a0bf528SMauro Carvalho Chehab if (stv0299_wait_diseqc_idle (state, 100) < 0)
3049a0bf528SMauro Carvalho Chehab return -ETIMEDOUT;
3059a0bf528SMauro Carvalho Chehab
3069a0bf528SMauro Carvalho Chehab val = stv0299_readreg (state, 0x08);
3079a0bf528SMauro Carvalho Chehab
3089a0bf528SMauro Carvalho Chehab if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */
3099a0bf528SMauro Carvalho Chehab return -EREMOTEIO;
3109a0bf528SMauro Carvalho Chehab
3119a0bf528SMauro Carvalho Chehab if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
3129a0bf528SMauro Carvalho Chehab return -EREMOTEIO;
3139a0bf528SMauro Carvalho Chehab
3149a0bf528SMauro Carvalho Chehab if (stv0299_wait_diseqc_idle (state, 100) < 0)
3159a0bf528SMauro Carvalho Chehab return -ETIMEDOUT;
3169a0bf528SMauro Carvalho Chehab
3179a0bf528SMauro Carvalho Chehab if (stv0299_writeregI (state, 0x08, val))
3189a0bf528SMauro Carvalho Chehab return -EREMOTEIO;
3199a0bf528SMauro Carvalho Chehab
3209a0bf528SMauro Carvalho Chehab return 0;
3219a0bf528SMauro Carvalho Chehab }
3229a0bf528SMauro Carvalho Chehab
stv0299_set_tone(struct dvb_frontend * fe,enum fe_sec_tone_mode tone)3230df289a2SMauro Carvalho Chehab static int stv0299_set_tone(struct dvb_frontend *fe,
3240df289a2SMauro Carvalho Chehab enum fe_sec_tone_mode tone)
3259a0bf528SMauro Carvalho Chehab {
3269a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
3279a0bf528SMauro Carvalho Chehab u8 val;
3289a0bf528SMauro Carvalho Chehab
3299a0bf528SMauro Carvalho Chehab if (stv0299_wait_diseqc_idle (state, 100) < 0)
3309a0bf528SMauro Carvalho Chehab return -ETIMEDOUT;
3319a0bf528SMauro Carvalho Chehab
3329a0bf528SMauro Carvalho Chehab val = stv0299_readreg (state, 0x08);
3339a0bf528SMauro Carvalho Chehab
3349a0bf528SMauro Carvalho Chehab switch (tone) {
3359a0bf528SMauro Carvalho Chehab case SEC_TONE_ON:
3369a0bf528SMauro Carvalho Chehab return stv0299_writeregI (state, 0x08, val | 0x3);
3379a0bf528SMauro Carvalho Chehab
3389a0bf528SMauro Carvalho Chehab case SEC_TONE_OFF:
3399a0bf528SMauro Carvalho Chehab return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);
3409a0bf528SMauro Carvalho Chehab
3419a0bf528SMauro Carvalho Chehab default:
3429a0bf528SMauro Carvalho Chehab return -EINVAL;
3439a0bf528SMauro Carvalho Chehab }
3449a0bf528SMauro Carvalho Chehab }
3459a0bf528SMauro Carvalho Chehab
stv0299_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)3460df289a2SMauro Carvalho Chehab static int stv0299_set_voltage(struct dvb_frontend *fe,
3470df289a2SMauro Carvalho Chehab enum fe_sec_voltage voltage)
3489a0bf528SMauro Carvalho Chehab {
3499a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
3509a0bf528SMauro Carvalho Chehab u8 reg0x08;
3519a0bf528SMauro Carvalho Chehab u8 reg0x0c;
3529a0bf528SMauro Carvalho Chehab
3539a0bf528SMauro Carvalho Chehab dprintk("%s: %s\n", __func__,
3549a0bf528SMauro Carvalho Chehab voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
3559a0bf528SMauro Carvalho Chehab voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
3569a0bf528SMauro Carvalho Chehab
3579a0bf528SMauro Carvalho Chehab reg0x08 = stv0299_readreg (state, 0x08);
3589a0bf528SMauro Carvalho Chehab reg0x0c = stv0299_readreg (state, 0x0c);
3599a0bf528SMauro Carvalho Chehab
360b95b0c98SMauro Carvalho Chehab /*
3619a0bf528SMauro Carvalho Chehab * H/V switching over OP0, OP1 and OP2 are LNB power enable bits
3629a0bf528SMauro Carvalho Chehab */
3639a0bf528SMauro Carvalho Chehab reg0x0c &= 0x0f;
3649a0bf528SMauro Carvalho Chehab reg0x08 = (reg0x08 & 0x3f) | (state->config->lock_output << 6);
3659a0bf528SMauro Carvalho Chehab
3669a0bf528SMauro Carvalho Chehab switch (voltage) {
3679a0bf528SMauro Carvalho Chehab case SEC_VOLTAGE_13:
3689a0bf528SMauro Carvalho Chehab if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
3699a0bf528SMauro Carvalho Chehab reg0x0c |= 0x10; /* OP1 off, OP0 on */
3709a0bf528SMauro Carvalho Chehab else
3719a0bf528SMauro Carvalho Chehab reg0x0c |= 0x40; /* OP1 on, OP0 off */
3729a0bf528SMauro Carvalho Chehab break;
3739a0bf528SMauro Carvalho Chehab case SEC_VOLTAGE_18:
3749a0bf528SMauro Carvalho Chehab reg0x0c |= 0x50; /* OP1 on, OP0 on */
3759a0bf528SMauro Carvalho Chehab break;
3769a0bf528SMauro Carvalho Chehab case SEC_VOLTAGE_OFF:
3779a0bf528SMauro Carvalho Chehab /* LNB power off! */
3789a0bf528SMauro Carvalho Chehab reg0x08 = 0x00;
3799a0bf528SMauro Carvalho Chehab reg0x0c = 0x00;
3809a0bf528SMauro Carvalho Chehab break;
3819a0bf528SMauro Carvalho Chehab default:
3829a0bf528SMauro Carvalho Chehab return -EINVAL;
383c2c1b415SPeter Senna Tschudin }
3849a0bf528SMauro Carvalho Chehab
3859a0bf528SMauro Carvalho Chehab if (state->config->op0_off)
3869a0bf528SMauro Carvalho Chehab reg0x0c &= ~0x10;
3879a0bf528SMauro Carvalho Chehab
3889a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, 0x08, reg0x08);
3899a0bf528SMauro Carvalho Chehab return stv0299_writeregI(state, 0x0c, reg0x0c);
3909a0bf528SMauro Carvalho Chehab }
3919a0bf528SMauro Carvalho Chehab
stv0299_send_legacy_dish_cmd(struct dvb_frontend * fe,unsigned long cmd)3929a0bf528SMauro Carvalho Chehab static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd)
3939a0bf528SMauro Carvalho Chehab {
3949a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
3959a0bf528SMauro Carvalho Chehab u8 reg0x08;
3969a0bf528SMauro Carvalho Chehab u8 reg0x0c;
3979a0bf528SMauro Carvalho Chehab u8 lv_mask = 0x40;
3989a0bf528SMauro Carvalho Chehab u8 last = 1;
3999a0bf528SMauro Carvalho Chehab int i;
4009056a23bSTina Ruchandani ktime_t nexttime;
4019056a23bSTina Ruchandani ktime_t tv[10];
4029a0bf528SMauro Carvalho Chehab
4039a0bf528SMauro Carvalho Chehab reg0x08 = stv0299_readreg (state, 0x08);
4049a0bf528SMauro Carvalho Chehab reg0x0c = stv0299_readreg (state, 0x0c);
4059a0bf528SMauro Carvalho Chehab reg0x0c &= 0x0f;
4069a0bf528SMauro Carvalho Chehab stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
4079a0bf528SMauro Carvalho Chehab if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
4089a0bf528SMauro Carvalho Chehab lv_mask = 0x10;
4099a0bf528SMauro Carvalho Chehab
4109a0bf528SMauro Carvalho Chehab cmd = cmd << 1;
4119a0bf528SMauro Carvalho Chehab if (debug_legacy_dish_switch)
4129a0bf528SMauro Carvalho Chehab printk ("%s switch command: 0x%04lx\n",__func__, cmd);
4139a0bf528SMauro Carvalho Chehab
4146b3f9998SAbhilash Jindal nexttime = ktime_get_boottime();
4159a0bf528SMauro Carvalho Chehab if (debug_legacy_dish_switch)
416ee45ddc1SEzequiel Garcia tv[0] = nexttime;
4179a0bf528SMauro Carvalho Chehab stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */
4189a0bf528SMauro Carvalho Chehab
4199a0bf528SMauro Carvalho Chehab dvb_frontend_sleep_until(&nexttime, 32000);
4209a0bf528SMauro Carvalho Chehab
4219a0bf528SMauro Carvalho Chehab for (i=0; i<9; i++) {
4229a0bf528SMauro Carvalho Chehab if (debug_legacy_dish_switch)
4236b3f9998SAbhilash Jindal tv[i+1] = ktime_get_boottime();
4249a0bf528SMauro Carvalho Chehab if((cmd & 0x01) != last) {
4259a0bf528SMauro Carvalho Chehab /* set voltage to (last ? 13V : 18V) */
4269a0bf528SMauro Carvalho Chehab stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50));
4279a0bf528SMauro Carvalho Chehab last = (last) ? 0 : 1;
4289a0bf528SMauro Carvalho Chehab }
4299a0bf528SMauro Carvalho Chehab
4309a0bf528SMauro Carvalho Chehab cmd = cmd >> 1;
4319a0bf528SMauro Carvalho Chehab
4329a0bf528SMauro Carvalho Chehab if (i != 8)
4339a0bf528SMauro Carvalho Chehab dvb_frontend_sleep_until(&nexttime, 8000);
4349a0bf528SMauro Carvalho Chehab }
4359a0bf528SMauro Carvalho Chehab if (debug_legacy_dish_switch) {
4369a0bf528SMauro Carvalho Chehab printk ("%s(%d): switch delay (should be 32k followed by all 8k\n",
4379a0bf528SMauro Carvalho Chehab __func__, fe->dvb->num);
4389a0bf528SMauro Carvalho Chehab for (i = 1; i < 10; i++)
4399056a23bSTina Ruchandani printk("%d: %d\n", i,
4409056a23bSTina Ruchandani (int) ktime_us_delta(tv[i], tv[i-1]));
4419a0bf528SMauro Carvalho Chehab }
4429a0bf528SMauro Carvalho Chehab
4439a0bf528SMauro Carvalho Chehab return 0;
4449a0bf528SMauro Carvalho Chehab }
4459a0bf528SMauro Carvalho Chehab
stv0299_init(struct dvb_frontend * fe)4469a0bf528SMauro Carvalho Chehab static int stv0299_init (struct dvb_frontend* fe)
4479a0bf528SMauro Carvalho Chehab {
4489a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
4499a0bf528SMauro Carvalho Chehab int i;
4509a0bf528SMauro Carvalho Chehab u8 reg;
4519a0bf528SMauro Carvalho Chehab u8 val;
4529a0bf528SMauro Carvalho Chehab
4539a0bf528SMauro Carvalho Chehab dprintk("stv0299: init chip\n");
4549a0bf528SMauro Carvalho Chehab
4559a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, 0x02, 0x30 | state->mcr_reg);
4569a0bf528SMauro Carvalho Chehab msleep(50);
4579a0bf528SMauro Carvalho Chehab
4589a0bf528SMauro Carvalho Chehab for (i = 0; ; i += 2) {
4599a0bf528SMauro Carvalho Chehab reg = state->config->inittab[i];
4609a0bf528SMauro Carvalho Chehab val = state->config->inittab[i+1];
4619a0bf528SMauro Carvalho Chehab if (reg == 0xff && val == 0xff)
4629a0bf528SMauro Carvalho Chehab break;
4639a0bf528SMauro Carvalho Chehab if (reg == 0x0c && state->config->op0_off)
4649a0bf528SMauro Carvalho Chehab val &= ~0x10;
4659a0bf528SMauro Carvalho Chehab if (reg == 0x2)
4669a0bf528SMauro Carvalho Chehab state->mcr_reg = val & 0xf;
4679a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, reg, val);
4689a0bf528SMauro Carvalho Chehab }
4699a0bf528SMauro Carvalho Chehab
4709a0bf528SMauro Carvalho Chehab return 0;
4719a0bf528SMauro Carvalho Chehab }
4729a0bf528SMauro Carvalho Chehab
stv0299_read_status(struct dvb_frontend * fe,enum fe_status * status)4730df289a2SMauro Carvalho Chehab static int stv0299_read_status(struct dvb_frontend *fe,
4740df289a2SMauro Carvalho Chehab enum fe_status *status)
4759a0bf528SMauro Carvalho Chehab {
4769a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
4779a0bf528SMauro Carvalho Chehab
4789a0bf528SMauro Carvalho Chehab u8 signal = 0xff - stv0299_readreg (state, 0x18);
4799a0bf528SMauro Carvalho Chehab u8 sync = stv0299_readreg (state, 0x1b);
4809a0bf528SMauro Carvalho Chehab
4819a0bf528SMauro Carvalho Chehab dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
4829a0bf528SMauro Carvalho Chehab *status = 0;
4839a0bf528SMauro Carvalho Chehab
4849a0bf528SMauro Carvalho Chehab if (signal > 10)
4859a0bf528SMauro Carvalho Chehab *status |= FE_HAS_SIGNAL;
4869a0bf528SMauro Carvalho Chehab
4879a0bf528SMauro Carvalho Chehab if (sync & 0x80)
4889a0bf528SMauro Carvalho Chehab *status |= FE_HAS_CARRIER;
4899a0bf528SMauro Carvalho Chehab
4909a0bf528SMauro Carvalho Chehab if (sync & 0x10)
4919a0bf528SMauro Carvalho Chehab *status |= FE_HAS_VITERBI;
4929a0bf528SMauro Carvalho Chehab
4939a0bf528SMauro Carvalho Chehab if (sync & 0x08)
4949a0bf528SMauro Carvalho Chehab *status |= FE_HAS_SYNC;
4959a0bf528SMauro Carvalho Chehab
4969a0bf528SMauro Carvalho Chehab if ((sync & 0x98) == 0x98)
4979a0bf528SMauro Carvalho Chehab *status |= FE_HAS_LOCK;
4989a0bf528SMauro Carvalho Chehab
4999a0bf528SMauro Carvalho Chehab return 0;
5009a0bf528SMauro Carvalho Chehab }
5019a0bf528SMauro Carvalho Chehab
stv0299_read_ber(struct dvb_frontend * fe,u32 * ber)5029a0bf528SMauro Carvalho Chehab static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
5039a0bf528SMauro Carvalho Chehab {
5049a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
5059a0bf528SMauro Carvalho Chehab
5069a0bf528SMauro Carvalho Chehab if (state->errmode != STATUS_BER)
5079a0bf528SMauro Carvalho Chehab return -ENOSYS;
5089a0bf528SMauro Carvalho Chehab
5099a0bf528SMauro Carvalho Chehab *ber = stv0299_readreg(state, 0x1e) | (stv0299_readreg(state, 0x1d) << 8);
5109a0bf528SMauro Carvalho Chehab
5119a0bf528SMauro Carvalho Chehab return 0;
5129a0bf528SMauro Carvalho Chehab }
5139a0bf528SMauro Carvalho Chehab
stv0299_read_signal_strength(struct dvb_frontend * fe,u16 * strength)5149a0bf528SMauro Carvalho Chehab static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
5159a0bf528SMauro Carvalho Chehab {
5169a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
5179a0bf528SMauro Carvalho Chehab
5189a0bf528SMauro Carvalho Chehab s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8)
5199a0bf528SMauro Carvalho Chehab | stv0299_readreg (state, 0x19));
5209a0bf528SMauro Carvalho Chehab
5219a0bf528SMauro Carvalho Chehab dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __func__,
5229a0bf528SMauro Carvalho Chehab stv0299_readreg (state, 0x18),
5239a0bf528SMauro Carvalho Chehab stv0299_readreg (state, 0x19), (int) signal);
5249a0bf528SMauro Carvalho Chehab
5259a0bf528SMauro Carvalho Chehab signal = signal * 5 / 4;
5269a0bf528SMauro Carvalho Chehab *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
5279a0bf528SMauro Carvalho Chehab
5289a0bf528SMauro Carvalho Chehab return 0;
5299a0bf528SMauro Carvalho Chehab }
5309a0bf528SMauro Carvalho Chehab
stv0299_read_snr(struct dvb_frontend * fe,u16 * snr)5319a0bf528SMauro Carvalho Chehab static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
5329a0bf528SMauro Carvalho Chehab {
5339a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
5349a0bf528SMauro Carvalho Chehab
5359a0bf528SMauro Carvalho Chehab s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
5369a0bf528SMauro Carvalho Chehab | stv0299_readreg (state, 0x25));
5379a0bf528SMauro Carvalho Chehab xsnr = 3 * (xsnr - 0xa100);
5389a0bf528SMauro Carvalho Chehab *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
5399a0bf528SMauro Carvalho Chehab
5409a0bf528SMauro Carvalho Chehab return 0;
5419a0bf528SMauro Carvalho Chehab }
5429a0bf528SMauro Carvalho Chehab
stv0299_read_ucblocks(struct dvb_frontend * fe,u32 * ucblocks)5439a0bf528SMauro Carvalho Chehab static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
5449a0bf528SMauro Carvalho Chehab {
5459a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
5469a0bf528SMauro Carvalho Chehab
5479a0bf528SMauro Carvalho Chehab if (state->errmode != STATUS_UCBLOCKS)
5489a0bf528SMauro Carvalho Chehab return -ENOSYS;
5499a0bf528SMauro Carvalho Chehab
5509a0bf528SMauro Carvalho Chehab state->ucblocks += stv0299_readreg(state, 0x1e);
5519a0bf528SMauro Carvalho Chehab state->ucblocks += (stv0299_readreg(state, 0x1d) << 8);
5529a0bf528SMauro Carvalho Chehab *ucblocks = state->ucblocks;
5539a0bf528SMauro Carvalho Chehab
5549a0bf528SMauro Carvalho Chehab return 0;
5559a0bf528SMauro Carvalho Chehab }
5569a0bf528SMauro Carvalho Chehab
stv0299_set_frontend(struct dvb_frontend * fe)5579a0bf528SMauro Carvalho Chehab static int stv0299_set_frontend(struct dvb_frontend *fe)
5589a0bf528SMauro Carvalho Chehab {
5599a0bf528SMauro Carvalho Chehab struct dtv_frontend_properties *p = &fe->dtv_property_cache;
5609a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
5619a0bf528SMauro Carvalho Chehab int invval = 0;
5629a0bf528SMauro Carvalho Chehab
5639a0bf528SMauro Carvalho Chehab dprintk ("%s : FE_SET_FRONTEND\n", __func__);
5649a0bf528SMauro Carvalho Chehab if (state->config->set_ts_params)
5659a0bf528SMauro Carvalho Chehab state->config->set_ts_params(fe, 0);
5669a0bf528SMauro Carvalho Chehab
5679a0bf528SMauro Carvalho Chehab // set the inversion
5689a0bf528SMauro Carvalho Chehab if (p->inversion == INVERSION_OFF) invval = 0;
5699a0bf528SMauro Carvalho Chehab else if (p->inversion == INVERSION_ON) invval = 1;
5709a0bf528SMauro Carvalho Chehab else {
5719a0bf528SMauro Carvalho Chehab printk("stv0299 does not support auto-inversion\n");
5729a0bf528SMauro Carvalho Chehab return -EINVAL;
5739a0bf528SMauro Carvalho Chehab }
5749a0bf528SMauro Carvalho Chehab if (state->config->invert) invval = (~invval) & 1;
5759a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval);
5769a0bf528SMauro Carvalho Chehab
5779a0bf528SMauro Carvalho Chehab if (fe->ops.tuner_ops.set_params) {
5789a0bf528SMauro Carvalho Chehab fe->ops.tuner_ops.set_params(fe);
5799a0bf528SMauro Carvalho Chehab if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
5809a0bf528SMauro Carvalho Chehab }
5819a0bf528SMauro Carvalho Chehab
5829a0bf528SMauro Carvalho Chehab stv0299_set_FEC(state, p->fec_inner);
5839a0bf528SMauro Carvalho Chehab stv0299_set_symbolrate(fe, p->symbol_rate);
5849a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, 0x22, 0x00);
5859a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, 0x23, 0x00);
5869a0bf528SMauro Carvalho Chehab
5879a0bf528SMauro Carvalho Chehab state->tuner_frequency = p->frequency;
5889a0bf528SMauro Carvalho Chehab state->fec_inner = p->fec_inner;
5899a0bf528SMauro Carvalho Chehab state->symbol_rate = p->symbol_rate;
5909a0bf528SMauro Carvalho Chehab
5919a0bf528SMauro Carvalho Chehab return 0;
5929a0bf528SMauro Carvalho Chehab }
5939a0bf528SMauro Carvalho Chehab
stv0299_get_frontend(struct dvb_frontend * fe,struct dtv_frontend_properties * p)5947e3e68bcSMauro Carvalho Chehab static int stv0299_get_frontend(struct dvb_frontend *fe,
5957e3e68bcSMauro Carvalho Chehab struct dtv_frontend_properties *p)
5969a0bf528SMauro Carvalho Chehab {
5979a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
5989a0bf528SMauro Carvalho Chehab s32 derot_freq;
5999a0bf528SMauro Carvalho Chehab int invval;
6009a0bf528SMauro Carvalho Chehab
6019a0bf528SMauro Carvalho Chehab derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
6029a0bf528SMauro Carvalho Chehab | stv0299_readreg (state, 0x23));
6039a0bf528SMauro Carvalho Chehab
6049a0bf528SMauro Carvalho Chehab derot_freq *= (state->config->mclk >> 16);
6059a0bf528SMauro Carvalho Chehab derot_freq += 500;
6069a0bf528SMauro Carvalho Chehab derot_freq /= 1000;
6079a0bf528SMauro Carvalho Chehab
6089a0bf528SMauro Carvalho Chehab p->frequency += derot_freq;
6099a0bf528SMauro Carvalho Chehab
6109a0bf528SMauro Carvalho Chehab invval = stv0299_readreg (state, 0x0c) & 1;
6119a0bf528SMauro Carvalho Chehab if (state->config->invert) invval = (~invval) & 1;
6129a0bf528SMauro Carvalho Chehab p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
6139a0bf528SMauro Carvalho Chehab
6149a0bf528SMauro Carvalho Chehab p->fec_inner = stv0299_get_fec(state);
6159a0bf528SMauro Carvalho Chehab p->symbol_rate = stv0299_get_symbolrate(state);
6169a0bf528SMauro Carvalho Chehab
6179a0bf528SMauro Carvalho Chehab return 0;
6189a0bf528SMauro Carvalho Chehab }
6199a0bf528SMauro Carvalho Chehab
stv0299_sleep(struct dvb_frontend * fe)6209a0bf528SMauro Carvalho Chehab static int stv0299_sleep(struct dvb_frontend* fe)
6219a0bf528SMauro Carvalho Chehab {
6229a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
6239a0bf528SMauro Carvalho Chehab
6249a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, 0x02, 0xb0 | state->mcr_reg);
6259a0bf528SMauro Carvalho Chehab state->initialised = 0;
6269a0bf528SMauro Carvalho Chehab
6279a0bf528SMauro Carvalho Chehab return 0;
6289a0bf528SMauro Carvalho Chehab }
6299a0bf528SMauro Carvalho Chehab
stv0299_i2c_gate_ctrl(struct dvb_frontend * fe,int enable)6309a0bf528SMauro Carvalho Chehab static int stv0299_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
6319a0bf528SMauro Carvalho Chehab {
6329a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
6339a0bf528SMauro Carvalho Chehab
6349a0bf528SMauro Carvalho Chehab if (enable) {
6359a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, 0x05, 0xb5);
6369a0bf528SMauro Carvalho Chehab } else {
6379a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, 0x05, 0x35);
6389a0bf528SMauro Carvalho Chehab }
6399a0bf528SMauro Carvalho Chehab udelay(1);
6409a0bf528SMauro Carvalho Chehab return 0;
6419a0bf528SMauro Carvalho Chehab }
6429a0bf528SMauro Carvalho Chehab
stv0299_get_tune_settings(struct dvb_frontend * fe,struct dvb_frontend_tune_settings * fesettings)6439a0bf528SMauro Carvalho Chehab static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
6449a0bf528SMauro Carvalho Chehab {
6459a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
6469a0bf528SMauro Carvalho Chehab struct dtv_frontend_properties *p = &fe->dtv_property_cache;
6479a0bf528SMauro Carvalho Chehab
6489a0bf528SMauro Carvalho Chehab fesettings->min_delay_ms = state->config->min_delay_ms;
6499a0bf528SMauro Carvalho Chehab if (p->symbol_rate < 10000000) {
6509a0bf528SMauro Carvalho Chehab fesettings->step_size = p->symbol_rate / 32000;
6519a0bf528SMauro Carvalho Chehab fesettings->max_drift = 5000;
6529a0bf528SMauro Carvalho Chehab } else {
6539a0bf528SMauro Carvalho Chehab fesettings->step_size = p->symbol_rate / 16000;
6549a0bf528SMauro Carvalho Chehab fesettings->max_drift = p->symbol_rate / 2000;
6559a0bf528SMauro Carvalho Chehab }
6569a0bf528SMauro Carvalho Chehab return 0;
6579a0bf528SMauro Carvalho Chehab }
6589a0bf528SMauro Carvalho Chehab
stv0299_release(struct dvb_frontend * fe)6599a0bf528SMauro Carvalho Chehab static void stv0299_release(struct dvb_frontend* fe)
6609a0bf528SMauro Carvalho Chehab {
6619a0bf528SMauro Carvalho Chehab struct stv0299_state* state = fe->demodulator_priv;
6629a0bf528SMauro Carvalho Chehab kfree(state);
6639a0bf528SMauro Carvalho Chehab }
6649a0bf528SMauro Carvalho Chehab
665bd336e63SMax Kellermann static const struct dvb_frontend_ops stv0299_ops;
6669a0bf528SMauro Carvalho Chehab
stv0299_attach(const struct stv0299_config * config,struct i2c_adapter * i2c)6679a0bf528SMauro Carvalho Chehab struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
6689a0bf528SMauro Carvalho Chehab struct i2c_adapter* i2c)
6699a0bf528SMauro Carvalho Chehab {
6709a0bf528SMauro Carvalho Chehab struct stv0299_state* state = NULL;
6719a0bf528SMauro Carvalho Chehab int id;
6729a0bf528SMauro Carvalho Chehab
6739a0bf528SMauro Carvalho Chehab /* allocate memory for the internal state */
6749a0bf528SMauro Carvalho Chehab state = kzalloc(sizeof(struct stv0299_state), GFP_KERNEL);
6759a0bf528SMauro Carvalho Chehab if (state == NULL) goto error;
6769a0bf528SMauro Carvalho Chehab
6779a0bf528SMauro Carvalho Chehab /* setup the state */
6789a0bf528SMauro Carvalho Chehab state->config = config;
6799a0bf528SMauro Carvalho Chehab state->i2c = i2c;
6809a0bf528SMauro Carvalho Chehab state->initialised = 0;
6819a0bf528SMauro Carvalho Chehab state->tuner_frequency = 0;
6829a0bf528SMauro Carvalho Chehab state->symbol_rate = 0;
6839a0bf528SMauro Carvalho Chehab state->fec_inner = 0;
6849a0bf528SMauro Carvalho Chehab state->errmode = STATUS_BER;
6859a0bf528SMauro Carvalho Chehab
6869a0bf528SMauro Carvalho Chehab /* check if the demod is there */
6879a0bf528SMauro Carvalho Chehab stv0299_writeregI(state, 0x02, 0x30); /* standby off */
6889a0bf528SMauro Carvalho Chehab msleep(200);
6899a0bf528SMauro Carvalho Chehab id = stv0299_readreg(state, 0x00);
6909a0bf528SMauro Carvalho Chehab
6919a0bf528SMauro Carvalho Chehab /* register 0x00 contains 0xa1 for STV0299 and STV0299B */
6929a0bf528SMauro Carvalho Chehab /* register 0x00 might contain 0x80 when returning from standby */
6939a0bf528SMauro Carvalho Chehab if (id != 0xa1 && id != 0x80) goto error;
6949a0bf528SMauro Carvalho Chehab
6959a0bf528SMauro Carvalho Chehab /* create dvb_frontend */
6969a0bf528SMauro Carvalho Chehab memcpy(&state->frontend.ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
6979a0bf528SMauro Carvalho Chehab state->frontend.demodulator_priv = state;
6989a0bf528SMauro Carvalho Chehab return &state->frontend;
6999a0bf528SMauro Carvalho Chehab
7009a0bf528SMauro Carvalho Chehab error:
7019a0bf528SMauro Carvalho Chehab kfree(state);
7029a0bf528SMauro Carvalho Chehab return NULL;
7039a0bf528SMauro Carvalho Chehab }
7049a0bf528SMauro Carvalho Chehab
705bd336e63SMax Kellermann static const struct dvb_frontend_ops stv0299_ops = {
7069a0bf528SMauro Carvalho Chehab .delsys = { SYS_DVBS },
7079a0bf528SMauro Carvalho Chehab .info = {
7089a0bf528SMauro Carvalho Chehab .name = "ST STV0299 DVB-S",
709f1b1eabfSMauro Carvalho Chehab .frequency_min_hz = 950 * MHz,
710f1b1eabfSMauro Carvalho Chehab .frequency_max_hz = 2150 * MHz,
711f1b1eabfSMauro Carvalho Chehab .frequency_stepsize_hz = 125 * kHz,
7129a0bf528SMauro Carvalho Chehab .symbol_rate_min = 1000000,
7139a0bf528SMauro Carvalho Chehab .symbol_rate_max = 45000000,
7149a0bf528SMauro Carvalho Chehab .symbol_rate_tolerance = 500, /* ppm */
7159a0bf528SMauro Carvalho Chehab .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
7169a0bf528SMauro Carvalho Chehab FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
7179a0bf528SMauro Carvalho Chehab FE_CAN_QPSK |
7189a0bf528SMauro Carvalho Chehab FE_CAN_FEC_AUTO
7199a0bf528SMauro Carvalho Chehab },
7209a0bf528SMauro Carvalho Chehab
7219a0bf528SMauro Carvalho Chehab .release = stv0299_release,
7229a0bf528SMauro Carvalho Chehab
7239a0bf528SMauro Carvalho Chehab .init = stv0299_init,
7249a0bf528SMauro Carvalho Chehab .sleep = stv0299_sleep,
7259a0bf528SMauro Carvalho Chehab .write = stv0299_write,
7269a0bf528SMauro Carvalho Chehab .i2c_gate_ctrl = stv0299_i2c_gate_ctrl,
7279a0bf528SMauro Carvalho Chehab
7289a0bf528SMauro Carvalho Chehab .set_frontend = stv0299_set_frontend,
7299a0bf528SMauro Carvalho Chehab .get_frontend = stv0299_get_frontend,
7309a0bf528SMauro Carvalho Chehab .get_tune_settings = stv0299_get_tune_settings,
7319a0bf528SMauro Carvalho Chehab
7329a0bf528SMauro Carvalho Chehab .read_status = stv0299_read_status,
7339a0bf528SMauro Carvalho Chehab .read_ber = stv0299_read_ber,
7349a0bf528SMauro Carvalho Chehab .read_signal_strength = stv0299_read_signal_strength,
7359a0bf528SMauro Carvalho Chehab .read_snr = stv0299_read_snr,
7369a0bf528SMauro Carvalho Chehab .read_ucblocks = stv0299_read_ucblocks,
7379a0bf528SMauro Carvalho Chehab
7389a0bf528SMauro Carvalho Chehab .diseqc_send_master_cmd = stv0299_send_diseqc_msg,
7399a0bf528SMauro Carvalho Chehab .diseqc_send_burst = stv0299_send_diseqc_burst,
7409a0bf528SMauro Carvalho Chehab .set_tone = stv0299_set_tone,
7419a0bf528SMauro Carvalho Chehab .set_voltage = stv0299_set_voltage,
7429a0bf528SMauro Carvalho Chehab .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
7439a0bf528SMauro Carvalho Chehab };
7449a0bf528SMauro Carvalho Chehab
7459a0bf528SMauro Carvalho Chehab module_param(debug_legacy_dish_switch, int, 0444);
7469a0bf528SMauro Carvalho Chehab MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches");
7479a0bf528SMauro Carvalho Chehab
7489a0bf528SMauro Carvalho Chehab module_param(debug, int, 0644);
7499a0bf528SMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
7509a0bf528SMauro Carvalho Chehab
7519a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
7524bd69e7bSMauro Carvalho Chehab MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, Andreas Oberritter, Andrew de Quincey, Kenneth Aafly");
7539a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
7549a0bf528SMauro Carvalho Chehab
755*86495af1SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(stv0299_attach);
756