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"
28 static int tda18271_debug;
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 #define dprintk(level, fmt, arg...) do {\
33 if (tda18271_debug & level) \
34 printk(KERN_DEBUG "%s: " fmt, __FUNCTION__, ##arg); } while (0)
40 #define dbg_info(fmt, arg...) dprintk(DBG_INFO, fmt, ##arg)
41 #define dbg_map(fmt, arg...) dprintk(DBG_MAP, fmt, ##arg)
42 #define dbg_reg(fmt, arg...) dprintk(DBG_REG, fmt, ##arg)
44 /*---------------------------------------------------------------------*/
46 #define TDA18271_ANALOG 0
47 #define TDA18271_DIGITAL 1
49 struct tda18271_priv {
51 struct i2c_adapter *i2c_adap;
52 unsigned char tda18271_regs[TDA18271_NUM_REGS];
59 static int tda18271_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
61 struct tda18271_priv *priv = fe->tuner_priv;
62 struct analog_tuner_ops *ops = fe->ops.analog_demod_ops;
67 if (ops && ops->i2c_gate_ctrl)
68 ret = ops->i2c_gate_ctrl(fe, enable);
70 case TDA18271_DIGITAL:
71 if (fe->ops.i2c_gate_ctrl)
72 ret = fe->ops.i2c_gate_ctrl(fe, enable);
79 /*---------------------------------------------------------------------*/
81 static void tda18271_dump_regs(struct dvb_frontend *fe)
83 struct tda18271_priv *priv = fe->tuner_priv;
84 unsigned char *regs = priv->tda18271_regs;
86 dbg_reg("=== TDA18271 REG DUMP ===\n");
87 dbg_reg("ID_BYTE = 0x%x\n", 0xff & regs[R_ID]);
88 dbg_reg("THERMO_BYTE = 0x%x\n", 0xff & regs[R_TM]);
89 dbg_reg("POWER_LEVEL_BYTE = 0x%x\n", 0xff & regs[R_PL]);
90 dbg_reg("EASY_PROG_BYTE_1 = 0x%x\n", 0xff & regs[R_EP1]);
91 dbg_reg("EASY_PROG_BYTE_2 = 0x%x\n", 0xff & regs[R_EP2]);
92 dbg_reg("EASY_PROG_BYTE_3 = 0x%x\n", 0xff & regs[R_EP3]);
93 dbg_reg("EASY_PROG_BYTE_4 = 0x%x\n", 0xff & regs[R_EP4]);
94 dbg_reg("EASY_PROG_BYTE_5 = 0x%x\n", 0xff & regs[R_EP5]);
95 dbg_reg("CAL_POST_DIV_BYTE = 0x%x\n", 0xff & regs[R_CPD]);
96 dbg_reg("CAL_DIV_BYTE_1 = 0x%x\n", 0xff & regs[R_CD1]);
97 dbg_reg("CAL_DIV_BYTE_2 = 0x%x\n", 0xff & regs[R_CD2]);
98 dbg_reg("CAL_DIV_BYTE_3 = 0x%x\n", 0xff & regs[R_CD3]);
99 dbg_reg("MAIN_POST_DIV_BYTE = 0x%x\n", 0xff & regs[R_MPD]);
100 dbg_reg("MAIN_DIV_BYTE_1 = 0x%x\n", 0xff & regs[R_MD1]);
101 dbg_reg("MAIN_DIV_BYTE_2 = 0x%x\n", 0xff & regs[R_MD2]);
102 dbg_reg("MAIN_DIV_BYTE_3 = 0x%x\n", 0xff & regs[R_MD3]);
105 static void tda18271_read_regs(struct dvb_frontend *fe)
107 struct tda18271_priv *priv = fe->tuner_priv;
108 unsigned char *regs = priv->tda18271_regs;
109 unsigned char buf = 0x00;
111 struct i2c_msg msg[] = {
112 { .addr = priv->i2c_addr, .flags = 0,
113 .buf = &buf, .len = 1 },
114 { .addr = priv->i2c_addr, .flags = I2C_M_RD,
115 .buf = regs, .len = 16 }
118 tda18271_i2c_gate_ctrl(fe, 1);
120 /* read all registers */
121 ret = i2c_transfer(priv->i2c_adap, msg, 2);
123 tda18271_i2c_gate_ctrl(fe, 0);
126 printk("ERROR: %s: i2c_transfer returned: %d\n",
129 if (tda18271_debug & DBG_REG)
130 tda18271_dump_regs(fe);
133 static void tda18271_write_regs(struct dvb_frontend *fe, int idx, int len)
135 struct tda18271_priv *priv = fe->tuner_priv;
136 unsigned char *regs = priv->tda18271_regs;
137 unsigned char buf[TDA18271_NUM_REGS+1];
138 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
139 .buf = buf, .len = len+1 };
142 BUG_ON((len == 0) || (idx+len > sizeof(buf)));
145 for (i = 1; i <= len; i++) {
146 buf[i] = regs[idx-1+i];
149 tda18271_i2c_gate_ctrl(fe, 1);
151 /* write registers */
152 ret = i2c_transfer(priv->i2c_adap, &msg, 1);
154 tda18271_i2c_gate_ctrl(fe, 0);
157 printk(KERN_WARNING "ERROR: %s: i2c_transfer returned: %d\n",
161 /*---------------------------------------------------------------------*/
163 static int tda18271_init_regs(struct dvb_frontend *fe)
165 struct tda18271_priv *priv = fe->tuner_priv;
166 unsigned char *regs = priv->tda18271_regs;
168 printk(KERN_INFO "tda18271: initializing registers\n");
170 /* initialize registers */
211 tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS);
212 /* setup AGC1 & AGC2 */
214 tda18271_write_regs(fe, R_EB17, 1);
216 tda18271_write_regs(fe, R_EB17, 1);
218 tda18271_write_regs(fe, R_EB17, 1);
220 tda18271_write_regs(fe, R_EB17, 1);
223 tda18271_write_regs(fe, R_EB20, 1);
225 tda18271_write_regs(fe, R_EB20, 1);
227 tda18271_write_regs(fe, R_EB20, 1);
229 tda18271_write_regs(fe, R_EB20, 1);
231 /* image rejection calibration */
246 tda18271_write_regs(fe, R_EP3, 11);
247 msleep(5); /* pll locking */
250 tda18271_write_regs(fe, R_EP1, 1);
251 msleep(5); /* wanted low measurement */
261 tda18271_write_regs(fe, R_EP3, 7);
262 msleep(5); /* pll locking */
265 tda18271_write_regs(fe, R_EP2, 1);
266 msleep(30); /* image low optimization completion */
281 tda18271_write_regs(fe, R_EP3, 11);
282 msleep(5); /* pll locking */
285 tda18271_write_regs(fe, R_EP1, 1);
286 msleep(5); /* wanted mid measurement */
296 tda18271_write_regs(fe, R_EP3, 7);
297 msleep(5); /* pll locking */
300 tda18271_write_regs(fe, R_EP2, 1);
301 msleep(30); /* image mid optimization completion */
316 tda18271_write_regs(fe, R_EP3, 11);
317 msleep(5); /* pll locking */
320 tda18271_write_regs(fe, R_EP1, 1);
321 msleep(5); /* wanted high measurement */
331 tda18271_write_regs(fe, R_EP3, 7);
332 msleep(5); /* pll locking */
336 tda18271_write_regs(fe, R_EP2, 1);
337 msleep(30); /* image high optimization completion */
340 tda18271_write_regs(fe, R_EP4, 1);
343 tda18271_write_regs(fe, R_EP1, 1);
348 static int tda18271_init(struct dvb_frontend *fe)
350 struct tda18271_priv *priv = fe->tuner_priv;
351 unsigned char *regs = priv->tda18271_regs;
353 tda18271_read_regs(fe);
355 /* test IR_CAL_OK to see if we need init */
356 if ((regs[R_EP1] & 0x08) == 0)
357 tda18271_init_regs(fe);
362 static int tda18271_tune(struct dvb_frontend *fe,
363 u32 ifc, u32 freq, u32 bw, u8 std)
365 struct tda18271_priv *priv = fe->tuner_priv;
366 unsigned char *regs = priv->tda18271_regs;
372 dbg_info("freq = %d, ifc = %d\n", freq, ifc);
374 /* RF tracking filter calibration */
376 /* calculate BP_Filter */
378 while ((tda18271_bp_filter[i].rfmax * 1000) < freq) {
379 if (tda18271_bp_filter[i + 1].rfmax == 0)
383 dbg_map("bp filter = 0x%x, i = %d\n", tda18271_bp_filter[i].val, i);
385 regs[R_EP1] &= ~0x07; /* clear bp filter bits */
386 regs[R_EP1] |= tda18271_bp_filter[i].val;
387 tda18271_write_regs(fe, R_EP1, 1);
391 tda18271_write_regs(fe, R_EB4, 1);
394 tda18271_write_regs(fe, R_EB7, 1);
397 tda18271_write_regs(fe, R_EB14, 1);
400 tda18271_write_regs(fe, R_EB20, 1);
402 /* set CAL mode to RF tracking filter calibration */
405 /* calculate CAL PLL */
407 switch (priv->mode) {
408 case TDA18271_ANALOG:
411 case TDA18271_DIGITAL:
417 while ((tda18271_cal_pll[i].lomax * 1000) < N) {
418 if (tda18271_cal_pll[i + 1].lomax == 0)
422 dbg_map("cal pll, pd = 0x%x, d = 0x%x, i = %d\n",
423 tda18271_cal_pll[i].pd, tda18271_cal_pll[i].d, i);
425 regs[R_CPD] = tda18271_cal_pll[i].pd;
427 div = ((tda18271_cal_pll[i].d * (N / 1000)) << 7) / 125;
428 regs[R_CD1] = 0xff & (div >> 16);
429 regs[R_CD2] = 0xff & (div >> 8);
430 regs[R_CD3] = 0xff & div;
432 /* calculate MAIN PLL */
434 switch (priv->mode) {
435 case TDA18271_ANALOG:
438 case TDA18271_DIGITAL:
439 N = freq + bw / 2 + 1000000;
444 while ((tda18271_main_pll[i].lomax * 1000) < N) {
445 if (tda18271_main_pll[i + 1].lomax == 0)
449 dbg_map("main pll, pd = 0x%x, d = 0x%x, i = %d\n",
450 tda18271_main_pll[i].pd, tda18271_main_pll[i].d, i);
452 regs[R_MPD] = (0x7f & tda18271_main_pll[i].pd);
454 switch (priv->mode) {
455 case TDA18271_ANALOG:
456 regs[R_MPD] &= ~0x08;
458 case TDA18271_DIGITAL:
463 div = ((tda18271_main_pll[i].d * (N / 1000)) << 7) / 125;
464 regs[R_MD1] = 0xff & (div >> 16);
465 regs[R_MD2] = 0xff & (div >> 8);
466 regs[R_MD3] = 0xff & div;
468 tda18271_write_regs(fe, R_EP3, 11);
469 msleep(5); /* RF tracking filter calibration initialization */
471 /* search for K,M,CO for RF Calibration */
473 while ((tda18271_km[i].rfmax * 1000) < freq) {
474 if (tda18271_km[i + 1].rfmax == 0)
478 dbg_map("km = 0x%x, i = %d\n", tda18271_km[i].val, i);
480 regs[R_EB13] &= 0x83;
481 regs[R_EB13] |= tda18271_km[i].val;
482 tda18271_write_regs(fe, R_EB13, 1);
484 /* search for RF_BAND */
486 while ((tda18271_rf_band[i].rfmax * 1000) < freq) {
487 if (tda18271_rf_band[i + 1].rfmax == 0)
491 dbg_map("rf band = 0x%x, i = %d\n", tda18271_rf_band[i].val, i);
493 regs[R_EP2] &= ~0xe0; /* clear rf band bits */
494 regs[R_EP2] |= (tda18271_rf_band[i].val << 5);
496 /* search for Gain_Taper */
498 while ((tda18271_gain_taper[i].rfmax * 1000) < freq) {
499 if (tda18271_gain_taper[i + 1].rfmax == 0)
503 dbg_map("gain taper = 0x%x, i = %d\n",
504 tda18271_gain_taper[i].val, i);
506 regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
507 regs[R_EP2] |= tda18271_gain_taper[i].val;
509 tda18271_write_regs(fe, R_EP2, 1);
510 tda18271_write_regs(fe, R_EP1, 1);
511 tda18271_write_regs(fe, R_EP2, 1);
512 tda18271_write_regs(fe, R_EP1, 1);
516 tda18271_write_regs(fe, R_EB4, 1);
519 tda18271_write_regs(fe, R_EB7, 1);
523 tda18271_write_regs(fe, R_EB20, 1);
524 msleep(60); /* RF tracking filter calibration completion */
526 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
527 tda18271_write_regs(fe, R_EP4, 1);
529 tda18271_write_regs(fe, R_EP1, 1);
531 /* RF tracking filer correction for VHF_Low band */
533 while ((tda18271_rf_cal[i].rfmax * 1000) < freq) {
534 if (tda18271_rf_cal[i].rfmax == 0)
538 dbg_map("rf cal = 0x%x, i = %d\n", tda18271_rf_cal[i].val, i);
540 /* VHF_Low band only */
541 if (tda18271_rf_cal[i].rfmax != 0) {
542 regs[R_EB14] = tda18271_rf_cal[i].val;
543 tda18271_write_regs(fe, R_EB14, 1);
546 /* Channel Configuration */
548 switch (priv->mode) {
549 case TDA18271_ANALOG:
552 case TDA18271_DIGITAL:
556 tda18271_write_regs(fe, R_EB22, 1);
558 regs[R_EP1] |= 0x40; /* set dis power level on */
561 regs[R_EP3] &= ~0x1f; /* clear std bits */
566 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
568 regs[R_EP4] &= ~0x1c; /* clear if level bits */
569 switch (priv->mode) {
570 case TDA18271_ANALOG:
571 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
573 case TDA18271_DIGITAL:
579 regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */
581 /* image rejection validity EP5[2:0] */
583 while ((tda18271_ir_measure[i].rfmax * 1000) < freq) {
584 if (tda18271_ir_measure[i].rfmax == 0)
588 dbg_map("ir measure, i = %d\n", i);
589 regs[R_EP5] &= ~0x07;
590 regs[R_EP5] |= tda18271_ir_measure[i].val;
592 /* calculate MAIN PLL */
596 while ((tda18271_main_pll[i].lomax * 1000) < N) {
597 if (tda18271_main_pll[i + 1].lomax == 0)
601 dbg_map("main pll, pd = 0x%x, d = 0x%x, i = %d\n",
602 tda18271_main_pll[i].pd, tda18271_main_pll[i].d, i);
604 regs[R_MPD] = (0x7f & tda18271_main_pll[i].pd);
605 switch (priv->mode) {
606 case TDA18271_ANALOG:
607 regs[R_MPD] &= ~0x08;
609 case TDA18271_DIGITAL:
614 div = ((tda18271_main_pll[i].d * (N / 1000)) << 7) / 125;
615 regs[R_MD1] = 0xff & (div >> 16);
616 regs[R_MD2] = 0xff & (div >> 8);
617 regs[R_MD3] = 0xff & div;
619 tda18271_write_regs(fe, R_TM, 15);
625 /* ------------------------------------------------------------------ */
627 static int tda18271_set_params(struct dvb_frontend *fe,
628 struct dvb_frontend_parameters *params)
630 struct tda18271_priv *priv = fe->tuner_priv;
634 u32 freq = params->frequency;
636 priv->mode = TDA18271_DIGITAL;
639 if (fe->ops.info.type == FE_ATSC) {
640 switch (params->u.vsb.modulation) {
643 std = 0x1b; /* device-specific (spec says 0x1c) */
648 std = 0x18; /* device-specific (spec says 0x1d) */
652 printk(KERN_WARNING "%s: modulation not set!\n",
656 freq += 1750000; /* Adjust to center (+1.75MHZ) */
658 } else if (fe->ops.info.type == FE_OFDM) {
659 switch (params->u.ofdm.bandwidth) {
660 case BANDWIDTH_6_MHZ:
661 std = 0x1b; /* device-specific (spec says 0x1c) */
665 case BANDWIDTH_7_MHZ:
666 std = 0x19; /* device-specific (spec says 0x1d) */
670 case BANDWIDTH_8_MHZ:
671 std = 0x1a; /* device-specific (spec says 0x1e) */
676 printk(KERN_WARNING "%s: bandwidth not set!\n",
681 printk(KERN_WARNING "%s: modulation type not supported!\n",
686 return tda18271_tune(fe, sgIF, freq, bw, std);
689 static int tda18271_set_analog_params(struct dvb_frontend *fe,
690 struct analog_parameters *params)
692 struct tda18271_priv *priv = fe->tuner_priv;
697 priv->mode = TDA18271_ANALOG;
700 if (params->std & V4L2_STD_MN) {
704 } else if (params->std & V4L2_STD_B) {
708 } else if (params->std & V4L2_STD_GH) {
712 } else if (params->std & V4L2_STD_PAL_I) {
716 } else if (params->std & V4L2_STD_DK) {
720 } else if (params->std & V4L2_STD_SECAM_L) {
724 } else if (params->std & V4L2_STD_SECAM_LC) {
734 if (params->mode == V4L2_TUNER_RADIO)
735 sgIF = 88; /* if frequency is 5.5 MHz */
737 dbg_info("setting tda18271 to system %s\n", mode);
739 return tda18271_tune(fe, sgIF * 62500, params->frequency * 62500,
743 static int tda18271_release(struct dvb_frontend *fe)
745 kfree(fe->tuner_priv);
746 fe->tuner_priv = NULL;
750 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
752 struct tda18271_priv *priv = fe->tuner_priv;
753 *frequency = priv->frequency;
757 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
759 struct tda18271_priv *priv = fe->tuner_priv;
760 *bandwidth = priv->bandwidth;
764 static struct dvb_tuner_ops tda18271_tuner_ops = {
766 .name = "NXP TDA18271HD",
767 .frequency_min = 45000000,
768 .frequency_max = 864000000,
769 .frequency_step = 62500
771 .init = tda18271_init,
772 .set_params = tda18271_set_params,
773 .set_analog_params = tda18271_set_analog_params,
774 .release = tda18271_release,
775 .get_frequency = tda18271_get_frequency,
776 .get_bandwidth = tda18271_get_bandwidth,
779 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
780 struct i2c_adapter *i2c)
782 struct tda18271_priv *priv = NULL;
784 dbg_info("@ %d-%04x\n", i2c_adapter_id(i2c), addr);
785 priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
789 priv->i2c_addr = addr;
790 priv->i2c_adap = i2c;
792 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
793 sizeof(struct dvb_tuner_ops));
795 fe->tuner_priv = priv;
797 tda18271_init_regs(fe);
801 EXPORT_SYMBOL_GPL(tda18271_attach);
802 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
803 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
804 MODULE_LICENSE("GPL");
807 * Overrides for Emacs so that we follow Linus's tabbing style.
808 * ---------------------------------------------------------------------------