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>
25 #include "tda18271-priv.h"
28 module_param_named(debug, tda18271_debug, int, 0644);
29 MODULE_PARM_DESC(debug, "set debug level (info=1, map=2, reg=4 (or-able))");
31 /*---------------------------------------------------------------------*/
38 struct tda18271_priv {
40 struct i2c_adapter *i2c_adap;
41 unsigned char tda18271_regs[TDA18271_NUM_REGS];
43 enum tda18271_mode mode;
44 enum tda18271_i2c_gate gate;
50 static int tda18271_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
52 struct tda18271_priv *priv = fe->tuner_priv;
53 enum tda18271_i2c_gate gate;
57 case TDA18271_GATE_DIGITAL:
58 case TDA18271_GATE_ANALOG:
61 case TDA18271_GATE_AUTO:
64 case TDA18271_DIGITAL:
65 gate = TDA18271_GATE_DIGITAL;
69 gate = TDA18271_GATE_ANALOG;
75 case TDA18271_GATE_ANALOG:
76 if (fe->ops.analog_ops.i2c_gate_ctrl)
77 ret = fe->ops.analog_ops.i2c_gate_ctrl(fe, enable);
79 case TDA18271_GATE_DIGITAL:
80 if (fe->ops.i2c_gate_ctrl)
81 ret = fe->ops.i2c_gate_ctrl(fe, enable);
91 /*---------------------------------------------------------------------*/
93 static void tda18271_dump_regs(struct dvb_frontend *fe)
95 struct tda18271_priv *priv = fe->tuner_priv;
96 unsigned char *regs = priv->tda18271_regs;
98 dbg_reg("=== TDA18271 REG DUMP ===\n");
99 dbg_reg("ID_BYTE = 0x%02x\n", 0xff & regs[R_ID]);
100 dbg_reg("THERMO_BYTE = 0x%02x\n", 0xff & regs[R_TM]);
101 dbg_reg("POWER_LEVEL_BYTE = 0x%02x\n", 0xff & regs[R_PL]);
102 dbg_reg("EASY_PROG_BYTE_1 = 0x%02x\n", 0xff & regs[R_EP1]);
103 dbg_reg("EASY_PROG_BYTE_2 = 0x%02x\n", 0xff & regs[R_EP2]);
104 dbg_reg("EASY_PROG_BYTE_3 = 0x%02x\n", 0xff & regs[R_EP3]);
105 dbg_reg("EASY_PROG_BYTE_4 = 0x%02x\n", 0xff & regs[R_EP4]);
106 dbg_reg("EASY_PROG_BYTE_5 = 0x%02x\n", 0xff & regs[R_EP5]);
107 dbg_reg("CAL_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_CPD]);
108 dbg_reg("CAL_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_CD1]);
109 dbg_reg("CAL_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_CD2]);
110 dbg_reg("CAL_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_CD3]);
111 dbg_reg("MAIN_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_MPD]);
112 dbg_reg("MAIN_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_MD1]);
113 dbg_reg("MAIN_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_MD2]);
114 dbg_reg("MAIN_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_MD3]);
117 static void tda18271_read_regs(struct dvb_frontend *fe)
119 struct tda18271_priv *priv = fe->tuner_priv;
120 unsigned char *regs = priv->tda18271_regs;
121 unsigned char buf = 0x00;
123 struct i2c_msg msg[] = {
124 { .addr = priv->i2c_addr, .flags = 0,
125 .buf = &buf, .len = 1 },
126 { .addr = priv->i2c_addr, .flags = I2C_M_RD,
127 .buf = regs, .len = 16 }
130 tda18271_i2c_gate_ctrl(fe, 1);
132 /* read all registers */
133 ret = i2c_transfer(priv->i2c_adap, msg, 2);
135 tda18271_i2c_gate_ctrl(fe, 0);
138 printk("ERROR: %s: i2c_transfer returned: %d\n",
141 if (tda18271_debug & DBG_REG)
142 tda18271_dump_regs(fe);
145 static void tda18271_write_regs(struct dvb_frontend *fe, int idx, int len)
147 struct tda18271_priv *priv = fe->tuner_priv;
148 unsigned char *regs = priv->tda18271_regs;
149 unsigned char buf[TDA18271_NUM_REGS+1];
150 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
151 .buf = buf, .len = len+1 };
154 BUG_ON((len == 0) || (idx+len > sizeof(buf)));
157 for (i = 1; i <= len; i++) {
158 buf[i] = regs[idx-1+i];
161 tda18271_i2c_gate_ctrl(fe, 1);
163 /* write registers */
164 ret = i2c_transfer(priv->i2c_adap, &msg, 1);
166 tda18271_i2c_gate_ctrl(fe, 0);
169 printk(KERN_WARNING "ERROR: %s: i2c_transfer returned: %d\n",
173 /*---------------------------------------------------------------------*/
175 static int tda18271_init_regs(struct dvb_frontend *fe)
177 struct tda18271_priv *priv = fe->tuner_priv;
178 unsigned char *regs = priv->tda18271_regs;
180 printk(KERN_INFO "tda18271: initializing registers\n");
182 /* initialize registers */
223 tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS);
224 /* setup AGC1 & AGC2 */
226 tda18271_write_regs(fe, R_EB17, 1);
228 tda18271_write_regs(fe, R_EB17, 1);
230 tda18271_write_regs(fe, R_EB17, 1);
232 tda18271_write_regs(fe, R_EB17, 1);
235 tda18271_write_regs(fe, R_EB20, 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 /* image rejection calibration */
258 tda18271_write_regs(fe, R_EP3, 11);
259 msleep(5); /* pll locking */
262 tda18271_write_regs(fe, R_EP1, 1);
263 msleep(5); /* wanted low measurement */
273 tda18271_write_regs(fe, R_EP3, 7);
274 msleep(5); /* pll locking */
277 tda18271_write_regs(fe, R_EP2, 1);
278 msleep(30); /* image low optimization completion */
293 tda18271_write_regs(fe, R_EP3, 11);
294 msleep(5); /* pll locking */
297 tda18271_write_regs(fe, R_EP1, 1);
298 msleep(5); /* wanted mid measurement */
308 tda18271_write_regs(fe, R_EP3, 7);
309 msleep(5); /* pll locking */
312 tda18271_write_regs(fe, R_EP2, 1);
313 msleep(30); /* image mid optimization completion */
328 tda18271_write_regs(fe, R_EP3, 11);
329 msleep(5); /* pll locking */
332 tda18271_write_regs(fe, R_EP1, 1);
333 msleep(5); /* wanted high measurement */
343 tda18271_write_regs(fe, R_EP3, 7);
344 msleep(5); /* pll locking */
348 tda18271_write_regs(fe, R_EP2, 1);
349 msleep(30); /* image high optimization completion */
352 tda18271_write_regs(fe, R_EP4, 1);
355 tda18271_write_regs(fe, R_EP1, 1);
360 static int tda18271_init(struct dvb_frontend *fe)
362 struct tda18271_priv *priv = fe->tuner_priv;
363 unsigned char *regs = priv->tda18271_regs;
365 tda18271_read_regs(fe);
367 /* test IR_CAL_OK to see if we need init */
368 if ((regs[R_EP1] & 0x08) == 0)
369 tda18271_init_regs(fe);
374 static int tda18271_tune(struct dvb_frontend *fe,
375 u32 ifc, u32 freq, u32 bw, u8 std)
377 struct tda18271_priv *priv = fe->tuner_priv;
378 unsigned char *regs = priv->tda18271_regs;
384 dbg_info("freq = %d, ifc = %d\n", freq, ifc);
386 /* RF tracking filter calibration */
388 /* calculate BP_Filter */
389 tda18271_calc_bp_filter(&freq, &val);
391 regs[R_EP1] &= ~0x07; /* clear bp filter bits */
393 tda18271_write_regs(fe, R_EP1, 1);
397 tda18271_write_regs(fe, R_EB4, 1);
400 tda18271_write_regs(fe, R_EB7, 1);
403 tda18271_write_regs(fe, R_EB14, 1);
406 tda18271_write_regs(fe, R_EB20, 1);
408 /* set CAL mode to RF tracking filter calibration */
411 /* calculate CAL PLL */
413 switch (priv->mode) {
414 case TDA18271_ANALOG:
417 case TDA18271_DIGITAL:
422 tda18271_calc_cal_pll(&N, &pd, &d);
426 div = ((d * (N / 1000)) << 7) / 125;
427 regs[R_CD1] = 0xff & (div >> 16);
428 regs[R_CD2] = 0xff & (div >> 8);
429 regs[R_CD3] = 0xff & div;
431 /* calculate MAIN PLL */
433 switch (priv->mode) {
434 case TDA18271_ANALOG:
437 case TDA18271_DIGITAL:
438 N = freq + bw / 2 + 1000000;
442 tda18271_calc_main_pll(&N, &pd, &d);
444 regs[R_MPD] = (0x7f & pd);
446 switch (priv->mode) {
447 case TDA18271_ANALOG:
448 regs[R_MPD] &= ~0x08;
450 case TDA18271_DIGITAL:
455 div = ((d * (N / 1000)) << 7) / 125;
456 regs[R_MD1] = 0xff & (div >> 16);
457 regs[R_MD2] = 0xff & (div >> 8);
458 regs[R_MD3] = 0xff & div;
460 tda18271_write_regs(fe, R_EP3, 11);
461 msleep(5); /* RF tracking filter calibration initialization */
463 /* search for K,M,CO for RF Calibration */
464 tda18271_calc_km(&freq, &val);
466 regs[R_EB13] &= 0x83;
468 tda18271_write_regs(fe, R_EB13, 1);
470 /* search for RF_BAND */
471 tda18271_calc_rf_band(&freq, &val);
473 regs[R_EP2] &= ~0xe0; /* clear rf band bits */
474 regs[R_EP2] |= (val << 5);
476 /* search for Gain_Taper */
477 tda18271_calc_gain_taper(&freq, &val);
479 regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
482 tda18271_write_regs(fe, R_EP2, 1);
483 tda18271_write_regs(fe, R_EP1, 1);
484 tda18271_write_regs(fe, R_EP2, 1);
485 tda18271_write_regs(fe, R_EP1, 1);
489 tda18271_write_regs(fe, R_EB4, 1);
492 tda18271_write_regs(fe, R_EB7, 1);
496 tda18271_write_regs(fe, R_EB20, 1);
497 msleep(60); /* RF tracking filter calibration completion */
499 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
500 tda18271_write_regs(fe, R_EP4, 1);
502 tda18271_write_regs(fe, R_EP1, 1);
504 /* RF tracking filer correction for VHF_Low band */
505 tda18271_calc_rf_cal(&freq, &val);
507 /* VHF_Low band only */
510 tda18271_write_regs(fe, R_EB14, 1);
513 /* Channel Configuration */
515 switch (priv->mode) {
516 case TDA18271_ANALOG:
519 case TDA18271_DIGITAL:
523 tda18271_write_regs(fe, R_EB22, 1);
525 regs[R_EP1] |= 0x40; /* set dis power level on */
528 regs[R_EP3] &= ~0x1f; /* clear std bits */
533 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
535 regs[R_EP4] &= ~0x1c; /* clear if level bits */
536 switch (priv->mode) {
537 case TDA18271_ANALOG:
538 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
540 case TDA18271_DIGITAL:
546 regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */
548 /* image rejection validity EP5[2:0] */
549 tda18271_calc_ir_measure(&freq, &val);
551 regs[R_EP5] &= ~0x07;
554 /* calculate MAIN PLL */
557 tda18271_calc_main_pll(&N, &pd, &d);
559 regs[R_MPD] = (0x7f & pd);
560 switch (priv->mode) {
561 case TDA18271_ANALOG:
562 regs[R_MPD] &= ~0x08;
564 case TDA18271_DIGITAL:
569 div = ((d * (N / 1000)) << 7) / 125;
570 regs[R_MD1] = 0xff & (div >> 16);
571 regs[R_MD2] = 0xff & (div >> 8);
572 regs[R_MD3] = 0xff & div;
574 tda18271_write_regs(fe, R_TM, 15);
580 /* ------------------------------------------------------------------ */
582 static int tda18271_set_params(struct dvb_frontend *fe,
583 struct dvb_frontend_parameters *params)
585 struct tda18271_priv *priv = fe->tuner_priv;
589 u32 freq = params->frequency;
591 priv->mode = TDA18271_DIGITAL;
594 if (fe->ops.info.type == FE_ATSC) {
595 switch (params->u.vsb.modulation) {
598 std = 0x1b; /* device-specific (spec says 0x1c) */
603 std = 0x18; /* device-specific (spec says 0x1d) */
607 printk(KERN_WARNING "%s: modulation not set!\n",
612 /* userspace request is already center adjusted */
613 freq += 1750000; /* Adjust to center (+1.75MHZ) */
616 } else if (fe->ops.info.type == FE_OFDM) {
617 switch (params->u.ofdm.bandwidth) {
618 case BANDWIDTH_6_MHZ:
619 std = 0x1b; /* device-specific (spec says 0x1c) */
623 case BANDWIDTH_7_MHZ:
624 std = 0x19; /* device-specific (spec says 0x1d) */
628 case BANDWIDTH_8_MHZ:
629 std = 0x1a; /* device-specific (spec says 0x1e) */
634 printk(KERN_WARNING "%s: bandwidth not set!\n",
639 printk(KERN_WARNING "%s: modulation type not supported!\n",
644 return tda18271_tune(fe, sgIF, freq, bw, std);
647 static int tda18271_set_analog_params(struct dvb_frontend *fe,
648 struct analog_parameters *params)
650 struct tda18271_priv *priv = fe->tuner_priv;
655 priv->mode = TDA18271_ANALOG;
658 if (params->std & V4L2_STD_MN) {
662 } else if (params->std & V4L2_STD_B) {
666 } else if (params->std & V4L2_STD_GH) {
670 } else if (params->std & V4L2_STD_PAL_I) {
674 } else if (params->std & V4L2_STD_DK) {
678 } else if (params->std & V4L2_STD_SECAM_L) {
682 } else if (params->std & V4L2_STD_SECAM_LC) {
692 if (params->mode == V4L2_TUNER_RADIO)
693 sgIF = 88; /* if frequency is 5.5 MHz */
695 dbg_info("setting tda18271 to system %s\n", mode);
697 return tda18271_tune(fe, sgIF * 62500, params->frequency * 62500,
701 static int tda18271_release(struct dvb_frontend *fe)
703 kfree(fe->tuner_priv);
704 fe->tuner_priv = NULL;
708 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
710 struct tda18271_priv *priv = fe->tuner_priv;
711 *frequency = priv->frequency;
715 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
717 struct tda18271_priv *priv = fe->tuner_priv;
718 *bandwidth = priv->bandwidth;
722 static struct dvb_tuner_ops tda18271_tuner_ops = {
724 .name = "NXP TDA18271HD",
725 .frequency_min = 45000000,
726 .frequency_max = 864000000,
727 .frequency_step = 62500
729 .init = tda18271_init,
730 .set_params = tda18271_set_params,
731 .set_analog_params = tda18271_set_analog_params,
732 .release = tda18271_release,
733 .get_frequency = tda18271_get_frequency,
734 .get_bandwidth = tda18271_get_bandwidth,
737 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
738 struct i2c_adapter *i2c,
739 enum tda18271_i2c_gate gate)
741 struct tda18271_priv *priv = NULL;
743 dbg_info("@ %d-%04x\n", i2c_adapter_id(i2c), addr);
744 priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
748 priv->i2c_addr = addr;
749 priv->i2c_adap = i2c;
752 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
753 sizeof(struct dvb_tuner_ops));
755 fe->tuner_priv = priv;
757 tda18271_init_regs(fe);
761 EXPORT_SYMBOL_GPL(tda18271_attach);
762 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
763 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
764 MODULE_LICENSE("GPL");
767 * Overrides for Emacs so that we follow Linus's tabbing style.
768 * ---------------------------------------------------------------------------