1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Driver for Philips TDA8083 based QPSK Demodulator
5 Copyright (C) 2001 Convergence Integrated Media GmbH
7 written by Ralph Metzler <ralph@convergence.de>
9 adoption to the new DVB frontend API and diagnostic ioctl's
10 by Holger Waechtler <holger@convergence.de>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/string.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
21 #include <media/dvb_frontend.h>
25 struct tda8083_state
{
26 struct i2c_adapter
* i2c
;
27 /* configuration settings */
28 const struct tda8083_config
* config
;
29 struct dvb_frontend frontend
;
33 #define dprintk(args...) \
35 if (debug) printk(KERN_DEBUG "tda8083: " args); \
39 static u8 tda8083_init_tab
[] = {
40 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea,
41 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10,
42 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8,
43 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00,
44 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00
49 static int tda8083_writereg (struct tda8083_state
* state
, u8 reg
, u8 data
)
52 u8 buf
[] = { reg
, data
};
53 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= buf
, .len
= 2 };
55 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
58 dprintk ("%s: writereg error (reg %02x, ret == %i)\n",
61 return (ret
!= 1) ? -1 : 0;
64 static int tda8083_readregs (struct tda8083_state
* state
, u8 reg1
, u8
*b
, u8 len
)
67 struct i2c_msg msg
[] = { { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= ®1
, .len
= 1 },
68 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= b
, .len
= len
} };
70 ret
= i2c_transfer(state
->i2c
, msg
, 2);
73 dprintk ("%s: readreg error (reg %02x, ret == %i)\n",
76 return ret
== 2 ? 0 : -1;
79 static inline u8
tda8083_readreg (struct tda8083_state
* state
, u8 reg
)
83 tda8083_readregs (state
, reg
, &val
, 1);
88 static int tda8083_set_inversion(struct tda8083_state
*state
,
89 enum fe_spectral_inversion inversion
)
91 /* XXX FIXME: implement other modes than FEC_AUTO */
92 if (inversion
== INVERSION_AUTO
)
98 static int tda8083_set_fec(struct tda8083_state
*state
, enum fe_code_rate fec
)
101 return tda8083_writereg (state
, 0x07, 0xff);
103 if (fec
>= FEC_1_2
&& fec
<= FEC_8_9
)
104 return tda8083_writereg (state
, 0x07, 1 << (FEC_8_9
- fec
));
109 static enum fe_code_rate
tda8083_get_fec(struct tda8083_state
*state
)
112 static enum fe_code_rate fec_tab
[] = {
113 FEC_8_9
, FEC_1_2
, FEC_2_3
, FEC_3_4
,
114 FEC_4_5
, FEC_5_6
, FEC_6_7
, FEC_7_8
117 index
= tda8083_readreg(state
, 0x0e) & 0x07;
119 return fec_tab
[index
];
122 static int tda8083_set_symbolrate (struct tda8083_state
* state
, u32 srate
)
128 if (srate
> 32000000)
134 if (srate
< 24000000)
136 if (srate
< 16000000)
142 tmp
= (tmp
% srate
) << 8;
143 ratio
= (ratio
<< 8) + tmp
/ srate
;
145 tmp
= (tmp
% srate
) << 8;
146 ratio
= (ratio
<< 8) + tmp
/ srate
;
148 dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio
);
150 tda8083_writereg (state
, 0x05, filter
);
151 tda8083_writereg (state
, 0x02, (ratio
>> 16) & 0xff);
152 tda8083_writereg (state
, 0x03, (ratio
>> 8) & 0xff);
153 tda8083_writereg (state
, 0x04, (ratio
) & 0xff);
155 tda8083_writereg (state
, 0x00, 0x3c);
156 tda8083_writereg (state
, 0x00, 0x04);
161 static void tda8083_wait_diseqc_fifo (struct tda8083_state
* state
, int timeout
)
163 unsigned long start
= jiffies
;
165 while (jiffies
- start
< timeout
&&
166 !(tda8083_readreg(state
, 0x02) & 0x80))
172 static int tda8083_set_tone(struct tda8083_state
*state
,
173 enum fe_sec_tone_mode tone
)
175 tda8083_writereg (state
, 0x26, 0xf1);
179 return tda8083_writereg (state
, 0x29, 0x00);
181 return tda8083_writereg (state
, 0x29, 0x80);
187 static int tda8083_set_voltage(struct tda8083_state
*state
,
188 enum fe_sec_voltage voltage
)
192 return tda8083_writereg (state
, 0x20, 0x00);
194 return tda8083_writereg (state
, 0x20, 0x11);
200 static int tda8083_send_diseqc_burst(struct tda8083_state
*state
,
201 enum fe_sec_mini_cmd burst
)
205 tda8083_writereg (state
, 0x29, (5 << 2)); /* send burst A */
208 tda8083_writereg (state
, 0x29, (7 << 2)); /* send B */
214 tda8083_wait_diseqc_fifo (state
, 100);
219 static int tda8083_send_diseqc_msg(struct dvb_frontend
*fe
,
220 struct dvb_diseqc_master_cmd
*m
)
222 struct tda8083_state
* state
= fe
->demodulator_priv
;
225 tda8083_writereg (state
, 0x29, (m
->msg_len
- 3) | (1 << 2)); /* enable */
227 for (i
=0; i
<m
->msg_len
; i
++)
228 tda8083_writereg (state
, 0x23 + i
, m
->msg
[i
]);
230 tda8083_writereg (state
, 0x29, (m
->msg_len
- 3) | (3 << 2)); /* send!! */
232 tda8083_wait_diseqc_fifo (state
, 100);
237 static int tda8083_read_status(struct dvb_frontend
*fe
,
238 enum fe_status
*status
)
240 struct tda8083_state
* state
= fe
->demodulator_priv
;
242 u8 signal
= ~tda8083_readreg (state
, 0x01);
243 u8 sync
= tda8083_readreg (state
, 0x02);
248 *status
|= FE_HAS_SIGNAL
;
251 *status
|= FE_HAS_CARRIER
;
254 *status
|= FE_HAS_VITERBI
;
257 *status
|= FE_HAS_SYNC
;
259 if (sync
& 0x20) /* frontend can not lock */
260 *status
|= FE_TIMEDOUT
;
262 if ((sync
& 0x1f) == 0x1f)
263 *status
|= FE_HAS_LOCK
;
268 static int tda8083_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
270 struct tda8083_state
* state
= fe
->demodulator_priv
;
274 if ((ret
= tda8083_readregs(state
, 0x0b, buf
, sizeof(buf
))))
277 *ber
= ((buf
[0] & 0x1f) << 16) | (buf
[1] << 8) | buf
[2];
282 static int tda8083_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
284 struct tda8083_state
* state
= fe
->demodulator_priv
;
286 u8 signal
= ~tda8083_readreg (state
, 0x01);
287 *strength
= (signal
<< 8) | signal
;
292 static int tda8083_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
294 struct tda8083_state
* state
= fe
->demodulator_priv
;
296 u8 _snr
= tda8083_readreg (state
, 0x08);
297 *snr
= (_snr
<< 8) | _snr
;
302 static int tda8083_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
304 struct tda8083_state
* state
= fe
->demodulator_priv
;
306 *ucblocks
= tda8083_readreg(state
, 0x0f);
307 if (*ucblocks
== 0xff)
308 *ucblocks
= 0xffffffff;
313 static int tda8083_set_frontend(struct dvb_frontend
*fe
)
315 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
316 struct tda8083_state
* state
= fe
->demodulator_priv
;
318 if (fe
->ops
.tuner_ops
.set_params
) {
319 fe
->ops
.tuner_ops
.set_params(fe
);
320 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
323 tda8083_set_inversion (state
, p
->inversion
);
324 tda8083_set_fec(state
, p
->fec_inner
);
325 tda8083_set_symbolrate(state
, p
->symbol_rate
);
327 tda8083_writereg (state
, 0x00, 0x3c);
328 tda8083_writereg (state
, 0x00, 0x04);
333 static int tda8083_get_frontend(struct dvb_frontend
*fe
,
334 struct dtv_frontend_properties
*p
)
336 struct tda8083_state
* state
= fe
->demodulator_priv
;
338 /* FIXME: get symbolrate & frequency offset...*/
339 /*p->frequency = ???;*/
340 p
->inversion
= (tda8083_readreg (state
, 0x0e) & 0x80) ?
341 INVERSION_ON
: INVERSION_OFF
;
342 p
->fec_inner
= tda8083_get_fec(state
);
343 /*p->symbol_rate = tda8083_get_symbolrate (state);*/
348 static int tda8083_sleep(struct dvb_frontend
* fe
)
350 struct tda8083_state
* state
= fe
->demodulator_priv
;
352 tda8083_writereg (state
, 0x00, 0x02);
356 static int tda8083_init(struct dvb_frontend
* fe
)
358 struct tda8083_state
* state
= fe
->demodulator_priv
;
362 tda8083_writereg (state
, i
, tda8083_init_tab
[i
]);
364 tda8083_writereg (state
, 0x00, 0x3c);
365 tda8083_writereg (state
, 0x00, 0x04);
370 static int tda8083_diseqc_send_burst(struct dvb_frontend
*fe
,
371 enum fe_sec_mini_cmd burst
)
373 struct tda8083_state
* state
= fe
->demodulator_priv
;
375 tda8083_send_diseqc_burst (state
, burst
);
376 tda8083_writereg (state
, 0x00, 0x3c);
377 tda8083_writereg (state
, 0x00, 0x04);
382 static int tda8083_diseqc_set_tone(struct dvb_frontend
*fe
,
383 enum fe_sec_tone_mode tone
)
385 struct tda8083_state
* state
= fe
->demodulator_priv
;
387 tda8083_set_tone (state
, tone
);
388 tda8083_writereg (state
, 0x00, 0x3c);
389 tda8083_writereg (state
, 0x00, 0x04);
394 static int tda8083_diseqc_set_voltage(struct dvb_frontend
*fe
,
395 enum fe_sec_voltage voltage
)
397 struct tda8083_state
* state
= fe
->demodulator_priv
;
399 tda8083_set_voltage (state
, voltage
);
400 tda8083_writereg (state
, 0x00, 0x3c);
401 tda8083_writereg (state
, 0x00, 0x04);
406 static void tda8083_release(struct dvb_frontend
* fe
)
408 struct tda8083_state
* state
= fe
->demodulator_priv
;
412 static const struct dvb_frontend_ops tda8083_ops
;
414 struct dvb_frontend
* tda8083_attach(const struct tda8083_config
* config
,
415 struct i2c_adapter
* i2c
)
417 struct tda8083_state
* state
= NULL
;
419 /* allocate memory for the internal state */
420 state
= kzalloc(sizeof(struct tda8083_state
), GFP_KERNEL
);
421 if (state
== NULL
) goto error
;
423 /* setup the state */
424 state
->config
= config
;
427 /* check if the demod is there */
428 if ((tda8083_readreg(state
, 0x00)) != 0x05) goto error
;
430 /* create dvb_frontend */
431 memcpy(&state
->frontend
.ops
, &tda8083_ops
, sizeof(struct dvb_frontend_ops
));
432 state
->frontend
.demodulator_priv
= state
;
433 return &state
->frontend
;
440 static const struct dvb_frontend_ops tda8083_ops
= {
441 .delsys
= { SYS_DVBS
},
443 .name
= "Philips TDA8083 DVB-S",
444 .frequency_min_hz
= 920 * MHz
, /* TDA8060 */
445 .frequency_max_hz
= 2200 * MHz
, /* TDA8060 */
446 .frequency_stepsize_hz
= 125 * kHz
,
447 .symbol_rate_min
= 12000000,
448 .symbol_rate_max
= 30000000,
449 /* .symbol_rate_tolerance = ???,*/
450 .caps
= FE_CAN_INVERSION_AUTO
|
451 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
452 FE_CAN_FEC_4_5
| FE_CAN_FEC_5_6
| FE_CAN_FEC_6_7
|
453 FE_CAN_FEC_7_8
| FE_CAN_FEC_8_9
| FE_CAN_FEC_AUTO
|
454 FE_CAN_QPSK
| FE_CAN_MUTE_TS
457 .release
= tda8083_release
,
459 .init
= tda8083_init
,
460 .sleep
= tda8083_sleep
,
462 .set_frontend
= tda8083_set_frontend
,
463 .get_frontend
= tda8083_get_frontend
,
465 .read_status
= tda8083_read_status
,
466 .read_signal_strength
= tda8083_read_signal_strength
,
467 .read_snr
= tda8083_read_snr
,
468 .read_ber
= tda8083_read_ber
,
469 .read_ucblocks
= tda8083_read_ucblocks
,
471 .diseqc_send_master_cmd
= tda8083_send_diseqc_msg
,
472 .diseqc_send_burst
= tda8083_diseqc_send_burst
,
473 .set_tone
= tda8083_diseqc_set_tone
,
474 .set_voltage
= tda8083_diseqc_set_voltage
,
477 module_param(debug
, int, 0644);
478 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
480 MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
481 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
482 MODULE_LICENSE("GPL");
484 EXPORT_SYMBOL(tda8083_attach
);