11 #define SP887X_DEFAULT_FIRMWARE "dvb-fe-sp887x.fw"
14 #include <linux/module.h>
15 #include <linux/device.h>
17 #include <linux/string.h>
18 #include <linux/slab.h>
34 #define dprintk(args...) \
36 if (debug) printk(KERN_DEBUG "sp887x: " args); \
41 struct i2c_msg msg = { .
addr = state->
config->demod_address, .flags = 0, .buf =
buf, .len = len };
45 printk (
"%s: i2c write error (addr %02x, err == %i)\n",
46 __func__, state->
config->demod_address, err);
55 u8 b0 [] = { reg >> 8 , reg & 0xff, data >> 8, data & 0xff };
56 struct i2c_msg msg = { .addr = state->
config->demod_address, .flags = 0, .buf = b0, .len = 4 };
63 if (!(reg == 0xf1a && data == 0x000 &&
66 printk(
"%s: writereg error "
67 "(reg %03x, data %03x, ret == %i)\n",
68 __func__, reg & 0xffff, data & 0xffff, ret);
78 u8 b0 [] = { reg >> 8 , reg & 0xff };
81 struct i2c_msg msg[] = {{ .
addr = state->
config->demod_address, .flags = 0, .buf = b0, .len = 2 },
82 { .addr = state->
config->demod_address, .flags =
I2C_M_RD, .buf = b1, .len = 2 }};
85 printk(
"%s: readreg error (ret == %i)\n", __func__, ret);
89 return (((b1[0] << 8) | b1[1]) & 0xfff);
92 static void sp887x_microcontroller_stop (
struct sp887x_state* state)
95 sp887x_writereg(state, 0xf08, 0x000);
96 sp887x_writereg(state, 0xf09, 0x000);
99 sp887x_writereg(state, 0xf00, 0x000);
102 static void sp887x_microcontroller_start (
struct sp887x_state* state)
105 sp887x_writereg(state, 0xf08, 0x000);
106 sp887x_writereg(state, 0xf09, 0x000);
109 sp887x_writereg(state, 0xf00, 0x001);
112 static void sp887x_setup_agc (
struct sp887x_state* state)
116 sp887x_writereg(state, 0x33c, 0x054);
117 sp887x_writereg(state, 0x33b, 0x04c);
118 sp887x_writereg(state, 0x328, 0x000);
119 sp887x_writereg(state, 0x327, 0x005);
120 sp887x_writereg(state, 0x326, 0x001);
121 sp887x_writereg(state, 0x325, 0x001);
122 sp887x_writereg(state, 0x324, 0x001);
123 sp887x_writereg(state, 0x318, 0x050);
124 sp887x_writereg(state, 0x317, 0x3fe);
125 sp887x_writereg(state, 0x316, 0x001);
126 sp887x_writereg(state, 0x313, 0x005);
127 sp887x_writereg(state, 0x312, 0x002);
128 sp887x_writereg(state, 0x306, 0x000);
129 sp887x_writereg(state, 0x303, 0x000);
133 #define FW_SIZE 0x4000
142 int fw_size = fw->
size;
143 const unsigned char *
mem = fw->
data;
154 sp887x_writereg(state, 0xf1a, 0x000);
156 sp887x_microcontroller_stop (state);
158 printk (
"%s: firmware upload... ", __func__);
162 sp887x_writereg(state, 0x8f08, 0x1fff);
165 sp887x_writereg(state, 0x8f0a, 0x0000);
180 memcpy(&buf[2], mem + i, c);
182 if ((err = i2c_writebytes (state, buf, c+2)) < 0) {
184 printk (
"%s: i2c error (err == %i)\n", __func__, err);
190 sp887x_writereg(state, 0xc13, 0x001);
193 sp887x_writereg(state, 0xc14, 0x000);
196 sp887x_writereg(state, 0xc1a, 0x872);
197 sp887x_writereg(state, 0xc1b, 0x001);
198 sp887x_writereg(state, 0xc1c, 0x000);
199 sp887x_writereg(state, 0xc1a, 0x871);
202 sp887x_writereg(state, 0x301, 0x002);
204 sp887x_setup_agc(state);
207 sp887x_writereg(state, 0xd00, 0x010);
208 sp887x_writereg(state, 0x0d1, 0x000);
214 int known_parameters = 1;
222 *reg0xc05 |= (1 << 10);
225 *reg0xc05 |= (2 << 10);
228 known_parameters = 0;
238 *reg0xc05 |= (1 << 7);
241 *reg0xc05 |= (2 << 7);
244 *reg0xc05 |= (3 << 7);
247 known_parameters = 0;
257 *reg0xc05 |= (1 << 3);
260 *reg0xc05 |= (2 << 3);
263 *reg0xc05 |= (3 << 3);
266 *reg0xc05 |= (4 << 3);
269 known_parameters = 0;
275 if (known_parameters)
276 *reg0xc05 |= (2 << 1);
278 *reg0xc05 |= (1 << 1);
287 static void divide (
int n,
int d,
int *quotient_i,
int *quotient_f)
299 q = (q << 8) | (r / d);
301 *quotient_f = (q << 8) | (r / d);
305 static void sp887x_correct_offsets (
struct sp887x_state* state,
309 static const u32 srate_correction [] = { 1879617, 4544878, 8098561 };
311 int freq_offset = actual_freq - p->
frequency;
312 int sysclock = 61003;
313 int ifreq = 36000000;
331 freq = ifreq - freq_offset;
333 freq = ifreq + freq_offset;
335 divide(freq / 333, sysclock,
NULL, &frequency_shift);
338 frequency_shift = -frequency_shift;
341 sp887x_writereg(state, 0x319, srate_correction[bw_index] >> 12);
342 sp887x_writereg(state, 0x31a, srate_correction[bw_index] & 0xfff);
345 sp887x_writereg(state, 0x309, frequency_shift >> 12);
346 sp887x_writereg(state, 0x30a, frequency_shift & 0xfff);
349 static int sp887x_setup_frontend_parameters(
struct dvb_frontend *fe)
353 unsigned actual_freq;
362 if ((err = configure_reg0xc05(p, ®0xc05)))
365 sp887x_microcontroller_stop(state);
368 if (fe->
ops.tuner_ops.set_params) {
369 fe->
ops.tuner_ops.set_params(fe);
370 if (fe->
ops.i2c_gate_ctrl) fe->
ops.i2c_gate_ctrl(fe, 0);
372 if (fe->
ops.tuner_ops.get_frequency) {
373 fe->
ops.tuner_ops.get_frequency(fe, &actual_freq);
374 if (fe->
ops.i2c_gate_ctrl) fe->
ops.i2c_gate_ctrl(fe, 0);
380 sp887x_readreg(state, 0x200);
382 sp887x_correct_offsets(state, p, actual_freq);
392 sp887x_writereg(state, 0x311, val);
396 sp887x_writereg(state, 0x338, 0x000);
398 sp887x_writereg(state, 0x338, 0x001);
400 sp887x_writereg(state, 0xc05, reg0xc05);
412 sp887x_writereg(state, 0xf14, 0x160 | val);
413 sp887x_writereg(state, 0xf15, 0x000);
415 sp887x_microcontroller_start(state);
422 u16 snr12 = sp887x_readreg(state, 0xf16);
423 u16 sync0x200 = sp887x_readreg(state, 0x200);
424 u16 sync0xf17 = sp887x_readreg(state, 0xf17);
437 if ((sync0xf17 & 0x00f) == 0x002) {
442 if (sync0x200 & 0x001) {
443 int steps = (sync0x200 >> 4) & 0x00f;
446 dprintk(
"sp887x: implement tuner adjustment (%+i steps)!!\n",
457 *ber = (sp887x_readreg(state, 0xc08) & 0x3f) |
458 (sp887x_readreg(state, 0xc07) << 6);
459 sp887x_writereg(state, 0xc08, 0x000);
460 sp887x_writereg(state, 0xc07, 0x000);
467 static int sp887x_read_signal_strength(
struct dvb_frontend* fe,
u16* strength)
471 u16 snr12 = sp887x_readreg(state, 0xf16);
472 u32 signal = 3 * (snr12 << 4);
473 *strength = (signal < 0xffff) ? signal : 0xffff;
482 u16 snr12 = sp887x_readreg(state, 0xf16);
483 *snr = (snr12 << 4) | (snr12 >> 8);
488 static int sp887x_read_ucblocks(
struct dvb_frontend* fe,
u32* ucblocks)
492 *ucblocks = sp887x_readreg(state, 0xc0c);
493 if (*ucblocks == 0xfff)
504 return sp887x_writereg(state, 0x206, 0x001);
506 return sp887x_writereg(state, 0x206, 0x000);
515 sp887x_writereg(state, 0xc18, 0x000);
531 printk(
"sp887x: no firmware upload (timeout or file not found?)\n");
535 ret = sp887x_initial_setup(fe, fw);
538 printk(
"sp887x: writing firmware to device failed\n");
541 printk(
"sp887x: firmware upload complete\n");
546 sp887x_writereg(state, 0xc18, 0x00d);
582 if (sp887x_readreg(state, 0x0200) < 0)
goto error;
597 .name =
"Spase SP887x DVB-T",
598 .frequency_min = 50500000,
599 .frequency_max = 858000000,
600 .frequency_stepsize = 166666,
607 .release = sp887x_release,
610 .sleep = sp887x_sleep,
611 .i2c_gate_ctrl = sp887x_i2c_gate_ctrl,
613 .set_frontend = sp887x_setup_frontend_parameters,
614 .get_tune_settings = sp887x_get_tune_settings,
616 .read_status = sp887x_read_status,
617 .read_ber = sp887x_read_ber,
618 .read_signal_strength = sp887x_read_signal_strength,
619 .read_snr = sp887x_read_snr,
620 .read_ucblocks = sp887x_read_ucblocks,