blob: bc4b004ba7dbed45f3a07ff0ef4d4411fa159737 [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 Krufky81016b42009-08-27 16:58:06 -030030static int tda18271_cal_on_startup = -1;
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
Michael Krufky7ae1ac42008-06-11 13:52:49 -0300106 /* make sure thermometer is off */
107 regs[R_TM] &= ~0x10;
108
Michael Krufky255b5112008-01-01 22:52:09 -0300109 /* frequency dependent parameters */
110
111 tda18271_calc_ir_measure(fe, &freq);
112
113 tda18271_calc_bp_filter(fe, &freq);
114
115 tda18271_calc_rf_band(fe, &freq);
116
117 tda18271_calc_gain_taper(fe, &freq);
118
119 /* --------------------------------------------------------------- */
120
121 /* dual tuner and agc1 extra configuration */
122
Michael Krufky868f5cc2008-04-22 14:46:23 -0300123 switch (priv->role) {
124 case TDA18271_MASTER:
125 regs[R_EB1] |= 0x04; /* main vco */
126 break;
127 case TDA18271_SLAVE:
128 regs[R_EB1] &= ~0x04; /* cal vco */
129 break;
130 }
Michael Krufky255b5112008-01-01 22:52:09 -0300131
132 /* agc1 always active */
133 regs[R_EB1] &= ~0x02;
134
135 /* agc1 has priority on agc2 */
136 regs[R_EB1] &= ~0x01;
137
Michael Krufky31940e32008-05-04 19:37:27 -0300138 ret = tda18271_write_regs(fe, R_EB1, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300139 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300140 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300141
142 /* --------------------------------------------------------------- */
143
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300144 N = map->if_freq * 1000 + freq;
Michael Krufky255b5112008-01-01 22:52:09 -0300145
Michael Krufky868f5cc2008-04-22 14:46:23 -0300146 switch (priv->role) {
147 case TDA18271_MASTER:
148 tda18271_calc_main_pll(fe, N);
Michael Krufky44e645c2008-06-08 20:10:29 -0300149 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300150 tda18271_write_regs(fe, R_MPD, 4);
151 break;
152 case TDA18271_SLAVE:
153 tda18271_calc_cal_pll(fe, N);
154 tda18271_write_regs(fe, R_CPD, 4);
155
156 regs[R_MPD] = regs[R_CPD] & 0x7f;
Michael Krufky44e645c2008-06-08 20:10:29 -0300157 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300158 tda18271_write_regs(fe, R_MPD, 1);
159 break;
160 }
Michael Krufky255b5112008-01-01 22:52:09 -0300161
Michael Krufky31940e32008-05-04 19:37:27 -0300162 ret = tda18271_write_regs(fe, R_TM, 7);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300163 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300164 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300165
Michael Krufky868f5cc2008-04-22 14:46:23 -0300166 /* force charge pump source */
167 charge_pump_source(fe, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300168
169 msleep(1);
170
Michael Krufky868f5cc2008-04-22 14:46:23 -0300171 /* return pll to normal operation */
172 charge_pump_source(fe, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300173
Michael Krufky40194b22008-04-22 14:46:22 -0300174 msleep(20);
175
Michael Krufky51858d12008-06-09 02:03:31 -0300176 if (priv->id == TDA18271HDC2) {
177 /* set rfagc to normal speed mode */
178 if (map->fm_rfn)
179 regs[R_EP3] &= ~0x04;
180 else
181 regs[R_EP3] |= 0x04;
182 ret = tda18271_write_regs(fe, R_EP3, 1);
183 }
Michael Krufky31940e32008-05-04 19:37:27 -0300184fail:
185 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300186}
187
188static int tda18271_read_thermometer(struct dvb_frontend *fe)
189{
190 struct tda18271_priv *priv = fe->tuner_priv;
191 unsigned char *regs = priv->tda18271_regs;
192 int tm;
193
194 /* switch thermometer on */
195 regs[R_TM] |= 0x10;
196 tda18271_write_regs(fe, R_TM, 1);
197
198 /* read thermometer info */
199 tda18271_read_regs(fe);
200
201 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
202 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
203
204 if ((regs[R_TM] & 0x20) == 0x20)
205 regs[R_TM] &= ~0x20;
206 else
207 regs[R_TM] |= 0x20;
208
209 tda18271_write_regs(fe, R_TM, 1);
210
211 msleep(10); /* temperature sensing */
212
213 /* read thermometer info */
214 tda18271_read_regs(fe);
215 }
216
217 tm = tda18271_lookup_thermometer(fe);
218
219 /* switch thermometer off */
220 regs[R_TM] &= ~0x10;
221 tda18271_write_regs(fe, R_TM, 1);
222
223 /* set CAL mode to normal */
224 regs[R_EP4] &= ~0x03;
225 tda18271_write_regs(fe, R_EP4, 1);
226
227 return tm;
228}
229
Michael Krufky12afe372008-04-22 14:42:07 -0300230/* ------------------------------------------------------------------ */
231
Michael Krufkyd1c53422008-04-22 14:42:07 -0300232static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
233 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300234{
235 struct tda18271_priv *priv = fe->tuner_priv;
236 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
237 unsigned char *regs = priv->tda18271_regs;
Michael Krufky20f42062008-05-04 19:57:06 -0300238 int tm_current, rfcal_comp, approx, i, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300239 u8 dc_over_dt, rf_tab;
240
241 /* power up */
Michael Krufky20f42062008-05-04 19:57:06 -0300242 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300243 if (tda_fail(ret))
Michael Krufky20f42062008-05-04 19:57:06 -0300244 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300245
246 /* read die current temperature */
247 tm_current = tda18271_read_thermometer(fe);
248
249 /* frequency dependent parameters */
250
251 tda18271_calc_rf_cal(fe, &freq);
252 rf_tab = regs[R_EB14];
253
254 i = tda18271_lookup_rf_band(fe, &freq, NULL);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300255 if (tda_fail(i))
256 return i;
Michael Krufky255b5112008-01-01 22:52:09 -0300257
258 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
259 approx = map[i].rf_a1 *
260 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
261 } else {
262 approx = map[i].rf_a2 *
263 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
264 }
265
266 if (approx < 0)
267 approx = 0;
268 if (approx > 255)
269 approx = 255;
270
271 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
272
273 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300274 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300275
276 regs[R_EB14] = approx + rfcal_comp;
Michael Krufky20f42062008-05-04 19:57:06 -0300277 ret = tda18271_write_regs(fe, R_EB14, 1);
278fail:
279 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300280}
281
282static int tda18271_por(struct dvb_frontend *fe)
283{
284 struct tda18271_priv *priv = fe->tuner_priv;
285 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300286 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300287
288 /* power up detector 1 */
289 regs[R_EB12] &= ~0x20;
Michael Krufky24124f72008-05-03 19:28:00 -0300290 ret = tda18271_write_regs(fe, R_EB12, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300291 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300292 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300293
294 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
295 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300296 ret = tda18271_write_regs(fe, R_EB18, 1);
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 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
301
302 /* POR mode */
Michael Krufky24124f72008-05-03 19:28:00 -0300303 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300304 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300305 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300306
307 /* disable 1.5 MHz low pass filter */
308 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
309 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
Michael Krufky24124f72008-05-03 19:28:00 -0300310 ret = tda18271_write_regs(fe, R_EB21, 3);
311fail:
312 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300313}
314
315static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
316{
317 struct tda18271_priv *priv = fe->tuner_priv;
318 unsigned char *regs = priv->tda18271_regs;
319 u32 N;
320
321 /* set CAL mode to normal */
322 regs[R_EP4] &= ~0x03;
323 tda18271_write_regs(fe, R_EP4, 1);
324
325 /* switch off agc1 */
326 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
327
328 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
329 tda18271_write_regs(fe, R_EB18, 1);
330
331 /* frequency dependent parameters */
332
333 tda18271_calc_bp_filter(fe, &freq);
334 tda18271_calc_gain_taper(fe, &freq);
335 tda18271_calc_rf_band(fe, &freq);
336 tda18271_calc_km(fe, &freq);
337
338 tda18271_write_regs(fe, R_EP1, 3);
339 tda18271_write_regs(fe, R_EB13, 1);
340
341 /* main pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300342 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300343
344 /* cal pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300345 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300346
347 /* force dcdc converter to 0 V */
348 regs[R_EB14] = 0x00;
349 tda18271_write_regs(fe, R_EB14, 1);
350
351 /* disable plls lock */
352 regs[R_EB20] &= ~0x20;
353 tda18271_write_regs(fe, R_EB20, 1);
354
355 /* set CAL mode to RF tracking filter calibration */
356 regs[R_EP4] |= 0x03;
357 tda18271_write_regs(fe, R_EP4, 2);
358
359 /* --------------------------------------------------------------- */
360
361 /* set the internal calibration signal */
362 N = freq;
363
Michael Krufkyae07d042008-04-22 14:46:21 -0300364 tda18271_calc_cal_pll(fe, N);
365 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300366
367 /* downconvert internal calibration */
368 N += 1000000;
369
370 tda18271_calc_main_pll(fe, N);
371 tda18271_write_regs(fe, R_MPD, 4);
372
373 msleep(5);
374
375 tda18271_write_regs(fe, R_EP2, 1);
376 tda18271_write_regs(fe, R_EP1, 1);
377 tda18271_write_regs(fe, R_EP2, 1);
378 tda18271_write_regs(fe, R_EP1, 1);
379
380 /* --------------------------------------------------------------- */
381
382 /* normal operation for the main pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300383 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300384
385 /* normal operation for the cal pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300386 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300387
Michael Krufkyae07d042008-04-22 14:46:21 -0300388 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300389
390 /* launch the rf tracking filters calibration */
391 regs[R_EB20] |= 0x20;
392 tda18271_write_regs(fe, R_EB20, 1);
393
394 msleep(60); /* calibration */
395
396 /* --------------------------------------------------------------- */
397
398 /* set CAL mode to normal */
399 regs[R_EP4] &= ~0x03;
400
401 /* switch on agc1 */
402 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
403
404 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
405 tda18271_write_regs(fe, R_EB18, 1);
406
407 tda18271_write_regs(fe, R_EP3, 2);
408
409 /* synchronization */
410 tda18271_write_regs(fe, R_EP1, 1);
411
412 /* get calibration result */
413 tda18271_read_extended(fe);
414
415 return regs[R_EB14];
416}
417
418static int tda18271_powerscan(struct dvb_frontend *fe,
419 u32 *freq_in, u32 *freq_out)
420{
421 struct tda18271_priv *priv = fe->tuner_priv;
422 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300423 int sgn, bcal, count, wait, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300424 u8 cid_target;
425 u16 count_limit;
426 u32 freq;
427
428 freq = *freq_in;
429
430 tda18271_calc_rf_band(fe, &freq);
431 tda18271_calc_rf_cal(fe, &freq);
432 tda18271_calc_gain_taper(fe, &freq);
433 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
434
435 tda18271_write_regs(fe, R_EP2, 1);
436 tda18271_write_regs(fe, R_EB14, 1);
437
438 /* downconvert frequency */
439 freq += 1000000;
440
441 tda18271_calc_main_pll(fe, freq);
442 tda18271_write_regs(fe, R_MPD, 4);
443
444 msleep(5); /* pll locking */
445
446 /* detection mode */
447 regs[R_EP4] &= ~0x03;
448 regs[R_EP4] |= 0x01;
449 tda18271_write_regs(fe, R_EP4, 1);
450
451 /* launch power detection measurement */
452 tda18271_write_regs(fe, R_EP2, 1);
453
454 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300455 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300456 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300457 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300458
459 /* algorithm initialization */
460 sgn = 1;
461 *freq_out = *freq_in;
462 bcal = 0;
463 count = 0;
464 wait = false;
465
466 while ((regs[R_EB10] & 0x3f) < cid_target) {
467 /* downconvert updated freq to 1 MHz */
468 freq = *freq_in + (sgn * count) + 1000000;
469
470 tda18271_calc_main_pll(fe, freq);
471 tda18271_write_regs(fe, R_MPD, 4);
472
473 if (wait) {
474 msleep(5); /* pll locking */
475 wait = false;
476 } else
477 udelay(100); /* pll locking */
478
479 /* launch power detection measurement */
480 tda18271_write_regs(fe, R_EP2, 1);
481
482 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300483 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300484 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300485 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300486
487 count += 200;
488
Michael Krufkye7809a02008-04-22 14:46:22 -0300489 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300490 continue;
491
492 if (sgn <= 0)
493 break;
494
495 sgn = -1 * sgn;
496 count = 200;
497 wait = true;
498 }
499
500 if ((regs[R_EB10] & 0x3f) >= cid_target) {
501 bcal = 1;
502 *freq_out = freq - 1000000;
503 } else
504 bcal = 0;
505
Michael Krufkycf04d292008-01-09 00:34:30 -0300506 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300507 bcal, *freq_in, *freq_out, freq);
508
509 return bcal;
510}
511
512static int tda18271_powerscan_init(struct dvb_frontend *fe)
513{
514 struct tda18271_priv *priv = fe->tuner_priv;
515 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300516 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300517
518 /* set standard to digital */
519 regs[R_EP3] &= ~0x1f; /* clear std bits */
520 regs[R_EP3] |= 0x12;
521
522 /* set cal mode to normal */
523 regs[R_EP4] &= ~0x03;
524
Michael Krufky44e645c2008-06-08 20:10:29 -0300525 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -0300526 regs[R_EP4] &= ~0x1c; /* clear if level bits */
527
Michael Krufky24124f72008-05-03 19:28:00 -0300528 ret = tda18271_write_regs(fe, R_EP3, 2);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300529 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300530 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300531
532 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300533 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300534 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300535 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300536
537 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
538
539 /* 1.5 MHz low pass filter */
540 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
541 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
542
Michael Krufky24124f72008-05-03 19:28:00 -0300543 ret = tda18271_write_regs(fe, R_EB21, 3);
544fail:
545 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300546}
547
548static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
549{
550 struct tda18271_priv *priv = fe->tuner_priv;
551 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
552 unsigned char *regs = priv->tda18271_regs;
553 int bcal, rf, i;
554#define RF1 0
555#define RF2 1
556#define RF3 2
557 u32 rf_default[3];
558 u32 rf_freq[3];
559 u8 prog_cal[3];
560 u8 prog_tab[3];
561
562 i = tda18271_lookup_rf_band(fe, &freq, NULL);
563
Michael Krufky4bd5d102008-05-04 21:32:21 -0300564 if (tda_fail(i))
Michael Krufky255b5112008-01-01 22:52:09 -0300565 return i;
566
567 rf_default[RF1] = 1000 * map[i].rf1_def;
568 rf_default[RF2] = 1000 * map[i].rf2_def;
569 rf_default[RF3] = 1000 * map[i].rf3_def;
570
571 for (rf = RF1; rf <= RF3; rf++) {
572 if (0 == rf_default[rf])
573 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300574 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300575
576 /* look for optimized calibration frequency */
577 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300578 if (tda_fail(bcal))
Michael Krufky24124f72008-05-03 19:28:00 -0300579 return bcal;
Michael Krufky255b5112008-01-01 22:52:09 -0300580
581 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
582 prog_tab[rf] = regs[R_EB14];
583
584 if (1 == bcal)
585 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
586 else
587 prog_cal[rf] = prog_tab[rf];
588
589 switch (rf) {
590 case RF1:
591 map[i].rf_a1 = 0;
592 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
593 map[i].rf1 = rf_freq[RF1] / 1000;
594 break;
595 case RF2:
596 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
597 prog_cal[RF1] + prog_tab[RF1]) /
598 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
599 map[i].rf2 = rf_freq[RF2] / 1000;
600 break;
601 case RF3:
602 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
603 prog_cal[RF2] + prog_tab[RF2]) /
604 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
605 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
606 map[i].rf3 = rf_freq[RF3] / 1000;
607 break;
608 default:
609 BUG();
610 }
611 }
612
613 return 0;
614}
615
Michael Krufky09f83c42008-01-05 20:00:09 -0300616static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300617{
618 struct tda18271_priv *priv = fe->tuner_priv;
619 unsigned int i;
Michael Krufky24124f72008-05-03 19:28:00 -0300620 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300621
622 tda_info("tda18271: performing RF tracking filter calibration\n");
623
624 /* wait for die temperature stabilization */
625 msleep(200);
626
Michael Krufky24124f72008-05-03 19:28:00 -0300627 ret = tda18271_powerscan_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300628 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300629 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300630
631 /* rf band calibration */
Michael Krufkyc151c322008-05-04 17:54:23 -0300632 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
633 ret =
Michael Krufky255b5112008-01-01 22:52:09 -0300634 tda18271_rf_tracking_filters_init(fe, 1000 *
635 priv->rf_cal_state[i].rfmax);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300636 if (tda_fail(ret))
Michael Krufkyc151c322008-05-04 17:54:23 -0300637 goto fail;
638 }
Michael Krufky255b5112008-01-01 22:52:09 -0300639
Michael Krufky09f83c42008-01-05 20:00:09 -0300640 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky24124f72008-05-03 19:28:00 -0300641fail:
642 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300643}
644
645/* ------------------------------------------------------------------ */
646
Michael Krufky12afe372008-04-22 14:42:07 -0300647static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300648{
649 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300650 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300651 int ret;
Michael Krufky839c6c92008-01-13 18:29:44 -0300652
653 /* test RF_CAL_OK to see if we need init */
654 if ((regs[R_EP1] & 0x10) == 0)
655 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300656
657 if (priv->cal_initialized)
658 return 0;
659
Michael Krufky24124f72008-05-03 19:28:00 -0300660 ret = tda18271_calc_rf_filter_curve(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300661 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300662 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300663
Michael Krufky24124f72008-05-03 19:28:00 -0300664 ret = tda18271_por(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300665 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300666 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300667
Michael Krufky6bfa6652008-01-07 00:51:48 -0300668 tda_info("tda18271: RF tracking filter calibration complete\n");
669
Michael Krufky255b5112008-01-01 22:52:09 -0300670 priv->cal_initialized = true;
Michael Krufkyc151c322008-05-04 17:54:23 -0300671 goto end;
Michael Krufky24124f72008-05-03 19:28:00 -0300672fail:
Michael Krufkyc151c322008-05-04 17:54:23 -0300673 tda_info("tda18271: RF tracking filter calibration failed!\n");
674end:
Michael Krufky24124f72008-05-03 19:28:00 -0300675 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300676}
677
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300678static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
679 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300680{
681 struct tda18271_priv *priv = fe->tuner_priv;
682 unsigned char *regs = priv->tda18271_regs;
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300683 int ret;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300684 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300685
Michael Krufky255b5112008-01-01 22:52:09 -0300686 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300687 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300688 tda18271_write_regs(fe, R_EP1, 1);
689
690 regs[R_EB4] &= 0x07;
691 regs[R_EB4] |= 0x60;
692 tda18271_write_regs(fe, R_EB4, 1);
693
694 regs[R_EB7] = 0x60;
695 tda18271_write_regs(fe, R_EB7, 1);
696
697 regs[R_EB14] = 0x00;
698 tda18271_write_regs(fe, R_EB14, 1);
699
700 regs[R_EB20] = 0xcc;
701 tda18271_write_regs(fe, R_EB20, 1);
702
Michael Krufky255b5112008-01-01 22:52:09 -0300703 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300704 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300705
Michael Krufky255b5112008-01-01 22:52:09 -0300706 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300707
708 switch (priv->mode) {
709 case TDA18271_ANALOG:
710 N = freq - 1250000;
711 break;
712 case TDA18271_DIGITAL:
713 N = freq + bw / 2;
714 break;
715 }
716
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300717 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300718
Michael Krufky255b5112008-01-01 22:52:09 -0300719 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300720
721 switch (priv->mode) {
722 case TDA18271_ANALOG:
723 N = freq - 250000;
724 break;
725 case TDA18271_DIGITAL:
726 N = freq + bw / 2 + 1000000;
727 break;
728 }
729
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300730 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300731
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300732 ret = tda18271_write_regs(fe, R_EP3, 11);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300733 if (tda_fail(ret))
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300734 return ret;
735
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300736 msleep(5); /* RF tracking filter calibration initialization */
737
Michael Krufky255b5112008-01-01 22:52:09 -0300738 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300739 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300740 tda18271_write_regs(fe, R_EB13, 1);
741
Michael Krufky255b5112008-01-01 22:52:09 -0300742 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300743 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300744
Michael Krufky255b5112008-01-01 22:52:09 -0300745 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300746 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300747
748 tda18271_write_regs(fe, R_EP2, 1);
749 tda18271_write_regs(fe, R_EP1, 1);
750 tda18271_write_regs(fe, R_EP2, 1);
751 tda18271_write_regs(fe, R_EP1, 1);
752
753 regs[R_EB4] &= 0x07;
754 regs[R_EB4] |= 0x40;
755 tda18271_write_regs(fe, R_EB4, 1);
756
757 regs[R_EB7] = 0x40;
758 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300759 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300760
761 regs[R_EB20] = 0xec;
762 tda18271_write_regs(fe, R_EB20, 1);
763 msleep(60); /* RF tracking filter calibration completion */
764
765 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
766 tda18271_write_regs(fe, R_EP4, 1);
767
768 tda18271_write_regs(fe, R_EP1, 1);
769
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300770 /* RF tracking filter correction for VHF_Low band */
771 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300772 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300773
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300774 return 0;
775}
776
Michael Krufkyd1c53422008-04-22 14:42:07 -0300777/* ------------------------------------------------------------------ */
778
Michael Krufky12afe372008-04-22 14:42:07 -0300779static int tda18271_ir_cal_init(struct dvb_frontend *fe)
780{
781 struct tda18271_priv *priv = fe->tuner_priv;
782 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300783 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300784
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300785 ret = tda18271_read_regs(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300786 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300787 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300788
789 /* test IR_CAL_OK to see if we need init */
790 if ((regs[R_EP1] & 0x08) == 0)
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300791 ret = tda18271_init_regs(fe);
792fail:
793 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300794}
795
796static int tda18271_init(struct dvb_frontend *fe)
797{
798 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300799 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300800
801 mutex_lock(&priv->lock);
802
803 /* power up */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300804 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300805 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300806 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300807
808 /* initialization */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300809 ret = tda18271_ir_cal_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300810 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300811 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300812
813 if (priv->id == TDA18271HDC2)
814 tda18271c2_rf_cal_init(fe);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300815fail:
Michael Krufky12afe372008-04-22 14:42:07 -0300816 mutex_unlock(&priv->lock);
817
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300818 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300819}
820
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300821/* ------------------------------------------------------------------ */
822
823static int tda18271_agc(struct dvb_frontend *fe)
824{
825 struct tda18271_priv *priv = fe->tuner_priv;
826 int ret = 0;
827
828 switch (priv->config) {
829 case 0:
830 /* no LNA */
831 tda_dbg("no agc configuration provided\n");
832 break;
833 case 3:
834 /* switch with GPIO of saa713x */
835 tda_dbg("invoking callback\n");
836 if (fe->callback)
837 ret = fe->callback(priv->i2c_props.adap->algo_data,
838 DVB_FRONTEND_COMPONENT_TUNER,
839 TDA18271_CALLBACK_CMD_AGC_ENABLE,
840 priv->mode);
841 break;
842 case 1:
843 case 2:
844 default:
845 /* n/a - currently not supported */
846 tda_err("unsupported configuration: %d\n", priv->config);
847 ret = -EINVAL;
848 break;
849 }
850 return ret;
851}
852
Michael Krufkyd1c53422008-04-22 14:42:07 -0300853static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300854 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300855{
856 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300857 int ret;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300858
Michael Krufky7f7203d2008-04-22 14:46:06 -0300859 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
860 freq, map->if_freq, bw, map->agc_mode, map->std);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300861
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300862 ret = tda18271_agc(fe);
863 if (tda_fail(ret))
864 tda_warn("failed to configure agc\n");
865
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300866 ret = tda18271_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300867 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300868 goto fail;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300869
870 mutex_lock(&priv->lock);
871
Michael Krufkyd1c53422008-04-22 14:42:07 -0300872 switch (priv->id) {
873 case TDA18271HDC1:
874 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
875 break;
876 case TDA18271HDC2:
877 tda18271c2_rf_tracking_filters_correction(fe, freq);
878 break;
879 }
Michael Krufky31940e32008-05-04 19:37:27 -0300880 ret = tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300881
Michael Krufky8d316bf2008-01-06 15:31:35 -0300882 mutex_unlock(&priv->lock);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300883fail:
884 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300885}
886
887/* ------------------------------------------------------------------ */
888
889static int tda18271_set_params(struct dvb_frontend *fe,
890 struct dvb_frontend_parameters *params)
891{
892 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300893 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300894 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300895 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300896 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300897
898 priv->mode = TDA18271_DIGITAL;
899
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300900 if (fe->ops.info.type == FE_ATSC) {
901 switch (params->u.vsb.modulation) {
902 case VSB_8:
903 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300904 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300905 break;
906 case QAM_64:
907 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300908 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300909 break;
910 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300911 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300912 return -EINVAL;
913 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300914#if 0
915 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300916 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300917#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300918 bw = 6000000;
919 } else if (fe->ops.info.type == FE_OFDM) {
920 switch (params->u.ofdm.bandwidth) {
921 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300922 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300923 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300924 break;
925 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300926 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300927 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300928 break;
929 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300930 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300931 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300932 break;
933 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300934 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300935 return -EINVAL;
936 }
937 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300938 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300939 return -EINVAL;
940 }
941
Michael Krufkyed736832008-01-19 17:41:04 -0300942 /* When tuning digital, the analog demod must be tri-stated */
943 if (fe->ops.analog_ops.standby)
944 fe->ops.analog_ops.standby(fe);
945
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300946 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300947
Michael Krufky4bd5d102008-05-04 21:32:21 -0300948 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300949 goto fail;
950
951 priv->frequency = freq;
952 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
953 params->u.ofdm.bandwidth : 0;
954fail:
955 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300956}
957
958static int tda18271_set_analog_params(struct dvb_frontend *fe,
959 struct analog_parameters *params)
960{
961 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300962 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300963 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300964 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300965 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300966 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300967
968 priv->mode = TDA18271_ANALOG;
969
Michael Krufkyc353f422008-01-08 10:38:10 -0300970 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc353f422008-01-08 10:38:10 -0300971 freq = freq / 1000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300972 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -0300973 mode = "fm";
974 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300975 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300976 mode = "MN";
977 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300978 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300979 mode = "B";
980 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300981 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300982 mode = "GH";
983 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300984 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300985 mode = "I";
986 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300987 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300988 mode = "DK";
989 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300990 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300991 mode = "L";
992 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300993 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -0300994 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300995 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300996 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300997 mode = "xx";
998 }
999
Michael Krufky182519f2007-12-25 15:10:11 -03001000 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001001
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001002 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001003
Michael Krufky4bd5d102008-05-04 21:32:21 -03001004 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001005 goto fail;
1006
1007 priv->frequency = freq;
1008 priv->bandwidth = 0;
1009fail:
1010 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001011}
1012
Michael Krufky518d8732008-01-13 17:01:01 -03001013static int tda18271_sleep(struct dvb_frontend *fe)
1014{
1015 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -03001016 int ret;
Michael Krufky518d8732008-01-13 17:01:01 -03001017
1018 mutex_lock(&priv->lock);
1019
1020 /* standby mode w/ slave tuner output
1021 * & loop thru & xtal oscillator on */
Michael Krufkyd35fcca2008-05-03 18:20:21 -03001022 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky518d8732008-01-13 17:01:01 -03001023
1024 mutex_unlock(&priv->lock);
1025
Michael Krufkyd35fcca2008-05-03 18:20:21 -03001026 return ret;
Michael Krufky518d8732008-01-13 17:01:01 -03001027}
1028
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001029static int tda18271_release(struct dvb_frontend *fe)
1030{
Michael Krufkya4f263b2008-01-06 15:52:56 -03001031 struct tda18271_priv *priv = fe->tuner_priv;
1032
1033 mutex_lock(&tda18271_list_mutex);
1034
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001035 if (priv)
1036 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -03001037
Michael Krufkya4f263b2008-01-06 15:52:56 -03001038 mutex_unlock(&tda18271_list_mutex);
1039
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001040 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001041
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001042 return 0;
1043}
1044
1045static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1046{
1047 struct tda18271_priv *priv = fe->tuner_priv;
1048 *frequency = priv->frequency;
1049 return 0;
1050}
1051
1052static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1053{
1054 struct tda18271_priv *priv = fe->tuner_priv;
1055 *bandwidth = priv->bandwidth;
1056 return 0;
1057}
1058
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001059/* ------------------------------------------------------------------ */
1060
1061#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001062 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -03001063 map->std_cfg.agc_mode + map->std_cfg.std + \
1064 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001065 tda_dbg("Using custom std config for %s\n", name); \
1066 memcpy(&std->std_cfg, &map->std_cfg, \
1067 sizeof(struct tda18271_std_map_item)); \
1068 } } while (0)
1069
1070#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -03001071 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1072 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001073 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -03001074 std->std_cfg.agc_mode, std->std_cfg.std, \
1075 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001076 } while (0)
1077
1078static int tda18271_dump_std_map(struct dvb_frontend *fe)
1079{
1080 struct tda18271_priv *priv = fe->tuner_priv;
1081 struct tda18271_std_map *std = &priv->std;
1082
1083 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -03001084 tda18271_dump_std_item(fm_radio, " fm ");
1085 tda18271_dump_std_item(atv_b, "atv b ");
1086 tda18271_dump_std_item(atv_dk, "atv dk");
1087 tda18271_dump_std_item(atv_gh, "atv gh");
1088 tda18271_dump_std_item(atv_i, "atv i ");
1089 tda18271_dump_std_item(atv_l, "atv l ");
1090 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001091 tda18271_dump_std_item(atv_mn, "atv mn");
1092 tda18271_dump_std_item(atsc_6, "atsc 6");
1093 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1094 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1095 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -03001096 tda18271_dump_std_item(qam_6, "qam 6 ");
1097 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001098
1099 return 0;
1100}
1101
1102static int tda18271_update_std_map(struct dvb_frontend *fe,
1103 struct tda18271_std_map *map)
1104{
1105 struct tda18271_priv *priv = fe->tuner_priv;
1106 struct tda18271_std_map *std = &priv->std;
1107
1108 if (!map)
1109 return -EINVAL;
1110
Michael Krufkyc353f422008-01-08 10:38:10 -03001111 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001112 tda18271_update_std(atv_b, "atv b");
1113 tda18271_update_std(atv_dk, "atv dk");
1114 tda18271_update_std(atv_gh, "atv gh");
1115 tda18271_update_std(atv_i, "atv i");
1116 tda18271_update_std(atv_l, "atv l");
1117 tda18271_update_std(atv_lc, "atv l'");
1118 tda18271_update_std(atv_mn, "atv mn");
1119 tda18271_update_std(atsc_6, "atsc 6");
1120 tda18271_update_std(dvbt_6, "dvbt 6");
1121 tda18271_update_std(dvbt_7, "dvbt 7");
1122 tda18271_update_std(dvbt_8, "dvbt 8");
1123 tda18271_update_std(qam_6, "qam 6");
1124 tda18271_update_std(qam_8, "qam 8");
1125
1126 return 0;
1127}
1128
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001129static int tda18271_get_id(struct dvb_frontend *fe)
1130{
1131 struct tda18271_priv *priv = fe->tuner_priv;
1132 unsigned char *regs = priv->tda18271_regs;
1133 char *name;
1134 int ret = 0;
1135
Michael Krufky8d316bf2008-01-06 15:31:35 -03001136 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001137 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001138 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001139
1140 switch (regs[R_ID] & 0x7f) {
1141 case 3:
1142 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001143 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001144 break;
1145 case 4:
1146 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001147 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001148 break;
1149 default:
1150 name = "Unknown device";
1151 ret = -EINVAL;
1152 break;
1153 }
1154
Michael Krufky182519f2007-12-25 15:10:11 -03001155 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001156 i2c_adapter_id(priv->i2c_props.adap),
1157 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001158 (0 == ret) ? "" : ", device not supported.");
1159
1160 return ret;
1161}
1162
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001163static struct dvb_tuner_ops tda18271_tuner_ops = {
1164 .info = {
1165 .name = "NXP TDA18271HD",
1166 .frequency_min = 45000000,
1167 .frequency_max = 864000000,
1168 .frequency_step = 62500
1169 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001170 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001171 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001172 .set_params = tda18271_set_params,
1173 .set_analog_params = tda18271_set_analog_params,
1174 .release = tda18271_release,
1175 .get_frequency = tda18271_get_frequency,
1176 .get_bandwidth = tda18271_get_bandwidth,
1177};
1178
1179struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001180 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001181 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001182{
1183 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001184 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001185
Michael Krufkya4f263b2008-01-06 15:52:56 -03001186 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001187
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001188 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1189 hybrid_tuner_instance_list,
1190 i2c, addr, "tda18271");
1191 switch (instance) {
1192 case 0:
1193 goto fail;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001194 case 1:
Michael Krufky81016b42009-08-27 16:58:06 -03001195 {
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001196 /* new tuner instance */
Michael Krufky81016b42009-08-27 16:58:06 -03001197 int rf_cal_on_startup;
1198
Michael Krufkya4f263b2008-01-06 15:52:56 -03001199 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
Michael Krufky868f5cc2008-04-22 14:46:23 -03001200 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
Michael Krufkyadcc4b32009-03-04 19:42:06 -03001201 priv->config = (cfg) ? cfg->config : 0;
Michael Krufky81016b42009-08-27 16:58:06 -03001202
1203 /* tda18271_cal_on_startup == -1 when cal
1204 * module option is unset */
1205 if (tda18271_cal_on_startup == -1) {
1206 /* honor attach-time configuration */
1207 rf_cal_on_startup =
1208 ((cfg) && (cfg->rf_cal_on_startup)) ? 1 : 0;
1209 } else {
1210 /* module option overrides attach configuration */
1211 rf_cal_on_startup = tda18271_cal_on_startup;
1212 }
1213
Michael Krufkya4f263b2008-01-06 15:52:56 -03001214 priv->cal_initialized = false;
1215 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001216
Michael Krufkya4f263b2008-01-06 15:52:56 -03001217 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001218
Michael Krufky55553092008-04-22 14:46:06 -03001219 if (cfg)
1220 priv->small_i2c = cfg->small_i2c;
1221
Michael Krufky4bd5d102008-05-04 21:32:21 -03001222 if (tda_fail(tda18271_get_id(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001223 goto fail;
1224
Michael Krufky4bd5d102008-05-04 21:32:21 -03001225 if (tda_fail(tda18271_assign_map_layout(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001226 goto fail;
1227
1228 mutex_lock(&priv->lock);
1229 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001230
Michael Krufky81016b42009-08-27 16:58:06 -03001231 if ((rf_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001232 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001233
Michael Krufkya4f263b2008-01-06 15:52:56 -03001234 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001235 break;
Michael Krufky81016b42009-08-27 16:58:06 -03001236 }
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001237 default:
1238 /* existing tuner instance */
1239 fe->tuner_priv = priv;
1240
1241 /* allow dvb driver to override i2c gate setting */
1242 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1243 priv->gate = cfg->gate;
1244 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001245 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001246
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001247 /* override default std map with values in config struct */
1248 if ((cfg) && (cfg->std_map))
1249 tda18271_update_std_map(fe, cfg->std_map);
1250
Michael Krufkya4f263b2008-01-06 15:52:56 -03001251 mutex_unlock(&tda18271_list_mutex);
1252
1253 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1254 sizeof(struct dvb_tuner_ops));
1255
Michael Krufkyc7353722008-03-30 19:40:20 -03001256 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001257 tda18271_dump_std_map(fe);
1258
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001259 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001260fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001261 mutex_unlock(&tda18271_list_mutex);
1262
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001263 tda18271_release(fe);
1264 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001265}
1266EXPORT_SYMBOL_GPL(tda18271_attach);
1267MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1268MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1269MODULE_LICENSE("GPL");
Michael Krufky5ec96b02008-04-22 14:46:23 -03001270MODULE_VERSION("0.3");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001271
1272/*
1273 * Overrides for Emacs so that we follow Linus's tabbing style.
1274 * ---------------------------------------------------------------------------
1275 * Local variables:
1276 * c-basic-offset: 8
1277 * End:
1278 */