V4L/DVB (3860): Convert l64871 to refactored tuner code
[pandora-kernel.git] / drivers / media / dvb / frontends / l64781.c
1 /*
2     driver for LSI L64781 COFDM demodulator
3
4     Copyright (C) 2001 Holger Waechtler for Convergence Integrated Media GmbH
5                        Marko Kohtala <marko.kohtala@luukku.com>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 */
22
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
29 #include "dvb_frontend.h"
30 #include "l64781.h"
31
32
33 struct l64781_state {
34         struct i2c_adapter* i2c;
35         struct dvb_frontend_ops ops;
36         const struct l64781_config* config;
37         struct dvb_frontend frontend;
38
39         /* private demodulator data */
40         int first:1;
41 };
42
43 #define dprintk(args...) \
44         do { \
45                 if (debug) printk(KERN_DEBUG "l64781: " args); \
46         } while (0)
47
48 static int debug;
49
50 module_param(debug, int, 0644);
51 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
52
53
54 static int l64781_writereg (struct l64781_state* state, u8 reg, u8 data)
55 {
56         int ret;
57         u8 buf [] = { reg, data };
58         struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
59
60         if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1)
61                 dprintk ("%s: write_reg error (reg == %02x) = %02x!\n",
62                          __FUNCTION__, reg, ret);
63
64         return (ret != 1) ? -1 : 0;
65 }
66
67 static int l64781_readreg (struct l64781_state* state, u8 reg)
68 {
69         int ret;
70         u8 b0 [] = { reg };
71         u8 b1 [] = { 0 };
72         struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
73                            { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
74
75         ret = i2c_transfer(state->i2c, msg, 2);
76
77         if (ret != 2) return ret;
78
79         return b1[0];
80 }
81
82 static void apply_tps (struct l64781_state* state)
83 {
84         l64781_writereg (state, 0x2a, 0x00);
85         l64781_writereg (state, 0x2a, 0x01);
86
87         /* This here is a little bit questionable because it enables
88            the automatic update of TPS registers. I think we'd need to
89            handle the IRQ from FE to update some other registers as
90            well, or at least implement some magic to tuning to correct
91            to the TPS received from transmission. */
92         l64781_writereg (state, 0x2a, 0x02);
93 }
94
95
96 static void reset_afc (struct l64781_state* state)
97 {
98         /* Set AFC stall for the AFC_INIT_FRQ setting, TIM_STALL for
99            timing offset */
100         l64781_writereg (state, 0x07, 0x9e); /* stall AFC */
101         l64781_writereg (state, 0x08, 0);    /* AFC INIT FREQ */
102         l64781_writereg (state, 0x09, 0);
103         l64781_writereg (state, 0x0a, 0);
104         l64781_writereg (state, 0x07, 0x8e);
105         l64781_writereg (state, 0x0e, 0);    /* AGC gain to zero in beginning */
106         l64781_writereg (state, 0x11, 0x80); /* stall TIM */
107         l64781_writereg (state, 0x10, 0);    /* TIM_OFFSET_LSB */
108         l64781_writereg (state, 0x12, 0);
109         l64781_writereg (state, 0x13, 0);
110         l64781_writereg (state, 0x11, 0x00);
111 }
112
113 static int reset_and_configure (struct l64781_state* state)
114 {
115         u8 buf [] = { 0x06 };
116         struct i2c_msg msg = { .addr = 0x00, .flags = 0, .buf = buf, .len = 1 };
117         // NOTE: this is correct in writing to address 0x00
118
119         return (i2c_transfer(state->i2c, &msg, 1) == 1) ? 0 : -ENODEV;
120 }
121
122 static int apply_frontend_param (struct dvb_frontend* fe, struct dvb_frontend_parameters *param)
123 {
124         struct l64781_state* state = fe->demodulator_priv;
125         /* The coderates for FEC_NONE, FEC_4_5 and FEC_FEC_6_7 are arbitrary */
126         static const u8 fec_tab[] = { 7, 0, 1, 2, 9, 3, 10, 4 };
127         /* QPSK, QAM_16, QAM_64 */
128         static const u8 qam_tab [] = { 2, 4, 0, 6 };
129         static const u8 bw_tab [] = { 8, 7, 6 };  /* 8Mhz, 7MHz, 6MHz */
130         static const u8 guard_tab [] = { 1, 2, 4, 8 };
131         /* The Grundig 29504-401.04 Tuner comes with 18.432MHz crystal. */
132         static const u32 ppm = 8000;
133         struct dvb_ofdm_parameters *p = &param->u.ofdm;
134         u32 ddfs_offset_fixed;
135 /*      u32 ddfs_offset_variable = 0x6000-((1000000UL+ppm)/ */
136 /*                      bw_tab[p->bandWidth]<<10)/15625; */
137         u32 init_freq;
138         u32 spi_bias;
139         u8 val0x04;
140         u8 val0x05;
141         u8 val0x06;
142         int bw = p->bandwidth - BANDWIDTH_8_MHZ;
143
144         if (fe->ops->tuner_ops.set_params) {
145                 fe->ops->tuner_ops.set_params(fe, param);
146                 if (fe->ops->i2c_gate_ctrl) fe->ops->i2c_gate_ctrl(fe, 0);
147         }
148
149         if (param->inversion != INVERSION_ON &&
150             param->inversion != INVERSION_OFF)
151                 return -EINVAL;
152
153         if (bw < 0 || bw > 2)
154                 return -EINVAL;
155
156         if (p->code_rate_HP != FEC_1_2 && p->code_rate_HP != FEC_2_3 &&
157             p->code_rate_HP != FEC_3_4 && p->code_rate_HP != FEC_5_6 &&
158             p->code_rate_HP != FEC_7_8)
159                 return -EINVAL;
160
161         if (p->hierarchy_information != HIERARCHY_NONE &&
162             (p->code_rate_LP != FEC_1_2 && p->code_rate_LP != FEC_2_3 &&
163              p->code_rate_LP != FEC_3_4 && p->code_rate_LP != FEC_5_6 &&
164              p->code_rate_LP != FEC_7_8))
165                 return -EINVAL;
166
167         if (p->constellation != QPSK && p->constellation != QAM_16 &&
168             p->constellation != QAM_64)
169                 return -EINVAL;
170
171         if (p->transmission_mode != TRANSMISSION_MODE_2K &&
172             p->transmission_mode != TRANSMISSION_MODE_8K)
173                 return -EINVAL;
174
175         if (p->guard_interval < GUARD_INTERVAL_1_32 ||
176             p->guard_interval > GUARD_INTERVAL_1_4)
177                 return -EINVAL;
178
179         if (p->hierarchy_information < HIERARCHY_NONE ||
180             p->hierarchy_information > HIERARCHY_4)
181                 return -EINVAL;
182
183         ddfs_offset_fixed = 0x4000-(ppm<<16)/bw_tab[p->bandwidth]/1000000;
184
185         /* This works up to 20000 ppm, it overflows if too large ppm! */
186         init_freq = (((8UL<<25) + (8UL<<19) / 25*ppm / (15625/25)) /
187                         bw_tab[p->bandwidth] & 0xFFFFFF);
188
189         /* SPI bias calculation is slightly modified to fit in 32bit */
190         /* will work for high ppm only... */
191         spi_bias = 378 * (1 << 10);
192         spi_bias *= 16;
193         spi_bias *= bw_tab[p->bandwidth];
194         spi_bias *= qam_tab[p->constellation];
195         spi_bias /= p->code_rate_HP + 1;
196         spi_bias /= (guard_tab[p->guard_interval] + 32);
197         spi_bias *= 1000ULL;
198         spi_bias /= 1000ULL + ppm/1000;
199         spi_bias *= p->code_rate_HP;
200
201         val0x04 = (p->transmission_mode << 2) | p->guard_interval;
202         val0x05 = fec_tab[p->code_rate_HP];
203
204         if (p->hierarchy_information != HIERARCHY_NONE)
205                 val0x05 |= (p->code_rate_LP - FEC_1_2) << 3;
206
207         val0x06 = (p->hierarchy_information << 2) | p->constellation;
208
209         l64781_writereg (state, 0x04, val0x04);
210         l64781_writereg (state, 0x05, val0x05);
211         l64781_writereg (state, 0x06, val0x06);
212
213         reset_afc (state);
214
215         /* Technical manual section 2.6.1, TIM_IIR_GAIN optimal values */
216         l64781_writereg (state, 0x15,
217                          p->transmission_mode == TRANSMISSION_MODE_2K ? 1 : 3);
218         l64781_writereg (state, 0x16, init_freq & 0xff);
219         l64781_writereg (state, 0x17, (init_freq >> 8) & 0xff);
220         l64781_writereg (state, 0x18, (init_freq >> 16) & 0xff);
221
222         l64781_writereg (state, 0x1b, spi_bias & 0xff);
223         l64781_writereg (state, 0x1c, (spi_bias >> 8) & 0xff);
224         l64781_writereg (state, 0x1d, ((spi_bias >> 16) & 0x7f) |
225                 (param->inversion == INVERSION_ON ? 0x80 : 0x00));
226
227         l64781_writereg (state, 0x22, ddfs_offset_fixed & 0xff);
228         l64781_writereg (state, 0x23, (ddfs_offset_fixed >> 8) & 0x3f);
229
230         l64781_readreg (state, 0x00);  /*  clear interrupt registers... */
231         l64781_readreg (state, 0x01);  /*  dto. */
232
233         apply_tps (state);
234
235         return 0;
236 }
237
238 static int get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters* param)
239 {
240         struct l64781_state* state = fe->demodulator_priv;
241         int tmp;
242
243
244         tmp = l64781_readreg(state, 0x04);
245         switch(tmp & 3) {
246         case 0:
247                 param->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
248                 break;
249         case 1:
250                 param->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
251                 break;
252         case 2:
253                 param->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
254                 break;
255         case 3:
256                 param->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
257                 break;
258         }
259         switch((tmp >> 2) & 3) {
260         case 0:
261                 param->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
262                 break;
263         case 1:
264                 param->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
265                 break;
266         default:
267                 printk("Unexpected value for transmission_mode\n");
268         }
269
270
271
272         tmp = l64781_readreg(state, 0x05);
273         switch(tmp & 7) {
274         case 0:
275                 param->u.ofdm.code_rate_HP = FEC_1_2;
276                 break;
277         case 1:
278                 param->u.ofdm.code_rate_HP = FEC_2_3;
279                 break;
280         case 2:
281                 param->u.ofdm.code_rate_HP = FEC_3_4;
282                 break;
283         case 3:
284                 param->u.ofdm.code_rate_HP = FEC_5_6;
285                 break;
286         case 4:
287                 param->u.ofdm.code_rate_HP = FEC_7_8;
288                 break;
289         default:
290                 printk("Unexpected value for code_rate_HP\n");
291         }
292         switch((tmp >> 3) & 7) {
293         case 0:
294                 param->u.ofdm.code_rate_LP = FEC_1_2;
295                 break;
296         case 1:
297                 param->u.ofdm.code_rate_LP = FEC_2_3;
298                 break;
299         case 2:
300                 param->u.ofdm.code_rate_LP = FEC_3_4;
301                 break;
302         case 3:
303                 param->u.ofdm.code_rate_LP = FEC_5_6;
304                 break;
305         case 4:
306                 param->u.ofdm.code_rate_LP = FEC_7_8;
307                 break;
308         default:
309                 printk("Unexpected value for code_rate_LP\n");
310         }
311
312
313         tmp = l64781_readreg(state, 0x06);
314         switch(tmp & 3) {
315         case 0:
316                 param->u.ofdm.constellation = QPSK;
317                 break;
318         case 1:
319                 param->u.ofdm.constellation = QAM_16;
320                 break;
321         case 2:
322                 param->u.ofdm.constellation = QAM_64;
323                 break;
324         default:
325                 printk("Unexpected value for constellation\n");
326         }
327         switch((tmp >> 2) & 7) {
328         case 0:
329                 param->u.ofdm.hierarchy_information = HIERARCHY_NONE;
330                 break;
331         case 1:
332                 param->u.ofdm.hierarchy_information = HIERARCHY_1;
333                 break;
334         case 2:
335                 param->u.ofdm.hierarchy_information = HIERARCHY_2;
336                 break;
337         case 3:
338                 param->u.ofdm.hierarchy_information = HIERARCHY_4;
339                 break;
340         default:
341                 printk("Unexpected value for hierarchy\n");
342         }
343
344
345         tmp = l64781_readreg (state, 0x1d);
346         param->inversion = (tmp & 0x80) ? INVERSION_ON : INVERSION_OFF;
347
348         tmp = (int) (l64781_readreg (state, 0x08) |
349                      (l64781_readreg (state, 0x09) << 8) |
350                      (l64781_readreg (state, 0x0a) << 16));
351         param->frequency += tmp;
352
353         return 0;
354 }
355
356 static int l64781_read_status(struct dvb_frontend* fe, fe_status_t* status)
357 {
358         struct l64781_state* state = fe->demodulator_priv;
359         int sync = l64781_readreg (state, 0x32);
360         int gain = l64781_readreg (state, 0x0e);
361
362         l64781_readreg (state, 0x00);  /*  clear interrupt registers... */
363         l64781_readreg (state, 0x01);  /*  dto. */
364
365         *status = 0;
366
367         if (gain > 5)
368                 *status |= FE_HAS_SIGNAL;
369
370         if (sync & 0x02) /* VCXO locked, this criteria should be ok */
371                 *status |= FE_HAS_CARRIER;
372
373         if (sync & 0x20)
374                 *status |= FE_HAS_VITERBI;
375
376         if (sync & 0x40)
377                 *status |= FE_HAS_SYNC;
378
379         if (sync == 0x7f)
380                 *status |= FE_HAS_LOCK;
381
382         return 0;
383 }
384
385 static int l64781_read_ber(struct dvb_frontend* fe, u32* ber)
386 {
387         struct l64781_state* state = fe->demodulator_priv;
388
389         /*   XXX FIXME: set up counting period (reg 0x26...0x28)
390          */
391         *ber = l64781_readreg (state, 0x39)
392             | (l64781_readreg (state, 0x3a) << 8);
393
394         return 0;
395 }
396
397 static int l64781_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
398 {
399         struct l64781_state* state = fe->demodulator_priv;
400
401         u8 gain = l64781_readreg (state, 0x0e);
402         *signal_strength = (gain << 8) | gain;
403
404         return 0;
405 }
406
407 static int l64781_read_snr(struct dvb_frontend* fe, u16* snr)
408 {
409         struct l64781_state* state = fe->demodulator_priv;
410
411         u8 avg_quality = 0xff - l64781_readreg (state, 0x33);
412         *snr = (avg_quality << 8) | avg_quality; /* not exact, but...*/
413
414         return 0;
415 }
416
417 static int l64781_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
418 {
419         struct l64781_state* state = fe->demodulator_priv;
420
421         *ucblocks = l64781_readreg (state, 0x37)
422            | (l64781_readreg (state, 0x38) << 8);
423
424         return 0;
425 }
426
427 static int l64781_sleep(struct dvb_frontend* fe)
428 {
429         struct l64781_state* state = fe->demodulator_priv;
430
431         /* Power down */
432         return l64781_writereg (state, 0x3e, 0x5a);
433 }
434
435 static int l64781_init(struct dvb_frontend* fe)
436 {
437         struct l64781_state* state = fe->demodulator_priv;
438
439         reset_and_configure (state);
440
441         /* Power up */
442         l64781_writereg (state, 0x3e, 0xa5);
443
444         /* Reset hard */
445         l64781_writereg (state, 0x2a, 0x04);
446         l64781_writereg (state, 0x2a, 0x00);
447
448         /* Set tuner specific things */
449         /* AFC_POL, set also in reset_afc */
450         l64781_writereg (state, 0x07, 0x8e);
451
452         /* Use internal ADC */
453         l64781_writereg (state, 0x0b, 0x81);
454
455         /* AGC loop gain, and polarity is positive */
456         l64781_writereg (state, 0x0c, 0x84);
457
458         /* Internal ADC outputs two's complement */
459         l64781_writereg (state, 0x0d, 0x8c);
460
461         /* With ppm=8000, it seems the DTR_SENSITIVITY will result in
462            value of 2 with all possible bandwidths and guard
463            intervals, which is the initial value anyway. */
464         /*l64781_writereg (state, 0x19, 0x92);*/
465
466         /* Everything is two's complement, soft bit and CSI_OUT too */
467         l64781_writereg (state, 0x1e, 0x09);
468
469         /* delay a bit after first init attempt */
470         if (state->first) {
471                 state->first = 0;
472                 msleep(200);
473         }
474
475         return 0;
476 }
477
478 static int l64781_get_tune_settings(struct dvb_frontend* fe,
479                                     struct dvb_frontend_tune_settings* fesettings)
480 {
481         fesettings->min_delay_ms = 4000;
482         fesettings->step_size = 0;
483         fesettings->max_drift = 0;
484         return 0;
485 }
486
487 static void l64781_release(struct dvb_frontend* fe)
488 {
489         struct l64781_state* state = fe->demodulator_priv;
490         kfree(state);
491 }
492
493 static struct dvb_frontend_ops l64781_ops;
494
495 struct dvb_frontend* l64781_attach(const struct l64781_config* config,
496                                    struct i2c_adapter* i2c)
497 {
498         struct l64781_state* state = NULL;
499         int reg0x3e = -1;
500         u8 b0 [] = { 0x1a };
501         u8 b1 [] = { 0x00 };
502         struct i2c_msg msg [] = { { .addr = config->demod_address, .flags = 0, .buf = b0, .len = 1 },
503                            { .addr = config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
504
505         /* allocate memory for the internal state */
506         state = kmalloc(sizeof(struct l64781_state), GFP_KERNEL);
507         if (state == NULL) goto error;
508
509         /* setup the state */
510         state->config = config;
511         state->i2c = i2c;
512         memcpy(&state->ops, &l64781_ops, sizeof(struct dvb_frontend_ops));
513         state->first = 1;
514
515         /**
516          *  the L64781 won't show up before we send the reset_and_configure()
517          *  broadcast. If nothing responds there is no L64781 on the bus...
518          */
519         if (reset_and_configure(state) < 0) {
520                 dprintk("No response to reset and configure broadcast...\n");
521                 goto error;
522         }
523
524         /* The chip always responds to reads */
525         if (i2c_transfer(state->i2c, msg, 2) != 2) {
526                 dprintk("No response to read on I2C bus\n");
527                 goto error;
528         }
529
530         /* Save current register contents for bailout */
531         reg0x3e = l64781_readreg(state, 0x3e);
532
533         /* Reading the POWER_DOWN register always returns 0 */
534         if (reg0x3e != 0) {
535                 dprintk("Device doesn't look like L64781\n");
536                 goto error;
537         }
538
539         /* Turn the chip off */
540         l64781_writereg (state, 0x3e, 0x5a);
541
542         /* Responds to all reads with 0 */
543         if (l64781_readreg(state, 0x1a) != 0) {
544                 dprintk("Read 1 returned unexpcted value\n");
545                 goto error;
546         }
547
548         /* Turn the chip on */
549         l64781_writereg (state, 0x3e, 0xa5);
550
551         /* Responds with register default value */
552         if (l64781_readreg(state, 0x1a) != 0xa1) {
553                 dprintk("Read 2 returned unexpcted value\n");
554                 goto error;
555         }
556
557         /* create dvb_frontend */
558         state->frontend.ops = &state->ops;
559         state->frontend.demodulator_priv = state;
560         return &state->frontend;
561
562 error:
563         if (reg0x3e >= 0)
564                 l64781_writereg (state, 0x3e, reg0x3e);  /* restore reg 0x3e */
565         kfree(state);
566         return NULL;
567 }
568
569 static struct dvb_frontend_ops l64781_ops = {
570
571         .info = {
572                 .name = "LSI L64781 DVB-T",
573                 .type = FE_OFDM,
574         /*      .frequency_min = ???,*/
575         /*      .frequency_max = ???,*/
576                 .frequency_stepsize = 166666,
577         /*      .frequency_tolerance = ???,*/
578         /*      .symbol_rate_tolerance = ???,*/
579                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
580                       FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
581                       FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
582                       FE_CAN_MUTE_TS
583         },
584
585         .release = l64781_release,
586
587         .init = l64781_init,
588         .sleep = l64781_sleep,
589
590         .set_frontend = apply_frontend_param,
591         .get_frontend = get_frontend,
592         .get_tune_settings = l64781_get_tune_settings,
593
594         .read_status = l64781_read_status,
595         .read_ber = l64781_read_ber,
596         .read_signal_strength = l64781_read_signal_strength,
597         .read_snr = l64781_read_snr,
598         .read_ucblocks = l64781_read_ucblocks,
599 };
600
601 MODULE_DESCRIPTION("LSI L64781 DVB-T Demodulator driver");
602 MODULE_AUTHOR("Holger Waechtler, Marko Kohtala");
603 MODULE_LICENSE("GPL");
604
605 EXPORT_SYMBOL(l64781_attach);