// SPDX-License-Identifier: GPL-2.0
/*
* Driver for the ST STV6111 tuner
*
* Copyright (C) 2014 Digital Devices GmbH
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <asm /div64.h>
#include "stv6111.h"
#include <media/dvb_frontend.h>
struct stv {
struct i2c_adapter *i2c;
u8 adr;
u8 reg[11 ];
u32 ref_freq;
u32 frequency;
};
struct slookup {
s16 value;
u16 reg_value;
};
static const struct slookup lnagain_nf_lookup[] = {
/* Gain *100dB // Reg */
{ 2572 , 0 },
{ 2575 , 1 },
{ 2580 , 2 },
{ 2588 , 3 },
{ 2596 , 4 },
{ 2611 , 5 },
{ 2633 , 6 },
{ 2664 , 7 },
{ 2701 , 8 },
{ 2753 , 9 },
{ 2816 , 10 },
{ 2902 , 11 },
{ 2995 , 12 },
{ 3104 , 13 },
{ 3215 , 14 },
{ 3337 , 15 },
{ 3492 , 16 },
{ 3614 , 17 },
{ 3731 , 18 },
{ 3861 , 19 },
{ 3988 , 20 },
{ 4124 , 21 },
{ 4253 , 22 },
{ 4386 , 23 },
{ 4505 , 24 },
{ 4623 , 25 },
{ 4726 , 26 },
{ 4821 , 27 },
{ 4903 , 28 },
{ 4979 , 29 },
{ 5045 , 30 },
{ 5102 , 31 }
};
static const struct slookup lnagain_iip3_lookup[] = {
/* Gain *100dB // reg */
{ 1548 , 0 },
{ 1552 , 1 },
{ 1569 , 2 },
{ 1565 , 3 },
{ 1577 , 4 },
{ 1594 , 5 },
{ 1627 , 6 },
{ 1656 , 7 },
{ 1700 , 8 },
{ 1748 , 9 },
{ 1805 , 10 },
{ 1896 , 11 },
{ 1995 , 12 },
{ 2113 , 13 },
{ 2233 , 14 },
{ 2366 , 15 },
{ 2543 , 16 },
{ 2687 , 17 },
{ 2842 , 18 },
{ 2999 , 19 },
{ 3167 , 20 },
{ 3342 , 21 },
{ 3507 , 22 },
{ 3679 , 23 },
{ 3827 , 24 },
{ 3970 , 25 },
{ 4094 , 26 },
{ 4210 , 27 },
{ 4308 , 28 },
{ 4396 , 29 },
{ 4468 , 30 },
{ 4535 , 31 }
};
static const struct slookup gain_rfagc_lookup[] = {
/* Gain *100dB // reg */
{ 4870 , 0 x3000 },
{ 4850 , 0 x3C00 },
{ 4800 , 0 x4500 },
{ 4750 , 0 x4800 },
{ 4700 , 0 x4B00 },
{ 4650 , 0 x4D00 },
{ 4600 , 0 x4F00 },
{ 4550 , 0 x5100 },
{ 4500 , 0 x5200 },
{ 4420 , 0 x5500 },
{ 4316 , 0 x5800 },
{ 4200 , 0 x5B00 },
{ 4119 , 0 x5D00 },
{ 3999 , 0 x6000 },
{ 3950 , 0 x6100 },
{ 3876 , 0 x6300 },
{ 3755 , 0 x6600 },
{ 3641 , 0 x6900 },
{ 3567 , 0 x6B00 },
{ 3425 , 0 x6F00 },
{ 3350 , 0 x7100 },
{ 3236 , 0 x7400 },
{ 3118 , 0 x7700 },
{ 3004 , 0 x7A00 },
{ 2917 , 0 x7C00 },
{ 2776 , 0 x7F00 },
{ 2635 , 0 x8200 },
{ 2516 , 0 x8500 },
{ 2406 , 0 x8800 },
{ 2290 , 0 x8B00 },
{ 2170 , 0 x8E00 },
{ 2073 , 0 x9100 },
{ 1949 , 0 x9400 },
{ 1836 , 0 x9700 },
{ 1712 , 0 x9A00 },
{ 1631 , 0 x9C00 },
{ 1515 , 0 x9F00 },
{ 1400 , 0 xA200 },
{ 1323 , 0 xA400 },
{ 1203 , 0 xA700 },
{ 1091 , 0 xAA00 },
{ 1011 , 0 xAC00 },
{ 904 , 0 xAF00 },
{ 787 , 0 xB200 },
{ 685 , 0 xB500 },
{ 571 , 0 xB800 },
{ 464 , 0 xBB00 },
{ 374 , 0 xBE00 },
{ 275 , 0 xC200 },
{ 181 , 0 xC600 },
{ 102 , 0 xCC00 },
{ 49 , 0 xD900 }
};
/*
* This table is 6 dB too low compared to the others (probably created with
* a different BB_MAG setting)
*/
static const struct slookup gain_channel_agc_nf_lookup[] = {
/* Gain *100dB // reg */
{ 7082 , 0 x3000 },
{ 7052 , 0 x4000 },
{ 7007 , 0 x4600 },
{ 6954 , 0 x4A00 },
{ 6909 , 0 x4D00 },
{ 6833 , 0 x5100 },
{ 6753 , 0 x5400 },
{ 6659 , 0 x5700 },
{ 6561 , 0 x5A00 },
{ 6472 , 0 x5C00 },
{ 6366 , 0 x5F00 },
{ 6259 , 0 x6100 },
{ 6151 , 0 x6400 },
{ 6026 , 0 x6700 },
{ 5920 , 0 x6900 },
{ 5835 , 0 x6B00 },
{ 5770 , 0 x6C00 },
{ 5681 , 0 x6E00 },
{ 5596 , 0 x7000 },
{ 5503 , 0 x7200 },
{ 5429 , 0 x7300 },
{ 5319 , 0 x7500 },
{ 5220 , 0 x7700 },
{ 5111 , 0 x7900 },
{ 4983 , 0 x7B00 },
{ 4876 , 0 x7D00 },
{ 4755 , 0 x7F00 },
{ 4635 , 0 x8100 },
{ 4499 , 0 x8300 },
{ 4405 , 0 x8500 },
{ 4323 , 0 x8600 },
{ 4233 , 0 x8800 },
{ 4156 , 0 x8A00 },
{ 4038 , 0 x8C00 },
{ 3935 , 0 x8E00 },
{ 3823 , 0 x9000 },
{ 3712 , 0 x9200 },
{ 3601 , 0 x9500 },
{ 3511 , 0 x9700 },
{ 3413 , 0 x9900 },
{ 3309 , 0 x9B00 },
{ 3213 , 0 x9D00 },
{ 3088 , 0 x9F00 },
{ 2992 , 0 xA100 },
{ 2878 , 0 xA400 },
{ 2769 , 0 xA700 },
{ 2645 , 0 xAA00 },
{ 2538 , 0 xAD00 },
{ 2441 , 0 xB000 },
{ 2350 , 0 xB600 },
{ 2237 , 0 xBA00 },
{ 2137 , 0 xBF00 },
{ 2039 , 0 xC500 },
{ 1938 , 0 xDF00 },
{ 1927 , 0 xFF00 }
};
static const struct slookup gain_channel_agc_iip3_lookup[] = {
/* Gain *100dB // reg */
{ 7070 , 0 x3000 },
{ 7028 , 0 x4000 },
{ 7019 , 0 x4600 },
{ 6900 , 0 x4A00 },
{ 6811 , 0 x4D00 },
{ 6763 , 0 x5100 },
{ 6690 , 0 x5400 },
{ 6644 , 0 x5700 },
{ 6617 , 0 x5A00 },
{ 6598 , 0 x5C00 },
{ 6462 , 0 x5F00 },
{ 6348 , 0 x6100 },
{ 6197 , 0 x6400 },
{ 6154 , 0 x6700 },
{ 6098 , 0 x6900 },
{ 5893 , 0 x6B00 },
{ 5812 , 0 x6C00 },
{ 5773 , 0 x6E00 },
{ 5723 , 0 x7000 },
{ 5661 , 0 x7200 },
{ 5579 , 0 x7300 },
{ 5460 , 0 x7500 },
{ 5308 , 0 x7700 },
{ 5099 , 0 x7900 },
{ 4910 , 0 x7B00 },
{ 4800 , 0 x7D00 },
{ 4785 , 0 x7F00 },
{ 4635 , 0 x8100 },
{ 4466 , 0 x8300 },
{ 4314 , 0 x8500 },
{ 4295 , 0 x8600 },
{ 4144 , 0 x8800 },
{ 3920 , 0 x8A00 },
{ 3889 , 0 x8C00 },
{ 3771 , 0 x8E00 },
{ 3655 , 0 x9000 },
{ 3446 , 0 x9200 },
{ 3298 , 0 x9500 },
{ 3083 , 0 x9700 },
{ 3015 , 0 x9900 },
{ 2833 , 0 x9B00 },
{ 2746 , 0 x9D00 },
{ 2632 , 0 x9F00 },
{ 2598 , 0 xA100 },
{ 2480 , 0 xA400 },
{ 2236 , 0 xA700 },
{ 2171 , 0 xAA00 },
{ 2060 , 0 xAD00 },
{ 1999 , 0 xB000 },
{ 1974 , 0 xB600 },
{ 1820 , 0 xBA00 },
{ 1741 , 0 xBF00 },
{ 1655 , 0 xC500 },
{ 1444 , 0 xDF00 },
{ 1325 , 0 xFF00 },
};
static inline u32 muldiv32(u32 a, u32 b, u32 c)
{
u64 tmp64;
tmp64 = (u64)a * (u64)b;
do_div(tmp64, c);
return (u32)tmp64;
}
static int i2c_read(struct i2c_adapter *adap,
u8 adr, u8 *msg, int len, u8 *answ, int alen)
{
struct i2c_msg msgs[2 ] = { { .addr = adr, .flags = 0 ,
.buf = msg, .len = len},
{ .addr = adr, .flags = I2C_M_RD,
.buf = answ, .len = alen } };
if (i2c_transfer(adap, msgs, 2 ) != 2 ) {
dev_err(&adap->dev, "i2c read error\n" );
return -EIO;
}
return 0 ;
}
static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len)
{
struct i2c_msg msg = {.addr = adr, .flags = 0 ,
.buf = data, .len = len};
if (i2c_transfer(adap, &msg, 1 ) != 1 ) {
dev_err(&adap->dev, "i2c write error\n" );
return -EIO;
}
return 0 ;
}
static int write_regs(struct stv *state, int reg, int len)
{
u8 d[12 ];
memcpy(&d[1 ], &state->reg[reg], len);
d[0 ] = reg;
return i2c_write(state->i2c, state->adr, d, len + 1 );
}
static int write_reg(struct stv *state, u8 reg, u8 val)
{
u8 d[2 ] = {reg, val};
return i2c_write(state->i2c, state->adr, d, 2 );
}
static int read_reg(struct stv *state, u8 reg, u8 *val)
{
return i2c_read(state->i2c, state->adr, ®, 1 , val, 1 );
}
static int wait_for_call_done(struct stv *state, u8 mask)
{
int status = 0 ;
u32 lock_retry_count = 10 ;
while (lock_retry_count > 0 ) {
u8 regval;
status = read_reg(state, 9 , ®val);
if (status < 0 )
return status;
if ((regval & mask) == 0 )
break ;
usleep_range(4000 , 6000 );
lock_retry_count -= 1 ;
status = -EIO;
}
return status;
}
static void init_state(struct stv *state)
{
u32 clkdiv = 0 ;
u32 agcmode = 0 ;
u32 agcref = 2 ;
u32 agcset = 0 xffffffff;
u32 bbmode = 0 xffffffff;
state->reg[0 ] = 0 x08;
state->reg[1 ] = 0 x41;
state->reg[2 ] = 0 x8f;
state->reg[3 ] = 0 x00;
state->reg[4 ] = 0 xce;
state->reg[5 ] = 0 x54;
state->reg[6 ] = 0 x55;
state->reg[7 ] = 0 x45;
state->reg[8 ] = 0 x46;
state->reg[9 ] = 0 xbd;
state->reg[10 ] = 0 x11;
state->ref_freq = 16000 ;
if (clkdiv <= 3 )
state->reg[0 x00] |= (clkdiv & 0 x03);
if (agcmode <= 3 ) {
state->reg[0 x03] |= (agcmode << 5 );
if (agcmode == 0 x01)
state->reg[0 x01] |= 0 x30;
}
if (bbmode <= 3 )
state->reg[0 x01] = (state->reg[0 x01] & ~0 x30) | (bbmode << 4 );
if (agcref <= 7 )
state->reg[0 x03] |= agcref;
if (agcset <= 31 )
state->reg[0 x02] = (state->reg[0 x02] & ~0 x1F) | agcset | 0 x40;
}
static int attach_init(struct stv *state)
{
if (write_regs(state, 0 , 11 ))
return -ENODEV;
return 0 ;
}
static void release(struct dvb_frontend *fe)
{
kfree(fe->tuner_priv);
fe->tuner_priv = NULL;
}
static int set_bandwidth(struct dvb_frontend *fe, u32 cutoff_frequency)
{
struct stv *state = fe->tuner_priv;
u32 index = (cutoff_frequency + 999999 ) / 1000000 ;
int stat = 0 ;
if (index < 6 )
index = 6 ;
if (index > 50 )
index = 50 ;
if ((state->reg[0 x08] & ~0 xFC) == ((index - 6 ) << 2 ))
return 0 ;
state->reg[0 x08] = (state->reg[0 x08] & ~0 xFC) | ((index - 6 ) << 2 );
state->reg[0 x09] = (state->reg[0 x09] & ~0 x0C) | 0 x08;
if (fe->ops.i2c_gate_ctrl)
stat = fe->ops.i2c_gate_ctrl(fe, 1 );
if (!stat) {
write_regs(state, 0 x08, 2 );
wait_for_call_done(state, 0 x08);
}
if (fe->ops.i2c_gate_ctrl && !stat)
fe->ops.i2c_gate_ctrl(fe, 0 );
return stat;
}
static int set_lof(struct stv *state, u32 local_frequency, u32 cutoff_frequency)
{
u32 index = (cutoff_frequency + 999999 ) / 1000000 ;
u32 frequency = (local_frequency + 500 ) / 1000 ;
u32 p = 1 , psel = 0 , fvco, div, frac;
u8 icp, tmp;
if (index < 6 )
index = 6 ;
if (index > 50 )
index = 50 ;
if (frequency <= 1300000 ) {
p = 4 ;
psel = 1 ;
} else {
p = 2 ;
psel = 0 ;
}
fvco = frequency * p;
div = fvco / state->ref_freq;
frac = fvco % state->ref_freq;
frac = muldiv32(frac, 0 x40000, state->ref_freq);
icp = 0 ;
if (fvco < 2700000 )
icp = 0 ;
else if (fvco < 2950000 )
icp = 1 ;
else if (fvco < 3300000 )
icp = 2 ;
else if (fvco < 3700000 )
icp = 3 ;
else if (fvco < 4200000 )
icp = 5 ;
else if (fvco < 4800000 )
icp = 6 ;
else
icp = 7 ;
state->reg[0 x02] |= 0 x80; /* LNA IIP3 Mode */
state->reg[0 x03] = (state->reg[0 x03] & ~0 x80) | (psel << 7 );
state->reg[0 x04] = (div & 0 xFF);
state->reg[0 x05] = (((div >> 8 ) & 0 x01) | ((frac & 0 x7F) << 1 )) & 0 xff;
state->reg[0 x06] = ((frac >> 7 ) & 0 xFF);
state->reg[0 x07] = (state->reg[0 x07] & ~0 x07) | ((frac >> 15 ) & 0 x07);
state->reg[0 x07] = (state->reg[0 x07] & ~0 xE0) | (icp << 5 );
state->reg[0 x08] = (state->reg[0 x08] & ~0 xFC) | ((index - 6 ) << 2 );
/* Start cal vco,CF */
state->reg[0 x09] = (state->reg[0 x09] & ~0 x0C) | 0 x0C;
write_regs(state, 2 , 8 );
wait_for_call_done(state, 0 x0C);
usleep_range(10000 , 12000 );
read_reg(state, 0 x03, &tmp);
if (tmp & 0 x10) {
state->reg[0 x02] &= ~0 x80; /* LNA NF Mode */
write_regs(state, 2 , 1 );
}
read_reg(state, 0 x08, &tmp);
state->frequency = frequency;
return 0 ;
}
static int set_params(struct dvb_frontend *fe)
{
struct stv *state = fe->tuner_priv;
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
u32 freq, cutoff;
int stat = 0 ;
if (p->delivery_system != SYS_DVBS && p->delivery_system != SYS_DVBS2)
return -EINVAL;
freq = p->frequency * 1000 ;
cutoff = 5000000 + muldiv32(p->symbol_rate, 135 , 200 );
if (fe->ops.i2c_gate_ctrl)
stat = fe->ops.i2c_gate_ctrl(fe, 1 );
if (!stat)
set_lof(state, freq, cutoff);
if (fe->ops.i2c_gate_ctrl && !stat)
fe->ops.i2c_gate_ctrl(fe, 0 );
return 0 ;
}
static s32 table_lookup(const struct slookup *table,
int table_size, u16 reg_value)
{
s32 gain;
s32 reg_diff;
int imin = 0 ;
int imax = table_size - 1 ;
int i;
/* Assumes Table[0].RegValue < Table[imax].RegValue */
if (reg_value <= table[0 ].reg_value) {
gain = table[0 ].value;
} else if (reg_value >= table[imax].reg_value) {
gain = table[imax].value;
} else {
while ((imax - imin) > 1 ) {
i = (imax + imin) / 2 ;
if ((table[imin].reg_value <= reg_value) &&
(reg_value <= table[i].reg_value))
imax = i;
else
imin = i;
}
reg_diff = table[imax].reg_value - table[imin].reg_value;
gain = table[imin].value;
if (reg_diff != 0 )
gain += ((s32)(reg_value - table[imin].reg_value) *
(s32)(table[imax].value
- table[imin].value)) / reg_diff;
}
return gain;
}
static int get_rf_strength(struct dvb_frontend *fe, u16 *st)
{
struct stv *state = fe->tuner_priv;
u16 rfagc = *st;
s32 gain;
if ((state->reg[0 x03] & 0 x60) == 0 ) {
/* RF Mode, Read AGC ADC */
u8 reg = 0 ;
int stat = 0 ;
if (fe->ops.i2c_gate_ctrl)
stat = fe->ops.i2c_gate_ctrl(fe, 1 );
if (!stat) {
write_reg(state, 0 x02, state->reg[0 x02] | 0 x20);
read_reg(state, 2 , ®);
if (reg & 0 x20)
read_reg(state, 2 , ®);
}
if (fe->ops.i2c_gate_ctrl && !stat)
fe->ops.i2c_gate_ctrl(fe, 0 );
if ((state->reg[0 x02] & 0 x80) == 0 )
/* NF */
gain = table_lookup(lnagain_nf_lookup,
ARRAY_SIZE(lnagain_nf_lookup),
reg & 0 x1F);
else
/* IIP3 */
gain = table_lookup(lnagain_iip3_lookup,
ARRAY_SIZE(lnagain_iip3_lookup),
reg & 0 x1F);
gain += table_lookup(gain_rfagc_lookup,
ARRAY_SIZE(gain_rfagc_lookup), rfagc);
gain -= 2400 ;
} else {
/* Channel Mode */
if ((state->reg[0 x02] & 0 x80) == 0 ) {
/* NF */
gain = table_lookup(
gain_channel_agc_nf_lookup,
ARRAY_SIZE(gain_channel_agc_nf_lookup), rfagc);
gain += 600 ;
} else {
/* IIP3 */
gain = table_lookup(
gain_channel_agc_iip3_lookup,
ARRAY_SIZE(gain_channel_agc_iip3_lookup),
rfagc);
}
}
if (state->frequency > 0 )
/* Tilt correction ( 0.00016 dB/MHz ) */
gain -= ((((s32)(state->frequency / 1000 ) - 1550 ) * 2 ) / 12 );
/* + (BBGain * 10); */
gain += (s32)((state->reg[0 x01] & 0 xC0) >> 6 ) * 600 - 1300 ;
if (gain < 0 )
gain = 0 ;
else if (gain > 10000 )
gain = 10000 ;
*st = 10000 - gain;
return 0 ;
}
static const struct dvb_tuner_ops tuner_ops = {
.info = {
.name = "ST STV6111" ,
.frequency_min_hz = 950 * MHz,
.frequency_max_hz = 2150 * MHz,
},
.set_params = set_params,
.release = release,
.get_rf_strength = get_rf_strength,
.set_bandwidth = set_bandwidth,
};
struct dvb_frontend *stv6111_attach(struct dvb_frontend *fe,
struct i2c_adapter *i2c, u8 adr)
{
struct stv *state;
int stat = -ENODEV;
int gatestat = 0 ;
state = kzalloc(sizeof (*state), GFP_KERNEL);
if (!state)
return NULL;
state->adr = adr;
state->i2c = i2c;
memcpy(&fe->ops.tuner_ops, &tuner_ops, sizeof (struct dvb_tuner_ops));
init_state(state);
if (fe->ops.i2c_gate_ctrl)
gatestat = fe->ops.i2c_gate_ctrl(fe, 1 );
if (!gatestat)
stat = attach_init(state);
if (fe->ops.i2c_gate_ctrl && !gatestat)
fe->ops.i2c_gate_ctrl(fe, 0 );
if (stat < 0 ) {
kfree(state);
return NULL;
}
fe->tuner_priv = state;
return fe;
}
EXPORT_SYMBOL_GPL(stv6111_attach);
MODULE_DESCRIPTION("ST STV6111 satellite tuner driver" );
MODULE_AUTHOR("Ralph Metzler, Manfred Voelkel" );
MODULE_LICENSE("GPL v2" );
Messung V0.5 in Prozent C=91 H=96 G=93
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-07)
¤
*© Formatika GbR, Deutschland