blob: 89c01fb1f85981c5cbfa909e1579c2f75212cf07 [file] [log] [blame]
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001/*
Michael Krufky6ca04de2007-11-23 16:52:15 -03002 tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
Michael Krufky5bea1cd2007-10-22 09:56:38 -03003
Michael Krufky59067f72008-01-02 01:58:26 -03004 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
Michael Krufky5bea1cd2007-10-22 09:56:38 -03005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
Michael Krufky5bea1cd2007-10-22 09:56:38 -030021#include <linux/delay.h>
22#include <linux/videodev2.h>
Michael Krufky6ca04de2007-11-23 16:52:15 -030023#include "tda18271-priv.h"
Michael Krufky5bea1cd2007-10-22 09:56:38 -030024
Michael Krufkyb5f3e1e2007-12-02 16:36:05 -030025int tda18271_debug;
Michael Krufky54465b02007-11-23 18:14:53 -030026module_param_named(debug, tda18271_debug, int, 0644);
Michael Krufky0e1fab92008-01-03 01:40:47 -030027MODULE_PARM_DESC(debug, "set debug level "
Michael Krufkycf04d292008-01-09 00:34:30 -030028 "(info=1, map=2, reg=4, adv=8, cal=16 (or-able))");
Michael Krufky5bea1cd2007-10-22 09:56:38 -030029
Michael Krufkybc835d82008-01-14 11:10:54 -030030static int tda18271_cal_on_startup;
Michael Krufky0f962512008-01-13 22:01:07 -030031module_param_named(cal, tda18271_cal_on_startup, int, 0644);
32MODULE_PARM_DESC(cal, "perform RF tracking filter calibration on startup");
33
Michael Krufkya4f263b2008-01-06 15:52:56 -030034static DEFINE_MUTEX(tda18271_list_mutex);
Michael Krufkyf9e315a2008-04-22 14:41:54 -030035static LIST_HEAD(hybrid_tuner_instance_list);
Michael Krufkya4f263b2008-01-06 15:52:56 -030036
Michael Krufky5bea1cd2007-10-22 09:56:38 -030037/*---------------------------------------------------------------------*/
38
Michael Krufky868f5cc2008-04-22 14:46:23 -030039static inline int charge_pump_source(struct dvb_frontend *fe, int force)
40{
41 struct tda18271_priv *priv = fe->tuner_priv;
42 return tda18271_charge_pump_source(fe,
43 (priv->role == TDA18271_SLAVE) ?
44 TDA18271_CAL_PLL :
45 TDA18271_MAIN_PLL, force);
46}
47
Michael Krufky255b5112008-01-01 22:52:09 -030048static int tda18271_channel_configuration(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -030049 struct tda18271_std_map_item *map,
50 u32 freq, u32 bw)
Michael Krufky255b5112008-01-01 22:52:09 -030051{
52 struct tda18271_priv *priv = fe->tuner_priv;
53 unsigned char *regs = priv->tda18271_regs;
Michael Krufky31940e32008-05-04 19:37:27 -030054 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -030055 u32 N;
56
57 /* update TV broadcast parameters */
58
59 /* set standard */
60 regs[R_EP3] &= ~0x1f; /* clear std bits */
Michael Krufky7f7203d2008-04-22 14:46:06 -030061 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
Michael Krufky255b5112008-01-01 22:52:09 -030062
Michael Krufky40194b22008-04-22 14:46:22 -030063 /* set rfagc to high speed mode */
64 regs[R_EP3] &= ~0x04;
65
Michael Krufky255b5112008-01-01 22:52:09 -030066 /* set cal mode to normal */
67 regs[R_EP4] &= ~0x03;
68
69 /* update IF output level & IF notch frequency */
70 regs[R_EP4] &= ~0x1c; /* clear if level bits */
Michael Krufky14c74b22008-04-22 14:46:21 -030071 regs[R_EP4] |= (map->if_lvl << 2);
Michael Krufky255b5112008-01-01 22:52:09 -030072
73 switch (priv->mode) {
74 case TDA18271_ANALOG:
75 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
76 break;
77 case TDA18271_DIGITAL:
Michael Krufky255b5112008-01-01 22:52:09 -030078 regs[R_MPD] |= 0x80; /* IF notch = 1 */
79 break;
80 }
Michael Krufkyc353f422008-01-08 10:38:10 -030081
Michael Krufkyc293d0a2008-04-22 14:46:06 -030082 /* update FM_RFn */
83 regs[R_EP4] &= ~0x80;
84 regs[R_EP4] |= map->fm_rfn << 7;
Michael Krufky255b5112008-01-01 22:52:09 -030085
Michael Krufkyc0dc0c12008-04-22 14:46:22 -030086 /* update rf top / if top */
87 regs[R_EB22] = 0x00;
88 regs[R_EB22] |= map->rfagc_top;
Michael Krufky31940e32008-05-04 19:37:27 -030089 ret = tda18271_write_regs(fe, R_EB22, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -030090 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -030091 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -030092
93 /* --------------------------------------------------------------- */
94
95 /* disable Power Level Indicator */
96 regs[R_EP1] |= 0x40;
97
98 /* frequency dependent parameters */
99
100 tda18271_calc_ir_measure(fe, &freq);
101
102 tda18271_calc_bp_filter(fe, &freq);
103
104 tda18271_calc_rf_band(fe, &freq);
105
106 tda18271_calc_gain_taper(fe, &freq);
107
108 /* --------------------------------------------------------------- */
109
110 /* dual tuner and agc1 extra configuration */
111
Michael Krufky868f5cc2008-04-22 14:46:23 -0300112 switch (priv->role) {
113 case TDA18271_MASTER:
114 regs[R_EB1] |= 0x04; /* main vco */
115 break;
116 case TDA18271_SLAVE:
117 regs[R_EB1] &= ~0x04; /* cal vco */
118 break;
119 }
Michael Krufky255b5112008-01-01 22:52:09 -0300120
121 /* agc1 always active */
122 regs[R_EB1] &= ~0x02;
123
124 /* agc1 has priority on agc2 */
125 regs[R_EB1] &= ~0x01;
126
Michael Krufky31940e32008-05-04 19:37:27 -0300127 ret = tda18271_write_regs(fe, R_EB1, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300128 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300129 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300130
131 /* --------------------------------------------------------------- */
132
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300133 N = map->if_freq * 1000 + freq;
Michael Krufky255b5112008-01-01 22:52:09 -0300134
Michael Krufky868f5cc2008-04-22 14:46:23 -0300135 switch (priv->role) {
136 case TDA18271_MASTER:
137 tda18271_calc_main_pll(fe, N);
138 tda18271_write_regs(fe, R_MPD, 4);
139 break;
140 case TDA18271_SLAVE:
141 tda18271_calc_cal_pll(fe, N);
142 tda18271_write_regs(fe, R_CPD, 4);
143
144 regs[R_MPD] = regs[R_CPD] & 0x7f;
145 tda18271_write_regs(fe, R_MPD, 1);
146 break;
147 }
Michael Krufky255b5112008-01-01 22:52:09 -0300148
Michael Krufky31940e32008-05-04 19:37:27 -0300149 ret = tda18271_write_regs(fe, R_TM, 7);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300150 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300151 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300152
Michael Krufky868f5cc2008-04-22 14:46:23 -0300153 /* force charge pump source */
154 charge_pump_source(fe, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300155
156 msleep(1);
157
Michael Krufky868f5cc2008-04-22 14:46:23 -0300158 /* return pll to normal operation */
159 charge_pump_source(fe, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300160
Michael Krufky40194b22008-04-22 14:46:22 -0300161 msleep(20);
162
163 /* set rfagc to normal speed mode */
164 if (map->fm_rfn)
165 regs[R_EP3] &= ~0x04;
166 else
167 regs[R_EP3] |= 0x04;
Michael Krufky31940e32008-05-04 19:37:27 -0300168 ret = tda18271_write_regs(fe, R_EP3, 1);
169fail:
170 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300171}
172
173static int tda18271_read_thermometer(struct dvb_frontend *fe)
174{
175 struct tda18271_priv *priv = fe->tuner_priv;
176 unsigned char *regs = priv->tda18271_regs;
177 int tm;
178
179 /* switch thermometer on */
180 regs[R_TM] |= 0x10;
181 tda18271_write_regs(fe, R_TM, 1);
182
183 /* read thermometer info */
184 tda18271_read_regs(fe);
185
186 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
187 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
188
189 if ((regs[R_TM] & 0x20) == 0x20)
190 regs[R_TM] &= ~0x20;
191 else
192 regs[R_TM] |= 0x20;
193
194 tda18271_write_regs(fe, R_TM, 1);
195
196 msleep(10); /* temperature sensing */
197
198 /* read thermometer info */
199 tda18271_read_regs(fe);
200 }
201
202 tm = tda18271_lookup_thermometer(fe);
203
204 /* switch thermometer off */
205 regs[R_TM] &= ~0x10;
206 tda18271_write_regs(fe, R_TM, 1);
207
208 /* set CAL mode to normal */
209 regs[R_EP4] &= ~0x03;
210 tda18271_write_regs(fe, R_EP4, 1);
211
212 return tm;
213}
214
Michael Krufky12afe372008-04-22 14:42:07 -0300215/* ------------------------------------------------------------------ */
216
Michael Krufkyd1c53422008-04-22 14:42:07 -0300217static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
218 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300219{
220 struct tda18271_priv *priv = fe->tuner_priv;
221 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
222 unsigned char *regs = priv->tda18271_regs;
Michael Krufky20f42062008-05-04 19:57:06 -0300223 int tm_current, rfcal_comp, approx, i, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300224 u8 dc_over_dt, rf_tab;
225
226 /* power up */
Michael Krufky20f42062008-05-04 19:57:06 -0300227 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300228 if (tda_fail(ret))
Michael Krufky20f42062008-05-04 19:57:06 -0300229 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300230
231 /* read die current temperature */
232 tm_current = tda18271_read_thermometer(fe);
233
234 /* frequency dependent parameters */
235
236 tda18271_calc_rf_cal(fe, &freq);
237 rf_tab = regs[R_EB14];
238
239 i = tda18271_lookup_rf_band(fe, &freq, NULL);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300240 if (tda_fail(i))
241 return i;
Michael Krufky255b5112008-01-01 22:52:09 -0300242
243 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
244 approx = map[i].rf_a1 *
245 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
246 } else {
247 approx = map[i].rf_a2 *
248 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
249 }
250
251 if (approx < 0)
252 approx = 0;
253 if (approx > 255)
254 approx = 255;
255
256 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
257
258 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300259 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300260
261 regs[R_EB14] = approx + rfcal_comp;
Michael Krufky20f42062008-05-04 19:57:06 -0300262 ret = tda18271_write_regs(fe, R_EB14, 1);
263fail:
264 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300265}
266
267static int tda18271_por(struct dvb_frontend *fe)
268{
269 struct tda18271_priv *priv = fe->tuner_priv;
270 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300271 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300272
273 /* power up detector 1 */
274 regs[R_EB12] &= ~0x20;
Michael Krufky24124f72008-05-03 19:28:00 -0300275 ret = tda18271_write_regs(fe, R_EB12, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300276 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300277 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300278
279 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
280 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300281 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300282 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300283 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300284
285 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
286
287 /* POR mode */
Michael Krufky24124f72008-05-03 19:28:00 -0300288 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300289 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300290 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300291
292 /* disable 1.5 MHz low pass filter */
293 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
294 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
Michael Krufky24124f72008-05-03 19:28:00 -0300295 ret = tda18271_write_regs(fe, R_EB21, 3);
296fail:
297 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300298}
299
300static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
301{
302 struct tda18271_priv *priv = fe->tuner_priv;
303 unsigned char *regs = priv->tda18271_regs;
304 u32 N;
305
306 /* set CAL mode to normal */
307 regs[R_EP4] &= ~0x03;
308 tda18271_write_regs(fe, R_EP4, 1);
309
310 /* switch off agc1 */
311 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
312
313 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
314 tda18271_write_regs(fe, R_EB18, 1);
315
316 /* frequency dependent parameters */
317
318 tda18271_calc_bp_filter(fe, &freq);
319 tda18271_calc_gain_taper(fe, &freq);
320 tda18271_calc_rf_band(fe, &freq);
321 tda18271_calc_km(fe, &freq);
322
323 tda18271_write_regs(fe, R_EP1, 3);
324 tda18271_write_regs(fe, R_EB13, 1);
325
326 /* main pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300327 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300328
329 /* cal pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300330 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300331
332 /* force dcdc converter to 0 V */
333 regs[R_EB14] = 0x00;
334 tda18271_write_regs(fe, R_EB14, 1);
335
336 /* disable plls lock */
337 regs[R_EB20] &= ~0x20;
338 tda18271_write_regs(fe, R_EB20, 1);
339
340 /* set CAL mode to RF tracking filter calibration */
341 regs[R_EP4] |= 0x03;
342 tda18271_write_regs(fe, R_EP4, 2);
343
344 /* --------------------------------------------------------------- */
345
346 /* set the internal calibration signal */
347 N = freq;
348
Michael Krufkyae07d042008-04-22 14:46:21 -0300349 tda18271_calc_cal_pll(fe, N);
350 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300351
352 /* downconvert internal calibration */
353 N += 1000000;
354
355 tda18271_calc_main_pll(fe, N);
356 tda18271_write_regs(fe, R_MPD, 4);
357
358 msleep(5);
359
360 tda18271_write_regs(fe, R_EP2, 1);
361 tda18271_write_regs(fe, R_EP1, 1);
362 tda18271_write_regs(fe, R_EP2, 1);
363 tda18271_write_regs(fe, R_EP1, 1);
364
365 /* --------------------------------------------------------------- */
366
367 /* normal operation for the main pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300368 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300369
370 /* normal operation for the cal pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300371 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300372
Michael Krufkyae07d042008-04-22 14:46:21 -0300373 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300374
375 /* launch the rf tracking filters calibration */
376 regs[R_EB20] |= 0x20;
377 tda18271_write_regs(fe, R_EB20, 1);
378
379 msleep(60); /* calibration */
380
381 /* --------------------------------------------------------------- */
382
383 /* set CAL mode to normal */
384 regs[R_EP4] &= ~0x03;
385
386 /* switch on agc1 */
387 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
388
389 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
390 tda18271_write_regs(fe, R_EB18, 1);
391
392 tda18271_write_regs(fe, R_EP3, 2);
393
394 /* synchronization */
395 tda18271_write_regs(fe, R_EP1, 1);
396
397 /* get calibration result */
398 tda18271_read_extended(fe);
399
400 return regs[R_EB14];
401}
402
403static int tda18271_powerscan(struct dvb_frontend *fe,
404 u32 *freq_in, u32 *freq_out)
405{
406 struct tda18271_priv *priv = fe->tuner_priv;
407 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300408 int sgn, bcal, count, wait, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300409 u8 cid_target;
410 u16 count_limit;
411 u32 freq;
412
413 freq = *freq_in;
414
415 tda18271_calc_rf_band(fe, &freq);
416 tda18271_calc_rf_cal(fe, &freq);
417 tda18271_calc_gain_taper(fe, &freq);
418 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
419
420 tda18271_write_regs(fe, R_EP2, 1);
421 tda18271_write_regs(fe, R_EB14, 1);
422
423 /* downconvert frequency */
424 freq += 1000000;
425
426 tda18271_calc_main_pll(fe, freq);
427 tda18271_write_regs(fe, R_MPD, 4);
428
429 msleep(5); /* pll locking */
430
431 /* detection mode */
432 regs[R_EP4] &= ~0x03;
433 regs[R_EP4] |= 0x01;
434 tda18271_write_regs(fe, R_EP4, 1);
435
436 /* launch power detection measurement */
437 tda18271_write_regs(fe, R_EP2, 1);
438
439 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300440 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300441 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300442 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300443
444 /* algorithm initialization */
445 sgn = 1;
446 *freq_out = *freq_in;
447 bcal = 0;
448 count = 0;
449 wait = false;
450
451 while ((regs[R_EB10] & 0x3f) < cid_target) {
452 /* downconvert updated freq to 1 MHz */
453 freq = *freq_in + (sgn * count) + 1000000;
454
455 tda18271_calc_main_pll(fe, freq);
456 tda18271_write_regs(fe, R_MPD, 4);
457
458 if (wait) {
459 msleep(5); /* pll locking */
460 wait = false;
461 } else
462 udelay(100); /* pll locking */
463
464 /* launch power detection measurement */
465 tda18271_write_regs(fe, R_EP2, 1);
466
467 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300468 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300469 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300470 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300471
472 count += 200;
473
Michael Krufkye7809a02008-04-22 14:46:22 -0300474 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300475 continue;
476
477 if (sgn <= 0)
478 break;
479
480 sgn = -1 * sgn;
481 count = 200;
482 wait = true;
483 }
484
485 if ((regs[R_EB10] & 0x3f) >= cid_target) {
486 bcal = 1;
487 *freq_out = freq - 1000000;
488 } else
489 bcal = 0;
490
Michael Krufkycf04d292008-01-09 00:34:30 -0300491 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300492 bcal, *freq_in, *freq_out, freq);
493
494 return bcal;
495}
496
497static int tda18271_powerscan_init(struct dvb_frontend *fe)
498{
499 struct tda18271_priv *priv = fe->tuner_priv;
500 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300501 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300502
503 /* set standard to digital */
504 regs[R_EP3] &= ~0x1f; /* clear std bits */
505 regs[R_EP3] |= 0x12;
506
507 /* set cal mode to normal */
508 regs[R_EP4] &= ~0x03;
509
510 /* update IF output level & IF notch frequency */
511 regs[R_EP4] &= ~0x1c; /* clear if level bits */
512
Michael Krufky24124f72008-05-03 19:28:00 -0300513 ret = tda18271_write_regs(fe, R_EP3, 2);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300514 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300515 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300516
517 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300518 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300519 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300520 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300521
522 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
523
524 /* 1.5 MHz low pass filter */
525 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
526 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
527
Michael Krufky24124f72008-05-03 19:28:00 -0300528 ret = tda18271_write_regs(fe, R_EB21, 3);
529fail:
530 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300531}
532
533static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
534{
535 struct tda18271_priv *priv = fe->tuner_priv;
536 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
537 unsigned char *regs = priv->tda18271_regs;
538 int bcal, rf, i;
539#define RF1 0
540#define RF2 1
541#define RF3 2
542 u32 rf_default[3];
543 u32 rf_freq[3];
544 u8 prog_cal[3];
545 u8 prog_tab[3];
546
547 i = tda18271_lookup_rf_band(fe, &freq, NULL);
548
Michael Krufky4bd5d102008-05-04 21:32:21 -0300549 if (tda_fail(i))
Michael Krufky255b5112008-01-01 22:52:09 -0300550 return i;
551
552 rf_default[RF1] = 1000 * map[i].rf1_def;
553 rf_default[RF2] = 1000 * map[i].rf2_def;
554 rf_default[RF3] = 1000 * map[i].rf3_def;
555
556 for (rf = RF1; rf <= RF3; rf++) {
557 if (0 == rf_default[rf])
558 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300559 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300560
561 /* look for optimized calibration frequency */
562 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300563 if (tda_fail(bcal))
Michael Krufky24124f72008-05-03 19:28:00 -0300564 return bcal;
Michael Krufky255b5112008-01-01 22:52:09 -0300565
566 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
567 prog_tab[rf] = regs[R_EB14];
568
569 if (1 == bcal)
570 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
571 else
572 prog_cal[rf] = prog_tab[rf];
573
574 switch (rf) {
575 case RF1:
576 map[i].rf_a1 = 0;
577 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
578 map[i].rf1 = rf_freq[RF1] / 1000;
579 break;
580 case RF2:
581 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
582 prog_cal[RF1] + prog_tab[RF1]) /
583 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
584 map[i].rf2 = rf_freq[RF2] / 1000;
585 break;
586 case RF3:
587 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
588 prog_cal[RF2] + prog_tab[RF2]) /
589 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
590 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
591 map[i].rf3 = rf_freq[RF3] / 1000;
592 break;
593 default:
594 BUG();
595 }
596 }
597
598 return 0;
599}
600
Michael Krufky09f83c42008-01-05 20:00:09 -0300601static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300602{
603 struct tda18271_priv *priv = fe->tuner_priv;
604 unsigned int i;
Michael Krufky24124f72008-05-03 19:28:00 -0300605 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300606
607 tda_info("tda18271: performing RF tracking filter calibration\n");
608
609 /* wait for die temperature stabilization */
610 msleep(200);
611
Michael Krufky24124f72008-05-03 19:28:00 -0300612 ret = tda18271_powerscan_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300613 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300614 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300615
616 /* rf band calibration */
Michael Krufkyc151c322008-05-04 17:54:23 -0300617 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
618 ret =
Michael Krufky255b5112008-01-01 22:52:09 -0300619 tda18271_rf_tracking_filters_init(fe, 1000 *
620 priv->rf_cal_state[i].rfmax);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300621 if (tda_fail(ret))
Michael Krufkyc151c322008-05-04 17:54:23 -0300622 goto fail;
623 }
Michael Krufky255b5112008-01-01 22:52:09 -0300624
Michael Krufky09f83c42008-01-05 20:00:09 -0300625 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky24124f72008-05-03 19:28:00 -0300626fail:
627 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300628}
629
630/* ------------------------------------------------------------------ */
631
Michael Krufky12afe372008-04-22 14:42:07 -0300632static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300633{
634 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300635 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300636 int ret;
Michael Krufky839c6c92008-01-13 18:29:44 -0300637
638 /* test RF_CAL_OK to see if we need init */
639 if ((regs[R_EP1] & 0x10) == 0)
640 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300641
642 if (priv->cal_initialized)
643 return 0;
644
Michael Krufky24124f72008-05-03 19:28:00 -0300645 ret = tda18271_calc_rf_filter_curve(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300646 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300647 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300648
Michael Krufky24124f72008-05-03 19:28:00 -0300649 ret = tda18271_por(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300650 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300651 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300652
Michael Krufky6bfa6652008-01-07 00:51:48 -0300653 tda_info("tda18271: RF tracking filter calibration complete\n");
654
Michael Krufky255b5112008-01-01 22:52:09 -0300655 priv->cal_initialized = true;
Michael Krufkyc151c322008-05-04 17:54:23 -0300656 goto end;
Michael Krufky24124f72008-05-03 19:28:00 -0300657fail:
Michael Krufkyc151c322008-05-04 17:54:23 -0300658 tda_info("tda18271: RF tracking filter calibration failed!\n");
659end:
Michael Krufky24124f72008-05-03 19:28:00 -0300660 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300661}
662
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300663static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
664 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300665{
666 struct tda18271_priv *priv = fe->tuner_priv;
667 unsigned char *regs = priv->tda18271_regs;
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300668 int ret;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300669 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300670
Michael Krufky255b5112008-01-01 22:52:09 -0300671 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300672 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300673 tda18271_write_regs(fe, R_EP1, 1);
674
675 regs[R_EB4] &= 0x07;
676 regs[R_EB4] |= 0x60;
677 tda18271_write_regs(fe, R_EB4, 1);
678
679 regs[R_EB7] = 0x60;
680 tda18271_write_regs(fe, R_EB7, 1);
681
682 regs[R_EB14] = 0x00;
683 tda18271_write_regs(fe, R_EB14, 1);
684
685 regs[R_EB20] = 0xcc;
686 tda18271_write_regs(fe, R_EB20, 1);
687
Michael Krufky255b5112008-01-01 22:52:09 -0300688 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300689 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300690
Michael Krufky255b5112008-01-01 22:52:09 -0300691 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300692
693 switch (priv->mode) {
694 case TDA18271_ANALOG:
695 N = freq - 1250000;
696 break;
697 case TDA18271_DIGITAL:
698 N = freq + bw / 2;
699 break;
700 }
701
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300702 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300703
Michael Krufky255b5112008-01-01 22:52:09 -0300704 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300705
706 switch (priv->mode) {
707 case TDA18271_ANALOG:
708 N = freq - 250000;
709 break;
710 case TDA18271_DIGITAL:
711 N = freq + bw / 2 + 1000000;
712 break;
713 }
714
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300715 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300716
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300717 ret = tda18271_write_regs(fe, R_EP3, 11);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300718 if (tda_fail(ret))
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300719 return ret;
720
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300721 msleep(5); /* RF tracking filter calibration initialization */
722
Michael Krufky255b5112008-01-01 22:52:09 -0300723 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300724 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300725 tda18271_write_regs(fe, R_EB13, 1);
726
Michael Krufky255b5112008-01-01 22:52:09 -0300727 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300728 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300729
Michael Krufky255b5112008-01-01 22:52:09 -0300730 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300731 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300732
733 tda18271_write_regs(fe, R_EP2, 1);
734 tda18271_write_regs(fe, R_EP1, 1);
735 tda18271_write_regs(fe, R_EP2, 1);
736 tda18271_write_regs(fe, R_EP1, 1);
737
738 regs[R_EB4] &= 0x07;
739 regs[R_EB4] |= 0x40;
740 tda18271_write_regs(fe, R_EB4, 1);
741
742 regs[R_EB7] = 0x40;
743 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300744 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300745
746 regs[R_EB20] = 0xec;
747 tda18271_write_regs(fe, R_EB20, 1);
748 msleep(60); /* RF tracking filter calibration completion */
749
750 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
751 tda18271_write_regs(fe, R_EP4, 1);
752
753 tda18271_write_regs(fe, R_EP1, 1);
754
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300755 /* RF tracking filter correction for VHF_Low band */
756 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300757 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300758
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300759 return 0;
760}
761
Michael Krufkyd1c53422008-04-22 14:42:07 -0300762/* ------------------------------------------------------------------ */
763
Michael Krufky12afe372008-04-22 14:42:07 -0300764static int tda18271_ir_cal_init(struct dvb_frontend *fe)
765{
766 struct tda18271_priv *priv = fe->tuner_priv;
767 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300768 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300769
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300770 ret = tda18271_read_regs(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300771 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300772 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300773
774 /* test IR_CAL_OK to see if we need init */
775 if ((regs[R_EP1] & 0x08) == 0)
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300776 ret = tda18271_init_regs(fe);
777fail:
778 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300779}
780
781static int tda18271_init(struct dvb_frontend *fe)
782{
783 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300784 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300785
786 mutex_lock(&priv->lock);
787
788 /* power up */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300789 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300790 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300791 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300792
793 /* initialization */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300794 ret = tda18271_ir_cal_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300795 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300796 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300797
798 if (priv->id == TDA18271HDC2)
799 tda18271c2_rf_cal_init(fe);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300800fail:
Michael Krufky12afe372008-04-22 14:42:07 -0300801 mutex_unlock(&priv->lock);
802
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300803 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300804}
805
Michael Krufkyd1c53422008-04-22 14:42:07 -0300806static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300807 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300808{
809 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300810 int ret;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300811
Michael Krufky7f7203d2008-04-22 14:46:06 -0300812 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
813 freq, map->if_freq, bw, map->agc_mode, map->std);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300814
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300815 ret = tda18271_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300816 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300817 goto fail;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300818
819 mutex_lock(&priv->lock);
820
Michael Krufkyd1c53422008-04-22 14:42:07 -0300821 switch (priv->id) {
822 case TDA18271HDC1:
823 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
824 break;
825 case TDA18271HDC2:
826 tda18271c2_rf_tracking_filters_correction(fe, freq);
827 break;
828 }
Michael Krufky31940e32008-05-04 19:37:27 -0300829 ret = tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300830
Michael Krufky8d316bf2008-01-06 15:31:35 -0300831 mutex_unlock(&priv->lock);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300832fail:
833 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300834}
835
836/* ------------------------------------------------------------------ */
837
838static int tda18271_set_params(struct dvb_frontend *fe,
839 struct dvb_frontend_parameters *params)
840{
841 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300842 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300843 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300844 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300845 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300846
847 priv->mode = TDA18271_DIGITAL;
848
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300849 if (fe->ops.info.type == FE_ATSC) {
850 switch (params->u.vsb.modulation) {
851 case VSB_8:
852 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300853 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300854 break;
855 case QAM_64:
856 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300857 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300858 break;
859 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300860 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300861 return -EINVAL;
862 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300863#if 0
864 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300865 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300866#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300867 bw = 6000000;
868 } else if (fe->ops.info.type == FE_OFDM) {
869 switch (params->u.ofdm.bandwidth) {
870 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300871 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300872 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300873 break;
874 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300875 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300876 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300877 break;
878 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300879 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300880 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300881 break;
882 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300883 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300884 return -EINVAL;
885 }
886 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300887 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300888 return -EINVAL;
889 }
890
Michael Krufkyed736832008-01-19 17:41:04 -0300891 /* When tuning digital, the analog demod must be tri-stated */
892 if (fe->ops.analog_ops.standby)
893 fe->ops.analog_ops.standby(fe);
894
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300895 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300896
Michael Krufky4bd5d102008-05-04 21:32:21 -0300897 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300898 goto fail;
899
900 priv->frequency = freq;
901 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
902 params->u.ofdm.bandwidth : 0;
903fail:
904 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300905}
906
907static int tda18271_set_analog_params(struct dvb_frontend *fe,
908 struct analog_parameters *params)
909{
910 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300911 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300912 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300913 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300914 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300915 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300916
917 priv->mode = TDA18271_ANALOG;
918
Michael Krufkyc353f422008-01-08 10:38:10 -0300919 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc353f422008-01-08 10:38:10 -0300920 freq = freq / 1000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300921 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -0300922 mode = "fm";
923 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300924 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300925 mode = "MN";
926 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300927 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300928 mode = "B";
929 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300930 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300931 mode = "GH";
932 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300933 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300934 mode = "I";
935 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300936 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300937 mode = "DK";
938 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300939 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300940 mode = "L";
941 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300942 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -0300943 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300944 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300945 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300946 mode = "xx";
947 }
948
Michael Krufky182519f2007-12-25 15:10:11 -0300949 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300950
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300951 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300952
Michael Krufky4bd5d102008-05-04 21:32:21 -0300953 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300954 goto fail;
955
956 priv->frequency = freq;
957 priv->bandwidth = 0;
958fail:
959 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300960}
961
Michael Krufky518d8732008-01-13 17:01:01 -0300962static int tda18271_sleep(struct dvb_frontend *fe)
963{
964 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300965 int ret;
Michael Krufky518d8732008-01-13 17:01:01 -0300966
967 mutex_lock(&priv->lock);
968
969 /* standby mode w/ slave tuner output
970 * & loop thru & xtal oscillator on */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300971 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky518d8732008-01-13 17:01:01 -0300972
973 mutex_unlock(&priv->lock);
974
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300975 return ret;
Michael Krufky518d8732008-01-13 17:01:01 -0300976}
977
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300978static int tda18271_release(struct dvb_frontend *fe)
979{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300980 struct tda18271_priv *priv = fe->tuner_priv;
981
982 mutex_lock(&tda18271_list_mutex);
983
Michael Krufkyf9e315a2008-04-22 14:41:54 -0300984 if (priv)
985 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -0300986
Michael Krufkya4f263b2008-01-06 15:52:56 -0300987 mutex_unlock(&tda18271_list_mutex);
988
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300989 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -0300990
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300991 return 0;
992}
993
994static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
995{
996 struct tda18271_priv *priv = fe->tuner_priv;
997 *frequency = priv->frequency;
998 return 0;
999}
1000
1001static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1002{
1003 struct tda18271_priv *priv = fe->tuner_priv;
1004 *bandwidth = priv->bandwidth;
1005 return 0;
1006}
1007
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001008/* ------------------------------------------------------------------ */
1009
1010#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001011 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -03001012 map->std_cfg.agc_mode + map->std_cfg.std + \
1013 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001014 tda_dbg("Using custom std config for %s\n", name); \
1015 memcpy(&std->std_cfg, &map->std_cfg, \
1016 sizeof(struct tda18271_std_map_item)); \
1017 } } while (0)
1018
1019#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -03001020 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1021 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001022 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -03001023 std->std_cfg.agc_mode, std->std_cfg.std, \
1024 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001025 } while (0)
1026
1027static int tda18271_dump_std_map(struct dvb_frontend *fe)
1028{
1029 struct tda18271_priv *priv = fe->tuner_priv;
1030 struct tda18271_std_map *std = &priv->std;
1031
1032 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -03001033 tda18271_dump_std_item(fm_radio, " fm ");
1034 tda18271_dump_std_item(atv_b, "atv b ");
1035 tda18271_dump_std_item(atv_dk, "atv dk");
1036 tda18271_dump_std_item(atv_gh, "atv gh");
1037 tda18271_dump_std_item(atv_i, "atv i ");
1038 tda18271_dump_std_item(atv_l, "atv l ");
1039 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001040 tda18271_dump_std_item(atv_mn, "atv mn");
1041 tda18271_dump_std_item(atsc_6, "atsc 6");
1042 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1043 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1044 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -03001045 tda18271_dump_std_item(qam_6, "qam 6 ");
1046 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001047
1048 return 0;
1049}
1050
1051static int tda18271_update_std_map(struct dvb_frontend *fe,
1052 struct tda18271_std_map *map)
1053{
1054 struct tda18271_priv *priv = fe->tuner_priv;
1055 struct tda18271_std_map *std = &priv->std;
1056
1057 if (!map)
1058 return -EINVAL;
1059
Michael Krufkyc353f422008-01-08 10:38:10 -03001060 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001061 tda18271_update_std(atv_b, "atv b");
1062 tda18271_update_std(atv_dk, "atv dk");
1063 tda18271_update_std(atv_gh, "atv gh");
1064 tda18271_update_std(atv_i, "atv i");
1065 tda18271_update_std(atv_l, "atv l");
1066 tda18271_update_std(atv_lc, "atv l'");
1067 tda18271_update_std(atv_mn, "atv mn");
1068 tda18271_update_std(atsc_6, "atsc 6");
1069 tda18271_update_std(dvbt_6, "dvbt 6");
1070 tda18271_update_std(dvbt_7, "dvbt 7");
1071 tda18271_update_std(dvbt_8, "dvbt 8");
1072 tda18271_update_std(qam_6, "qam 6");
1073 tda18271_update_std(qam_8, "qam 8");
1074
1075 return 0;
1076}
1077
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001078static int tda18271_get_id(struct dvb_frontend *fe)
1079{
1080 struct tda18271_priv *priv = fe->tuner_priv;
1081 unsigned char *regs = priv->tda18271_regs;
1082 char *name;
1083 int ret = 0;
1084
Michael Krufky8d316bf2008-01-06 15:31:35 -03001085 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001086 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001087 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001088
1089 switch (regs[R_ID] & 0x7f) {
1090 case 3:
1091 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001092 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001093 break;
1094 case 4:
1095 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001096 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001097 break;
1098 default:
1099 name = "Unknown device";
1100 ret = -EINVAL;
1101 break;
1102 }
1103
Michael Krufky182519f2007-12-25 15:10:11 -03001104 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001105 i2c_adapter_id(priv->i2c_props.adap),
1106 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001107 (0 == ret) ? "" : ", device not supported.");
1108
1109 return ret;
1110}
1111
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001112static struct dvb_tuner_ops tda18271_tuner_ops = {
1113 .info = {
1114 .name = "NXP TDA18271HD",
1115 .frequency_min = 45000000,
1116 .frequency_max = 864000000,
1117 .frequency_step = 62500
1118 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001119 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001120 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001121 .set_params = tda18271_set_params,
1122 .set_analog_params = tda18271_set_analog_params,
1123 .release = tda18271_release,
1124 .get_frequency = tda18271_get_frequency,
1125 .get_bandwidth = tda18271_get_bandwidth,
1126};
1127
1128struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001129 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001130 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001131{
1132 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001133 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001134
Michael Krufkya4f263b2008-01-06 15:52:56 -03001135 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001136
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001137 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1138 hybrid_tuner_instance_list,
1139 i2c, addr, "tda18271");
1140 switch (instance) {
1141 case 0:
1142 goto fail;
1143 break;
1144 case 1:
1145 /* new tuner instance */
Michael Krufkya4f263b2008-01-06 15:52:56 -03001146 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
Michael Krufky868f5cc2008-04-22 14:46:23 -03001147 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001148 priv->cal_initialized = false;
1149 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001150
Michael Krufkya4f263b2008-01-06 15:52:56 -03001151 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001152
Michael Krufky55553092008-04-22 14:46:06 -03001153 if (cfg)
1154 priv->small_i2c = cfg->small_i2c;
1155
Michael Krufky4bd5d102008-05-04 21:32:21 -03001156 if (tda_fail(tda18271_get_id(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001157 goto fail;
1158
Michael Krufky4bd5d102008-05-04 21:32:21 -03001159 if (tda_fail(tda18271_assign_map_layout(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001160 goto fail;
1161
1162 mutex_lock(&priv->lock);
1163 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001164
1165 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001166 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001167
Michael Krufkya4f263b2008-01-06 15:52:56 -03001168 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001169 break;
1170 default:
1171 /* existing tuner instance */
1172 fe->tuner_priv = priv;
1173
1174 /* allow dvb driver to override i2c gate setting */
1175 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1176 priv->gate = cfg->gate;
1177 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001178 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001179
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001180 /* override default std map with values in config struct */
1181 if ((cfg) && (cfg->std_map))
1182 tda18271_update_std_map(fe, cfg->std_map);
1183
Michael Krufkya4f263b2008-01-06 15:52:56 -03001184 mutex_unlock(&tda18271_list_mutex);
1185
1186 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1187 sizeof(struct dvb_tuner_ops));
1188
Michael Krufkyc7353722008-03-30 19:40:20 -03001189 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001190 tda18271_dump_std_map(fe);
1191
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001192 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001193fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001194 mutex_unlock(&tda18271_list_mutex);
1195
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001196 tda18271_release(fe);
1197 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001198}
1199EXPORT_SYMBOL_GPL(tda18271_attach);
1200MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1201MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1202MODULE_LICENSE("GPL");
Michael Krufky5ec96b02008-04-22 14:46:23 -03001203MODULE_VERSION("0.3");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001204
1205/*
1206 * Overrides for Emacs so that we follow Linus's tabbing style.
1207 * ---------------------------------------------------------------------------
1208 * Local variables:
1209 * c-basic-offset: 8
1210 * End:
1211 */