]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/tuner-simple.c
d6d922c32bcef63a8780cc3b1cfa2adf92d2e34b
[linux-2.6-omap-h63xx.git] / drivers / media / video / tuner-simple.c
1 /*
2  * i2c tv tuner chip device driver
3  * controls all those simple 4-control-bytes style tuners.
4  *
5  * This "tuner-simple" module was split apart from the original "tuner" module.
6  */
7 #include <linux/delay.h>
8 #include <linux/i2c.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"
15
16 static int debug;
17 module_param(debug, int, 0644);
18 MODULE_PARM_DESC(debug, "enable verbose debug messages");
19
20 static int offset;
21 module_param(offset, int, 0664);
22 MODULE_PARM_DESC(offset, "Allows to specify an offset for tuner");
23
24 /* ---------------------------------------------------------------------- */
25
26 /* tv standard selection for Temic 4046 FM5
27    this value takes the low bits of control byte 2
28    from datasheet Rev.01, Feb.00
29      standard     BG      I       L       L2      D
30      picture IF   38.9    38.9    38.9    33.95   38.9
31      sound 1      33.4    32.9    32.4    40.45   32.4
32      sound 2      33.16
33      NICAM        33.05   32.348  33.05           33.05
34  */
35 #define TEMIC_SET_PAL_I         0x05
36 #define TEMIC_SET_PAL_DK        0x09
37 #define TEMIC_SET_PAL_L         0x0a /* SECAM ? */
38 #define TEMIC_SET_PAL_L2        0x0b /* change IF ! */
39 #define TEMIC_SET_PAL_BG        0x0c
40
41 /* tv tuner system standard selection for Philips FQ1216ME
42    this value takes the low bits of control byte 2
43    from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
44      standard           BG      DK      I       L       L`
45      picture carrier    38.90   38.90   38.90   38.90   33.95
46      colour             34.47   34.47   34.47   34.47   38.38
47      sound 1            33.40   32.40   32.90   32.40   40.45
48      sound 2            33.16   -       -       -       -
49      NICAM              33.05   33.05   32.35   33.05   39.80
50  */
51 #define PHILIPS_SET_PAL_I       0x01 /* Bit 2 always zero !*/
52 #define PHILIPS_SET_PAL_BGDK    0x09
53 #define PHILIPS_SET_PAL_L2      0x0a
54 #define PHILIPS_SET_PAL_L       0x0b
55
56 /* system switching for Philips FI1216MF MK2
57    from datasheet "1996 Jul 09",
58     standard         BG     L      L'
59     picture carrier  38.90  38.90  33.95
60     colour           34.47  34.37  38.38
61     sound 1          33.40  32.40  40.45
62     sound 2          33.16  -      -
63     NICAM            33.05  33.05  39.80
64  */
65 #define PHILIPS_MF_SET_STD_BG   0x01 /* Bit 2 must be zero, Bit 3 is system output */
66 #define PHILIPS_MF_SET_STD_L    0x03 /* Used on Secam France */
67 #define PHILIPS_MF_SET_STD_LC   0x02 /* Used on SECAM L' */
68
69 /* Control byte */
70
71 #define TUNER_RATIO_MASK        0x06 /* Bit cb1:cb2 */
72 #define TUNER_RATIO_SELECT_50   0x00
73 #define TUNER_RATIO_SELECT_32   0x02
74 #define TUNER_RATIO_SELECT_166  0x04
75 #define TUNER_RATIO_SELECT_62   0x06
76
77 #define TUNER_CHARGE_PUMP       0x40  /* Bit cb6 */
78
79 /* Status byte */
80
81 #define TUNER_POR         0x80
82 #define TUNER_FL          0x40
83 #define TUNER_MODE        0x38
84 #define TUNER_AFC         0x07
85 #define TUNER_SIGNAL      0x07
86 #define TUNER_STEREO      0x10
87
88 #define TUNER_PLL_LOCKED   0x40
89 #define TUNER_STEREO_MK3   0x04
90
91 static DEFINE_MUTEX(tuner_simple_list_mutex);
92 static LIST_HEAD(hybrid_tuner_instance_list);
93
94 struct tuner_simple_priv {
95         u16 last_div;
96
97         struct tuner_i2c_props i2c_props;
98         struct list_head hybrid_tuner_instance_list;
99
100         unsigned int type;
101         struct tunertype *tun;
102
103         u32 frequency;
104         u32 bandwidth;
105 };
106
107 /* ---------------------------------------------------------------------- */
108
109 static int tuner_read_status(struct dvb_frontend *fe)
110 {
111         struct tuner_simple_priv *priv = fe->tuner_priv;
112         unsigned char byte;
113
114         if (1 != tuner_i2c_xfer_recv(&priv->i2c_props, &byte, 1))
115                 return 0;
116
117         return byte;
118 }
119
120 static inline int tuner_signal(const int status)
121 {
122         return (status & TUNER_SIGNAL) << 13;
123 }
124
125 static inline int tuner_stereo(const int type, const int status)
126 {
127         switch (type) {
128         case TUNER_PHILIPS_FM1216ME_MK3:
129         case TUNER_PHILIPS_FM1236_MK3:
130         case TUNER_PHILIPS_FM1256_IH3:
131         case TUNER_LG_NTSC_TAPE:
132                 return ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
133         default:
134                 return status & TUNER_STEREO;
135         }
136 }
137
138 static inline int tuner_islocked(const int status)
139 {
140         return (status & TUNER_FL);
141 }
142
143 static inline int tuner_afcstatus(const int status)
144 {
145         return (status & TUNER_AFC) - 2;
146 }
147
148
149 static int simple_get_status(struct dvb_frontend *fe, u32 *status)
150 {
151         struct tuner_simple_priv *priv = fe->tuner_priv;
152         int tuner_status = tuner_read_status(fe);
153
154         *status = 0;
155
156         if (tuner_islocked(tuner_status))
157                 *status = TUNER_STATUS_LOCKED;
158         if (tuner_stereo(priv->type, tuner_status))
159                 *status |= TUNER_STATUS_STEREO;
160
161         tuner_dbg("AFC Status: %d\n", tuner_afcstatus(tuner_status));
162
163         return 0;
164 }
165
166 static int simple_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
167 {
168         struct tuner_simple_priv *priv = fe->tuner_priv;
169         int signal = tuner_signal(tuner_read_status(fe));
170
171         *strength = signal;
172
173         tuner_dbg("Signal strength: %d\n", signal);
174
175         return 0;
176 }
177
178 /* ---------------------------------------------------------------------- */
179
180 static inline char *tuner_param_name(enum param_type type)
181 {
182         char *name;
183
184         switch (type) {
185         case TUNER_PARAM_TYPE_RADIO:
186                 name = "radio";
187                 break;
188         case TUNER_PARAM_TYPE_PAL:
189                 name = "pal";
190                 break;
191         case TUNER_PARAM_TYPE_SECAM:
192                 name = "secam";
193                 break;
194         case TUNER_PARAM_TYPE_NTSC:
195                 name = "ntsc";
196                 break;
197         case TUNER_PARAM_TYPE_DIGITAL:
198                 name = "digital";
199                 break;
200         default:
201                 name = "unknown";
202                 break;
203         }
204         return name;
205 }
206
207 static struct tuner_params *simple_tuner_params(struct dvb_frontend *fe,
208                                                 enum param_type desired_type)
209 {
210         struct tuner_simple_priv *priv = fe->tuner_priv;
211         struct tunertype *tun = priv->tun;
212         int i;
213
214         for (i = 0; i < tun->count; i++)
215                 if (desired_type == tun->params[i].type)
216                         break;
217
218         /* use default tuner params if desired_type not available */
219         if (i == tun->count) {
220                 tuner_dbg("desired params (%s) undefined for tuner %d\n",
221                           tuner_param_name(desired_type), priv->type);
222                 i = 0;
223         }
224
225         tuner_dbg("using tuner params #%d (%s)\n", i,
226                   tuner_param_name(tun->params[i].type));
227
228         return &tun->params[i];
229 }
230
231 static int simple_config_lookup(struct dvb_frontend *fe,
232                                 struct tuner_params *t_params,
233                                 int *frequency, u8 *config, u8 *cb)
234 {
235         struct tuner_simple_priv *priv = fe->tuner_priv;
236         int i;
237
238         for (i = 0; i < t_params->count; i++) {
239                 if (*frequency > t_params->ranges[i].limit)
240                         continue;
241                 break;
242         }
243         if (i == t_params->count) {
244                 tuner_dbg("frequency out of range (%d > %d)\n",
245                           *frequency, t_params->ranges[i - 1].limit);
246                 *frequency = t_params->ranges[--i].limit;
247         }
248         *config = t_params->ranges[i].config;
249         *cb     = t_params->ranges[i].cb;
250
251         tuner_dbg("freq = %d.%02d (%d), range = %d, "
252                   "config = 0x%02x, cb = 0x%02x\n",
253                   *frequency / 16, *frequency % 16 * 100 / 16, *frequency,
254                   i, *config, *cb);
255
256         return i;
257 }
258
259 /* ---------------------------------------------------------------------- */
260
261 static int simple_std_setup(struct dvb_frontend *fe,
262                             struct analog_parameters *params,
263                             u8 *config, u8 *cb)
264 {
265         struct tuner_simple_priv *priv = fe->tuner_priv;
266         u8 tuneraddr;
267         int rc;
268
269         /* tv norm specific stuff for multi-norm tuners */
270         switch (priv->type) {
271         case TUNER_PHILIPS_SECAM: /* FI1216MF */
272                 /* 0x01 -> ??? no change ??? */
273                 /* 0x02 -> PAL BDGHI / SECAM L */
274                 /* 0x04 -> ??? PAL others / SECAM others ??? */
275                 *cb &= ~0x03;
276                 if (params->std & V4L2_STD_SECAM_L)
277                         /* also valid for V4L2_STD_SECAM */
278                         *cb |= PHILIPS_MF_SET_STD_L;
279                 else if (params->std & V4L2_STD_SECAM_LC)
280                         *cb |= PHILIPS_MF_SET_STD_LC;
281                 else /* V4L2_STD_B|V4L2_STD_GH */
282                         *cb |= PHILIPS_MF_SET_STD_BG;
283                 break;
284
285         case TUNER_TEMIC_4046FM5:
286                 *cb &= ~0x0f;
287
288                 if (params->std & V4L2_STD_PAL_BG) {
289                         *cb |= TEMIC_SET_PAL_BG;
290
291                 } else if (params->std & V4L2_STD_PAL_I) {
292                         *cb |= TEMIC_SET_PAL_I;
293
294                 } else if (params->std & V4L2_STD_PAL_DK) {
295                         *cb |= TEMIC_SET_PAL_DK;
296
297                 } else if (params->std & V4L2_STD_SECAM_L) {
298                         *cb |= TEMIC_SET_PAL_L;
299
300                 }
301                 break;
302
303         case TUNER_PHILIPS_FQ1216ME:
304                 *cb &= ~0x0f;
305
306                 if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
307                         *cb |= PHILIPS_SET_PAL_BGDK;
308
309                 } else if (params->std & V4L2_STD_PAL_I) {
310                         *cb |= PHILIPS_SET_PAL_I;
311
312                 } else if (params->std & V4L2_STD_SECAM_L) {
313                         *cb |= PHILIPS_SET_PAL_L;
314
315                 }
316                 break;
317
318         case TUNER_PHILIPS_ATSC:
319                 /* 0x00 -> ATSC antenna input 1 */
320                 /* 0x01 -> ATSC antenna input 2 */
321                 /* 0x02 -> NTSC antenna input 1 */
322                 /* 0x03 -> NTSC antenna input 2 */
323                 *cb &= ~0x03;
324                 if (!(params->std & V4L2_STD_ATSC))
325                         *cb |= 2;
326                 /* FIXME: input */
327                 break;
328
329         case TUNER_MICROTUNE_4042FI5:
330                 /* Set the charge pump for fast tuning */
331                 *config |= TUNER_CHARGE_PUMP;
332                 break;
333
334         case TUNER_PHILIPS_TUV1236D:
335         {
336                 /* 0x40 -> ATSC antenna input 1 */
337                 /* 0x48 -> ATSC antenna input 2 */
338                 /* 0x00 -> NTSC antenna input 1 */
339                 /* 0x08 -> NTSC antenna input 2 */
340                 u8 buffer[4] = { 0x14, 0x00, 0x17, 0x00};
341                 *cb &= ~0x40;
342                 if (params->std & V4L2_STD_ATSC) {
343                         *cb |= 0x40;
344                         buffer[1] = 0x04;
345                 }
346                 /* set to the correct mode (analog or digital) */
347                 tuneraddr = priv->i2c_props.addr;
348                 priv->i2c_props.addr = 0x0a;
349                 rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[0], 2);
350                 if (2 != rc)
351                         tuner_warn("i2c i/o error: rc == %d "
352                                    "(should be 2)\n", rc);
353                 rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[2], 2);
354                 if (2 != rc)
355                         tuner_warn("i2c i/o error: rc == %d "
356                                    "(should be 2)\n", rc);
357                 priv->i2c_props.addr = tuneraddr;
358                 /* FIXME: input */
359                 break;
360         }
361         }
362
363         return 0;
364 }
365
366 static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
367                             u16 div, u8 config, u8 cb)
368 {
369         struct tuner_simple_priv *priv = fe->tuner_priv;
370         int rc;
371
372         switch (priv->type) {
373         case TUNER_LG_TDVS_H06XF:
374                 /* Set the Auxiliary Byte. */
375                 buffer[0] = buffer[2];
376                 buffer[0] &= ~0x20;
377                 buffer[0] |= 0x18;
378                 buffer[1] = 0x20;
379                 tuner_dbg("tv 0x%02x 0x%02x\n", buffer[0], buffer[1]);
380
381                 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
382                 if (2 != rc)
383                         tuner_warn("i2c i/o error: rc == %d "
384                                    "(should be 2)\n", rc);
385                 break;
386         case TUNER_MICROTUNE_4042FI5:
387         {
388                 /* FIXME - this may also work for other tuners */
389                 unsigned long timeout = jiffies + msecs_to_jiffies(1);
390                 u8 status_byte = 0;
391
392                 /* Wait until the PLL locks */
393                 for (;;) {
394                         if (time_after(jiffies, timeout))
395                                 return 0;
396                         rc = tuner_i2c_xfer_recv(&priv->i2c_props,
397                                                  &status_byte, 1);
398                         if (1 != rc) {
399                                 tuner_warn("i2c i/o read error: rc == %d "
400                                            "(should be 1)\n", rc);
401                                 break;
402                         }
403                         if (status_byte & TUNER_PLL_LOCKED)
404                                 break;
405                         udelay(10);
406                 }
407
408                 /* Set the charge pump for optimized phase noise figure */
409                 config &= ~TUNER_CHARGE_PUMP;
410                 buffer[0] = (div>>8) & 0x7f;
411                 buffer[1] = div      & 0xff;
412                 buffer[2] = config;
413                 buffer[3] = cb;
414                 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
415                           buffer[0], buffer[1], buffer[2], buffer[3]);
416
417                 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
418                 if (4 != rc)
419                         tuner_warn("i2c i/o error: rc == %d "
420                                    "(should be 4)\n", rc);
421                 break;
422         }
423         }
424
425         return 0;
426 }
427
428 static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
429 {
430         struct tuner_simple_priv *priv = fe->tuner_priv;
431
432         switch (priv->type) {
433         case TUNER_TENA_9533_DI:
434         case TUNER_YMEC_TVF_5533MF:
435                 tuner_dbg("This tuner doesn't have FM. "
436                           "Most cards have a TEA5767 for FM\n");
437                 return 0;
438         case TUNER_PHILIPS_FM1216ME_MK3:
439         case TUNER_PHILIPS_FM1236_MK3:
440         case TUNER_PHILIPS_FMD1216ME_MK3:
441         case TUNER_LG_NTSC_TAPE:
442         case TUNER_PHILIPS_FM1256_IH3:
443                 buffer[3] = 0x19;
444                 break;
445         case TUNER_TNF_5335MF:
446                 buffer[3] = 0x11;
447                 break;
448         case TUNER_LG_PAL_FM:
449                 buffer[3] = 0xa5;
450                 break;
451         case TUNER_THOMSON_DTT761X:
452                 buffer[3] = 0x39;
453                 break;
454         case TUNER_MICROTUNE_4049FM5:
455         default:
456                 buffer[3] = 0xa4;
457                 break;
458         }
459
460         return 0;
461 }
462
463 /* ---------------------------------------------------------------------- */
464
465 static int simple_set_tv_freq(struct dvb_frontend *fe,
466                               struct analog_parameters *params)
467 {
468         struct tuner_simple_priv *priv = fe->tuner_priv;
469         u8 config, cb;
470         u16 div;
471         struct tunertype *tun;
472         u8 buffer[4];
473         int rc, IFPCoff, i;
474         enum param_type desired_type;
475         struct tuner_params *t_params;
476
477         tun = priv->tun;
478
479         /* IFPCoff = Video Intermediate Frequency - Vif:
480                 940  =16*58.75  NTSC/J (Japan)
481                 732  =16*45.75  M/N STD
482                 704  =16*44     ATSC (at DVB code)
483                 632  =16*39.50  I U.K.
484                 622.4=16*38.90  B/G D/K I, L STD
485                 592  =16*37.00  D China
486                 590  =16.36.875 B Australia
487                 543.2=16*33.95  L' STD
488                 171.2=16*10.70  FM Radio (at set_radio_freq)
489         */
490
491         if (params->std == V4L2_STD_NTSC_M_JP) {
492                 IFPCoff      = 940;
493                 desired_type = TUNER_PARAM_TYPE_NTSC;
494         } else if ((params->std & V4L2_STD_MN) &&
495                   !(params->std & ~V4L2_STD_MN)) {
496                 IFPCoff      = 732;
497                 desired_type = TUNER_PARAM_TYPE_NTSC;
498         } else if (params->std == V4L2_STD_SECAM_LC) {
499                 IFPCoff      = 543;
500                 desired_type = TUNER_PARAM_TYPE_SECAM;
501         } else {
502                 IFPCoff      = 623;
503                 desired_type = TUNER_PARAM_TYPE_PAL;
504         }
505
506         t_params = simple_tuner_params(fe, desired_type);
507
508         i = simple_config_lookup(fe, t_params, &params->frequency,
509                                  &config, &cb);
510
511         div = params->frequency + IFPCoff + offset;
512
513         tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, "
514                   "Offset=%d.%02d MHz, div=%0d\n",
515                   params->frequency / 16, params->frequency % 16 * 100 / 16,
516                   IFPCoff / 16, IFPCoff % 16 * 100 / 16,
517                   offset / 16, offset % 16 * 100 / 16, div);
518
519         /* tv norm specific stuff for multi-norm tuners */
520         simple_std_setup(fe, params, &config, &cb);
521
522         if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
523                 buffer[0] = config;
524                 buffer[1] = cb;
525                 buffer[2] = (div>>8) & 0x7f;
526                 buffer[3] = div      & 0xff;
527         } else {
528                 buffer[0] = (div>>8) & 0x7f;
529                 buffer[1] = div      & 0xff;
530                 buffer[2] = config;
531                 buffer[3] = cb;
532         }
533         priv->last_div = div;
534         if (t_params->has_tda9887) {
535                 struct v4l2_priv_tun_config tda9887_cfg;
536                 int config = 0;
537                 int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
538                                                  V4L2_STD_SECAM_LC)) &&
539                         !(params->std & ~(V4L2_STD_SECAM_L |
540                                           V4L2_STD_SECAM_LC));
541
542                 tda9887_cfg.tuner = TUNER_TDA9887;
543                 tda9887_cfg.priv  = &config;
544
545                 if (params->std == V4L2_STD_SECAM_LC) {
546                         if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
547                                 config |= TDA9887_PORT1_ACTIVE;
548                         if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
549                                 config |= TDA9887_PORT2_ACTIVE;
550                 } else {
551                         if (t_params->port1_active)
552                                 config |= TDA9887_PORT1_ACTIVE;
553                         if (t_params->port2_active)
554                                 config |= TDA9887_PORT2_ACTIVE;
555                 }
556                 if (t_params->intercarrier_mode)
557                         config |= TDA9887_INTERCARRIER;
558                 if (is_secam_l) {
559                         if (i == 0 && t_params->default_top_secam_low)
560                                 config |= TDA9887_TOP(t_params->default_top_secam_low);
561                         else if (i == 1 && t_params->default_top_secam_mid)
562                                 config |= TDA9887_TOP(t_params->default_top_secam_mid);
563                         else if (t_params->default_top_secam_high)
564                                 config |= TDA9887_TOP(t_params->default_top_secam_high);
565                 } else {
566                         if (i == 0 && t_params->default_top_low)
567                                 config |= TDA9887_TOP(t_params->default_top_low);
568                         else if (i == 1 && t_params->default_top_mid)
569                                 config |= TDA9887_TOP(t_params->default_top_mid);
570                         else if (t_params->default_top_high)
571                                 config |= TDA9887_TOP(t_params->default_top_high);
572                 }
573                 if (t_params->default_pll_gating_18)
574                         config |= TDA9887_GATING_18;
575                 i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
576                                     &tda9887_cfg);
577         }
578         tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
579                   buffer[0], buffer[1], buffer[2], buffer[3]);
580
581         rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
582         if (4 != rc)
583                 tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
584
585         simple_post_tune(fe, &buffer[0], div, config, cb);
586
587         return 0;
588 }
589
590 static int simple_set_radio_freq(struct dvb_frontend *fe,
591                                  struct analog_parameters *params)
592 {
593         struct tunertype *tun;
594         struct tuner_simple_priv *priv = fe->tuner_priv;
595         u8 buffer[4];
596         u16 div;
597         int rc, j;
598         struct tuner_params *t_params;
599         unsigned int freq = params->frequency;
600
601         tun = priv->tun;
602
603         for (j = tun->count-1; j > 0; j--)
604                 if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
605                         break;
606         /* default t_params (j=0) will be used if desired type wasn't found */
607         t_params = &tun->params[j];
608
609         /* Select Radio 1st IF used */
610         switch (t_params->radio_if) {
611         case 0: /* 10.7 MHz */
612                 freq += (unsigned int)(10.7*16000);
613                 break;
614         case 1: /* 33.3 MHz */
615                 freq += (unsigned int)(33.3*16000);
616                 break;
617         case 2: /* 41.3 MHz */
618                 freq += (unsigned int)(41.3*16000);
619                 break;
620         default:
621                 tuner_warn("Unsupported radio_if value %d\n",
622                            t_params->radio_if);
623                 return 0;
624         }
625
626         /* Bandswitch byte */
627         simple_radio_bandswitch(fe, &buffer[0]);
628
629         buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
630                     TUNER_RATIO_SELECT_50; /* 50 kHz step */
631
632         /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
633            freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
634            freq * (1/800) */
635         div = (freq + 400) / 800;
636
637         if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
638                 buffer[0] = buffer[2];
639                 buffer[1] = buffer[3];
640                 buffer[2] = (div>>8) & 0x7f;
641                 buffer[3] = div      & 0xff;
642         } else {
643                 buffer[0] = (div>>8) & 0x7f;
644                 buffer[1] = div      & 0xff;
645         }
646
647         tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
648                buffer[0], buffer[1], buffer[2], buffer[3]);
649         priv->last_div = div;
650
651         if (t_params->has_tda9887) {
652                 int config = 0;
653                 struct v4l2_priv_tun_config tda9887_cfg;
654
655                 tda9887_cfg.tuner = TUNER_TDA9887;
656                 tda9887_cfg.priv = &config;
657
658                 if (t_params->port1_active &&
659                     !t_params->port1_fm_high_sensitivity)
660                         config |= TDA9887_PORT1_ACTIVE;
661                 if (t_params->port2_active &&
662                     !t_params->port2_fm_high_sensitivity)
663                         config |= TDA9887_PORT2_ACTIVE;
664                 if (t_params->intercarrier_mode)
665                         config |= TDA9887_INTERCARRIER;
666 /*              if (t_params->port1_set_for_fm_mono)
667                         config &= ~TDA9887_PORT1_ACTIVE;*/
668                 if (t_params->fm_gain_normal)
669                         config |= TDA9887_GAIN_NORMAL;
670                 if (t_params->radio_if == 2)
671                         config |= TDA9887_RIF_41_3;
672                 i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
673                                     &tda9887_cfg);
674         }
675         rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
676         if (4 != rc)
677                 tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
678
679         return 0;
680 }
681
682 static int simple_set_params(struct dvb_frontend *fe,
683                              struct analog_parameters *params)
684 {
685         struct tuner_simple_priv *priv = fe->tuner_priv;
686         int ret = -EINVAL;
687
688         switch (params->mode) {
689         case V4L2_TUNER_RADIO:
690                 ret = simple_set_radio_freq(fe, params);
691                 priv->frequency = params->frequency * 125 / 2;
692                 break;
693         case V4L2_TUNER_ANALOG_TV:
694         case V4L2_TUNER_DIGITAL_TV:
695                 ret = simple_set_tv_freq(fe, params);
696                 priv->frequency = params->frequency * 62500;
697                 break;
698         }
699         priv->bandwidth = 0;
700
701         return ret;
702 }
703
704 static int simple_dvb_configure(struct dvb_frontend *fe, u8 *buf,
705                                 const struct dvb_frontend_parameters *params)
706 {
707         struct tuner_simple_priv *priv = fe->tuner_priv;
708         struct tunertype *tun = priv->tun;
709         static struct tuner_params *t_params;
710         u8 config, cb;
711         u32 div;
712         int ret, frequency = params->frequency / 62500;
713
714         t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
715         ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
716         if (ret < 0)
717                 return ret;
718
719         div = ((frequency + t_params->iffreq) * 62500 + offset +
720                tun->stepsize/2) / tun->stepsize;
721
722         buf[0] = div >> 8;
723         buf[1] = div & 0xff;
724         buf[2] = config;
725         buf[3] = cb;
726
727         tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
728                   tun->name, div, buf[0], buf[1], buf[2], buf[3]);
729
730         /* calculate the frequency we set it to */
731         return (div * tun->stepsize) - t_params->iffreq;
732 }
733
734 static int simple_dvb_calc_regs(struct dvb_frontend *fe,
735                                 struct dvb_frontend_parameters *params,
736                                 u8 *buf, int buf_len)
737 {
738         struct tuner_simple_priv *priv = fe->tuner_priv;
739         int ret;
740         u32 frequency;
741
742         if (buf_len < 5)
743                 return -EINVAL;
744
745         ret = simple_dvb_configure(fe, buf+1, params);
746         if (ret < 0)
747                 return ret;
748         else
749                 frequency = ret;
750
751         buf[0] = priv->i2c_props.addr;
752
753         priv->frequency = frequency;
754         priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
755                 params->u.ofdm.bandwidth : 0;
756
757         return 5;
758 }
759
760 static int simple_dvb_set_params(struct dvb_frontend *fe,
761                                  struct dvb_frontend_parameters *params)
762 {
763         struct tuner_simple_priv *priv = fe->tuner_priv;
764         u32 prev_freq, prev_bw;
765         int ret;
766         u8 buf[5];
767
768         if (priv->i2c_props.adap == NULL)
769                 return -EINVAL;
770
771         prev_freq = priv->frequency;
772         prev_bw   = priv->bandwidth;
773
774         ret = simple_dvb_calc_regs(fe, params, buf, 5);
775         if (ret != 5)
776                 goto fail;
777
778         /* put analog demod in standby when tuning digital */
779         if (fe->ops.analog_ops.standby)
780                 fe->ops.analog_ops.standby(fe);
781
782         if (fe->ops.i2c_gate_ctrl)
783                 fe->ops.i2c_gate_ctrl(fe, 1);
784
785         /* buf[0] contains the i2c address, but *
786          * we already have it in i2c_props.addr */
787         ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
788         if (ret != 4)
789                 goto fail;
790
791         return 0;
792 fail:
793         /* calc_regs sets frequency and bandwidth. if we failed, unset them */
794         priv->frequency = prev_freq;
795         priv->bandwidth = prev_bw;
796
797         return ret;
798 }
799
800 static int simple_release(struct dvb_frontend *fe)
801 {
802         struct tuner_simple_priv *priv = fe->tuner_priv;
803
804         mutex_lock(&tuner_simple_list_mutex);
805
806         if (priv)
807                 hybrid_tuner_release_state(priv);
808
809         mutex_unlock(&tuner_simple_list_mutex);
810
811         fe->tuner_priv = NULL;
812
813         return 0;
814 }
815
816 static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
817 {
818         struct tuner_simple_priv *priv = fe->tuner_priv;
819         *frequency = priv->frequency;
820         return 0;
821 }
822
823 static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
824 {
825         struct tuner_simple_priv *priv = fe->tuner_priv;
826         *bandwidth = priv->bandwidth;
827         return 0;
828 }
829
830 static struct dvb_tuner_ops simple_tuner_ops = {
831         .set_analog_params = simple_set_params,
832         .set_params        = simple_dvb_set_params,
833         .calc_regs         = simple_dvb_calc_regs,
834         .release           = simple_release,
835         .get_frequency     = simple_get_frequency,
836         .get_bandwidth     = simple_get_bandwidth,
837         .get_status        = simple_get_status,
838         .get_rf_strength   = simple_get_rf_strength,
839 };
840
841 struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
842                                          struct i2c_adapter *i2c_adap,
843                                          u8 i2c_addr,
844                                          unsigned int type)
845 {
846         struct tuner_simple_priv *priv = NULL;
847         int instance;
848
849         if (type >= tuner_count) {
850                 printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
851                        __FUNCTION__, type, tuner_count-1);
852                 return NULL;
853         }
854
855         mutex_lock(&tuner_simple_list_mutex);
856
857         instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
858                                               hybrid_tuner_instance_list,
859                                               i2c_adap, i2c_addr,
860                                               "tuner-simple");
861         switch (instance) {
862         case 0:
863                 mutex_unlock(&tuner_simple_list_mutex);
864                 return NULL;
865                 break;
866         case 1:
867                 fe->tuner_priv = priv;
868
869                 priv->type = type;
870                 priv->tun  = &tuners[type];
871                 break;
872         default:
873                 fe->tuner_priv = priv;
874                 break;
875         }
876
877         mutex_unlock(&tuner_simple_list_mutex);
878
879         memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
880                sizeof(struct dvb_tuner_ops));
881
882         tuner_info("type set to %d (%s)\n", type, priv->tun->name);
883
884         strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
885                 sizeof(fe->ops.tuner_ops.info.name));
886
887         return fe;
888 }
889 EXPORT_SYMBOL_GPL(simple_tuner_attach);
890
891 MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
892 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
893 MODULE_LICENSE("GPL");
894
895 /*
896  * Overrides for Emacs so that we follow Linus's tabbing style.
897  * ---------------------------------------------------------------------------
898  * Local variables:
899  * c-basic-offset: 8
900  * End:
901  */