V4L/DVB (6006): tuner: move last_div to tuner-simple private data
[wrt350n-kernel.git] / drivers / media / video / tuner-simple.c
blob572f22423b98e0cef0b6c0c40da7fb2bc38c8ea0
1 /*
3 * i2c tv tuner chip device driver
4 * controls all those simple 4-control-bytes style tuners.
5 */
6 #include <linux/delay.h>
7 #include <linux/i2c.h>
8 #include <linux/videodev.h>
9 #include <media/tuner.h>
10 #include <media/v4l2-common.h>
11 #include <media/tuner-types.h>
12 #include "tuner-driver.h"
14 static int offset = 0;
15 module_param(offset, int, 0664);
16 MODULE_PARM_DESC(offset,"Allows to specify an offset for tuner");
18 /* ---------------------------------------------------------------------- */
20 /* tv standard selection for Temic 4046 FM5
21 this value takes the low bits of control byte 2
22 from datasheet Rev.01, Feb.00
23 standard BG I L L2 D
24 picture IF 38.9 38.9 38.9 33.95 38.9
25 sound 1 33.4 32.9 32.4 40.45 32.4
26 sound 2 33.16
27 NICAM 33.05 32.348 33.05 33.05
29 #define TEMIC_SET_PAL_I 0x05
30 #define TEMIC_SET_PAL_DK 0x09
31 #define TEMIC_SET_PAL_L 0x0a // SECAM ?
32 #define TEMIC_SET_PAL_L2 0x0b // change IF !
33 #define TEMIC_SET_PAL_BG 0x0c
35 /* tv tuner system standard selection for Philips FQ1216ME
36 this value takes the low bits of control byte 2
37 from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
38 standard BG DK I L L`
39 picture carrier 38.90 38.90 38.90 38.90 33.95
40 colour 34.47 34.47 34.47 34.47 38.38
41 sound 1 33.40 32.40 32.90 32.40 40.45
42 sound 2 33.16 - - - -
43 NICAM 33.05 33.05 32.35 33.05 39.80
45 #define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
46 #define PHILIPS_SET_PAL_BGDK 0x09
47 #define PHILIPS_SET_PAL_L2 0x0a
48 #define PHILIPS_SET_PAL_L 0x0b
50 /* system switching for Philips FI1216MF MK2
51 from datasheet "1996 Jul 09",
52 standard BG L L'
53 picture carrier 38.90 38.90 33.95
54 colour 34.47 34.37 38.38
55 sound 1 33.40 32.40 40.45
56 sound 2 33.16 - -
57 NICAM 33.05 33.05 39.80
59 #define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
60 #define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
61 #define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
63 /* Control byte */
65 #define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
66 #define TUNER_RATIO_SELECT_50 0x00
67 #define TUNER_RATIO_SELECT_32 0x02
68 #define TUNER_RATIO_SELECT_166 0x04
69 #define TUNER_RATIO_SELECT_62 0x06
71 #define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
73 /* Status byte */
75 #define TUNER_POR 0x80
76 #define TUNER_FL 0x40
77 #define TUNER_MODE 0x38
78 #define TUNER_AFC 0x07
79 #define TUNER_SIGNAL 0x07
80 #define TUNER_STEREO 0x10
82 #define TUNER_PLL_LOCKED 0x40
83 #define TUNER_STEREO_MK3 0x04
85 struct tuner_simple_priv {
86 u16 last_div;
89 /* ---------------------------------------------------------------------- */
91 static int tuner_getstatus(struct i2c_client *c)
93 unsigned char byte;
95 if (1 != i2c_master_recv(c,&byte,1))
96 return 0;
98 return byte;
101 static int tuner_signal(struct i2c_client *c)
103 return (tuner_getstatus(c) & TUNER_SIGNAL) << 13;
106 static int tuner_stereo(struct i2c_client *c)
108 int stereo, status;
109 struct tuner *t = i2c_get_clientdata(c);
111 status = tuner_getstatus (c);
113 switch (t->type) {
114 case TUNER_PHILIPS_FM1216ME_MK3:
115 case TUNER_PHILIPS_FM1236_MK3:
116 case TUNER_PHILIPS_FM1256_IH3:
117 case TUNER_LG_NTSC_TAPE:
118 stereo = ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
119 break;
120 default:
121 stereo = status & TUNER_STEREO;
124 return stereo;
128 /* ---------------------------------------------------------------------- */
130 static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
132 struct tuner *t = i2c_get_clientdata(c);
133 struct tuner_simple_priv *priv = t->priv;
134 u8 config, cb, tuneraddr;
135 u16 div;
136 struct tunertype *tun;
137 u8 buffer[4];
138 int rc, IFPCoff, i, j;
139 enum param_type desired_type;
140 struct tuner_params *params;
142 tun = &tuners[t->type];
144 /* IFPCoff = Video Intermediate Frequency - Vif:
145 940 =16*58.75 NTSC/J (Japan)
146 732 =16*45.75 M/N STD
147 704 =16*44 ATSC (at DVB code)
148 632 =16*39.50 I U.K.
149 622.4=16*38.90 B/G D/K I, L STD
150 592 =16*37.00 D China
151 590 =16.36.875 B Australia
152 543.2=16*33.95 L' STD
153 171.2=16*10.70 FM Radio (at set_radio_freq)
156 if (t->std == V4L2_STD_NTSC_M_JP) {
157 IFPCoff = 940;
158 desired_type = TUNER_PARAM_TYPE_NTSC;
159 } else if ((t->std & V4L2_STD_MN) &&
160 !(t->std & ~V4L2_STD_MN)) {
161 IFPCoff = 732;
162 desired_type = TUNER_PARAM_TYPE_NTSC;
163 } else if (t->std == V4L2_STD_SECAM_LC) {
164 IFPCoff = 543;
165 desired_type = TUNER_PARAM_TYPE_SECAM;
166 } else {
167 IFPCoff = 623;
168 desired_type = TUNER_PARAM_TYPE_PAL;
171 for (j = 0; j < tun->count-1; j++) {
172 if (desired_type != tun->params[j].type)
173 continue;
174 break;
176 /* use default tuner_params if desired_type not available */
177 if (desired_type != tun->params[j].type) {
178 tuner_dbg("IFPCoff = %d: tuner_params undefined for tuner %d\n",
179 IFPCoff,t->type);
180 j = 0;
182 params = &tun->params[j];
184 for (i = 0; i < params->count; i++) {
185 if (freq > params->ranges[i].limit)
186 continue;
187 break;
189 if (i == params->count) {
190 tuner_dbg("TV frequency out of range (%d > %d)",
191 freq, params->ranges[i - 1].limit);
192 freq = params->ranges[--i].limit;
194 config = params->ranges[i].config;
195 cb = params->ranges[i].cb;
196 /* i == 0 -> VHF_LO
197 * i == 1 -> VHF_HI
198 * i == 2 -> UHF */
199 tuner_dbg("tv: param %d, range %d\n",j,i);
201 div=freq + IFPCoff + offset;
203 tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
204 freq / 16, freq % 16 * 100 / 16,
205 IFPCoff / 16, IFPCoff % 16 * 100 / 16,
206 offset / 16, offset % 16 * 100 / 16,
207 div);
209 /* tv norm specific stuff for multi-norm tuners */
210 switch (t->type) {
211 case TUNER_PHILIPS_SECAM: // FI1216MF
212 /* 0x01 -> ??? no change ??? */
213 /* 0x02 -> PAL BDGHI / SECAM L */
214 /* 0x04 -> ??? PAL others / SECAM others ??? */
215 cb &= ~0x03;
216 if (t->std & V4L2_STD_SECAM_L) //also valid for V4L2_STD_SECAM
217 cb |= PHILIPS_MF_SET_STD_L;
218 else if (t->std & V4L2_STD_SECAM_LC)
219 cb |= PHILIPS_MF_SET_STD_LC;
220 else /* V4L2_STD_B|V4L2_STD_GH */
221 cb |= PHILIPS_MF_SET_STD_BG;
222 break;
224 case TUNER_TEMIC_4046FM5:
225 cb &= ~0x0f;
227 if (t->std & V4L2_STD_PAL_BG) {
228 cb |= TEMIC_SET_PAL_BG;
230 } else if (t->std & V4L2_STD_PAL_I) {
231 cb |= TEMIC_SET_PAL_I;
233 } else if (t->std & V4L2_STD_PAL_DK) {
234 cb |= TEMIC_SET_PAL_DK;
236 } else if (t->std & V4L2_STD_SECAM_L) {
237 cb |= TEMIC_SET_PAL_L;
240 break;
242 case TUNER_PHILIPS_FQ1216ME:
243 cb &= ~0x0f;
245 if (t->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
246 cb |= PHILIPS_SET_PAL_BGDK;
248 } else if (t->std & V4L2_STD_PAL_I) {
249 cb |= PHILIPS_SET_PAL_I;
251 } else if (t->std & V4L2_STD_SECAM_L) {
252 cb |= PHILIPS_SET_PAL_L;
255 break;
257 case TUNER_PHILIPS_ATSC:
258 /* 0x00 -> ATSC antenna input 1 */
259 /* 0x01 -> ATSC antenna input 2 */
260 /* 0x02 -> NTSC antenna input 1 */
261 /* 0x03 -> NTSC antenna input 2 */
262 cb &= ~0x03;
263 if (!(t->std & V4L2_STD_ATSC))
264 cb |= 2;
265 /* FIXME: input */
266 break;
268 case TUNER_MICROTUNE_4042FI5:
269 /* Set the charge pump for fast tuning */
270 config |= TUNER_CHARGE_PUMP;
271 break;
273 case TUNER_PHILIPS_TUV1236D:
274 /* 0x40 -> ATSC antenna input 1 */
275 /* 0x48 -> ATSC antenna input 2 */
276 /* 0x00 -> NTSC antenna input 1 */
277 /* 0x08 -> NTSC antenna input 2 */
278 buffer[0] = 0x14;
279 buffer[1] = 0x00;
280 buffer[2] = 0x17;
281 buffer[3] = 0x00;
282 cb &= ~0x40;
283 if (t->std & V4L2_STD_ATSC) {
284 cb |= 0x40;
285 buffer[1] = 0x04;
287 /* set to the correct mode (analog or digital) */
288 tuneraddr = c->addr;
289 c->addr = 0x0a;
290 if (2 != (rc = i2c_master_send(c,&buffer[0],2)))
291 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
292 if (2 != (rc = i2c_master_send(c,&buffer[2],2)))
293 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
294 c->addr = tuneraddr;
295 /* FIXME: input */
296 break;
299 if (params->cb_first_if_lower_freq && div < priv->last_div) {
300 buffer[0] = config;
301 buffer[1] = cb;
302 buffer[2] = (div>>8) & 0x7f;
303 buffer[3] = div & 0xff;
304 } else {
305 buffer[0] = (div>>8) & 0x7f;
306 buffer[1] = div & 0xff;
307 buffer[2] = config;
308 buffer[3] = cb;
310 priv->last_div = div;
311 if (params->has_tda9887) {
312 int config = 0;
313 int is_secam_l = (t->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) &&
314 !(t->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC));
316 if (t->std == V4L2_STD_SECAM_LC) {
317 if (params->port1_active ^ params->port1_invert_for_secam_lc)
318 config |= TDA9887_PORT1_ACTIVE;
319 if (params->port2_active ^ params->port2_invert_for_secam_lc)
320 config |= TDA9887_PORT2_ACTIVE;
322 else {
323 if (params->port1_active)
324 config |= TDA9887_PORT1_ACTIVE;
325 if (params->port2_active)
326 config |= TDA9887_PORT2_ACTIVE;
328 if (params->intercarrier_mode)
329 config |= TDA9887_INTERCARRIER;
330 if (is_secam_l) {
331 if (i == 0 && params->default_top_secam_low)
332 config |= TDA9887_TOP(params->default_top_secam_low);
333 else if (i == 1 && params->default_top_secam_mid)
334 config |= TDA9887_TOP(params->default_top_secam_mid);
335 else if (params->default_top_secam_high)
336 config |= TDA9887_TOP(params->default_top_secam_high);
338 else {
339 if (i == 0 && params->default_top_low)
340 config |= TDA9887_TOP(params->default_top_low);
341 else if (i == 1 && params->default_top_mid)
342 config |= TDA9887_TOP(params->default_top_mid);
343 else if (params->default_top_high)
344 config |= TDA9887_TOP(params->default_top_high);
346 if (params->default_pll_gating_18)
347 config |= TDA9887_GATING_18;
348 i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
350 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
351 buffer[0],buffer[1],buffer[2],buffer[3]);
353 if (4 != (rc = i2c_master_send(c,buffer,4)))
354 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
356 switch (t->type) {
357 case TUNER_LG_TDVS_H06XF:
358 /* Set the Auxiliary Byte. */
359 buffer[0] = buffer[2];
360 buffer[0] &= ~0x20;
361 buffer[0] |= 0x18;
362 buffer[1] = 0x20;
363 tuner_dbg("tv 0x%02x 0x%02x\n",buffer[0],buffer[1]);
365 if (2 != (rc = i2c_master_send(c,buffer,2)))
366 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
367 break;
368 case TUNER_MICROTUNE_4042FI5:
370 // FIXME - this may also work for other tuners
371 unsigned long timeout = jiffies + msecs_to_jiffies(1);
372 u8 status_byte = 0;
374 /* Wait until the PLL locks */
375 for (;;) {
376 if (time_after(jiffies,timeout))
377 return;
378 if (1 != (rc = i2c_master_recv(c,&status_byte,1))) {
379 tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",rc);
380 break;
382 if (status_byte & TUNER_PLL_LOCKED)
383 break;
384 udelay(10);
387 /* Set the charge pump for optimized phase noise figure */
388 config &= ~TUNER_CHARGE_PUMP;
389 buffer[0] = (div>>8) & 0x7f;
390 buffer[1] = div & 0xff;
391 buffer[2] = config;
392 buffer[3] = cb;
393 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
394 buffer[0],buffer[1],buffer[2],buffer[3]);
396 if (4 != (rc = i2c_master_send(c,buffer,4)))
397 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
398 break;
403 static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
405 struct tunertype *tun;
406 struct tuner *t = i2c_get_clientdata(c);
407 struct tuner_simple_priv *priv = t->priv;
408 u8 buffer[4];
409 u16 div;
410 int rc, j;
411 struct tuner_params *params;
413 tun = &tuners[t->type];
415 for (j = tun->count-1; j > 0; j--)
416 if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
417 break;
418 /* default params (j=0) will be used if desired type wasn't found */
419 params = &tun->params[j];
421 /* Select Radio 1st IF used */
422 switch (params->radio_if) {
423 case 0: /* 10.7 MHz */
424 freq += (unsigned int)(10.7*16000);
425 break;
426 case 1: /* 33.3 MHz */
427 freq += (unsigned int)(33.3*16000);
428 break;
429 case 2: /* 41.3 MHz */
430 freq += (unsigned int)(41.3*16000);
431 break;
432 default:
433 tuner_warn("Unsupported radio_if value %d\n", params->radio_if);
434 return;
437 /* Bandswitch byte */
438 switch (t->type) {
439 case TUNER_TENA_9533_DI:
440 case TUNER_YMEC_TVF_5533MF:
441 tuner_dbg ("This tuner doesn't have FM. Most cards have a TEA5767 for FM\n");
442 return;
443 case TUNER_PHILIPS_FM1216ME_MK3:
444 case TUNER_PHILIPS_FM1236_MK3:
445 case TUNER_PHILIPS_FMD1216ME_MK3:
446 case TUNER_LG_NTSC_TAPE:
447 case TUNER_PHILIPS_FM1256_IH3:
448 buffer[3] = 0x19;
449 break;
450 case TUNER_TNF_5335MF:
451 buffer[3] = 0x11;
452 break;
453 case TUNER_LG_PAL_FM:
454 buffer[3] = 0xa5;
455 break;
456 case TUNER_THOMSON_DTT761X:
457 buffer[3] = 0x39;
458 break;
459 case TUNER_MICROTUNE_4049FM5:
460 default:
461 buffer[3] = 0xa4;
462 break;
465 buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) |
466 TUNER_RATIO_SELECT_50; /* 50 kHz step */
468 /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
469 freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
470 freq * (1/800) */
471 div = (freq + 400) / 800;
473 if (params->cb_first_if_lower_freq && div < priv->last_div) {
474 buffer[0] = buffer[2];
475 buffer[1] = buffer[3];
476 buffer[2] = (div>>8) & 0x7f;
477 buffer[3] = div & 0xff;
478 } else {
479 buffer[0] = (div>>8) & 0x7f;
480 buffer[1] = div & 0xff;
483 tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
484 buffer[0],buffer[1],buffer[2],buffer[3]);
485 priv->last_div = div;
487 if (params->has_tda9887) {
488 int config = 0;
489 if (params->port1_active && !params->port1_fm_high_sensitivity)
490 config |= TDA9887_PORT1_ACTIVE;
491 if (params->port2_active && !params->port2_fm_high_sensitivity)
492 config |= TDA9887_PORT2_ACTIVE;
493 if (params->intercarrier_mode)
494 config |= TDA9887_INTERCARRIER;
495 /* if (params->port1_set_for_fm_mono)
496 config &= ~TDA9887_PORT1_ACTIVE;*/
497 if (params->fm_gain_normal)
498 config |= TDA9887_GAIN_NORMAL;
499 if (params->radio_if == 2)
500 config |= TDA9887_RIF_41_3;
501 i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
503 if (4 != (rc = i2c_master_send(c,buffer,4)))
504 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
507 static void tuner_release(struct i2c_client *c)
509 struct tuner *t = i2c_get_clientdata(c);
511 kfree(t->priv);
512 t->priv = NULL;
515 static struct tuner_operations simple_tuner_ops = {
516 .set_tv_freq = default_set_tv_freq,
517 .set_radio_freq = default_set_radio_freq,
518 .has_signal = tuner_signal,
519 .is_stereo = tuner_stereo,
520 .release = tuner_release,
523 int default_tuner_init(struct i2c_client *c)
525 struct tuner *t = i2c_get_clientdata(c);
526 struct tuner_simple_priv *priv = NULL;
528 priv = kzalloc(sizeof(struct tuner_simple_priv), GFP_KERNEL);
529 if (priv == NULL)
530 return -ENOMEM;
531 t->priv = priv;
533 tuner_info("type set to %d (%s)\n",
534 t->type, tuners[t->type].name);
535 strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
537 memcpy(&t->ops, &simple_tuner_ops, sizeof(struct tuner_operations));
539 return 0;
543 * Overrides for Emacs so that we follow Linus's tabbing style.
544 * ---------------------------------------------------------------------------
545 * Local variables:
546 * c-basic-offset: 8
547 * End: