2 driver for LSI L64781 COFDM demodulator
4 Copyright (C) 2001 Holger Waechtler for Convergence Integrated Media GmbH
5 Marko Kohtala <marko.kohtala@luukku.com>
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.
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.
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.
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include "dvb_frontend.h"
33 struct i2c_adapter
* i2c
;
34 const struct l64781_config
* config
;
35 struct dvb_frontend frontend
;
37 /* private demodulator data */
41 #define dprintk(args...) \
43 if (debug) printk(KERN_DEBUG "l64781: " args); \
48 module_param(debug
, int, 0644);
49 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
52 static int l64781_writereg (struct l64781_state
* state
, u8 reg
, u8 data
)
55 u8 buf
[] = { reg
, data
};
56 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= buf
, .len
= 2 };
58 if ((ret
= i2c_transfer(state
->i2c
, &msg
, 1)) != 1)
59 dprintk ("%s: write_reg error (reg == %02x) = %02x!\n",
62 return (ret
!= 1) ? -1 : 0;
65 static int l64781_readreg (struct l64781_state
* state
, u8 reg
)
70 struct i2c_msg msg
[] = { { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= b0
, .len
= 1 },
71 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 } };
73 ret
= i2c_transfer(state
->i2c
, msg
, 2);
75 if (ret
!= 2) return ret
;
80 static void apply_tps (struct l64781_state
* state
)
82 l64781_writereg (state
, 0x2a, 0x00);
83 l64781_writereg (state
, 0x2a, 0x01);
85 /* This here is a little bit questionable because it enables
86 the automatic update of TPS registers. I think we'd need to
87 handle the IRQ from FE to update some other registers as
88 well, or at least implement some magic to tuning to correct
89 to the TPS received from transmission. */
90 l64781_writereg (state
, 0x2a, 0x02);
94 static void reset_afc (struct l64781_state
* state
)
96 /* Set AFC stall for the AFC_INIT_FRQ setting, TIM_STALL for
98 l64781_writereg (state
, 0x07, 0x9e); /* stall AFC */
99 l64781_writereg (state
, 0x08, 0); /* AFC INIT FREQ */
100 l64781_writereg (state
, 0x09, 0);
101 l64781_writereg (state
, 0x0a, 0);
102 l64781_writereg (state
, 0x07, 0x8e);
103 l64781_writereg (state
, 0x0e, 0); /* AGC gain to zero in beginning */
104 l64781_writereg (state
, 0x11, 0x80); /* stall TIM */
105 l64781_writereg (state
, 0x10, 0); /* TIM_OFFSET_LSB */
106 l64781_writereg (state
, 0x12, 0);
107 l64781_writereg (state
, 0x13, 0);
108 l64781_writereg (state
, 0x11, 0x00);
111 static int reset_and_configure (struct l64781_state
* state
)
113 u8 buf
[] = { 0x06 };
114 struct i2c_msg msg
= { .addr
= 0x00, .flags
= 0, .buf
= buf
, .len
= 1 };
115 // NOTE: this is correct in writing to address 0x00
117 return (i2c_transfer(state
->i2c
, &msg
, 1) == 1) ? 0 : -ENODEV
;
120 static int apply_frontend_param(struct dvb_frontend
*fe
)
122 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
123 struct l64781_state
* state
= fe
->demodulator_priv
;
124 /* The coderates for FEC_NONE, FEC_4_5 and FEC_FEC_6_7 are arbitrary */
125 static const u8 fec_tab
[] = { 7, 0, 1, 2, 9, 3, 10, 4 };
126 /* QPSK, QAM_16, QAM_64 */
127 static const u8 qam_tab
[] = { 2, 4, 0, 6 };
128 static const u8 guard_tab
[] = { 1, 2, 4, 8 };
129 /* The Grundig 29504-401.04 Tuner comes with 18.432MHz crystal. */
130 static const u32 ppm
= 8000;
131 u32 ddfs_offset_fixed
;
132 /* u32 ddfs_offset_variable = 0x6000-((1000000UL+ppm)/ */
133 /* bw_tab[p->bandWidth]<<10)/15625; */
141 switch (p
->bandwidth_hz
) {
155 if (fe
->ops
.tuner_ops
.set_params
) {
156 fe
->ops
.tuner_ops
.set_params(fe
);
157 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
160 if (p
->inversion
!= INVERSION_ON
&&
161 p
->inversion
!= INVERSION_OFF
)
164 if (p
->code_rate_HP
!= FEC_1_2
&& p
->code_rate_HP
!= FEC_2_3
&&
165 p
->code_rate_HP
!= FEC_3_4
&& p
->code_rate_HP
!= FEC_5_6
&&
166 p
->code_rate_HP
!= FEC_7_8
)
169 if (p
->hierarchy
!= HIERARCHY_NONE
&&
170 (p
->code_rate_LP
!= FEC_1_2
&& p
->code_rate_LP
!= FEC_2_3
&&
171 p
->code_rate_LP
!= FEC_3_4
&& p
->code_rate_LP
!= FEC_5_6
&&
172 p
->code_rate_LP
!= FEC_7_8
))
175 if (p
->modulation
!= QPSK
&& p
->modulation
!= QAM_16
&&
176 p
->modulation
!= QAM_64
)
179 if (p
->transmission_mode
!= TRANSMISSION_MODE_2K
&&
180 p
->transmission_mode
!= TRANSMISSION_MODE_8K
)
183 if ((int)p
->guard_interval
< GUARD_INTERVAL_1_32
||
184 p
->guard_interval
> GUARD_INTERVAL_1_4
)
187 if ((int)p
->hierarchy
< HIERARCHY_NONE
||
188 p
->hierarchy
> HIERARCHY_4
)
191 ddfs_offset_fixed
= 0x4000-(ppm
<<16)/bw
/1000000;
193 /* This works up to 20000 ppm, it overflows if too large ppm! */
194 init_freq
= (((8UL<<25) + (8UL<<19) / 25*ppm
/ (15625/25)) /
197 /* SPI bias calculation is slightly modified to fit in 32bit */
198 /* will work for high ppm only... */
199 spi_bias
= 378 * (1 << 10);
202 spi_bias
*= qam_tab
[p
->modulation
];
203 spi_bias
/= p
->code_rate_HP
+ 1;
204 spi_bias
/= (guard_tab
[p
->guard_interval
] + 32);
206 spi_bias
/= 1000 + ppm
/1000;
207 spi_bias
*= p
->code_rate_HP
;
209 val0x04
= (p
->transmission_mode
<< 2) | p
->guard_interval
;
210 val0x05
= fec_tab
[p
->code_rate_HP
];
212 if (p
->hierarchy
!= HIERARCHY_NONE
)
213 val0x05
|= (p
->code_rate_LP
- FEC_1_2
) << 3;
215 val0x06
= (p
->hierarchy
<< 2) | p
->modulation
;
217 l64781_writereg (state
, 0x04, val0x04
);
218 l64781_writereg (state
, 0x05, val0x05
);
219 l64781_writereg (state
, 0x06, val0x06
);
223 /* Technical manual section 2.6.1, TIM_IIR_GAIN optimal values */
224 l64781_writereg (state
, 0x15,
225 p
->transmission_mode
== TRANSMISSION_MODE_2K
? 1 : 3);
226 l64781_writereg (state
, 0x16, init_freq
& 0xff);
227 l64781_writereg (state
, 0x17, (init_freq
>> 8) & 0xff);
228 l64781_writereg (state
, 0x18, (init_freq
>> 16) & 0xff);
230 l64781_writereg (state
, 0x1b, spi_bias
& 0xff);
231 l64781_writereg (state
, 0x1c, (spi_bias
>> 8) & 0xff);
232 l64781_writereg (state
, 0x1d, ((spi_bias
>> 16) & 0x7f) |
233 (p
->inversion
== INVERSION_ON
? 0x80 : 0x00));
235 l64781_writereg (state
, 0x22, ddfs_offset_fixed
& 0xff);
236 l64781_writereg (state
, 0x23, (ddfs_offset_fixed
>> 8) & 0x3f);
238 l64781_readreg (state
, 0x00); /* clear interrupt registers... */
239 l64781_readreg (state
, 0x01); /* dto. */
246 static int get_frontend(struct dvb_frontend
*fe
)
248 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
249 struct l64781_state
* state
= fe
->demodulator_priv
;
253 tmp
= l64781_readreg(state
, 0x04);
256 p
->guard_interval
= GUARD_INTERVAL_1_32
;
259 p
->guard_interval
= GUARD_INTERVAL_1_16
;
262 p
->guard_interval
= GUARD_INTERVAL_1_8
;
265 p
->guard_interval
= GUARD_INTERVAL_1_4
;
268 switch((tmp
>> 2) & 3) {
270 p
->transmission_mode
= TRANSMISSION_MODE_2K
;
273 p
->transmission_mode
= TRANSMISSION_MODE_8K
;
276 printk(KERN_WARNING
"Unexpected value for transmission_mode\n");
279 tmp
= l64781_readreg(state
, 0x05);
282 p
->code_rate_HP
= FEC_1_2
;
285 p
->code_rate_HP
= FEC_2_3
;
288 p
->code_rate_HP
= FEC_3_4
;
291 p
->code_rate_HP
= FEC_5_6
;
294 p
->code_rate_HP
= FEC_7_8
;
297 printk("Unexpected value for code_rate_HP\n");
299 switch((tmp
>> 3) & 7) {
301 p
->code_rate_LP
= FEC_1_2
;
304 p
->code_rate_LP
= FEC_2_3
;
307 p
->code_rate_LP
= FEC_3_4
;
310 p
->code_rate_LP
= FEC_5_6
;
313 p
->code_rate_LP
= FEC_7_8
;
316 printk("Unexpected value for code_rate_LP\n");
319 tmp
= l64781_readreg(state
, 0x06);
322 p
->modulation
= QPSK
;
325 p
->modulation
= QAM_16
;
328 p
->modulation
= QAM_64
;
331 printk(KERN_WARNING
"Unexpected value for modulation\n");
333 switch((tmp
>> 2) & 7) {
335 p
->hierarchy
= HIERARCHY_NONE
;
338 p
->hierarchy
= HIERARCHY_1
;
341 p
->hierarchy
= HIERARCHY_2
;
344 p
->hierarchy
= HIERARCHY_4
;
347 printk("Unexpected value for hierarchy\n");
351 tmp
= l64781_readreg (state
, 0x1d);
352 p
->inversion
= (tmp
& 0x80) ? INVERSION_ON
: INVERSION_OFF
;
354 tmp
= (int) (l64781_readreg (state
, 0x08) |
355 (l64781_readreg (state
, 0x09) << 8) |
356 (l64781_readreg (state
, 0x0a) << 16));
362 static int l64781_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
364 struct l64781_state
* state
= fe
->demodulator_priv
;
365 int sync
= l64781_readreg (state
, 0x32);
366 int gain
= l64781_readreg (state
, 0x0e);
368 l64781_readreg (state
, 0x00); /* clear interrupt registers... */
369 l64781_readreg (state
, 0x01); /* dto. */
374 *status
|= FE_HAS_SIGNAL
;
376 if (sync
& 0x02) /* VCXO locked, this criteria should be ok */
377 *status
|= FE_HAS_CARRIER
;
380 *status
|= FE_HAS_VITERBI
;
383 *status
|= FE_HAS_SYNC
;
386 *status
|= FE_HAS_LOCK
;
391 static int l64781_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
393 struct l64781_state
* state
= fe
->demodulator_priv
;
395 /* XXX FIXME: set up counting period (reg 0x26...0x28)
397 *ber
= l64781_readreg (state
, 0x39)
398 | (l64781_readreg (state
, 0x3a) << 8);
403 static int l64781_read_signal_strength(struct dvb_frontend
* fe
, u16
* signal_strength
)
405 struct l64781_state
* state
= fe
->demodulator_priv
;
407 u8 gain
= l64781_readreg (state
, 0x0e);
408 *signal_strength
= (gain
<< 8) | gain
;
413 static int l64781_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
415 struct l64781_state
* state
= fe
->demodulator_priv
;
417 u8 avg_quality
= 0xff - l64781_readreg (state
, 0x33);
418 *snr
= (avg_quality
<< 8) | avg_quality
; /* not exact, but...*/
423 static int l64781_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
425 struct l64781_state
* state
= fe
->demodulator_priv
;
427 *ucblocks
= l64781_readreg (state
, 0x37)
428 | (l64781_readreg (state
, 0x38) << 8);
433 static int l64781_sleep(struct dvb_frontend
* fe
)
435 struct l64781_state
* state
= fe
->demodulator_priv
;
438 return l64781_writereg (state
, 0x3e, 0x5a);
441 static int l64781_init(struct dvb_frontend
* fe
)
443 struct l64781_state
* state
= fe
->demodulator_priv
;
445 reset_and_configure (state
);
448 l64781_writereg (state
, 0x3e, 0xa5);
451 l64781_writereg (state
, 0x2a, 0x04);
452 l64781_writereg (state
, 0x2a, 0x00);
454 /* Set tuner specific things */
455 /* AFC_POL, set also in reset_afc */
456 l64781_writereg (state
, 0x07, 0x8e);
458 /* Use internal ADC */
459 l64781_writereg (state
, 0x0b, 0x81);
461 /* AGC loop gain, and polarity is positive */
462 l64781_writereg (state
, 0x0c, 0x84);
464 /* Internal ADC outputs two's complement */
465 l64781_writereg (state
, 0x0d, 0x8c);
467 /* With ppm=8000, it seems the DTR_SENSITIVITY will result in
468 value of 2 with all possible bandwidths and guard
469 intervals, which is the initial value anyway. */
470 /*l64781_writereg (state, 0x19, 0x92);*/
472 /* Everything is two's complement, soft bit and CSI_OUT too */
473 l64781_writereg (state
, 0x1e, 0x09);
475 /* delay a bit after first init attempt */
484 static int l64781_get_tune_settings(struct dvb_frontend
* fe
,
485 struct dvb_frontend_tune_settings
* fesettings
)
487 fesettings
->min_delay_ms
= 4000;
488 fesettings
->step_size
= 0;
489 fesettings
->max_drift
= 0;
493 static void l64781_release(struct dvb_frontend
* fe
)
495 struct l64781_state
* state
= fe
->demodulator_priv
;
499 static struct dvb_frontend_ops l64781_ops
;
501 struct dvb_frontend
* l64781_attach(const struct l64781_config
* config
,
502 struct i2c_adapter
* i2c
)
504 struct l64781_state
* state
= NULL
;
508 struct i2c_msg msg
[] = { { .addr
= config
->demod_address
, .flags
= 0, .buf
= b0
, .len
= 1 },
509 { .addr
= config
->demod_address
, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 } };
511 /* allocate memory for the internal state */
512 state
= kzalloc(sizeof(struct l64781_state
), GFP_KERNEL
);
513 if (state
== NULL
) goto error
;
515 /* setup the state */
516 state
->config
= config
;
521 * the L64781 won't show up before we send the reset_and_configure()
522 * broadcast. If nothing responds there is no L64781 on the bus...
524 if (reset_and_configure(state
) < 0) {
525 dprintk("No response to reset and configure broadcast...\n");
529 /* The chip always responds to reads */
530 if (i2c_transfer(state
->i2c
, msg
, 2) != 2) {
531 dprintk("No response to read on I2C bus\n");
535 /* Save current register contents for bailout */
536 reg0x3e
= l64781_readreg(state
, 0x3e);
538 /* Reading the POWER_DOWN register always returns 0 */
540 dprintk("Device doesn't look like L64781\n");
544 /* Turn the chip off */
545 l64781_writereg (state
, 0x3e, 0x5a);
547 /* Responds to all reads with 0 */
548 if (l64781_readreg(state
, 0x1a) != 0) {
549 dprintk("Read 1 returned unexpcted value\n");
553 /* Turn the chip on */
554 l64781_writereg (state
, 0x3e, 0xa5);
556 /* Responds with register default value */
557 if (l64781_readreg(state
, 0x1a) != 0xa1) {
558 dprintk("Read 2 returned unexpcted value\n");
562 /* create dvb_frontend */
563 memcpy(&state
->frontend
.ops
, &l64781_ops
, sizeof(struct dvb_frontend_ops
));
564 state
->frontend
.demodulator_priv
= state
;
565 return &state
->frontend
;
569 l64781_writereg (state
, 0x3e, reg0x3e
); /* restore reg 0x3e */
574 static struct dvb_frontend_ops l64781_ops
= {
575 .delsys
= { SYS_DVBT
},
577 .name
= "LSI L64781 DVB-T",
578 /* .frequency_min = ???,*/
579 /* .frequency_max = ???,*/
580 .frequency_stepsize
= 166666,
581 /* .frequency_tolerance = ???,*/
582 /* .symbol_rate_tolerance = ???,*/
583 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
584 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
|
585 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
|
589 .release
= l64781_release
,
592 .sleep
= l64781_sleep
,
594 .set_frontend
= apply_frontend_param
,
595 .get_frontend
= get_frontend
,
596 .get_tune_settings
= l64781_get_tune_settings
,
598 .read_status
= l64781_read_status
,
599 .read_ber
= l64781_read_ber
,
600 .read_signal_strength
= l64781_read_signal_strength
,
601 .read_snr
= l64781_read_snr
,
602 .read_ucblocks
= l64781_read_ucblocks
,
605 MODULE_DESCRIPTION("LSI L64781 DVB-T Demodulator driver");
606 MODULE_AUTHOR("Holger Waechtler, Marko Kohtala");
607 MODULE_LICENSE("GPL");
609 EXPORT_SYMBOL(l64781_attach
);