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/moduleparam.h>
33 #include <linux/device.h>
34 #include <linux/firmware.h>
35 #include <linux/delay.h>
36 #include <linux/string.h>
37 #include <linux/slab.h>
39 #include "dvb_frontend.h"
45 struct i2c_adapter
* i2c
;
47 const struct sp8870_config
* config
;
49 struct dvb_frontend frontend
;
51 /* demodulator private data */
56 #define dprintk(args...) \
58 if (debug) printk(KERN_DEBUG "sp8870: " args); \
61 /* firmware size for sp8870 */
62 #define SP8870_FIRMWARE_SIZE 16382
64 /* starting point for firmware in file 'Sc_main.mc' */
65 #define SP8870_FIRMWARE_OFFSET 0x0A
67 static int sp8870_writereg (struct sp8870_state
* state
, u16 reg
, u16 data
)
69 u8 buf
[] = { reg
>> 8, reg
& 0xff, data
>> 8, data
& 0xff };
70 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= buf
, .len
= 4 };
73 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
74 dprintk ("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __FUNCTION__
, err
, reg
, data
);
81 static int sp8870_readreg (struct sp8870_state
* state
, u16 reg
)
84 u8 b0
[] = { reg
>> 8 , reg
& 0xff };
86 struct i2c_msg msg
[] = { { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= b0
, .len
= 2 },
87 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 2 } };
89 ret
= i2c_transfer (state
->i2c
, msg
, 2);
92 dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__
, ret
);
96 return (b1
[0] << 8 | b1
[1]);
99 static int sp8870_firmware_upload (struct sp8870_state
* state
, const struct firmware
*fw
)
102 char *fw_buf
= fw
->data
;
108 dprintk ("%s: ...\n", __FUNCTION__
);
110 if (fw
->size
< SP8870_FIRMWARE_SIZE
+ SP8870_FIRMWARE_OFFSET
)
113 // system controller stop
114 sp8870_writereg(state
, 0x0F00, 0x0000);
116 // instruction RAM register hiword
117 sp8870_writereg(state
, 0x8F08, ((SP8870_FIRMWARE_SIZE
/ 2) & 0xFFFF));
119 // instruction RAM MWR
120 sp8870_writereg(state
, 0x8F0A, ((SP8870_FIRMWARE_SIZE
/ 2) >> 16));
122 // do firmware upload
123 fw_pos
= SP8870_FIRMWARE_OFFSET
;
124 while (fw_pos
< SP8870_FIRMWARE_SIZE
+ SP8870_FIRMWARE_OFFSET
){
125 tx_len
= (fw_pos
<= SP8870_FIRMWARE_SIZE
+ SP8870_FIRMWARE_OFFSET
- 252) ? 252 : SP8870_FIRMWARE_SIZE
+ SP8870_FIRMWARE_OFFSET
- fw_pos
;
126 // write register 0xCF0A
129 memcpy(&tx_buf
[2], fw_buf
+ fw_pos
, tx_len
);
130 msg
.addr
= state
->config
->demod_address
;
133 msg
.len
= tx_len
+ 2;
134 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
135 printk("%s: firmware upload failed!\n", __FUNCTION__
);
136 printk ("%s: i2c error (err == %i)\n", __FUNCTION__
, err
);
142 dprintk ("%s: done!\n", __FUNCTION__
);
146 static void sp8870_microcontroller_stop (struct sp8870_state
* state
)
148 sp8870_writereg(state
, 0x0F08, 0x000);
149 sp8870_writereg(state
, 0x0F09, 0x000);
151 // microcontroller STOP
152 sp8870_writereg(state
, 0x0F00, 0x000);
155 static void sp8870_microcontroller_start (struct sp8870_state
* state
)
157 sp8870_writereg(state
, 0x0F08, 0x000);
158 sp8870_writereg(state
, 0x0F09, 0x000);
160 // microcontroller START
161 sp8870_writereg(state
, 0x0F00, 0x001);
162 // not documented but if we don't read 0x0D01 out here
163 // we don't get a correct data valid signal
164 sp8870_readreg(state
, 0x0D01);
167 static int sp8870_read_data_valid_signal(struct sp8870_state
* state
)
169 return (sp8870_readreg(state
, 0x0D02) > 0);
172 static int configure_reg0xc05 (struct dvb_frontend_parameters
*p
, u16
*reg0xc05
)
174 int known_parameters
= 1;
178 switch (p
->u
.ofdm
.constellation
) {
182 *reg0xc05
|= (1 << 10);
185 *reg0xc05
|= (2 << 10);
188 known_parameters
= 0;
194 switch (p
->u
.ofdm
.hierarchy_information
) {
198 *reg0xc05
|= (1 << 7);
201 *reg0xc05
|= (2 << 7);
204 *reg0xc05
|= (3 << 7);
207 known_parameters
= 0;
213 switch (p
->u
.ofdm
.code_rate_HP
) {
217 *reg0xc05
|= (1 << 3);
220 *reg0xc05
|= (2 << 3);
223 *reg0xc05
|= (3 << 3);
226 *reg0xc05
|= (4 << 3);
229 known_parameters
= 0;
235 if (known_parameters
)
236 *reg0xc05
|= (2 << 1); /* use specified parameters */
238 *reg0xc05
|= (1 << 1); /* enable autoprobing */
243 static int sp8870_wake_up(struct sp8870_state
* state
)
245 // enable TS output and interface pins
246 return sp8870_writereg(state
, 0xC18, 0x00D);
249 static int sp8870_set_frontend_parameters (struct dvb_frontend
* fe
,
250 struct dvb_frontend_parameters
*p
)
252 struct sp8870_state
* state
= fe
->demodulator_priv
;
256 if ((err
= configure_reg0xc05(p
, ®0xc05
)))
259 // system controller stop
260 sp8870_microcontroller_stop(state
);
262 // set tuner parameters
263 if (fe
->ops
.tuner_ops
.set_params
) {
264 fe
->ops
.tuner_ops
.set_params(fe
, p
);
265 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
268 // sample rate correction bit [23..17]
269 sp8870_writereg(state
, 0x0319, 0x000A);
271 // sample rate correction bit [16..0]
272 sp8870_writereg(state
, 0x031A, 0x0AAB);
274 // integer carrier offset
275 sp8870_writereg(state
, 0x0309, 0x0400);
277 // fractional carrier offset
278 sp8870_writereg(state
, 0x030A, 0x0000);
280 // filter for 6/7/8 Mhz channel
281 if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_6_MHZ
)
282 sp8870_writereg(state
, 0x0311, 0x0002);
283 else if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_7_MHZ
)
284 sp8870_writereg(state
, 0x0311, 0x0001);
286 sp8870_writereg(state
, 0x0311, 0x0000);
288 // scan order: 2k first = 0x0000, 8k first = 0x0001
289 if (p
->u
.ofdm
.transmission_mode
== TRANSMISSION_MODE_2K
)
290 sp8870_writereg(state
, 0x0338, 0x0000);
292 sp8870_writereg(state
, 0x0338, 0x0001);
294 sp8870_writereg(state
, 0xc05, reg0xc05
);
296 // read status reg in order to clear pending irqs
297 sp8870_readreg(state
, 0x200);
299 // system controller start
300 sp8870_microcontroller_start(state
);
305 static int sp8870_init (struct dvb_frontend
* fe
)
307 struct sp8870_state
* state
= fe
->demodulator_priv
;
308 const struct firmware
*fw
= NULL
;
310 sp8870_wake_up(state
);
311 if (state
->initialised
) return 0;
312 state
->initialised
= 1;
314 dprintk ("%s\n", __FUNCTION__
);
317 /* request the firmware, this will block until someone uploads it */
318 printk("sp8870: waiting for firmware upload (%s)...\n", SP8870_DEFAULT_FIRMWARE
);
319 if (state
->config
->request_firmware(fe
, &fw
, SP8870_DEFAULT_FIRMWARE
)) {
320 printk("sp8870: no firmware upload (timeout or file not found?)\n");
324 if (sp8870_firmware_upload(state
, fw
)) {
325 printk("sp8870: writing firmware to device failed\n");
326 release_firmware(fw
);
329 release_firmware(fw
);
330 printk("sp8870: firmware upload complete\n");
332 /* enable TS output and interface pins */
333 sp8870_writereg(state
, 0xc18, 0x00d);
335 // system controller stop
336 sp8870_microcontroller_stop(state
);
339 sp8870_writereg(state
, 0x0301, 0x0003);
341 // Reed Solomon parity bytes passed to output
342 sp8870_writereg(state
, 0x0C13, 0x0001);
344 // MPEG clock is suppressed if no valid data
345 sp8870_writereg(state
, 0x0C14, 0x0001);
347 /* bit 0x010: enable data valid signal */
348 sp8870_writereg(state
, 0x0D00, 0x010);
349 sp8870_writereg(state
, 0x0D01, 0x000);
354 static int sp8870_read_status (struct dvb_frontend
* fe
, fe_status_t
* fe_status
)
356 struct sp8870_state
* state
= fe
->demodulator_priv
;
362 status
= sp8870_readreg (state
, 0x0200);
366 signal
= sp8870_readreg (state
, 0x0303);
371 *fe_status
|= FE_HAS_SIGNAL
;
373 *fe_status
|= FE_HAS_SYNC
;
375 *fe_status
|= FE_HAS_LOCK
| FE_HAS_CARRIER
| FE_HAS_VITERBI
;
380 static int sp8870_read_ber (struct dvb_frontend
* fe
, u32
* ber
)
382 struct sp8870_state
* state
= fe
->demodulator_priv
;
388 ret
= sp8870_readreg(state
, 0xC08);
394 ret
= sp8870_readreg(state
, 0xC07);
408 static int sp8870_read_signal_strength(struct dvb_frontend
* fe
, u16
* signal
)
410 struct sp8870_state
* state
= fe
->demodulator_priv
;
416 ret
= sp8870_readreg (state
, 0x306);
422 ret
= sp8870_readreg (state
, 0x303);
429 *signal
= 0xFFFF - tmp
;
434 static int sp8870_read_uncorrected_blocks (struct dvb_frontend
* fe
, u32
* ublocks
)
436 struct sp8870_state
* state
= fe
->demodulator_priv
;
441 ret
= sp8870_readreg(state
, 0xC0C);
453 // number of trials to recover from lockup
455 // maximum checks for data valid signal
456 #define MAXCHECKS 100
458 // only for debugging: counter for detected lockups
459 static int lockups
= 0;
460 // only for debugging: counter for channel switches
461 static int switches
= 0;
463 static int sp8870_set_frontend (struct dvb_frontend
* fe
, struct dvb_frontend_parameters
*p
)
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", __FUNCTION__
, p
->frequency
);
481 for (trials
= 1; trials
<= MAXTRIALS
; trials
++) {
483 if ((err
= sp8870_set_frontend_parameters(fe
, p
)))
486 for (check_count
= 0; check_count
< MAXCHECKS
; check_count
++) {
487 // valid = ((sp8870_readreg(i2c, 0x0200) & 4) == 0);
488 valid
= sp8870_read_data_valid_signal(state
);
490 dprintk("%s: delay = %i usec\n",
491 __FUNCTION__
, check_count
* 10);
501 printk("%s: firmware crash!!!!!!\n", __FUNCTION__
);
508 printk("%s: firmware lockup!!!\n", __FUNCTION__
);
509 printk("%s: recovered after %i trial(s))\n", __FUNCTION__
, trials
- 1);
514 printk("%s: switches = %i lockups = %i\n", __FUNCTION__
, switches
, lockups
);
520 static int sp8870_sleep(struct dvb_frontend
* fe
)
522 struct sp8870_state
* state
= fe
->demodulator_priv
;
524 // tristate TS output and disable interface pins
525 return sp8870_writereg(state
, 0xC18, 0x000);
528 static int sp8870_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
* fesettings
)
530 fesettings
->min_delay_ms
= 350;
531 fesettings
->step_size
= 0;
532 fesettings
->max_drift
= 0;
536 static int sp8870_i2c_gate_ctrl(struct dvb_frontend
* fe
, int enable
)
538 struct sp8870_state
* state
= fe
->demodulator_priv
;
541 return sp8870_writereg(state
, 0x206, 0x001);
543 return sp8870_writereg(state
, 0x206, 0x000);
547 static void sp8870_release(struct dvb_frontend
* fe
)
549 struct sp8870_state
* state
= fe
->demodulator_priv
;
553 static struct dvb_frontend_ops sp8870_ops
;
555 struct dvb_frontend
* sp8870_attach(const struct sp8870_config
* config
,
556 struct i2c_adapter
* i2c
)
558 struct sp8870_state
* state
= NULL
;
560 /* allocate memory for the internal state */
561 state
= kmalloc(sizeof(struct sp8870_state
), GFP_KERNEL
);
562 if (state
== NULL
) goto error
;
564 /* setup the state */
565 state
->config
= config
;
567 state
->initialised
= 0;
569 /* check if the demod is there */
570 if (sp8870_readreg(state
, 0x0200) < 0) goto error
;
572 /* create dvb_frontend */
573 memcpy(&state
->frontend
.ops
, &sp8870_ops
, sizeof(struct dvb_frontend_ops
));
574 state
->frontend
.demodulator_priv
= state
;
575 return &state
->frontend
;
582 static struct dvb_frontend_ops sp8870_ops
= {
585 .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
);