1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the ST STV6111 tuner
5 * Copyright (C) 2014 Digital Devices GmbH
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
14 #include <linux/i2c.h>
15 #include <asm/div64.h>
19 #include <media/dvb_frontend.h>
22 struct i2c_adapter *i2c;
35 static const struct slookup lnagain_nf_lookup[] = {
36 /* Gain *100dB // Reg */
71 static const struct slookup lnagain_iip3_lookup[] = {
72 /* Gain *100dB // reg */
107 static const struct slookup gain_rfagc_lookup[] = {
108 /* Gain *100dB // reg */
164 * This table is 6 dB too low comapred to the others (probably created with
165 * a different BB_MAG setting)
167 static const struct slookup gain_channel_agc_nf_lookup[] = {
168 /* Gain *100dB // reg */
226 static const struct slookup gain_channel_agc_iip3_lookup[] = {
227 /* Gain *100dB // reg */
285 static inline u32 muldiv32(u32 a, u32 b, u32 c)
289 tmp64 = (u64)a * (u64)b;
295 static int i2c_read(struct i2c_adapter *adap,
296 u8 adr, u8 *msg, int len, u8 *answ, int alen)
298 struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0,
299 .buf = msg, .len = len},
300 { .addr = adr, .flags = I2C_M_RD,
301 .buf = answ, .len = alen } };
302 if (i2c_transfer(adap, msgs, 2) != 2) {
303 dev_err(&adap->dev, "i2c read error\n");
309 static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len)
311 struct i2c_msg msg = {.addr = adr, .flags = 0,
312 .buf = data, .len = len};
314 if (i2c_transfer(adap, &msg, 1) != 1) {
315 dev_err(&adap->dev, "i2c write error\n");
321 static int write_regs(struct stv *state, int reg, int len)
325 memcpy(&d[1], &state->reg[reg], len);
327 return i2c_write(state->i2c, state->adr, d, len + 1);
330 static int write_reg(struct stv *state, u8 reg, u8 val)
332 u8 d[2] = {reg, val};
334 return i2c_write(state->i2c, state->adr, d, 2);
337 static int read_reg(struct stv *state, u8 reg, u8 *val)
339 return i2c_read(state->i2c, state->adr, ®, 1, val, 1);
342 static int wait_for_call_done(struct stv *state, u8 mask)
345 u32 lock_retry_count = 10;
347 while (lock_retry_count > 0) {
350 status = read_reg(state, 9, ®val);
354 if ((regval & mask) == 0)
356 usleep_range(4000, 6000);
357 lock_retry_count -= 1;
364 static void init_state(struct stv *state)
369 u32 agcset = 0xffffffff;
370 u32 bbmode = 0xffffffff;
372 state->reg[0] = 0x08;
373 state->reg[1] = 0x41;
374 state->reg[2] = 0x8f;
375 state->reg[3] = 0x00;
376 state->reg[4] = 0xce;
377 state->reg[5] = 0x54;
378 state->reg[6] = 0x55;
379 state->reg[7] = 0x45;
380 state->reg[8] = 0x46;
381 state->reg[9] = 0xbd;
382 state->reg[10] = 0x11;
384 state->ref_freq = 16000;
387 state->reg[0x00] |= (clkdiv & 0x03);
389 state->reg[0x03] |= (agcmode << 5);
391 state->reg[0x01] |= 0x30;
394 state->reg[0x01] = (state->reg[0x01] & ~0x30) | (bbmode << 4);
396 state->reg[0x03] |= agcref;
398 state->reg[0x02] = (state->reg[0x02] & ~0x1F) | agcset | 0x40;
401 static int attach_init(struct stv *state)
403 if (write_regs(state, 0, 11))
408 static void release(struct dvb_frontend *fe)
410 kfree(fe->tuner_priv);
411 fe->tuner_priv = NULL;
414 static int set_bandwidth(struct dvb_frontend *fe, u32 cutoff_frequency)
416 struct stv *state = fe->tuner_priv;
417 u32 index = (cutoff_frequency + 999999) / 1000000;
424 if ((state->reg[0x08] & ~0xFC) == ((index - 6) << 2))
427 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2);
428 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x08;
429 if (fe->ops.i2c_gate_ctrl)
430 stat = fe->ops.i2c_gate_ctrl(fe, 1);
432 write_regs(state, 0x08, 2);
433 wait_for_call_done(state, 0x08);
435 if (fe->ops.i2c_gate_ctrl && !stat)
436 fe->ops.i2c_gate_ctrl(fe, 0);
440 static int set_lof(struct stv *state, u32 local_frequency, u32 cutoff_frequency)
442 u32 index = (cutoff_frequency + 999999) / 1000000;
443 u32 frequency = (local_frequency + 500) / 1000;
444 u32 p = 1, psel = 0, fvco, div, frac;
452 if (frequency <= 1300000) {
459 fvco = frequency * p;
460 div = fvco / state->ref_freq;
461 frac = fvco % state->ref_freq;
462 frac = muldiv32(frac, 0x40000, state->ref_freq);
467 else if (fvco < 2950000)
469 else if (fvco < 3300000)
471 else if (fvco < 3700000)
473 else if (fvco < 4200000)
475 else if (fvco < 4800000)
480 state->reg[0x02] |= 0x80; /* LNA IIP3 Mode */
482 state->reg[0x03] = (state->reg[0x03] & ~0x80) | (psel << 7);
483 state->reg[0x04] = (div & 0xFF);
484 state->reg[0x05] = (((div >> 8) & 0x01) | ((frac & 0x7F) << 1)) & 0xff;
485 state->reg[0x06] = ((frac >> 7) & 0xFF);
486 state->reg[0x07] = (state->reg[0x07] & ~0x07) | ((frac >> 15) & 0x07);
487 state->reg[0x07] = (state->reg[0x07] & ~0xE0) | (icp << 5);
489 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2);
490 /* Start cal vco,CF */
491 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x0C;
492 write_regs(state, 2, 8);
494 wait_for_call_done(state, 0x0C);
496 usleep_range(10000, 12000);
498 read_reg(state, 0x03, &tmp);
500 state->reg[0x02] &= ~0x80; /* LNA NF Mode */
501 write_regs(state, 2, 1);
503 read_reg(state, 0x08, &tmp);
505 state->frequency = frequency;
510 static int set_params(struct dvb_frontend *fe)
512 struct stv *state = fe->tuner_priv;
513 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
517 if (p->delivery_system != SYS_DVBS && p->delivery_system != SYS_DVBS2)
520 freq = p->frequency * 1000;
521 cutoff = 5000000 + muldiv32(p->symbol_rate, 135, 200);
523 if (fe->ops.i2c_gate_ctrl)
524 stat = fe->ops.i2c_gate_ctrl(fe, 1);
526 set_lof(state, freq, cutoff);
527 if (fe->ops.i2c_gate_ctrl && !stat)
528 fe->ops.i2c_gate_ctrl(fe, 0);
532 static s32 table_lookup(const struct slookup *table,
533 int table_size, u16 reg_value)
538 int imax = table_size - 1;
541 /* Assumes Table[0].RegValue < Table[imax].RegValue */
542 if (reg_value <= table[0].reg_value) {
543 gain = table[0].value;
544 } else if (reg_value >= table[imax].reg_value) {
545 gain = table[imax].value;
547 while ((imax - imin) > 1) {
548 i = (imax + imin) / 2;
549 if ((table[imin].reg_value <= reg_value) &&
550 (reg_value <= table[i].reg_value))
555 reg_diff = table[imax].reg_value - table[imin].reg_value;
556 gain = table[imin].value;
558 gain += ((s32)(reg_value - table[imin].reg_value) *
559 (s32)(table[imax].value
560 - table[imin].value)) / reg_diff;
565 static int get_rf_strength(struct dvb_frontend *fe, u16 *st)
567 struct stv *state = fe->tuner_priv;
571 if ((state->reg[0x03] & 0x60) == 0) {
572 /* RF Mode, Read AGC ADC */
576 if (fe->ops.i2c_gate_ctrl)
577 stat = fe->ops.i2c_gate_ctrl(fe, 1);
579 write_reg(state, 0x02, state->reg[0x02] | 0x20);
580 read_reg(state, 2, ®);
582 read_reg(state, 2, ®);
584 if (fe->ops.i2c_gate_ctrl && !stat)
585 fe->ops.i2c_gate_ctrl(fe, 0);
587 if ((state->reg[0x02] & 0x80) == 0)
589 gain = table_lookup(lnagain_nf_lookup,
590 ARRAY_SIZE(lnagain_nf_lookup),
594 gain = table_lookup(lnagain_iip3_lookup,
595 ARRAY_SIZE(lnagain_iip3_lookup),
598 gain += table_lookup(gain_rfagc_lookup,
599 ARRAY_SIZE(gain_rfagc_lookup), rfagc);
604 if ((state->reg[0x02] & 0x80) == 0) {
607 gain_channel_agc_nf_lookup,
608 ARRAY_SIZE(gain_channel_agc_nf_lookup), rfagc);
614 gain_channel_agc_iip3_lookup,
615 ARRAY_SIZE(gain_channel_agc_iip3_lookup),
620 if (state->frequency > 0)
621 /* Tilt correction ( 0.00016 dB/MHz ) */
622 gain -= ((((s32)(state->frequency / 1000) - 1550) * 2) / 12);
624 /* + (BBGain * 10); */
625 gain += (s32)((state->reg[0x01] & 0xC0) >> 6) * 600 - 1300;
629 else if (gain > 10000)
637 static const struct dvb_tuner_ops tuner_ops = {
639 .name = "ST STV6111",
640 .frequency_min_hz = 950 * MHz,
641 .frequency_max_hz = 2150 * MHz,
643 .set_params = set_params,
645 .get_rf_strength = get_rf_strength,
646 .set_bandwidth = set_bandwidth,
649 struct dvb_frontend *stv6111_attach(struct dvb_frontend *fe,
650 struct i2c_adapter *i2c, u8 adr)
656 state = kzalloc(sizeof(*state), GFP_KERNEL);
661 memcpy(&fe->ops.tuner_ops, &tuner_ops, sizeof(struct dvb_tuner_ops));
664 if (fe->ops.i2c_gate_ctrl)
665 gatestat = fe->ops.i2c_gate_ctrl(fe, 1);
667 stat = attach_init(state);
668 if (fe->ops.i2c_gate_ctrl && !gatestat)
669 fe->ops.i2c_gate_ctrl(fe, 0);
674 fe->tuner_priv = state;
677 EXPORT_SYMBOL_GPL(stv6111_attach);
679 MODULE_DESCRIPTION("ST STV6111 satellite tuner driver");
680 MODULE_AUTHOR("Ralph Metzler, Manfred Voelkel");
681 MODULE_LICENSE("GPL v2");