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