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 tda_reg("=== TDA18271 REG DUMP ===\n");
80 tda_reg("ID_BYTE = 0x%02x\n", 0xff & regs[R_ID]);
81 tda_reg("THERMO_BYTE = 0x%02x\n", 0xff & regs[R_TM]);
82 tda_reg("POWER_LEVEL_BYTE = 0x%02x\n", 0xff & regs[R_PL]);
83 tda_reg("EASY_PROG_BYTE_1 = 0x%02x\n", 0xff & regs[R_EP1]);
84 tda_reg("EASY_PROG_BYTE_2 = 0x%02x\n", 0xff & regs[R_EP2]);
85 tda_reg("EASY_PROG_BYTE_3 = 0x%02x\n", 0xff & regs[R_EP3]);
86 tda_reg("EASY_PROG_BYTE_4 = 0x%02x\n", 0xff & regs[R_EP4]);
87 tda_reg("EASY_PROG_BYTE_5 = 0x%02x\n", 0xff & regs[R_EP5]);
88 tda_reg("CAL_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_CPD]);
89 tda_reg("CAL_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_CD1]);
90 tda_reg("CAL_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_CD2]);
91 tda_reg("CAL_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_CD3]);
92 tda_reg("MAIN_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_MPD]);
93 tda_reg("MAIN_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_MD1]);
94 tda_reg("MAIN_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_MD2]);
95 tda_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 tda_err("ERROR: i2c_transfer returned: %d\n", ret);
121 if (tda18271_debug & DBG_REG)
122 tda18271_dump_regs(fe);
125 static void tda18271_write_regs(struct dvb_frontend *fe, int idx, int len)
127 struct tda18271_priv *priv = fe->tuner_priv;
128 unsigned char *regs = priv->tda18271_regs;
129 unsigned char buf[TDA18271_NUM_REGS+1];
130 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
131 .buf = buf, .len = len+1 };
134 BUG_ON((len == 0) || (idx+len > sizeof(buf)));
137 for (i = 1; i <= len; i++) {
138 buf[i] = regs[idx-1+i];
141 tda18271_i2c_gate_ctrl(fe, 1);
143 /* write registers */
144 ret = i2c_transfer(priv->i2c_adap, &msg, 1);
146 tda18271_i2c_gate_ctrl(fe, 0);
149 tda_err("ERROR: i2c_transfer returned: %d\n", ret);
152 /*---------------------------------------------------------------------*/
154 static int tda18271_init_regs(struct dvb_frontend *fe)
156 struct tda18271_priv *priv = fe->tuner_priv;
157 unsigned char *regs = priv->tda18271_regs;
159 tda_dbg("initializing registers for device @ %d-%04x\n",
160 i2c_adapter_id(priv->i2c_adap), priv->i2c_addr);
162 /* initialize registers */
203 tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS);
204 /* setup AGC1 & AGC2 */
206 tda18271_write_regs(fe, R_EB17, 1);
208 tda18271_write_regs(fe, R_EB17, 1);
210 tda18271_write_regs(fe, R_EB17, 1);
212 tda18271_write_regs(fe, R_EB17, 1);
215 tda18271_write_regs(fe, R_EB20, 1);
217 tda18271_write_regs(fe, R_EB20, 1);
219 tda18271_write_regs(fe, R_EB20, 1);
221 tda18271_write_regs(fe, R_EB20, 1);
223 /* image rejection calibration */
238 tda18271_write_regs(fe, R_EP3, 11);
239 msleep(5); /* pll locking */
242 tda18271_write_regs(fe, R_EP1, 1);
243 msleep(5); /* wanted low measurement */
253 tda18271_write_regs(fe, R_EP3, 7);
254 msleep(5); /* pll locking */
257 tda18271_write_regs(fe, R_EP2, 1);
258 msleep(30); /* image low optimization completion */
273 tda18271_write_regs(fe, R_EP3, 11);
274 msleep(5); /* pll locking */
277 tda18271_write_regs(fe, R_EP1, 1);
278 msleep(5); /* wanted mid measurement */
288 tda18271_write_regs(fe, R_EP3, 7);
289 msleep(5); /* pll locking */
292 tda18271_write_regs(fe, R_EP2, 1);
293 msleep(30); /* image mid optimization completion */
308 tda18271_write_regs(fe, R_EP3, 11);
309 msleep(5); /* pll locking */
312 tda18271_write_regs(fe, R_EP1, 1);
313 msleep(5); /* wanted high measurement */
323 tda18271_write_regs(fe, R_EP3, 7);
324 msleep(5); /* pll locking */
328 tda18271_write_regs(fe, R_EP2, 1);
329 msleep(30); /* image high optimization completion */
332 tda18271_write_regs(fe, R_EP4, 1);
335 tda18271_write_regs(fe, R_EP1, 1);
340 static int tda18271_init(struct dvb_frontend *fe)
342 struct tda18271_priv *priv = fe->tuner_priv;
343 unsigned char *regs = priv->tda18271_regs;
345 tda18271_read_regs(fe);
347 /* test IR_CAL_OK to see if we need init */
348 if ((regs[R_EP1] & 0x08) == 0)
349 tda18271_init_regs(fe);
354 static int tda18271_calc_main_pll(struct dvb_frontend *fe, u32 freq)
356 /* Sets Main Post-Divider & Divider bytes, but does not write them */
357 struct tda18271_priv *priv = fe->tuner_priv;
358 unsigned char *regs = priv->tda18271_regs;
362 int ret = tda18271_lookup_pll_map(MAIN_PLL, &freq, &pd, &d);
366 regs[R_MPD] = (0x77 & pd);
368 switch (priv->mode) {
369 case TDA18271_ANALOG:
370 regs[R_MPD] &= ~0x08;
372 case TDA18271_DIGITAL:
377 div = ((d * (freq / 1000)) << 7) / 125;
379 regs[R_MD1] = 0x7f & (div >> 16);
380 regs[R_MD2] = 0xff & (div >> 8);
381 regs[R_MD3] = 0xff & div;
386 static int tda18271_calc_cal_pll(struct dvb_frontend *fe, u32 freq)
388 /* Sets Cal Post-Divider & Divider bytes, but does not write them */
389 struct tda18271_priv *priv = fe->tuner_priv;
390 unsigned char *regs = priv->tda18271_regs;
394 int ret = tda18271_lookup_pll_map(CAL_PLL, &freq, &pd, &d);
400 div = ((d * (freq / 1000)) << 7) / 125;
402 regs[R_CD1] = 0x7f & (div >> 16);
403 regs[R_CD2] = 0xff & (div >> 8);
404 regs[R_CD3] = 0xff & div;
409 static int tda18271_tune(struct dvb_frontend *fe,
410 u32 ifc, u32 freq, u32 bw, u8 std)
412 struct tda18271_priv *priv = fe->tuner_priv;
413 unsigned char *regs = priv->tda18271_regs;
419 tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
421 /* RF tracking filter calibration */
423 /* calculate BP_Filter */
424 tda18271_lookup_map(BP_FILTER, &freq, &val);
426 regs[R_EP1] &= ~0x07; /* clear bp filter bits */
428 tda18271_write_regs(fe, R_EP1, 1);
432 tda18271_write_regs(fe, R_EB4, 1);
435 tda18271_write_regs(fe, R_EB7, 1);
438 tda18271_write_regs(fe, R_EB14, 1);
441 tda18271_write_regs(fe, R_EB20, 1);
443 /* set CAL mode to RF tracking filter calibration */
446 /* calculate CAL PLL */
448 switch (priv->mode) {
449 case TDA18271_ANALOG:
452 case TDA18271_DIGITAL:
457 tda18271_calc_cal_pll(fe, N);
459 /* calculate MAIN PLL */
461 switch (priv->mode) {
462 case TDA18271_ANALOG:
465 case TDA18271_DIGITAL:
466 N = freq + bw / 2 + 1000000;
470 tda18271_calc_main_pll(fe, N);
472 tda18271_write_regs(fe, R_EP3, 11);
473 msleep(5); /* RF tracking filter calibration initialization */
475 /* search for K,M,CO for RF Calibration */
476 tda18271_lookup_map(RF_CAL_KMCO, &freq, &val);
478 regs[R_EB13] &= 0x83;
480 tda18271_write_regs(fe, R_EB13, 1);
482 /* search for RF_BAND */
483 tda18271_lookup_map(RF_BAND, &freq, &val);
485 regs[R_EP2] &= ~0xe0; /* clear rf band bits */
486 regs[R_EP2] |= (val << 5);
488 /* search for Gain_Taper */
489 tda18271_lookup_map(GAIN_TAPER, &freq, &val);
491 regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
494 tda18271_write_regs(fe, R_EP2, 1);
495 tda18271_write_regs(fe, R_EP1, 1);
496 tda18271_write_regs(fe, R_EP2, 1);
497 tda18271_write_regs(fe, R_EP1, 1);
501 tda18271_write_regs(fe, R_EB4, 1);
504 tda18271_write_regs(fe, R_EB7, 1);
508 tda18271_write_regs(fe, R_EB20, 1);
509 msleep(60); /* RF tracking filter calibration completion */
511 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
512 tda18271_write_regs(fe, R_EP4, 1);
514 tda18271_write_regs(fe, R_EP1, 1);
516 /* RF tracking filer correction for VHF_Low band */
517 tda18271_lookup_map(RF_CAL, &freq, &val);
519 /* VHF_Low band only */
522 tda18271_write_regs(fe, R_EB14, 1);
525 /* Channel Configuration */
527 switch (priv->mode) {
528 case TDA18271_ANALOG:
531 case TDA18271_DIGITAL:
535 tda18271_write_regs(fe, R_EB22, 1);
537 regs[R_EP1] |= 0x40; /* set dis power level on */
540 regs[R_EP3] &= ~0x1f; /* clear std bits */
545 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
547 regs[R_EP4] &= ~0x1c; /* clear if level bits */
548 switch (priv->mode) {
549 case TDA18271_ANALOG:
550 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
552 case TDA18271_DIGITAL:
558 regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */
560 /* image rejection validity EP5[2:0] */
561 tda18271_lookup_map(IR_MEASURE, &freq, &val);
563 regs[R_EP5] &= ~0x07;
566 /* calculate MAIN PLL */
569 tda18271_calc_main_pll(fe, N);
571 tda18271_write_regs(fe, R_TM, 15);
577 /* ------------------------------------------------------------------ */
579 static int tda18271_set_params(struct dvb_frontend *fe,
580 struct dvb_frontend_parameters *params)
582 struct tda18271_priv *priv = fe->tuner_priv;
586 u32 freq = params->frequency;
588 priv->mode = TDA18271_DIGITAL;
591 if (fe->ops.info.type == FE_ATSC) {
592 switch (params->u.vsb.modulation) {
595 std = 0x1b; /* device-specific (spec says 0x1c) */
600 std = 0x18; /* device-specific (spec says 0x1d) */
604 tda_warn("modulation not set!\n");
608 /* userspace request is already center adjusted */
609 freq += 1750000; /* Adjust to center (+1.75MHZ) */
612 } else if (fe->ops.info.type == FE_OFDM) {
613 switch (params->u.ofdm.bandwidth) {
614 case BANDWIDTH_6_MHZ:
615 std = 0x1b; /* device-specific (spec says 0x1c) */
619 case BANDWIDTH_7_MHZ:
620 std = 0x19; /* device-specific (spec says 0x1d) */
624 case BANDWIDTH_8_MHZ:
625 std = 0x1a; /* device-specific (spec says 0x1e) */
630 tda_warn("bandwidth not set!\n");
634 tda_warn("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 tda_dbg("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 tda_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 * ---------------------------------------------------------------------------