1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the ST STV6111 tuner
5 * Copyright (C) 2014 Digital Devices GmbH
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 only, as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * 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 <media/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;
433 if ((state
->reg
[0x08] & ~0xFC) == ((index
- 6) << 2))
436 state
->reg
[0x08] = (state
->reg
[0x08] & ~0xFC) | ((index
- 6) << 2);
437 state
->reg
[0x09] = (state
->reg
[0x09] & ~0x0C) | 0x08;
438 if (fe
->ops
.i2c_gate_ctrl
)
439 stat
= fe
->ops
.i2c_gate_ctrl(fe
, 1);
441 write_regs(state
, 0x08, 2);
442 wait_for_call_done(state
, 0x08);
444 if (fe
->ops
.i2c_gate_ctrl
&& !stat
)
445 fe
->ops
.i2c_gate_ctrl(fe
, 0);
449 static int set_lof(struct stv
*state
, u32 local_frequency
, u32 cutoff_frequency
)
451 u32 index
= (cutoff_frequency
+ 999999) / 1000000;
452 u32 frequency
= (local_frequency
+ 500) / 1000;
453 u32 p
= 1, psel
= 0, fvco
, div
, frac
;
461 if (frequency
<= 1300000) {
468 fvco
= frequency
* p
;
469 div
= fvco
/ state
->ref_freq
;
470 frac
= fvco
% state
->ref_freq
;
471 frac
= muldiv32(frac
, 0x40000, state
->ref_freq
);
476 else if (fvco
< 2950000)
478 else if (fvco
< 3300000)
480 else if (fvco
< 3700000)
482 else if (fvco
< 4200000)
484 else if (fvco
< 4800000)
489 state
->reg
[0x02] |= 0x80; /* LNA IIP3 Mode */
491 state
->reg
[0x03] = (state
->reg
[0x03] & ~0x80) | (psel
<< 7);
492 state
->reg
[0x04] = (div
& 0xFF);
493 state
->reg
[0x05] = (((div
>> 8) & 0x01) | ((frac
& 0x7F) << 1)) & 0xff;
494 state
->reg
[0x06] = ((frac
>> 7) & 0xFF);
495 state
->reg
[0x07] = (state
->reg
[0x07] & ~0x07) | ((frac
>> 15) & 0x07);
496 state
->reg
[0x07] = (state
->reg
[0x07] & ~0xE0) | (icp
<< 5);
498 state
->reg
[0x08] = (state
->reg
[0x08] & ~0xFC) | ((index
- 6) << 2);
499 /* Start cal vco,CF */
500 state
->reg
[0x09] = (state
->reg
[0x09] & ~0x0C) | 0x0C;
501 write_regs(state
, 2, 8);
503 wait_for_call_done(state
, 0x0C);
505 usleep_range(10000, 12000);
507 read_reg(state
, 0x03, &tmp
);
509 state
->reg
[0x02] &= ~0x80; /* LNA NF Mode */
510 write_regs(state
, 2, 1);
512 read_reg(state
, 0x08, &tmp
);
514 state
->frequency
= frequency
;
519 static int set_params(struct dvb_frontend
*fe
)
521 struct stv
*state
= fe
->tuner_priv
;
522 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
526 if (p
->delivery_system
!= SYS_DVBS
&& p
->delivery_system
!= SYS_DVBS2
)
529 freq
= p
->frequency
* 1000;
530 cutoff
= 5000000 + muldiv32(p
->symbol_rate
, 135, 200);
532 if (fe
->ops
.i2c_gate_ctrl
)
533 stat
= fe
->ops
.i2c_gate_ctrl(fe
, 1);
535 set_lof(state
, freq
, cutoff
);
536 if (fe
->ops
.i2c_gate_ctrl
&& !stat
)
537 fe
->ops
.i2c_gate_ctrl(fe
, 0);
541 static s32
table_lookup(const struct slookup
*table
,
542 int table_size
, u16 reg_value
)
547 int imax
= table_size
- 1;
550 /* Assumes Table[0].RegValue < Table[imax].RegValue */
551 if (reg_value
<= table
[0].reg_value
) {
552 gain
= table
[0].value
;
553 } else if (reg_value
>= table
[imax
].reg_value
) {
554 gain
= table
[imax
].value
;
556 while ((imax
- imin
) > 1) {
557 i
= (imax
+ imin
) / 2;
558 if ((table
[imin
].reg_value
<= reg_value
) &&
559 (reg_value
<= table
[i
].reg_value
))
564 reg_diff
= table
[imax
].reg_value
- table
[imin
].reg_value
;
565 gain
= table
[imin
].value
;
567 gain
+= ((s32
)(reg_value
- table
[imin
].reg_value
) *
568 (s32
)(table
[imax
].value
569 - table
[imin
].value
)) / reg_diff
;
574 static int get_rf_strength(struct dvb_frontend
*fe
, u16
*st
)
576 struct stv
*state
= fe
->tuner_priv
;
580 if ((state
->reg
[0x03] & 0x60) == 0) {
581 /* RF Mode, Read AGC ADC */
585 if (fe
->ops
.i2c_gate_ctrl
)
586 stat
= fe
->ops
.i2c_gate_ctrl(fe
, 1);
588 write_reg(state
, 0x02, state
->reg
[0x02] | 0x20);
589 read_reg(state
, 2, ®
);
591 read_reg(state
, 2, ®
);
593 if (fe
->ops
.i2c_gate_ctrl
&& !stat
)
594 fe
->ops
.i2c_gate_ctrl(fe
, 0);
596 if ((state
->reg
[0x02] & 0x80) == 0)
598 gain
= table_lookup(lnagain_nf_lookup
,
599 ARRAY_SIZE(lnagain_nf_lookup
),
603 gain
= table_lookup(lnagain_iip3_lookup
,
604 ARRAY_SIZE(lnagain_iip3_lookup
),
607 gain
+= table_lookup(gain_rfagc_lookup
,
608 ARRAY_SIZE(gain_rfagc_lookup
), rfagc
);
613 if ((state
->reg
[0x02] & 0x80) == 0) {
616 gain_channel_agc_nf_lookup
,
617 ARRAY_SIZE(gain_channel_agc_nf_lookup
), rfagc
);
623 gain_channel_agc_iip3_lookup
,
624 ARRAY_SIZE(gain_channel_agc_iip3_lookup
),
629 if (state
->frequency
> 0)
630 /* Tilt correction ( 0.00016 dB/MHz ) */
631 gain
-= ((((s32
)(state
->frequency
/ 1000) - 1550) * 2) / 12);
633 /* + (BBGain * 10); */
634 gain
+= (s32
)((state
->reg
[0x01] & 0xC0) >> 6) * 600 - 1300;
638 else if (gain
> 10000)
646 static const struct dvb_tuner_ops tuner_ops
= {
648 .name
= "ST STV6111",
649 .frequency_min_hz
= 950 * MHz
,
650 .frequency_max_hz
= 2150 * MHz
,
652 .set_params
= set_params
,
654 .get_rf_strength
= get_rf_strength
,
655 .set_bandwidth
= set_bandwidth
,
658 struct dvb_frontend
*stv6111_attach(struct dvb_frontend
*fe
,
659 struct i2c_adapter
*i2c
, u8 adr
)
665 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
670 memcpy(&fe
->ops
.tuner_ops
, &tuner_ops
, sizeof(struct dvb_tuner_ops
));
673 if (fe
->ops
.i2c_gate_ctrl
)
674 gatestat
= fe
->ops
.i2c_gate_ctrl(fe
, 1);
676 stat
= attach_init(state
);
677 if (fe
->ops
.i2c_gate_ctrl
&& !gatestat
)
678 fe
->ops
.i2c_gate_ctrl(fe
, 0);
683 fe
->tuner_priv
= state
;
686 EXPORT_SYMBOL_GPL(stv6111_attach
);
688 MODULE_DESCRIPTION("ST STV6111 satellite tuner driver");
689 MODULE_AUTHOR("Ralph Metzler, Manfred Voelkel");
690 MODULE_LICENSE("GPL v2");