1 // SPDX-License-Identifier: GPL-2.0-or-later
3 VES1820 - Single Chip Cable Channel Receiver driver module
5 Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
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 <asm/div64.h>
18 #include <media/dvb_frontend.h>
23 struct ves1820_state
{
24 struct i2c_adapter
* i2c
;
25 /* configuration settings */
26 const struct ves1820_config
* config
;
27 struct dvb_frontend frontend
;
29 /* private demodulator data */
37 static u8 ves1820_inittab
[] = {
38 0x69, 0x6A, 0x93, 0x1A, 0x12, 0x46, 0x26, 0x1A,
39 0x43, 0x6A, 0xAA, 0xAA, 0x1E, 0x85, 0x43, 0x20,
40 0xE0, 0x00, 0xA1, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
42 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x40
47 static int ves1820_writereg(struct ves1820_state
*state
, u8 reg
, u8 data
)
49 u8 buf
[] = { 0x00, reg
, data
};
50 struct i2c_msg msg
= {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= buf
,.len
= 3 };
53 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
56 printk("ves1820: %s(): writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
57 __func__
, reg
, data
, ret
);
59 return (ret
!= 1) ? -EREMOTEIO
: 0;
62 static u8
ves1820_readreg(struct ves1820_state
*state
, u8 reg
)
64 u8 b0
[] = { 0x00, reg
};
66 struct i2c_msg msg
[] = {
67 {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= b0
,.len
= 2},
68 {.addr
= state
->config
->demod_address
,.flags
= I2C_M_RD
,.buf
= b1
,.len
= 1}
72 ret
= i2c_transfer(state
->i2c
, msg
, 2);
75 printk("ves1820: %s(): readreg error (reg == 0x%02x, ret == %i)\n",
81 static int ves1820_setup_reg0(struct ves1820_state
*state
,
82 u8 reg0
, enum fe_spectral_inversion inversion
)
84 reg0
|= state
->reg0
& 0x62;
86 if (INVERSION_ON
== inversion
) {
87 if (!state
->config
->invert
) reg0
|= 0x20;
89 } else if (INVERSION_OFF
== inversion
) {
90 if (!state
->config
->invert
) reg0
&= ~0x20;
94 ves1820_writereg(state
, 0x00, reg0
& 0xfe);
95 ves1820_writereg(state
, 0x00, reg0
| 0x01);
102 static int ves1820_set_symbolrate(struct ves1820_state
*state
, u32 symbolrate
)
114 if (symbolrate
> state
->config
->xin
/ 2)
115 symbolrate
= state
->config
->xin
/ 2;
117 if (symbolrate
< 500000)
120 if (symbolrate
< state
->config
->xin
/ 16)
122 if (symbolrate
< state
->config
->xin
/ 32)
124 if (symbolrate
< state
->config
->xin
/ 64)
128 fpxin
= state
->config
->xin
* 10ULL;
129 fptmp
= fpxin
; do_div(fptmp
, 123);
130 if (symbolrate
< fptmp
)
132 fptmp
= fpxin
; do_div(fptmp
, 160);
133 if (symbolrate
< fptmp
)
135 fptmp
= fpxin
; do_div(fptmp
, 246);
136 if (symbolrate
< fptmp
)
138 fptmp
= fpxin
; do_div(fptmp
, 320);
139 if (symbolrate
< fptmp
)
141 fptmp
= fpxin
; do_div(fptmp
, 492);
142 if (symbolrate
< fptmp
)
144 fptmp
= fpxin
; do_div(fptmp
, 640);
145 if (symbolrate
< fptmp
)
147 fptmp
= fpxin
; do_div(fptmp
, 984);
148 if (symbolrate
< fptmp
)
151 fin
= state
->config
->xin
>> 4;
153 ratio
= (symbolrate
<< 4) / fin
;
154 tmp
= ((symbolrate
<< 4) % fin
) << 8;
155 ratio
= (ratio
<< 8) + tmp
/ fin
;
156 tmp
= (tmp
% fin
) << 8;
157 ratio
= (ratio
<< 8) + DIV_ROUND_CLOSEST(tmp
, fin
);
160 BDRI
= (((state
->config
->xin
<< 5) / symbolrate
) + 1) / 2;
165 SFIL
= (SFIL
<< 4) | ves1820_inittab
[0x0E];
167 NDEC
= (NDEC
<< 6) | ves1820_inittab
[0x03];
169 ves1820_writereg(state
, 0x03, NDEC
);
170 ves1820_writereg(state
, 0x0a, BDR
& 0xff);
171 ves1820_writereg(state
, 0x0b, (BDR
>> 8) & 0xff);
172 ves1820_writereg(state
, 0x0c, (BDR
>> 16) & 0x3f);
174 ves1820_writereg(state
, 0x0d, BDRI
);
175 ves1820_writereg(state
, 0x0e, SFIL
);
180 static int ves1820_init(struct dvb_frontend
* fe
)
182 struct ves1820_state
* state
= fe
->demodulator_priv
;
185 ves1820_writereg(state
, 0, 0);
187 for (i
= 0; i
< sizeof(ves1820_inittab
); i
++)
188 ves1820_writereg(state
, i
, ves1820_inittab
[i
]);
189 if (state
->config
->selagc
)
190 ves1820_writereg(state
, 2, ves1820_inittab
[2] | 0x08);
192 ves1820_writereg(state
, 0x34, state
->pwm
);
197 static int ves1820_set_parameters(struct dvb_frontend
*fe
)
199 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
200 struct ves1820_state
* state
= fe
->demodulator_priv
;
201 static const u8 reg0x00
[] = { 0x00, 0x04, 0x08, 0x0c, 0x10 };
202 static const u8 reg0x01
[] = { 140, 140, 106, 100, 92 };
203 static const u8 reg0x05
[] = { 135, 100, 70, 54, 38 };
204 static const u8 reg0x08
[] = { 162, 116, 67, 52, 35 };
205 static const u8 reg0x09
[] = { 145, 150, 106, 126, 107 };
206 int real_qam
= p
->modulation
- QAM_16
;
208 if (real_qam
< 0 || real_qam
> 4)
211 if (fe
->ops
.tuner_ops
.set_params
) {
212 fe
->ops
.tuner_ops
.set_params(fe
);
213 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
216 ves1820_set_symbolrate(state
, p
->symbol_rate
);
217 ves1820_writereg(state
, 0x34, state
->pwm
);
219 ves1820_writereg(state
, 0x01, reg0x01
[real_qam
]);
220 ves1820_writereg(state
, 0x05, reg0x05
[real_qam
]);
221 ves1820_writereg(state
, 0x08, reg0x08
[real_qam
]);
222 ves1820_writereg(state
, 0x09, reg0x09
[real_qam
]);
224 ves1820_setup_reg0(state
, reg0x00
[real_qam
], p
->inversion
);
225 ves1820_writereg(state
, 2, ves1820_inittab
[2] | (state
->config
->selagc
? 0x08 : 0));
229 static int ves1820_read_status(struct dvb_frontend
*fe
,
230 enum fe_status
*status
)
232 struct ves1820_state
* state
= fe
->demodulator_priv
;
236 sync
= ves1820_readreg(state
, 0x11);
239 *status
|= FE_HAS_SIGNAL
;
242 *status
|= FE_HAS_CARRIER
;
244 if (sync
& 2) /* XXX FIXME! */
245 *status
|= FE_HAS_VITERBI
;
248 *status
|= FE_HAS_SYNC
;
251 *status
|= FE_HAS_LOCK
;
256 static int ves1820_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
258 struct ves1820_state
* state
= fe
->demodulator_priv
;
260 u32 _ber
= ves1820_readreg(state
, 0x14) |
261 (ves1820_readreg(state
, 0x15) << 8) |
262 ((ves1820_readreg(state
, 0x16) & 0x0f) << 16);
268 static int ves1820_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
270 struct ves1820_state
* state
= fe
->demodulator_priv
;
272 u8 gain
= ves1820_readreg(state
, 0x17);
273 *strength
= (gain
<< 8) | gain
;
278 static int ves1820_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
280 struct ves1820_state
* state
= fe
->demodulator_priv
;
282 u8 quality
= ~ves1820_readreg(state
, 0x18);
283 *snr
= (quality
<< 8) | quality
;
288 static int ves1820_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
290 struct ves1820_state
* state
= fe
->demodulator_priv
;
292 *ucblocks
= ves1820_readreg(state
, 0x13) & 0x7f;
293 if (*ucblocks
== 0x7f)
294 *ucblocks
= 0xffffffff;
296 /* reset uncorrected block counter */
297 ves1820_writereg(state
, 0x10, ves1820_inittab
[0x10] & 0xdf);
298 ves1820_writereg(state
, 0x10, ves1820_inittab
[0x10]);
303 static int ves1820_get_frontend(struct dvb_frontend
*fe
,
304 struct dtv_frontend_properties
*p
)
306 struct ves1820_state
* state
= fe
->demodulator_priv
;
310 sync
= ves1820_readreg(state
, 0x11);
311 afc
= ves1820_readreg(state
, 0x19);
313 /* AFC only valid when carrier has been recovered */
314 printk(sync
& 2 ? "ves1820: AFC (%d) %dHz\n" :
315 "ves1820: [AFC (%d) %dHz]\n", afc
, -((s32
) p
->symbol_rate
* afc
) >> 10);
318 if (!state
->config
->invert
) {
319 p
->inversion
= (state
->reg0
& 0x20) ? INVERSION_ON
: INVERSION_OFF
;
321 p
->inversion
= (!(state
->reg0
& 0x20)) ? INVERSION_ON
: INVERSION_OFF
;
324 p
->modulation
= ((state
->reg0
>> 2) & 7) + QAM_16
;
326 p
->fec_inner
= FEC_NONE
;
328 p
->frequency
= ((p
->frequency
+ 31250) / 62500) * 62500;
330 p
->frequency
-= ((s32
) p
->symbol_rate
* afc
) >> 10;
335 static int ves1820_sleep(struct dvb_frontend
* fe
)
337 struct ves1820_state
* state
= fe
->demodulator_priv
;
339 ves1820_writereg(state
, 0x1b, 0x02); /* pdown ADC */
340 ves1820_writereg(state
, 0x00, 0x80); /* standby */
345 static int ves1820_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
* fesettings
)
348 fesettings
->min_delay_ms
= 200;
349 fesettings
->step_size
= 0;
350 fesettings
->max_drift
= 0;
354 static void ves1820_release(struct dvb_frontend
* fe
)
356 struct ves1820_state
* state
= fe
->demodulator_priv
;
360 static const struct dvb_frontend_ops ves1820_ops
;
362 struct dvb_frontend
* ves1820_attach(const struct ves1820_config
* config
,
363 struct i2c_adapter
* i2c
,
366 struct ves1820_state
* state
= NULL
;
368 /* allocate memory for the internal state */
369 state
= kzalloc(sizeof(struct ves1820_state
), GFP_KERNEL
);
373 /* setup the state */
374 state
->reg0
= ves1820_inittab
[0];
375 state
->config
= config
;
379 /* check if the demod is there */
380 if ((ves1820_readreg(state
, 0x1a) & 0xf0) != 0x70)
384 printk("ves1820: pwm=0x%02x\n", state
->pwm
);
386 /* create dvb_frontend */
387 memcpy(&state
->frontend
.ops
, &ves1820_ops
, sizeof(struct dvb_frontend_ops
));
388 state
->frontend
.ops
.info
.symbol_rate_min
= (state
->config
->xin
/ 2) / 64; /* SACLK/64 == (XIN/2)/64 */
389 state
->frontend
.ops
.info
.symbol_rate_max
= (state
->config
->xin
/ 2) / 4; /* SACLK/4 */
390 state
->frontend
.demodulator_priv
= state
;
392 return &state
->frontend
;
399 static const struct dvb_frontend_ops ves1820_ops
= {
400 .delsys
= { SYS_DVBC_ANNEX_A
},
402 .name
= "VLSI VES1820 DVB-C",
403 .frequency_min_hz
= 47 * MHz
,
404 .frequency_max_hz
= 862 * MHz
,
405 .frequency_stepsize_hz
= 62500,
406 .caps
= FE_CAN_QAM_16
|
414 .release
= ves1820_release
,
416 .init
= ves1820_init
,
417 .sleep
= ves1820_sleep
,
419 .set_frontend
= ves1820_set_parameters
,
420 .get_frontend
= ves1820_get_frontend
,
421 .get_tune_settings
= ves1820_get_tune_settings
,
423 .read_status
= ves1820_read_status
,
424 .read_ber
= ves1820_read_ber
,
425 .read_signal_strength
= ves1820_read_signal_strength
,
426 .read_snr
= ves1820_read_snr
,
427 .read_ucblocks
= ves1820_read_ucblocks
,
430 module_param(verbose
, int, 0644);
431 MODULE_PARM_DESC(verbose
, "print AFC offset after tuning for debugging the PWM setting");
433 MODULE_DESCRIPTION("VLSI VES1820 DVB-C Demodulator driver");
434 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
435 MODULE_LICENSE("GPL");
437 EXPORT_SYMBOL(ves1820_attach
);