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 "tuner-driver.h"
26 #include "tda18271-priv.h"
29 module_param_named(debug, tda18271_debug, int, 0644);
30 MODULE_PARM_DESC(debug, "set debug level (info=1, map=2, reg=4 (or-able))");
32 /*---------------------------------------------------------------------*/
39 struct tda18271_priv {
41 struct i2c_adapter *i2c_adap;
42 unsigned char tda18271_regs[TDA18271_NUM_REGS];
44 enum tda18271_mode mode;
45 enum tda18271_i2c_gate gate;
51 static int tda18271_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
53 struct tda18271_priv *priv = fe->tuner_priv;
54 struct analog_tuner_ops *ops = fe->ops.analog_demod_ops;
55 enum tda18271_i2c_gate gate;
59 case TDA18271_GATE_DIGITAL:
60 case TDA18271_GATE_ANALOG:
63 case TDA18271_GATE_AUTO:
66 case TDA18271_DIGITAL:
67 gate = TDA18271_GATE_DIGITAL;
71 gate = TDA18271_GATE_ANALOG;
77 case TDA18271_GATE_ANALOG:
78 if (ops && ops->i2c_gate_ctrl)
79 ret = ops->i2c_gate_ctrl(fe, enable);
81 case TDA18271_GATE_DIGITAL:
82 if (fe->ops.i2c_gate_ctrl)
83 ret = fe->ops.i2c_gate_ctrl(fe, enable);
93 /*---------------------------------------------------------------------*/
95 static void tda18271_dump_regs(struct dvb_frontend *fe)
97 struct tda18271_priv *priv = fe->tuner_priv;
98 unsigned char *regs = priv->tda18271_regs;
100 dbg_reg("=== TDA18271 REG DUMP ===\n");
101 dbg_reg("ID_BYTE = 0x%02x\n", 0xff & regs[R_ID]);
102 dbg_reg("THERMO_BYTE = 0x%02x\n", 0xff & regs[R_TM]);
103 dbg_reg("POWER_LEVEL_BYTE = 0x%02x\n", 0xff & regs[R_PL]);
104 dbg_reg("EASY_PROG_BYTE_1 = 0x%02x\n", 0xff & regs[R_EP1]);
105 dbg_reg("EASY_PROG_BYTE_2 = 0x%02x\n", 0xff & regs[R_EP2]);
106 dbg_reg("EASY_PROG_BYTE_3 = 0x%02x\n", 0xff & regs[R_EP3]);
107 dbg_reg("EASY_PROG_BYTE_4 = 0x%02x\n", 0xff & regs[R_EP4]);
108 dbg_reg("EASY_PROG_BYTE_5 = 0x%02x\n", 0xff & regs[R_EP5]);
109 dbg_reg("CAL_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_CPD]);
110 dbg_reg("CAL_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_CD1]);
111 dbg_reg("CAL_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_CD2]);
112 dbg_reg("CAL_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_CD3]);
113 dbg_reg("MAIN_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_MPD]);
114 dbg_reg("MAIN_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_MD1]);
115 dbg_reg("MAIN_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_MD2]);
116 dbg_reg("MAIN_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_MD3]);
119 static void tda18271_read_regs(struct dvb_frontend *fe)
121 struct tda18271_priv *priv = fe->tuner_priv;
122 unsigned char *regs = priv->tda18271_regs;
123 unsigned char buf = 0x00;
125 struct i2c_msg msg[] = {
126 { .addr = priv->i2c_addr, .flags = 0,
127 .buf = &buf, .len = 1 },
128 { .addr = priv->i2c_addr, .flags = I2C_M_RD,
129 .buf = regs, .len = 16 }
132 tda18271_i2c_gate_ctrl(fe, 1);
134 /* read all registers */
135 ret = i2c_transfer(priv->i2c_adap, msg, 2);
137 tda18271_i2c_gate_ctrl(fe, 0);
140 printk("ERROR: %s: i2c_transfer returned: %d\n",
143 if (tda18271_debug & DBG_REG)
144 tda18271_dump_regs(fe);
147 static void tda18271_write_regs(struct dvb_frontend *fe, int idx, int len)
149 struct tda18271_priv *priv = fe->tuner_priv;
150 unsigned char *regs = priv->tda18271_regs;
151 unsigned char buf[TDA18271_NUM_REGS+1];
152 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
153 .buf = buf, .len = len+1 };
156 BUG_ON((len == 0) || (idx+len > sizeof(buf)));
159 for (i = 1; i <= len; i++) {
160 buf[i] = regs[idx-1+i];
163 tda18271_i2c_gate_ctrl(fe, 1);
165 /* write registers */
166 ret = i2c_transfer(priv->i2c_adap, &msg, 1);
168 tda18271_i2c_gate_ctrl(fe, 0);
171 printk(KERN_WARNING "ERROR: %s: i2c_transfer returned: %d\n",
175 /*---------------------------------------------------------------------*/
177 static int tda18271_init_regs(struct dvb_frontend *fe)
179 struct tda18271_priv *priv = fe->tuner_priv;
180 unsigned char *regs = priv->tda18271_regs;
182 printk(KERN_INFO "tda18271: initializing registers\n");
184 /* initialize registers */
225 tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS);
226 /* setup AGC1 & AGC2 */
228 tda18271_write_regs(fe, R_EB17, 1);
230 tda18271_write_regs(fe, R_EB17, 1);
232 tda18271_write_regs(fe, R_EB17, 1);
234 tda18271_write_regs(fe, R_EB17, 1);
237 tda18271_write_regs(fe, R_EB20, 1);
239 tda18271_write_regs(fe, R_EB20, 1);
241 tda18271_write_regs(fe, R_EB20, 1);
243 tda18271_write_regs(fe, R_EB20, 1);
245 /* image rejection calibration */
260 tda18271_write_regs(fe, R_EP3, 11);
261 msleep(5); /* pll locking */
264 tda18271_write_regs(fe, R_EP1, 1);
265 msleep(5); /* wanted low measurement */
275 tda18271_write_regs(fe, R_EP3, 7);
276 msleep(5); /* pll locking */
279 tda18271_write_regs(fe, R_EP2, 1);
280 msleep(30); /* image low optimization completion */
295 tda18271_write_regs(fe, R_EP3, 11);
296 msleep(5); /* pll locking */
299 tda18271_write_regs(fe, R_EP1, 1);
300 msleep(5); /* wanted mid measurement */
310 tda18271_write_regs(fe, R_EP3, 7);
311 msleep(5); /* pll locking */
314 tda18271_write_regs(fe, R_EP2, 1);
315 msleep(30); /* image mid optimization completion */
330 tda18271_write_regs(fe, R_EP3, 11);
331 msleep(5); /* pll locking */
334 tda18271_write_regs(fe, R_EP1, 1);
335 msleep(5); /* wanted high measurement */
345 tda18271_write_regs(fe, R_EP3, 7);
346 msleep(5); /* pll locking */
350 tda18271_write_regs(fe, R_EP2, 1);
351 msleep(30); /* image high optimization completion */
354 tda18271_write_regs(fe, R_EP4, 1);
357 tda18271_write_regs(fe, R_EP1, 1);
362 static int tda18271_init(struct dvb_frontend *fe)
364 struct tda18271_priv *priv = fe->tuner_priv;
365 unsigned char *regs = priv->tda18271_regs;
367 tda18271_read_regs(fe);
369 /* test IR_CAL_OK to see if we need init */
370 if ((regs[R_EP1] & 0x08) == 0)
371 tda18271_init_regs(fe);
376 static int tda18271_tune(struct dvb_frontend *fe,
377 u32 ifc, u32 freq, u32 bw, u8 std)
379 struct tda18271_priv *priv = fe->tuner_priv;
380 unsigned char *regs = priv->tda18271_regs;
386 dbg_info("freq = %d, ifc = %d\n", freq, ifc);
388 /* RF tracking filter calibration */
390 /* calculate BP_Filter */
391 tda18271_calc_bp_filter(&freq, &val);
393 regs[R_EP1] &= ~0x07; /* clear bp filter bits */
395 tda18271_write_regs(fe, R_EP1, 1);
399 tda18271_write_regs(fe, R_EB4, 1);
402 tda18271_write_regs(fe, R_EB7, 1);
405 tda18271_write_regs(fe, R_EB14, 1);
408 tda18271_write_regs(fe, R_EB20, 1);
410 /* set CAL mode to RF tracking filter calibration */
413 /* calculate CAL PLL */
415 switch (priv->mode) {
416 case TDA18271_ANALOG:
419 case TDA18271_DIGITAL:
424 tda18271_calc_cal_pll(&N, &pd, &d);
428 div = ((d * (N / 1000)) << 7) / 125;
429 regs[R_CD1] = 0xff & (div >> 16);
430 regs[R_CD2] = 0xff & (div >> 8);
431 regs[R_CD3] = 0xff & div;
433 /* calculate MAIN PLL */
435 switch (priv->mode) {
436 case TDA18271_ANALOG:
439 case TDA18271_DIGITAL:
440 N = freq + bw / 2 + 1000000;
444 tda18271_calc_main_pll(&N, &pd, &d);
446 regs[R_MPD] = (0x7f & pd);
448 switch (priv->mode) {
449 case TDA18271_ANALOG:
450 regs[R_MPD] &= ~0x08;
452 case TDA18271_DIGITAL:
457 div = ((d * (N / 1000)) << 7) / 125;
458 regs[R_MD1] = 0xff & (div >> 16);
459 regs[R_MD2] = 0xff & (div >> 8);
460 regs[R_MD3] = 0xff & div;
462 tda18271_write_regs(fe, R_EP3, 11);
463 msleep(5); /* RF tracking filter calibration initialization */
465 /* search for K,M,CO for RF Calibration */
466 tda18271_calc_km(&freq, &val);
468 regs[R_EB13] &= 0x83;
470 tda18271_write_regs(fe, R_EB13, 1);
472 /* search for RF_BAND */
473 tda18271_calc_rf_band(&freq, &val);
475 regs[R_EP2] &= ~0xe0; /* clear rf band bits */
476 regs[R_EP2] |= (val << 5);
478 /* search for Gain_Taper */
479 tda18271_calc_gain_taper(&freq, &val);
481 regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
484 tda18271_write_regs(fe, R_EP2, 1);
485 tda18271_write_regs(fe, R_EP1, 1);
486 tda18271_write_regs(fe, R_EP2, 1);
487 tda18271_write_regs(fe, R_EP1, 1);
491 tda18271_write_regs(fe, R_EB4, 1);
494 tda18271_write_regs(fe, R_EB7, 1);
498 tda18271_write_regs(fe, R_EB20, 1);
499 msleep(60); /* RF tracking filter calibration completion */
501 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
502 tda18271_write_regs(fe, R_EP4, 1);
504 tda18271_write_regs(fe, R_EP1, 1);
506 /* RF tracking filer correction for VHF_Low band */
507 tda18271_calc_rf_cal(&freq, &val);
509 /* VHF_Low band only */
512 tda18271_write_regs(fe, R_EB14, 1);
515 /* Channel Configuration */
517 switch (priv->mode) {
518 case TDA18271_ANALOG:
521 case TDA18271_DIGITAL:
525 tda18271_write_regs(fe, R_EB22, 1);
527 regs[R_EP1] |= 0x40; /* set dis power level on */
530 regs[R_EP3] &= ~0x1f; /* clear std bits */
535 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
537 regs[R_EP4] &= ~0x1c; /* clear if level bits */
538 switch (priv->mode) {
539 case TDA18271_ANALOG:
540 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
542 case TDA18271_DIGITAL:
548 regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */
550 /* image rejection validity EP5[2:0] */
551 tda18271_calc_ir_measure(&freq, &val);
553 regs[R_EP5] &= ~0x07;
556 /* calculate MAIN PLL */
559 tda18271_calc_main_pll(&N, &pd, &d);
561 regs[R_MPD] = (0x7f & pd);
562 switch (priv->mode) {
563 case TDA18271_ANALOG:
564 regs[R_MPD] &= ~0x08;
566 case TDA18271_DIGITAL:
571 div = ((d * (N / 1000)) << 7) / 125;
572 regs[R_MD1] = 0xff & (div >> 16);
573 regs[R_MD2] = 0xff & (div >> 8);
574 regs[R_MD3] = 0xff & div;
576 tda18271_write_regs(fe, R_TM, 15);
582 /* ------------------------------------------------------------------ */
584 static int tda18271_set_params(struct dvb_frontend *fe,
585 struct dvb_frontend_parameters *params)
587 struct tda18271_priv *priv = fe->tuner_priv;
591 u32 freq = params->frequency;
593 priv->mode = TDA18271_DIGITAL;
596 if (fe->ops.info.type == FE_ATSC) {
597 switch (params->u.vsb.modulation) {
600 std = 0x1b; /* device-specific (spec says 0x1c) */
605 std = 0x18; /* device-specific (spec says 0x1d) */
609 printk(KERN_WARNING "%s: modulation not set!\n",
614 /* userspace request is already center adjusted */
615 freq += 1750000; /* Adjust to center (+1.75MHZ) */
618 } else if (fe->ops.info.type == FE_OFDM) {
619 switch (params->u.ofdm.bandwidth) {
620 case BANDWIDTH_6_MHZ:
621 std = 0x1b; /* device-specific (spec says 0x1c) */
625 case BANDWIDTH_7_MHZ:
626 std = 0x19; /* device-specific (spec says 0x1d) */
630 case BANDWIDTH_8_MHZ:
631 std = 0x1a; /* device-specific (spec says 0x1e) */
636 printk(KERN_WARNING "%s: bandwidth not set!\n",
641 printk(KERN_WARNING "%s: modulation type not supported!\n",
646 return tda18271_tune(fe, sgIF, freq, bw, std);
649 static int tda18271_set_analog_params(struct dvb_frontend *fe,
650 struct analog_parameters *params)
652 struct tda18271_priv *priv = fe->tuner_priv;
657 priv->mode = TDA18271_ANALOG;
660 if (params->std & V4L2_STD_MN) {
664 } else if (params->std & V4L2_STD_B) {
668 } else if (params->std & V4L2_STD_GH) {
672 } else if (params->std & V4L2_STD_PAL_I) {
676 } else if (params->std & V4L2_STD_DK) {
680 } else if (params->std & V4L2_STD_SECAM_L) {
684 } else if (params->std & V4L2_STD_SECAM_LC) {
694 if (params->mode == V4L2_TUNER_RADIO)
695 sgIF = 88; /* if frequency is 5.5 MHz */
697 dbg_info("setting tda18271 to system %s\n", mode);
699 return tda18271_tune(fe, sgIF * 62500, params->frequency * 62500,
703 static int tda18271_release(struct dvb_frontend *fe)
705 kfree(fe->tuner_priv);
706 fe->tuner_priv = NULL;
710 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
712 struct tda18271_priv *priv = fe->tuner_priv;
713 *frequency = priv->frequency;
717 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
719 struct tda18271_priv *priv = fe->tuner_priv;
720 *bandwidth = priv->bandwidth;
724 static struct dvb_tuner_ops tda18271_tuner_ops = {
726 .name = "NXP TDA18271HD",
727 .frequency_min = 45000000,
728 .frequency_max = 864000000,
729 .frequency_step = 62500
731 .init = tda18271_init,
732 .set_params = tda18271_set_params,
733 .set_analog_params = tda18271_set_analog_params,
734 .release = tda18271_release,
735 .get_frequency = tda18271_get_frequency,
736 .get_bandwidth = tda18271_get_bandwidth,
739 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
740 struct i2c_adapter *i2c,
741 enum tda18271_i2c_gate gate)
743 struct tda18271_priv *priv = NULL;
745 dbg_info("@ %d-%04x\n", i2c_adapter_id(i2c), addr);
746 priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
750 priv->i2c_addr = addr;
751 priv->i2c_adap = i2c;
754 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
755 sizeof(struct dvb_tuner_ops));
757 fe->tuner_priv = priv;
759 tda18271_init_regs(fe);
763 EXPORT_SYMBOL_GPL(tda18271_attach);
764 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
765 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
766 MODULE_LICENSE("GPL");
769 * Overrides for Emacs so that we follow Linus's tabbing style.
770 * ---------------------------------------------------------------------------