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 {
87 struct tuner_i2c_props i2c_props;
90 /* ---------------------------------------------------------------------- */
92 static int tuner_getstatus(struct tuner *t)
94 struct tuner_simple_priv *priv = t->priv;
97 if (1 != tuner_i2c_xfer_recv(&priv->i2c_props,&byte,1))
103 static int tuner_signal(struct tuner *t)
105 return (tuner_getstatus(t) & TUNER_SIGNAL) << 13;
108 static int tuner_stereo(struct tuner *t)
112 status = tuner_getstatus(t);
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 tuner *t, unsigned int freq)
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) */
288 tuneraddr = priv->i2c_props.addr;
289 priv->i2c_props.addr = 0x0a;
290 if (2 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,&buffer[0],2)))
291 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
292 if (2 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,&buffer[2],2)))
293 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
294 priv->i2c_props.addr = tuneraddr;
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(priv->i2c_props.adap, 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 = tuner_i2c_xfer_send(&priv->i2c_props,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 = tuner_i2c_xfer_send(&priv->i2c_props,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 = tuner_i2c_xfer_recv(&priv->i2c_props,&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 = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4)))
397 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
403 static void default_set_radio_freq(struct tuner *t, unsigned int freq)
405 struct tunertype *tun;
406 struct tuner_simple_priv *priv = t->priv;
410 struct tuner_params *params;
412 tun = &tuners[t->type];
414 for (j = tun->count-1; j > 0; j--)
415 if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
417 /* default params (j=0) will be used if desired type wasn't found */
418 params = &tun->params[j];
420 /* Select Radio 1st IF used */
421 switch (params->radio_if) {
422 case 0: /* 10.7 MHz */
423 freq += (unsigned int)(10.7*16000);
425 case 1: /* 33.3 MHz */
426 freq += (unsigned int)(33.3*16000);
428 case 2: /* 41.3 MHz */
429 freq += (unsigned int)(41.3*16000);
432 tuner_warn("Unsupported radio_if value %d\n", params->radio_if);
436 /* Bandswitch byte */
438 case TUNER_TENA_9533_DI:
439 case TUNER_YMEC_TVF_5533MF:
440 tuner_dbg ("This tuner doesn't have FM. Most cards have a TEA5767 for FM\n");
442 case TUNER_PHILIPS_FM1216ME_MK3:
443 case TUNER_PHILIPS_FM1236_MK3:
444 case TUNER_PHILIPS_FMD1216ME_MK3:
445 case TUNER_LG_NTSC_TAPE:
446 case TUNER_PHILIPS_FM1256_IH3:
449 case TUNER_TNF_5335MF:
452 case TUNER_LG_PAL_FM:
455 case TUNER_THOMSON_DTT761X:
458 case TUNER_MICROTUNE_4049FM5:
464 buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) |
465 TUNER_RATIO_SELECT_50; /* 50 kHz step */
467 /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
468 freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
470 div = (freq + 400) / 800;
472 if (params->cb_first_if_lower_freq && div < priv->last_div) {
473 buffer[0] = buffer[2];
474 buffer[1] = buffer[3];
475 buffer[2] = (div>>8) & 0x7f;
476 buffer[3] = div & 0xff;
478 buffer[0] = (div>>8) & 0x7f;
479 buffer[1] = div & 0xff;
482 tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
483 buffer[0],buffer[1],buffer[2],buffer[3]);
484 priv->last_div = div;
486 if (params->has_tda9887) {
488 if (params->port1_active && !params->port1_fm_high_sensitivity)
489 config |= TDA9887_PORT1_ACTIVE;
490 if (params->port2_active && !params->port2_fm_high_sensitivity)
491 config |= TDA9887_PORT2_ACTIVE;
492 if (params->intercarrier_mode)
493 config |= TDA9887_INTERCARRIER;
494 /* if (params->port1_set_for_fm_mono)
495 config &= ~TDA9887_PORT1_ACTIVE;*/
496 if (params->fm_gain_normal)
497 config |= TDA9887_GAIN_NORMAL;
498 if (params->radio_if == 2)
499 config |= TDA9887_RIF_41_3;
500 i2c_clients_command(priv->i2c_props.adap, TDA9887_SET_CONFIG, &config);
502 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4)))
503 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
506 static void tuner_release(struct tuner *t)
512 static struct tuner_operations simple_tuner_ops = {
513 .set_tv_freq = default_set_tv_freq,
514 .set_radio_freq = default_set_radio_freq,
515 .has_signal = tuner_signal,
516 .is_stereo = tuner_stereo,
517 .release = tuner_release,
520 int default_tuner_init(struct tuner *t)
522 struct tuner_simple_priv *priv = NULL;
524 priv = kzalloc(sizeof(struct tuner_simple_priv), GFP_KERNEL);
529 priv->i2c_props.addr = t->i2c.addr;
530 priv->i2c_props.adap = t->i2c.adapter;
532 tuner_info("type set to %d (%s)\n",
533 t->type, tuners[t->type].name);
534 strlcpy(t->i2c.name, tuners[t->type].name, sizeof(t->i2c.name));
536 memcpy(&t->ops, &simple_tuner_ops, sizeof(struct tuner_operations));
542 * Overrides for Emacs so that we follow Linus's tabbing style.
543 * ---------------------------------------------------------------------------