]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/video/tuner-simple.c
V4L/DVB (6075): tuner: kernel headers go above subsystem headers
[karo-tx-linux.git] / drivers / media / video / tuner-simple.c
1 /*
2  *
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/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"
14
15 static int offset = 0;
16 module_param(offset, int, 0664);
17 MODULE_PARM_DESC(offset,"Allows to specify an offset for tuner");
18
19 /* ---------------------------------------------------------------------- */
20
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
24      standard     BG      I       L       L2      D
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
27      sound 2      33.16
28      NICAM        33.05   32.348  33.05           33.05
29  */
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
35
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")
39      standard           BG      DK      I       L       L`
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
43      sound 2            33.16   -       -       -       -
44      NICAM              33.05   33.05   32.35   33.05   39.80
45  */
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
50
51 /* system switching for Philips FI1216MF MK2
52    from datasheet "1996 Jul 09",
53     standard         BG     L      L'
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
57     sound 2          33.16  -      -
58     NICAM            33.05  33.05  39.80
59  */
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' */
63
64 /* Control byte */
65
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
71
72 #define TUNER_CHARGE_PUMP       0x40  /* Bit cb6 */
73
74 /* Status byte */
75
76 #define TUNER_POR         0x80
77 #define TUNER_FL          0x40
78 #define TUNER_MODE        0x38
79 #define TUNER_AFC         0x07
80 #define TUNER_SIGNAL      0x07
81 #define TUNER_STEREO      0x10
82
83 #define TUNER_PLL_LOCKED   0x40
84 #define TUNER_STEREO_MK3   0x04
85
86 struct tuner_simple_priv {
87         u16 last_div;
88 };
89
90 /* ---------------------------------------------------------------------- */
91
92 static int tuner_getstatus(struct i2c_client *c)
93 {
94         unsigned char byte;
95
96         if (1 != i2c_master_recv(c,&byte,1))
97                 return 0;
98
99         return byte;
100 }
101
102 static int tuner_signal(struct i2c_client *c)
103 {
104         return (tuner_getstatus(c) & TUNER_SIGNAL) << 13;
105 }
106
107 static int tuner_stereo(struct i2c_client *c)
108 {
109         int stereo, status;
110         struct tuner *t = i2c_get_clientdata(c);
111
112         status = tuner_getstatus (c);
113
114         switch (t->type) {
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);
120                         break;
121                 default:
122                         stereo = status & TUNER_STEREO;
123         }
124
125         return stereo;
126 }
127
128
129 /* ---------------------------------------------------------------------- */
130
131 static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
132 {
133         struct tuner *t = i2c_get_clientdata(c);
134         struct tuner_simple_priv *priv = t->priv;
135         u8 config, cb, tuneraddr;
136         u16 div;
137         struct tunertype *tun;
138         u8 buffer[4];
139         int rc, IFPCoff, i, j;
140         enum param_type desired_type;
141         struct tuner_params *params;
142
143         tun = &tuners[t->type];
144
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)
149                 632  =16*39.50  I U.K.
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)
155         */
156
157         if (t->std == V4L2_STD_NTSC_M_JP) {
158                 IFPCoff      = 940;
159                 desired_type = TUNER_PARAM_TYPE_NTSC;
160         } else if ((t->std & V4L2_STD_MN) &&
161                   !(t->std & ~V4L2_STD_MN)) {
162                 IFPCoff      = 732;
163                 desired_type = TUNER_PARAM_TYPE_NTSC;
164         } else if (t->std == V4L2_STD_SECAM_LC) {
165                 IFPCoff      = 543;
166                 desired_type = TUNER_PARAM_TYPE_SECAM;
167         } else {
168                 IFPCoff      = 623;
169                 desired_type = TUNER_PARAM_TYPE_PAL;
170         }
171
172         for (j = 0; j < tun->count-1; j++) {
173                 if (desired_type != tun->params[j].type)
174                         continue;
175                 break;
176         }
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",
180                           IFPCoff,t->type);
181                 j = 0;
182         }
183         params = &tun->params[j];
184
185         for (i = 0; i < params->count; i++) {
186                 if (freq > params->ranges[i].limit)
187                         continue;
188                 break;
189         }
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;
194         }
195         config = params->ranges[i].config;
196         cb     = params->ranges[i].cb;
197         /*  i == 0 -> VHF_LO
198          *  i == 1 -> VHF_HI
199          *  i == 2 -> UHF     */
200         tuner_dbg("tv: param %d, range %d\n",j,i);
201
202         div=freq + IFPCoff + offset;
203
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,
208                                         div);
209
210         /* tv norm specific stuff for multi-norm tuners */
211         switch (t->type) {
212         case TUNER_PHILIPS_SECAM: // FI1216MF
213                 /* 0x01 -> ??? no change ??? */
214                 /* 0x02 -> PAL BDGHI / SECAM L */
215                 /* 0x04 -> ??? PAL others / SECAM others ??? */
216                 cb &= ~0x03;
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;
223                 break;
224
225         case TUNER_TEMIC_4046FM5:
226                 cb &= ~0x0f;
227
228                 if (t->std & V4L2_STD_PAL_BG) {
229                         cb |= TEMIC_SET_PAL_BG;
230
231                 } else if (t->std & V4L2_STD_PAL_I) {
232                         cb |= TEMIC_SET_PAL_I;
233
234                 } else if (t->std & V4L2_STD_PAL_DK) {
235                         cb |= TEMIC_SET_PAL_DK;
236
237                 } else if (t->std & V4L2_STD_SECAM_L) {
238                         cb |= TEMIC_SET_PAL_L;
239
240                 }
241                 break;
242
243         case TUNER_PHILIPS_FQ1216ME:
244                 cb &= ~0x0f;
245
246                 if (t->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
247                         cb |= PHILIPS_SET_PAL_BGDK;
248
249                 } else if (t->std & V4L2_STD_PAL_I) {
250                         cb |= PHILIPS_SET_PAL_I;
251
252                 } else if (t->std & V4L2_STD_SECAM_L) {
253                         cb |= PHILIPS_SET_PAL_L;
254
255                 }
256                 break;
257
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 */
263                 cb &= ~0x03;
264                 if (!(t->std & V4L2_STD_ATSC))
265                         cb |= 2;
266                 /* FIXME: input */
267                 break;
268
269         case TUNER_MICROTUNE_4042FI5:
270                 /* Set the charge pump for fast tuning */
271                 config |= TUNER_CHARGE_PUMP;
272                 break;
273
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 */
279                 buffer[0] = 0x14;
280                 buffer[1] = 0x00;
281                 buffer[2] = 0x17;
282                 buffer[3] = 0x00;
283                 cb &= ~0x40;
284                 if (t->std & V4L2_STD_ATSC) {
285                         cb |= 0x40;
286                         buffer[1] = 0x04;
287                 }
288                 /* set to the correct mode (analog or digital) */
289                 tuneraddr = c->addr;
290                 c->addr = 0x0a;
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);
295                 c->addr = tuneraddr;
296                 /* FIXME: input */
297                 break;
298         }
299
300         if (params->cb_first_if_lower_freq && div < priv->last_div) {
301                 buffer[0] = config;
302                 buffer[1] = cb;
303                 buffer[2] = (div>>8) & 0x7f;
304                 buffer[3] = div      & 0xff;
305         } else {
306                 buffer[0] = (div>>8) & 0x7f;
307                 buffer[1] = div      & 0xff;
308                 buffer[2] = config;
309                 buffer[3] = cb;
310         }
311         priv->last_div = div;
312         if (params->has_tda9887) {
313                 int config = 0;
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));
316
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;
322                 }
323                 else {
324                         if (params->port1_active)
325                                 config |= TDA9887_PORT1_ACTIVE;
326                         if (params->port2_active)
327                                 config |= TDA9887_PORT2_ACTIVE;
328                 }
329                 if (params->intercarrier_mode)
330                         config |= TDA9887_INTERCARRIER;
331                 if (is_secam_l) {
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);
338                 }
339                 else {
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);
346                 }
347                 if (params->default_pll_gating_18)
348                         config |= TDA9887_GATING_18;
349                 i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
350         }
351         tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
352                   buffer[0],buffer[1],buffer[2],buffer[3]);
353
354         if (4 != (rc = i2c_master_send(c,buffer,4)))
355                 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
356
357         switch (t->type) {
358         case TUNER_LG_TDVS_H06XF:
359                 /* Set the Auxiliary Byte. */
360                 buffer[0] = buffer[2];
361                 buffer[0] &= ~0x20;
362                 buffer[0] |= 0x18;
363                 buffer[1] = 0x20;
364                 tuner_dbg("tv 0x%02x 0x%02x\n",buffer[0],buffer[1]);
365
366                 if (2 != (rc = i2c_master_send(c,buffer,2)))
367                         tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
368                 break;
369         case TUNER_MICROTUNE_4042FI5:
370         {
371                 // FIXME - this may also work for other tuners
372                 unsigned long timeout = jiffies + msecs_to_jiffies(1);
373                 u8 status_byte = 0;
374
375                 /* Wait until the PLL locks */
376                 for (;;) {
377                         if (time_after(jiffies,timeout))
378                                 return;
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);
381                                 break;
382                         }
383                         if (status_byte & TUNER_PLL_LOCKED)
384                                 break;
385                         udelay(10);
386                 }
387
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;
392                 buffer[2] = config;
393                 buffer[3] = cb;
394                 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
395                           buffer[0],buffer[1],buffer[2],buffer[3]);
396
397                 if (4 != (rc = i2c_master_send(c,buffer,4)))
398                         tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
399                 break;
400         }
401         }
402 }
403
404 static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
405 {
406         struct tunertype *tun;
407         struct tuner *t = i2c_get_clientdata(c);
408         struct tuner_simple_priv *priv = t->priv;
409         u8 buffer[4];
410         u16 div;
411         int rc, j;
412         struct tuner_params *params;
413
414         tun = &tuners[t->type];
415
416         for (j = tun->count-1; j > 0; j--)
417                 if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
418                         break;
419         /* default params (j=0) will be used if desired type wasn't found */
420         params = &tun->params[j];
421
422         /* Select Radio 1st IF used */
423         switch (params->radio_if) {
424         case 0: /* 10.7 MHz */
425                 freq += (unsigned int)(10.7*16000);
426                 break;
427         case 1: /* 33.3 MHz */
428                 freq += (unsigned int)(33.3*16000);
429                 break;
430         case 2: /* 41.3 MHz */
431                 freq += (unsigned int)(41.3*16000);
432                 break;
433         default:
434                 tuner_warn("Unsupported radio_if value %d\n", params->radio_if);
435                 return;
436         }
437
438         /* Bandswitch byte */
439         switch (t->type) {
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");
443                 return;
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:
449                 buffer[3] = 0x19;
450                 break;
451         case TUNER_TNF_5335MF:
452                 buffer[3] = 0x11;
453                 break;
454         case TUNER_LG_PAL_FM:
455                 buffer[3] = 0xa5;
456                 break;
457         case TUNER_THOMSON_DTT761X:
458                 buffer[3] = 0x39;
459                 break;
460         case TUNER_MICROTUNE_4049FM5:
461         default:
462                 buffer[3] = 0xa4;
463                 break;
464         }
465
466         buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) |
467                     TUNER_RATIO_SELECT_50; /* 50 kHz step */
468
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) =
471            freq * (1/800) */
472         div = (freq + 400) / 800;
473
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;
479         } else {
480                 buffer[0] = (div>>8) & 0x7f;
481                 buffer[1] = div      & 0xff;
482         }
483
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;
487
488         if (params->has_tda9887) {
489                 int config = 0;
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);
503         }
504         if (4 != (rc = i2c_master_send(c,buffer,4)))
505                 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
506 }
507
508 static void tuner_release(struct i2c_client *c)
509 {
510         struct tuner *t = i2c_get_clientdata(c);
511
512         kfree(t->priv);
513         t->priv = NULL;
514 }
515
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,
522 };
523
524 int default_tuner_init(struct i2c_client *c)
525 {
526         struct tuner *t = i2c_get_clientdata(c);
527         struct tuner_simple_priv *priv = NULL;
528
529         priv = kzalloc(sizeof(struct tuner_simple_priv), GFP_KERNEL);
530         if (priv == NULL)
531                 return -ENOMEM;
532         t->priv = priv;
533
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));
537
538         memcpy(&t->ops, &simple_tuner_ops, sizeof(struct tuner_operations));
539
540         return 0;
541 }
542
543 /*
544  * Overrides for Emacs so that we follow Linus's tabbing style.
545  * ---------------------------------------------------------------------------
546  * Local variables:
547  * c-basic-offset: 8
548  * End:
549  */