blob: 4a025849a327c9d1b38579bcb6221cc8b071319b [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
53 /* set cal mode to normal */
54 regs[R_EP4] &= ~0x03;
55
56 /* update IF output level & IF notch frequency */
57 regs[R_EP4] &= ~0x1c; /* clear if level bits */
Michael Krufky14c74b22008-04-22 14:46:21 -030058 regs[R_EP4] |= (map->if_lvl << 2);
Michael Krufky255b5112008-01-01 22:52:09 -030059
60 switch (priv->mode) {
61 case TDA18271_ANALOG:
62 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
63 break;
64 case TDA18271_DIGITAL:
Michael Krufky255b5112008-01-01 22:52:09 -030065 regs[R_MPD] |= 0x80; /* IF notch = 1 */
66 break;
67 }
Michael Krufkyc353f422008-01-08 10:38:10 -030068
Michael Krufkyc293d0a2008-04-22 14:46:06 -030069 /* update FM_RFn */
70 regs[R_EP4] &= ~0x80;
71 regs[R_EP4] |= map->fm_rfn << 7;
Michael Krufky255b5112008-01-01 22:52:09 -030072
Michael Krufkyc0dc0c12008-04-22 14:46:22 -030073 /* update rf top / if top */
74 regs[R_EB22] = 0x00;
75 regs[R_EB22] |= map->rfagc_top;
Michael Krufky255b5112008-01-01 22:52:09 -030076 tda18271_write_regs(fe, R_EB22, 1);
77
78 /* --------------------------------------------------------------- */
79
80 /* disable Power Level Indicator */
81 regs[R_EP1] |= 0x40;
82
83 /* frequency dependent parameters */
84
85 tda18271_calc_ir_measure(fe, &freq);
86
87 tda18271_calc_bp_filter(fe, &freq);
88
89 tda18271_calc_rf_band(fe, &freq);
90
91 tda18271_calc_gain_taper(fe, &freq);
92
93 /* --------------------------------------------------------------- */
94
95 /* dual tuner and agc1 extra configuration */
96
97 /* main vco when Master, cal vco when slave */
98 regs[R_EB1] |= 0x04; /* FIXME: assumes master */
99
100 /* agc1 always active */
101 regs[R_EB1] &= ~0x02;
102
103 /* agc1 has priority on agc2 */
104 regs[R_EB1] &= ~0x01;
105
106 tda18271_write_regs(fe, R_EB1, 1);
107
108 /* --------------------------------------------------------------- */
109
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300110 N = map->if_freq * 1000 + freq;
Michael Krufky255b5112008-01-01 22:52:09 -0300111
112 /* FIXME: assumes master */
113 tda18271_calc_main_pll(fe, N);
114 tda18271_write_regs(fe, R_MPD, 4);
115
116 tda18271_write_regs(fe, R_TM, 7);
117
118 /* main pll charge pump source */
119 regs[R_EB4] |= 0x20;
120 tda18271_write_regs(fe, R_EB4, 1);
121
122 msleep(1);
123
124 /* normal operation for the main pll */
125 regs[R_EB4] &= ~0x20;
126 tda18271_write_regs(fe, R_EB4, 1);
127
128 msleep(5);
129
130 return 0;
131}
132
133static int tda18271_read_thermometer(struct dvb_frontend *fe)
134{
135 struct tda18271_priv *priv = fe->tuner_priv;
136 unsigned char *regs = priv->tda18271_regs;
137 int tm;
138
139 /* switch thermometer on */
140 regs[R_TM] |= 0x10;
141 tda18271_write_regs(fe, R_TM, 1);
142
143 /* read thermometer info */
144 tda18271_read_regs(fe);
145
146 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
147 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
148
149 if ((regs[R_TM] & 0x20) == 0x20)
150 regs[R_TM] &= ~0x20;
151 else
152 regs[R_TM] |= 0x20;
153
154 tda18271_write_regs(fe, R_TM, 1);
155
156 msleep(10); /* temperature sensing */
157
158 /* read thermometer info */
159 tda18271_read_regs(fe);
160 }
161
162 tm = tda18271_lookup_thermometer(fe);
163
164 /* switch thermometer off */
165 regs[R_TM] &= ~0x10;
166 tda18271_write_regs(fe, R_TM, 1);
167
168 /* set CAL mode to normal */
169 regs[R_EP4] &= ~0x03;
170 tda18271_write_regs(fe, R_EP4, 1);
171
172 return tm;
173}
174
Michael Krufky12afe372008-04-22 14:42:07 -0300175/* ------------------------------------------------------------------ */
176
Michael Krufkyd1c53422008-04-22 14:42:07 -0300177static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
178 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300179{
180 struct tda18271_priv *priv = fe->tuner_priv;
181 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
182 unsigned char *regs = priv->tda18271_regs;
183 int tm_current, rfcal_comp, approx, i;
184 u8 dc_over_dt, rf_tab;
185
186 /* power up */
Michael Krufky518d8732008-01-13 17:01:01 -0300187 tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300188
189 /* read die current temperature */
190 tm_current = tda18271_read_thermometer(fe);
191
192 /* frequency dependent parameters */
193
194 tda18271_calc_rf_cal(fe, &freq);
195 rf_tab = regs[R_EB14];
196
197 i = tda18271_lookup_rf_band(fe, &freq, NULL);
198 if (i < 0)
199 return -EINVAL;
200
201 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
202 approx = map[i].rf_a1 *
203 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
204 } else {
205 approx = map[i].rf_a2 *
206 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
207 }
208
209 if (approx < 0)
210 approx = 0;
211 if (approx > 255)
212 approx = 255;
213
214 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
215
216 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300217 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300218
219 regs[R_EB14] = approx + rfcal_comp;
220 tda18271_write_regs(fe, R_EB14, 1);
221
222 return 0;
223}
224
225static int tda18271_por(struct dvb_frontend *fe)
226{
227 struct tda18271_priv *priv = fe->tuner_priv;
228 unsigned char *regs = priv->tda18271_regs;
229
230 /* power up detector 1 */
231 regs[R_EB12] &= ~0x20;
232 tda18271_write_regs(fe, R_EB12, 1);
233
234 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
235 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
236 tda18271_write_regs(fe, R_EB18, 1);
237
238 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
239
240 /* POR mode */
Michael Krufky518d8732008-01-13 17:01:01 -0300241 tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300242
243 /* disable 1.5 MHz low pass filter */
244 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
245 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
246 tda18271_write_regs(fe, R_EB21, 3);
247
248 return 0;
249}
250
251static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
252{
253 struct tda18271_priv *priv = fe->tuner_priv;
254 unsigned char *regs = priv->tda18271_regs;
255 u32 N;
256
257 /* set CAL mode to normal */
258 regs[R_EP4] &= ~0x03;
259 tda18271_write_regs(fe, R_EP4, 1);
260
261 /* switch off agc1 */
262 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
263
264 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
265 tda18271_write_regs(fe, R_EB18, 1);
266
267 /* frequency dependent parameters */
268
269 tda18271_calc_bp_filter(fe, &freq);
270 tda18271_calc_gain_taper(fe, &freq);
271 tda18271_calc_rf_band(fe, &freq);
272 tda18271_calc_km(fe, &freq);
273
274 tda18271_write_regs(fe, R_EP1, 3);
275 tda18271_write_regs(fe, R_EB13, 1);
276
277 /* main pll charge pump source */
278 regs[R_EB4] |= 0x20;
279 tda18271_write_regs(fe, R_EB4, 1);
280
281 /* cal pll charge pump source */
282 regs[R_EB7] |= 0x20;
283 tda18271_write_regs(fe, R_EB7, 1);
284
285 /* force dcdc converter to 0 V */
286 regs[R_EB14] = 0x00;
287 tda18271_write_regs(fe, R_EB14, 1);
288
289 /* disable plls lock */
290 regs[R_EB20] &= ~0x20;
291 tda18271_write_regs(fe, R_EB20, 1);
292
293 /* set CAL mode to RF tracking filter calibration */
294 regs[R_EP4] |= 0x03;
295 tda18271_write_regs(fe, R_EP4, 2);
296
297 /* --------------------------------------------------------------- */
298
299 /* set the internal calibration signal */
300 N = freq;
301
Michael Krufkyae07d042008-04-22 14:46:21 -0300302 tda18271_calc_cal_pll(fe, N);
303 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300304
305 /* downconvert internal calibration */
306 N += 1000000;
307
308 tda18271_calc_main_pll(fe, N);
309 tda18271_write_regs(fe, R_MPD, 4);
310
311 msleep(5);
312
313 tda18271_write_regs(fe, R_EP2, 1);
314 tda18271_write_regs(fe, R_EP1, 1);
315 tda18271_write_regs(fe, R_EP2, 1);
316 tda18271_write_regs(fe, R_EP1, 1);
317
318 /* --------------------------------------------------------------- */
319
320 /* normal operation for the main pll */
321 regs[R_EB4] &= ~0x20;
322 tda18271_write_regs(fe, R_EB4, 1);
323
324 /* normal operation for the cal pll */
325 regs[R_EB7] &= ~0x20;
326 tda18271_write_regs(fe, R_EB7, 1);
327
Michael Krufkyae07d042008-04-22 14:46:21 -0300328 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300329
330 /* launch the rf tracking filters calibration */
331 regs[R_EB20] |= 0x20;
332 tda18271_write_regs(fe, R_EB20, 1);
333
334 msleep(60); /* calibration */
335
336 /* --------------------------------------------------------------- */
337
338 /* set CAL mode to normal */
339 regs[R_EP4] &= ~0x03;
340
341 /* switch on agc1 */
342 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
343
344 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
345 tda18271_write_regs(fe, R_EB18, 1);
346
347 tda18271_write_regs(fe, R_EP3, 2);
348
349 /* synchronization */
350 tda18271_write_regs(fe, R_EP1, 1);
351
352 /* get calibration result */
353 tda18271_read_extended(fe);
354
355 return regs[R_EB14];
356}
357
358static int tda18271_powerscan(struct dvb_frontend *fe,
359 u32 *freq_in, u32 *freq_out)
360{
361 struct tda18271_priv *priv = fe->tuner_priv;
362 unsigned char *regs = priv->tda18271_regs;
363 int sgn, bcal, count, wait;
364 u8 cid_target;
365 u16 count_limit;
366 u32 freq;
367
368 freq = *freq_in;
369
370 tda18271_calc_rf_band(fe, &freq);
371 tda18271_calc_rf_cal(fe, &freq);
372 tda18271_calc_gain_taper(fe, &freq);
373 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
374
375 tda18271_write_regs(fe, R_EP2, 1);
376 tda18271_write_regs(fe, R_EB14, 1);
377
378 /* downconvert frequency */
379 freq += 1000000;
380
381 tda18271_calc_main_pll(fe, freq);
382 tda18271_write_regs(fe, R_MPD, 4);
383
384 msleep(5); /* pll locking */
385
386 /* detection mode */
387 regs[R_EP4] &= ~0x03;
388 regs[R_EP4] |= 0x01;
389 tda18271_write_regs(fe, R_EP4, 1);
390
391 /* launch power detection measurement */
392 tda18271_write_regs(fe, R_EP2, 1);
393
394 /* read power detection info, stored in EB10 */
395 tda18271_read_extended(fe);
396
397 /* algorithm initialization */
398 sgn = 1;
399 *freq_out = *freq_in;
400 bcal = 0;
401 count = 0;
402 wait = false;
403
404 while ((regs[R_EB10] & 0x3f) < cid_target) {
405 /* downconvert updated freq to 1 MHz */
406 freq = *freq_in + (sgn * count) + 1000000;
407
408 tda18271_calc_main_pll(fe, freq);
409 tda18271_write_regs(fe, R_MPD, 4);
410
411 if (wait) {
412 msleep(5); /* pll locking */
413 wait = false;
414 } else
415 udelay(100); /* pll locking */
416
417 /* launch power detection measurement */
418 tda18271_write_regs(fe, R_EP2, 1);
419
420 /* read power detection info, stored in EB10 */
421 tda18271_read_extended(fe);
422
423 count += 200;
424
Michael Krufkye7809a02008-04-22 14:46:22 -0300425 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300426 continue;
427
428 if (sgn <= 0)
429 break;
430
431 sgn = -1 * sgn;
432 count = 200;
433 wait = true;
434 }
435
436 if ((regs[R_EB10] & 0x3f) >= cid_target) {
437 bcal = 1;
438 *freq_out = freq - 1000000;
439 } else
440 bcal = 0;
441
Michael Krufkycf04d292008-01-09 00:34:30 -0300442 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300443 bcal, *freq_in, *freq_out, freq);
444
445 return bcal;
446}
447
448static int tda18271_powerscan_init(struct dvb_frontend *fe)
449{
450 struct tda18271_priv *priv = fe->tuner_priv;
451 unsigned char *regs = priv->tda18271_regs;
452
453 /* set standard to digital */
454 regs[R_EP3] &= ~0x1f; /* clear std bits */
455 regs[R_EP3] |= 0x12;
456
457 /* set cal mode to normal */
458 regs[R_EP4] &= ~0x03;
459
460 /* update IF output level & IF notch frequency */
461 regs[R_EP4] &= ~0x1c; /* clear if level bits */
462
463 tda18271_write_regs(fe, R_EP3, 2);
464
465 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
466 tda18271_write_regs(fe, R_EB18, 1);
467
468 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
469
470 /* 1.5 MHz low pass filter */
471 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
472 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
473
474 tda18271_write_regs(fe, R_EB21, 3);
475
476 return 0;
477}
478
479static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
480{
481 struct tda18271_priv *priv = fe->tuner_priv;
482 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
483 unsigned char *regs = priv->tda18271_regs;
484 int bcal, rf, i;
485#define RF1 0
486#define RF2 1
487#define RF3 2
488 u32 rf_default[3];
489 u32 rf_freq[3];
490 u8 prog_cal[3];
491 u8 prog_tab[3];
492
493 i = tda18271_lookup_rf_band(fe, &freq, NULL);
494
495 if (i < 0)
496 return i;
497
498 rf_default[RF1] = 1000 * map[i].rf1_def;
499 rf_default[RF2] = 1000 * map[i].rf2_def;
500 rf_default[RF3] = 1000 * map[i].rf3_def;
501
502 for (rf = RF1; rf <= RF3; rf++) {
503 if (0 == rf_default[rf])
504 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300505 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300506
507 /* look for optimized calibration frequency */
508 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
509
510 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
511 prog_tab[rf] = regs[R_EB14];
512
513 if (1 == bcal)
514 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
515 else
516 prog_cal[rf] = prog_tab[rf];
517
518 switch (rf) {
519 case RF1:
520 map[i].rf_a1 = 0;
521 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
522 map[i].rf1 = rf_freq[RF1] / 1000;
523 break;
524 case RF2:
525 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
526 prog_cal[RF1] + prog_tab[RF1]) /
527 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
528 map[i].rf2 = rf_freq[RF2] / 1000;
529 break;
530 case RF3:
531 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
532 prog_cal[RF2] + prog_tab[RF2]) /
533 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
534 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
535 map[i].rf3 = rf_freq[RF3] / 1000;
536 break;
537 default:
538 BUG();
539 }
540 }
541
542 return 0;
543}
544
Michael Krufky09f83c42008-01-05 20:00:09 -0300545static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300546{
547 struct tda18271_priv *priv = fe->tuner_priv;
548 unsigned int i;
549
550 tda_info("tda18271: performing RF tracking filter calibration\n");
551
552 /* wait for die temperature stabilization */
553 msleep(200);
554
555 tda18271_powerscan_init(fe);
556
557 /* rf band calibration */
558 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
559 tda18271_rf_tracking_filters_init(fe, 1000 *
560 priv->rf_cal_state[i].rfmax);
561
Michael Krufky09f83c42008-01-05 20:00:09 -0300562 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300563
564 return 0;
565}
566
567/* ------------------------------------------------------------------ */
568
Michael Krufky12afe372008-04-22 14:42:07 -0300569static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300570{
571 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300572 unsigned char *regs = priv->tda18271_regs;
573
574 /* test RF_CAL_OK to see if we need init */
575 if ((regs[R_EP1] & 0x10) == 0)
576 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300577
578 if (priv->cal_initialized)
579 return 0;
580
Michael Krufky09f83c42008-01-05 20:00:09 -0300581 tda18271_calc_rf_filter_curve(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300582
583 tda18271_por(fe);
584
Michael Krufky6bfa6652008-01-07 00:51:48 -0300585 tda_info("tda18271: RF tracking filter calibration complete\n");
586
Michael Krufky255b5112008-01-01 22:52:09 -0300587 priv->cal_initialized = true;
588
589 return 0;
590}
591
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300592static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
593 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300594{
595 struct tda18271_priv *priv = fe->tuner_priv;
596 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300597 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300598
Michael Krufky255b5112008-01-01 22:52:09 -0300599 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300600 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300601 tda18271_write_regs(fe, R_EP1, 1);
602
603 regs[R_EB4] &= 0x07;
604 regs[R_EB4] |= 0x60;
605 tda18271_write_regs(fe, R_EB4, 1);
606
607 regs[R_EB7] = 0x60;
608 tda18271_write_regs(fe, R_EB7, 1);
609
610 regs[R_EB14] = 0x00;
611 tda18271_write_regs(fe, R_EB14, 1);
612
613 regs[R_EB20] = 0xcc;
614 tda18271_write_regs(fe, R_EB20, 1);
615
Michael Krufky255b5112008-01-01 22:52:09 -0300616 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300617 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300618
Michael Krufky255b5112008-01-01 22:52:09 -0300619 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300620
621 switch (priv->mode) {
622 case TDA18271_ANALOG:
623 N = freq - 1250000;
624 break;
625 case TDA18271_DIGITAL:
626 N = freq + bw / 2;
627 break;
628 }
629
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300630 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300631
Michael Krufky255b5112008-01-01 22:52:09 -0300632 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300633
634 switch (priv->mode) {
635 case TDA18271_ANALOG:
636 N = freq - 250000;
637 break;
638 case TDA18271_DIGITAL:
639 N = freq + bw / 2 + 1000000;
640 break;
641 }
642
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300643 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300644
645 tda18271_write_regs(fe, R_EP3, 11);
646 msleep(5); /* RF tracking filter calibration initialization */
647
Michael Krufky255b5112008-01-01 22:52:09 -0300648 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300649 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300650 tda18271_write_regs(fe, R_EB13, 1);
651
Michael Krufky255b5112008-01-01 22:52:09 -0300652 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300653 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300654
Michael Krufky255b5112008-01-01 22:52:09 -0300655 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300656 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300657
658 tda18271_write_regs(fe, R_EP2, 1);
659 tda18271_write_regs(fe, R_EP1, 1);
660 tda18271_write_regs(fe, R_EP2, 1);
661 tda18271_write_regs(fe, R_EP1, 1);
662
663 regs[R_EB4] &= 0x07;
664 regs[R_EB4] |= 0x40;
665 tda18271_write_regs(fe, R_EB4, 1);
666
667 regs[R_EB7] = 0x40;
668 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300669 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300670
671 regs[R_EB20] = 0xec;
672 tda18271_write_regs(fe, R_EB20, 1);
673 msleep(60); /* RF tracking filter calibration completion */
674
675 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
676 tda18271_write_regs(fe, R_EP4, 1);
677
678 tda18271_write_regs(fe, R_EP1, 1);
679
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300680 /* RF tracking filter correction for VHF_Low band */
681 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300682 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300683
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300684 return 0;
685}
686
Michael Krufkyd1c53422008-04-22 14:42:07 -0300687/* ------------------------------------------------------------------ */
688
Michael Krufky12afe372008-04-22 14:42:07 -0300689static int tda18271_ir_cal_init(struct dvb_frontend *fe)
690{
691 struct tda18271_priv *priv = fe->tuner_priv;
692 unsigned char *regs = priv->tda18271_regs;
693
694 tda18271_read_regs(fe);
695
696 /* test IR_CAL_OK to see if we need init */
697 if ((regs[R_EP1] & 0x08) == 0)
698 tda18271_init_regs(fe);
699
700 return 0;
701}
702
703static int tda18271_init(struct dvb_frontend *fe)
704{
705 struct tda18271_priv *priv = fe->tuner_priv;
706
707 mutex_lock(&priv->lock);
708
709 /* power up */
710 tda18271_set_standby_mode(fe, 0, 0, 0);
711
712 /* initialization */
713 tda18271_ir_cal_init(fe);
714
715 if (priv->id == TDA18271HDC2)
716 tda18271c2_rf_cal_init(fe);
717
718 mutex_unlock(&priv->lock);
719
720 return 0;
721}
722
Michael Krufkyd1c53422008-04-22 14:42:07 -0300723static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300724 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300725{
726 struct tda18271_priv *priv = fe->tuner_priv;
727
Michael Krufky7f7203d2008-04-22 14:46:06 -0300728 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
729 freq, map->if_freq, bw, map->agc_mode, map->std);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300730
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300731 tda18271_init(fe);
732
733 mutex_lock(&priv->lock);
734
Michael Krufkyd1c53422008-04-22 14:42:07 -0300735 switch (priv->id) {
736 case TDA18271HDC1:
737 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
738 break;
739 case TDA18271HDC2:
740 tda18271c2_rf_tracking_filters_correction(fe, freq);
741 break;
742 }
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300743 tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300744
Michael Krufky8d316bf2008-01-06 15:31:35 -0300745 mutex_unlock(&priv->lock);
Michael Krufky6ca04de2007-11-23 16:52:15 -0300746
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300747 return 0;
748}
749
750/* ------------------------------------------------------------------ */
751
752static int tda18271_set_params(struct dvb_frontend *fe,
753 struct dvb_frontend_parameters *params)
754{
755 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300756 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300757 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300758 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300759 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300760
761 priv->mode = TDA18271_DIGITAL;
762
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300763 if (fe->ops.info.type == FE_ATSC) {
764 switch (params->u.vsb.modulation) {
765 case VSB_8:
766 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300767 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300768 break;
769 case QAM_64:
770 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300771 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300772 break;
773 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300774 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300775 return -EINVAL;
776 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300777#if 0
778 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300779 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300780#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300781 bw = 6000000;
782 } else if (fe->ops.info.type == FE_OFDM) {
783 switch (params->u.ofdm.bandwidth) {
784 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300785 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300786 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300787 break;
788 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300789 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300790 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300791 break;
792 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300793 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300794 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300795 break;
796 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300797 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300798 return -EINVAL;
799 }
800 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300801 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300802 return -EINVAL;
803 }
804
Michael Krufkyed736832008-01-19 17:41:04 -0300805 /* When tuning digital, the analog demod must be tri-stated */
806 if (fe->ops.analog_ops.standby)
807 fe->ops.analog_ops.standby(fe);
808
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300809 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300810
811 if (ret < 0)
812 goto fail;
813
814 priv->frequency = freq;
815 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
816 params->u.ofdm.bandwidth : 0;
817fail:
818 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300819}
820
821static int tda18271_set_analog_params(struct dvb_frontend *fe,
822 struct analog_parameters *params)
823{
824 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300825 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300826 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300827 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300828 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300829 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300830
831 priv->mode = TDA18271_ANALOG;
832
Michael Krufkyc353f422008-01-08 10:38:10 -0300833 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc353f422008-01-08 10:38:10 -0300834 freq = freq / 1000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300835 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -0300836 mode = "fm";
837 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300838 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300839 mode = "MN";
840 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300841 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300842 mode = "B";
843 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300844 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300845 mode = "GH";
846 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300847 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300848 mode = "I";
849 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300850 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300851 mode = "DK";
852 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300853 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300854 mode = "L";
855 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300856 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -0300857 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300858 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300859 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300860 mode = "xx";
861 }
862
Michael Krufky182519f2007-12-25 15:10:11 -0300863 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300864
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300865 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300866
867 if (ret < 0)
868 goto fail;
869
870 priv->frequency = freq;
871 priv->bandwidth = 0;
872fail:
873 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300874}
875
Michael Krufky518d8732008-01-13 17:01:01 -0300876static int tda18271_sleep(struct dvb_frontend *fe)
877{
878 struct tda18271_priv *priv = fe->tuner_priv;
879
880 mutex_lock(&priv->lock);
881
882 /* standby mode w/ slave tuner output
883 * & loop thru & xtal oscillator on */
884 tda18271_set_standby_mode(fe, 1, 0, 0);
885
886 mutex_unlock(&priv->lock);
887
888 return 0;
889}
890
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300891static int tda18271_release(struct dvb_frontend *fe)
892{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300893 struct tda18271_priv *priv = fe->tuner_priv;
894
895 mutex_lock(&tda18271_list_mutex);
896
Michael Krufkyf9e315a2008-04-22 14:41:54 -0300897 if (priv)
898 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -0300899
Michael Krufkya4f263b2008-01-06 15:52:56 -0300900 mutex_unlock(&tda18271_list_mutex);
901
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300902 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -0300903
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300904 return 0;
905}
906
907static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
908{
909 struct tda18271_priv *priv = fe->tuner_priv;
910 *frequency = priv->frequency;
911 return 0;
912}
913
914static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
915{
916 struct tda18271_priv *priv = fe->tuner_priv;
917 *bandwidth = priv->bandwidth;
918 return 0;
919}
920
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300921/* ------------------------------------------------------------------ */
922
923#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -0300924 if (map->std_cfg.if_freq + \
925 map->std_cfg.agc_mode + map->std_cfg.std > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300926 tda_dbg("Using custom std config for %s\n", name); \
927 memcpy(&std->std_cfg, &map->std_cfg, \
928 sizeof(struct tda18271_std_map_item)); \
929 } } while (0)
930
931#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -0300932 tda_dbg("(%s) if freq = %d, agc_mode = %d, std = %d\n", \
933 name, std->std_cfg.if_freq, \
934 std->std_cfg.agc_mode, std->std_cfg.std); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300935 } while (0)
936
937static int tda18271_dump_std_map(struct dvb_frontend *fe)
938{
939 struct tda18271_priv *priv = fe->tuner_priv;
940 struct tda18271_std_map *std = &priv->std;
941
942 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc353f422008-01-08 10:38:10 -0300943 tda18271_dump_std_item(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300944 tda18271_dump_std_item(atv_b, "pal b");
945 tda18271_dump_std_item(atv_dk, "pal dk");
946 tda18271_dump_std_item(atv_gh, "pal gh");
947 tda18271_dump_std_item(atv_i, "pal i");
948 tda18271_dump_std_item(atv_l, "pal l");
949 tda18271_dump_std_item(atv_lc, "pal l'");
950 tda18271_dump_std_item(atv_mn, "atv mn");
951 tda18271_dump_std_item(atsc_6, "atsc 6");
952 tda18271_dump_std_item(dvbt_6, "dvbt 6");
953 tda18271_dump_std_item(dvbt_7, "dvbt 7");
954 tda18271_dump_std_item(dvbt_8, "dvbt 8");
955 tda18271_dump_std_item(qam_6, "qam 6");
956 tda18271_dump_std_item(qam_8, "qam 8");
957
958 return 0;
959}
960
961static int tda18271_update_std_map(struct dvb_frontend *fe,
962 struct tda18271_std_map *map)
963{
964 struct tda18271_priv *priv = fe->tuner_priv;
965 struct tda18271_std_map *std = &priv->std;
966
967 if (!map)
968 return -EINVAL;
969
Michael Krufkyc353f422008-01-08 10:38:10 -0300970 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300971 tda18271_update_std(atv_b, "atv b");
972 tda18271_update_std(atv_dk, "atv dk");
973 tda18271_update_std(atv_gh, "atv gh");
974 tda18271_update_std(atv_i, "atv i");
975 tda18271_update_std(atv_l, "atv l");
976 tda18271_update_std(atv_lc, "atv l'");
977 tda18271_update_std(atv_mn, "atv mn");
978 tda18271_update_std(atsc_6, "atsc 6");
979 tda18271_update_std(dvbt_6, "dvbt 6");
980 tda18271_update_std(dvbt_7, "dvbt 7");
981 tda18271_update_std(dvbt_8, "dvbt 8");
982 tda18271_update_std(qam_6, "qam 6");
983 tda18271_update_std(qam_8, "qam 8");
984
985 return 0;
986}
987
Michael Krufky49e7aaf2007-12-24 04:15:20 -0300988static int tda18271_get_id(struct dvb_frontend *fe)
989{
990 struct tda18271_priv *priv = fe->tuner_priv;
991 unsigned char *regs = priv->tda18271_regs;
992 char *name;
993 int ret = 0;
994
Michael Krufky8d316bf2008-01-06 15:31:35 -0300995 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -0300996 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -0300997 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -0300998
999 switch (regs[R_ID] & 0x7f) {
1000 case 3:
1001 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001002 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001003 break;
1004 case 4:
1005 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001006 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001007 break;
1008 default:
1009 name = "Unknown device";
1010 ret = -EINVAL;
1011 break;
1012 }
1013
Michael Krufky182519f2007-12-25 15:10:11 -03001014 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001015 i2c_adapter_id(priv->i2c_props.adap),
1016 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001017 (0 == ret) ? "" : ", device not supported.");
1018
1019 return ret;
1020}
1021
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001022static struct dvb_tuner_ops tda18271_tuner_ops = {
1023 .info = {
1024 .name = "NXP TDA18271HD",
1025 .frequency_min = 45000000,
1026 .frequency_max = 864000000,
1027 .frequency_step = 62500
1028 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001029 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001030 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001031 .set_params = tda18271_set_params,
1032 .set_analog_params = tda18271_set_analog_params,
1033 .release = tda18271_release,
1034 .get_frequency = tda18271_get_frequency,
1035 .get_bandwidth = tda18271_get_bandwidth,
1036};
1037
1038struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001039 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001040 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001041{
1042 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001043 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001044
Michael Krufkya4f263b2008-01-06 15:52:56 -03001045 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001046
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001047 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1048 hybrid_tuner_instance_list,
1049 i2c, addr, "tda18271");
1050 switch (instance) {
1051 case 0:
1052 goto fail;
1053 break;
1054 case 1:
1055 /* new tuner instance */
Michael Krufkya4f263b2008-01-06 15:52:56 -03001056 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1057 priv->cal_initialized = false;
1058 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001059
Michael Krufkya4f263b2008-01-06 15:52:56 -03001060 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001061
Michael Krufky55553092008-04-22 14:46:06 -03001062 if (cfg)
1063 priv->small_i2c = cfg->small_i2c;
1064
Michael Krufkya4f263b2008-01-06 15:52:56 -03001065 if (tda18271_get_id(fe) < 0)
1066 goto fail;
1067
1068 if (tda18271_assign_map_layout(fe) < 0)
1069 goto fail;
1070
1071 mutex_lock(&priv->lock);
1072 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001073
1074 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001075 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001076
Michael Krufkya4f263b2008-01-06 15:52:56 -03001077 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001078 break;
1079 default:
1080 /* existing tuner instance */
1081 fe->tuner_priv = priv;
1082
1083 /* allow dvb driver to override i2c gate setting */
1084 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1085 priv->gate = cfg->gate;
1086 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001087 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001088
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001089 /* override default std map with values in config struct */
1090 if ((cfg) && (cfg->std_map))
1091 tda18271_update_std_map(fe, cfg->std_map);
1092
Michael Krufkya4f263b2008-01-06 15:52:56 -03001093 mutex_unlock(&tda18271_list_mutex);
1094
1095 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1096 sizeof(struct dvb_tuner_ops));
1097
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001098 if (tda18271_debug & DBG_MAP)
1099 tda18271_dump_std_map(fe);
1100
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001101 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001102fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001103 mutex_unlock(&tda18271_list_mutex);
1104
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001105 tda18271_release(fe);
1106 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001107}
1108EXPORT_SYMBOL_GPL(tda18271_attach);
1109MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1110MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1111MODULE_LICENSE("GPL");
Michael Krufky255b5112008-01-01 22:52:09 -03001112MODULE_VERSION("0.2");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001113
1114/*
1115 * Overrides for Emacs so that we follow Linus's tabbing style.
1116 * ---------------------------------------------------------------------------
1117 * Local variables:
1118 * c-basic-offset: 8
1119 * End:
1120 */