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/moduleparam.h>
9 #include <linux/videodev.h>
10 #include <media/tuner.h>
11 #include <media/v4l2-common.h>
12 #include <media/tuner-types.h>
13 #include "tuner-driver.h"
15 static int offset = 0;
16 module_param(offset, int, 0664);
17 MODULE_PARM_DESC(offset,"Allows to specify an offset for tuner");
19 /* ---------------------------------------------------------------------- */
21 /* tv standard selection for Temic 4046 FM5
22 this value takes the low bits of control byte 2
23 from datasheet Rev.01, Feb.00
25 picture IF 38.9 38.9 38.9 33.95 38.9
26 sound 1 33.4 32.9 32.4 40.45 32.4
28 NICAM 33.05 32.348 33.05 33.05
30 #define TEMIC_SET_PAL_I 0x05
31 #define TEMIC_SET_PAL_DK 0x09
32 #define TEMIC_SET_PAL_L 0x0a // SECAM ?
33 #define TEMIC_SET_PAL_L2 0x0b // change IF !
34 #define TEMIC_SET_PAL_BG 0x0c
36 /* tv tuner system standard selection for Philips FQ1216ME
37 this value takes the low bits of control byte 2
38 from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
40 picture carrier 38.90 38.90 38.90 38.90 33.95
41 colour 34.47 34.47 34.47 34.47 38.38
42 sound 1 33.40 32.40 32.90 32.40 40.45
44 NICAM 33.05 33.05 32.35 33.05 39.80
46 #define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
47 #define PHILIPS_SET_PAL_BGDK 0x09
48 #define PHILIPS_SET_PAL_L2 0x0a
49 #define PHILIPS_SET_PAL_L 0x0b
51 /* system switching for Philips FI1216MF MK2
52 from datasheet "1996 Jul 09",
54 picture carrier 38.90 38.90 33.95
55 colour 34.47 34.37 38.38
56 sound 1 33.40 32.40 40.45
58 NICAM 33.05 33.05 39.80
60 #define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
61 #define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
62 #define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
66 #define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
67 #define TUNER_RATIO_SELECT_50 0x00
68 #define TUNER_RATIO_SELECT_32 0x02
69 #define TUNER_RATIO_SELECT_166 0x04
70 #define TUNER_RATIO_SELECT_62 0x06
72 #define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
76 #define TUNER_POR 0x80
78 #define TUNER_MODE 0x38
79 #define TUNER_AFC 0x07
80 #define TUNER_SIGNAL 0x07
81 #define TUNER_STEREO 0x10
83 #define TUNER_PLL_LOCKED 0x40
84 #define TUNER_STEREO_MK3 0x04
86 struct tuner_simple_priv {
90 /* ---------------------------------------------------------------------- */
92 static int tuner_getstatus(struct i2c_client *c)
96 if (1 != i2c_master_recv(c,&byte,1))
102 static int tuner_signal(struct i2c_client *c)
104 return (tuner_getstatus(c) & TUNER_SIGNAL) << 13;
107 static int tuner_stereo(struct i2c_client *c)
110 struct tuner *t = i2c_get_clientdata(c);
112 status = tuner_getstatus (c);
115 case TUNER_PHILIPS_FM1216ME_MK3:
116 case TUNER_PHILIPS_FM1236_MK3:
117 case TUNER_PHILIPS_FM1256_IH3:
118 case TUNER_LG_NTSC_TAPE:
119 stereo = ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
122 stereo = status & TUNER_STEREO;
129 /* ---------------------------------------------------------------------- */
131 static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
133 struct tuner *t = i2c_get_clientdata(c);
134 struct tuner_simple_priv *priv = t->priv;
135 u8 config, cb, tuneraddr;
137 struct tunertype *tun;
139 int rc, IFPCoff, i, j;
140 enum param_type desired_type;
141 struct tuner_params *params;
143 tun = &tuners[t->type];
145 /* IFPCoff = Video Intermediate Frequency - Vif:
146 940 =16*58.75 NTSC/J (Japan)
147 732 =16*45.75 M/N STD
148 704 =16*44 ATSC (at DVB code)
150 622.4=16*38.90 B/G D/K I, L STD
151 592 =16*37.00 D China
152 590 =16.36.875 B Australia
153 543.2=16*33.95 L' STD
154 171.2=16*10.70 FM Radio (at set_radio_freq)
157 if (t->std == V4L2_STD_NTSC_M_JP) {
159 desired_type = TUNER_PARAM_TYPE_NTSC;
160 } else if ((t->std & V4L2_STD_MN) &&
161 !(t->std & ~V4L2_STD_MN)) {
163 desired_type = TUNER_PARAM_TYPE_NTSC;
164 } else if (t->std == V4L2_STD_SECAM_LC) {
166 desired_type = TUNER_PARAM_TYPE_SECAM;
169 desired_type = TUNER_PARAM_TYPE_PAL;
172 for (j = 0; j < tun->count-1; j++) {
173 if (desired_type != tun->params[j].type)
177 /* use default tuner_params if desired_type not available */
178 if (desired_type != tun->params[j].type) {
179 tuner_dbg("IFPCoff = %d: tuner_params undefined for tuner %d\n",
183 params = &tun->params[j];
185 for (i = 0; i < params->count; i++) {
186 if (freq > params->ranges[i].limit)
190 if (i == params->count) {
191 tuner_dbg("TV frequency out of range (%d > %d)",
192 freq, params->ranges[i - 1].limit);
193 freq = params->ranges[--i].limit;
195 config = params->ranges[i].config;
196 cb = params->ranges[i].cb;
200 tuner_dbg("tv: param %d, range %d\n",j,i);
202 div=freq + IFPCoff + offset;
204 tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
205 freq / 16, freq % 16 * 100 / 16,
206 IFPCoff / 16, IFPCoff % 16 * 100 / 16,
207 offset / 16, offset % 16 * 100 / 16,
210 /* tv norm specific stuff for multi-norm tuners */
212 case TUNER_PHILIPS_SECAM: // FI1216MF
213 /* 0x01 -> ??? no change ??? */
214 /* 0x02 -> PAL BDGHI / SECAM L */
215 /* 0x04 -> ??? PAL others / SECAM others ??? */
217 if (t->std & V4L2_STD_SECAM_L) //also valid for V4L2_STD_SECAM
218 cb |= PHILIPS_MF_SET_STD_L;
219 else if (t->std & V4L2_STD_SECAM_LC)
220 cb |= PHILIPS_MF_SET_STD_LC;
221 else /* V4L2_STD_B|V4L2_STD_GH */
222 cb |= PHILIPS_MF_SET_STD_BG;
225 case TUNER_TEMIC_4046FM5:
228 if (t->std & V4L2_STD_PAL_BG) {
229 cb |= TEMIC_SET_PAL_BG;
231 } else if (t->std & V4L2_STD_PAL_I) {
232 cb |= TEMIC_SET_PAL_I;
234 } else if (t->std & V4L2_STD_PAL_DK) {
235 cb |= TEMIC_SET_PAL_DK;
237 } else if (t->std & V4L2_STD_SECAM_L) {
238 cb |= TEMIC_SET_PAL_L;
243 case TUNER_PHILIPS_FQ1216ME:
246 if (t->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
247 cb |= PHILIPS_SET_PAL_BGDK;
249 } else if (t->std & V4L2_STD_PAL_I) {
250 cb |= PHILIPS_SET_PAL_I;
252 } else if (t->std & V4L2_STD_SECAM_L) {
253 cb |= PHILIPS_SET_PAL_L;
258 case TUNER_PHILIPS_ATSC:
259 /* 0x00 -> ATSC antenna input 1 */
260 /* 0x01 -> ATSC antenna input 2 */
261 /* 0x02 -> NTSC antenna input 1 */
262 /* 0x03 -> NTSC antenna input 2 */
264 if (!(t->std & V4L2_STD_ATSC))
269 case TUNER_MICROTUNE_4042FI5:
270 /* Set the charge pump for fast tuning */
271 config |= TUNER_CHARGE_PUMP;
274 case TUNER_PHILIPS_TUV1236D:
275 /* 0x40 -> ATSC antenna input 1 */
276 /* 0x48 -> ATSC antenna input 2 */
277 /* 0x00 -> NTSC antenna input 1 */
278 /* 0x08 -> NTSC antenna input 2 */
284 if (t->std & V4L2_STD_ATSC) {
288 /* set to the correct mode (analog or digital) */
291 if (2 != (rc = i2c_master_send(c,&buffer[0],2)))
292 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
293 if (2 != (rc = i2c_master_send(c,&buffer[2],2)))
294 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
300 if (params->cb_first_if_lower_freq && div < priv->last_div) {
303 buffer[2] = (div>>8) & 0x7f;
304 buffer[3] = div & 0xff;
306 buffer[0] = (div>>8) & 0x7f;
307 buffer[1] = div & 0xff;
311 priv->last_div = div;
312 if (params->has_tda9887) {
314 int is_secam_l = (t->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) &&
315 !(t->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC));
317 if (t->std == V4L2_STD_SECAM_LC) {
318 if (params->port1_active ^ params->port1_invert_for_secam_lc)
319 config |= TDA9887_PORT1_ACTIVE;
320 if (params->port2_active ^ params->port2_invert_for_secam_lc)
321 config |= TDA9887_PORT2_ACTIVE;
324 if (params->port1_active)
325 config |= TDA9887_PORT1_ACTIVE;
326 if (params->port2_active)
327 config |= TDA9887_PORT2_ACTIVE;
329 if (params->intercarrier_mode)
330 config |= TDA9887_INTERCARRIER;
332 if (i == 0 && params->default_top_secam_low)
333 config |= TDA9887_TOP(params->default_top_secam_low);
334 else if (i == 1 && params->default_top_secam_mid)
335 config |= TDA9887_TOP(params->default_top_secam_mid);
336 else if (params->default_top_secam_high)
337 config |= TDA9887_TOP(params->default_top_secam_high);
340 if (i == 0 && params->default_top_low)
341 config |= TDA9887_TOP(params->default_top_low);
342 else if (i == 1 && params->default_top_mid)
343 config |= TDA9887_TOP(params->default_top_mid);
344 else if (params->default_top_high)
345 config |= TDA9887_TOP(params->default_top_high);
347 if (params->default_pll_gating_18)
348 config |= TDA9887_GATING_18;
349 i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
351 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
352 buffer[0],buffer[1],buffer[2],buffer[3]);
354 if (4 != (rc = i2c_master_send(c,buffer,4)))
355 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
358 case TUNER_LG_TDVS_H06XF:
359 /* Set the Auxiliary Byte. */
360 buffer[0] = buffer[2];
364 tuner_dbg("tv 0x%02x 0x%02x\n",buffer[0],buffer[1]);
366 if (2 != (rc = i2c_master_send(c,buffer,2)))
367 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
369 case TUNER_MICROTUNE_4042FI5:
371 // FIXME - this may also work for other tuners
372 unsigned long timeout = jiffies + msecs_to_jiffies(1);
375 /* Wait until the PLL locks */
377 if (time_after(jiffies,timeout))
379 if (1 != (rc = i2c_master_recv(c,&status_byte,1))) {
380 tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",rc);
383 if (status_byte & TUNER_PLL_LOCKED)
388 /* Set the charge pump for optimized phase noise figure */
389 config &= ~TUNER_CHARGE_PUMP;
390 buffer[0] = (div>>8) & 0x7f;
391 buffer[1] = div & 0xff;
394 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
395 buffer[0],buffer[1],buffer[2],buffer[3]);
397 if (4 != (rc = i2c_master_send(c,buffer,4)))
398 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
404 static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
406 struct tunertype *tun;
407 struct tuner *t = i2c_get_clientdata(c);
408 struct tuner_simple_priv *priv = t->priv;
412 struct tuner_params *params;
414 tun = &tuners[t->type];
416 for (j = tun->count-1; j > 0; j--)
417 if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
419 /* default params (j=0) will be used if desired type wasn't found */
420 params = &tun->params[j];
422 /* Select Radio 1st IF used */
423 switch (params->radio_if) {
424 case 0: /* 10.7 MHz */
425 freq += (unsigned int)(10.7*16000);
427 case 1: /* 33.3 MHz */
428 freq += (unsigned int)(33.3*16000);
430 case 2: /* 41.3 MHz */
431 freq += (unsigned int)(41.3*16000);
434 tuner_warn("Unsupported radio_if value %d\n", params->radio_if);
438 /* Bandswitch byte */
440 case TUNER_TENA_9533_DI:
441 case TUNER_YMEC_TVF_5533MF:
442 tuner_dbg ("This tuner doesn't have FM. Most cards have a TEA5767 for FM\n");
444 case TUNER_PHILIPS_FM1216ME_MK3:
445 case TUNER_PHILIPS_FM1236_MK3:
446 case TUNER_PHILIPS_FMD1216ME_MK3:
447 case TUNER_LG_NTSC_TAPE:
448 case TUNER_PHILIPS_FM1256_IH3:
451 case TUNER_TNF_5335MF:
454 case TUNER_LG_PAL_FM:
457 case TUNER_THOMSON_DTT761X:
460 case TUNER_MICROTUNE_4049FM5:
466 buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) |
467 TUNER_RATIO_SELECT_50; /* 50 kHz step */
469 /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
470 freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
472 div = (freq + 400) / 800;
474 if (params->cb_first_if_lower_freq && div < priv->last_div) {
475 buffer[0] = buffer[2];
476 buffer[1] = buffer[3];
477 buffer[2] = (div>>8) & 0x7f;
478 buffer[3] = div & 0xff;
480 buffer[0] = (div>>8) & 0x7f;
481 buffer[1] = div & 0xff;
484 tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
485 buffer[0],buffer[1],buffer[2],buffer[3]);
486 priv->last_div = div;
488 if (params->has_tda9887) {
490 if (params->port1_active && !params->port1_fm_high_sensitivity)
491 config |= TDA9887_PORT1_ACTIVE;
492 if (params->port2_active && !params->port2_fm_high_sensitivity)
493 config |= TDA9887_PORT2_ACTIVE;
494 if (params->intercarrier_mode)
495 config |= TDA9887_INTERCARRIER;
496 /* if (params->port1_set_for_fm_mono)
497 config &= ~TDA9887_PORT1_ACTIVE;*/
498 if (params->fm_gain_normal)
499 config |= TDA9887_GAIN_NORMAL;
500 if (params->radio_if == 2)
501 config |= TDA9887_RIF_41_3;
502 i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
504 if (4 != (rc = i2c_master_send(c,buffer,4)))
505 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
508 static void tuner_release(struct i2c_client *c)
510 struct tuner *t = i2c_get_clientdata(c);
516 static struct tuner_operations simple_tuner_ops = {
517 .set_tv_freq = default_set_tv_freq,
518 .set_radio_freq = default_set_radio_freq,
519 .has_signal = tuner_signal,
520 .is_stereo = tuner_stereo,
521 .release = tuner_release,
524 int default_tuner_init(struct i2c_client *c)
526 struct tuner *t = i2c_get_clientdata(c);
527 struct tuner_simple_priv *priv = NULL;
529 priv = kzalloc(sizeof(struct tuner_simple_priv), GFP_KERNEL);
534 tuner_info("type set to %d (%s)\n",
535 t->type, tuners[t->type].name);
536 strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
538 memcpy(&t->ops, &simple_tuner_ops, sizeof(struct tuner_operations));
544 * Overrides for Emacs so that we follow Linus's tabbing style.
545 * ---------------------------------------------------------------------------