Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1 | /* |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 2 | tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 3 | |
| 4 | Copyright (C) 2007 Michael Krufky (mkrufky@linuxtv.org) |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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. |
| 19 | */ |
| 20 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 21 | #include <linux/delay.h> |
| 22 | #include <linux/videodev2.h> |
Michael Krufky | 7d11c53 | 2007-10-24 09:55:54 -0300 | [diff] [blame] | 23 | #include "tuner-driver.h" |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 24 | |
| 25 | #include "tda18271.h" |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 26 | #include "tda18271-priv.h" |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 27 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 28 | int tda18271_debug; |
Michael Krufky | 54465b0 | 2007-11-23 18:14:53 -0300 | [diff] [blame] | 29 | module_param_named(debug, tda18271_debug, int, 0644); |
Michael Krufky | 293da0e | 2007-12-02 02:45:04 -0300 | [diff] [blame] | 30 | MODULE_PARM_DESC(debug, "set debug level (info=1, map=2, reg=4 (or-able))"); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 31 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 32 | /*---------------------------------------------------------------------*/ |
| 33 | |
Michael Krufky | 7206abb | 2007-12-09 22:13:01 -0300 | [diff] [blame^] | 34 | enum tda18271_mode { |
| 35 | TDA18271_ANALOG, |
| 36 | TDA18271_DIGITAL, |
| 37 | }; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 38 | |
| 39 | struct tda18271_priv { |
| 40 | u8 i2c_addr; |
| 41 | struct i2c_adapter *i2c_adap; |
| 42 | unsigned char tda18271_regs[TDA18271_NUM_REGS]; |
Michael Krufky | 7206abb | 2007-12-09 22:13:01 -0300 | [diff] [blame^] | 43 | enum tda18271_mode mode; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 44 | |
| 45 | u32 frequency; |
| 46 | u32 bandwidth; |
| 47 | }; |
| 48 | |
Michael Krufky | 7d11c53 | 2007-10-24 09:55:54 -0300 | [diff] [blame] | 49 | static int tda18271_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
| 50 | { |
| 51 | struct tda18271_priv *priv = fe->tuner_priv; |
| 52 | struct analog_tuner_ops *ops = fe->ops.analog_demod_ops; |
| 53 | int ret = 0; |
| 54 | |
| 55 | switch (priv->mode) { |
| 56 | case TDA18271_ANALOG: |
| 57 | if (ops && ops->i2c_gate_ctrl) |
| 58 | ret = ops->i2c_gate_ctrl(fe, enable); |
| 59 | break; |
| 60 | case TDA18271_DIGITAL: |
| 61 | if (fe->ops.i2c_gate_ctrl) |
| 62 | ret = fe->ops.i2c_gate_ctrl(fe, enable); |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | return ret; |
| 67 | }; |
| 68 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 69 | /*---------------------------------------------------------------------*/ |
| 70 | |
| 71 | static void tda18271_dump_regs(struct dvb_frontend *fe) |
| 72 | { |
| 73 | struct tda18271_priv *priv = fe->tuner_priv; |
| 74 | unsigned char *regs = priv->tda18271_regs; |
| 75 | |
Michael Krufky | 293da0e | 2007-12-02 02:45:04 -0300 | [diff] [blame] | 76 | dbg_reg("=== TDA18271 REG DUMP ===\n"); |
Michael Krufky | 5d2bf93 | 2007-12-02 17:37:38 -0300 | [diff] [blame] | 77 | dbg_reg("ID_BYTE = 0x%02x\n", 0xff & regs[R_ID]); |
| 78 | dbg_reg("THERMO_BYTE = 0x%02x\n", 0xff & regs[R_TM]); |
| 79 | dbg_reg("POWER_LEVEL_BYTE = 0x%02x\n", 0xff & regs[R_PL]); |
| 80 | dbg_reg("EASY_PROG_BYTE_1 = 0x%02x\n", 0xff & regs[R_EP1]); |
| 81 | dbg_reg("EASY_PROG_BYTE_2 = 0x%02x\n", 0xff & regs[R_EP2]); |
| 82 | dbg_reg("EASY_PROG_BYTE_3 = 0x%02x\n", 0xff & regs[R_EP3]); |
| 83 | dbg_reg("EASY_PROG_BYTE_4 = 0x%02x\n", 0xff & regs[R_EP4]); |
| 84 | dbg_reg("EASY_PROG_BYTE_5 = 0x%02x\n", 0xff & regs[R_EP5]); |
| 85 | dbg_reg("CAL_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_CPD]); |
| 86 | dbg_reg("CAL_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_CD1]); |
| 87 | dbg_reg("CAL_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_CD2]); |
| 88 | dbg_reg("CAL_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_CD3]); |
| 89 | dbg_reg("MAIN_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_MPD]); |
| 90 | dbg_reg("MAIN_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_MD1]); |
| 91 | dbg_reg("MAIN_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_MD2]); |
| 92 | dbg_reg("MAIN_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_MD3]); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void tda18271_read_regs(struct dvb_frontend *fe) |
| 96 | { |
| 97 | struct tda18271_priv *priv = fe->tuner_priv; |
| 98 | unsigned char *regs = priv->tda18271_regs; |
| 99 | unsigned char buf = 0x00; |
| 100 | int ret; |
| 101 | struct i2c_msg msg[] = { |
| 102 | { .addr = priv->i2c_addr, .flags = 0, |
| 103 | .buf = &buf, .len = 1 }, |
| 104 | { .addr = priv->i2c_addr, .flags = I2C_M_RD, |
| 105 | .buf = regs, .len = 16 } |
| 106 | }; |
| 107 | |
Michael Krufky | 7d11c53 | 2007-10-24 09:55:54 -0300 | [diff] [blame] | 108 | tda18271_i2c_gate_ctrl(fe, 1); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 109 | |
| 110 | /* read all registers */ |
| 111 | ret = i2c_transfer(priv->i2c_adap, msg, 2); |
| 112 | |
Michael Krufky | 7d11c53 | 2007-10-24 09:55:54 -0300 | [diff] [blame] | 113 | tda18271_i2c_gate_ctrl(fe, 0); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 114 | |
| 115 | if (ret != 2) |
| 116 | printk("ERROR: %s: i2c_transfer returned: %d\n", |
| 117 | __FUNCTION__, ret); |
| 118 | |
Michael Krufky | 293da0e | 2007-12-02 02:45:04 -0300 | [diff] [blame] | 119 | if (tda18271_debug & DBG_REG) |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 120 | tda18271_dump_regs(fe); |
| 121 | } |
| 122 | |
| 123 | static void tda18271_write_regs(struct dvb_frontend *fe, int idx, int len) |
| 124 | { |
| 125 | struct tda18271_priv *priv = fe->tuner_priv; |
| 126 | unsigned char *regs = priv->tda18271_regs; |
| 127 | unsigned char buf[TDA18271_NUM_REGS+1]; |
| 128 | struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0, |
| 129 | .buf = buf, .len = len+1 }; |
| 130 | int i, ret; |
| 131 | |
| 132 | BUG_ON((len == 0) || (idx+len > sizeof(buf))); |
| 133 | |
| 134 | buf[0] = idx; |
| 135 | for (i = 1; i <= len; i++) { |
| 136 | buf[i] = regs[idx-1+i]; |
| 137 | } |
| 138 | |
Michael Krufky | 7d11c53 | 2007-10-24 09:55:54 -0300 | [diff] [blame] | 139 | tda18271_i2c_gate_ctrl(fe, 1); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 140 | |
| 141 | /* write registers */ |
| 142 | ret = i2c_transfer(priv->i2c_adap, &msg, 1); |
| 143 | |
Michael Krufky | 7d11c53 | 2007-10-24 09:55:54 -0300 | [diff] [blame] | 144 | tda18271_i2c_gate_ctrl(fe, 0); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 145 | |
| 146 | if (ret != 1) |
| 147 | printk(KERN_WARNING "ERROR: %s: i2c_transfer returned: %d\n", |
| 148 | __FUNCTION__, ret); |
| 149 | } |
| 150 | |
| 151 | /*---------------------------------------------------------------------*/ |
| 152 | |
Michael Krufky | 22ee125 | 2007-11-22 17:13:00 -0300 | [diff] [blame] | 153 | static int tda18271_init_regs(struct dvb_frontend *fe) |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 154 | { |
| 155 | struct tda18271_priv *priv = fe->tuner_priv; |
| 156 | unsigned char *regs = priv->tda18271_regs; |
| 157 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 158 | printk(KERN_INFO "tda18271: initializing registers\n"); |
| 159 | |
| 160 | /* initialize registers */ |
| 161 | regs[R_ID] = 0x83; |
| 162 | regs[R_TM] = 0x08; |
| 163 | regs[R_PL] = 0x80; |
| 164 | regs[R_EP1] = 0xc6; |
| 165 | regs[R_EP2] = 0xdf; |
| 166 | regs[R_EP3] = 0x16; |
| 167 | regs[R_EP4] = 0x60; |
| 168 | regs[R_EP5] = 0x80; |
| 169 | regs[R_CPD] = 0x80; |
| 170 | regs[R_CD1] = 0x00; |
| 171 | regs[R_CD2] = 0x00; |
| 172 | regs[R_CD3] = 0x00; |
| 173 | regs[R_MPD] = 0x00; |
| 174 | regs[R_MD1] = 0x00; |
| 175 | regs[R_MD2] = 0x00; |
| 176 | regs[R_MD3] = 0x00; |
| 177 | regs[R_EB1] = 0xff; |
| 178 | regs[R_EB2] = 0x01; |
| 179 | regs[R_EB3] = 0x84; |
| 180 | regs[R_EB4] = 0x41; |
| 181 | regs[R_EB5] = 0x01; |
| 182 | regs[R_EB6] = 0x84; |
| 183 | regs[R_EB7] = 0x40; |
| 184 | regs[R_EB8] = 0x07; |
| 185 | regs[R_EB9] = 0x00; |
| 186 | regs[R_EB10] = 0x00; |
| 187 | regs[R_EB11] = 0x96; |
| 188 | regs[R_EB12] = 0x0f; |
| 189 | regs[R_EB13] = 0xc1; |
| 190 | regs[R_EB14] = 0x00; |
| 191 | regs[R_EB15] = 0x8f; |
| 192 | regs[R_EB16] = 0x00; |
| 193 | regs[R_EB17] = 0x00; |
| 194 | regs[R_EB18] = 0x00; |
| 195 | regs[R_EB19] = 0x00; |
| 196 | regs[R_EB20] = 0x20; |
| 197 | regs[R_EB21] = 0x33; |
| 198 | regs[R_EB22] = 0x48; |
| 199 | regs[R_EB23] = 0xb0; |
| 200 | |
| 201 | tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS); |
| 202 | /* setup AGC1 & AGC2 */ |
| 203 | regs[R_EB17] = 0x00; |
| 204 | tda18271_write_regs(fe, R_EB17, 1); |
| 205 | regs[R_EB17] = 0x03; |
| 206 | tda18271_write_regs(fe, R_EB17, 1); |
| 207 | regs[R_EB17] = 0x43; |
| 208 | tda18271_write_regs(fe, R_EB17, 1); |
| 209 | regs[R_EB17] = 0x4c; |
| 210 | tda18271_write_regs(fe, R_EB17, 1); |
| 211 | |
| 212 | regs[R_EB20] = 0xa0; |
| 213 | tda18271_write_regs(fe, R_EB20, 1); |
| 214 | regs[R_EB20] = 0xa7; |
| 215 | tda18271_write_regs(fe, R_EB20, 1); |
| 216 | regs[R_EB20] = 0xe7; |
| 217 | tda18271_write_regs(fe, R_EB20, 1); |
| 218 | regs[R_EB20] = 0xec; |
| 219 | tda18271_write_regs(fe, R_EB20, 1); |
| 220 | |
| 221 | /* image rejection calibration */ |
| 222 | |
| 223 | /* low-band */ |
| 224 | regs[R_EP3] = 0x1f; |
| 225 | regs[R_EP4] = 0x66; |
| 226 | regs[R_EP5] = 0x81; |
| 227 | regs[R_CPD] = 0xcc; |
| 228 | regs[R_CD1] = 0x6c; |
| 229 | regs[R_CD2] = 0x00; |
| 230 | regs[R_CD3] = 0x00; |
| 231 | regs[R_MPD] = 0xcd; |
| 232 | regs[R_MD1] = 0x77; |
| 233 | regs[R_MD2] = 0x08; |
| 234 | regs[R_MD3] = 0x00; |
| 235 | |
| 236 | tda18271_write_regs(fe, R_EP3, 11); |
| 237 | msleep(5); /* pll locking */ |
| 238 | |
| 239 | regs[R_EP1] = 0xc6; |
| 240 | tda18271_write_regs(fe, R_EP1, 1); |
| 241 | msleep(5); /* wanted low measurement */ |
| 242 | |
| 243 | regs[R_EP3] = 0x1f; |
| 244 | regs[R_EP4] = 0x66; |
| 245 | regs[R_EP5] = 0x85; |
| 246 | regs[R_CPD] = 0xcb; |
| 247 | regs[R_CD1] = 0x66; |
| 248 | regs[R_CD2] = 0x70; |
| 249 | regs[R_CD3] = 0x00; |
| 250 | |
| 251 | tda18271_write_regs(fe, R_EP3, 7); |
| 252 | msleep(5); /* pll locking */ |
| 253 | |
| 254 | regs[R_EP2] = 0xdf; |
| 255 | tda18271_write_regs(fe, R_EP2, 1); |
| 256 | msleep(30); /* image low optimization completion */ |
| 257 | |
| 258 | /* mid-band */ |
| 259 | regs[R_EP3] = 0x1f; |
| 260 | regs[R_EP4] = 0x66; |
| 261 | regs[R_EP5] = 0x82; |
| 262 | regs[R_CPD] = 0xa8; |
| 263 | regs[R_CD1] = 0x66; |
| 264 | regs[R_CD2] = 0x00; |
| 265 | regs[R_CD3] = 0x00; |
| 266 | regs[R_MPD] = 0xa9; |
| 267 | regs[R_MD1] = 0x73; |
| 268 | regs[R_MD2] = 0x1a; |
| 269 | regs[R_MD3] = 0x00; |
| 270 | |
| 271 | tda18271_write_regs(fe, R_EP3, 11); |
| 272 | msleep(5); /* pll locking */ |
| 273 | |
| 274 | regs[R_EP1] = 0xc6; |
| 275 | tda18271_write_regs(fe, R_EP1, 1); |
| 276 | msleep(5); /* wanted mid measurement */ |
| 277 | |
| 278 | regs[R_EP3] = 0x1f; |
| 279 | regs[R_EP4] = 0x66; |
| 280 | regs[R_EP5] = 0x86; |
| 281 | regs[R_CPD] = 0xa8; |
| 282 | regs[R_CD1] = 0x66; |
| 283 | regs[R_CD2] = 0xa0; |
| 284 | regs[R_CD3] = 0x00; |
| 285 | |
| 286 | tda18271_write_regs(fe, R_EP3, 7); |
| 287 | msleep(5); /* pll locking */ |
| 288 | |
| 289 | regs[R_EP2] = 0xdf; |
| 290 | tda18271_write_regs(fe, R_EP2, 1); |
| 291 | msleep(30); /* image mid optimization completion */ |
| 292 | |
| 293 | /* high-band */ |
| 294 | regs[R_EP3] = 0x1f; |
| 295 | regs[R_EP4] = 0x66; |
| 296 | regs[R_EP5] = 0x83; |
| 297 | regs[R_CPD] = 0x98; |
| 298 | regs[R_CD1] = 0x65; |
| 299 | regs[R_CD2] = 0x00; |
| 300 | regs[R_CD3] = 0x00; |
| 301 | regs[R_MPD] = 0x99; |
| 302 | regs[R_MD1] = 0x71; |
| 303 | regs[R_MD2] = 0xcd; |
| 304 | regs[R_MD3] = 0x00; |
| 305 | |
| 306 | tda18271_write_regs(fe, R_EP3, 11); |
| 307 | msleep(5); /* pll locking */ |
| 308 | |
| 309 | regs[R_EP1] = 0xc6; |
| 310 | tda18271_write_regs(fe, R_EP1, 1); |
| 311 | msleep(5); /* wanted high measurement */ |
| 312 | |
| 313 | regs[R_EP3] = 0x1f; |
| 314 | regs[R_EP4] = 0x66; |
| 315 | regs[R_EP5] = 0x87; |
| 316 | regs[R_CPD] = 0x98; |
| 317 | regs[R_CD1] = 0x65; |
| 318 | regs[R_CD2] = 0x50; |
| 319 | regs[R_CD3] = 0x00; |
| 320 | |
| 321 | tda18271_write_regs(fe, R_EP3, 7); |
| 322 | msleep(5); /* pll locking */ |
| 323 | |
| 324 | regs[R_EP2] = 0xdf; |
| 325 | |
| 326 | tda18271_write_regs(fe, R_EP2, 1); |
| 327 | msleep(30); /* image high optimization completion */ |
| 328 | |
| 329 | regs[R_EP4] = 0x64; |
| 330 | tda18271_write_regs(fe, R_EP4, 1); |
| 331 | |
| 332 | regs[R_EP1] = 0xc6; |
| 333 | tda18271_write_regs(fe, R_EP1, 1); |
Michael Krufky | 22ee125 | 2007-11-22 17:13:00 -0300 | [diff] [blame] | 334 | |
| 335 | return 0; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 336 | } |
| 337 | |
Michael Krufky | efce841 | 2007-12-01 17:40:16 -0300 | [diff] [blame] | 338 | static int tda18271_init(struct dvb_frontend *fe) |
| 339 | { |
| 340 | struct tda18271_priv *priv = fe->tuner_priv; |
| 341 | unsigned char *regs = priv->tda18271_regs; |
| 342 | |
| 343 | tda18271_read_regs(fe); |
| 344 | |
| 345 | /* test IR_CAL_OK to see if we need init */ |
| 346 | if ((regs[R_EP1] & 0x08) == 0) |
| 347 | tda18271_init_regs(fe); |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 352 | static int tda18271_tune(struct dvb_frontend *fe, |
| 353 | u32 ifc, u32 freq, u32 bw, u8 std) |
| 354 | { |
| 355 | struct tda18271_priv *priv = fe->tuner_priv; |
| 356 | unsigned char *regs = priv->tda18271_regs; |
| 357 | u32 div, N = 0; |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 358 | u8 d, pd, val; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 359 | |
Michael Krufky | 1457263 | 2007-12-02 02:32:49 -0300 | [diff] [blame] | 360 | tda18271_init(fe); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 361 | |
Michael Krufky | 293da0e | 2007-12-02 02:45:04 -0300 | [diff] [blame] | 362 | dbg_info("freq = %d, ifc = %d\n", freq, ifc); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 363 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 364 | /* RF tracking filter calibration */ |
| 365 | |
| 366 | /* calculate BP_Filter */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 367 | tda18271_calc_bp_filter(&freq, &val); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 368 | |
| 369 | regs[R_EP1] &= ~0x07; /* clear bp filter bits */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 370 | regs[R_EP1] |= val; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 371 | tda18271_write_regs(fe, R_EP1, 1); |
| 372 | |
| 373 | regs[R_EB4] &= 0x07; |
| 374 | regs[R_EB4] |= 0x60; |
| 375 | tda18271_write_regs(fe, R_EB4, 1); |
| 376 | |
| 377 | regs[R_EB7] = 0x60; |
| 378 | tda18271_write_regs(fe, R_EB7, 1); |
| 379 | |
| 380 | regs[R_EB14] = 0x00; |
| 381 | tda18271_write_regs(fe, R_EB14, 1); |
| 382 | |
| 383 | regs[R_EB20] = 0xcc; |
| 384 | tda18271_write_regs(fe, R_EB20, 1); |
| 385 | |
| 386 | /* set CAL mode to RF tracking filter calibration */ |
| 387 | regs[R_EB4] |= 0x03; |
| 388 | |
| 389 | /* calculate CAL PLL */ |
| 390 | |
| 391 | switch (priv->mode) { |
| 392 | case TDA18271_ANALOG: |
| 393 | N = freq - 1250000; |
| 394 | break; |
| 395 | case TDA18271_DIGITAL: |
| 396 | N = freq + bw / 2; |
| 397 | break; |
| 398 | } |
| 399 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 400 | tda18271_calc_cal_pll(&N, &pd, &d); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 401 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 402 | regs[R_CPD] = pd; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 403 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 404 | div = ((d * (N / 1000)) << 7) / 125; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 405 | regs[R_CD1] = 0xff & (div >> 16); |
| 406 | regs[R_CD2] = 0xff & (div >> 8); |
| 407 | regs[R_CD3] = 0xff & div; |
| 408 | |
| 409 | /* calculate MAIN PLL */ |
| 410 | |
| 411 | switch (priv->mode) { |
| 412 | case TDA18271_ANALOG: |
| 413 | N = freq - 250000; |
| 414 | break; |
| 415 | case TDA18271_DIGITAL: |
| 416 | N = freq + bw / 2 + 1000000; |
| 417 | break; |
| 418 | } |
| 419 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 420 | tda18271_calc_main_pll(&N, &pd, &d); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 421 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 422 | regs[R_MPD] = (0x7f & pd); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 423 | |
| 424 | switch (priv->mode) { |
| 425 | case TDA18271_ANALOG: |
| 426 | regs[R_MPD] &= ~0x08; |
| 427 | break; |
| 428 | case TDA18271_DIGITAL: |
| 429 | regs[R_MPD] |= 0x08; |
| 430 | break; |
| 431 | } |
| 432 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 433 | div = ((d * (N / 1000)) << 7) / 125; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 434 | regs[R_MD1] = 0xff & (div >> 16); |
| 435 | regs[R_MD2] = 0xff & (div >> 8); |
| 436 | regs[R_MD3] = 0xff & div; |
| 437 | |
| 438 | tda18271_write_regs(fe, R_EP3, 11); |
| 439 | msleep(5); /* RF tracking filter calibration initialization */ |
| 440 | |
| 441 | /* search for K,M,CO for RF Calibration */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 442 | tda18271_calc_km(&freq, &val); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 443 | |
| 444 | regs[R_EB13] &= 0x83; |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 445 | regs[R_EB13] |= val; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 446 | tda18271_write_regs(fe, R_EB13, 1); |
| 447 | |
| 448 | /* search for RF_BAND */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 449 | tda18271_calc_rf_band(&freq, &val); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 450 | |
| 451 | regs[R_EP2] &= ~0xe0; /* clear rf band bits */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 452 | regs[R_EP2] |= (val << 5); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 453 | |
| 454 | /* search for Gain_Taper */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 455 | tda18271_calc_gain_taper(&freq, &val); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 456 | |
| 457 | regs[R_EP2] &= ~0x1f; /* clear gain taper bits */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 458 | regs[R_EP2] |= val; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 459 | |
| 460 | tda18271_write_regs(fe, R_EP2, 1); |
| 461 | tda18271_write_regs(fe, R_EP1, 1); |
| 462 | tda18271_write_regs(fe, R_EP2, 1); |
| 463 | tda18271_write_regs(fe, R_EP1, 1); |
| 464 | |
| 465 | regs[R_EB4] &= 0x07; |
| 466 | regs[R_EB4] |= 0x40; |
| 467 | tda18271_write_regs(fe, R_EB4, 1); |
| 468 | |
| 469 | regs[R_EB7] = 0x40; |
| 470 | tda18271_write_regs(fe, R_EB7, 1); |
| 471 | msleep(10); |
| 472 | |
| 473 | regs[R_EB20] = 0xec; |
| 474 | tda18271_write_regs(fe, R_EB20, 1); |
| 475 | msleep(60); /* RF tracking filter calibration completion */ |
| 476 | |
| 477 | regs[R_EP4] &= ~0x03; /* set cal mode to normal */ |
| 478 | tda18271_write_regs(fe, R_EP4, 1); |
| 479 | |
| 480 | tda18271_write_regs(fe, R_EP1, 1); |
| 481 | |
| 482 | /* RF tracking filer correction for VHF_Low band */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 483 | tda18271_calc_rf_cal(&freq, &val); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 484 | |
| 485 | /* VHF_Low band only */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 486 | if (val != 0) { |
| 487 | regs[R_EB14] = val; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 488 | tda18271_write_regs(fe, R_EB14, 1); |
| 489 | } |
| 490 | |
| 491 | /* Channel Configuration */ |
| 492 | |
| 493 | switch (priv->mode) { |
| 494 | case TDA18271_ANALOG: |
| 495 | regs[R_EB22] = 0x2c; |
| 496 | break; |
| 497 | case TDA18271_DIGITAL: |
| 498 | regs[R_EB22] = 0x37; |
| 499 | break; |
| 500 | } |
| 501 | tda18271_write_regs(fe, R_EB22, 1); |
| 502 | |
| 503 | regs[R_EP1] |= 0x40; /* set dis power level on */ |
| 504 | |
| 505 | /* set standard */ |
| 506 | regs[R_EP3] &= ~0x1f; /* clear std bits */ |
| 507 | |
| 508 | /* see table 22 */ |
| 509 | regs[R_EP3] |= std; |
| 510 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 511 | regs[R_EP4] &= ~0x03; /* set cal mode to normal */ |
| 512 | |
| 513 | regs[R_EP4] &= ~0x1c; /* clear if level bits */ |
| 514 | switch (priv->mode) { |
| 515 | case TDA18271_ANALOG: |
| 516 | regs[R_MPD] &= ~0x80; /* IF notch = 0 */ |
| 517 | break; |
| 518 | case TDA18271_DIGITAL: |
| 519 | regs[R_EP4] |= 0x04; |
| 520 | regs[R_MPD] |= 0x80; |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */ |
| 525 | |
Michael Krufky | aaeccba | 2007-12-02 11:03:57 -0300 | [diff] [blame] | 526 | /* image rejection validity EP5[2:0] */ |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 527 | tda18271_calc_ir_measure(&freq, &val); |
| 528 | |
Michael Krufky | aaeccba | 2007-12-02 11:03:57 -0300 | [diff] [blame] | 529 | regs[R_EP5] &= ~0x07; |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 530 | regs[R_EP5] |= val; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 531 | |
| 532 | /* calculate MAIN PLL */ |
| 533 | N = freq + ifc; |
| 534 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 535 | tda18271_calc_main_pll(&N, &pd, &d); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 536 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 537 | regs[R_MPD] = (0x7f & pd); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 538 | switch (priv->mode) { |
| 539 | case TDA18271_ANALOG: |
| 540 | regs[R_MPD] &= ~0x08; |
| 541 | break; |
| 542 | case TDA18271_DIGITAL: |
| 543 | regs[R_MPD] |= 0x08; |
| 544 | break; |
| 545 | } |
| 546 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 547 | div = ((d * (N / 1000)) << 7) / 125; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 548 | regs[R_MD1] = 0xff & (div >> 16); |
| 549 | regs[R_MD2] = 0xff & (div >> 8); |
| 550 | regs[R_MD3] = 0xff & div; |
| 551 | |
| 552 | tda18271_write_regs(fe, R_TM, 15); |
| 553 | msleep(5); |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 554 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | /* ------------------------------------------------------------------ */ |
| 559 | |
| 560 | static int tda18271_set_params(struct dvb_frontend *fe, |
| 561 | struct dvb_frontend_parameters *params) |
| 562 | { |
| 563 | struct tda18271_priv *priv = fe->tuner_priv; |
| 564 | u8 std; |
| 565 | u32 bw, sgIF = 0; |
| 566 | |
| 567 | u32 freq = params->frequency; |
| 568 | |
| 569 | priv->mode = TDA18271_DIGITAL; |
| 570 | |
| 571 | /* see table 22 */ |
| 572 | if (fe->ops.info.type == FE_ATSC) { |
| 573 | switch (params->u.vsb.modulation) { |
| 574 | case VSB_8: |
| 575 | case VSB_16: |
| 576 | std = 0x1b; /* device-specific (spec says 0x1c) */ |
| 577 | sgIF = 5380000; |
| 578 | break; |
| 579 | case QAM_64: |
| 580 | case QAM_256: |
| 581 | std = 0x18; /* device-specific (spec says 0x1d) */ |
| 582 | sgIF = 4000000; |
| 583 | break; |
| 584 | default: |
| 585 | printk(KERN_WARNING "%s: modulation not set!\n", |
| 586 | __FUNCTION__); |
| 587 | return -EINVAL; |
| 588 | } |
Michael Krufky | 14e3c15 | 2007-12-07 00:33:08 -0300 | [diff] [blame] | 589 | #if 0 |
| 590 | /* userspace request is already center adjusted */ |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 591 | freq += 1750000; /* Adjust to center (+1.75MHZ) */ |
Michael Krufky | 14e3c15 | 2007-12-07 00:33:08 -0300 | [diff] [blame] | 592 | #endif |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 593 | bw = 6000000; |
| 594 | } else if (fe->ops.info.type == FE_OFDM) { |
| 595 | switch (params->u.ofdm.bandwidth) { |
| 596 | case BANDWIDTH_6_MHZ: |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 597 | std = 0x1b; /* device-specific (spec says 0x1c) */ |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 598 | bw = 6000000; |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 599 | sgIF = 3300000; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 600 | break; |
| 601 | case BANDWIDTH_7_MHZ: |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 602 | std = 0x19; /* device-specific (spec says 0x1d) */ |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 603 | bw = 7000000; |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 604 | sgIF = 3800000; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 605 | break; |
| 606 | case BANDWIDTH_8_MHZ: |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 607 | std = 0x1a; /* device-specific (spec says 0x1e) */ |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 608 | bw = 8000000; |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 609 | sgIF = 4300000; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 610 | break; |
| 611 | default: |
| 612 | printk(KERN_WARNING "%s: bandwidth not set!\n", |
| 613 | __FUNCTION__); |
| 614 | return -EINVAL; |
| 615 | } |
| 616 | } else { |
| 617 | printk(KERN_WARNING "%s: modulation type not supported!\n", |
| 618 | __FUNCTION__); |
| 619 | return -EINVAL; |
| 620 | } |
| 621 | |
| 622 | return tda18271_tune(fe, sgIF, freq, bw, std); |
| 623 | } |
| 624 | |
| 625 | static int tda18271_set_analog_params(struct dvb_frontend *fe, |
| 626 | struct analog_parameters *params) |
| 627 | { |
| 628 | struct tda18271_priv *priv = fe->tuner_priv; |
| 629 | u8 std; |
| 630 | unsigned int sgIF; |
| 631 | char *mode; |
| 632 | |
| 633 | priv->mode = TDA18271_ANALOG; |
| 634 | |
| 635 | /* see table 22 */ |
| 636 | if (params->std & V4L2_STD_MN) { |
| 637 | std = 0x0d; |
| 638 | sgIF = 92; |
| 639 | mode = "MN"; |
| 640 | } else if (params->std & V4L2_STD_B) { |
| 641 | std = 0x0e; |
| 642 | sgIF = 108; |
| 643 | mode = "B"; |
| 644 | } else if (params->std & V4L2_STD_GH) { |
| 645 | std = 0x0f; |
| 646 | sgIF = 124; |
| 647 | mode = "GH"; |
| 648 | } else if (params->std & V4L2_STD_PAL_I) { |
| 649 | std = 0x0f; |
| 650 | sgIF = 124; |
| 651 | mode = "I"; |
| 652 | } else if (params->std & V4L2_STD_DK) { |
| 653 | std = 0x0f; |
| 654 | sgIF = 124; |
| 655 | mode = "DK"; |
| 656 | } else if (params->std & V4L2_STD_SECAM_L) { |
| 657 | std = 0x0f; |
| 658 | sgIF = 124; |
| 659 | mode = "L"; |
| 660 | } else if (params->std & V4L2_STD_SECAM_LC) { |
| 661 | std = 0x0f; |
| 662 | sgIF = 20; |
| 663 | mode = "LC"; |
| 664 | } else { |
| 665 | std = 0x0f; |
| 666 | sgIF = 124; |
| 667 | mode = "xx"; |
| 668 | } |
| 669 | |
| 670 | if (params->mode == V4L2_TUNER_RADIO) |
| 671 | sgIF = 88; /* if frequency is 5.5 MHz */ |
| 672 | |
Michael Krufky | 293da0e | 2007-12-02 02:45:04 -0300 | [diff] [blame] | 673 | dbg_info("setting tda18271 to system %s\n", mode); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 674 | |
| 675 | return tda18271_tune(fe, sgIF * 62500, params->frequency * 62500, |
| 676 | 0, std); |
| 677 | } |
| 678 | |
| 679 | static int tda18271_release(struct dvb_frontend *fe) |
| 680 | { |
| 681 | kfree(fe->tuner_priv); |
| 682 | fe->tuner_priv = NULL; |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 687 | { |
| 688 | struct tda18271_priv *priv = fe->tuner_priv; |
| 689 | *frequency = priv->frequency; |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) |
| 694 | { |
| 695 | struct tda18271_priv *priv = fe->tuner_priv; |
| 696 | *bandwidth = priv->bandwidth; |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static struct dvb_tuner_ops tda18271_tuner_ops = { |
| 701 | .info = { |
| 702 | .name = "NXP TDA18271HD", |
| 703 | .frequency_min = 45000000, |
| 704 | .frequency_max = 864000000, |
| 705 | .frequency_step = 62500 |
| 706 | }, |
Michael Krufky | efce841 | 2007-12-01 17:40:16 -0300 | [diff] [blame] | 707 | .init = tda18271_init, |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 708 | .set_params = tda18271_set_params, |
| 709 | .set_analog_params = tda18271_set_analog_params, |
| 710 | .release = tda18271_release, |
| 711 | .get_frequency = tda18271_get_frequency, |
| 712 | .get_bandwidth = tda18271_get_bandwidth, |
| 713 | }; |
| 714 | |
| 715 | struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr, |
| 716 | struct i2c_adapter *i2c) |
| 717 | { |
| 718 | struct tda18271_priv *priv = NULL; |
| 719 | |
Michael Krufky | 293da0e | 2007-12-02 02:45:04 -0300 | [diff] [blame] | 720 | dbg_info("@ %d-%04x\n", i2c_adapter_id(i2c), addr); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 721 | priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL); |
| 722 | if (priv == NULL) |
| 723 | return NULL; |
| 724 | |
| 725 | priv->i2c_addr = addr; |
| 726 | priv->i2c_adap = i2c; |
| 727 | |
| 728 | memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops, |
| 729 | sizeof(struct dvb_tuner_ops)); |
| 730 | |
| 731 | fe->tuner_priv = priv; |
| 732 | |
Michael Krufky | efce841 | 2007-12-01 17:40:16 -0300 | [diff] [blame] | 733 | tda18271_init_regs(fe); |
| 734 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 735 | return fe; |
| 736 | } |
| 737 | EXPORT_SYMBOL_GPL(tda18271_attach); |
| 738 | MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver"); |
| 739 | MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>"); |
| 740 | MODULE_LICENSE("GPL"); |
| 741 | |
| 742 | /* |
| 743 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 744 | * --------------------------------------------------------------------------- |
| 745 | * Local variables: |
| 746 | * c-basic-offset: 8 |
| 747 | * End: |
| 748 | */ |