blob: b3b0425dbbed03ed740efb34edc80c86bd07beb3 [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 Krufky255b5112008-01-01 22:52:09 -030039static int tda18271_channel_configuration(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -030040 struct tda18271_std_map_item *map,
41 u32 freq, u32 bw)
Michael Krufky255b5112008-01-01 22:52:09 -030042{
43 struct tda18271_priv *priv = fe->tuner_priv;
44 unsigned char *regs = priv->tda18271_regs;
45 u32 N;
46
47 /* update TV broadcast parameters */
48
49 /* set standard */
50 regs[R_EP3] &= ~0x1f; /* clear std bits */
Michael Krufky7f7203d2008-04-22 14:46:06 -030051 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
Michael Krufky255b5112008-01-01 22:52:09 -030052
Michael Krufky40194b22008-04-22 14:46:22 -030053 /* set rfagc to high speed mode */
54 regs[R_EP3] &= ~0x04;
55
Michael Krufky255b5112008-01-01 22:52:09 -030056 /* set cal mode to normal */
57 regs[R_EP4] &= ~0x03;
58
59 /* update IF output level & IF notch frequency */
60 regs[R_EP4] &= ~0x1c; /* clear if level bits */
Michael Krufky14c74b22008-04-22 14:46:21 -030061 regs[R_EP4] |= (map->if_lvl << 2);
Michael Krufky255b5112008-01-01 22:52:09 -030062
63 switch (priv->mode) {
64 case TDA18271_ANALOG:
65 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
66 break;
67 case TDA18271_DIGITAL:
Michael Krufky255b5112008-01-01 22:52:09 -030068 regs[R_MPD] |= 0x80; /* IF notch = 1 */
69 break;
70 }
Michael Krufkyc353f422008-01-08 10:38:10 -030071
Michael Krufkyc293d0a2008-04-22 14:46:06 -030072 /* update FM_RFn */
73 regs[R_EP4] &= ~0x80;
74 regs[R_EP4] |= map->fm_rfn << 7;
Michael Krufky255b5112008-01-01 22:52:09 -030075
Michael Krufkyc0dc0c12008-04-22 14:46:22 -030076 /* update rf top / if top */
77 regs[R_EB22] = 0x00;
78 regs[R_EB22] |= map->rfagc_top;
Michael Krufky255b5112008-01-01 22:52:09 -030079 tda18271_write_regs(fe, R_EB22, 1);
80
81 /* --------------------------------------------------------------- */
82
83 /* disable Power Level Indicator */
84 regs[R_EP1] |= 0x40;
85
86 /* frequency dependent parameters */
87
88 tda18271_calc_ir_measure(fe, &freq);
89
90 tda18271_calc_bp_filter(fe, &freq);
91
92 tda18271_calc_rf_band(fe, &freq);
93
94 tda18271_calc_gain_taper(fe, &freq);
95
96 /* --------------------------------------------------------------- */
97
98 /* dual tuner and agc1 extra configuration */
99
100 /* main vco when Master, cal vco when slave */
101 regs[R_EB1] |= 0x04; /* FIXME: assumes master */
102
103 /* agc1 always active */
104 regs[R_EB1] &= ~0x02;
105
106 /* agc1 has priority on agc2 */
107 regs[R_EB1] &= ~0x01;
108
109 tda18271_write_regs(fe, R_EB1, 1);
110
111 /* --------------------------------------------------------------- */
112
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300113 N = map->if_freq * 1000 + freq;
Michael Krufky255b5112008-01-01 22:52:09 -0300114
115 /* FIXME: assumes master */
116 tda18271_calc_main_pll(fe, N);
117 tda18271_write_regs(fe, R_MPD, 4);
118
119 tda18271_write_regs(fe, R_TM, 7);
120
121 /* main pll charge pump source */
122 regs[R_EB4] |= 0x20;
123 tda18271_write_regs(fe, R_EB4, 1);
124
125 msleep(1);
126
127 /* normal operation for the main pll */
128 regs[R_EB4] &= ~0x20;
129 tda18271_write_regs(fe, R_EB4, 1);
130
Michael Krufky40194b22008-04-22 14:46:22 -0300131 msleep(20);
132
133 /* set rfagc to normal speed mode */
134 if (map->fm_rfn)
135 regs[R_EP3] &= ~0x04;
136 else
137 regs[R_EP3] |= 0x04;
138 tda18271_write_regs(fe, R_EP3, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300139
140 return 0;
141}
142
143static int tda18271_read_thermometer(struct dvb_frontend *fe)
144{
145 struct tda18271_priv *priv = fe->tuner_priv;
146 unsigned char *regs = priv->tda18271_regs;
147 int tm;
148
149 /* switch thermometer on */
150 regs[R_TM] |= 0x10;
151 tda18271_write_regs(fe, R_TM, 1);
152
153 /* read thermometer info */
154 tda18271_read_regs(fe);
155
156 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
157 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
158
159 if ((regs[R_TM] & 0x20) == 0x20)
160 regs[R_TM] &= ~0x20;
161 else
162 regs[R_TM] |= 0x20;
163
164 tda18271_write_regs(fe, R_TM, 1);
165
166 msleep(10); /* temperature sensing */
167
168 /* read thermometer info */
169 tda18271_read_regs(fe);
170 }
171
172 tm = tda18271_lookup_thermometer(fe);
173
174 /* switch thermometer off */
175 regs[R_TM] &= ~0x10;
176 tda18271_write_regs(fe, R_TM, 1);
177
178 /* set CAL mode to normal */
179 regs[R_EP4] &= ~0x03;
180 tda18271_write_regs(fe, R_EP4, 1);
181
182 return tm;
183}
184
Michael Krufky12afe372008-04-22 14:42:07 -0300185/* ------------------------------------------------------------------ */
186
Michael Krufkyd1c53422008-04-22 14:42:07 -0300187static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
188 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300189{
190 struct tda18271_priv *priv = fe->tuner_priv;
191 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
192 unsigned char *regs = priv->tda18271_regs;
193 int tm_current, rfcal_comp, approx, i;
194 u8 dc_over_dt, rf_tab;
195
196 /* power up */
Michael Krufky518d8732008-01-13 17:01:01 -0300197 tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300198
199 /* read die current temperature */
200 tm_current = tda18271_read_thermometer(fe);
201
202 /* frequency dependent parameters */
203
204 tda18271_calc_rf_cal(fe, &freq);
205 rf_tab = regs[R_EB14];
206
207 i = tda18271_lookup_rf_band(fe, &freq, NULL);
208 if (i < 0)
209 return -EINVAL;
210
211 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
212 approx = map[i].rf_a1 *
213 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
214 } else {
215 approx = map[i].rf_a2 *
216 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
217 }
218
219 if (approx < 0)
220 approx = 0;
221 if (approx > 255)
222 approx = 255;
223
224 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
225
226 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300227 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300228
229 regs[R_EB14] = approx + rfcal_comp;
230 tda18271_write_regs(fe, R_EB14, 1);
231
232 return 0;
233}
234
235static int tda18271_por(struct dvb_frontend *fe)
236{
237 struct tda18271_priv *priv = fe->tuner_priv;
238 unsigned char *regs = priv->tda18271_regs;
239
240 /* power up detector 1 */
241 regs[R_EB12] &= ~0x20;
242 tda18271_write_regs(fe, R_EB12, 1);
243
244 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
245 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
246 tda18271_write_regs(fe, R_EB18, 1);
247
248 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
249
250 /* POR mode */
Michael Krufky518d8732008-01-13 17:01:01 -0300251 tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300252
253 /* disable 1.5 MHz low pass filter */
254 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
255 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
256 tda18271_write_regs(fe, R_EB21, 3);
257
258 return 0;
259}
260
261static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
262{
263 struct tda18271_priv *priv = fe->tuner_priv;
264 unsigned char *regs = priv->tda18271_regs;
265 u32 N;
266
267 /* set CAL mode to normal */
268 regs[R_EP4] &= ~0x03;
269 tda18271_write_regs(fe, R_EP4, 1);
270
271 /* switch off agc1 */
272 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
273
274 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
275 tda18271_write_regs(fe, R_EB18, 1);
276
277 /* frequency dependent parameters */
278
279 tda18271_calc_bp_filter(fe, &freq);
280 tda18271_calc_gain_taper(fe, &freq);
281 tda18271_calc_rf_band(fe, &freq);
282 tda18271_calc_km(fe, &freq);
283
284 tda18271_write_regs(fe, R_EP1, 3);
285 tda18271_write_regs(fe, R_EB13, 1);
286
287 /* main pll charge pump source */
288 regs[R_EB4] |= 0x20;
289 tda18271_write_regs(fe, R_EB4, 1);
290
291 /* cal pll charge pump source */
292 regs[R_EB7] |= 0x20;
293 tda18271_write_regs(fe, R_EB7, 1);
294
295 /* force dcdc converter to 0 V */
296 regs[R_EB14] = 0x00;
297 tda18271_write_regs(fe, R_EB14, 1);
298
299 /* disable plls lock */
300 regs[R_EB20] &= ~0x20;
301 tda18271_write_regs(fe, R_EB20, 1);
302
303 /* set CAL mode to RF tracking filter calibration */
304 regs[R_EP4] |= 0x03;
305 tda18271_write_regs(fe, R_EP4, 2);
306
307 /* --------------------------------------------------------------- */
308
309 /* set the internal calibration signal */
310 N = freq;
311
Michael Krufkyae07d042008-04-22 14:46:21 -0300312 tda18271_calc_cal_pll(fe, N);
313 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300314
315 /* downconvert internal calibration */
316 N += 1000000;
317
318 tda18271_calc_main_pll(fe, N);
319 tda18271_write_regs(fe, R_MPD, 4);
320
321 msleep(5);
322
323 tda18271_write_regs(fe, R_EP2, 1);
324 tda18271_write_regs(fe, R_EP1, 1);
325 tda18271_write_regs(fe, R_EP2, 1);
326 tda18271_write_regs(fe, R_EP1, 1);
327
328 /* --------------------------------------------------------------- */
329
330 /* normal operation for the main pll */
331 regs[R_EB4] &= ~0x20;
332 tda18271_write_regs(fe, R_EB4, 1);
333
334 /* normal operation for the cal pll */
335 regs[R_EB7] &= ~0x20;
336 tda18271_write_regs(fe, R_EB7, 1);
337
Michael Krufkyae07d042008-04-22 14:46:21 -0300338 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300339
340 /* launch the rf tracking filters calibration */
341 regs[R_EB20] |= 0x20;
342 tda18271_write_regs(fe, R_EB20, 1);
343
344 msleep(60); /* calibration */
345
346 /* --------------------------------------------------------------- */
347
348 /* set CAL mode to normal */
349 regs[R_EP4] &= ~0x03;
350
351 /* switch on agc1 */
352 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
353
354 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
355 tda18271_write_regs(fe, R_EB18, 1);
356
357 tda18271_write_regs(fe, R_EP3, 2);
358
359 /* synchronization */
360 tda18271_write_regs(fe, R_EP1, 1);
361
362 /* get calibration result */
363 tda18271_read_extended(fe);
364
365 return regs[R_EB14];
366}
367
368static int tda18271_powerscan(struct dvb_frontend *fe,
369 u32 *freq_in, u32 *freq_out)
370{
371 struct tda18271_priv *priv = fe->tuner_priv;
372 unsigned char *regs = priv->tda18271_regs;
373 int sgn, bcal, count, wait;
374 u8 cid_target;
375 u16 count_limit;
376 u32 freq;
377
378 freq = *freq_in;
379
380 tda18271_calc_rf_band(fe, &freq);
381 tda18271_calc_rf_cal(fe, &freq);
382 tda18271_calc_gain_taper(fe, &freq);
383 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
384
385 tda18271_write_regs(fe, R_EP2, 1);
386 tda18271_write_regs(fe, R_EB14, 1);
387
388 /* downconvert frequency */
389 freq += 1000000;
390
391 tda18271_calc_main_pll(fe, freq);
392 tda18271_write_regs(fe, R_MPD, 4);
393
394 msleep(5); /* pll locking */
395
396 /* detection mode */
397 regs[R_EP4] &= ~0x03;
398 regs[R_EP4] |= 0x01;
399 tda18271_write_regs(fe, R_EP4, 1);
400
401 /* launch power detection measurement */
402 tda18271_write_regs(fe, R_EP2, 1);
403
404 /* read power detection info, stored in EB10 */
405 tda18271_read_extended(fe);
406
407 /* algorithm initialization */
408 sgn = 1;
409 *freq_out = *freq_in;
410 bcal = 0;
411 count = 0;
412 wait = false;
413
414 while ((regs[R_EB10] & 0x3f) < cid_target) {
415 /* downconvert updated freq to 1 MHz */
416 freq = *freq_in + (sgn * count) + 1000000;
417
418 tda18271_calc_main_pll(fe, freq);
419 tda18271_write_regs(fe, R_MPD, 4);
420
421 if (wait) {
422 msleep(5); /* pll locking */
423 wait = false;
424 } else
425 udelay(100); /* pll locking */
426
427 /* launch power detection measurement */
428 tda18271_write_regs(fe, R_EP2, 1);
429
430 /* read power detection info, stored in EB10 */
431 tda18271_read_extended(fe);
432
433 count += 200;
434
Michael Krufkye7809a02008-04-22 14:46:22 -0300435 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300436 continue;
437
438 if (sgn <= 0)
439 break;
440
441 sgn = -1 * sgn;
442 count = 200;
443 wait = true;
444 }
445
446 if ((regs[R_EB10] & 0x3f) >= cid_target) {
447 bcal = 1;
448 *freq_out = freq - 1000000;
449 } else
450 bcal = 0;
451
Michael Krufkycf04d292008-01-09 00:34:30 -0300452 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300453 bcal, *freq_in, *freq_out, freq);
454
455 return bcal;
456}
457
458static int tda18271_powerscan_init(struct dvb_frontend *fe)
459{
460 struct tda18271_priv *priv = fe->tuner_priv;
461 unsigned char *regs = priv->tda18271_regs;
462
463 /* set standard to digital */
464 regs[R_EP3] &= ~0x1f; /* clear std bits */
465 regs[R_EP3] |= 0x12;
466
467 /* set cal mode to normal */
468 regs[R_EP4] &= ~0x03;
469
470 /* update IF output level & IF notch frequency */
471 regs[R_EP4] &= ~0x1c; /* clear if level bits */
472
473 tda18271_write_regs(fe, R_EP3, 2);
474
475 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
476 tda18271_write_regs(fe, R_EB18, 1);
477
478 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
479
480 /* 1.5 MHz low pass filter */
481 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
482 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
483
484 tda18271_write_regs(fe, R_EB21, 3);
485
486 return 0;
487}
488
489static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
490{
491 struct tda18271_priv *priv = fe->tuner_priv;
492 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
493 unsigned char *regs = priv->tda18271_regs;
494 int bcal, rf, i;
495#define RF1 0
496#define RF2 1
497#define RF3 2
498 u32 rf_default[3];
499 u32 rf_freq[3];
500 u8 prog_cal[3];
501 u8 prog_tab[3];
502
503 i = tda18271_lookup_rf_band(fe, &freq, NULL);
504
505 if (i < 0)
506 return i;
507
508 rf_default[RF1] = 1000 * map[i].rf1_def;
509 rf_default[RF2] = 1000 * map[i].rf2_def;
510 rf_default[RF3] = 1000 * map[i].rf3_def;
511
512 for (rf = RF1; rf <= RF3; rf++) {
513 if (0 == rf_default[rf])
514 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300515 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300516
517 /* look for optimized calibration frequency */
518 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
519
520 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
521 prog_tab[rf] = regs[R_EB14];
522
523 if (1 == bcal)
524 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
525 else
526 prog_cal[rf] = prog_tab[rf];
527
528 switch (rf) {
529 case RF1:
530 map[i].rf_a1 = 0;
531 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
532 map[i].rf1 = rf_freq[RF1] / 1000;
533 break;
534 case RF2:
535 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
536 prog_cal[RF1] + prog_tab[RF1]) /
537 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
538 map[i].rf2 = rf_freq[RF2] / 1000;
539 break;
540 case RF3:
541 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
542 prog_cal[RF2] + prog_tab[RF2]) /
543 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
544 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
545 map[i].rf3 = rf_freq[RF3] / 1000;
546 break;
547 default:
548 BUG();
549 }
550 }
551
552 return 0;
553}
554
Michael Krufky09f83c42008-01-05 20:00:09 -0300555static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300556{
557 struct tda18271_priv *priv = fe->tuner_priv;
558 unsigned int i;
559
560 tda_info("tda18271: performing RF tracking filter calibration\n");
561
562 /* wait for die temperature stabilization */
563 msleep(200);
564
565 tda18271_powerscan_init(fe);
566
567 /* rf band calibration */
568 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
569 tda18271_rf_tracking_filters_init(fe, 1000 *
570 priv->rf_cal_state[i].rfmax);
571
Michael Krufky09f83c42008-01-05 20:00:09 -0300572 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300573
574 return 0;
575}
576
577/* ------------------------------------------------------------------ */
578
Michael Krufky12afe372008-04-22 14:42:07 -0300579static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300580{
581 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300582 unsigned char *regs = priv->tda18271_regs;
583
584 /* test RF_CAL_OK to see if we need init */
585 if ((regs[R_EP1] & 0x10) == 0)
586 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300587
588 if (priv->cal_initialized)
589 return 0;
590
Michael Krufky09f83c42008-01-05 20:00:09 -0300591 tda18271_calc_rf_filter_curve(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300592
593 tda18271_por(fe);
594
Michael Krufky6bfa6652008-01-07 00:51:48 -0300595 tda_info("tda18271: RF tracking filter calibration complete\n");
596
Michael Krufky255b5112008-01-01 22:52:09 -0300597 priv->cal_initialized = true;
598
599 return 0;
600}
601
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300602static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
603 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300604{
605 struct tda18271_priv *priv = fe->tuner_priv;
606 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300607 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300608
Michael Krufky255b5112008-01-01 22:52:09 -0300609 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300610 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300611 tda18271_write_regs(fe, R_EP1, 1);
612
613 regs[R_EB4] &= 0x07;
614 regs[R_EB4] |= 0x60;
615 tda18271_write_regs(fe, R_EB4, 1);
616
617 regs[R_EB7] = 0x60;
618 tda18271_write_regs(fe, R_EB7, 1);
619
620 regs[R_EB14] = 0x00;
621 tda18271_write_regs(fe, R_EB14, 1);
622
623 regs[R_EB20] = 0xcc;
624 tda18271_write_regs(fe, R_EB20, 1);
625
Michael Krufky255b5112008-01-01 22:52:09 -0300626 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300627 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300628
Michael Krufky255b5112008-01-01 22:52:09 -0300629 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300630
631 switch (priv->mode) {
632 case TDA18271_ANALOG:
633 N = freq - 1250000;
634 break;
635 case TDA18271_DIGITAL:
636 N = freq + bw / 2;
637 break;
638 }
639
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300640 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300641
Michael Krufky255b5112008-01-01 22:52:09 -0300642 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300643
644 switch (priv->mode) {
645 case TDA18271_ANALOG:
646 N = freq - 250000;
647 break;
648 case TDA18271_DIGITAL:
649 N = freq + bw / 2 + 1000000;
650 break;
651 }
652
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300653 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300654
655 tda18271_write_regs(fe, R_EP3, 11);
656 msleep(5); /* RF tracking filter calibration initialization */
657
Michael Krufky255b5112008-01-01 22:52:09 -0300658 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300659 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300660 tda18271_write_regs(fe, R_EB13, 1);
661
Michael Krufky255b5112008-01-01 22:52:09 -0300662 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300663 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300664
Michael Krufky255b5112008-01-01 22:52:09 -0300665 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300666 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300667
668 tda18271_write_regs(fe, R_EP2, 1);
669 tda18271_write_regs(fe, R_EP1, 1);
670 tda18271_write_regs(fe, R_EP2, 1);
671 tda18271_write_regs(fe, R_EP1, 1);
672
673 regs[R_EB4] &= 0x07;
674 regs[R_EB4] |= 0x40;
675 tda18271_write_regs(fe, R_EB4, 1);
676
677 regs[R_EB7] = 0x40;
678 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300679 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300680
681 regs[R_EB20] = 0xec;
682 tda18271_write_regs(fe, R_EB20, 1);
683 msleep(60); /* RF tracking filter calibration completion */
684
685 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
686 tda18271_write_regs(fe, R_EP4, 1);
687
688 tda18271_write_regs(fe, R_EP1, 1);
689
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300690 /* RF tracking filter correction for VHF_Low band */
691 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300692 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300693
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300694 return 0;
695}
696
Michael Krufkyd1c53422008-04-22 14:42:07 -0300697/* ------------------------------------------------------------------ */
698
Michael Krufky12afe372008-04-22 14:42:07 -0300699static int tda18271_ir_cal_init(struct dvb_frontend *fe)
700{
701 struct tda18271_priv *priv = fe->tuner_priv;
702 unsigned char *regs = priv->tda18271_regs;
703
704 tda18271_read_regs(fe);
705
706 /* test IR_CAL_OK to see if we need init */
707 if ((regs[R_EP1] & 0x08) == 0)
708 tda18271_init_regs(fe);
709
710 return 0;
711}
712
713static int tda18271_init(struct dvb_frontend *fe)
714{
715 struct tda18271_priv *priv = fe->tuner_priv;
716
717 mutex_lock(&priv->lock);
718
719 /* power up */
720 tda18271_set_standby_mode(fe, 0, 0, 0);
721
722 /* initialization */
723 tda18271_ir_cal_init(fe);
724
725 if (priv->id == TDA18271HDC2)
726 tda18271c2_rf_cal_init(fe);
727
728 mutex_unlock(&priv->lock);
729
730 return 0;
731}
732
Michael Krufkyd1c53422008-04-22 14:42:07 -0300733static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300734 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300735{
736 struct tda18271_priv *priv = fe->tuner_priv;
737
Michael Krufky7f7203d2008-04-22 14:46:06 -0300738 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
739 freq, map->if_freq, bw, map->agc_mode, map->std);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300740
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300741 tda18271_init(fe);
742
743 mutex_lock(&priv->lock);
744
Michael Krufkyd1c53422008-04-22 14:42:07 -0300745 switch (priv->id) {
746 case TDA18271HDC1:
747 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
748 break;
749 case TDA18271HDC2:
750 tda18271c2_rf_tracking_filters_correction(fe, freq);
751 break;
752 }
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300753 tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300754
Michael Krufky8d316bf2008-01-06 15:31:35 -0300755 mutex_unlock(&priv->lock);
Michael Krufky6ca04de2007-11-23 16:52:15 -0300756
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300757 return 0;
758}
759
760/* ------------------------------------------------------------------ */
761
762static int tda18271_set_params(struct dvb_frontend *fe,
763 struct dvb_frontend_parameters *params)
764{
765 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300766 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300767 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300768 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300769 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300770
771 priv->mode = TDA18271_DIGITAL;
772
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300773 if (fe->ops.info.type == FE_ATSC) {
774 switch (params->u.vsb.modulation) {
775 case VSB_8:
776 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300777 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300778 break;
779 case QAM_64:
780 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300781 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300782 break;
783 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300784 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300785 return -EINVAL;
786 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300787#if 0
788 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300789 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300790#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300791 bw = 6000000;
792 } else if (fe->ops.info.type == FE_OFDM) {
793 switch (params->u.ofdm.bandwidth) {
794 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300795 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300796 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300797 break;
798 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300799 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300800 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300801 break;
802 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300803 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300804 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300805 break;
806 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300807 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300808 return -EINVAL;
809 }
810 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300811 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300812 return -EINVAL;
813 }
814
Michael Krufkyed736832008-01-19 17:41:04 -0300815 /* When tuning digital, the analog demod must be tri-stated */
816 if (fe->ops.analog_ops.standby)
817 fe->ops.analog_ops.standby(fe);
818
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300819 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300820
821 if (ret < 0)
822 goto fail;
823
824 priv->frequency = freq;
825 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
826 params->u.ofdm.bandwidth : 0;
827fail:
828 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300829}
830
831static int tda18271_set_analog_params(struct dvb_frontend *fe,
832 struct analog_parameters *params)
833{
834 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300835 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300836 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300837 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300838 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300839 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300840
841 priv->mode = TDA18271_ANALOG;
842
Michael Krufkyc353f422008-01-08 10:38:10 -0300843 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc353f422008-01-08 10:38:10 -0300844 freq = freq / 1000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300845 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -0300846 mode = "fm";
847 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300848 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300849 mode = "MN";
850 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300851 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300852 mode = "B";
853 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300854 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300855 mode = "GH";
856 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300857 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300858 mode = "I";
859 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300860 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300861 mode = "DK";
862 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300863 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300864 mode = "L";
865 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300866 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -0300867 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300868 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300869 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300870 mode = "xx";
871 }
872
Michael Krufky182519f2007-12-25 15:10:11 -0300873 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300874
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300875 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300876
877 if (ret < 0)
878 goto fail;
879
880 priv->frequency = freq;
881 priv->bandwidth = 0;
882fail:
883 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300884}
885
Michael Krufky518d8732008-01-13 17:01:01 -0300886static int tda18271_sleep(struct dvb_frontend *fe)
887{
888 struct tda18271_priv *priv = fe->tuner_priv;
889
890 mutex_lock(&priv->lock);
891
892 /* standby mode w/ slave tuner output
893 * & loop thru & xtal oscillator on */
894 tda18271_set_standby_mode(fe, 1, 0, 0);
895
896 mutex_unlock(&priv->lock);
897
898 return 0;
899}
900
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300901static int tda18271_release(struct dvb_frontend *fe)
902{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300903 struct tda18271_priv *priv = fe->tuner_priv;
904
905 mutex_lock(&tda18271_list_mutex);
906
Michael Krufkyf9e315a2008-04-22 14:41:54 -0300907 if (priv)
908 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -0300909
Michael Krufkya4f263b2008-01-06 15:52:56 -0300910 mutex_unlock(&tda18271_list_mutex);
911
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300912 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -0300913
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300914 return 0;
915}
916
917static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
918{
919 struct tda18271_priv *priv = fe->tuner_priv;
920 *frequency = priv->frequency;
921 return 0;
922}
923
924static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
925{
926 struct tda18271_priv *priv = fe->tuner_priv;
927 *bandwidth = priv->bandwidth;
928 return 0;
929}
930
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300931/* ------------------------------------------------------------------ */
932
933#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -0300934 if (map->std_cfg.if_freq + \
935 map->std_cfg.agc_mode + map->std_cfg.std > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300936 tda_dbg("Using custom std config for %s\n", name); \
937 memcpy(&std->std_cfg, &map->std_cfg, \
938 sizeof(struct tda18271_std_map_item)); \
939 } } while (0)
940
941#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -0300942 tda_dbg("(%s) if freq = %d, agc_mode = %d, std = %d\n", \
943 name, std->std_cfg.if_freq, \
944 std->std_cfg.agc_mode, std->std_cfg.std); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300945 } while (0)
946
947static int tda18271_dump_std_map(struct dvb_frontend *fe)
948{
949 struct tda18271_priv *priv = fe->tuner_priv;
950 struct tda18271_std_map *std = &priv->std;
951
952 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc353f422008-01-08 10:38:10 -0300953 tda18271_dump_std_item(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300954 tda18271_dump_std_item(atv_b, "pal b");
955 tda18271_dump_std_item(atv_dk, "pal dk");
956 tda18271_dump_std_item(atv_gh, "pal gh");
957 tda18271_dump_std_item(atv_i, "pal i");
958 tda18271_dump_std_item(atv_l, "pal l");
959 tda18271_dump_std_item(atv_lc, "pal l'");
960 tda18271_dump_std_item(atv_mn, "atv mn");
961 tda18271_dump_std_item(atsc_6, "atsc 6");
962 tda18271_dump_std_item(dvbt_6, "dvbt 6");
963 tda18271_dump_std_item(dvbt_7, "dvbt 7");
964 tda18271_dump_std_item(dvbt_8, "dvbt 8");
965 tda18271_dump_std_item(qam_6, "qam 6");
966 tda18271_dump_std_item(qam_8, "qam 8");
967
968 return 0;
969}
970
971static int tda18271_update_std_map(struct dvb_frontend *fe,
972 struct tda18271_std_map *map)
973{
974 struct tda18271_priv *priv = fe->tuner_priv;
975 struct tda18271_std_map *std = &priv->std;
976
977 if (!map)
978 return -EINVAL;
979
Michael Krufkyc353f422008-01-08 10:38:10 -0300980 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300981 tda18271_update_std(atv_b, "atv b");
982 tda18271_update_std(atv_dk, "atv dk");
983 tda18271_update_std(atv_gh, "atv gh");
984 tda18271_update_std(atv_i, "atv i");
985 tda18271_update_std(atv_l, "atv l");
986 tda18271_update_std(atv_lc, "atv l'");
987 tda18271_update_std(atv_mn, "atv mn");
988 tda18271_update_std(atsc_6, "atsc 6");
989 tda18271_update_std(dvbt_6, "dvbt 6");
990 tda18271_update_std(dvbt_7, "dvbt 7");
991 tda18271_update_std(dvbt_8, "dvbt 8");
992 tda18271_update_std(qam_6, "qam 6");
993 tda18271_update_std(qam_8, "qam 8");
994
995 return 0;
996}
997
Michael Krufky49e7aaf2007-12-24 04:15:20 -0300998static int tda18271_get_id(struct dvb_frontend *fe)
999{
1000 struct tda18271_priv *priv = fe->tuner_priv;
1001 unsigned char *regs = priv->tda18271_regs;
1002 char *name;
1003 int ret = 0;
1004
Michael Krufky8d316bf2008-01-06 15:31:35 -03001005 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001006 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001007 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001008
1009 switch (regs[R_ID] & 0x7f) {
1010 case 3:
1011 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001012 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001013 break;
1014 case 4:
1015 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001016 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001017 break;
1018 default:
1019 name = "Unknown device";
1020 ret = -EINVAL;
1021 break;
1022 }
1023
Michael Krufky182519f2007-12-25 15:10:11 -03001024 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001025 i2c_adapter_id(priv->i2c_props.adap),
1026 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001027 (0 == ret) ? "" : ", device not supported.");
1028
1029 return ret;
1030}
1031
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001032static struct dvb_tuner_ops tda18271_tuner_ops = {
1033 .info = {
1034 .name = "NXP TDA18271HD",
1035 .frequency_min = 45000000,
1036 .frequency_max = 864000000,
1037 .frequency_step = 62500
1038 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001039 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001040 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001041 .set_params = tda18271_set_params,
1042 .set_analog_params = tda18271_set_analog_params,
1043 .release = tda18271_release,
1044 .get_frequency = tda18271_get_frequency,
1045 .get_bandwidth = tda18271_get_bandwidth,
1046};
1047
1048struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001049 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001050 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001051{
1052 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001053 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001054
Michael Krufkya4f263b2008-01-06 15:52:56 -03001055 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001056
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001057 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1058 hybrid_tuner_instance_list,
1059 i2c, addr, "tda18271");
1060 switch (instance) {
1061 case 0:
1062 goto fail;
1063 break;
1064 case 1:
1065 /* new tuner instance */
Michael Krufkya4f263b2008-01-06 15:52:56 -03001066 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1067 priv->cal_initialized = false;
1068 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001069
Michael Krufkya4f263b2008-01-06 15:52:56 -03001070 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001071
Michael Krufky55553092008-04-22 14:46:06 -03001072 if (cfg)
1073 priv->small_i2c = cfg->small_i2c;
1074
Michael Krufkya4f263b2008-01-06 15:52:56 -03001075 if (tda18271_get_id(fe) < 0)
1076 goto fail;
1077
1078 if (tda18271_assign_map_layout(fe) < 0)
1079 goto fail;
1080
1081 mutex_lock(&priv->lock);
1082 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001083
1084 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001085 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001086
Michael Krufkya4f263b2008-01-06 15:52:56 -03001087 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001088 break;
1089 default:
1090 /* existing tuner instance */
1091 fe->tuner_priv = priv;
1092
1093 /* allow dvb driver to override i2c gate setting */
1094 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1095 priv->gate = cfg->gate;
1096 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001097 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001098
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001099 /* override default std map with values in config struct */
1100 if ((cfg) && (cfg->std_map))
1101 tda18271_update_std_map(fe, cfg->std_map);
1102
Michael Krufkya4f263b2008-01-06 15:52:56 -03001103 mutex_unlock(&tda18271_list_mutex);
1104
1105 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1106 sizeof(struct dvb_tuner_ops));
1107
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001108 if (tda18271_debug & DBG_MAP)
1109 tda18271_dump_std_map(fe);
1110
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001111 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001112fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001113 mutex_unlock(&tda18271_list_mutex);
1114
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001115 tda18271_release(fe);
1116 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001117}
1118EXPORT_SYMBOL_GPL(tda18271_attach);
1119MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1120MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1121MODULE_LICENSE("GPL");
Michael Krufky255b5112008-01-01 22:52:09 -03001122MODULE_VERSION("0.2");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001123
1124/*
1125 * Overrides for Emacs so that we follow Linus's tabbing style.
1126 * ---------------------------------------------------------------------------
1127 * Local variables:
1128 * c-basic-offset: 8
1129 * End:
1130 */