blob: 46c080089eb02a1875960c463be77d84b4d0cbf2 [file] [log] [blame]
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001/*
Michael Krufky6ca04de2007-11-23 16:52:15 -03002 tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
Michael Krufky5bea1cd2007-10-22 09:56:38 -03003
Michael Krufky59067f72008-01-02 01:58:26 -03004 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
Michael Krufky5bea1cd2007-10-22 09:56:38 -03005
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 Krufky5bea1cd2007-10-22 09:56:38 -030021#include <linux/delay.h>
22#include <linux/videodev2.h>
Michael Krufky6ca04de2007-11-23 16:52:15 -030023#include "tda18271-priv.h"
Michael Krufky5bea1cd2007-10-22 09:56:38 -030024
Michael Krufkyb5f3e1e2007-12-02 16:36:05 -030025int tda18271_debug;
Michael Krufky54465b02007-11-23 18:14:53 -030026module_param_named(debug, tda18271_debug, int, 0644);
Michael Krufky0e1fab92008-01-03 01:40:47 -030027MODULE_PARM_DESC(debug, "set debug level "
Michael Krufkycf04d292008-01-09 00:34:30 -030028 "(info=1, map=2, reg=4, adv=8, cal=16 (or-able))");
Michael Krufky5bea1cd2007-10-22 09:56:38 -030029
Michael Krufkybc835d82008-01-14 11:10:54 -030030static int tda18271_cal_on_startup;
Michael Krufky0f962512008-01-13 22:01:07 -030031module_param_named(cal, tda18271_cal_on_startup, int, 0644);
32MODULE_PARM_DESC(cal, "perform RF tracking filter calibration on startup");
33
Michael Krufkya4f263b2008-01-06 15:52:56 -030034static DEFINE_MUTEX(tda18271_list_mutex);
Michael Krufkyf9e315a2008-04-22 14:41:54 -030035static LIST_HEAD(hybrid_tuner_instance_list);
Michael Krufkya4f263b2008-01-06 15:52:56 -030036
Michael Krufky5bea1cd2007-10-22 09:56:38 -030037/*---------------------------------------------------------------------*/
38
Michael Krufky868f5cc2008-04-22 14:46:23 -030039static inline int charge_pump_source(struct dvb_frontend *fe, int force)
40{
41 struct tda18271_priv *priv = fe->tuner_priv;
42 return tda18271_charge_pump_source(fe,
43 (priv->role == TDA18271_SLAVE) ?
44 TDA18271_CAL_PLL :
45 TDA18271_MAIN_PLL, force);
46}
47
Michael Krufky255b5112008-01-01 22:52:09 -030048static int tda18271_channel_configuration(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -030049 struct tda18271_std_map_item *map,
50 u32 freq, u32 bw)
Michael Krufky255b5112008-01-01 22:52:09 -030051{
52 struct tda18271_priv *priv = fe->tuner_priv;
53 unsigned char *regs = priv->tda18271_regs;
54 u32 N;
55
56 /* update TV broadcast parameters */
57
58 /* set standard */
59 regs[R_EP3] &= ~0x1f; /* clear std bits */
Michael Krufky7f7203d2008-04-22 14:46:06 -030060 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
Michael Krufky255b5112008-01-01 22:52:09 -030061
Michael Krufky40194b22008-04-22 14:46:22 -030062 /* set rfagc to high speed mode */
63 regs[R_EP3] &= ~0x04;
64
Michael Krufky255b5112008-01-01 22:52:09 -030065 /* set cal mode to normal */
66 regs[R_EP4] &= ~0x03;
67
68 /* update IF output level & IF notch frequency */
69 regs[R_EP4] &= ~0x1c; /* clear if level bits */
Michael Krufky14c74b22008-04-22 14:46:21 -030070 regs[R_EP4] |= (map->if_lvl << 2);
Michael Krufky255b5112008-01-01 22:52:09 -030071
72 switch (priv->mode) {
73 case TDA18271_ANALOG:
74 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
75 break;
76 case TDA18271_DIGITAL:
Michael Krufky255b5112008-01-01 22:52:09 -030077 regs[R_MPD] |= 0x80; /* IF notch = 1 */
78 break;
79 }
Michael Krufkyc353f422008-01-08 10:38:10 -030080
Michael Krufkyc293d0a2008-04-22 14:46:06 -030081 /* update FM_RFn */
82 regs[R_EP4] &= ~0x80;
83 regs[R_EP4] |= map->fm_rfn << 7;
Michael Krufky255b5112008-01-01 22:52:09 -030084
Michael Krufkyc0dc0c12008-04-22 14:46:22 -030085 /* update rf top / if top */
86 regs[R_EB22] = 0x00;
87 regs[R_EB22] |= map->rfagc_top;
Michael Krufky255b5112008-01-01 22:52:09 -030088 tda18271_write_regs(fe, R_EB22, 1);
89
90 /* --------------------------------------------------------------- */
91
92 /* disable Power Level Indicator */
93 regs[R_EP1] |= 0x40;
94
95 /* frequency dependent parameters */
96
97 tda18271_calc_ir_measure(fe, &freq);
98
99 tda18271_calc_bp_filter(fe, &freq);
100
101 tda18271_calc_rf_band(fe, &freq);
102
103 tda18271_calc_gain_taper(fe, &freq);
104
105 /* --------------------------------------------------------------- */
106
107 /* dual tuner and agc1 extra configuration */
108
Michael Krufky868f5cc2008-04-22 14:46:23 -0300109 switch (priv->role) {
110 case TDA18271_MASTER:
111 regs[R_EB1] |= 0x04; /* main vco */
112 break;
113 case TDA18271_SLAVE:
114 regs[R_EB1] &= ~0x04; /* cal vco */
115 break;
116 }
Michael Krufky255b5112008-01-01 22:52:09 -0300117
118 /* agc1 always active */
119 regs[R_EB1] &= ~0x02;
120
121 /* agc1 has priority on agc2 */
122 regs[R_EB1] &= ~0x01;
123
124 tda18271_write_regs(fe, R_EB1, 1);
125
126 /* --------------------------------------------------------------- */
127
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300128 N = map->if_freq * 1000 + freq;
Michael Krufky255b5112008-01-01 22:52:09 -0300129
Michael Krufky868f5cc2008-04-22 14:46:23 -0300130 switch (priv->role) {
131 case TDA18271_MASTER:
132 tda18271_calc_main_pll(fe, N);
133 tda18271_write_regs(fe, R_MPD, 4);
134 break;
135 case TDA18271_SLAVE:
136 tda18271_calc_cal_pll(fe, N);
137 tda18271_write_regs(fe, R_CPD, 4);
138
139 regs[R_MPD] = regs[R_CPD] & 0x7f;
140 tda18271_write_regs(fe, R_MPD, 1);
141 break;
142 }
Michael Krufky255b5112008-01-01 22:52:09 -0300143
144 tda18271_write_regs(fe, R_TM, 7);
145
Michael Krufky868f5cc2008-04-22 14:46:23 -0300146 /* force charge pump source */
147 charge_pump_source(fe, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300148
149 msleep(1);
150
Michael Krufky868f5cc2008-04-22 14:46:23 -0300151 /* return pll to normal operation */
152 charge_pump_source(fe, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300153
Michael Krufky40194b22008-04-22 14:46:22 -0300154 msleep(20);
155
156 /* set rfagc to normal speed mode */
157 if (map->fm_rfn)
158 regs[R_EP3] &= ~0x04;
159 else
160 regs[R_EP3] |= 0x04;
161 tda18271_write_regs(fe, R_EP3, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300162
163 return 0;
164}
165
166static int tda18271_read_thermometer(struct dvb_frontend *fe)
167{
168 struct tda18271_priv *priv = fe->tuner_priv;
169 unsigned char *regs = priv->tda18271_regs;
170 int tm;
171
172 /* switch thermometer on */
173 regs[R_TM] |= 0x10;
174 tda18271_write_regs(fe, R_TM, 1);
175
176 /* read thermometer info */
177 tda18271_read_regs(fe);
178
179 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
180 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
181
182 if ((regs[R_TM] & 0x20) == 0x20)
183 regs[R_TM] &= ~0x20;
184 else
185 regs[R_TM] |= 0x20;
186
187 tda18271_write_regs(fe, R_TM, 1);
188
189 msleep(10); /* temperature sensing */
190
191 /* read thermometer info */
192 tda18271_read_regs(fe);
193 }
194
195 tm = tda18271_lookup_thermometer(fe);
196
197 /* switch thermometer off */
198 regs[R_TM] &= ~0x10;
199 tda18271_write_regs(fe, R_TM, 1);
200
201 /* set CAL mode to normal */
202 regs[R_EP4] &= ~0x03;
203 tda18271_write_regs(fe, R_EP4, 1);
204
205 return tm;
206}
207
Michael Krufky12afe372008-04-22 14:42:07 -0300208/* ------------------------------------------------------------------ */
209
Michael Krufkyd1c53422008-04-22 14:42:07 -0300210static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
211 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300212{
213 struct tda18271_priv *priv = fe->tuner_priv;
214 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
215 unsigned char *regs = priv->tda18271_regs;
216 int tm_current, rfcal_comp, approx, i;
217 u8 dc_over_dt, rf_tab;
218
219 /* power up */
Michael Krufky518d8732008-01-13 17:01:01 -0300220 tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300221
222 /* read die current temperature */
223 tm_current = tda18271_read_thermometer(fe);
224
225 /* frequency dependent parameters */
226
227 tda18271_calc_rf_cal(fe, &freq);
228 rf_tab = regs[R_EB14];
229
230 i = tda18271_lookup_rf_band(fe, &freq, NULL);
231 if (i < 0)
232 return -EINVAL;
233
234 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
235 approx = map[i].rf_a1 *
236 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
237 } else {
238 approx = map[i].rf_a2 *
239 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
240 }
241
242 if (approx < 0)
243 approx = 0;
244 if (approx > 255)
245 approx = 255;
246
247 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
248
249 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300250 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300251
252 regs[R_EB14] = approx + rfcal_comp;
253 tda18271_write_regs(fe, R_EB14, 1);
254
255 return 0;
256}
257
258static int tda18271_por(struct dvb_frontend *fe)
259{
260 struct tda18271_priv *priv = fe->tuner_priv;
261 unsigned char *regs = priv->tda18271_regs;
262
263 /* power up detector 1 */
264 regs[R_EB12] &= ~0x20;
265 tda18271_write_regs(fe, R_EB12, 1);
266
267 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
268 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
269 tda18271_write_regs(fe, R_EB18, 1);
270
271 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
272
273 /* POR mode */
Michael Krufky518d8732008-01-13 17:01:01 -0300274 tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300275
276 /* disable 1.5 MHz low pass filter */
277 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
278 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
279 tda18271_write_regs(fe, R_EB21, 3);
280
281 return 0;
282}
283
284static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
285{
286 struct tda18271_priv *priv = fe->tuner_priv;
287 unsigned char *regs = priv->tda18271_regs;
288 u32 N;
289
290 /* set CAL mode to normal */
291 regs[R_EP4] &= ~0x03;
292 tda18271_write_regs(fe, R_EP4, 1);
293
294 /* switch off agc1 */
295 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
296
297 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
298 tda18271_write_regs(fe, R_EB18, 1);
299
300 /* frequency dependent parameters */
301
302 tda18271_calc_bp_filter(fe, &freq);
303 tda18271_calc_gain_taper(fe, &freq);
304 tda18271_calc_rf_band(fe, &freq);
305 tda18271_calc_km(fe, &freq);
306
307 tda18271_write_regs(fe, R_EP1, 3);
308 tda18271_write_regs(fe, R_EB13, 1);
309
310 /* main pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300311 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300312
313 /* cal pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300314 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300315
316 /* force dcdc converter to 0 V */
317 regs[R_EB14] = 0x00;
318 tda18271_write_regs(fe, R_EB14, 1);
319
320 /* disable plls lock */
321 regs[R_EB20] &= ~0x20;
322 tda18271_write_regs(fe, R_EB20, 1);
323
324 /* set CAL mode to RF tracking filter calibration */
325 regs[R_EP4] |= 0x03;
326 tda18271_write_regs(fe, R_EP4, 2);
327
328 /* --------------------------------------------------------------- */
329
330 /* set the internal calibration signal */
331 N = freq;
332
Michael Krufkyae07d042008-04-22 14:46:21 -0300333 tda18271_calc_cal_pll(fe, N);
334 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300335
336 /* downconvert internal calibration */
337 N += 1000000;
338
339 tda18271_calc_main_pll(fe, N);
340 tda18271_write_regs(fe, R_MPD, 4);
341
342 msleep(5);
343
344 tda18271_write_regs(fe, R_EP2, 1);
345 tda18271_write_regs(fe, R_EP1, 1);
346 tda18271_write_regs(fe, R_EP2, 1);
347 tda18271_write_regs(fe, R_EP1, 1);
348
349 /* --------------------------------------------------------------- */
350
351 /* normal operation for the main pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300352 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300353
354 /* normal operation for the cal pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300355 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300356
Michael Krufkyae07d042008-04-22 14:46:21 -0300357 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300358
359 /* launch the rf tracking filters calibration */
360 regs[R_EB20] |= 0x20;
361 tda18271_write_regs(fe, R_EB20, 1);
362
363 msleep(60); /* calibration */
364
365 /* --------------------------------------------------------------- */
366
367 /* set CAL mode to normal */
368 regs[R_EP4] &= ~0x03;
369
370 /* switch on agc1 */
371 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
372
373 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
374 tda18271_write_regs(fe, R_EB18, 1);
375
376 tda18271_write_regs(fe, R_EP3, 2);
377
378 /* synchronization */
379 tda18271_write_regs(fe, R_EP1, 1);
380
381 /* get calibration result */
382 tda18271_read_extended(fe);
383
384 return regs[R_EB14];
385}
386
387static int tda18271_powerscan(struct dvb_frontend *fe,
388 u32 *freq_in, u32 *freq_out)
389{
390 struct tda18271_priv *priv = fe->tuner_priv;
391 unsigned char *regs = priv->tda18271_regs;
392 int sgn, bcal, count, wait;
393 u8 cid_target;
394 u16 count_limit;
395 u32 freq;
396
397 freq = *freq_in;
398
399 tda18271_calc_rf_band(fe, &freq);
400 tda18271_calc_rf_cal(fe, &freq);
401 tda18271_calc_gain_taper(fe, &freq);
402 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
403
404 tda18271_write_regs(fe, R_EP2, 1);
405 tda18271_write_regs(fe, R_EB14, 1);
406
407 /* downconvert frequency */
408 freq += 1000000;
409
410 tda18271_calc_main_pll(fe, freq);
411 tda18271_write_regs(fe, R_MPD, 4);
412
413 msleep(5); /* pll locking */
414
415 /* detection mode */
416 regs[R_EP4] &= ~0x03;
417 regs[R_EP4] |= 0x01;
418 tda18271_write_regs(fe, R_EP4, 1);
419
420 /* launch power detection measurement */
421 tda18271_write_regs(fe, R_EP2, 1);
422
423 /* read power detection info, stored in EB10 */
424 tda18271_read_extended(fe);
425
426 /* algorithm initialization */
427 sgn = 1;
428 *freq_out = *freq_in;
429 bcal = 0;
430 count = 0;
431 wait = false;
432
433 while ((regs[R_EB10] & 0x3f) < cid_target) {
434 /* downconvert updated freq to 1 MHz */
435 freq = *freq_in + (sgn * count) + 1000000;
436
437 tda18271_calc_main_pll(fe, freq);
438 tda18271_write_regs(fe, R_MPD, 4);
439
440 if (wait) {
441 msleep(5); /* pll locking */
442 wait = false;
443 } else
444 udelay(100); /* pll locking */
445
446 /* launch power detection measurement */
447 tda18271_write_regs(fe, R_EP2, 1);
448
449 /* read power detection info, stored in EB10 */
450 tda18271_read_extended(fe);
451
452 count += 200;
453
Michael Krufkye7809a02008-04-22 14:46:22 -0300454 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300455 continue;
456
457 if (sgn <= 0)
458 break;
459
460 sgn = -1 * sgn;
461 count = 200;
462 wait = true;
463 }
464
465 if ((regs[R_EB10] & 0x3f) >= cid_target) {
466 bcal = 1;
467 *freq_out = freq - 1000000;
468 } else
469 bcal = 0;
470
Michael Krufkycf04d292008-01-09 00:34:30 -0300471 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300472 bcal, *freq_in, *freq_out, freq);
473
474 return bcal;
475}
476
477static int tda18271_powerscan_init(struct dvb_frontend *fe)
478{
479 struct tda18271_priv *priv = fe->tuner_priv;
480 unsigned char *regs = priv->tda18271_regs;
481
482 /* set standard to digital */
483 regs[R_EP3] &= ~0x1f; /* clear std bits */
484 regs[R_EP3] |= 0x12;
485
486 /* set cal mode to normal */
487 regs[R_EP4] &= ~0x03;
488
489 /* update IF output level & IF notch frequency */
490 regs[R_EP4] &= ~0x1c; /* clear if level bits */
491
492 tda18271_write_regs(fe, R_EP3, 2);
493
494 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
495 tda18271_write_regs(fe, R_EB18, 1);
496
497 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
498
499 /* 1.5 MHz low pass filter */
500 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
501 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
502
503 tda18271_write_regs(fe, R_EB21, 3);
504
505 return 0;
506}
507
508static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
509{
510 struct tda18271_priv *priv = fe->tuner_priv;
511 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
512 unsigned char *regs = priv->tda18271_regs;
513 int bcal, rf, i;
514#define RF1 0
515#define RF2 1
516#define RF3 2
517 u32 rf_default[3];
518 u32 rf_freq[3];
519 u8 prog_cal[3];
520 u8 prog_tab[3];
521
522 i = tda18271_lookup_rf_band(fe, &freq, NULL);
523
524 if (i < 0)
525 return i;
526
527 rf_default[RF1] = 1000 * map[i].rf1_def;
528 rf_default[RF2] = 1000 * map[i].rf2_def;
529 rf_default[RF3] = 1000 * map[i].rf3_def;
530
531 for (rf = RF1; rf <= RF3; rf++) {
532 if (0 == rf_default[rf])
533 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300534 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300535
536 /* look for optimized calibration frequency */
537 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
538
539 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
540 prog_tab[rf] = regs[R_EB14];
541
542 if (1 == bcal)
543 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
544 else
545 prog_cal[rf] = prog_tab[rf];
546
547 switch (rf) {
548 case RF1:
549 map[i].rf_a1 = 0;
550 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
551 map[i].rf1 = rf_freq[RF1] / 1000;
552 break;
553 case RF2:
554 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
555 prog_cal[RF1] + prog_tab[RF1]) /
556 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
557 map[i].rf2 = rf_freq[RF2] / 1000;
558 break;
559 case RF3:
560 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
561 prog_cal[RF2] + prog_tab[RF2]) /
562 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
563 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
564 map[i].rf3 = rf_freq[RF3] / 1000;
565 break;
566 default:
567 BUG();
568 }
569 }
570
571 return 0;
572}
573
Michael Krufky09f83c42008-01-05 20:00:09 -0300574static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300575{
576 struct tda18271_priv *priv = fe->tuner_priv;
577 unsigned int i;
578
579 tda_info("tda18271: performing RF tracking filter calibration\n");
580
581 /* wait for die temperature stabilization */
582 msleep(200);
583
584 tda18271_powerscan_init(fe);
585
586 /* rf band calibration */
587 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
588 tda18271_rf_tracking_filters_init(fe, 1000 *
589 priv->rf_cal_state[i].rfmax);
590
Michael Krufky09f83c42008-01-05 20:00:09 -0300591 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300592
593 return 0;
594}
595
596/* ------------------------------------------------------------------ */
597
Michael Krufky12afe372008-04-22 14:42:07 -0300598static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300599{
600 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300601 unsigned char *regs = priv->tda18271_regs;
602
603 /* test RF_CAL_OK to see if we need init */
604 if ((regs[R_EP1] & 0x10) == 0)
605 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300606
607 if (priv->cal_initialized)
608 return 0;
609
Michael Krufky09f83c42008-01-05 20:00:09 -0300610 tda18271_calc_rf_filter_curve(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300611
612 tda18271_por(fe);
613
Michael Krufky6bfa6652008-01-07 00:51:48 -0300614 tda_info("tda18271: RF tracking filter calibration complete\n");
615
Michael Krufky255b5112008-01-01 22:52:09 -0300616 priv->cal_initialized = true;
617
618 return 0;
619}
620
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300621static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
622 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300623{
624 struct tda18271_priv *priv = fe->tuner_priv;
625 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300626 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300627
Michael Krufky255b5112008-01-01 22:52:09 -0300628 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300629 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300630 tda18271_write_regs(fe, R_EP1, 1);
631
632 regs[R_EB4] &= 0x07;
633 regs[R_EB4] |= 0x60;
634 tda18271_write_regs(fe, R_EB4, 1);
635
636 regs[R_EB7] = 0x60;
637 tda18271_write_regs(fe, R_EB7, 1);
638
639 regs[R_EB14] = 0x00;
640 tda18271_write_regs(fe, R_EB14, 1);
641
642 regs[R_EB20] = 0xcc;
643 tda18271_write_regs(fe, R_EB20, 1);
644
Michael Krufky255b5112008-01-01 22:52:09 -0300645 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300646 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300647
Michael Krufky255b5112008-01-01 22:52:09 -0300648 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300649
650 switch (priv->mode) {
651 case TDA18271_ANALOG:
652 N = freq - 1250000;
653 break;
654 case TDA18271_DIGITAL:
655 N = freq + bw / 2;
656 break;
657 }
658
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300659 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300660
Michael Krufky255b5112008-01-01 22:52:09 -0300661 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300662
663 switch (priv->mode) {
664 case TDA18271_ANALOG:
665 N = freq - 250000;
666 break;
667 case TDA18271_DIGITAL:
668 N = freq + bw / 2 + 1000000;
669 break;
670 }
671
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300672 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300673
674 tda18271_write_regs(fe, R_EP3, 11);
675 msleep(5); /* RF tracking filter calibration initialization */
676
Michael Krufky255b5112008-01-01 22:52:09 -0300677 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300678 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300679 tda18271_write_regs(fe, R_EB13, 1);
680
Michael Krufky255b5112008-01-01 22:52:09 -0300681 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300682 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300683
Michael Krufky255b5112008-01-01 22:52:09 -0300684 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300685 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300686
687 tda18271_write_regs(fe, R_EP2, 1);
688 tda18271_write_regs(fe, R_EP1, 1);
689 tda18271_write_regs(fe, R_EP2, 1);
690 tda18271_write_regs(fe, R_EP1, 1);
691
692 regs[R_EB4] &= 0x07;
693 regs[R_EB4] |= 0x40;
694 tda18271_write_regs(fe, R_EB4, 1);
695
696 regs[R_EB7] = 0x40;
697 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300698 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300699
700 regs[R_EB20] = 0xec;
701 tda18271_write_regs(fe, R_EB20, 1);
702 msleep(60); /* RF tracking filter calibration completion */
703
704 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
705 tda18271_write_regs(fe, R_EP4, 1);
706
707 tda18271_write_regs(fe, R_EP1, 1);
708
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300709 /* RF tracking filter correction for VHF_Low band */
710 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300711 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300712
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300713 return 0;
714}
715
Michael Krufkyd1c53422008-04-22 14:42:07 -0300716/* ------------------------------------------------------------------ */
717
Michael Krufky12afe372008-04-22 14:42:07 -0300718static int tda18271_ir_cal_init(struct dvb_frontend *fe)
719{
720 struct tda18271_priv *priv = fe->tuner_priv;
721 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300722 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300723
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300724 ret = tda18271_read_regs(fe);
725 if (ret < 0)
726 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300727
728 /* test IR_CAL_OK to see if we need init */
729 if ((regs[R_EP1] & 0x08) == 0)
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300730 ret = tda18271_init_regs(fe);
731fail:
732 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300733}
734
735static int tda18271_init(struct dvb_frontend *fe)
736{
737 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300738 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300739
740 mutex_lock(&priv->lock);
741
742 /* power up */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300743 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
744 if (ret < 0)
745 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300746
747 /* initialization */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300748 ret = tda18271_ir_cal_init(fe);
749 if (ret < 0)
750 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300751
752 if (priv->id == TDA18271HDC2)
753 tda18271c2_rf_cal_init(fe);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300754fail:
Michael Krufky12afe372008-04-22 14:42:07 -0300755 mutex_unlock(&priv->lock);
756
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300757 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300758}
759
Michael Krufkyd1c53422008-04-22 14:42:07 -0300760static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300761 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300762{
763 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300764 int ret;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300765
Michael Krufky7f7203d2008-04-22 14:46:06 -0300766 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
767 freq, map->if_freq, bw, map->agc_mode, map->std);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300768
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300769 ret = tda18271_init(fe);
770 if (ret < 0)
771 goto fail;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300772
773 mutex_lock(&priv->lock);
774
Michael Krufkyd1c53422008-04-22 14:42:07 -0300775 switch (priv->id) {
776 case TDA18271HDC1:
777 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
778 break;
779 case TDA18271HDC2:
780 tda18271c2_rf_tracking_filters_correction(fe, freq);
781 break;
782 }
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300783 tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300784
Michael Krufky8d316bf2008-01-06 15:31:35 -0300785 mutex_unlock(&priv->lock);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300786fail:
787 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300788}
789
790/* ------------------------------------------------------------------ */
791
792static int tda18271_set_params(struct dvb_frontend *fe,
793 struct dvb_frontend_parameters *params)
794{
795 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300796 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300797 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300798 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300799 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300800
801 priv->mode = TDA18271_DIGITAL;
802
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300803 if (fe->ops.info.type == FE_ATSC) {
804 switch (params->u.vsb.modulation) {
805 case VSB_8:
806 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300807 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300808 break;
809 case QAM_64:
810 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300811 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300812 break;
813 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300814 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300815 return -EINVAL;
816 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300817#if 0
818 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300819 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300820#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300821 bw = 6000000;
822 } else if (fe->ops.info.type == FE_OFDM) {
823 switch (params->u.ofdm.bandwidth) {
824 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300825 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300826 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300827 break;
828 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300829 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300830 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300831 break;
832 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300833 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300834 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300835 break;
836 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300837 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300838 return -EINVAL;
839 }
840 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300841 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300842 return -EINVAL;
843 }
844
Michael Krufkyed736832008-01-19 17:41:04 -0300845 /* When tuning digital, the analog demod must be tri-stated */
846 if (fe->ops.analog_ops.standby)
847 fe->ops.analog_ops.standby(fe);
848
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300849 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300850
851 if (ret < 0)
852 goto fail;
853
854 priv->frequency = freq;
855 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
856 params->u.ofdm.bandwidth : 0;
857fail:
858 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300859}
860
861static int tda18271_set_analog_params(struct dvb_frontend *fe,
862 struct analog_parameters *params)
863{
864 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300865 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300866 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300867 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300868 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300869 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300870
871 priv->mode = TDA18271_ANALOG;
872
Michael Krufkyc353f422008-01-08 10:38:10 -0300873 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc353f422008-01-08 10:38:10 -0300874 freq = freq / 1000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300875 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -0300876 mode = "fm";
877 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300878 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300879 mode = "MN";
880 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300881 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300882 mode = "B";
883 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300884 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300885 mode = "GH";
886 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300887 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300888 mode = "I";
889 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300890 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300891 mode = "DK";
892 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300893 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300894 mode = "L";
895 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300896 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -0300897 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300898 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300899 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300900 mode = "xx";
901 }
902
Michael Krufky182519f2007-12-25 15:10:11 -0300903 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300904
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300905 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300906
907 if (ret < 0)
908 goto fail;
909
910 priv->frequency = freq;
911 priv->bandwidth = 0;
912fail:
913 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300914}
915
Michael Krufky518d8732008-01-13 17:01:01 -0300916static int tda18271_sleep(struct dvb_frontend *fe)
917{
918 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300919 int ret;
Michael Krufky518d8732008-01-13 17:01:01 -0300920
921 mutex_lock(&priv->lock);
922
923 /* standby mode w/ slave tuner output
924 * & loop thru & xtal oscillator on */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300925 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky518d8732008-01-13 17:01:01 -0300926
927 mutex_unlock(&priv->lock);
928
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300929 return ret;
Michael Krufky518d8732008-01-13 17:01:01 -0300930}
931
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300932static int tda18271_release(struct dvb_frontend *fe)
933{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300934 struct tda18271_priv *priv = fe->tuner_priv;
935
936 mutex_lock(&tda18271_list_mutex);
937
Michael Krufkyf9e315a2008-04-22 14:41:54 -0300938 if (priv)
939 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -0300940
Michael Krufkya4f263b2008-01-06 15:52:56 -0300941 mutex_unlock(&tda18271_list_mutex);
942
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300943 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -0300944
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300945 return 0;
946}
947
948static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
949{
950 struct tda18271_priv *priv = fe->tuner_priv;
951 *frequency = priv->frequency;
952 return 0;
953}
954
955static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
956{
957 struct tda18271_priv *priv = fe->tuner_priv;
958 *bandwidth = priv->bandwidth;
959 return 0;
960}
961
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300962/* ------------------------------------------------------------------ */
963
964#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -0300965 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -0300966 map->std_cfg.agc_mode + map->std_cfg.std + \
967 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300968 tda_dbg("Using custom std config for %s\n", name); \
969 memcpy(&std->std_cfg, &map->std_cfg, \
970 sizeof(struct tda18271_std_map_item)); \
971 } } while (0)
972
973#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -0300974 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
975 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -0300976 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -0300977 std->std_cfg.agc_mode, std->std_cfg.std, \
978 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300979 } while (0)
980
981static int tda18271_dump_std_map(struct dvb_frontend *fe)
982{
983 struct tda18271_priv *priv = fe->tuner_priv;
984 struct tda18271_std_map *std = &priv->std;
985
986 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -0300987 tda18271_dump_std_item(fm_radio, " fm ");
988 tda18271_dump_std_item(atv_b, "atv b ");
989 tda18271_dump_std_item(atv_dk, "atv dk");
990 tda18271_dump_std_item(atv_gh, "atv gh");
991 tda18271_dump_std_item(atv_i, "atv i ");
992 tda18271_dump_std_item(atv_l, "atv l ");
993 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300994 tda18271_dump_std_item(atv_mn, "atv mn");
995 tda18271_dump_std_item(atsc_6, "atsc 6");
996 tda18271_dump_std_item(dvbt_6, "dvbt 6");
997 tda18271_dump_std_item(dvbt_7, "dvbt 7");
998 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -0300999 tda18271_dump_std_item(qam_6, "qam 6 ");
1000 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001001
1002 return 0;
1003}
1004
1005static int tda18271_update_std_map(struct dvb_frontend *fe,
1006 struct tda18271_std_map *map)
1007{
1008 struct tda18271_priv *priv = fe->tuner_priv;
1009 struct tda18271_std_map *std = &priv->std;
1010
1011 if (!map)
1012 return -EINVAL;
1013
Michael Krufkyc353f422008-01-08 10:38:10 -03001014 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001015 tda18271_update_std(atv_b, "atv b");
1016 tda18271_update_std(atv_dk, "atv dk");
1017 tda18271_update_std(atv_gh, "atv gh");
1018 tda18271_update_std(atv_i, "atv i");
1019 tda18271_update_std(atv_l, "atv l");
1020 tda18271_update_std(atv_lc, "atv l'");
1021 tda18271_update_std(atv_mn, "atv mn");
1022 tda18271_update_std(atsc_6, "atsc 6");
1023 tda18271_update_std(dvbt_6, "dvbt 6");
1024 tda18271_update_std(dvbt_7, "dvbt 7");
1025 tda18271_update_std(dvbt_8, "dvbt 8");
1026 tda18271_update_std(qam_6, "qam 6");
1027 tda18271_update_std(qam_8, "qam 8");
1028
1029 return 0;
1030}
1031
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001032static int tda18271_get_id(struct dvb_frontend *fe)
1033{
1034 struct tda18271_priv *priv = fe->tuner_priv;
1035 unsigned char *regs = priv->tda18271_regs;
1036 char *name;
1037 int ret = 0;
1038
Michael Krufky8d316bf2008-01-06 15:31:35 -03001039 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001040 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001041 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001042
1043 switch (regs[R_ID] & 0x7f) {
1044 case 3:
1045 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001046 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001047 break;
1048 case 4:
1049 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001050 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001051 break;
1052 default:
1053 name = "Unknown device";
1054 ret = -EINVAL;
1055 break;
1056 }
1057
Michael Krufky182519f2007-12-25 15:10:11 -03001058 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001059 i2c_adapter_id(priv->i2c_props.adap),
1060 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001061 (0 == ret) ? "" : ", device not supported.");
1062
1063 return ret;
1064}
1065
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001066static struct dvb_tuner_ops tda18271_tuner_ops = {
1067 .info = {
1068 .name = "NXP TDA18271HD",
1069 .frequency_min = 45000000,
1070 .frequency_max = 864000000,
1071 .frequency_step = 62500
1072 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001073 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001074 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001075 .set_params = tda18271_set_params,
1076 .set_analog_params = tda18271_set_analog_params,
1077 .release = tda18271_release,
1078 .get_frequency = tda18271_get_frequency,
1079 .get_bandwidth = tda18271_get_bandwidth,
1080};
1081
1082struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001083 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001084 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001085{
1086 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001087 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001088
Michael Krufkya4f263b2008-01-06 15:52:56 -03001089 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001090
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001091 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1092 hybrid_tuner_instance_list,
1093 i2c, addr, "tda18271");
1094 switch (instance) {
1095 case 0:
1096 goto fail;
1097 break;
1098 case 1:
1099 /* new tuner instance */
Michael Krufkya4f263b2008-01-06 15:52:56 -03001100 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
Michael Krufky868f5cc2008-04-22 14:46:23 -03001101 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001102 priv->cal_initialized = false;
1103 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001104
Michael Krufkya4f263b2008-01-06 15:52:56 -03001105 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001106
Michael Krufky55553092008-04-22 14:46:06 -03001107 if (cfg)
1108 priv->small_i2c = cfg->small_i2c;
1109
Michael Krufkya4f263b2008-01-06 15:52:56 -03001110 if (tda18271_get_id(fe) < 0)
1111 goto fail;
1112
1113 if (tda18271_assign_map_layout(fe) < 0)
1114 goto fail;
1115
1116 mutex_lock(&priv->lock);
1117 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001118
1119 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001120 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001121
Michael Krufkya4f263b2008-01-06 15:52:56 -03001122 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001123 break;
1124 default:
1125 /* existing tuner instance */
1126 fe->tuner_priv = priv;
1127
1128 /* allow dvb driver to override i2c gate setting */
1129 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1130 priv->gate = cfg->gate;
1131 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001132 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001133
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001134 /* override default std map with values in config struct */
1135 if ((cfg) && (cfg->std_map))
1136 tda18271_update_std_map(fe, cfg->std_map);
1137
Michael Krufkya4f263b2008-01-06 15:52:56 -03001138 mutex_unlock(&tda18271_list_mutex);
1139
1140 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1141 sizeof(struct dvb_tuner_ops));
1142
Michael Krufkyc7353722008-03-30 19:40:20 -03001143 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001144 tda18271_dump_std_map(fe);
1145
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001146 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001147fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001148 mutex_unlock(&tda18271_list_mutex);
1149
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001150 tda18271_release(fe);
1151 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001152}
1153EXPORT_SYMBOL_GPL(tda18271_attach);
1154MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1155MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1156MODULE_LICENSE("GPL");
Michael Krufky5ec96b02008-04-22 14:46:23 -03001157MODULE_VERSION("0.3");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001158
1159/*
1160 * Overrides for Emacs so that we follow Linus's tabbing style.
1161 * ---------------------------------------------------------------------------
1162 * Local variables:
1163 * c-basic-offset: 8
1164 * End:
1165 */