1 // SPDX-License-Identifier: GPL-2.0-or-later
3 tda18271-common.c - driver for the Philips / NXP TDA18271 silicon tuner
5 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
9 #include "tda18271-priv.h"
11 static int tda18271_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
13 struct tda18271_priv
*priv
= fe
->tuner_priv
;
14 enum tda18271_i2c_gate gate
;
18 case TDA18271_GATE_DIGITAL
:
19 case TDA18271_GATE_ANALOG
:
22 case TDA18271_GATE_AUTO
:
25 case TDA18271_DIGITAL
:
26 gate
= TDA18271_GATE_DIGITAL
;
30 gate
= TDA18271_GATE_ANALOG
;
36 case TDA18271_GATE_ANALOG
:
37 if (fe
->ops
.analog_ops
.i2c_gate_ctrl
)
38 ret
= fe
->ops
.analog_ops
.i2c_gate_ctrl(fe
, enable
);
40 case TDA18271_GATE_DIGITAL
:
41 if (fe
->ops
.i2c_gate_ctrl
)
42 ret
= fe
->ops
.i2c_gate_ctrl(fe
, enable
);
52 /*---------------------------------------------------------------------*/
54 static void tda18271_dump_regs(struct dvb_frontend
*fe
, int extended
)
56 struct tda18271_priv
*priv
= fe
->tuner_priv
;
57 unsigned char *regs
= priv
->tda18271_regs
;
59 tda_reg("=== TDA18271 REG DUMP ===\n");
60 tda_reg("ID_BYTE = 0x%02x\n", 0xff & regs
[R_ID
]);
61 tda_reg("THERMO_BYTE = 0x%02x\n", 0xff & regs
[R_TM
]);
62 tda_reg("POWER_LEVEL_BYTE = 0x%02x\n", 0xff & regs
[R_PL
]);
63 tda_reg("EASY_PROG_BYTE_1 = 0x%02x\n", 0xff & regs
[R_EP1
]);
64 tda_reg("EASY_PROG_BYTE_2 = 0x%02x\n", 0xff & regs
[R_EP2
]);
65 tda_reg("EASY_PROG_BYTE_3 = 0x%02x\n", 0xff & regs
[R_EP3
]);
66 tda_reg("EASY_PROG_BYTE_4 = 0x%02x\n", 0xff & regs
[R_EP4
]);
67 tda_reg("EASY_PROG_BYTE_5 = 0x%02x\n", 0xff & regs
[R_EP5
]);
68 tda_reg("CAL_POST_DIV_BYTE = 0x%02x\n", 0xff & regs
[R_CPD
]);
69 tda_reg("CAL_DIV_BYTE_1 = 0x%02x\n", 0xff & regs
[R_CD1
]);
70 tda_reg("CAL_DIV_BYTE_2 = 0x%02x\n", 0xff & regs
[R_CD2
]);
71 tda_reg("CAL_DIV_BYTE_3 = 0x%02x\n", 0xff & regs
[R_CD3
]);
72 tda_reg("MAIN_POST_DIV_BYTE = 0x%02x\n", 0xff & regs
[R_MPD
]);
73 tda_reg("MAIN_DIV_BYTE_1 = 0x%02x\n", 0xff & regs
[R_MD1
]);
74 tda_reg("MAIN_DIV_BYTE_2 = 0x%02x\n", 0xff & regs
[R_MD2
]);
75 tda_reg("MAIN_DIV_BYTE_3 = 0x%02x\n", 0xff & regs
[R_MD3
]);
77 /* only dump extended regs if DBG_ADV is set */
78 if (!(tda18271_debug
& DBG_ADV
))
81 /* W indicates write-only registers.
82 * Register dump for write-only registers shows last value written. */
84 tda_reg("EXTENDED_BYTE_1 = 0x%02x\n", 0xff & regs
[R_EB1
]);
85 tda_reg("EXTENDED_BYTE_2 = 0x%02x\n", 0xff & regs
[R_EB2
]);
86 tda_reg("EXTENDED_BYTE_3 = 0x%02x\n", 0xff & regs
[R_EB3
]);
87 tda_reg("EXTENDED_BYTE_4 = 0x%02x\n", 0xff & regs
[R_EB4
]);
88 tda_reg("EXTENDED_BYTE_5 = 0x%02x\n", 0xff & regs
[R_EB5
]);
89 tda_reg("EXTENDED_BYTE_6 = 0x%02x\n", 0xff & regs
[R_EB6
]);
90 tda_reg("EXTENDED_BYTE_7 = 0x%02x\n", 0xff & regs
[R_EB7
]);
91 tda_reg("EXTENDED_BYTE_8 = 0x%02x\n", 0xff & regs
[R_EB8
]);
92 tda_reg("EXTENDED_BYTE_9 W = 0x%02x\n", 0xff & regs
[R_EB9
]);
93 tda_reg("EXTENDED_BYTE_10 = 0x%02x\n", 0xff & regs
[R_EB10
]);
94 tda_reg("EXTENDED_BYTE_11 = 0x%02x\n", 0xff & regs
[R_EB11
]);
95 tda_reg("EXTENDED_BYTE_12 = 0x%02x\n", 0xff & regs
[R_EB12
]);
96 tda_reg("EXTENDED_BYTE_13 = 0x%02x\n", 0xff & regs
[R_EB13
]);
97 tda_reg("EXTENDED_BYTE_14 = 0x%02x\n", 0xff & regs
[R_EB14
]);
98 tda_reg("EXTENDED_BYTE_15 = 0x%02x\n", 0xff & regs
[R_EB15
]);
99 tda_reg("EXTENDED_BYTE_16 W = 0x%02x\n", 0xff & regs
[R_EB16
]);
100 tda_reg("EXTENDED_BYTE_17 W = 0x%02x\n", 0xff & regs
[R_EB17
]);
101 tda_reg("EXTENDED_BYTE_18 = 0x%02x\n", 0xff & regs
[R_EB18
]);
102 tda_reg("EXTENDED_BYTE_19 W = 0x%02x\n", 0xff & regs
[R_EB19
]);
103 tda_reg("EXTENDED_BYTE_20 W = 0x%02x\n", 0xff & regs
[R_EB20
]);
104 tda_reg("EXTENDED_BYTE_21 = 0x%02x\n", 0xff & regs
[R_EB21
]);
105 tda_reg("EXTENDED_BYTE_22 = 0x%02x\n", 0xff & regs
[R_EB22
]);
106 tda_reg("EXTENDED_BYTE_23 = 0x%02x\n", 0xff & regs
[R_EB23
]);
109 int tda18271_read_regs(struct dvb_frontend
*fe
)
111 struct tda18271_priv
*priv
= fe
->tuner_priv
;
112 unsigned char *regs
= priv
->tda18271_regs
;
113 unsigned char buf
= 0x00;
115 struct i2c_msg msg
[] = {
116 { .addr
= priv
->i2c_props
.addr
, .flags
= 0,
117 .buf
= &buf
, .len
= 1 },
118 { .addr
= priv
->i2c_props
.addr
, .flags
= I2C_M_RD
,
119 .buf
= regs
, .len
= 16 }
122 tda18271_i2c_gate_ctrl(fe
, 1);
124 /* read all registers */
125 ret
= i2c_transfer(priv
->i2c_props
.adap
, msg
, 2);
127 tda18271_i2c_gate_ctrl(fe
, 0);
130 tda_err("ERROR: i2c_transfer returned: %d\n", ret
);
132 if (tda18271_debug
& DBG_REG
)
133 tda18271_dump_regs(fe
, 0);
135 return (ret
== 2 ? 0 : ret
);
138 int tda18271_read_extended(struct dvb_frontend
*fe
)
140 struct tda18271_priv
*priv
= fe
->tuner_priv
;
141 unsigned char *regs
= priv
->tda18271_regs
;
142 unsigned char regdump
[TDA18271_NUM_REGS
];
143 unsigned char buf
= 0x00;
145 struct i2c_msg msg
[] = {
146 { .addr
= priv
->i2c_props
.addr
, .flags
= 0,
147 .buf
= &buf
, .len
= 1 },
148 { .addr
= priv
->i2c_props
.addr
, .flags
= I2C_M_RD
,
149 .buf
= regdump
, .len
= TDA18271_NUM_REGS
}
152 tda18271_i2c_gate_ctrl(fe
, 1);
154 /* read all registers */
155 ret
= i2c_transfer(priv
->i2c_props
.adap
, msg
, 2);
157 tda18271_i2c_gate_ctrl(fe
, 0);
160 tda_err("ERROR: i2c_transfer returned: %d\n", ret
);
162 for (i
= 0; i
< TDA18271_NUM_REGS
; i
++) {
163 /* don't update write-only registers */
169 regs
[i
] = regdump
[i
];
172 if (tda18271_debug
& DBG_REG
)
173 tda18271_dump_regs(fe
, 1);
175 return (ret
== 2 ? 0 : ret
);
178 static int __tda18271_write_regs(struct dvb_frontend
*fe
, int idx
, int len
,
181 struct tda18271_priv
*priv
= fe
->tuner_priv
;
182 unsigned char *regs
= priv
->tda18271_regs
;
183 unsigned char buf
[TDA18271_NUM_REGS
+ 1];
184 struct i2c_msg msg
= { .addr
= priv
->i2c_props
.addr
, .flags
= 0,
188 BUG_ON((len
== 0) || (idx
+ len
> sizeof(buf
)));
190 switch (priv
->small_i2c
) {
191 case TDA18271_03_BYTE_CHUNK_INIT
:
194 case TDA18271_08_BYTE_CHUNK_INIT
:
197 case TDA18271_16_BYTE_CHUNK_INIT
:
200 case TDA18271_39_BYTE_CHUNK_INIT
:
207 * If lock_i2c is true, it will take the I2C bus for tda18271 private
208 * usage during the entire write ops, as otherwise, bad things could
210 * During device init, several write operations will happen. So,
211 * tda18271_init_regs controls the I2C lock directly,
212 * disabling lock_i2c here.
215 tda18271_i2c_gate_ctrl(fe
, 1);
216 i2c_lock_bus(priv
->i2c_props
.adap
, I2C_LOCK_SEGMENT
);
223 for (i
= 1; i
<= max
; i
++)
224 buf
[i
] = regs
[idx
- 1 + i
];
228 /* write registers */
229 ret
= __i2c_transfer(priv
->i2c_props
.adap
, &msg
, 1);
237 i2c_unlock_bus(priv
->i2c_props
.adap
, I2C_LOCK_SEGMENT
);
238 tda18271_i2c_gate_ctrl(fe
, 0);
242 tda_err("ERROR: idx = 0x%x, len = %d, i2c_transfer returned: %d\n",
245 return (ret
== 1 ? 0 : ret
);
248 int tda18271_write_regs(struct dvb_frontend
*fe
, int idx
, int len
)
250 return __tda18271_write_regs(fe
, idx
, len
, true);
253 /*---------------------------------------------------------------------*/
255 static int __tda18271_charge_pump_source(struct dvb_frontend
*fe
,
256 enum tda18271_pll pll
, int force
,
259 struct tda18271_priv
*priv
= fe
->tuner_priv
;
260 unsigned char *regs
= priv
->tda18271_regs
;
262 int r_cp
= (pll
== TDA18271_CAL_PLL
) ? R_EB7
: R_EB4
;
265 regs
[r_cp
] |= ((force
& 1) << 5);
267 return __tda18271_write_regs(fe
, r_cp
, 1, lock_i2c
);
270 int tda18271_charge_pump_source(struct dvb_frontend
*fe
,
271 enum tda18271_pll pll
, int force
)
273 return __tda18271_charge_pump_source(fe
, pll
, force
, true);
277 int tda18271_init_regs(struct dvb_frontend
*fe
)
279 struct tda18271_priv
*priv
= fe
->tuner_priv
;
280 unsigned char *regs
= priv
->tda18271_regs
;
282 tda_dbg("initializing registers for device @ %d-%04x\n",
283 i2c_adapter_id(priv
->i2c_props
.adap
),
284 priv
->i2c_props
.addr
);
287 * Don't let any other I2C transfer to happen at adapter during init,
288 * as those could cause bad things
290 tda18271_i2c_gate_ctrl(fe
, 1);
291 i2c_lock_bus(priv
->i2c_props
.adap
, I2C_LOCK_SEGMENT
);
293 /* initialize registers */
378 __tda18271_write_regs(fe
, 0x00, TDA18271_NUM_REGS
, false);
380 /* setup agc1 gain */
382 __tda18271_write_regs(fe
, R_EB17
, 1, false);
384 __tda18271_write_regs(fe
, R_EB17
, 1, false);
386 __tda18271_write_regs(fe
, R_EB17
, 1, false);
388 __tda18271_write_regs(fe
, R_EB17
, 1, false);
390 /* setup agc2 gain */
391 if ((priv
->id
) == TDA18271HDC1
) {
393 __tda18271_write_regs(fe
, R_EB20
, 1, false);
395 __tda18271_write_regs(fe
, R_EB20
, 1, false);
397 __tda18271_write_regs(fe
, R_EB20
, 1, false);
399 __tda18271_write_regs(fe
, R_EB20
, 1, false);
402 /* image rejection calibration */
417 __tda18271_write_regs(fe
, R_EP3
, 11, false);
419 if ((priv
->id
) == TDA18271HDC2
) {
420 /* main pll cp source on */
421 __tda18271_charge_pump_source(fe
, TDA18271_MAIN_PLL
, 1, false);
424 /* main pll cp source off */
425 __tda18271_charge_pump_source(fe
, TDA18271_MAIN_PLL
, 0, false);
428 msleep(5); /* pll locking */
430 /* launch detector */
431 __tda18271_write_regs(fe
, R_EP1
, 1, false);
432 msleep(5); /* wanted low measurement */
439 __tda18271_write_regs(fe
, R_EP3
, 7, false);
440 msleep(5); /* pll locking */
442 /* launch optimization algorithm */
443 __tda18271_write_regs(fe
, R_EP2
, 1, false);
444 msleep(30); /* image low optimization completion */
454 __tda18271_write_regs(fe
, R_EP3
, 11, false);
455 msleep(5); /* pll locking */
457 /* launch detector */
458 __tda18271_write_regs(fe
, R_EP1
, 1, false);
459 msleep(5); /* wanted mid measurement */
466 __tda18271_write_regs(fe
, R_EP3
, 7, false);
467 msleep(5); /* pll locking */
469 /* launch optimization algorithm */
470 __tda18271_write_regs(fe
, R_EP2
, 1, false);
471 msleep(30); /* image mid optimization completion */
482 __tda18271_write_regs(fe
, R_EP3
, 11, false);
483 msleep(5); /* pll locking */
485 /* launch detector */
486 __tda18271_write_regs(fe
, R_EP1
, 1, false);
487 msleep(5); /* wanted high measurement */
493 __tda18271_write_regs(fe
, R_EP3
, 7, false);
494 msleep(5); /* pll locking */
496 /* launch optimization algorithm */
497 __tda18271_write_regs(fe
, R_EP2
, 1, false);
498 msleep(30); /* image high optimization completion */
500 /* return to normal mode */
502 __tda18271_write_regs(fe
, R_EP4
, 1, false);
505 __tda18271_write_regs(fe
, R_EP1
, 1, false);
507 i2c_unlock_bus(priv
->i2c_props
.adap
, I2C_LOCK_SEGMENT
);
508 tda18271_i2c_gate_ctrl(fe
, 0);
513 /*---------------------------------------------------------------------*/
516 * Standby modes, EP3 [7:5]
518 * | SM || SM_LT || SM_XT || mode description
519 * |=====\\=======\\=======\\====================================
520 * | 0 || 0 || 0 || normal mode
521 * |-----||-------||-------||------------------------------------
522 * | || || || standby mode w/ slave tuner output
523 * | 1 || 0 || 0 || & loop through & xtal oscillator on
524 * |-----||-------||-------||------------------------------------
525 * | 1 || 1 || 0 || standby mode w/ xtal oscillator on
526 * |-----||-------||-------||------------------------------------
527 * | 1 || 1 || 1 || power off
531 int tda18271_set_standby_mode(struct dvb_frontend
*fe
,
532 int sm
, int sm_lt
, int sm_xt
)
534 struct tda18271_priv
*priv
= fe
->tuner_priv
;
535 unsigned char *regs
= priv
->tda18271_regs
;
537 if (tda18271_debug
& DBG_ADV
)
538 tda_dbg("sm = %d, sm_lt = %d, sm_xt = %d\n", sm
, sm_lt
, sm_xt
);
540 regs
[R_EP3
] &= ~0xe0; /* clear sm, sm_lt, sm_xt */
541 regs
[R_EP3
] |= (sm
? (1 << 7) : 0) |
542 (sm_lt
? (1 << 6) : 0) |
543 (sm_xt
? (1 << 5) : 0);
545 return tda18271_write_regs(fe
, R_EP3
, 1);
548 /*---------------------------------------------------------------------*/
550 int tda18271_calc_main_pll(struct dvb_frontend
*fe
, u32 freq
)
552 /* sets main post divider & divider bytes, but does not write them */
553 struct tda18271_priv
*priv
= fe
->tuner_priv
;
554 unsigned char *regs
= priv
->tda18271_regs
;
558 int ret
= tda18271_lookup_pll_map(fe
, MAIN_PLL
, &freq
, &pd
, &d
);
562 regs
[R_MPD
] = (0x7f & pd
);
564 div
= ((d
* (freq
/ 1000)) << 7) / 125;
566 regs
[R_MD1
] = 0x7f & (div
>> 16);
567 regs
[R_MD2
] = 0xff & (div
>> 8);
568 regs
[R_MD3
] = 0xff & div
;
573 int tda18271_calc_cal_pll(struct dvb_frontend
*fe
, u32 freq
)
575 /* sets cal post divider & divider bytes, but does not write them */
576 struct tda18271_priv
*priv
= fe
->tuner_priv
;
577 unsigned char *regs
= priv
->tda18271_regs
;
581 int ret
= tda18271_lookup_pll_map(fe
, CAL_PLL
, &freq
, &pd
, &d
);
587 div
= ((d
* (freq
/ 1000)) << 7) / 125;
589 regs
[R_CD1
] = 0x7f & (div
>> 16);
590 regs
[R_CD2
] = 0xff & (div
>> 8);
591 regs
[R_CD3
] = 0xff & div
;
596 /*---------------------------------------------------------------------*/
598 int tda18271_calc_bp_filter(struct dvb_frontend
*fe
, u32
*freq
)
600 /* sets bp filter bits, but does not write them */
601 struct tda18271_priv
*priv
= fe
->tuner_priv
;
602 unsigned char *regs
= priv
->tda18271_regs
;
605 int ret
= tda18271_lookup_map(fe
, BP_FILTER
, freq
, &val
);
609 regs
[R_EP1
] &= ~0x07; /* clear bp filter bits */
610 regs
[R_EP1
] |= (0x07 & val
);
615 int tda18271_calc_km(struct dvb_frontend
*fe
, u32
*freq
)
617 /* sets K & M bits, but does not write them */
618 struct tda18271_priv
*priv
= fe
->tuner_priv
;
619 unsigned char *regs
= priv
->tda18271_regs
;
622 int ret
= tda18271_lookup_map(fe
, RF_CAL_KMCO
, freq
, &val
);
626 regs
[R_EB13
] &= ~0x7c; /* clear k & m bits */
627 regs
[R_EB13
] |= (0x7c & val
);
632 int tda18271_calc_rf_band(struct dvb_frontend
*fe
, u32
*freq
)
634 /* sets rf band bits, but does not write them */
635 struct tda18271_priv
*priv
= fe
->tuner_priv
;
636 unsigned char *regs
= priv
->tda18271_regs
;
639 int ret
= tda18271_lookup_map(fe
, RF_BAND
, freq
, &val
);
643 regs
[R_EP2
] &= ~0xe0; /* clear rf band bits */
644 regs
[R_EP2
] |= (0xe0 & (val
<< 5));
649 int tda18271_calc_gain_taper(struct dvb_frontend
*fe
, u32
*freq
)
651 /* sets gain taper bits, but does not write them */
652 struct tda18271_priv
*priv
= fe
->tuner_priv
;
653 unsigned char *regs
= priv
->tda18271_regs
;
656 int ret
= tda18271_lookup_map(fe
, GAIN_TAPER
, freq
, &val
);
660 regs
[R_EP2
] &= ~0x1f; /* clear gain taper bits */
661 regs
[R_EP2
] |= (0x1f & val
);
666 int tda18271_calc_ir_measure(struct dvb_frontend
*fe
, u32
*freq
)
668 /* sets IR Meas bits, but does not write them */
669 struct tda18271_priv
*priv
= fe
->tuner_priv
;
670 unsigned char *regs
= priv
->tda18271_regs
;
673 int ret
= tda18271_lookup_map(fe
, IR_MEASURE
, freq
, &val
);
677 regs
[R_EP5
] &= ~0x07;
678 regs
[R_EP5
] |= (0x07 & val
);
683 int tda18271_calc_rf_cal(struct dvb_frontend
*fe
, u32
*freq
)
685 /* sets rf cal byte (RFC_Cprog), but does not write it */
686 struct tda18271_priv
*priv
= fe
->tuner_priv
;
687 unsigned char *regs
= priv
->tda18271_regs
;
690 int ret
= tda18271_lookup_map(fe
, RF_CAL
, freq
, &val
);
691 /* The TDA18271HD/C1 rf_cal map lookup is expected to go out of range
692 * for frequencies above 61.1 MHz. In these cases, the internal RF
693 * tracking filters calibration mechanism is used.
695 * There is no need to warn the user about this.
705 void _tda_printk(struct tda18271_priv
*state
, const char *level
,
706 const char *func
, const char *fmt
, ...)
708 struct va_format vaf
;
717 printk("%s%s: [%d-%04x|%c] %pV",
718 level
, func
, i2c_adapter_id(state
->i2c_props
.adap
),
719 state
->i2c_props
.addr
,
720 (state
->role
== TDA18271_MASTER
) ? 'M' : 'S',
723 printk("%s%s: %pV", level
, func
, &vaf
);