2 * Driver for the ST STV6111 tuner
4 * Copyright (C) 2014 Digital Devices GmbH
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 only, as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/firmware.h>
23 #include <linux/i2c.h>
24 #include <asm/div64.h>
28 #include "dvb_frontend.h"
31 struct i2c_adapter *i2c;
44 static const struct slookup lnagain_nf_lookup[] = {
45 /* Gain *100dB // Reg */
80 static const struct slookup lnagain_iip3_lookup[] = {
81 /* Gain *100dB // reg */
116 static const struct slookup gain_rfagc_lookup[] = {
117 /* Gain *100dB // reg */
173 * This table is 6 dB too low comapred to the others (probably created with
174 * a different BB_MAG setting)
176 static const struct slookup gain_channel_agc_nf_lookup[] = {
177 /* Gain *100dB // reg */
235 static const struct slookup gain_channel_agc_iip3_lookup[] = {
236 /* Gain *100dB // reg */
294 static inline u32 muldiv32(u32 a, u32 b, u32 c)
298 tmp64 = (u64)a * (u64)b;
304 static int i2c_read(struct i2c_adapter *adap,
305 u8 adr, u8 *msg, int len, u8 *answ, int alen)
307 struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0,
308 .buf = msg, .len = len},
309 { .addr = adr, .flags = I2C_M_RD,
310 .buf = answ, .len = alen } };
311 if (i2c_transfer(adap, msgs, 2) != 2) {
312 dev_err(&adap->dev, "i2c read error\n");
318 static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len)
320 struct i2c_msg msg = {.addr = adr, .flags = 0,
321 .buf = data, .len = len};
323 if (i2c_transfer(adap, &msg, 1) != 1) {
324 dev_err(&adap->dev, "i2c write error\n");
330 static int write_regs(struct stv *state, int reg, int len)
334 memcpy(&d[1], &state->reg[reg], len);
336 return i2c_write(state->i2c, state->adr, d, len + 1);
339 static int write_reg(struct stv *state, u8 reg, u8 val)
341 u8 d[2] = {reg, val};
343 return i2c_write(state->i2c, state->adr, d, 2);
346 static int read_reg(struct stv *state, u8 reg, u8 *val)
348 return i2c_read(state->i2c, state->adr, ®, 1, val, 1);
351 static int wait_for_call_done(struct stv *state, u8 mask)
354 u32 lock_retry_count = 10;
356 while (lock_retry_count > 0) {
359 status = read_reg(state, 9, ®val);
363 if ((regval & mask) == 0)
365 usleep_range(4000, 6000);
366 lock_retry_count -= 1;
373 static void init_state(struct stv *state)
378 u32 agcset = 0xffffffff;
379 u32 bbmode = 0xffffffff;
381 state->reg[0] = 0x08;
382 state->reg[1] = 0x41;
383 state->reg[2] = 0x8f;
384 state->reg[3] = 0x00;
385 state->reg[4] = 0xce;
386 state->reg[5] = 0x54;
387 state->reg[6] = 0x55;
388 state->reg[7] = 0x45;
389 state->reg[8] = 0x46;
390 state->reg[9] = 0xbd;
391 state->reg[10] = 0x11;
393 state->ref_freq = 16000;
396 state->reg[0x00] |= (clkdiv & 0x03);
398 state->reg[0x03] |= (agcmode << 5);
400 state->reg[0x01] |= 0x30;
403 state->reg[0x01] = (state->reg[0x01] & ~0x30) | (bbmode << 4);
405 state->reg[0x03] |= agcref;
407 state->reg[0x02] = (state->reg[0x02] & ~0x1F) | agcset | 0x40;
410 static int attach_init(struct stv *state)
412 if (write_regs(state, 0, 11))
417 static void release(struct dvb_frontend *fe)
419 kfree(fe->tuner_priv);
420 fe->tuner_priv = NULL;
423 static int set_bandwidth(struct dvb_frontend *fe, u32 cutoff_frequency)
425 struct stv *state = fe->tuner_priv;
426 u32 index = (cutoff_frequency + 999999) / 1000000;
432 if ((state->reg[0x08] & ~0xFC) == ((index - 6) << 2))
435 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2);
436 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x08;
437 if (fe->ops.i2c_gate_ctrl)
438 fe->ops.i2c_gate_ctrl(fe, 1);
439 write_regs(state, 0x08, 2);
440 wait_for_call_done(state, 0x08);
441 if (fe->ops.i2c_gate_ctrl)
442 fe->ops.i2c_gate_ctrl(fe, 0);
446 static int set_lof(struct stv *state, u32 local_frequency, u32 cutoff_frequency)
448 u32 index = (cutoff_frequency + 999999) / 1000000;
449 u32 frequency = (local_frequency + 500) / 1000;
450 u32 p = 1, psel = 0, fvco, div, frac;
458 if (frequency <= 1300000) {
465 fvco = frequency * p;
466 div = fvco / state->ref_freq;
467 frac = fvco % state->ref_freq;
468 frac = muldiv32(frac, 0x40000, state->ref_freq);
473 else if (fvco < 2950000)
475 else if (fvco < 3300000)
477 else if (fvco < 3700000)
479 else if (fvco < 4200000)
481 else if (fvco < 4800000)
486 state->reg[0x02] |= 0x80; /* LNA IIP3 Mode */
488 state->reg[0x03] = (state->reg[0x03] & ~0x80) | (psel << 7);
489 state->reg[0x04] = (div & 0xFF);
490 state->reg[0x05] = (((div >> 8) & 0x01) | ((frac & 0x7F) << 1)) & 0xff;
491 state->reg[0x06] = ((frac >> 7) & 0xFF);
492 state->reg[0x07] = (state->reg[0x07] & ~0x07) | ((frac >> 15) & 0x07);
493 state->reg[0x07] = (state->reg[0x07] & ~0xE0) | (icp << 5);
495 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2);
496 /* Start cal vco,CF */
497 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x0C;
498 write_regs(state, 2, 8);
500 wait_for_call_done(state, 0x0C);
502 usleep_range(10000, 12000);
504 read_reg(state, 0x03, &tmp);
506 state->reg[0x02] &= ~0x80; /* LNA NF Mode */
507 write_regs(state, 2, 1);
509 read_reg(state, 0x08, &tmp);
511 state->frequency = frequency;
516 static int set_params(struct dvb_frontend *fe)
518 struct stv *state = fe->tuner_priv;
519 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
522 if (p->delivery_system != SYS_DVBS && p->delivery_system != SYS_DVBS2)
525 freq = p->frequency * 1000;
526 cutoff = 5000000 + muldiv32(p->symbol_rate, 135, 200);
528 if (fe->ops.i2c_gate_ctrl)
529 fe->ops.i2c_gate_ctrl(fe, 1);
530 set_lof(state, freq, cutoff);
531 if (fe->ops.i2c_gate_ctrl)
532 fe->ops.i2c_gate_ctrl(fe, 0);
536 static s32 table_lookup(const struct slookup *table,
537 int table_size, u16 reg_value)
542 int imax = table_size - 1;
545 /* Assumes Table[0].RegValue < Table[imax].RegValue */
546 if (reg_value <= table[0].reg_value) {
547 gain = table[0].value;
548 } else if (reg_value >= table[imax].reg_value) {
549 gain = table[imax].value;
551 while ((imax - imin) > 1) {
552 i = (imax + imin) / 2;
553 if ((table[imin].reg_value <= reg_value) &&
554 (reg_value <= table[i].reg_value))
559 reg_diff = table[imax].reg_value - table[imin].reg_value;
560 gain = table[imin].value;
562 gain += ((s32)(reg_value - table[imin].reg_value) *
563 (s32)(table[imax].value
564 - table[imin].value)) / reg_diff;
569 static int get_rf_strength(struct dvb_frontend *fe, u16 *st)
571 struct stv *state = fe->tuner_priv;
575 if ((state->reg[0x03] & 0x60) == 0) {
576 /* RF Mode, Read AGC ADC */
579 if (fe->ops.i2c_gate_ctrl)
580 fe->ops.i2c_gate_ctrl(fe, 1);
581 write_reg(state, 0x02, state->reg[0x02] | 0x20);
582 read_reg(state, 2, ®);
584 read_reg(state, 2, ®);
585 if (fe->ops.i2c_gate_ctrl)
586 fe->ops.i2c_gate_ctrl(fe, 0);
588 if ((state->reg[0x02] & 0x80) == 0)
590 gain = table_lookup(lnagain_nf_lookup,
591 ARRAY_SIZE(lnagain_nf_lookup),
595 gain = table_lookup(lnagain_iip3_lookup,
596 ARRAY_SIZE(lnagain_iip3_lookup),
599 gain += table_lookup(gain_rfagc_lookup,
600 ARRAY_SIZE(gain_rfagc_lookup), rfagc);
605 if ((state->reg[0x02] & 0x80) == 0) {
608 gain_channel_agc_nf_lookup,
609 ARRAY_SIZE(gain_channel_agc_nf_lookup), rfagc);
615 gain_channel_agc_iip3_lookup,
616 ARRAY_SIZE(gain_channel_agc_iip3_lookup),
621 if (state->frequency > 0)
622 /* Tilt correction ( 0.00016 dB/MHz ) */
623 gain -= ((((s32)(state->frequency / 1000) - 1550) * 2) / 12);
625 /* + (BBGain * 10); */
626 gain += (s32)((state->reg[0x01] & 0xC0) >> 6) * 600 - 1300;
630 else if (gain > 10000)
638 static const struct dvb_tuner_ops tuner_ops = {
640 .name = "ST STV6111",
641 .frequency_min = 950000,
642 .frequency_max = 2150000,
645 .set_params = set_params,
647 .get_rf_strength = get_rf_strength,
648 .set_bandwidth = set_bandwidth,
651 struct dvb_frontend *stv6111_attach(struct dvb_frontend *fe,
652 struct i2c_adapter *i2c, u8 adr)
657 state = kzalloc(sizeof(*state), GFP_KERNEL);
662 memcpy(&fe->ops.tuner_ops, &tuner_ops, sizeof(struct dvb_tuner_ops));
665 if (fe->ops.i2c_gate_ctrl)
666 fe->ops.i2c_gate_ctrl(fe, 1);
667 stat = attach_init(state);
668 if (fe->ops.i2c_gate_ctrl)
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");