2 tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
4 Copyright (C) 2007 Michael Krufky (mkrufky@linuxtv.org)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/delay.h>
22 #include <linux/videodev2.h>
23 #include "tda18271-priv.h"
26 module_param_named(debug
, tda18271_debug
, int, 0644);
27 MODULE_PARM_DESC(debug
, "set debug level (info=1, map=2, reg=4 (or-able))");
29 /*---------------------------------------------------------------------*/
31 static int tda18271_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
33 struct tda18271_priv
*priv
= fe
->tuner_priv
;
34 enum tda18271_i2c_gate gate
;
38 case TDA18271_GATE_DIGITAL
:
39 case TDA18271_GATE_ANALOG
:
42 case TDA18271_GATE_AUTO
:
45 case TDA18271_DIGITAL
:
46 gate
= TDA18271_GATE_DIGITAL
;
50 gate
= TDA18271_GATE_ANALOG
;
56 case TDA18271_GATE_ANALOG
:
57 if (fe
->ops
.analog_ops
.i2c_gate_ctrl
)
58 ret
= fe
->ops
.analog_ops
.i2c_gate_ctrl(fe
, enable
);
60 case TDA18271_GATE_DIGITAL
:
61 if (fe
->ops
.i2c_gate_ctrl
)
62 ret
= fe
->ops
.i2c_gate_ctrl(fe
, enable
);
72 /*---------------------------------------------------------------------*/
74 static void tda18271_dump_regs(struct dvb_frontend
*fe
)
76 struct tda18271_priv
*priv
= fe
->tuner_priv
;
77 unsigned char *regs
= priv
->tda18271_regs
;
79 dbg_reg("=== TDA18271 REG DUMP ===\n");
80 dbg_reg("ID_BYTE = 0x%02x\n", 0xff & regs
[R_ID
]);
81 dbg_reg("THERMO_BYTE = 0x%02x\n", 0xff & regs
[R_TM
]);
82 dbg_reg("POWER_LEVEL_BYTE = 0x%02x\n", 0xff & regs
[R_PL
]);
83 dbg_reg("EASY_PROG_BYTE_1 = 0x%02x\n", 0xff & regs
[R_EP1
]);
84 dbg_reg("EASY_PROG_BYTE_2 = 0x%02x\n", 0xff & regs
[R_EP2
]);
85 dbg_reg("EASY_PROG_BYTE_3 = 0x%02x\n", 0xff & regs
[R_EP3
]);
86 dbg_reg("EASY_PROG_BYTE_4 = 0x%02x\n", 0xff & regs
[R_EP4
]);
87 dbg_reg("EASY_PROG_BYTE_5 = 0x%02x\n", 0xff & regs
[R_EP5
]);
88 dbg_reg("CAL_POST_DIV_BYTE = 0x%02x\n", 0xff & regs
[R_CPD
]);
89 dbg_reg("CAL_DIV_BYTE_1 = 0x%02x\n", 0xff & regs
[R_CD1
]);
90 dbg_reg("CAL_DIV_BYTE_2 = 0x%02x\n", 0xff & regs
[R_CD2
]);
91 dbg_reg("CAL_DIV_BYTE_3 = 0x%02x\n", 0xff & regs
[R_CD3
]);
92 dbg_reg("MAIN_POST_DIV_BYTE = 0x%02x\n", 0xff & regs
[R_MPD
]);
93 dbg_reg("MAIN_DIV_BYTE_1 = 0x%02x\n", 0xff & regs
[R_MD1
]);
94 dbg_reg("MAIN_DIV_BYTE_2 = 0x%02x\n", 0xff & regs
[R_MD2
]);
95 dbg_reg("MAIN_DIV_BYTE_3 = 0x%02x\n", 0xff & regs
[R_MD3
]);
98 static void tda18271_read_regs(struct dvb_frontend
*fe
)
100 struct tda18271_priv
*priv
= fe
->tuner_priv
;
101 unsigned char *regs
= priv
->tda18271_regs
;
102 unsigned char buf
= 0x00;
104 struct i2c_msg msg
[] = {
105 { .addr
= priv
->i2c_addr
, .flags
= 0,
106 .buf
= &buf
, .len
= 1 },
107 { .addr
= priv
->i2c_addr
, .flags
= I2C_M_RD
,
108 .buf
= regs
, .len
= 16 }
111 tda18271_i2c_gate_ctrl(fe
, 1);
113 /* read all registers */
114 ret
= i2c_transfer(priv
->i2c_adap
, msg
, 2);
116 tda18271_i2c_gate_ctrl(fe
, 0);
119 printk("ERROR: %s: i2c_transfer returned: %d\n",
122 if (tda18271_debug
& DBG_REG
)
123 tda18271_dump_regs(fe
);
126 static void tda18271_write_regs(struct dvb_frontend
*fe
, int idx
, int len
)
128 struct tda18271_priv
*priv
= fe
->tuner_priv
;
129 unsigned char *regs
= priv
->tda18271_regs
;
130 unsigned char buf
[TDA18271_NUM_REGS
+1];
131 struct i2c_msg msg
= { .addr
= priv
->i2c_addr
, .flags
= 0,
132 .buf
= buf
, .len
= len
+1 };
135 BUG_ON((len
== 0) || (idx
+len
> sizeof(buf
)));
138 for (i
= 1; i
<= len
; i
++) {
139 buf
[i
] = regs
[idx
-1+i
];
142 tda18271_i2c_gate_ctrl(fe
, 1);
144 /* write registers */
145 ret
= i2c_transfer(priv
->i2c_adap
, &msg
, 1);
147 tda18271_i2c_gate_ctrl(fe
, 0);
150 printk(KERN_WARNING
"ERROR: %s: i2c_transfer returned: %d\n",
154 /*---------------------------------------------------------------------*/
156 static int tda18271_init_regs(struct dvb_frontend
*fe
)
158 struct tda18271_priv
*priv
= fe
->tuner_priv
;
159 unsigned char *regs
= priv
->tda18271_regs
;
161 printk(KERN_INFO
"tda18271: initializing registers\n");
163 /* initialize registers */
204 tda18271_write_regs(fe
, 0x00, TDA18271_NUM_REGS
);
205 /* setup AGC1 & AGC2 */
207 tda18271_write_regs(fe
, R_EB17
, 1);
209 tda18271_write_regs(fe
, R_EB17
, 1);
211 tda18271_write_regs(fe
, R_EB17
, 1);
213 tda18271_write_regs(fe
, R_EB17
, 1);
216 tda18271_write_regs(fe
, R_EB20
, 1);
218 tda18271_write_regs(fe
, R_EB20
, 1);
220 tda18271_write_regs(fe
, R_EB20
, 1);
222 tda18271_write_regs(fe
, R_EB20
, 1);
224 /* image rejection calibration */
239 tda18271_write_regs(fe
, R_EP3
, 11);
240 msleep(5); /* pll locking */
243 tda18271_write_regs(fe
, R_EP1
, 1);
244 msleep(5); /* wanted low measurement */
254 tda18271_write_regs(fe
, R_EP3
, 7);
255 msleep(5); /* pll locking */
258 tda18271_write_regs(fe
, R_EP2
, 1);
259 msleep(30); /* image low optimization completion */
274 tda18271_write_regs(fe
, R_EP3
, 11);
275 msleep(5); /* pll locking */
278 tda18271_write_regs(fe
, R_EP1
, 1);
279 msleep(5); /* wanted mid measurement */
289 tda18271_write_regs(fe
, R_EP3
, 7);
290 msleep(5); /* pll locking */
293 tda18271_write_regs(fe
, R_EP2
, 1);
294 msleep(30); /* image mid optimization completion */
309 tda18271_write_regs(fe
, R_EP3
, 11);
310 msleep(5); /* pll locking */
313 tda18271_write_regs(fe
, R_EP1
, 1);
314 msleep(5); /* wanted high measurement */
324 tda18271_write_regs(fe
, R_EP3
, 7);
325 msleep(5); /* pll locking */
329 tda18271_write_regs(fe
, R_EP2
, 1);
330 msleep(30); /* image high optimization completion */
333 tda18271_write_regs(fe
, R_EP4
, 1);
336 tda18271_write_regs(fe
, R_EP1
, 1);
341 static int tda18271_init(struct dvb_frontend
*fe
)
343 struct tda18271_priv
*priv
= fe
->tuner_priv
;
344 unsigned char *regs
= priv
->tda18271_regs
;
346 tda18271_read_regs(fe
);
348 /* test IR_CAL_OK to see if we need init */
349 if ((regs
[R_EP1
] & 0x08) == 0)
350 tda18271_init_regs(fe
);
355 static int tda18271_calc_main_pll(struct dvb_frontend
*fe
, u32 freq
)
357 /* Sets Main Post-Divider & Divider bytes, but does not write them */
358 struct tda18271_priv
*priv
= fe
->tuner_priv
;
359 unsigned char *regs
= priv
->tda18271_regs
;
363 tda18271_lookup_main_pll(&freq
, &pd
, &d
);
365 regs
[R_MPD
] = (0x77 & pd
);
367 switch (priv
->mode
) {
368 case TDA18271_ANALOG
:
369 regs
[R_MPD
] &= ~0x08;
371 case TDA18271_DIGITAL
:
376 div
= ((d
* (freq
/ 1000)) << 7) / 125;
378 regs
[R_MD1
] = 0x7f & (div
>> 16);
379 regs
[R_MD2
] = 0xff & (div
>> 8);
380 regs
[R_MD3
] = 0xff & div
;
385 static int tda18271_calc_cal_pll(struct dvb_frontend
*fe
, u32 freq
)
387 /* Sets Cal Post-Divider & Divider bytes, but does not write them */
388 struct tda18271_priv
*priv
= fe
->tuner_priv
;
389 unsigned char *regs
= priv
->tda18271_regs
;
393 tda18271_lookup_cal_pll(&freq
, &pd
, &d
);
397 div
= ((d
* (freq
/ 1000)) << 7) / 125;
399 regs
[R_CD1
] = 0x7f & (div
>> 16);
400 regs
[R_CD2
] = 0xff & (div
>> 8);
401 regs
[R_CD3
] = 0xff & div
;
406 static int tda18271_tune(struct dvb_frontend
*fe
,
407 u32 ifc
, u32 freq
, u32 bw
, u8 std
)
409 struct tda18271_priv
*priv
= fe
->tuner_priv
;
410 unsigned char *regs
= priv
->tda18271_regs
;
416 dbg_info("freq = %d, ifc = %d\n", freq
, ifc
);
418 /* RF tracking filter calibration */
420 /* calculate BP_Filter */
421 tda18271_lookup_bp_filter(&freq
, &val
);
423 regs
[R_EP1
] &= ~0x07; /* clear bp filter bits */
425 tda18271_write_regs(fe
, R_EP1
, 1);
429 tda18271_write_regs(fe
, R_EB4
, 1);
432 tda18271_write_regs(fe
, R_EB7
, 1);
435 tda18271_write_regs(fe
, R_EB14
, 1);
438 tda18271_write_regs(fe
, R_EB20
, 1);
440 /* set CAL mode to RF tracking filter calibration */
443 /* calculate CAL PLL */
445 switch (priv
->mode
) {
446 case TDA18271_ANALOG
:
449 case TDA18271_DIGITAL
:
454 tda18271_calc_cal_pll(fe
, N
);
456 /* calculate MAIN PLL */
458 switch (priv
->mode
) {
459 case TDA18271_ANALOG
:
462 case TDA18271_DIGITAL
:
463 N
= freq
+ bw
/ 2 + 1000000;
467 tda18271_calc_main_pll(fe
, N
);
469 tda18271_write_regs(fe
, R_EP3
, 11);
470 msleep(5); /* RF tracking filter calibration initialization */
472 /* search for K,M,CO for RF Calibration */
473 tda18271_lookup_km(&freq
, &val
);
475 regs
[R_EB13
] &= 0x83;
477 tda18271_write_regs(fe
, R_EB13
, 1);
479 /* search for RF_BAND */
480 tda18271_lookup_rf_band(&freq
, &val
);
482 regs
[R_EP2
] &= ~0xe0; /* clear rf band bits */
483 regs
[R_EP2
] |= (val
<< 5);
485 /* search for Gain_Taper */
486 tda18271_lookup_gain_taper(&freq
, &val
);
488 regs
[R_EP2
] &= ~0x1f; /* clear gain taper bits */
491 tda18271_write_regs(fe
, R_EP2
, 1);
492 tda18271_write_regs(fe
, R_EP1
, 1);
493 tda18271_write_regs(fe
, R_EP2
, 1);
494 tda18271_write_regs(fe
, R_EP1
, 1);
498 tda18271_write_regs(fe
, R_EB4
, 1);
501 tda18271_write_regs(fe
, R_EB7
, 1);
505 tda18271_write_regs(fe
, R_EB20
, 1);
506 msleep(60); /* RF tracking filter calibration completion */
508 regs
[R_EP4
] &= ~0x03; /* set cal mode to normal */
509 tda18271_write_regs(fe
, R_EP4
, 1);
511 tda18271_write_regs(fe
, R_EP1
, 1);
513 /* RF tracking filer correction for VHF_Low band */
514 tda18271_lookup_rf_cal(&freq
, &val
);
516 /* VHF_Low band only */
519 tda18271_write_regs(fe
, R_EB14
, 1);
522 /* Channel Configuration */
524 switch (priv
->mode
) {
525 case TDA18271_ANALOG
:
528 case TDA18271_DIGITAL
:
532 tda18271_write_regs(fe
, R_EB22
, 1);
534 regs
[R_EP1
] |= 0x40; /* set dis power level on */
537 regs
[R_EP3
] &= ~0x1f; /* clear std bits */
542 regs
[R_EP4
] &= ~0x03; /* set cal mode to normal */
544 regs
[R_EP4
] &= ~0x1c; /* clear if level bits */
545 switch (priv
->mode
) {
546 case TDA18271_ANALOG
:
547 regs
[R_MPD
] &= ~0x80; /* IF notch = 0 */
549 case TDA18271_DIGITAL
:
555 regs
[R_EP4
] &= ~0x80; /* turn this bit on only for fm */
557 /* image rejection validity EP5[2:0] */
558 tda18271_lookup_ir_measure(&freq
, &val
);
560 regs
[R_EP5
] &= ~0x07;
563 /* calculate MAIN PLL */
566 tda18271_calc_main_pll(fe
, N
);
568 tda18271_write_regs(fe
, R_TM
, 15);
574 /* ------------------------------------------------------------------ */
576 static int tda18271_set_params(struct dvb_frontend
*fe
,
577 struct dvb_frontend_parameters
*params
)
579 struct tda18271_priv
*priv
= fe
->tuner_priv
;
583 u32 freq
= params
->frequency
;
585 priv
->mode
= TDA18271_DIGITAL
;
588 if (fe
->ops
.info
.type
== FE_ATSC
) {
589 switch (params
->u
.vsb
.modulation
) {
592 std
= 0x1b; /* device-specific (spec says 0x1c) */
597 std
= 0x18; /* device-specific (spec says 0x1d) */
601 printk(KERN_WARNING
"%s: modulation not set!\n",
606 /* userspace request is already center adjusted */
607 freq
+= 1750000; /* Adjust to center (+1.75MHZ) */
610 } else if (fe
->ops
.info
.type
== FE_OFDM
) {
611 switch (params
->u
.ofdm
.bandwidth
) {
612 case BANDWIDTH_6_MHZ
:
613 std
= 0x1b; /* device-specific (spec says 0x1c) */
617 case BANDWIDTH_7_MHZ
:
618 std
= 0x19; /* device-specific (spec says 0x1d) */
622 case BANDWIDTH_8_MHZ
:
623 std
= 0x1a; /* device-specific (spec says 0x1e) */
628 printk(KERN_WARNING
"%s: bandwidth not set!\n",
633 printk(KERN_WARNING
"%s: modulation type not supported!\n",
638 return tda18271_tune(fe
, sgIF
, freq
, bw
, std
);
641 static int tda18271_set_analog_params(struct dvb_frontend
*fe
,
642 struct analog_parameters
*params
)
644 struct tda18271_priv
*priv
= fe
->tuner_priv
;
649 priv
->mode
= TDA18271_ANALOG
;
652 if (params
->std
& V4L2_STD_MN
) {
656 } else if (params
->std
& V4L2_STD_B
) {
660 } else if (params
->std
& V4L2_STD_GH
) {
664 } else if (params
->std
& V4L2_STD_PAL_I
) {
668 } else if (params
->std
& V4L2_STD_DK
) {
672 } else if (params
->std
& V4L2_STD_SECAM_L
) {
676 } else if (params
->std
& V4L2_STD_SECAM_LC
) {
686 if (params
->mode
== V4L2_TUNER_RADIO
)
687 sgIF
= 88; /* if frequency is 5.5 MHz */
689 dbg_info("setting tda18271 to system %s\n", mode
);
691 return tda18271_tune(fe
, sgIF
* 62500, params
->frequency
* 62500,
695 static int tda18271_release(struct dvb_frontend
*fe
)
697 kfree(fe
->tuner_priv
);
698 fe
->tuner_priv
= NULL
;
702 static int tda18271_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
704 struct tda18271_priv
*priv
= fe
->tuner_priv
;
705 *frequency
= priv
->frequency
;
709 static int tda18271_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
711 struct tda18271_priv
*priv
= fe
->tuner_priv
;
712 *bandwidth
= priv
->bandwidth
;
716 static int tda18271_get_id(struct dvb_frontend
*fe
)
718 struct tda18271_priv
*priv
= fe
->tuner_priv
;
719 unsigned char *regs
= priv
->tda18271_regs
;
723 tda18271_read_regs(fe
);
725 switch (regs
[R_ID
] & 0x7f) {
727 name
= "TDA18271HD/C1";
730 name
= "TDA18271HD/C2";
731 ret
= -EPROTONOSUPPORT
;
734 name
= "Unknown device";
739 dbg_info("%s detected @ %d-%04x%s\n", name
,
740 i2c_adapter_id(priv
->i2c_adap
), priv
->i2c_addr
,
741 (0 == ret
) ? "" : ", device not supported.");
746 static struct dvb_tuner_ops tda18271_tuner_ops
= {
748 .name
= "NXP TDA18271HD",
749 .frequency_min
= 45000000,
750 .frequency_max
= 864000000,
751 .frequency_step
= 62500
753 .init
= tda18271_init
,
754 .set_params
= tda18271_set_params
,
755 .set_analog_params
= tda18271_set_analog_params
,
756 .release
= tda18271_release
,
757 .get_frequency
= tda18271_get_frequency
,
758 .get_bandwidth
= tda18271_get_bandwidth
,
761 struct dvb_frontend
*tda18271_attach(struct dvb_frontend
*fe
, u8 addr
,
762 struct i2c_adapter
*i2c
,
763 enum tda18271_i2c_gate gate
)
765 struct tda18271_priv
*priv
= NULL
;
767 priv
= kzalloc(sizeof(struct tda18271_priv
), GFP_KERNEL
);
771 priv
->i2c_addr
= addr
;
772 priv
->i2c_adap
= i2c
;
775 fe
->tuner_priv
= priv
;
777 if (tda18271_get_id(fe
) < 0)
780 memcpy(&fe
->ops
.tuner_ops
, &tda18271_tuner_ops
,
781 sizeof(struct dvb_tuner_ops
));
783 tda18271_init_regs(fe
);
787 tda18271_release(fe
);
790 EXPORT_SYMBOL_GPL(tda18271_attach
);
791 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
792 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
793 MODULE_LICENSE("GPL");
796 * Overrides for Emacs so that we follow Linus's tabbing style.
797 * ---------------------------------------------------------------------------