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