blob: e86d16979e75b6bb08f64baf439e8909b88ff99a [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 Krufky4240b462009-08-29 16:25:37 -030039static int tda18271_toggle_output(struct dvb_frontend *fe, int standby)
40{
41 struct tda18271_priv *priv = fe->tuner_priv;
42
43 int ret = tda18271_set_standby_mode(fe, standby ? 1 : 0,
44 priv->output_opt & TDA18271_OUTPUT_LT_OFF ? 1 : 0,
45 priv->output_opt & TDA18271_OUTPUT_XT_OFF ? 1 : 0);
46
47 if (tda_fail(ret))
48 goto fail;
49
50 tda_dbg("%s mode: xtal oscillator %s, slave tuner loop thru %s\n",
51 standby ? "standby" : "active",
52 priv->output_opt & TDA18271_OUTPUT_XT_OFF ? "off" : "on",
53 priv->output_opt & TDA18271_OUTPUT_LT_OFF ? "off" : "on");
54fail:
55 return ret;
56}
57
58/*---------------------------------------------------------------------*/
59
Michael Krufky868f5cc2008-04-22 14:46:23 -030060static inline int charge_pump_source(struct dvb_frontend *fe, int force)
61{
62 struct tda18271_priv *priv = fe->tuner_priv;
63 return tda18271_charge_pump_source(fe,
64 (priv->role == TDA18271_SLAVE) ?
65 TDA18271_CAL_PLL :
66 TDA18271_MAIN_PLL, force);
67}
68
Michael Krufky44e645c2008-06-08 20:10:29 -030069static inline void tda18271_set_if_notch(struct dvb_frontend *fe)
70{
71 struct tda18271_priv *priv = fe->tuner_priv;
72 unsigned char *regs = priv->tda18271_regs;
73
74 switch (priv->mode) {
75 case TDA18271_ANALOG:
76 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
77 break;
78 case TDA18271_DIGITAL:
79 regs[R_MPD] |= 0x80; /* IF notch = 1 */
80 break;
81 }
82}
83
Michael Krufky255b5112008-01-01 22:52:09 -030084static int tda18271_channel_configuration(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -030085 struct tda18271_std_map_item *map,
86 u32 freq, u32 bw)
Michael Krufky255b5112008-01-01 22:52:09 -030087{
88 struct tda18271_priv *priv = fe->tuner_priv;
89 unsigned char *regs = priv->tda18271_regs;
Michael Krufky31940e32008-05-04 19:37:27 -030090 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -030091 u32 N;
92
93 /* update TV broadcast parameters */
94
95 /* set standard */
96 regs[R_EP3] &= ~0x1f; /* clear std bits */
Michael Krufky7f7203d2008-04-22 14:46:06 -030097 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
Michael Krufky255b5112008-01-01 22:52:09 -030098
Michael Krufky51858d12008-06-09 02:03:31 -030099 if (priv->id == TDA18271HDC2) {
100 /* set rfagc to high speed mode */
101 regs[R_EP3] &= ~0x04;
102 }
Michael Krufky40194b22008-04-22 14:46:22 -0300103
Michael Krufky255b5112008-01-01 22:52:09 -0300104 /* set cal mode to normal */
105 regs[R_EP4] &= ~0x03;
106
Michael Krufky44e645c2008-06-08 20:10:29 -0300107 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -0300108 regs[R_EP4] &= ~0x1c; /* clear if level bits */
Michael Krufky14c74b22008-04-22 14:46:21 -0300109 regs[R_EP4] |= (map->if_lvl << 2);
Michael Krufky255b5112008-01-01 22:52:09 -0300110
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300111 /* update FM_RFn */
112 regs[R_EP4] &= ~0x80;
113 regs[R_EP4] |= map->fm_rfn << 7;
Michael Krufky255b5112008-01-01 22:52:09 -0300114
Michael Krufkyc0dc0c12008-04-22 14:46:22 -0300115 /* update rf top / if top */
116 regs[R_EB22] = 0x00;
117 regs[R_EB22] |= map->rfagc_top;
Michael Krufky31940e32008-05-04 19:37:27 -0300118 ret = tda18271_write_regs(fe, R_EB22, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300119 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300120 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300121
122 /* --------------------------------------------------------------- */
123
124 /* disable Power Level Indicator */
125 regs[R_EP1] |= 0x40;
126
Michael Krufky7ae1ac42008-06-11 13:52:49 -0300127 /* make sure thermometer is off */
128 regs[R_TM] &= ~0x10;
129
Michael Krufky255b5112008-01-01 22:52:09 -0300130 /* frequency dependent parameters */
131
132 tda18271_calc_ir_measure(fe, &freq);
133
134 tda18271_calc_bp_filter(fe, &freq);
135
136 tda18271_calc_rf_band(fe, &freq);
137
138 tda18271_calc_gain_taper(fe, &freq);
139
140 /* --------------------------------------------------------------- */
141
142 /* dual tuner and agc1 extra configuration */
143
Michael Krufky868f5cc2008-04-22 14:46:23 -0300144 switch (priv->role) {
145 case TDA18271_MASTER:
146 regs[R_EB1] |= 0x04; /* main vco */
147 break;
148 case TDA18271_SLAVE:
149 regs[R_EB1] &= ~0x04; /* cal vco */
150 break;
151 }
Michael Krufky255b5112008-01-01 22:52:09 -0300152
153 /* agc1 always active */
154 regs[R_EB1] &= ~0x02;
155
156 /* agc1 has priority on agc2 */
157 regs[R_EB1] &= ~0x01;
158
Michael Krufky31940e32008-05-04 19:37:27 -0300159 ret = tda18271_write_regs(fe, R_EB1, 1);
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
163 /* --------------------------------------------------------------- */
164
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300165 N = map->if_freq * 1000 + freq;
Michael Krufky255b5112008-01-01 22:52:09 -0300166
Michael Krufky868f5cc2008-04-22 14:46:23 -0300167 switch (priv->role) {
168 case TDA18271_MASTER:
169 tda18271_calc_main_pll(fe, N);
Michael Krufky44e645c2008-06-08 20:10:29 -0300170 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300171 tda18271_write_regs(fe, R_MPD, 4);
172 break;
173 case TDA18271_SLAVE:
174 tda18271_calc_cal_pll(fe, N);
175 tda18271_write_regs(fe, R_CPD, 4);
176
177 regs[R_MPD] = regs[R_CPD] & 0x7f;
Michael Krufky44e645c2008-06-08 20:10:29 -0300178 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300179 tda18271_write_regs(fe, R_MPD, 1);
180 break;
181 }
Michael Krufky255b5112008-01-01 22:52:09 -0300182
Michael Krufky31940e32008-05-04 19:37:27 -0300183 ret = tda18271_write_regs(fe, R_TM, 7);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300184 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300185 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300186
Michael Krufky868f5cc2008-04-22 14:46:23 -0300187 /* force charge pump source */
188 charge_pump_source(fe, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300189
190 msleep(1);
191
Michael Krufky868f5cc2008-04-22 14:46:23 -0300192 /* return pll to normal operation */
193 charge_pump_source(fe, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300194
Michael Krufky40194b22008-04-22 14:46:22 -0300195 msleep(20);
196
Michael Krufky51858d12008-06-09 02:03:31 -0300197 if (priv->id == TDA18271HDC2) {
198 /* set rfagc to normal speed mode */
199 if (map->fm_rfn)
200 regs[R_EP3] &= ~0x04;
201 else
202 regs[R_EP3] |= 0x04;
203 ret = tda18271_write_regs(fe, R_EP3, 1);
204 }
Michael Krufky31940e32008-05-04 19:37:27 -0300205fail:
206 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300207}
208
209static int tda18271_read_thermometer(struct dvb_frontend *fe)
210{
211 struct tda18271_priv *priv = fe->tuner_priv;
212 unsigned char *regs = priv->tda18271_regs;
213 int tm;
214
215 /* switch thermometer on */
216 regs[R_TM] |= 0x10;
217 tda18271_write_regs(fe, R_TM, 1);
218
219 /* read thermometer info */
220 tda18271_read_regs(fe);
221
222 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
223 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
224
225 if ((regs[R_TM] & 0x20) == 0x20)
226 regs[R_TM] &= ~0x20;
227 else
228 regs[R_TM] |= 0x20;
229
230 tda18271_write_regs(fe, R_TM, 1);
231
232 msleep(10); /* temperature sensing */
233
234 /* read thermometer info */
235 tda18271_read_regs(fe);
236 }
237
238 tm = tda18271_lookup_thermometer(fe);
239
240 /* switch thermometer off */
241 regs[R_TM] &= ~0x10;
242 tda18271_write_regs(fe, R_TM, 1);
243
244 /* set CAL mode to normal */
245 regs[R_EP4] &= ~0x03;
246 tda18271_write_regs(fe, R_EP4, 1);
247
248 return tm;
249}
250
Michael Krufky12afe372008-04-22 14:42:07 -0300251/* ------------------------------------------------------------------ */
252
Michael Krufkyd1c53422008-04-22 14:42:07 -0300253static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
254 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300255{
256 struct tda18271_priv *priv = fe->tuner_priv;
257 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
258 unsigned char *regs = priv->tda18271_regs;
Michael Krufky20f42062008-05-04 19:57:06 -0300259 int tm_current, rfcal_comp, approx, i, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300260 u8 dc_over_dt, rf_tab;
261
262 /* power up */
Michael Krufky20f42062008-05-04 19:57:06 -0300263 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300264 if (tda_fail(ret))
Michael Krufky20f42062008-05-04 19:57:06 -0300265 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300266
267 /* read die current temperature */
268 tm_current = tda18271_read_thermometer(fe);
269
270 /* frequency dependent parameters */
271
272 tda18271_calc_rf_cal(fe, &freq);
273 rf_tab = regs[R_EB14];
274
275 i = tda18271_lookup_rf_band(fe, &freq, NULL);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300276 if (tda_fail(i))
277 return i;
Michael Krufky255b5112008-01-01 22:52:09 -0300278
279 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
280 approx = map[i].rf_a1 *
281 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
282 } else {
283 approx = map[i].rf_a2 *
284 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
285 }
286
287 if (approx < 0)
288 approx = 0;
289 if (approx > 255)
290 approx = 255;
291
292 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
293
294 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300295 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300296
297 regs[R_EB14] = approx + rfcal_comp;
Michael Krufky20f42062008-05-04 19:57:06 -0300298 ret = tda18271_write_regs(fe, R_EB14, 1);
299fail:
300 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300301}
302
303static int tda18271_por(struct dvb_frontend *fe)
304{
305 struct tda18271_priv *priv = fe->tuner_priv;
306 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300307 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300308
309 /* power up detector 1 */
310 regs[R_EB12] &= ~0x20;
Michael Krufky24124f72008-05-03 19:28:00 -0300311 ret = tda18271_write_regs(fe, R_EB12, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300312 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300313 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300314
315 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
316 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300317 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300318 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300319 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300320
321 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
322
323 /* POR mode */
Michael Krufky24124f72008-05-03 19:28:00 -0300324 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300325 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300326 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300327
328 /* disable 1.5 MHz low pass filter */
329 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
330 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
Michael Krufky24124f72008-05-03 19:28:00 -0300331 ret = tda18271_write_regs(fe, R_EB21, 3);
332fail:
333 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300334}
335
336static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
337{
338 struct tda18271_priv *priv = fe->tuner_priv;
339 unsigned char *regs = priv->tda18271_regs;
340 u32 N;
341
342 /* set CAL mode to normal */
343 regs[R_EP4] &= ~0x03;
344 tda18271_write_regs(fe, R_EP4, 1);
345
346 /* switch off agc1 */
347 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
348
349 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
350 tda18271_write_regs(fe, R_EB18, 1);
351
352 /* frequency dependent parameters */
353
354 tda18271_calc_bp_filter(fe, &freq);
355 tda18271_calc_gain_taper(fe, &freq);
356 tda18271_calc_rf_band(fe, &freq);
357 tda18271_calc_km(fe, &freq);
358
359 tda18271_write_regs(fe, R_EP1, 3);
360 tda18271_write_regs(fe, R_EB13, 1);
361
362 /* main pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300363 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300364
365 /* cal pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300366 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300367
368 /* force dcdc converter to 0 V */
369 regs[R_EB14] = 0x00;
370 tda18271_write_regs(fe, R_EB14, 1);
371
372 /* disable plls lock */
373 regs[R_EB20] &= ~0x20;
374 tda18271_write_regs(fe, R_EB20, 1);
375
376 /* set CAL mode to RF tracking filter calibration */
377 regs[R_EP4] |= 0x03;
378 tda18271_write_regs(fe, R_EP4, 2);
379
380 /* --------------------------------------------------------------- */
381
382 /* set the internal calibration signal */
383 N = freq;
384
Michael Krufkyae07d042008-04-22 14:46:21 -0300385 tda18271_calc_cal_pll(fe, N);
386 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300387
388 /* downconvert internal calibration */
389 N += 1000000;
390
391 tda18271_calc_main_pll(fe, N);
392 tda18271_write_regs(fe, R_MPD, 4);
393
394 msleep(5);
395
396 tda18271_write_regs(fe, R_EP2, 1);
397 tda18271_write_regs(fe, R_EP1, 1);
398 tda18271_write_regs(fe, R_EP2, 1);
399 tda18271_write_regs(fe, R_EP1, 1);
400
401 /* --------------------------------------------------------------- */
402
403 /* normal operation for the main pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300404 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300405
406 /* normal operation for the cal pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300407 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300408
Michael Krufkyae07d042008-04-22 14:46:21 -0300409 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300410
411 /* launch the rf tracking filters calibration */
412 regs[R_EB20] |= 0x20;
413 tda18271_write_regs(fe, R_EB20, 1);
414
415 msleep(60); /* calibration */
416
417 /* --------------------------------------------------------------- */
418
419 /* set CAL mode to normal */
420 regs[R_EP4] &= ~0x03;
421
422 /* switch on agc1 */
423 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
424
425 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
426 tda18271_write_regs(fe, R_EB18, 1);
427
428 tda18271_write_regs(fe, R_EP3, 2);
429
430 /* synchronization */
431 tda18271_write_regs(fe, R_EP1, 1);
432
433 /* get calibration result */
434 tda18271_read_extended(fe);
435
436 return regs[R_EB14];
437}
438
439static int tda18271_powerscan(struct dvb_frontend *fe,
440 u32 *freq_in, u32 *freq_out)
441{
442 struct tda18271_priv *priv = fe->tuner_priv;
443 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300444 int sgn, bcal, count, wait, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300445 u8 cid_target;
446 u16 count_limit;
447 u32 freq;
448
449 freq = *freq_in;
450
451 tda18271_calc_rf_band(fe, &freq);
452 tda18271_calc_rf_cal(fe, &freq);
453 tda18271_calc_gain_taper(fe, &freq);
454 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
455
456 tda18271_write_regs(fe, R_EP2, 1);
457 tda18271_write_regs(fe, R_EB14, 1);
458
459 /* downconvert frequency */
460 freq += 1000000;
461
462 tda18271_calc_main_pll(fe, freq);
463 tda18271_write_regs(fe, R_MPD, 4);
464
465 msleep(5); /* pll locking */
466
467 /* detection mode */
468 regs[R_EP4] &= ~0x03;
469 regs[R_EP4] |= 0x01;
470 tda18271_write_regs(fe, R_EP4, 1);
471
472 /* launch power detection measurement */
473 tda18271_write_regs(fe, R_EP2, 1);
474
475 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300476 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300477 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300478 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300479
480 /* algorithm initialization */
481 sgn = 1;
482 *freq_out = *freq_in;
483 bcal = 0;
484 count = 0;
485 wait = false;
486
487 while ((regs[R_EB10] & 0x3f) < cid_target) {
488 /* downconvert updated freq to 1 MHz */
489 freq = *freq_in + (sgn * count) + 1000000;
490
491 tda18271_calc_main_pll(fe, freq);
492 tda18271_write_regs(fe, R_MPD, 4);
493
494 if (wait) {
495 msleep(5); /* pll locking */
496 wait = false;
497 } else
498 udelay(100); /* pll locking */
499
500 /* launch power detection measurement */
501 tda18271_write_regs(fe, R_EP2, 1);
502
503 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300504 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300505 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300506 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300507
508 count += 200;
509
Michael Krufkye7809a02008-04-22 14:46:22 -0300510 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300511 continue;
512
513 if (sgn <= 0)
514 break;
515
516 sgn = -1 * sgn;
517 count = 200;
518 wait = true;
519 }
520
521 if ((regs[R_EB10] & 0x3f) >= cid_target) {
522 bcal = 1;
523 *freq_out = freq - 1000000;
524 } else
525 bcal = 0;
526
Michael Krufkycf04d292008-01-09 00:34:30 -0300527 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300528 bcal, *freq_in, *freq_out, freq);
529
530 return bcal;
531}
532
533static int tda18271_powerscan_init(struct dvb_frontend *fe)
534{
535 struct tda18271_priv *priv = fe->tuner_priv;
536 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300537 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300538
539 /* set standard to digital */
540 regs[R_EP3] &= ~0x1f; /* clear std bits */
541 regs[R_EP3] |= 0x12;
542
543 /* set cal mode to normal */
544 regs[R_EP4] &= ~0x03;
545
Michael Krufky44e645c2008-06-08 20:10:29 -0300546 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -0300547 regs[R_EP4] &= ~0x1c; /* clear if level bits */
548
Michael Krufky24124f72008-05-03 19:28:00 -0300549 ret = tda18271_write_regs(fe, R_EP3, 2);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300550 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300551 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300552
553 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300554 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300555 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300556 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300557
558 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
559
560 /* 1.5 MHz low pass filter */
561 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
562 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
563
Michael Krufky24124f72008-05-03 19:28:00 -0300564 ret = tda18271_write_regs(fe, R_EB21, 3);
565fail:
566 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300567}
568
569static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
570{
571 struct tda18271_priv *priv = fe->tuner_priv;
572 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
573 unsigned char *regs = priv->tda18271_regs;
574 int bcal, rf, i;
575#define RF1 0
576#define RF2 1
577#define RF3 2
578 u32 rf_default[3];
579 u32 rf_freq[3];
580 u8 prog_cal[3];
581 u8 prog_tab[3];
582
583 i = tda18271_lookup_rf_band(fe, &freq, NULL);
584
Michael Krufky4bd5d102008-05-04 21:32:21 -0300585 if (tda_fail(i))
Michael Krufky255b5112008-01-01 22:52:09 -0300586 return i;
587
588 rf_default[RF1] = 1000 * map[i].rf1_def;
589 rf_default[RF2] = 1000 * map[i].rf2_def;
590 rf_default[RF3] = 1000 * map[i].rf3_def;
591
592 for (rf = RF1; rf <= RF3; rf++) {
593 if (0 == rf_default[rf])
594 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300595 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300596
597 /* look for optimized calibration frequency */
598 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300599 if (tda_fail(bcal))
Michael Krufky24124f72008-05-03 19:28:00 -0300600 return bcal;
Michael Krufky255b5112008-01-01 22:52:09 -0300601
602 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
603 prog_tab[rf] = regs[R_EB14];
604
605 if (1 == bcal)
606 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
607 else
608 prog_cal[rf] = prog_tab[rf];
609
610 switch (rf) {
611 case RF1:
612 map[i].rf_a1 = 0;
613 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
614 map[i].rf1 = rf_freq[RF1] / 1000;
615 break;
616 case RF2:
617 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
618 prog_cal[RF1] + prog_tab[RF1]) /
619 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
620 map[i].rf2 = rf_freq[RF2] / 1000;
621 break;
622 case RF3:
623 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
624 prog_cal[RF2] + prog_tab[RF2]) /
625 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
626 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
627 map[i].rf3 = rf_freq[RF3] / 1000;
628 break;
629 default:
630 BUG();
631 }
632 }
633
634 return 0;
635}
636
Michael Krufky09f83c42008-01-05 20:00:09 -0300637static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300638{
639 struct tda18271_priv *priv = fe->tuner_priv;
640 unsigned int i;
Michael Krufky24124f72008-05-03 19:28:00 -0300641 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300642
643 tda_info("tda18271: performing RF tracking filter calibration\n");
644
645 /* wait for die temperature stabilization */
646 msleep(200);
647
Michael Krufky24124f72008-05-03 19:28:00 -0300648 ret = tda18271_powerscan_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300649 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300650 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300651
652 /* rf band calibration */
Michael Krufkyc151c322008-05-04 17:54:23 -0300653 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
654 ret =
Michael Krufky255b5112008-01-01 22:52:09 -0300655 tda18271_rf_tracking_filters_init(fe, 1000 *
656 priv->rf_cal_state[i].rfmax);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300657 if (tda_fail(ret))
Michael Krufkyc151c322008-05-04 17:54:23 -0300658 goto fail;
659 }
Michael Krufky255b5112008-01-01 22:52:09 -0300660
Michael Krufky09f83c42008-01-05 20:00:09 -0300661 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky24124f72008-05-03 19:28:00 -0300662fail:
663 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300664}
665
666/* ------------------------------------------------------------------ */
667
Michael Krufky12afe372008-04-22 14:42:07 -0300668static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300669{
670 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300671 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300672 int ret;
Michael Krufky839c6c92008-01-13 18:29:44 -0300673
674 /* test RF_CAL_OK to see if we need init */
675 if ((regs[R_EP1] & 0x10) == 0)
676 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300677
678 if (priv->cal_initialized)
679 return 0;
680
Michael Krufky24124f72008-05-03 19:28:00 -0300681 ret = tda18271_calc_rf_filter_curve(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300682 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300683 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300684
Michael Krufky24124f72008-05-03 19:28:00 -0300685 ret = tda18271_por(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300686 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300687 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300688
Michael Krufky6bfa6652008-01-07 00:51:48 -0300689 tda_info("tda18271: RF tracking filter calibration complete\n");
690
Michael Krufky255b5112008-01-01 22:52:09 -0300691 priv->cal_initialized = true;
Michael Krufkyc151c322008-05-04 17:54:23 -0300692 goto end;
Michael Krufky24124f72008-05-03 19:28:00 -0300693fail:
Michael Krufkyc151c322008-05-04 17:54:23 -0300694 tda_info("tda18271: RF tracking filter calibration failed!\n");
695end:
Michael Krufky24124f72008-05-03 19:28:00 -0300696 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300697}
698
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300699static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
700 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300701{
702 struct tda18271_priv *priv = fe->tuner_priv;
703 unsigned char *regs = priv->tda18271_regs;
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300704 int ret;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300705 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300706
Michael Krufky255b5112008-01-01 22:52:09 -0300707 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300708 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300709 tda18271_write_regs(fe, R_EP1, 1);
710
711 regs[R_EB4] &= 0x07;
712 regs[R_EB4] |= 0x60;
713 tda18271_write_regs(fe, R_EB4, 1);
714
715 regs[R_EB7] = 0x60;
716 tda18271_write_regs(fe, R_EB7, 1);
717
718 regs[R_EB14] = 0x00;
719 tda18271_write_regs(fe, R_EB14, 1);
720
721 regs[R_EB20] = 0xcc;
722 tda18271_write_regs(fe, R_EB20, 1);
723
Michael Krufky255b5112008-01-01 22:52:09 -0300724 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300725 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300726
Michael Krufky255b5112008-01-01 22:52:09 -0300727 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300728
729 switch (priv->mode) {
730 case TDA18271_ANALOG:
731 N = freq - 1250000;
732 break;
733 case TDA18271_DIGITAL:
734 N = freq + bw / 2;
735 break;
736 }
737
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300738 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300739
Michael Krufky255b5112008-01-01 22:52:09 -0300740 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300741
742 switch (priv->mode) {
743 case TDA18271_ANALOG:
744 N = freq - 250000;
745 break;
746 case TDA18271_DIGITAL:
747 N = freq + bw / 2 + 1000000;
748 break;
749 }
750
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300751 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300752
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300753 ret = tda18271_write_regs(fe, R_EP3, 11);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300754 if (tda_fail(ret))
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300755 return ret;
756
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300757 msleep(5); /* RF tracking filter calibration initialization */
758
Michael Krufky255b5112008-01-01 22:52:09 -0300759 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300760 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300761 tda18271_write_regs(fe, R_EB13, 1);
762
Michael Krufky255b5112008-01-01 22:52:09 -0300763 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300764 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300765
Michael Krufky255b5112008-01-01 22:52:09 -0300766 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300767 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300768
769 tda18271_write_regs(fe, R_EP2, 1);
770 tda18271_write_regs(fe, R_EP1, 1);
771 tda18271_write_regs(fe, R_EP2, 1);
772 tda18271_write_regs(fe, R_EP1, 1);
773
774 regs[R_EB4] &= 0x07;
775 regs[R_EB4] |= 0x40;
776 tda18271_write_regs(fe, R_EB4, 1);
777
778 regs[R_EB7] = 0x40;
779 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300780 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300781
782 regs[R_EB20] = 0xec;
783 tda18271_write_regs(fe, R_EB20, 1);
784 msleep(60); /* RF tracking filter calibration completion */
785
786 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
787 tda18271_write_regs(fe, R_EP4, 1);
788
789 tda18271_write_regs(fe, R_EP1, 1);
790
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300791 /* RF tracking filter correction for VHF_Low band */
792 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300793 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300794
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300795 return 0;
796}
797
Michael Krufkyd1c53422008-04-22 14:42:07 -0300798/* ------------------------------------------------------------------ */
799
Michael Krufky12afe372008-04-22 14:42:07 -0300800static int tda18271_ir_cal_init(struct dvb_frontend *fe)
801{
802 struct tda18271_priv *priv = fe->tuner_priv;
803 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300804 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300805
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300806 ret = tda18271_read_regs(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 /* test IR_CAL_OK to see if we need init */
811 if ((regs[R_EP1] & 0x08) == 0)
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300812 ret = tda18271_init_regs(fe);
813fail:
814 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300815}
816
817static int tda18271_init(struct dvb_frontend *fe)
818{
819 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300820 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300821
822 mutex_lock(&priv->lock);
823
Michael Krufky4240b462009-08-29 16:25:37 -0300824 /* full power up */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300825 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300826 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300827 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300828
829 /* initialization */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300830 ret = tda18271_ir_cal_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300831 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300832 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300833
834 if (priv->id == TDA18271HDC2)
835 tda18271c2_rf_cal_init(fe);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300836fail:
Michael Krufky12afe372008-04-22 14:42:07 -0300837 mutex_unlock(&priv->lock);
838
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300839 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300840}
841
Michael Krufkycc7e26d2009-08-29 16:27:21 -0300842static int tda18271_sleep(struct dvb_frontend *fe)
843{
844 struct tda18271_priv *priv = fe->tuner_priv;
845 int ret;
846
847 mutex_lock(&priv->lock);
848
849 /* enter standby mode, with required output features enabled */
850 ret = tda18271_toggle_output(fe, 1);
851
852 mutex_unlock(&priv->lock);
853
854 return ret;
855}
856
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300857/* ------------------------------------------------------------------ */
858
859static int tda18271_agc(struct dvb_frontend *fe)
860{
861 struct tda18271_priv *priv = fe->tuner_priv;
862 int ret = 0;
863
864 switch (priv->config) {
865 case 0:
866 /* no LNA */
867 tda_dbg("no agc configuration provided\n");
868 break;
869 case 3:
870 /* switch with GPIO of saa713x */
871 tda_dbg("invoking callback\n");
872 if (fe->callback)
873 ret = fe->callback(priv->i2c_props.adap->algo_data,
874 DVB_FRONTEND_COMPONENT_TUNER,
875 TDA18271_CALLBACK_CMD_AGC_ENABLE,
876 priv->mode);
877 break;
878 case 1:
879 case 2:
880 default:
881 /* n/a - currently not supported */
882 tda_err("unsupported configuration: %d\n", priv->config);
883 ret = -EINVAL;
884 break;
885 }
886 return ret;
887}
888
Michael Krufkyd1c53422008-04-22 14:42:07 -0300889static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300890 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300891{
892 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300893 int ret;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300894
Michael Krufky7f7203d2008-04-22 14:46:06 -0300895 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
896 freq, map->if_freq, bw, map->agc_mode, map->std);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300897
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300898 ret = tda18271_agc(fe);
899 if (tda_fail(ret))
900 tda_warn("failed to configure agc\n");
901
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300902 ret = tda18271_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300903 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300904 goto fail;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300905
906 mutex_lock(&priv->lock);
907
Michael Krufkyd1c53422008-04-22 14:42:07 -0300908 switch (priv->id) {
909 case TDA18271HDC1:
910 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
911 break;
912 case TDA18271HDC2:
913 tda18271c2_rf_tracking_filters_correction(fe, freq);
914 break;
915 }
Michael Krufky31940e32008-05-04 19:37:27 -0300916 ret = tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300917
Michael Krufky8d316bf2008-01-06 15:31:35 -0300918 mutex_unlock(&priv->lock);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300919fail:
920 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300921}
922
923/* ------------------------------------------------------------------ */
924
925static int tda18271_set_params(struct dvb_frontend *fe,
926 struct dvb_frontend_parameters *params)
927{
928 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300929 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300930 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300931 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300932 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300933
934 priv->mode = TDA18271_DIGITAL;
935
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300936 if (fe->ops.info.type == FE_ATSC) {
937 switch (params->u.vsb.modulation) {
938 case VSB_8:
939 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300940 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300941 break;
942 case QAM_64:
943 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300944 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300945 break;
946 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300947 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300948 return -EINVAL;
949 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300950#if 0
951 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300952 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300953#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300954 bw = 6000000;
955 } else if (fe->ops.info.type == FE_OFDM) {
956 switch (params->u.ofdm.bandwidth) {
957 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300958 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300959 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300960 break;
961 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300962 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300963 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300964 break;
965 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300966 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300967 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300968 break;
969 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300970 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300971 return -EINVAL;
972 }
973 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300974 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300975 return -EINVAL;
976 }
977
Michael Krufkyed736832008-01-19 17:41:04 -0300978 /* When tuning digital, the analog demod must be tri-stated */
979 if (fe->ops.analog_ops.standby)
980 fe->ops.analog_ops.standby(fe);
981
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300982 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300983
Michael Krufky4bd5d102008-05-04 21:32:21 -0300984 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300985 goto fail;
986
987 priv->frequency = freq;
988 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
989 params->u.ofdm.bandwidth : 0;
990fail:
991 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300992}
993
994static int tda18271_set_analog_params(struct dvb_frontend *fe,
995 struct analog_parameters *params)
996{
997 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300998 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300999 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001000 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001001 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -03001002 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001003
1004 priv->mode = TDA18271_ANALOG;
1005
Michael Krufkyc353f422008-01-08 10:38:10 -03001006 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc353f422008-01-08 10:38:10 -03001007 freq = freq / 1000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001008 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -03001009 mode = "fm";
1010 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001011 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001012 mode = "MN";
1013 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001014 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001015 mode = "B";
1016 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001017 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001018 mode = "GH";
1019 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001020 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001021 mode = "I";
1022 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001023 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001024 mode = "DK";
1025 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001026 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001027 mode = "L";
1028 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001029 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -03001030 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001031 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001032 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001033 mode = "xx";
1034 }
1035
Michael Krufky182519f2007-12-25 15:10:11 -03001036 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001037
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001038 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001039
Michael Krufky4bd5d102008-05-04 21:32:21 -03001040 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001041 goto fail;
1042
1043 priv->frequency = freq;
1044 priv->bandwidth = 0;
1045fail:
1046 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001047}
1048
1049static int tda18271_release(struct dvb_frontend *fe)
1050{
Michael Krufkya4f263b2008-01-06 15:52:56 -03001051 struct tda18271_priv *priv = fe->tuner_priv;
1052
1053 mutex_lock(&tda18271_list_mutex);
1054
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001055 if (priv)
1056 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -03001057
Michael Krufkya4f263b2008-01-06 15:52:56 -03001058 mutex_unlock(&tda18271_list_mutex);
1059
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001060 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001061
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001062 return 0;
1063}
1064
1065static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1066{
1067 struct tda18271_priv *priv = fe->tuner_priv;
1068 *frequency = priv->frequency;
1069 return 0;
1070}
1071
1072static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1073{
1074 struct tda18271_priv *priv = fe->tuner_priv;
1075 *bandwidth = priv->bandwidth;
1076 return 0;
1077}
1078
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001079/* ------------------------------------------------------------------ */
1080
1081#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001082 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -03001083 map->std_cfg.agc_mode + map->std_cfg.std + \
1084 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001085 tda_dbg("Using custom std config for %s\n", name); \
1086 memcpy(&std->std_cfg, &map->std_cfg, \
1087 sizeof(struct tda18271_std_map_item)); \
1088 } } while (0)
1089
1090#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -03001091 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1092 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001093 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -03001094 std->std_cfg.agc_mode, std->std_cfg.std, \
1095 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001096 } while (0)
1097
1098static int tda18271_dump_std_map(struct dvb_frontend *fe)
1099{
1100 struct tda18271_priv *priv = fe->tuner_priv;
1101 struct tda18271_std_map *std = &priv->std;
1102
1103 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -03001104 tda18271_dump_std_item(fm_radio, " fm ");
1105 tda18271_dump_std_item(atv_b, "atv b ");
1106 tda18271_dump_std_item(atv_dk, "atv dk");
1107 tda18271_dump_std_item(atv_gh, "atv gh");
1108 tda18271_dump_std_item(atv_i, "atv i ");
1109 tda18271_dump_std_item(atv_l, "atv l ");
1110 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001111 tda18271_dump_std_item(atv_mn, "atv mn");
1112 tda18271_dump_std_item(atsc_6, "atsc 6");
1113 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1114 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1115 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -03001116 tda18271_dump_std_item(qam_6, "qam 6 ");
1117 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001118
1119 return 0;
1120}
1121
1122static int tda18271_update_std_map(struct dvb_frontend *fe,
1123 struct tda18271_std_map *map)
1124{
1125 struct tda18271_priv *priv = fe->tuner_priv;
1126 struct tda18271_std_map *std = &priv->std;
1127
1128 if (!map)
1129 return -EINVAL;
1130
Michael Krufkyc353f422008-01-08 10:38:10 -03001131 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001132 tda18271_update_std(atv_b, "atv b");
1133 tda18271_update_std(atv_dk, "atv dk");
1134 tda18271_update_std(atv_gh, "atv gh");
1135 tda18271_update_std(atv_i, "atv i");
1136 tda18271_update_std(atv_l, "atv l");
1137 tda18271_update_std(atv_lc, "atv l'");
1138 tda18271_update_std(atv_mn, "atv mn");
1139 tda18271_update_std(atsc_6, "atsc 6");
1140 tda18271_update_std(dvbt_6, "dvbt 6");
1141 tda18271_update_std(dvbt_7, "dvbt 7");
1142 tda18271_update_std(dvbt_8, "dvbt 8");
1143 tda18271_update_std(qam_6, "qam 6");
1144 tda18271_update_std(qam_8, "qam 8");
1145
1146 return 0;
1147}
1148
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001149static int tda18271_get_id(struct dvb_frontend *fe)
1150{
1151 struct tda18271_priv *priv = fe->tuner_priv;
1152 unsigned char *regs = priv->tda18271_regs;
1153 char *name;
1154 int ret = 0;
1155
Michael Krufky8d316bf2008-01-06 15:31:35 -03001156 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001157 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001158 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001159
1160 switch (regs[R_ID] & 0x7f) {
1161 case 3:
1162 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001163 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001164 break;
1165 case 4:
1166 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001167 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001168 break;
1169 default:
1170 name = "Unknown device";
1171 ret = -EINVAL;
1172 break;
1173 }
1174
Michael Krufky182519f2007-12-25 15:10:11 -03001175 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001176 i2c_adapter_id(priv->i2c_props.adap),
1177 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001178 (0 == ret) ? "" : ", device not supported.");
1179
1180 return ret;
1181}
1182
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001183static struct dvb_tuner_ops tda18271_tuner_ops = {
1184 .info = {
1185 .name = "NXP TDA18271HD",
1186 .frequency_min = 45000000,
1187 .frequency_max = 864000000,
1188 .frequency_step = 62500
1189 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001190 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001191 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001192 .set_params = tda18271_set_params,
1193 .set_analog_params = tda18271_set_analog_params,
1194 .release = tda18271_release,
1195 .get_frequency = tda18271_get_frequency,
1196 .get_bandwidth = tda18271_get_bandwidth,
1197};
1198
1199struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001200 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001201 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001202{
1203 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001204 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001205
Michael Krufkya4f263b2008-01-06 15:52:56 -03001206 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001207
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001208 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1209 hybrid_tuner_instance_list,
1210 i2c, addr, "tda18271");
1211 switch (instance) {
1212 case 0:
1213 goto fail;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001214 case 1:
Michael Krufky81016b42009-08-27 16:58:06 -03001215 {
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001216 /* new tuner instance */
Michael Krufky81016b42009-08-27 16:58:06 -03001217 int rf_cal_on_startup;
1218
Michael Krufkya4f263b2008-01-06 15:52:56 -03001219 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
Michael Krufky868f5cc2008-04-22 14:46:23 -03001220 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
Michael Krufkyadcc4b32009-03-04 19:42:06 -03001221 priv->config = (cfg) ? cfg->config : 0;
Michael Krufky4240b462009-08-29 16:25:37 -03001222 priv->output_opt = (cfg) ?
1223 cfg->output_opt : TDA18271_OUTPUT_LT_XT_ON;
Michael Krufky81016b42009-08-27 16:58:06 -03001224
1225 /* tda18271_cal_on_startup == -1 when cal
1226 * module option is unset */
1227 if (tda18271_cal_on_startup == -1) {
1228 /* honor attach-time configuration */
1229 rf_cal_on_startup =
1230 ((cfg) && (cfg->rf_cal_on_startup)) ? 1 : 0;
1231 } else {
1232 /* module option overrides attach configuration */
1233 rf_cal_on_startup = tda18271_cal_on_startup;
1234 }
1235
Michael Krufkya4f263b2008-01-06 15:52:56 -03001236 priv->cal_initialized = false;
1237 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001238
Michael Krufkya4f263b2008-01-06 15:52:56 -03001239 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001240
Michael Krufky55553092008-04-22 14:46:06 -03001241 if (cfg)
1242 priv->small_i2c = cfg->small_i2c;
1243
Michael Krufky4bd5d102008-05-04 21:32:21 -03001244 if (tda_fail(tda18271_get_id(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001245 goto fail;
1246
Michael Krufky4bd5d102008-05-04 21:32:21 -03001247 if (tda_fail(tda18271_assign_map_layout(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001248 goto fail;
1249
1250 mutex_lock(&priv->lock);
1251 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001252
Michael Krufky81016b42009-08-27 16:58:06 -03001253 if ((rf_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001254 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001255
Michael Krufkya4f263b2008-01-06 15:52:56 -03001256 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001257 break;
Michael Krufky81016b42009-08-27 16:58:06 -03001258 }
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001259 default:
1260 /* existing tuner instance */
1261 fe->tuner_priv = priv;
1262
1263 /* allow dvb driver to override i2c gate setting */
1264 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1265 priv->gate = cfg->gate;
1266 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001267 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001268
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001269 /* override default std map with values in config struct */
1270 if ((cfg) && (cfg->std_map))
1271 tda18271_update_std_map(fe, cfg->std_map);
1272
Michael Krufkya4f263b2008-01-06 15:52:56 -03001273 mutex_unlock(&tda18271_list_mutex);
1274
1275 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1276 sizeof(struct dvb_tuner_ops));
1277
Michael Krufkyc7353722008-03-30 19:40:20 -03001278 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001279 tda18271_dump_std_map(fe);
1280
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001281 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001282fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001283 mutex_unlock(&tda18271_list_mutex);
1284
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001285 tda18271_release(fe);
1286 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001287}
1288EXPORT_SYMBOL_GPL(tda18271_attach);
1289MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1290MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1291MODULE_LICENSE("GPL");
Michael Krufky5ec96b02008-04-22 14:46:23 -03001292MODULE_VERSION("0.3");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001293
1294/*
1295 * Overrides for Emacs so that we follow Linus's tabbing style.
1296 * ---------------------------------------------------------------------------
1297 * Local variables:
1298 * c-basic-offset: 8
1299 * End:
1300 */