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 | |
Michael Krufky | 59067f7 | 2008-01-02 01:58:26 -0300 | [diff] [blame] | 4 | Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org> |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 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 | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 23 | #include "tda18271-priv.h" |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 24 | |
Michael Krufky | b5f3e1e | 2007-12-02 16:36:05 -0300 | [diff] [blame] | 25 | int tda18271_debug; |
Michael Krufky | 54465b0 | 2007-11-23 18:14:53 -0300 | [diff] [blame] | 26 | module_param_named(debug, tda18271_debug, int, 0644); |
Michael Krufky | 0e1fab9 | 2008-01-03 01:40:47 -0300 | [diff] [blame] | 27 | MODULE_PARM_DESC(debug, "set debug level " |
| 28 | "(info=1, map=2, reg=4, adv=8 (or-able))"); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 29 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 30 | /*---------------------------------------------------------------------*/ |
| 31 | |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 32 | static int tda18271_ir_cal_init(struct dvb_frontend *fe) |
Michael Krufky | efce841 | 2007-12-01 17:40:16 -0300 | [diff] [blame] | 33 | { |
| 34 | struct tda18271_priv *priv = fe->tuner_priv; |
| 35 | unsigned char *regs = priv->tda18271_regs; |
| 36 | |
| 37 | tda18271_read_regs(fe); |
| 38 | |
| 39 | /* test IR_CAL_OK to see if we need init */ |
| 40 | if ((regs[R_EP1] & 0x08) == 0) |
| 41 | tda18271_init_regs(fe); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 46 | /* ------------------------------------------------------------------ */ |
| 47 | |
| 48 | static int tda18271_channel_configuration(struct dvb_frontend *fe, |
| 49 | u32 ifc, u32 freq, u32 bw, u8 std) |
| 50 | { |
| 51 | struct tda18271_priv *priv = fe->tuner_priv; |
| 52 | unsigned char *regs = priv->tda18271_regs; |
| 53 | u32 N; |
| 54 | |
| 55 | /* update TV broadcast parameters */ |
| 56 | |
| 57 | /* set standard */ |
| 58 | regs[R_EP3] &= ~0x1f; /* clear std bits */ |
| 59 | regs[R_EP3] |= std; |
| 60 | |
| 61 | /* set cal mode to normal */ |
| 62 | regs[R_EP4] &= ~0x03; |
| 63 | |
| 64 | /* update IF output level & IF notch frequency */ |
| 65 | regs[R_EP4] &= ~0x1c; /* clear if level bits */ |
| 66 | |
| 67 | switch (priv->mode) { |
| 68 | case TDA18271_ANALOG: |
| 69 | regs[R_MPD] &= ~0x80; /* IF notch = 0 */ |
| 70 | break; |
| 71 | case TDA18271_DIGITAL: |
| 72 | regs[R_EP4] |= 0x04; /* IF level = 1 */ |
| 73 | regs[R_MPD] |= 0x80; /* IF notch = 1 */ |
| 74 | break; |
| 75 | } |
| 76 | regs[R_EP4] &= ~0x80; /* FM_RFn: turn this bit on only for fm radio */ |
| 77 | |
| 78 | /* update RF_TOP / IF_TOP */ |
| 79 | switch (priv->mode) { |
| 80 | case TDA18271_ANALOG: |
| 81 | regs[R_EB22] = 0x2c; |
| 82 | break; |
| 83 | case TDA18271_DIGITAL: |
| 84 | regs[R_EB22] = 0x37; |
| 85 | break; |
| 86 | } |
| 87 | tda18271_write_regs(fe, R_EB22, 1); |
| 88 | |
| 89 | /* --------------------------------------------------------------- */ |
| 90 | |
| 91 | /* disable Power Level Indicator */ |
| 92 | regs[R_EP1] |= 0x40; |
| 93 | |
| 94 | /* frequency dependent parameters */ |
| 95 | |
| 96 | tda18271_calc_ir_measure(fe, &freq); |
| 97 | |
| 98 | tda18271_calc_bp_filter(fe, &freq); |
| 99 | |
| 100 | tda18271_calc_rf_band(fe, &freq); |
| 101 | |
| 102 | tda18271_calc_gain_taper(fe, &freq); |
| 103 | |
| 104 | /* --------------------------------------------------------------- */ |
| 105 | |
| 106 | /* dual tuner and agc1 extra configuration */ |
| 107 | |
| 108 | /* main vco when Master, cal vco when slave */ |
| 109 | regs[R_EB1] |= 0x04; /* FIXME: assumes master */ |
| 110 | |
| 111 | /* agc1 always active */ |
| 112 | regs[R_EB1] &= ~0x02; |
| 113 | |
| 114 | /* agc1 has priority on agc2 */ |
| 115 | regs[R_EB1] &= ~0x01; |
| 116 | |
| 117 | tda18271_write_regs(fe, R_EB1, 1); |
| 118 | |
| 119 | /* --------------------------------------------------------------- */ |
| 120 | |
| 121 | N = freq + ifc; |
| 122 | |
| 123 | /* FIXME: assumes master */ |
| 124 | tda18271_calc_main_pll(fe, N); |
| 125 | tda18271_write_regs(fe, R_MPD, 4); |
| 126 | |
| 127 | tda18271_write_regs(fe, R_TM, 7); |
| 128 | |
| 129 | /* main pll charge pump source */ |
| 130 | regs[R_EB4] |= 0x20; |
| 131 | tda18271_write_regs(fe, R_EB4, 1); |
| 132 | |
| 133 | msleep(1); |
| 134 | |
| 135 | /* normal operation for the main pll */ |
| 136 | regs[R_EB4] &= ~0x20; |
| 137 | tda18271_write_regs(fe, R_EB4, 1); |
| 138 | |
| 139 | msleep(5); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int tda18271_read_thermometer(struct dvb_frontend *fe) |
| 145 | { |
| 146 | struct tda18271_priv *priv = fe->tuner_priv; |
| 147 | unsigned char *regs = priv->tda18271_regs; |
| 148 | int tm; |
| 149 | |
| 150 | /* switch thermometer on */ |
| 151 | regs[R_TM] |= 0x10; |
| 152 | tda18271_write_regs(fe, R_TM, 1); |
| 153 | |
| 154 | /* read thermometer info */ |
| 155 | tda18271_read_regs(fe); |
| 156 | |
| 157 | if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) || |
| 158 | (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) { |
| 159 | |
| 160 | if ((regs[R_TM] & 0x20) == 0x20) |
| 161 | regs[R_TM] &= ~0x20; |
| 162 | else |
| 163 | regs[R_TM] |= 0x20; |
| 164 | |
| 165 | tda18271_write_regs(fe, R_TM, 1); |
| 166 | |
| 167 | msleep(10); /* temperature sensing */ |
| 168 | |
| 169 | /* read thermometer info */ |
| 170 | tda18271_read_regs(fe); |
| 171 | } |
| 172 | |
| 173 | tm = tda18271_lookup_thermometer(fe); |
| 174 | |
| 175 | /* switch thermometer off */ |
| 176 | regs[R_TM] &= ~0x10; |
| 177 | tda18271_write_regs(fe, R_TM, 1); |
| 178 | |
| 179 | /* set CAL mode to normal */ |
| 180 | regs[R_EP4] &= ~0x03; |
| 181 | tda18271_write_regs(fe, R_EP4, 1); |
| 182 | |
| 183 | return tm; |
| 184 | } |
| 185 | |
| 186 | static int tda18271_rf_tracking_filters_correction(struct dvb_frontend *fe, |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 187 | u32 freq) |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 188 | { |
| 189 | struct tda18271_priv *priv = fe->tuner_priv; |
| 190 | struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state; |
| 191 | unsigned char *regs = priv->tda18271_regs; |
| 192 | int tm_current, rfcal_comp, approx, i; |
| 193 | u8 dc_over_dt, rf_tab; |
| 194 | |
| 195 | /* power up */ |
| 196 | regs[R_EP3] &= ~0xe0; /* sm = 0, sm_lt = 0, sm_xt = 0 */ |
| 197 | tda18271_write_regs(fe, R_EP3, 1); |
| 198 | |
| 199 | /* read die current temperature */ |
| 200 | tm_current = tda18271_read_thermometer(fe); |
| 201 | |
| 202 | /* frequency dependent parameters */ |
| 203 | |
| 204 | tda18271_calc_rf_cal(fe, &freq); |
| 205 | rf_tab = regs[R_EB14]; |
| 206 | |
| 207 | i = tda18271_lookup_rf_band(fe, &freq, NULL); |
| 208 | if (i < 0) |
| 209 | return -EINVAL; |
| 210 | |
| 211 | if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) { |
| 212 | approx = map[i].rf_a1 * |
| 213 | (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab; |
| 214 | } else { |
| 215 | approx = map[i].rf_a2 * |
| 216 | (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab; |
| 217 | } |
| 218 | |
| 219 | if (approx < 0) |
| 220 | approx = 0; |
| 221 | if (approx > 255) |
| 222 | approx = 255; |
| 223 | |
| 224 | tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt); |
| 225 | |
| 226 | /* calculate temperature compensation */ |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 227 | rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal); |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 228 | |
| 229 | regs[R_EB14] = approx + rfcal_comp; |
| 230 | tda18271_write_regs(fe, R_EB14, 1); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int tda18271_por(struct dvb_frontend *fe) |
| 236 | { |
| 237 | struct tda18271_priv *priv = fe->tuner_priv; |
| 238 | unsigned char *regs = priv->tda18271_regs; |
| 239 | |
| 240 | /* power up detector 1 */ |
| 241 | regs[R_EB12] &= ~0x20; |
| 242 | tda18271_write_regs(fe, R_EB12, 1); |
| 243 | |
| 244 | regs[R_EB18] &= ~0x80; /* turn agc1 loop on */ |
| 245 | regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */ |
| 246 | tda18271_write_regs(fe, R_EB18, 1); |
| 247 | |
| 248 | regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */ |
| 249 | |
| 250 | /* POR mode */ |
| 251 | regs[R_EP3] &= ~0xe0; /* clear sm, sm_lt, sm_xt */ |
| 252 | regs[R_EP3] |= 0x80; /* sm = 1, sm_lt = 0, sm_xt = 0 */ |
| 253 | tda18271_write_regs(fe, R_EP3, 1); |
| 254 | |
| 255 | /* disable 1.5 MHz low pass filter */ |
| 256 | regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */ |
| 257 | regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */ |
| 258 | tda18271_write_regs(fe, R_EB21, 3); |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq) |
| 264 | { |
| 265 | struct tda18271_priv *priv = fe->tuner_priv; |
| 266 | unsigned char *regs = priv->tda18271_regs; |
| 267 | u32 N; |
| 268 | |
| 269 | /* set CAL mode to normal */ |
| 270 | regs[R_EP4] &= ~0x03; |
| 271 | tda18271_write_regs(fe, R_EP4, 1); |
| 272 | |
| 273 | /* switch off agc1 */ |
| 274 | regs[R_EP3] |= 0x40; /* sm_lt = 1 */ |
| 275 | |
| 276 | regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */ |
| 277 | tda18271_write_regs(fe, R_EB18, 1); |
| 278 | |
| 279 | /* frequency dependent parameters */ |
| 280 | |
| 281 | tda18271_calc_bp_filter(fe, &freq); |
| 282 | tda18271_calc_gain_taper(fe, &freq); |
| 283 | tda18271_calc_rf_band(fe, &freq); |
| 284 | tda18271_calc_km(fe, &freq); |
| 285 | |
| 286 | tda18271_write_regs(fe, R_EP1, 3); |
| 287 | tda18271_write_regs(fe, R_EB13, 1); |
| 288 | |
| 289 | /* main pll charge pump source */ |
| 290 | regs[R_EB4] |= 0x20; |
| 291 | tda18271_write_regs(fe, R_EB4, 1); |
| 292 | |
| 293 | /* cal pll charge pump source */ |
| 294 | regs[R_EB7] |= 0x20; |
| 295 | tda18271_write_regs(fe, R_EB7, 1); |
| 296 | |
| 297 | /* force dcdc converter to 0 V */ |
| 298 | regs[R_EB14] = 0x00; |
| 299 | tda18271_write_regs(fe, R_EB14, 1); |
| 300 | |
| 301 | /* disable plls lock */ |
| 302 | regs[R_EB20] &= ~0x20; |
| 303 | tda18271_write_regs(fe, R_EB20, 1); |
| 304 | |
| 305 | /* set CAL mode to RF tracking filter calibration */ |
| 306 | regs[R_EP4] |= 0x03; |
| 307 | tda18271_write_regs(fe, R_EP4, 2); |
| 308 | |
| 309 | /* --------------------------------------------------------------- */ |
| 310 | |
| 311 | /* set the internal calibration signal */ |
| 312 | N = freq; |
| 313 | |
| 314 | tda18271_calc_main_pll(fe, N); |
| 315 | tda18271_write_regs(fe, R_MPD, 4); |
| 316 | |
| 317 | /* downconvert internal calibration */ |
| 318 | N += 1000000; |
| 319 | |
| 320 | tda18271_calc_main_pll(fe, N); |
| 321 | tda18271_write_regs(fe, R_MPD, 4); |
| 322 | |
| 323 | msleep(5); |
| 324 | |
| 325 | tda18271_write_regs(fe, R_EP2, 1); |
| 326 | tda18271_write_regs(fe, R_EP1, 1); |
| 327 | tda18271_write_regs(fe, R_EP2, 1); |
| 328 | tda18271_write_regs(fe, R_EP1, 1); |
| 329 | |
| 330 | /* --------------------------------------------------------------- */ |
| 331 | |
| 332 | /* normal operation for the main pll */ |
| 333 | regs[R_EB4] &= ~0x20; |
| 334 | tda18271_write_regs(fe, R_EB4, 1); |
| 335 | |
| 336 | /* normal operation for the cal pll */ |
| 337 | regs[R_EB7] &= ~0x20; |
| 338 | tda18271_write_regs(fe, R_EB7, 1); |
| 339 | |
| 340 | msleep(5); /* plls locking */ |
| 341 | |
| 342 | /* launch the rf tracking filters calibration */ |
| 343 | regs[R_EB20] |= 0x20; |
| 344 | tda18271_write_regs(fe, R_EB20, 1); |
| 345 | |
| 346 | msleep(60); /* calibration */ |
| 347 | |
| 348 | /* --------------------------------------------------------------- */ |
| 349 | |
| 350 | /* set CAL mode to normal */ |
| 351 | regs[R_EP4] &= ~0x03; |
| 352 | |
| 353 | /* switch on agc1 */ |
| 354 | regs[R_EP3] &= ~0x40; /* sm_lt = 0 */ |
| 355 | |
| 356 | regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */ |
| 357 | tda18271_write_regs(fe, R_EB18, 1); |
| 358 | |
| 359 | tda18271_write_regs(fe, R_EP3, 2); |
| 360 | |
| 361 | /* synchronization */ |
| 362 | tda18271_write_regs(fe, R_EP1, 1); |
| 363 | |
| 364 | /* get calibration result */ |
| 365 | tda18271_read_extended(fe); |
| 366 | |
| 367 | return regs[R_EB14]; |
| 368 | } |
| 369 | |
| 370 | static int tda18271_powerscan(struct dvb_frontend *fe, |
| 371 | u32 *freq_in, u32 *freq_out) |
| 372 | { |
| 373 | struct tda18271_priv *priv = fe->tuner_priv; |
| 374 | unsigned char *regs = priv->tda18271_regs; |
| 375 | int sgn, bcal, count, wait; |
| 376 | u8 cid_target; |
| 377 | u16 count_limit; |
| 378 | u32 freq; |
| 379 | |
| 380 | freq = *freq_in; |
| 381 | |
| 382 | tda18271_calc_rf_band(fe, &freq); |
| 383 | tda18271_calc_rf_cal(fe, &freq); |
| 384 | tda18271_calc_gain_taper(fe, &freq); |
| 385 | tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit); |
| 386 | |
| 387 | tda18271_write_regs(fe, R_EP2, 1); |
| 388 | tda18271_write_regs(fe, R_EB14, 1); |
| 389 | |
| 390 | /* downconvert frequency */ |
| 391 | freq += 1000000; |
| 392 | |
| 393 | tda18271_calc_main_pll(fe, freq); |
| 394 | tda18271_write_regs(fe, R_MPD, 4); |
| 395 | |
| 396 | msleep(5); /* pll locking */ |
| 397 | |
| 398 | /* detection mode */ |
| 399 | regs[R_EP4] &= ~0x03; |
| 400 | regs[R_EP4] |= 0x01; |
| 401 | tda18271_write_regs(fe, R_EP4, 1); |
| 402 | |
| 403 | /* launch power detection measurement */ |
| 404 | tda18271_write_regs(fe, R_EP2, 1); |
| 405 | |
| 406 | /* read power detection info, stored in EB10 */ |
| 407 | tda18271_read_extended(fe); |
| 408 | |
| 409 | /* algorithm initialization */ |
| 410 | sgn = 1; |
| 411 | *freq_out = *freq_in; |
| 412 | bcal = 0; |
| 413 | count = 0; |
| 414 | wait = false; |
| 415 | |
| 416 | while ((regs[R_EB10] & 0x3f) < cid_target) { |
| 417 | /* downconvert updated freq to 1 MHz */ |
| 418 | freq = *freq_in + (sgn * count) + 1000000; |
| 419 | |
| 420 | tda18271_calc_main_pll(fe, freq); |
| 421 | tda18271_write_regs(fe, R_MPD, 4); |
| 422 | |
| 423 | if (wait) { |
| 424 | msleep(5); /* pll locking */ |
| 425 | wait = false; |
| 426 | } else |
| 427 | udelay(100); /* pll locking */ |
| 428 | |
| 429 | /* launch power detection measurement */ |
| 430 | tda18271_write_regs(fe, R_EP2, 1); |
| 431 | |
| 432 | /* read power detection info, stored in EB10 */ |
| 433 | tda18271_read_extended(fe); |
| 434 | |
| 435 | count += 200; |
| 436 | |
| 437 | if (count < count_limit) |
| 438 | continue; |
| 439 | |
| 440 | if (sgn <= 0) |
| 441 | break; |
| 442 | |
| 443 | sgn = -1 * sgn; |
| 444 | count = 200; |
| 445 | wait = true; |
| 446 | } |
| 447 | |
| 448 | if ((regs[R_EB10] & 0x3f) >= cid_target) { |
| 449 | bcal = 1; |
| 450 | *freq_out = freq - 1000000; |
| 451 | } else |
| 452 | bcal = 0; |
| 453 | |
| 454 | tda_dbg("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n", |
| 455 | bcal, *freq_in, *freq_out, freq); |
| 456 | |
| 457 | return bcal; |
| 458 | } |
| 459 | |
| 460 | static int tda18271_powerscan_init(struct dvb_frontend *fe) |
| 461 | { |
| 462 | struct tda18271_priv *priv = fe->tuner_priv; |
| 463 | unsigned char *regs = priv->tda18271_regs; |
| 464 | |
| 465 | /* set standard to digital */ |
| 466 | regs[R_EP3] &= ~0x1f; /* clear std bits */ |
| 467 | regs[R_EP3] |= 0x12; |
| 468 | |
| 469 | /* set cal mode to normal */ |
| 470 | regs[R_EP4] &= ~0x03; |
| 471 | |
| 472 | /* update IF output level & IF notch frequency */ |
| 473 | regs[R_EP4] &= ~0x1c; /* clear if level bits */ |
| 474 | |
| 475 | tda18271_write_regs(fe, R_EP3, 2); |
| 476 | |
| 477 | regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */ |
| 478 | tda18271_write_regs(fe, R_EB18, 1); |
| 479 | |
| 480 | regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */ |
| 481 | |
| 482 | /* 1.5 MHz low pass filter */ |
| 483 | regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */ |
| 484 | regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */ |
| 485 | |
| 486 | tda18271_write_regs(fe, R_EB21, 3); |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq) |
| 492 | { |
| 493 | struct tda18271_priv *priv = fe->tuner_priv; |
| 494 | struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state; |
| 495 | unsigned char *regs = priv->tda18271_regs; |
| 496 | int bcal, rf, i; |
| 497 | #define RF1 0 |
| 498 | #define RF2 1 |
| 499 | #define RF3 2 |
| 500 | u32 rf_default[3]; |
| 501 | u32 rf_freq[3]; |
| 502 | u8 prog_cal[3]; |
| 503 | u8 prog_tab[3]; |
| 504 | |
| 505 | i = tda18271_lookup_rf_band(fe, &freq, NULL); |
| 506 | |
| 507 | if (i < 0) |
| 508 | return i; |
| 509 | |
| 510 | rf_default[RF1] = 1000 * map[i].rf1_def; |
| 511 | rf_default[RF2] = 1000 * map[i].rf2_def; |
| 512 | rf_default[RF3] = 1000 * map[i].rf3_def; |
| 513 | |
| 514 | for (rf = RF1; rf <= RF3; rf++) { |
| 515 | if (0 == rf_default[rf]) |
| 516 | return 0; |
| 517 | tda_dbg("freq = %d, rf = %d\n", freq, rf); |
| 518 | |
| 519 | /* look for optimized calibration frequency */ |
| 520 | bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]); |
| 521 | |
| 522 | tda18271_calc_rf_cal(fe, &rf_freq[rf]); |
| 523 | prog_tab[rf] = regs[R_EB14]; |
| 524 | |
| 525 | if (1 == bcal) |
| 526 | prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]); |
| 527 | else |
| 528 | prog_cal[rf] = prog_tab[rf]; |
| 529 | |
| 530 | switch (rf) { |
| 531 | case RF1: |
| 532 | map[i].rf_a1 = 0; |
| 533 | map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1]; |
| 534 | map[i].rf1 = rf_freq[RF1] / 1000; |
| 535 | break; |
| 536 | case RF2: |
| 537 | map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] - |
| 538 | prog_cal[RF1] + prog_tab[RF1]) / |
| 539 | ((rf_freq[RF2] - rf_freq[RF1]) / 1000); |
| 540 | map[i].rf2 = rf_freq[RF2] / 1000; |
| 541 | break; |
| 542 | case RF3: |
| 543 | map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] - |
| 544 | prog_cal[RF2] + prog_tab[RF2]) / |
| 545 | ((rf_freq[RF3] - rf_freq[RF2]) / 1000); |
| 546 | map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2]; |
| 547 | map[i].rf3 = rf_freq[RF3] / 1000; |
| 548 | break; |
| 549 | default: |
| 550 | BUG(); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 557 | static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe) |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 558 | { |
| 559 | struct tda18271_priv *priv = fe->tuner_priv; |
| 560 | unsigned int i; |
| 561 | |
| 562 | tda_info("tda18271: performing RF tracking filter calibration\n"); |
| 563 | |
| 564 | /* wait for die temperature stabilization */ |
| 565 | msleep(200); |
| 566 | |
| 567 | tda18271_powerscan_init(fe); |
| 568 | |
| 569 | /* rf band calibration */ |
| 570 | for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) |
| 571 | tda18271_rf_tracking_filters_init(fe, 1000 * |
| 572 | priv->rf_cal_state[i].rfmax); |
| 573 | |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 574 | priv->tm_rfcal = tda18271_read_thermometer(fe); |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /* ------------------------------------------------------------------ */ |
| 580 | |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 581 | static int tda18271_rf_cal_init(struct dvb_frontend *fe) |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 582 | { |
| 583 | struct tda18271_priv *priv = fe->tuner_priv; |
| 584 | |
| 585 | if (priv->cal_initialized) |
| 586 | return 0; |
| 587 | |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 588 | tda18271_calc_rf_filter_curve(fe); |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 589 | |
| 590 | tda18271_por(fe); |
| 591 | |
| 592 | priv->cal_initialized = true; |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 597 | static int tda18271_init(struct dvb_frontend *fe) |
| 598 | { |
| 599 | struct tda18271_priv *priv = fe->tuner_priv; |
| 600 | |
| 601 | /* initialization */ |
| 602 | tda18271_ir_cal_init(fe); |
| 603 | |
| 604 | if (priv->id == TDA18271HDC2) |
| 605 | tda18271_rf_cal_init(fe); |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 610 | static int tda18271c2_tune(struct dvb_frontend *fe, |
| 611 | u32 ifc, u32 freq, u32 bw, u8 std) |
| 612 | { |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 613 | tda_dbg("freq = %d, ifc = %d\n", freq, ifc); |
| 614 | |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 615 | tda18271_init(fe); |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 616 | |
Michael Krufky | 09f83c4 | 2008-01-05 20:00:09 -0300 | [diff] [blame^] | 617 | tda18271_rf_tracking_filters_correction(fe, freq); |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 618 | |
| 619 | tda18271_channel_configuration(fe, ifc, freq, bw, std); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* ------------------------------------------------------------------ */ |
| 625 | |
| 626 | static int tda18271c1_tune(struct dvb_frontend *fe, |
| 627 | u32 ifc, u32 freq, u32 bw, u8 std) |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 628 | { |
| 629 | struct tda18271_priv *priv = fe->tuner_priv; |
| 630 | unsigned char *regs = priv->tda18271_regs; |
Michael Krufky | fe0bf6d | 2007-12-24 05:05:05 -0300 | [diff] [blame] | 631 | u32 N = 0; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 632 | |
Michael Krufky | 1457263 | 2007-12-02 02:32:49 -0300 | [diff] [blame] | 633 | tda18271_init(fe); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 634 | |
Michael Krufky | 182519f | 2007-12-25 15:10:11 -0300 | [diff] [blame] | 635 | tda_dbg("freq = %d, ifc = %d\n", freq, ifc); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 636 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 637 | /* RF tracking filter calibration */ |
| 638 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 639 | /* calculate bp filter */ |
Michael Krufky | b92bf0f | 2007-12-25 18:54:22 -0300 | [diff] [blame] | 640 | tda18271_calc_bp_filter(fe, &freq); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 641 | tda18271_write_regs(fe, R_EP1, 1); |
| 642 | |
| 643 | regs[R_EB4] &= 0x07; |
| 644 | regs[R_EB4] |= 0x60; |
| 645 | tda18271_write_regs(fe, R_EB4, 1); |
| 646 | |
| 647 | regs[R_EB7] = 0x60; |
| 648 | tda18271_write_regs(fe, R_EB7, 1); |
| 649 | |
| 650 | regs[R_EB14] = 0x00; |
| 651 | tda18271_write_regs(fe, R_EB14, 1); |
| 652 | |
| 653 | regs[R_EB20] = 0xcc; |
| 654 | tda18271_write_regs(fe, R_EB20, 1); |
| 655 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 656 | /* set cal mode to RF tracking filter calibration */ |
Michael Krufky | 26501a7 | 2007-12-21 14:28:46 -0300 | [diff] [blame] | 657 | regs[R_EP4] |= 0x03; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 658 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 659 | /* calculate cal pll */ |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 660 | |
| 661 | switch (priv->mode) { |
| 662 | case TDA18271_ANALOG: |
| 663 | N = freq - 1250000; |
| 664 | break; |
| 665 | case TDA18271_DIGITAL: |
| 666 | N = freq + bw / 2; |
| 667 | break; |
| 668 | } |
| 669 | |
Michael Krufky | fe0bf6d | 2007-12-24 05:05:05 -0300 | [diff] [blame] | 670 | tda18271_calc_cal_pll(fe, N); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 671 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 672 | /* calculate main pll */ |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 673 | |
| 674 | switch (priv->mode) { |
| 675 | case TDA18271_ANALOG: |
| 676 | N = freq - 250000; |
| 677 | break; |
| 678 | case TDA18271_DIGITAL: |
| 679 | N = freq + bw / 2 + 1000000; |
| 680 | break; |
| 681 | } |
| 682 | |
Michael Krufky | fe0bf6d | 2007-12-24 05:05:05 -0300 | [diff] [blame] | 683 | tda18271_calc_main_pll(fe, N); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 684 | |
| 685 | tda18271_write_regs(fe, R_EP3, 11); |
| 686 | msleep(5); /* RF tracking filter calibration initialization */ |
| 687 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 688 | /* search for K,M,CO for RF calibration */ |
Michael Krufky | b92bf0f | 2007-12-25 18:54:22 -0300 | [diff] [blame] | 689 | tda18271_calc_km(fe, &freq); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 690 | tda18271_write_regs(fe, R_EB13, 1); |
| 691 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 692 | /* search for rf band */ |
Michael Krufky | b92bf0f | 2007-12-25 18:54:22 -0300 | [diff] [blame] | 693 | tda18271_calc_rf_band(fe, &freq); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 694 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 695 | /* search for gain taper */ |
Michael Krufky | b92bf0f | 2007-12-25 18:54:22 -0300 | [diff] [blame] | 696 | tda18271_calc_gain_taper(fe, &freq); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 697 | |
| 698 | tda18271_write_regs(fe, R_EP2, 1); |
| 699 | tda18271_write_regs(fe, R_EP1, 1); |
| 700 | tda18271_write_regs(fe, R_EP2, 1); |
| 701 | tda18271_write_regs(fe, R_EP1, 1); |
| 702 | |
| 703 | regs[R_EB4] &= 0x07; |
| 704 | regs[R_EB4] |= 0x40; |
| 705 | tda18271_write_regs(fe, R_EB4, 1); |
| 706 | |
| 707 | regs[R_EB7] = 0x40; |
| 708 | tda18271_write_regs(fe, R_EB7, 1); |
| 709 | msleep(10); |
| 710 | |
| 711 | regs[R_EB20] = 0xec; |
| 712 | tda18271_write_regs(fe, R_EB20, 1); |
| 713 | msleep(60); /* RF tracking filter calibration completion */ |
| 714 | |
| 715 | regs[R_EP4] &= ~0x03; /* set cal mode to normal */ |
| 716 | tda18271_write_regs(fe, R_EP4, 1); |
| 717 | |
| 718 | tda18271_write_regs(fe, R_EP1, 1); |
| 719 | |
Michael Krufky | b92bf0f | 2007-12-25 18:54:22 -0300 | [diff] [blame] | 720 | /* RF tracking filter correction for VHF_Low band */ |
| 721 | if (0 == tda18271_calc_rf_cal(fe, &freq)) |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 722 | tda18271_write_regs(fe, R_EB14, 1); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 723 | |
| 724 | /* Channel Configuration */ |
| 725 | |
| 726 | switch (priv->mode) { |
| 727 | case TDA18271_ANALOG: |
| 728 | regs[R_EB22] = 0x2c; |
| 729 | break; |
| 730 | case TDA18271_DIGITAL: |
| 731 | regs[R_EB22] = 0x37; |
| 732 | break; |
| 733 | } |
| 734 | tda18271_write_regs(fe, R_EB22, 1); |
| 735 | |
| 736 | regs[R_EP1] |= 0x40; /* set dis power level on */ |
| 737 | |
| 738 | /* set standard */ |
| 739 | regs[R_EP3] &= ~0x1f; /* clear std bits */ |
| 740 | |
| 741 | /* see table 22 */ |
| 742 | regs[R_EP3] |= std; |
| 743 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 744 | regs[R_EP4] &= ~0x03; /* set cal mode to normal */ |
| 745 | |
| 746 | regs[R_EP4] &= ~0x1c; /* clear if level bits */ |
| 747 | switch (priv->mode) { |
| 748 | case TDA18271_ANALOG: |
| 749 | regs[R_MPD] &= ~0x80; /* IF notch = 0 */ |
| 750 | break; |
| 751 | case TDA18271_DIGITAL: |
| 752 | regs[R_EP4] |= 0x04; |
| 753 | regs[R_MPD] |= 0x80; |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */ |
| 758 | |
Michael Krufky | b92bf0f | 2007-12-25 18:54:22 -0300 | [diff] [blame] | 759 | /* image rejection validity */ |
| 760 | tda18271_calc_ir_measure(fe, &freq); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 761 | |
| 762 | /* calculate MAIN PLL */ |
| 763 | N = freq + ifc; |
| 764 | |
Michael Krufky | fe0bf6d | 2007-12-24 05:05:05 -0300 | [diff] [blame] | 765 | tda18271_calc_main_pll(fe, N); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 766 | |
| 767 | tda18271_write_regs(fe, R_TM, 15); |
| 768 | msleep(5); |
Michael Krufky | 6ca04de | 2007-11-23 16:52:15 -0300 | [diff] [blame] | 769 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | /* ------------------------------------------------------------------ */ |
| 774 | |
| 775 | static int tda18271_set_params(struct dvb_frontend *fe, |
| 776 | struct dvb_frontend_parameters *params) |
| 777 | { |
| 778 | struct tda18271_priv *priv = fe->tuner_priv; |
Michael Krufky | f21e0d7 | 2008-01-02 03:01:54 -0300 | [diff] [blame] | 779 | struct tda18271_std_map *std_map = &priv->std; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 780 | u8 std; |
Michael Krufky | 2ba65d5 | 2008-01-03 01:17:45 -0300 | [diff] [blame] | 781 | u16 sgIF; |
| 782 | u32 bw, freq = params->frequency; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 783 | |
Michael Krufky | f21e0d7 | 2008-01-02 03:01:54 -0300 | [diff] [blame] | 784 | BUG_ON(!priv->tune); |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 785 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 786 | priv->mode = TDA18271_DIGITAL; |
| 787 | |
| 788 | /* see table 22 */ |
| 789 | if (fe->ops.info.type == FE_ATSC) { |
| 790 | switch (params->u.vsb.modulation) { |
| 791 | case VSB_8: |
| 792 | case VSB_16: |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 793 | std = std_map->atsc_6.std_bits; |
| 794 | sgIF = std_map->atsc_6.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 795 | break; |
| 796 | case QAM_64: |
| 797 | case QAM_256: |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 798 | std = std_map->qam_6.std_bits; |
| 799 | sgIF = std_map->qam_6.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 800 | break; |
| 801 | default: |
Michael Krufky | 182519f | 2007-12-25 15:10:11 -0300 | [diff] [blame] | 802 | tda_warn("modulation not set!\n"); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 803 | return -EINVAL; |
| 804 | } |
Michael Krufky | 14e3c15 | 2007-12-07 00:33:08 -0300 | [diff] [blame] | 805 | #if 0 |
| 806 | /* userspace request is already center adjusted */ |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 807 | freq += 1750000; /* Adjust to center (+1.75MHZ) */ |
Michael Krufky | 14e3c15 | 2007-12-07 00:33:08 -0300 | [diff] [blame] | 808 | #endif |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 809 | bw = 6000000; |
| 810 | } else if (fe->ops.info.type == FE_OFDM) { |
| 811 | switch (params->u.ofdm.bandwidth) { |
| 812 | case BANDWIDTH_6_MHZ: |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 813 | bw = 6000000; |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 814 | std = std_map->dvbt_6.std_bits; |
| 815 | sgIF = std_map->dvbt_6.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 816 | break; |
| 817 | case BANDWIDTH_7_MHZ: |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 818 | bw = 7000000; |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 819 | std = std_map->dvbt_7.std_bits; |
| 820 | sgIF = std_map->dvbt_7.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 821 | break; |
| 822 | case BANDWIDTH_8_MHZ: |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 823 | bw = 8000000; |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 824 | std = std_map->dvbt_8.std_bits; |
| 825 | sgIF = std_map->dvbt_8.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 826 | break; |
| 827 | default: |
Michael Krufky | 182519f | 2007-12-25 15:10:11 -0300 | [diff] [blame] | 828 | tda_warn("bandwidth not set!\n"); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 829 | return -EINVAL; |
| 830 | } |
| 831 | } else { |
Michael Krufky | 182519f | 2007-12-25 15:10:11 -0300 | [diff] [blame] | 832 | tda_warn("modulation type not supported!\n"); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 833 | return -EINVAL; |
| 834 | } |
| 835 | |
Michael Krufky | 2ba65d5 | 2008-01-03 01:17:45 -0300 | [diff] [blame] | 836 | return priv->tune(fe, sgIF * 1000, freq, bw, std); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static int tda18271_set_analog_params(struct dvb_frontend *fe, |
| 840 | struct analog_parameters *params) |
| 841 | { |
| 842 | struct tda18271_priv *priv = fe->tuner_priv; |
Michael Krufky | f21e0d7 | 2008-01-02 03:01:54 -0300 | [diff] [blame] | 843 | struct tda18271_std_map *std_map = &priv->std; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 844 | char *mode; |
Michael Krufky | 95af8a2 | 2008-01-01 18:31:34 -0300 | [diff] [blame] | 845 | u8 std; |
Michael Krufky | 2ba65d5 | 2008-01-03 01:17:45 -0300 | [diff] [blame] | 846 | u16 sgIF; |
| 847 | u32 freq = params->frequency * 62500; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 848 | |
Michael Krufky | f21e0d7 | 2008-01-02 03:01:54 -0300 | [diff] [blame] | 849 | BUG_ON(!priv->tune); |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 850 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 851 | priv->mode = TDA18271_ANALOG; |
| 852 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 853 | if (params->std & V4L2_STD_MN) { |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 854 | std = std_map->atv_mn.std_bits; |
| 855 | sgIF = std_map->atv_mn.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 856 | mode = "MN"; |
| 857 | } else if (params->std & V4L2_STD_B) { |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 858 | std = std_map->atv_b.std_bits; |
| 859 | sgIF = std_map->atv_b.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 860 | mode = "B"; |
| 861 | } else if (params->std & V4L2_STD_GH) { |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 862 | std = std_map->atv_gh.std_bits; |
| 863 | sgIF = std_map->atv_gh.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 864 | mode = "GH"; |
| 865 | } else if (params->std & V4L2_STD_PAL_I) { |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 866 | std = std_map->atv_i.std_bits; |
| 867 | sgIF = std_map->atv_i.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 868 | mode = "I"; |
| 869 | } else if (params->std & V4L2_STD_DK) { |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 870 | std = std_map->atv_dk.std_bits; |
| 871 | sgIF = std_map->atv_dk.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 872 | mode = "DK"; |
| 873 | } else if (params->std & V4L2_STD_SECAM_L) { |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 874 | std = std_map->atv_l.std_bits; |
| 875 | sgIF = std_map->atv_l.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 876 | mode = "L"; |
| 877 | } else if (params->std & V4L2_STD_SECAM_LC) { |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 878 | std = std_map->atv_lc.std_bits; |
| 879 | sgIF = std_map->atv_lc.if_freq; |
Michael Krufky | 95af8a2 | 2008-01-01 18:31:34 -0300 | [diff] [blame] | 880 | mode = "L'"; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 881 | } else { |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 882 | std = std_map->atv_i.std_bits; |
| 883 | sgIF = std_map->atv_i.if_freq; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 884 | mode = "xx"; |
| 885 | } |
| 886 | |
Michael Krufky | 182519f | 2007-12-25 15:10:11 -0300 | [diff] [blame] | 887 | tda_dbg("setting tda18271 to system %s\n", mode); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 888 | |
Michael Krufky | 2ba65d5 | 2008-01-03 01:17:45 -0300 | [diff] [blame] | 889 | return priv->tune(fe, sgIF * 1000, freq, 0, std); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static int tda18271_release(struct dvb_frontend *fe) |
| 893 | { |
| 894 | kfree(fe->tuner_priv); |
| 895 | fe->tuner_priv = NULL; |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 900 | { |
| 901 | struct tda18271_priv *priv = fe->tuner_priv; |
| 902 | *frequency = priv->frequency; |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) |
| 907 | { |
| 908 | struct tda18271_priv *priv = fe->tuner_priv; |
| 909 | *bandwidth = priv->bandwidth; |
| 910 | return 0; |
| 911 | } |
| 912 | |
Michael Krufky | f21e0d7 | 2008-01-02 03:01:54 -0300 | [diff] [blame] | 913 | /* ------------------------------------------------------------------ */ |
| 914 | |
| 915 | #define tda18271_update_std(std_cfg, name) do { \ |
| 916 | if (map->std_cfg.if_freq + map->std_cfg.std_bits > 0) { \ |
| 917 | tda_dbg("Using custom std config for %s\n", name); \ |
| 918 | memcpy(&std->std_cfg, &map->std_cfg, \ |
| 919 | sizeof(struct tda18271_std_map_item)); \ |
| 920 | } } while (0) |
| 921 | |
| 922 | #define tda18271_dump_std_item(std_cfg, name) do { \ |
| 923 | tda_dbg("(%s) if freq = %d, std bits = 0x%02x\n", \ |
| 924 | name, std->std_cfg.if_freq, std->std_cfg.std_bits); \ |
| 925 | } while (0) |
| 926 | |
| 927 | static int tda18271_dump_std_map(struct dvb_frontend *fe) |
| 928 | { |
| 929 | struct tda18271_priv *priv = fe->tuner_priv; |
| 930 | struct tda18271_std_map *std = &priv->std; |
| 931 | |
| 932 | tda_dbg("========== STANDARD MAP SETTINGS ==========\n"); |
| 933 | tda18271_dump_std_item(atv_b, "pal b"); |
| 934 | tda18271_dump_std_item(atv_dk, "pal dk"); |
| 935 | tda18271_dump_std_item(atv_gh, "pal gh"); |
| 936 | tda18271_dump_std_item(atv_i, "pal i"); |
| 937 | tda18271_dump_std_item(atv_l, "pal l"); |
| 938 | tda18271_dump_std_item(atv_lc, "pal l'"); |
| 939 | tda18271_dump_std_item(atv_mn, "atv mn"); |
| 940 | tda18271_dump_std_item(atsc_6, "atsc 6"); |
| 941 | tda18271_dump_std_item(dvbt_6, "dvbt 6"); |
| 942 | tda18271_dump_std_item(dvbt_7, "dvbt 7"); |
| 943 | tda18271_dump_std_item(dvbt_8, "dvbt 8"); |
| 944 | tda18271_dump_std_item(qam_6, "qam 6"); |
| 945 | tda18271_dump_std_item(qam_8, "qam 8"); |
| 946 | |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | static int tda18271_update_std_map(struct dvb_frontend *fe, |
| 951 | struct tda18271_std_map *map) |
| 952 | { |
| 953 | struct tda18271_priv *priv = fe->tuner_priv; |
| 954 | struct tda18271_std_map *std = &priv->std; |
| 955 | |
| 956 | if (!map) |
| 957 | return -EINVAL; |
| 958 | |
| 959 | tda18271_update_std(atv_b, "atv b"); |
| 960 | tda18271_update_std(atv_dk, "atv dk"); |
| 961 | tda18271_update_std(atv_gh, "atv gh"); |
| 962 | tda18271_update_std(atv_i, "atv i"); |
| 963 | tda18271_update_std(atv_l, "atv l"); |
| 964 | tda18271_update_std(atv_lc, "atv l'"); |
| 965 | tda18271_update_std(atv_mn, "atv mn"); |
| 966 | tda18271_update_std(atsc_6, "atsc 6"); |
| 967 | tda18271_update_std(dvbt_6, "dvbt 6"); |
| 968 | tda18271_update_std(dvbt_7, "dvbt 7"); |
| 969 | tda18271_update_std(dvbt_8, "dvbt 8"); |
| 970 | tda18271_update_std(qam_6, "qam 6"); |
| 971 | tda18271_update_std(qam_8, "qam 8"); |
| 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
Michael Krufky | 49e7aaf | 2007-12-24 04:15:20 -0300 | [diff] [blame] | 976 | static int tda18271_get_id(struct dvb_frontend *fe) |
| 977 | { |
| 978 | struct tda18271_priv *priv = fe->tuner_priv; |
| 979 | unsigned char *regs = priv->tda18271_regs; |
| 980 | char *name; |
| 981 | int ret = 0; |
| 982 | |
| 983 | tda18271_read_regs(fe); |
| 984 | |
| 985 | switch (regs[R_ID] & 0x7f) { |
| 986 | case 3: |
| 987 | name = "TDA18271HD/C1"; |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 988 | priv->id = TDA18271HDC1; |
| 989 | priv->tune = tda18271c1_tune; |
Michael Krufky | 49e7aaf | 2007-12-24 04:15:20 -0300 | [diff] [blame] | 990 | break; |
| 991 | case 4: |
| 992 | name = "TDA18271HD/C2"; |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 993 | priv->id = TDA18271HDC2; |
| 994 | priv->tune = tda18271c2_tune; |
Michael Krufky | 49e7aaf | 2007-12-24 04:15:20 -0300 | [diff] [blame] | 995 | break; |
| 996 | default: |
| 997 | name = "Unknown device"; |
| 998 | ret = -EINVAL; |
| 999 | break; |
| 1000 | } |
| 1001 | |
Michael Krufky | 182519f | 2007-12-25 15:10:11 -0300 | [diff] [blame] | 1002 | tda_info("%s detected @ %d-%04x%s\n", name, |
Michael Krufky | 49e7aaf | 2007-12-24 04:15:20 -0300 | [diff] [blame] | 1003 | i2c_adapter_id(priv->i2c_adap), priv->i2c_addr, |
| 1004 | (0 == ret) ? "" : ", device not supported."); |
| 1005 | |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1009 | static struct dvb_tuner_ops tda18271_tuner_ops = { |
| 1010 | .info = { |
| 1011 | .name = "NXP TDA18271HD", |
| 1012 | .frequency_min = 45000000, |
| 1013 | .frequency_max = 864000000, |
| 1014 | .frequency_step = 62500 |
| 1015 | }, |
Michael Krufky | efce841 | 2007-12-01 17:40:16 -0300 | [diff] [blame] | 1016 | .init = tda18271_init, |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1017 | .set_params = tda18271_set_params, |
| 1018 | .set_analog_params = tda18271_set_analog_params, |
| 1019 | .release = tda18271_release, |
| 1020 | .get_frequency = tda18271_get_frequency, |
| 1021 | .get_bandwidth = tda18271_get_bandwidth, |
| 1022 | }; |
| 1023 | |
| 1024 | struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr, |
Michael Krufky | e435f95 | 2007-12-09 22:23:30 -0300 | [diff] [blame] | 1025 | struct i2c_adapter *i2c, |
Michael Krufky | f21e0d7 | 2008-01-02 03:01:54 -0300 | [diff] [blame] | 1026 | struct tda18271_config *cfg) |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1027 | { |
| 1028 | struct tda18271_priv *priv = NULL; |
| 1029 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1030 | priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL); |
| 1031 | if (priv == NULL) |
| 1032 | return NULL; |
| 1033 | |
| 1034 | priv->i2c_addr = addr; |
| 1035 | priv->i2c_adap = i2c; |
Michael Krufky | f21e0d7 | 2008-01-02 03:01:54 -0300 | [diff] [blame] | 1036 | priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO; |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 1037 | priv->cal_initialized = false; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1038 | |
Michael Krufky | 49e7aaf | 2007-12-24 04:15:20 -0300 | [diff] [blame] | 1039 | fe->tuner_priv = priv; |
| 1040 | |
| 1041 | if (tda18271_get_id(fe) < 0) |
| 1042 | goto fail; |
| 1043 | |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 1044 | if (tda18271_assign_map_layout(fe) < 0) |
| 1045 | goto fail; |
| 1046 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1047 | memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops, |
| 1048 | sizeof(struct dvb_tuner_ops)); |
| 1049 | |
Michael Krufky | f21e0d7 | 2008-01-02 03:01:54 -0300 | [diff] [blame] | 1050 | /* override default std map with values in config struct */ |
| 1051 | if ((cfg) && (cfg->std_map)) |
| 1052 | tda18271_update_std_map(fe, cfg->std_map); |
| 1053 | |
| 1054 | if (tda18271_debug & DBG_MAP) |
| 1055 | tda18271_dump_std_map(fe); |
| 1056 | |
Michael Krufky | efce841 | 2007-12-01 17:40:16 -0300 | [diff] [blame] | 1057 | tda18271_init_regs(fe); |
| 1058 | |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1059 | return fe; |
Michael Krufky | 49e7aaf | 2007-12-24 04:15:20 -0300 | [diff] [blame] | 1060 | fail: |
| 1061 | tda18271_release(fe); |
| 1062 | return NULL; |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1063 | } |
| 1064 | EXPORT_SYMBOL_GPL(tda18271_attach); |
| 1065 | MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver"); |
| 1066 | MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>"); |
| 1067 | MODULE_LICENSE("GPL"); |
Michael Krufky | 255b511 | 2008-01-01 22:52:09 -0300 | [diff] [blame] | 1068 | MODULE_VERSION("0.2"); |
Michael Krufky | 5bea1cd | 2007-10-22 09:56:38 -0300 | [diff] [blame] | 1069 | |
| 1070 | /* |
| 1071 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 1072 | * --------------------------------------------------------------------------- |
| 1073 | * Local variables: |
| 1074 | * c-basic-offset: 8 |
| 1075 | * End: |
| 1076 | */ |