2 Driver for Spase SP8870 demodulator
4 Copyright (C) 1999 Juergen Peitz
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 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 * This driver needs external firmware. Please use the command
24 * "<kerneldir>/Documentation/dvb/get_dvb_firmware alps_tdlb7" to
25 * download/extract it, and then copy it to /usr/lib/hotplug/firmware
26 * or /lib/firmware (depending on configuration of firmware hotplug).
28 #define SP8870_DEFAULT_FIRMWARE "dvb-fe-sp8870.fw"
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/device.h>
33 #include <linux/firmware.h>
34 #include <linux/delay.h>
35 #include <linux/string.h>
36 #include <linux/slab.h>
38 #include "dvb_frontend.h"
44 struct i2c_adapter
* i2c
;
46 const struct sp8870_config
* config
;
48 struct dvb_frontend frontend
;
50 /* demodulator private data */
55 #define dprintk(args...) \
57 if (debug) printk(KERN_DEBUG "sp8870: " args); \
60 /* firmware size for sp8870 */
61 #define SP8870_FIRMWARE_SIZE 16382
63 /* starting point for firmware in file 'Sc_main.mc' */
64 #define SP8870_FIRMWARE_OFFSET 0x0A
66 static int sp8870_writereg (struct sp8870_state
* state
, u16 reg
, u16 data
)
68 u8 buf
[] = { reg
>> 8, reg
& 0xff, data
>> 8, data
& 0xff };
69 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= buf
, .len
= 4 };
72 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
73 dprintk ("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __func__
, err
, reg
, data
);
80 static int sp8870_readreg (struct sp8870_state
* state
, u16 reg
)
83 u8 b0
[] = { reg
>> 8 , reg
& 0xff };
85 struct i2c_msg msg
[] = { { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= b0
, .len
= 2 },
86 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 2 } };
88 ret
= i2c_transfer (state
->i2c
, msg
, 2);
91 dprintk("%s: readreg error (ret == %i)\n", __func__
, ret
);
95 return (b1
[0] << 8 | b1
[1]);
98 static int sp8870_firmware_upload (struct sp8870_state
* state
, const struct firmware
*fw
)
101 const char *fw_buf
= fw
->data
;
107 dprintk ("%s: ...\n", __func__
);
109 if (fw
->size
< SP8870_FIRMWARE_SIZE
+ SP8870_FIRMWARE_OFFSET
)
112 // system controller stop
113 sp8870_writereg(state
, 0x0F00, 0x0000);
115 // instruction RAM register hiword
116 sp8870_writereg(state
, 0x8F08, ((SP8870_FIRMWARE_SIZE
/ 2) & 0xFFFF));
118 // instruction RAM MWR
119 sp8870_writereg(state
, 0x8F0A, ((SP8870_FIRMWARE_SIZE
/ 2) >> 16));
121 // do firmware upload
122 fw_pos
= SP8870_FIRMWARE_OFFSET
;
123 while (fw_pos
< SP8870_FIRMWARE_SIZE
+ SP8870_FIRMWARE_OFFSET
){
124 tx_len
= (fw_pos
<= SP8870_FIRMWARE_SIZE
+ SP8870_FIRMWARE_OFFSET
- 252) ? 252 : SP8870_FIRMWARE_SIZE
+ SP8870_FIRMWARE_OFFSET
- fw_pos
;
125 // write register 0xCF0A
128 memcpy(&tx_buf
[2], fw_buf
+ fw_pos
, tx_len
);
129 msg
.addr
= state
->config
->demod_address
;
132 msg
.len
= tx_len
+ 2;
133 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
134 printk("%s: firmware upload failed!\n", __func__
);
135 printk ("%s: i2c error (err == %i)\n", __func__
, err
);
141 dprintk ("%s: done!\n", __func__
);
145 static void sp8870_microcontroller_stop (struct sp8870_state
* state
)
147 sp8870_writereg(state
, 0x0F08, 0x000);
148 sp8870_writereg(state
, 0x0F09, 0x000);
150 // microcontroller STOP
151 sp8870_writereg(state
, 0x0F00, 0x000);
154 static void sp8870_microcontroller_start (struct sp8870_state
* state
)
156 sp8870_writereg(state
, 0x0F08, 0x000);
157 sp8870_writereg(state
, 0x0F09, 0x000);
159 // microcontroller START
160 sp8870_writereg(state
, 0x0F00, 0x001);
161 // not documented but if we don't read 0x0D01 out here
162 // we don't get a correct data valid signal
163 sp8870_readreg(state
, 0x0D01);
166 static int sp8870_read_data_valid_signal(struct sp8870_state
* state
)
168 return (sp8870_readreg(state
, 0x0D02) > 0);
171 static int configure_reg0xc05 (struct dtv_frontend_properties
*p
, u16
*reg0xc05
)
173 int known_parameters
= 1;
177 switch (p
->modulation
) {
181 *reg0xc05
|= (1 << 10);
184 *reg0xc05
|= (2 << 10);
187 known_parameters
= 0;
193 switch (p
->hierarchy
) {
197 *reg0xc05
|= (1 << 7);
200 *reg0xc05
|= (2 << 7);
203 *reg0xc05
|= (3 << 7);
206 known_parameters
= 0;
212 switch (p
->code_rate_HP
) {
216 *reg0xc05
|= (1 << 3);
219 *reg0xc05
|= (2 << 3);
222 *reg0xc05
|= (3 << 3);
225 *reg0xc05
|= (4 << 3);
228 known_parameters
= 0;
234 if (known_parameters
)
235 *reg0xc05
|= (2 << 1); /* use specified parameters */
237 *reg0xc05
|= (1 << 1); /* enable autoprobing */
242 static int sp8870_wake_up(struct sp8870_state
* state
)
244 // enable TS output and interface pins
245 return sp8870_writereg(state
, 0xC18, 0x00D);
248 static int sp8870_set_frontend_parameters(struct dvb_frontend
*fe
)
250 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
251 struct sp8870_state
* state
= fe
->demodulator_priv
;
255 if ((err
= configure_reg0xc05(p
, ®0xc05
)))
258 // system controller stop
259 sp8870_microcontroller_stop(state
);
261 // set tuner parameters
262 if (fe
->ops
.tuner_ops
.set_params
) {
263 fe
->ops
.tuner_ops
.set_params(fe
);
264 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
267 // sample rate correction bit [23..17]
268 sp8870_writereg(state
, 0x0319, 0x000A);
270 // sample rate correction bit [16..0]
271 sp8870_writereg(state
, 0x031A, 0x0AAB);
273 // integer carrier offset
274 sp8870_writereg(state
, 0x0309, 0x0400);
276 // fractional carrier offset
277 sp8870_writereg(state
, 0x030A, 0x0000);
279 // filter for 6/7/8 Mhz channel
280 if (p
->bandwidth_hz
== 6000000)
281 sp8870_writereg(state
, 0x0311, 0x0002);
282 else if (p
->bandwidth_hz
== 7000000)
283 sp8870_writereg(state
, 0x0311, 0x0001);
285 sp8870_writereg(state
, 0x0311, 0x0000);
287 // scan order: 2k first = 0x0000, 8k first = 0x0001
288 if (p
->transmission_mode
== TRANSMISSION_MODE_2K
)
289 sp8870_writereg(state
, 0x0338, 0x0000);
291 sp8870_writereg(state
, 0x0338, 0x0001);
293 sp8870_writereg(state
, 0xc05, reg0xc05
);
295 // read status reg in order to clear pending irqs
296 sp8870_readreg(state
, 0x200);
298 // system controller start
299 sp8870_microcontroller_start(state
);
304 static int sp8870_init (struct dvb_frontend
* fe
)
306 struct sp8870_state
* state
= fe
->demodulator_priv
;
307 const struct firmware
*fw
= NULL
;
309 sp8870_wake_up(state
);
310 if (state
->initialised
) return 0;
311 state
->initialised
= 1;
313 dprintk ("%s\n", __func__
);
316 /* request the firmware, this will block until someone uploads it */
317 printk("sp8870: waiting for firmware upload (%s)...\n", SP8870_DEFAULT_FIRMWARE
);
318 if (state
->config
->request_firmware(fe
, &fw
, SP8870_DEFAULT_FIRMWARE
)) {
319 printk("sp8870: no firmware upload (timeout or file not found?)\n");
323 if (sp8870_firmware_upload(state
, fw
)) {
324 printk("sp8870: writing firmware to device failed\n");
325 release_firmware(fw
);
328 release_firmware(fw
);
329 printk("sp8870: firmware upload complete\n");
331 /* enable TS output and interface pins */
332 sp8870_writereg(state
, 0xc18, 0x00d);
334 // system controller stop
335 sp8870_microcontroller_stop(state
);
338 sp8870_writereg(state
, 0x0301, 0x0003);
340 // Reed Solomon parity bytes passed to output
341 sp8870_writereg(state
, 0x0C13, 0x0001);
343 // MPEG clock is suppressed if no valid data
344 sp8870_writereg(state
, 0x0C14, 0x0001);
346 /* bit 0x010: enable data valid signal */
347 sp8870_writereg(state
, 0x0D00, 0x010);
348 sp8870_writereg(state
, 0x0D01, 0x000);
353 static int sp8870_read_status (struct dvb_frontend
* fe
, fe_status_t
* fe_status
)
355 struct sp8870_state
* state
= fe
->demodulator_priv
;
361 status
= sp8870_readreg (state
, 0x0200);
365 signal
= sp8870_readreg (state
, 0x0303);
370 *fe_status
|= FE_HAS_SIGNAL
;
372 *fe_status
|= FE_HAS_SYNC
;
374 *fe_status
|= FE_HAS_LOCK
| FE_HAS_CARRIER
| FE_HAS_VITERBI
;
379 static int sp8870_read_ber (struct dvb_frontend
* fe
, u32
* ber
)
381 struct sp8870_state
* state
= fe
->demodulator_priv
;
387 ret
= sp8870_readreg(state
, 0xC08);
393 ret
= sp8870_readreg(state
, 0xC07);
407 static int sp8870_read_signal_strength(struct dvb_frontend
* fe
, u16
* signal
)
409 struct sp8870_state
* state
= fe
->demodulator_priv
;
415 ret
= sp8870_readreg (state
, 0x306);
421 ret
= sp8870_readreg (state
, 0x303);
428 *signal
= 0xFFFF - tmp
;
433 static int sp8870_read_uncorrected_blocks (struct dvb_frontend
* fe
, u32
* ublocks
)
435 struct sp8870_state
* state
= fe
->demodulator_priv
;
440 ret
= sp8870_readreg(state
, 0xC0C);
452 /* number of trials to recover from lockup */
454 /* maximum checks for data valid signal */
455 #define MAXCHECKS 100
457 /* only for debugging: counter for detected lockups */
459 /* only for debugging: counter for channel switches */
462 static int sp8870_set_frontend(struct dvb_frontend
*fe
)
464 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
465 struct sp8870_state
* state
= fe
->demodulator_priv
;
468 The firmware of the sp8870 sometimes locks up after setting frontend parameters.
469 We try to detect this by checking the data valid signal.
470 If it is not set after MAXCHECKS we try to recover the lockup by setting
471 the frontend parameters again.
479 dprintk("%s: frequency = %i\n", __func__
, p
->frequency
);
481 for (trials
= 1; trials
<= MAXTRIALS
; trials
++) {
483 err
= sp8870_set_frontend_parameters(fe
);
487 for (check_count
= 0; check_count
< MAXCHECKS
; check_count
++) {
488 // valid = ((sp8870_readreg(i2c, 0x0200) & 4) == 0);
489 valid
= sp8870_read_data_valid_signal(state
);
491 dprintk("%s: delay = %i usec\n",
492 __func__
, check_count
* 10);
502 printk("%s: firmware crash!!!!!!\n", __func__
);
509 printk("%s: firmware lockup!!!\n", __func__
);
510 printk("%s: recovered after %i trial(s))\n", __func__
, trials
- 1);
515 printk("%s: switches = %i lockups = %i\n", __func__
, switches
, lockups
);
521 static int sp8870_sleep(struct dvb_frontend
* fe
)
523 struct sp8870_state
* state
= fe
->demodulator_priv
;
525 // tristate TS output and disable interface pins
526 return sp8870_writereg(state
, 0xC18, 0x000);
529 static int sp8870_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
* fesettings
)
531 fesettings
->min_delay_ms
= 350;
532 fesettings
->step_size
= 0;
533 fesettings
->max_drift
= 0;
537 static int sp8870_i2c_gate_ctrl(struct dvb_frontend
* fe
, int enable
)
539 struct sp8870_state
* state
= fe
->demodulator_priv
;
542 return sp8870_writereg(state
, 0x206, 0x001);
544 return sp8870_writereg(state
, 0x206, 0x000);
548 static void sp8870_release(struct dvb_frontend
* fe
)
550 struct sp8870_state
* state
= fe
->demodulator_priv
;
554 static struct dvb_frontend_ops sp8870_ops
;
556 struct dvb_frontend
* sp8870_attach(const struct sp8870_config
* config
,
557 struct i2c_adapter
* i2c
)
559 struct sp8870_state
* state
= NULL
;
561 /* allocate memory for the internal state */
562 state
= kzalloc(sizeof(struct sp8870_state
), GFP_KERNEL
);
563 if (state
== NULL
) goto error
;
565 /* setup the state */
566 state
->config
= config
;
568 state
->initialised
= 0;
570 /* check if the demod is there */
571 if (sp8870_readreg(state
, 0x0200) < 0) goto error
;
573 /* create dvb_frontend */
574 memcpy(&state
->frontend
.ops
, &sp8870_ops
, sizeof(struct dvb_frontend_ops
));
575 state
->frontend
.demodulator_priv
= state
;
576 return &state
->frontend
;
583 static struct dvb_frontend_ops sp8870_ops
= {
584 .delsys
= { SYS_DVBT
},
586 .name
= "Spase SP8870 DVB-T",
587 .frequency_min
= 470000000,
588 .frequency_max
= 860000000,
589 .frequency_stepsize
= 166666,
590 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
|
591 FE_CAN_FEC_3_4
| FE_CAN_FEC_5_6
|
592 FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
593 FE_CAN_QPSK
| FE_CAN_QAM_16
|
594 FE_CAN_QAM_64
| FE_CAN_QAM_AUTO
|
595 FE_CAN_HIERARCHY_AUTO
| FE_CAN_RECOVER
598 .release
= sp8870_release
,
601 .sleep
= sp8870_sleep
,
602 .i2c_gate_ctrl
= sp8870_i2c_gate_ctrl
,
604 .set_frontend
= sp8870_set_frontend
,
605 .get_tune_settings
= sp8870_get_tune_settings
,
607 .read_status
= sp8870_read_status
,
608 .read_ber
= sp8870_read_ber
,
609 .read_signal_strength
= sp8870_read_signal_strength
,
610 .read_ucblocks
= sp8870_read_uncorrected_blocks
,
613 module_param(debug
, int, 0644);
614 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
616 MODULE_DESCRIPTION("Spase SP8870 DVB-T Demodulator driver");
617 MODULE_AUTHOR("Juergen Peitz");
618 MODULE_LICENSE("GPL");
620 EXPORT_SYMBOL(sp8870_attach
);