3 * i2c tv tuner chip device driver
4 * controls all those simple 4-control-bytes style tuners.
6 #include <linux/delay.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
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
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")
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
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",
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
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' */
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 */
75 #define TUNER_POR 0x80
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
{
89 /* ---------------------------------------------------------------------- */
91 static int tuner_getstatus(struct i2c_client
*c
)
95 if (1 != i2c_master_recv(c
,&byte
,1))
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
)
109 struct tuner
*t
= i2c_get_clientdata(c
);
111 status
= tuner_getstatus (c
);
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
);
121 stereo
= status
& TUNER_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
;
136 struct tunertype
*tun
;
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)
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
) {
158 desired_type
= TUNER_PARAM_TYPE_NTSC
;
159 } else if ((t
->std
& V4L2_STD_MN
) &&
160 !(t
->std
& ~V4L2_STD_MN
)) {
162 desired_type
= TUNER_PARAM_TYPE_NTSC
;
163 } else if (t
->std
== V4L2_STD_SECAM_LC
) {
165 desired_type
= TUNER_PARAM_TYPE_SECAM
;
168 desired_type
= TUNER_PARAM_TYPE_PAL
;
171 for (j
= 0; j
< tun
->count
-1; j
++) {
172 if (desired_type
!= tun
->params
[j
].type
)
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",
182 params
= &tun
->params
[j
];
184 for (i
= 0; i
< params
->count
; i
++) {
185 if (freq
> params
->ranges
[i
].limit
)
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
;
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,
209 /* tv norm specific stuff for multi-norm tuners */
211 case TUNER_PHILIPS_SECAM
: // FI1216MF
212 /* 0x01 -> ??? no change ??? */
213 /* 0x02 -> PAL BDGHI / SECAM L */
214 /* 0x04 -> ??? PAL others / SECAM others ??? */
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
;
224 case TUNER_TEMIC_4046FM5
:
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
;
242 case TUNER_PHILIPS_FQ1216ME
:
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
;
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 */
263 if (!(t
->std
& V4L2_STD_ATSC
))
268 case TUNER_MICROTUNE_4042FI5
:
269 /* Set the charge pump for fast tuning */
270 config
|= TUNER_CHARGE_PUMP
;
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 */
283 if (t
->std
& V4L2_STD_ATSC
) {
287 /* set to the correct mode (analog or digital) */
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
);
299 if (params
->cb_first_if_lower_freq
&& div
< priv
->last_div
) {
302 buffer
[2] = (div
>>8) & 0x7f;
303 buffer
[3] = div
& 0xff;
305 buffer
[0] = (div
>>8) & 0x7f;
306 buffer
[1] = div
& 0xff;
310 priv
->last_div
= div
;
311 if (params
->has_tda9887
) {
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
;
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
;
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
);
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
);
357 case TUNER_LG_TDVS_H06XF
:
358 /* Set the Auxiliary Byte. */
359 buffer
[0] = buffer
[2];
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
);
368 case TUNER_MICROTUNE_4042FI5
:
370 // FIXME - this may also work for other tuners
371 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1);
374 /* Wait until the PLL locks */
376 if (time_after(jiffies
,timeout
))
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
);
382 if (status_byte
& TUNER_PLL_LOCKED
)
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;
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
);
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
;
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
)
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);
426 case 1: /* 33.3 MHz */
427 freq
+= (unsigned int)(33.3*16000);
429 case 2: /* 41.3 MHz */
430 freq
+= (unsigned int)(41.3*16000);
433 tuner_warn("Unsupported radio_if value %d\n", params
->radio_if
);
437 /* Bandswitch byte */
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");
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
:
450 case TUNER_TNF_5335MF
:
453 case TUNER_LG_PAL_FM
:
456 case TUNER_THOMSON_DTT761X
:
459 case TUNER_MICROTUNE_4049FM5
:
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) =
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;
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
) {
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
);
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
);
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
));
543 * Overrides for Emacs so that we follow Linus's tabbing style.
544 * ---------------------------------------------------------------------------