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