blob: 1ebf95d6770fe3f626f58d929b8fd2eb26f92e07 [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 Krufky51858d12008-06-09 02:03:31 -030078 if (priv->id == TDA18271HDC2) {
79 /* set rfagc to high speed mode */
80 regs[R_EP3] &= ~0x04;
81 }
Michael Krufky40194b22008-04-22 14:46:22 -030082
Michael Krufky255b5112008-01-01 22:52:09 -030083 /* set cal mode to normal */
84 regs[R_EP4] &= ~0x03;
85
Michael Krufky44e645c2008-06-08 20:10:29 -030086 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -030087 regs[R_EP4] &= ~0x1c; /* clear if level bits */
Michael Krufky14c74b22008-04-22 14:46:21 -030088 regs[R_EP4] |= (map->if_lvl << 2);
Michael Krufky255b5112008-01-01 22:52:09 -030089
Michael Krufkyc293d0a2008-04-22 14:46:06 -030090 /* update FM_RFn */
91 regs[R_EP4] &= ~0x80;
92 regs[R_EP4] |= map->fm_rfn << 7;
Michael Krufky255b5112008-01-01 22:52:09 -030093
Michael Krufkyc0dc0c12008-04-22 14:46:22 -030094 /* update rf top / if top */
95 regs[R_EB22] = 0x00;
96 regs[R_EB22] |= map->rfagc_top;
Michael Krufky31940e32008-05-04 19:37:27 -030097 ret = tda18271_write_regs(fe, R_EB22, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -030098 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -030099 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300100
101 /* --------------------------------------------------------------- */
102
103 /* disable Power Level Indicator */
104 regs[R_EP1] |= 0x40;
105
106 /* frequency dependent parameters */
107
108 tda18271_calc_ir_measure(fe, &freq);
109
110 tda18271_calc_bp_filter(fe, &freq);
111
112 tda18271_calc_rf_band(fe, &freq);
113
114 tda18271_calc_gain_taper(fe, &freq);
115
116 /* --------------------------------------------------------------- */
117
118 /* dual tuner and agc1 extra configuration */
119
Michael Krufky868f5cc2008-04-22 14:46:23 -0300120 switch (priv->role) {
121 case TDA18271_MASTER:
122 regs[R_EB1] |= 0x04; /* main vco */
123 break;
124 case TDA18271_SLAVE:
125 regs[R_EB1] &= ~0x04; /* cal vco */
126 break;
127 }
Michael Krufky255b5112008-01-01 22:52:09 -0300128
129 /* agc1 always active */
130 regs[R_EB1] &= ~0x02;
131
132 /* agc1 has priority on agc2 */
133 regs[R_EB1] &= ~0x01;
134
Michael Krufky31940e32008-05-04 19:37:27 -0300135 ret = tda18271_write_regs(fe, R_EB1, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300136 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300137 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300138
139 /* --------------------------------------------------------------- */
140
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300141 N = map->if_freq * 1000 + freq;
Michael Krufky255b5112008-01-01 22:52:09 -0300142
Michael Krufky868f5cc2008-04-22 14:46:23 -0300143 switch (priv->role) {
144 case TDA18271_MASTER:
145 tda18271_calc_main_pll(fe, N);
Michael Krufky44e645c2008-06-08 20:10:29 -0300146 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300147 tda18271_write_regs(fe, R_MPD, 4);
148 break;
149 case TDA18271_SLAVE:
150 tda18271_calc_cal_pll(fe, N);
151 tda18271_write_regs(fe, R_CPD, 4);
152
153 regs[R_MPD] = regs[R_CPD] & 0x7f;
Michael Krufky44e645c2008-06-08 20:10:29 -0300154 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300155 tda18271_write_regs(fe, R_MPD, 1);
156 break;
157 }
Michael Krufky255b5112008-01-01 22:52:09 -0300158
Michael Krufky31940e32008-05-04 19:37:27 -0300159 ret = tda18271_write_regs(fe, R_TM, 7);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300160 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300161 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300162
Michael Krufky868f5cc2008-04-22 14:46:23 -0300163 /* force charge pump source */
164 charge_pump_source(fe, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300165
166 msleep(1);
167
Michael Krufky868f5cc2008-04-22 14:46:23 -0300168 /* return pll to normal operation */
169 charge_pump_source(fe, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300170
Michael Krufky40194b22008-04-22 14:46:22 -0300171 msleep(20);
172
Michael Krufky51858d12008-06-09 02:03:31 -0300173 if (priv->id == TDA18271HDC2) {
174 /* set rfagc to normal speed mode */
175 if (map->fm_rfn)
176 regs[R_EP3] &= ~0x04;
177 else
178 regs[R_EP3] |= 0x04;
179 ret = tda18271_write_regs(fe, R_EP3, 1);
180 }
Michael Krufky31940e32008-05-04 19:37:27 -0300181fail:
182 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300183}
184
185static int tda18271_read_thermometer(struct dvb_frontend *fe)
186{
187 struct tda18271_priv *priv = fe->tuner_priv;
188 unsigned char *regs = priv->tda18271_regs;
189 int tm;
190
191 /* switch thermometer on */
192 regs[R_TM] |= 0x10;
193 tda18271_write_regs(fe, R_TM, 1);
194
195 /* read thermometer info */
196 tda18271_read_regs(fe);
197
198 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
199 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
200
201 if ((regs[R_TM] & 0x20) == 0x20)
202 regs[R_TM] &= ~0x20;
203 else
204 regs[R_TM] |= 0x20;
205
206 tda18271_write_regs(fe, R_TM, 1);
207
208 msleep(10); /* temperature sensing */
209
210 /* read thermometer info */
211 tda18271_read_regs(fe);
212 }
213
214 tm = tda18271_lookup_thermometer(fe);
215
216 /* switch thermometer off */
217 regs[R_TM] &= ~0x10;
218 tda18271_write_regs(fe, R_TM, 1);
219
220 /* set CAL mode to normal */
221 regs[R_EP4] &= ~0x03;
222 tda18271_write_regs(fe, R_EP4, 1);
223
224 return tm;
225}
226
Michael Krufky12afe372008-04-22 14:42:07 -0300227/* ------------------------------------------------------------------ */
228
Michael Krufkyd1c53422008-04-22 14:42:07 -0300229static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
230 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300231{
232 struct tda18271_priv *priv = fe->tuner_priv;
233 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
234 unsigned char *regs = priv->tda18271_regs;
Michael Krufky20f42062008-05-04 19:57:06 -0300235 int tm_current, rfcal_comp, approx, i, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300236 u8 dc_over_dt, rf_tab;
237
238 /* power up */
Michael Krufky20f42062008-05-04 19:57:06 -0300239 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300240 if (tda_fail(ret))
Michael Krufky20f42062008-05-04 19:57:06 -0300241 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300242
243 /* read die current temperature */
244 tm_current = tda18271_read_thermometer(fe);
245
246 /* frequency dependent parameters */
247
248 tda18271_calc_rf_cal(fe, &freq);
249 rf_tab = regs[R_EB14];
250
251 i = tda18271_lookup_rf_band(fe, &freq, NULL);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300252 if (tda_fail(i))
253 return i;
Michael Krufky255b5112008-01-01 22:52:09 -0300254
255 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
256 approx = map[i].rf_a1 *
257 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
258 } else {
259 approx = map[i].rf_a2 *
260 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
261 }
262
263 if (approx < 0)
264 approx = 0;
265 if (approx > 255)
266 approx = 255;
267
268 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
269
270 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300271 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300272
273 regs[R_EB14] = approx + rfcal_comp;
Michael Krufky20f42062008-05-04 19:57:06 -0300274 ret = tda18271_write_regs(fe, R_EB14, 1);
275fail:
276 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300277}
278
279static int tda18271_por(struct dvb_frontend *fe)
280{
281 struct tda18271_priv *priv = fe->tuner_priv;
282 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300283 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300284
285 /* power up detector 1 */
286 regs[R_EB12] &= ~0x20;
Michael Krufky24124f72008-05-03 19:28:00 -0300287 ret = tda18271_write_regs(fe, R_EB12, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300288 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300289 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300290
291 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
292 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300293 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300294 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300295 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300296
297 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
298
299 /* POR mode */
Michael Krufky24124f72008-05-03 19:28:00 -0300300 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300301 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300302 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300303
304 /* disable 1.5 MHz low pass filter */
305 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
306 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
Michael Krufky24124f72008-05-03 19:28:00 -0300307 ret = tda18271_write_regs(fe, R_EB21, 3);
308fail:
309 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300310}
311
312static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
313{
314 struct tda18271_priv *priv = fe->tuner_priv;
315 unsigned char *regs = priv->tda18271_regs;
316 u32 N;
317
318 /* set CAL mode to normal */
319 regs[R_EP4] &= ~0x03;
320 tda18271_write_regs(fe, R_EP4, 1);
321
322 /* switch off agc1 */
323 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
324
325 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
326 tda18271_write_regs(fe, R_EB18, 1);
327
328 /* frequency dependent parameters */
329
330 tda18271_calc_bp_filter(fe, &freq);
331 tda18271_calc_gain_taper(fe, &freq);
332 tda18271_calc_rf_band(fe, &freq);
333 tda18271_calc_km(fe, &freq);
334
335 tda18271_write_regs(fe, R_EP1, 3);
336 tda18271_write_regs(fe, R_EB13, 1);
337
338 /* main pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300339 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300340
341 /* cal pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300342 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300343
344 /* force dcdc converter to 0 V */
345 regs[R_EB14] = 0x00;
346 tda18271_write_regs(fe, R_EB14, 1);
347
348 /* disable plls lock */
349 regs[R_EB20] &= ~0x20;
350 tda18271_write_regs(fe, R_EB20, 1);
351
352 /* set CAL mode to RF tracking filter calibration */
353 regs[R_EP4] |= 0x03;
354 tda18271_write_regs(fe, R_EP4, 2);
355
356 /* --------------------------------------------------------------- */
357
358 /* set the internal calibration signal */
359 N = freq;
360
Michael Krufkyae07d042008-04-22 14:46:21 -0300361 tda18271_calc_cal_pll(fe, N);
362 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300363
364 /* downconvert internal calibration */
365 N += 1000000;
366
367 tda18271_calc_main_pll(fe, N);
368 tda18271_write_regs(fe, R_MPD, 4);
369
370 msleep(5);
371
372 tda18271_write_regs(fe, R_EP2, 1);
373 tda18271_write_regs(fe, R_EP1, 1);
374 tda18271_write_regs(fe, R_EP2, 1);
375 tda18271_write_regs(fe, R_EP1, 1);
376
377 /* --------------------------------------------------------------- */
378
379 /* normal operation for the main pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300380 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300381
382 /* normal operation for the cal pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300383 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300384
Michael Krufkyae07d042008-04-22 14:46:21 -0300385 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300386
387 /* launch the rf tracking filters calibration */
388 regs[R_EB20] |= 0x20;
389 tda18271_write_regs(fe, R_EB20, 1);
390
391 msleep(60); /* calibration */
392
393 /* --------------------------------------------------------------- */
394
395 /* set CAL mode to normal */
396 regs[R_EP4] &= ~0x03;
397
398 /* switch on agc1 */
399 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
400
401 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
402 tda18271_write_regs(fe, R_EB18, 1);
403
404 tda18271_write_regs(fe, R_EP3, 2);
405
406 /* synchronization */
407 tda18271_write_regs(fe, R_EP1, 1);
408
409 /* get calibration result */
410 tda18271_read_extended(fe);
411
412 return regs[R_EB14];
413}
414
415static int tda18271_powerscan(struct dvb_frontend *fe,
416 u32 *freq_in, u32 *freq_out)
417{
418 struct tda18271_priv *priv = fe->tuner_priv;
419 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300420 int sgn, bcal, count, wait, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300421 u8 cid_target;
422 u16 count_limit;
423 u32 freq;
424
425 freq = *freq_in;
426
427 tda18271_calc_rf_band(fe, &freq);
428 tda18271_calc_rf_cal(fe, &freq);
429 tda18271_calc_gain_taper(fe, &freq);
430 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
431
432 tda18271_write_regs(fe, R_EP2, 1);
433 tda18271_write_regs(fe, R_EB14, 1);
434
435 /* downconvert frequency */
436 freq += 1000000;
437
438 tda18271_calc_main_pll(fe, freq);
439 tda18271_write_regs(fe, R_MPD, 4);
440
441 msleep(5); /* pll locking */
442
443 /* detection mode */
444 regs[R_EP4] &= ~0x03;
445 regs[R_EP4] |= 0x01;
446 tda18271_write_regs(fe, R_EP4, 1);
447
448 /* launch power detection measurement */
449 tda18271_write_regs(fe, R_EP2, 1);
450
451 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300452 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300453 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300454 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300455
456 /* algorithm initialization */
457 sgn = 1;
458 *freq_out = *freq_in;
459 bcal = 0;
460 count = 0;
461 wait = false;
462
463 while ((regs[R_EB10] & 0x3f) < cid_target) {
464 /* downconvert updated freq to 1 MHz */
465 freq = *freq_in + (sgn * count) + 1000000;
466
467 tda18271_calc_main_pll(fe, freq);
468 tda18271_write_regs(fe, R_MPD, 4);
469
470 if (wait) {
471 msleep(5); /* pll locking */
472 wait = false;
473 } else
474 udelay(100); /* pll locking */
475
476 /* launch power detection measurement */
477 tda18271_write_regs(fe, R_EP2, 1);
478
479 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300480 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300481 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300482 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300483
484 count += 200;
485
Michael Krufkye7809a02008-04-22 14:46:22 -0300486 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300487 continue;
488
489 if (sgn <= 0)
490 break;
491
492 sgn = -1 * sgn;
493 count = 200;
494 wait = true;
495 }
496
497 if ((regs[R_EB10] & 0x3f) >= cid_target) {
498 bcal = 1;
499 *freq_out = freq - 1000000;
500 } else
501 bcal = 0;
502
Michael Krufkycf04d292008-01-09 00:34:30 -0300503 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300504 bcal, *freq_in, *freq_out, freq);
505
506 return bcal;
507}
508
509static int tda18271_powerscan_init(struct dvb_frontend *fe)
510{
511 struct tda18271_priv *priv = fe->tuner_priv;
512 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300513 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300514
515 /* set standard to digital */
516 regs[R_EP3] &= ~0x1f; /* clear std bits */
517 regs[R_EP3] |= 0x12;
518
519 /* set cal mode to normal */
520 regs[R_EP4] &= ~0x03;
521
Michael Krufky44e645c2008-06-08 20:10:29 -0300522 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -0300523 regs[R_EP4] &= ~0x1c; /* clear if level bits */
524
Michael Krufky24124f72008-05-03 19:28:00 -0300525 ret = tda18271_write_regs(fe, R_EP3, 2);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300526 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300527 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300528
529 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300530 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300531 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300532 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300533
534 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
535
536 /* 1.5 MHz low pass filter */
537 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
538 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
539
Michael Krufky24124f72008-05-03 19:28:00 -0300540 ret = tda18271_write_regs(fe, R_EB21, 3);
541fail:
542 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300543}
544
545static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
546{
547 struct tda18271_priv *priv = fe->tuner_priv;
548 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
549 unsigned char *regs = priv->tda18271_regs;
550 int bcal, rf, i;
551#define RF1 0
552#define RF2 1
553#define RF3 2
554 u32 rf_default[3];
555 u32 rf_freq[3];
556 u8 prog_cal[3];
557 u8 prog_tab[3];
558
559 i = tda18271_lookup_rf_band(fe, &freq, NULL);
560
Michael Krufky4bd5d102008-05-04 21:32:21 -0300561 if (tda_fail(i))
Michael Krufky255b5112008-01-01 22:52:09 -0300562 return i;
563
564 rf_default[RF1] = 1000 * map[i].rf1_def;
565 rf_default[RF2] = 1000 * map[i].rf2_def;
566 rf_default[RF3] = 1000 * map[i].rf3_def;
567
568 for (rf = RF1; rf <= RF3; rf++) {
569 if (0 == rf_default[rf])
570 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300571 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300572
573 /* look for optimized calibration frequency */
574 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300575 if (tda_fail(bcal))
Michael Krufky24124f72008-05-03 19:28:00 -0300576 return bcal;
Michael Krufky255b5112008-01-01 22:52:09 -0300577
578 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
579 prog_tab[rf] = regs[R_EB14];
580
581 if (1 == bcal)
582 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
583 else
584 prog_cal[rf] = prog_tab[rf];
585
586 switch (rf) {
587 case RF1:
588 map[i].rf_a1 = 0;
589 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
590 map[i].rf1 = rf_freq[RF1] / 1000;
591 break;
592 case RF2:
593 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
594 prog_cal[RF1] + prog_tab[RF1]) /
595 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
596 map[i].rf2 = rf_freq[RF2] / 1000;
597 break;
598 case RF3:
599 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
600 prog_cal[RF2] + prog_tab[RF2]) /
601 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
602 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
603 map[i].rf3 = rf_freq[RF3] / 1000;
604 break;
605 default:
606 BUG();
607 }
608 }
609
610 return 0;
611}
612
Michael Krufky09f83c42008-01-05 20:00:09 -0300613static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300614{
615 struct tda18271_priv *priv = fe->tuner_priv;
616 unsigned int i;
Michael Krufky24124f72008-05-03 19:28:00 -0300617 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300618
619 tda_info("tda18271: performing RF tracking filter calibration\n");
620
621 /* wait for die temperature stabilization */
622 msleep(200);
623
Michael Krufky24124f72008-05-03 19:28:00 -0300624 ret = tda18271_powerscan_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300625 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300626 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300627
628 /* rf band calibration */
Michael Krufkyc151c322008-05-04 17:54:23 -0300629 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
630 ret =
Michael Krufky255b5112008-01-01 22:52:09 -0300631 tda18271_rf_tracking_filters_init(fe, 1000 *
632 priv->rf_cal_state[i].rfmax);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300633 if (tda_fail(ret))
Michael Krufkyc151c322008-05-04 17:54:23 -0300634 goto fail;
635 }
Michael Krufky255b5112008-01-01 22:52:09 -0300636
Michael Krufky09f83c42008-01-05 20:00:09 -0300637 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky24124f72008-05-03 19:28:00 -0300638fail:
639 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300640}
641
642/* ------------------------------------------------------------------ */
643
Michael Krufky12afe372008-04-22 14:42:07 -0300644static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300645{
646 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300647 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300648 int ret;
Michael Krufky839c6c92008-01-13 18:29:44 -0300649
650 /* test RF_CAL_OK to see if we need init */
651 if ((regs[R_EP1] & 0x10) == 0)
652 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300653
654 if (priv->cal_initialized)
655 return 0;
656
Michael Krufky24124f72008-05-03 19:28:00 -0300657 ret = tda18271_calc_rf_filter_curve(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 Krufky24124f72008-05-03 19:28:00 -0300661 ret = tda18271_por(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300662 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300663 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300664
Michael Krufky6bfa6652008-01-07 00:51:48 -0300665 tda_info("tda18271: RF tracking filter calibration complete\n");
666
Michael Krufky255b5112008-01-01 22:52:09 -0300667 priv->cal_initialized = true;
Michael Krufkyc151c322008-05-04 17:54:23 -0300668 goto end;
Michael Krufky24124f72008-05-03 19:28:00 -0300669fail:
Michael Krufkyc151c322008-05-04 17:54:23 -0300670 tda_info("tda18271: RF tracking filter calibration failed!\n");
671end:
Michael Krufky24124f72008-05-03 19:28:00 -0300672 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300673}
674
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300675static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
676 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300677{
678 struct tda18271_priv *priv = fe->tuner_priv;
679 unsigned char *regs = priv->tda18271_regs;
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300680 int ret;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300681 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300682
Michael Krufky255b5112008-01-01 22:52:09 -0300683 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300684 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300685 tda18271_write_regs(fe, R_EP1, 1);
686
687 regs[R_EB4] &= 0x07;
688 regs[R_EB4] |= 0x60;
689 tda18271_write_regs(fe, R_EB4, 1);
690
691 regs[R_EB7] = 0x60;
692 tda18271_write_regs(fe, R_EB7, 1);
693
694 regs[R_EB14] = 0x00;
695 tda18271_write_regs(fe, R_EB14, 1);
696
697 regs[R_EB20] = 0xcc;
698 tda18271_write_regs(fe, R_EB20, 1);
699
Michael Krufky255b5112008-01-01 22:52:09 -0300700 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300701 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300702
Michael Krufky255b5112008-01-01 22:52:09 -0300703 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300704
705 switch (priv->mode) {
706 case TDA18271_ANALOG:
707 N = freq - 1250000;
708 break;
709 case TDA18271_DIGITAL:
710 N = freq + bw / 2;
711 break;
712 }
713
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300714 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300715
Michael Krufky255b5112008-01-01 22:52:09 -0300716 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300717
718 switch (priv->mode) {
719 case TDA18271_ANALOG:
720 N = freq - 250000;
721 break;
722 case TDA18271_DIGITAL:
723 N = freq + bw / 2 + 1000000;
724 break;
725 }
726
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300727 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300728
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300729 ret = tda18271_write_regs(fe, R_EP3, 11);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300730 if (tda_fail(ret))
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300731 return ret;
732
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300733 msleep(5); /* RF tracking filter calibration initialization */
734
Michael Krufky255b5112008-01-01 22:52:09 -0300735 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300736 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300737 tda18271_write_regs(fe, R_EB13, 1);
738
Michael Krufky255b5112008-01-01 22:52:09 -0300739 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300740 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300741
Michael Krufky255b5112008-01-01 22:52:09 -0300742 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300743 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300744
745 tda18271_write_regs(fe, R_EP2, 1);
746 tda18271_write_regs(fe, R_EP1, 1);
747 tda18271_write_regs(fe, R_EP2, 1);
748 tda18271_write_regs(fe, R_EP1, 1);
749
750 regs[R_EB4] &= 0x07;
751 regs[R_EB4] |= 0x40;
752 tda18271_write_regs(fe, R_EB4, 1);
753
754 regs[R_EB7] = 0x40;
755 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300756 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300757
758 regs[R_EB20] = 0xec;
759 tda18271_write_regs(fe, R_EB20, 1);
760 msleep(60); /* RF tracking filter calibration completion */
761
762 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
763 tda18271_write_regs(fe, R_EP4, 1);
764
765 tda18271_write_regs(fe, R_EP1, 1);
766
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300767 /* RF tracking filter correction for VHF_Low band */
768 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300769 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300770
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300771 return 0;
772}
773
Michael Krufkyd1c53422008-04-22 14:42:07 -0300774/* ------------------------------------------------------------------ */
775
Michael Krufky12afe372008-04-22 14:42:07 -0300776static int tda18271_ir_cal_init(struct dvb_frontend *fe)
777{
778 struct tda18271_priv *priv = fe->tuner_priv;
779 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300780 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300781
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300782 ret = tda18271_read_regs(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300783 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300784 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300785
786 /* test IR_CAL_OK to see if we need init */
787 if ((regs[R_EP1] & 0x08) == 0)
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300788 ret = tda18271_init_regs(fe);
789fail:
790 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300791}
792
793static int tda18271_init(struct dvb_frontend *fe)
794{
795 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300796 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300797
798 mutex_lock(&priv->lock);
799
800 /* power up */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300801 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300802 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300803 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300804
805 /* initialization */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300806 ret = tda18271_ir_cal_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300807 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300808 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300809
810 if (priv->id == TDA18271HDC2)
811 tda18271c2_rf_cal_init(fe);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300812fail:
Michael Krufky12afe372008-04-22 14:42:07 -0300813 mutex_unlock(&priv->lock);
814
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300815 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300816}
817
Michael Krufkyd1c53422008-04-22 14:42:07 -0300818static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300819 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300820{
821 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300822 int ret;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300823
Michael Krufky7f7203d2008-04-22 14:46:06 -0300824 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
825 freq, map->if_freq, bw, map->agc_mode, map->std);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300826
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300827 ret = tda18271_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300828 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300829 goto fail;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300830
831 mutex_lock(&priv->lock);
832
Michael Krufkyd1c53422008-04-22 14:42:07 -0300833 switch (priv->id) {
834 case TDA18271HDC1:
835 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
836 break;
837 case TDA18271HDC2:
838 tda18271c2_rf_tracking_filters_correction(fe, freq);
839 break;
840 }
Michael Krufky31940e32008-05-04 19:37:27 -0300841 ret = tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300842
Michael Krufky8d316bf2008-01-06 15:31:35 -0300843 mutex_unlock(&priv->lock);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300844fail:
845 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300846}
847
848/* ------------------------------------------------------------------ */
849
850static int tda18271_set_params(struct dvb_frontend *fe,
851 struct dvb_frontend_parameters *params)
852{
853 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300854 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300855 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300856 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300857 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300858
859 priv->mode = TDA18271_DIGITAL;
860
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300861 if (fe->ops.info.type == FE_ATSC) {
862 switch (params->u.vsb.modulation) {
863 case VSB_8:
864 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300865 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300866 break;
867 case QAM_64:
868 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300869 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300870 break;
871 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300872 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300873 return -EINVAL;
874 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300875#if 0
876 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300877 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300878#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300879 bw = 6000000;
880 } else if (fe->ops.info.type == FE_OFDM) {
881 switch (params->u.ofdm.bandwidth) {
882 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300883 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300884 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300885 break;
886 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300887 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300888 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300889 break;
890 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300891 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300892 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300893 break;
894 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300895 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300896 return -EINVAL;
897 }
898 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300899 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300900 return -EINVAL;
901 }
902
Michael Krufkyed736832008-01-19 17:41:04 -0300903 /* When tuning digital, the analog demod must be tri-stated */
904 if (fe->ops.analog_ops.standby)
905 fe->ops.analog_ops.standby(fe);
906
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300907 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300908
Michael Krufky4bd5d102008-05-04 21:32:21 -0300909 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300910 goto fail;
911
912 priv->frequency = freq;
913 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
914 params->u.ofdm.bandwidth : 0;
915fail:
916 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300917}
918
919static int tda18271_set_analog_params(struct dvb_frontend *fe,
920 struct analog_parameters *params)
921{
922 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300923 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300924 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300925 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300926 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300927 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300928
929 priv->mode = TDA18271_ANALOG;
930
Michael Krufkyc353f422008-01-08 10:38:10 -0300931 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc353f422008-01-08 10:38:10 -0300932 freq = freq / 1000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300933 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -0300934 mode = "fm";
935 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300936 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300937 mode = "MN";
938 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300939 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300940 mode = "B";
941 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300942 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300943 mode = "GH";
944 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300945 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300946 mode = "I";
947 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300948 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300949 mode = "DK";
950 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300951 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300952 mode = "L";
953 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300954 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -0300955 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300956 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300957 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300958 mode = "xx";
959 }
960
Michael Krufky182519f2007-12-25 15:10:11 -0300961 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300962
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300963 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300964
Michael Krufky4bd5d102008-05-04 21:32:21 -0300965 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300966 goto fail;
967
968 priv->frequency = freq;
969 priv->bandwidth = 0;
970fail:
971 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300972}
973
Michael Krufky518d8732008-01-13 17:01:01 -0300974static int tda18271_sleep(struct dvb_frontend *fe)
975{
976 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300977 int ret;
Michael Krufky518d8732008-01-13 17:01:01 -0300978
979 mutex_lock(&priv->lock);
980
981 /* standby mode w/ slave tuner output
982 * & loop thru & xtal oscillator on */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300983 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky518d8732008-01-13 17:01:01 -0300984
985 mutex_unlock(&priv->lock);
986
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300987 return ret;
Michael Krufky518d8732008-01-13 17:01:01 -0300988}
989
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300990static int tda18271_release(struct dvb_frontend *fe)
991{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300992 struct tda18271_priv *priv = fe->tuner_priv;
993
994 mutex_lock(&tda18271_list_mutex);
995
Michael Krufkyf9e315a2008-04-22 14:41:54 -0300996 if (priv)
997 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -0300998
Michael Krufkya4f263b2008-01-06 15:52:56 -0300999 mutex_unlock(&tda18271_list_mutex);
1000
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001001 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001002
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001003 return 0;
1004}
1005
1006static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1007{
1008 struct tda18271_priv *priv = fe->tuner_priv;
1009 *frequency = priv->frequency;
1010 return 0;
1011}
1012
1013static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1014{
1015 struct tda18271_priv *priv = fe->tuner_priv;
1016 *bandwidth = priv->bandwidth;
1017 return 0;
1018}
1019
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001020/* ------------------------------------------------------------------ */
1021
1022#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001023 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -03001024 map->std_cfg.agc_mode + map->std_cfg.std + \
1025 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001026 tda_dbg("Using custom std config for %s\n", name); \
1027 memcpy(&std->std_cfg, &map->std_cfg, \
1028 sizeof(struct tda18271_std_map_item)); \
1029 } } while (0)
1030
1031#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -03001032 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1033 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001034 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -03001035 std->std_cfg.agc_mode, std->std_cfg.std, \
1036 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001037 } while (0)
1038
1039static int tda18271_dump_std_map(struct dvb_frontend *fe)
1040{
1041 struct tda18271_priv *priv = fe->tuner_priv;
1042 struct tda18271_std_map *std = &priv->std;
1043
1044 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -03001045 tda18271_dump_std_item(fm_radio, " fm ");
1046 tda18271_dump_std_item(atv_b, "atv b ");
1047 tda18271_dump_std_item(atv_dk, "atv dk");
1048 tda18271_dump_std_item(atv_gh, "atv gh");
1049 tda18271_dump_std_item(atv_i, "atv i ");
1050 tda18271_dump_std_item(atv_l, "atv l ");
1051 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001052 tda18271_dump_std_item(atv_mn, "atv mn");
1053 tda18271_dump_std_item(atsc_6, "atsc 6");
1054 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1055 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1056 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -03001057 tda18271_dump_std_item(qam_6, "qam 6 ");
1058 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001059
1060 return 0;
1061}
1062
1063static int tda18271_update_std_map(struct dvb_frontend *fe,
1064 struct tda18271_std_map *map)
1065{
1066 struct tda18271_priv *priv = fe->tuner_priv;
1067 struct tda18271_std_map *std = &priv->std;
1068
1069 if (!map)
1070 return -EINVAL;
1071
Michael Krufkyc353f422008-01-08 10:38:10 -03001072 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001073 tda18271_update_std(atv_b, "atv b");
1074 tda18271_update_std(atv_dk, "atv dk");
1075 tda18271_update_std(atv_gh, "atv gh");
1076 tda18271_update_std(atv_i, "atv i");
1077 tda18271_update_std(atv_l, "atv l");
1078 tda18271_update_std(atv_lc, "atv l'");
1079 tda18271_update_std(atv_mn, "atv mn");
1080 tda18271_update_std(atsc_6, "atsc 6");
1081 tda18271_update_std(dvbt_6, "dvbt 6");
1082 tda18271_update_std(dvbt_7, "dvbt 7");
1083 tda18271_update_std(dvbt_8, "dvbt 8");
1084 tda18271_update_std(qam_6, "qam 6");
1085 tda18271_update_std(qam_8, "qam 8");
1086
1087 return 0;
1088}
1089
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001090static int tda18271_get_id(struct dvb_frontend *fe)
1091{
1092 struct tda18271_priv *priv = fe->tuner_priv;
1093 unsigned char *regs = priv->tda18271_regs;
1094 char *name;
1095 int ret = 0;
1096
Michael Krufky8d316bf2008-01-06 15:31:35 -03001097 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001098 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001099 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001100
1101 switch (regs[R_ID] & 0x7f) {
1102 case 3:
1103 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001104 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001105 break;
1106 case 4:
1107 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001108 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001109 break;
1110 default:
1111 name = "Unknown device";
1112 ret = -EINVAL;
1113 break;
1114 }
1115
Michael Krufky182519f2007-12-25 15:10:11 -03001116 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001117 i2c_adapter_id(priv->i2c_props.adap),
1118 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001119 (0 == ret) ? "" : ", device not supported.");
1120
1121 return ret;
1122}
1123
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001124static struct dvb_tuner_ops tda18271_tuner_ops = {
1125 .info = {
1126 .name = "NXP TDA18271HD",
1127 .frequency_min = 45000000,
1128 .frequency_max = 864000000,
1129 .frequency_step = 62500
1130 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001131 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001132 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001133 .set_params = tda18271_set_params,
1134 .set_analog_params = tda18271_set_analog_params,
1135 .release = tda18271_release,
1136 .get_frequency = tda18271_get_frequency,
1137 .get_bandwidth = tda18271_get_bandwidth,
1138};
1139
1140struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001141 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001142 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001143{
1144 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001145 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001146
Michael Krufkya4f263b2008-01-06 15:52:56 -03001147 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001148
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001149 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1150 hybrid_tuner_instance_list,
1151 i2c, addr, "tda18271");
1152 switch (instance) {
1153 case 0:
1154 goto fail;
1155 break;
1156 case 1:
1157 /* new tuner instance */
Michael Krufkya4f263b2008-01-06 15:52:56 -03001158 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
Michael Krufky868f5cc2008-04-22 14:46:23 -03001159 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001160 priv->cal_initialized = false;
1161 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001162
Michael Krufkya4f263b2008-01-06 15:52:56 -03001163 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001164
Michael Krufky55553092008-04-22 14:46:06 -03001165 if (cfg)
1166 priv->small_i2c = cfg->small_i2c;
1167
Michael Krufky4bd5d102008-05-04 21:32:21 -03001168 if (tda_fail(tda18271_get_id(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001169 goto fail;
1170
Michael Krufky4bd5d102008-05-04 21:32:21 -03001171 if (tda_fail(tda18271_assign_map_layout(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001172 goto fail;
1173
1174 mutex_lock(&priv->lock);
1175 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001176
1177 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001178 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001179
Michael Krufkya4f263b2008-01-06 15:52:56 -03001180 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001181 break;
1182 default:
1183 /* existing tuner instance */
1184 fe->tuner_priv = priv;
1185
1186 /* allow dvb driver to override i2c gate setting */
1187 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1188 priv->gate = cfg->gate;
1189 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001190 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001191
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001192 /* override default std map with values in config struct */
1193 if ((cfg) && (cfg->std_map))
1194 tda18271_update_std_map(fe, cfg->std_map);
1195
Michael Krufkya4f263b2008-01-06 15:52:56 -03001196 mutex_unlock(&tda18271_list_mutex);
1197
1198 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1199 sizeof(struct dvb_tuner_ops));
1200
Michael Krufkyc7353722008-03-30 19:40:20 -03001201 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001202 tda18271_dump_std_map(fe);
1203
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001204 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001205fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001206 mutex_unlock(&tda18271_list_mutex);
1207
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001208 tda18271_release(fe);
1209 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001210}
1211EXPORT_SYMBOL_GPL(tda18271_attach);
1212MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1213MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1214MODULE_LICENSE("GPL");
Michael Krufky5ec96b02008-04-22 14:46:23 -03001215MODULE_VERSION("0.3");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001216
1217/*
1218 * Overrides for Emacs so that we follow Linus's tabbing style.
1219 * ---------------------------------------------------------------------------
1220 * Local variables:
1221 * c-basic-offset: 8
1222 * End:
1223 */