1 // SPDX-License-Identifier: GPL-2.0-or-later
3 driver for LSI L64781 COFDM demodulator
5 Copyright (C) 2001 Holger Waechtler for Convergence Integrated Media GmbH
6 Marko Kohtala <marko.kohtala@luukku.com>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
16 #include <media/dvb_frontend.h>
21 struct i2c_adapter
* i2c
;
22 const struct l64781_config
* config
;
23 struct dvb_frontend frontend
;
25 /* private demodulator data */
29 #define dprintk(args...) \
31 if (debug) printk(KERN_DEBUG "l64781: " args); \
36 module_param(debug
, int, 0644);
37 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
40 static int l64781_writereg (struct l64781_state
* state
, u8 reg
, u8 data
)
43 u8 buf
[] = { reg
, data
};
44 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= buf
, .len
= 2 };
46 if ((ret
= i2c_transfer(state
->i2c
, &msg
, 1)) != 1)
47 dprintk ("%s: write_reg error (reg == %02x) = %02x!\n",
50 return (ret
!= 1) ? -1 : 0;
53 static int l64781_readreg (struct l64781_state
* state
, u8 reg
)
58 struct i2c_msg msg
[] = { { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= b0
, .len
= 1 },
59 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 } };
61 ret
= i2c_transfer(state
->i2c
, msg
, 2);
63 if (ret
!= 2) return ret
;
68 static void apply_tps (struct l64781_state
* state
)
70 l64781_writereg (state
, 0x2a, 0x00);
71 l64781_writereg (state
, 0x2a, 0x01);
73 /* This here is a little bit questionable because it enables
74 the automatic update of TPS registers. I think we'd need to
75 handle the IRQ from FE to update some other registers as
76 well, or at least implement some magic to tuning to correct
77 to the TPS received from transmission. */
78 l64781_writereg (state
, 0x2a, 0x02);
82 static void reset_afc (struct l64781_state
* state
)
84 /* Set AFC stall for the AFC_INIT_FRQ setting, TIM_STALL for
86 l64781_writereg (state
, 0x07, 0x9e); /* stall AFC */
87 l64781_writereg (state
, 0x08, 0); /* AFC INIT FREQ */
88 l64781_writereg (state
, 0x09, 0);
89 l64781_writereg (state
, 0x0a, 0);
90 l64781_writereg (state
, 0x07, 0x8e);
91 l64781_writereg (state
, 0x0e, 0); /* AGC gain to zero in beginning */
92 l64781_writereg (state
, 0x11, 0x80); /* stall TIM */
93 l64781_writereg (state
, 0x10, 0); /* TIM_OFFSET_LSB */
94 l64781_writereg (state
, 0x12, 0);
95 l64781_writereg (state
, 0x13, 0);
96 l64781_writereg (state
, 0x11, 0x00);
99 static int reset_and_configure (struct l64781_state
* state
)
101 u8 buf
[] = { 0x06 };
102 struct i2c_msg msg
= { .addr
= 0x00, .flags
= 0, .buf
= buf
, .len
= 1 };
103 // NOTE: this is correct in writing to address 0x00
105 return (i2c_transfer(state
->i2c
, &msg
, 1) == 1) ? 0 : -ENODEV
;
108 static int apply_frontend_param(struct dvb_frontend
*fe
)
110 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
111 struct l64781_state
* state
= fe
->demodulator_priv
;
112 /* The coderates for FEC_NONE, FEC_4_5 and FEC_FEC_6_7 are arbitrary */
113 static const u8 fec_tab
[] = { 7, 0, 1, 2, 9, 3, 10, 4 };
114 /* QPSK, QAM_16, QAM_64 */
115 static const u8 qam_tab
[] = { 2, 4, 0, 6 };
116 static const u8 guard_tab
[] = { 1, 2, 4, 8 };
117 /* The Grundig 29504-401.04 Tuner comes with 18.432MHz crystal. */
118 static const u32 ppm
= 8000;
119 u32 ddfs_offset_fixed
;
120 /* u32 ddfs_offset_variable = 0x6000-((1000000UL+ppm)/ */
121 /* bw_tab[p->bandWidth]<<10)/15625; */
129 switch (p
->bandwidth_hz
) {
143 if (fe
->ops
.tuner_ops
.set_params
) {
144 fe
->ops
.tuner_ops
.set_params(fe
);
145 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
148 if (p
->inversion
!= INVERSION_ON
&&
149 p
->inversion
!= INVERSION_OFF
)
152 if (p
->code_rate_HP
!= FEC_1_2
&& p
->code_rate_HP
!= FEC_2_3
&&
153 p
->code_rate_HP
!= FEC_3_4
&& p
->code_rate_HP
!= FEC_5_6
&&
154 p
->code_rate_HP
!= FEC_7_8
)
157 if (p
->hierarchy
!= HIERARCHY_NONE
&&
158 (p
->code_rate_LP
!= FEC_1_2
&& p
->code_rate_LP
!= FEC_2_3
&&
159 p
->code_rate_LP
!= FEC_3_4
&& p
->code_rate_LP
!= FEC_5_6
&&
160 p
->code_rate_LP
!= FEC_7_8
))
163 if (p
->modulation
!= QPSK
&& p
->modulation
!= QAM_16
&&
164 p
->modulation
!= QAM_64
)
167 if (p
->transmission_mode
!= TRANSMISSION_MODE_2K
&&
168 p
->transmission_mode
!= TRANSMISSION_MODE_8K
)
171 if ((int)p
->guard_interval
< GUARD_INTERVAL_1_32
||
172 p
->guard_interval
> GUARD_INTERVAL_1_4
)
175 if ((int)p
->hierarchy
< HIERARCHY_NONE
||
176 p
->hierarchy
> HIERARCHY_4
)
179 ddfs_offset_fixed
= 0x4000-(ppm
<<16)/bw
/1000000;
181 /* This works up to 20000 ppm, it overflows if too large ppm! */
182 init_freq
= (((8UL<<25) + (8UL<<19) / 25*ppm
/ (15625/25)) /
185 /* SPI bias calculation is slightly modified to fit in 32bit */
186 /* will work for high ppm only... */
187 spi_bias
= 378 * (1 << 10);
190 spi_bias
*= qam_tab
[p
->modulation
];
191 spi_bias
/= p
->code_rate_HP
+ 1;
192 spi_bias
/= (guard_tab
[p
->guard_interval
] + 32);
194 spi_bias
/= 1000 + ppm
/1000;
195 spi_bias
*= p
->code_rate_HP
;
197 val0x04
= (p
->transmission_mode
<< 2) | p
->guard_interval
;
198 val0x05
= fec_tab
[p
->code_rate_HP
];
200 if (p
->hierarchy
!= HIERARCHY_NONE
)
201 val0x05
|= (p
->code_rate_LP
- FEC_1_2
) << 3;
203 val0x06
= (p
->hierarchy
<< 2) | p
->modulation
;
205 l64781_writereg (state
, 0x04, val0x04
);
206 l64781_writereg (state
, 0x05, val0x05
);
207 l64781_writereg (state
, 0x06, val0x06
);
211 /* Technical manual section 2.6.1, TIM_IIR_GAIN optimal values */
212 l64781_writereg (state
, 0x15,
213 p
->transmission_mode
== TRANSMISSION_MODE_2K
? 1 : 3);
214 l64781_writereg (state
, 0x16, init_freq
& 0xff);
215 l64781_writereg (state
, 0x17, (init_freq
>> 8) & 0xff);
216 l64781_writereg (state
, 0x18, (init_freq
>> 16) & 0xff);
218 l64781_writereg (state
, 0x1b, spi_bias
& 0xff);
219 l64781_writereg (state
, 0x1c, (spi_bias
>> 8) & 0xff);
220 l64781_writereg (state
, 0x1d, ((spi_bias
>> 16) & 0x7f) |
221 (p
->inversion
== INVERSION_ON
? 0x80 : 0x00));
223 l64781_writereg (state
, 0x22, ddfs_offset_fixed
& 0xff);
224 l64781_writereg (state
, 0x23, (ddfs_offset_fixed
>> 8) & 0x3f);
226 l64781_readreg (state
, 0x00); /* clear interrupt registers... */
227 l64781_readreg (state
, 0x01); /* dto. */
234 static int get_frontend(struct dvb_frontend
*fe
,
235 struct dtv_frontend_properties
*p
)
237 struct l64781_state
* state
= fe
->demodulator_priv
;
241 tmp
= l64781_readreg(state
, 0x04);
244 p
->guard_interval
= GUARD_INTERVAL_1_32
;
247 p
->guard_interval
= GUARD_INTERVAL_1_16
;
250 p
->guard_interval
= GUARD_INTERVAL_1_8
;
253 p
->guard_interval
= GUARD_INTERVAL_1_4
;
256 switch((tmp
>> 2) & 3) {
258 p
->transmission_mode
= TRANSMISSION_MODE_2K
;
261 p
->transmission_mode
= TRANSMISSION_MODE_8K
;
264 printk(KERN_WARNING
"Unexpected value for transmission_mode\n");
267 tmp
= l64781_readreg(state
, 0x05);
270 p
->code_rate_HP
= FEC_1_2
;
273 p
->code_rate_HP
= FEC_2_3
;
276 p
->code_rate_HP
= FEC_3_4
;
279 p
->code_rate_HP
= FEC_5_6
;
282 p
->code_rate_HP
= FEC_7_8
;
285 printk("Unexpected value for code_rate_HP\n");
287 switch((tmp
>> 3) & 7) {
289 p
->code_rate_LP
= FEC_1_2
;
292 p
->code_rate_LP
= FEC_2_3
;
295 p
->code_rate_LP
= FEC_3_4
;
298 p
->code_rate_LP
= FEC_5_6
;
301 p
->code_rate_LP
= FEC_7_8
;
304 printk("Unexpected value for code_rate_LP\n");
307 tmp
= l64781_readreg(state
, 0x06);
310 p
->modulation
= QPSK
;
313 p
->modulation
= QAM_16
;
316 p
->modulation
= QAM_64
;
319 printk(KERN_WARNING
"Unexpected value for modulation\n");
321 switch((tmp
>> 2) & 7) {
323 p
->hierarchy
= HIERARCHY_NONE
;
326 p
->hierarchy
= HIERARCHY_1
;
329 p
->hierarchy
= HIERARCHY_2
;
332 p
->hierarchy
= HIERARCHY_4
;
335 printk("Unexpected value for hierarchy\n");
339 tmp
= l64781_readreg (state
, 0x1d);
340 p
->inversion
= (tmp
& 0x80) ? INVERSION_ON
: INVERSION_OFF
;
342 tmp
= (int) (l64781_readreg (state
, 0x08) |
343 (l64781_readreg (state
, 0x09) << 8) |
344 (l64781_readreg (state
, 0x0a) << 16));
350 static int l64781_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
352 struct l64781_state
* state
= fe
->demodulator_priv
;
353 int sync
= l64781_readreg (state
, 0x32);
354 int gain
= l64781_readreg (state
, 0x0e);
356 l64781_readreg (state
, 0x00); /* clear interrupt registers... */
357 l64781_readreg (state
, 0x01); /* dto. */
362 *status
|= FE_HAS_SIGNAL
;
364 if (sync
& 0x02) /* VCXO locked, this criteria should be ok */
365 *status
|= FE_HAS_CARRIER
;
368 *status
|= FE_HAS_VITERBI
;
371 *status
|= FE_HAS_SYNC
;
374 *status
|= FE_HAS_LOCK
;
379 static int l64781_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
381 struct l64781_state
* state
= fe
->demodulator_priv
;
383 /* XXX FIXME: set up counting period (reg 0x26...0x28)
385 *ber
= l64781_readreg (state
, 0x39)
386 | (l64781_readreg (state
, 0x3a) << 8);
391 static int l64781_read_signal_strength(struct dvb_frontend
* fe
, u16
* signal_strength
)
393 struct l64781_state
* state
= fe
->demodulator_priv
;
395 u8 gain
= l64781_readreg (state
, 0x0e);
396 *signal_strength
= (gain
<< 8) | gain
;
401 static int l64781_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
403 struct l64781_state
* state
= fe
->demodulator_priv
;
405 u8 avg_quality
= 0xff - l64781_readreg (state
, 0x33);
406 *snr
= (avg_quality
<< 8) | avg_quality
; /* not exact, but...*/
411 static int l64781_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
413 struct l64781_state
* state
= fe
->demodulator_priv
;
415 *ucblocks
= l64781_readreg (state
, 0x37)
416 | (l64781_readreg (state
, 0x38) << 8);
421 static int l64781_sleep(struct dvb_frontend
* fe
)
423 struct l64781_state
* state
= fe
->demodulator_priv
;
426 return l64781_writereg (state
, 0x3e, 0x5a);
429 static int l64781_init(struct dvb_frontend
* fe
)
431 struct l64781_state
* state
= fe
->demodulator_priv
;
433 reset_and_configure (state
);
436 l64781_writereg (state
, 0x3e, 0xa5);
439 l64781_writereg (state
, 0x2a, 0x04);
440 l64781_writereg (state
, 0x2a, 0x00);
442 /* Set tuner specific things */
443 /* AFC_POL, set also in reset_afc */
444 l64781_writereg (state
, 0x07, 0x8e);
446 /* Use internal ADC */
447 l64781_writereg (state
, 0x0b, 0x81);
449 /* AGC loop gain, and polarity is positive */
450 l64781_writereg (state
, 0x0c, 0x84);
452 /* Internal ADC outputs two's complement */
453 l64781_writereg (state
, 0x0d, 0x8c);
455 /* With ppm=8000, it seems the DTR_SENSITIVITY will result in
456 value of 2 with all possible bandwidths and guard
457 intervals, which is the initial value anyway. */
458 /*l64781_writereg (state, 0x19, 0x92);*/
460 /* Everything is two's complement, soft bit and CSI_OUT too */
461 l64781_writereg (state
, 0x1e, 0x09);
463 /* delay a bit after first init attempt */
472 static int l64781_get_tune_settings(struct dvb_frontend
* fe
,
473 struct dvb_frontend_tune_settings
* fesettings
)
475 fesettings
->min_delay_ms
= 4000;
476 fesettings
->step_size
= 0;
477 fesettings
->max_drift
= 0;
481 static void l64781_release(struct dvb_frontend
* fe
)
483 struct l64781_state
* state
= fe
->demodulator_priv
;
487 static const struct dvb_frontend_ops l64781_ops
;
489 struct dvb_frontend
* l64781_attach(const struct l64781_config
* config
,
490 struct i2c_adapter
* i2c
)
492 struct l64781_state
* state
= NULL
;
496 struct i2c_msg msg
[] = { { .addr
= config
->demod_address
, .flags
= 0, .buf
= b0
, .len
= 1 },
497 { .addr
= config
->demod_address
, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 } };
499 /* allocate memory for the internal state */
500 state
= kzalloc(sizeof(struct l64781_state
), GFP_KERNEL
);
501 if (state
== NULL
) goto error
;
503 /* setup the state */
504 state
->config
= config
;
509 * the L64781 won't show up before we send the reset_and_configure()
510 * broadcast. If nothing responds there is no L64781 on the bus...
512 if (reset_and_configure(state
) < 0) {
513 dprintk("No response to reset and configure broadcast...\n");
517 /* The chip always responds to reads */
518 if (i2c_transfer(state
->i2c
, msg
, 2) != 2) {
519 dprintk("No response to read on I2C bus\n");
523 /* Save current register contents for bailout */
524 reg0x3e
= l64781_readreg(state
, 0x3e);
526 /* Reading the POWER_DOWN register always returns 0 */
528 dprintk("Device doesn't look like L64781\n");
532 /* Turn the chip off */
533 l64781_writereg (state
, 0x3e, 0x5a);
535 /* Responds to all reads with 0 */
536 if (l64781_readreg(state
, 0x1a) != 0) {
537 dprintk("Read 1 returned unexpected value\n");
541 /* Turn the chip on */
542 l64781_writereg (state
, 0x3e, 0xa5);
544 /* Responds with register default value */
545 if (l64781_readreg(state
, 0x1a) != 0xa1) {
546 dprintk("Read 2 returned unexpected value\n");
550 /* create dvb_frontend */
551 memcpy(&state
->frontend
.ops
, &l64781_ops
, sizeof(struct dvb_frontend_ops
));
552 state
->frontend
.demodulator_priv
= state
;
553 return &state
->frontend
;
557 l64781_writereg (state
, 0x3e, reg0x3e
); /* restore reg 0x3e */
562 static const struct dvb_frontend_ops l64781_ops
= {
563 .delsys
= { SYS_DVBT
},
565 .name
= "LSI L64781 DVB-T",
566 /* .frequency_min_hz = ???,*/
567 /* .frequency_max_hz = ???,*/
568 .frequency_stepsize_hz
= 166666,
569 /* .symbol_rate_tolerance = ???,*/
570 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
571 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
|
572 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
|
576 .release
= l64781_release
,
579 .sleep
= l64781_sleep
,
581 .set_frontend
= apply_frontend_param
,
582 .get_frontend
= get_frontend
,
583 .get_tune_settings
= l64781_get_tune_settings
,
585 .read_status
= l64781_read_status
,
586 .read_ber
= l64781_read_ber
,
587 .read_signal_strength
= l64781_read_signal_strength
,
588 .read_snr
= l64781_read_snr
,
589 .read_ucblocks
= l64781_read_ucblocks
,
592 MODULE_DESCRIPTION("LSI L64781 DVB-T Demodulator driver");
593 MODULE_AUTHOR("Holger Waechtler, Marko Kohtala");
594 MODULE_LICENSE("GPL");
596 EXPORT_SYMBOL(l64781_attach
);