blob: 98acd588739ab37361bb0ca9c2627105364077c1 [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 Krufky44e645c2008-06-08 20:10:29 -030048static inline void tda18271_set_if_notch(struct dvb_frontend *fe)
49{
50 struct tda18271_priv *priv = fe->tuner_priv;
51 unsigned char *regs = priv->tda18271_regs;
52
53 switch (priv->mode) {
54 case TDA18271_ANALOG:
55 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
56 break;
57 case TDA18271_DIGITAL:
58 regs[R_MPD] |= 0x80; /* IF notch = 1 */
59 break;
60 }
61}
62
Michael Krufky255b5112008-01-01 22:52:09 -030063static int tda18271_channel_configuration(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -030064 struct tda18271_std_map_item *map,
65 u32 freq, u32 bw)
Michael Krufky255b5112008-01-01 22:52:09 -030066{
67 struct tda18271_priv *priv = fe->tuner_priv;
68 unsigned char *regs = priv->tda18271_regs;
Michael Krufky31940e32008-05-04 19:37:27 -030069 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -030070 u32 N;
71
72 /* update TV broadcast parameters */
73
74 /* set standard */
75 regs[R_EP3] &= ~0x1f; /* clear std bits */
Michael Krufky7f7203d2008-04-22 14:46:06 -030076 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
Michael Krufky255b5112008-01-01 22:52:09 -030077
Michael Krufky40194b22008-04-22 14:46:22 -030078 /* set rfagc to high speed mode */
79 regs[R_EP3] &= ~0x04;
80
Michael Krufky255b5112008-01-01 22:52:09 -030081 /* set cal mode to normal */
82 regs[R_EP4] &= ~0x03;
83
Michael Krufky44e645c2008-06-08 20:10:29 -030084 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -030085 regs[R_EP4] &= ~0x1c; /* clear if level bits */
Michael Krufky14c74b22008-04-22 14:46:21 -030086 regs[R_EP4] |= (map->if_lvl << 2);
Michael Krufky255b5112008-01-01 22:52:09 -030087
Michael Krufkyc293d0a2008-04-22 14:46:06 -030088 /* update FM_RFn */
89 regs[R_EP4] &= ~0x80;
90 regs[R_EP4] |= map->fm_rfn << 7;
Michael Krufky255b5112008-01-01 22:52:09 -030091
Michael Krufkyc0dc0c12008-04-22 14:46:22 -030092 /* update rf top / if top */
93 regs[R_EB22] = 0x00;
94 regs[R_EB22] |= map->rfagc_top;
Michael Krufky31940e32008-05-04 19:37:27 -030095 ret = tda18271_write_regs(fe, R_EB22, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -030096 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -030097 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -030098
99 /* --------------------------------------------------------------- */
100
101 /* disable Power Level Indicator */
102 regs[R_EP1] |= 0x40;
103
104 /* frequency dependent parameters */
105
106 tda18271_calc_ir_measure(fe, &freq);
107
108 tda18271_calc_bp_filter(fe, &freq);
109
110 tda18271_calc_rf_band(fe, &freq);
111
112 tda18271_calc_gain_taper(fe, &freq);
113
114 /* --------------------------------------------------------------- */
115
116 /* dual tuner and agc1 extra configuration */
117
Michael Krufky868f5cc2008-04-22 14:46:23 -0300118 switch (priv->role) {
119 case TDA18271_MASTER:
120 regs[R_EB1] |= 0x04; /* main vco */
121 break;
122 case TDA18271_SLAVE:
123 regs[R_EB1] &= ~0x04; /* cal vco */
124 break;
125 }
Michael Krufky255b5112008-01-01 22:52:09 -0300126
127 /* agc1 always active */
128 regs[R_EB1] &= ~0x02;
129
130 /* agc1 has priority on agc2 */
131 regs[R_EB1] &= ~0x01;
132
Michael Krufky31940e32008-05-04 19:37:27 -0300133 ret = tda18271_write_regs(fe, R_EB1, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300134 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300135 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300136
137 /* --------------------------------------------------------------- */
138
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300139 N = map->if_freq * 1000 + freq;
Michael Krufky255b5112008-01-01 22:52:09 -0300140
Michael Krufky868f5cc2008-04-22 14:46:23 -0300141 switch (priv->role) {
142 case TDA18271_MASTER:
143 tda18271_calc_main_pll(fe, N);
Michael Krufky44e645c2008-06-08 20:10:29 -0300144 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300145 tda18271_write_regs(fe, R_MPD, 4);
146 break;
147 case TDA18271_SLAVE:
148 tda18271_calc_cal_pll(fe, N);
149 tda18271_write_regs(fe, R_CPD, 4);
150
151 regs[R_MPD] = regs[R_CPD] & 0x7f;
Michael Krufky44e645c2008-06-08 20:10:29 -0300152 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300153 tda18271_write_regs(fe, R_MPD, 1);
154 break;
155 }
Michael Krufky255b5112008-01-01 22:52:09 -0300156
Michael Krufky31940e32008-05-04 19:37:27 -0300157 ret = tda18271_write_regs(fe, R_TM, 7);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300158 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300159 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300160
Michael Krufky868f5cc2008-04-22 14:46:23 -0300161 /* force charge pump source */
162 charge_pump_source(fe, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300163
164 msleep(1);
165
Michael Krufky868f5cc2008-04-22 14:46:23 -0300166 /* return pll to normal operation */
167 charge_pump_source(fe, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300168
Michael Krufky40194b22008-04-22 14:46:22 -0300169 msleep(20);
170
171 /* set rfagc to normal speed mode */
172 if (map->fm_rfn)
173 regs[R_EP3] &= ~0x04;
174 else
175 regs[R_EP3] |= 0x04;
Michael Krufky31940e32008-05-04 19:37:27 -0300176 ret = tda18271_write_regs(fe, R_EP3, 1);
177fail:
178 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300179}
180
181static int tda18271_read_thermometer(struct dvb_frontend *fe)
182{
183 struct tda18271_priv *priv = fe->tuner_priv;
184 unsigned char *regs = priv->tda18271_regs;
185 int tm;
186
187 /* switch thermometer on */
188 regs[R_TM] |= 0x10;
189 tda18271_write_regs(fe, R_TM, 1);
190
191 /* read thermometer info */
192 tda18271_read_regs(fe);
193
194 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
195 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
196
197 if ((regs[R_TM] & 0x20) == 0x20)
198 regs[R_TM] &= ~0x20;
199 else
200 regs[R_TM] |= 0x20;
201
202 tda18271_write_regs(fe, R_TM, 1);
203
204 msleep(10); /* temperature sensing */
205
206 /* read thermometer info */
207 tda18271_read_regs(fe);
208 }
209
210 tm = tda18271_lookup_thermometer(fe);
211
212 /* switch thermometer off */
213 regs[R_TM] &= ~0x10;
214 tda18271_write_regs(fe, R_TM, 1);
215
216 /* set CAL mode to normal */
217 regs[R_EP4] &= ~0x03;
218 tda18271_write_regs(fe, R_EP4, 1);
219
220 return tm;
221}
222
Michael Krufky12afe372008-04-22 14:42:07 -0300223/* ------------------------------------------------------------------ */
224
Michael Krufkyd1c53422008-04-22 14:42:07 -0300225static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
226 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300227{
228 struct tda18271_priv *priv = fe->tuner_priv;
229 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
230 unsigned char *regs = priv->tda18271_regs;
Michael Krufky20f42062008-05-04 19:57:06 -0300231 int tm_current, rfcal_comp, approx, i, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300232 u8 dc_over_dt, rf_tab;
233
234 /* power up */
Michael Krufky20f42062008-05-04 19:57:06 -0300235 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300236 if (tda_fail(ret))
Michael Krufky20f42062008-05-04 19:57:06 -0300237 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300238
239 /* read die current temperature */
240 tm_current = tda18271_read_thermometer(fe);
241
242 /* frequency dependent parameters */
243
244 tda18271_calc_rf_cal(fe, &freq);
245 rf_tab = regs[R_EB14];
246
247 i = tda18271_lookup_rf_band(fe, &freq, NULL);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300248 if (tda_fail(i))
249 return i;
Michael Krufky255b5112008-01-01 22:52:09 -0300250
251 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
252 approx = map[i].rf_a1 *
253 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
254 } else {
255 approx = map[i].rf_a2 *
256 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
257 }
258
259 if (approx < 0)
260 approx = 0;
261 if (approx > 255)
262 approx = 255;
263
264 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
265
266 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300267 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300268
269 regs[R_EB14] = approx + rfcal_comp;
Michael Krufky20f42062008-05-04 19:57:06 -0300270 ret = tda18271_write_regs(fe, R_EB14, 1);
271fail:
272 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300273}
274
275static int tda18271_por(struct dvb_frontend *fe)
276{
277 struct tda18271_priv *priv = fe->tuner_priv;
278 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300279 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300280
281 /* power up detector 1 */
282 regs[R_EB12] &= ~0x20;
Michael Krufky24124f72008-05-03 19:28:00 -0300283 ret = tda18271_write_regs(fe, R_EB12, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300284 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300285 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300286
287 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
288 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300289 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300290 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300291 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300292
293 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
294
295 /* POR mode */
Michael Krufky24124f72008-05-03 19:28:00 -0300296 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300297 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300298 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300299
300 /* disable 1.5 MHz low pass filter */
301 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
302 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
Michael Krufky24124f72008-05-03 19:28:00 -0300303 ret = tda18271_write_regs(fe, R_EB21, 3);
304fail:
305 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300306}
307
308static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
309{
310 struct tda18271_priv *priv = fe->tuner_priv;
311 unsigned char *regs = priv->tda18271_regs;
312 u32 N;
313
314 /* set CAL mode to normal */
315 regs[R_EP4] &= ~0x03;
316 tda18271_write_regs(fe, R_EP4, 1);
317
318 /* switch off agc1 */
319 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
320
321 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
322 tda18271_write_regs(fe, R_EB18, 1);
323
324 /* frequency dependent parameters */
325
326 tda18271_calc_bp_filter(fe, &freq);
327 tda18271_calc_gain_taper(fe, &freq);
328 tda18271_calc_rf_band(fe, &freq);
329 tda18271_calc_km(fe, &freq);
330
331 tda18271_write_regs(fe, R_EP1, 3);
332 tda18271_write_regs(fe, R_EB13, 1);
333
334 /* main pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300335 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300336
337 /* cal pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300338 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300339
340 /* force dcdc converter to 0 V */
341 regs[R_EB14] = 0x00;
342 tda18271_write_regs(fe, R_EB14, 1);
343
344 /* disable plls lock */
345 regs[R_EB20] &= ~0x20;
346 tda18271_write_regs(fe, R_EB20, 1);
347
348 /* set CAL mode to RF tracking filter calibration */
349 regs[R_EP4] |= 0x03;
350 tda18271_write_regs(fe, R_EP4, 2);
351
352 /* --------------------------------------------------------------- */
353
354 /* set the internal calibration signal */
355 N = freq;
356
Michael Krufkyae07d042008-04-22 14:46:21 -0300357 tda18271_calc_cal_pll(fe, N);
358 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300359
360 /* downconvert internal calibration */
361 N += 1000000;
362
363 tda18271_calc_main_pll(fe, N);
364 tda18271_write_regs(fe, R_MPD, 4);
365
366 msleep(5);
367
368 tda18271_write_regs(fe, R_EP2, 1);
369 tda18271_write_regs(fe, R_EP1, 1);
370 tda18271_write_regs(fe, R_EP2, 1);
371 tda18271_write_regs(fe, R_EP1, 1);
372
373 /* --------------------------------------------------------------- */
374
375 /* normal operation for the main pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300376 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300377
378 /* normal operation for the cal pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300379 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300380
Michael Krufkyae07d042008-04-22 14:46:21 -0300381 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300382
383 /* launch the rf tracking filters calibration */
384 regs[R_EB20] |= 0x20;
385 tda18271_write_regs(fe, R_EB20, 1);
386
387 msleep(60); /* calibration */
388
389 /* --------------------------------------------------------------- */
390
391 /* set CAL mode to normal */
392 regs[R_EP4] &= ~0x03;
393
394 /* switch on agc1 */
395 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
396
397 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
398 tda18271_write_regs(fe, R_EB18, 1);
399
400 tda18271_write_regs(fe, R_EP3, 2);
401
402 /* synchronization */
403 tda18271_write_regs(fe, R_EP1, 1);
404
405 /* get calibration result */
406 tda18271_read_extended(fe);
407
408 return regs[R_EB14];
409}
410
411static int tda18271_powerscan(struct dvb_frontend *fe,
412 u32 *freq_in, u32 *freq_out)
413{
414 struct tda18271_priv *priv = fe->tuner_priv;
415 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300416 int sgn, bcal, count, wait, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300417 u8 cid_target;
418 u16 count_limit;
419 u32 freq;
420
421 freq = *freq_in;
422
423 tda18271_calc_rf_band(fe, &freq);
424 tda18271_calc_rf_cal(fe, &freq);
425 tda18271_calc_gain_taper(fe, &freq);
426 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
427
428 tda18271_write_regs(fe, R_EP2, 1);
429 tda18271_write_regs(fe, R_EB14, 1);
430
431 /* downconvert frequency */
432 freq += 1000000;
433
434 tda18271_calc_main_pll(fe, freq);
435 tda18271_write_regs(fe, R_MPD, 4);
436
437 msleep(5); /* pll locking */
438
439 /* detection mode */
440 regs[R_EP4] &= ~0x03;
441 regs[R_EP4] |= 0x01;
442 tda18271_write_regs(fe, R_EP4, 1);
443
444 /* launch power detection measurement */
445 tda18271_write_regs(fe, R_EP2, 1);
446
447 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300448 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300449 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300450 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300451
452 /* algorithm initialization */
453 sgn = 1;
454 *freq_out = *freq_in;
455 bcal = 0;
456 count = 0;
457 wait = false;
458
459 while ((regs[R_EB10] & 0x3f) < cid_target) {
460 /* downconvert updated freq to 1 MHz */
461 freq = *freq_in + (sgn * count) + 1000000;
462
463 tda18271_calc_main_pll(fe, freq);
464 tda18271_write_regs(fe, R_MPD, 4);
465
466 if (wait) {
467 msleep(5); /* pll locking */
468 wait = false;
469 } else
470 udelay(100); /* pll locking */
471
472 /* launch power detection measurement */
473 tda18271_write_regs(fe, R_EP2, 1);
474
475 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300476 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300477 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300478 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300479
480 count += 200;
481
Michael Krufkye7809a02008-04-22 14:46:22 -0300482 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300483 continue;
484
485 if (sgn <= 0)
486 break;
487
488 sgn = -1 * sgn;
489 count = 200;
490 wait = true;
491 }
492
493 if ((regs[R_EB10] & 0x3f) >= cid_target) {
494 bcal = 1;
495 *freq_out = freq - 1000000;
496 } else
497 bcal = 0;
498
Michael Krufkycf04d292008-01-09 00:34:30 -0300499 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300500 bcal, *freq_in, *freq_out, freq);
501
502 return bcal;
503}
504
505static int tda18271_powerscan_init(struct dvb_frontend *fe)
506{
507 struct tda18271_priv *priv = fe->tuner_priv;
508 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300509 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300510
511 /* set standard to digital */
512 regs[R_EP3] &= ~0x1f; /* clear std bits */
513 regs[R_EP3] |= 0x12;
514
515 /* set cal mode to normal */
516 regs[R_EP4] &= ~0x03;
517
Michael Krufky44e645c2008-06-08 20:10:29 -0300518 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -0300519 regs[R_EP4] &= ~0x1c; /* clear if level bits */
520
Michael Krufky24124f72008-05-03 19:28:00 -0300521 ret = tda18271_write_regs(fe, R_EP3, 2);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300522 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300523 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300524
525 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300526 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300527 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300528 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300529
530 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
531
532 /* 1.5 MHz low pass filter */
533 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
534 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
535
Michael Krufky24124f72008-05-03 19:28:00 -0300536 ret = tda18271_write_regs(fe, R_EB21, 3);
537fail:
538 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300539}
540
541static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
542{
543 struct tda18271_priv *priv = fe->tuner_priv;
544 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
545 unsigned char *regs = priv->tda18271_regs;
546 int bcal, rf, i;
547#define RF1 0
548#define RF2 1
549#define RF3 2
550 u32 rf_default[3];
551 u32 rf_freq[3];
552 u8 prog_cal[3];
553 u8 prog_tab[3];
554
555 i = tda18271_lookup_rf_band(fe, &freq, NULL);
556
Michael Krufky4bd5d102008-05-04 21:32:21 -0300557 if (tda_fail(i))
Michael Krufky255b5112008-01-01 22:52:09 -0300558 return i;
559
560 rf_default[RF1] = 1000 * map[i].rf1_def;
561 rf_default[RF2] = 1000 * map[i].rf2_def;
562 rf_default[RF3] = 1000 * map[i].rf3_def;
563
564 for (rf = RF1; rf <= RF3; rf++) {
565 if (0 == rf_default[rf])
566 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300567 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300568
569 /* look for optimized calibration frequency */
570 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300571 if (tda_fail(bcal))
Michael Krufky24124f72008-05-03 19:28:00 -0300572 return bcal;
Michael Krufky255b5112008-01-01 22:52:09 -0300573
574 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
575 prog_tab[rf] = regs[R_EB14];
576
577 if (1 == bcal)
578 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
579 else
580 prog_cal[rf] = prog_tab[rf];
581
582 switch (rf) {
583 case RF1:
584 map[i].rf_a1 = 0;
585 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
586 map[i].rf1 = rf_freq[RF1] / 1000;
587 break;
588 case RF2:
589 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
590 prog_cal[RF1] + prog_tab[RF1]) /
591 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
592 map[i].rf2 = rf_freq[RF2] / 1000;
593 break;
594 case RF3:
595 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
596 prog_cal[RF2] + prog_tab[RF2]) /
597 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
598 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
599 map[i].rf3 = rf_freq[RF3] / 1000;
600 break;
601 default:
602 BUG();
603 }
604 }
605
606 return 0;
607}
608
Michael Krufky09f83c42008-01-05 20:00:09 -0300609static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300610{
611 struct tda18271_priv *priv = fe->tuner_priv;
612 unsigned int i;
Michael Krufky24124f72008-05-03 19:28:00 -0300613 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300614
615 tda_info("tda18271: performing RF tracking filter calibration\n");
616
617 /* wait for die temperature stabilization */
618 msleep(200);
619
Michael Krufky24124f72008-05-03 19:28:00 -0300620 ret = tda18271_powerscan_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300621 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300622 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300623
624 /* rf band calibration */
Michael Krufkyc151c322008-05-04 17:54:23 -0300625 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
626 ret =
Michael Krufky255b5112008-01-01 22:52:09 -0300627 tda18271_rf_tracking_filters_init(fe, 1000 *
628 priv->rf_cal_state[i].rfmax);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300629 if (tda_fail(ret))
Michael Krufkyc151c322008-05-04 17:54:23 -0300630 goto fail;
631 }
Michael Krufky255b5112008-01-01 22:52:09 -0300632
Michael Krufky09f83c42008-01-05 20:00:09 -0300633 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky24124f72008-05-03 19:28:00 -0300634fail:
635 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300636}
637
638/* ------------------------------------------------------------------ */
639
Michael Krufky12afe372008-04-22 14:42:07 -0300640static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300641{
642 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300643 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300644 int ret;
Michael Krufky839c6c92008-01-13 18:29:44 -0300645
646 /* test RF_CAL_OK to see if we need init */
647 if ((regs[R_EP1] & 0x10) == 0)
648 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300649
650 if (priv->cal_initialized)
651 return 0;
652
Michael Krufky24124f72008-05-03 19:28:00 -0300653 ret = tda18271_calc_rf_filter_curve(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300654 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300655 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300656
Michael Krufky24124f72008-05-03 19:28:00 -0300657 ret = tda18271_por(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300658 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300659 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300660
Michael Krufky6bfa6652008-01-07 00:51:48 -0300661 tda_info("tda18271: RF tracking filter calibration complete\n");
662
Michael Krufky255b5112008-01-01 22:52:09 -0300663 priv->cal_initialized = true;
Michael Krufkyc151c322008-05-04 17:54:23 -0300664 goto end;
Michael Krufky24124f72008-05-03 19:28:00 -0300665fail:
Michael Krufkyc151c322008-05-04 17:54:23 -0300666 tda_info("tda18271: RF tracking filter calibration failed!\n");
667end:
Michael Krufky24124f72008-05-03 19:28:00 -0300668 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300669}
670
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300671static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
672 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300673{
674 struct tda18271_priv *priv = fe->tuner_priv;
675 unsigned char *regs = priv->tda18271_regs;
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300676 int ret;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300677 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300678
Michael Krufky255b5112008-01-01 22:52:09 -0300679 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300680 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300681 tda18271_write_regs(fe, R_EP1, 1);
682
683 regs[R_EB4] &= 0x07;
684 regs[R_EB4] |= 0x60;
685 tda18271_write_regs(fe, R_EB4, 1);
686
687 regs[R_EB7] = 0x60;
688 tda18271_write_regs(fe, R_EB7, 1);
689
690 regs[R_EB14] = 0x00;
691 tda18271_write_regs(fe, R_EB14, 1);
692
693 regs[R_EB20] = 0xcc;
694 tda18271_write_regs(fe, R_EB20, 1);
695
Michael Krufky255b5112008-01-01 22:52:09 -0300696 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300697 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300698
Michael Krufky255b5112008-01-01 22:52:09 -0300699 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300700
701 switch (priv->mode) {
702 case TDA18271_ANALOG:
703 N = freq - 1250000;
704 break;
705 case TDA18271_DIGITAL:
706 N = freq + bw / 2;
707 break;
708 }
709
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300710 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300711
Michael Krufky255b5112008-01-01 22:52:09 -0300712 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300713
714 switch (priv->mode) {
715 case TDA18271_ANALOG:
716 N = freq - 250000;
717 break;
718 case TDA18271_DIGITAL:
719 N = freq + bw / 2 + 1000000;
720 break;
721 }
722
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300723 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300724
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300725 ret = tda18271_write_regs(fe, R_EP3, 11);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300726 if (tda_fail(ret))
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300727 return ret;
728
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300729 msleep(5); /* RF tracking filter calibration initialization */
730
Michael Krufky255b5112008-01-01 22:52:09 -0300731 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300732 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300733 tda18271_write_regs(fe, R_EB13, 1);
734
Michael Krufky255b5112008-01-01 22:52:09 -0300735 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300736 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300737
Michael Krufky255b5112008-01-01 22:52:09 -0300738 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300739 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300740
741 tda18271_write_regs(fe, R_EP2, 1);
742 tda18271_write_regs(fe, R_EP1, 1);
743 tda18271_write_regs(fe, R_EP2, 1);
744 tda18271_write_regs(fe, R_EP1, 1);
745
746 regs[R_EB4] &= 0x07;
747 regs[R_EB4] |= 0x40;
748 tda18271_write_regs(fe, R_EB4, 1);
749
750 regs[R_EB7] = 0x40;
751 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300752 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300753
754 regs[R_EB20] = 0xec;
755 tda18271_write_regs(fe, R_EB20, 1);
756 msleep(60); /* RF tracking filter calibration completion */
757
758 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
759 tda18271_write_regs(fe, R_EP4, 1);
760
761 tda18271_write_regs(fe, R_EP1, 1);
762
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300763 /* RF tracking filter correction for VHF_Low band */
764 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300765 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300766
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300767 return 0;
768}
769
Michael Krufkyd1c53422008-04-22 14:42:07 -0300770/* ------------------------------------------------------------------ */
771
Michael Krufky12afe372008-04-22 14:42:07 -0300772static int tda18271_ir_cal_init(struct dvb_frontend *fe)
773{
774 struct tda18271_priv *priv = fe->tuner_priv;
775 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300776 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300777
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300778 ret = tda18271_read_regs(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300779 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300780 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300781
782 /* test IR_CAL_OK to see if we need init */
783 if ((regs[R_EP1] & 0x08) == 0)
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300784 ret = tda18271_init_regs(fe);
785fail:
786 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300787}
788
789static int tda18271_init(struct dvb_frontend *fe)
790{
791 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300792 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300793
794 mutex_lock(&priv->lock);
795
796 /* power up */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300797 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300798 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300799 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300800
801 /* initialization */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300802 ret = tda18271_ir_cal_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300803 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300804 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300805
806 if (priv->id == TDA18271HDC2)
807 tda18271c2_rf_cal_init(fe);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300808fail:
Michael Krufky12afe372008-04-22 14:42:07 -0300809 mutex_unlock(&priv->lock);
810
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300811 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300812}
813
Michael Krufkyd1c53422008-04-22 14:42:07 -0300814static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300815 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300816{
817 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300818 int ret;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300819
Michael Krufky7f7203d2008-04-22 14:46:06 -0300820 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
821 freq, map->if_freq, bw, map->agc_mode, map->std);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300822
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300823 ret = tda18271_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300824 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300825 goto fail;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300826
827 mutex_lock(&priv->lock);
828
Michael Krufkyd1c53422008-04-22 14:42:07 -0300829 switch (priv->id) {
830 case TDA18271HDC1:
831 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
832 break;
833 case TDA18271HDC2:
834 tda18271c2_rf_tracking_filters_correction(fe, freq);
835 break;
836 }
Michael Krufky31940e32008-05-04 19:37:27 -0300837 ret = tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300838
Michael Krufky8d316bf2008-01-06 15:31:35 -0300839 mutex_unlock(&priv->lock);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300840fail:
841 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300842}
843
844/* ------------------------------------------------------------------ */
845
846static int tda18271_set_params(struct dvb_frontend *fe,
847 struct dvb_frontend_parameters *params)
848{
849 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300850 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300851 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300852 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300853 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300854
855 priv->mode = TDA18271_DIGITAL;
856
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300857 if (fe->ops.info.type == FE_ATSC) {
858 switch (params->u.vsb.modulation) {
859 case VSB_8:
860 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300861 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300862 break;
863 case QAM_64:
864 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300865 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300866 break;
867 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300868 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300869 return -EINVAL;
870 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300871#if 0
872 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300873 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300874#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300875 bw = 6000000;
876 } else if (fe->ops.info.type == FE_OFDM) {
877 switch (params->u.ofdm.bandwidth) {
878 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300879 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300880 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300881 break;
882 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300883 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300884 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300885 break;
886 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300887 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300888 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300889 break;
890 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300891 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300892 return -EINVAL;
893 }
894 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300895 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300896 return -EINVAL;
897 }
898
Michael Krufkyed736832008-01-19 17:41:04 -0300899 /* When tuning digital, the analog demod must be tri-stated */
900 if (fe->ops.analog_ops.standby)
901 fe->ops.analog_ops.standby(fe);
902
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300903 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300904
Michael Krufky4bd5d102008-05-04 21:32:21 -0300905 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300906 goto fail;
907
908 priv->frequency = freq;
909 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
910 params->u.ofdm.bandwidth : 0;
911fail:
912 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300913}
914
915static int tda18271_set_analog_params(struct dvb_frontend *fe,
916 struct analog_parameters *params)
917{
918 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300919 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300920 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300921 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300922 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300923 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300924
925 priv->mode = TDA18271_ANALOG;
926
Michael Krufkyc353f422008-01-08 10:38:10 -0300927 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc353f422008-01-08 10:38:10 -0300928 freq = freq / 1000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300929 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -0300930 mode = "fm";
931 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300932 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300933 mode = "MN";
934 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300935 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300936 mode = "B";
937 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300938 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300939 mode = "GH";
940 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300941 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300942 mode = "I";
943 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300944 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300945 mode = "DK";
946 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300947 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300948 mode = "L";
949 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300950 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -0300951 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300952 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300953 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300954 mode = "xx";
955 }
956
Michael Krufky182519f2007-12-25 15:10:11 -0300957 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300958
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300959 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300960
Michael Krufky4bd5d102008-05-04 21:32:21 -0300961 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300962 goto fail;
963
964 priv->frequency = freq;
965 priv->bandwidth = 0;
966fail:
967 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300968}
969
Michael Krufky518d8732008-01-13 17:01:01 -0300970static int tda18271_sleep(struct dvb_frontend *fe)
971{
972 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300973 int ret;
Michael Krufky518d8732008-01-13 17:01:01 -0300974
975 mutex_lock(&priv->lock);
976
977 /* standby mode w/ slave tuner output
978 * & loop thru & xtal oscillator on */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300979 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky518d8732008-01-13 17:01:01 -0300980
981 mutex_unlock(&priv->lock);
982
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300983 return ret;
Michael Krufky518d8732008-01-13 17:01:01 -0300984}
985
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300986static int tda18271_release(struct dvb_frontend *fe)
987{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300988 struct tda18271_priv *priv = fe->tuner_priv;
989
990 mutex_lock(&tda18271_list_mutex);
991
Michael Krufkyf9e315a2008-04-22 14:41:54 -0300992 if (priv)
993 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -0300994
Michael Krufkya4f263b2008-01-06 15:52:56 -0300995 mutex_unlock(&tda18271_list_mutex);
996
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300997 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -0300998
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300999 return 0;
1000}
1001
1002static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1003{
1004 struct tda18271_priv *priv = fe->tuner_priv;
1005 *frequency = priv->frequency;
1006 return 0;
1007}
1008
1009static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1010{
1011 struct tda18271_priv *priv = fe->tuner_priv;
1012 *bandwidth = priv->bandwidth;
1013 return 0;
1014}
1015
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001016/* ------------------------------------------------------------------ */
1017
1018#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001019 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -03001020 map->std_cfg.agc_mode + map->std_cfg.std + \
1021 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001022 tda_dbg("Using custom std config for %s\n", name); \
1023 memcpy(&std->std_cfg, &map->std_cfg, \
1024 sizeof(struct tda18271_std_map_item)); \
1025 } } while (0)
1026
1027#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -03001028 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1029 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001030 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -03001031 std->std_cfg.agc_mode, std->std_cfg.std, \
1032 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001033 } while (0)
1034
1035static int tda18271_dump_std_map(struct dvb_frontend *fe)
1036{
1037 struct tda18271_priv *priv = fe->tuner_priv;
1038 struct tda18271_std_map *std = &priv->std;
1039
1040 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -03001041 tda18271_dump_std_item(fm_radio, " fm ");
1042 tda18271_dump_std_item(atv_b, "atv b ");
1043 tda18271_dump_std_item(atv_dk, "atv dk");
1044 tda18271_dump_std_item(atv_gh, "atv gh");
1045 tda18271_dump_std_item(atv_i, "atv i ");
1046 tda18271_dump_std_item(atv_l, "atv l ");
1047 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001048 tda18271_dump_std_item(atv_mn, "atv mn");
1049 tda18271_dump_std_item(atsc_6, "atsc 6");
1050 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1051 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1052 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -03001053 tda18271_dump_std_item(qam_6, "qam 6 ");
1054 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001055
1056 return 0;
1057}
1058
1059static int tda18271_update_std_map(struct dvb_frontend *fe,
1060 struct tda18271_std_map *map)
1061{
1062 struct tda18271_priv *priv = fe->tuner_priv;
1063 struct tda18271_std_map *std = &priv->std;
1064
1065 if (!map)
1066 return -EINVAL;
1067
Michael Krufkyc353f422008-01-08 10:38:10 -03001068 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001069 tda18271_update_std(atv_b, "atv b");
1070 tda18271_update_std(atv_dk, "atv dk");
1071 tda18271_update_std(atv_gh, "atv gh");
1072 tda18271_update_std(atv_i, "atv i");
1073 tda18271_update_std(atv_l, "atv l");
1074 tda18271_update_std(atv_lc, "atv l'");
1075 tda18271_update_std(atv_mn, "atv mn");
1076 tda18271_update_std(atsc_6, "atsc 6");
1077 tda18271_update_std(dvbt_6, "dvbt 6");
1078 tda18271_update_std(dvbt_7, "dvbt 7");
1079 tda18271_update_std(dvbt_8, "dvbt 8");
1080 tda18271_update_std(qam_6, "qam 6");
1081 tda18271_update_std(qam_8, "qam 8");
1082
1083 return 0;
1084}
1085
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001086static int tda18271_get_id(struct dvb_frontend *fe)
1087{
1088 struct tda18271_priv *priv = fe->tuner_priv;
1089 unsigned char *regs = priv->tda18271_regs;
1090 char *name;
1091 int ret = 0;
1092
Michael Krufky8d316bf2008-01-06 15:31:35 -03001093 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001094 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001095 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001096
1097 switch (regs[R_ID] & 0x7f) {
1098 case 3:
1099 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001100 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001101 break;
1102 case 4:
1103 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001104 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001105 break;
1106 default:
1107 name = "Unknown device";
1108 ret = -EINVAL;
1109 break;
1110 }
1111
Michael Krufky182519f2007-12-25 15:10:11 -03001112 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001113 i2c_adapter_id(priv->i2c_props.adap),
1114 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001115 (0 == ret) ? "" : ", device not supported.");
1116
1117 return ret;
1118}
1119
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001120static struct dvb_tuner_ops tda18271_tuner_ops = {
1121 .info = {
1122 .name = "NXP TDA18271HD",
1123 .frequency_min = 45000000,
1124 .frequency_max = 864000000,
1125 .frequency_step = 62500
1126 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001127 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001128 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001129 .set_params = tda18271_set_params,
1130 .set_analog_params = tda18271_set_analog_params,
1131 .release = tda18271_release,
1132 .get_frequency = tda18271_get_frequency,
1133 .get_bandwidth = tda18271_get_bandwidth,
1134};
1135
1136struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001137 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001138 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001139{
1140 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001141 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001142
Michael Krufkya4f263b2008-01-06 15:52:56 -03001143 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001144
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001145 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1146 hybrid_tuner_instance_list,
1147 i2c, addr, "tda18271");
1148 switch (instance) {
1149 case 0:
1150 goto fail;
1151 break;
1152 case 1:
1153 /* new tuner instance */
Michael Krufkya4f263b2008-01-06 15:52:56 -03001154 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
Michael Krufky868f5cc2008-04-22 14:46:23 -03001155 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001156 priv->cal_initialized = false;
1157 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001158
Michael Krufkya4f263b2008-01-06 15:52:56 -03001159 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001160
Michael Krufky55553092008-04-22 14:46:06 -03001161 if (cfg)
1162 priv->small_i2c = cfg->small_i2c;
1163
Michael Krufky4bd5d102008-05-04 21:32:21 -03001164 if (tda_fail(tda18271_get_id(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001165 goto fail;
1166
Michael Krufky4bd5d102008-05-04 21:32:21 -03001167 if (tda_fail(tda18271_assign_map_layout(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001168 goto fail;
1169
1170 mutex_lock(&priv->lock);
1171 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001172
1173 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001174 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001175
Michael Krufkya4f263b2008-01-06 15:52:56 -03001176 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001177 break;
1178 default:
1179 /* existing tuner instance */
1180 fe->tuner_priv = priv;
1181
1182 /* allow dvb driver to override i2c gate setting */
1183 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1184 priv->gate = cfg->gate;
1185 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001186 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001187
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001188 /* override default std map with values in config struct */
1189 if ((cfg) && (cfg->std_map))
1190 tda18271_update_std_map(fe, cfg->std_map);
1191
Michael Krufkya4f263b2008-01-06 15:52:56 -03001192 mutex_unlock(&tda18271_list_mutex);
1193
1194 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1195 sizeof(struct dvb_tuner_ops));
1196
Michael Krufkyc7353722008-03-30 19:40:20 -03001197 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001198 tda18271_dump_std_map(fe);
1199
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001200 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001201fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001202 mutex_unlock(&tda18271_list_mutex);
1203
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001204 tda18271_release(fe);
1205 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001206}
1207EXPORT_SYMBOL_GPL(tda18271_attach);
1208MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1209MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1210MODULE_LICENSE("GPL");
Michael Krufky5ec96b02008-04-22 14:46:23 -03001211MODULE_VERSION("0.3");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001212
1213/*
1214 * Overrides for Emacs so that we follow Linus's tabbing style.
1215 * ---------------------------------------------------------------------------
1216 * Local variables:
1217 * c-basic-offset: 8
1218 * End:
1219 */