2 * i2c tv tuner chip device driver
3 * controls all those simple 4-control-bytes style tuners.
5 * This "tuner-simple" module was split apart from the original "tuner" module.
7 #include <linux/delay.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-i2c.h"
14 #include "tuner-simple.h"
17 module_param(debug, int, 0644);
18 MODULE_PARM_DESC(debug, "enable verbose debug messages");
20 #define PREFIX "tuner-simple "
22 static int offset = 0;
23 module_param(offset, int, 0664);
24 MODULE_PARM_DESC(offset,"Allows to specify an offset for tuner");
26 /* ---------------------------------------------------------------------- */
28 /* tv standard selection for Temic 4046 FM5
29 this value takes the low bits of control byte 2
30 from datasheet Rev.01, Feb.00
32 picture IF 38.9 38.9 38.9 33.95 38.9
33 sound 1 33.4 32.9 32.4 40.45 32.4
35 NICAM 33.05 32.348 33.05 33.05
37 #define TEMIC_SET_PAL_I 0x05
38 #define TEMIC_SET_PAL_DK 0x09
39 #define TEMIC_SET_PAL_L 0x0a // SECAM ?
40 #define TEMIC_SET_PAL_L2 0x0b // change IF !
41 #define TEMIC_SET_PAL_BG 0x0c
43 /* tv tuner system standard selection for Philips FQ1216ME
44 this value takes the low bits of control byte 2
45 from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
47 picture carrier 38.90 38.90 38.90 38.90 33.95
48 colour 34.47 34.47 34.47 34.47 38.38
49 sound 1 33.40 32.40 32.90 32.40 40.45
51 NICAM 33.05 33.05 32.35 33.05 39.80
53 #define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
54 #define PHILIPS_SET_PAL_BGDK 0x09
55 #define PHILIPS_SET_PAL_L2 0x0a
56 #define PHILIPS_SET_PAL_L 0x0b
58 /* system switching for Philips FI1216MF MK2
59 from datasheet "1996 Jul 09",
61 picture carrier 38.90 38.90 33.95
62 colour 34.47 34.37 38.38
63 sound 1 33.40 32.40 40.45
65 NICAM 33.05 33.05 39.80
67 #define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
68 #define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
69 #define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
73 #define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
74 #define TUNER_RATIO_SELECT_50 0x00
75 #define TUNER_RATIO_SELECT_32 0x02
76 #define TUNER_RATIO_SELECT_166 0x04
77 #define TUNER_RATIO_SELECT_62 0x06
79 #define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
83 #define TUNER_POR 0x80
85 #define TUNER_MODE 0x38
86 #define TUNER_AFC 0x07
87 #define TUNER_SIGNAL 0x07
88 #define TUNER_STEREO 0x10
90 #define TUNER_PLL_LOCKED 0x40
91 #define TUNER_STEREO_MK3 0x04
93 struct tuner_simple_priv {
95 struct tuner_i2c_props i2c_props;
98 struct tunertype *tun;
103 /* ---------------------------------------------------------------------- */
105 static int tuner_getstatus(struct dvb_frontend *fe)
107 struct tuner_simple_priv *priv = fe->tuner_priv;
110 if (1 != tuner_i2c_xfer_recv(&priv->i2c_props,&byte,1))
116 static int tuner_signal(struct dvb_frontend *fe)
118 return (tuner_getstatus(fe) & TUNER_SIGNAL) << 13;
121 static int tuner_stereo(struct dvb_frontend *fe)
123 struct tuner_simple_priv *priv = fe->tuner_priv;
127 status = tuner_getstatus(fe);
129 switch (priv->type) {
130 case TUNER_PHILIPS_FM1216ME_MK3:
131 case TUNER_PHILIPS_FM1236_MK3:
132 case TUNER_PHILIPS_FM1256_IH3:
133 case TUNER_LG_NTSC_TAPE:
134 stereo = ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
137 stereo = status & TUNER_STEREO;
144 static int simple_get_status(struct dvb_frontend *fe, u32 *status)
146 struct tuner_simple_priv *priv = fe->tuner_priv;
147 int signal = tuner_signal(fe);
152 *status = TUNER_STATUS_LOCKED;
153 if (tuner_stereo(fe))
154 *status |= TUNER_STATUS_STEREO;
156 tuner_dbg("tuner-simple: Signal strength: %d\n", signal);
161 /* ---------------------------------------------------------------------- */
163 static int simple_set_tv_freq(struct dvb_frontend *fe,
164 struct analog_parameters *params)
166 struct tuner_simple_priv *priv = fe->tuner_priv;
167 u8 config, cb, tuneraddr;
169 struct tunertype *tun;
171 int rc, IFPCoff, i, j;
172 enum param_type desired_type;
173 struct tuner_params *t_params;
177 /* IFPCoff = Video Intermediate Frequency - Vif:
178 940 =16*58.75 NTSC/J (Japan)
179 732 =16*45.75 M/N STD
180 704 =16*44 ATSC (at DVB code)
182 622.4=16*38.90 B/G D/K I, L STD
183 592 =16*37.00 D China
184 590 =16.36.875 B Australia
185 543.2=16*33.95 L' STD
186 171.2=16*10.70 FM Radio (at set_radio_freq)
189 if (params->std == V4L2_STD_NTSC_M_JP) {
191 desired_type = TUNER_PARAM_TYPE_NTSC;
192 } else if ((params->std & V4L2_STD_MN) &&
193 !(params->std & ~V4L2_STD_MN)) {
195 desired_type = TUNER_PARAM_TYPE_NTSC;
196 } else if (params->std == V4L2_STD_SECAM_LC) {
198 desired_type = TUNER_PARAM_TYPE_SECAM;
201 desired_type = TUNER_PARAM_TYPE_PAL;
204 for (j = 0; j < tun->count-1; j++) {
205 if (desired_type != tun->params[j].type)
209 /* use default tuner_t_params if desired_type not available */
210 if (desired_type != tun->params[j].type) {
211 tuner_dbg("IFPCoff = %d: tuner_t_params undefined for tuner %d\n",
212 IFPCoff, priv->type);
215 t_params = &tun->params[j];
217 for (i = 0; i < t_params->count; i++) {
218 if (params->frequency > t_params->ranges[i].limit)
222 if (i == t_params->count) {
223 tuner_dbg("TV frequency out of range (%d > %d)",
224 params->frequency, t_params->ranges[i - 1].limit);
225 params->frequency = t_params->ranges[--i].limit;
227 config = t_params->ranges[i].config;
228 cb = t_params->ranges[i].cb;
232 tuner_dbg("tv: param %d, range %d\n",j,i);
234 div=params->frequency + IFPCoff + offset;
236 tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
237 params->frequency / 16, params->frequency % 16 * 100 / 16,
238 IFPCoff / 16, IFPCoff % 16 * 100 / 16,
239 offset / 16, offset % 16 * 100 / 16,
242 /* tv norm specific stuff for multi-norm tuners */
243 switch (priv->type) {
244 case TUNER_PHILIPS_SECAM: // FI1216MF
245 /* 0x01 -> ??? no change ??? */
246 /* 0x02 -> PAL BDGHI / SECAM L */
247 /* 0x04 -> ??? PAL others / SECAM others ??? */
249 if (params->std & V4L2_STD_SECAM_L) //also valid for V4L2_STD_SECAM
250 cb |= PHILIPS_MF_SET_STD_L;
251 else if (params->std & V4L2_STD_SECAM_LC)
252 cb |= PHILIPS_MF_SET_STD_LC;
253 else /* V4L2_STD_B|V4L2_STD_GH */
254 cb |= PHILIPS_MF_SET_STD_BG;
257 case TUNER_TEMIC_4046FM5:
260 if (params->std & V4L2_STD_PAL_BG) {
261 cb |= TEMIC_SET_PAL_BG;
263 } else if (params->std & V4L2_STD_PAL_I) {
264 cb |= TEMIC_SET_PAL_I;
266 } else if (params->std & V4L2_STD_PAL_DK) {
267 cb |= TEMIC_SET_PAL_DK;
269 } else if (params->std & V4L2_STD_SECAM_L) {
270 cb |= TEMIC_SET_PAL_L;
275 case TUNER_PHILIPS_FQ1216ME:
278 if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
279 cb |= PHILIPS_SET_PAL_BGDK;
281 } else if (params->std & V4L2_STD_PAL_I) {
282 cb |= PHILIPS_SET_PAL_I;
284 } else if (params->std & V4L2_STD_SECAM_L) {
285 cb |= PHILIPS_SET_PAL_L;
290 case TUNER_PHILIPS_ATSC:
291 /* 0x00 -> ATSC antenna input 1 */
292 /* 0x01 -> ATSC antenna input 2 */
293 /* 0x02 -> NTSC antenna input 1 */
294 /* 0x03 -> NTSC antenna input 2 */
296 if (!(params->std & V4L2_STD_ATSC))
301 case TUNER_MICROTUNE_4042FI5:
302 /* Set the charge pump for fast tuning */
303 config |= TUNER_CHARGE_PUMP;
306 case TUNER_PHILIPS_TUV1236D:
307 /* 0x40 -> ATSC antenna input 1 */
308 /* 0x48 -> ATSC antenna input 2 */
309 /* 0x00 -> NTSC antenna input 1 */
310 /* 0x08 -> NTSC antenna input 2 */
316 if (params->std & V4L2_STD_ATSC) {
320 /* set to the correct mode (analog or digital) */
321 tuneraddr = priv->i2c_props.addr;
322 priv->i2c_props.addr = 0x0a;
323 if (2 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,&buffer[0],2)))
324 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
325 if (2 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,&buffer[2],2)))
326 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
327 priv->i2c_props.addr = tuneraddr;
332 if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
335 buffer[2] = (div>>8) & 0x7f;
336 buffer[3] = div & 0xff;
338 buffer[0] = (div>>8) & 0x7f;
339 buffer[1] = div & 0xff;
343 priv->last_div = div;
344 if (t_params->has_tda9887) {
346 int is_secam_l = (params->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) &&
347 !(params->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC));
349 if (params->std == V4L2_STD_SECAM_LC) {
350 if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
351 config |= TDA9887_PORT1_ACTIVE;
352 if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
353 config |= TDA9887_PORT2_ACTIVE;
356 if (t_params->port1_active)
357 config |= TDA9887_PORT1_ACTIVE;
358 if (t_params->port2_active)
359 config |= TDA9887_PORT2_ACTIVE;
361 if (t_params->intercarrier_mode)
362 config |= TDA9887_INTERCARRIER;
364 if (i == 0 && t_params->default_top_secam_low)
365 config |= TDA9887_TOP(t_params->default_top_secam_low);
366 else if (i == 1 && t_params->default_top_secam_mid)
367 config |= TDA9887_TOP(t_params->default_top_secam_mid);
368 else if (t_params->default_top_secam_high)
369 config |= TDA9887_TOP(t_params->default_top_secam_high);
372 if (i == 0 && t_params->default_top_low)
373 config |= TDA9887_TOP(t_params->default_top_low);
374 else if (i == 1 && t_params->default_top_mid)
375 config |= TDA9887_TOP(t_params->default_top_mid);
376 else if (t_params->default_top_high)
377 config |= TDA9887_TOP(t_params->default_top_high);
379 if (t_params->default_pll_gating_18)
380 config |= TDA9887_GATING_18;
381 i2c_clients_command(priv->i2c_props.adap, TDA9887_SET_CONFIG, &config);
383 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
384 buffer[0],buffer[1],buffer[2],buffer[3]);
386 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4)))
387 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
389 switch (priv->type) {
390 case TUNER_LG_TDVS_H06XF:
391 /* Set the Auxiliary Byte. */
392 buffer[0] = buffer[2];
396 tuner_dbg("tv 0x%02x 0x%02x\n",buffer[0],buffer[1]);
398 if (2 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,2)))
399 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
401 case TUNER_MICROTUNE_4042FI5:
403 // FIXME - this may also work for other tuners
404 unsigned long timeout = jiffies + msecs_to_jiffies(1);
407 /* Wait until the PLL locks */
409 if (time_after(jiffies,timeout))
411 if (1 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props,&status_byte,1))) {
412 tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",rc);
415 if (status_byte & TUNER_PLL_LOCKED)
420 /* Set the charge pump for optimized phase noise figure */
421 config &= ~TUNER_CHARGE_PUMP;
422 buffer[0] = (div>>8) & 0x7f;
423 buffer[1] = div & 0xff;
426 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
427 buffer[0],buffer[1],buffer[2],buffer[3]);
429 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4)))
430 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
437 static int simple_set_radio_freq(struct dvb_frontend *fe,
438 struct analog_parameters *params)
440 struct tunertype *tun;
441 struct tuner_simple_priv *priv = fe->tuner_priv;
445 struct tuner_params *t_params;
446 unsigned int freq = params->frequency;
450 for (j = tun->count-1; j > 0; j--)
451 if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
453 /* default t_params (j=0) will be used if desired type wasn't found */
454 t_params = &tun->params[j];
456 /* Select Radio 1st IF used */
457 switch (t_params->radio_if) {
458 case 0: /* 10.7 MHz */
459 freq += (unsigned int)(10.7*16000);
461 case 1: /* 33.3 MHz */
462 freq += (unsigned int)(33.3*16000);
464 case 2: /* 41.3 MHz */
465 freq += (unsigned int)(41.3*16000);
468 tuner_warn("Unsupported radio_if value %d\n", t_params->radio_if);
472 /* Bandswitch byte */
473 switch (priv->type) {
474 case TUNER_TENA_9533_DI:
475 case TUNER_YMEC_TVF_5533MF:
476 tuner_dbg("This tuner doesn't have FM. Most cards have a TEA5767 for FM\n");
478 case TUNER_PHILIPS_FM1216ME_MK3:
479 case TUNER_PHILIPS_FM1236_MK3:
480 case TUNER_PHILIPS_FMD1216ME_MK3:
481 case TUNER_LG_NTSC_TAPE:
482 case TUNER_PHILIPS_FM1256_IH3:
485 case TUNER_TNF_5335MF:
488 case TUNER_LG_PAL_FM:
491 case TUNER_THOMSON_DTT761X:
494 case TUNER_MICROTUNE_4049FM5:
500 buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
501 TUNER_RATIO_SELECT_50; /* 50 kHz step */
503 /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
504 freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
506 div = (freq + 400) / 800;
508 if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
509 buffer[0] = buffer[2];
510 buffer[1] = buffer[3];
511 buffer[2] = (div>>8) & 0x7f;
512 buffer[3] = div & 0xff;
514 buffer[0] = (div>>8) & 0x7f;
515 buffer[1] = div & 0xff;
518 tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
519 buffer[0],buffer[1],buffer[2],buffer[3]);
520 priv->last_div = div;
522 if (t_params->has_tda9887) {
524 if (t_params->port1_active && !t_params->port1_fm_high_sensitivity)
525 config |= TDA9887_PORT1_ACTIVE;
526 if (t_params->port2_active && !t_params->port2_fm_high_sensitivity)
527 config |= TDA9887_PORT2_ACTIVE;
528 if (t_params->intercarrier_mode)
529 config |= TDA9887_INTERCARRIER;
530 /* if (t_params->port1_set_for_fm_mono)
531 config &= ~TDA9887_PORT1_ACTIVE;*/
532 if (t_params->fm_gain_normal)
533 config |= TDA9887_GAIN_NORMAL;
534 if (t_params->radio_if == 2)
535 config |= TDA9887_RIF_41_3;
536 i2c_clients_command(priv->i2c_props.adap, TDA9887_SET_CONFIG, &config);
538 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4)))
539 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
544 static int simple_set_params(struct dvb_frontend *fe,
545 struct analog_parameters *params)
547 struct tuner_simple_priv *priv = fe->tuner_priv;
550 switch (params->mode) {
551 case V4L2_TUNER_RADIO:
552 ret = simple_set_radio_freq(fe, params);
553 priv->frequency = params->frequency * 125 / 2;
555 case V4L2_TUNER_ANALOG_TV:
556 case V4L2_TUNER_DIGITAL_TV:
557 ret = simple_set_tv_freq(fe, params);
558 priv->frequency = params->frequency * 62500;
566 static int simple_release(struct dvb_frontend *fe)
568 kfree(fe->tuner_priv);
569 fe->tuner_priv = NULL;
574 static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
576 struct tuner_simple_priv *priv = fe->tuner_priv;
577 *frequency = priv->frequency;
581 static struct dvb_tuner_ops simple_tuner_ops = {
582 .set_analog_params = simple_set_params,
583 .release = simple_release,
584 .get_frequency = simple_get_frequency,
585 .get_status = simple_get_status,
588 struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
589 struct i2c_adapter *i2c_adap,
591 struct simple_tuner_config *cfg)
593 struct tuner_simple_priv *priv = NULL;
595 priv = kzalloc(sizeof(struct tuner_simple_priv), GFP_KERNEL);
598 fe->tuner_priv = priv;
600 priv->i2c_props.addr = i2c_addr;
601 priv->i2c_props.adap = i2c_adap;
602 priv->type = cfg->type;
603 priv->tun = cfg->tun;
605 memcpy(&fe->ops.tuner_ops, &simple_tuner_ops, sizeof(struct dvb_tuner_ops));
607 tuner_info("type set to %d (%s)\n", cfg->type, cfg->tun->name);
609 strlcpy(fe->ops.tuner_ops.info.name, cfg->tun->name, sizeof(fe->ops.tuner_ops.info.name));
615 EXPORT_SYMBOL_GPL(simple_tuner_attach);
617 MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
618 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
619 MODULE_LICENSE("GPL");
622 * Overrides for Emacs so that we follow Linus's tabbing style.
623 * ---------------------------------------------------------------------------